xref: /titanic_44/usr/src/uts/common/io/conskbd.c (revision ace75d0b5c29ec7e1580313f73070f4c2cc52f01)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Console kbd multiplexor driver for Sun.
317c478bd9Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "kbd" module pushed
327c478bd9Sstevel@tonic-gate  * on top of it.
337c478bd9Sstevel@tonic-gate  * Minor device 0 is what programs normally use.
347c478bd9Sstevel@tonic-gate  * Minor device 1 is used to feed predigested keystrokes to the "workstation
357c478bd9Sstevel@tonic-gate  * console" driver, which it is linked beneath.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *     This module can support multiple keyboards to be used simultaneously.
397c478bd9Sstevel@tonic-gate  * and enable users to use at a time multiple keyboards connected to the
407c478bd9Sstevel@tonic-gate  * same system. All the keyboards are linked under conskbd, and act as a
417c478bd9Sstevel@tonic-gate  * keyboard with replicated keys.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
447c478bd9Sstevel@tonic-gate  * a two-level architecure. The lower one is one of serialport drivers, such
457c478bd9Sstevel@tonic-gate  * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
467c478bd9Sstevel@tonic-gate  * drivers don't support polled I/O interfaces, we couldn't group the keyboard
477c478bd9Sstevel@tonic-gate  * of this kind under conskbd. So we do as the follows:
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
507c478bd9Sstevel@tonic-gate  *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
517c478bd9Sstevel@tonic-gate  *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
527c478bd9Sstevel@tonic-gate  *     requesting to be linked under conskbd. If the lower driver does't
537c478bd9Sstevel@tonic-gate  *     recognize this ioctl, the virtual keyboard will be disabled so that
547c478bd9Sstevel@tonic-gate  *     only one keyboard instance could be linked under conskbd.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/types.h>
597c478bd9Sstevel@tonic-gate #include <sys/param.h>
607c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
617c478bd9Sstevel@tonic-gate #include <sys/stream.h>
627c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
637c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
647c478bd9Sstevel@tonic-gate #include <sys/conf.h>
657c478bd9Sstevel@tonic-gate #include <sys/stat.h>
667c478bd9Sstevel@tonic-gate #include <sys/errno.h>
677c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
687c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
697c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
707c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
717c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
727c478bd9Sstevel@tonic-gate #include <sys/note.h>
737c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
747c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
757c478bd9Sstevel@tonic-gate #include <sys/policy.h>
767c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
777c478bd9Sstevel@tonic-gate #include <sys/kbtrans.h>
787c478bd9Sstevel@tonic-gate #include <sys/promif.h>
797c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
807c478bd9Sstevel@tonic-gate #include <sys/conskbd.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate extern struct keyboard *kbtrans_usbkb_maptab_init(void);
837c478bd9Sstevel@tonic-gate extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
847c478bd9Sstevel@tonic-gate extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Module linkage routines for the kernel
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate static int conskbd_attach(dev_info_t *, ddi_attach_cmd_t);
907c478bd9Sstevel@tonic-gate static int conskbd_detach(dev_info_t *, ddi_detach_cmd_t);
917c478bd9Sstevel@tonic-gate static int conskbd_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * STREAMS queue processing procedures
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate static void	conskbduwsrv(queue_t *);
977c478bd9Sstevel@tonic-gate static void	conskbdlwserv(queue_t *);
987c478bd9Sstevel@tonic-gate static void	conskbdlrput(queue_t *, mblk_t *);
997c478bd9Sstevel@tonic-gate static void	conskbdioctl(queue_t *, mblk_t *);
1007c478bd9Sstevel@tonic-gate static int	conskbdclose(queue_t *, int, cred_t *);
1017c478bd9Sstevel@tonic-gate static int	conskbdopen(queue_t *, dev_t *, int, int, cred_t *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* STREAMS driver id and limit value struct */
1057c478bd9Sstevel@tonic-gate static struct module_info conskbdm_info = {
1067c478bd9Sstevel@tonic-gate 	0,		/* mi_idnum */
1077c478bd9Sstevel@tonic-gate 	"conskbd",	/* mi_idname */
1087c478bd9Sstevel@tonic-gate 	0,		/* mi_minpsz */
1097c478bd9Sstevel@tonic-gate 	1024,		/* mi_maxpsz */
1107c478bd9Sstevel@tonic-gate 	2048,		/* mi_hiwat */
1117c478bd9Sstevel@tonic-gate 	128		/* mi_lowat */
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * STREAMS queue processing procedure structures
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate /* upper read queue processing procedure structures */
1187c478bd9Sstevel@tonic-gate static struct qinit conskbdurinit = {
1197c478bd9Sstevel@tonic-gate 	NULL,			/* qi_putp */
1207c478bd9Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_srvp */
1217c478bd9Sstevel@tonic-gate 	conskbdopen,		/* qi_qopen */
1227c478bd9Sstevel@tonic-gate 	conskbdclose,		/* qi_qclose */
1237c478bd9Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_qadmin */
1247c478bd9Sstevel@tonic-gate 	&conskbdm_info,		/* qi_minfo */
1257c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* upper write queue processing procedures structuresi */
1297c478bd9Sstevel@tonic-gate static struct qinit conskbduwinit = {
1307c478bd9Sstevel@tonic-gate 	(int (*)())putq,		/* qi_putp */
1317c478bd9Sstevel@tonic-gate 	(int (*)())conskbduwsrv,	/* qi_srvp */
1327c478bd9Sstevel@tonic-gate 	conskbdopen,			/* qi_qopen */
1337c478bd9Sstevel@tonic-gate 	conskbdclose,			/* qi_qclose */
1347c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1357c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1367c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* lower read queue processing procedures structures */
1407c478bd9Sstevel@tonic-gate static struct qinit conskbdlrinit = {
1417c478bd9Sstevel@tonic-gate 	(int (*)())conskbdlrput,	/* qi_putp */
1427c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_srvp */
1437c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1447c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1457c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1467c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1477c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* lower write processing procedures structures */
1517c478bd9Sstevel@tonic-gate static struct qinit conskbdlwinit = {
1527c478bd9Sstevel@tonic-gate 	putq,				/* qi_putp */
1537c478bd9Sstevel@tonic-gate 	(int (*)())conskbdlwserv,	/* qi_srvp */
1547c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1557c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1567c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1577c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1587c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
1597c478bd9Sstevel@tonic-gate };
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /* STREAMS entity declaration structure */
1627c478bd9Sstevel@tonic-gate static struct streamtab conskbd_str_info = {
1637c478bd9Sstevel@tonic-gate 	&conskbdurinit,		/* st_rdinit */
1647c478bd9Sstevel@tonic-gate 	&conskbduwinit,		/* st_wrinit */
1657c478bd9Sstevel@tonic-gate 	&conskbdlrinit,		/* st_muxrinit */
1667c478bd9Sstevel@tonic-gate 	&conskbdlwinit,		/* st_muxwinit */
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /* Entry points structure */
1717c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_conskbd_ops = {
1727c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_open */
1737c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_close */
1747c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1757c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
1767c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
1777c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
1787c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
1797c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
1807c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
1817c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1827c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
1837c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1847c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1857c478bd9Sstevel@tonic-gate 	&conskbd_str_info,	/* cb_stream */
1867c478bd9Sstevel@tonic-gate 	D_MP | D_MTOUTPERIM	/* cb_flag */
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Device operations structure
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate static struct dev_ops conskbd_ops = {
1947c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1957c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
1967c478bd9Sstevel@tonic-gate 	conskbd_info,		/* devo_getinfo */
1977c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1987c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1997c478bd9Sstevel@tonic-gate 	conskbd_attach,		/* devo_attach */
2007c478bd9Sstevel@tonic-gate 	conskbd_detach,		/* devo_detach */
2017c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
2027c478bd9Sstevel@tonic-gate 	&(cb_conskbd_ops),	/* devo_cb_ops */
2037c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2047c478bd9Sstevel@tonic-gate 	NULL			/* devo_power */
2057c478bd9Sstevel@tonic-gate };
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2117c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module.  This one is a pseudo driver */
2127c478bd9Sstevel@tonic-gate 	"Console kbd Multiplexer driver 'conskbd' %I%",
2137c478bd9Sstevel@tonic-gate 	&conskbd_ops,	/* driver ops */
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * Module linkage structure
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2207c478bd9Sstevel@tonic-gate 	MODREV_1,	/* ml_rev */
2217c478bd9Sstevel@tonic-gate 	&modldrv,	/* ml_linkage */
2227c478bd9Sstevel@tonic-gate 	NULL		/* NULL terminates the list */
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Debug printing
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate #ifndef DPRINTF
2297c478bd9Sstevel@tonic-gate #ifdef DEBUG
2307c478bd9Sstevel@tonic-gate void	conskbd_dprintf(const char *fmt, ...);
2317c478bd9Sstevel@tonic-gate #define	DPRINTF(l, m, args) \
2327c478bd9Sstevel@tonic-gate 	(((l) >= conskbd_errlevel) && ((m) & conskbd_errmask) ?	\
2337c478bd9Sstevel@tonic-gate 		conskbd_dprintf args :				\
2347c478bd9Sstevel@tonic-gate 		(void) 0)
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * Severity levels for printing
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate #define	PRINT_L0	0	/* print every message */
2407c478bd9Sstevel@tonic-gate #define	PRINT_L1	1	/* debug */
2417c478bd9Sstevel@tonic-gate #define	PRINT_L2	2	/* quiet */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * Masks
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate #define	PRINT_MASK_ALL		0xFFFFFFFFU
2477c478bd9Sstevel@tonic-gate uint_t	conskbd_errmask = PRINT_MASK_ALL;
2487c478bd9Sstevel@tonic-gate uint_t	conskbd_errlevel = PRINT_L2;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate #else
2517c478bd9Sstevel@tonic-gate #define	DPRINTF(l, m, args)	/* NOTHING */
2527c478bd9Sstevel@tonic-gate #endif
2537c478bd9Sstevel@tonic-gate #endif
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Module global data are protected by the per-module inner perimeter
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
2597c478bd9Sstevel@tonic-gate static	queue_t	*conskbd_consqueue; /* console queue above us */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
2637c478bd9Sstevel@tonic-gate static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
2647c478bd9Sstevel@tonic-gate static struct keyboard *conskbd_keyindex;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
2687c478bd9Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
2697c478bd9Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
2707c478bd9Sstevel@tonic-gate  * will be easier to add new statistics later.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate static struct {
2737c478bd9Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last keystroke */
2747c478bd9Sstevel@tonic-gate } conskbd_kstat = {
2757c478bd9Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
2767c478bd9Sstevel@tonic-gate };
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Local routines prototypes
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate static int conskbd_kstat_update(kstat_t *, int);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static void conskbd_ioc_plink(queue_t *, mblk_t *);
2847c478bd9Sstevel@tonic-gate static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
2857c478bd9Sstevel@tonic-gate static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
2887c478bd9Sstevel@tonic-gate static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
2897c478bd9Sstevel@tonic-gate static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
2907c478bd9Sstevel@tonic-gate static void conskbd_link_lower_queue(conskbd_lower_queue_t *);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
2937c478bd9Sstevel@tonic-gate static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
2947c478bd9Sstevel@tonic-gate static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
2957c478bd9Sstevel@tonic-gate static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
2967c478bd9Sstevel@tonic-gate static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
2977c478bd9Sstevel@tonic-gate static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
2987c478bd9Sstevel@tonic-gate static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
2997c478bd9Sstevel@tonic-gate static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static void conskbd_polledio_enter(struct cons_polledio_arg *);
3027c478bd9Sstevel@tonic-gate static void conskbd_polledio_exit(struct cons_polledio_arg *);
3037c478bd9Sstevel@tonic-gate static int  conskbd_polledio_ischar(struct cons_polledio_arg *);
3047c478bd9Sstevel@tonic-gate static int  conskbd_polledio_getchar(struct cons_polledio_arg *);
3057c478bd9Sstevel@tonic-gate static void conskbd_polledio_setled(struct kbtrans_hardware *, int);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static void conskbd_streams_setled(struct kbtrans_hardware *, int);
3087c478bd9Sstevel@tonic-gate static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
3097c478bd9Sstevel@tonic-gate static boolean_t
3107c478bd9Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *,
3117c478bd9Sstevel@tonic-gate 		kbtrans_key_t *, enum keystate *);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * Callbacks needed by kbtrans
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate static struct kbtrans_callbacks conskbd_callbacks = {
3177c478bd9Sstevel@tonic-gate 	conskbd_streams_setled,
3187c478bd9Sstevel@tonic-gate 	conskbd_polledio_setled,
3197c478bd9Sstevel@tonic-gate 	conskbd_polled_keycheck,
3207c478bd9Sstevel@tonic-gate };
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Single private "global" lock for the few rare conditions
3247c478bd9Sstevel@tonic-gate  * we want single-threaded.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate static	kmutex_t	conskbd_lq_lock;
3277c478bd9Sstevel@tonic-gate static	kmutex_t	conskbd_msgq_lock;
3287c478bd9Sstevel@tonic-gate static	conskbd_pending_msg_t	*conskbd_msg_queue;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * The software state structure of virtual keyboard.
3327c478bd9Sstevel@tonic-gate  * Currently, only one virtual keyboard is support.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate static conskbd_state_t	conskbd = { 0 };
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * _init()
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  * Description:
3407c478bd9Sstevel@tonic-gate  *      Driver initialization, called when driver is first loaded.
3417c478bd9Sstevel@tonic-gate  *      This is how access is initially given to all the static structures.
3427c478bd9Sstevel@tonic-gate  *
3437c478bd9Sstevel@tonic-gate  * Arguments:
3447c478bd9Sstevel@tonic-gate  *      None
3457c478bd9Sstevel@tonic-gate  *
3467c478bd9Sstevel@tonic-gate  * Returns:
3477c478bd9Sstevel@tonic-gate  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
3487c478bd9Sstevel@tonic-gate  *      mod_install() status, see mod_install(9f)
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate int
3517c478bd9Sstevel@tonic-gate _init(void)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	int	error;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3567c478bd9Sstevel@tonic-gate 	if (error != 0) {
3577c478bd9Sstevel@tonic-gate 		return (error);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	conskbd_keyindex = kbtrans_usbkb_maptab_init();
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	mutex_init(&conskbd_lq_lock, NULL, MUTEX_DRIVER, NULL);
3637c478bd9Sstevel@tonic-gate 	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	return (error);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate }	/* _init() */
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * _fini()
3717c478bd9Sstevel@tonic-gate  *
3727c478bd9Sstevel@tonic-gate  * Description:
3737c478bd9Sstevel@tonic-gate  *      Module de-initialization, called when the driver is to be unloaded.
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  * Arguments:
3767c478bd9Sstevel@tonic-gate  *      None
3777c478bd9Sstevel@tonic-gate  *
3787c478bd9Sstevel@tonic-gate  * Returns:
3797c478bd9Sstevel@tonic-gate  *      mod_remove() status, see mod_remove(9f)
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate int
3827c478bd9Sstevel@tonic-gate _fini(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	int	error;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3877c478bd9Sstevel@tonic-gate 	if (error != 0)
3887c478bd9Sstevel@tonic-gate 		return (error);
3897c478bd9Sstevel@tonic-gate 	mutex_destroy(&conskbd_lq_lock);
3907c478bd9Sstevel@tonic-gate 	mutex_destroy(&conskbd_msgq_lock);
3917c478bd9Sstevel@tonic-gate 	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	return (0);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate }	/* _fini() */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * _info()
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  * Description:
4017c478bd9Sstevel@tonic-gate  *      Module information, returns information about the driver.
4027c478bd9Sstevel@tonic-gate  *
4037c478bd9Sstevel@tonic-gate  * Arguments:
4047c478bd9Sstevel@tonic-gate  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  * Returns:
4077c478bd9Sstevel@tonic-gate  *      mod_info() status, see mod_info(9f)
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate int
4107c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate }	/* _info() */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * conskbd_attach()
4197c478bd9Sstevel@tonic-gate  *
4207c478bd9Sstevel@tonic-gate  * Description:
4217c478bd9Sstevel@tonic-gate  * 	This routine creates two device nodes. One is the "kbd" node, which
4227c478bd9Sstevel@tonic-gate  * is used by user application programs(such as Xserver).The other is the
4237c478bd9Sstevel@tonic-gate  * "conskbd" node, which is an internal node. consconfig_dacf module will
4247c478bd9Sstevel@tonic-gate  * open this internal node, and link the conskbd under the wc (workstaion
4257c478bd9Sstevel@tonic-gate  * console).
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * Arguments:
4287c478bd9Sstevel@tonic-gate  *      dev_info_t      *dip    Pointer to the device's dev_info struct
4297c478bd9Sstevel@tonic-gate  *      ddi_attach_cmd_t cmd    Attach command
4307c478bd9Sstevel@tonic-gate  *
4317c478bd9Sstevel@tonic-gate  * Returns:
4327c478bd9Sstevel@tonic-gate  *      DDI_SUCCESS             The driver was initialized properly
4337c478bd9Sstevel@tonic-gate  *      DDI_FAILURE             The driver couldn't be initialized properly
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4367c478bd9Sstevel@tonic-gate static int
4377c478bd9Sstevel@tonic-gate conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	switch (cmd) {
4427c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	default:
4467c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
4507c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) ||
4517c478bd9Sstevel@tonic-gate 	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
4527c478bd9Sstevel@tonic-gate 	    1) == DDI_FAILURE)) {
4537c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	conskbd_dip = devi;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
4597c478bd9Sstevel@tonic-gate 	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
4607c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
4617c478bd9Sstevel@tonic-gate 	if (ksp) {
4627c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &conskbd_kstat;
4637c478bd9Sstevel@tonic-gate 		ksp->ks_update = conskbd_kstat_update;
4647c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
4657c478bd9Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	conskbd.conskbd_layout = -1;	/* invalid layout */
4697c478bd9Sstevel@tonic-gate 	conskbd.conskbd_led_state = -1;
4707c478bd9Sstevel@tonic-gate 	conskbd.conskbd_bypassed = B_FALSE;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate }	/* conskbd_attach() */
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  * conskbd_detach()
4787c478bd9Sstevel@tonic-gate  *
4797c478bd9Sstevel@tonic-gate  * Description:
4807c478bd9Sstevel@tonic-gate  *      Detach an instance of the conskbd driver. In fact, the driver can not
4817c478bd9Sstevel@tonic-gate  * be detached.
4827c478bd9Sstevel@tonic-gate  *
4837c478bd9Sstevel@tonic-gate  * Arguments:
4847c478bd9Sstevel@tonic-gate  *      dev_info_t              *dip    Pointer to the device's dev_info struct
4857c478bd9Sstevel@tonic-gate  *      ddi_detach_cmd_t        cmd     Detach command
4867c478bd9Sstevel@tonic-gate  *
4877c478bd9Sstevel@tonic-gate  * Returns:
4887c478bd9Sstevel@tonic-gate  *      DDI_SUCCESS     The driver was detached
4897c478bd9Sstevel@tonic-gate  *      DDI_FAILURE     The driver couldn't be detached
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4927c478bd9Sstevel@tonic-gate static int
4937c478bd9Sstevel@tonic-gate conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate }	/* conskbd_detach() */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /* ARGSUSED */
5007c478bd9Sstevel@tonic-gate static int
5017c478bd9Sstevel@tonic-gate conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
5027c478bd9Sstevel@tonic-gate 	void **result)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	register int error;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	switch (infocmd) {
5077c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5087c478bd9Sstevel@tonic-gate 		if (conskbd_dip == NULL) {
5097c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
5107c478bd9Sstevel@tonic-gate 		} else {
5117c478bd9Sstevel@tonic-gate 			*result = (void *) conskbd_dip;
5127c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		break;
5157c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5167c478bd9Sstevel@tonic-gate 		*result = (void *)0;
5177c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 	default:
5207c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	return (error);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate }	/* conskbd_info() */
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5277c478bd9Sstevel@tonic-gate static int
5287c478bd9Sstevel@tonic-gate conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	dev_t	unit;
5317c478bd9Sstevel@tonic-gate 	int	err;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	unit = getminor(*devp);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (unit == 0) {
5367c478bd9Sstevel@tonic-gate 		/*
5377c478bd9Sstevel@tonic-gate 		 * Opening "/dev/kbd".
5387c478bd9Sstevel@tonic-gate 		 */
5397c478bd9Sstevel@tonic-gate 		conskbd_regqueue = q;
5407c478bd9Sstevel@tonic-gate 		qprocson(q);
5417c478bd9Sstevel@tonic-gate 		return (0);
5427c478bd9Sstevel@tonic-gate 	} else if (unit != 1) {
5437c478bd9Sstevel@tonic-gate 		/* we don't do that under Bozo's Big Tent */
5447c478bd9Sstevel@tonic-gate 		return (ENODEV);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * Opening the device to be linked under the console.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	conskbd_consqueue = q;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
553*ace75d0bSlq150181 	 * initialize kbtrans module for conskbd
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	err = kbtrans_streams_init(q, sflag, crp, (struct kbtrans_hardware *)
5567c478bd9Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
5577c478bd9Sstevel@tonic-gate 	if (err != 0)
5587c478bd9Sstevel@tonic-gate 		return (err);
5597c478bd9Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
5607c478bd9Sstevel@tonic-gate 	    conskbd_keyindex);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
5637c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
5647c478bd9Sstevel@tonic-gate 	    (struct cons_polledio_arg *)&conskbd;
5657c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
5667c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
5677c478bd9Sstevel@tonic-gate 	    (int (*)(struct cons_polledio_arg *)) conskbd_polledio_getchar;
5687c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
5697c478bd9Sstevel@tonic-gate 	    (boolean_t (*)(struct cons_polledio_arg *))conskbd_polledio_ischar;
5707c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
5717c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
5727c478bd9Sstevel@tonic-gate 	qprocson(q);
5737c478bd9Sstevel@tonic-gate 	kbtrans_streams_enable(conskbd.conskbd_kbtrans);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	return (0);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate }	/* conskbd_open() */
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5817c478bd9Sstevel@tonic-gate static int
5827c478bd9Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
5877c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
5907c478bd9Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
5917c478bd9Sstevel@tonic-gate 		    conskbd_consqueue);
5927c478bd9Sstevel@tonic-gate 		qprocsoff(q);
5937c478bd9Sstevel@tonic-gate 		conskbd_regqueue = NULL;
5947c478bd9Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
5957c478bd9Sstevel@tonic-gate 		/*
5967c478bd9Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
5977c478bd9Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
5987c478bd9Sstevel@tonic-gate 		 */
5997c478bd9Sstevel@tonic-gate 		qprocsoff(q);
6007c478bd9Sstevel@tonic-gate 		conskbd_consqueue = NULL;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	return (0);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate }	/* conskbd_close() */
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Service procedure for upper write queue.
6097c478bd9Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
6107c478bd9Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
6117c478bd9Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
6127c478bd9Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
6137c478bd9Sstevel@tonic-gate  * deal with all messages.
6147c478bd9Sstevel@tonic-gate  * 	This routine is invoked when ioctl commands are send down
6157c478bd9Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
6167c478bd9Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
6177c478bd9Sstevel@tonic-gate  * the led states.
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate static void
6207c478bd9Sstevel@tonic-gate conskbduwsrv(queue_t *q)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
6237c478bd9Sstevel@tonic-gate 	queue_t	*oldq;
6247c478bd9Sstevel@tonic-gate 	enum kbtrans_message_response ret;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * if the virtual keyboard is supported
6307c478bd9Sstevel@tonic-gate 		 */
6317c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
6347c478bd9Sstevel@tonic-gate 				continue;
6357c478bd9Sstevel@tonic-gate 			/*
6367c478bd9Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
6377c478bd9Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
6387c478bd9Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
6397c478bd9Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
6407c478bd9Sstevel@tonic-gate 			 * and regular queue.
6417c478bd9Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
6427c478bd9Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
6437c478bd9Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
6447c478bd9Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
6457c478bd9Sstevel@tonic-gate 			 * to be switched between regular queue and console
6467c478bd9Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
6477c478bd9Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
6487c478bd9Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
6497c478bd9Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
6507c478bd9Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
6517c478bd9Sstevel@tonic-gate 			 * need to return response to right queue.
6527c478bd9Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
6537c478bd9Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
6547c478bd9Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
6557c478bd9Sstevel@tonic-gate 			 * and then switch back to old queue.
6567c478bd9Sstevel@tonic-gate 			 */
6577c478bd9Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
6587c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
6597c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
6607c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
6617c478bd9Sstevel@tonic-gate 			ret = kbtrans_streams_message(
6627c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
6637c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
6647c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 			switch (ret) {
6677c478bd9Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
6687c478bd9Sstevel@tonic-gate 					continue;
6697c478bd9Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
6707c478bd9Sstevel@tonic-gate 					break;
6717c478bd9Sstevel@tonic-gate 			}
6727c478bd9Sstevel@tonic-gate 		}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		case M_IOCTL:
6777c478bd9Sstevel@tonic-gate 			conskbdioctl(q, mp);
6787c478bd9Sstevel@tonic-gate 			break;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		case M_FLUSH:
6817c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
6827c478bd9Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 			/*
6857c478bd9Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
6867c478bd9Sstevel@tonic-gate 			 * may be lost so that upper module or applications
6877c478bd9Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
6887c478bd9Sstevel@tonic-gate 			 * ever.
6897c478bd9Sstevel@tonic-gate 			 */
6907c478bd9Sstevel@tonic-gate 			freemsg(mp);
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		case M_DATA:
6947c478bd9Sstevel@tonic-gate 			/*
6957c478bd9Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
6967c478bd9Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
6977c478bd9Sstevel@tonic-gate 			 * down to lower queue.
6987c478bd9Sstevel@tonic-gate 			 */
6997c478bd9Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
7007c478bd9Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
7017c478bd9Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
7027c478bd9Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
7037c478bd9Sstevel@tonic-gate 					freemsg(mp);
7047c478bd9Sstevel@tonic-gate 			} else {
7057c478bd9Sstevel@tonic-gate 				freemsg(mp);
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 			break;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		default:
7107c478bd9Sstevel@tonic-gate 			/*
7117c478bd9Sstevel@tonic-gate 			 * Pass an error message up.
7127c478bd9Sstevel@tonic-gate 			 */
7137c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
7147c478bd9Sstevel@tonic-gate 			if (mp->b_cont) {
7157c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
7167c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
7197c478bd9Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
7207c478bd9Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
7217c478bd9Sstevel@tonic-gate 			qreply(q, mp);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 	}	/* end of while */
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate }	/* conskbduwsrv() */
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate static void
7287c478bd9Sstevel@tonic-gate conskbdioctl(queue_t *q, mblk_t *mp)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t		*prev;
7317c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t		*lqs;
7327c478bd9Sstevel@tonic-gate 	struct	iocblk			*iocp;
7337c478bd9Sstevel@tonic-gate 	struct	linkblk			*linkp;
7347c478bd9Sstevel@tonic-gate 	int	index;
7357c478bd9Sstevel@tonic-gate 	int	error = 0;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	case I_LINK:
7427c478bd9Sstevel@tonic-gate 	case I_PLINK:
743b7e54746Scg149915 		if (conskbd.conskbd_bypassed == B_TRUE) {
744b7e54746Scg149915 		/*
745b7e54746Scg149915 		 * A legacy keyboard can NOT be connected to conskbd together
746b7e54746Scg149915 		 * with other keyboards. So when a legacy keyboard is already
747b7e54746Scg149915 		 * linked under conkbd, we just reject all others.
748b7e54746Scg149915 		 */
749b7e54746Scg149915 			miocnak(q, mp, 0, EAGAIN);
750b7e54746Scg149915 			break;
751b7e54746Scg149915 		}
752b7e54746Scg149915 
7537c478bd9Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
7547c478bd9Sstevel@tonic-gate 		conskbd_ioc_plink(q, mp);
7557c478bd9Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
7567c478bd9Sstevel@tonic-gate 		break;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	case I_UNLINK:
7597c478bd9Sstevel@tonic-gate 	case I_PUNLINK:
7607c478bd9Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
7617c478bd9Sstevel@tonic-gate 		linkp = (struct linkblk *)mp->b_cont->b_rptr;
7627c478bd9Sstevel@tonic-gate 		prev = conskbd.conskbd_lqueue_list;
7637c478bd9Sstevel@tonic-gate 		for (lqs = prev; lqs; lqs = lqs->lqs_next) {
7647c478bd9Sstevel@tonic-gate 			if (lqs->lqs_queue == linkp->l_qbot) {
7657c478bd9Sstevel@tonic-gate 				if (prev == lqs)
7667c478bd9Sstevel@tonic-gate 					conskbd.conskbd_lqueue_list =
7677c478bd9Sstevel@tonic-gate 					    lqs->lqs_next;
7687c478bd9Sstevel@tonic-gate 				else
7697c478bd9Sstevel@tonic-gate 					prev->lqs_next = lqs->lqs_next;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 				lqs->lqs_queue->q_ptr =  NULL;
7727c478bd9Sstevel@tonic-gate 				conskbd.conskbd_lqueue_nums --;
7737c478bd9Sstevel@tonic-gate 				if (conskbd.conskbd_lqueue_nums == 0)
7747c478bd9Sstevel@tonic-gate 					conskbd.conskbd_layout = -1;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 				mutex_exit(&conskbd_lq_lock);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 				for (index = 0; index < KBTRANS_KEYNUMS_MAX;
7797c478bd9Sstevel@tonic-gate 				    index ++) {
7807c478bd9Sstevel@tonic-gate 					if (lqs->lqs_key_state[index] ==
7817c478bd9Sstevel@tonic-gate 					    KEY_PRESSED)
7827c478bd9Sstevel@tonic-gate 						kbtrans_streams_key(
7837c478bd9Sstevel@tonic-gate 						    conskbd.conskbd_kbtrans,
7847c478bd9Sstevel@tonic-gate 						    index,
7857c478bd9Sstevel@tonic-gate 						    KEY_RELEASED);
7867c478bd9Sstevel@tonic-gate 				}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 				kmem_free(lqs, sizeof (*lqs));
7897c478bd9Sstevel@tonic-gate 				miocack(q, mp, 0, 0);
7907c478bd9Sstevel@tonic-gate 				return;
7917c478bd9Sstevel@tonic-gate 			}
7927c478bd9Sstevel@tonic-gate 			prev = lqs;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
7957c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
7967c478bd9Sstevel@tonic-gate 		break;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	case KIOCSKABORTEN:
7997c478bd9Sstevel@tonic-gate 		/*
8007c478bd9Sstevel@tonic-gate 		 * Check if privileged
8017c478bd9Sstevel@tonic-gate 		 */
8027c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
8037c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8047c478bd9Sstevel@tonic-gate 			return;
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8087c478bd9Sstevel@tonic-gate 		if (error != 0) {
8097c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8107c478bd9Sstevel@tonic-gate 			return;
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
8147c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8157c478bd9Sstevel@tonic-gate 		break;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	default:
8187c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
8197c478bd9Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
8207c478bd9Sstevel@tonic-gate 		} else {
8217c478bd9Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate }	/* conskbdioctl() */
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate static void
8297c478bd9Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
8327c478bd9Sstevel@tonic-gate 	mblk_t			*datap;
8337c478bd9Sstevel@tonic-gate 	int			cmd;
8347c478bd9Sstevel@tonic-gate 	int			error = 0;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8397c478bd9Sstevel@tonic-gate 	case KIOCLAYOUT:
8407c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
8417c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
8427c478bd9Sstevel@tonic-gate 			break;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
8467c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
8477c478bd9Sstevel@tonic-gate 		else
8487c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
8517c478bd9Sstevel@tonic-gate 		if (mp->b_cont)
8527c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
8537c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
8547c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
8557c478bd9Sstevel@tonic-gate 		break;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	case KIOCSLAYOUT:
8587c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
8597c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
8637c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8647c478bd9Sstevel@tonic-gate 		break;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
8677c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
8687c478bd9Sstevel@tonic-gate 		if (error != 0) {
8697c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8707c478bd9Sstevel@tonic-gate 			break;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
8737c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
8777c478bd9Sstevel@tonic-gate 		break;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
8807c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
8817c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
8857c478bd9Sstevel@tonic-gate 		break;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
8887c478bd9Sstevel@tonic-gate 		/*
8897c478bd9Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
8907c478bd9Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
8917c478bd9Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
8927c478bd9Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
8937c478bd9Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
8947c478bd9Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
8957c478bd9Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
8967c478bd9Sstevel@tonic-gate 		 *
8977c478bd9Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
8987c478bd9Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
8997c478bd9Sstevel@tonic-gate 		 * already handle the message though it returned to us a
9007c478bd9Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
9017c478bd9Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
9027c478bd9Sstevel@tonic-gate 		 * So we just return ACK to upper module.
9037c478bd9Sstevel@tonic-gate 		 */
9047c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9057c478bd9Sstevel@tonic-gate 		break;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	case KIOCCMD:
9087c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
9097c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL) {
9107c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9117c478bd9Sstevel@tonic-gate 			break;
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
9147c478bd9Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
9157c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9167c478bd9Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
9177c478bd9Sstevel@tonic-gate 			if (datap == NULL) {
9187c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9197c478bd9Sstevel@tonic-gate 				return;
9207c478bd9Sstevel@tonic-gate 			}
9217c478bd9Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
9227c478bd9Sstevel@tonic-gate 				*(int *)datap->b_wptr =
9237c478bd9Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
9247c478bd9Sstevel@tonic-gate 			else
9257c478bd9Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 			mp->b_cont = datap;
9287c478bd9Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
9297c478bd9Sstevel@tonic-gate 			return;
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9337c478bd9Sstevel@tonic-gate 		break;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	default:
9367c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
9377c478bd9Sstevel@tonic-gate 		break;
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate static void
9437c478bd9Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
9467c478bd9Sstevel@tonic-gate 	struct	iocblk		*iocp;
9477c478bd9Sstevel@tonic-gate 	int	error = 0;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
9527c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT: {
9557c478bd9Sstevel@tonic-gate 		mblk_t *datap;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
9587c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
9597c478bd9Sstevel@tonic-gate 			break;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
9637c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9647c478bd9Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
9657c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9667c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9697c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
9707c478bd9Sstevel@tonic-gate 		break;
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
9747c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
9757c478bd9Sstevel@tonic-gate 		if (error != 0) {
9767c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9777c478bd9Sstevel@tonic-gate 			break;
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		/*
9827c478bd9Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
9837c478bd9Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
9847c478bd9Sstevel@tonic-gate 		 * itself.
9857c478bd9Sstevel@tonic-gate 		 */
9867c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
9877c478bd9Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
9887c478bd9Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
9897c478bd9Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
9907c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
9917c478bd9Sstevel@tonic-gate 				return;
9927c478bd9Sstevel@tonic-gate 			}
9937c478bd9Sstevel@tonic-gate 			break;
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9977c478bd9Sstevel@tonic-gate 		break;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	default:
10007c478bd9Sstevel@tonic-gate 		/*
10017c478bd9Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
10027c478bd9Sstevel@tonic-gate 		 * through to; otherwise, reject it.
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10057c478bd9Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10067c478bd9Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10077c478bd9Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10087c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10097c478bd9Sstevel@tonic-gate 				return;
10107c478bd9Sstevel@tonic-gate 			}
10117c478bd9Sstevel@tonic-gate 			break;
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 		/* nobody below us; reject it */
10157c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10167c478bd9Sstevel@tonic-gate 		break;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate /*
10237c478bd9Sstevel@tonic-gate  * Service procedure for lower write queue.
10247c478bd9Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate static void
10277c478bd9Sstevel@tonic-gate conskbdlwserv(queue_t *q)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	register mblk_t *mp;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
10327c478bd9Sstevel@tonic-gate 		putnext(q, mp);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate }	/* conskbdlwserv() */
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate /*
10377c478bd9Sstevel@tonic-gate  * Put procedure for lower read queue.
10387c478bd9Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
10397c478bd9Sstevel@tonic-gate  * device 1.
10407c478bd9Sstevel@tonic-gate  */
10417c478bd9Sstevel@tonic-gate static void
10427c478bd9Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
10457c478bd9Sstevel@tonic-gate 	struct iocblk 	*iocp;
10467c478bd9Sstevel@tonic-gate 	Firm_event	*fe;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	case M_FLUSH:
10537c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
10547c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
10557c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
10567c478bd9Sstevel@tonic-gate 		}
10577c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
10587c478bd9Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
10597c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
10607c478bd9Sstevel@tonic-gate 		} else
10617c478bd9Sstevel@tonic-gate 			freemsg(mp);
10627c478bd9Sstevel@tonic-gate 		break;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	case M_DATA:
10657c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 			/*
10707c478bd9Sstevel@tonic-gate 			 * This is a workaround.
10717c478bd9Sstevel@tonic-gate 			 *
10727c478bd9Sstevel@tonic-gate 			 * According to HID specification, there are the
10737c478bd9Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
10747c478bd9Sstevel@tonic-gate 			 *
10757c478bd9Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
10767c478bd9Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
10777c478bd9Sstevel@tonic-gate 			 *
10787c478bd9Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
10797c478bd9Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
10807c478bd9Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
10817c478bd9Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
10827c478bd9Sstevel@tonic-gate 			 * not know which is being pressed or released when we
10837c478bd9Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
10847c478bd9Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
10857c478bd9Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
10867c478bd9Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
10877c478bd9Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
10887c478bd9Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
10897c478bd9Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
10907c478bd9Sstevel@tonic-gate 			 * the key to 49.
10917c478bd9Sstevel@tonic-gate 			 */
10927c478bd9Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
10937c478bd9Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
10947c478bd9Sstevel@tonic-gate 					fe->id = 49;
10957c478bd9Sstevel@tonic-gate 				else
10967c478bd9Sstevel@tonic-gate 					fe->id = 50;
10977c478bd9Sstevel@tonic-gate 			}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 			/*
11007c478bd9Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
11017c478bd9Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
11027c478bd9Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
11037c478bd9Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
11047c478bd9Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
11057c478bd9Sstevel@tonic-gate 			 * as held-down for ever.
11067c478bd9Sstevel@tonic-gate 			 */
11077c478bd9Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
11087c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
11097c478bd9Sstevel@tonic-gate 			if (fe->value)
11107c478bd9Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
11117c478bd9Sstevel@tonic-gate 			else
11127c478bd9Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
11157c478bd9Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
11167c478bd9Sstevel@tonic-gate 			freemsg(mp);
11177c478bd9Sstevel@tonic-gate 		} else {
11187c478bd9Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
11197c478bd9Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
11207c478bd9Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
11217c478bd9Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
11227c478bd9Sstevel@tonic-gate 			else
11237c478bd9Sstevel@tonic-gate 				freemsg(mp);
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
11267c478bd9Sstevel@tonic-gate 		break;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	case M_IOCACK:
11297c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
11307c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
11317c478bd9Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
11347c478bd9Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
11377c478bd9Sstevel@tonic-gate 		break;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	case M_ERROR:
11407c478bd9Sstevel@tonic-gate 	case M_HANGUP:
11417c478bd9Sstevel@tonic-gate 	default:
11427c478bd9Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
11437c478bd9Sstevel@tonic-gate 		break;
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate }	/* conskbdlrput() */
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /* ARGSUSED */
11507c478bd9Sstevel@tonic-gate static int
11517c478bd9Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
11547c478bd9Sstevel@tonic-gate 		return (EACCES);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	return (0);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate }	/* conskbd_kstat_update() */
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate /*
11637c478bd9Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
11647c478bd9Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
11657c478bd9Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
11667c478bd9Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
11677c478bd9Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
11687c478bd9Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
11697c478bd9Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
11707c478bd9Sstevel@tonic-gate  * request matching received responses by searching the global linked
11717c478bd9Sstevel@tonic-gate  * list to find the request which has the same message ID of the
11727c478bd9Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
11737c478bd9Sstevel@tonic-gate  * give a response to upstreams based the following policy:
11747c478bd9Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
11757c478bd9Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
11767c478bd9Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
11777c478bd9Sstevel@tonic-gate  * the first response is sent to upstream.
11787c478bd9Sstevel@tonic-gate  */
11797c478bd9Sstevel@tonic-gate static void
11807c478bd9Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
11837c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
11847c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
11857c478bd9Sstevel@tonic-gate 	mblk_t		*clonemp;
11867c478bd9Sstevel@tonic-gate 	int		retry;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
11897c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
11907c478bd9Sstevel@tonic-gate 		return;
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
11947c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
11957c478bd9Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
11967c478bd9Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
11977c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
12007c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
12017c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == KIOCCMD);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
12047c478bd9Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
12057c478bd9Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
12067c478bd9Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
12077c478bd9Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
12087c478bd9Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		/*
12137c478bd9Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
12147c478bd9Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
12157c478bd9Sstevel@tonic-gate 		 * otherwise, system will panic.
12167c478bd9Sstevel@tonic-gate 		 */
12177c478bd9Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
12187c478bd9Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
12197c478bd9Sstevel@tonic-gate 			lqs = lqs->lqs_next;
12207c478bd9Sstevel@tonic-gate 			msg->kpm_req_nums --;
12217c478bd9Sstevel@tonic-gate 			retry = 0;
12227c478bd9Sstevel@tonic-gate 			continue;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		clonemp = copymsg(mp);
12267c478bd9Sstevel@tonic-gate 		if (clonemp != NULL) {
12277c478bd9Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
12287c478bd9Sstevel@tonic-gate 				lqs = lqs->lqs_next;
12297c478bd9Sstevel@tonic-gate 				retry = 0;
12307c478bd9Sstevel@tonic-gate 				continue;
12317c478bd9Sstevel@tonic-gate 			}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			/*
12347c478bd9Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
12357c478bd9Sstevel@tonic-gate 			 */
12367c478bd9Sstevel@tonic-gate 			freemsg(clonemp);
12377c478bd9Sstevel@tonic-gate 		}
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 		/*
12407c478bd9Sstevel@tonic-gate 		 * During testing it was observed that occasionally
12417c478bd9Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
12427c478bd9Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
12437c478bd9Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
12447c478bd9Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
12457c478bd9Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
12467c478bd9Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
12477c478bd9Sstevel@tonic-gate 		 *
12487c478bd9Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
12497c478bd9Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
12507c478bd9Sstevel@tonic-gate 		 * or behave weirdly,
12517c478bd9Sstevel@tonic-gate 		 *
12527c478bd9Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
12537c478bd9Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
12547c478bd9Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
12557c478bd9Sstevel@tonic-gate 		 *	entering kmdb;
12567c478bd9Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
12577c478bd9Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
12587c478bd9Sstevel@tonic-gate 		 *	is not available in normal mode once returning
12597c478bd9Sstevel@tonic-gate 		 *	from kmdb;
12607c478bd9Sstevel@tonic-gate 		 * 3) for KIOCCMD
12617c478bd9Sstevel@tonic-gate 		 * 	3.1) for KBD_CMD_NOBELL
12627c478bd9Sstevel@tonic-gate 		 * 		there's no beep in USB and PS2 keyboard,
12637c478bd9Sstevel@tonic-gate 		 * 		this ioctl actually disables the beep on
12647c478bd9Sstevel@tonic-gate 		 * 		system mainboard. Note that all the cloned
12657c478bd9Sstevel@tonic-gate 		 * 		messages sent down to lower queues do the
12667c478bd9Sstevel@tonic-gate 		 * 		same job for system mainboard. Therefore,
12677c478bd9Sstevel@tonic-gate 		 * 		even if we fail to send this ioctl to most
12687c478bd9Sstevel@tonic-gate 		 * 		of lower queues, the beep still would be
12697c478bd9Sstevel@tonic-gate 		 * 		disabled. So, no trouble exists here.
12707c478bd9Sstevel@tonic-gate 		 *	3.2) for others
12717c478bd9Sstevel@tonic-gate 		 *		nothing;
12727c478bd9Sstevel@tonic-gate 		 *
12737c478bd9Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
12747c478bd9Sstevel@tonic-gate 		 * same request comes again.
12757c478bd9Sstevel@tonic-gate 		 */
12767c478bd9Sstevel@tonic-gate 		if (retry ++ >= 5) {
12777c478bd9Sstevel@tonic-gate 			dev_t	devt;
12787c478bd9Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
12817c478bd9Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
12827c478bd9Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
12837c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
12847c478bd9Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
12857c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
12867c478bd9Sstevel@tonic-gate 					    "keyboard is not available"
12877c478bd9Sstevel@tonic-gate 					    " for system debugging: %s",
12887c478bd9Sstevel@tonic-gate 					    path);
12897c478bd9Sstevel@tonic-gate 				break;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
12927c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
12937c478bd9Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
12947c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
12957c478bd9Sstevel@tonic-gate 					    "keyboard is not available:"
12967c478bd9Sstevel@tonic-gate 					    " %s", path);
12977c478bd9Sstevel@tonic-gate 				break;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 			default:
13007c478bd9Sstevel@tonic-gate 				break;
13017c478bd9Sstevel@tonic-gate 			}
13027c478bd9Sstevel@tonic-gate 			msg->kpm_req_nums --;
13037c478bd9Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13047c478bd9Sstevel@tonic-gate 			retry = 0;
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
13097c478bd9Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
13107c478bd9Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
13117c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate static void
13187c478bd9Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate 	mblk_t		*req;
13217c478bd9Sstevel@tonic-gate 	queue_t		*lowque;
13227c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
13237c478bd9Sstevel@tonic-gate 	struct linkblk		*linkp;
13247c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&conskbd_lq_lock));
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
13297c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
13327c478bd9Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
13337c478bd9Sstevel@tonic-gate 	lowque = linkp->l_qbot;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	lowque->q_ptr = (void *)lqs;
13367c478bd9Sstevel@tonic-gate 	OTHERQ(lowque)->q_ptr = (void *)lqs;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
13397c478bd9Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
13407c478bd9Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
13437c478bd9Sstevel@tonic-gate 	if (req == NULL) {
13447c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13457c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
13467c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13477c478bd9Sstevel@tonic-gate 		return;
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
13517c478bd9Sstevel@tonic-gate 	if (req->b_cont == NULL) {
13527c478bd9Sstevel@tonic-gate 		freemsg(req);
13537c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
13547c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
13557c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13567c478bd9Sstevel@tonic-gate 		return;
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	iocp->ioc_count = 0;
13607c478bd9Sstevel@tonic-gate 	iocp->ioc_rval = 0;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
13637c478bd9Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
13687c478bd9Sstevel@tonic-gate 		freemsg(req);
13697c478bd9Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
13707c478bd9Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
13717c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
13727c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
13737c478bd9Sstevel@tonic-gate 	}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate /*
13797c478bd9Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
13807c478bd9Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
13817c478bd9Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
13827c478bd9Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
13837c478bd9Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
13847c478bd9Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
13857c478bd9Sstevel@tonic-gate  */
13867c478bd9Sstevel@tonic-gate static void
13877c478bd9Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	switch (lqs->lqs_state) {
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
13927c478bd9Sstevel@tonic-gate 	case LQS_INITIALIZED:
13937c478bd9Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
13947c478bd9Sstevel@tonic-gate 		break;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
13977c478bd9Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
13987c478bd9Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
13997c478bd9Sstevel@tonic-gate 		break;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	/* S4: wait lower queue to acknowledge KIOCSLED  message */
14027c478bd9Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
14037c478bd9Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
14047c478bd9Sstevel@tonic-gate 		break;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
14077c478bd9Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
14087c478bd9Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
14097c478bd9Sstevel@tonic-gate 		break;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
14127c478bd9Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
14137c478bd9Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
14147c478bd9Sstevel@tonic-gate 		break;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
14177c478bd9Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
14187c478bd9Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
14197c478bd9Sstevel@tonic-gate 		break;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	/* if reaching here, there must be a error */
14227c478bd9Sstevel@tonic-gate 	default:
14237c478bd9Sstevel@tonic-gate 		freemsg(mp);
14247c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
14257c478bd9Sstevel@tonic-gate 		break;
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate static void
14327c478bd9Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
14337c478bd9Sstevel@tonic-gate {
14347c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
14357c478bd9Sstevel@tonic-gate 	mblk_t		*msg;
14367c478bd9Sstevel@tonic-gate 	mblk_t		*req;
14377c478bd9Sstevel@tonic-gate 	queue_t		*lowerque;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
14407c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
14457c478bd9Sstevel@tonic-gate 	case M_IOCACK:
14467c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
14477c478bd9Sstevel@tonic-gate 		if (req == NULL) {
14487c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
14497c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
14507c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
14517c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
14527c478bd9Sstevel@tonic-gate 			freemsg(mp);
14537c478bd9Sstevel@tonic-gate 			return;
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
14577c478bd9Sstevel@tonic-gate 		if (req->b_cont == NULL) {
14587c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
14597c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
14607c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
14617c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
14627c478bd9Sstevel@tonic-gate 			freemsg(req);
14637c478bd9Sstevel@tonic-gate 			freemsg(mp);
14647c478bd9Sstevel@tonic-gate 			return;
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
14687c478bd9Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
14697c478bd9Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
14727c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
14757c478bd9Sstevel@tonic-gate 			freemsg(req);
14767c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue,
14777c478bd9Sstevel@tonic-gate 			    lqs->lqs_pending_plink, 0, ENOMEM);
14787c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
14797c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
14807c478bd9Sstevel@tonic-gate 		}
14817c478bd9Sstevel@tonic-gate 		break;
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
14847c478bd9Sstevel@tonic-gate 		/*
14857c478bd9Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
14867c478bd9Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
14877c478bd9Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
14887c478bd9Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
14897c478bd9Sstevel@tonic-gate 		 * will bypass the conskbd module.
14907c478bd9Sstevel@tonic-gate 		 */
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 		/*
14937c478bd9Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
14947c478bd9Sstevel@tonic-gate 		 * conskbd, we reject the current one.
14957c478bd9Sstevel@tonic-gate 		 */
14967c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
14977c478bd9Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
14987c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
14997c478bd9Sstevel@tonic-gate 			    0, iocp->ioc_error);
15007c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
15017c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15027c478bd9Sstevel@tonic-gate 			break;
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		/*
15067c478bd9Sstevel@tonic-gate 		 * Bypass the virutal keyboard for old hardware
15077c478bd9Sstevel@tonic-gate 		 */
15087c478bd9Sstevel@tonic-gate 		conskbd.conskbd_bypassed = B_TRUE;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 		msg = lqs->lqs_pending_plink;
15117c478bd9Sstevel@tonic-gate 		msg->b_datap->db_type = M_IOCACK;
15127c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)msg->b_rptr;
15137c478bd9Sstevel@tonic-gate 		iocp->ioc_error = 0;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		/*
15167c478bd9Sstevel@tonic-gate 		 * link this keyboard under conskbd
15177c478bd9Sstevel@tonic-gate 		 */
15187c478bd9Sstevel@tonic-gate 		mutex_enter(&conskbd_lq_lock);
15197c478bd9Sstevel@tonic-gate 		lqs->lqs_next = conskbd.conskbd_lqueue_list;
15207c478bd9Sstevel@tonic-gate 		conskbd.conskbd_lqueue_list = lqs;
15217c478bd9Sstevel@tonic-gate 		conskbd.conskbd_lqueue_nums++;
15227c478bd9Sstevel@tonic-gate 		mutex_exit(&conskbd_lq_lock);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_INITIALIZED_LEGACY;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 		qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
15277c478bd9Sstevel@tonic-gate 		break;
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	freemsg(mp);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate static void
15357c478bd9Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15367c478bd9Sstevel@tonic-gate {
15377c478bd9Sstevel@tonic-gate 	struct iocblk 	*iocp;
15387c478bd9Sstevel@tonic-gate 	mblk_t		*req;
15397c478bd9Sstevel@tonic-gate 	queue_t		*lowerque;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
15427c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15477c478bd9Sstevel@tonic-gate 	case M_IOCACK:
15487c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
15497c478bd9Sstevel@tonic-gate 		if (req == NULL) {
15507c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15517c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
15527c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
15537c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15547c478bd9Sstevel@tonic-gate 			freemsg(mp);
15557c478bd9Sstevel@tonic-gate 			return;
15567c478bd9Sstevel@tonic-gate 		}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
15597c478bd9Sstevel@tonic-gate 		if (req->b_cont == NULL) {
15607c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15617c478bd9Sstevel@tonic-gate 			    0, ENOMEM);
15627c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15637c478bd9Sstevel@tonic-gate 			freemsg(req);
15647c478bd9Sstevel@tonic-gate 			freemsg(mp);
15657c478bd9Sstevel@tonic-gate 			return;
15667c478bd9Sstevel@tonic-gate 		}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
15697c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
15707c478bd9Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
15717c478bd9Sstevel@tonic-gate 			freemsg(req);
15727c478bd9Sstevel@tonic-gate 			miocnak(lqs->lqs_pending_queue,
15737c478bd9Sstevel@tonic-gate 			    lqs->lqs_pending_plink, 0, ENOMEM);
15747c478bd9Sstevel@tonic-gate 			lowerque->q_ptr = NULL;
15757c478bd9Sstevel@tonic-gate 			kmem_free(lqs, sizeof (*lqs));
15767c478bd9Sstevel@tonic-gate 		}
15777c478bd9Sstevel@tonic-gate 		break;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
15807c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
15817c478bd9Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink,
15827c478bd9Sstevel@tonic-gate 		    0, iocp->ioc_error);
15837c478bd9Sstevel@tonic-gate 		lowerque->q_ptr = NULL;
15847c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
15857c478bd9Sstevel@tonic-gate 		break;
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	freemsg(mp);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate static void
15937c478bd9Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	mblk_t		*req;
15967c478bd9Sstevel@tonic-gate 	int		layout;
15977c478bd9Sstevel@tonic-gate 	boolean_t	fail;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16007c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
16037c478bd9Sstevel@tonic-gate 	case M_IOCACK:
16047c478bd9Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
16057c478bd9Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
16067c478bd9Sstevel@tonic-gate 			/*
16077c478bd9Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
16087c478bd9Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
16097c478bd9Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
16107c478bd9Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
16117c478bd9Sstevel@tonic-gate 			 */
16127c478bd9Sstevel@tonic-gate 			if (layout != -1 && conskbd.conskbd_layout == -1)
16137c478bd9Sstevel@tonic-gate 				conskbd.conskbd_layout = layout;
16147c478bd9Sstevel@tonic-gate 		}
16157c478bd9Sstevel@tonic-gate 		break;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
16197c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
16207c478bd9Sstevel@tonic-gate 		break;
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	freemsg(mp);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	fail = B_TRUE;
16267c478bd9Sstevel@tonic-gate 	req = mkiocb(KIOCSLED);
16277c478bd9Sstevel@tonic-gate 	if (req) {
16287c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
16297c478bd9Sstevel@tonic-gate 		if (req->b_cont) {
16307c478bd9Sstevel@tonic-gate 			*(uchar_t *)req->b_cont->b_wptr =
16317c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_led_state;
16327c478bd9Sstevel@tonic-gate 			req->b_cont->b_wptr += sizeof (uchar_t);
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
16357c478bd9Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
16367c478bd9Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
16377c478bd9Sstevel@tonic-gate 				fail = B_FALSE;
16387c478bd9Sstevel@tonic-gate 			} else {
16397c478bd9Sstevel@tonic-gate 				freemsg(req);
16407c478bd9Sstevel@tonic-gate 			}
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 		} else {
16437c478bd9Sstevel@tonic-gate 			freemsg(req);
16447c478bd9Sstevel@tonic-gate 		}
16457c478bd9Sstevel@tonic-gate 	}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if (fail) {
16487c478bd9Sstevel@tonic-gate 		/*
16497c478bd9Sstevel@tonic-gate 		 * If fail to allocate KIOCSLED message or put the message
16507c478bd9Sstevel@tonic-gate 		 * into lower queue, we immediately link current keyboard
16517c478bd9Sstevel@tonic-gate 		 * under conskbd. Thus, even if fails to set LED, this
16527c478bd9Sstevel@tonic-gate 		 * keyboard could be available.
16537c478bd9Sstevel@tonic-gate 		 */
16547c478bd9Sstevel@tonic-gate 		conskbd_link_lower_queue(lqs);
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate static void
16617c478bd9Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16627c478bd9Sstevel@tonic-gate {
16637c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16647c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	/*
16677c478bd9Sstevel@tonic-gate 	 * Basically, failure of setting LED is not a fatal error,
16687c478bd9Sstevel@tonic-gate 	 * so we will plumb the lower queue into conskbd whether
16697c478bd9Sstevel@tonic-gate 	 * setting LED succeeds or fails.
16707c478bd9Sstevel@tonic-gate 	 */
16717c478bd9Sstevel@tonic-gate 	freemsg(mp);
16727c478bd9Sstevel@tonic-gate 	conskbd_link_lower_queue(lqs);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate static void
16787c478bd9Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
16797c478bd9Sstevel@tonic-gate {
16807c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
16817c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
16827c478bd9Sstevel@tonic-gate 	int			error;
16837c478bd9Sstevel@tonic-gate 	dev_t			devt;
16847c478bd9Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
16877c478bd9Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	if (!msg) {
16907c478bd9Sstevel@tonic-gate 		/*
16917c478bd9Sstevel@tonic-gate 		 * Here, we just discard the responses to KIOCSLED request.
16927c478bd9Sstevel@tonic-gate 		 * Please refer to conskbd_streams_setled().
16937c478bd9Sstevel@tonic-gate 		 */
16947c478bd9Sstevel@tonic-gate 		ASSERT(((struct iocblk *)mp->b_rptr)->ioc_cmd == KIOCSLED);
16957c478bd9Sstevel@tonic-gate 		freemsg(mp);
16967c478bd9Sstevel@tonic-gate 		return;
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
17017c478bd9Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
17027c478bd9Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
17037c478bd9Sstevel@tonic-gate 	 * the lower queue from which the current response message
17047c478bd9Sstevel@tonic-gate 	 * comes.
17057c478bd9Sstevel@tonic-gate 	 */
17067c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
17077c478bd9Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
17087c478bd9Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
17097c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
17107c478bd9Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
17117c478bd9Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
17127c478bd9Sstevel@tonic-gate 	mutex_exit(&msg->kpm_lock);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (msg->kpm_resp_nums < msg->kpm_req_nums)
17157c478bd9Sstevel@tonic-gate 		return;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
17187c478bd9Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/*
17247c478bd9Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
17257c478bd9Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
17267c478bd9Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
17277c478bd9Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
17287c478bd9Sstevel@tonic-gate 	 * is sent to upstreams
17297c478bd9Sstevel@tonic-gate 	 */
17307c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
17317c478bd9Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
17347c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
17357c478bd9Sstevel@tonic-gate 		/*
17367c478bd9Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
17377c478bd9Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
17387c478bd9Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
17397c478bd9Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
17407c478bd9Sstevel@tonic-gate 		 * check if the pointer is null.
17417c478bd9Sstevel@tonic-gate 		 */
17427c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
17437c478bd9Sstevel@tonic-gate 			cons_polledio_t		*polledio;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
17467c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
17477c478bd9Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
17487c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
17497c478bd9Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
17507c478bd9Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
17517c478bd9Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
17527c478bd9Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
17537c478bd9Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
17547c478bd9Sstevel@tonic-gate 					error = 0;
17557c478bd9Sstevel@tonic-gate 				} else {
17567c478bd9Sstevel@tonic-gate 					/*
17577c478bd9Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
17587c478bd9Sstevel@tonic-gate 					 * use the same cons_polledio structure
17597c478bd9Sstevel@tonic-gate 					 * as conskbd.
17607c478bd9Sstevel@tonic-gate 					 */
17617c478bd9Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
17627c478bd9Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
17637c478bd9Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
17647c478bd9Sstevel@tonic-gate 						    "driver does not support "
17657c478bd9Sstevel@tonic-gate 						    "system debugging: %s",
17667c478bd9Sstevel@tonic-gate 						    path);
17677c478bd9Sstevel@tonic-gate 					}
17687c478bd9Sstevel@tonic-gate 					error = EINVAL;
17697c478bd9Sstevel@tonic-gate 				}
17707c478bd9Sstevel@tonic-gate 			} else {
17717c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
17727c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
17737c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
17747c478bd9Sstevel@tonic-gate 					    " not available for system"
17757c478bd9Sstevel@tonic-gate 					    " debugging:  %s", path);
17767c478bd9Sstevel@tonic-gate 				}
17777c478bd9Sstevel@tonic-gate 			}
17787c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
17797c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
17807c478bd9Sstevel@tonic-gate 			freemsg(mp);
17817c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
17857c478bd9Sstevel@tonic-gate 		if (error == 0) {
17867c478bd9Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
17877c478bd9Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
17887c478bd9Sstevel@tonic-gate 		}
17897c478bd9Sstevel@tonic-gate 		break;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
17927c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
17937c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
17947c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
17957c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
17967c478bd9Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
17977c478bd9Sstevel@tonic-gate 				error = 0;
17987c478bd9Sstevel@tonic-gate 			} else {
17997c478bd9Sstevel@tonic-gate 				devt =
18007c478bd9Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
18037c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
18047c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
18057c478bd9Sstevel@tonic-gate 					    " not available: %s", path);
18067c478bd9Sstevel@tonic-gate 				}
18077c478bd9Sstevel@tonic-gate 			}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
18107c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
18117c478bd9Sstevel@tonic-gate 			freemsg(mp);
18127c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18137c478bd9Sstevel@tonic-gate 		}
18147c478bd9Sstevel@tonic-gate 		break;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	case KIOCCMD:
18177c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
18187c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
18217c478bd9Sstevel@tonic-gate 				error = 0;
18227c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
18237c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
18247c478bd9Sstevel@tonic-gate 			freemsg(mp);
18257c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 		break;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
18307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
18347c478bd9Sstevel@tonic-gate 	if (error == 0) {
18357c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
18367c478bd9Sstevel@tonic-gate 	} else {
18377c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
18407c478bd9Sstevel@tonic-gate 	iocp->ioc_error = error;
18417c478bd9Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
18427c478bd9Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
18437c478bd9Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate static void
18497c478bd9Sstevel@tonic-gate conskbd_link_lower_queue(conskbd_lower_queue_t *lqs)
18507c478bd9Sstevel@tonic-gate {
18517c478bd9Sstevel@tonic-gate 	struct iocblk 	*iocp;
18527c478bd9Sstevel@tonic-gate 	mblk_t		*msg;
18537c478bd9Sstevel@tonic-gate 	int		index;
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	msg = lqs->lqs_pending_plink;
18587c478bd9Sstevel@tonic-gate 	msg->b_datap->db_type = M_IOCACK;
18597c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)msg->b_rptr;
18607c478bd9Sstevel@tonic-gate 	iocp->ioc_error = 0;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	/*
18637c478bd9Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
18647c478bd9Sstevel@tonic-gate 	 */
18657c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_lq_lock);
18667c478bd9Sstevel@tonic-gate 	conskbd.conskbd_lqueue_nums++;
18677c478bd9Sstevel@tonic-gate 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
18687c478bd9Sstevel@tonic-gate 	conskbd.conskbd_lqueue_list = lqs;
18697c478bd9Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
18707c478bd9Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
18717c478bd9Sstevel@tonic-gate 	}
18727c478bd9Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
18737c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_lq_lock);
18747c478bd9Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18817c478bd9Sstevel@tonic-gate static void
18827c478bd9Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
18837c478bd9Sstevel@tonic-gate {
18847c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
18907c478bd9Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
18917c478bd9Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
18927c478bd9Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
18937c478bd9Sstevel@tonic-gate 	 * should be added to this list.
18947c478bd9Sstevel@tonic-gate 	 */
18957c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
18987c478bd9Sstevel@tonic-gate 			(iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19017c478bd9Sstevel@tonic-gate 			("conskbd_legacy_upstream_msg: "
19027c478bd9Sstevel@tonic-gate 			"CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
19037c478bd9Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
19067c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19077c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
19127c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
19137c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
19147c478bd9Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
19157c478bd9Sstevel@tonic-gate 	} else {
19167c478bd9Sstevel@tonic-gate 		/* if reached here, it must be a error */
19177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
19187c478bd9Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
19197c478bd9Sstevel@tonic-gate 		freemsg(mp);
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate /*
19257c478bd9Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
19267c478bd9Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
19277c478bd9Sstevel@tonic-gate  *
19287c478bd9Sstevel@tonic-gate  * 1) application initiated request
19297c478bd9Sstevel@tonic-gate  * 	A KIOCSLED ioctl is sent by an application. The ioctl will be
19307c478bd9Sstevel@tonic-gate  * 	be prcoessed by queue service procedure conskbduwsrv(), which
19317c478bd9Sstevel@tonic-gate  * 	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
19327c478bd9Sstevel@tonic-gate  * 	conskbd_streams_setled() to set LED, after that,  kbtrans will
19337c478bd9Sstevel@tonic-gate  * 	return an ACK message to upper module.
19347c478bd9Sstevel@tonic-gate  *
19357c478bd9Sstevel@tonic-gate  * 2) Kbtrans initiated the request
19367c478bd9Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
19377c478bd9Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
19387c478bd9Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
19397c478bd9Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
19407c478bd9Sstevel@tonic-gate  *
19417c478bd9Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
19427c478bd9Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
19437c478bd9Sstevel@tonic-gate  * has no return value.
19447c478bd9Sstevel@tonic-gate  */
19457c478bd9Sstevel@tonic-gate static void
19467c478bd9Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
19497c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
19507c478bd9Sstevel@tonic-gate 	mblk_t		*req;
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (led_state == -1)
19557c478bd9Sstevel@tonic-gate 		return;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	/*
19607c478bd9Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
19617c478bd9Sstevel@tonic-gate 	 * it if this happens.
19627c478bd9Sstevel@tonic-gate 	 */
19637c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
19647c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 		if (!req) {
19677c478bd9Sstevel@tonic-gate 			continue;
19687c478bd9Sstevel@tonic-gate 		}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
19717c478bd9Sstevel@tonic-gate 		if (!req->b_cont) {
19727c478bd9Sstevel@tonic-gate 			freemsg(req);
19737c478bd9Sstevel@tonic-gate 			continue;
19747c478bd9Sstevel@tonic-gate 		}
19757c478bd9Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
19767c478bd9Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
19777c478bd9Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
19787c478bd9Sstevel@tonic-gate 			freemsg(req);
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate }	/* conskbd_streams_setled() */
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate static void
19847c478bd9Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
19857c478bd9Sstevel@tonic-gate {
19867c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
19877c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
19887c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
19917c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
19927c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
19937c478bd9Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
19947c478bd9Sstevel@tonic-gate 			    led_state);
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate static boolean_t
20017c478bd9Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
20027c478bd9Sstevel@tonic-gate 		kbtrans_key_t *keycode, enum keystate *state)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
20057c478bd9Sstevel@tonic-gate 	struct cons_polledio 		*cb;
20067c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
20077c478bd9Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
20107c478bd9Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
20117c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
20127c478bd9Sstevel@tonic-gate 		if ((cb != NULL) &&
20137c478bd9Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
20147c478bd9Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
20157c478bd9Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
20197c478bd9Sstevel@tonic-gate 		if (ret == B_TRUE) {
2020*ace75d0bSlq150181 
2021*ace75d0bSlq150181 			/* A legacy keyboard ? */
2022*ace75d0bSlq150181 			if (conskbd.conskbd_bypassed == B_TRUE)
2023*ace75d0bSlq150181 				break;
2024*ace75d0bSlq150181 
2025*ace75d0bSlq150181 			/*
2026*ace75d0bSlq150181 			 * This is the PS2 scancode 0x2B -> USB(49) /
2027*ace75d0bSlq150181 			 * USB(50) keycode mapping workaround, for
2028*ace75d0bSlq150181 			 * polled mode.
2029*ace75d0bSlq150181 			 *
2030*ace75d0bSlq150181 			 * There are two possible USB keycode mappings
2031*ace75d0bSlq150181 			 * for PS2 scancode 0x2B and this workaround
2032*ace75d0bSlq150181 			 * makes sure that we use the USB keycode that
2033*ace75d0bSlq150181 			 * does not end up being mapped to a HOLE key
2034*ace75d0bSlq150181 			 * using the current keyboard translation
2035*ace75d0bSlq150181 			 * tables.
2036*ace75d0bSlq150181 			 *
2037*ace75d0bSlq150181 			 * See conskbdlrput() for a detailed
2038*ace75d0bSlq150181 			 * explanation of the problem.
2039*ace75d0bSlq150181 			 */
2040*ace75d0bSlq150181 			if (*keycode == 49 || *keycode == 50) {
2041*ace75d0bSlq150181 				if (conskbd_keyindex->k_normal[50] == HOLE)
2042*ace75d0bSlq150181 					*keycode = 49;
2043*ace75d0bSlq150181 				else
2044*ace75d0bSlq150181 					*keycode = 50;
2045*ace75d0bSlq150181 			}
2046*ace75d0bSlq150181 
20477c478bd9Sstevel@tonic-gate 			break;
20487c478bd9Sstevel@tonic-gate 		}
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	return (ret);
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate static boolean_t
20567c478bd9Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
20577c478bd9Sstevel@tonic-gate {
20587c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
20597c478bd9Sstevel@tonic-gate 	int		directio;
20607c478bd9Sstevel@tonic-gate 	int		error;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
20637c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
20687c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT: {
20697c478bd9Sstevel@tonic-gate 		/*
20707c478bd9Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
20717c478bd9Sstevel@tonic-gate 		 * respond incorrectly.
20727c478bd9Sstevel@tonic-gate 		 */
20737c478bd9Sstevel@tonic-gate 		register mblk_t *datap;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
20767c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
20777c478bd9Sstevel@tonic-gate 			return (B_TRUE);
20787c478bd9Sstevel@tonic-gate 		}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
20817c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
20827c478bd9Sstevel@tonic-gate 		if (mp->b_cont) {
20837c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
20847c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
20857c478bd9Sstevel@tonic-gate 		}
20867c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
20877c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
20887c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
20927c478bd9Sstevel@tonic-gate 		/*
20937c478bd9Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
20947c478bd9Sstevel@tonic-gate 		 * based code see it and respond to it.
20957c478bd9Sstevel@tonic-gate 		 */
20967c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
20977c478bd9Sstevel@tonic-gate 		if (error != 0) {
20987c478bd9Sstevel@tonic-gate 			return (B_FALSE);
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
21027c478bd9Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
21037c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
21047c478bd9Sstevel@tonic-gate 			return (B_TRUE);
21057c478bd9Sstevel@tonic-gate 		}
21067c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
21097c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
21107c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
21117c478bd9Sstevel@tonic-gate 		} else {
21127c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
21137c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
21147c478bd9Sstevel@tonic-gate 		}
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 		/*
21177c478bd9Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
21187c478bd9Sstevel@tonic-gate 		 */
21197c478bd9Sstevel@tonic-gate 		return (B_FALSE);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	default:
21227c478bd9Sstevel@tonic-gate 		return (B_FALSE);
21237c478bd9Sstevel@tonic-gate 	}
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate static void
21297c478bd9Sstevel@tonic-gate conskbd_polledio_enter(struct cons_polledio_arg *arg)
21307c478bd9Sstevel@tonic-gate {
21317c478bd9Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
21327c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
21337c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
21367c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21377c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21387c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
21397c478bd9Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
21407c478bd9Sstevel@tonic-gate 		}
21417c478bd9Sstevel@tonic-gate 	}
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate static void
21467c478bd9Sstevel@tonic-gate conskbd_polledio_exit(struct cons_polledio_arg *arg)
21477c478bd9Sstevel@tonic-gate {
21487c478bd9Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
21497c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
21507c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
21537c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21547c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21557c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
21567c478bd9Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
21577c478bd9Sstevel@tonic-gate 		}
21587c478bd9Sstevel@tonic-gate 	}
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate static int
21637c478bd9Sstevel@tonic-gate conskbd_polledio_getchar(struct cons_polledio_arg *arg)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate static int
21747c478bd9Sstevel@tonic-gate conskbd_polledio_ischar(struct cons_polledio_arg *arg)
21757c478bd9Sstevel@tonic-gate {
21767c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate static void
21867c478bd9Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
21877c478bd9Sstevel@tonic-gate {
21887c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
21897c478bd9Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
21907c478bd9Sstevel@tonic-gate 	conskbd_msg_queue = msg;
21917c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate /*
21967c478bd9Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
21977c478bd9Sstevel@tonic-gate  */
21987c478bd9Sstevel@tonic-gate static conskbd_pending_msg_t *
21997c478bd9Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
22007c478bd9Sstevel@tonic-gate {
22017c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
22027c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
22037c478bd9Sstevel@tonic-gate 	uint_t	id;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
22067c478bd9Sstevel@tonic-gate 	msg = conskbd_msg_queue;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
22097c478bd9Sstevel@tonic-gate 	ASSERT(iocp);
22107c478bd9Sstevel@tonic-gate 	id = iocp->ioc_id;
22117c478bd9Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
22127c478bd9Sstevel@tonic-gate 		msg = msg->kpm_next;
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	return (msg);
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate static void
22227c478bd9Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
22237c478bd9Sstevel@tonic-gate {
22247c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
22257c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
22287c478bd9Sstevel@tonic-gate 	prev = conskbd_msg_queue;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	for (p = prev; p != msg; p = p->kpm_next)
22317c478bd9Sstevel@tonic-gate 		prev = p;
22327c478bd9Sstevel@tonic-gate 	ASSERT(p && p == msg);
22337c478bd9Sstevel@tonic-gate 	if (prev == p) {
22347c478bd9Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
22357c478bd9Sstevel@tonic-gate 	} else {
22367c478bd9Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
22377c478bd9Sstevel@tonic-gate 	}
22387c478bd9Sstevel@tonic-gate 	p->kpm_next = NULL;
22397c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate #ifdef DEBUG
22447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22457c478bd9Sstevel@tonic-gate void
22467c478bd9Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate 	char buf[256];
22497c478bd9Sstevel@tonic-gate 	va_list ap;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
22527c478bd9Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
22537c478bd9Sstevel@tonic-gate 	va_end(ap);
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate }	/* conskbd_dprintf() */
22587c478bd9Sstevel@tonic-gate #endif
2259