xref: /onnv-gate/usr/src/uts/common/io/conskbd.c (revision 1828:536c715cc3e8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51272Slq150181  * Common Development and Distribution License (the "License").
61272Slq150181  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211272Slq150181 
220Sstevel@tonic-gate /*
231272Slq150181  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Console kbd multiplexor driver for Sun.
310Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "kbd" module pushed
320Sstevel@tonic-gate  * on top of it.
330Sstevel@tonic-gate  * Minor device 0 is what programs normally use.
340Sstevel@tonic-gate  * Minor device 1 is used to feed predigested keystrokes to the "workstation
350Sstevel@tonic-gate  * console" driver, which it is linked beneath.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  *     This module can support multiple keyboards to be used simultaneously.
390Sstevel@tonic-gate  * and enable users to use at a time multiple keyboards connected to the
400Sstevel@tonic-gate  * same system. All the keyboards are linked under conskbd, and act as a
410Sstevel@tonic-gate  * keyboard with replicated keys.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
440Sstevel@tonic-gate  * a two-level architecure. The lower one is one of serialport drivers, such
450Sstevel@tonic-gate  * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
460Sstevel@tonic-gate  * drivers don't support polled I/O interfaces, we couldn't group the keyboard
470Sstevel@tonic-gate  * of this kind under conskbd. So we do as the follows:
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
500Sstevel@tonic-gate  *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
510Sstevel@tonic-gate  *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
520Sstevel@tonic-gate  *     requesting to be linked under conskbd. If the lower driver does't
530Sstevel@tonic-gate  *     recognize this ioctl, the virtual keyboard will be disabled so that
540Sstevel@tonic-gate  *     only one keyboard instance could be linked under conskbd.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/types.h>
590Sstevel@tonic-gate #include <sys/param.h>
600Sstevel@tonic-gate #include <sys/stropts.h>
610Sstevel@tonic-gate #include <sys/stream.h>
620Sstevel@tonic-gate #include <sys/strsubr.h>
630Sstevel@tonic-gate #include <sys/strsun.h>
640Sstevel@tonic-gate #include <sys/conf.h>
650Sstevel@tonic-gate #include <sys/stat.h>
660Sstevel@tonic-gate #include <sys/errno.h>
670Sstevel@tonic-gate #include <sys/modctl.h>
680Sstevel@tonic-gate #include <sys/kbio.h>
690Sstevel@tonic-gate #include <sys/ddi.h>
700Sstevel@tonic-gate #include <sys/sunddi.h>
710Sstevel@tonic-gate #include <sys/consdev.h>
720Sstevel@tonic-gate #include <sys/note.h>
730Sstevel@tonic-gate #include <sys/kmem.h>
740Sstevel@tonic-gate #include <sys/kstat.h>
750Sstevel@tonic-gate #include <sys/policy.h>
760Sstevel@tonic-gate #include <sys/kbd.h>
770Sstevel@tonic-gate #include <sys/kbtrans.h>
780Sstevel@tonic-gate #include <sys/promif.h>
790Sstevel@tonic-gate #include <sys/vuid_event.h>
800Sstevel@tonic-gate #include <sys/conskbd.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate extern struct keyboard *kbtrans_usbkb_maptab_init(void);
830Sstevel@tonic-gate extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
840Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Module linkage routines for the kernel
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
900Sstevel@tonic-gate static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
910Sstevel@tonic-gate static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * STREAMS queue processing procedures
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate static void	conskbduwsrv(queue_t *);
970Sstevel@tonic-gate static void	conskbdlwserv(queue_t *);
980Sstevel@tonic-gate static void	conskbdlrput(queue_t *, mblk_t *);
990Sstevel@tonic-gate static 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 */
185*1828Scg149915 	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 */
2030Sstevel@tonic-gate 	NULL			/* devo_power */
2040Sstevel@tonic-gate };
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * Module linkage information for the kernel.
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate static struct modldrv modldrv = {
2100Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
211*1828Scg149915 	"conskbd multiplexer driver %I%",
2120Sstevel@tonic-gate 	&conskbd_ops,	/* driver ops */
2130Sstevel@tonic-gate };
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * Module linkage structure
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate static struct modlinkage modlinkage = {
2190Sstevel@tonic-gate 	MODREV_1,	/* ml_rev */
2200Sstevel@tonic-gate 	&modldrv,	/* ml_linkage */
2210Sstevel@tonic-gate 	NULL		/* NULL terminates the list */
2220Sstevel@tonic-gate };
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * Debug printing
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate #ifndef DPRINTF
2280Sstevel@tonic-gate #ifdef DEBUG
2290Sstevel@tonic-gate void	conskbd_dprintf(const char *fmt, ...);
2300Sstevel@tonic-gate #define	DPRINTF(l, m, args) \
2310Sstevel@tonic-gate 	(((l) >= conskbd_errlevel) && ((m) & conskbd_errmask) ?	\
2320Sstevel@tonic-gate 		conskbd_dprintf args :				\
2330Sstevel@tonic-gate 		(void) 0)
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  * Severity levels for printing
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate #define	PRINT_L0	0	/* print every message */
2390Sstevel@tonic-gate #define	PRINT_L1	1	/* debug */
2400Sstevel@tonic-gate #define	PRINT_L2	2	/* quiet */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * Masks
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate #define	PRINT_MASK_ALL		0xFFFFFFFFU
2460Sstevel@tonic-gate uint_t	conskbd_errmask = PRINT_MASK_ALL;
2470Sstevel@tonic-gate uint_t	conskbd_errlevel = PRINT_L2;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate #else
2500Sstevel@tonic-gate #define	DPRINTF(l, m, args)	/* NOTHING */
2510Sstevel@tonic-gate #endif
2520Sstevel@tonic-gate #endif
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
255*1828Scg149915  * Module global data are protected by outer perimeter. Modifying
256*1828Scg149915  * these global data is executed in outer perimeter exclusively.
257*1828Scg149915  * Except in conskbdopen() and conskbdclose(), which are entered
258*1828Scg149915  * exclusively (Refer to D_MTOCEXCL flag), all changes for the
259*1828Scg149915  * global variables are protected by qwriter().
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
2620Sstevel@tonic-gate static	queue_t	*conskbd_consqueue; /* console queue above us */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
2660Sstevel@tonic-gate static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
2670Sstevel@tonic-gate static struct keyboard *conskbd_keyindex;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
2710Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
2720Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
2730Sstevel@tonic-gate  * will be easier to add new statistics later.
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate static struct {
2760Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last keystroke */
2770Sstevel@tonic-gate } conskbd_kstat = {
2780Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
2790Sstevel@tonic-gate };
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * Local routines prototypes
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate static int conskbd_kstat_update(kstat_t *, int);
2850Sstevel@tonic-gate 
286*1828Scg149915 static void conskbd_ioctl(queue_t *, mblk_t *);
2870Sstevel@tonic-gate static void conskbd_ioc_plink(queue_t *, mblk_t *);
288*1828Scg149915 static void conskbd_ioc_punlink(queue_t *, mblk_t *);
2890Sstevel@tonic-gate static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
2900Sstevel@tonic-gate static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
2911274Sqz150045 static mblk_t *conskbd_alloc_firm_event(int, int);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
2940Sstevel@tonic-gate static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
2950Sstevel@tonic-gate static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
296*1828Scg149915 static void conskbd_link_lowque_virt(queue_t *, mblk_t *);
297*1828Scg149915 static void conskbd_link_lowque_legacy(queue_t *, mblk_t *);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
3000Sstevel@tonic-gate static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
3010Sstevel@tonic-gate static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
3020Sstevel@tonic-gate static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
3030Sstevel@tonic-gate static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
3040Sstevel@tonic-gate static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3050Sstevel@tonic-gate static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3060Sstevel@tonic-gate static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);
3070Sstevel@tonic-gate 
3081762Slt200341 static void conskbd_polledio_enter(cons_polledio_arg_t);
3091762Slt200341 static void conskbd_polledio_exit(cons_polledio_arg_t);
3101762Slt200341 static int  conskbd_polledio_ischar(cons_polledio_arg_t);
3111762Slt200341 static int  conskbd_polledio_getchar(cons_polledio_arg_t);
3120Sstevel@tonic-gate static void conskbd_polledio_setled(struct kbtrans_hardware *, int);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate static void conskbd_streams_setled(struct kbtrans_hardware *, int);
3150Sstevel@tonic-gate static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
3160Sstevel@tonic-gate static boolean_t
3170Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *,
3180Sstevel@tonic-gate 		kbtrans_key_t *, enum keystate *);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Callbacks needed by kbtrans
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate static struct kbtrans_callbacks conskbd_callbacks = {
3240Sstevel@tonic-gate 	conskbd_streams_setled,
3250Sstevel@tonic-gate 	conskbd_polledio_setled,
3260Sstevel@tonic-gate 	conskbd_polled_keycheck,
3270Sstevel@tonic-gate };
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate  * Single private "global" lock for the few rare conditions
3310Sstevel@tonic-gate  * we want single-threaded.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate static	kmutex_t	conskbd_msgq_lock;
3340Sstevel@tonic-gate static	conskbd_pending_msg_t	*conskbd_msg_queue;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * The software state structure of virtual keyboard.
338*1828Scg149915  * Currently, only one virtual keyboard is supported.
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate static conskbd_state_t	conskbd = { 0 };
3410Sstevel@tonic-gate 
3421274Sqz150045 /* This variable backs up the layout state for non-self-ID keyboards */
3431274Sqz150045 static int kbd_layout_bak = 0;
3441274Sqz150045 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * _init()
3470Sstevel@tonic-gate  *
3480Sstevel@tonic-gate  * Description:
3490Sstevel@tonic-gate  *      Driver initialization, called when driver is first loaded.
3500Sstevel@tonic-gate  *      This is how access is initially given to all the static structures.
3510Sstevel@tonic-gate  *
3520Sstevel@tonic-gate  * Arguments:
3530Sstevel@tonic-gate  *      None
3540Sstevel@tonic-gate  *
3550Sstevel@tonic-gate  * Returns:
3560Sstevel@tonic-gate  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
3570Sstevel@tonic-gate  *      mod_install() status, see mod_install(9f)
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate int
3600Sstevel@tonic-gate _init(void)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	int	error;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3650Sstevel@tonic-gate 	if (error != 0) {
3660Sstevel@tonic-gate 		return (error);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	conskbd_keyindex = kbtrans_usbkb_maptab_init();
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	return (error);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate }	/* _init() */
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * _fini()
3790Sstevel@tonic-gate  *
3800Sstevel@tonic-gate  * Description:
3810Sstevel@tonic-gate  *      Module de-initialization, called when the driver is to be unloaded.
3820Sstevel@tonic-gate  *
3830Sstevel@tonic-gate  * Arguments:
3840Sstevel@tonic-gate  *      None
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * Returns:
3870Sstevel@tonic-gate  *      mod_remove() status, see mod_remove(9f)
3880Sstevel@tonic-gate  */
3890Sstevel@tonic-gate int
3900Sstevel@tonic-gate _fini(void)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	int	error;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3950Sstevel@tonic-gate 	if (error != 0)
3960Sstevel@tonic-gate 		return (error);
3970Sstevel@tonic-gate 	mutex_destroy(&conskbd_msgq_lock);
3980Sstevel@tonic-gate 	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	return (0);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate }	/* _fini() */
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate  * _info()
4060Sstevel@tonic-gate  *
4070Sstevel@tonic-gate  * Description:
4080Sstevel@tonic-gate  *      Module information, returns information about the driver.
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * Arguments:
4110Sstevel@tonic-gate  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
4120Sstevel@tonic-gate  *
4130Sstevel@tonic-gate  * Returns:
4140Sstevel@tonic-gate  *      mod_info() status, see mod_info(9f)
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate int
4170Sstevel@tonic-gate _info(struct modinfo *modinfop)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate }	/* _info() */
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * conskbd_attach()
4260Sstevel@tonic-gate  *
4270Sstevel@tonic-gate  * Description:
4280Sstevel@tonic-gate  * 	This routine creates two device nodes. One is the "kbd" node, which
4290Sstevel@tonic-gate  * is used by user application programs(such as Xserver).The other is the
4300Sstevel@tonic-gate  * "conskbd" node, which is an internal node. consconfig_dacf module will
4310Sstevel@tonic-gate  * open this internal node, and link the conskbd under the wc (workstaion
4320Sstevel@tonic-gate  * console).
4330Sstevel@tonic-gate  *
4340Sstevel@tonic-gate  * Arguments:
4350Sstevel@tonic-gate  *      dev_info_t      *dip    Pointer to the device's dev_info struct
4360Sstevel@tonic-gate  *      ddi_attach_cmd_t cmd    Attach command
4370Sstevel@tonic-gate  *
4380Sstevel@tonic-gate  * Returns:
4390Sstevel@tonic-gate  *      DDI_SUCCESS             The driver was initialized properly
4400Sstevel@tonic-gate  *      DDI_FAILURE             The driver couldn't be initialized properly
4410Sstevel@tonic-gate  */
4420Sstevel@tonic-gate /*ARGSUSED*/
4430Sstevel@tonic-gate static int
4440Sstevel@tonic-gate conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	kstat_t	*ksp;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	switch (cmd) {
4490Sstevel@tonic-gate 	case DDI_ATTACH:
4500Sstevel@tonic-gate 		break;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	default:
4530Sstevel@tonic-gate 		return (DDI_FAILURE);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
4570Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) ||
4580Sstevel@tonic-gate 	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
4590Sstevel@tonic-gate 	    1) == DDI_FAILURE)) {
4600Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4610Sstevel@tonic-gate 		return (DDI_FAILURE);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 	conskbd_dip = devi;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
4660Sstevel@tonic-gate 	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
4670Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
4680Sstevel@tonic-gate 	if (ksp) {
4690Sstevel@tonic-gate 		ksp->ks_data = (void *) &conskbd_kstat;
4700Sstevel@tonic-gate 		ksp->ks_update = conskbd_kstat_update;
4710Sstevel@tonic-gate 		kstat_install(ksp);
4720Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	conskbd.conskbd_layout = -1;	/* invalid layout */
4760Sstevel@tonic-gate 	conskbd.conskbd_led_state = -1;
4770Sstevel@tonic-gate 	conskbd.conskbd_bypassed = B_FALSE;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	return (DDI_SUCCESS);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate }	/* conskbd_attach() */
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate  * conskbd_detach()
4850Sstevel@tonic-gate  *
4860Sstevel@tonic-gate  * Description:
4870Sstevel@tonic-gate  *      Detach an instance of the conskbd driver. In fact, the driver can not
4880Sstevel@tonic-gate  * be detached.
4890Sstevel@tonic-gate  *
4900Sstevel@tonic-gate  * Arguments:
4910Sstevel@tonic-gate  *      dev_info_t              *dip    Pointer to the device's dev_info struct
4920Sstevel@tonic-gate  *      ddi_detach_cmd_t        cmd     Detach command
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  * Returns:
4950Sstevel@tonic-gate  *      DDI_SUCCESS     The driver was detached
4960Sstevel@tonic-gate  *      DDI_FAILURE     The driver couldn't be detached
4970Sstevel@tonic-gate  */
4980Sstevel@tonic-gate /*ARGSUSED*/
4990Sstevel@tonic-gate static int
5000Sstevel@tonic-gate conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	return (DDI_FAILURE);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate }	/* conskbd_detach() */
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /* ARGSUSED */
5070Sstevel@tonic-gate static int
5080Sstevel@tonic-gate conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
5090Sstevel@tonic-gate 	void **result)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	register int error;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	switch (infocmd) {
5140Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5150Sstevel@tonic-gate 		if (conskbd_dip == NULL) {
5160Sstevel@tonic-gate 			error = DDI_FAILURE;
5170Sstevel@tonic-gate 		} else {
5180Sstevel@tonic-gate 			*result = (void *) conskbd_dip;
5190Sstevel@tonic-gate 			error = DDI_SUCCESS;
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 		break;
5220Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5230Sstevel@tonic-gate 		*result = (void *)0;
5240Sstevel@tonic-gate 		error = DDI_SUCCESS;
5250Sstevel@tonic-gate 		break;
5260Sstevel@tonic-gate 	default:
5270Sstevel@tonic-gate 		error = DDI_FAILURE;
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 	return (error);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate }	/* conskbd_info() */
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*ARGSUSED*/
5340Sstevel@tonic-gate static int
5350Sstevel@tonic-gate conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	dev_t	unit;
5380Sstevel@tonic-gate 	int	err;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	unit = getminor(*devp);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (unit == 0) {
5430Sstevel@tonic-gate 		/*
5440Sstevel@tonic-gate 		 * Opening "/dev/kbd".
5450Sstevel@tonic-gate 		 */
5460Sstevel@tonic-gate 		conskbd_regqueue = q;
5470Sstevel@tonic-gate 		qprocson(q);
5480Sstevel@tonic-gate 		return (0);
5490Sstevel@tonic-gate 	} else if (unit != 1) {
5500Sstevel@tonic-gate 		/* we don't do that under Bozo's Big Tent */
5510Sstevel@tonic-gate 		return (ENODEV);
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
555*1828Scg149915 	 * Check if already initialized
556*1828Scg149915 	 */
557*1828Scg149915 	if (conskbd_consqueue != NULL)
558*1828Scg149915 		return (0);
559*1828Scg149915 
560*1828Scg149915 	/*
5610Sstevel@tonic-gate 	 * Opening the device to be linked under the console.
5620Sstevel@tonic-gate 	 */
5630Sstevel@tonic-gate 	conskbd_consqueue = q;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5661097Slq150181 	 * initialize kbtrans module for conskbd
5670Sstevel@tonic-gate 	 */
5680Sstevel@tonic-gate 	err = kbtrans_streams_init(q, sflag, crp, (struct kbtrans_hardware *)
5690Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
5700Sstevel@tonic-gate 	if (err != 0)
5710Sstevel@tonic-gate 		return (err);
5720Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
5730Sstevel@tonic-gate 	    conskbd_keyindex);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
5760Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
5771762Slt200341 	    (cons_polledio_arg_t)&conskbd;
5780Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
5790Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
5801762Slt200341 	    (int (*)(cons_polledio_arg_t)) conskbd_polledio_getchar;
5810Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
5821762Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))conskbd_polledio_ischar;
5830Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
5840Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
5850Sstevel@tonic-gate 	qprocson(q);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	return (0);
5880Sstevel@tonic-gate 
589*1828Scg149915 }	/* conskbdopen() */
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate /*ARGSUSED*/
5930Sstevel@tonic-gate static int
5940Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
5970Sstevel@tonic-gate 
598*1828Scg149915 		conskbd_pending_msg_t	*pmsg, *prev, *next;
599*1828Scg149915 		mblk_t		*mp;
600*1828Scg149915 
6010Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
6020Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
6050Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
6060Sstevel@tonic-gate 		    conskbd_consqueue);
6070Sstevel@tonic-gate 		qprocsoff(q);
6080Sstevel@tonic-gate 		conskbd_regqueue = NULL;
609*1828Scg149915 
610*1828Scg149915 		/*
611*1828Scg149915 		 * If there are any pending ioctls which conskbd hasn't
612*1828Scg149915 		 * responded to yet, remove them from conskbd_msg_queue.
613*1828Scg149915 		 * Otherwise, we might send the response to a nonexistent
614*1828Scg149915 		 * closed queue. Refer to: conskbd_mux_upstream_msg().
615*1828Scg149915 		 */
616*1828Scg149915 		for (prev = NULL, pmsg = conskbd_msg_queue; pmsg != NULL;
617*1828Scg149915 		    pmsg = next) {
618*1828Scg149915 			next = pmsg->kpm_next;
619*1828Scg149915 			if (pmsg->kpm_upper_queue == q) {
620*1828Scg149915 				if (prev == NULL)
621*1828Scg149915 					conskbd_msg_queue = next;
622*1828Scg149915 				else
623*1828Scg149915 					prev->kpm_next = next;
624*1828Scg149915 
625*1828Scg149915 				while (pmsg->kpm_resp_list != NULL) {
626*1828Scg149915 					mp = pmsg->kpm_resp_list;
627*1828Scg149915 					pmsg->kpm_resp_list = mp->b_next;
628*1828Scg149915 					mp->b_next = mp->b_prev = NULL;
629*1828Scg149915 					freemsg(mp);
630*1828Scg149915 				}
631*1828Scg149915 				mutex_destroy(&pmsg->kpm_lock);
632*1828Scg149915 				kmem_free(pmsg, sizeof (*pmsg));
633*1828Scg149915 			} else {
634*1828Scg149915 				prev = pmsg;
635*1828Scg149915 			}
636*1828Scg149915 		}
6370Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
6400Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
6410Sstevel@tonic-gate 		 */
6420Sstevel@tonic-gate 		qprocsoff(q);
6430Sstevel@tonic-gate 		conskbd_consqueue = NULL;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	return (0);
6470Sstevel@tonic-gate 
648*1828Scg149915 }	/* conskbdclose() */
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * Service procedure for upper write queue.
6520Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
6530Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
6540Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
6550Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
6560Sstevel@tonic-gate  * deal with all messages.
6570Sstevel@tonic-gate  * 	This routine is invoked when ioctl commands are send down
6580Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
6590Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
6600Sstevel@tonic-gate  * the led states.
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate static void
6630Sstevel@tonic-gate conskbduwsrv(queue_t *q)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	mblk_t	*mp;
6660Sstevel@tonic-gate 	queue_t	*oldq;
6670Sstevel@tonic-gate 	enum kbtrans_message_response ret;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		/*
6720Sstevel@tonic-gate 		 * if the virtual keyboard is supported
6730Sstevel@tonic-gate 		 */
6740Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
6770Sstevel@tonic-gate 				continue;
6780Sstevel@tonic-gate 			/*
6790Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
6800Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
6810Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
6820Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
6830Sstevel@tonic-gate 			 * and regular queue.
6840Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
6850Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
6860Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
6870Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
6880Sstevel@tonic-gate 			 * to be switched between regular queue and console
6890Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
6900Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
6910Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
6920Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
6930Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
6940Sstevel@tonic-gate 			 * need to return response to right queue.
6950Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
6960Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
6970Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
6980Sstevel@tonic-gate 			 * and then switch back to old queue.
6990Sstevel@tonic-gate 			 */
7000Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
7010Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
7020Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7030Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
7040Sstevel@tonic-gate 			ret = kbtrans_streams_message(
7050Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
7060Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7070Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 			switch (ret) {
7100Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
7110Sstevel@tonic-gate 					continue;
7120Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
7130Sstevel@tonic-gate 					break;
7140Sstevel@tonic-gate 			}
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		case M_IOCTL:
720*1828Scg149915 			conskbd_ioctl(q, mp);
7210Sstevel@tonic-gate 			break;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		case M_FLUSH:
7240Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
7250Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
7260Sstevel@tonic-gate 			}
7270Sstevel@tonic-gate 			/*
7280Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
7290Sstevel@tonic-gate 			 * may be lost so that upper module or applications
7300Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
7310Sstevel@tonic-gate 			 * ever.
7320Sstevel@tonic-gate 			 */
7330Sstevel@tonic-gate 			freemsg(mp);
7340Sstevel@tonic-gate 			break;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		case M_DATA:
7370Sstevel@tonic-gate 			/*
7380Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
7390Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
7400Sstevel@tonic-gate 			 * down to lower queue.
7410Sstevel@tonic-gate 			 */
7420Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
7430Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
7440Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
7450Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
7460Sstevel@tonic-gate 					freemsg(mp);
7470Sstevel@tonic-gate 			} else {
7480Sstevel@tonic-gate 				freemsg(mp);
7490Sstevel@tonic-gate 			}
7500Sstevel@tonic-gate 			break;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		default:
7530Sstevel@tonic-gate 			/*
7540Sstevel@tonic-gate 			 * Pass an error message up.
7550Sstevel@tonic-gate 			 */
7560Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
7570Sstevel@tonic-gate 			if (mp->b_cont) {
7580Sstevel@tonic-gate 				freemsg(mp->b_cont);
7590Sstevel@tonic-gate 				mp->b_cont = NULL;
7600Sstevel@tonic-gate 			}
7610Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
7620Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
7630Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
7640Sstevel@tonic-gate 			qreply(q, mp);
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 	}	/* end of while */
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate }	/* conskbduwsrv() */
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate static void
771*1828Scg149915 conskbd_ioctl(queue_t *q, mblk_t *mp)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	struct	iocblk			*iocp;
7740Sstevel@tonic-gate 	int	error = 0;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	case I_LINK:
7810Sstevel@tonic-gate 	case I_PLINK:
782267Scg149915 		if (conskbd.conskbd_bypassed == B_TRUE) {
783267Scg149915 		/*
784267Scg149915 		 * A legacy keyboard can NOT be connected to conskbd together
785267Scg149915 		 * with other keyboards. So when a legacy keyboard is already
786267Scg149915 		 * linked under conkbd, we just reject all others.
787267Scg149915 		 */
788267Scg149915 			miocnak(q, mp, 0, EAGAIN);
789267Scg149915 			break;
790267Scg149915 		}
791*1828Scg149915 		qwriter(q, mp, conskbd_ioc_plink, PERIM_OUTER);
7920Sstevel@tonic-gate 		break;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	case I_UNLINK:
7950Sstevel@tonic-gate 	case I_PUNLINK:
796*1828Scg149915 		qwriter(q, mp, conskbd_ioc_punlink, PERIM_OUTER);
7970Sstevel@tonic-gate 		break;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	case KIOCSKABORTEN:
8000Sstevel@tonic-gate 		/*
8010Sstevel@tonic-gate 		 * Check if privileged
8020Sstevel@tonic-gate 		 */
8030Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
8040Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8050Sstevel@tonic-gate 			return;
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8090Sstevel@tonic-gate 		if (error != 0) {
8100Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8110Sstevel@tonic-gate 			return;
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
8150Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8160Sstevel@tonic-gate 		break;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	default:
8190Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
8200Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
8210Sstevel@tonic-gate 		} else {
8220Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
8230Sstevel@tonic-gate 		}
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
826*1828Scg149915 }	/* conskbd_ioctl() */
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate static void
8300Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
8310Sstevel@tonic-gate {
8320Sstevel@tonic-gate 	struct iocblk		*iocp;
8330Sstevel@tonic-gate 	mblk_t			*datap;
8340Sstevel@tonic-gate 	int			cmd;
8350Sstevel@tonic-gate 	int			error = 0;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8400Sstevel@tonic-gate 	case KIOCLAYOUT:
8410Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
8420Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
8430Sstevel@tonic-gate 			break;
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
8470Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
8480Sstevel@tonic-gate 		else
8490Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
8520Sstevel@tonic-gate 		if (mp->b_cont)
8530Sstevel@tonic-gate 			freemsg(mp->b_cont);
8540Sstevel@tonic-gate 		mp->b_cont = datap;
8550Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
8560Sstevel@tonic-gate 		break;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	case KIOCSLAYOUT:
8590Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8600Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8610Sstevel@tonic-gate 			break;
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
8640Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8650Sstevel@tonic-gate 		break;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
8680Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
8690Sstevel@tonic-gate 		if (error != 0) {
8700Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8710Sstevel@tonic-gate 			break;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
8740Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8750Sstevel@tonic-gate 			break;
8760Sstevel@tonic-gate 		}
8770Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
8780Sstevel@tonic-gate 		break;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
8810Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
8820Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8830Sstevel@tonic-gate 			break;
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
8860Sstevel@tonic-gate 		break;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	case CONSSETABORTENABLE:
8890Sstevel@tonic-gate 		/*
8900Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
8910Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
8920Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
8930Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
8940Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
8950Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
8960Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
8970Sstevel@tonic-gate 		 *
8980Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
8990Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
9000Sstevel@tonic-gate 		 * already handle the message though it returned to us a
9010Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
9020Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
9030Sstevel@tonic-gate 		 * So we just return ACK to upper module.
9040Sstevel@tonic-gate 		 */
9050Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9060Sstevel@tonic-gate 		break;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	case KIOCCMD:
9090Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
9100Sstevel@tonic-gate 		    mp->b_cont == NULL) {
9110Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9120Sstevel@tonic-gate 			break;
9130Sstevel@tonic-gate 		}
9140Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
9150Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
9160Sstevel@tonic-gate 			freemsg(mp->b_cont);
9170Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
9180Sstevel@tonic-gate 			if (datap == NULL) {
9190Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9200Sstevel@tonic-gate 				return;
9210Sstevel@tonic-gate 			}
9220Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
9230Sstevel@tonic-gate 				*(int *)datap->b_wptr =
9240Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
9250Sstevel@tonic-gate 			else
9260Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 			mp->b_cont = datap;
9290Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
9300Sstevel@tonic-gate 			return;
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9330Sstevel@tonic-gate 		break;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	default:
9360Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
9370Sstevel@tonic-gate 		break;
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate static void
9430Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
9460Sstevel@tonic-gate 	struct	iocblk		*iocp;
9470Sstevel@tonic-gate 	int	error = 0;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
9520Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	case KIOCGDIRECT: {
9550Sstevel@tonic-gate 		mblk_t *datap;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
9580Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
9590Sstevel@tonic-gate 			break;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
9630Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9640Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
9650Sstevel@tonic-gate 			freemsg(mp->b_cont);
9660Sstevel@tonic-gate 			mp->b_cont = NULL;
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 		mp->b_cont = datap;
9690Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
9700Sstevel@tonic-gate 		break;
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	case KIOCSDIRECT:
9740Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9750Sstevel@tonic-gate 		if (error != 0) {
9760Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9770Sstevel@tonic-gate 			break;
9780Sstevel@tonic-gate 		}
9790Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 		/*
9820Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
9830Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
9840Sstevel@tonic-gate 		 * itself.
9850Sstevel@tonic-gate 		 */
9860Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
9870Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
9880Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
9890Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
9900Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9910Sstevel@tonic-gate 				return;
9920Sstevel@tonic-gate 			}
9930Sstevel@tonic-gate 			break;
9940Sstevel@tonic-gate 		}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9970Sstevel@tonic-gate 		break;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	default:
10000Sstevel@tonic-gate 		/*
10010Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
10020Sstevel@tonic-gate 		 * through to; otherwise, reject it.
10030Sstevel@tonic-gate 		 */
10040Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10050Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10060Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10070Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10080Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10090Sstevel@tonic-gate 				return;
10100Sstevel@tonic-gate 			}
10110Sstevel@tonic-gate 			break;
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 		/* nobody below us; reject it */
10150Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10160Sstevel@tonic-gate 		break;
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate /*
10230Sstevel@tonic-gate  * Service procedure for lower write queue.
10240Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
10250Sstevel@tonic-gate  */
10260Sstevel@tonic-gate static void
10270Sstevel@tonic-gate conskbdlwserv(queue_t *q)
10280Sstevel@tonic-gate {
10290Sstevel@tonic-gate 	register mblk_t *mp;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
10320Sstevel@tonic-gate 		putnext(q, mp);
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate }	/* conskbdlwserv() */
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate /*
10370Sstevel@tonic-gate  * Put procedure for lower read queue.
10380Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
10390Sstevel@tonic-gate  * device 1.
10400Sstevel@tonic-gate  */
10410Sstevel@tonic-gate static void
10420Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
10450Sstevel@tonic-gate 	struct iocblk 	*iocp;
10460Sstevel@tonic-gate 	Firm_event	*fe;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	case M_FLUSH:
10530Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
10540Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
10550Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
10580Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
10590Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
10600Sstevel@tonic-gate 		} else
10610Sstevel@tonic-gate 			freemsg(mp);
10620Sstevel@tonic-gate 		break;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	case M_DATA:
10650Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 			/*
10700Sstevel@tonic-gate 			 * This is a workaround.
10710Sstevel@tonic-gate 			 *
10720Sstevel@tonic-gate 			 * According to HID specification, there are the
10730Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
10740Sstevel@tonic-gate 			 *
10750Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
10760Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
10770Sstevel@tonic-gate 			 *
10780Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
10790Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
10800Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
10810Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
10820Sstevel@tonic-gate 			 * not know which is being pressed or released when we
10830Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
10840Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
10850Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
10860Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
10870Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
10880Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
10890Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
10900Sstevel@tonic-gate 			 * the key to 49.
10910Sstevel@tonic-gate 			 */
10920Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
10930Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
10940Sstevel@tonic-gate 					fe->id = 49;
10950Sstevel@tonic-gate 				else
10960Sstevel@tonic-gate 					fe->id = 50;
10970Sstevel@tonic-gate 			}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 			/*
11000Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
11010Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
11020Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
11030Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
11040Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
11050Sstevel@tonic-gate 			 * as held-down for ever.
11060Sstevel@tonic-gate 			 */
11070Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
11080Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
11090Sstevel@tonic-gate 			if (fe->value)
11100Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
11110Sstevel@tonic-gate 			else
11120Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
11150Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
11160Sstevel@tonic-gate 			freemsg(mp);
11170Sstevel@tonic-gate 		} else {
11180Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
11190Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
11200Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
11210Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
11220Sstevel@tonic-gate 			else
11230Sstevel@tonic-gate 				freemsg(mp);
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
11260Sstevel@tonic-gate 		break;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	case M_IOCACK:
11290Sstevel@tonic-gate 	case M_IOCNAK:
11300Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
11310Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
11340Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
11370Sstevel@tonic-gate 		break;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	case M_ERROR:
11400Sstevel@tonic-gate 	case M_HANGUP:
11410Sstevel@tonic-gate 	default:
11420Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
11430Sstevel@tonic-gate 		break;
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate }	/* conskbdlrput() */
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /* ARGSUSED */
11500Sstevel@tonic-gate static int
11510Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
11540Sstevel@tonic-gate 		return (EACCES);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	return (0);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate }	/* conskbd_kstat_update() */
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate /*
11630Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
11640Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
11650Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
11660Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
11670Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
11680Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
11690Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
11700Sstevel@tonic-gate  * request matching received responses by searching the global linked
11710Sstevel@tonic-gate  * list to find the request which has the same message ID of the
11720Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
11730Sstevel@tonic-gate  * give a response to upstreams based the following policy:
11740Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
11750Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
11760Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
11770Sstevel@tonic-gate  * the first response is sent to upstream.
11780Sstevel@tonic-gate  */
11790Sstevel@tonic-gate static void
11800Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
11810Sstevel@tonic-gate {
11820Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
11830Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
11840Sstevel@tonic-gate 	struct iocblk	*iocp;
11850Sstevel@tonic-gate 	mblk_t		*clonemp;
11860Sstevel@tonic-gate 	int		retry;
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
11890Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
11900Sstevel@tonic-gate 		return;
11910Sstevel@tonic-gate 	}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
11940Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
11950Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
11960Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
11970Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
12000Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
12010Sstevel@tonic-gate 	    iocp->ioc_cmd == KIOCCMD);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
12040Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
12050Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
12060Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
12070Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
12080Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 		/*
12130Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
12140Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
12150Sstevel@tonic-gate 		 * otherwise, system will panic.
12160Sstevel@tonic-gate 		 */
12170Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
12180Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
12190Sstevel@tonic-gate 			lqs = lqs->lqs_next;
12200Sstevel@tonic-gate 			msg->kpm_req_nums --;
12210Sstevel@tonic-gate 			retry = 0;
12220Sstevel@tonic-gate 			continue;
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 		clonemp = copymsg(mp);
12260Sstevel@tonic-gate 		if (clonemp != NULL) {
12270Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
12280Sstevel@tonic-gate 				lqs = lqs->lqs_next;
12290Sstevel@tonic-gate 				retry = 0;
12300Sstevel@tonic-gate 				continue;
12310Sstevel@tonic-gate 			}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 			/*
12340Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
12350Sstevel@tonic-gate 			 */
12360Sstevel@tonic-gate 			freemsg(clonemp);
12370Sstevel@tonic-gate 		}
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		/*
12400Sstevel@tonic-gate 		 * During testing it was observed that occasionally
12410Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
12420Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
12430Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
12440Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
12450Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
12460Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
12470Sstevel@tonic-gate 		 *
12480Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
12490Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
12500Sstevel@tonic-gate 		 * or behave weirdly,
12510Sstevel@tonic-gate 		 *
12520Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
12530Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
12540Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
12550Sstevel@tonic-gate 		 *	entering kmdb;
12560Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
12570Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
12580Sstevel@tonic-gate 		 *	is not available in normal mode once returning
12590Sstevel@tonic-gate 		 *	from kmdb;
12600Sstevel@tonic-gate 		 * 3) for KIOCCMD
12610Sstevel@tonic-gate 		 * 	3.1) for KBD_CMD_NOBELL
12620Sstevel@tonic-gate 		 * 		there's no beep in USB and PS2 keyboard,
12630Sstevel@tonic-gate 		 * 		this ioctl actually disables the beep on
12640Sstevel@tonic-gate 		 * 		system mainboard. Note that all the cloned
12650Sstevel@tonic-gate 		 * 		messages sent down to lower queues do the
12660Sstevel@tonic-gate 		 * 		same job for system mainboard. Therefore,
12670Sstevel@tonic-gate 		 * 		even if we fail to send this ioctl to most
12680Sstevel@tonic-gate 		 * 		of lower queues, the beep still would be
12690Sstevel@tonic-gate 		 * 		disabled. So, no trouble exists here.
12700Sstevel@tonic-gate 		 *	3.2) for others
12710Sstevel@tonic-gate 		 *		nothing;
12720Sstevel@tonic-gate 		 *
12730Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
12740Sstevel@tonic-gate 		 * same request comes again.
12750Sstevel@tonic-gate 		 */
12760Sstevel@tonic-gate 		if (retry ++ >= 5) {
12770Sstevel@tonic-gate 			dev_t	devt;
12780Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
12810Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
12820Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
12830Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
12840Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
12850Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
12860Sstevel@tonic-gate 					    "keyboard is not available"
12870Sstevel@tonic-gate 					    " for system debugging: %s",
12880Sstevel@tonic-gate 					    path);
12890Sstevel@tonic-gate 				break;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
12920Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
12930Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
12940Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
12950Sstevel@tonic-gate 					    "keyboard is not available:"
12960Sstevel@tonic-gate 					    " %s", path);
12970Sstevel@tonic-gate 				break;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 			default:
13000Sstevel@tonic-gate 				break;
13010Sstevel@tonic-gate 			}
13020Sstevel@tonic-gate 			msg->kpm_req_nums --;
13030Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13040Sstevel@tonic-gate 			retry = 0;
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
13090Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
13100Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
13110Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate static void
13180Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	mblk_t		*req;
13210Sstevel@tonic-gate 	queue_t		*lowque;
13220Sstevel@tonic-gate 	struct linkblk		*linkp;
13230Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
13260Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
13290Sstevel@tonic-gate 	lowque = linkp->l_qbot;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
13320Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
13330Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
13360Sstevel@tonic-gate 	if (req == NULL) {
13370Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13380Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13390Sstevel@tonic-gate 		return;
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
13430Sstevel@tonic-gate 	if (req->b_cont == NULL) {
13440Sstevel@tonic-gate 		freemsg(req);
13450Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13460Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13470Sstevel@tonic-gate 		return;
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
1350*1828Scg149915 	lowque->q_ptr = lqs;
1351*1828Scg149915 	OTHERQ(lowque)->q_ptr = lqs;
13520Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
13530Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
13580Sstevel@tonic-gate 		freemsg(req);
13590Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
13600Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
13610Sstevel@tonic-gate 		lowque->q_ptr = NULL;
1362*1828Scg149915 		OTHERQ(lowque)->q_ptr = NULL;
13630Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 
1369*1828Scg149915 static void
1370*1828Scg149915 conskbd_ioc_punlink(queue_t *q, mblk_t *mp)
1371*1828Scg149915 {
1372*1828Scg149915 	int			index;
1373*1828Scg149915 	struct linkblk		*linkp;
1374*1828Scg149915 	conskbd_lower_queue_t	*lqs;
1375*1828Scg149915 	conskbd_lower_queue_t	*prev;
1376*1828Scg149915 
1377*1828Scg149915 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
1378*1828Scg149915 	prev = conskbd.conskbd_lqueue_list;
1379*1828Scg149915 	for (lqs = prev; lqs; lqs = lqs->lqs_next) {
1380*1828Scg149915 		if (lqs->lqs_queue == linkp->l_qbot) {
1381*1828Scg149915 			if (prev == lqs)
1382*1828Scg149915 				conskbd.conskbd_lqueue_list =
1383*1828Scg149915 				    lqs->lqs_next;
1384*1828Scg149915 			else
1385*1828Scg149915 				prev->lqs_next = lqs->lqs_next;
1386*1828Scg149915 
1387*1828Scg149915 			lqs->lqs_queue->q_ptr =  NULL;
1388*1828Scg149915 			OTHERQ(lqs->lqs_queue)->q_ptr = NULL;
1389*1828Scg149915 			conskbd.conskbd_lqueue_nums --;
1390*1828Scg149915 			if (conskbd.conskbd_lqueue_nums == 0) {
1391*1828Scg149915 				kbd_layout_bak = conskbd.conskbd_layout;
1392*1828Scg149915 				conskbd.conskbd_layout = -1;
1393*1828Scg149915 			}
1394*1828Scg149915 
1395*1828Scg149915 			for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
1396*1828Scg149915 				if (lqs->lqs_key_state[index] == KEY_PRESSED)
1397*1828Scg149915 					kbtrans_streams_key(
1398*1828Scg149915 					    conskbd.conskbd_kbtrans,
1399*1828Scg149915 					    index,
1400*1828Scg149915 					    KEY_RELEASED);
1401*1828Scg149915 			}
1402*1828Scg149915 
1403*1828Scg149915 			kmem_free(lqs, sizeof (*lqs));
1404*1828Scg149915 			miocack(q, mp, 0, 0);
1405*1828Scg149915 			return;
1406*1828Scg149915 		}
1407*1828Scg149915 		prev = lqs;
1408*1828Scg149915 	}
1409*1828Scg149915 	miocnak(q, mp, 0, EINVAL);
1410*1828Scg149915 
1411*1828Scg149915 }	/* conskbd_ioc_punlink() */
1412*1828Scg149915 
14130Sstevel@tonic-gate /*
14140Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
14150Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
14160Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
14170Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
14180Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
14190Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
14200Sstevel@tonic-gate  */
14210Sstevel@tonic-gate static void
14220Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
14230Sstevel@tonic-gate {
14240Sstevel@tonic-gate 	switch (lqs->lqs_state) {
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
14270Sstevel@tonic-gate 	case LQS_INITIALIZED:
14280Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
14290Sstevel@tonic-gate 		break;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
14320Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
14330Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
14340Sstevel@tonic-gate 		break;
14350Sstevel@tonic-gate 
14361272Slq150181 	/* S4: wait lower queue to acknowledge KIOCSLED/KIOCGLED  message */
14370Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
14380Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
14390Sstevel@tonic-gate 		break;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
14420Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
14430Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
14440Sstevel@tonic-gate 		break;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
14470Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
14480Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
14490Sstevel@tonic-gate 		break;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
14520Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
14530Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
14540Sstevel@tonic-gate 		break;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	/* if reaching here, there must be a error */
14570Sstevel@tonic-gate 	default:
14580Sstevel@tonic-gate 		freemsg(mp);
14590Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
14600Sstevel@tonic-gate 		break;
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate static void
14670Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
14680Sstevel@tonic-gate {
14690Sstevel@tonic-gate 	struct iocblk	*iocp;
14700Sstevel@tonic-gate 	mblk_t		*req;
14710Sstevel@tonic-gate 	queue_t		*lowerque;
1472*1828Scg149915 	int		err = ENOMEM;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
14750Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
14800Sstevel@tonic-gate 	case M_IOCACK:
14810Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
14820Sstevel@tonic-gate 		if (req == NULL) {
1483*1828Scg149915 			goto err_exit;
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
14870Sstevel@tonic-gate 		if (req->b_cont == NULL) {
14880Sstevel@tonic-gate 			freemsg(req);
1489*1828Scg149915 			goto err_exit;
14900Sstevel@tonic-gate 		}
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
14930Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
14940Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
14970Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
15000Sstevel@tonic-gate 			freemsg(req);
1501*1828Scg149915 			goto err_exit;
15020Sstevel@tonic-gate 		}
1503*1828Scg149915 		freemsg(mp);
1504*1828Scg149915 		return;
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	case M_IOCNAK:
15070Sstevel@tonic-gate 		/*
15080Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
15090Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
15100Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
15110Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
15120Sstevel@tonic-gate 		 * will bypass the conskbd module.
15130Sstevel@tonic-gate 		 */
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 		/*
15160Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
15170Sstevel@tonic-gate 		 * conskbd, we reject the current one.
15180Sstevel@tonic-gate 		 */
15190Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
15200Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
1521*1828Scg149915 			err = iocp->ioc_error;
1522*1828Scg149915 			goto err_exit;
15230Sstevel@tonic-gate 		}
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 		/*
1526*1828Scg149915 		 * link this keyboard under conskbd.
15270Sstevel@tonic-gate 		 */
1528*1828Scg149915 		qwriter(lowerque, mp, conskbd_link_lowque_legacy, PERIM_OUTER);
1529*1828Scg149915 		return;
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
1532*1828Scg149915 err_exit:
1533*1828Scg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
1534*1828Scg149915 	lowerque->q_ptr = NULL;
1535*1828Scg149915 	OTHERQ(lowerque)->q_ptr = NULL;
1536*1828Scg149915 	kmem_free(lqs, sizeof (*lqs));
15370Sstevel@tonic-gate 	freemsg(mp);
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
15400Sstevel@tonic-gate 
15410Sstevel@tonic-gate static void
15420Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15430Sstevel@tonic-gate {
15440Sstevel@tonic-gate 	struct iocblk 	*iocp;
15450Sstevel@tonic-gate 	mblk_t		*req;
15460Sstevel@tonic-gate 	queue_t		*lowerque;
1547*1828Scg149915 	int		err = ENOMEM;
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
15500Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15550Sstevel@tonic-gate 	case M_IOCACK:
15560Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
15570Sstevel@tonic-gate 		if (req == NULL) {
1558*1828Scg149915 			goto err_exit;
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
15620Sstevel@tonic-gate 		if (req->b_cont == NULL) {
15630Sstevel@tonic-gate 			freemsg(req);
1564*1828Scg149915 			goto err_exit;
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
15680Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
15690Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
15700Sstevel@tonic-gate 			freemsg(req);
1571*1828Scg149915 			goto err_exit;
15720Sstevel@tonic-gate 		}
1573*1828Scg149915 		freemsg(mp);
1574*1828Scg149915 		return;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	case M_IOCNAK:
15770Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
1578*1828Scg149915 		err = iocp->ioc_error;
1579*1828Scg149915 		goto err_exit;
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate 
1582*1828Scg149915 err_exit:
1583*1828Scg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
1584*1828Scg149915 	lowerque->q_ptr = NULL;
1585*1828Scg149915 	OTHERQ(lowerque)->q_ptr = NULL;
1586*1828Scg149915 	kmem_free(lqs, sizeof (*lqs));
15870Sstevel@tonic-gate 	freemsg(mp);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
15900Sstevel@tonic-gate 
15911274Sqz150045 /*
15921274Sqz150045  * Allocate a firm event
15931274Sqz150045  */
15941274Sqz150045 static mblk_t *
15951274Sqz150045 conskbd_alloc_firm_event(int id, int value)
15961274Sqz150045 {
15971274Sqz150045 	mblk_t	*mb;
15981274Sqz150045 	Firm_event *fe;
15991274Sqz150045 
16001274Sqz150045 	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
16011274Sqz150045 		fe = (Firm_event *)mb->b_wptr;
16021274Sqz150045 		fe->id = id;
16031274Sqz150045 		fe->pair_type = FE_PAIR_NONE;
16041274Sqz150045 		fe->pair = NULL;
16051274Sqz150045 		fe->value = value;
16061274Sqz150045 		mb->b_wptr += sizeof (Firm_event);
16071274Sqz150045 	}
16081274Sqz150045 
16091274Sqz150045 	return (mb);
16101274Sqz150045 }
16111274Sqz150045 
16120Sstevel@tonic-gate static void
16130Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16140Sstevel@tonic-gate {
16150Sstevel@tonic-gate 	mblk_t		*req;
16160Sstevel@tonic-gate 	int		layout;
16170Sstevel@tonic-gate 	boolean_t	fail;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16200Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
16230Sstevel@tonic-gate 	case M_IOCACK:
16240Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
16250Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
16260Sstevel@tonic-gate 			/*
16270Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
16280Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
16290Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
16300Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
16310Sstevel@tonic-gate 			 */
16321274Sqz150045 			if (layout != -1 && conskbd.conskbd_layout == -1) {
16331274Sqz150045 				if (layout == 0) {
16341274Sqz150045 					conskbd.conskbd_layout = kbd_layout_bak;
16351274Sqz150045 				} else {
16361274Sqz150045 					conskbd.conskbd_layout = layout;
16371274Sqz150045 					if (layout == kbd_layout_bak) {
16381274Sqz150045 						break;
16391274Sqz150045 					}
16401274Sqz150045 					if ((req = conskbd_alloc_firm_event(
16411274Sqz150045 						KEYBOARD_LAYOUT_CHANGE,
16421274Sqz150045 						layout)) != NULL) {
16431274Sqz150045 						if (conskbd.conskbd_directio)
16441274Sqz150045 							putnext(
16451274Sqz150045 							    conskbd_regqueue,
16461274Sqz150045 							    req);
16471274Sqz150045 						else if (conskbd_consqueue
16481274Sqz150045 							    != NULL)
16491274Sqz150045 							putnext(
16501274Sqz150045 							    conskbd_consqueue,
16511274Sqz150045 							    req);
16521274Sqz150045 					}
16531274Sqz150045 				}
16541274Sqz150045 			}
16550Sstevel@tonic-gate 		}
16560Sstevel@tonic-gate 		break;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
16600Sstevel@tonic-gate 	case M_IOCNAK:
16610Sstevel@tonic-gate 		break;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	fail = B_TRUE;
16651272Slq150181 
16661272Slq150181 	if (conskbd.conskbd_led_state == -1)
16671272Slq150181 		req = mkiocb(KIOCGLED);
16681272Slq150181 	else
16691272Slq150181 		req = mkiocb(KIOCSLED);
16701272Slq150181 
16710Sstevel@tonic-gate 	if (req) {
16720Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
16730Sstevel@tonic-gate 		if (req->b_cont) {
16741272Slq150181 			if (conskbd.conskbd_led_state != -1) {
16751272Slq150181 				*(uchar_t *)req->b_cont->b_wptr =
16761272Slq150181 				    conskbd.conskbd_led_state;
16771272Slq150181 				req->b_cont->b_wptr += sizeof (uchar_t);
16781272Slq150181 			}
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
16810Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
16820Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
16830Sstevel@tonic-gate 				fail = B_FALSE;
16840Sstevel@tonic-gate 			} else {
16850Sstevel@tonic-gate 				freemsg(req);
16860Sstevel@tonic-gate 			}
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 		} else {
16890Sstevel@tonic-gate 			freemsg(req);
16900Sstevel@tonic-gate 		}
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	if (fail) {
16940Sstevel@tonic-gate 		/*
16951272Slq150181 		 * If fail to allocate KIOCSLED/KIOCGLED message or put
16961272Slq150181 		 * the message into lower queue, we immediately link
16971272Slq150181 		 * current keyboard under conskbd. Thus, even if fails
16981272Slq150181 		 * to set/get LED, this keyboard could be available.
16990Sstevel@tonic-gate 		 */
1700*1828Scg149915 		qwriter(lqs->lqs_queue,
1701*1828Scg149915 		    mp, conskbd_link_lowque_virt, PERIM_OUTER);
1702*1828Scg149915 	} else {
1703*1828Scg149915 		freemsg(mp);
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate static void
17100Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
17110Sstevel@tonic-gate {
17121272Slq150181 	int	led_state;
17131272Slq150181 
17140Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
17150Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
17160Sstevel@tonic-gate 
17171272Slq150181 	if (conskbd.conskbd_led_state == -1) {
17181272Slq150181 		switch (mp->b_datap->db_type) {
17191272Slq150181 		case M_IOCACK:
17201272Slq150181 			if (miocpullup(mp, sizeof (uchar_t)) == 0) {
17211272Slq150181 				led_state = *(uchar_t *)mp->b_cont->b_rptr;
17221272Slq150181 				conskbd.conskbd_led_state = led_state;
17231272Slq150181 				kbtrans_streams_setled(conskbd.conskbd_kbtrans,
17241272Slq150181 				    led_state);
17251272Slq150181 			}
17261272Slq150181 			break;
17271272Slq150181 
17281272Slq150181 		/* if fail, leave conskbd's led_state as it is */
17291272Slq150181 		case M_IOCNAK:
17301272Slq150181 			break;
17311272Slq150181 		}
17321272Slq150181 	}
17331272Slq150181 
17340Sstevel@tonic-gate 	/*
17351272Slq150181 	 * Basically, failure of setting/getting LED is not a fatal
17361272Slq150181 	 * error, so we will plumb the lower queue into conskbd whether
17371272Slq150181 	 * setting/getting LED succeeds or fails.
17380Sstevel@tonic-gate 	 */
1739*1828Scg149915 	qwriter(lqs->lqs_queue, mp, conskbd_link_lowque_virt, PERIM_OUTER);
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate static void
17450Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
17460Sstevel@tonic-gate {
17470Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
17480Sstevel@tonic-gate 	struct iocblk		*iocp;
17490Sstevel@tonic-gate 	int			error;
17500Sstevel@tonic-gate 	dev_t			devt;
17510Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
17540Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	if (!msg) {
17570Sstevel@tonic-gate 		/*
1758*1828Scg149915 		 * Here we discard the response if:
1759*1828Scg149915 		 *
1760*1828Scg149915 		 *   1. It's an KIOCSLED request; see conskbd_streams_setled().
1761*1828Scg149915 		 *   2. The application has already closed the upper stream;
1762*1828Scg149915 		 *		see conskbdclose()
17630Sstevel@tonic-gate 		 */
17640Sstevel@tonic-gate 		freemsg(mp);
17650Sstevel@tonic-gate 		return;
17660Sstevel@tonic-gate 	}
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	/*
17690Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
17700Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
17710Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
17720Sstevel@tonic-gate 	 * the lower queue from which the current response message
17730Sstevel@tonic-gate 	 * comes.
17740Sstevel@tonic-gate 	 */
17750Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
17760Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
17770Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
17780Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
17790Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
17800Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
17810Sstevel@tonic-gate 	mutex_exit(&msg->kpm_lock);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	if (msg->kpm_resp_nums < msg->kpm_req_nums)
17840Sstevel@tonic-gate 		return;
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
17870Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	/*
17930Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
17940Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
17950Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
17960Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
17970Sstevel@tonic-gate 	 * is sent to upstreams
17980Sstevel@tonic-gate 	 */
17990Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
18000Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
18030Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
18040Sstevel@tonic-gate 		/*
18050Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
18060Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
18070Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
18080Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
18090Sstevel@tonic-gate 		 * check if the pointer is null.
18100Sstevel@tonic-gate 		 */
18110Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18120Sstevel@tonic-gate 			cons_polledio_t		*polledio;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18150Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
18160Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
18170Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
18180Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
18190Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
18200Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
18210Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
18220Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
18230Sstevel@tonic-gate 					error = 0;
18240Sstevel@tonic-gate 				} else {
18250Sstevel@tonic-gate 					/*
18260Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
18270Sstevel@tonic-gate 					 * use the same cons_polledio structure
18280Sstevel@tonic-gate 					 * as conskbd.
18290Sstevel@tonic-gate 					 */
18300Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
18310Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
18320Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
18330Sstevel@tonic-gate 						    "driver does not support "
18340Sstevel@tonic-gate 						    "system debugging: %s",
18350Sstevel@tonic-gate 						    path);
18360Sstevel@tonic-gate 					}
18370Sstevel@tonic-gate 					error = EINVAL;
18380Sstevel@tonic-gate 				}
18390Sstevel@tonic-gate 			} else {
18400Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
18410Sstevel@tonic-gate 				    DDI_SUCCESS) {
18420Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
18430Sstevel@tonic-gate 					    " not available for system"
18440Sstevel@tonic-gate 					    " debugging:  %s", path);
18450Sstevel@tonic-gate 				}
18460Sstevel@tonic-gate 			}
18470Sstevel@tonic-gate 			mp->b_next = NULL;
18480Sstevel@tonic-gate 			mp->b_prev = NULL;
18490Sstevel@tonic-gate 			freemsg(mp);
18500Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18510Sstevel@tonic-gate 		}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
18540Sstevel@tonic-gate 		if (error == 0) {
18550Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
18560Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
18570Sstevel@tonic-gate 		}
18580Sstevel@tonic-gate 		break;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
18610Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18620Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18630Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
18640Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
18650Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
18660Sstevel@tonic-gate 				error = 0;
18670Sstevel@tonic-gate 			} else {
18680Sstevel@tonic-gate 				devt =
18690Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
18720Sstevel@tonic-gate 				    DDI_SUCCESS) {
18730Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
18740Sstevel@tonic-gate 					    " not available: %s", path);
18750Sstevel@tonic-gate 				}
18760Sstevel@tonic-gate 			}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 			mp->b_next = NULL;
18790Sstevel@tonic-gate 			mp->b_prev = NULL;
18800Sstevel@tonic-gate 			freemsg(mp);
18810Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18820Sstevel@tonic-gate 		}
18830Sstevel@tonic-gate 		break;
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	case KIOCCMD:
18860Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18870Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
18900Sstevel@tonic-gate 				error = 0;
18910Sstevel@tonic-gate 			mp->b_next = NULL;
18920Sstevel@tonic-gate 			mp->b_prev = NULL;
18930Sstevel@tonic-gate 			freemsg(mp);
18940Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18950Sstevel@tonic-gate 		}
18960Sstevel@tonic-gate 		break;
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
18990Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
19000Sstevel@tonic-gate 	}
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
19030Sstevel@tonic-gate 	if (error == 0) {
19040Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
19050Sstevel@tonic-gate 	} else {
19060Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
19070Sstevel@tonic-gate 	}
19080Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
19090Sstevel@tonic-gate 	iocp->ioc_error = error;
19100Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
19110Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
19120Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
19150Sstevel@tonic-gate 
1916*1828Scg149915 static void
1917*1828Scg149915 conskbd_link_lowque_legacy(queue_t *lowque, mblk_t *mp)
1918*1828Scg149915 {
1919*1828Scg149915 	conskbd_lower_queue_t *lqs;
1920*1828Scg149915 
1921*1828Scg149915 	freemsg(mp);
1922*1828Scg149915 
1923*1828Scg149915 	/*
1924*1828Scg149915 	 * Bypass the virutal keyboard for old hardware,
1925*1828Scg149915 	 * Now, only current legacy keyboard can be linked
1926*1828Scg149915 	 * under conskbd
1927*1828Scg149915 	 */
1928*1828Scg149915 	conskbd.conskbd_bypassed = B_TRUE;
1929*1828Scg149915 
1930*1828Scg149915 	/*
1931*1828Scg149915 	 * Link the lower queue under conskbd
1932*1828Scg149915 	 */
1933*1828Scg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
1934*1828Scg149915 	lqs->lqs_state = LQS_INITIALIZED_LEGACY;
1935*1828Scg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
1936*1828Scg149915 	conskbd.conskbd_lqueue_list = lqs;
1937*1828Scg149915 	conskbd.conskbd_lqueue_nums++;
1938*1828Scg149915 
1939*1828Scg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
1940*1828Scg149915 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
1941*1828Scg149915 
1942*1828Scg149915 }	/* conskbd_link_lowque_legacy() */
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate static void
1945*1828Scg149915 conskbd_link_lowque_virt(queue_t *lowque, mblk_t *mp)
19460Sstevel@tonic-gate {
19470Sstevel@tonic-gate 	int		index;
1948*1828Scg149915 	conskbd_lower_queue_t *lqs;
19490Sstevel@tonic-gate 
1950*1828Scg149915 	freemsg(mp);
19510Sstevel@tonic-gate 
1952*1828Scg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
1953*1828Scg149915 
1954*1828Scg149915 	ASSERT(lqs->lqs_queue == lowque);
1955*1828Scg149915 	ASSERT(lqs->lqs_pending_plink != NULL);
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	/*
19580Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
19590Sstevel@tonic-gate 	 */
19600Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
19610Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
19620Sstevel@tonic-gate 	}
1963*1828Scg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
19640Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
1965*1828Scg149915 	conskbd.conskbd_lqueue_nums++;
1966*1828Scg149915 	conskbd.conskbd_lqueue_list = lqs;
1967*1828Scg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
19680Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
19690Sstevel@tonic-gate 
1970*1828Scg149915 }	/* conskbd_link_lowque_virt() */
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate /*ARGSUSED*/
19730Sstevel@tonic-gate static void
19740Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
19750Sstevel@tonic-gate {
19760Sstevel@tonic-gate 	struct iocblk	*iocp;
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	/*
19810Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
19820Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
19830Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
19840Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
19850Sstevel@tonic-gate 	 * should be added to this list.
19860Sstevel@tonic-gate 	 */
19870Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
19900Sstevel@tonic-gate 			(iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19930Sstevel@tonic-gate 			("conskbd_legacy_upstream_msg: "
19940Sstevel@tonic-gate 			"CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
19950Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
19980Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19990Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
20040Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20050Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
20060Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
20070Sstevel@tonic-gate 	} else {
20080Sstevel@tonic-gate 		/* if reached here, it must be a error */
20090Sstevel@tonic-gate 		cmn_err(CE_WARN,
20100Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
20110Sstevel@tonic-gate 		freemsg(mp);
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate /*
20170Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
20180Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
20190Sstevel@tonic-gate  *
20200Sstevel@tonic-gate  * 1) application initiated request
20210Sstevel@tonic-gate  * 	A KIOCSLED ioctl is sent by an application. The ioctl will be
20220Sstevel@tonic-gate  * 	be prcoessed by queue service procedure conskbduwsrv(), which
20230Sstevel@tonic-gate  * 	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
20240Sstevel@tonic-gate  * 	conskbd_streams_setled() to set LED, after that,  kbtrans will
20250Sstevel@tonic-gate  * 	return an ACK message to upper module.
20260Sstevel@tonic-gate  *
20270Sstevel@tonic-gate  * 2) Kbtrans initiated the request
20280Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
20290Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
20300Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
20310Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
20320Sstevel@tonic-gate  *
20330Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
20340Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
20350Sstevel@tonic-gate  * has no return value.
20360Sstevel@tonic-gate  */
20370Sstevel@tonic-gate static void
20380Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
20390Sstevel@tonic-gate {
20400Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20410Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
20420Sstevel@tonic-gate 	mblk_t		*req;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	if (led_state == -1)
20470Sstevel@tonic-gate 		return;
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	/*
20520Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
20530Sstevel@tonic-gate 	 * it if this happens.
20540Sstevel@tonic-gate 	 */
20550Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
20560Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 		if (!req) {
20590Sstevel@tonic-gate 			continue;
20600Sstevel@tonic-gate 		}
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
20630Sstevel@tonic-gate 		if (!req->b_cont) {
20640Sstevel@tonic-gate 			freemsg(req);
20650Sstevel@tonic-gate 			continue;
20660Sstevel@tonic-gate 		}
20670Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
20680Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
20690Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
20700Sstevel@tonic-gate 			freemsg(req);
20710Sstevel@tonic-gate 	}
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate }	/* conskbd_streams_setled() */
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate static void
20760Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
20770Sstevel@tonic-gate {
20780Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20790Sstevel@tonic-gate 	struct cons_polledio		*cb;
20800Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
20830Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
20840Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
20850Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
20860Sstevel@tonic-gate 			    led_state);
20870Sstevel@tonic-gate 		}
20880Sstevel@tonic-gate 	}
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate static boolean_t
20930Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
20940Sstevel@tonic-gate 		kbtrans_key_t *keycode, enum keystate *state)
20950Sstevel@tonic-gate {
20960Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20970Sstevel@tonic-gate 	struct cons_polledio 		*cb;
20980Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
20990Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
21020Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
21030Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21040Sstevel@tonic-gate 		if ((cb != NULL) &&
21050Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
21060Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
21070Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
21080Sstevel@tonic-gate 		}
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
21110Sstevel@tonic-gate 		if (ret == B_TRUE) {
21121097Slq150181 
21131097Slq150181 			/* A legacy keyboard ? */
21141097Slq150181 			if (conskbd.conskbd_bypassed == B_TRUE)
21151097Slq150181 				break;
21161097Slq150181 
21171097Slq150181 			/*
21181097Slq150181 			 * This is the PS2 scancode 0x2B -> USB(49) /
21191097Slq150181 			 * USB(50) keycode mapping workaround, for
21201097Slq150181 			 * polled mode.
21211097Slq150181 			 *
21221097Slq150181 			 * There are two possible USB keycode mappings
21231097Slq150181 			 * for PS2 scancode 0x2B and this workaround
21241097Slq150181 			 * makes sure that we use the USB keycode that
21251097Slq150181 			 * does not end up being mapped to a HOLE key
21261097Slq150181 			 * using the current keyboard translation
21271097Slq150181 			 * tables.
21281097Slq150181 			 *
21291097Slq150181 			 * See conskbdlrput() for a detailed
21301097Slq150181 			 * explanation of the problem.
21311097Slq150181 			 */
21321097Slq150181 			if (*keycode == 49 || *keycode == 50) {
21331097Slq150181 				if (conskbd_keyindex->k_normal[50] == HOLE)
21341097Slq150181 					*keycode = 49;
21351097Slq150181 				else
21361097Slq150181 					*keycode = 50;
21371097Slq150181 			}
21381097Slq150181 
21390Sstevel@tonic-gate 			break;
21400Sstevel@tonic-gate 		}
21410Sstevel@tonic-gate 	}
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	return (ret);
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate static boolean_t
21480Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
21490Sstevel@tonic-gate {
21500Sstevel@tonic-gate 	struct iocblk		*iocp;
21510Sstevel@tonic-gate 	int		directio;
21520Sstevel@tonic-gate 	int		error;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
21550Sstevel@tonic-gate 		return (B_FALSE);
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
21600Sstevel@tonic-gate 	case KIOCGDIRECT: {
21610Sstevel@tonic-gate 		/*
21620Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
21630Sstevel@tonic-gate 		 * respond incorrectly.
21640Sstevel@tonic-gate 		 */
21650Sstevel@tonic-gate 		register mblk_t *datap;
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
21680Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
21690Sstevel@tonic-gate 			return (B_TRUE);
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
21730Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
21740Sstevel@tonic-gate 		if (mp->b_cont) {
21750Sstevel@tonic-gate 			freemsg(mp->b_cont);
21760Sstevel@tonic-gate 			mp->b_cont = NULL;
21770Sstevel@tonic-gate 		}
21780Sstevel@tonic-gate 		mp->b_cont = datap;
21790Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
21800Sstevel@tonic-gate 		return (B_TRUE);
21810Sstevel@tonic-gate 	}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	case KIOCSDIRECT:
21840Sstevel@tonic-gate 		/*
21850Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
21860Sstevel@tonic-gate 		 * based code see it and respond to it.
21870Sstevel@tonic-gate 		 */
21880Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
21890Sstevel@tonic-gate 		if (error != 0) {
21900Sstevel@tonic-gate 			return (B_FALSE);
21910Sstevel@tonic-gate 		}
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
21940Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
21950Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
21960Sstevel@tonic-gate 			return (B_TRUE);
21970Sstevel@tonic-gate 		}
21980Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
22010Sstevel@tonic-gate 			kbtrans_streams_set_queue(
22020Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
22030Sstevel@tonic-gate 		} else {
22040Sstevel@tonic-gate 			kbtrans_streams_set_queue(
22050Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
22060Sstevel@tonic-gate 		}
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 		/*
22090Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
22100Sstevel@tonic-gate 		 */
22110Sstevel@tonic-gate 		return (B_FALSE);
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	default:
22140Sstevel@tonic-gate 		return (B_FALSE);
22150Sstevel@tonic-gate 	}
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate static void
22211762Slt200341 conskbd_polledio_enter(cons_polledio_arg_t arg)
22220Sstevel@tonic-gate {
22230Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
22240Sstevel@tonic-gate 	struct cons_polledio		*cb;
22250Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22280Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
22290Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
22300Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
22310Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 	}
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate static void
22381762Slt200341 conskbd_polledio_exit(cons_polledio_arg_t arg)
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
22410Sstevel@tonic-gate 	struct cons_polledio		*cb;
22420Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22450Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
22460Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
22470Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
22480Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
22490Sstevel@tonic-gate 		}
22500Sstevel@tonic-gate 	}
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate static int
22551762Slt200341 conskbd_polledio_getchar(cons_polledio_arg_t arg)
22560Sstevel@tonic-gate {
22570Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate static int
22661762Slt200341 conskbd_polledio_ischar(cons_polledio_arg_t arg)
22670Sstevel@tonic-gate {
22680Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate static void
22780Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
22790Sstevel@tonic-gate {
22800Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
22810Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
22820Sstevel@tonic-gate 	conskbd_msg_queue = msg;
22830Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate /*
22880Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
22890Sstevel@tonic-gate  */
22900Sstevel@tonic-gate static conskbd_pending_msg_t *
22910Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
22920Sstevel@tonic-gate {
22930Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
22940Sstevel@tonic-gate 	struct iocblk		*iocp;
22950Sstevel@tonic-gate 	uint_t	id;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
22980Sstevel@tonic-gate 	msg = conskbd_msg_queue;
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
23010Sstevel@tonic-gate 	ASSERT(iocp);
23020Sstevel@tonic-gate 	id = iocp->ioc_id;
23030Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
23040Sstevel@tonic-gate 		msg = msg->kpm_next;
23050Sstevel@tonic-gate 	}
23060Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	return (msg);
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate static void
23140Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
23150Sstevel@tonic-gate {
23160Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
23170Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23200Sstevel@tonic-gate 	prev = conskbd_msg_queue;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	for (p = prev; p != msg; p = p->kpm_next)
23230Sstevel@tonic-gate 		prev = p;
23240Sstevel@tonic-gate 	ASSERT(p && p == msg);
23250Sstevel@tonic-gate 	if (prev == p) {
23260Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
23270Sstevel@tonic-gate 	} else {
23280Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 	p->kpm_next = NULL;
23310Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate #ifdef DEBUG
23360Sstevel@tonic-gate /*ARGSUSED*/
23370Sstevel@tonic-gate void
23380Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
23390Sstevel@tonic-gate {
23400Sstevel@tonic-gate 	char buf[256];
23410Sstevel@tonic-gate 	va_list ap;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	va_start(ap, fmt);
23440Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
23450Sstevel@tonic-gate 	va_end(ap);
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate }	/* conskbd_dprintf() */
23500Sstevel@tonic-gate #endif
2351