xref: /illumos-gate/usr/src/uts/common/io/conskbd.c (revision 5c22bad5dcbd030d95aca36e1c2fb0f77b505e17)
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
599d47a04Slq150181  * Common Development and Distribution License (the "License").
699d47a04Slq150181  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2199d47a04Slq150181 
227c478bd9Sstevel@tonic-gate /*
23cd2135d0Spengcheng chen - Sun Microsystems - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Console kbd multiplexor driver for Sun.
307c478bd9Sstevel@tonic-gate  * The console "zs" port is linked under us, with the "kbd" module pushed
317c478bd9Sstevel@tonic-gate  * on top of it.
327c478bd9Sstevel@tonic-gate  * Minor device 0 is what programs normally use.
337c478bd9Sstevel@tonic-gate  * Minor device 1 is used to feed predigested keystrokes to the "workstation
347c478bd9Sstevel@tonic-gate  * console" driver, which it is linked beneath.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *     This module can support multiple keyboards to be used simultaneously.
387c478bd9Sstevel@tonic-gate  * and enable users to use at a time multiple keyboards connected to the
397c478bd9Sstevel@tonic-gate  * same system. All the keyboards are linked under conskbd, and act as a
407c478bd9Sstevel@tonic-gate  * keyboard with replicated keys.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *     The DIN keyboards of SUN, for exmple , type 3/4/5,  are supported via
437c478bd9Sstevel@tonic-gate  * a two-level architecure. The lower one is one of serialport drivers, such
447c478bd9Sstevel@tonic-gate  * as zs, se, and the upper is  "kb" STREAMS module. Currenly, the serialport
457c478bd9Sstevel@tonic-gate  * drivers don't support polled I/O interfaces, we couldn't group the keyboard
467c478bd9Sstevel@tonic-gate  * of this kind under conskbd. So we do as the follows:
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  *         A new ioctl CONSSETKBDTYPE interface between conskbd and lower
497c478bd9Sstevel@tonic-gate  *     keyboard drivers is added. When conskbd receives I_LINK or I_PLINK
507c478bd9Sstevel@tonic-gate  *     ioctl, it will send a CONSSETKBDTYPE ioctl to the driver which is
517c478bd9Sstevel@tonic-gate  *     requesting to be linked under conskbd. If the lower driver does't
527c478bd9Sstevel@tonic-gate  *     recognize this ioctl, the virtual keyboard will be disabled so that
537c478bd9Sstevel@tonic-gate  *     only one keyboard instance could be linked under conskbd.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <sys/types.h>
587c478bd9Sstevel@tonic-gate #include <sys/param.h>
597c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
607c478bd9Sstevel@tonic-gate #include <sys/stream.h>
617c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
627c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
637c478bd9Sstevel@tonic-gate #include <sys/conf.h>
647c478bd9Sstevel@tonic-gate #include <sys/stat.h>
657c478bd9Sstevel@tonic-gate #include <sys/errno.h>
667c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
677c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
687c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
697c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
707c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
717c478bd9Sstevel@tonic-gate #include <sys/note.h>
727c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
737c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
747c478bd9Sstevel@tonic-gate #include <sys/policy.h>
757c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
767c478bd9Sstevel@tonic-gate #include <sys/kbtrans.h>
777c478bd9Sstevel@tonic-gate #include <sys/promif.h>
787c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
797c478bd9Sstevel@tonic-gate #include <sys/conskbd.h>
808ffc942dSrz201010 #include <sys/beep.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  */
96fb05fcb9SToomas Soome static int	conskbduwsrv(queue_t *);
97fb05fcb9SToomas Soome static int	conskbdlwserv(queue_t *);
98fb05fcb9SToomas Soome static int	conskbdlrput(queue_t *, mblk_t *);
997c478bd9Sstevel@tonic-gate static int	conskbdclose(queue_t *, int, cred_t *);
1007c478bd9Sstevel@tonic-gate static int	conskbdopen(queue_t *, dev_t *, int, int, cred_t *);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* STREAMS driver id and limit value struct */
1047c478bd9Sstevel@tonic-gate static struct module_info conskbdm_info = {
1057c478bd9Sstevel@tonic-gate 	0,		/* mi_idnum */
1067c478bd9Sstevel@tonic-gate 	"conskbd",	/* mi_idname */
1077c478bd9Sstevel@tonic-gate 	0,		/* mi_minpsz */
1087c478bd9Sstevel@tonic-gate 	1024,		/* mi_maxpsz */
1097c478bd9Sstevel@tonic-gate 	2048,		/* mi_hiwat */
1107c478bd9Sstevel@tonic-gate 	128		/* mi_lowat */
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * STREAMS queue processing procedure structures
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate /* upper read queue processing procedure structures */
1177c478bd9Sstevel@tonic-gate static struct qinit conskbdurinit = {
1187c478bd9Sstevel@tonic-gate 	NULL,			/* qi_putp */
1197c478bd9Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_srvp */
1207c478bd9Sstevel@tonic-gate 	conskbdopen,		/* qi_qopen */
1217c478bd9Sstevel@tonic-gate 	conskbdclose,		/* qi_qclose */
1227c478bd9Sstevel@tonic-gate 	(int (*)())NULL,	/* qi_qadmin */
1237c478bd9Sstevel@tonic-gate 	&conskbdm_info,		/* qi_minfo */
1247c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1257c478bd9Sstevel@tonic-gate };
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* upper write queue processing procedures structuresi */
1287c478bd9Sstevel@tonic-gate static struct qinit conskbduwinit = {
129fb05fcb9SToomas Soome 	putq,		/* qi_putp */
130fb05fcb9SToomas Soome 	conskbduwsrv,	/* qi_srvp */
1317c478bd9Sstevel@tonic-gate 	conskbdopen,			/* qi_qopen */
1327c478bd9Sstevel@tonic-gate 	conskbdclose,			/* qi_qclose */
1337c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1347c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1357c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* lower read queue processing procedures structures */
1397c478bd9Sstevel@tonic-gate static struct qinit conskbdlrinit = {
140fb05fcb9SToomas Soome 	conskbdlrput,	/* qi_putp */
1417c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_srvp */
1427c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1437c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1447c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1457c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1467c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
1477c478bd9Sstevel@tonic-gate };
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* lower write processing procedures structures */
1507c478bd9Sstevel@tonic-gate static struct qinit conskbdlwinit = {
1517c478bd9Sstevel@tonic-gate 	putq,				/* qi_putp */
152fb05fcb9SToomas Soome 	conskbdlwserv,			/* qi_srvp */
1537c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qopen */
1547c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qclose */
1557c478bd9Sstevel@tonic-gate 	(int (*)())NULL,		/* qi_qadmin */
1567c478bd9Sstevel@tonic-gate 	&conskbdm_info,			/* qi_minfo */
1577c478bd9Sstevel@tonic-gate 	NULL				/* qi_mstat */
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* STREAMS entity declaration structure */
1617c478bd9Sstevel@tonic-gate static struct streamtab conskbd_str_info = {
1627c478bd9Sstevel@tonic-gate 	&conskbdurinit,		/* st_rdinit */
1637c478bd9Sstevel@tonic-gate 	&conskbduwinit,		/* st_wrinit */
1647c478bd9Sstevel@tonic-gate 	&conskbdlrinit,		/* st_muxrinit */
1657c478bd9Sstevel@tonic-gate 	&conskbdlwinit,		/* st_muxwinit */
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* Entry points structure */
1707c478bd9Sstevel@tonic-gate static struct cb_ops cb_conskbd_ops = {
1717c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_open */
1727c478bd9Sstevel@tonic-gate 	nulldev,		/* cb_close */
1737c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1747c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
1757c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
1767c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
1777c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
1787c478bd9Sstevel@tonic-gate 	nodev,			/* cb_ioctl */
1797c478bd9Sstevel@tonic-gate 	nodev,			/* cb_devmap */
1807c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1817c478bd9Sstevel@tonic-gate 	nodev,			/* cb_segmap */
1827c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1837c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1847c478bd9Sstevel@tonic-gate 	&conskbd_str_info,	/* cb_stream */
18578575d6eScg149915 	D_MP | D_MTOUTPERIM | D_MTOCEXCL	/* cb_flag */
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Device operations structure
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static struct dev_ops conskbd_ops = {
1937c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1947c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
1957c478bd9Sstevel@tonic-gate 	conskbd_info,		/* devo_getinfo */
1967c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1977c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1987c478bd9Sstevel@tonic-gate 	conskbd_attach,		/* devo_attach */
1997c478bd9Sstevel@tonic-gate 	conskbd_detach,		/* devo_detach */
2007c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
2017c478bd9Sstevel@tonic-gate 	&(cb_conskbd_ops),	/* devo_cb_ops */
2027c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
20319397407SSherry Moore 	NULL,			/* devo_power */
20419397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
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 */
21219397407SSherry Moore 	"conskbd multiplexer driver",
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 /*
25678575d6eScg149915  * Module global data are protected by outer perimeter. Modifying
25778575d6eScg149915  * these global data is executed in outer perimeter exclusively.
25878575d6eScg149915  * Except in conskbdopen() and conskbdclose(), which are entered
25978575d6eScg149915  * exclusively (Refer to D_MTOCEXCL flag), all changes for the
26078575d6eScg149915  * global variables are protected by qwriter().
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static	queue_t	*conskbd_regqueue; /* regular keyboard queue above us */
2637c478bd9Sstevel@tonic-gate static	queue_t	*conskbd_consqueue; /* console queue above us */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static dev_info_t *conskbd_dip;		/* private copy of devinfo pointer */
2677c478bd9Sstevel@tonic-gate static long	conskbd_idle_stamp;	/* seconds tstamp of latest keystroke */
2687c478bd9Sstevel@tonic-gate static struct keyboard *conskbd_keyindex;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * Normally, kstats of type KSTAT_TYPE_NAMED have multiple elements.  In
2727c478bd9Sstevel@tonic-gate  * this case we use this type for a single element because the ioctl code
2737c478bd9Sstevel@tonic-gate  * for it knows how to handle mixed kernel/user data models.  Also, it
2747c478bd9Sstevel@tonic-gate  * will be easier to add new statistics later.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate static struct {
2777c478bd9Sstevel@tonic-gate 	kstat_named_t idle_sec;		/* seconds since last keystroke */
2787c478bd9Sstevel@tonic-gate } conskbd_kstat = {
2797c478bd9Sstevel@tonic-gate 	{ "idle_sec", KSTAT_DATA_LONG, }
2807c478bd9Sstevel@tonic-gate };
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * Local routines prototypes
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate static int conskbd_kstat_update(kstat_t *, int);
2867c478bd9Sstevel@tonic-gate 
28778575d6eScg149915 static void conskbd_ioctl(queue_t *, mblk_t *);
2887c478bd9Sstevel@tonic-gate static void conskbd_ioc_plink(queue_t *, mblk_t *);
28978575d6eScg149915 static void conskbd_ioc_punlink(queue_t *, mblk_t *);
2907c478bd9Sstevel@tonic-gate static void conskbd_legacy_kbd_ioctl(queue_t *, mblk_t *);
2917c478bd9Sstevel@tonic-gate static void conskbd_virtual_kbd_ioctl(queue_t *, mblk_t *);
2929ec394dbShh224818 static mblk_t *conskbd_alloc_firm_event(ushort_t, int);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate static conskbd_pending_msg_t *conskbd_mux_find_msg(mblk_t *);
2957c478bd9Sstevel@tonic-gate static void conskbd_mux_enqueue_msg(conskbd_pending_msg_t *);
2967c478bd9Sstevel@tonic-gate static void conskbd_mux_dequeue_msg(conskbd_pending_msg_t *);
29778575d6eScg149915 static void conskbd_link_lowque_virt(queue_t *, mblk_t *);
29878575d6eScg149915 static void conskbd_link_lowque_legacy(queue_t *, mblk_t *);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate static void conskbd_handle_downstream_msg(queue_t *, mblk_t *);
3017c478bd9Sstevel@tonic-gate static void conskbd_kioctype_complete(conskbd_lower_queue_t *, mblk_t *);
3027c478bd9Sstevel@tonic-gate static void conskbd_kioctrans_complete(conskbd_lower_queue_t *, mblk_t *);
3037c478bd9Sstevel@tonic-gate static void conskbd_kioclayout_complete(conskbd_lower_queue_t *, mblk_t *);
3047c478bd9Sstevel@tonic-gate static void conskbd_kiocsled_complete(conskbd_lower_queue_t *, mblk_t *);
3057c478bd9Sstevel@tonic-gate static void conskbd_mux_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3067c478bd9Sstevel@tonic-gate static void conskbd_legacy_upstream_msg(conskbd_lower_queue_t *, mblk_t *);
3077c478bd9Sstevel@tonic-gate static void conskbd_lqs_ack_complete(conskbd_lower_queue_t *, mblk_t *);
3087c478bd9Sstevel@tonic-gate 
309281f0747Slt200341 static void conskbd_polledio_enter(cons_polledio_arg_t);
310281f0747Slt200341 static void conskbd_polledio_exit(cons_polledio_arg_t);
311281f0747Slt200341 static int  conskbd_polledio_ischar(cons_polledio_arg_t);
312281f0747Slt200341 static int  conskbd_polledio_getchar(cons_polledio_arg_t);
3137c478bd9Sstevel@tonic-gate static void conskbd_polledio_setled(struct kbtrans_hardware *, int);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate static void conskbd_streams_setled(struct kbtrans_hardware *, int);
3167c478bd9Sstevel@tonic-gate static boolean_t conskbd_override_kbtrans(queue_t *, mblk_t *);
3177c478bd9Sstevel@tonic-gate static boolean_t
3187c478bd9Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *,
3197c478bd9Sstevel@tonic-gate     kbtrans_key_t *, enum keystate *);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Callbacks needed by kbtrans
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate static struct kbtrans_callbacks conskbd_callbacks = {
3257c478bd9Sstevel@tonic-gate 	conskbd_streams_setled,
3267c478bd9Sstevel@tonic-gate 	conskbd_polledio_setled,
3277c478bd9Sstevel@tonic-gate 	conskbd_polled_keycheck,
3287c478bd9Sstevel@tonic-gate };
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * Single private "global" lock for the few rare conditions
3327c478bd9Sstevel@tonic-gate  * we want single-threaded.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate static	kmutex_t	conskbd_msgq_lock;
3357c478bd9Sstevel@tonic-gate static	conskbd_pending_msg_t	*conskbd_msg_queue;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * The software state structure of virtual keyboard.
33978575d6eScg149915  * Currently, only one virtual keyboard is supported.
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate static conskbd_state_t	conskbd = { 0 };
3427c478bd9Sstevel@tonic-gate 
3437db6e34eSqz150045 /* This variable backs up the layout state for non-self-ID keyboards */
3447db6e34eSqz150045 static int kbd_layout_bak = 0;
3457db6e34eSqz150045 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * _init()
3487c478bd9Sstevel@tonic-gate  *
3497c478bd9Sstevel@tonic-gate  * Description:
3507c478bd9Sstevel@tonic-gate  *      Driver initialization, called when driver is first loaded.
3517c478bd9Sstevel@tonic-gate  *      This is how access is initially given to all the static structures.
3527c478bd9Sstevel@tonic-gate  *
3537c478bd9Sstevel@tonic-gate  * Arguments:
3547c478bd9Sstevel@tonic-gate  *      None
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  * Returns:
3577c478bd9Sstevel@tonic-gate  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
3587c478bd9Sstevel@tonic-gate  *      mod_install() status, see mod_install(9f)
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate int
_init(void)3617c478bd9Sstevel@tonic-gate _init(void)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	int	error;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
3667c478bd9Sstevel@tonic-gate 	if (error != 0) {
3677c478bd9Sstevel@tonic-gate 		return (error);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	conskbd_keyindex = kbtrans_usbkb_maptab_init();
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	mutex_init(&conskbd_msgq_lock, NULL, MUTEX_DRIVER, NULL);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	return (error);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate }	/* _init() */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * _fini()
3807c478bd9Sstevel@tonic-gate  *
3817c478bd9Sstevel@tonic-gate  * Description:
3827c478bd9Sstevel@tonic-gate  *      Module de-initialization, called when the driver is to be unloaded.
3837c478bd9Sstevel@tonic-gate  *
3847c478bd9Sstevel@tonic-gate  * Arguments:
3857c478bd9Sstevel@tonic-gate  *      None
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  * Returns:
3887c478bd9Sstevel@tonic-gate  *      mod_remove() status, see mod_remove(9f)
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate int
_fini(void)3917c478bd9Sstevel@tonic-gate _fini(void)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	int	error;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
3967c478bd9Sstevel@tonic-gate 	if (error != 0)
3977c478bd9Sstevel@tonic-gate 		return (error);
3987c478bd9Sstevel@tonic-gate 	mutex_destroy(&conskbd_msgq_lock);
3997c478bd9Sstevel@tonic-gate 	kbtrans_usbkb_maptab_fini(&conskbd_keyindex);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	return (0);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate }	/* _fini() */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * _info()
4077c478bd9Sstevel@tonic-gate  *
4087c478bd9Sstevel@tonic-gate  * Description:
4097c478bd9Sstevel@tonic-gate  *      Module information, returns information about the driver.
4107c478bd9Sstevel@tonic-gate  *
4117c478bd9Sstevel@tonic-gate  * Arguments:
4127c478bd9Sstevel@tonic-gate  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
4137c478bd9Sstevel@tonic-gate  *
4147c478bd9Sstevel@tonic-gate  * Returns:
4157c478bd9Sstevel@tonic-gate  *      mod_info() status, see mod_info(9f)
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)4187c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate }	/* _info() */
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * conskbd_attach()
4277c478bd9Sstevel@tonic-gate  *
4287c478bd9Sstevel@tonic-gate  * Description:
4297c478bd9Sstevel@tonic-gate  *	This routine creates two device nodes. One is the "kbd" node, which
4307c478bd9Sstevel@tonic-gate  * is used by user application programs(such as Xserver).The other is the
4317c478bd9Sstevel@tonic-gate  * "conskbd" node, which is an internal node. consconfig_dacf module will
4327c478bd9Sstevel@tonic-gate  * open this internal node, and link the conskbd under the wc (workstaion
4337c478bd9Sstevel@tonic-gate  * console).
4347c478bd9Sstevel@tonic-gate  *
4357c478bd9Sstevel@tonic-gate  * Arguments:
4367c478bd9Sstevel@tonic-gate  *      dev_info_t      *dip    Pointer to the device's dev_info struct
4377c478bd9Sstevel@tonic-gate  *      ddi_attach_cmd_t cmd    Attach command
4387c478bd9Sstevel@tonic-gate  *
4397c478bd9Sstevel@tonic-gate  * Returns:
4407c478bd9Sstevel@tonic-gate  *      DDI_SUCCESS             The driver was initialized properly
4417c478bd9Sstevel@tonic-gate  *      DDI_FAILURE             The driver couldn't be initialized properly
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4447c478bd9Sstevel@tonic-gate static int
conskbd_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)4457c478bd9Sstevel@tonic-gate conskbd_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	switch (cmd) {
4507c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4517c478bd9Sstevel@tonic-gate 		break;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	default:
4547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "kbd", S_IFCHR,
458a897f299SToomas Soome 	    0, DDI_PSEUDO, 0) == DDI_FAILURE) ||
4597c478bd9Sstevel@tonic-gate 	    (ddi_create_internal_pathname(devi, "conskbd", S_IFCHR,
4607c478bd9Sstevel@tonic-gate 	    1) == DDI_FAILURE)) {
4617c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
4627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	conskbd_dip = devi;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	ksp = kstat_create("conskbd", 0, "activity", "misc", KSTAT_TYPE_NAMED,
4677c478bd9Sstevel@tonic-gate 	    sizeof (conskbd_kstat) / sizeof (kstat_named_t),
4687c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
4697c478bd9Sstevel@tonic-gate 	if (ksp) {
4707c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &conskbd_kstat;
4717c478bd9Sstevel@tonic-gate 		ksp->ks_update = conskbd_kstat_update;
4727c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
4737c478bd9Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();	/* initial value */
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	conskbd.conskbd_layout = -1;	/* invalid layout */
4777c478bd9Sstevel@tonic-gate 	conskbd.conskbd_led_state = -1;
4787c478bd9Sstevel@tonic-gate 	conskbd.conskbd_bypassed = B_FALSE;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate }	/* conskbd_attach() */
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * conskbd_detach()
4867c478bd9Sstevel@tonic-gate  *
4877c478bd9Sstevel@tonic-gate  * Description:
4887c478bd9Sstevel@tonic-gate  *      Detach an instance of the conskbd driver. In fact, the driver can not
4897c478bd9Sstevel@tonic-gate  * be detached.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * Arguments:
4927c478bd9Sstevel@tonic-gate  *      dev_info_t              *dip    Pointer to the device's dev_info struct
4937c478bd9Sstevel@tonic-gate  *      ddi_detach_cmd_t        cmd     Detach command
4947c478bd9Sstevel@tonic-gate  *
4957c478bd9Sstevel@tonic-gate  * Returns:
4967c478bd9Sstevel@tonic-gate  *      DDI_SUCCESS     The driver was detached
4977c478bd9Sstevel@tonic-gate  *      DDI_FAILURE     The driver couldn't be detached
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5007c478bd9Sstevel@tonic-gate static int
conskbd_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)5017c478bd9Sstevel@tonic-gate conskbd_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate }	/* conskbd_detach() */
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /* ARGSUSED */
5087c478bd9Sstevel@tonic-gate static int
conskbd_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)5097c478bd9Sstevel@tonic-gate conskbd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
5107c478bd9Sstevel@tonic-gate     void **result)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	register int error;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	switch (infocmd) {
5157c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5167c478bd9Sstevel@tonic-gate 		if (conskbd_dip == NULL) {
5177c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
5187c478bd9Sstevel@tonic-gate 		} else {
5197c478bd9Sstevel@tonic-gate 			*result = (void *) conskbd_dip;
5207c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		break;
5237c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5247c478bd9Sstevel@tonic-gate 		*result = (void *)0;
5257c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
5267c478bd9Sstevel@tonic-gate 		break;
5277c478bd9Sstevel@tonic-gate 	default:
5287c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	return (error);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate }	/* conskbd_info() */
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5357c478bd9Sstevel@tonic-gate static int
conskbdopen(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * crp)5367c478bd9Sstevel@tonic-gate conskbdopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *crp)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	dev_t	unit;
5397c478bd9Sstevel@tonic-gate 	int	err;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	unit = getminor(*devp);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (unit == 0) {
5447c478bd9Sstevel@tonic-gate 		/*
5457c478bd9Sstevel@tonic-gate 		 * Opening "/dev/kbd".
5467c478bd9Sstevel@tonic-gate 		 */
5477c478bd9Sstevel@tonic-gate 		conskbd_regqueue = q;
5487c478bd9Sstevel@tonic-gate 		qprocson(q);
5497c478bd9Sstevel@tonic-gate 		return (0);
5507c478bd9Sstevel@tonic-gate 	} else if (unit != 1) {
5517c478bd9Sstevel@tonic-gate 		/* we don't do that under Bozo's Big Tent */
5527c478bd9Sstevel@tonic-gate 		return (ENODEV);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
55678575d6eScg149915 	 * Check if already initialized
55778575d6eScg149915 	 */
55878575d6eScg149915 	if (conskbd_consqueue != NULL)
55978575d6eScg149915 		return (0);
56078575d6eScg149915 
56178575d6eScg149915 	/*
5627c478bd9Sstevel@tonic-gate 	 * Opening the device to be linked under the console.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	conskbd_consqueue = q;
5657c478bd9Sstevel@tonic-gate 
566cd2135d0Spengcheng chen - Sun Microsystems - Beijing China 	if (secpolicy_console(crp) != 0)
567cd2135d0Spengcheng chen - Sun Microsystems - Beijing China 		return (EPERM);
568cd2135d0Spengcheng chen - Sun Microsystems - Beijing China 
5697c478bd9Sstevel@tonic-gate 	/*
570ace75d0bSlq150181 	 * initialize kbtrans module for conskbd
5717c478bd9Sstevel@tonic-gate 	 */
572cd2135d0Spengcheng chen - Sun Microsystems - Beijing China 	err = kbtrans_streams_init(q, sflag, (struct kbtrans_hardware *)
5737c478bd9Sstevel@tonic-gate 	    &conskbd, &conskbd_callbacks, &conskbd.conskbd_kbtrans, 0, 0);
5747c478bd9Sstevel@tonic-gate 	if (err != 0)
5757c478bd9Sstevel@tonic-gate 		return (err);
5767c478bd9Sstevel@tonic-gate 	kbtrans_streams_set_keyboard(conskbd.conskbd_kbtrans, KB_USB,
5777c478bd9Sstevel@tonic-gate 	    conskbd_keyindex);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_version = CONSPOLLEDIO_V1;
5807c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_argument =
581281f0747Slt200341 	    (cons_polledio_arg_t)&conskbd;
5827c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_putchar = NULL;
5837c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_getchar =
584281f0747Slt200341 	    (int (*)(cons_polledio_arg_t)) conskbd_polledio_getchar;
5857c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_ischar =
586281f0747Slt200341 	    (boolean_t (*)(cons_polledio_arg_t))conskbd_polledio_ischar;
5877c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_enter = conskbd_polledio_enter;
5887c478bd9Sstevel@tonic-gate 	conskbd.conskbd_polledio.cons_polledio_exit = conskbd_polledio_exit;
5897c478bd9Sstevel@tonic-gate 	qprocson(q);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	return (0);
5927c478bd9Sstevel@tonic-gate 
59378575d6eScg149915 }	/* conskbdopen() */
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5977c478bd9Sstevel@tonic-gate static int
conskbdclose(queue_t * q,int flag,cred_t * crp)5987c478bd9Sstevel@tonic-gate conskbdclose(queue_t *q, int flag, cred_t *crp)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	if (q == conskbd_regqueue) {
6017c478bd9Sstevel@tonic-gate 
60278575d6eScg149915 		conskbd_pending_msg_t	*pmsg, *prev, *next;
60378575d6eScg149915 		mblk_t		*mp;
60478575d6eScg149915 
6057c478bd9Sstevel@tonic-gate 		/* switch the input stream back to conskbd_consqueue */
6067c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = B_FALSE;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		kbtrans_streams_untimeout(conskbd.conskbd_kbtrans);
6097c478bd9Sstevel@tonic-gate 		kbtrans_streams_set_queue(conskbd.conskbd_kbtrans,
6107c478bd9Sstevel@tonic-gate 		    conskbd_consqueue);
6117c478bd9Sstevel@tonic-gate 		qprocsoff(q);
6127c478bd9Sstevel@tonic-gate 		conskbd_regqueue = NULL;
61378575d6eScg149915 
61478575d6eScg149915 		/*
61578575d6eScg149915 		 * If there are any pending ioctls which conskbd hasn't
61678575d6eScg149915 		 * responded to yet, remove them from conskbd_msg_queue.
61778575d6eScg149915 		 * Otherwise, we might send the response to a nonexistent
61878575d6eScg149915 		 * closed queue. Refer to: conskbd_mux_upstream_msg().
61978575d6eScg149915 		 */
62078575d6eScg149915 		for (prev = NULL, pmsg = conskbd_msg_queue; pmsg != NULL;
62178575d6eScg149915 		    pmsg = next) {
62278575d6eScg149915 			next = pmsg->kpm_next;
6234c0e8699Scg149915 			if (pmsg->kpm_upper_queue == WR(q)) {
62478575d6eScg149915 				if (prev == NULL)
62578575d6eScg149915 					conskbd_msg_queue = next;
62678575d6eScg149915 				else
62778575d6eScg149915 					prev->kpm_next = next;
62878575d6eScg149915 
62978575d6eScg149915 				while (pmsg->kpm_resp_list != NULL) {
63078575d6eScg149915 					mp = pmsg->kpm_resp_list;
63178575d6eScg149915 					pmsg->kpm_resp_list = mp->b_next;
63278575d6eScg149915 					mp->b_next = mp->b_prev = NULL;
63378575d6eScg149915 					freemsg(mp);
63478575d6eScg149915 				}
63578575d6eScg149915 				mutex_destroy(&pmsg->kpm_lock);
63678575d6eScg149915 				kmem_free(pmsg, sizeof (*pmsg));
63778575d6eScg149915 			} else {
63878575d6eScg149915 				prev = pmsg;
63978575d6eScg149915 			}
64078575d6eScg149915 		}
6417c478bd9Sstevel@tonic-gate 	} else if (q == conskbd_consqueue) {
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * Well, this is probably a mistake, but we will permit you
6447c478bd9Sstevel@tonic-gate 		 * to close the path to the console if you really insist.
6457c478bd9Sstevel@tonic-gate 		 */
6467c478bd9Sstevel@tonic-gate 		qprocsoff(q);
6477c478bd9Sstevel@tonic-gate 		conskbd_consqueue = NULL;
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	return (0);
6517c478bd9Sstevel@tonic-gate 
65278575d6eScg149915 }	/* conskbdclose() */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * Service procedure for upper write queue.
6567c478bd9Sstevel@tonic-gate  *	To make sure the order of messages, we don't process any
6577c478bd9Sstevel@tonic-gate  * message in qi_putq() routine of upper write queue, instead the
6587c478bd9Sstevel@tonic-gate  * qi_putq() routine, which is a standard putq() routine, puts all
6597c478bd9Sstevel@tonic-gate  * messages into a queue, and lets the following service procedure
6607c478bd9Sstevel@tonic-gate  * deal with all messages.
6617c478bd9Sstevel@tonic-gate  *	This routine is invoked when ioctl commands are send down
6627c478bd9Sstevel@tonic-gate  * by a consumer of the keyboard device, eg, when the keyboard
6637c478bd9Sstevel@tonic-gate  * consumer tries to determine the keyboard layout type, or sets
6647c478bd9Sstevel@tonic-gate  * the led states.
6657c478bd9Sstevel@tonic-gate  */
666fb05fcb9SToomas Soome static int
conskbduwsrv(queue_t * q)6677c478bd9Sstevel@tonic-gate conskbduwsrv(queue_t *q)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
6707c478bd9Sstevel@tonic-gate 	queue_t	*oldq;
6717c478bd9Sstevel@tonic-gate 	enum kbtrans_message_response ret;
6728ffc942dSrz201010 	struct copyresp *csp;
6738ffc942dSrz201010 	struct freq_request *frqp;
6748ffc942dSrz201010 	int error;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		/*
6797c478bd9Sstevel@tonic-gate 		 * if the virtual keyboard is supported
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 			if (conskbd_override_kbtrans(q, mp) == B_TRUE)
6847c478bd9Sstevel@tonic-gate 				continue;
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * The conskbd driver is a psaudo driver. It has two
6877c478bd9Sstevel@tonic-gate 			 * devcice nodes, one is used by kernel, and the other
6887c478bd9Sstevel@tonic-gate 			 * is used by end-users. There are two STREAMS queues
6897c478bd9Sstevel@tonic-gate 			 * corresponding to the two device nodes, console queue
6907c478bd9Sstevel@tonic-gate 			 * and regular queue.
6917c478bd9Sstevel@tonic-gate 			 * In conskbd_override_kbtrans() routine, when receives
6927c478bd9Sstevel@tonic-gate 			 * KIOCSDIRECT ioctl, we need change the direction of
6937c478bd9Sstevel@tonic-gate 			 * keyboard input messages, and direct the input stream
6947c478bd9Sstevel@tonic-gate 			 * from keyboard into right queue. It causes this queue
6957c478bd9Sstevel@tonic-gate 			 * to be switched between regular queue and console
6967c478bd9Sstevel@tonic-gate 			 * queue. And here, in this routine, the in-parameter
6977c478bd9Sstevel@tonic-gate 			 * "q" can be any one of the two. Moreover, this module
6987c478bd9Sstevel@tonic-gate 			 * is executed in multithreaded environment, even if the
6997c478bd9Sstevel@tonic-gate 			 * q is switched to regular queue, it is possible that
7007c478bd9Sstevel@tonic-gate 			 * the in-parameter is still the console queue, and we
7017c478bd9Sstevel@tonic-gate 			 * need to return response to right queue.
7027c478bd9Sstevel@tonic-gate 			 * The response is sent to upstream by the kbtrans
7037c478bd9Sstevel@tonic-gate 			 * module. so we need to save the old queue, and wait
7047c478bd9Sstevel@tonic-gate 			 * kbtrans to proces message and to send response out,
7057c478bd9Sstevel@tonic-gate 			 * and then switch back to old queue.
7067c478bd9Sstevel@tonic-gate 			 */
7077c478bd9Sstevel@tonic-gate 			oldq = kbtrans_streams_get_queue(
7087c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans);
7097c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7107c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, RD(q));
7117c478bd9Sstevel@tonic-gate 			ret = kbtrans_streams_message(
7127c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, mp);
7137c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
7147c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, oldq);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 			switch (ret) {
7177c478bd9Sstevel@tonic-gate 				case KBTRANS_MESSAGE_HANDLED:
7187c478bd9Sstevel@tonic-gate 					continue;
7197c478bd9Sstevel@tonic-gate 				case KBTRANS_MESSAGE_NOT_HANDLED:
7207c478bd9Sstevel@tonic-gate 					break;
7217c478bd9Sstevel@tonic-gate 			}
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		switch (mp->b_datap->db_type) {
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		case M_IOCTL:
72778575d6eScg149915 			conskbd_ioctl(q, mp);
7287c478bd9Sstevel@tonic-gate 			break;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		case M_FLUSH:
7317c478bd9Sstevel@tonic-gate 			if (*mp->b_rptr & FLUSHW) {
7327c478bd9Sstevel@tonic-gate 				flushq(q, FLUSHDATA);
7337c478bd9Sstevel@tonic-gate 			}
7347c478bd9Sstevel@tonic-gate 			/*
7357c478bd9Sstevel@tonic-gate 			 * here, if flush read queue, some key-up messages
7367c478bd9Sstevel@tonic-gate 			 * may be lost so that upper module or applications
7377c478bd9Sstevel@tonic-gate 			 * treat corresponding keys as being held down for
7387c478bd9Sstevel@tonic-gate 			 * ever.
7397c478bd9Sstevel@tonic-gate 			 */
7407c478bd9Sstevel@tonic-gate 			freemsg(mp);
7417c478bd9Sstevel@tonic-gate 			break;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		case M_DATA:
7447c478bd9Sstevel@tonic-gate 			/*
7457c478bd9Sstevel@tonic-gate 			 * virtual keyboard doesn't support this interface.
7467c478bd9Sstevel@tonic-gate 			 * only when it is disabled, we pass the message
7477c478bd9Sstevel@tonic-gate 			 * down to lower queue.
7487c478bd9Sstevel@tonic-gate 			 */
7497c478bd9Sstevel@tonic-gate 			if ((conskbd.conskbd_bypassed) &&
7507c478bd9Sstevel@tonic-gate 			    (conskbd.conskbd_lqueue_nums > 0)) {
7517c478bd9Sstevel@tonic-gate 				if (putq(conskbd.conskbd_lqueue_list->
7527c478bd9Sstevel@tonic-gate 				    lqs_queue, mp) != 1)
7537c478bd9Sstevel@tonic-gate 					freemsg(mp);
7547c478bd9Sstevel@tonic-gate 			} else {
7557c478bd9Sstevel@tonic-gate 				freemsg(mp);
7567c478bd9Sstevel@tonic-gate 			}
7577c478bd9Sstevel@tonic-gate 			break;
7587c478bd9Sstevel@tonic-gate 
7598ffc942dSrz201010 		case M_IOCDATA:
7608ffc942dSrz201010 			/*
7618ffc942dSrz201010 			 * Only deal with copyresp to KIOCSETFREQ
7628ffc942dSrz201010 			 * transparent ioctl now
7638ffc942dSrz201010 			 */
7648ffc942dSrz201010 			csp = (struct copyresp *)mp->b_rptr;
7658ffc942dSrz201010 			if (csp->cp_rval) {
7668ffc942dSrz201010 				miocnak(q, mp, 0, EINVAL);
7678ffc942dSrz201010 				break;
7688ffc942dSrz201010 			}
7698ffc942dSrz201010 
7708ffc942dSrz201010 			error = 0;
7718ffc942dSrz201010 			switch (csp->cp_cmd) {
7728ffc942dSrz201010 			case KIOCSETFREQ:
7738ffc942dSrz201010 				frqp = (struct freq_request *)mp->
7748ffc942dSrz201010 				    b_cont->b_rptr;
7758ffc942dSrz201010 
7768ffc942dSrz201010 				switch (frqp->type) {
7778ffc942dSrz201010 				case CONSOLE_BEEP:
7788ffc942dSrz201010 					error = beeper_freq(BEEP_CONSOLE,
7798ffc942dSrz201010 					    (int)frqp->freq);
7808ffc942dSrz201010 						break;
7818ffc942dSrz201010 
7828ffc942dSrz201010 				case KBD_BEEP:
7838ffc942dSrz201010 					error = beeper_freq(BEEP_TYPE4,
7848ffc942dSrz201010 					    (int)frqp->freq);
7858ffc942dSrz201010 						break;
7868ffc942dSrz201010 
7878ffc942dSrz201010 				default:
7888ffc942dSrz201010 					error = 1;
7898ffc942dSrz201010 				} /* frqp->type */
7908ffc942dSrz201010 
7918ffc942dSrz201010 				break;
7928ffc942dSrz201010 
7938ffc942dSrz201010 			default:
7948ffc942dSrz201010 				error = 1;
7958ffc942dSrz201010 			} /* csp->cp_cmd */
7968ffc942dSrz201010 
7978ffc942dSrz201010 			if (error == 0)
7988ffc942dSrz201010 				miocack(q, mp, 0, 0);
7998ffc942dSrz201010 			else
8008ffc942dSrz201010 				miocnak(q, mp, 0, EINVAL);
8018ffc942dSrz201010 
8028ffc942dSrz201010 			break;
8038ffc942dSrz201010 
8047c478bd9Sstevel@tonic-gate 		default:
8057c478bd9Sstevel@tonic-gate 			/*
8067c478bd9Sstevel@tonic-gate 			 * Pass an error message up.
8077c478bd9Sstevel@tonic-gate 			 */
8087c478bd9Sstevel@tonic-gate 			mp->b_datap->db_type = M_ERROR;
8097c478bd9Sstevel@tonic-gate 			if (mp->b_cont) {
8107c478bd9Sstevel@tonic-gate 				freemsg(mp->b_cont);
8117c478bd9Sstevel@tonic-gate 				mp->b_cont = NULL;
8127c478bd9Sstevel@tonic-gate 			}
8137c478bd9Sstevel@tonic-gate 			mp->b_rptr = mp->b_datap->db_base;
8147c478bd9Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + sizeof (char);
8157c478bd9Sstevel@tonic-gate 			*mp->b_rptr = EINVAL;
8167c478bd9Sstevel@tonic-gate 			qreply(q, mp);
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 	}	/* end of while */
8197c478bd9Sstevel@tonic-gate 
820fb05fcb9SToomas Soome 	return (0);
8217c478bd9Sstevel@tonic-gate }	/* conskbduwsrv() */
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate static void
conskbd_ioctl(queue_t * q,mblk_t * mp)82478575d6eScg149915 conskbd_ioctl(queue_t *q, mblk_t *mp)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	struct	iocblk			*iocp;
8277c478bd9Sstevel@tonic-gate 	int	error = 0;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	case I_LINK:
8347c478bd9Sstevel@tonic-gate 	case I_PLINK:
835b7e54746Scg149915 		if (conskbd.conskbd_bypassed == B_TRUE) {
836b7e54746Scg149915 		/*
837b7e54746Scg149915 		 * A legacy keyboard can NOT be connected to conskbd together
838b7e54746Scg149915 		 * with other keyboards. So when a legacy keyboard is already
839b7e54746Scg149915 		 * linked under conkbd, we just reject all others.
840b7e54746Scg149915 		 */
841b7e54746Scg149915 			miocnak(q, mp, 0, EAGAIN);
842b7e54746Scg149915 			break;
843b7e54746Scg149915 		}
84478575d6eScg149915 		qwriter(q, mp, conskbd_ioc_plink, PERIM_OUTER);
8457c478bd9Sstevel@tonic-gate 		break;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	case I_UNLINK:
8487c478bd9Sstevel@tonic-gate 	case I_PUNLINK:
84978575d6eScg149915 		qwriter(q, mp, conskbd_ioc_punlink, PERIM_OUTER);
8507c478bd9Sstevel@tonic-gate 		break;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	case KIOCSKABORTEN:
8537c478bd9Sstevel@tonic-gate 		/*
8547c478bd9Sstevel@tonic-gate 		 * Check if privileged
8557c478bd9Sstevel@tonic-gate 		 */
8567c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_sys_config(iocp->ioc_cr, B_FALSE))) {
8577c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8587c478bd9Sstevel@tonic-gate 			return;
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
8627c478bd9Sstevel@tonic-gate 		if (error != 0) {
8637c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
8647c478bd9Sstevel@tonic-gate 			return;
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		abort_enable = *(int *)mp->b_cont->b_rptr;
8687c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
8697c478bd9Sstevel@tonic-gate 		break;
8707c478bd9Sstevel@tonic-gate 
8718ffc942dSrz201010 	case KIOCSETFREQ:
8728ffc942dSrz201010 		if (iocp->ioc_count != TRANSPARENT) {
8738ffc942dSrz201010 			/*
8748ffc942dSrz201010 			 * We don't support non-transparent ioctls,
8758ffc942dSrz201010 			 * i.e. I_STR ioctls
8768ffc942dSrz201010 			 */
8778ffc942dSrz201010 			miocnak(q, mp, 0, EINVAL);
8788ffc942dSrz201010 		} else {
8798ffc942dSrz201010 			/* Transparent ioctl */
8808ffc942dSrz201010 			mcopyin(mp, NULL, sizeof (struct freq_request), NULL);
8818ffc942dSrz201010 			qreply(q, mp);
8828ffc942dSrz201010 		}
8838ffc942dSrz201010 		break;
8848ffc942dSrz201010 
8857c478bd9Sstevel@tonic-gate 	default:
8867c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_TRUE) {
8877c478bd9Sstevel@tonic-gate 			conskbd_legacy_kbd_ioctl(q, mp);
8887c478bd9Sstevel@tonic-gate 		} else {
8897c478bd9Sstevel@tonic-gate 			conskbd_virtual_kbd_ioctl(q, mp);
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
89378575d6eScg149915 }	/* conskbd_ioctl() */
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate static void
conskbd_virtual_kbd_ioctl(queue_t * q,mblk_t * mp)8977c478bd9Sstevel@tonic-gate conskbd_virtual_kbd_ioctl(queue_t *q, mblk_t *mp)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
9007c478bd9Sstevel@tonic-gate 	mblk_t			*datap;
9017c478bd9Sstevel@tonic-gate 	int			cmd;
9027c478bd9Sstevel@tonic-gate 	int			error = 0;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
9077c478bd9Sstevel@tonic-gate 	case KIOCLAYOUT:
9087c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9097c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
9107c478bd9Sstevel@tonic-gate 			break;
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_layout == -1)
9147c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9157c478bd9Sstevel@tonic-gate 		else
9167c478bd9Sstevel@tonic-gate 			*(int *)datap->b_wptr = conskbd.conskbd_layout;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9197c478bd9Sstevel@tonic-gate 		if (mp->b_cont)
9207c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9217c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9227c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
9237c478bd9Sstevel@tonic-gate 		break;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	case KIOCSLAYOUT:
9267c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
9277c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9287c478bd9Sstevel@tonic-gate 			break;
9297c478bd9Sstevel@tonic-gate 		}
930b49b27deSqz150045 		kbd_layout_bak = conskbd.conskbd_layout;
9317c478bd9Sstevel@tonic-gate 		conskbd.conskbd_layout = *(intptr_t *)(mp->b_cont->b_rptr);
932b49b27deSqz150045 		if (conskbd.conskbd_layout != kbd_layout_bak) {
933b49b27deSqz150045 
934b49b27deSqz150045 			/* notify the upper of the change event */
935b49b27deSqz150045 			if ((datap = conskbd_alloc_firm_event(
936b49b27deSqz150045 			    KEYBOARD_LAYOUT_CHANGE,
937b49b27deSqz150045 			    conskbd.conskbd_layout)) != NULL) {
938b49b27deSqz150045 				if (conskbd.conskbd_directio) {
939b49b27deSqz150045 					putnext(conskbd_regqueue, datap);
940b49b27deSqz150045 				} else {
941b49b27deSqz150045 					freemsg(datap);
942b49b27deSqz150045 				}
943b49b27deSqz150045 			}
944b49b27deSqz150045 		}
9457c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
9497c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (struct cons_polledio *));
9507c478bd9Sstevel@tonic-gate 		if (error != 0) {
9517c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
9527c478bd9Sstevel@tonic-gate 			break;
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
9557c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9567c478bd9Sstevel@tonic-gate 			break;
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9597c478bd9Sstevel@tonic-gate 		break;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
9627c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL) {
9637c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9647c478bd9Sstevel@tonic-gate 			break;
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
9677c478bd9Sstevel@tonic-gate 		break;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
9707c478bd9Sstevel@tonic-gate 		/*
9717c478bd9Sstevel@tonic-gate 		 * To enable combined STOP-A(or F1-A) to trap into kmdb,
9727c478bd9Sstevel@tonic-gate 		 * the lower physical keyboard drivers are always told not
9737c478bd9Sstevel@tonic-gate 		 * to parse abort sequence(refer to consconfig_dacf module).
9747c478bd9Sstevel@tonic-gate 		 * Instead, lower drivers always send all keydown & keyup
9757c478bd9Sstevel@tonic-gate 		 * messages up to conskbd, so that when key STOP(or F1) is
9767c478bd9Sstevel@tonic-gate 		 * pressed on one keyboard and key A is pressed on another
9777c478bd9Sstevel@tonic-gate 		 * keyboard, the system could trap into kmdb.
9787c478bd9Sstevel@tonic-gate 		 *
9797c478bd9Sstevel@tonic-gate 		 * When we by kbtrans_streams_message() invoked kbtrans to
9807c478bd9Sstevel@tonic-gate 		 * handle ioctls in conskbduwsrv() routine, kbtrans module
9817c478bd9Sstevel@tonic-gate 		 * already handle the message though it returned to us a
9827c478bd9Sstevel@tonic-gate 		 * KBTRANS_MESSAGE_NOT_HANDLED. For virtual keyboard, no
9837c478bd9Sstevel@tonic-gate 		 * special initialization or un-initialization is needed.
9847c478bd9Sstevel@tonic-gate 		 * So we just return ACK to upper module.
9857c478bd9Sstevel@tonic-gate 		 */
9867c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
9877c478bd9Sstevel@tonic-gate 		break;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	case KIOCCMD:
990c35aa225Smarx 	case KIOCMKTONE:
9917c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_list == NULL ||
9927c478bd9Sstevel@tonic-gate 		    mp->b_cont == NULL) {
9937c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
9947c478bd9Sstevel@tonic-gate 			break;
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		cmd = *(int *)mp->b_cont->b_rptr;
9977c478bd9Sstevel@tonic-gate 		if (cmd == KBD_CMD_GETLAYOUT) {
9987c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9997c478bd9Sstevel@tonic-gate 			datap = allocb(sizeof (int), BPRI_HI);
10007c478bd9Sstevel@tonic-gate 			if (datap == NULL) {
10017c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10027c478bd9Sstevel@tonic-gate 				return;
10037c478bd9Sstevel@tonic-gate 			}
10047c478bd9Sstevel@tonic-gate 			if (conskbd.conskbd_layout == -1)
10057c478bd9Sstevel@tonic-gate 				*(int *)datap->b_wptr =
10067c478bd9Sstevel@tonic-gate 				    KBTRANS_USBKB_DEFAULT_LAYOUT;
10077c478bd9Sstevel@tonic-gate 			else
10087c478bd9Sstevel@tonic-gate 				*(int *)datap->b_wptr = conskbd.conskbd_layout;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 			mp->b_cont = datap;
10117c478bd9Sstevel@tonic-gate 			miocack(q, mp, sizeof (int), 0);
10127c478bd9Sstevel@tonic-gate 			return;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 		conskbd_handle_downstream_msg(q, mp);
10157c478bd9Sstevel@tonic-gate 		break;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	default:
10187c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10197c478bd9Sstevel@tonic-gate 		break;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate }	/* conskbd_virtual_kbd_ioctl() */
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate static void
conskbd_legacy_kbd_ioctl(queue_t * q,mblk_t * mp)10257c478bd9Sstevel@tonic-gate conskbd_legacy_kbd_ioctl(queue_t *q, mblk_t *mp)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lq;
10287c478bd9Sstevel@tonic-gate 	struct	iocblk		*iocp;
10297c478bd9Sstevel@tonic-gate 	int	error = 0;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	ASSERT(conskbd.conskbd_lqueue_nums == 1);
10347c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT: {
10377c478bd9Sstevel@tonic-gate 		mblk_t *datap;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_MED)) == NULL) {
10407c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
10417c478bd9Sstevel@tonic-gate 			break;
10427c478bd9Sstevel@tonic-gate 		}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
10457c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
10467c478bd9Sstevel@tonic-gate 		if (mp->b_cont != NULL) {
10477c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
10487c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
10517c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
10527c478bd9Sstevel@tonic-gate 		break;
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
10567c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
10577c478bd9Sstevel@tonic-gate 		if (error != 0) {
10587c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, error);
10597c478bd9Sstevel@tonic-gate 			break;
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = *(int *)mp->b_cont->b_rptr;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		/*
10647c478bd9Sstevel@tonic-gate 		 * Pass this through, if there's something to pass
10657c478bd9Sstevel@tonic-gate 		 * it through to, so the system keyboard can reset
10667c478bd9Sstevel@tonic-gate 		 * itself.
10677c478bd9Sstevel@tonic-gate 		 */
10687c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10697c478bd9Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10707c478bd9Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10717c478bd9Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10727c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10737c478bd9Sstevel@tonic-gate 				return;
10747c478bd9Sstevel@tonic-gate 			}
10757c478bd9Sstevel@tonic-gate 			break;
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		miocack(q, mp, 0, 0);
10797c478bd9Sstevel@tonic-gate 		break;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	default:
10827c478bd9Sstevel@tonic-gate 		/*
10837c478bd9Sstevel@tonic-gate 		 * Pass this through, if there's something to pass it
10847c478bd9Sstevel@tonic-gate 		 * through to; otherwise, reject it.
10857c478bd9Sstevel@tonic-gate 		 */
10867c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
10877c478bd9Sstevel@tonic-gate 			lq = conskbd.conskbd_lqueue_list;
10887c478bd9Sstevel@tonic-gate 			ASSERT(lq && lq->lqs_next == NULL);
10897c478bd9Sstevel@tonic-gate 			if (putq(lq->lqs_queue, mp) != 1) {
10907c478bd9Sstevel@tonic-gate 				miocnak(q, mp, 0, ENOMEM);
10917c478bd9Sstevel@tonic-gate 				return;
10927c478bd9Sstevel@tonic-gate 			}
10937c478bd9Sstevel@tonic-gate 			break;
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		/* nobody below us; reject it */
10977c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
10987c478bd9Sstevel@tonic-gate 		break;
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate }	/* conskbd_legacy_kbd_ioctl() */
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate /*
11057c478bd9Sstevel@tonic-gate  * Service procedure for lower write queue.
11067c478bd9Sstevel@tonic-gate  * Puts things on the queue below us, if it lets us.
11077c478bd9Sstevel@tonic-gate  */
1108fb05fcb9SToomas Soome static int
conskbdlwserv(queue_t * q)11097c478bd9Sstevel@tonic-gate conskbdlwserv(queue_t *q)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	register mblk_t *mp;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	while (canput(q->q_next) && (mp = getq(q)) != NULL)
11147c478bd9Sstevel@tonic-gate 		putnext(q, mp);
11157c478bd9Sstevel@tonic-gate 
1116fb05fcb9SToomas Soome 	return (0);
11177c478bd9Sstevel@tonic-gate }	/* conskbdlwserv() */
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate /*
11207c478bd9Sstevel@tonic-gate  * Put procedure for lower read queue.
11217c478bd9Sstevel@tonic-gate  * Pass everything up to minor device 0 if "directio" set, otherwise to minor
11227c478bd9Sstevel@tonic-gate  * device 1.
11237c478bd9Sstevel@tonic-gate  */
1124fb05fcb9SToomas Soome static int
conskbdlrput(queue_t * q,mblk_t * mp)11257c478bd9Sstevel@tonic-gate conskbdlrput(queue_t *q, mblk_t *mp)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
11287c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
11297c478bd9Sstevel@tonic-gate 	Firm_event	*fe;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput\n"));
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1134*5c22bad5SToomas Soome 	case M_CTL:
1135*5c22bad5SToomas Soome 		mp->b_datap->db_type = M_DATA;
1136*5c22bad5SToomas Soome 		if (conskbd.conskbd_directio)
1137*5c22bad5SToomas Soome 			putnext(conskbd_regqueue, mp);
1138*5c22bad5SToomas Soome 		else if (conskbd_consqueue != NULL)
1139*5c22bad5SToomas Soome 			putnext(conskbd_consqueue, mp);
1140*5c22bad5SToomas Soome 		else
1141*5c22bad5SToomas Soome 			freemsg(mp);
1142*5c22bad5SToomas Soome 		break;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	case M_FLUSH:
11457c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHR) {
11467c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);	/* XXX doesn't flush M_DELAY */
11477c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;	/* it has been flushed */
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr == FLUSHW) {
11507c478bd9Sstevel@tonic-gate 			flushq(WR(q), FLUSHDATA);
11517c478bd9Sstevel@tonic-gate 			qreply(q, mp);	/* give the read queues a crack at it */
11527c478bd9Sstevel@tonic-gate 		} else
11537c478bd9Sstevel@tonic-gate 			freemsg(mp);
11547c478bd9Sstevel@tonic-gate 		break;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	case M_DATA:
11577c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_bypassed == B_FALSE) {
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 			fe = (Firm_event *)mp->b_rptr;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 			/*
11627c478bd9Sstevel@tonic-gate 			 * This is a workaround.
11637c478bd9Sstevel@tonic-gate 			 *
11647c478bd9Sstevel@tonic-gate 			 * According to HID specification, there are the
11657c478bd9Sstevel@tonic-gate 			 * following keycode mapping between PS2 and USB,
11667c478bd9Sstevel@tonic-gate 			 *
11677c478bd9Sstevel@tonic-gate 			 *	PS2 AT-101 keycode(29)  --->    USB(49)
11687c478bd9Sstevel@tonic-gate 			 *	PS2 AT-102 keycode(42)  --->    USB(50)
11697c478bd9Sstevel@tonic-gate 			 *
11707c478bd9Sstevel@tonic-gate 			 * However, the two keys, AT-101(29) and AT-102(42),
11717c478bd9Sstevel@tonic-gate 			 * have the same scancode,0x2B, in PS2 scancode SET1
11727c478bd9Sstevel@tonic-gate 			 * which we are using. The Kb8042 driver always
11737c478bd9Sstevel@tonic-gate 			 * recognizes the two keys as PS2(29) so that we could
11747c478bd9Sstevel@tonic-gate 			 * not know which is being pressed or released when we
11757c478bd9Sstevel@tonic-gate 			 * receive scancode 0x2B. Fortunately, the two keys can
11767c478bd9Sstevel@tonic-gate 			 * not co-exist in a specific layout. In other words,
11777c478bd9Sstevel@tonic-gate 			 * in the table of keycode-to-symbol mapping, either
11787c478bd9Sstevel@tonic-gate 			 * entry 49 or 50 is a hole. So, if we're processing a
11797c478bd9Sstevel@tonic-gate 			 * keycode 49, we look at the entry for 49.  If it's
11807c478bd9Sstevel@tonic-gate 			 * HOLE, remap the key to 50; If we're processing a 50,
11817c478bd9Sstevel@tonic-gate 			 * look at the entry for 50.  If it's HOLE, we remap
11827c478bd9Sstevel@tonic-gate 			 * the key to 49.
11837c478bd9Sstevel@tonic-gate 			 */
11847c478bd9Sstevel@tonic-gate 			if (fe->id == 49 || fe->id == 50) {
11857c478bd9Sstevel@tonic-gate 				if (conskbd_keyindex->k_normal[50] == HOLE)
11867c478bd9Sstevel@tonic-gate 					fe->id = 49;
11877c478bd9Sstevel@tonic-gate 				else
11887c478bd9Sstevel@tonic-gate 					fe->id = 50;
11897c478bd9Sstevel@tonic-gate 			}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 			/*
11927c478bd9Sstevel@tonic-gate 			 * Remember key state of each key of lower physical
11937c478bd9Sstevel@tonic-gate 			 * keyboard. When a keyboard is umplumbed from conskbd,
11947c478bd9Sstevel@tonic-gate 			 * we will check all key states. By then,  we will fake
11957c478bd9Sstevel@tonic-gate 			 * a KEY_RELEASED message for each key in KEY_PRESSED
11967c478bd9Sstevel@tonic-gate 			 * state. Otherwise, upper module will treat these keys
11977c478bd9Sstevel@tonic-gate 			 * as held-down for ever.
11987c478bd9Sstevel@tonic-gate 			 */
11997c478bd9Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
12007c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)q->q_ptr;
12017c478bd9Sstevel@tonic-gate 			if (fe->value)
12027c478bd9Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_PRESSED;
12037c478bd9Sstevel@tonic-gate 			else
12047c478bd9Sstevel@tonic-gate 				lqs->lqs_key_state[fe->id] = KEY_RELEASED;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 			kbtrans_streams_key(conskbd.conskbd_kbtrans,
12077c478bd9Sstevel@tonic-gate 			    fe->id, fe->value ? KEY_PRESSED : KEY_RELEASED);
12087c478bd9Sstevel@tonic-gate 			freemsg(mp);
12097c478bd9Sstevel@tonic-gate 		} else {
12107c478bd9Sstevel@tonic-gate 			if (conskbd.conskbd_directio)
12117c478bd9Sstevel@tonic-gate 				putnext(conskbd_regqueue, mp);
12127c478bd9Sstevel@tonic-gate 			else if (conskbd_consqueue != NULL)
12137c478bd9Sstevel@tonic-gate 				putnext(conskbd_consqueue, mp);
12147c478bd9Sstevel@tonic-gate 			else
12157c478bd9Sstevel@tonic-gate 				freemsg(mp);
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 		conskbd_idle_stamp = gethrestime_sec();
12187c478bd9Sstevel@tonic-gate 		break;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	case M_IOCACK:
12217c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
12227c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
12237c478bd9Sstevel@tonic-gate 		lqs = (conskbd_lower_queue_t *)q->q_ptr;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL, ("conskbdlrput: "
12267c478bd9Sstevel@tonic-gate 		    "ACK/NAK - cmd 0x%x\n", iocp->ioc_cmd));
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		conskbd_lqs_ack_complete(lqs, mp);
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	case M_ERROR:
12327c478bd9Sstevel@tonic-gate 	case M_HANGUP:
12337c478bd9Sstevel@tonic-gate 	default:
12347c478bd9Sstevel@tonic-gate 		freemsg(mp);	/* anything useful here? */
12357c478bd9Sstevel@tonic-gate 		break;
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
1238fb05fcb9SToomas Soome 	return (0);
12397c478bd9Sstevel@tonic-gate }	/* conskbdlrput() */
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate /* ARGSUSED */
12437c478bd9Sstevel@tonic-gate static int
conskbd_kstat_update(kstat_t * ksp,int rw)12447c478bd9Sstevel@tonic-gate conskbd_kstat_update(kstat_t *ksp, int rw)
12457c478bd9Sstevel@tonic-gate {
12467c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
12477c478bd9Sstevel@tonic-gate 		return (EACCES);
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	conskbd_kstat.idle_sec.value.l = gethrestime_sec() - conskbd_idle_stamp;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	return (0);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate }	/* conskbd_kstat_update() */
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate  * STREAMS architecuture provides guarantee that the ID of each
12577c478bd9Sstevel@tonic-gate  * message, iocblk.ioc_id, in a stream is unique. The following
12587c478bd9Sstevel@tonic-gate  * routine performes the task: When receive request from upstream,
12597c478bd9Sstevel@tonic-gate  * it saves the request in a global link list, clones the request,
12607c478bd9Sstevel@tonic-gate  * and then sends a copy of the request to each of lower queues
12617c478bd9Sstevel@tonic-gate  * which are plumbed into conskbd. And then, when receives responses
12627c478bd9Sstevel@tonic-gate  * from lower queues in conskbdlrput() routine, we can know the
12637c478bd9Sstevel@tonic-gate  * request matching received responses by searching the global linked
12647c478bd9Sstevel@tonic-gate  * list to find the request which has the same message ID of the
12657c478bd9Sstevel@tonic-gate  * response. Then, when all lower queues response this request, we
12667c478bd9Sstevel@tonic-gate  * give a response to upstreams based the following policy:
12677c478bd9Sstevel@tonic-gate  * If any one of lower queues acks our reuqest, then we return ack
12687c478bd9Sstevel@tonic-gate  * to upstreams; only if all lower queues nak our request, we return
12697c478bd9Sstevel@tonic-gate  * nak to upstreams. If all responses are nak, the error number of
12707c478bd9Sstevel@tonic-gate  * the first response is sent to upstream.
12717c478bd9Sstevel@tonic-gate  */
12727c478bd9Sstevel@tonic-gate static void
conskbd_handle_downstream_msg(queue_t * q,mblk_t * mp)12737c478bd9Sstevel@tonic-gate conskbd_handle_downstream_msg(queue_t *q, mblk_t *mp)
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
12767c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
12777c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
12787c478bd9Sstevel@tonic-gate 	mblk_t		*clonemp;
12797c478bd9Sstevel@tonic-gate 	int		retry;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if (conskbd.conskbd_lqueue_nums == 0) {
12827c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
12837c478bd9Sstevel@tonic-gate 		return;
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	msg = (conskbd_pending_msg_t *)
12877c478bd9Sstevel@tonic-gate 	    kmem_zalloc(sizeof (conskbd_pending_msg_t), KM_SLEEP);
12887c478bd9Sstevel@tonic-gate 	mutex_init(&msg->kpm_lock, NULL, MUTEX_DRIVER, NULL);
12897c478bd9Sstevel@tonic-gate 	lqs = conskbd.conskbd_lqueue_list;
12907c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	ASSERT(iocp->ioc_cmd == CONSOPENPOLLEDIO ||
12937c478bd9Sstevel@tonic-gate 	    iocp->ioc_cmd == CONSCLOSEPOLLEDIO ||
1294c35aa225Smarx 	    iocp->ioc_cmd == KIOCCMD ||
1295c35aa225Smarx 	    iocp->ioc_cmd == KIOCMKTONE);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	msg->kpm_upper_queue = q;
12987c478bd9Sstevel@tonic-gate 	msg->kpm_req_msg = mp;
12997c478bd9Sstevel@tonic-gate 	msg->kpm_req_id = iocp->ioc_id;
13007c478bd9Sstevel@tonic-gate 	msg->kpm_req_cmd = iocp->ioc_cmd;
13017c478bd9Sstevel@tonic-gate 	msg->kpm_req_nums = conskbd.conskbd_lqueue_nums;
13027c478bd9Sstevel@tonic-gate 	conskbd_mux_enqueue_msg(msg);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	for (retry = 0, lqs = conskbd.conskbd_lqueue_list; lqs; ) {
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 		/*
13077c478bd9Sstevel@tonic-gate 		 * if a lower physical keyboard is not in polled I/O
13087c478bd9Sstevel@tonic-gate 		 * mode, we couldn't send CONSCLOSEPOLLEDIO to it,
13097c478bd9Sstevel@tonic-gate 		 * otherwise, system will panic.
13107c478bd9Sstevel@tonic-gate 		 */
13117c478bd9Sstevel@tonic-gate 		if (iocp->ioc_cmd == CONSCLOSEPOLLEDIO &&
13127c478bd9Sstevel@tonic-gate 		    lqs->lqs_polledio == NULL) {
13137c478bd9Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13147c478bd9Sstevel@tonic-gate 			msg->kpm_req_nums --;
13157c478bd9Sstevel@tonic-gate 			retry = 0;
13167c478bd9Sstevel@tonic-gate 			continue;
13177c478bd9Sstevel@tonic-gate 		}
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 		clonemp = copymsg(mp);
13207c478bd9Sstevel@tonic-gate 		if (clonemp != NULL) {
13217c478bd9Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, clonemp) == 1) {
13227c478bd9Sstevel@tonic-gate 				lqs = lqs->lqs_next;
13237c478bd9Sstevel@tonic-gate 				retry = 0;
13247c478bd9Sstevel@tonic-gate 				continue;
13257c478bd9Sstevel@tonic-gate 			}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 			/*
13287c478bd9Sstevel@tonic-gate 			 * failed to invoke putq(), retry.
13297c478bd9Sstevel@tonic-gate 			 */
13307c478bd9Sstevel@tonic-gate 			freemsg(clonemp);
13317c478bd9Sstevel@tonic-gate 		}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		/*
13347c478bd9Sstevel@tonic-gate 		 * During testing it was observed that occasionally
13357c478bd9Sstevel@tonic-gate 		 * copymsg() would fail during boot. The reason for
13367c478bd9Sstevel@tonic-gate 		 * these failures is unknown. Since we really want
13377c478bd9Sstevel@tonic-gate 		 * to successfully plumb up all the attached keyboards
13387c478bd9Sstevel@tonic-gate 		 * during boot we do a best effort here by retrying
13397c478bd9Sstevel@tonic-gate 		 * the copymsg() call in the hopes that it will
13407c478bd9Sstevel@tonic-gate 		 * succeeded upon subsequent invocations.
13417c478bd9Sstevel@tonic-gate 		 *
13427c478bd9Sstevel@tonic-gate 		 * If all the calls to copymsg() fails, it will cause
13437c478bd9Sstevel@tonic-gate 		 * the corresponding keyboard to be unavailable, or
13447c478bd9Sstevel@tonic-gate 		 * or behave weirdly,
13457c478bd9Sstevel@tonic-gate 		 *
13467c478bd9Sstevel@tonic-gate 		 * 1) for CONSOPENPOLLEDIO
13477c478bd9Sstevel@tonic-gate 		 *	if copymsg()fails, the corresponding keyboard
13487c478bd9Sstevel@tonic-gate 		 *	is not available in polled I/O mode once
13497c478bd9Sstevel@tonic-gate 		 *	entering kmdb;
13507c478bd9Sstevel@tonic-gate 		 * 2) for CONSCLOSEPOLLEDIO
13517c478bd9Sstevel@tonic-gate 		 *	if copymsg() fails, the corresponding keyboard
13527c478bd9Sstevel@tonic-gate 		 *	is not available in normal mode once returning
13537c478bd9Sstevel@tonic-gate 		 *	from kmdb;
13547c478bd9Sstevel@tonic-gate 		 * 3) for KIOCCMD
13557c478bd9Sstevel@tonic-gate 		 *	3.1) for KBD_CMD_NOBELL
13567c478bd9Sstevel@tonic-gate 		 *		there's no beep in USB and PS2 keyboard,
13577c478bd9Sstevel@tonic-gate 		 *		this ioctl actually disables the beep on
13587c478bd9Sstevel@tonic-gate 		 *		system mainboard. Note that all the cloned
13597c478bd9Sstevel@tonic-gate 		 *		messages sent down to lower queues do the
13607c478bd9Sstevel@tonic-gate 		 *		same job for system mainboard. Therefore,
13617c478bd9Sstevel@tonic-gate 		 *		even if we fail to send this ioctl to most
13627c478bd9Sstevel@tonic-gate 		 *		of lower queues, the beep still would be
13637c478bd9Sstevel@tonic-gate 		 *		disabled. So, no trouble exists here.
13647c478bd9Sstevel@tonic-gate 		 *	3.2) for others
13657c478bd9Sstevel@tonic-gate 		 *		nothing;
13667c478bd9Sstevel@tonic-gate 		 *
13677c478bd9Sstevel@tonic-gate 		 * However, all cases could be resume next time when the
13687c478bd9Sstevel@tonic-gate 		 * same request comes again.
13697c478bd9Sstevel@tonic-gate 		 */
13707c478bd9Sstevel@tonic-gate 		if (retry ++ >= 5) {
13717c478bd9Sstevel@tonic-gate 			dev_t	devt;
13727c478bd9Sstevel@tonic-gate 			char	path[MAXPATHLEN + 1];
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
13757c478bd9Sstevel@tonic-gate 			switch (iocp->ioc_cmd) {
13767c478bd9Sstevel@tonic-gate 			case CONSOPENPOLLEDIO:
13777c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
13787c478bd9Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
13797c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13807c478bd9Sstevel@tonic-gate 					    "keyboard is not available"
13817c478bd9Sstevel@tonic-gate 					    " for system debugging: %s",
13827c478bd9Sstevel@tonic-gate 					    path);
13837c478bd9Sstevel@tonic-gate 				break;
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 			case CONSCLOSEPOLLEDIO:
13867c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR,
13877c478bd9Sstevel@tonic-gate 				    path) == DDI_SUCCESS)
13887c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: "
13897c478bd9Sstevel@tonic-gate 					    "keyboard is not available:"
13907c478bd9Sstevel@tonic-gate 					    " %s", path);
13917c478bd9Sstevel@tonic-gate 				break;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 			default:
13947c478bd9Sstevel@tonic-gate 				break;
13957c478bd9Sstevel@tonic-gate 			}
13967c478bd9Sstevel@tonic-gate 			msg->kpm_req_nums --;
13977c478bd9Sstevel@tonic-gate 			lqs = lqs->lqs_next;
13987c478bd9Sstevel@tonic-gate 			retry = 0;
13997c478bd9Sstevel@tonic-gate 		}
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	if (msg->kpm_req_nums == 0) {
14037c478bd9Sstevel@tonic-gate 		conskbd_mux_dequeue_msg(msg);
14047c478bd9Sstevel@tonic-gate 		kmem_free(msg, sizeof (*msg));
14057c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate }	/* conskbd_handle_downstream_msg() */
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate static void
conskbd_ioc_plink(queue_t * q,mblk_t * mp)14127c478bd9Sstevel@tonic-gate conskbd_ioc_plink(queue_t *q, mblk_t *mp)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	mblk_t		*req;
14157c478bd9Sstevel@tonic-gate 	queue_t		*lowque;
14167c478bd9Sstevel@tonic-gate 	struct linkblk		*linkp;
14177c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	lqs = kmem_zalloc(sizeof (*lqs), KM_SLEEP);
14207c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_UNINITIALIZED);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
14237c478bd9Sstevel@tonic-gate 	lowque = linkp->l_qbot;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	lqs->lqs_queue = lowque;
14267c478bd9Sstevel@tonic-gate 	lqs->lqs_pending_plink = mp;
14277c478bd9Sstevel@tonic-gate 	lqs->lqs_pending_queue = q;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	req = mkiocb(CONSSETKBDTYPE);
14307c478bd9Sstevel@tonic-gate 	if (req == NULL) {
14317c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14327c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14337c478bd9Sstevel@tonic-gate 		return;
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	req->b_cont = allocb(sizeof (int), BPRI_MED);
14377c478bd9Sstevel@tonic-gate 	if (req->b_cont == NULL) {
14387c478bd9Sstevel@tonic-gate 		freemsg(req);
14397c478bd9Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
14407c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14417c478bd9Sstevel@tonic-gate 		return;
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 
144478575d6eScg149915 	lowque->q_ptr = lqs;
144578575d6eScg149915 	OTHERQ(lowque)->q_ptr = lqs;
14467c478bd9Sstevel@tonic-gate 	*(int *)req->b_cont->b_wptr = KB_USB;
14477c478bd9Sstevel@tonic-gate 	req->b_cont->b_wptr += sizeof (int);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	lqs->lqs_state = LQS_KIOCTYPE_ACK_PENDING;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	if (putq(lowque, req) != 1) {
14527c478bd9Sstevel@tonic-gate 		freemsg(req);
14537c478bd9Sstevel@tonic-gate 		miocnak(lqs->lqs_pending_queue,
14547c478bd9Sstevel@tonic-gate 		    lqs->lqs_pending_plink, 0, ENOMEM);
14557c478bd9Sstevel@tonic-gate 		lowque->q_ptr = NULL;
145678575d6eScg149915 		OTHERQ(lowque)->q_ptr = NULL;
14577c478bd9Sstevel@tonic-gate 		kmem_free(lqs, sizeof (*lqs));
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate }	/* conskbd_ioc_plink() */
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 
146378575d6eScg149915 static void
conskbd_ioc_punlink(queue_t * q,mblk_t * mp)146478575d6eScg149915 conskbd_ioc_punlink(queue_t *q, mblk_t *mp)
146578575d6eScg149915 {
146678575d6eScg149915 	int			index;
146778575d6eScg149915 	struct linkblk		*linkp;
146878575d6eScg149915 	conskbd_lower_queue_t	*lqs;
146978575d6eScg149915 	conskbd_lower_queue_t	*prev;
147078575d6eScg149915 
147178575d6eScg149915 	linkp = (struct linkblk *)mp->b_cont->b_rptr;
147278575d6eScg149915 	prev = conskbd.conskbd_lqueue_list;
147378575d6eScg149915 	for (lqs = prev; lqs; lqs = lqs->lqs_next) {
147478575d6eScg149915 		if (lqs->lqs_queue == linkp->l_qbot) {
147578575d6eScg149915 			if (prev == lqs)
147678575d6eScg149915 				conskbd.conskbd_lqueue_list =
147778575d6eScg149915 				    lqs->lqs_next;
147878575d6eScg149915 			else
147978575d6eScg149915 				prev->lqs_next = lqs->lqs_next;
148078575d6eScg149915 
148178575d6eScg149915 			lqs->lqs_queue->q_ptr =  NULL;
148278575d6eScg149915 			OTHERQ(lqs->lqs_queue)->q_ptr = NULL;
148378575d6eScg149915 			conskbd.conskbd_lqueue_nums --;
148478575d6eScg149915 			if (conskbd.conskbd_lqueue_nums == 0) {
148578575d6eScg149915 				kbd_layout_bak = conskbd.conskbd_layout;
148678575d6eScg149915 				conskbd.conskbd_layout = -1;
148778575d6eScg149915 			}
148878575d6eScg149915 
148978575d6eScg149915 			for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
149078575d6eScg149915 				if (lqs->lqs_key_state[index] == KEY_PRESSED)
149178575d6eScg149915 					kbtrans_streams_key(
149278575d6eScg149915 					    conskbd.conskbd_kbtrans,
149378575d6eScg149915 					    index,
149478575d6eScg149915 					    KEY_RELEASED);
149578575d6eScg149915 			}
149678575d6eScg149915 
149778575d6eScg149915 			kmem_free(lqs, sizeof (*lqs));
149878575d6eScg149915 			miocack(q, mp, 0, 0);
149978575d6eScg149915 			return;
150078575d6eScg149915 		}
150178575d6eScg149915 		prev = lqs;
150278575d6eScg149915 	}
150378575d6eScg149915 	miocnak(q, mp, 0, EINVAL);
150478575d6eScg149915 
150578575d6eScg149915 }	/* conskbd_ioc_punlink() */
150678575d6eScg149915 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * Every physical keyboard has a corresponding STREAMS queue. We call this
15097c478bd9Sstevel@tonic-gate  * queue lower queue. Every lower queue has a state, refer to conskbd.h file
15107c478bd9Sstevel@tonic-gate  * about "enum conskbd_lqs_state".
15117c478bd9Sstevel@tonic-gate  * The following routine is used to handle response messages from lower queue.
15127c478bd9Sstevel@tonic-gate  * When receiving ack/nak message from lower queue(s), the routine determines
15137c478bd9Sstevel@tonic-gate  * the passage for it according to the current state of this lower queue.
15147c478bd9Sstevel@tonic-gate  */
15157c478bd9Sstevel@tonic-gate static void
conskbd_lqs_ack_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)15167c478bd9Sstevel@tonic-gate conskbd_lqs_ack_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	switch (lqs->lqs_state) {
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	/* S6: working in virtual keyboard mode, multi-keyboards are usable */
15217c478bd9Sstevel@tonic-gate 	case LQS_INITIALIZED:
15227c478bd9Sstevel@tonic-gate 		conskbd_mux_upstream_msg(lqs, mp);
15237c478bd9Sstevel@tonic-gate 		break;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	/* S5: working in legacy mode, only one keyboard is usable */
15267c478bd9Sstevel@tonic-gate 	case LQS_INITIALIZED_LEGACY:
15277c478bd9Sstevel@tonic-gate 		conskbd_legacy_upstream_msg(lqs, mp);
15287c478bd9Sstevel@tonic-gate 		break;
15297c478bd9Sstevel@tonic-gate 
153099d47a04Slq150181 	/* S4: wait lower queue to acknowledge KIOCSLED/KIOCGLED  message */
15317c478bd9Sstevel@tonic-gate 	case LQS_KIOCSLED_ACK_PENDING:
15327c478bd9Sstevel@tonic-gate 		conskbd_kiocsled_complete(lqs, mp);
15337c478bd9Sstevel@tonic-gate 		break;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	/* S3: wait lower queue to acknowledge KIOCLAYOUT  message */
15367c478bd9Sstevel@tonic-gate 	case LQS_KIOCLAYOUT_ACK_PENDING:
15377c478bd9Sstevel@tonic-gate 		conskbd_kioclayout_complete(lqs, mp);
15387c478bd9Sstevel@tonic-gate 		break;
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	/* S2: wait lower queue to acknowledge KIOCTRANS  message */
15417c478bd9Sstevel@tonic-gate 	case LQS_KIOCTRANS_ACK_PENDING:
15427c478bd9Sstevel@tonic-gate 		conskbd_kioctrans_complete(lqs, mp);
15437c478bd9Sstevel@tonic-gate 		break;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	/* S1: wait lower queue to acknowledge KIOCTYPE  message */
15467c478bd9Sstevel@tonic-gate 	case LQS_KIOCTYPE_ACK_PENDING:
15477c478bd9Sstevel@tonic-gate 		conskbd_kioctype_complete(lqs, mp);
15487c478bd9Sstevel@tonic-gate 		break;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	/* if reaching here, there must be a error */
15517c478bd9Sstevel@tonic-gate 	default:
15527c478bd9Sstevel@tonic-gate 		freemsg(mp);
15537c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: lqs_ack_complete() state error");
15547c478bd9Sstevel@tonic-gate 		break;
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate }	/* conskbd_lqs_ack_complete() */
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate static void
conskbd_kioctype_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)15617c478bd9Sstevel@tonic-gate conskbd_kioctype_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
15647c478bd9Sstevel@tonic-gate 	mblk_t		*req;
15657c478bd9Sstevel@tonic-gate 	queue_t		*lowerque;
156678575d6eScg149915 	int		err = ENOMEM;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink);
15697c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTYPE_ACK_PENDING);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
15747c478bd9Sstevel@tonic-gate 	case M_IOCACK:
15757c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCTRANS);
15767c478bd9Sstevel@tonic-gate 		if (req == NULL) {
157778575d6eScg149915 			goto err_exit;
15787c478bd9Sstevel@tonic-gate 		}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
15817c478bd9Sstevel@tonic-gate 		if (req->b_cont == NULL) {
15827c478bd9Sstevel@tonic-gate 			freemsg(req);
158378575d6eScg149915 			goto err_exit;
15847c478bd9Sstevel@tonic-gate 		}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 		/* Set the translate mode to TR_UNTRANS_EVENT */
15877c478bd9Sstevel@tonic-gate 		*(int *)req->b_cont->b_wptr = TR_UNTRANS_EVENT;
15887c478bd9Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (int);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 		/* Ready to handle the response to KIOCTRANS */
15917c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCTRANS_ACK_PENDING;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 		if (putq(lowerque, req) != 1) {
15947c478bd9Sstevel@tonic-gate 			freemsg(req);
159578575d6eScg149915 			goto err_exit;
15967c478bd9Sstevel@tonic-gate 		}
159778575d6eScg149915 		freemsg(mp);
159878575d6eScg149915 		return;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
16017c478bd9Sstevel@tonic-gate 		/*
16027c478bd9Sstevel@tonic-gate 		 * The lower keyboard driver can't mimic USB keyboard,
16037c478bd9Sstevel@tonic-gate 		 * that's say, the physical keyboard is an old one, such
16047c478bd9Sstevel@tonic-gate 		 * as TYPE 3/4/5 one. In this case, the virtual keyboard
16057c478bd9Sstevel@tonic-gate 		 * is disabled, and the data from lower keyboard driver
16067c478bd9Sstevel@tonic-gate 		 * will bypass the conskbd module.
16077c478bd9Sstevel@tonic-gate 		 */
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 		/*
16107c478bd9Sstevel@tonic-gate 		 * if there is any other keyborad already linked under the
16117c478bd9Sstevel@tonic-gate 		 * conskbd, we reject the current one.
16127c478bd9Sstevel@tonic-gate 		 */
16137c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_lqueue_nums > 0) {
16147c478bd9Sstevel@tonic-gate 			iocp = (struct iocblk *)mp->b_rptr;
161578575d6eScg149915 			err = iocp->ioc_error;
161678575d6eScg149915 			goto err_exit;
161778575d6eScg149915 		}
161878575d6eScg149915 
161978575d6eScg149915 		/*
162078575d6eScg149915 		 * link this keyboard under conskbd.
162178575d6eScg149915 		 */
162278575d6eScg149915 		qwriter(lowerque, mp, conskbd_link_lowque_legacy, PERIM_OUTER);
162378575d6eScg149915 		return;
162478575d6eScg149915 	}
162578575d6eScg149915 
162678575d6eScg149915 err_exit:
162778575d6eScg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
16287c478bd9Sstevel@tonic-gate 	lowerque->q_ptr = NULL;
162978575d6eScg149915 	OTHERQ(lowerque)->q_ptr = NULL;
16307c478bd9Sstevel@tonic-gate 	kmem_free(lqs, sizeof (*lqs));
16317c478bd9Sstevel@tonic-gate 	freemsg(mp);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate }	/* conskbd_kioctype_complete() */
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate static void
conskbd_kioctrans_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)16367c478bd9Sstevel@tonic-gate conskbd_kioctrans_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
16397c478bd9Sstevel@tonic-gate 	mblk_t		*req;
16407c478bd9Sstevel@tonic-gate 	queue_t		*lowerque;
164178575d6eScg149915 	int		err = ENOMEM;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
16447c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCTRANS_ACK_PENDING);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	lowerque = lqs->lqs_queue;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
16497c478bd9Sstevel@tonic-gate 	case M_IOCACK:
16507c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCLAYOUT);
16517c478bd9Sstevel@tonic-gate 		if (req == NULL) {
165278575d6eScg149915 			goto err_exit;
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (int), BPRI_MED);
16567c478bd9Sstevel@tonic-gate 		if (req->b_cont == NULL) {
16577c478bd9Sstevel@tonic-gate 			freemsg(req);
165878575d6eScg149915 			goto err_exit;
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		/* waiting for response to KIOCLAYOUT */
16627c478bd9Sstevel@tonic-gate 		lqs->lqs_state = LQS_KIOCLAYOUT_ACK_PENDING;
16637c478bd9Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1) {
16647c478bd9Sstevel@tonic-gate 			freemsg(req);
166578575d6eScg149915 			goto err_exit;
16667c478bd9Sstevel@tonic-gate 		}
166778575d6eScg149915 		freemsg(mp);
166878575d6eScg149915 		return;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
16717c478bd9Sstevel@tonic-gate 		iocp = (struct iocblk *)mp->b_rptr;
167278575d6eScg149915 		err = iocp->ioc_error;
167378575d6eScg149915 		goto err_exit;
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
167678575d6eScg149915 err_exit:
167778575d6eScg149915 	miocnak(lqs->lqs_pending_queue, lqs->lqs_pending_plink, 0, err);
167878575d6eScg149915 	lowerque->q_ptr = NULL;
167978575d6eScg149915 	OTHERQ(lowerque)->q_ptr = NULL;
168078575d6eScg149915 	kmem_free(lqs, sizeof (*lqs));
16817c478bd9Sstevel@tonic-gate 	freemsg(mp);
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate }	/* conskbd_kioctrans_complete() */
16847c478bd9Sstevel@tonic-gate 
16857db6e34eSqz150045 /*
16867db6e34eSqz150045  * Allocate a firm event
16877db6e34eSqz150045  */
16887db6e34eSqz150045 static mblk_t *
conskbd_alloc_firm_event(ushort_t id,int value)16899ec394dbShh224818 conskbd_alloc_firm_event(ushort_t id, int value)
16907db6e34eSqz150045 {
16917db6e34eSqz150045 	mblk_t	*mb;
16927db6e34eSqz150045 	Firm_event *fe;
16937db6e34eSqz150045 
16947db6e34eSqz150045 	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
16957db6e34eSqz150045 		fe = (Firm_event *)mb->b_wptr;
16967db6e34eSqz150045 		fe->id = id;
16977db6e34eSqz150045 		fe->pair_type = FE_PAIR_NONE;
1698a897f299SToomas Soome 		fe->pair = '\0';
16997db6e34eSqz150045 		fe->value = value;
17007db6e34eSqz150045 		mb->b_wptr += sizeof (Firm_event);
17017db6e34eSqz150045 	}
17027db6e34eSqz150045 
17037db6e34eSqz150045 	return (mb);
17047db6e34eSqz150045 }
17057db6e34eSqz150045 
17067c478bd9Sstevel@tonic-gate static void
conskbd_kioclayout_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)17077c478bd9Sstevel@tonic-gate conskbd_kioclayout_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	mblk_t		*req;
17107c478bd9Sstevel@tonic-gate 	int		layout;
17117c478bd9Sstevel@tonic-gate 	boolean_t	fail;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
17147c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCLAYOUT_ACK_PENDING);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
17177c478bd9Sstevel@tonic-gate 	case M_IOCACK:
17187c478bd9Sstevel@tonic-gate 		if (miocpullup(mp, sizeof (int)) == 0) {
17197c478bd9Sstevel@tonic-gate 			layout = *(int *)mp->b_cont->b_rptr;
17207c478bd9Sstevel@tonic-gate 			/*
17217c478bd9Sstevel@tonic-gate 			 * We just accept the layout of the first keyboard
17227c478bd9Sstevel@tonic-gate 			 * requesting to be linked under conskbd. If current
17237c478bd9Sstevel@tonic-gate 			 * keyboard is the first one, and if we get right
17247c478bd9Sstevel@tonic-gate 			 * layout from it, we set conskbd's layout
17257c478bd9Sstevel@tonic-gate 			 */
17267db6e34eSqz150045 			if (layout != -1 && conskbd.conskbd_layout == -1) {
17277db6e34eSqz150045 				if (layout == 0) {
17287db6e34eSqz150045 					conskbd.conskbd_layout = kbd_layout_bak;
17297db6e34eSqz150045 				} else {
17307c478bd9Sstevel@tonic-gate 					conskbd.conskbd_layout = layout;
17317db6e34eSqz150045 					if (layout == kbd_layout_bak) {
17327db6e34eSqz150045 						break;
17337db6e34eSqz150045 					}
17347db6e34eSqz150045 					if ((req = conskbd_alloc_firm_event(
17357db6e34eSqz150045 					    KEYBOARD_LAYOUT_CHANGE,
17367db6e34eSqz150045 					    layout)) != NULL) {
1737b49b27deSqz150045 						if (conskbd.conskbd_directio) {
17387db6e34eSqz150045 							putnext(
17397db6e34eSqz150045 							    conskbd_regqueue,
17407db6e34eSqz150045 							    req);
1741b49b27deSqz150045 						} else if (conskbd_consqueue
1742b49b27deSqz150045 						    != NULL) {
17437db6e34eSqz150045 							putnext(
17447db6e34eSqz150045 							    conskbd_consqueue,
17457db6e34eSqz150045 							    req);
1746b49b27deSqz150045 						} else {
1747b49b27deSqz150045 							freemsg(req);
1748b49b27deSqz150045 						}
17497db6e34eSqz150045 					}
17507db6e34eSqz150045 				}
17517db6e34eSqz150045 			}
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 		break;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/* if fail, leave conskbd's layout as it is */
17577c478bd9Sstevel@tonic-gate 	case M_IOCNAK:
17587c478bd9Sstevel@tonic-gate 		break;
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	fail = B_TRUE;
176299d47a04Slq150181 
176399d47a04Slq150181 	if (conskbd.conskbd_led_state == -1)
176499d47a04Slq150181 		req = mkiocb(KIOCGLED);
176599d47a04Slq150181 	else
17667c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
176799d47a04Slq150181 
17687c478bd9Sstevel@tonic-gate 	if (req) {
17697c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
17707c478bd9Sstevel@tonic-gate 		if (req->b_cont) {
177199d47a04Slq150181 			if (conskbd.conskbd_led_state != -1) {
17727c478bd9Sstevel@tonic-gate 				*(uchar_t *)req->b_cont->b_wptr =
17737c478bd9Sstevel@tonic-gate 				    conskbd.conskbd_led_state;
17747c478bd9Sstevel@tonic-gate 				req->b_cont->b_wptr += sizeof (uchar_t);
177599d47a04Slq150181 			}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			/* waiting for response to KIOCSLED */
17787c478bd9Sstevel@tonic-gate 			lqs->lqs_state = LQS_KIOCSLED_ACK_PENDING;
17797c478bd9Sstevel@tonic-gate 			if (putq(lqs->lqs_queue, req) == 1) {
17807c478bd9Sstevel@tonic-gate 				fail = B_FALSE;
17817c478bd9Sstevel@tonic-gate 			} else {
17827c478bd9Sstevel@tonic-gate 				freemsg(req);
17837c478bd9Sstevel@tonic-gate 			}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		} else {
17867c478bd9Sstevel@tonic-gate 			freemsg(req);
17877c478bd9Sstevel@tonic-gate 		}
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if (fail) {
17917c478bd9Sstevel@tonic-gate 		/*
179299d47a04Slq150181 		 * If fail to allocate KIOCSLED/KIOCGLED message or put
179399d47a04Slq150181 		 * the message into lower queue, we immediately link
179499d47a04Slq150181 		 * current keyboard under conskbd. Thus, even if fails
179599d47a04Slq150181 		 * to set/get LED, this keyboard could be available.
17967c478bd9Sstevel@tonic-gate 		 */
179778575d6eScg149915 		qwriter(lqs->lqs_queue,
179878575d6eScg149915 		    mp, conskbd_link_lowque_virt, PERIM_OUTER);
179978575d6eScg149915 	} else {
180078575d6eScg149915 		freemsg(mp);
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate }	/* conskbd_kioclayout_complete() */
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate static void
conskbd_kiocsled_complete(conskbd_lower_queue_t * lqs,mblk_t * mp)18077c478bd9Sstevel@tonic-gate conskbd_kiocsled_complete(conskbd_lower_queue_t *lqs, mblk_t *mp)
18087c478bd9Sstevel@tonic-gate {
180999d47a04Slq150181 	int	led_state;
181099d47a04Slq150181 
18117c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
18127c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_KIOCSLED_ACK_PENDING);
18137c478bd9Sstevel@tonic-gate 
181499d47a04Slq150181 	if (conskbd.conskbd_led_state == -1) {
181599d47a04Slq150181 		switch (mp->b_datap->db_type) {
181699d47a04Slq150181 		case M_IOCACK:
181799d47a04Slq150181 			if (miocpullup(mp, sizeof (uchar_t)) == 0) {
181899d47a04Slq150181 				led_state = *(uchar_t *)mp->b_cont->b_rptr;
181999d47a04Slq150181 				conskbd.conskbd_led_state = led_state;
182099d47a04Slq150181 				kbtrans_streams_setled(conskbd.conskbd_kbtrans,
182199d47a04Slq150181 				    led_state);
182299d47a04Slq150181 			}
182399d47a04Slq150181 			break;
182499d47a04Slq150181 
182599d47a04Slq150181 		/* if fail, leave conskbd's led_state as it is */
182699d47a04Slq150181 		case M_IOCNAK:
182799d47a04Slq150181 			break;
182899d47a04Slq150181 		}
182999d47a04Slq150181 	}
183099d47a04Slq150181 
18317c478bd9Sstevel@tonic-gate 	/*
183299d47a04Slq150181 	 * Basically, failure of setting/getting LED is not a fatal
183399d47a04Slq150181 	 * error, so we will plumb the lower queue into conskbd whether
183499d47a04Slq150181 	 * setting/getting LED succeeds or fails.
18357c478bd9Sstevel@tonic-gate 	 */
183678575d6eScg149915 	qwriter(lqs->lqs_queue, mp, conskbd_link_lowque_virt, PERIM_OUTER);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate }	/* conskbd_kiocsled_complete() */
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate static void
conskbd_mux_upstream_msg(conskbd_lower_queue_t * lqs,mblk_t * mp)18427c478bd9Sstevel@tonic-gate conskbd_mux_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
18437c478bd9Sstevel@tonic-gate {
18447c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
18457c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
18467c478bd9Sstevel@tonic-gate 	int			error;
18477c478bd9Sstevel@tonic-gate 	dev_t			devt;
18487c478bd9Sstevel@tonic-gate 	char			path[MAXPATHLEN + 1];
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_state == LQS_INITIALIZED);
18517c478bd9Sstevel@tonic-gate 	msg = conskbd_mux_find_msg(mp);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (!msg) {
18547c478bd9Sstevel@tonic-gate 		/*
185578575d6eScg149915 		 * Here we discard the response if:
185678575d6eScg149915 		 *
185778575d6eScg149915 		 *   1. It's an KIOCSLED request; see conskbd_streams_setled().
185878575d6eScg149915 		 *   2. The application has already closed the upper stream;
185978575d6eScg149915 		 *		see conskbdclose()
18607c478bd9Sstevel@tonic-gate 		 */
18617c478bd9Sstevel@tonic-gate 		freemsg(mp);
18627c478bd9Sstevel@tonic-gate 		return;
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * We use the b_next field of mblk_t structure to link all
18677c478bd9Sstevel@tonic-gate 	 * response coming from lower queues into a linkage list,
18687c478bd9Sstevel@tonic-gate 	 * and make use of the b_prev field to save a pointer to
18697c478bd9Sstevel@tonic-gate 	 * the lower queue from which the current response message
18707c478bd9Sstevel@tonic-gate 	 * comes.
18717c478bd9Sstevel@tonic-gate 	 */
18727c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
18737c478bd9Sstevel@tonic-gate 	mutex_enter(&msg->kpm_lock);
18747c478bd9Sstevel@tonic-gate 	mp->b_next = msg->kpm_resp_list;
18757c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)lqs;
18767c478bd9Sstevel@tonic-gate 	msg->kpm_resp_list = mp;
18777c478bd9Sstevel@tonic-gate 	msg->kpm_resp_nums ++;
18787c478bd9Sstevel@tonic-gate 
1879b94a3996Slq150181 	if (msg->kpm_resp_nums < msg->kpm_req_nums) {
1880b94a3996Slq150181 		mutex_exit(&msg->kpm_lock);
18817c478bd9Sstevel@tonic-gate 		return;
1882b94a3996Slq150181 	}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	ASSERT(msg->kpm_resp_nums == msg->kpm_req_nums);
18857c478bd9Sstevel@tonic-gate 	ASSERT(mp == msg->kpm_resp_list);
18867c478bd9Sstevel@tonic-gate 
1887b94a3996Slq150181 	mutex_exit(&msg->kpm_lock);
1888b94a3996Slq150181 
18897c478bd9Sstevel@tonic-gate 	conskbd_mux_dequeue_msg(msg);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	/*
18937c478bd9Sstevel@tonic-gate 	 * Here, we have the policy that, if any one lower queue ACK
18947c478bd9Sstevel@tonic-gate 	 * our reuqest, then we return ACK to upstreams; only if all
18957c478bd9Sstevel@tonic-gate 	 * lower queues NAK our request, we return NAK to upstreams.
18967c478bd9Sstevel@tonic-gate 	 * if all responses are nak, the errno of the  first response
18977c478bd9Sstevel@tonic-gate 	 * is sent to upstreams
18987c478bd9Sstevel@tonic-gate 	 */
18997c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_rptr);
19007c478bd9Sstevel@tonic-gate 	error = ((struct iocblk *)mp->b_rptr)->ioc_error;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	switch (msg->kpm_req_cmd) {
19037c478bd9Sstevel@tonic-gate 	case CONSOPENPOLLEDIO:
19047c478bd9Sstevel@tonic-gate 		/*
19057c478bd9Sstevel@tonic-gate 		 * Here, we can safely ignore the NAK message. If any one lower
19067c478bd9Sstevel@tonic-gate 		 * queue returns NAK, the pointer to the corresponding polledio
19077c478bd9Sstevel@tonic-gate 		 * structure will remain null, that's say lqs->lqs_polledio =
19087c478bd9Sstevel@tonic-gate 		 * null. When we need to invoke polled I/O interface, we will
19097c478bd9Sstevel@tonic-gate 		 * check if the pointer is null.
19107c478bd9Sstevel@tonic-gate 		 */
19117c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19127c478bd9Sstevel@tonic-gate 			cons_polledio_t		*polledio;
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19157c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
19167c478bd9Sstevel@tonic-gate 			devt = lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
19177c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
19187c478bd9Sstevel@tonic-gate 				polledio = *(struct cons_polledio **)
19197c478bd9Sstevel@tonic-gate 				    mp->b_cont->b_rptr;
19207c478bd9Sstevel@tonic-gate 				if (polledio->cons_polledio_version ==
19217c478bd9Sstevel@tonic-gate 				    CONSPOLLEDIO_V1) {
19227c478bd9Sstevel@tonic-gate 					lqs->lqs_polledio = polledio;
19237c478bd9Sstevel@tonic-gate 					error = 0;
19247c478bd9Sstevel@tonic-gate 				} else {
19257c478bd9Sstevel@tonic-gate 					/*
19267c478bd9Sstevel@tonic-gate 					 * USB and PS2 keyboard drivers should
19277c478bd9Sstevel@tonic-gate 					 * use the same cons_polledio structure
19287c478bd9Sstevel@tonic-gate 					 * as conskbd.
19297c478bd9Sstevel@tonic-gate 					 */
19307c478bd9Sstevel@tonic-gate 					if (ddi_dev_pathname(devt, S_IFCHR,
19317c478bd9Sstevel@tonic-gate 					    path) == DDI_SUCCESS) {
19327c478bd9Sstevel@tonic-gate 						cmn_err(CE_WARN, "keyboard "
19337c478bd9Sstevel@tonic-gate 						    "driver does not support "
19347c478bd9Sstevel@tonic-gate 						    "system debugging: %s",
19357c478bd9Sstevel@tonic-gate 						    path);
19367c478bd9Sstevel@tonic-gate 					}
19377c478bd9Sstevel@tonic-gate 					error = EINVAL;
19387c478bd9Sstevel@tonic-gate 				}
19397c478bd9Sstevel@tonic-gate 			} else {
19407c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
19417c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
19427c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
19437c478bd9Sstevel@tonic-gate 					    " not available for system"
19447c478bd9Sstevel@tonic-gate 					    " debugging:  %s", path);
19457c478bd9Sstevel@tonic-gate 				}
19467c478bd9Sstevel@tonic-gate 			}
19477c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
19487c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
19497c478bd9Sstevel@tonic-gate 			freemsg(mp);
19507c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19517c478bd9Sstevel@tonic-gate 		}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 		mp = msg->kpm_req_msg;
19547c478bd9Sstevel@tonic-gate 		if (error == 0) {
19557c478bd9Sstevel@tonic-gate 			*(struct cons_polledio **)mp->b_cont->b_rptr =
19567c478bd9Sstevel@tonic-gate 			    &conskbd.conskbd_polledio;
19577c478bd9Sstevel@tonic-gate 		}
19587c478bd9Sstevel@tonic-gate 		break;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	case CONSCLOSEPOLLEDIO:
19617c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19627c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19637c478bd9Sstevel@tonic-gate 			lqs = (conskbd_lower_queue_t *)mp->b_prev;
19647c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK) {
19657c478bd9Sstevel@tonic-gate 				lqs->lqs_polledio = NULL;
19667c478bd9Sstevel@tonic-gate 				error = 0;
19677c478bd9Sstevel@tonic-gate 			} else {
19687c478bd9Sstevel@tonic-gate 				devt =
19697c478bd9Sstevel@tonic-gate 				    lqs->lqs_queue->q_stream->sd_vnode->v_rdev;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 				if (ddi_dev_pathname(devt, S_IFCHR, path) ==
19727c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
19737c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, "conskbd: keyboard is"
19747c478bd9Sstevel@tonic-gate 					    " not available: %s", path);
19757c478bd9Sstevel@tonic-gate 				}
19767c478bd9Sstevel@tonic-gate 			}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
19797c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
19807c478bd9Sstevel@tonic-gate 			freemsg(mp);
19817c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19827c478bd9Sstevel@tonic-gate 		}
19837c478bd9Sstevel@tonic-gate 		break;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	case KIOCCMD:
1986c35aa225Smarx 	case KIOCMKTONE:
19877c478bd9Sstevel@tonic-gate 		for (mp = msg->kpm_resp_list; mp; ) {
19887c478bd9Sstevel@tonic-gate 			msg->kpm_resp_list = mp->b_next;
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 			if (mp->b_datap->db_type == M_IOCACK)
19917c478bd9Sstevel@tonic-gate 				error = 0;
19927c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
19937c478bd9Sstevel@tonic-gate 			mp->b_prev = NULL;
19947c478bd9Sstevel@tonic-gate 			freemsg(mp);
19957c478bd9Sstevel@tonic-gate 			mp = msg->kpm_resp_list;
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 		break;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	default:  /* it is impossible to reach here */
20007c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "conskbd: unexpected ioctl reply");
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	mp = msg->kpm_req_msg;
20047c478bd9Sstevel@tonic-gate 	if (error == 0) {
20057c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCACK;
20067c478bd9Sstevel@tonic-gate 	} else {
20077c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_IOCNAK;
20087c478bd9Sstevel@tonic-gate 	}
20097c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
20107c478bd9Sstevel@tonic-gate 	iocp->ioc_error = error;
20117c478bd9Sstevel@tonic-gate 	qreply(msg->kpm_upper_queue, mp);
20127c478bd9Sstevel@tonic-gate 	mutex_destroy(&msg->kpm_lock);
20137c478bd9Sstevel@tonic-gate 	kmem_free(msg, sizeof (*msg));
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate }	/* conskbd_mux_upstream_msg() */
20167c478bd9Sstevel@tonic-gate 
201778575d6eScg149915 static void
conskbd_link_lowque_legacy(queue_t * lowque,mblk_t * mp)201878575d6eScg149915 conskbd_link_lowque_legacy(queue_t *lowque, mblk_t *mp)
201978575d6eScg149915 {
202078575d6eScg149915 	conskbd_lower_queue_t *lqs;
202178575d6eScg149915 
202278575d6eScg149915 	freemsg(mp);
202378575d6eScg149915 
202478575d6eScg149915 	/*
202578575d6eScg149915 	 * Bypass the virutal keyboard for old hardware,
202678575d6eScg149915 	 * Now, only current legacy keyboard can be linked
202778575d6eScg149915 	 * under conskbd
202878575d6eScg149915 	 */
202978575d6eScg149915 	conskbd.conskbd_bypassed = B_TRUE;
203078575d6eScg149915 
203178575d6eScg149915 	/*
203278575d6eScg149915 	 * Link the lower queue under conskbd
203378575d6eScg149915 	 */
203478575d6eScg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
203578575d6eScg149915 	lqs->lqs_state = LQS_INITIALIZED_LEGACY;
203678575d6eScg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
203778575d6eScg149915 	conskbd.conskbd_lqueue_list = lqs;
203878575d6eScg149915 	conskbd.conskbd_lqueue_nums++;
203978575d6eScg149915 
204078575d6eScg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
204178575d6eScg149915 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
204278575d6eScg149915 
204378575d6eScg149915 }	/* conskbd_link_lowque_legacy() */
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate static void
conskbd_link_lowque_virt(queue_t * lowque,mblk_t * mp)204678575d6eScg149915 conskbd_link_lowque_virt(queue_t *lowque, mblk_t *mp)
20477c478bd9Sstevel@tonic-gate {
20487c478bd9Sstevel@tonic-gate 	int		index;
204978575d6eScg149915 	conskbd_lower_queue_t *lqs;
20507c478bd9Sstevel@tonic-gate 
205178575d6eScg149915 	freemsg(mp);
205278575d6eScg149915 
205378575d6eScg149915 	lqs = (conskbd_lower_queue_t *)lowque->q_ptr;
205478575d6eScg149915 
205578575d6eScg149915 	ASSERT(lqs->lqs_queue == lowque);
20567c478bd9Sstevel@tonic-gate 	ASSERT(lqs->lqs_pending_plink != NULL);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	/*
20597c478bd9Sstevel@tonic-gate 	 * Now, link the lower queue under conskbd
20607c478bd9Sstevel@tonic-gate 	 */
20617c478bd9Sstevel@tonic-gate 	for (index = 0; index < KBTRANS_KEYNUMS_MAX; index ++) {
20627c478bd9Sstevel@tonic-gate 		lqs->lqs_key_state[index] = KEY_RELEASED;
20637c478bd9Sstevel@tonic-gate 	}
206478575d6eScg149915 	lqs->lqs_next = conskbd.conskbd_lqueue_list;
20657c478bd9Sstevel@tonic-gate 	lqs->lqs_state = LQS_INITIALIZED;
206678575d6eScg149915 	conskbd.conskbd_lqueue_nums++;
206778575d6eScg149915 	conskbd.conskbd_lqueue_list = lqs;
206878575d6eScg149915 	mioc2ack(lqs->lqs_pending_plink, NULL, 0, 0);
20697c478bd9Sstevel@tonic-gate 	qreply(lqs->lqs_pending_queue, lqs->lqs_pending_plink);
20707c478bd9Sstevel@tonic-gate 
207178575d6eScg149915 }	/* conskbd_link_lowque_virt() */
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20747c478bd9Sstevel@tonic-gate static void
conskbd_legacy_upstream_msg(conskbd_lower_queue_t * lqs,mblk_t * mp)20757c478bd9Sstevel@tonic-gate conskbd_legacy_upstream_msg(conskbd_lower_queue_t *lqs, mblk_t *mp)
20767c478bd9Sstevel@tonic-gate {
20777c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	ASSERT(lqs && lqs->lqs_state == LQS_INITIALIZED_LEGACY);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	/*
20827c478bd9Sstevel@tonic-gate 	 * We assume that all of the ioctls are headed to the
20837c478bd9Sstevel@tonic-gate 	 * conskbd_regqueue if it is open.  We are intercepting a few ioctls
20847c478bd9Sstevel@tonic-gate 	 * that we know belong to conskbd_consqueue, and sending them there.
20857c478bd9Sstevel@tonic-gate 	 * Any other, new ioctls that have to be routed to conskbd_consqueue
20867c478bd9Sstevel@tonic-gate 	 * should be added to this list.
20877c478bd9Sstevel@tonic-gate 	 */
20887c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if ((iocp->ioc_cmd == CONSOPENPOLLEDIO) ||
20917c478bd9Sstevel@tonic-gate 	    (iocp->ioc_cmd == CONSCLOSEPOLLEDIO)) {
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
20947c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: "
20957c478bd9Sstevel@tonic-gate 		    "CONSOPEN/CLOSEPOLLEDIO ACK/NAK\n"));
20967c478bd9Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	} else if (conskbd_regqueue != NULL) {
20997c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
21007c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_regqueue != NULL"));
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 		putnext(conskbd_regqueue, mp);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	} else if (conskbd_consqueue != NULL) {
21057c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_ALL,
21067c478bd9Sstevel@tonic-gate 		    ("conskbd_legacy_upstream_msg: conskbd_consqueue != NULL"));
21077c478bd9Sstevel@tonic-gate 		putnext(conskbd_consqueue, mp);
21087c478bd9Sstevel@tonic-gate 	} else {
21097c478bd9Sstevel@tonic-gate 		/* if reached here, it must be a error */
21107c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
21117c478bd9Sstevel@tonic-gate 		    "kb:  no destination for IOCACK/IOCNAK!");
21127c478bd9Sstevel@tonic-gate 		freemsg(mp);
21137c478bd9Sstevel@tonic-gate 	}
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate }	/* conskbd_legacy_upstream_msg() */
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate /*
21187c478bd9Sstevel@tonic-gate  * This routine is a callback routine for kbtrans module to set LED.
21197c478bd9Sstevel@tonic-gate  * Kbtrans will invoke it in two cases:
21207c478bd9Sstevel@tonic-gate  *
21217c478bd9Sstevel@tonic-gate  * 1) application initiated request
21227c478bd9Sstevel@tonic-gate  *	A KIOCSLED ioctl is sent by an application. The ioctl will be
21237c478bd9Sstevel@tonic-gate  *	be prcoessed by queue service procedure conskbduwsrv(), which
21247c478bd9Sstevel@tonic-gate  *	in turn calls kbtrans to process the ioctl. Then kbtrans invokes
21257c478bd9Sstevel@tonic-gate  *	conskbd_streams_setled() to set LED, after that,  kbtrans will
21267c478bd9Sstevel@tonic-gate  *	return an ACK message to upper module.
21277c478bd9Sstevel@tonic-gate  *
21287c478bd9Sstevel@tonic-gate  * 2) Kbtrans initiated the request
21297c478bd9Sstevel@tonic-gate  *	When conskbd works in TR_ASCII translation mode, if anyone of
21307c478bd9Sstevel@tonic-gate  *	CapsLock, NumberLock and Compose keys is pressed, kbtrans need
21317c478bd9Sstevel@tonic-gate  *	to set LED. In this case, there is no ioctl from upper module.
21327c478bd9Sstevel@tonic-gate  *	There is no requirement to send response to somebody.
21337c478bd9Sstevel@tonic-gate  *
21347c478bd9Sstevel@tonic-gate  * In first case, kbtrans will send response to upper module; and in the
21357c478bd9Sstevel@tonic-gate  * second, we don't need to send response. So conskbd_streams_setled()
21367c478bd9Sstevel@tonic-gate  * has no return value.
21377c478bd9Sstevel@tonic-gate  */
21387c478bd9Sstevel@tonic-gate static void
conskbd_streams_setled(struct kbtrans_hardware * hw,int led_state)21397c478bd9Sstevel@tonic-gate conskbd_streams_setled(struct kbtrans_hardware *hw, int led_state)
21407c478bd9Sstevel@tonic-gate {
21417c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21427c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t *lqs;
21437c478bd9Sstevel@tonic-gate 	mblk_t		*req;
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	ASSERT(&conskbd == conskbdp);
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	if (led_state == -1)
21487c478bd9Sstevel@tonic-gate 		return;
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	conskbdp->conskbd_led_state = led_state;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	/*
21537c478bd9Sstevel@tonic-gate 	 * Basically, failing to set LED is not a fatal error, we just skip
21547c478bd9Sstevel@tonic-gate 	 * it if this happens.
21557c478bd9Sstevel@tonic-gate 	 */
21567c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21577c478bd9Sstevel@tonic-gate 		req = mkiocb(KIOCSLED);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 		if (!req) {
21607c478bd9Sstevel@tonic-gate 			continue;
21617c478bd9Sstevel@tonic-gate 		}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 		req->b_cont = allocb(sizeof (uchar_t), BPRI_MED);
21647c478bd9Sstevel@tonic-gate 		if (!req->b_cont) {
21657c478bd9Sstevel@tonic-gate 			freemsg(req);
21667c478bd9Sstevel@tonic-gate 			continue;
21677c478bd9Sstevel@tonic-gate 		}
21687c478bd9Sstevel@tonic-gate 		*(uchar_t *)req->b_cont->b_wptr = led_state;
21697c478bd9Sstevel@tonic-gate 		req->b_cont->b_wptr += sizeof (uchar_t);
21707c478bd9Sstevel@tonic-gate 		if (putq(lqs->lqs_queue, req) != 1)
21717c478bd9Sstevel@tonic-gate 			freemsg(req);
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate }	/* conskbd_streams_setled() */
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate static void
conskbd_polledio_setled(struct kbtrans_hardware * hw,int led_state)21777c478bd9Sstevel@tonic-gate conskbd_polledio_setled(struct kbtrans_hardware *hw, int led_state)
21787c478bd9Sstevel@tonic-gate {
21797c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21807c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
21817c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
21847c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
21857c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_setled != NULL)) {
21867c478bd9Sstevel@tonic-gate 			cb->cons_polledio_setled(cb->cons_polledio_argument,
21877c478bd9Sstevel@tonic-gate 			    led_state);
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_setled() */
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate static boolean_t
conskbd_polled_keycheck(struct kbtrans_hardware * hw,kbtrans_key_t * keycode,enum keystate * state)21947c478bd9Sstevel@tonic-gate conskbd_polled_keycheck(struct kbtrans_hardware *hw,
21957c478bd9Sstevel@tonic-gate     kbtrans_key_t *keycode, enum keystate *state)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp = (conskbd_state_t *)hw;
21987c478bd9Sstevel@tonic-gate 	struct cons_polledio	*cb;
21997c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
22007c478bd9Sstevel@tonic-gate 	boolean_t	ret = B_FALSE;
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	for (ret = B_FALSE, lqs = conskbdp->conskbd_lqueue_list; lqs != NULL;
22037c478bd9Sstevel@tonic-gate 	    lqs = lqs->lqs_next) {
22047c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
22057c478bd9Sstevel@tonic-gate 		if ((cb != NULL) &&
22067c478bd9Sstevel@tonic-gate 		    (cb->cons_polledio_keycheck != NULL)) {
22077c478bd9Sstevel@tonic-gate 			ret = cb->cons_polledio_keycheck(
22087c478bd9Sstevel@tonic-gate 			    cb->cons_polledio_argument, keycode, state);
22097c478bd9Sstevel@tonic-gate 		}
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 		/* Get a char from lower queue(hardware) ? */
22127c478bd9Sstevel@tonic-gate 		if (ret == B_TRUE) {
2213ace75d0bSlq150181 
2214ace75d0bSlq150181 			/* A legacy keyboard ? */
2215ace75d0bSlq150181 			if (conskbd.conskbd_bypassed == B_TRUE)
2216ace75d0bSlq150181 				break;
2217ace75d0bSlq150181 
2218ace75d0bSlq150181 			/*
2219ace75d0bSlq150181 			 * This is the PS2 scancode 0x2B -> USB(49) /
2220ace75d0bSlq150181 			 * USB(50) keycode mapping workaround, for
2221ace75d0bSlq150181 			 * polled mode.
2222ace75d0bSlq150181 			 *
2223ace75d0bSlq150181 			 * There are two possible USB keycode mappings
2224ace75d0bSlq150181 			 * for PS2 scancode 0x2B and this workaround
2225ace75d0bSlq150181 			 * makes sure that we use the USB keycode that
2226ace75d0bSlq150181 			 * does not end up being mapped to a HOLE key
2227ace75d0bSlq150181 			 * using the current keyboard translation
2228ace75d0bSlq150181 			 * tables.
2229ace75d0bSlq150181 			 *
2230ace75d0bSlq150181 			 * See conskbdlrput() for a detailed
2231ace75d0bSlq150181 			 * explanation of the problem.
2232ace75d0bSlq150181 			 */
2233ace75d0bSlq150181 			if (*keycode == 49 || *keycode == 50) {
2234ace75d0bSlq150181 				if (conskbd_keyindex->k_normal[50] == HOLE)
2235ace75d0bSlq150181 					*keycode = 49;
2236ace75d0bSlq150181 				else
2237ace75d0bSlq150181 					*keycode = 50;
2238ace75d0bSlq150181 			}
2239ace75d0bSlq150181 
22407c478bd9Sstevel@tonic-gate 			break;
22417c478bd9Sstevel@tonic-gate 		}
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	return (ret);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate }	/* conskbd_polled_keycheck() */
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate static boolean_t
conskbd_override_kbtrans(queue_t * q,mblk_t * mp)22497c478bd9Sstevel@tonic-gate conskbd_override_kbtrans(queue_t *q, mblk_t *mp)
22507c478bd9Sstevel@tonic-gate {
22517c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
22527c478bd9Sstevel@tonic-gate 	int		directio;
22537c478bd9Sstevel@tonic-gate 	int		error;
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	if (mp->b_datap->db_type != M_IOCTL)
22567c478bd9Sstevel@tonic-gate 		return (B_FALSE);
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
22617c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT: {
22627c478bd9Sstevel@tonic-gate 		/*
22637c478bd9Sstevel@tonic-gate 		 * Don't let the kbtrans-based code see this; it will
22647c478bd9Sstevel@tonic-gate 		 * respond incorrectly.
22657c478bd9Sstevel@tonic-gate 		 */
22667c478bd9Sstevel@tonic-gate 		register mblk_t *datap;
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 		if ((datap = allocb((int)sizeof (int), BPRI_MED)) == NULL) {
22697c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, ENOMEM);
22707c478bd9Sstevel@tonic-gate 			return (B_TRUE);
22717c478bd9Sstevel@tonic-gate 		}
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = conskbd.conskbd_directio;
22747c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
22757c478bd9Sstevel@tonic-gate 		if (mp->b_cont) {
22767c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
22777c478bd9Sstevel@tonic-gate 			mp->b_cont = NULL;
22787c478bd9Sstevel@tonic-gate 		}
22797c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
22807c478bd9Sstevel@tonic-gate 		miocack(q, mp, sizeof (int), 0);
22817c478bd9Sstevel@tonic-gate 		return (B_TRUE);
22827c478bd9Sstevel@tonic-gate 	}
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
22857c478bd9Sstevel@tonic-gate 		/*
22867c478bd9Sstevel@tonic-gate 		 * Peek at this, set our variables, and then let the kbtrans
22877c478bd9Sstevel@tonic-gate 		 * based code see it and respond to it.
22887c478bd9Sstevel@tonic-gate 		 */
22897c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (int));
22907c478bd9Sstevel@tonic-gate 		if (error != 0) {
22917c478bd9Sstevel@tonic-gate 			return (B_FALSE);
22927c478bd9Sstevel@tonic-gate 		}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 		directio = *(int *)mp->b_cont->b_rptr;
22957c478bd9Sstevel@tonic-gate 		if (directio != 0 && directio != 1) {
22967c478bd9Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
22977c478bd9Sstevel@tonic-gate 			return (B_TRUE);
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 		conskbd.conskbd_directio = directio;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		if (conskbd.conskbd_directio) {
23027c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
23037c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_regqueue);
23047c478bd9Sstevel@tonic-gate 		} else {
23057c478bd9Sstevel@tonic-gate 			kbtrans_streams_set_queue(
23067c478bd9Sstevel@tonic-gate 			    conskbd.conskbd_kbtrans, conskbd_consqueue);
23077c478bd9Sstevel@tonic-gate 		}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		/*
23107c478bd9Sstevel@tonic-gate 		 * Let the kbtrans-based code see this and respond to it.
23117c478bd9Sstevel@tonic-gate 		 */
23127c478bd9Sstevel@tonic-gate 		return (B_FALSE);
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	default:
23157c478bd9Sstevel@tonic-gate 		return (B_FALSE);
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate }	/* conskbd_override_kbtrans() */
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate static void
conskbd_polledio_enter(cons_polledio_arg_t arg)2322281f0747Slt200341 conskbd_polledio_enter(cons_polledio_arg_t arg)
23237c478bd9Sstevel@tonic-gate {
23247c478bd9Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
23257c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
23267c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23297c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
23307c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
23317c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_enter != NULL)) {
23327c478bd9Sstevel@tonic-gate 			cb->cons_polledio_enter(cb->cons_polledio_argument);
23337c478bd9Sstevel@tonic-gate 		}
23347c478bd9Sstevel@tonic-gate 	}
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_enter() */
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate static void
conskbd_polledio_exit(cons_polledio_arg_t arg)2339281f0747Slt200341 conskbd_polledio_exit(cons_polledio_arg_t arg)
23407c478bd9Sstevel@tonic-gate {
23417c478bd9Sstevel@tonic-gate 	conskbd_state_t		*conskbdp;
23427c478bd9Sstevel@tonic-gate 	struct cons_polledio		*cb;
23437c478bd9Sstevel@tonic-gate 	conskbd_lower_queue_t	*lqs;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23467c478bd9Sstevel@tonic-gate 	for (lqs = conskbdp->conskbd_lqueue_list; lqs; lqs = lqs->lqs_next) {
23477c478bd9Sstevel@tonic-gate 		cb = lqs->lqs_polledio;
23487c478bd9Sstevel@tonic-gate 		if ((cb != NULL) && (cb->cons_polledio_exit != NULL)) {
23497c478bd9Sstevel@tonic-gate 			cb->cons_polledio_exit(cb->cons_polledio_argument);
23507c478bd9Sstevel@tonic-gate 		}
23517c478bd9Sstevel@tonic-gate 	}
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_exit() */
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate static int
conskbd_polledio_getchar(cons_polledio_arg_t arg)2356281f0747Slt200341 conskbd_polledio_getchar(cons_polledio_arg_t arg)
23577c478bd9Sstevel@tonic-gate {
23587c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	return (kbtrans_getchar(conskbdp->conskbd_kbtrans));
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_getchar() */
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate static int
conskbd_polledio_ischar(cons_polledio_arg_t arg)2367281f0747Slt200341 conskbd_polledio_ischar(cons_polledio_arg_t arg)
23687c478bd9Sstevel@tonic-gate {
23697c478bd9Sstevel@tonic-gate 	conskbd_state_t  *conskbdp;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	conskbdp = (conskbd_state_t *)arg;
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	return (kbtrans_ischar(conskbdp->conskbd_kbtrans));
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate }	/* conskbd_polledio_ischar() */
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate static void
conskbd_mux_enqueue_msg(conskbd_pending_msg_t * msg)23797c478bd9Sstevel@tonic-gate conskbd_mux_enqueue_msg(conskbd_pending_msg_t *msg)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23827c478bd9Sstevel@tonic-gate 	msg->kpm_next = conskbd_msg_queue;
23837c478bd9Sstevel@tonic-gate 	conskbd_msg_queue = msg;
23847c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate }	/* conskbd_mux_enqueue_msg() */
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate /*
23897c478bd9Sstevel@tonic-gate  * the messages in conskbd_msg_queue we just enqueue
23907c478bd9Sstevel@tonic-gate  */
23917c478bd9Sstevel@tonic-gate static conskbd_pending_msg_t *
conskbd_mux_find_msg(mblk_t * mp)23927c478bd9Sstevel@tonic-gate conskbd_mux_find_msg(mblk_t *mp)
23937c478bd9Sstevel@tonic-gate {
23947c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t	*msg;
23957c478bd9Sstevel@tonic-gate 	struct iocblk		*iocp;
23967c478bd9Sstevel@tonic-gate 	uint_t	id;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
23997c478bd9Sstevel@tonic-gate 	msg = conskbd_msg_queue;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
24027c478bd9Sstevel@tonic-gate 	ASSERT(iocp);
24037c478bd9Sstevel@tonic-gate 	id = iocp->ioc_id;
24047c478bd9Sstevel@tonic-gate 	while (msg && msg->kpm_req_id != id) {
24057c478bd9Sstevel@tonic-gate 		msg = msg->kpm_next;
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	return (msg);
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate }	/* conskbd_mux_find_msg() */
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate static void
conskbd_mux_dequeue_msg(conskbd_pending_msg_t * msg)24157c478bd9Sstevel@tonic-gate conskbd_mux_dequeue_msg(conskbd_pending_msg_t *msg)
24167c478bd9Sstevel@tonic-gate {
24177c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t *prev;
24187c478bd9Sstevel@tonic-gate 	conskbd_pending_msg_t *p;
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	mutex_enter(&conskbd_msgq_lock);
24217c478bd9Sstevel@tonic-gate 	prev = conskbd_msg_queue;
24227c478bd9Sstevel@tonic-gate 
2423b94a3996Slq150181 	for (p = prev; p && p != msg; p = p->kpm_next)
24247c478bd9Sstevel@tonic-gate 		prev = p;
2425b94a3996Slq150181 
24267c478bd9Sstevel@tonic-gate 	ASSERT(p && p == msg);
2427b94a3996Slq150181 
24287c478bd9Sstevel@tonic-gate 	if (prev == p) {
24297c478bd9Sstevel@tonic-gate 		conskbd_msg_queue = msg->kpm_next;
24307c478bd9Sstevel@tonic-gate 	} else {
24317c478bd9Sstevel@tonic-gate 		prev->kpm_next = p->kpm_next;
24327c478bd9Sstevel@tonic-gate 	}
24337c478bd9Sstevel@tonic-gate 	p->kpm_next = NULL;
24347c478bd9Sstevel@tonic-gate 	mutex_exit(&conskbd_msgq_lock);
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate }	/* conskbd_mux_dequeue_msg() */
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate #ifdef DEBUG
24397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24407c478bd9Sstevel@tonic-gate void
conskbd_dprintf(const char * fmt,...)24417c478bd9Sstevel@tonic-gate conskbd_dprintf(const char *fmt, ...)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate 	char buf[256];
24447c478bd9Sstevel@tonic-gate 	va_list ap;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
24477c478bd9Sstevel@tonic-gate 	(void) vsprintf(buf, fmt, ap);
24487c478bd9Sstevel@tonic-gate 	va_end(ap);
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "conskbd: %s", buf);
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate }	/* conskbd_dprintf() */
24537c478bd9Sstevel@tonic-gate #endif
2454