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