xref: /onnv-gate/usr/src/uts/common/io/conskbd.c (revision 10617:ae54b3d31f50)
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 /*
23*10617SPengcheng.Chen@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Console kbd multiplexor driver for Sun.
300Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "kbd" module pushed
310Sstevel@tonic-gate  * on top of it.
320Sstevel@tonic-gate  * Minor device 0 is what programs normally use.
330Sstevel@tonic-gate  * Minor device 1 is used to feed predigested keystrokes to the "workstation
340Sstevel@tonic-gate  * console" driver, which it is linked beneath.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *     This module can support multiple keyboards to be used simultaneously.
380Sstevel@tonic-gate  * and enable users to use at a time multiple keyboards connected to the
390Sstevel@tonic-gate  * same system. All the keyboards are linked under conskbd, and act as a
400Sstevel@tonic-gate  * keyboard with replicated keys.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
430Sstevel@tonic-gate  * a two-level architecure. The lower one is one of serialport drivers, such
440Sstevel@tonic-gate  * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
450Sstevel@tonic-gate  * drivers don't support polled I/O interfaces, we couldn't group the keyboard
460Sstevel@tonic-gate  * of this kind under conskbd. So we do as the follows:
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
490Sstevel@tonic-gate  *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
500Sstevel@tonic-gate  *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
510Sstevel@tonic-gate  *     requesting to be linked under conskbd. If the lower driver does't
520Sstevel@tonic-gate  *     recognize this ioctl, the virtual keyboard will be disabled so that
530Sstevel@tonic-gate  *     only one keyboard instance could be linked under conskbd.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/types.h>
580Sstevel@tonic-gate #include <sys/param.h>
590Sstevel@tonic-gate #include <sys/stropts.h>
600Sstevel@tonic-gate #include <sys/stream.h>
610Sstevel@tonic-gate #include <sys/strsubr.h>
620Sstevel@tonic-gate #include <sys/strsun.h>
630Sstevel@tonic-gate #include <sys/conf.h>
640Sstevel@tonic-gate #include <sys/stat.h>
650Sstevel@tonic-gate #include <sys/errno.h>
660Sstevel@tonic-gate #include <sys/modctl.h>
670Sstevel@tonic-gate #include <sys/kbio.h>
680Sstevel@tonic-gate #include <sys/ddi.h>
690Sstevel@tonic-gate #include <sys/sunddi.h>
700Sstevel@tonic-gate #include <sys/consdev.h>
710Sstevel@tonic-gate #include <sys/note.h>
720Sstevel@tonic-gate #include <sys/kmem.h>
730Sstevel@tonic-gate #include <sys/kstat.h>
740Sstevel@tonic-gate #include <sys/policy.h>
750Sstevel@tonic-gate #include <sys/kbd.h>
760Sstevel@tonic-gate #include <sys/kbtrans.h>
770Sstevel@tonic-gate #include <sys/promif.h>
780Sstevel@tonic-gate #include <sys/vuid_event.h>
790Sstevel@tonic-gate #include <sys/conskbd.h>
803497Srz201010 #include <sys/beep.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate extern struct keyboard *kbtrans_usbkb_maptab_init(void);
830Sstevel@tonic-gate extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
840Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Module linkage routines for the kernel
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
900Sstevel@tonic-gate static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
910Sstevel@tonic-gate static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * STREAMS queue processing procedures
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate static void	conskbduwsrv(queue_t *);
970Sstevel@tonic-gate static void	conskbdlwserv(queue_t *);
980Sstevel@tonic-gate static void	conskbdlrput(queue_t *, mblk_t *);
990Sstevel@tonic-gate static int	conskbdclose(queue_t *, int, cred_t *);
1000Sstevel@tonic-gate static int	conskbdopen(queue_t *, dev_t *, int, int, cred_t *);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /* STREAMS driver id and limit value struct */
1040Sstevel@tonic-gate static struct module_info conskbdm_info = {
1050Sstevel@tonic-gate 	0,		/* mi_idnum */
1060Sstevel@tonic-gate 	"conskbd",	/* mi_idname */
1070Sstevel@tonic-gate 	0,		/* mi_minpsz */
1080Sstevel@tonic-gate 	1024,		/* mi_maxpsz */
1090Sstevel@tonic-gate 	2048,		/* mi_hiwat */
1100Sstevel@tonic-gate 	128		/* mi_lowat */
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * STREAMS queue processing procedure structures
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate /* upper read queue processing procedure structures */
1170Sstevel@tonic-gate static struct qinit conskbdurinit = {
1180Sstevel@tonic-gate 	NULL,			/* qi_putp */
1190Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_srvp */
1200Sstevel@tonic-gate 	conskbdopen,		/* qi_qopen */
1210Sstevel@tonic-gate 	conskbdclose,		/* qi_qclose */
1220Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_qadmin */
1230Sstevel@tonic-gate 	&conskbdm_info,		/* qi_minfo */
1240Sstevel@tonic-gate 	NULL			/* qi_mstat */
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /* upper write queue processing procedures structuresi */
1280Sstevel@tonic-gate static struct qinit conskbduwinit = {
1290Sstevel@tonic-gate 	(int (*)())putq,		/* qi_putp */
1300Sstevel@tonic-gate 	(int (*)())conskbduwsrv,	/* qi_srvp */
1310Sstevel@tonic-gate 	conskbdopen,			/* qi_qopen */
1320Sstevel@tonic-gate 	conskbdclose,			/* qi_qclose */
1330Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1340Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1350Sstevel@tonic-gate 	NULL				/* qi_mstat */
1360Sstevel@tonic-gate };
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /* lower read queue processing procedures structures */
1390Sstevel@tonic-gate static struct qinit conskbdlrinit = {
1400Sstevel@tonic-gate 	(int (*)())conskbdlrput,	/* qi_putp */
1410Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_srvp */
1420Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1430Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1440Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1450Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1460Sstevel@tonic-gate 	NULL				/* qi_mstat */
1470Sstevel@tonic-gate };
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* lower write processing procedures structures */
1500Sstevel@tonic-gate static struct qinit conskbdlwinit = {
1510Sstevel@tonic-gate 	putq,				/* qi_putp */
1520Sstevel@tonic-gate 	(int (*)())conskbdlwserv,	/* qi_srvp */
1530Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1540Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1550Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1560Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1570Sstevel@tonic-gate 	NULL				/* qi_mstat */
1580Sstevel@tonic-gate };
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /* STREAMS entity declaration structure */
1610Sstevel@tonic-gate static struct streamtab conskbd_str_info = {
1620Sstevel@tonic-gate 	&conskbdurinit,		/* st_rdinit */
1630Sstevel@tonic-gate 	&conskbduwinit,		/* st_wrinit */
1640Sstevel@tonic-gate 	&conskbdlrinit,		/* st_muxrinit */
1650Sstevel@tonic-gate 	&conskbdlwinit,		/* st_muxwinit */
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /* Entry points structure */
1700Sstevel@tonic-gate static 	struct cb_ops cb_conskbd_ops = {
1710Sstevel@tonic-gate 	nulldev,		/* cb_open */
1720Sstevel@tonic-gate 	nulldev,		/* cb_close */
1730Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1740Sstevel@tonic-gate 	nodev,			/* cb_print */
1750Sstevel@tonic-gate 	nodev,			/* cb_dump */
1760Sstevel@tonic-gate 	nodev,			/* cb_read */
1770Sstevel@tonic-gate 	nodev,			/* cb_write */
1780Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
1790Sstevel@tonic-gate 	nodev,			/* cb_devmap */
1800Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1810Sstevel@tonic-gate 	nodev,			/* cb_segmap */
1820Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1830Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1840Sstevel@tonic-gate 	&conskbd_str_info,	/* cb_stream */
1851828Scg149915 	D_MP | D_MTOUTPERIM | D_MTOCEXCL	/* cb_flag */
1860Sstevel@tonic-gate };
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Device operations structure
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static struct dev_ops conskbd_ops = {
1930Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1940Sstevel@tonic-gate 	0,			/* devo_refcnt */
1950Sstevel@tonic-gate 	conskbd_info,		/* devo_getinfo */
1960Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1970Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1980Sstevel@tonic-gate 	conskbd_attach,		/* devo_attach */
1990Sstevel@tonic-gate 	conskbd_detach,		/* devo_detach */
2000Sstevel@tonic-gate 	nodev,			/* devo_reset */
2010Sstevel@tonic-gate 	&(cb_conskbd_ops),	/* devo_cb_ops */
2020Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2037656SSherry.Moore@Sun.COM 	NULL,			/* devo_power */
2047656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
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 */
2127656SSherry.Moore@Sun.COM 	"conskbd multiplexer driver",
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 *);
2927187Shh224818 static mblk_t *conskbd_alloc_firm_event(ushort_t, 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
_init(void)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
_fini(void)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
_info(struct modinfo * modinfop)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
conskbd_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)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
conskbd_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)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
conskbd_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)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
conskbdopen(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * crp)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 
566*10617SPengcheng.Chen@Sun.COM 	if (secpolicy_console(crp) != 0)
567*10617SPengcheng.Chen@Sun.COM 		return (EPERM);
568*10617SPengcheng.Chen@Sun.COM 
5690Sstevel@tonic-gate 	/*
5701097Slq150181 	 * initialize kbtrans module for conskbd
5710Sstevel@tonic-gate 	 */
572*10617SPengcheng.Chen@Sun.COM 	err = kbtrans_streams_init(q, sflag, (struct kbtrans_hardware *)
5730Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
5740Sstevel@tonic-gate 	if (err != 0)
5750Sstevel@tonic-gate 		return (err);
5760Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
5770Sstevel@tonic-gate 	    conskbd_keyindex);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
5800Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
5811762Slt200341 	    (cons_polledio_arg_t)&conskbd;
5820Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
5830Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
5841762Slt200341 	    (int (*)(cons_polledio_arg_t)) conskbd_polledio_getchar;
5850Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
5861762Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))conskbd_polledio_ischar;
5870Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
5880Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
5890Sstevel@tonic-gate 	qprocson(q);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	return (0);
5920Sstevel@tonic-gate 
5931828Scg149915 }	/* conskbdopen() */
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /*ARGSUSED*/
5970Sstevel@tonic-gate static int
conskbdclose(queue_t * q,int flag,cred_t * crp)5980Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
6010Sstevel@tonic-gate 
6021828Scg149915 		conskbd_pending_msg_t	*pmsg, *prev, *next;
6031828Scg149915 		mblk_t		*mp;
6041828Scg149915 
6050Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
6060Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
6090Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
6100Sstevel@tonic-gate 		    conskbd_consqueue);
6110Sstevel@tonic-gate 		qprocsoff(q);
6120Sstevel@tonic-gate 		conskbd_regqueue = NULL;
6131828Scg149915 
6141828Scg149915 		/*
6151828Scg149915 		 * If there are any pending ioctls which conskbd hasn't
6161828Scg149915 		 * responded to yet, remove them from conskbd_msg_queue.
6171828Scg149915 		 * Otherwise, we might send the response to a nonexistent
6181828Scg149915 		 * closed queue. Refer to: conskbd_mux_upstream_msg().
6191828Scg149915 		 */
6201828Scg149915 		for (prev = NULL, pmsg = conskbd_msg_queue; pmsg != NULL;
6211828Scg149915 		    pmsg = next) {
6221828Scg149915 			next = pmsg->kpm_next;
6232092Scg149915 			if (pmsg->kpm_upper_queue == WR(q)) {
6241828Scg149915 				if (prev == NULL)
6251828Scg149915 					conskbd_msg_queue = next;
6261828Scg149915 				else
6271828Scg149915 					prev->kpm_next = next;
6281828Scg149915 
6291828Scg149915 				while (pmsg->kpm_resp_list != NULL) {
6301828Scg149915 					mp = pmsg->kpm_resp_list;
6311828Scg149915 					pmsg->kpm_resp_list = mp->b_next;
6321828Scg149915 					mp->b_next = mp->b_prev = NULL;
6331828Scg149915 					freemsg(mp);
6341828Scg149915 				}
6351828Scg149915 				mutex_destroy(&pmsg->kpm_lock);
6361828Scg149915 				kmem_free(pmsg, sizeof (*pmsg));
6371828Scg149915 			} else {
6381828Scg149915 				prev = pmsg;
6391828Scg149915 			}
6401828Scg149915 		}
6410Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
6420Sstevel@tonic-gate 		/*
6430Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
6440Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
6450Sstevel@tonic-gate 		 */
6460Sstevel@tonic-gate 		qprocsoff(q);
6470Sstevel@tonic-gate 		conskbd_consqueue = NULL;
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	return (0);
6510Sstevel@tonic-gate 
6521828Scg149915 }	/* conskbdclose() */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * Service procedure for upper write queue.
6560Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
6570Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
6580Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
6590Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
6600Sstevel@tonic-gate  * deal with all messages.
6610Sstevel@tonic-gate  * 	This routine is invoked when ioctl commands are send down
6620Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
6630Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
6640Sstevel@tonic-gate  * the led states.
6650Sstevel@tonic-gate  */
6660Sstevel@tonic-gate static void
conskbduwsrv(queue_t * q)6670Sstevel@tonic-gate conskbduwsrv(queue_t *q)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	mblk_t	*mp;
6700Sstevel@tonic-gate 	queue_t	*oldq;
6710Sstevel@tonic-gate 	enum kbtrans_message_response ret;
6723497Srz201010 	struct copyresp *csp;
6733497Srz201010 	struct freq_request *frqp;
6743497Srz201010 	int error;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		/*
6790Sstevel@tonic-gate 		 * if the virtual keyboard is supported
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
6840Sstevel@tonic-gate 				continue;
6850Sstevel@tonic-gate 			/*
6860Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
6870Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
6880Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
6890Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
6900Sstevel@tonic-gate 			 * and regular queue.
6910Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
6920Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
6930Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
6940Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
6950Sstevel@tonic-gate 			 * to be switched between regular queue and console
6960Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
6970Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
6980Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
6990Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
7000Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
7010Sstevel@tonic-gate 			 * need to return response to right queue.
7020Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
7030Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
7040Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
7050Sstevel@tonic-gate 			 * and then switch back to old queue.
7060Sstevel@tonic-gate 			 */
7070Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
7080Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
7090Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7100Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
7110Sstevel@tonic-gate 			ret = kbtrans_streams_message(
7120Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
7130Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7140Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 			switch (ret) {
7170Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
7180Sstevel@tonic-gate 					continue;
7190Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
7200Sstevel@tonic-gate 					break;
7210Sstevel@tonic-gate 			}
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		case M_IOCTL:
7271828Scg149915 			conskbd_ioctl(q, mp);
7280Sstevel@tonic-gate 			break;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		case M_FLUSH:
7310Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
7320Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
7330Sstevel@tonic-gate 			}
7340Sstevel@tonic-gate 			/*
7350Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
7360Sstevel@tonic-gate 			 * may be lost so that upper module or applications
7370Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
7380Sstevel@tonic-gate 			 * ever.
7390Sstevel@tonic-gate 			 */
7400Sstevel@tonic-gate 			freemsg(mp);
7410Sstevel@tonic-gate 			break;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		case M_DATA:
7440Sstevel@tonic-gate 			/*
7450Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
7460Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
7470Sstevel@tonic-gate 			 * down to lower queue.
7480Sstevel@tonic-gate 			 */
7490Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
7500Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
7510Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
7520Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
7530Sstevel@tonic-gate 					freemsg(mp);
7540Sstevel@tonic-gate 			} else {
7550Sstevel@tonic-gate 				freemsg(mp);
7560Sstevel@tonic-gate 			}
7570Sstevel@tonic-gate 			break;
7580Sstevel@tonic-gate 
7593497Srz201010 		case M_IOCDATA:
7603497Srz201010 			/*
7613497Srz201010 			 * Only deal with copyresp to KIOCSETFREQ
7623497Srz201010 			 * transparent ioctl now
7633497Srz201010 			 */
7643497Srz201010 			csp = (struct copyresp *)mp->b_rptr;
7653497Srz201010 			if (csp->cp_rval) {
7663497Srz201010 				miocnak(q, mp, 0, EINVAL);
7673497Srz201010 				break;
7683497Srz201010 			}
7693497Srz201010 
7703497Srz201010 			error = 0;
7713497Srz201010 			switch (csp->cp_cmd) {
7723497Srz201010 			case KIOCSETFREQ:
7733497Srz201010 				frqp = (struct freq_request *)mp->
7744925Sqz150045 				    b_cont->b_rptr;
7753497Srz201010 
7763497Srz201010 				switch (frqp->type) {
7773497Srz201010 				case CONSOLE_BEEP:
7783497Srz201010 					error = beeper_freq(BEEP_CONSOLE,
7794925Sqz150045 					    (int)frqp->freq);
7803497Srz201010 						break;
7813497Srz201010 
7823497Srz201010 				case KBD_BEEP:
7833497Srz201010 					error = beeper_freq(BEEP_TYPE4,
7844925Sqz150045 					    (int)frqp->freq);
7853497Srz201010 						break;
7863497Srz201010 
7873497Srz201010 				default:
7883497Srz201010 					error = 1;
7893497Srz201010 				} /* frqp->type */
7903497Srz201010 
7913497Srz201010 				break;
7923497Srz201010 
7933497Srz201010 			default:
7943497Srz201010 				error = 1;
7953497Srz201010 			} /* csp->cp_cmd */
7963497Srz201010 
7973497Srz201010 			if (error == 0)
7983497Srz201010 				miocack(q, mp, 0, 0);
7993497Srz201010 			else
8003497Srz201010 				miocnak(q, mp, 0, EINVAL);
8013497Srz201010 
8023497Srz201010 			break;
8033497Srz201010 
8040Sstevel@tonic-gate 		default:
8050Sstevel@tonic-gate 			/*
8060Sstevel@tonic-gate 			 * Pass an error message up.
8070Sstevel@tonic-gate 			 */
8080Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
8090Sstevel@tonic-gate 			if (mp->b_cont) {
8100Sstevel@tonic-gate 				freemsg(mp->b_cont);
8110Sstevel@tonic-gate 				mp->b_cont = NULL;
8120Sstevel@tonic-gate 			}
8130Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
8140Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
8150Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
8160Sstevel@tonic-gate 			qreply(q, mp);
8170Sstevel@tonic-gate 		}
8180Sstevel@tonic-gate 	}	/* end of while */
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate }	/* conskbduwsrv() */
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate static void
conskbd_ioctl(queue_t * q,mblk_t * mp)8231828Scg149915 conskbd_ioctl(queue_t *q, mblk_t *mp)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	struct	iocblk			*iocp;
8260Sstevel@tonic-gate 	int	error = 0;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	case I_LINK:
8330Sstevel@tonic-gate 	case I_PLINK:
834267Scg149915 		if (conskbd.conskbd_bypassed == B_TRUE) {
835267Scg149915 		/*
836267Scg149915 		 * A legacy keyboard can NOT be connected to conskbd together
837267Scg149915 		 * with other keyboards. So when a legacy keyboard is already
838267Scg149915 		 * linked under conkbd, we just reject all others.
839267Scg149915 		 */
840267Scg149915 			miocnak(q, mp, 0, EAGAIN);
841267Scg149915 			break;
842267Scg149915 		}
8431828Scg149915 		qwriter(q, mp, conskbd_ioc_plink, PERIM_OUTER);
8440Sstevel@tonic-gate 		break;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	case I_UNLINK:
8470Sstevel@tonic-gate 	case I_PUNLINK:
8481828Scg149915 		qwriter(q, mp, conskbd_ioc_punlink, PERIM_OUTER);
8490Sstevel@tonic-gate 		break;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	case KIOCSKABORTEN:
8520Sstevel@tonic-gate 		/*
8530Sstevel@tonic-gate 		 * Check if privileged
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
8560Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8570Sstevel@tonic-gate 			return;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8610Sstevel@tonic-gate 		if (error != 0) {
8620Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8630Sstevel@tonic-gate 			return;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
8670Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8680Sstevel@tonic-gate 		break;
8690Sstevel@tonic-gate 
8703497Srz201010 	case KIOCSETFREQ:
8713497Srz201010 		if (iocp->ioc_count != TRANSPARENT) {
8723497Srz201010 			/*
8733497Srz201010 			 * We don't support non-transparent ioctls,
8743497Srz201010 			 * i.e. I_STR ioctls
8753497Srz201010 			 */
8763497Srz201010 			miocnak(q, mp, 0, EINVAL);
8773497Srz201010 		} else {
8783497Srz201010 			/* Transparent ioctl */
8793497Srz201010 			mcopyin(mp, NULL, sizeof (struct freq_request), NULL);
8803497Srz201010 			qreply(q, mp);
8813497Srz201010 		}
8823497Srz201010 		break;
8833497Srz201010 
8840Sstevel@tonic-gate 	default:
8850Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
8860Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
8870Sstevel@tonic-gate 		} else {
8880Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8921828Scg149915 }	/* conskbd_ioctl() */
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate static void
conskbd_virtual_kbd_ioctl(queue_t * q,mblk_t * mp)8960Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	struct iocblk		*iocp;
8990Sstevel@tonic-gate 	mblk_t			*datap;
9000Sstevel@tonic-gate 	int			cmd;
9010Sstevel@tonic-gate 	int			error = 0;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
9060Sstevel@tonic-gate 	case KIOCLAYOUT:
9070Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9080Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
9090Sstevel@tonic-gate 			break;
9100Sstevel@tonic-gate 		}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
9130Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9140Sstevel@tonic-gate 		else
9150Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9180Sstevel@tonic-gate 		if (mp->b_cont)
9190Sstevel@tonic-gate 			freemsg(mp->b_cont);
9200Sstevel@tonic-gate 		mp->b_cont = datap;
9210Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
9220Sstevel@tonic-gate 		break;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	case KIOCSLAYOUT:
9250Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
9260Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9270Sstevel@tonic-gate 			break;
9280Sstevel@tonic-gate 		}
9294925Sqz150045 		kbd_layout_bak = conskbd.conskbd_layout;
9300Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
9314925Sqz150045 		if (conskbd.conskbd_layout != kbd_layout_bak) {
9324925Sqz150045 
9334925Sqz150045 			/* notify the upper of the change event */
9344925Sqz150045 			if ((datap = conskbd_alloc_firm_event(
9354925Sqz150045 			    KEYBOARD_LAYOUT_CHANGE,
9364925Sqz150045 			    conskbd.conskbd_layout)) != NULL) {
9374925Sqz150045 				if (conskbd.conskbd_directio) {
9384925Sqz150045 					putnext(conskbd_regqueue, datap);
9394925Sqz150045 				} else {
9404925Sqz150045 					freemsg(datap);
9414925Sqz150045 				}
9424925Sqz150045 			}
9434925Sqz150045 		}
9440Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9450Sstevel@tonic-gate 		break;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
9480Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
9490Sstevel@tonic-gate 		if (error != 0) {
9500Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9510Sstevel@tonic-gate 			break;
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
9540Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9550Sstevel@tonic-gate 			break;
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9580Sstevel@tonic-gate 		break;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
9610Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
9620Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9630Sstevel@tonic-gate 			break;
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9660Sstevel@tonic-gate 		break;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	case CONSSETABORTENABLE:
9690Sstevel@tonic-gate 		/*
9700Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
9710Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
9720Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
9730Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
9740Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
9750Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
9760Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
9770Sstevel@tonic-gate 		 *
9780Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
9790Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
9800Sstevel@tonic-gate 		 * already handle the message though it returned to us a
9810Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
9820Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
9830Sstevel@tonic-gate 		 * So we just return ACK to upper module.
9840Sstevel@tonic-gate 		 */
9850Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9860Sstevel@tonic-gate 		break;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	case KIOCCMD:
9895129Smarx 	case KIOCMKTONE:
9900Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
9910Sstevel@tonic-gate 		    mp->b_cont == NULL) {
9920Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9930Sstevel@tonic-gate 			break;
9940Sstevel@tonic-gate 		}
9950Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
9960Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
9970Sstevel@tonic-gate 			freemsg(mp->b_cont);
9980Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
9990Sstevel@tonic-gate 			if (datap == NULL) {
10000Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10010Sstevel@tonic-gate 				return;
10020Sstevel@tonic-gate 			}
10030Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
10040Sstevel@tonic-gate 				*(int *)datap->b_wptr =
10050Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
10060Sstevel@tonic-gate 			else
10070Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 			mp->b_cont = datap;
10100Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
10110Sstevel@tonic-gate 			return;
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
10140Sstevel@tonic-gate 		break;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	default:
10170Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10180Sstevel@tonic-gate 		break;
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate static void
conskbd_legacy_kbd_ioctl(queue_t * q,mblk_t * mp)10240Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
10270Sstevel@tonic-gate 	struct	iocblk		*iocp;
10280Sstevel@tonic-gate 	int	error = 0;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
10330Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	case KIOCGDIRECT: {
10360Sstevel@tonic-gate 		mblk_t *datap;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
10390Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
10400Sstevel@tonic-gate 			break;
10410Sstevel@tonic-gate 		}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
10440Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
10450Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
10460Sstevel@tonic-gate 			freemsg(mp->b_cont);
10470Sstevel@tonic-gate 			mp->b_cont = NULL;
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 		mp->b_cont = datap;
10500Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
10510Sstevel@tonic-gate 		break;
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	case KIOCSDIRECT:
10550Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
10560Sstevel@tonic-gate 		if (error != 0) {
10570Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
10580Sstevel@tonic-gate 			break;
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 		/*
10630Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
10640Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
10650Sstevel@tonic-gate 		 * itself.
10660Sstevel@tonic-gate 		 */
10670Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10680Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10690Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10700Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10710Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10720Sstevel@tonic-gate 				return;
10730Sstevel@tonic-gate 			}
10740Sstevel@tonic-gate 			break;
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
10780Sstevel@tonic-gate 		break;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	default:
10810Sstevel@tonic-gate 		/*
10820Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
10830Sstevel@tonic-gate 		 * through to; otherwise, reject it.
10840Sstevel@tonic-gate 		 */
10850Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10860Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10870Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10880Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10890Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10900Sstevel@tonic-gate 				return;
10910Sstevel@tonic-gate 			}
10920Sstevel@tonic-gate 			break;
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 		/* nobody below us; reject it */
10960Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10970Sstevel@tonic-gate 		break;
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate  * Service procedure for lower write queue.
11050Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
11060Sstevel@tonic-gate  */
11070Sstevel@tonic-gate static void
conskbdlwserv(queue_t * q)11080Sstevel@tonic-gate conskbdlwserv(queue_t *q)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	register mblk_t *mp;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
11130Sstevel@tonic-gate 		putnext(q, mp);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate }	/* conskbdlwserv() */
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Put procedure for lower read queue.
11190Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
11200Sstevel@tonic-gate  * device 1.
11210Sstevel@tonic-gate  */
11220Sstevel@tonic-gate static void
conskbdlrput(queue_t * q,mblk_t * mp)11230Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
11260Sstevel@tonic-gate 	struct iocblk 	*iocp;
11270Sstevel@tonic-gate 	Firm_event	*fe;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	case M_FLUSH:
11340Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
11350Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
11360Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
11370Sstevel@tonic-gate 		}
11380Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
11390Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
11400Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
11410Sstevel@tonic-gate 		} else
11420Sstevel@tonic-gate 			freemsg(mp);
11430Sstevel@tonic-gate 		break;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	case M_DATA:
11460Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			/*
11510Sstevel@tonic-gate 			 * This is a workaround.
11520Sstevel@tonic-gate 			 *
11530Sstevel@tonic-gate 			 * According to HID specification, there are the
11540Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
11550Sstevel@tonic-gate 			 *
11560Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
11570Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
11580Sstevel@tonic-gate 			 *
11590Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
11600Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
11610Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
11620Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
11630Sstevel@tonic-gate 			 * not know which is being pressed or released when we
11640Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
11650Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
11660Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
11670Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
11680Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
11690Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
11700Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
11710Sstevel@tonic-gate 			 * the key to 49.
11720Sstevel@tonic-gate 			 */
11730Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
11740Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
11750Sstevel@tonic-gate 					fe->id = 49;
11760Sstevel@tonic-gate 				else
11770Sstevel@tonic-gate 					fe->id = 50;
11780Sstevel@tonic-gate 			}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 			/*
11810Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
11820Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
11830Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
11840Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
11850Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
11860Sstevel@tonic-gate 			 * as held-down for ever.
11870Sstevel@tonic-gate 			 */
11880Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
11890Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
11900Sstevel@tonic-gate 			if (fe->value)
11910Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
11920Sstevel@tonic-gate 			else
11930Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
11960Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
11970Sstevel@tonic-gate 			freemsg(mp);
11980Sstevel@tonic-gate 		} else {
11990Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
12000Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
12010Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
12020Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
12030Sstevel@tonic-gate 			else
12040Sstevel@tonic-gate 				freemsg(mp);
12050Sstevel@tonic-gate 		}
12060Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
12070Sstevel@tonic-gate 		break;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	case M_IOCACK:
12100Sstevel@tonic-gate 	case M_IOCNAK:
12110Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
12120Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
12150Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
12180Sstevel@tonic-gate 		break;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	case M_ERROR:
12210Sstevel@tonic-gate 	case M_HANGUP:
12220Sstevel@tonic-gate 	default:
12230Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
12240Sstevel@tonic-gate 		break;
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate }	/* conskbdlrput() */
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate /* ARGSUSED */
12310Sstevel@tonic-gate static int
conskbd_kstat_update(kstat_t * ksp,int rw)12320Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
12330Sstevel@tonic-gate {
12340Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
12350Sstevel@tonic-gate 		return (EACCES);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	return (0);
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate }	/* conskbd_kstat_update() */
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
12450Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
12460Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
12470Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
12480Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
12490Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
12500Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
12510Sstevel@tonic-gate  * request matching received responses by searching the global linked
12520Sstevel@tonic-gate  * list to find the request which has the same message ID of the
12530Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
12540Sstevel@tonic-gate  * give a response to upstreams based the following policy:
12550Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
12560Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
12570Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
12580Sstevel@tonic-gate  * the first response is sent to upstream.
12590Sstevel@tonic-gate  */
12600Sstevel@tonic-gate static void
conskbd_handle_downstream_msg(queue_t * q,mblk_t * mp)12610Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
12620Sstevel@tonic-gate {
12630Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
12640Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
12650Sstevel@tonic-gate 	struct iocblk	*iocp;
12660Sstevel@tonic-gate 	mblk_t		*clonemp;
12670Sstevel@tonic-gate 	int		retry;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
12700Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
12710Sstevel@tonic-gate 		return;
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
12750Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
12760Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
12770Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
12780Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
12810Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
12825129Smarx 	    iocp->ioc_cmd == KIOCCMD ||
12835129Smarx 	    iocp->ioc_cmd == KIOCMKTONE);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
12860Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
12870Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
12880Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
12890Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
12900Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		/*
12950Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
12960Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
12970Sstevel@tonic-gate 		 * otherwise, system will panic.
12980Sstevel@tonic-gate 		 */
12990Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
13000Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
13010Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13020Sstevel@tonic-gate 			msg->kpm_req_nums --;
13030Sstevel@tonic-gate 			retry = 0;
13040Sstevel@tonic-gate 			continue;
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 		clonemp = copymsg(mp);
13080Sstevel@tonic-gate 		if (clonemp != NULL) {
13090Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
13100Sstevel@tonic-gate 				lqs = lqs->lqs_next;
13110Sstevel@tonic-gate 				retry = 0;
13120Sstevel@tonic-gate 				continue;
13130Sstevel@tonic-gate 			}
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 			/*
13160Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
13170Sstevel@tonic-gate 			 */
13180Sstevel@tonic-gate 			freemsg(clonemp);
13190Sstevel@tonic-gate 		}
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 		/*
13220Sstevel@tonic-gate 		 * During testing it was observed that occasionally
13230Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
13240Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
13250Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
13260Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
13270Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
13280Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
13290Sstevel@tonic-gate 		 *
13300Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
13310Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
13320Sstevel@tonic-gate 		 * or behave weirdly,
13330Sstevel@tonic-gate 		 *
13340Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
13350Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
13360Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
13370Sstevel@tonic-gate 		 *	entering kmdb;
13380Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
13390Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
13400Sstevel@tonic-gate 		 *	is not available in normal mode once returning
13410Sstevel@tonic-gate 		 *	from kmdb;
13420Sstevel@tonic-gate 		 * 3) for KIOCCMD
13430Sstevel@tonic-gate 		 * 	3.1) for KBD_CMD_NOBELL
13440Sstevel@tonic-gate 		 * 		there's no beep in USB and PS2 keyboard,
13450Sstevel@tonic-gate 		 * 		this ioctl actually disables the beep on
13460Sstevel@tonic-gate 		 * 		system mainboard. Note that all the cloned
13470Sstevel@tonic-gate 		 * 		messages sent down to lower queues do the
13480Sstevel@tonic-gate 		 * 		same job for system mainboard. Therefore,
13490Sstevel@tonic-gate 		 * 		even if we fail to send this ioctl to most
13500Sstevel@tonic-gate 		 * 		of lower queues, the beep still would be
13510Sstevel@tonic-gate 		 * 		disabled. So, no trouble exists here.
13520Sstevel@tonic-gate 		 *	3.2) for others
13530Sstevel@tonic-gate 		 *		nothing;
13540Sstevel@tonic-gate 		 *
13550Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
13560Sstevel@tonic-gate 		 * same request comes again.
13570Sstevel@tonic-gate 		 */
13580Sstevel@tonic-gate 		if (retry ++ >= 5) {
13590Sstevel@tonic-gate 			dev_t	devt;
13600Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
13630Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
13640Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
13650Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
13660Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
13670Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13680Sstevel@tonic-gate 					    "keyboard is not available"
13690Sstevel@tonic-gate 					    " for system debugging: %s",
13700Sstevel@tonic-gate 					    path);
13710Sstevel@tonic-gate 				break;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
13740Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
13750Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
13760Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13770Sstevel@tonic-gate 					    "keyboard is not available:"
13780Sstevel@tonic-gate 					    " %s", path);
13790Sstevel@tonic-gate 				break;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 			default:
13820Sstevel@tonic-gate 				break;
13830Sstevel@tonic-gate 			}
13840Sstevel@tonic-gate 			msg->kpm_req_nums --;
13850Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13860Sstevel@tonic-gate 			retry = 0;
13870Sstevel@tonic-gate 		}
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
13910Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
13920Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
13930Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate static void
conskbd_ioc_plink(queue_t * q,mblk_t * mp)14000Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate 	mblk_t		*req;
14030Sstevel@tonic-gate 	queue_t		*lowque;
14040Sstevel@tonic-gate 	struct linkblk		*linkp;
14050Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
14080Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
14110Sstevel@tonic-gate 	lowque = linkp->l_qbot;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
14140Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
14150Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
14180Sstevel@tonic-gate 	if (req == NULL) {
14190Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14200Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14210Sstevel@tonic-gate 		return;
14220Sstevel@tonic-gate 	}
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
14250Sstevel@tonic-gate 	if (req->b_cont == NULL) {
14260Sstevel@tonic-gate 		freemsg(req);
14270Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14280Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14290Sstevel@tonic-gate 		return;
14300Sstevel@tonic-gate 	}
14310Sstevel@tonic-gate 
14321828Scg149915 	lowque->q_ptr = lqs;
14331828Scg149915 	OTHERQ(lowque)->q_ptr = lqs;
14340Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
14350Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
14400Sstevel@tonic-gate 		freemsg(req);
14410Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
14420Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
14430Sstevel@tonic-gate 		lowque->q_ptr = NULL;
14441828Scg149915 		OTHERQ(lowque)->q_ptr = NULL;
14450Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14460Sstevel@tonic-gate 	}
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 
14511828Scg149915 static void
conskbd_ioc_punlink(queue_t * q,mblk_t * mp)14521828Scg149915 conskbd_ioc_punlink(queue_t *q, mblk_t *mp)
14531828Scg149915 {
14541828Scg149915 	int			index;
14551828Scg149915 	struct linkblk		*linkp;
14561828Scg149915 	conskbd_lower_queue_t	*lqs;
14571828Scg149915 	conskbd_lower_queue_t	*prev;
14581828Scg149915 
14591828Scg149915 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
14601828Scg149915 	prev = conskbd.conskbd_lqueue_list;
14611828Scg149915 	for (lqs = prev; lqs; lqs = lqs->lqs_next) {
14621828Scg149915 		if (lqs->lqs_queue == linkp->l_qbot) {
14631828Scg149915 			if (prev == lqs)
14641828Scg149915 				conskbd.conskbd_lqueue_list =
14651828Scg149915 				    lqs->lqs_next;
14661828Scg149915 			else
14671828Scg149915 				prev->lqs_next = lqs->lqs_next;
14681828Scg149915 
14691828Scg149915 			lqs->lqs_queue->q_ptr =  NULL;
14701828Scg149915 			OTHERQ(lqs->lqs_queue)->q_ptr = NULL;
14711828Scg149915 			conskbd.conskbd_lqueue_nums --;
14721828Scg149915 			if (conskbd.conskbd_lqueue_nums == 0) {
14731828Scg149915 				kbd_layout_bak = conskbd.conskbd_layout;
14741828Scg149915 				conskbd.conskbd_layout = -1;
14751828Scg149915 			}
14761828Scg149915 
14771828Scg149915 			for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
14781828Scg149915 				if (lqs->lqs_key_state[index] == KEY_PRESSED)
14791828Scg149915 					kbtrans_streams_key(
14801828Scg149915 					    conskbd.conskbd_kbtrans,
14811828Scg149915 					    index,
14821828Scg149915 					    KEY_RELEASED);
14831828Scg149915 			}
14841828Scg149915 
14851828Scg149915 			kmem_free(lqs, sizeof (*lqs));
14861828Scg149915 			miocack(q, mp, 0, 0);
14871828Scg149915 			return;
14881828Scg149915 		}
14891828Scg149915 		prev = lqs;
14901828Scg149915 	}
14911828Scg149915 	miocnak(q, mp, 0, EINVAL);
14921828Scg149915 
14931828Scg149915 }	/* conskbd_ioc_punlink() */
14941828Scg149915 
14950Sstevel@tonic-gate /*
14960Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
14970Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
14980Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
14990Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
15000Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
15010Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
15020Sstevel@tonic-gate  */
15030Sstevel@tonic-gate static void
conskbd_lqs_ack_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)15040Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15050Sstevel@tonic-gate {
15060Sstevel@tonic-gate 	switch (lqs->lqs_state) {
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
15090Sstevel@tonic-gate 	case LQS_INITIALIZED:
15100Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
15110Sstevel@tonic-gate 		break;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
15140Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
15150Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
15160Sstevel@tonic-gate 		break;
15170Sstevel@tonic-gate 
15181272Slq150181 	/* S4: wait lower queue to acknowledge KIOCSLED/KIOCGLED  message */
15190Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
15200Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
15210Sstevel@tonic-gate 		break;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
15240Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
15250Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
15260Sstevel@tonic-gate 		break;
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
15290Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
15300Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
15310Sstevel@tonic-gate 		break;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
15340Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
15350Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
15360Sstevel@tonic-gate 		break;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/* if reaching here, there must be a error */
15390Sstevel@tonic-gate 	default:
15400Sstevel@tonic-gate 		freemsg(mp);
15410Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
15420Sstevel@tonic-gate 		break;
15430Sstevel@tonic-gate 	}
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate static void
conskbd_kioctype_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)15490Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	struct iocblk	*iocp;
15520Sstevel@tonic-gate 	mblk_t		*req;
15530Sstevel@tonic-gate 	queue_t		*lowerque;
15541828Scg149915 	int		err = ENOMEM;
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
15570Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15620Sstevel@tonic-gate 	case M_IOCACK:
15630Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
15640Sstevel@tonic-gate 		if (req == NULL) {
15651828Scg149915 			goto err_exit;
15660Sstevel@tonic-gate 		}
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
15690Sstevel@tonic-gate 		if (req->b_cont == NULL) {
15700Sstevel@tonic-gate 			freemsg(req);
15711828Scg149915 			goto err_exit;
15720Sstevel@tonic-gate 		}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
15750Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
15760Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
15790Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
15820Sstevel@tonic-gate 			freemsg(req);
15831828Scg149915 			goto err_exit;
15840Sstevel@tonic-gate 		}
15851828Scg149915 		freemsg(mp);
15861828Scg149915 		return;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	case M_IOCNAK:
15890Sstevel@tonic-gate 		/*
15900Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
15910Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
15920Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
15930Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
15940Sstevel@tonic-gate 		 * will bypass the conskbd module.
15950Sstevel@tonic-gate 		 */
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 		/*
15980Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
15990Sstevel@tonic-gate 		 * conskbd, we reject the current one.
16000Sstevel@tonic-gate 		 */
16010Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
16020Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
16031828Scg149915 			err = iocp->ioc_error;
16041828Scg149915 			goto err_exit;
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 		/*
16081828Scg149915 		 * link this keyboard under conskbd.
16090Sstevel@tonic-gate 		 */
16101828Scg149915 		qwriter(lowerque, mp, conskbd_link_lowque_legacy, PERIM_OUTER);
16111828Scg149915 		return;
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16141828Scg149915 err_exit:
16151828Scg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
16161828Scg149915 	lowerque->q_ptr = NULL;
16171828Scg149915 	OTHERQ(lowerque)->q_ptr = NULL;
16181828Scg149915 	kmem_free(lqs, sizeof (*lqs));
16190Sstevel@tonic-gate 	freemsg(mp);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate static void
conskbd_kioctrans_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)16240Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16250Sstevel@tonic-gate {
16260Sstevel@tonic-gate 	struct iocblk 	*iocp;
16270Sstevel@tonic-gate 	mblk_t		*req;
16280Sstevel@tonic-gate 	queue_t		*lowerque;
16291828Scg149915 	int		err = ENOMEM;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16320Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
16370Sstevel@tonic-gate 	case M_IOCACK:
16380Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
16390Sstevel@tonic-gate 		if (req == NULL) {
16401828Scg149915 			goto err_exit;
16410Sstevel@tonic-gate 		}
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
16440Sstevel@tonic-gate 		if (req->b_cont == NULL) {
16450Sstevel@tonic-gate 			freemsg(req);
16461828Scg149915 			goto err_exit;
16470Sstevel@tonic-gate 		}
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
16500Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
16510Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
16520Sstevel@tonic-gate 			freemsg(req);
16531828Scg149915 			goto err_exit;
16540Sstevel@tonic-gate 		}
16551828Scg149915 		freemsg(mp);
16561828Scg149915 		return;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	case M_IOCNAK:
16590Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
16601828Scg149915 		err = iocp->ioc_error;
16611828Scg149915 		goto err_exit;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16641828Scg149915 err_exit:
16651828Scg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
16661828Scg149915 	lowerque->q_ptr = NULL;
16671828Scg149915 	OTHERQ(lowerque)->q_ptr = NULL;
16681828Scg149915 	kmem_free(lqs, sizeof (*lqs));
16690Sstevel@tonic-gate 	freemsg(mp);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
16720Sstevel@tonic-gate 
16731274Sqz150045 /*
16741274Sqz150045  * Allocate a firm event
16751274Sqz150045  */
16761274Sqz150045 static mblk_t *
conskbd_alloc_firm_event(ushort_t id,int value)16777187Shh224818 conskbd_alloc_firm_event(ushort_t id, int value)
16781274Sqz150045 {
16791274Sqz150045 	mblk_t	*mb;
16801274Sqz150045 	Firm_event *fe;
16811274Sqz150045 
16821274Sqz150045 	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
16831274Sqz150045 		fe = (Firm_event *)mb->b_wptr;
16841274Sqz150045 		fe->id = id;
16851274Sqz150045 		fe->pair_type = FE_PAIR_NONE;
16861274Sqz150045 		fe->pair = NULL;
16871274Sqz150045 		fe->value = value;
16881274Sqz150045 		mb->b_wptr += sizeof (Firm_event);
16891274Sqz150045 	}
16901274Sqz150045 
16911274Sqz150045 	return (mb);
16921274Sqz150045 }
16931274Sqz150045 
16940Sstevel@tonic-gate static void
conskbd_kioclayout_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)16950Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	mblk_t		*req;
16980Sstevel@tonic-gate 	int		layout;
16990Sstevel@tonic-gate 	boolean_t	fail;
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
17020Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
17050Sstevel@tonic-gate 	case M_IOCACK:
17060Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
17070Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
17080Sstevel@tonic-gate 			/*
17090Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
17100Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
17110Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
17120Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
17130Sstevel@tonic-gate 			 */
17141274Sqz150045 			if (layout != -1 && conskbd.conskbd_layout == -1) {
17151274Sqz150045 				if (layout == 0) {
17161274Sqz150045 					conskbd.conskbd_layout = kbd_layout_bak;
17171274Sqz150045 				} else {
17181274Sqz150045 					conskbd.conskbd_layout = layout;
17191274Sqz150045 					if (layout == kbd_layout_bak) {
17201274Sqz150045 						break;
17211274Sqz150045 					}
17221274Sqz150045 					if ((req = conskbd_alloc_firm_event(
17234925Sqz150045 					    KEYBOARD_LAYOUT_CHANGE,
17244925Sqz150045 					    layout)) != NULL) {
17254925Sqz150045 						if (conskbd.conskbd_directio) {
17261274Sqz150045 							putnext(
17271274Sqz150045 							    conskbd_regqueue,
17281274Sqz150045 							    req);
17294925Sqz150045 						} else if (conskbd_consqueue
17304925Sqz150045 						    != NULL) {
17311274Sqz150045 							putnext(
17321274Sqz150045 							    conskbd_consqueue,
17331274Sqz150045 							    req);
17344925Sqz150045 						} else {
17354925Sqz150045 							freemsg(req);
17364925Sqz150045 						}
17371274Sqz150045 					}
17381274Sqz150045 				}
17391274Sqz150045 			}
17400Sstevel@tonic-gate 		}
17410Sstevel@tonic-gate 		break;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
17450Sstevel@tonic-gate 	case M_IOCNAK:
17460Sstevel@tonic-gate 		break;
17470Sstevel@tonic-gate 	}
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	fail = B_TRUE;
17501272Slq150181 
17511272Slq150181 	if (conskbd.conskbd_led_state == -1)
17521272Slq150181 		req = mkiocb(KIOCGLED);
17531272Slq150181 	else
17541272Slq150181 		req = mkiocb(KIOCSLED);
17551272Slq150181 
17560Sstevel@tonic-gate 	if (req) {
17570Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
17580Sstevel@tonic-gate 		if (req->b_cont) {
17591272Slq150181 			if (conskbd.conskbd_led_state != -1) {
17601272Slq150181 				*(uchar_t *)req->b_cont->b_wptr =
17611272Slq150181 				    conskbd.conskbd_led_state;
17621272Slq150181 				req->b_cont->b_wptr += sizeof (uchar_t);
17631272Slq150181 			}
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
17660Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
17670Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
17680Sstevel@tonic-gate 				fail = B_FALSE;
17690Sstevel@tonic-gate 			} else {
17700Sstevel@tonic-gate 				freemsg(req);
17710Sstevel@tonic-gate 			}
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 		} else {
17740Sstevel@tonic-gate 			freemsg(req);
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 	}
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 	if (fail) {
17790Sstevel@tonic-gate 		/*
17801272Slq150181 		 * If fail to allocate KIOCSLED/KIOCGLED message or put
17811272Slq150181 		 * the message into lower queue, we immediately link
17821272Slq150181 		 * current keyboard under conskbd. Thus, even if fails
17831272Slq150181 		 * to set/get LED, this keyboard could be available.
17840Sstevel@tonic-gate 		 */
17851828Scg149915 		qwriter(lqs->lqs_queue,
17861828Scg149915 		    mp, conskbd_link_lowque_virt, PERIM_OUTER);
17871828Scg149915 	} else {
17881828Scg149915 		freemsg(mp);
17890Sstevel@tonic-gate 	}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate static void
conskbd_kiocsled_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)17950Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
17960Sstevel@tonic-gate {
17971272Slq150181 	int	led_state;
17981272Slq150181 
17990Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
18000Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
18010Sstevel@tonic-gate 
18021272Slq150181 	if (conskbd.conskbd_led_state == -1) {
18031272Slq150181 		switch (mp->b_datap->db_type) {
18041272Slq150181 		case M_IOCACK:
18051272Slq150181 			if (miocpullup(mp, sizeof (uchar_t)) == 0) {
18061272Slq150181 				led_state = *(uchar_t *)mp->b_cont->b_rptr;
18071272Slq150181 				conskbd.conskbd_led_state = led_state;
18081272Slq150181 				kbtrans_streams_setled(conskbd.conskbd_kbtrans,
18091272Slq150181 				    led_state);
18101272Slq150181 			}
18111272Slq150181 			break;
18121272Slq150181 
18131272Slq150181 		/* if fail, leave conskbd's led_state as it is */
18141272Slq150181 		case M_IOCNAK:
18151272Slq150181 			break;
18161272Slq150181 		}
18171272Slq150181 	}
18181272Slq150181 
18190Sstevel@tonic-gate 	/*
18201272Slq150181 	 * Basically, failure of setting/getting LED is not a fatal
18211272Slq150181 	 * error, so we will plumb the lower queue into conskbd whether
18221272Slq150181 	 * setting/getting LED succeeds or fails.
18230Sstevel@tonic-gate 	 */
18241828Scg149915 	qwriter(lqs->lqs_queue, mp, conskbd_link_lowque_virt, PERIM_OUTER);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate static void
conskbd_mux_upstream_msg(conskbd_lower_queue_t * lqs,mblk_t * mp)18300Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
18330Sstevel@tonic-gate 	struct iocblk		*iocp;
18340Sstevel@tonic-gate 	int			error;
18350Sstevel@tonic-gate 	dev_t			devt;
18360Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
18390Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	if (!msg) {
18420Sstevel@tonic-gate 		/*
18431828Scg149915 		 * Here we discard the response if:
18441828Scg149915 		 *
18451828Scg149915 		 *   1. It's an KIOCSLED request; see conskbd_streams_setled().
18461828Scg149915 		 *   2. The application has already closed the upper stream;
18471828Scg149915 		 *		see conskbdclose()
18480Sstevel@tonic-gate 		 */
18490Sstevel@tonic-gate 		freemsg(mp);
18500Sstevel@tonic-gate 		return;
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	/*
18540Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
18550Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
18560Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
18570Sstevel@tonic-gate 	 * the lower queue from which the current response message
18580Sstevel@tonic-gate 	 * comes.
18590Sstevel@tonic-gate 	 */
18600Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
18610Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
18620Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
18630Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
18640Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
18650Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
18660Sstevel@tonic-gate 
18675186Slq150181 	if (msg->kpm_resp_nums < msg->kpm_req_nums) {
18685186Slq150181 		mutex_exit(&msg->kpm_lock);
18690Sstevel@tonic-gate 		return;
18705186Slq150181 	}
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
18730Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
18740Sstevel@tonic-gate 
18755186Slq150181 	mutex_exit(&msg->kpm_lock);
18765186Slq150181 
18770Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	/*
18810Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
18820Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
18830Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
18840Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
18850Sstevel@tonic-gate 	 * is sent to upstreams
18860Sstevel@tonic-gate 	 */
18870Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
18880Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
18910Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
18920Sstevel@tonic-gate 		/*
18930Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
18940Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
18950Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
18960Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
18970Sstevel@tonic-gate 		 * check if the pointer is null.
18980Sstevel@tonic-gate 		 */
18990Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19000Sstevel@tonic-gate 			cons_polledio_t		*polledio;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19030Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
19040Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
19050Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
19060Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
19070Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
19080Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
19090Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
19100Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
19110Sstevel@tonic-gate 					error = 0;
19120Sstevel@tonic-gate 				} else {
19130Sstevel@tonic-gate 					/*
19140Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
19150Sstevel@tonic-gate 					 * use the same cons_polledio structure
19160Sstevel@tonic-gate 					 * as conskbd.
19170Sstevel@tonic-gate 					 */
19180Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
19190Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
19200Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
19210Sstevel@tonic-gate 						    "driver does not support "
19220Sstevel@tonic-gate 						    "system debugging: %s",
19230Sstevel@tonic-gate 						    path);
19240Sstevel@tonic-gate 					}
19250Sstevel@tonic-gate 					error = EINVAL;
19260Sstevel@tonic-gate 				}
19270Sstevel@tonic-gate 			} else {
19280Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
19290Sstevel@tonic-gate 				    DDI_SUCCESS) {
19300Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
19310Sstevel@tonic-gate 					    " not available for system"
19320Sstevel@tonic-gate 					    " debugging:  %s", path);
19330Sstevel@tonic-gate 				}
19340Sstevel@tonic-gate 			}
19350Sstevel@tonic-gate 			mp->b_next = NULL;
19360Sstevel@tonic-gate 			mp->b_prev = NULL;
19370Sstevel@tonic-gate 			freemsg(mp);
19380Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19390Sstevel@tonic-gate 		}
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
19420Sstevel@tonic-gate 		if (error == 0) {
19430Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
19440Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
19450Sstevel@tonic-gate 		}
19460Sstevel@tonic-gate 		break;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
19490Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19500Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19510Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
19520Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
19530Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
19540Sstevel@tonic-gate 				error = 0;
19550Sstevel@tonic-gate 			} else {
19560Sstevel@tonic-gate 				devt =
19570Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
19600Sstevel@tonic-gate 				    DDI_SUCCESS) {
19610Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
19620Sstevel@tonic-gate 					    " not available: %s", path);
19630Sstevel@tonic-gate 				}
19640Sstevel@tonic-gate 			}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 			mp->b_next = NULL;
19670Sstevel@tonic-gate 			mp->b_prev = NULL;
19680Sstevel@tonic-gate 			freemsg(mp);
19690Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19700Sstevel@tonic-gate 		}
19710Sstevel@tonic-gate 		break;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	case KIOCCMD:
19745129Smarx 	case KIOCMKTONE:
19750Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19760Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
19790Sstevel@tonic-gate 				error = 0;
19800Sstevel@tonic-gate 			mp->b_next = NULL;
19810Sstevel@tonic-gate 			mp->b_prev = NULL;
19820Sstevel@tonic-gate 			freemsg(mp);
19830Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19840Sstevel@tonic-gate 		}
19850Sstevel@tonic-gate 		break;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
19880Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
19890Sstevel@tonic-gate 	}
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
19920Sstevel@tonic-gate 	if (error == 0) {
19930Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
19940Sstevel@tonic-gate 	} else {
19950Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
19980Sstevel@tonic-gate 	iocp->ioc_error = error;
19990Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
20000Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
20010Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
20040Sstevel@tonic-gate 
20051828Scg149915 static void
conskbd_link_lowque_legacy(queue_t * lowque,mblk_t * mp)20061828Scg149915 conskbd_link_lowque_legacy(queue_t *lowque, mblk_t *mp)
20071828Scg149915 {
20081828Scg149915 	conskbd_lower_queue_t *lqs;
20091828Scg149915 
20101828Scg149915 	freemsg(mp);
20111828Scg149915 
20121828Scg149915 	/*
20131828Scg149915 	 * Bypass the virutal keyboard for old hardware,
20141828Scg149915 	 * Now, only current legacy keyboard can be linked
20151828Scg149915 	 * under conskbd
20161828Scg149915 	 */
20171828Scg149915 	conskbd.conskbd_bypassed = B_TRUE;
20181828Scg149915 
20191828Scg149915 	/*
20201828Scg149915 	 * Link the lower queue under conskbd
20211828Scg149915 	 */
20221828Scg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
20231828Scg149915 	lqs->lqs_state = LQS_INITIALIZED_LEGACY;
20241828Scg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
20251828Scg149915 	conskbd.conskbd_lqueue_list = lqs;
20261828Scg149915 	conskbd.conskbd_lqueue_nums++;
20271828Scg149915 
20281828Scg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
20291828Scg149915 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
20301828Scg149915 
20311828Scg149915 }	/* conskbd_link_lowque_legacy() */
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate static void
conskbd_link_lowque_virt(queue_t * lowque,mblk_t * mp)20341828Scg149915 conskbd_link_lowque_virt(queue_t *lowque, mblk_t *mp)
20350Sstevel@tonic-gate {
20360Sstevel@tonic-gate 	int		index;
20371828Scg149915 	conskbd_lower_queue_t *lqs;
20380Sstevel@tonic-gate 
20391828Scg149915 	freemsg(mp);
20400Sstevel@tonic-gate 
20411828Scg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
20421828Scg149915 
20431828Scg149915 	ASSERT(lqs->lqs_queue == lowque);
20441828Scg149915 	ASSERT(lqs->lqs_pending_plink != NULL);
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	/*
20470Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
20480Sstevel@tonic-gate 	 */
20490Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
20500Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
20510Sstevel@tonic-gate 	}
20521828Scg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
20530Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
20541828Scg149915 	conskbd.conskbd_lqueue_nums++;
20551828Scg149915 	conskbd.conskbd_lqueue_list = lqs;
20561828Scg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
20570Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
20580Sstevel@tonic-gate 
20591828Scg149915 }	/* conskbd_link_lowque_virt() */
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate /*ARGSUSED*/
20620Sstevel@tonic-gate static void
conskbd_legacy_upstream_msg(conskbd_lower_queue_t * lqs,mblk_t * mp)20630Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
20640Sstevel@tonic-gate {
20650Sstevel@tonic-gate 	struct iocblk	*iocp;
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	/*
20700Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
20710Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
20720Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
20730Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
20740Sstevel@tonic-gate 	 * should be added to this list.
20750Sstevel@tonic-gate 	 */
20760Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
20794925Sqz150045 	    (iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20824925Sqz150045 		    ("conskbd_legacy_upstream_msg: "
20834925Sqz150045 		    "CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
20840Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
20870Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20880Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
20930Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20940Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
20950Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
20960Sstevel@tonic-gate 	} else {
20970Sstevel@tonic-gate 		/* if reached here, it must be a error */
20980Sstevel@tonic-gate 		cmn_err(CE_WARN,
20990Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
21000Sstevel@tonic-gate 		freemsg(mp);
21010Sstevel@tonic-gate 	}
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate /*
21060Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
21070Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
21080Sstevel@tonic-gate  *
21090Sstevel@tonic-gate  * 1) application initiated request
21100Sstevel@tonic-gate  * 	A KIOCSLED ioctl is sent by an application. The ioctl will be
21110Sstevel@tonic-gate  * 	be prcoessed by queue service procedure conskbduwsrv(), which
21120Sstevel@tonic-gate  * 	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
21130Sstevel@tonic-gate  * 	conskbd_streams_setled() to set LED, after that,  kbtrans will
21140Sstevel@tonic-gate  * 	return an ACK message to upper module.
21150Sstevel@tonic-gate  *
21160Sstevel@tonic-gate  * 2) Kbtrans initiated the request
21170Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
21180Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
21190Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
21200Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
21210Sstevel@tonic-gate  *
21220Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
21230Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
21240Sstevel@tonic-gate  * has no return value.
21250Sstevel@tonic-gate  */
21260Sstevel@tonic-gate static void
conskbd_streams_setled(struct kbtrans_hardware * hw,int led_state)21270Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
21280Sstevel@tonic-gate {
21290Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21300Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
21310Sstevel@tonic-gate 	mblk_t		*req;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	if (led_state == -1)
21360Sstevel@tonic-gate 		return;
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	/*
21410Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
21420Sstevel@tonic-gate 	 * it if this happens.
21430Sstevel@tonic-gate 	 */
21440Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21450Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 		if (!req) {
21480Sstevel@tonic-gate 			continue;
21490Sstevel@tonic-gate 		}
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
21520Sstevel@tonic-gate 		if (!req->b_cont) {
21530Sstevel@tonic-gate 			freemsg(req);
21540Sstevel@tonic-gate 			continue;
21550Sstevel@tonic-gate 		}
21560Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
21570Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
21580Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
21590Sstevel@tonic-gate 			freemsg(req);
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate }	/* conskbd_streams_setled() */
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate static void
conskbd_polledio_setled(struct kbtrans_hardware * hw,int led_state)21650Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
21660Sstevel@tonic-gate {
21670Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21680Sstevel@tonic-gate 	struct cons_polledio		*cb;
21690Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21720Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21730Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
21740Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
21750Sstevel@tonic-gate 			    led_state);
21760Sstevel@tonic-gate 		}
21770Sstevel@tonic-gate 	}
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate static boolean_t
conskbd_polled_keycheck(struct kbtrans_hardware * hw,kbtrans_key_t * keycode,enum keystate * state)21820Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
21830Sstevel@tonic-gate 		kbtrans_key_t *keycode, enum keystate *state)
21840Sstevel@tonic-gate {
21850Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21860Sstevel@tonic-gate 	struct cons_polledio 		*cb;
21870Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21880Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
21910Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
21920Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21930Sstevel@tonic-gate 		if ((cb != NULL) &&
21940Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
21950Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
21960Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
21970Sstevel@tonic-gate 		}
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
22000Sstevel@tonic-gate 		if (ret == B_TRUE) {
22011097Slq150181 
22021097Slq150181 			/* A legacy keyboard ? */
22031097Slq150181 			if (conskbd.conskbd_bypassed == B_TRUE)
22041097Slq150181 				break;
22051097Slq150181 
22061097Slq150181 			/*
22071097Slq150181 			 * This is the PS2 scancode 0x2B -> USB(49) /
22081097Slq150181 			 * USB(50) keycode mapping workaround, for
22091097Slq150181 			 * polled mode.
22101097Slq150181 			 *
22111097Slq150181 			 * There are two possible USB keycode mappings
22121097Slq150181 			 * for PS2 scancode 0x2B and this workaround
22131097Slq150181 			 * makes sure that we use the USB keycode that
22141097Slq150181 			 * does not end up being mapped to a HOLE key
22151097Slq150181 			 * using the current keyboard translation
22161097Slq150181 			 * tables.
22171097Slq150181 			 *
22181097Slq150181 			 * See conskbdlrput() for a detailed
22191097Slq150181 			 * explanation of the problem.
22201097Slq150181 			 */
22211097Slq150181 			if (*keycode == 49 || *keycode == 50) {
22221097Slq150181 				if (conskbd_keyindex->k_normal[50] == HOLE)
22231097Slq150181 					*keycode = 49;
22241097Slq150181 				else
22251097Slq150181 					*keycode = 50;
22261097Slq150181 			}
22271097Slq150181 
22280Sstevel@tonic-gate 			break;
22290Sstevel@tonic-gate 		}
22300Sstevel@tonic-gate 	}
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	return (ret);
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate static boolean_t
conskbd_override_kbtrans(queue_t * q,mblk_t * mp)22370Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
22380Sstevel@tonic-gate {
22390Sstevel@tonic-gate 	struct iocblk		*iocp;
22400Sstevel@tonic-gate 	int		directio;
22410Sstevel@tonic-gate 	int		error;
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
22440Sstevel@tonic-gate 		return (B_FALSE);
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
22490Sstevel@tonic-gate 	case KIOCGDIRECT: {
22500Sstevel@tonic-gate 		/*
22510Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
22520Sstevel@tonic-gate 		 * respond incorrectly.
22530Sstevel@tonic-gate 		 */
22540Sstevel@tonic-gate 		register mblk_t *datap;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
22570Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
22580Sstevel@tonic-gate 			return (B_TRUE);
22590Sstevel@tonic-gate 		}
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
22620Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
22630Sstevel@tonic-gate 		if (mp->b_cont) {
22640Sstevel@tonic-gate 			freemsg(mp->b_cont);
22650Sstevel@tonic-gate 			mp->b_cont = NULL;
22660Sstevel@tonic-gate 		}
22670Sstevel@tonic-gate 		mp->b_cont = datap;
22680Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
22690Sstevel@tonic-gate 		return (B_TRUE);
22700Sstevel@tonic-gate 	}
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	case KIOCSDIRECT:
22730Sstevel@tonic-gate 		/*
22740Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
22750Sstevel@tonic-gate 		 * based code see it and respond to it.
22760Sstevel@tonic-gate 		 */
22770Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
22780Sstevel@tonic-gate 		if (error != 0) {
22790Sstevel@tonic-gate 			return (B_FALSE);
22800Sstevel@tonic-gate 		}
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
22830Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
22840Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
22850Sstevel@tonic-gate 			return (B_TRUE);
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
22900Sstevel@tonic-gate 			kbtrans_streams_set_queue(
22910Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
22920Sstevel@tonic-gate 		} else {
22930Sstevel@tonic-gate 			kbtrans_streams_set_queue(
22940Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
22950Sstevel@tonic-gate 		}
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 		/*
22980Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
22990Sstevel@tonic-gate 		 */
23000Sstevel@tonic-gate 		return (B_FALSE);
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 	default:
23030Sstevel@tonic-gate 		return (B_FALSE);
23040Sstevel@tonic-gate 	}
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate static void
conskbd_polledio_enter(cons_polledio_arg_t arg)23101762Slt200341 conskbd_polledio_enter(cons_polledio_arg_t arg)
23110Sstevel@tonic-gate {
23120Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
23130Sstevel@tonic-gate 	struct cons_polledio		*cb;
23140Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23170Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
23180Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
23190Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
23200Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
23210Sstevel@tonic-gate 		}
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate static void
conskbd_polledio_exit(cons_polledio_arg_t arg)23271762Slt200341 conskbd_polledio_exit(cons_polledio_arg_t arg)
23280Sstevel@tonic-gate {
23290Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
23300Sstevel@tonic-gate 	struct cons_polledio		*cb;
23310Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23340Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
23350Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
23360Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
23370Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate static int
conskbd_polledio_getchar(cons_polledio_arg_t arg)23441762Slt200341 conskbd_polledio_getchar(cons_polledio_arg_t arg)
23450Sstevel@tonic-gate {
23460Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate static int
conskbd_polledio_ischar(cons_polledio_arg_t arg)23551762Slt200341 conskbd_polledio_ischar(cons_polledio_arg_t arg)
23560Sstevel@tonic-gate {
23570Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate static void
conskbd_mux_enqueue_msg(conskbd_pending_msg_t * msg)23670Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
23680Sstevel@tonic-gate {
23690Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23700Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
23710Sstevel@tonic-gate 	conskbd_msg_queue = msg;
23720Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate /*
23770Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
23780Sstevel@tonic-gate  */
23790Sstevel@tonic-gate static conskbd_pending_msg_t *
conskbd_mux_find_msg(mblk_t * mp)23800Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
23830Sstevel@tonic-gate 	struct iocblk		*iocp;
23840Sstevel@tonic-gate 	uint_t	id;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23870Sstevel@tonic-gate 	msg = conskbd_msg_queue;
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
23900Sstevel@tonic-gate 	ASSERT(iocp);
23910Sstevel@tonic-gate 	id = iocp->ioc_id;
23920Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
23930Sstevel@tonic-gate 		msg = msg->kpm_next;
23940Sstevel@tonic-gate 	}
23950Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	return (msg);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate static void
conskbd_mux_dequeue_msg(conskbd_pending_msg_t * msg)24030Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
24040Sstevel@tonic-gate {
24050Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
24060Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
24090Sstevel@tonic-gate 	prev = conskbd_msg_queue;
24100Sstevel@tonic-gate 
24115186Slq150181 	for (p = prev; p && p != msg; p = p->kpm_next)
24120Sstevel@tonic-gate 		prev = p;
24135186Slq150181 
24140Sstevel@tonic-gate 	ASSERT(p && p == msg);
24155186Slq150181 
24160Sstevel@tonic-gate 	if (prev == p) {
24170Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
24180Sstevel@tonic-gate 	} else {
24190Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
24200Sstevel@tonic-gate 	}
24210Sstevel@tonic-gate 	p->kpm_next = NULL;
24220Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate #ifdef DEBUG
24270Sstevel@tonic-gate /*ARGSUSED*/
24280Sstevel@tonic-gate void
conskbd_dprintf(const char * fmt,...)24290Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
24300Sstevel@tonic-gate {
24310Sstevel@tonic-gate 	char buf[256];
24320Sstevel@tonic-gate 	va_list ap;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	va_start(ap, fmt);
24350Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
24360Sstevel@tonic-gate 	va_end(ap);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate }	/* conskbd_dprintf() */
24410Sstevel@tonic-gate #endif
2442