xref: /onnv-gate/usr/src/uts/common/io/conskbd.c (revision 1762:b91aae9ab33e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51272Slq150181  * Common Development and Distribution License (the "License").
61272Slq150181  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211272Slq150181 
220Sstevel@tonic-gate /*
231272Slq150181  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Console kbd multiplexor driver for Sun.
310Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "kbd" module pushed
320Sstevel@tonic-gate  * on top of it.
330Sstevel@tonic-gate  * Minor device 0 is what programs normally use.
340Sstevel@tonic-gate  * Minor device 1 is used to feed predigested keystrokes to the "workstation
350Sstevel@tonic-gate  * console" driver, which it is linked beneath.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *     This module can support multiple keyboards to be used simultaneously.
390Sstevel@tonic-gate  * and enable users to use at a time multiple keyboards connected to the
400Sstevel@tonic-gate  * same system. All the keyboards are linked under conskbd, and act as a
410Sstevel@tonic-gate  * keyboard with replicated keys.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
440Sstevel@tonic-gate  * a two-level architecure. The lower one is one of serialport drivers, such
450Sstevel@tonic-gate  * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
460Sstevel@tonic-gate  * drivers don't support polled I/O interfaces, we couldn't group the keyboard
470Sstevel@tonic-gate  * of this kind under conskbd. So we do as the follows:
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
500Sstevel@tonic-gate  *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
510Sstevel@tonic-gate  *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
520Sstevel@tonic-gate  *     requesting to be linked under conskbd. If the lower driver does't
530Sstevel@tonic-gate  *     recognize this ioctl, the virtual keyboard will be disabled so that
540Sstevel@tonic-gate  *     only one keyboard instance could be linked under conskbd.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/types.h>
590Sstevel@tonic-gate #include <sys/param.h>
600Sstevel@tonic-gate #include <sys/stropts.h>
610Sstevel@tonic-gate #include <sys/stream.h>
620Sstevel@tonic-gate #include <sys/strsubr.h>
630Sstevel@tonic-gate #include <sys/strsun.h>
640Sstevel@tonic-gate #include <sys/conf.h>
650Sstevel@tonic-gate #include <sys/stat.h>
660Sstevel@tonic-gate #include <sys/errno.h>
670Sstevel@tonic-gate #include <sys/modctl.h>
680Sstevel@tonic-gate #include <sys/kbio.h>
690Sstevel@tonic-gate #include <sys/ddi.h>
700Sstevel@tonic-gate #include <sys/sunddi.h>
710Sstevel@tonic-gate #include <sys/consdev.h>
720Sstevel@tonic-gate #include <sys/note.h>
730Sstevel@tonic-gate #include <sys/kmem.h>
740Sstevel@tonic-gate #include <sys/kstat.h>
750Sstevel@tonic-gate #include <sys/policy.h>
760Sstevel@tonic-gate #include <sys/kbd.h>
770Sstevel@tonic-gate #include <sys/kbtrans.h>
780Sstevel@tonic-gate #include <sys/promif.h>
790Sstevel@tonic-gate #include <sys/vuid_event.h>
800Sstevel@tonic-gate #include <sys/conskbd.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate extern struct keyboard *kbtrans_usbkb_maptab_init(void);
830Sstevel@tonic-gate extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
840Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Module linkage routines for the kernel
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
900Sstevel@tonic-gate static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
910Sstevel@tonic-gate static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * STREAMS queue processing procedures
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate static void	conskbduwsrv(queue_t *);
970Sstevel@tonic-gate static void	conskbdlwserv(queue_t *);
980Sstevel@tonic-gate static void	conskbdlrput(queue_t *, mblk_t *);
990Sstevel@tonic-gate static void	conskbdioctl(queue_t *, mblk_t *);
1000Sstevel@tonic-gate static int	conskbdclose(queue_t *, int, cred_t *);
1010Sstevel@tonic-gate static int	conskbdopen(queue_t *, dev_t *, int, int, cred_t *);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /* STREAMS driver id and limit value struct */
1050Sstevel@tonic-gate static struct module_info conskbdm_info = {
1060Sstevel@tonic-gate 	0,		/* mi_idnum */
1070Sstevel@tonic-gate 	"conskbd",	/* mi_idname */
1080Sstevel@tonic-gate 	0,		/* mi_minpsz */
1090Sstevel@tonic-gate 	1024,		/* mi_maxpsz */
1100Sstevel@tonic-gate 	2048,		/* mi_hiwat */
1110Sstevel@tonic-gate 	128		/* mi_lowat */
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * STREAMS queue processing procedure structures
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate /* upper read queue processing procedure structures */
1180Sstevel@tonic-gate static struct qinit conskbdurinit = {
1190Sstevel@tonic-gate 	NULL,			/* qi_putp */
1200Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_srvp */
1210Sstevel@tonic-gate 	conskbdopen,		/* qi_qopen */
1220Sstevel@tonic-gate 	conskbdclose,		/* qi_qclose */
1230Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_qadmin */
1240Sstevel@tonic-gate 	&conskbdm_info,		/* qi_minfo */
1250Sstevel@tonic-gate 	NULL			/* qi_mstat */
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /* upper write queue processing procedures structuresi */
1290Sstevel@tonic-gate static struct qinit conskbduwinit = {
1300Sstevel@tonic-gate 	(int (*)())putq,		/* qi_putp */
1310Sstevel@tonic-gate 	(int (*)())conskbduwsrv,	/* qi_srvp */
1320Sstevel@tonic-gate 	conskbdopen,			/* qi_qopen */
1330Sstevel@tonic-gate 	conskbdclose,			/* qi_qclose */
1340Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1350Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1360Sstevel@tonic-gate 	NULL				/* qi_mstat */
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* lower read queue processing procedures structures */
1400Sstevel@tonic-gate static struct qinit conskbdlrinit = {
1410Sstevel@tonic-gate 	(int (*)())conskbdlrput,	/* qi_putp */
1420Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_srvp */
1430Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1440Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1450Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1460Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1470Sstevel@tonic-gate 	NULL				/* qi_mstat */
1480Sstevel@tonic-gate };
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /* lower write processing procedures structures */
1510Sstevel@tonic-gate static struct qinit conskbdlwinit = {
1520Sstevel@tonic-gate 	putq,				/* qi_putp */
1530Sstevel@tonic-gate 	(int (*)())conskbdlwserv,	/* qi_srvp */
1540Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1550Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1560Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1570Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1580Sstevel@tonic-gate 	NULL				/* qi_mstat */
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /* STREAMS entity declaration structure */
1620Sstevel@tonic-gate static struct streamtab conskbd_str_info = {
1630Sstevel@tonic-gate 	&conskbdurinit,		/* st_rdinit */
1640Sstevel@tonic-gate 	&conskbduwinit,		/* st_wrinit */
1650Sstevel@tonic-gate 	&conskbdlrinit,		/* st_muxrinit */
1660Sstevel@tonic-gate 	&conskbdlwinit,		/* st_muxwinit */
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /* Entry points structure */
1710Sstevel@tonic-gate static 	struct cb_ops cb_conskbd_ops = {
1720Sstevel@tonic-gate 	nulldev,		/* cb_open */
1730Sstevel@tonic-gate 	nulldev,		/* cb_close */
1740Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1750Sstevel@tonic-gate 	nodev,			/* cb_print */
1760Sstevel@tonic-gate 	nodev,			/* cb_dump */
1770Sstevel@tonic-gate 	nodev,			/* cb_read */
1780Sstevel@tonic-gate 	nodev,			/* cb_write */
1790Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
1800Sstevel@tonic-gate 	nodev,			/* cb_devmap */
1810Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1820Sstevel@tonic-gate 	nodev,			/* cb_segmap */
1830Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1840Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1850Sstevel@tonic-gate 	&conskbd_str_info,	/* cb_stream */
1860Sstevel@tonic-gate 	D_MP | D_MTOUTPERIM	/* cb_flag */
1870Sstevel@tonic-gate };
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * Device operations structure
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate static struct dev_ops conskbd_ops = {
1940Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1950Sstevel@tonic-gate 	0,			/* devo_refcnt */
1960Sstevel@tonic-gate 	conskbd_info,		/* devo_getinfo */
1970Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1980Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1990Sstevel@tonic-gate 	conskbd_attach,		/* devo_attach */
2000Sstevel@tonic-gate 	conskbd_detach,		/* devo_detach */
2010Sstevel@tonic-gate 	nodev,			/* devo_reset */
2020Sstevel@tonic-gate 	&(cb_conskbd_ops),	/* devo_cb_ops */
2030Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2040Sstevel@tonic-gate 	NULL			/* devo_power */
2050Sstevel@tonic-gate };
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * Module linkage information for the kernel.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate static struct modldrv modldrv = {
2110Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
2120Sstevel@tonic-gate 	"Console kbd Multiplexer driver 'conskbd' %I%",
2130Sstevel@tonic-gate 	&conskbd_ops,	/* driver ops */
2140Sstevel@tonic-gate };
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * Module linkage structure
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate static struct modlinkage modlinkage = {
2200Sstevel@tonic-gate 	MODREV_1,	/* ml_rev */
2210Sstevel@tonic-gate 	&modldrv,	/* ml_linkage */
2220Sstevel@tonic-gate 	NULL		/* NULL terminates the list */
2230Sstevel@tonic-gate };
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * Debug printing
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate #ifndef DPRINTF
2290Sstevel@tonic-gate #ifdef DEBUG
2300Sstevel@tonic-gate void	conskbd_dprintf(const char *fmt, ...);
2310Sstevel@tonic-gate #define	DPRINTF(l, m, args) \
2320Sstevel@tonic-gate 	(((l) >= conskbd_errlevel) && ((m) & conskbd_errmask) ?	\
2330Sstevel@tonic-gate 		conskbd_dprintf args :				\
2340Sstevel@tonic-gate 		(void) 0)
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * Severity levels for printing
2380Sstevel@tonic-gate  */
2390Sstevel@tonic-gate #define	PRINT_L0	0	/* print every message */
2400Sstevel@tonic-gate #define	PRINT_L1	1	/* debug */
2410Sstevel@tonic-gate #define	PRINT_L2	2	/* quiet */
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Masks
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate #define	PRINT_MASK_ALL		0xFFFFFFFFU
2470Sstevel@tonic-gate uint_t	conskbd_errmask = PRINT_MASK_ALL;
2480Sstevel@tonic-gate uint_t	conskbd_errlevel = PRINT_L2;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate #else
2510Sstevel@tonic-gate #define	DPRINTF(l, m, args)	/* NOTHING */
2520Sstevel@tonic-gate #endif
2530Sstevel@tonic-gate #endif
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate  * Module global data are protected by the per-module inner perimeter
2570Sstevel@tonic-gate  */
2580Sstevel@tonic-gate static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
2590Sstevel@tonic-gate static	queue_t	*conskbd_consqueue; /* console queue above us */
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
2630Sstevel@tonic-gate static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
2640Sstevel@tonic-gate static struct keyboard *conskbd_keyindex;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
2680Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
2690Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
2700Sstevel@tonic-gate  * will be easier to add new statistics later.
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate static struct {
2730Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last keystroke */
2740Sstevel@tonic-gate } conskbd_kstat = {
2750Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
2760Sstevel@tonic-gate };
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Local routines prototypes
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate static int conskbd_kstat_update(kstat_t *, int);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate static void conskbd_ioc_plink(queue_t *, mblk_t *);
2840Sstevel@tonic-gate static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
2850Sstevel@tonic-gate static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
2861274Sqz150045 static mblk_t *conskbd_alloc_firm_event(int, int);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
2890Sstevel@tonic-gate static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
2900Sstevel@tonic-gate static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
2910Sstevel@tonic-gate static void conskbd_link_lower_queue(conskbd_lower_queue_t *);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
2940Sstevel@tonic-gate static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
2950Sstevel@tonic-gate static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
2960Sstevel@tonic-gate static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
2970Sstevel@tonic-gate static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
2980Sstevel@tonic-gate static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
2990Sstevel@tonic-gate static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3000Sstevel@tonic-gate static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);
3010Sstevel@tonic-gate 
302*1762Slt200341 static void conskbd_polledio_enter(cons_polledio_arg_t);
303*1762Slt200341 static void conskbd_polledio_exit(cons_polledio_arg_t);
304*1762Slt200341 static int  conskbd_polledio_ischar(cons_polledio_arg_t);
305*1762Slt200341 static int  conskbd_polledio_getchar(cons_polledio_arg_t);
3060Sstevel@tonic-gate static void conskbd_polledio_setled(struct kbtrans_hardware *, int);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate static void conskbd_streams_setled(struct kbtrans_hardware *, int);
3090Sstevel@tonic-gate static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
3100Sstevel@tonic-gate static boolean_t
3110Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *,
3120Sstevel@tonic-gate 		kbtrans_key_t *, enum keystate *);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Callbacks needed by kbtrans
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate static struct kbtrans_callbacks conskbd_callbacks = {
3180Sstevel@tonic-gate 	conskbd_streams_setled,
3190Sstevel@tonic-gate 	conskbd_polledio_setled,
3200Sstevel@tonic-gate 	conskbd_polled_keycheck,
3210Sstevel@tonic-gate };
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate  * Single private "global" lock for the few rare conditions
3250Sstevel@tonic-gate  * we want single-threaded.
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate static	kmutex_t	conskbd_lq_lock;
3280Sstevel@tonic-gate static	kmutex_t	conskbd_msgq_lock;
3290Sstevel@tonic-gate static	conskbd_pending_msg_t	*conskbd_msg_queue;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * The software state structure of virtual keyboard.
3330Sstevel@tonic-gate  * Currently, only one virtual keyboard is support.
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate static conskbd_state_t	conskbd = { 0 };
3360Sstevel@tonic-gate 
3371274Sqz150045 /* This variable backs up the layout state for non-self-ID keyboards */
3381274Sqz150045 static int kbd_layout_bak = 0;
3391274Sqz150045 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * _init()
3420Sstevel@tonic-gate  *
3430Sstevel@tonic-gate  * Description:
3440Sstevel@tonic-gate  *      Driver initialization, called when driver is first loaded.
3450Sstevel@tonic-gate  *      This is how access is initially given to all the static structures.
3460Sstevel@tonic-gate  *
3470Sstevel@tonic-gate  * Arguments:
3480Sstevel@tonic-gate  *      None
3490Sstevel@tonic-gate  *
3500Sstevel@tonic-gate  * Returns:
3510Sstevel@tonic-gate  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
3520Sstevel@tonic-gate  *      mod_install() status, see mod_install(9f)
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate int
3550Sstevel@tonic-gate _init(void)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	int	error;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3600Sstevel@tonic-gate 	if (error != 0) {
3610Sstevel@tonic-gate 		return (error);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	conskbd_keyindex = kbtrans_usbkb_maptab_init();
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	mutex_init(&conskbd_lq_lock, NULL, MUTEX_DRIVER, NULL);
3670Sstevel@tonic-gate 	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	return (error);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate }	/* _init() */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /*
3740Sstevel@tonic-gate  * _fini()
3750Sstevel@tonic-gate  *
3760Sstevel@tonic-gate  * Description:
3770Sstevel@tonic-gate  *      Module de-initialization, called when the driver is to be unloaded.
3780Sstevel@tonic-gate  *
3790Sstevel@tonic-gate  * Arguments:
3800Sstevel@tonic-gate  *      None
3810Sstevel@tonic-gate  *
3820Sstevel@tonic-gate  * Returns:
3830Sstevel@tonic-gate  *      mod_remove() status, see mod_remove(9f)
3840Sstevel@tonic-gate  */
3850Sstevel@tonic-gate int
3860Sstevel@tonic-gate _fini(void)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	int	error;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3910Sstevel@tonic-gate 	if (error != 0)
3920Sstevel@tonic-gate 		return (error);
3930Sstevel@tonic-gate 	mutex_destroy(&conskbd_lq_lock);
3940Sstevel@tonic-gate 	mutex_destroy(&conskbd_msgq_lock);
3950Sstevel@tonic-gate 	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	return (0);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate }	/* _fini() */
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate  * _info()
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * Description:
4050Sstevel@tonic-gate  *      Module information, returns information about the driver.
4060Sstevel@tonic-gate  *
4070Sstevel@tonic-gate  * Arguments:
4080Sstevel@tonic-gate  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * Returns:
4110Sstevel@tonic-gate  *      mod_info() status, see mod_info(9f)
4120Sstevel@tonic-gate  */
4130Sstevel@tonic-gate int
4140Sstevel@tonic-gate _info(struct modinfo *modinfop)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate }	/* _info() */
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * conskbd_attach()
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * Description:
4250Sstevel@tonic-gate  * 	This routine creates two device nodes. One is the "kbd" node, which
4260Sstevel@tonic-gate  * is used by user application programs(such as Xserver).The other is the
4270Sstevel@tonic-gate  * "conskbd" node, which is an internal node. consconfig_dacf module will
4280Sstevel@tonic-gate  * open this internal node, and link the conskbd under the wc (workstaion
4290Sstevel@tonic-gate  * console).
4300Sstevel@tonic-gate  *
4310Sstevel@tonic-gate  * Arguments:
4320Sstevel@tonic-gate  *      dev_info_t      *dip    Pointer to the device's dev_info struct
4330Sstevel@tonic-gate  *      ddi_attach_cmd_t cmd    Attach command
4340Sstevel@tonic-gate  *
4350Sstevel@tonic-gate  * Returns:
4360Sstevel@tonic-gate  *      DDI_SUCCESS             The driver was initialized properly
4370Sstevel@tonic-gate  *      DDI_FAILURE             The driver couldn't be initialized properly
4380Sstevel@tonic-gate  */
4390Sstevel@tonic-gate /*ARGSUSED*/
4400Sstevel@tonic-gate static int
4410Sstevel@tonic-gate conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	kstat_t	*ksp;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	switch (cmd) {
4460Sstevel@tonic-gate 	case DDI_ATTACH:
4470Sstevel@tonic-gate 		break;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	default:
4500Sstevel@tonic-gate 		return (DDI_FAILURE);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
4540Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) ||
4550Sstevel@tonic-gate 	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
4560Sstevel@tonic-gate 	    1) == DDI_FAILURE)) {
4570Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4580Sstevel@tonic-gate 		return (DDI_FAILURE);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 	conskbd_dip = devi;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
4630Sstevel@tonic-gate 	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
4640Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
4650Sstevel@tonic-gate 	if (ksp) {
4660Sstevel@tonic-gate 		ksp->ks_data = (void *) &conskbd_kstat;
4670Sstevel@tonic-gate 		ksp->ks_update = conskbd_kstat_update;
4680Sstevel@tonic-gate 		kstat_install(ksp);
4690Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	conskbd.conskbd_layout = -1;	/* invalid layout */
4730Sstevel@tonic-gate 	conskbd.conskbd_led_state = -1;
4740Sstevel@tonic-gate 	conskbd.conskbd_bypassed = B_FALSE;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	return (DDI_SUCCESS);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate }	/* conskbd_attach() */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * conskbd_detach()
4820Sstevel@tonic-gate  *
4830Sstevel@tonic-gate  * Description:
4840Sstevel@tonic-gate  *      Detach an instance of the conskbd driver. In fact, the driver can not
4850Sstevel@tonic-gate  * be detached.
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  * Arguments:
4880Sstevel@tonic-gate  *      dev_info_t              *dip    Pointer to the device's dev_info struct
4890Sstevel@tonic-gate  *      ddi_detach_cmd_t        cmd     Detach command
4900Sstevel@tonic-gate  *
4910Sstevel@tonic-gate  * Returns:
4920Sstevel@tonic-gate  *      DDI_SUCCESS     The driver was detached
4930Sstevel@tonic-gate  *      DDI_FAILURE     The driver couldn't be detached
4940Sstevel@tonic-gate  */
4950Sstevel@tonic-gate /*ARGSUSED*/
4960Sstevel@tonic-gate static int
4970Sstevel@tonic-gate conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	return (DDI_FAILURE);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate }	/* conskbd_detach() */
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /* ARGSUSED */
5040Sstevel@tonic-gate static int
5050Sstevel@tonic-gate conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
5060Sstevel@tonic-gate 	void **result)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	register int error;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	switch (infocmd) {
5110Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5120Sstevel@tonic-gate 		if (conskbd_dip == NULL) {
5130Sstevel@tonic-gate 			error = DDI_FAILURE;
5140Sstevel@tonic-gate 		} else {
5150Sstevel@tonic-gate 			*result = (void *) conskbd_dip;
5160Sstevel@tonic-gate 			error = DDI_SUCCESS;
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 		break;
5190Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5200Sstevel@tonic-gate 		*result = (void *)0;
5210Sstevel@tonic-gate 		error = DDI_SUCCESS;
5220Sstevel@tonic-gate 		break;
5230Sstevel@tonic-gate 	default:
5240Sstevel@tonic-gate 		error = DDI_FAILURE;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	return (error);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate }	/* conskbd_info() */
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*ARGSUSED*/
5310Sstevel@tonic-gate static int
5320Sstevel@tonic-gate conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	dev_t	unit;
5350Sstevel@tonic-gate 	int	err;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	unit = getminor(*devp);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	if (unit == 0) {
5400Sstevel@tonic-gate 		/*
5410Sstevel@tonic-gate 		 * Opening "/dev/kbd".
5420Sstevel@tonic-gate 		 */
5430Sstevel@tonic-gate 		conskbd_regqueue = q;
5440Sstevel@tonic-gate 		qprocson(q);
5450Sstevel@tonic-gate 		return (0);
5460Sstevel@tonic-gate 	} else if (unit != 1) {
5470Sstevel@tonic-gate 		/* we don't do that under Bozo's Big Tent */
5480Sstevel@tonic-gate 		return (ENODEV);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/*
5520Sstevel@tonic-gate 	 * Opening the device to be linked under the console.
5530Sstevel@tonic-gate 	 */
5540Sstevel@tonic-gate 	conskbd_consqueue = q;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/*
5571097Slq150181 	 * initialize kbtrans module for conskbd
5580Sstevel@tonic-gate 	 */
5590Sstevel@tonic-gate 	err = kbtrans_streams_init(q, sflag, crp, (struct kbtrans_hardware *)
5600Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
5610Sstevel@tonic-gate 	if (err != 0)
5620Sstevel@tonic-gate 		return (err);
5630Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
5640Sstevel@tonic-gate 	    conskbd_keyindex);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
5670Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
568*1762Slt200341 	    (cons_polledio_arg_t)&conskbd;
5690Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
5700Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
571*1762Slt200341 	    (int (*)(cons_polledio_arg_t)) conskbd_polledio_getchar;
5720Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
573*1762Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))conskbd_polledio_ischar;
5740Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
5750Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
5760Sstevel@tonic-gate 	qprocson(q);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	return (0);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate }	/* conskbd_open() */
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate /*ARGSUSED*/
5840Sstevel@tonic-gate static int
5850Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
5900Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
5930Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
5940Sstevel@tonic-gate 		    conskbd_consqueue);
5950Sstevel@tonic-gate 		qprocsoff(q);
5960Sstevel@tonic-gate 		conskbd_regqueue = NULL;
5970Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
5980Sstevel@tonic-gate 		/*
5990Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
6000Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 		qprocsoff(q);
6030Sstevel@tonic-gate 		conskbd_consqueue = NULL;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	return (0);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate }	/* conskbd_close() */
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate /*
6110Sstevel@tonic-gate  * Service procedure for upper write queue.
6120Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
6130Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
6140Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
6150Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
6160Sstevel@tonic-gate  * deal with all messages.
6170Sstevel@tonic-gate  * 	This routine is invoked when ioctl commands are send down
6180Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
6190Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
6200Sstevel@tonic-gate  * the led states.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate static void
6230Sstevel@tonic-gate conskbduwsrv(queue_t *q)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate 	mblk_t	*mp;
6260Sstevel@tonic-gate 	queue_t	*oldq;
6270Sstevel@tonic-gate 	enum kbtrans_message_response ret;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		/*
6320Sstevel@tonic-gate 		 * if the virtual keyboard is supported
6330Sstevel@tonic-gate 		 */
6340Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
6370Sstevel@tonic-gate 				continue;
6380Sstevel@tonic-gate 			/*
6390Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
6400Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
6410Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
6420Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
6430Sstevel@tonic-gate 			 * and regular queue.
6440Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
6450Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
6460Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
6470Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
6480Sstevel@tonic-gate 			 * to be switched between regular queue and console
6490Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
6500Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
6510Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
6520Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
6530Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
6540Sstevel@tonic-gate 			 * need to return response to right queue.
6550Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
6560Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
6570Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
6580Sstevel@tonic-gate 			 * and then switch back to old queue.
6590Sstevel@tonic-gate 			 */
6600Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
6610Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
6620Sstevel@tonic-gate 			kbtrans_streams_set_queue(
6630Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
6640Sstevel@tonic-gate 			ret = kbtrans_streams_message(
6650Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
6660Sstevel@tonic-gate 			kbtrans_streams_set_queue(
6670Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 			switch (ret) {
6700Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
6710Sstevel@tonic-gate 					continue;
6720Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
6730Sstevel@tonic-gate 					break;
6740Sstevel@tonic-gate 			}
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		case M_IOCTL:
6800Sstevel@tonic-gate 			conskbdioctl(q, mp);
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		case M_FLUSH:
6840Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
6850Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
6860Sstevel@tonic-gate 			}
6870Sstevel@tonic-gate 			/*
6880Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
6890Sstevel@tonic-gate 			 * may be lost so that upper module or applications
6900Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
6910Sstevel@tonic-gate 			 * ever.
6920Sstevel@tonic-gate 			 */
6930Sstevel@tonic-gate 			freemsg(mp);
6940Sstevel@tonic-gate 			break;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		case M_DATA:
6970Sstevel@tonic-gate 			/*
6980Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
6990Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
7000Sstevel@tonic-gate 			 * down to lower queue.
7010Sstevel@tonic-gate 			 */
7020Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
7030Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
7040Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
7050Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
7060Sstevel@tonic-gate 					freemsg(mp);
7070Sstevel@tonic-gate 			} else {
7080Sstevel@tonic-gate 				freemsg(mp);
7090Sstevel@tonic-gate 			}
7100Sstevel@tonic-gate 			break;
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		default:
7130Sstevel@tonic-gate 			/*
7140Sstevel@tonic-gate 			 * Pass an error message up.
7150Sstevel@tonic-gate 			 */
7160Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
7170Sstevel@tonic-gate 			if (mp->b_cont) {
7180Sstevel@tonic-gate 				freemsg(mp->b_cont);
7190Sstevel@tonic-gate 				mp->b_cont = NULL;
7200Sstevel@tonic-gate 			}
7210Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
7220Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
7230Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
7240Sstevel@tonic-gate 			qreply(q, mp);
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 	}	/* end of while */
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate }	/* conskbduwsrv() */
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate static void
7310Sstevel@tonic-gate conskbdioctl(queue_t *q, mblk_t *mp)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	conskbd_lower_queue_t		*prev;
7340Sstevel@tonic-gate 	conskbd_lower_queue_t		*lqs;
7350Sstevel@tonic-gate 	struct	iocblk			*iocp;
7360Sstevel@tonic-gate 	struct	linkblk			*linkp;
7370Sstevel@tonic-gate 	int	index;
7380Sstevel@tonic-gate 	int	error = 0;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	case I_LINK:
7450Sstevel@tonic-gate 	case I_PLINK:
746267Scg149915 		if (conskbd.conskbd_bypassed == B_TRUE) {
747267Scg149915 		/*
748267Scg149915 		 * A legacy keyboard can NOT be connected to conskbd together
749267Scg149915 		 * with other keyboards. So when a legacy keyboard is already
750267Scg149915 		 * linked under conkbd, we just reject all others.
751267Scg149915 		 */
752267Scg149915 			miocnak(q, mp, 0, EAGAIN);
753267Scg149915 			break;
754267Scg149915 		}
755267Scg149915 
7560Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
7570Sstevel@tonic-gate 		conskbd_ioc_plink(q, mp);
7580Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
7590Sstevel@tonic-gate 		break;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	case I_UNLINK:
7620Sstevel@tonic-gate 	case I_PUNLINK:
7630Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
7640Sstevel@tonic-gate 		linkp = (struct linkblk *)mp->b_cont->b_rptr;
7650Sstevel@tonic-gate 		prev = conskbd.conskbd_lqueue_list;
7660Sstevel@tonic-gate 		for (lqs = prev; lqs; lqs = lqs->lqs_next) {
7670Sstevel@tonic-gate 			if (lqs->lqs_queue == linkp->l_qbot) {
7680Sstevel@tonic-gate 				if (prev == lqs)
7690Sstevel@tonic-gate 					conskbd.conskbd_lqueue_list =
7700Sstevel@tonic-gate 					    lqs->lqs_next;
7710Sstevel@tonic-gate 				else
7720Sstevel@tonic-gate 					prev->lqs_next = lqs->lqs_next;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 				lqs->lqs_queue->q_ptr =  NULL;
7750Sstevel@tonic-gate 				conskbd.conskbd_lqueue_nums --;
7761274Sqz150045 				if (conskbd.conskbd_lqueue_nums == 0) {
7771274Sqz150045 					kbd_layout_bak = conskbd.conskbd_layout;
7780Sstevel@tonic-gate 					conskbd.conskbd_layout = -1;
7791274Sqz150045 				}
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 				mutex_exit(&conskbd_lq_lock);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 				for (index = 0; index < KBTRANS_KEYNUMS_MAX;
7840Sstevel@tonic-gate 				    index ++) {
7850Sstevel@tonic-gate 					if (lqs->lqs_key_state[index] ==
7860Sstevel@tonic-gate 					    KEY_PRESSED)
7870Sstevel@tonic-gate 						kbtrans_streams_key(
7880Sstevel@tonic-gate 						    conskbd.conskbd_kbtrans,
7890Sstevel@tonic-gate 						    index,
7900Sstevel@tonic-gate 						    KEY_RELEASED);
7910Sstevel@tonic-gate 				}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 				kmem_free(lqs, sizeof (*lqs));
7940Sstevel@tonic-gate 				miocack(q, mp, 0, 0);
7950Sstevel@tonic-gate 				return;
7960Sstevel@tonic-gate 			}
7970Sstevel@tonic-gate 			prev = lqs;
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
8000Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
8010Sstevel@tonic-gate 		break;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	case KIOCSKABORTEN:
8040Sstevel@tonic-gate 		/*
8050Sstevel@tonic-gate 		 * Check if privileged
8060Sstevel@tonic-gate 		 */
8070Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
8080Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8090Sstevel@tonic-gate 			return;
8100Sstevel@tonic-gate 		}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8130Sstevel@tonic-gate 		if (error != 0) {
8140Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8150Sstevel@tonic-gate 			return;
8160Sstevel@tonic-gate 		}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
8190Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8200Sstevel@tonic-gate 		break;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	default:
8230Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
8240Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
8250Sstevel@tonic-gate 		} else {
8260Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate }	/* conskbdioctl() */
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate static void
8340Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	struct iocblk		*iocp;
8370Sstevel@tonic-gate 	mblk_t			*datap;
8380Sstevel@tonic-gate 	int			cmd;
8390Sstevel@tonic-gate 	int			error = 0;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8440Sstevel@tonic-gate 	case KIOCLAYOUT:
8450Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
8460Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
8470Sstevel@tonic-gate 			break;
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
8510Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
8520Sstevel@tonic-gate 		else
8530Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
8560Sstevel@tonic-gate 		if (mp->b_cont)
8570Sstevel@tonic-gate 			freemsg(mp->b_cont);
8580Sstevel@tonic-gate 		mp->b_cont = datap;
8590Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
8600Sstevel@tonic-gate 		break;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	case KIOCSLAYOUT:
8630Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8640Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8650Sstevel@tonic-gate 			break;
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
8680Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8690Sstevel@tonic-gate 		break;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
8720Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
8730Sstevel@tonic-gate 		if (error != 0) {
8740Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8750Sstevel@tonic-gate 			break;
8760Sstevel@tonic-gate 		}
8770Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
8780Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8790Sstevel@tonic-gate 			break;
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
8820Sstevel@tonic-gate 		break;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
8850Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
8860Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8870Sstevel@tonic-gate 			break;
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
8900Sstevel@tonic-gate 		break;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	case CONSSETABORTENABLE:
8930Sstevel@tonic-gate 		/*
8940Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
8950Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
8960Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
8970Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
8980Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
8990Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
9000Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
9010Sstevel@tonic-gate 		 *
9020Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
9030Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
9040Sstevel@tonic-gate 		 * already handle the message though it returned to us a
9050Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
9060Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
9070Sstevel@tonic-gate 		 * So we just return ACK to upper module.
9080Sstevel@tonic-gate 		 */
9090Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9100Sstevel@tonic-gate 		break;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	case KIOCCMD:
9130Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
9140Sstevel@tonic-gate 		    mp->b_cont == NULL) {
9150Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9160Sstevel@tonic-gate 			break;
9170Sstevel@tonic-gate 		}
9180Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
9190Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
9200Sstevel@tonic-gate 			freemsg(mp->b_cont);
9210Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
9220Sstevel@tonic-gate 			if (datap == NULL) {
9230Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9240Sstevel@tonic-gate 				return;
9250Sstevel@tonic-gate 			}
9260Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
9270Sstevel@tonic-gate 				*(int *)datap->b_wptr =
9280Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
9290Sstevel@tonic-gate 			else
9300Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 			mp->b_cont = datap;
9330Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
9340Sstevel@tonic-gate 			return;
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9380Sstevel@tonic-gate 		break;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	default:
9410Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
9420Sstevel@tonic-gate 		break;
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate static void
9480Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
9510Sstevel@tonic-gate 	struct	iocblk		*iocp;
9520Sstevel@tonic-gate 	int	error = 0;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
9570Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	case KIOCGDIRECT: {
9600Sstevel@tonic-gate 		mblk_t *datap;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
9630Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
9640Sstevel@tonic-gate 			break;
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
9680Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9690Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
9700Sstevel@tonic-gate 			freemsg(mp->b_cont);
9710Sstevel@tonic-gate 			mp->b_cont = NULL;
9720Sstevel@tonic-gate 		}
9730Sstevel@tonic-gate 		mp->b_cont = datap;
9740Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
9750Sstevel@tonic-gate 		break;
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	case KIOCSDIRECT:
9790Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9800Sstevel@tonic-gate 		if (error != 0) {
9810Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9820Sstevel@tonic-gate 			break;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		/*
9870Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
9880Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
9890Sstevel@tonic-gate 		 * itself.
9900Sstevel@tonic-gate 		 */
9910Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
9920Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
9930Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
9940Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
9950Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9960Sstevel@tonic-gate 				return;
9970Sstevel@tonic-gate 			}
9980Sstevel@tonic-gate 			break;
9990Sstevel@tonic-gate 		}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
10020Sstevel@tonic-gate 		break;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	default:
10050Sstevel@tonic-gate 		/*
10060Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
10070Sstevel@tonic-gate 		 * through to; otherwise, reject it.
10080Sstevel@tonic-gate 		 */
10090Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10100Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10110Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10120Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10130Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10140Sstevel@tonic-gate 				return;
10150Sstevel@tonic-gate 			}
10160Sstevel@tonic-gate 			break;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 		/* nobody below us; reject it */
10200Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10210Sstevel@tonic-gate 		break;
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate  * Service procedure for lower write queue.
10290Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate static void
10320Sstevel@tonic-gate conskbdlwserv(queue_t *q)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	register mblk_t *mp;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
10370Sstevel@tonic-gate 		putnext(q, mp);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate }	/* conskbdlwserv() */
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate /*
10420Sstevel@tonic-gate  * Put procedure for lower read queue.
10430Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
10440Sstevel@tonic-gate  * device 1.
10450Sstevel@tonic-gate  */
10460Sstevel@tonic-gate static void
10470Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
10500Sstevel@tonic-gate 	struct iocblk 	*iocp;
10510Sstevel@tonic-gate 	Firm_event	*fe;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	case M_FLUSH:
10580Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
10590Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
10600Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
10610Sstevel@tonic-gate 		}
10620Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
10630Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
10640Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
10650Sstevel@tonic-gate 		} else
10660Sstevel@tonic-gate 			freemsg(mp);
10670Sstevel@tonic-gate 		break;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	case M_DATA:
10700Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 			/*
10750Sstevel@tonic-gate 			 * This is a workaround.
10760Sstevel@tonic-gate 			 *
10770Sstevel@tonic-gate 			 * According to HID specification, there are the
10780Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
10790Sstevel@tonic-gate 			 *
10800Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
10810Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
10820Sstevel@tonic-gate 			 *
10830Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
10840Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
10850Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
10860Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
10870Sstevel@tonic-gate 			 * not know which is being pressed or released when we
10880Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
10890Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
10900Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
10910Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
10920Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
10930Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
10940Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
10950Sstevel@tonic-gate 			 * the key to 49.
10960Sstevel@tonic-gate 			 */
10970Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
10980Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
10990Sstevel@tonic-gate 					fe->id = 49;
11000Sstevel@tonic-gate 				else
11010Sstevel@tonic-gate 					fe->id = 50;
11020Sstevel@tonic-gate 			}
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 			/*
11050Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
11060Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
11070Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
11080Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
11090Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
11100Sstevel@tonic-gate 			 * as held-down for ever.
11110Sstevel@tonic-gate 			 */
11120Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
11130Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
11140Sstevel@tonic-gate 			if (fe->value)
11150Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
11160Sstevel@tonic-gate 			else
11170Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
11200Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
11210Sstevel@tonic-gate 			freemsg(mp);
11220Sstevel@tonic-gate 		} else {
11230Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
11240Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
11250Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
11260Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
11270Sstevel@tonic-gate 			else
11280Sstevel@tonic-gate 				freemsg(mp);
11290Sstevel@tonic-gate 		}
11300Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
11310Sstevel@tonic-gate 		break;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	case M_IOCACK:
11340Sstevel@tonic-gate 	case M_IOCNAK:
11350Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
11360Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
11390Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
11420Sstevel@tonic-gate 		break;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	case M_ERROR:
11450Sstevel@tonic-gate 	case M_HANGUP:
11460Sstevel@tonic-gate 	default:
11470Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
11480Sstevel@tonic-gate 		break;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate }	/* conskbdlrput() */
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate /* ARGSUSED */
11550Sstevel@tonic-gate static int
11560Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
11590Sstevel@tonic-gate 		return (EACCES);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	return (0);
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate }	/* conskbd_kstat_update() */
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
11690Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
11700Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
11710Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
11720Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
11730Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
11740Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
11750Sstevel@tonic-gate  * request matching received responses by searching the global linked
11760Sstevel@tonic-gate  * list to find the request which has the same message ID of the
11770Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
11780Sstevel@tonic-gate  * give a response to upstreams based the following policy:
11790Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
11800Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
11810Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
11820Sstevel@tonic-gate  * the first response is sent to upstream.
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate static void
11850Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
11880Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
11890Sstevel@tonic-gate 	struct iocblk	*iocp;
11900Sstevel@tonic-gate 	mblk_t		*clonemp;
11910Sstevel@tonic-gate 	int		retry;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
11940Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
11950Sstevel@tonic-gate 		return;
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
11990Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
12000Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
12010Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
12020Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
12050Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
12060Sstevel@tonic-gate 	    iocp->ioc_cmd == KIOCCMD);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
12090Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
12100Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
12110Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
12120Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
12130Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		/*
12180Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
12190Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
12200Sstevel@tonic-gate 		 * otherwise, system will panic.
12210Sstevel@tonic-gate 		 */
12220Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
12230Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
12240Sstevel@tonic-gate 			lqs = lqs->lqs_next;
12250Sstevel@tonic-gate 			msg->kpm_req_nums --;
12260Sstevel@tonic-gate 			retry = 0;
12270Sstevel@tonic-gate 			continue;
12280Sstevel@tonic-gate 		}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 		clonemp = copymsg(mp);
12310Sstevel@tonic-gate 		if (clonemp != NULL) {
12320Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
12330Sstevel@tonic-gate 				lqs = lqs->lqs_next;
12340Sstevel@tonic-gate 				retry = 0;
12350Sstevel@tonic-gate 				continue;
12360Sstevel@tonic-gate 			}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 			/*
12390Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
12400Sstevel@tonic-gate 			 */
12410Sstevel@tonic-gate 			freemsg(clonemp);
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 		/*
12450Sstevel@tonic-gate 		 * During testing it was observed that occasionally
12460Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
12470Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
12480Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
12490Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
12500Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
12510Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
12520Sstevel@tonic-gate 		 *
12530Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
12540Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
12550Sstevel@tonic-gate 		 * or behave weirdly,
12560Sstevel@tonic-gate 		 *
12570Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
12580Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
12590Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
12600Sstevel@tonic-gate 		 *	entering kmdb;
12610Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
12620Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
12630Sstevel@tonic-gate 		 *	is not available in normal mode once returning
12640Sstevel@tonic-gate 		 *	from kmdb;
12650Sstevel@tonic-gate 		 * 3) for KIOCCMD
12660Sstevel@tonic-gate 		 * 	3.1) for KBD_CMD_NOBELL
12670Sstevel@tonic-gate 		 * 		there's no beep in USB and PS2 keyboard,
12680Sstevel@tonic-gate 		 * 		this ioctl actually disables the beep on
12690Sstevel@tonic-gate 		 * 		system mainboard. Note that all the cloned
12700Sstevel@tonic-gate 		 * 		messages sent down to lower queues do the
12710Sstevel@tonic-gate 		 * 		same job for system mainboard. Therefore,
12720Sstevel@tonic-gate 		 * 		even if we fail to send this ioctl to most
12730Sstevel@tonic-gate 		 * 		of lower queues, the beep still would be
12740Sstevel@tonic-gate 		 * 		disabled. So, no trouble exists here.
12750Sstevel@tonic-gate 		 *	3.2) for others
12760Sstevel@tonic-gate 		 *		nothing;
12770Sstevel@tonic-gate 		 *
12780Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
12790Sstevel@tonic-gate 		 * same request comes again.
12800Sstevel@tonic-gate 		 */
12810Sstevel@tonic-gate 		if (retry ++ >= 5) {
12820Sstevel@tonic-gate 			dev_t	devt;
12830Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
12860Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
12870Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
12880Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
12890Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
12900Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
12910Sstevel@tonic-gate 					    "keyboard is not available"
12920Sstevel@tonic-gate 					    " for system debugging: %s",
12930Sstevel@tonic-gate 					    path);
12940Sstevel@tonic-gate 				break;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
12970Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
12980Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
12990Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13000Sstevel@tonic-gate 					    "keyboard is not available:"
13010Sstevel@tonic-gate 					    " %s", path);
13020Sstevel@tonic-gate 				break;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 			default:
13050Sstevel@tonic-gate 				break;
13060Sstevel@tonic-gate 			}
13070Sstevel@tonic-gate 			msg->kpm_req_nums --;
13080Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13090Sstevel@tonic-gate 			retry = 0;
13100Sstevel@tonic-gate 		}
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
13140Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
13150Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
13160Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate static void
13230Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
13240Sstevel@tonic-gate {
13250Sstevel@tonic-gate 	mblk_t		*req;
13260Sstevel@tonic-gate 	queue_t		*lowque;
13270Sstevel@tonic-gate 	struct iocblk		*iocp;
13280Sstevel@tonic-gate 	struct linkblk		*linkp;
13290Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	ASSERT(mutex_owned(&conskbd_lq_lock));
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
13340Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
13370Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
13380Sstevel@tonic-gate 	lowque = linkp->l_qbot;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	lowque->q_ptr = (void *)lqs;
13410Sstevel@tonic-gate 	OTHERQ(lowque)->q_ptr = (void *)lqs;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
13440Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
13450Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
13480Sstevel@tonic-gate 	if (req == NULL) {
13490Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13500Sstevel@tonic-gate 		lowque->q_ptr = NULL;
13510Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13520Sstevel@tonic-gate 		return;
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
13560Sstevel@tonic-gate 	if (req->b_cont == NULL) {
13570Sstevel@tonic-gate 		freemsg(req);
13580Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13590Sstevel@tonic-gate 		lowque->q_ptr = NULL;
13600Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13610Sstevel@tonic-gate 		return;
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	iocp->ioc_count = 0;
13650Sstevel@tonic-gate 	iocp->ioc_rval = 0;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
13680Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
13730Sstevel@tonic-gate 		freemsg(req);
13740Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
13750Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
13760Sstevel@tonic-gate 		lowque->q_ptr = NULL;
13770Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
13850Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
13860Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
13870Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
13880Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
13890Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
13900Sstevel@tonic-gate  */
13910Sstevel@tonic-gate static void
13920Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
13930Sstevel@tonic-gate {
13940Sstevel@tonic-gate 	switch (lqs->lqs_state) {
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
13970Sstevel@tonic-gate 	case LQS_INITIALIZED:
13980Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
13990Sstevel@tonic-gate 		break;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
14020Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
14030Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
14040Sstevel@tonic-gate 		break;
14050Sstevel@tonic-gate 
14061272Slq150181 	/* S4: wait lower queue to acknowledge KIOCSLED/KIOCGLED  message */
14070Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
14080Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
14090Sstevel@tonic-gate 		break;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
14120Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
14130Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
14140Sstevel@tonic-gate 		break;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
14170Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
14180Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
14190Sstevel@tonic-gate 		break;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
14220Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
14230Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
14240Sstevel@tonic-gate 		break;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	/* if reaching here, there must be a error */
14270Sstevel@tonic-gate 	default:
14280Sstevel@tonic-gate 		freemsg(mp);
14290Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
14300Sstevel@tonic-gate 		break;
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate static void
14370Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	struct iocblk	*iocp;
14400Sstevel@tonic-gate 	mblk_t		*msg;
14410Sstevel@tonic-gate 	mblk_t		*req;
14420Sstevel@tonic-gate 	queue_t		*lowerque;
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
14450Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
14500Sstevel@tonic-gate 	case M_IOCACK:
14510Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
14520Sstevel@tonic-gate 		if (req == NULL) {
14530Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
14540Sstevel@tonic-gate 			    0, ENOMEM);
14550Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
14560Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
14570Sstevel@tonic-gate 			freemsg(mp);
14580Sstevel@tonic-gate 			return;
14590Sstevel@tonic-gate 		}
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
14620Sstevel@tonic-gate 		if (req->b_cont == NULL) {
14630Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
14640Sstevel@tonic-gate 			    0, ENOMEM);
14650Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
14660Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
14670Sstevel@tonic-gate 			freemsg(req);
14680Sstevel@tonic-gate 			freemsg(mp);
14690Sstevel@tonic-gate 			return;
14700Sstevel@tonic-gate 		}
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
14730Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
14740Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
14770Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
14800Sstevel@tonic-gate 			freemsg(req);
14810Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue,
14820Sstevel@tonic-gate 			    lqs->lqs_pending_plink, 0, ENOMEM);
14830Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
14840Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
14850Sstevel@tonic-gate 		}
14860Sstevel@tonic-gate 		break;
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	case M_IOCNAK:
14890Sstevel@tonic-gate 		/*
14900Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
14910Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
14920Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
14930Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
14940Sstevel@tonic-gate 		 * will bypass the conskbd module.
14950Sstevel@tonic-gate 		 */
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 		/*
14980Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
14990Sstevel@tonic-gate 		 * conskbd, we reject the current one.
15000Sstevel@tonic-gate 		 */
15010Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
15020Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
15030Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15040Sstevel@tonic-gate 			    0, iocp->ioc_error);
15050Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
15060Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15070Sstevel@tonic-gate 			break;
15080Sstevel@tonic-gate 		}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 		/*
15110Sstevel@tonic-gate 		 * Bypass the virutal keyboard for old hardware
15120Sstevel@tonic-gate 		 */
15130Sstevel@tonic-gate 		conskbd.conskbd_bypassed = B_TRUE;
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 		msg = lqs->lqs_pending_plink;
15160Sstevel@tonic-gate 		msg->b_datap->db_type = M_IOCACK;
15170Sstevel@tonic-gate 		iocp = (struct iocblk *)msg->b_rptr;
15180Sstevel@tonic-gate 		iocp->ioc_error = 0;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		/*
15210Sstevel@tonic-gate 		 * link this keyboard under conskbd
15220Sstevel@tonic-gate 		 */
15230Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
15240Sstevel@tonic-gate 		lqs->lqs_next = conskbd.conskbd_lqueue_list;
15250Sstevel@tonic-gate 		conskbd.conskbd_lqueue_list = lqs;
15260Sstevel@tonic-gate 		conskbd.conskbd_lqueue_nums++;
15270Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		lqs->lqs_state = LQS_INITIALIZED_LEGACY;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 		qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
15320Sstevel@tonic-gate 		break;
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	freemsg(mp);
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate static void
15400Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15410Sstevel@tonic-gate {
15420Sstevel@tonic-gate 	struct iocblk 	*iocp;
15430Sstevel@tonic-gate 	mblk_t		*req;
15440Sstevel@tonic-gate 	queue_t		*lowerque;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
15470Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15520Sstevel@tonic-gate 	case M_IOCACK:
15530Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
15540Sstevel@tonic-gate 		if (req == NULL) {
15550Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15560Sstevel@tonic-gate 			    0, ENOMEM);
15570Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
15580Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15590Sstevel@tonic-gate 			freemsg(mp);
15600Sstevel@tonic-gate 			return;
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
15640Sstevel@tonic-gate 		if (req->b_cont == NULL) {
15650Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15660Sstevel@tonic-gate 			    0, ENOMEM);
15670Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15680Sstevel@tonic-gate 			freemsg(req);
15690Sstevel@tonic-gate 			freemsg(mp);
15700Sstevel@tonic-gate 			return;
15710Sstevel@tonic-gate 		}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
15740Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
15750Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
15760Sstevel@tonic-gate 			freemsg(req);
15770Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue,
15780Sstevel@tonic-gate 			    lqs->lqs_pending_plink, 0, ENOMEM);
15790Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
15800Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15810Sstevel@tonic-gate 		}
15820Sstevel@tonic-gate 		break;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	case M_IOCNAK:
15850Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
15860Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15870Sstevel@tonic-gate 		    0, iocp->ioc_error);
15880Sstevel@tonic-gate 		lowerque->q_ptr = NULL;
15890Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
15900Sstevel@tonic-gate 		break;
15910Sstevel@tonic-gate 	}
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	freemsg(mp);
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
15960Sstevel@tonic-gate 
15971274Sqz150045 /*
15981274Sqz150045  * Allocate a firm event
15991274Sqz150045  */
16001274Sqz150045 static mblk_t *
16011274Sqz150045 conskbd_alloc_firm_event(int id, int value)
16021274Sqz150045 {
16031274Sqz150045 	mblk_t	*mb;
16041274Sqz150045 	Firm_event *fe;
16051274Sqz150045 
16061274Sqz150045 	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
16071274Sqz150045 		fe = (Firm_event *)mb->b_wptr;
16081274Sqz150045 		fe->id = id;
16091274Sqz150045 		fe->pair_type = FE_PAIR_NONE;
16101274Sqz150045 		fe->pair = NULL;
16111274Sqz150045 		fe->value = value;
16121274Sqz150045 		mb->b_wptr += sizeof (Firm_event);
16131274Sqz150045 	}
16141274Sqz150045 
16151274Sqz150045 	return (mb);
16161274Sqz150045 }
16171274Sqz150045 
16180Sstevel@tonic-gate static void
16190Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16200Sstevel@tonic-gate {
16210Sstevel@tonic-gate 	mblk_t		*req;
16220Sstevel@tonic-gate 	int		layout;
16230Sstevel@tonic-gate 	boolean_t	fail;
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16260Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
16290Sstevel@tonic-gate 	case M_IOCACK:
16300Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
16310Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
16320Sstevel@tonic-gate 			/*
16330Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
16340Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
16350Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
16360Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
16370Sstevel@tonic-gate 			 */
16381274Sqz150045 			if (layout != -1 && conskbd.conskbd_layout == -1) {
16391274Sqz150045 				if (layout == 0) {
16401274Sqz150045 					conskbd.conskbd_layout = kbd_layout_bak;
16411274Sqz150045 				} else {
16421274Sqz150045 					conskbd.conskbd_layout = layout;
16431274Sqz150045 					if (layout == kbd_layout_bak) {
16441274Sqz150045 						break;
16451274Sqz150045 					}
16461274Sqz150045 					if ((req = conskbd_alloc_firm_event(
16471274Sqz150045 						KEYBOARD_LAYOUT_CHANGE,
16481274Sqz150045 						layout)) != NULL) {
16491274Sqz150045 						if (conskbd.conskbd_directio)
16501274Sqz150045 							putnext(
16511274Sqz150045 							    conskbd_regqueue,
16521274Sqz150045 							    req);
16531274Sqz150045 						else if (conskbd_consqueue
16541274Sqz150045 							    != NULL)
16551274Sqz150045 							putnext(
16561274Sqz150045 							    conskbd_consqueue,
16571274Sqz150045 							    req);
16581274Sqz150045 					}
16591274Sqz150045 				}
16601274Sqz150045 			}
16610Sstevel@tonic-gate 		}
16620Sstevel@tonic-gate 		break;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
16660Sstevel@tonic-gate 	case M_IOCNAK:
16670Sstevel@tonic-gate 		break;
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	freemsg(mp);
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	fail = B_TRUE;
16731272Slq150181 
16741272Slq150181 	if (conskbd.conskbd_led_state == -1)
16751272Slq150181 		req = mkiocb(KIOCGLED);
16761272Slq150181 	else
16771272Slq150181 		req = mkiocb(KIOCSLED);
16781272Slq150181 
16790Sstevel@tonic-gate 	if (req) {
16800Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
16810Sstevel@tonic-gate 		if (req->b_cont) {
16821272Slq150181 			if (conskbd.conskbd_led_state != -1) {
16831272Slq150181 				*(uchar_t *)req->b_cont->b_wptr =
16841272Slq150181 				    conskbd.conskbd_led_state;
16851272Slq150181 				req->b_cont->b_wptr += sizeof (uchar_t);
16861272Slq150181 			}
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
16890Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
16900Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
16910Sstevel@tonic-gate 				fail = B_FALSE;
16920Sstevel@tonic-gate 			} else {
16930Sstevel@tonic-gate 				freemsg(req);
16940Sstevel@tonic-gate 			}
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 		} else {
16970Sstevel@tonic-gate 			freemsg(req);
16980Sstevel@tonic-gate 		}
16990Sstevel@tonic-gate 	}
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	if (fail) {
17020Sstevel@tonic-gate 		/*
17031272Slq150181 		 * If fail to allocate KIOCSLED/KIOCGLED message or put
17041272Slq150181 		 * the message into lower queue, we immediately link
17051272Slq150181 		 * current keyboard under conskbd. Thus, even if fails
17061272Slq150181 		 * to set/get LED, this keyboard could be available.
17070Sstevel@tonic-gate 		 */
17080Sstevel@tonic-gate 		conskbd_link_lower_queue(lqs);
17090Sstevel@tonic-gate 	}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate static void
17150Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
17160Sstevel@tonic-gate {
17171272Slq150181 	int	led_state;
17181272Slq150181 
17190Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
17200Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
17210Sstevel@tonic-gate 
17221272Slq150181 	if (conskbd.conskbd_led_state == -1) {
17231272Slq150181 		switch (mp->b_datap->db_type) {
17241272Slq150181 		case M_IOCACK:
17251272Slq150181 			if (miocpullup(mp, sizeof (uchar_t)) == 0) {
17261272Slq150181 				led_state = *(uchar_t *)mp->b_cont->b_rptr;
17271272Slq150181 				conskbd.conskbd_led_state = led_state;
17281272Slq150181 				kbtrans_streams_setled(conskbd.conskbd_kbtrans,
17291272Slq150181 				    led_state);
17301272Slq150181 			}
17311272Slq150181 			break;
17321272Slq150181 
17331272Slq150181 		/* if fail, leave conskbd's led_state as it is */
17341272Slq150181 		case M_IOCNAK:
17351272Slq150181 			break;
17361272Slq150181 		}
17371272Slq150181 	}
17381272Slq150181 
17390Sstevel@tonic-gate 	/*
17401272Slq150181 	 * Basically, failure of setting/getting LED is not a fatal
17411272Slq150181 	 * error, so we will plumb the lower queue into conskbd whether
17421272Slq150181 	 * setting/getting LED succeeds or fails.
17430Sstevel@tonic-gate 	 */
17440Sstevel@tonic-gate 	freemsg(mp);
17450Sstevel@tonic-gate 	conskbd_link_lower_queue(lqs);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate static void
17510Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
17540Sstevel@tonic-gate 	struct iocblk		*iocp;
17550Sstevel@tonic-gate 	int			error;
17560Sstevel@tonic-gate 	dev_t			devt;
17570Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
17600Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (!msg) {
17630Sstevel@tonic-gate 		/*
17640Sstevel@tonic-gate 		 * Here, we just discard the responses to KIOCSLED request.
17650Sstevel@tonic-gate 		 * Please refer to conskbd_streams_setled().
17660Sstevel@tonic-gate 		 */
17670Sstevel@tonic-gate 		ASSERT(((struct iocblk *)mp->b_rptr)->ioc_cmd == KIOCSLED);
17680Sstevel@tonic-gate 		freemsg(mp);
17690Sstevel@tonic-gate 		return;
17700Sstevel@tonic-gate 	}
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	/*
17730Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
17740Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
17750Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
17760Sstevel@tonic-gate 	 * the lower queue from which the current response message
17770Sstevel@tonic-gate 	 * comes.
17780Sstevel@tonic-gate 	 */
17790Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
17800Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
17810Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
17820Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
17830Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
17840Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
17850Sstevel@tonic-gate 	mutex_exit(&msg->kpm_lock);
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	if (msg->kpm_resp_nums < msg->kpm_req_nums)
17880Sstevel@tonic-gate 		return;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
17910Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	/*
17970Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
17980Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
17990Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
18000Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
18010Sstevel@tonic-gate 	 * is sent to upstreams
18020Sstevel@tonic-gate 	 */
18030Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
18040Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
18070Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
18080Sstevel@tonic-gate 		/*
18090Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
18100Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
18110Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
18120Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
18130Sstevel@tonic-gate 		 * check if the pointer is null.
18140Sstevel@tonic-gate 		 */
18150Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18160Sstevel@tonic-gate 			cons_polledio_t		*polledio;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18190Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
18200Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
18210Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
18220Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
18230Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
18240Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
18250Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
18260Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
18270Sstevel@tonic-gate 					error = 0;
18280Sstevel@tonic-gate 				} else {
18290Sstevel@tonic-gate 					/*
18300Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
18310Sstevel@tonic-gate 					 * use the same cons_polledio structure
18320Sstevel@tonic-gate 					 * as conskbd.
18330Sstevel@tonic-gate 					 */
18340Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
18350Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
18360Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
18370Sstevel@tonic-gate 						    "driver does not support "
18380Sstevel@tonic-gate 						    "system debugging: %s",
18390Sstevel@tonic-gate 						    path);
18400Sstevel@tonic-gate 					}
18410Sstevel@tonic-gate 					error = EINVAL;
18420Sstevel@tonic-gate 				}
18430Sstevel@tonic-gate 			} else {
18440Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
18450Sstevel@tonic-gate 				    DDI_SUCCESS) {
18460Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
18470Sstevel@tonic-gate 					    " not available for system"
18480Sstevel@tonic-gate 					    " debugging:  %s", path);
18490Sstevel@tonic-gate 				}
18500Sstevel@tonic-gate 			}
18510Sstevel@tonic-gate 			mp->b_next = NULL;
18520Sstevel@tonic-gate 			mp->b_prev = NULL;
18530Sstevel@tonic-gate 			freemsg(mp);
18540Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18550Sstevel@tonic-gate 		}
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
18580Sstevel@tonic-gate 		if (error == 0) {
18590Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
18600Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
18610Sstevel@tonic-gate 		}
18620Sstevel@tonic-gate 		break;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
18650Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18660Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18670Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
18680Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
18690Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
18700Sstevel@tonic-gate 				error = 0;
18710Sstevel@tonic-gate 			} else {
18720Sstevel@tonic-gate 				devt =
18730Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
18760Sstevel@tonic-gate 				    DDI_SUCCESS) {
18770Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
18780Sstevel@tonic-gate 					    " not available: %s", path);
18790Sstevel@tonic-gate 				}
18800Sstevel@tonic-gate 			}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 			mp->b_next = NULL;
18830Sstevel@tonic-gate 			mp->b_prev = NULL;
18840Sstevel@tonic-gate 			freemsg(mp);
18850Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18860Sstevel@tonic-gate 		}
18870Sstevel@tonic-gate 		break;
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	case KIOCCMD:
18900Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18910Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
18940Sstevel@tonic-gate 				error = 0;
18950Sstevel@tonic-gate 			mp->b_next = NULL;
18960Sstevel@tonic-gate 			mp->b_prev = NULL;
18970Sstevel@tonic-gate 			freemsg(mp);
18980Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18990Sstevel@tonic-gate 		}
19000Sstevel@tonic-gate 		break;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
19030Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
19040Sstevel@tonic-gate 	}
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
19070Sstevel@tonic-gate 	if (error == 0) {
19080Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
19090Sstevel@tonic-gate 	} else {
19100Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
19130Sstevel@tonic-gate 	iocp->ioc_error = error;
19140Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
19150Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
19160Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate static void
19220Sstevel@tonic-gate conskbd_link_lower_queue(conskbd_lower_queue_t *lqs)
19230Sstevel@tonic-gate {
19240Sstevel@tonic-gate 	struct iocblk 	*iocp;
19250Sstevel@tonic-gate 	mblk_t		*msg;
19260Sstevel@tonic-gate 	int		index;
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	msg = lqs->lqs_pending_plink;
19310Sstevel@tonic-gate 	msg->b_datap->db_type = M_IOCACK;
19320Sstevel@tonic-gate 	iocp = (struct iocblk *)msg->b_rptr;
19330Sstevel@tonic-gate 	iocp->ioc_error = 0;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	/*
19360Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
19370Sstevel@tonic-gate 	 */
19380Sstevel@tonic-gate 	mutex_enter(&conskbd_lq_lock);
19390Sstevel@tonic-gate 	conskbd.conskbd_lqueue_nums++;
19400Sstevel@tonic-gate 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
19410Sstevel@tonic-gate 	conskbd.conskbd_lqueue_list = lqs;
19420Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
19430Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
19440Sstevel@tonic-gate 	}
19450Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
19460Sstevel@tonic-gate 	mutex_exit(&conskbd_lq_lock);
19470Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate /*ARGSUSED*/
19540Sstevel@tonic-gate static void
19550Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
19560Sstevel@tonic-gate {
19570Sstevel@tonic-gate 	struct iocblk	*iocp;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	/*
19620Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
19630Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
19640Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
19650Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
19660Sstevel@tonic-gate 	 * should be added to this list.
19670Sstevel@tonic-gate 	 */
19680Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
19710Sstevel@tonic-gate 			(iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19740Sstevel@tonic-gate 			("conskbd_legacy_upstream_msg: "
19750Sstevel@tonic-gate 			"CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
19760Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
19790Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19800Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
19850Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19860Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
19870Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
19880Sstevel@tonic-gate 	} else {
19890Sstevel@tonic-gate 		/* if reached here, it must be a error */
19900Sstevel@tonic-gate 		cmn_err(CE_WARN,
19910Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
19920Sstevel@tonic-gate 		freemsg(mp);
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate /*
19980Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
19990Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
20000Sstevel@tonic-gate  *
20010Sstevel@tonic-gate  * 1) application initiated request
20020Sstevel@tonic-gate  * 	A KIOCSLED ioctl is sent by an application. The ioctl will be
20030Sstevel@tonic-gate  * 	be prcoessed by queue service procedure conskbduwsrv(), which
20040Sstevel@tonic-gate  * 	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
20050Sstevel@tonic-gate  * 	conskbd_streams_setled() to set LED, after that,  kbtrans will
20060Sstevel@tonic-gate  * 	return an ACK message to upper module.
20070Sstevel@tonic-gate  *
20080Sstevel@tonic-gate  * 2) Kbtrans initiated the request
20090Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
20100Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
20110Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
20120Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
20130Sstevel@tonic-gate  *
20140Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
20150Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
20160Sstevel@tonic-gate  * has no return value.
20170Sstevel@tonic-gate  */
20180Sstevel@tonic-gate static void
20190Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
20200Sstevel@tonic-gate {
20210Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20220Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
20230Sstevel@tonic-gate 	mblk_t		*req;
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	if (led_state == -1)
20280Sstevel@tonic-gate 		return;
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 	/*
20330Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
20340Sstevel@tonic-gate 	 * it if this happens.
20350Sstevel@tonic-gate 	 */
20360Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
20370Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 		if (!req) {
20400Sstevel@tonic-gate 			continue;
20410Sstevel@tonic-gate 		}
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
20440Sstevel@tonic-gate 		if (!req->b_cont) {
20450Sstevel@tonic-gate 			freemsg(req);
20460Sstevel@tonic-gate 			continue;
20470Sstevel@tonic-gate 		}
20480Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
20490Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
20500Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
20510Sstevel@tonic-gate 			freemsg(req);
20520Sstevel@tonic-gate 	}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate }	/* conskbd_streams_setled() */
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate static void
20570Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
20580Sstevel@tonic-gate {
20590Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20600Sstevel@tonic-gate 	struct cons_polledio		*cb;
20610Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
20640Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
20650Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
20660Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
20670Sstevel@tonic-gate 			    led_state);
20680Sstevel@tonic-gate 		}
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate static boolean_t
20740Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
20750Sstevel@tonic-gate 		kbtrans_key_t *keycode, enum keystate *state)
20760Sstevel@tonic-gate {
20770Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20780Sstevel@tonic-gate 	struct cons_polledio 		*cb;
20790Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
20800Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
20830Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
20840Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
20850Sstevel@tonic-gate 		if ((cb != NULL) &&
20860Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
20870Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
20880Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
20890Sstevel@tonic-gate 		}
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
20920Sstevel@tonic-gate 		if (ret == B_TRUE) {
20931097Slq150181 
20941097Slq150181 			/* A legacy keyboard ? */
20951097Slq150181 			if (conskbd.conskbd_bypassed == B_TRUE)
20961097Slq150181 				break;
20971097Slq150181 
20981097Slq150181 			/*
20991097Slq150181 			 * This is the PS2 scancode 0x2B -> USB(49) /
21001097Slq150181 			 * USB(50) keycode mapping workaround, for
21011097Slq150181 			 * polled mode.
21021097Slq150181 			 *
21031097Slq150181 			 * There are two possible USB keycode mappings
21041097Slq150181 			 * for PS2 scancode 0x2B and this workaround
21051097Slq150181 			 * makes sure that we use the USB keycode that
21061097Slq150181 			 * does not end up being mapped to a HOLE key
21071097Slq150181 			 * using the current keyboard translation
21081097Slq150181 			 * tables.
21091097Slq150181 			 *
21101097Slq150181 			 * See conskbdlrput() for a detailed
21111097Slq150181 			 * explanation of the problem.
21121097Slq150181 			 */
21131097Slq150181 			if (*keycode == 49 || *keycode == 50) {
21141097Slq150181 				if (conskbd_keyindex->k_normal[50] == HOLE)
21151097Slq150181 					*keycode = 49;
21161097Slq150181 				else
21171097Slq150181 					*keycode = 50;
21181097Slq150181 			}
21191097Slq150181 
21200Sstevel@tonic-gate 			break;
21210Sstevel@tonic-gate 		}
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	return (ret);
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate static boolean_t
21290Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
21300Sstevel@tonic-gate {
21310Sstevel@tonic-gate 	struct iocblk		*iocp;
21320Sstevel@tonic-gate 	int		directio;
21330Sstevel@tonic-gate 	int		error;
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
21360Sstevel@tonic-gate 		return (B_FALSE);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
21410Sstevel@tonic-gate 	case KIOCGDIRECT: {
21420Sstevel@tonic-gate 		/*
21430Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
21440Sstevel@tonic-gate 		 * respond incorrectly.
21450Sstevel@tonic-gate 		 */
21460Sstevel@tonic-gate 		register mblk_t *datap;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
21490Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
21500Sstevel@tonic-gate 			return (B_TRUE);
21510Sstevel@tonic-gate 		}
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
21540Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
21550Sstevel@tonic-gate 		if (mp->b_cont) {
21560Sstevel@tonic-gate 			freemsg(mp->b_cont);
21570Sstevel@tonic-gate 			mp->b_cont = NULL;
21580Sstevel@tonic-gate 		}
21590Sstevel@tonic-gate 		mp->b_cont = datap;
21600Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
21610Sstevel@tonic-gate 		return (B_TRUE);
21620Sstevel@tonic-gate 	}
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	case KIOCSDIRECT:
21650Sstevel@tonic-gate 		/*
21660Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
21670Sstevel@tonic-gate 		 * based code see it and respond to it.
21680Sstevel@tonic-gate 		 */
21690Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
21700Sstevel@tonic-gate 		if (error != 0) {
21710Sstevel@tonic-gate 			return (B_FALSE);
21720Sstevel@tonic-gate 		}
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
21750Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
21760Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
21770Sstevel@tonic-gate 			return (B_TRUE);
21780Sstevel@tonic-gate 		}
21790Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
21820Sstevel@tonic-gate 			kbtrans_streams_set_queue(
21830Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
21840Sstevel@tonic-gate 		} else {
21850Sstevel@tonic-gate 			kbtrans_streams_set_queue(
21860Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 		/*
21900Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
21910Sstevel@tonic-gate 		 */
21920Sstevel@tonic-gate 		return (B_FALSE);
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 	default:
21950Sstevel@tonic-gate 		return (B_FALSE);
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate static void
2202*1762Slt200341 conskbd_polledio_enter(cons_polledio_arg_t arg)
22030Sstevel@tonic-gate {
22040Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
22050Sstevel@tonic-gate 	struct cons_polledio		*cb;
22060Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22090Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
22100Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
22110Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
22120Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
22130Sstevel@tonic-gate 		}
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate static void
2219*1762Slt200341 conskbd_polledio_exit(cons_polledio_arg_t arg)
22200Sstevel@tonic-gate {
22210Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
22220Sstevel@tonic-gate 	struct cons_polledio		*cb;
22230Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22260Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
22270Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
22280Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
22290Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
22300Sstevel@tonic-gate 		}
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate static int
2236*1762Slt200341 conskbd_polledio_getchar(cons_polledio_arg_t arg)
22370Sstevel@tonic-gate {
22380Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate static int
2247*1762Slt200341 conskbd_polledio_ischar(cons_polledio_arg_t arg)
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate static void
22590Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
22600Sstevel@tonic-gate {
22610Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
22620Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
22630Sstevel@tonic-gate 	conskbd_msg_queue = msg;
22640Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate /*
22690Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
22700Sstevel@tonic-gate  */
22710Sstevel@tonic-gate static conskbd_pending_msg_t *
22720Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
22730Sstevel@tonic-gate {
22740Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
22750Sstevel@tonic-gate 	struct iocblk		*iocp;
22760Sstevel@tonic-gate 	uint_t	id;
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
22790Sstevel@tonic-gate 	msg = conskbd_msg_queue;
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
22820Sstevel@tonic-gate 	ASSERT(iocp);
22830Sstevel@tonic-gate 	id = iocp->ioc_id;
22840Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
22850Sstevel@tonic-gate 		msg = msg->kpm_next;
22860Sstevel@tonic-gate 	}
22870Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	return (msg);
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate static void
22950Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
22960Sstevel@tonic-gate {
22970Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
22980Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23010Sstevel@tonic-gate 	prev = conskbd_msg_queue;
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	for (p = prev; p != msg; p = p->kpm_next)
23040Sstevel@tonic-gate 		prev = p;
23050Sstevel@tonic-gate 	ASSERT(p && p == msg);
23060Sstevel@tonic-gate 	if (prev == p) {
23070Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
23080Sstevel@tonic-gate 	} else {
23090Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
23100Sstevel@tonic-gate 	}
23110Sstevel@tonic-gate 	p->kpm_next = NULL;
23120Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate #ifdef DEBUG
23170Sstevel@tonic-gate /*ARGSUSED*/
23180Sstevel@tonic-gate void
23190Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
23200Sstevel@tonic-gate {
23210Sstevel@tonic-gate 	char buf[256];
23220Sstevel@tonic-gate 	va_list ap;
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	va_start(ap, fmt);
23250Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
23260Sstevel@tonic-gate 	va_end(ap);
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate }	/* conskbd_dprintf() */
23310Sstevel@tonic-gate #endif
2332