xref: /titanic_44/usr/src/uts/common/io/conskbd.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Console kbd multiplexor driver for Sun.
31*7c478bd9Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "kbd" module pushed
32*7c478bd9Sstevel@tonic-gate  * on top of it.
33*7c478bd9Sstevel@tonic-gate  * Minor device 0 is what programs normally use.
34*7c478bd9Sstevel@tonic-gate  * Minor device 1 is used to feed predigested keystrokes to the "workstation
35*7c478bd9Sstevel@tonic-gate  * console" driver, which it is linked beneath.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  *     This module can support multiple keyboards to be used simultaneously.
39*7c478bd9Sstevel@tonic-gate  * and enable users to use at a time multiple keyboards connected to the
40*7c478bd9Sstevel@tonic-gate  * same system. All the keyboards are linked under conskbd, and act as a
41*7c478bd9Sstevel@tonic-gate  * keyboard with replicated keys.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
44*7c478bd9Sstevel@tonic-gate  * a two-level architecure. The lower one is one of serialport drivers, such
45*7c478bd9Sstevel@tonic-gate  * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
46*7c478bd9Sstevel@tonic-gate  * drivers don't support polled I/O interfaces, we couldn't group the keyboard
47*7c478bd9Sstevel@tonic-gate  * of this kind under conskbd. So we do as the follows:
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
50*7c478bd9Sstevel@tonic-gate  *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
51*7c478bd9Sstevel@tonic-gate  *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
52*7c478bd9Sstevel@tonic-gate  *     requesting to be linked under conskbd. If the lower driver does't
53*7c478bd9Sstevel@tonic-gate  *     recognize this ioctl, the virtual keyboard will be disabled so that
54*7c478bd9Sstevel@tonic-gate  *     only one keyboard instance could be linked under conskbd.
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/note.h>
73*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
74*7c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
75*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
76*7c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
77*7c478bd9Sstevel@tonic-gate #include <sys/kbtrans.h>
78*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
79*7c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
80*7c478bd9Sstevel@tonic-gate #include <sys/conskbd.h>
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate extern struct keyboard *kbtrans_usbkb_maptab_init(void);
83*7c478bd9Sstevel@tonic-gate extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
84*7c478bd9Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /*
87*7c478bd9Sstevel@tonic-gate  * Module linkage routines for the kernel
88*7c478bd9Sstevel@tonic-gate  */
89*7c478bd9Sstevel@tonic-gate static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
90*7c478bd9Sstevel@tonic-gate static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
91*7c478bd9Sstevel@tonic-gate static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  * STREAMS queue processing procedures
95*7c478bd9Sstevel@tonic-gate  */
96*7c478bd9Sstevel@tonic-gate static void	conskbduwsrv(queue_t *);
97*7c478bd9Sstevel@tonic-gate static void	conskbdlwserv(queue_t *);
98*7c478bd9Sstevel@tonic-gate static void	conskbdlrput(queue_t *, mblk_t *);
99*7c478bd9Sstevel@tonic-gate static void	conskbdioctl(queue_t *, mblk_t *);
100*7c478bd9Sstevel@tonic-gate static int	conskbdclose(queue_t *, int, cred_t *);
101*7c478bd9Sstevel@tonic-gate static int	conskbdopen(queue_t *, dev_t *, int, int, cred_t *);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /* STREAMS driver id and limit value struct */
105*7c478bd9Sstevel@tonic-gate static struct module_info conskbdm_info = {
106*7c478bd9Sstevel@tonic-gate 	0,		/* mi_idnum */
107*7c478bd9Sstevel@tonic-gate 	"conskbd",	/* mi_idname */
108*7c478bd9Sstevel@tonic-gate 	0,		/* mi_minpsz */
109*7c478bd9Sstevel@tonic-gate 	1024,		/* mi_maxpsz */
110*7c478bd9Sstevel@tonic-gate 	2048,		/* mi_hiwat */
111*7c478bd9Sstevel@tonic-gate 	128		/* mi_lowat */
112*7c478bd9Sstevel@tonic-gate };
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /*
115*7c478bd9Sstevel@tonic-gate  * STREAMS queue processing procedure structures
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate /* upper read queue processing procedure structures */
118*7c478bd9Sstevel@tonic-gate static struct qinit conskbdurinit = {
119*7c478bd9Sstevel@tonic-gate 	NULL,			/* qi_putp */
120*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_srvp */
121*7c478bd9Sstevel@tonic-gate 	conskbdopen,		/* qi_qopen */
122*7c478bd9Sstevel@tonic-gate 	conskbdclose,		/* qi_qclose */
123*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_qadmin */
124*7c478bd9Sstevel@tonic-gate 	&conskbdm_info,		/* qi_minfo */
125*7c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
126*7c478bd9Sstevel@tonic-gate };
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /* upper write queue processing procedures structuresi */
129*7c478bd9Sstevel@tonic-gate static struct qinit conskbduwinit = {
130*7c478bd9Sstevel@tonic-gate 	(int (*)())putq,		/* qi_putp */
131*7c478bd9Sstevel@tonic-gate 	(int (*)())conskbduwsrv,	/* qi_srvp */
132*7c478bd9Sstevel@tonic-gate 	conskbdopen,			/* qi_qopen */
133*7c478bd9Sstevel@tonic-gate 	conskbdclose,			/* qi_qclose */
134*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
135*7c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
136*7c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
137*7c478bd9Sstevel@tonic-gate };
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /* lower read queue processing procedures structures */
140*7c478bd9Sstevel@tonic-gate static struct qinit conskbdlrinit = {
141*7c478bd9Sstevel@tonic-gate 	(int (*)())conskbdlrput,	/* qi_putp */
142*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_srvp */
143*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
144*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
145*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
146*7c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
147*7c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
148*7c478bd9Sstevel@tonic-gate };
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate /* lower write processing procedures structures */
151*7c478bd9Sstevel@tonic-gate static struct qinit conskbdlwinit = {
152*7c478bd9Sstevel@tonic-gate 	putq,				/* qi_putp */
153*7c478bd9Sstevel@tonic-gate 	(int (*)())conskbdlwserv,	/* qi_srvp */
154*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
155*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
156*7c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
157*7c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
158*7c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
159*7c478bd9Sstevel@tonic-gate };
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /* STREAMS entity declaration structure */
162*7c478bd9Sstevel@tonic-gate static struct streamtab conskbd_str_info = {
163*7c478bd9Sstevel@tonic-gate 	&conskbdurinit,		/* st_rdinit */
164*7c478bd9Sstevel@tonic-gate 	&conskbduwinit,		/* st_wrinit */
165*7c478bd9Sstevel@tonic-gate 	&conskbdlrinit,		/* st_muxrinit */
166*7c478bd9Sstevel@tonic-gate 	&conskbdlwinit,		/* st_muxwinit */
167*7c478bd9Sstevel@tonic-gate };
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /* Entry points structure */
171*7c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_conskbd_ops = {
172*7c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_open */
173*7c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_close */
174*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
175*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
176*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
177*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
178*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
179*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
180*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
181*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
182*7c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
183*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
184*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
185*7c478bd9Sstevel@tonic-gate 	&conskbd_str_info,	/* cb_stream */
186*7c478bd9Sstevel@tonic-gate 	D_MP | D_MTOUTPERIM	/* cb_flag */
187*7c478bd9Sstevel@tonic-gate };
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate  * Device operations structure
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate static struct dev_ops conskbd_ops = {
194*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
195*7c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
196*7c478bd9Sstevel@tonic-gate 	conskbd_info,		/* devo_getinfo */
197*7c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
198*7c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
199*7c478bd9Sstevel@tonic-gate 	conskbd_attach,		/* devo_attach */
200*7c478bd9Sstevel@tonic-gate 	conskbd_detach,		/* devo_detach */
201*7c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
202*7c478bd9Sstevel@tonic-gate 	&(cb_conskbd_ops),	/* devo_cb_ops */
203*7c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
204*7c478bd9Sstevel@tonic-gate 	NULL			/* devo_power */
205*7c478bd9Sstevel@tonic-gate };
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
209*7c478bd9Sstevel@tonic-gate  */
210*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
211*7c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
212*7c478bd9Sstevel@tonic-gate 	"Console kbd Multiplexer driver 'conskbd' %I%",
213*7c478bd9Sstevel@tonic-gate 	&conskbd_ops,	/* driver ops */
214*7c478bd9Sstevel@tonic-gate };
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate /*
217*7c478bd9Sstevel@tonic-gate  * Module linkage structure
218*7c478bd9Sstevel@tonic-gate  */
219*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
220*7c478bd9Sstevel@tonic-gate 	MODREV_1,	/* ml_rev */
221*7c478bd9Sstevel@tonic-gate 	&modldrv,	/* ml_linkage */
222*7c478bd9Sstevel@tonic-gate 	NULL		/* NULL terminates the list */
223*7c478bd9Sstevel@tonic-gate };
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate /*
226*7c478bd9Sstevel@tonic-gate  * Debug printing
227*7c478bd9Sstevel@tonic-gate  */
228*7c478bd9Sstevel@tonic-gate #ifndef DPRINTF
229*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
230*7c478bd9Sstevel@tonic-gate void	conskbd_dprintf(const char *fmt, ...);
231*7c478bd9Sstevel@tonic-gate #define	DPRINTF(l, m, args) \
232*7c478bd9Sstevel@tonic-gate 	(((l) >= conskbd_errlevel) && ((m) & conskbd_errmask) ?	\
233*7c478bd9Sstevel@tonic-gate 		conskbd_dprintf args :				\
234*7c478bd9Sstevel@tonic-gate 		(void) 0)
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * Severity levels for printing
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate #define	PRINT_L0	0	/* print every message */
240*7c478bd9Sstevel@tonic-gate #define	PRINT_L1	1	/* debug */
241*7c478bd9Sstevel@tonic-gate #define	PRINT_L2	2	/* quiet */
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate /*
244*7c478bd9Sstevel@tonic-gate  * Masks
245*7c478bd9Sstevel@tonic-gate  */
246*7c478bd9Sstevel@tonic-gate #define	PRINT_MASK_ALL		0xFFFFFFFFU
247*7c478bd9Sstevel@tonic-gate uint_t	conskbd_errmask = PRINT_MASK_ALL;
248*7c478bd9Sstevel@tonic-gate uint_t	conskbd_errlevel = PRINT_L2;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate #else
251*7c478bd9Sstevel@tonic-gate #define	DPRINTF(l, m, args)	/* NOTHING */
252*7c478bd9Sstevel@tonic-gate #endif
253*7c478bd9Sstevel@tonic-gate #endif
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate /*
256*7c478bd9Sstevel@tonic-gate  * Module global data are protected by the per-module inner perimeter
257*7c478bd9Sstevel@tonic-gate  */
258*7c478bd9Sstevel@tonic-gate static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
259*7c478bd9Sstevel@tonic-gate static	queue_t	*conskbd_consqueue; /* console queue above us */
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
263*7c478bd9Sstevel@tonic-gate static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
264*7c478bd9Sstevel@tonic-gate static struct keyboard *conskbd_keyindex;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate /*
267*7c478bd9Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
268*7c478bd9Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
269*7c478bd9Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
270*7c478bd9Sstevel@tonic-gate  * will be easier to add new statistics later.
271*7c478bd9Sstevel@tonic-gate  */
272*7c478bd9Sstevel@tonic-gate static struct {
273*7c478bd9Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last keystroke */
274*7c478bd9Sstevel@tonic-gate } conskbd_kstat = {
275*7c478bd9Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
276*7c478bd9Sstevel@tonic-gate };
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * Local routines prototypes
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate static int conskbd_kstat_update(kstat_t *, int);
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate static void conskbd_ioc_plink(queue_t *, mblk_t *);
284*7c478bd9Sstevel@tonic-gate static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
285*7c478bd9Sstevel@tonic-gate static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
288*7c478bd9Sstevel@tonic-gate static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
289*7c478bd9Sstevel@tonic-gate static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
290*7c478bd9Sstevel@tonic-gate static void conskbd_link_lower_queue(conskbd_lower_queue_t *);
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
293*7c478bd9Sstevel@tonic-gate static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
294*7c478bd9Sstevel@tonic-gate static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
295*7c478bd9Sstevel@tonic-gate static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
296*7c478bd9Sstevel@tonic-gate static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
297*7c478bd9Sstevel@tonic-gate static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
298*7c478bd9Sstevel@tonic-gate static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
299*7c478bd9Sstevel@tonic-gate static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate static void conskbd_polledio_enter(struct cons_polledio_arg *);
302*7c478bd9Sstevel@tonic-gate static void conskbd_polledio_exit(struct cons_polledio_arg *);
303*7c478bd9Sstevel@tonic-gate static int  conskbd_polledio_ischar(struct cons_polledio_arg *);
304*7c478bd9Sstevel@tonic-gate static int  conskbd_polledio_getchar(struct cons_polledio_arg *);
305*7c478bd9Sstevel@tonic-gate static void conskbd_polledio_setled(struct kbtrans_hardware *, int);
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate static void conskbd_streams_setled(struct kbtrans_hardware *, int);
308*7c478bd9Sstevel@tonic-gate static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
309*7c478bd9Sstevel@tonic-gate static boolean_t
310*7c478bd9Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *,
311*7c478bd9Sstevel@tonic-gate 		kbtrans_key_t *, enum keystate *);
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate /*
314*7c478bd9Sstevel@tonic-gate  * Callbacks needed by kbtrans
315*7c478bd9Sstevel@tonic-gate  */
316*7c478bd9Sstevel@tonic-gate static struct kbtrans_callbacks conskbd_callbacks = {
317*7c478bd9Sstevel@tonic-gate 	conskbd_streams_setled,
318*7c478bd9Sstevel@tonic-gate 	conskbd_polledio_setled,
319*7c478bd9Sstevel@tonic-gate 	conskbd_polled_keycheck,
320*7c478bd9Sstevel@tonic-gate };
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate /*
323*7c478bd9Sstevel@tonic-gate  * Single private "global" lock for the few rare conditions
324*7c478bd9Sstevel@tonic-gate  * we want single-threaded.
325*7c478bd9Sstevel@tonic-gate  */
326*7c478bd9Sstevel@tonic-gate static	kmutex_t	conskbd_lq_lock;
327*7c478bd9Sstevel@tonic-gate static	kmutex_t	conskbd_msgq_lock;
328*7c478bd9Sstevel@tonic-gate static	conskbd_pending_msg_t	*conskbd_msg_queue;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate /*
331*7c478bd9Sstevel@tonic-gate  * The software state structure of virtual keyboard.
332*7c478bd9Sstevel@tonic-gate  * Currently, only one virtual keyboard is support.
333*7c478bd9Sstevel@tonic-gate  */
334*7c478bd9Sstevel@tonic-gate static conskbd_state_t	conskbd = { 0 };
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate /*
337*7c478bd9Sstevel@tonic-gate  * _init()
338*7c478bd9Sstevel@tonic-gate  *
339*7c478bd9Sstevel@tonic-gate  * Description:
340*7c478bd9Sstevel@tonic-gate  *      Driver initialization, called when driver is first loaded.
341*7c478bd9Sstevel@tonic-gate  *      This is how access is initially given to all the static structures.
342*7c478bd9Sstevel@tonic-gate  *
343*7c478bd9Sstevel@tonic-gate  * Arguments:
344*7c478bd9Sstevel@tonic-gate  *      None
345*7c478bd9Sstevel@tonic-gate  *
346*7c478bd9Sstevel@tonic-gate  * Returns:
347*7c478bd9Sstevel@tonic-gate  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
348*7c478bd9Sstevel@tonic-gate  *      mod_install() status, see mod_install(9f)
349*7c478bd9Sstevel@tonic-gate  */
350*7c478bd9Sstevel@tonic-gate int
351*7c478bd9Sstevel@tonic-gate _init(void)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	int	error;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
356*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
357*7c478bd9Sstevel@tonic-gate 		return (error);
358*7c478bd9Sstevel@tonic-gate 	}
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	conskbd_keyindex = kbtrans_usbkb_maptab_init();
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	mutex_init(&conskbd_lq_lock, NULL, MUTEX_DRIVER, NULL);
363*7c478bd9Sstevel@tonic-gate 	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	return (error);
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate }	/* _init() */
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate /*
370*7c478bd9Sstevel@tonic-gate  * _fini()
371*7c478bd9Sstevel@tonic-gate  *
372*7c478bd9Sstevel@tonic-gate  * Description:
373*7c478bd9Sstevel@tonic-gate  *      Module de-initialization, called when the driver is to be unloaded.
374*7c478bd9Sstevel@tonic-gate  *
375*7c478bd9Sstevel@tonic-gate  * Arguments:
376*7c478bd9Sstevel@tonic-gate  *      None
377*7c478bd9Sstevel@tonic-gate  *
378*7c478bd9Sstevel@tonic-gate  * Returns:
379*7c478bd9Sstevel@tonic-gate  *      mod_remove() status, see mod_remove(9f)
380*7c478bd9Sstevel@tonic-gate  */
381*7c478bd9Sstevel@tonic-gate int
382*7c478bd9Sstevel@tonic-gate _fini(void)
383*7c478bd9Sstevel@tonic-gate {
384*7c478bd9Sstevel@tonic-gate 	int	error;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
387*7c478bd9Sstevel@tonic-gate 	if (error != 0)
388*7c478bd9Sstevel@tonic-gate 		return (error);
389*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&conskbd_lq_lock);
390*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&conskbd_msgq_lock);
391*7c478bd9Sstevel@tonic-gate 	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	return (0);
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate }	/* _fini() */
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate /*
398*7c478bd9Sstevel@tonic-gate  * _info()
399*7c478bd9Sstevel@tonic-gate  *
400*7c478bd9Sstevel@tonic-gate  * Description:
401*7c478bd9Sstevel@tonic-gate  *      Module information, returns information about the driver.
402*7c478bd9Sstevel@tonic-gate  *
403*7c478bd9Sstevel@tonic-gate  * Arguments:
404*7c478bd9Sstevel@tonic-gate  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
405*7c478bd9Sstevel@tonic-gate  *
406*7c478bd9Sstevel@tonic-gate  * Returns:
407*7c478bd9Sstevel@tonic-gate  *      mod_info() status, see mod_info(9f)
408*7c478bd9Sstevel@tonic-gate  */
409*7c478bd9Sstevel@tonic-gate int
410*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
411*7c478bd9Sstevel@tonic-gate {
412*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate }	/* _info() */
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate /*
418*7c478bd9Sstevel@tonic-gate  * conskbd_attach()
419*7c478bd9Sstevel@tonic-gate  *
420*7c478bd9Sstevel@tonic-gate  * Description:
421*7c478bd9Sstevel@tonic-gate  * 	This routine creates two device nodes. One is the "kbd" node, which
422*7c478bd9Sstevel@tonic-gate  * is used by user application programs(such as Xserver).The other is the
423*7c478bd9Sstevel@tonic-gate  * "conskbd" node, which is an internal node. consconfig_dacf module will
424*7c478bd9Sstevel@tonic-gate  * open this internal node, and link the conskbd under the wc (workstaion
425*7c478bd9Sstevel@tonic-gate  * console).
426*7c478bd9Sstevel@tonic-gate  *
427*7c478bd9Sstevel@tonic-gate  * Arguments:
428*7c478bd9Sstevel@tonic-gate  *      dev_info_t      *dip    Pointer to the device's dev_info struct
429*7c478bd9Sstevel@tonic-gate  *      ddi_attach_cmd_t cmd    Attach command
430*7c478bd9Sstevel@tonic-gate  *
431*7c478bd9Sstevel@tonic-gate  * Returns:
432*7c478bd9Sstevel@tonic-gate  *      DDI_SUCCESS             The driver was initialized properly
433*7c478bd9Sstevel@tonic-gate  *      DDI_FAILURE             The driver couldn't be initialized properly
434*7c478bd9Sstevel@tonic-gate  */
435*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
436*7c478bd9Sstevel@tonic-gate static int
437*7c478bd9Sstevel@tonic-gate conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
438*7c478bd9Sstevel@tonic-gate {
439*7c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
442*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
443*7c478bd9Sstevel@tonic-gate 		break;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	default:
446*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	}
449*7c478bd9Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
450*7c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) ||
451*7c478bd9Sstevel@tonic-gate 	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
452*7c478bd9Sstevel@tonic-gate 	    1) == DDI_FAILURE)) {
453*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
454*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
455*7c478bd9Sstevel@tonic-gate 	}
456*7c478bd9Sstevel@tonic-gate 	conskbd_dip = devi;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
459*7c478bd9Sstevel@tonic-gate 	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
460*7c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
461*7c478bd9Sstevel@tonic-gate 	if (ksp) {
462*7c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &conskbd_kstat;
463*7c478bd9Sstevel@tonic-gate 		ksp->ks_update = conskbd_kstat_update;
464*7c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
465*7c478bd9Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_layout = -1;	/* invalid layout */
469*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_led_state = -1;
470*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_bypassed = B_FALSE;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate }	/* conskbd_attach() */
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate /*
477*7c478bd9Sstevel@tonic-gate  * conskbd_detach()
478*7c478bd9Sstevel@tonic-gate  *
479*7c478bd9Sstevel@tonic-gate  * Description:
480*7c478bd9Sstevel@tonic-gate  *      Detach an instance of the conskbd driver. In fact, the driver can not
481*7c478bd9Sstevel@tonic-gate  * be detached.
482*7c478bd9Sstevel@tonic-gate  *
483*7c478bd9Sstevel@tonic-gate  * Arguments:
484*7c478bd9Sstevel@tonic-gate  *      dev_info_t              *dip    Pointer to the device's dev_info struct
485*7c478bd9Sstevel@tonic-gate  *      ddi_detach_cmd_t        cmd     Detach command
486*7c478bd9Sstevel@tonic-gate  *
487*7c478bd9Sstevel@tonic-gate  * Returns:
488*7c478bd9Sstevel@tonic-gate  *      DDI_SUCCESS     The driver was detached
489*7c478bd9Sstevel@tonic-gate  *      DDI_FAILURE     The driver couldn't be detached
490*7c478bd9Sstevel@tonic-gate  */
491*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
492*7c478bd9Sstevel@tonic-gate static int
493*7c478bd9Sstevel@tonic-gate conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
494*7c478bd9Sstevel@tonic-gate {
495*7c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate }	/* conskbd_detach() */
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
500*7c478bd9Sstevel@tonic-gate static int
501*7c478bd9Sstevel@tonic-gate conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
502*7c478bd9Sstevel@tonic-gate 	void **result)
503*7c478bd9Sstevel@tonic-gate {
504*7c478bd9Sstevel@tonic-gate 	register int error;
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
507*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
508*7c478bd9Sstevel@tonic-gate 		if (conskbd_dip == NULL) {
509*7c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
510*7c478bd9Sstevel@tonic-gate 		} else {
511*7c478bd9Sstevel@tonic-gate 			*result = (void *) conskbd_dip;
512*7c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
513*7c478bd9Sstevel@tonic-gate 		}
514*7c478bd9Sstevel@tonic-gate 		break;
515*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
516*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
517*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
518*7c478bd9Sstevel@tonic-gate 		break;
519*7c478bd9Sstevel@tonic-gate 	default:
520*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 	return (error);
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate }	/* conskbd_info() */
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
527*7c478bd9Sstevel@tonic-gate static int
528*7c478bd9Sstevel@tonic-gate conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
529*7c478bd9Sstevel@tonic-gate {
530*7c478bd9Sstevel@tonic-gate 	dev_t	unit;
531*7c478bd9Sstevel@tonic-gate 	int	err;
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	unit = getminor(*devp);
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	if (unit == 0) {
536*7c478bd9Sstevel@tonic-gate 		/*
537*7c478bd9Sstevel@tonic-gate 		 * Opening "/dev/kbd".
538*7c478bd9Sstevel@tonic-gate 		 */
539*7c478bd9Sstevel@tonic-gate 		conskbd_regqueue = q;
540*7c478bd9Sstevel@tonic-gate 		qprocson(q);
541*7c478bd9Sstevel@tonic-gate 		return (0);
542*7c478bd9Sstevel@tonic-gate 	} else if (unit != 1) {
543*7c478bd9Sstevel@tonic-gate 		/* we don't do that under Bozo's Big Tent */
544*7c478bd9Sstevel@tonic-gate 		return (ENODEV);
545*7c478bd9Sstevel@tonic-gate 	}
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	/*
548*7c478bd9Sstevel@tonic-gate 	 * Opening the device to be linked under the console.
549*7c478bd9Sstevel@tonic-gate 	 */
550*7c478bd9Sstevel@tonic-gate 	conskbd_consqueue = q;
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	/*
553*7c478bd9Sstevel@tonic-gate 	 * initialzie kbtrans module for conskbd
554*7c478bd9Sstevel@tonic-gate 	 */
555*7c478bd9Sstevel@tonic-gate 	err = kbtrans_streams_init(q, sflag, crp, (struct kbtrans_hardware *)
556*7c478bd9Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
557*7c478bd9Sstevel@tonic-gate 	if (err != 0)
558*7c478bd9Sstevel@tonic-gate 		return (err);
559*7c478bd9Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
560*7c478bd9Sstevel@tonic-gate 	    conskbd_keyindex);
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
563*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
564*7c478bd9Sstevel@tonic-gate 	    (struct cons_polledio_arg *)&conskbd;
565*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
566*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
567*7c478bd9Sstevel@tonic-gate 	    (int (*)(struct cons_polledio_arg *)) conskbd_polledio_getchar;
568*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
569*7c478bd9Sstevel@tonic-gate 	    (boolean_t (*)(struct cons_polledio_arg *))conskbd_polledio_ischar;
570*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
571*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
572*7c478bd9Sstevel@tonic-gate 	qprocson(q);
573*7c478bd9Sstevel@tonic-gate 	kbtrans_streams_enable(conskbd.conskbd_kbtrans);
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate 	return (0);
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate }	/* conskbd_open() */
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
581*7c478bd9Sstevel@tonic-gate static int
582*7c478bd9Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
583*7c478bd9Sstevel@tonic-gate {
584*7c478bd9Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
587*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
588*7c478bd9Sstevel@tonic-gate 
589*7c478bd9Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
590*7c478bd9Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
591*7c478bd9Sstevel@tonic-gate 		    conskbd_consqueue);
592*7c478bd9Sstevel@tonic-gate 		qprocsoff(q);
593*7c478bd9Sstevel@tonic-gate 		conskbd_regqueue = NULL;
594*7c478bd9Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
595*7c478bd9Sstevel@tonic-gate 		/*
596*7c478bd9Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
597*7c478bd9Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
598*7c478bd9Sstevel@tonic-gate 		 */
599*7c478bd9Sstevel@tonic-gate 		qprocsoff(q);
600*7c478bd9Sstevel@tonic-gate 		conskbd_consqueue = NULL;
601*7c478bd9Sstevel@tonic-gate 	}
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 	return (0);
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate }	/* conskbd_close() */
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate /*
608*7c478bd9Sstevel@tonic-gate  * Service procedure for upper write queue.
609*7c478bd9Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
610*7c478bd9Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
611*7c478bd9Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
612*7c478bd9Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
613*7c478bd9Sstevel@tonic-gate  * deal with all messages.
614*7c478bd9Sstevel@tonic-gate  * 	This routine is invoked when ioctl commands are send down
615*7c478bd9Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
616*7c478bd9Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
617*7c478bd9Sstevel@tonic-gate  * the led states.
618*7c478bd9Sstevel@tonic-gate  */
619*7c478bd9Sstevel@tonic-gate static void
620*7c478bd9Sstevel@tonic-gate conskbduwsrv(queue_t *q)
621*7c478bd9Sstevel@tonic-gate {
622*7c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
623*7c478bd9Sstevel@tonic-gate 	queue_t	*oldq;
624*7c478bd9Sstevel@tonic-gate 	enum kbtrans_message_response ret;
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 		/*
629*7c478bd9Sstevel@tonic-gate 		 * if the virtual keyboard is supported
630*7c478bd9Sstevel@tonic-gate 		 */
631*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
634*7c478bd9Sstevel@tonic-gate 				continue;
635*7c478bd9Sstevel@tonic-gate 			/*
636*7c478bd9Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
637*7c478bd9Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
638*7c478bd9Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
639*7c478bd9Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
640*7c478bd9Sstevel@tonic-gate 			 * and regular queue.
641*7c478bd9Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
642*7c478bd9Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
643*7c478bd9Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
644*7c478bd9Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
645*7c478bd9Sstevel@tonic-gate 			 * to be switched between regular queue and console
646*7c478bd9Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
647*7c478bd9Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
648*7c478bd9Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
649*7c478bd9Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
650*7c478bd9Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
651*7c478bd9Sstevel@tonic-gate 			 * need to return response to right queue.
652*7c478bd9Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
653*7c478bd9Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
654*7c478bd9Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
655*7c478bd9Sstevel@tonic-gate 			 * and then switch back to old queue.
656*7c478bd9Sstevel@tonic-gate 			 */
657*7c478bd9Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
658*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
659*7c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
660*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
661*7c478bd9Sstevel@tonic-gate 			ret = kbtrans_streams_message(
662*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
663*7c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
664*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 			switch (ret) {
667*7c478bd9Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
668*7c478bd9Sstevel@tonic-gate 					continue;
669*7c478bd9Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
670*7c478bd9Sstevel@tonic-gate 					break;
671*7c478bd9Sstevel@tonic-gate 			}
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		case M_IOCTL:
677*7c478bd9Sstevel@tonic-gate 			conskbdioctl(q, mp);
678*7c478bd9Sstevel@tonic-gate 			break;
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 		case M_FLUSH:
681*7c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
682*7c478bd9Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
683*7c478bd9Sstevel@tonic-gate 			}
684*7c478bd9Sstevel@tonic-gate 			/*
685*7c478bd9Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
686*7c478bd9Sstevel@tonic-gate 			 * may be lost so that upper module or applications
687*7c478bd9Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
688*7c478bd9Sstevel@tonic-gate 			 * ever.
689*7c478bd9Sstevel@tonic-gate 			 */
690*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
691*7c478bd9Sstevel@tonic-gate 			break;
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 		case M_DATA:
694*7c478bd9Sstevel@tonic-gate 			/*
695*7c478bd9Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
696*7c478bd9Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
697*7c478bd9Sstevel@tonic-gate 			 * down to lower queue.
698*7c478bd9Sstevel@tonic-gate 			 */
699*7c478bd9Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
700*7c478bd9Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
701*7c478bd9Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
702*7c478bd9Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
703*7c478bd9Sstevel@tonic-gate 					freemsg(mp);
704*7c478bd9Sstevel@tonic-gate 			} else {
705*7c478bd9Sstevel@tonic-gate 				freemsg(mp);
706*7c478bd9Sstevel@tonic-gate 			}
707*7c478bd9Sstevel@tonic-gate 			break;
708*7c478bd9Sstevel@tonic-gate 
709*7c478bd9Sstevel@tonic-gate 		default:
710*7c478bd9Sstevel@tonic-gate 			/*
711*7c478bd9Sstevel@tonic-gate 			 * Pass an error message up.
712*7c478bd9Sstevel@tonic-gate 			 */
713*7c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
714*7c478bd9Sstevel@tonic-gate 			if (mp->b_cont) {
715*7c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
716*7c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
717*7c478bd9Sstevel@tonic-gate 			}
718*7c478bd9Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
719*7c478bd9Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
720*7c478bd9Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
721*7c478bd9Sstevel@tonic-gate 			qreply(q, mp);
722*7c478bd9Sstevel@tonic-gate 		}
723*7c478bd9Sstevel@tonic-gate 	}	/* end of while */
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate }	/* conskbduwsrv() */
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate static void
728*7c478bd9Sstevel@tonic-gate conskbdioctl(queue_t *q, mblk_t *mp)
729*7c478bd9Sstevel@tonic-gate {
730*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t		*prev;
731*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t		*lqs;
732*7c478bd9Sstevel@tonic-gate 	struct	iocblk			*iocp;
733*7c478bd9Sstevel@tonic-gate 	struct	linkblk			*linkp;
734*7c478bd9Sstevel@tonic-gate 	int	index;
735*7c478bd9Sstevel@tonic-gate 	int	error = 0;
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 	case I_LINK:
742*7c478bd9Sstevel@tonic-gate 	case I_PLINK:
743*7c478bd9Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
744*7c478bd9Sstevel@tonic-gate 		conskbd_ioc_plink(q, mp);
745*7c478bd9Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
746*7c478bd9Sstevel@tonic-gate 		break;
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 	case I_UNLINK:
749*7c478bd9Sstevel@tonic-gate 	case I_PUNLINK:
750*7c478bd9Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
751*7c478bd9Sstevel@tonic-gate 		linkp = (struct linkblk *)mp->b_cont->b_rptr;
752*7c478bd9Sstevel@tonic-gate 		prev = conskbd.conskbd_lqueue_list;
753*7c478bd9Sstevel@tonic-gate 		for (lqs = prev; lqs; lqs = lqs->lqs_next) {
754*7c478bd9Sstevel@tonic-gate 			if (lqs->lqs_queue == linkp->l_qbot) {
755*7c478bd9Sstevel@tonic-gate 				if (prev == lqs)
756*7c478bd9Sstevel@tonic-gate 					conskbd.conskbd_lqueue_list =
757*7c478bd9Sstevel@tonic-gate 					    lqs->lqs_next;
758*7c478bd9Sstevel@tonic-gate 				else
759*7c478bd9Sstevel@tonic-gate 					prev->lqs_next = lqs->lqs_next;
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate 				lqs->lqs_queue->q_ptr =  NULL;
762*7c478bd9Sstevel@tonic-gate 				conskbd.conskbd_lqueue_nums --;
763*7c478bd9Sstevel@tonic-gate 				if (conskbd.conskbd_lqueue_nums == 0)
764*7c478bd9Sstevel@tonic-gate 					conskbd.conskbd_layout = -1;
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate 				mutex_exit(&conskbd_lq_lock);
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 				for (index = 0; index < KBTRANS_KEYNUMS_MAX;
769*7c478bd9Sstevel@tonic-gate 				    index ++) {
770*7c478bd9Sstevel@tonic-gate 					if (lqs->lqs_key_state[index] ==
771*7c478bd9Sstevel@tonic-gate 					    KEY_PRESSED)
772*7c478bd9Sstevel@tonic-gate 						kbtrans_streams_key(
773*7c478bd9Sstevel@tonic-gate 						    conskbd.conskbd_kbtrans,
774*7c478bd9Sstevel@tonic-gate 						    index,
775*7c478bd9Sstevel@tonic-gate 						    KEY_RELEASED);
776*7c478bd9Sstevel@tonic-gate 				}
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate 				kmem_free(lqs, sizeof (*lqs));
779*7c478bd9Sstevel@tonic-gate 				miocack(q, mp, 0, 0);
780*7c478bd9Sstevel@tonic-gate 				return;
781*7c478bd9Sstevel@tonic-gate 			}
782*7c478bd9Sstevel@tonic-gate 			prev = lqs;
783*7c478bd9Sstevel@tonic-gate 		}
784*7c478bd9Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
785*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
786*7c478bd9Sstevel@tonic-gate 		break;
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	case KIOCSKABORTEN:
789*7c478bd9Sstevel@tonic-gate 		/*
790*7c478bd9Sstevel@tonic-gate 		 * Check if privileged
791*7c478bd9Sstevel@tonic-gate 		 */
792*7c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
793*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
794*7c478bd9Sstevel@tonic-gate 			return;
795*7c478bd9Sstevel@tonic-gate 		}
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
798*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
799*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
800*7c478bd9Sstevel@tonic-gate 			return;
801*7c478bd9Sstevel@tonic-gate 		}
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
804*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
805*7c478bd9Sstevel@tonic-gate 		break;
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 	default:
808*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
809*7c478bd9Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
810*7c478bd9Sstevel@tonic-gate 		} else {
811*7c478bd9Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
812*7c478bd9Sstevel@tonic-gate 		}
813*7c478bd9Sstevel@tonic-gate 	}
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate }	/* conskbdioctl() */
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate static void
819*7c478bd9Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
820*7c478bd9Sstevel@tonic-gate {
821*7c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
822*7c478bd9Sstevel@tonic-gate 	mblk_t			*datap;
823*7c478bd9Sstevel@tonic-gate 	int			cmd;
824*7c478bd9Sstevel@tonic-gate 	int			error = 0;
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
829*7c478bd9Sstevel@tonic-gate 	case KIOCLAYOUT:
830*7c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
831*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
832*7c478bd9Sstevel@tonic-gate 			break;
833*7c478bd9Sstevel@tonic-gate 		}
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
836*7c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
837*7c478bd9Sstevel@tonic-gate 		else
838*7c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
841*7c478bd9Sstevel@tonic-gate 		if (mp->b_cont)
842*7c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
843*7c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
844*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
845*7c478bd9Sstevel@tonic-gate 		break;
846*7c478bd9Sstevel@tonic-gate 
847*7c478bd9Sstevel@tonic-gate 	case KIOCSLAYOUT:
848*7c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
849*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
850*7c478bd9Sstevel@tonic-gate 			break;
851*7c478bd9Sstevel@tonic-gate 		}
852*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
853*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
854*7c478bd9Sstevel@tonic-gate 		break;
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
857*7c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
858*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
859*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
860*7c478bd9Sstevel@tonic-gate 			break;
861*7c478bd9Sstevel@tonic-gate 		}
862*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
863*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
864*7c478bd9Sstevel@tonic-gate 			break;
865*7c478bd9Sstevel@tonic-gate 		}
866*7c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
867*7c478bd9Sstevel@tonic-gate 		break;
868*7c478bd9Sstevel@tonic-gate 
869*7c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
870*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
871*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
872*7c478bd9Sstevel@tonic-gate 			break;
873*7c478bd9Sstevel@tonic-gate 		}
874*7c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
875*7c478bd9Sstevel@tonic-gate 		break;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
878*7c478bd9Sstevel@tonic-gate 		/*
879*7c478bd9Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
880*7c478bd9Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
881*7c478bd9Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
882*7c478bd9Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
883*7c478bd9Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
884*7c478bd9Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
885*7c478bd9Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
886*7c478bd9Sstevel@tonic-gate 		 *
887*7c478bd9Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
888*7c478bd9Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
889*7c478bd9Sstevel@tonic-gate 		 * already handle the message though it returned to us a
890*7c478bd9Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
891*7c478bd9Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
892*7c478bd9Sstevel@tonic-gate 		 * So we just return ACK to upper module.
893*7c478bd9Sstevel@tonic-gate 		 */
894*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
895*7c478bd9Sstevel@tonic-gate 		break;
896*7c478bd9Sstevel@tonic-gate 
897*7c478bd9Sstevel@tonic-gate 	case KIOCCMD:
898*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
899*7c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL) {
900*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
901*7c478bd9Sstevel@tonic-gate 			break;
902*7c478bd9Sstevel@tonic-gate 		}
903*7c478bd9Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
904*7c478bd9Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
905*7c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
906*7c478bd9Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
907*7c478bd9Sstevel@tonic-gate 			if (datap == NULL) {
908*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
909*7c478bd9Sstevel@tonic-gate 				return;
910*7c478bd9Sstevel@tonic-gate 			}
911*7c478bd9Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
912*7c478bd9Sstevel@tonic-gate 				*(int *)datap->b_wptr =
913*7c478bd9Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
914*7c478bd9Sstevel@tonic-gate 			else
915*7c478bd9Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate 			mp->b_cont = datap;
918*7c478bd9Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
919*7c478bd9Sstevel@tonic-gate 			return;
920*7c478bd9Sstevel@tonic-gate 		}
921*7c478bd9Sstevel@tonic-gate 
922*7c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
923*7c478bd9Sstevel@tonic-gate 		break;
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate 	default:
926*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
927*7c478bd9Sstevel@tonic-gate 		break;
928*7c478bd9Sstevel@tonic-gate 	}
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate static void
933*7c478bd9Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
934*7c478bd9Sstevel@tonic-gate {
935*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
936*7c478bd9Sstevel@tonic-gate 	struct	iocblk		*iocp;
937*7c478bd9Sstevel@tonic-gate 	int	error = 0;
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
942*7c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT: {
945*7c478bd9Sstevel@tonic-gate 		mblk_t *datap;
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
948*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
949*7c478bd9Sstevel@tonic-gate 			break;
950*7c478bd9Sstevel@tonic-gate 		}
951*7c478bd9Sstevel@tonic-gate 
952*7c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
953*7c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
954*7c478bd9Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
955*7c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
956*7c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
957*7c478bd9Sstevel@tonic-gate 		}
958*7c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
959*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
960*7c478bd9Sstevel@tonic-gate 		break;
961*7c478bd9Sstevel@tonic-gate 	}
962*7c478bd9Sstevel@tonic-gate 
963*7c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
964*7c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
965*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
966*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
967*7c478bd9Sstevel@tonic-gate 			break;
968*7c478bd9Sstevel@tonic-gate 		}
969*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate 		/*
972*7c478bd9Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
973*7c478bd9Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
974*7c478bd9Sstevel@tonic-gate 		 * itself.
975*7c478bd9Sstevel@tonic-gate 		 */
976*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
977*7c478bd9Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
978*7c478bd9Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
979*7c478bd9Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
980*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
981*7c478bd9Sstevel@tonic-gate 				return;
982*7c478bd9Sstevel@tonic-gate 			}
983*7c478bd9Sstevel@tonic-gate 			break;
984*7c478bd9Sstevel@tonic-gate 		}
985*7c478bd9Sstevel@tonic-gate 
986*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
987*7c478bd9Sstevel@tonic-gate 		break;
988*7c478bd9Sstevel@tonic-gate 
989*7c478bd9Sstevel@tonic-gate 	default:
990*7c478bd9Sstevel@tonic-gate 		/*
991*7c478bd9Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
992*7c478bd9Sstevel@tonic-gate 		 * through to; otherwise, reject it.
993*7c478bd9Sstevel@tonic-gate 		 */
994*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
995*7c478bd9Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
996*7c478bd9Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
997*7c478bd9Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
998*7c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
999*7c478bd9Sstevel@tonic-gate 				return;
1000*7c478bd9Sstevel@tonic-gate 			}
1001*7c478bd9Sstevel@tonic-gate 			break;
1002*7c478bd9Sstevel@tonic-gate 		}
1003*7c478bd9Sstevel@tonic-gate 
1004*7c478bd9Sstevel@tonic-gate 		/* nobody below us; reject it */
1005*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
1006*7c478bd9Sstevel@tonic-gate 		break;
1007*7c478bd9Sstevel@tonic-gate 	}
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
1010*7c478bd9Sstevel@tonic-gate 
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate /*
1013*7c478bd9Sstevel@tonic-gate  * Service procedure for lower write queue.
1014*7c478bd9Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
1015*7c478bd9Sstevel@tonic-gate  */
1016*7c478bd9Sstevel@tonic-gate static void
1017*7c478bd9Sstevel@tonic-gate conskbdlwserv(queue_t *q)
1018*7c478bd9Sstevel@tonic-gate {
1019*7c478bd9Sstevel@tonic-gate 	register mblk_t *mp;
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
1022*7c478bd9Sstevel@tonic-gate 		putnext(q, mp);
1023*7c478bd9Sstevel@tonic-gate 
1024*7c478bd9Sstevel@tonic-gate }	/* conskbdlwserv() */
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate /*
1027*7c478bd9Sstevel@tonic-gate  * Put procedure for lower read queue.
1028*7c478bd9Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
1029*7c478bd9Sstevel@tonic-gate  * device 1.
1030*7c478bd9Sstevel@tonic-gate  */
1031*7c478bd9Sstevel@tonic-gate static void
1032*7c478bd9Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
1033*7c478bd9Sstevel@tonic-gate {
1034*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
1035*7c478bd9Sstevel@tonic-gate 	struct iocblk 	*iocp;
1036*7c478bd9Sstevel@tonic-gate 	Firm_event	*fe;
1037*7c478bd9Sstevel@tonic-gate 
1038*7c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
1039*7c478bd9Sstevel@tonic-gate 
1040*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate 	case M_FLUSH:
1043*7c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
1044*7c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
1045*7c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
1046*7c478bd9Sstevel@tonic-gate 		}
1047*7c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
1048*7c478bd9Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
1049*7c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
1050*7c478bd9Sstevel@tonic-gate 		} else
1051*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1052*7c478bd9Sstevel@tonic-gate 		break;
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate 	case M_DATA:
1055*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
1056*7c478bd9Sstevel@tonic-gate 
1057*7c478bd9Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
1058*7c478bd9Sstevel@tonic-gate 
1059*7c478bd9Sstevel@tonic-gate 			/*
1060*7c478bd9Sstevel@tonic-gate 			 * This is a workaround.
1061*7c478bd9Sstevel@tonic-gate 			 *
1062*7c478bd9Sstevel@tonic-gate 			 * According to HID specification, there are the
1063*7c478bd9Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
1064*7c478bd9Sstevel@tonic-gate 			 *
1065*7c478bd9Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
1066*7c478bd9Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
1067*7c478bd9Sstevel@tonic-gate 			 *
1068*7c478bd9Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
1069*7c478bd9Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
1070*7c478bd9Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
1071*7c478bd9Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
1072*7c478bd9Sstevel@tonic-gate 			 * not know which is being pressed or released when we
1073*7c478bd9Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
1074*7c478bd9Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
1075*7c478bd9Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
1076*7c478bd9Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
1077*7c478bd9Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
1078*7c478bd9Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
1079*7c478bd9Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
1080*7c478bd9Sstevel@tonic-gate 			 * the key to 49.
1081*7c478bd9Sstevel@tonic-gate 			 */
1082*7c478bd9Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
1083*7c478bd9Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
1084*7c478bd9Sstevel@tonic-gate 					fe->id = 49;
1085*7c478bd9Sstevel@tonic-gate 				else
1086*7c478bd9Sstevel@tonic-gate 					fe->id = 50;
1087*7c478bd9Sstevel@tonic-gate 			}
1088*7c478bd9Sstevel@tonic-gate 
1089*7c478bd9Sstevel@tonic-gate 			/*
1090*7c478bd9Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
1091*7c478bd9Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
1092*7c478bd9Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
1093*7c478bd9Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
1094*7c478bd9Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
1095*7c478bd9Sstevel@tonic-gate 			 * as held-down for ever.
1096*7c478bd9Sstevel@tonic-gate 			 */
1097*7c478bd9Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
1098*7c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
1099*7c478bd9Sstevel@tonic-gate 			if (fe->value)
1100*7c478bd9Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
1101*7c478bd9Sstevel@tonic-gate 			else
1102*7c478bd9Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
1103*7c478bd9Sstevel@tonic-gate 
1104*7c478bd9Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
1105*7c478bd9Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
1106*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1107*7c478bd9Sstevel@tonic-gate 		} else {
1108*7c478bd9Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
1109*7c478bd9Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
1110*7c478bd9Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
1111*7c478bd9Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
1112*7c478bd9Sstevel@tonic-gate 			else
1113*7c478bd9Sstevel@tonic-gate 				freemsg(mp);
1114*7c478bd9Sstevel@tonic-gate 		}
1115*7c478bd9Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
1116*7c478bd9Sstevel@tonic-gate 		break;
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate 	case M_IOCACK:
1119*7c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
1120*7c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
1121*7c478bd9Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
1122*7c478bd9Sstevel@tonic-gate 
1123*7c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
1124*7c478bd9Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
1127*7c478bd9Sstevel@tonic-gate 		break;
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate 	case M_ERROR:
1130*7c478bd9Sstevel@tonic-gate 	case M_HANGUP:
1131*7c478bd9Sstevel@tonic-gate 	default:
1132*7c478bd9Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
1133*7c478bd9Sstevel@tonic-gate 		break;
1134*7c478bd9Sstevel@tonic-gate 	}
1135*7c478bd9Sstevel@tonic-gate 
1136*7c478bd9Sstevel@tonic-gate }	/* conskbdlrput() */
1137*7c478bd9Sstevel@tonic-gate 
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1140*7c478bd9Sstevel@tonic-gate static int
1141*7c478bd9Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
1142*7c478bd9Sstevel@tonic-gate {
1143*7c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
1144*7c478bd9Sstevel@tonic-gate 		return (EACCES);
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 	return (0);
1149*7c478bd9Sstevel@tonic-gate 
1150*7c478bd9Sstevel@tonic-gate }	/* conskbd_kstat_update() */
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate /*
1153*7c478bd9Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
1154*7c478bd9Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
1155*7c478bd9Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
1156*7c478bd9Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
1157*7c478bd9Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
1158*7c478bd9Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
1159*7c478bd9Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
1160*7c478bd9Sstevel@tonic-gate  * request matching received responses by searching the global linked
1161*7c478bd9Sstevel@tonic-gate  * list to find the request which has the same message ID of the
1162*7c478bd9Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
1163*7c478bd9Sstevel@tonic-gate  * give a response to upstreams based the following policy:
1164*7c478bd9Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
1165*7c478bd9Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
1166*7c478bd9Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
1167*7c478bd9Sstevel@tonic-gate  * the first response is sent to upstream.
1168*7c478bd9Sstevel@tonic-gate  */
1169*7c478bd9Sstevel@tonic-gate static void
1170*7c478bd9Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
1171*7c478bd9Sstevel@tonic-gate {
1172*7c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
1173*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
1174*7c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
1175*7c478bd9Sstevel@tonic-gate 	mblk_t		*clonemp;
1176*7c478bd9Sstevel@tonic-gate 	int		retry;
1177*7c478bd9Sstevel@tonic-gate 
1178*7c478bd9Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
1179*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
1180*7c478bd9Sstevel@tonic-gate 		return;
1181*7c478bd9Sstevel@tonic-gate 	}
1182*7c478bd9Sstevel@tonic-gate 
1183*7c478bd9Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
1184*7c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
1185*7c478bd9Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
1186*7c478bd9Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
1187*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
1190*7c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
1191*7c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == KIOCCMD);
1192*7c478bd9Sstevel@tonic-gate 
1193*7c478bd9Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
1194*7c478bd9Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
1195*7c478bd9Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
1196*7c478bd9Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
1197*7c478bd9Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
1198*7c478bd9Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
1199*7c478bd9Sstevel@tonic-gate 
1200*7c478bd9Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
1201*7c478bd9Sstevel@tonic-gate 
1202*7c478bd9Sstevel@tonic-gate 		/*
1203*7c478bd9Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
1204*7c478bd9Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
1205*7c478bd9Sstevel@tonic-gate 		 * otherwise, system will panic.
1206*7c478bd9Sstevel@tonic-gate 		 */
1207*7c478bd9Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
1208*7c478bd9Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
1209*7c478bd9Sstevel@tonic-gate 			lqs = lqs->lqs_next;
1210*7c478bd9Sstevel@tonic-gate 			msg->kpm_req_nums --;
1211*7c478bd9Sstevel@tonic-gate 			retry = 0;
1212*7c478bd9Sstevel@tonic-gate 			continue;
1213*7c478bd9Sstevel@tonic-gate 		}
1214*7c478bd9Sstevel@tonic-gate 
1215*7c478bd9Sstevel@tonic-gate 		clonemp = copymsg(mp);
1216*7c478bd9Sstevel@tonic-gate 		if (clonemp != NULL) {
1217*7c478bd9Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
1218*7c478bd9Sstevel@tonic-gate 				lqs = lqs->lqs_next;
1219*7c478bd9Sstevel@tonic-gate 				retry = 0;
1220*7c478bd9Sstevel@tonic-gate 				continue;
1221*7c478bd9Sstevel@tonic-gate 			}
1222*7c478bd9Sstevel@tonic-gate 
1223*7c478bd9Sstevel@tonic-gate 			/*
1224*7c478bd9Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
1225*7c478bd9Sstevel@tonic-gate 			 */
1226*7c478bd9Sstevel@tonic-gate 			freemsg(clonemp);
1227*7c478bd9Sstevel@tonic-gate 		}
1228*7c478bd9Sstevel@tonic-gate 
1229*7c478bd9Sstevel@tonic-gate 		/*
1230*7c478bd9Sstevel@tonic-gate 		 * During testing it was observed that occasionally
1231*7c478bd9Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
1232*7c478bd9Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
1233*7c478bd9Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
1234*7c478bd9Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
1235*7c478bd9Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
1236*7c478bd9Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
1237*7c478bd9Sstevel@tonic-gate 		 *
1238*7c478bd9Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
1239*7c478bd9Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
1240*7c478bd9Sstevel@tonic-gate 		 * or behave weirdly,
1241*7c478bd9Sstevel@tonic-gate 		 *
1242*7c478bd9Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
1243*7c478bd9Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
1244*7c478bd9Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
1245*7c478bd9Sstevel@tonic-gate 		 *	entering kmdb;
1246*7c478bd9Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
1247*7c478bd9Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
1248*7c478bd9Sstevel@tonic-gate 		 *	is not available in normal mode once returning
1249*7c478bd9Sstevel@tonic-gate 		 *	from kmdb;
1250*7c478bd9Sstevel@tonic-gate 		 * 3) for KIOCCMD
1251*7c478bd9Sstevel@tonic-gate 		 * 	3.1) for KBD_CMD_NOBELL
1252*7c478bd9Sstevel@tonic-gate 		 * 		there's no beep in USB and PS2 keyboard,
1253*7c478bd9Sstevel@tonic-gate 		 * 		this ioctl actually disables the beep on
1254*7c478bd9Sstevel@tonic-gate 		 * 		system mainboard. Note that all the cloned
1255*7c478bd9Sstevel@tonic-gate 		 * 		messages sent down to lower queues do the
1256*7c478bd9Sstevel@tonic-gate 		 * 		same job for system mainboard. Therefore,
1257*7c478bd9Sstevel@tonic-gate 		 * 		even if we fail to send this ioctl to most
1258*7c478bd9Sstevel@tonic-gate 		 * 		of lower queues, the beep still would be
1259*7c478bd9Sstevel@tonic-gate 		 * 		disabled. So, no trouble exists here.
1260*7c478bd9Sstevel@tonic-gate 		 *	3.2) for others
1261*7c478bd9Sstevel@tonic-gate 		 *		nothing;
1262*7c478bd9Sstevel@tonic-gate 		 *
1263*7c478bd9Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
1264*7c478bd9Sstevel@tonic-gate 		 * same request comes again.
1265*7c478bd9Sstevel@tonic-gate 		 */
1266*7c478bd9Sstevel@tonic-gate 		if (retry ++ >= 5) {
1267*7c478bd9Sstevel@tonic-gate 			dev_t	devt;
1268*7c478bd9Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
1269*7c478bd9Sstevel@tonic-gate 
1270*7c478bd9Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
1271*7c478bd9Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
1272*7c478bd9Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
1273*7c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
1274*7c478bd9Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
1275*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
1276*7c478bd9Sstevel@tonic-gate 					    "keyboard is not available"
1277*7c478bd9Sstevel@tonic-gate 					    " for system debugging: %s",
1278*7c478bd9Sstevel@tonic-gate 					    path);
1279*7c478bd9Sstevel@tonic-gate 				break;
1280*7c478bd9Sstevel@tonic-gate 
1281*7c478bd9Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
1282*7c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
1283*7c478bd9Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
1284*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
1285*7c478bd9Sstevel@tonic-gate 					    "keyboard is not available:"
1286*7c478bd9Sstevel@tonic-gate 					    " %s", path);
1287*7c478bd9Sstevel@tonic-gate 				break;
1288*7c478bd9Sstevel@tonic-gate 
1289*7c478bd9Sstevel@tonic-gate 			default:
1290*7c478bd9Sstevel@tonic-gate 				break;
1291*7c478bd9Sstevel@tonic-gate 			}
1292*7c478bd9Sstevel@tonic-gate 			msg->kpm_req_nums --;
1293*7c478bd9Sstevel@tonic-gate 			lqs = lqs->lqs_next;
1294*7c478bd9Sstevel@tonic-gate 			retry = 0;
1295*7c478bd9Sstevel@tonic-gate 		}
1296*7c478bd9Sstevel@tonic-gate 	}
1297*7c478bd9Sstevel@tonic-gate 
1298*7c478bd9Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
1299*7c478bd9Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
1300*7c478bd9Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
1301*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
1302*7c478bd9Sstevel@tonic-gate 	}
1303*7c478bd9Sstevel@tonic-gate 
1304*7c478bd9Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
1305*7c478bd9Sstevel@tonic-gate 
1306*7c478bd9Sstevel@tonic-gate 
1307*7c478bd9Sstevel@tonic-gate static void
1308*7c478bd9Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
1309*7c478bd9Sstevel@tonic-gate {
1310*7c478bd9Sstevel@tonic-gate 	mblk_t		*req;
1311*7c478bd9Sstevel@tonic-gate 	queue_t		*lowque;
1312*7c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
1313*7c478bd9Sstevel@tonic-gate 	struct linkblk		*linkp;
1314*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
1315*7c478bd9Sstevel@tonic-gate 
1316*7c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&conskbd_lq_lock));
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
1319*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
1320*7c478bd9Sstevel@tonic-gate 
1321*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1322*7c478bd9Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
1323*7c478bd9Sstevel@tonic-gate 	lowque = linkp->l_qbot;
1324*7c478bd9Sstevel@tonic-gate 
1325*7c478bd9Sstevel@tonic-gate 	lowque->q_ptr = (void *)lqs;
1326*7c478bd9Sstevel@tonic-gate 	OTHERQ(lowque)->q_ptr = (void *)lqs;
1327*7c478bd9Sstevel@tonic-gate 
1328*7c478bd9Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
1329*7c478bd9Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
1330*7c478bd9Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
1331*7c478bd9Sstevel@tonic-gate 
1332*7c478bd9Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
1333*7c478bd9Sstevel@tonic-gate 	if (req == NULL) {
1334*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
1335*7c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
1336*7c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
1337*7c478bd9Sstevel@tonic-gate 		return;
1338*7c478bd9Sstevel@tonic-gate 	}
1339*7c478bd9Sstevel@tonic-gate 
1340*7c478bd9Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
1341*7c478bd9Sstevel@tonic-gate 	if (req->b_cont == NULL) {
1342*7c478bd9Sstevel@tonic-gate 		freemsg(req);
1343*7c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
1344*7c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
1345*7c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
1346*7c478bd9Sstevel@tonic-gate 		return;
1347*7c478bd9Sstevel@tonic-gate 	}
1348*7c478bd9Sstevel@tonic-gate 
1349*7c478bd9Sstevel@tonic-gate 	iocp->ioc_count = 0;
1350*7c478bd9Sstevel@tonic-gate 	iocp->ioc_rval = 0;
1351*7c478bd9Sstevel@tonic-gate 
1352*7c478bd9Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
1353*7c478bd9Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
1356*7c478bd9Sstevel@tonic-gate 
1357*7c478bd9Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
1358*7c478bd9Sstevel@tonic-gate 		freemsg(req);
1359*7c478bd9Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
1360*7c478bd9Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
1361*7c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
1362*7c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
1363*7c478bd9Sstevel@tonic-gate 	}
1364*7c478bd9Sstevel@tonic-gate 
1365*7c478bd9Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
1366*7c478bd9Sstevel@tonic-gate 
1367*7c478bd9Sstevel@tonic-gate 
1368*7c478bd9Sstevel@tonic-gate /*
1369*7c478bd9Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
1370*7c478bd9Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
1371*7c478bd9Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
1372*7c478bd9Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
1373*7c478bd9Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
1374*7c478bd9Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
1375*7c478bd9Sstevel@tonic-gate  */
1376*7c478bd9Sstevel@tonic-gate static void
1377*7c478bd9Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
1378*7c478bd9Sstevel@tonic-gate {
1379*7c478bd9Sstevel@tonic-gate 	switch (lqs->lqs_state) {
1380*7c478bd9Sstevel@tonic-gate 
1381*7c478bd9Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
1382*7c478bd9Sstevel@tonic-gate 	case LQS_INITIALIZED:
1383*7c478bd9Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
1384*7c478bd9Sstevel@tonic-gate 		break;
1385*7c478bd9Sstevel@tonic-gate 
1386*7c478bd9Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
1387*7c478bd9Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
1388*7c478bd9Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
1389*7c478bd9Sstevel@tonic-gate 		break;
1390*7c478bd9Sstevel@tonic-gate 
1391*7c478bd9Sstevel@tonic-gate 	/* S4: wait lower queue to acknowledge KIOCSLED  message */
1392*7c478bd9Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
1393*7c478bd9Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
1394*7c478bd9Sstevel@tonic-gate 		break;
1395*7c478bd9Sstevel@tonic-gate 
1396*7c478bd9Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
1397*7c478bd9Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
1398*7c478bd9Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
1399*7c478bd9Sstevel@tonic-gate 		break;
1400*7c478bd9Sstevel@tonic-gate 
1401*7c478bd9Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
1402*7c478bd9Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
1403*7c478bd9Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
1404*7c478bd9Sstevel@tonic-gate 		break;
1405*7c478bd9Sstevel@tonic-gate 
1406*7c478bd9Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
1407*7c478bd9Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
1408*7c478bd9Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
1409*7c478bd9Sstevel@tonic-gate 		break;
1410*7c478bd9Sstevel@tonic-gate 
1411*7c478bd9Sstevel@tonic-gate 	/* if reaching here, there must be a error */
1412*7c478bd9Sstevel@tonic-gate 	default:
1413*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
1414*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
1415*7c478bd9Sstevel@tonic-gate 		break;
1416*7c478bd9Sstevel@tonic-gate 	}
1417*7c478bd9Sstevel@tonic-gate 
1418*7c478bd9Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
1419*7c478bd9Sstevel@tonic-gate 
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate static void
1422*7c478bd9Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
1423*7c478bd9Sstevel@tonic-gate {
1424*7c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
1425*7c478bd9Sstevel@tonic-gate 	mblk_t		*msg;
1426*7c478bd9Sstevel@tonic-gate 	mblk_t		*req;
1427*7c478bd9Sstevel@tonic-gate 	queue_t		*lowerque;
1428*7c478bd9Sstevel@tonic-gate 
1429*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
1430*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
1431*7c478bd9Sstevel@tonic-gate 
1432*7c478bd9Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
1433*7c478bd9Sstevel@tonic-gate 
1434*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1435*7c478bd9Sstevel@tonic-gate 	case M_IOCACK:
1436*7c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
1437*7c478bd9Sstevel@tonic-gate 		if (req == NULL) {
1438*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
1439*7c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
1440*7c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
1441*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1442*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1443*7c478bd9Sstevel@tonic-gate 			return;
1444*7c478bd9Sstevel@tonic-gate 		}
1445*7c478bd9Sstevel@tonic-gate 
1446*7c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
1447*7c478bd9Sstevel@tonic-gate 		if (req->b_cont == NULL) {
1448*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
1449*7c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
1450*7c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
1451*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1452*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1453*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1454*7c478bd9Sstevel@tonic-gate 			return;
1455*7c478bd9Sstevel@tonic-gate 		}
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
1458*7c478bd9Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
1459*7c478bd9Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
1462*7c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
1463*7c478bd9Sstevel@tonic-gate 
1464*7c478bd9Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
1465*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1466*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue,
1467*7c478bd9Sstevel@tonic-gate 			    lqs->lqs_pending_plink, 0, ENOMEM);
1468*7c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
1469*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1470*7c478bd9Sstevel@tonic-gate 		}
1471*7c478bd9Sstevel@tonic-gate 		break;
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
1474*7c478bd9Sstevel@tonic-gate 		/*
1475*7c478bd9Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
1476*7c478bd9Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
1477*7c478bd9Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
1478*7c478bd9Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
1479*7c478bd9Sstevel@tonic-gate 		 * will bypass the conskbd module.
1480*7c478bd9Sstevel@tonic-gate 		 */
1481*7c478bd9Sstevel@tonic-gate 
1482*7c478bd9Sstevel@tonic-gate 		/*
1483*7c478bd9Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
1484*7c478bd9Sstevel@tonic-gate 		 * conskbd, we reject the current one.
1485*7c478bd9Sstevel@tonic-gate 		 */
1486*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
1487*7c478bd9Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
1488*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
1489*7c478bd9Sstevel@tonic-gate 			    0, iocp->ioc_error);
1490*7c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
1491*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1492*7c478bd9Sstevel@tonic-gate 			break;
1493*7c478bd9Sstevel@tonic-gate 		}
1494*7c478bd9Sstevel@tonic-gate 
1495*7c478bd9Sstevel@tonic-gate 		/*
1496*7c478bd9Sstevel@tonic-gate 		 * Bypass the virutal keyboard for old hardware
1497*7c478bd9Sstevel@tonic-gate 		 */
1498*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_bypassed = B_TRUE;
1499*7c478bd9Sstevel@tonic-gate 
1500*7c478bd9Sstevel@tonic-gate 		msg = lqs->lqs_pending_plink;
1501*7c478bd9Sstevel@tonic-gate 		msg->b_datap->db_type = M_IOCACK;
1502*7c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)msg->b_rptr;
1503*7c478bd9Sstevel@tonic-gate 		iocp->ioc_error = 0;
1504*7c478bd9Sstevel@tonic-gate 
1505*7c478bd9Sstevel@tonic-gate 		/*
1506*7c478bd9Sstevel@tonic-gate 		 * link this keyboard under conskbd
1507*7c478bd9Sstevel@tonic-gate 		 */
1508*7c478bd9Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
1509*7c478bd9Sstevel@tonic-gate 		lqs->lqs_next = conskbd.conskbd_lqueue_list;
1510*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_lqueue_list = lqs;
1511*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_lqueue_nums++;
1512*7c478bd9Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_INITIALIZED_LEGACY;
1515*7c478bd9Sstevel@tonic-gate 
1516*7c478bd9Sstevel@tonic-gate 		qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
1517*7c478bd9Sstevel@tonic-gate 		break;
1518*7c478bd9Sstevel@tonic-gate 	}
1519*7c478bd9Sstevel@tonic-gate 
1520*7c478bd9Sstevel@tonic-gate 	freemsg(mp);
1521*7c478bd9Sstevel@tonic-gate 
1522*7c478bd9Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
1523*7c478bd9Sstevel@tonic-gate 
1524*7c478bd9Sstevel@tonic-gate static void
1525*7c478bd9Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
1526*7c478bd9Sstevel@tonic-gate {
1527*7c478bd9Sstevel@tonic-gate 	struct iocblk 	*iocp;
1528*7c478bd9Sstevel@tonic-gate 	mblk_t		*req;
1529*7c478bd9Sstevel@tonic-gate 	queue_t		*lowerque;
1530*7c478bd9Sstevel@tonic-gate 
1531*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
1532*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
1535*7c478bd9Sstevel@tonic-gate 
1536*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1537*7c478bd9Sstevel@tonic-gate 	case M_IOCACK:
1538*7c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
1539*7c478bd9Sstevel@tonic-gate 		if (req == NULL) {
1540*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
1541*7c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
1542*7c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
1543*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1544*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1545*7c478bd9Sstevel@tonic-gate 			return;
1546*7c478bd9Sstevel@tonic-gate 		}
1547*7c478bd9Sstevel@tonic-gate 
1548*7c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
1549*7c478bd9Sstevel@tonic-gate 		if (req->b_cont == NULL) {
1550*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
1551*7c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
1552*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1553*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1554*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1555*7c478bd9Sstevel@tonic-gate 			return;
1556*7c478bd9Sstevel@tonic-gate 		}
1557*7c478bd9Sstevel@tonic-gate 
1558*7c478bd9Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
1559*7c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
1560*7c478bd9Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
1561*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1562*7c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue,
1563*7c478bd9Sstevel@tonic-gate 			    lqs->lqs_pending_plink, 0, ENOMEM);
1564*7c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
1565*7c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
1566*7c478bd9Sstevel@tonic-gate 		}
1567*7c478bd9Sstevel@tonic-gate 		break;
1568*7c478bd9Sstevel@tonic-gate 
1569*7c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
1570*7c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
1571*7c478bd9Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
1572*7c478bd9Sstevel@tonic-gate 		    0, iocp->ioc_error);
1573*7c478bd9Sstevel@tonic-gate 		lowerque->q_ptr = NULL;
1574*7c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
1575*7c478bd9Sstevel@tonic-gate 		break;
1576*7c478bd9Sstevel@tonic-gate 	}
1577*7c478bd9Sstevel@tonic-gate 
1578*7c478bd9Sstevel@tonic-gate 	freemsg(mp);
1579*7c478bd9Sstevel@tonic-gate 
1580*7c478bd9Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
1581*7c478bd9Sstevel@tonic-gate 
1582*7c478bd9Sstevel@tonic-gate static void
1583*7c478bd9Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
1584*7c478bd9Sstevel@tonic-gate {
1585*7c478bd9Sstevel@tonic-gate 	mblk_t		*req;
1586*7c478bd9Sstevel@tonic-gate 	int		layout;
1587*7c478bd9Sstevel@tonic-gate 	boolean_t	fail;
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
1590*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
1591*7c478bd9Sstevel@tonic-gate 
1592*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1593*7c478bd9Sstevel@tonic-gate 	case M_IOCACK:
1594*7c478bd9Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
1595*7c478bd9Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
1596*7c478bd9Sstevel@tonic-gate 			/*
1597*7c478bd9Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
1598*7c478bd9Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
1599*7c478bd9Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
1600*7c478bd9Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
1601*7c478bd9Sstevel@tonic-gate 			 */
1602*7c478bd9Sstevel@tonic-gate 			if (layout != -1 && conskbd.conskbd_layout == -1)
1603*7c478bd9Sstevel@tonic-gate 				conskbd.conskbd_layout = layout;
1604*7c478bd9Sstevel@tonic-gate 		}
1605*7c478bd9Sstevel@tonic-gate 		break;
1606*7c478bd9Sstevel@tonic-gate 
1607*7c478bd9Sstevel@tonic-gate 
1608*7c478bd9Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
1609*7c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
1610*7c478bd9Sstevel@tonic-gate 		break;
1611*7c478bd9Sstevel@tonic-gate 	}
1612*7c478bd9Sstevel@tonic-gate 
1613*7c478bd9Sstevel@tonic-gate 	freemsg(mp);
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate 	fail = B_TRUE;
1616*7c478bd9Sstevel@tonic-gate 	req = mkiocb(KIOCSLED);
1617*7c478bd9Sstevel@tonic-gate 	if (req) {
1618*7c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
1619*7c478bd9Sstevel@tonic-gate 		if (req->b_cont) {
1620*7c478bd9Sstevel@tonic-gate 			*(uchar_t *)req->b_cont->b_wptr =
1621*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_led_state;
1622*7c478bd9Sstevel@tonic-gate 			req->b_cont->b_wptr += sizeof (uchar_t);
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
1625*7c478bd9Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
1626*7c478bd9Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
1627*7c478bd9Sstevel@tonic-gate 				fail = B_FALSE;
1628*7c478bd9Sstevel@tonic-gate 			} else {
1629*7c478bd9Sstevel@tonic-gate 				freemsg(req);
1630*7c478bd9Sstevel@tonic-gate 			}
1631*7c478bd9Sstevel@tonic-gate 
1632*7c478bd9Sstevel@tonic-gate 		} else {
1633*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1634*7c478bd9Sstevel@tonic-gate 		}
1635*7c478bd9Sstevel@tonic-gate 	}
1636*7c478bd9Sstevel@tonic-gate 
1637*7c478bd9Sstevel@tonic-gate 	if (fail) {
1638*7c478bd9Sstevel@tonic-gate 		/*
1639*7c478bd9Sstevel@tonic-gate 		 * If fail to allocate KIOCSLED message or put the message
1640*7c478bd9Sstevel@tonic-gate 		 * into lower queue, we immediately link current keyboard
1641*7c478bd9Sstevel@tonic-gate 		 * under conskbd. Thus, even if fails to set LED, this
1642*7c478bd9Sstevel@tonic-gate 		 * keyboard could be available.
1643*7c478bd9Sstevel@tonic-gate 		 */
1644*7c478bd9Sstevel@tonic-gate 		conskbd_link_lower_queue(lqs);
1645*7c478bd9Sstevel@tonic-gate 	}
1646*7c478bd9Sstevel@tonic-gate 
1647*7c478bd9Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
1648*7c478bd9Sstevel@tonic-gate 
1649*7c478bd9Sstevel@tonic-gate 
1650*7c478bd9Sstevel@tonic-gate static void
1651*7c478bd9Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
1652*7c478bd9Sstevel@tonic-gate {
1653*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
1654*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
1655*7c478bd9Sstevel@tonic-gate 
1656*7c478bd9Sstevel@tonic-gate 	/*
1657*7c478bd9Sstevel@tonic-gate 	 * Basically, failure of setting LED is not a fatal error,
1658*7c478bd9Sstevel@tonic-gate 	 * so we will plumb the lower queue into conskbd whether
1659*7c478bd9Sstevel@tonic-gate 	 * setting LED succeeds or fails.
1660*7c478bd9Sstevel@tonic-gate 	 */
1661*7c478bd9Sstevel@tonic-gate 	freemsg(mp);
1662*7c478bd9Sstevel@tonic-gate 	conskbd_link_lower_queue(lqs);
1663*7c478bd9Sstevel@tonic-gate 
1664*7c478bd9Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
1665*7c478bd9Sstevel@tonic-gate 
1666*7c478bd9Sstevel@tonic-gate 
1667*7c478bd9Sstevel@tonic-gate static void
1668*7c478bd9Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
1669*7c478bd9Sstevel@tonic-gate {
1670*7c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
1671*7c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
1672*7c478bd9Sstevel@tonic-gate 	int			error;
1673*7c478bd9Sstevel@tonic-gate 	dev_t			devt;
1674*7c478bd9Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
1675*7c478bd9Sstevel@tonic-gate 
1676*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
1677*7c478bd9Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
1678*7c478bd9Sstevel@tonic-gate 
1679*7c478bd9Sstevel@tonic-gate 	if (!msg) {
1680*7c478bd9Sstevel@tonic-gate 		/*
1681*7c478bd9Sstevel@tonic-gate 		 * Here, we just discard the responses to KIOCSLED request.
1682*7c478bd9Sstevel@tonic-gate 		 * Please refer to conskbd_streams_setled().
1683*7c478bd9Sstevel@tonic-gate 		 */
1684*7c478bd9Sstevel@tonic-gate 		ASSERT(((struct iocblk *)mp->b_rptr)->ioc_cmd == KIOCSLED);
1685*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
1686*7c478bd9Sstevel@tonic-gate 		return;
1687*7c478bd9Sstevel@tonic-gate 	}
1688*7c478bd9Sstevel@tonic-gate 
1689*7c478bd9Sstevel@tonic-gate 	/*
1690*7c478bd9Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
1691*7c478bd9Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
1692*7c478bd9Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
1693*7c478bd9Sstevel@tonic-gate 	 * the lower queue from which the current response message
1694*7c478bd9Sstevel@tonic-gate 	 * comes.
1695*7c478bd9Sstevel@tonic-gate 	 */
1696*7c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
1697*7c478bd9Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
1698*7c478bd9Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
1699*7c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
1700*7c478bd9Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
1701*7c478bd9Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
1702*7c478bd9Sstevel@tonic-gate 	mutex_exit(&msg->kpm_lock);
1703*7c478bd9Sstevel@tonic-gate 
1704*7c478bd9Sstevel@tonic-gate 	if (msg->kpm_resp_nums < msg->kpm_req_nums)
1705*7c478bd9Sstevel@tonic-gate 		return;
1706*7c478bd9Sstevel@tonic-gate 
1707*7c478bd9Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
1708*7c478bd9Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
1709*7c478bd9Sstevel@tonic-gate 
1710*7c478bd9Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
1711*7c478bd9Sstevel@tonic-gate 
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate 	/*
1714*7c478bd9Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
1715*7c478bd9Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
1716*7c478bd9Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
1717*7c478bd9Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
1718*7c478bd9Sstevel@tonic-gate 	 * is sent to upstreams
1719*7c478bd9Sstevel@tonic-gate 	 */
1720*7c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
1721*7c478bd9Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
1722*7c478bd9Sstevel@tonic-gate 
1723*7c478bd9Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
1724*7c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
1725*7c478bd9Sstevel@tonic-gate 		/*
1726*7c478bd9Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
1727*7c478bd9Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
1728*7c478bd9Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
1729*7c478bd9Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
1730*7c478bd9Sstevel@tonic-gate 		 * check if the pointer is null.
1731*7c478bd9Sstevel@tonic-gate 		 */
1732*7c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
1733*7c478bd9Sstevel@tonic-gate 			cons_polledio_t		*polledio;
1734*7c478bd9Sstevel@tonic-gate 
1735*7c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
1736*7c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
1737*7c478bd9Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
1738*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
1739*7c478bd9Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
1740*7c478bd9Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
1741*7c478bd9Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
1742*7c478bd9Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
1743*7c478bd9Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
1744*7c478bd9Sstevel@tonic-gate 					error = 0;
1745*7c478bd9Sstevel@tonic-gate 				} else {
1746*7c478bd9Sstevel@tonic-gate 					/*
1747*7c478bd9Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
1748*7c478bd9Sstevel@tonic-gate 					 * use the same cons_polledio structure
1749*7c478bd9Sstevel@tonic-gate 					 * as conskbd.
1750*7c478bd9Sstevel@tonic-gate 					 */
1751*7c478bd9Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
1752*7c478bd9Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
1753*7c478bd9Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
1754*7c478bd9Sstevel@tonic-gate 						    "driver does not support "
1755*7c478bd9Sstevel@tonic-gate 						    "system debugging: %s",
1756*7c478bd9Sstevel@tonic-gate 						    path);
1757*7c478bd9Sstevel@tonic-gate 					}
1758*7c478bd9Sstevel@tonic-gate 					error = EINVAL;
1759*7c478bd9Sstevel@tonic-gate 				}
1760*7c478bd9Sstevel@tonic-gate 			} else {
1761*7c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
1762*7c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
1763*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
1764*7c478bd9Sstevel@tonic-gate 					    " not available for system"
1765*7c478bd9Sstevel@tonic-gate 					    " debugging:  %s", path);
1766*7c478bd9Sstevel@tonic-gate 				}
1767*7c478bd9Sstevel@tonic-gate 			}
1768*7c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
1769*7c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
1770*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1771*7c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
1772*7c478bd9Sstevel@tonic-gate 		}
1773*7c478bd9Sstevel@tonic-gate 
1774*7c478bd9Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
1775*7c478bd9Sstevel@tonic-gate 		if (error == 0) {
1776*7c478bd9Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
1777*7c478bd9Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
1778*7c478bd9Sstevel@tonic-gate 		}
1779*7c478bd9Sstevel@tonic-gate 		break;
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
1782*7c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
1783*7c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
1784*7c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
1785*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
1786*7c478bd9Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
1787*7c478bd9Sstevel@tonic-gate 				error = 0;
1788*7c478bd9Sstevel@tonic-gate 			} else {
1789*7c478bd9Sstevel@tonic-gate 				devt =
1790*7c478bd9Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
1791*7c478bd9Sstevel@tonic-gate 
1792*7c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
1793*7c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
1794*7c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
1795*7c478bd9Sstevel@tonic-gate 					    " not available: %s", path);
1796*7c478bd9Sstevel@tonic-gate 				}
1797*7c478bd9Sstevel@tonic-gate 			}
1798*7c478bd9Sstevel@tonic-gate 
1799*7c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
1800*7c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
1801*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1802*7c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
1803*7c478bd9Sstevel@tonic-gate 		}
1804*7c478bd9Sstevel@tonic-gate 		break;
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate 	case KIOCCMD:
1807*7c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
1808*7c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
1809*7c478bd9Sstevel@tonic-gate 
1810*7c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
1811*7c478bd9Sstevel@tonic-gate 				error = 0;
1812*7c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
1813*7c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
1814*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
1815*7c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
1816*7c478bd9Sstevel@tonic-gate 		}
1817*7c478bd9Sstevel@tonic-gate 		break;
1818*7c478bd9Sstevel@tonic-gate 
1819*7c478bd9Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
1820*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
1821*7c478bd9Sstevel@tonic-gate 	}
1822*7c478bd9Sstevel@tonic-gate 
1823*7c478bd9Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
1824*7c478bd9Sstevel@tonic-gate 	if (error == 0) {
1825*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
1826*7c478bd9Sstevel@tonic-gate 	} else {
1827*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
1828*7c478bd9Sstevel@tonic-gate 	}
1829*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1830*7c478bd9Sstevel@tonic-gate 	iocp->ioc_error = error;
1831*7c478bd9Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
1832*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
1833*7c478bd9Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
1834*7c478bd9Sstevel@tonic-gate 
1835*7c478bd9Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
1836*7c478bd9Sstevel@tonic-gate 
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate static void
1839*7c478bd9Sstevel@tonic-gate conskbd_link_lower_queue(conskbd_lower_queue_t *lqs)
1840*7c478bd9Sstevel@tonic-gate {
1841*7c478bd9Sstevel@tonic-gate 	struct iocblk 	*iocp;
1842*7c478bd9Sstevel@tonic-gate 	mblk_t		*msg;
1843*7c478bd9Sstevel@tonic-gate 	int		index;
1844*7c478bd9Sstevel@tonic-gate 
1845*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
1846*7c478bd9Sstevel@tonic-gate 
1847*7c478bd9Sstevel@tonic-gate 	msg = lqs->lqs_pending_plink;
1848*7c478bd9Sstevel@tonic-gate 	msg->b_datap->db_type = M_IOCACK;
1849*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)msg->b_rptr;
1850*7c478bd9Sstevel@tonic-gate 	iocp->ioc_error = 0;
1851*7c478bd9Sstevel@tonic-gate 
1852*7c478bd9Sstevel@tonic-gate 	/*
1853*7c478bd9Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
1854*7c478bd9Sstevel@tonic-gate 	 */
1855*7c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_lq_lock);
1856*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_lqueue_nums++;
1857*7c478bd9Sstevel@tonic-gate 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
1858*7c478bd9Sstevel@tonic-gate 	conskbd.conskbd_lqueue_list = lqs;
1859*7c478bd9Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
1860*7c478bd9Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
1861*7c478bd9Sstevel@tonic-gate 	}
1862*7c478bd9Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
1863*7c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_lq_lock);
1864*7c478bd9Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
1865*7c478bd9Sstevel@tonic-gate 
1866*7c478bd9Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
1867*7c478bd9Sstevel@tonic-gate 
1868*7c478bd9Sstevel@tonic-gate 
1869*7c478bd9Sstevel@tonic-gate 
1870*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1871*7c478bd9Sstevel@tonic-gate static void
1872*7c478bd9Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
1873*7c478bd9Sstevel@tonic-gate {
1874*7c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
1875*7c478bd9Sstevel@tonic-gate 
1876*7c478bd9Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
1877*7c478bd9Sstevel@tonic-gate 
1878*7c478bd9Sstevel@tonic-gate 	/*
1879*7c478bd9Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
1880*7c478bd9Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
1881*7c478bd9Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
1882*7c478bd9Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
1883*7c478bd9Sstevel@tonic-gate 	 * should be added to this list.
1884*7c478bd9Sstevel@tonic-gate 	 */
1885*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
1886*7c478bd9Sstevel@tonic-gate 
1887*7c478bd9Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
1888*7c478bd9Sstevel@tonic-gate 			(iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
1889*7c478bd9Sstevel@tonic-gate 
1890*7c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1891*7c478bd9Sstevel@tonic-gate 			("conskbd_legacy_upstream_msg: "
1892*7c478bd9Sstevel@tonic-gate 			"CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
1893*7c478bd9Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
1894*7c478bd9Sstevel@tonic-gate 
1895*7c478bd9Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
1896*7c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1897*7c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
1898*7c478bd9Sstevel@tonic-gate 
1899*7c478bd9Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
1900*7c478bd9Sstevel@tonic-gate 
1901*7c478bd9Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
1902*7c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
1903*7c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
1904*7c478bd9Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
1905*7c478bd9Sstevel@tonic-gate 	} else {
1906*7c478bd9Sstevel@tonic-gate 		/* if reached here, it must be a error */
1907*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
1908*7c478bd9Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
1909*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
1910*7c478bd9Sstevel@tonic-gate 	}
1911*7c478bd9Sstevel@tonic-gate 
1912*7c478bd9Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
1913*7c478bd9Sstevel@tonic-gate 
1914*7c478bd9Sstevel@tonic-gate /*
1915*7c478bd9Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
1916*7c478bd9Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
1917*7c478bd9Sstevel@tonic-gate  *
1918*7c478bd9Sstevel@tonic-gate  * 1) application initiated request
1919*7c478bd9Sstevel@tonic-gate  * 	A KIOCSLED ioctl is sent by an application. The ioctl will be
1920*7c478bd9Sstevel@tonic-gate  * 	be prcoessed by queue service procedure conskbduwsrv(), which
1921*7c478bd9Sstevel@tonic-gate  * 	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
1922*7c478bd9Sstevel@tonic-gate  * 	conskbd_streams_setled() to set LED, after that,  kbtrans will
1923*7c478bd9Sstevel@tonic-gate  * 	return an ACK message to upper module.
1924*7c478bd9Sstevel@tonic-gate  *
1925*7c478bd9Sstevel@tonic-gate  * 2) Kbtrans initiated the request
1926*7c478bd9Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
1927*7c478bd9Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
1928*7c478bd9Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
1929*7c478bd9Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
1930*7c478bd9Sstevel@tonic-gate  *
1931*7c478bd9Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
1932*7c478bd9Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
1933*7c478bd9Sstevel@tonic-gate  * has no return value.
1934*7c478bd9Sstevel@tonic-gate  */
1935*7c478bd9Sstevel@tonic-gate static void
1936*7c478bd9Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
1937*7c478bd9Sstevel@tonic-gate {
1938*7c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
1939*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
1940*7c478bd9Sstevel@tonic-gate 	mblk_t		*req;
1941*7c478bd9Sstevel@tonic-gate 
1942*7c478bd9Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
1943*7c478bd9Sstevel@tonic-gate 
1944*7c478bd9Sstevel@tonic-gate 	if (led_state == -1)
1945*7c478bd9Sstevel@tonic-gate 		return;
1946*7c478bd9Sstevel@tonic-gate 
1947*7c478bd9Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
1948*7c478bd9Sstevel@tonic-gate 
1949*7c478bd9Sstevel@tonic-gate 	/*
1950*7c478bd9Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
1951*7c478bd9Sstevel@tonic-gate 	 * it if this happens.
1952*7c478bd9Sstevel@tonic-gate 	 */
1953*7c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
1954*7c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
1955*7c478bd9Sstevel@tonic-gate 
1956*7c478bd9Sstevel@tonic-gate 		if (!req) {
1957*7c478bd9Sstevel@tonic-gate 			continue;
1958*7c478bd9Sstevel@tonic-gate 		}
1959*7c478bd9Sstevel@tonic-gate 
1960*7c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
1961*7c478bd9Sstevel@tonic-gate 		if (!req->b_cont) {
1962*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1963*7c478bd9Sstevel@tonic-gate 			continue;
1964*7c478bd9Sstevel@tonic-gate 		}
1965*7c478bd9Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
1966*7c478bd9Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
1967*7c478bd9Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
1968*7c478bd9Sstevel@tonic-gate 			freemsg(req);
1969*7c478bd9Sstevel@tonic-gate 	}
1970*7c478bd9Sstevel@tonic-gate 
1971*7c478bd9Sstevel@tonic-gate }	/* conskbd_streams_setled() */
1972*7c478bd9Sstevel@tonic-gate 
1973*7c478bd9Sstevel@tonic-gate static void
1974*7c478bd9Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
1975*7c478bd9Sstevel@tonic-gate {
1976*7c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
1977*7c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
1978*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
1979*7c478bd9Sstevel@tonic-gate 
1980*7c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
1981*7c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
1982*7c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
1983*7c478bd9Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
1984*7c478bd9Sstevel@tonic-gate 			    led_state);
1985*7c478bd9Sstevel@tonic-gate 		}
1986*7c478bd9Sstevel@tonic-gate 	}
1987*7c478bd9Sstevel@tonic-gate 
1988*7c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
1989*7c478bd9Sstevel@tonic-gate 
1990*7c478bd9Sstevel@tonic-gate static boolean_t
1991*7c478bd9Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
1992*7c478bd9Sstevel@tonic-gate 		kbtrans_key_t *keycode, enum keystate *state)
1993*7c478bd9Sstevel@tonic-gate {
1994*7c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
1995*7c478bd9Sstevel@tonic-gate 	struct cons_polledio 		*cb;
1996*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
1997*7c478bd9Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
1998*7c478bd9Sstevel@tonic-gate 
1999*7c478bd9Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
2000*7c478bd9Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
2001*7c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
2002*7c478bd9Sstevel@tonic-gate 		if ((cb != NULL) &&
2003*7c478bd9Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
2004*7c478bd9Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
2005*7c478bd9Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
2006*7c478bd9Sstevel@tonic-gate 		}
2007*7c478bd9Sstevel@tonic-gate 
2008*7c478bd9Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
2009*7c478bd9Sstevel@tonic-gate 		if (ret == B_TRUE) {
2010*7c478bd9Sstevel@tonic-gate 			break;
2011*7c478bd9Sstevel@tonic-gate 		}
2012*7c478bd9Sstevel@tonic-gate 	}
2013*7c478bd9Sstevel@tonic-gate 
2014*7c478bd9Sstevel@tonic-gate 	return (ret);
2015*7c478bd9Sstevel@tonic-gate 
2016*7c478bd9Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate static boolean_t
2019*7c478bd9Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
2020*7c478bd9Sstevel@tonic-gate {
2021*7c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
2022*7c478bd9Sstevel@tonic-gate 	int		directio;
2023*7c478bd9Sstevel@tonic-gate 	int		error;
2024*7c478bd9Sstevel@tonic-gate 
2025*7c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
2026*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2027*7c478bd9Sstevel@tonic-gate 
2028*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
2029*7c478bd9Sstevel@tonic-gate 
2030*7c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
2031*7c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT: {
2032*7c478bd9Sstevel@tonic-gate 		/*
2033*7c478bd9Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
2034*7c478bd9Sstevel@tonic-gate 		 * respond incorrectly.
2035*7c478bd9Sstevel@tonic-gate 		 */
2036*7c478bd9Sstevel@tonic-gate 		register mblk_t *datap;
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
2039*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
2040*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
2041*7c478bd9Sstevel@tonic-gate 		}
2042*7c478bd9Sstevel@tonic-gate 
2043*7c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
2044*7c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
2045*7c478bd9Sstevel@tonic-gate 		if (mp->b_cont) {
2046*7c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
2047*7c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
2048*7c478bd9Sstevel@tonic-gate 		}
2049*7c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
2050*7c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
2051*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
2052*7c478bd9Sstevel@tonic-gate 	}
2053*7c478bd9Sstevel@tonic-gate 
2054*7c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
2055*7c478bd9Sstevel@tonic-gate 		/*
2056*7c478bd9Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
2057*7c478bd9Sstevel@tonic-gate 		 * based code see it and respond to it.
2058*7c478bd9Sstevel@tonic-gate 		 */
2059*7c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
2060*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
2061*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
2062*7c478bd9Sstevel@tonic-gate 		}
2063*7c478bd9Sstevel@tonic-gate 
2064*7c478bd9Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
2065*7c478bd9Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
2066*7c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
2067*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
2068*7c478bd9Sstevel@tonic-gate 		}
2069*7c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
2070*7c478bd9Sstevel@tonic-gate 
2071*7c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
2072*7c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
2073*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
2074*7c478bd9Sstevel@tonic-gate 		} else {
2075*7c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
2076*7c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
2077*7c478bd9Sstevel@tonic-gate 		}
2078*7c478bd9Sstevel@tonic-gate 
2079*7c478bd9Sstevel@tonic-gate 		/*
2080*7c478bd9Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
2081*7c478bd9Sstevel@tonic-gate 		 */
2082*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2083*7c478bd9Sstevel@tonic-gate 
2084*7c478bd9Sstevel@tonic-gate 	default:
2085*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
2086*7c478bd9Sstevel@tonic-gate 	}
2087*7c478bd9Sstevel@tonic-gate 
2088*7c478bd9Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
2089*7c478bd9Sstevel@tonic-gate 
2090*7c478bd9Sstevel@tonic-gate 
2091*7c478bd9Sstevel@tonic-gate static void
2092*7c478bd9Sstevel@tonic-gate conskbd_polledio_enter(struct cons_polledio_arg *arg)
2093*7c478bd9Sstevel@tonic-gate {
2094*7c478bd9Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
2095*7c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
2096*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
2097*7c478bd9Sstevel@tonic-gate 
2098*7c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
2099*7c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
2100*7c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
2101*7c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
2102*7c478bd9Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
2103*7c478bd9Sstevel@tonic-gate 		}
2104*7c478bd9Sstevel@tonic-gate 	}
2105*7c478bd9Sstevel@tonic-gate 
2106*7c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
2107*7c478bd9Sstevel@tonic-gate 
2108*7c478bd9Sstevel@tonic-gate static void
2109*7c478bd9Sstevel@tonic-gate conskbd_polledio_exit(struct cons_polledio_arg *arg)
2110*7c478bd9Sstevel@tonic-gate {
2111*7c478bd9Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
2112*7c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
2113*7c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
2114*7c478bd9Sstevel@tonic-gate 
2115*7c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
2116*7c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
2117*7c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
2118*7c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
2119*7c478bd9Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
2120*7c478bd9Sstevel@tonic-gate 		}
2121*7c478bd9Sstevel@tonic-gate 	}
2122*7c478bd9Sstevel@tonic-gate 
2123*7c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
2124*7c478bd9Sstevel@tonic-gate 
2125*7c478bd9Sstevel@tonic-gate static int
2126*7c478bd9Sstevel@tonic-gate conskbd_polledio_getchar(struct cons_polledio_arg *arg)
2127*7c478bd9Sstevel@tonic-gate {
2128*7c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
2129*7c478bd9Sstevel@tonic-gate 
2130*7c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
2131*7c478bd9Sstevel@tonic-gate 
2132*7c478bd9Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
2133*7c478bd9Sstevel@tonic-gate 
2134*7c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
2135*7c478bd9Sstevel@tonic-gate 
2136*7c478bd9Sstevel@tonic-gate static int
2137*7c478bd9Sstevel@tonic-gate conskbd_polledio_ischar(struct cons_polledio_arg *arg)
2138*7c478bd9Sstevel@tonic-gate {
2139*7c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
2140*7c478bd9Sstevel@tonic-gate 
2141*7c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
2142*7c478bd9Sstevel@tonic-gate 
2143*7c478bd9Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
2144*7c478bd9Sstevel@tonic-gate 
2145*7c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
2146*7c478bd9Sstevel@tonic-gate 
2147*7c478bd9Sstevel@tonic-gate 
2148*7c478bd9Sstevel@tonic-gate static void
2149*7c478bd9Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
2150*7c478bd9Sstevel@tonic-gate {
2151*7c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
2152*7c478bd9Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
2153*7c478bd9Sstevel@tonic-gate 	conskbd_msg_queue = msg;
2154*7c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
2155*7c478bd9Sstevel@tonic-gate 
2156*7c478bd9Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
2157*7c478bd9Sstevel@tonic-gate 
2158*7c478bd9Sstevel@tonic-gate /*
2159*7c478bd9Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
2160*7c478bd9Sstevel@tonic-gate  */
2161*7c478bd9Sstevel@tonic-gate static conskbd_pending_msg_t *
2162*7c478bd9Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
2163*7c478bd9Sstevel@tonic-gate {
2164*7c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
2165*7c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
2166*7c478bd9Sstevel@tonic-gate 	uint_t	id;
2167*7c478bd9Sstevel@tonic-gate 
2168*7c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
2169*7c478bd9Sstevel@tonic-gate 	msg = conskbd_msg_queue;
2170*7c478bd9Sstevel@tonic-gate 
2171*7c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
2172*7c478bd9Sstevel@tonic-gate 	ASSERT(iocp);
2173*7c478bd9Sstevel@tonic-gate 	id = iocp->ioc_id;
2174*7c478bd9Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
2175*7c478bd9Sstevel@tonic-gate 		msg = msg->kpm_next;
2176*7c478bd9Sstevel@tonic-gate 	}
2177*7c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
2178*7c478bd9Sstevel@tonic-gate 
2179*7c478bd9Sstevel@tonic-gate 	return (msg);
2180*7c478bd9Sstevel@tonic-gate 
2181*7c478bd9Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
2182*7c478bd9Sstevel@tonic-gate 
2183*7c478bd9Sstevel@tonic-gate 
2184*7c478bd9Sstevel@tonic-gate static void
2185*7c478bd9Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
2186*7c478bd9Sstevel@tonic-gate {
2187*7c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
2188*7c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
2189*7c478bd9Sstevel@tonic-gate 
2190*7c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
2191*7c478bd9Sstevel@tonic-gate 	prev = conskbd_msg_queue;
2192*7c478bd9Sstevel@tonic-gate 
2193*7c478bd9Sstevel@tonic-gate 	for (p = prev; p != msg; p = p->kpm_next)
2194*7c478bd9Sstevel@tonic-gate 		prev = p;
2195*7c478bd9Sstevel@tonic-gate 	ASSERT(p && p == msg);
2196*7c478bd9Sstevel@tonic-gate 	if (prev == p) {
2197*7c478bd9Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
2198*7c478bd9Sstevel@tonic-gate 	} else {
2199*7c478bd9Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
2200*7c478bd9Sstevel@tonic-gate 	}
2201*7c478bd9Sstevel@tonic-gate 	p->kpm_next = NULL;
2202*7c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
2203*7c478bd9Sstevel@tonic-gate 
2204*7c478bd9Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
2205*7c478bd9Sstevel@tonic-gate 
2206*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
2207*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2208*7c478bd9Sstevel@tonic-gate void
2209*7c478bd9Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
2210*7c478bd9Sstevel@tonic-gate {
2211*7c478bd9Sstevel@tonic-gate 	char buf[256];
2212*7c478bd9Sstevel@tonic-gate 	va_list ap;
2213*7c478bd9Sstevel@tonic-gate 
2214*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2215*7c478bd9Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
2216*7c478bd9Sstevel@tonic-gate 	va_end(ap);
2217*7c478bd9Sstevel@tonic-gate 
2218*7c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
2219*7c478bd9Sstevel@tonic-gate 
2220*7c478bd9Sstevel@tonic-gate }	/* conskbd_dprintf() */
2221*7c478bd9Sstevel@tonic-gate #endif
2222