xref: /onnv-gate/usr/src/uts/common/io/conskbd.c (revision 5186:fbc9ffaea5e4)
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 /*
233497Srz201010  * Copyright 2007 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>
813497Srz201010 #include <sys/beep.h>
820Sstevel@tonic-gate 
830Sstevel@tonic-gate extern struct keyboard *kbtrans_usbkb_maptab_init(void);
840Sstevel@tonic-gate extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
850Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Module linkage routines for the kernel
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
910Sstevel@tonic-gate static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
920Sstevel@tonic-gate static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * STREAMS queue processing procedures
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate static void	conskbduwsrv(queue_t *);
980Sstevel@tonic-gate static void	conskbdlwserv(queue_t *);
990Sstevel@tonic-gate static void	conskbdlrput(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 */
1861828Scg149915 	D_MP | D_MTOUTPERIM | D_MTOCEXCL	/* 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 */
2121828Scg149915 	"conskbd multiplexer driver %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 /*
2561828Scg149915  * Module global data are protected by outer perimeter. Modifying
2571828Scg149915  * these global data is executed in outer perimeter exclusively.
2581828Scg149915  * Except in conskbdopen() and conskbdclose(), which are entered
2591828Scg149915  * exclusively (Refer to D_MTOCEXCL flag), all changes for the
2601828Scg149915  * global variables are protected by qwriter().
2610Sstevel@tonic-gate  */
2620Sstevel@tonic-gate static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
2630Sstevel@tonic-gate static	queue_t	*conskbd_consqueue; /* console queue above us */
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
2670Sstevel@tonic-gate static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
2680Sstevel@tonic-gate static struct keyboard *conskbd_keyindex;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
2720Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
2730Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
2740Sstevel@tonic-gate  * will be easier to add new statistics later.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate static struct {
2770Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last keystroke */
2780Sstevel@tonic-gate } conskbd_kstat = {
2790Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
2800Sstevel@tonic-gate };
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * Local routines prototypes
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate static int conskbd_kstat_update(kstat_t *, int);
2860Sstevel@tonic-gate 
2871828Scg149915 static void conskbd_ioctl(queue_t *, mblk_t *);
2880Sstevel@tonic-gate static void conskbd_ioc_plink(queue_t *, mblk_t *);
2891828Scg149915 static void conskbd_ioc_punlink(queue_t *, mblk_t *);
2900Sstevel@tonic-gate static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
2910Sstevel@tonic-gate static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
2921274Sqz150045 static mblk_t *conskbd_alloc_firm_event(int, int);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
2950Sstevel@tonic-gate static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
2960Sstevel@tonic-gate static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
2971828Scg149915 static void conskbd_link_lowque_virt(queue_t *, mblk_t *);
2981828Scg149915 static void conskbd_link_lowque_legacy(queue_t *, mblk_t *);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
3010Sstevel@tonic-gate static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
3020Sstevel@tonic-gate static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
3030Sstevel@tonic-gate static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
3040Sstevel@tonic-gate static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
3050Sstevel@tonic-gate static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3060Sstevel@tonic-gate static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3070Sstevel@tonic-gate static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);
3080Sstevel@tonic-gate 
3091762Slt200341 static void conskbd_polledio_enter(cons_polledio_arg_t);
3101762Slt200341 static void conskbd_polledio_exit(cons_polledio_arg_t);
3111762Slt200341 static int  conskbd_polledio_ischar(cons_polledio_arg_t);
3121762Slt200341 static int  conskbd_polledio_getchar(cons_polledio_arg_t);
3130Sstevel@tonic-gate static void conskbd_polledio_setled(struct kbtrans_hardware *, int);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static void conskbd_streams_setled(struct kbtrans_hardware *, int);
3160Sstevel@tonic-gate static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
3170Sstevel@tonic-gate static boolean_t
3180Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *,
3190Sstevel@tonic-gate 		kbtrans_key_t *, enum keystate *);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * Callbacks needed by kbtrans
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate static struct kbtrans_callbacks conskbd_callbacks = {
3250Sstevel@tonic-gate 	conskbd_streams_setled,
3260Sstevel@tonic-gate 	conskbd_polledio_setled,
3270Sstevel@tonic-gate 	conskbd_polled_keycheck,
3280Sstevel@tonic-gate };
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate  * Single private "global" lock for the few rare conditions
3320Sstevel@tonic-gate  * we want single-threaded.
3330Sstevel@tonic-gate  */
3340Sstevel@tonic-gate static	kmutex_t	conskbd_msgq_lock;
3350Sstevel@tonic-gate static	conskbd_pending_msg_t	*conskbd_msg_queue;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * The software state structure of virtual keyboard.
3391828Scg149915  * Currently, only one virtual keyboard is supported.
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate static conskbd_state_t	conskbd = { 0 };
3420Sstevel@tonic-gate 
3431274Sqz150045 /* This variable backs up the layout state for non-self-ID keyboards */
3441274Sqz150045 static int kbd_layout_bak = 0;
3451274Sqz150045 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * _init()
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * Description:
3500Sstevel@tonic-gate  *      Driver initialization, called when driver is first loaded.
3510Sstevel@tonic-gate  *      This is how access is initially given to all the static structures.
3520Sstevel@tonic-gate  *
3530Sstevel@tonic-gate  * Arguments:
3540Sstevel@tonic-gate  *      None
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * Returns:
3570Sstevel@tonic-gate  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
3580Sstevel@tonic-gate  *      mod_install() status, see mod_install(9f)
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate int
3610Sstevel@tonic-gate _init(void)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	int	error;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3660Sstevel@tonic-gate 	if (error != 0) {
3670Sstevel@tonic-gate 		return (error);
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	conskbd_keyindex = kbtrans_usbkb_maptab_init();
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	return (error);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate }	/* _init() */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * _fini()
3800Sstevel@tonic-gate  *
3810Sstevel@tonic-gate  * Description:
3820Sstevel@tonic-gate  *      Module de-initialization, called when the driver is to be unloaded.
3830Sstevel@tonic-gate  *
3840Sstevel@tonic-gate  * Arguments:
3850Sstevel@tonic-gate  *      None
3860Sstevel@tonic-gate  *
3870Sstevel@tonic-gate  * Returns:
3880Sstevel@tonic-gate  *      mod_remove() status, see mod_remove(9f)
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate int
3910Sstevel@tonic-gate _fini(void)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	int	error;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3960Sstevel@tonic-gate 	if (error != 0)
3970Sstevel@tonic-gate 		return (error);
3980Sstevel@tonic-gate 	mutex_destroy(&conskbd_msgq_lock);
3990Sstevel@tonic-gate 	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	return (0);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate }	/* _fini() */
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * _info()
4070Sstevel@tonic-gate  *
4080Sstevel@tonic-gate  * Description:
4090Sstevel@tonic-gate  *      Module information, returns information about the driver.
4100Sstevel@tonic-gate  *
4110Sstevel@tonic-gate  * Arguments:
4120Sstevel@tonic-gate  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
4130Sstevel@tonic-gate  *
4140Sstevel@tonic-gate  * Returns:
4150Sstevel@tonic-gate  *      mod_info() status, see mod_info(9f)
4160Sstevel@tonic-gate  */
4170Sstevel@tonic-gate int
4180Sstevel@tonic-gate _info(struct modinfo *modinfop)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate }	/* _info() */
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * conskbd_attach()
4270Sstevel@tonic-gate  *
4280Sstevel@tonic-gate  * Description:
4290Sstevel@tonic-gate  * 	This routine creates two device nodes. One is the "kbd" node, which
4300Sstevel@tonic-gate  * is used by user application programs(such as Xserver).The other is the
4310Sstevel@tonic-gate  * "conskbd" node, which is an internal node. consconfig_dacf module will
4320Sstevel@tonic-gate  * open this internal node, and link the conskbd under the wc (workstaion
4330Sstevel@tonic-gate  * console).
4340Sstevel@tonic-gate  *
4350Sstevel@tonic-gate  * Arguments:
4360Sstevel@tonic-gate  *      dev_info_t      *dip    Pointer to the device's dev_info struct
4370Sstevel@tonic-gate  *      ddi_attach_cmd_t cmd    Attach command
4380Sstevel@tonic-gate  *
4390Sstevel@tonic-gate  * Returns:
4400Sstevel@tonic-gate  *      DDI_SUCCESS             The driver was initialized properly
4410Sstevel@tonic-gate  *      DDI_FAILURE             The driver couldn't be initialized properly
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate /*ARGSUSED*/
4440Sstevel@tonic-gate static int
4450Sstevel@tonic-gate conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	kstat_t	*ksp;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	switch (cmd) {
4500Sstevel@tonic-gate 	case DDI_ATTACH:
4510Sstevel@tonic-gate 		break;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	default:
4540Sstevel@tonic-gate 		return (DDI_FAILURE);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
4580Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) ||
4590Sstevel@tonic-gate 	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
4600Sstevel@tonic-gate 	    1) == DDI_FAILURE)) {
4610Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4620Sstevel@tonic-gate 		return (DDI_FAILURE);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	conskbd_dip = devi;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
4670Sstevel@tonic-gate 	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
4680Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
4690Sstevel@tonic-gate 	if (ksp) {
4700Sstevel@tonic-gate 		ksp->ks_data = (void *) &conskbd_kstat;
4710Sstevel@tonic-gate 		ksp->ks_update = conskbd_kstat_update;
4720Sstevel@tonic-gate 		kstat_install(ksp);
4730Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	conskbd.conskbd_layout = -1;	/* invalid layout */
4770Sstevel@tonic-gate 	conskbd.conskbd_led_state = -1;
4780Sstevel@tonic-gate 	conskbd.conskbd_bypassed = B_FALSE;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	return (DDI_SUCCESS);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate }	/* conskbd_attach() */
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * conskbd_detach()
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  * Description:
4880Sstevel@tonic-gate  *      Detach an instance of the conskbd driver. In fact, the driver can not
4890Sstevel@tonic-gate  * be detached.
4900Sstevel@tonic-gate  *
4910Sstevel@tonic-gate  * Arguments:
4920Sstevel@tonic-gate  *      dev_info_t              *dip    Pointer to the device's dev_info struct
4930Sstevel@tonic-gate  *      ddi_detach_cmd_t        cmd     Detach command
4940Sstevel@tonic-gate  *
4950Sstevel@tonic-gate  * Returns:
4960Sstevel@tonic-gate  *      DDI_SUCCESS     The driver was detached
4970Sstevel@tonic-gate  *      DDI_FAILURE     The driver couldn't be detached
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate /*ARGSUSED*/
5000Sstevel@tonic-gate static int
5010Sstevel@tonic-gate conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	return (DDI_FAILURE);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate }	/* conskbd_detach() */
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /* ARGSUSED */
5080Sstevel@tonic-gate static int
5090Sstevel@tonic-gate conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
5100Sstevel@tonic-gate 	void **result)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	register int error;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	switch (infocmd) {
5150Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5160Sstevel@tonic-gate 		if (conskbd_dip == NULL) {
5170Sstevel@tonic-gate 			error = DDI_FAILURE;
5180Sstevel@tonic-gate 		} else {
5190Sstevel@tonic-gate 			*result = (void *) conskbd_dip;
5200Sstevel@tonic-gate 			error = DDI_SUCCESS;
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 		break;
5230Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5240Sstevel@tonic-gate 		*result = (void *)0;
5250Sstevel@tonic-gate 		error = DDI_SUCCESS;
5260Sstevel@tonic-gate 		break;
5270Sstevel@tonic-gate 	default:
5280Sstevel@tonic-gate 		error = DDI_FAILURE;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	return (error);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate }	/* conskbd_info() */
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*ARGSUSED*/
5350Sstevel@tonic-gate static int
5360Sstevel@tonic-gate conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	dev_t	unit;
5390Sstevel@tonic-gate 	int	err;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	unit = getminor(*devp);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	if (unit == 0) {
5440Sstevel@tonic-gate 		/*
5450Sstevel@tonic-gate 		 * Opening "/dev/kbd".
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 		conskbd_regqueue = q;
5480Sstevel@tonic-gate 		qprocson(q);
5490Sstevel@tonic-gate 		return (0);
5500Sstevel@tonic-gate 	} else if (unit != 1) {
5510Sstevel@tonic-gate 		/* we don't do that under Bozo's Big Tent */
5520Sstevel@tonic-gate 		return (ENODEV);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5561828Scg149915 	 * Check if already initialized
5571828Scg149915 	 */
5581828Scg149915 	if (conskbd_consqueue != NULL)
5591828Scg149915 		return (0);
5601828Scg149915 
5611828Scg149915 	/*
5620Sstevel@tonic-gate 	 * Opening the device to be linked under the console.
5630Sstevel@tonic-gate 	 */
5640Sstevel@tonic-gate 	conskbd_consqueue = q;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5671097Slq150181 	 * initialize kbtrans module for conskbd
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	err = kbtrans_streams_init(q, sflag, crp, (struct kbtrans_hardware *)
5700Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
5710Sstevel@tonic-gate 	if (err != 0)
5720Sstevel@tonic-gate 		return (err);
5730Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
5740Sstevel@tonic-gate 	    conskbd_keyindex);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
5770Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
5781762Slt200341 	    (cons_polledio_arg_t)&conskbd;
5790Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
5800Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
5811762Slt200341 	    (int (*)(cons_polledio_arg_t)) conskbd_polledio_getchar;
5820Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
5831762Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))conskbd_polledio_ischar;
5840Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
5850Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
5860Sstevel@tonic-gate 	qprocson(q);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	return (0);
5890Sstevel@tonic-gate 
5901828Scg149915 }	/* conskbdopen() */
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*ARGSUSED*/
5940Sstevel@tonic-gate static int
5950Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
5980Sstevel@tonic-gate 
5991828Scg149915 		conskbd_pending_msg_t	*pmsg, *prev, *next;
6001828Scg149915 		mblk_t		*mp;
6011828Scg149915 
6020Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
6030Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
6060Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
6070Sstevel@tonic-gate 		    conskbd_consqueue);
6080Sstevel@tonic-gate 		qprocsoff(q);
6090Sstevel@tonic-gate 		conskbd_regqueue = NULL;
6101828Scg149915 
6111828Scg149915 		/*
6121828Scg149915 		 * If there are any pending ioctls which conskbd hasn't
6131828Scg149915 		 * responded to yet, remove them from conskbd_msg_queue.
6141828Scg149915 		 * Otherwise, we might send the response to a nonexistent
6151828Scg149915 		 * closed queue. Refer to: conskbd_mux_upstream_msg().
6161828Scg149915 		 */
6171828Scg149915 		for (prev = NULL, pmsg = conskbd_msg_queue; pmsg != NULL;
6181828Scg149915 		    pmsg = next) {
6191828Scg149915 			next = pmsg->kpm_next;
6202092Scg149915 			if (pmsg->kpm_upper_queue == WR(q)) {
6211828Scg149915 				if (prev == NULL)
6221828Scg149915 					conskbd_msg_queue = next;
6231828Scg149915 				else
6241828Scg149915 					prev->kpm_next = next;
6251828Scg149915 
6261828Scg149915 				while (pmsg->kpm_resp_list != NULL) {
6271828Scg149915 					mp = pmsg->kpm_resp_list;
6281828Scg149915 					pmsg->kpm_resp_list = mp->b_next;
6291828Scg149915 					mp->b_next = mp->b_prev = NULL;
6301828Scg149915 					freemsg(mp);
6311828Scg149915 				}
6321828Scg149915 				mutex_destroy(&pmsg->kpm_lock);
6331828Scg149915 				kmem_free(pmsg, sizeof (*pmsg));
6341828Scg149915 			} else {
6351828Scg149915 				prev = pmsg;
6361828Scg149915 			}
6371828Scg149915 		}
6380Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
6410Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
6420Sstevel@tonic-gate 		 */
6430Sstevel@tonic-gate 		qprocsoff(q);
6440Sstevel@tonic-gate 		conskbd_consqueue = NULL;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	return (0);
6480Sstevel@tonic-gate 
6491828Scg149915 }	/* conskbdclose() */
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * Service procedure for upper write queue.
6530Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
6540Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
6550Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
6560Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
6570Sstevel@tonic-gate  * deal with all messages.
6580Sstevel@tonic-gate  * 	This routine is invoked when ioctl commands are send down
6590Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
6600Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
6610Sstevel@tonic-gate  * the led states.
6620Sstevel@tonic-gate  */
6630Sstevel@tonic-gate static void
6640Sstevel@tonic-gate conskbduwsrv(queue_t *q)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	mblk_t	*mp;
6670Sstevel@tonic-gate 	queue_t	*oldq;
6680Sstevel@tonic-gate 	enum kbtrans_message_response ret;
6693497Srz201010 	struct copyresp *csp;
6703497Srz201010 	struct freq_request *frqp;
6713497Srz201010 	int error;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		/*
6760Sstevel@tonic-gate 		 * if the virtual keyboard is supported
6770Sstevel@tonic-gate 		 */
6780Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
6810Sstevel@tonic-gate 				continue;
6820Sstevel@tonic-gate 			/*
6830Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
6840Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
6850Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
6860Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
6870Sstevel@tonic-gate 			 * and regular queue.
6880Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
6890Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
6900Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
6910Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
6920Sstevel@tonic-gate 			 * to be switched between regular queue and console
6930Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
6940Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
6950Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
6960Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
6970Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
6980Sstevel@tonic-gate 			 * need to return response to right queue.
6990Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
7000Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
7010Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
7020Sstevel@tonic-gate 			 * and then switch back to old queue.
7030Sstevel@tonic-gate 			 */
7040Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
7050Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
7060Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7070Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
7080Sstevel@tonic-gate 			ret = kbtrans_streams_message(
7090Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
7100Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7110Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 			switch (ret) {
7140Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
7150Sstevel@tonic-gate 					continue;
7160Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
7170Sstevel@tonic-gate 					break;
7180Sstevel@tonic-gate 			}
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		case M_IOCTL:
7241828Scg149915 			conskbd_ioctl(q, mp);
7250Sstevel@tonic-gate 			break;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		case M_FLUSH:
7280Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
7290Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
7300Sstevel@tonic-gate 			}
7310Sstevel@tonic-gate 			/*
7320Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
7330Sstevel@tonic-gate 			 * may be lost so that upper module or applications
7340Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
7350Sstevel@tonic-gate 			 * ever.
7360Sstevel@tonic-gate 			 */
7370Sstevel@tonic-gate 			freemsg(mp);
7380Sstevel@tonic-gate 			break;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		case M_DATA:
7410Sstevel@tonic-gate 			/*
7420Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
7430Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
7440Sstevel@tonic-gate 			 * down to lower queue.
7450Sstevel@tonic-gate 			 */
7460Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
7470Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
7480Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
7490Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
7500Sstevel@tonic-gate 					freemsg(mp);
7510Sstevel@tonic-gate 			} else {
7520Sstevel@tonic-gate 				freemsg(mp);
7530Sstevel@tonic-gate 			}
7540Sstevel@tonic-gate 			break;
7550Sstevel@tonic-gate 
7563497Srz201010 		case M_IOCDATA:
7573497Srz201010 			/*
7583497Srz201010 			 * Only deal with copyresp to KIOCSETFREQ
7593497Srz201010 			 * transparent ioctl now
7603497Srz201010 			 */
7613497Srz201010 			csp = (struct copyresp *)mp->b_rptr;
7623497Srz201010 			if (csp->cp_rval) {
7633497Srz201010 				miocnak(q, mp, 0, EINVAL);
7643497Srz201010 				break;
7653497Srz201010 			}
7663497Srz201010 
7673497Srz201010 			error = 0;
7683497Srz201010 			switch (csp->cp_cmd) {
7693497Srz201010 			case KIOCSETFREQ:
7703497Srz201010 				frqp = (struct freq_request *)mp->
7714925Sqz150045 				    b_cont->b_rptr;
7723497Srz201010 
7733497Srz201010 				switch (frqp->type) {
7743497Srz201010 				case CONSOLE_BEEP:
7753497Srz201010 					error = beeper_freq(BEEP_CONSOLE,
7764925Sqz150045 					    (int)frqp->freq);
7773497Srz201010 						break;
7783497Srz201010 
7793497Srz201010 				case KBD_BEEP:
7803497Srz201010 					error = beeper_freq(BEEP_TYPE4,
7814925Sqz150045 					    (int)frqp->freq);
7823497Srz201010 						break;
7833497Srz201010 
7843497Srz201010 				default:
7853497Srz201010 					error = 1;
7863497Srz201010 				} /* frqp->type */
7873497Srz201010 
7883497Srz201010 				break;
7893497Srz201010 
7903497Srz201010 			default:
7913497Srz201010 				error = 1;
7923497Srz201010 			} /* csp->cp_cmd */
7933497Srz201010 
7943497Srz201010 			if (error == 0)
7953497Srz201010 				miocack(q, mp, 0, 0);
7963497Srz201010 			else
7973497Srz201010 				miocnak(q, mp, 0, EINVAL);
7983497Srz201010 
7993497Srz201010 			break;
8003497Srz201010 
8010Sstevel@tonic-gate 		default:
8020Sstevel@tonic-gate 			/*
8030Sstevel@tonic-gate 			 * Pass an error message up.
8040Sstevel@tonic-gate 			 */
8050Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
8060Sstevel@tonic-gate 			if (mp->b_cont) {
8070Sstevel@tonic-gate 				freemsg(mp->b_cont);
8080Sstevel@tonic-gate 				mp->b_cont = NULL;
8090Sstevel@tonic-gate 			}
8100Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
8110Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
8120Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
8130Sstevel@tonic-gate 			qreply(q, mp);
8140Sstevel@tonic-gate 		}
8150Sstevel@tonic-gate 	}	/* end of while */
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate }	/* conskbduwsrv() */
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate static void
8201828Scg149915 conskbd_ioctl(queue_t *q, mblk_t *mp)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	struct	iocblk			*iocp;
8230Sstevel@tonic-gate 	int	error = 0;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	case I_LINK:
8300Sstevel@tonic-gate 	case I_PLINK:
831267Scg149915 		if (conskbd.conskbd_bypassed == B_TRUE) {
832267Scg149915 		/*
833267Scg149915 		 * A legacy keyboard can NOT be connected to conskbd together
834267Scg149915 		 * with other keyboards. So when a legacy keyboard is already
835267Scg149915 		 * linked under conkbd, we just reject all others.
836267Scg149915 		 */
837267Scg149915 			miocnak(q, mp, 0, EAGAIN);
838267Scg149915 			break;
839267Scg149915 		}
8401828Scg149915 		qwriter(q, mp, conskbd_ioc_plink, PERIM_OUTER);
8410Sstevel@tonic-gate 		break;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	case I_UNLINK:
8440Sstevel@tonic-gate 	case I_PUNLINK:
8451828Scg149915 		qwriter(q, mp, conskbd_ioc_punlink, PERIM_OUTER);
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	case KIOCSKABORTEN:
8490Sstevel@tonic-gate 		/*
8500Sstevel@tonic-gate 		 * Check if privileged
8510Sstevel@tonic-gate 		 */
8520Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
8530Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8540Sstevel@tonic-gate 			return;
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8580Sstevel@tonic-gate 		if (error != 0) {
8590Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8600Sstevel@tonic-gate 			return;
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
8640Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8650Sstevel@tonic-gate 		break;
8660Sstevel@tonic-gate 
8673497Srz201010 	case KIOCSETFREQ:
8683497Srz201010 		if (iocp->ioc_count != TRANSPARENT) {
8693497Srz201010 			/*
8703497Srz201010 			 * We don't support non-transparent ioctls,
8713497Srz201010 			 * i.e. I_STR ioctls
8723497Srz201010 			 */
8733497Srz201010 			miocnak(q, mp, 0, EINVAL);
8743497Srz201010 		} else {
8753497Srz201010 			/* Transparent ioctl */
8763497Srz201010 			mcopyin(mp, NULL, sizeof (struct freq_request), NULL);
8773497Srz201010 			qreply(q, mp);
8783497Srz201010 		}
8793497Srz201010 		break;
8803497Srz201010 
8810Sstevel@tonic-gate 	default:
8820Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
8830Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
8840Sstevel@tonic-gate 		} else {
8850Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
8860Sstevel@tonic-gate 		}
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 
8891828Scg149915 }	/* conskbd_ioctl() */
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate static void
8930Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
8940Sstevel@tonic-gate {
8950Sstevel@tonic-gate 	struct iocblk		*iocp;
8960Sstevel@tonic-gate 	mblk_t			*datap;
8970Sstevel@tonic-gate 	int			cmd;
8980Sstevel@tonic-gate 	int			error = 0;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
9030Sstevel@tonic-gate 	case KIOCLAYOUT:
9040Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9050Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
9060Sstevel@tonic-gate 			break;
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
9100Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9110Sstevel@tonic-gate 		else
9120Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9150Sstevel@tonic-gate 		if (mp->b_cont)
9160Sstevel@tonic-gate 			freemsg(mp->b_cont);
9170Sstevel@tonic-gate 		mp->b_cont = datap;
9180Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
9190Sstevel@tonic-gate 		break;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	case KIOCSLAYOUT:
9220Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
9230Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 		}
9264925Sqz150045 		kbd_layout_bak = conskbd.conskbd_layout;
9270Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
9284925Sqz150045 		if (conskbd.conskbd_layout != kbd_layout_bak) {
9294925Sqz150045 
9304925Sqz150045 			/* notify the upper of the change event */
9314925Sqz150045 			if ((datap = conskbd_alloc_firm_event(
9324925Sqz150045 			    KEYBOARD_LAYOUT_CHANGE,
9334925Sqz150045 			    conskbd.conskbd_layout)) != NULL) {
9344925Sqz150045 				if (conskbd.conskbd_directio) {
9354925Sqz150045 					putnext(conskbd_regqueue, datap);
9364925Sqz150045 				} else {
9374925Sqz150045 					freemsg(datap);
9384925Sqz150045 				}
9394925Sqz150045 			}
9404925Sqz150045 		}
9410Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9420Sstevel@tonic-gate 		break;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
9450Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
9460Sstevel@tonic-gate 		if (error != 0) {
9470Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9480Sstevel@tonic-gate 			break;
9490Sstevel@tonic-gate 		}
9500Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
9510Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9520Sstevel@tonic-gate 			break;
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9550Sstevel@tonic-gate 		break;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
9580Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
9590Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9600Sstevel@tonic-gate 			break;
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9630Sstevel@tonic-gate 		break;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	case CONSSETABORTENABLE:
9660Sstevel@tonic-gate 		/*
9670Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
9680Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
9690Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
9700Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
9710Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
9720Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
9730Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
9740Sstevel@tonic-gate 		 *
9750Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
9760Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
9770Sstevel@tonic-gate 		 * already handle the message though it returned to us a
9780Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
9790Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
9800Sstevel@tonic-gate 		 * So we just return ACK to upper module.
9810Sstevel@tonic-gate 		 */
9820Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9830Sstevel@tonic-gate 		break;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	case KIOCCMD:
9865129Smarx 	case KIOCMKTONE:
9870Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
9880Sstevel@tonic-gate 		    mp->b_cont == NULL) {
9890Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9900Sstevel@tonic-gate 			break;
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
9930Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
9940Sstevel@tonic-gate 			freemsg(mp->b_cont);
9950Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
9960Sstevel@tonic-gate 			if (datap == NULL) {
9970Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9980Sstevel@tonic-gate 				return;
9990Sstevel@tonic-gate 			}
10000Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
10010Sstevel@tonic-gate 				*(int *)datap->b_wptr =
10020Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
10030Sstevel@tonic-gate 			else
10040Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 			mp->b_cont = datap;
10070Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
10080Sstevel@tonic-gate 			return;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
10110Sstevel@tonic-gate 		break;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	default:
10140Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10150Sstevel@tonic-gate 		break;
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate static void
10210Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
10220Sstevel@tonic-gate {
10230Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
10240Sstevel@tonic-gate 	struct	iocblk		*iocp;
10250Sstevel@tonic-gate 	int	error = 0;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
10300Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	case KIOCGDIRECT: {
10330Sstevel@tonic-gate 		mblk_t *datap;
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
10360Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
10370Sstevel@tonic-gate 			break;
10380Sstevel@tonic-gate 		}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
10410Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
10420Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
10430Sstevel@tonic-gate 			freemsg(mp->b_cont);
10440Sstevel@tonic-gate 			mp->b_cont = NULL;
10450Sstevel@tonic-gate 		}
10460Sstevel@tonic-gate 		mp->b_cont = datap;
10470Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
10480Sstevel@tonic-gate 		break;
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	case KIOCSDIRECT:
10520Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
10530Sstevel@tonic-gate 		if (error != 0) {
10540Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
10550Sstevel@tonic-gate 			break;
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 		/*
10600Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
10610Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
10620Sstevel@tonic-gate 		 * itself.
10630Sstevel@tonic-gate 		 */
10640Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10650Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10660Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10670Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10680Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10690Sstevel@tonic-gate 				return;
10700Sstevel@tonic-gate 			}
10710Sstevel@tonic-gate 			break;
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
10750Sstevel@tonic-gate 		break;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	default:
10780Sstevel@tonic-gate 		/*
10790Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
10800Sstevel@tonic-gate 		 * through to; otherwise, reject it.
10810Sstevel@tonic-gate 		 */
10820Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10830Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10840Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10850Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10860Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10870Sstevel@tonic-gate 				return;
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 			break;
10900Sstevel@tonic-gate 		}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 		/* nobody below us; reject it */
10930Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10940Sstevel@tonic-gate 		break;
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate  * Service procedure for lower write queue.
11020Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
11030Sstevel@tonic-gate  */
11040Sstevel@tonic-gate static void
11050Sstevel@tonic-gate conskbdlwserv(queue_t *q)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	register mblk_t *mp;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
11100Sstevel@tonic-gate 		putnext(q, mp);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate }	/* conskbdlwserv() */
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate  * Put procedure for lower read queue.
11160Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
11170Sstevel@tonic-gate  * device 1.
11180Sstevel@tonic-gate  */
11190Sstevel@tonic-gate static void
11200Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
11210Sstevel@tonic-gate {
11220Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
11230Sstevel@tonic-gate 	struct iocblk 	*iocp;
11240Sstevel@tonic-gate 	Firm_event	*fe;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	case M_FLUSH:
11310Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
11320Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
11330Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
11340Sstevel@tonic-gate 		}
11350Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
11360Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
11370Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
11380Sstevel@tonic-gate 		} else
11390Sstevel@tonic-gate 			freemsg(mp);
11400Sstevel@tonic-gate 		break;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	case M_DATA:
11430Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 			/*
11480Sstevel@tonic-gate 			 * This is a workaround.
11490Sstevel@tonic-gate 			 *
11500Sstevel@tonic-gate 			 * According to HID specification, there are the
11510Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
11520Sstevel@tonic-gate 			 *
11530Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
11540Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
11550Sstevel@tonic-gate 			 *
11560Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
11570Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
11580Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
11590Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
11600Sstevel@tonic-gate 			 * not know which is being pressed or released when we
11610Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
11620Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
11630Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
11640Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
11650Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
11660Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
11670Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
11680Sstevel@tonic-gate 			 * the key to 49.
11690Sstevel@tonic-gate 			 */
11700Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
11710Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
11720Sstevel@tonic-gate 					fe->id = 49;
11730Sstevel@tonic-gate 				else
11740Sstevel@tonic-gate 					fe->id = 50;
11750Sstevel@tonic-gate 			}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 			/*
11780Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
11790Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
11800Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
11810Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
11820Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
11830Sstevel@tonic-gate 			 * as held-down for ever.
11840Sstevel@tonic-gate 			 */
11850Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
11860Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
11870Sstevel@tonic-gate 			if (fe->value)
11880Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
11890Sstevel@tonic-gate 			else
11900Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
11930Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
11940Sstevel@tonic-gate 			freemsg(mp);
11950Sstevel@tonic-gate 		} else {
11960Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
11970Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
11980Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
11990Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
12000Sstevel@tonic-gate 			else
12010Sstevel@tonic-gate 				freemsg(mp);
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
12040Sstevel@tonic-gate 		break;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	case M_IOCACK:
12070Sstevel@tonic-gate 	case M_IOCNAK:
12080Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
12090Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
12120Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
12150Sstevel@tonic-gate 		break;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	case M_ERROR:
12180Sstevel@tonic-gate 	case M_HANGUP:
12190Sstevel@tonic-gate 	default:
12200Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
12210Sstevel@tonic-gate 		break;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate }	/* conskbdlrput() */
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /* ARGSUSED */
12280Sstevel@tonic-gate static int
12290Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
12320Sstevel@tonic-gate 		return (EACCES);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	return (0);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate }	/* conskbd_kstat_update() */
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
12420Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
12430Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
12440Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
12450Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
12460Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
12470Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
12480Sstevel@tonic-gate  * request matching received responses by searching the global linked
12490Sstevel@tonic-gate  * list to find the request which has the same message ID of the
12500Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
12510Sstevel@tonic-gate  * give a response to upstreams based the following policy:
12520Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
12530Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
12540Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
12550Sstevel@tonic-gate  * the first response is sent to upstream.
12560Sstevel@tonic-gate  */
12570Sstevel@tonic-gate static void
12580Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
12610Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
12620Sstevel@tonic-gate 	struct iocblk	*iocp;
12630Sstevel@tonic-gate 	mblk_t		*clonemp;
12640Sstevel@tonic-gate 	int		retry;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
12670Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
12680Sstevel@tonic-gate 		return;
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
12720Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
12730Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
12740Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
12750Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
12780Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
12795129Smarx 	    iocp->ioc_cmd == KIOCCMD ||
12805129Smarx 	    iocp->ioc_cmd == KIOCMKTONE);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
12830Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
12840Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
12850Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
12860Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
12870Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 		/*
12920Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
12930Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
12940Sstevel@tonic-gate 		 * otherwise, system will panic.
12950Sstevel@tonic-gate 		 */
12960Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
12970Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
12980Sstevel@tonic-gate 			lqs = lqs->lqs_next;
12990Sstevel@tonic-gate 			msg->kpm_req_nums --;
13000Sstevel@tonic-gate 			retry = 0;
13010Sstevel@tonic-gate 			continue;
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		clonemp = copymsg(mp);
13050Sstevel@tonic-gate 		if (clonemp != NULL) {
13060Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
13070Sstevel@tonic-gate 				lqs = lqs->lqs_next;
13080Sstevel@tonic-gate 				retry = 0;
13090Sstevel@tonic-gate 				continue;
13100Sstevel@tonic-gate 			}
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 			/*
13130Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
13140Sstevel@tonic-gate 			 */
13150Sstevel@tonic-gate 			freemsg(clonemp);
13160Sstevel@tonic-gate 		}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 		/*
13190Sstevel@tonic-gate 		 * During testing it was observed that occasionally
13200Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
13210Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
13220Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
13230Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
13240Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
13250Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
13260Sstevel@tonic-gate 		 *
13270Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
13280Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
13290Sstevel@tonic-gate 		 * or behave weirdly,
13300Sstevel@tonic-gate 		 *
13310Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
13320Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
13330Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
13340Sstevel@tonic-gate 		 *	entering kmdb;
13350Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
13360Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
13370Sstevel@tonic-gate 		 *	is not available in normal mode once returning
13380Sstevel@tonic-gate 		 *	from kmdb;
13390Sstevel@tonic-gate 		 * 3) for KIOCCMD
13400Sstevel@tonic-gate 		 * 	3.1) for KBD_CMD_NOBELL
13410Sstevel@tonic-gate 		 * 		there's no beep in USB and PS2 keyboard,
13420Sstevel@tonic-gate 		 * 		this ioctl actually disables the beep on
13430Sstevel@tonic-gate 		 * 		system mainboard. Note that all the cloned
13440Sstevel@tonic-gate 		 * 		messages sent down to lower queues do the
13450Sstevel@tonic-gate 		 * 		same job for system mainboard. Therefore,
13460Sstevel@tonic-gate 		 * 		even if we fail to send this ioctl to most
13470Sstevel@tonic-gate 		 * 		of lower queues, the beep still would be
13480Sstevel@tonic-gate 		 * 		disabled. So, no trouble exists here.
13490Sstevel@tonic-gate 		 *	3.2) for others
13500Sstevel@tonic-gate 		 *		nothing;
13510Sstevel@tonic-gate 		 *
13520Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
13530Sstevel@tonic-gate 		 * same request comes again.
13540Sstevel@tonic-gate 		 */
13550Sstevel@tonic-gate 		if (retry ++ >= 5) {
13560Sstevel@tonic-gate 			dev_t	devt;
13570Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
13600Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
13610Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
13620Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
13630Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
13640Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13650Sstevel@tonic-gate 					    "keyboard is not available"
13660Sstevel@tonic-gate 					    " for system debugging: %s",
13670Sstevel@tonic-gate 					    path);
13680Sstevel@tonic-gate 				break;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
13710Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
13720Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
13730Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13740Sstevel@tonic-gate 					    "keyboard is not available:"
13750Sstevel@tonic-gate 					    " %s", path);
13760Sstevel@tonic-gate 				break;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 			default:
13790Sstevel@tonic-gate 				break;
13800Sstevel@tonic-gate 			}
13810Sstevel@tonic-gate 			msg->kpm_req_nums --;
13820Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13830Sstevel@tonic-gate 			retry = 0;
13840Sstevel@tonic-gate 		}
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
13880Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
13890Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
13900Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate static void
13970Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	mblk_t		*req;
14000Sstevel@tonic-gate 	queue_t		*lowque;
14010Sstevel@tonic-gate 	struct linkblk		*linkp;
14020Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
14050Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
14080Sstevel@tonic-gate 	lowque = linkp->l_qbot;
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
14110Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
14120Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
14150Sstevel@tonic-gate 	if (req == NULL) {
14160Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14170Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14180Sstevel@tonic-gate 		return;
14190Sstevel@tonic-gate 	}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
14220Sstevel@tonic-gate 	if (req->b_cont == NULL) {
14230Sstevel@tonic-gate 		freemsg(req);
14240Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14250Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14260Sstevel@tonic-gate 		return;
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 
14291828Scg149915 	lowque->q_ptr = lqs;
14301828Scg149915 	OTHERQ(lowque)->q_ptr = lqs;
14310Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
14320Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
14370Sstevel@tonic-gate 		freemsg(req);
14380Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
14390Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
14400Sstevel@tonic-gate 		lowque->q_ptr = NULL;
14411828Scg149915 		OTHERQ(lowque)->q_ptr = NULL;
14420Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 
14481828Scg149915 static void
14491828Scg149915 conskbd_ioc_punlink(queue_t *q, mblk_t *mp)
14501828Scg149915 {
14511828Scg149915 	int			index;
14521828Scg149915 	struct linkblk		*linkp;
14531828Scg149915 	conskbd_lower_queue_t	*lqs;
14541828Scg149915 	conskbd_lower_queue_t	*prev;
14551828Scg149915 
14561828Scg149915 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
14571828Scg149915 	prev = conskbd.conskbd_lqueue_list;
14581828Scg149915 	for (lqs = prev; lqs; lqs = lqs->lqs_next) {
14591828Scg149915 		if (lqs->lqs_queue == linkp->l_qbot) {
14601828Scg149915 			if (prev == lqs)
14611828Scg149915 				conskbd.conskbd_lqueue_list =
14621828Scg149915 				    lqs->lqs_next;
14631828Scg149915 			else
14641828Scg149915 				prev->lqs_next = lqs->lqs_next;
14651828Scg149915 
14661828Scg149915 			lqs->lqs_queue->q_ptr =  NULL;
14671828Scg149915 			OTHERQ(lqs->lqs_queue)->q_ptr = NULL;
14681828Scg149915 			conskbd.conskbd_lqueue_nums --;
14691828Scg149915 			if (conskbd.conskbd_lqueue_nums == 0) {
14701828Scg149915 				kbd_layout_bak = conskbd.conskbd_layout;
14711828Scg149915 				conskbd.conskbd_layout = -1;
14721828Scg149915 			}
14731828Scg149915 
14741828Scg149915 			for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
14751828Scg149915 				if (lqs->lqs_key_state[index] == KEY_PRESSED)
14761828Scg149915 					kbtrans_streams_key(
14771828Scg149915 					    conskbd.conskbd_kbtrans,
14781828Scg149915 					    index,
14791828Scg149915 					    KEY_RELEASED);
14801828Scg149915 			}
14811828Scg149915 
14821828Scg149915 			kmem_free(lqs, sizeof (*lqs));
14831828Scg149915 			miocack(q, mp, 0, 0);
14841828Scg149915 			return;
14851828Scg149915 		}
14861828Scg149915 		prev = lqs;
14871828Scg149915 	}
14881828Scg149915 	miocnak(q, mp, 0, EINVAL);
14891828Scg149915 
14901828Scg149915 }	/* conskbd_ioc_punlink() */
14911828Scg149915 
14920Sstevel@tonic-gate /*
14930Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
14940Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
14950Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
14960Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
14970Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
14980Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
14990Sstevel@tonic-gate  */
15000Sstevel@tonic-gate static void
15010Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15020Sstevel@tonic-gate {
15030Sstevel@tonic-gate 	switch (lqs->lqs_state) {
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
15060Sstevel@tonic-gate 	case LQS_INITIALIZED:
15070Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
15080Sstevel@tonic-gate 		break;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
15110Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
15120Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
15130Sstevel@tonic-gate 		break;
15140Sstevel@tonic-gate 
15151272Slq150181 	/* S4: wait lower queue to acknowledge KIOCSLED/KIOCGLED  message */
15160Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
15170Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
15180Sstevel@tonic-gate 		break;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
15210Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
15220Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
15230Sstevel@tonic-gate 		break;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
15260Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
15270Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
15280Sstevel@tonic-gate 		break;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
15310Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
15320Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
15330Sstevel@tonic-gate 		break;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	/* if reaching here, there must be a error */
15360Sstevel@tonic-gate 	default:
15370Sstevel@tonic-gate 		freemsg(mp);
15380Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
15390Sstevel@tonic-gate 		break;
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate static void
15460Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	struct iocblk	*iocp;
15490Sstevel@tonic-gate 	mblk_t		*req;
15500Sstevel@tonic-gate 	queue_t		*lowerque;
15511828Scg149915 	int		err = ENOMEM;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
15540Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15590Sstevel@tonic-gate 	case M_IOCACK:
15600Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
15610Sstevel@tonic-gate 		if (req == NULL) {
15621828Scg149915 			goto err_exit;
15630Sstevel@tonic-gate 		}
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
15660Sstevel@tonic-gate 		if (req->b_cont == NULL) {
15670Sstevel@tonic-gate 			freemsg(req);
15681828Scg149915 			goto err_exit;
15690Sstevel@tonic-gate 		}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
15720Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
15730Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
15760Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
15790Sstevel@tonic-gate 			freemsg(req);
15801828Scg149915 			goto err_exit;
15810Sstevel@tonic-gate 		}
15821828Scg149915 		freemsg(mp);
15831828Scg149915 		return;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	case M_IOCNAK:
15860Sstevel@tonic-gate 		/*
15870Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
15880Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
15890Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
15900Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
15910Sstevel@tonic-gate 		 * will bypass the conskbd module.
15920Sstevel@tonic-gate 		 */
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 		/*
15950Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
15960Sstevel@tonic-gate 		 * conskbd, we reject the current one.
15970Sstevel@tonic-gate 		 */
15980Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
15990Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
16001828Scg149915 			err = iocp->ioc_error;
16011828Scg149915 			goto err_exit;
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 		/*
16051828Scg149915 		 * link this keyboard under conskbd.
16060Sstevel@tonic-gate 		 */
16071828Scg149915 		qwriter(lowerque, mp, conskbd_link_lowque_legacy, PERIM_OUTER);
16081828Scg149915 		return;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16111828Scg149915 err_exit:
16121828Scg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
16131828Scg149915 	lowerque->q_ptr = NULL;
16141828Scg149915 	OTHERQ(lowerque)->q_ptr = NULL;
16151828Scg149915 	kmem_free(lqs, sizeof (*lqs));
16160Sstevel@tonic-gate 	freemsg(mp);
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate static void
16210Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16220Sstevel@tonic-gate {
16230Sstevel@tonic-gate 	struct iocblk 	*iocp;
16240Sstevel@tonic-gate 	mblk_t		*req;
16250Sstevel@tonic-gate 	queue_t		*lowerque;
16261828Scg149915 	int		err = ENOMEM;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16290Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
16340Sstevel@tonic-gate 	case M_IOCACK:
16350Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
16360Sstevel@tonic-gate 		if (req == NULL) {
16371828Scg149915 			goto err_exit;
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
16410Sstevel@tonic-gate 		if (req->b_cont == NULL) {
16420Sstevel@tonic-gate 			freemsg(req);
16431828Scg149915 			goto err_exit;
16440Sstevel@tonic-gate 		}
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
16470Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
16480Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
16490Sstevel@tonic-gate 			freemsg(req);
16501828Scg149915 			goto err_exit;
16510Sstevel@tonic-gate 		}
16521828Scg149915 		freemsg(mp);
16531828Scg149915 		return;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	case M_IOCNAK:
16560Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
16571828Scg149915 		err = iocp->ioc_error;
16581828Scg149915 		goto err_exit;
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 
16611828Scg149915 err_exit:
16621828Scg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
16631828Scg149915 	lowerque->q_ptr = NULL;
16641828Scg149915 	OTHERQ(lowerque)->q_ptr = NULL;
16651828Scg149915 	kmem_free(lqs, sizeof (*lqs));
16660Sstevel@tonic-gate 	freemsg(mp);
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
16690Sstevel@tonic-gate 
16701274Sqz150045 /*
16711274Sqz150045  * Allocate a firm event
16721274Sqz150045  */
16731274Sqz150045 static mblk_t *
16741274Sqz150045 conskbd_alloc_firm_event(int id, int value)
16751274Sqz150045 {
16761274Sqz150045 	mblk_t	*mb;
16771274Sqz150045 	Firm_event *fe;
16781274Sqz150045 
16791274Sqz150045 	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
16801274Sqz150045 		fe = (Firm_event *)mb->b_wptr;
16811274Sqz150045 		fe->id = id;
16821274Sqz150045 		fe->pair_type = FE_PAIR_NONE;
16831274Sqz150045 		fe->pair = NULL;
16841274Sqz150045 		fe->value = value;
16851274Sqz150045 		mb->b_wptr += sizeof (Firm_event);
16861274Sqz150045 	}
16871274Sqz150045 
16881274Sqz150045 	return (mb);
16891274Sqz150045 }
16901274Sqz150045 
16910Sstevel@tonic-gate static void
16920Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16930Sstevel@tonic-gate {
16940Sstevel@tonic-gate 	mblk_t		*req;
16950Sstevel@tonic-gate 	int		layout;
16960Sstevel@tonic-gate 	boolean_t	fail;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16990Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
17020Sstevel@tonic-gate 	case M_IOCACK:
17030Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
17040Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
17050Sstevel@tonic-gate 			/*
17060Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
17070Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
17080Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
17090Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
17100Sstevel@tonic-gate 			 */
17111274Sqz150045 			if (layout != -1 && conskbd.conskbd_layout == -1) {
17121274Sqz150045 				if (layout == 0) {
17131274Sqz150045 					conskbd.conskbd_layout = kbd_layout_bak;
17141274Sqz150045 				} else {
17151274Sqz150045 					conskbd.conskbd_layout = layout;
17161274Sqz150045 					if (layout == kbd_layout_bak) {
17171274Sqz150045 						break;
17181274Sqz150045 					}
17191274Sqz150045 					if ((req = conskbd_alloc_firm_event(
17204925Sqz150045 					    KEYBOARD_LAYOUT_CHANGE,
17214925Sqz150045 					    layout)) != NULL) {
17224925Sqz150045 						if (conskbd.conskbd_directio) {
17231274Sqz150045 							putnext(
17241274Sqz150045 							    conskbd_regqueue,
17251274Sqz150045 							    req);
17264925Sqz150045 						} else if (conskbd_consqueue
17274925Sqz150045 						    != NULL) {
17281274Sqz150045 							putnext(
17291274Sqz150045 							    conskbd_consqueue,
17301274Sqz150045 							    req);
17314925Sqz150045 						} else {
17324925Sqz150045 							freemsg(req);
17334925Sqz150045 						}
17341274Sqz150045 					}
17351274Sqz150045 				}
17361274Sqz150045 			}
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		break;
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
17420Sstevel@tonic-gate 	case M_IOCNAK:
17430Sstevel@tonic-gate 		break;
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	fail = B_TRUE;
17471272Slq150181 
17481272Slq150181 	if (conskbd.conskbd_led_state == -1)
17491272Slq150181 		req = mkiocb(KIOCGLED);
17501272Slq150181 	else
17511272Slq150181 		req = mkiocb(KIOCSLED);
17521272Slq150181 
17530Sstevel@tonic-gate 	if (req) {
17540Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
17550Sstevel@tonic-gate 		if (req->b_cont) {
17561272Slq150181 			if (conskbd.conskbd_led_state != -1) {
17571272Slq150181 				*(uchar_t *)req->b_cont->b_wptr =
17581272Slq150181 				    conskbd.conskbd_led_state;
17591272Slq150181 				req->b_cont->b_wptr += sizeof (uchar_t);
17601272Slq150181 			}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
17630Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
17640Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
17650Sstevel@tonic-gate 				fail = B_FALSE;
17660Sstevel@tonic-gate 			} else {
17670Sstevel@tonic-gate 				freemsg(req);
17680Sstevel@tonic-gate 			}
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 		} else {
17710Sstevel@tonic-gate 			freemsg(req);
17720Sstevel@tonic-gate 		}
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	if (fail) {
17760Sstevel@tonic-gate 		/*
17771272Slq150181 		 * If fail to allocate KIOCSLED/KIOCGLED message or put
17781272Slq150181 		 * the message into lower queue, we immediately link
17791272Slq150181 		 * current keyboard under conskbd. Thus, even if fails
17801272Slq150181 		 * to set/get LED, this keyboard could be available.
17810Sstevel@tonic-gate 		 */
17821828Scg149915 		qwriter(lqs->lqs_queue,
17831828Scg149915 		    mp, conskbd_link_lowque_virt, PERIM_OUTER);
17841828Scg149915 	} else {
17851828Scg149915 		freemsg(mp);
17860Sstevel@tonic-gate 	}
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate static void
17920Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
17930Sstevel@tonic-gate {
17941272Slq150181 	int	led_state;
17951272Slq150181 
17960Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
17970Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
17980Sstevel@tonic-gate 
17991272Slq150181 	if (conskbd.conskbd_led_state == -1) {
18001272Slq150181 		switch (mp->b_datap->db_type) {
18011272Slq150181 		case M_IOCACK:
18021272Slq150181 			if (miocpullup(mp, sizeof (uchar_t)) == 0) {
18031272Slq150181 				led_state = *(uchar_t *)mp->b_cont->b_rptr;
18041272Slq150181 				conskbd.conskbd_led_state = led_state;
18051272Slq150181 				kbtrans_streams_setled(conskbd.conskbd_kbtrans,
18061272Slq150181 				    led_state);
18071272Slq150181 			}
18081272Slq150181 			break;
18091272Slq150181 
18101272Slq150181 		/* if fail, leave conskbd's led_state as it is */
18111272Slq150181 		case M_IOCNAK:
18121272Slq150181 			break;
18131272Slq150181 		}
18141272Slq150181 	}
18151272Slq150181 
18160Sstevel@tonic-gate 	/*
18171272Slq150181 	 * Basically, failure of setting/getting LED is not a fatal
18181272Slq150181 	 * error, so we will plumb the lower queue into conskbd whether
18191272Slq150181 	 * setting/getting LED succeeds or fails.
18200Sstevel@tonic-gate 	 */
18211828Scg149915 	qwriter(lqs->lqs_queue, mp, conskbd_link_lowque_virt, PERIM_OUTER);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate static void
18270Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
18280Sstevel@tonic-gate {
18290Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
18300Sstevel@tonic-gate 	struct iocblk		*iocp;
18310Sstevel@tonic-gate 	int			error;
18320Sstevel@tonic-gate 	dev_t			devt;
18330Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
18360Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	if (!msg) {
18390Sstevel@tonic-gate 		/*
18401828Scg149915 		 * Here we discard the response if:
18411828Scg149915 		 *
18421828Scg149915 		 *   1. It's an KIOCSLED request; see conskbd_streams_setled().
18431828Scg149915 		 *   2. The application has already closed the upper stream;
18441828Scg149915 		 *		see conskbdclose()
18450Sstevel@tonic-gate 		 */
18460Sstevel@tonic-gate 		freemsg(mp);
18470Sstevel@tonic-gate 		return;
18480Sstevel@tonic-gate 	}
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	/*
18510Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
18520Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
18530Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
18540Sstevel@tonic-gate 	 * the lower queue from which the current response message
18550Sstevel@tonic-gate 	 * comes.
18560Sstevel@tonic-gate 	 */
18570Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
18580Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
18590Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
18600Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
18610Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
18620Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
18630Sstevel@tonic-gate 
1864*5186Slq150181 	if (msg->kpm_resp_nums < msg->kpm_req_nums) {
1865*5186Slq150181 		mutex_exit(&msg->kpm_lock);
18660Sstevel@tonic-gate 		return;
1867*5186Slq150181 	}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
18700Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
18710Sstevel@tonic-gate 
1872*5186Slq150181 	mutex_exit(&msg->kpm_lock);
1873*5186Slq150181 
18740Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	/*
18780Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
18790Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
18800Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
18810Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
18820Sstevel@tonic-gate 	 * is sent to upstreams
18830Sstevel@tonic-gate 	 */
18840Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
18850Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
18880Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
18890Sstevel@tonic-gate 		/*
18900Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
18910Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
18920Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
18930Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
18940Sstevel@tonic-gate 		 * check if the pointer is null.
18950Sstevel@tonic-gate 		 */
18960Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18970Sstevel@tonic-gate 			cons_polledio_t		*polledio;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19000Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
19010Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
19020Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
19030Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
19040Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
19050Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
19060Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
19070Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
19080Sstevel@tonic-gate 					error = 0;
19090Sstevel@tonic-gate 				} else {
19100Sstevel@tonic-gate 					/*
19110Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
19120Sstevel@tonic-gate 					 * use the same cons_polledio structure
19130Sstevel@tonic-gate 					 * as conskbd.
19140Sstevel@tonic-gate 					 */
19150Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
19160Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
19170Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
19180Sstevel@tonic-gate 						    "driver does not support "
19190Sstevel@tonic-gate 						    "system debugging: %s",
19200Sstevel@tonic-gate 						    path);
19210Sstevel@tonic-gate 					}
19220Sstevel@tonic-gate 					error = EINVAL;
19230Sstevel@tonic-gate 				}
19240Sstevel@tonic-gate 			} else {
19250Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
19260Sstevel@tonic-gate 				    DDI_SUCCESS) {
19270Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
19280Sstevel@tonic-gate 					    " not available for system"
19290Sstevel@tonic-gate 					    " debugging:  %s", path);
19300Sstevel@tonic-gate 				}
19310Sstevel@tonic-gate 			}
19320Sstevel@tonic-gate 			mp->b_next = NULL;
19330Sstevel@tonic-gate 			mp->b_prev = NULL;
19340Sstevel@tonic-gate 			freemsg(mp);
19350Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19360Sstevel@tonic-gate 		}
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
19390Sstevel@tonic-gate 		if (error == 0) {
19400Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
19410Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 		break;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
19460Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19470Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19480Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
19490Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
19500Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
19510Sstevel@tonic-gate 				error = 0;
19520Sstevel@tonic-gate 			} else {
19530Sstevel@tonic-gate 				devt =
19540Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
19570Sstevel@tonic-gate 				    DDI_SUCCESS) {
19580Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
19590Sstevel@tonic-gate 					    " not available: %s", path);
19600Sstevel@tonic-gate 				}
19610Sstevel@tonic-gate 			}
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 			mp->b_next = NULL;
19640Sstevel@tonic-gate 			mp->b_prev = NULL;
19650Sstevel@tonic-gate 			freemsg(mp);
19660Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19670Sstevel@tonic-gate 		}
19680Sstevel@tonic-gate 		break;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	case KIOCCMD:
19715129Smarx 	case KIOCMKTONE:
19720Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19730Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
19760Sstevel@tonic-gate 				error = 0;
19770Sstevel@tonic-gate 			mp->b_next = NULL;
19780Sstevel@tonic-gate 			mp->b_prev = NULL;
19790Sstevel@tonic-gate 			freemsg(mp);
19800Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19810Sstevel@tonic-gate 		}
19820Sstevel@tonic-gate 		break;
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
19850Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
19890Sstevel@tonic-gate 	if (error == 0) {
19900Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
19910Sstevel@tonic-gate 	} else {
19920Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
19950Sstevel@tonic-gate 	iocp->ioc_error = error;
19960Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
19970Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
19980Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
20010Sstevel@tonic-gate 
20021828Scg149915 static void
20031828Scg149915 conskbd_link_lowque_legacy(queue_t *lowque, mblk_t *mp)
20041828Scg149915 {
20051828Scg149915 	conskbd_lower_queue_t *lqs;
20061828Scg149915 
20071828Scg149915 	freemsg(mp);
20081828Scg149915 
20091828Scg149915 	/*
20101828Scg149915 	 * Bypass the virutal keyboard for old hardware,
20111828Scg149915 	 * Now, only current legacy keyboard can be linked
20121828Scg149915 	 * under conskbd
20131828Scg149915 	 */
20141828Scg149915 	conskbd.conskbd_bypassed = B_TRUE;
20151828Scg149915 
20161828Scg149915 	/*
20171828Scg149915 	 * Link the lower queue under conskbd
20181828Scg149915 	 */
20191828Scg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
20201828Scg149915 	lqs->lqs_state = LQS_INITIALIZED_LEGACY;
20211828Scg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
20221828Scg149915 	conskbd.conskbd_lqueue_list = lqs;
20231828Scg149915 	conskbd.conskbd_lqueue_nums++;
20241828Scg149915 
20251828Scg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
20261828Scg149915 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
20271828Scg149915 
20281828Scg149915 }	/* conskbd_link_lowque_legacy() */
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate static void
20311828Scg149915 conskbd_link_lowque_virt(queue_t *lowque, mblk_t *mp)
20320Sstevel@tonic-gate {
20330Sstevel@tonic-gate 	int		index;
20341828Scg149915 	conskbd_lower_queue_t *lqs;
20350Sstevel@tonic-gate 
20361828Scg149915 	freemsg(mp);
20370Sstevel@tonic-gate 
20381828Scg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
20391828Scg149915 
20401828Scg149915 	ASSERT(lqs->lqs_queue == lowque);
20411828Scg149915 	ASSERT(lqs->lqs_pending_plink != NULL);
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	/*
20440Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
20450Sstevel@tonic-gate 	 */
20460Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
20470Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
20480Sstevel@tonic-gate 	}
20491828Scg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
20500Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
20511828Scg149915 	conskbd.conskbd_lqueue_nums++;
20521828Scg149915 	conskbd.conskbd_lqueue_list = lqs;
20531828Scg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
20540Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
20550Sstevel@tonic-gate 
20561828Scg149915 }	/* conskbd_link_lowque_virt() */
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate /*ARGSUSED*/
20590Sstevel@tonic-gate static void
20600Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
20610Sstevel@tonic-gate {
20620Sstevel@tonic-gate 	struct iocblk	*iocp;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	/*
20670Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
20680Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
20690Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
20700Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
20710Sstevel@tonic-gate 	 * should be added to this list.
20720Sstevel@tonic-gate 	 */
20730Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
20764925Sqz150045 	    (iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20794925Sqz150045 		    ("conskbd_legacy_upstream_msg: "
20804925Sqz150045 		    "CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
20810Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
20840Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20850Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
20900Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20910Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
20920Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
20930Sstevel@tonic-gate 	} else {
20940Sstevel@tonic-gate 		/* if reached here, it must be a error */
20950Sstevel@tonic-gate 		cmn_err(CE_WARN,
20960Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
20970Sstevel@tonic-gate 		freemsg(mp);
20980Sstevel@tonic-gate 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate /*
21030Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
21040Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
21050Sstevel@tonic-gate  *
21060Sstevel@tonic-gate  * 1) application initiated request
21070Sstevel@tonic-gate  * 	A KIOCSLED ioctl is sent by an application. The ioctl will be
21080Sstevel@tonic-gate  * 	be prcoessed by queue service procedure conskbduwsrv(), which
21090Sstevel@tonic-gate  * 	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
21100Sstevel@tonic-gate  * 	conskbd_streams_setled() to set LED, after that,  kbtrans will
21110Sstevel@tonic-gate  * 	return an ACK message to upper module.
21120Sstevel@tonic-gate  *
21130Sstevel@tonic-gate  * 2) Kbtrans initiated the request
21140Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
21150Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
21160Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
21170Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
21180Sstevel@tonic-gate  *
21190Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
21200Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
21210Sstevel@tonic-gate  * has no return value.
21220Sstevel@tonic-gate  */
21230Sstevel@tonic-gate static void
21240Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
21250Sstevel@tonic-gate {
21260Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21270Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
21280Sstevel@tonic-gate 	mblk_t		*req;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	if (led_state == -1)
21330Sstevel@tonic-gate 		return;
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	/*
21380Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
21390Sstevel@tonic-gate 	 * it if this happens.
21400Sstevel@tonic-gate 	 */
21410Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21420Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 		if (!req) {
21450Sstevel@tonic-gate 			continue;
21460Sstevel@tonic-gate 		}
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
21490Sstevel@tonic-gate 		if (!req->b_cont) {
21500Sstevel@tonic-gate 			freemsg(req);
21510Sstevel@tonic-gate 			continue;
21520Sstevel@tonic-gate 		}
21530Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
21540Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
21550Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
21560Sstevel@tonic-gate 			freemsg(req);
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate }	/* conskbd_streams_setled() */
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate static void
21620Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
21630Sstevel@tonic-gate {
21640Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21650Sstevel@tonic-gate 	struct cons_polledio		*cb;
21660Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21690Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21700Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
21710Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
21720Sstevel@tonic-gate 			    led_state);
21730Sstevel@tonic-gate 		}
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate static boolean_t
21790Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
21800Sstevel@tonic-gate 		kbtrans_key_t *keycode, enum keystate *state)
21810Sstevel@tonic-gate {
21820Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21830Sstevel@tonic-gate 	struct cons_polledio 		*cb;
21840Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21850Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
21880Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
21890Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21900Sstevel@tonic-gate 		if ((cb != NULL) &&
21910Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
21920Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
21930Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
21970Sstevel@tonic-gate 		if (ret == B_TRUE) {
21981097Slq150181 
21991097Slq150181 			/* A legacy keyboard ? */
22001097Slq150181 			if (conskbd.conskbd_bypassed == B_TRUE)
22011097Slq150181 				break;
22021097Slq150181 
22031097Slq150181 			/*
22041097Slq150181 			 * This is the PS2 scancode 0x2B -> USB(49) /
22051097Slq150181 			 * USB(50) keycode mapping workaround, for
22061097Slq150181 			 * polled mode.
22071097Slq150181 			 *
22081097Slq150181 			 * There are two possible USB keycode mappings
22091097Slq150181 			 * for PS2 scancode 0x2B and this workaround
22101097Slq150181 			 * makes sure that we use the USB keycode that
22111097Slq150181 			 * does not end up being mapped to a HOLE key
22121097Slq150181 			 * using the current keyboard translation
22131097Slq150181 			 * tables.
22141097Slq150181 			 *
22151097Slq150181 			 * See conskbdlrput() for a detailed
22161097Slq150181 			 * explanation of the problem.
22171097Slq150181 			 */
22181097Slq150181 			if (*keycode == 49 || *keycode == 50) {
22191097Slq150181 				if (conskbd_keyindex->k_normal[50] == HOLE)
22201097Slq150181 					*keycode = 49;
22211097Slq150181 				else
22221097Slq150181 					*keycode = 50;
22231097Slq150181 			}
22241097Slq150181 
22250Sstevel@tonic-gate 			break;
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 	}
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	return (ret);
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate static boolean_t
22340Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
22350Sstevel@tonic-gate {
22360Sstevel@tonic-gate 	struct iocblk		*iocp;
22370Sstevel@tonic-gate 	int		directio;
22380Sstevel@tonic-gate 	int		error;
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
22410Sstevel@tonic-gate 		return (B_FALSE);
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
22460Sstevel@tonic-gate 	case KIOCGDIRECT: {
22470Sstevel@tonic-gate 		/*
22480Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
22490Sstevel@tonic-gate 		 * respond incorrectly.
22500Sstevel@tonic-gate 		 */
22510Sstevel@tonic-gate 		register mblk_t *datap;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
22540Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
22550Sstevel@tonic-gate 			return (B_TRUE);
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
22590Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
22600Sstevel@tonic-gate 		if (mp->b_cont) {
22610Sstevel@tonic-gate 			freemsg(mp->b_cont);
22620Sstevel@tonic-gate 			mp->b_cont = NULL;
22630Sstevel@tonic-gate 		}
22640Sstevel@tonic-gate 		mp->b_cont = datap;
22650Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
22660Sstevel@tonic-gate 		return (B_TRUE);
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	case KIOCSDIRECT:
22700Sstevel@tonic-gate 		/*
22710Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
22720Sstevel@tonic-gate 		 * based code see it and respond to it.
22730Sstevel@tonic-gate 		 */
22740Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
22750Sstevel@tonic-gate 		if (error != 0) {
22760Sstevel@tonic-gate 			return (B_FALSE);
22770Sstevel@tonic-gate 		}
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
22800Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
22810Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
22820Sstevel@tonic-gate 			return (B_TRUE);
22830Sstevel@tonic-gate 		}
22840Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
22870Sstevel@tonic-gate 			kbtrans_streams_set_queue(
22880Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
22890Sstevel@tonic-gate 		} else {
22900Sstevel@tonic-gate 			kbtrans_streams_set_queue(
22910Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
22920Sstevel@tonic-gate 		}
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 		/*
22950Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
22960Sstevel@tonic-gate 		 */
22970Sstevel@tonic-gate 		return (B_FALSE);
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	default:
23000Sstevel@tonic-gate 		return (B_FALSE);
23010Sstevel@tonic-gate 	}
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate static void
23071762Slt200341 conskbd_polledio_enter(cons_polledio_arg_t arg)
23080Sstevel@tonic-gate {
23090Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
23100Sstevel@tonic-gate 	struct cons_polledio		*cb;
23110Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23140Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
23150Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
23160Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
23170Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
23180Sstevel@tonic-gate 		}
23190Sstevel@tonic-gate 	}
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate static void
23241762Slt200341 conskbd_polledio_exit(cons_polledio_arg_t arg)
23250Sstevel@tonic-gate {
23260Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
23270Sstevel@tonic-gate 	struct cons_polledio		*cb;
23280Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23310Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
23320Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
23330Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
23340Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
23350Sstevel@tonic-gate 		}
23360Sstevel@tonic-gate 	}
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate static int
23411762Slt200341 conskbd_polledio_getchar(cons_polledio_arg_t arg)
23420Sstevel@tonic-gate {
23430Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate static int
23521762Slt200341 conskbd_polledio_ischar(cons_polledio_arg_t arg)
23530Sstevel@tonic-gate {
23540Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate static void
23640Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
23650Sstevel@tonic-gate {
23660Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23670Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
23680Sstevel@tonic-gate 	conskbd_msg_queue = msg;
23690Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate /*
23740Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
23750Sstevel@tonic-gate  */
23760Sstevel@tonic-gate static conskbd_pending_msg_t *
23770Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
23780Sstevel@tonic-gate {
23790Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
23800Sstevel@tonic-gate 	struct iocblk		*iocp;
23810Sstevel@tonic-gate 	uint_t	id;
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23840Sstevel@tonic-gate 	msg = conskbd_msg_queue;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
23870Sstevel@tonic-gate 	ASSERT(iocp);
23880Sstevel@tonic-gate 	id = iocp->ioc_id;
23890Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
23900Sstevel@tonic-gate 		msg = msg->kpm_next;
23910Sstevel@tonic-gate 	}
23920Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	return (msg);
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate static void
24000Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
24010Sstevel@tonic-gate {
24020Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
24030Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
24060Sstevel@tonic-gate 	prev = conskbd_msg_queue;
24070Sstevel@tonic-gate 
2408*5186Slq150181 	for (p = prev; p && p != msg; p = p->kpm_next)
24090Sstevel@tonic-gate 		prev = p;
2410*5186Slq150181 
24110Sstevel@tonic-gate 	ASSERT(p && p == msg);
2412*5186Slq150181 
24130Sstevel@tonic-gate 	if (prev == p) {
24140Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
24150Sstevel@tonic-gate 	} else {
24160Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate 	p->kpm_next = NULL;
24190Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate #ifdef DEBUG
24240Sstevel@tonic-gate /*ARGSUSED*/
24250Sstevel@tonic-gate void
24260Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
24270Sstevel@tonic-gate {
24280Sstevel@tonic-gate 	char buf[256];
24290Sstevel@tonic-gate 	va_list ap;
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	va_start(ap, fmt);
24320Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
24330Sstevel@tonic-gate 	va_end(ap);
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate }	/* conskbd_dprintf() */
24380Sstevel@tonic-gate #endif
2439