xref: /onnv-gate/usr/src/uts/common/io/consconfig_dacf.c (revision 2191:053dcaf72871)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51253Slq150181  * Common Development and Distribution License (the "License").
61253Slq150181  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211253Slq150181 
220Sstevel@tonic-gate /*
231253Slq150181  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This module performs two functions.  First, it kicks off the driver loading
310Sstevel@tonic-gate  * of the console devices during boot in dynamic_console_config().
320Sstevel@tonic-gate  * The loading of the drivers for the console devices triggers the
330Sstevel@tonic-gate  * additional device autoconfiguration to link the drivers into the keyboard
340Sstevel@tonic-gate  * and mouse console streams.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * The second function of this module is to provide the dacf functions
370Sstevel@tonic-gate  * to be called after a driver has attached and before it detaches.  For
380Sstevel@tonic-gate  * example, a driver associated with the keyboard will have kb_config called
390Sstevel@tonic-gate  * after the driver attaches and kb_unconfig before it detaches.  Similar
400Sstevel@tonic-gate  * configuration actions are performed on behalf of minor nodes representing
410Sstevel@tonic-gate  * mice.  The configuration functions for the attach case take a module
420Sstevel@tonic-gate  * name as a parameter.  The module is pushed on top of the driver during
430Sstevel@tonic-gate  * the configuration.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * Although the dacf framework is used to configure all keyboards and mice,
460Sstevel@tonic-gate  * its primary function is to allow keyboard and mouse hotplugging.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * This module supports multiple keyboards and mice at the same time.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * From the kernel perspective, there are roughly three different possible
510Sstevel@tonic-gate  * console configurations.  Across these three configurations, the following
520Sstevel@tonic-gate  * elements are constant:
530Sstevel@tonic-gate  * 	wsconsvp = IWSCN_PATH
540Sstevel@tonic-gate  * 	rwsconsvp = WC_PATH
550Sstevel@tonic-gate  * 	consms -> msdev
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * The "->" syntax indicates that the streams device on the right is
580Sstevel@tonic-gate  * linked under the streams device on the left.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * The following lists how the system is configured for different setups:
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * stdin is a local keyboard.  use stdin and stdout as the console.
630Sstevel@tonic-gate  * 	sp->cons_input_type = CONSOLE_LOCAL
640Sstevel@tonic-gate  *	rconsvp = IWSCN_PATH
650Sstevel@tonic-gate  *	wc -> conskbd -> kbddev
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * stdin is not a keyboard and stdin is the same as stdout.
680Sstevel@tonic-gate  * assume we running on a tip line and use stdin/stdout as the console.
690Sstevel@tonic-gate  * 	sp->cons_input_type = CONSOLE_TIP
700Sstevel@tonic-gate  *	rconsvp = (stdindev/stdoutdev)
710Sstevel@tonic-gate  *	wc -> conskbd -> kbddev
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * stdin is not a keyboard device and it's not the same as stdout.
740Sstevel@tonic-gate  * assume we have a serial keyboard hooked up and use it along with
750Sstevel@tonic-gate  * stdout as the console.
760Sstevel@tonic-gate  * 	sp->cons_input_type = CONSOLE_SERIAL_KEYBOARD
770Sstevel@tonic-gate  *	rconsvp = IWSCN_PATH
780Sstevel@tonic-gate  *	wc -> stdindev
790Sstevel@tonic-gate  *	conskbd -> kbddev
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * CAVEAT:
820Sstevel@tonic-gate  * 	The above is all true except for one possible Intel configuration.
830Sstevel@tonic-gate  * 	If stdin is set to a local keyboard but stdout is set to something
840Sstevel@tonic-gate  * 	other than the local display (a tip port for example) stdout will
850Sstevel@tonic-gate  * 	still go to the local display.  This is an artifact of the console
860Sstevel@tonic-gate  * 	implementation on intel.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #include <sys/types.h>
900Sstevel@tonic-gate #include <sys/param.h>
910Sstevel@tonic-gate #include <sys/cmn_err.h>
920Sstevel@tonic-gate #include <sys/user.h>
930Sstevel@tonic-gate #include <sys/vfs.h>
940Sstevel@tonic-gate #include <sys/vnode.h>
950Sstevel@tonic-gate #include <sys/pathname.h>
960Sstevel@tonic-gate #include <sys/systm.h>
970Sstevel@tonic-gate #include <sys/file.h>
980Sstevel@tonic-gate #include <sys/stropts.h>
990Sstevel@tonic-gate #include <sys/stream.h>
1000Sstevel@tonic-gate #include <sys/strsubr.h>
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #include <sys/consdev.h>
1031253Slq150181 #include <sys/console.h>
1041253Slq150181 #include <sys/wscons.h>
1050Sstevel@tonic-gate #include <sys/kbio.h>
1060Sstevel@tonic-gate #include <sys/debug.h>
1070Sstevel@tonic-gate #include <sys/reboot.h>
1080Sstevel@tonic-gate #include <sys/termios.h>
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #include <sys/ddi.h>
1110Sstevel@tonic-gate #include <sys/sunddi.h>
1120Sstevel@tonic-gate #include <sys/sunldi.h>
1130Sstevel@tonic-gate #include <sys/sunndi.h>
1140Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
1150Sstevel@tonic-gate #include <sys/modctl.h>
1160Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
1170Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
1180Sstevel@tonic-gate #include <sys/promif.h>
1190Sstevel@tonic-gate #include <sys/fs/snode.h>
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #include <sys/errno.h>
1220Sstevel@tonic-gate #include <sys/devops.h>
1230Sstevel@tonic-gate #include <sys/note.h>
1240Sstevel@tonic-gate 
1251253Slq150181 #include <sys/tem_impl.h>
1260Sstevel@tonic-gate #include <sys/polled_io.h>
1270Sstevel@tonic-gate #include <sys/kmem.h>
1280Sstevel@tonic-gate #include <sys/dacf.h>
1290Sstevel@tonic-gate #include <sys/consconfig_dacf.h>
130*2191Sszhou #include <sys/log.h>
131*2191Sszhou #include <sys/disp.h>
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * External global variables
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate extern vnode_t		*rconsvp;
1370Sstevel@tonic-gate extern dev_t		rwsconsdev;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * External functions
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate extern uintptr_t	space_fetch(char *key);
1430Sstevel@tonic-gate extern int		space_store(char *key, uintptr_t ptr);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Tasks
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate static int	kb_config(dacf_infohdl_t, dacf_arghdl_t, int);
1490Sstevel@tonic-gate static int	kb_unconfig(dacf_infohdl_t, dacf_arghdl_t, int);
1500Sstevel@tonic-gate static int	ms_config(dacf_infohdl_t, dacf_arghdl_t, int);
1510Sstevel@tonic-gate static int	ms_unconfig(dacf_infohdl_t, dacf_arghdl_t, int);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * Internal functions
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate static int	consconfig_setmodes(dev_t dev, struct termios *termiosp);
1570Sstevel@tonic-gate static void	consconfig_check_phys_kbd(cons_state_t *);
1580Sstevel@tonic-gate static void	consconfig_rem_dev(cons_state_t *, dev_t);
1590Sstevel@tonic-gate static void	consconfig_add_dev(cons_state_t *, cons_prop_t *);
1600Sstevel@tonic-gate static cons_prop_t *consconfig_find_dev(cons_state_t *, dev_t);
1610Sstevel@tonic-gate static void	consconfig_free_prop(cons_prop_t *prop);
162*2191Sszhou static void	flush_usb_serial_buf(void);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * On supported configurations, the firmware defines the keyboard and mouse
1670Sstevel@tonic-gate  * paths.  However, during USB development, it is useful to be able to use
1680Sstevel@tonic-gate  * the USB keyboard and mouse on machines without full USB firmware support.
1690Sstevel@tonic-gate  * These variables may be set in /etc/system according to a machine's
1700Sstevel@tonic-gate  * USB configuration.  This module will override the firmware's values
1710Sstevel@tonic-gate  * with these.
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * NOTE:
1740Sstevel@tonic-gate  * The master copies of these variables in the misc/consconfig module.
1750Sstevel@tonic-gate  * The reason for this is historic.  In versions of solaris up to and
1760Sstevel@tonic-gate  * including solaris 9 the conscole configuration code was split into a
1770Sstevel@tonic-gate  * seperate sparc and intel version.  These variables were defined
1780Sstevel@tonic-gate  * in misc/consconfig on sparc and dacf/consconfig_dacf on intel.
1790Sstevel@tonic-gate  *
1800Sstevel@tonic-gate  * Unfortunatly the sparc variables were well documented.
1810Sstevel@tonic-gate  * So to aviod breaking either sparc or intel we'll declare the variables
1820Sstevel@tonic-gate  * in both modules.  This will allow any /etc/system entries that
1830Sstevel@tonic-gate  * users may have to continue working.
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  * The variables in misc/consconfig will take precedence over the variables
1860Sstevel@tonic-gate  * found in this file.  Since we eventually want to remove the variables
1870Sstevel@tonic-gate  * local to this this file, if the user set them we'll emmit an error
1880Sstevel@tonic-gate  * message telling them they need to set the variables in misc/consconfig
1890Sstevel@tonic-gate  * instead.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate static char *usb_kb_path = NULL;
1920Sstevel@tonic-gate static char *usb_ms_path = NULL;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Access functions in the misc/consconfig module used to retrieve the
1960Sstevel@tonic-gate  * values of it local usb_kb_path and usb_ms_path variables
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate extern char *consconfig_get_usb_kb_path();
1990Sstevel@tonic-gate extern char *consconfig_get_usb_ms_path();
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Local variables used to store the value of the usb_kb_path and
2030Sstevel@tonic-gate  * usb_ms_path variables found in misc/consconfig
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static char *consconfig_usb_kb_path = NULL;
2060Sstevel@tonic-gate static char *consconfig_usb_ms_path = NULL;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * Internal variables
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static dev_t		stdoutdev;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * consconfig_errlevel:  debug verbosity; smaller numbers are more
2150Sstevel@tonic-gate  * verbose.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate static int consconfig_errlevel = DPRINT_L3;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * Baud rate table
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate #define	MAX_SPEEDS 24
2230Sstevel@tonic-gate static struct speed {
2240Sstevel@tonic-gate 	char *name;
2250Sstevel@tonic-gate 	int code;
2260Sstevel@tonic-gate } speedtab[MAX_SPEEDS] = {
2270Sstevel@tonic-gate 	{"0", B0},		{"50", B50},		{"75", B75},
2280Sstevel@tonic-gate 	{"110", B110},		{"134", B134},		{"150", B150},
2290Sstevel@tonic-gate 	{"200", B200},		{"300", B300},		{"600", B600},
2300Sstevel@tonic-gate 	{"1200", B1200},	{"1800", B1800},	{"2400", B2400},
2310Sstevel@tonic-gate 	{"4800", B4800},	{"9600", B9600},	{"19200", B19200},
2320Sstevel@tonic-gate 	{"38400", B38400},	{"57600", B57600},	{"76800", B76800},
2330Sstevel@tonic-gate 	{"115200", B115200},	{"153600", B153600},	{"230400", B230400},
2340Sstevel@tonic-gate 	{"307200", B307200},	{"460800", B460800},	{"", 0}
2350Sstevel@tonic-gate };
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate static dacf_op_t kbconfig_op[] = {
2380Sstevel@tonic-gate 	{ DACF_OPID_POSTATTACH,	kb_config },
2390Sstevel@tonic-gate 	{ DACF_OPID_PREDETACH,	kb_unconfig },
2400Sstevel@tonic-gate 	{ DACF_OPID_END,	NULL },
2410Sstevel@tonic-gate };
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static dacf_op_t msconfig_op[] = {
2440Sstevel@tonic-gate 	{ DACF_OPID_POSTATTACH,	ms_config },
2450Sstevel@tonic-gate 	{ DACF_OPID_PREDETACH,	ms_unconfig },
2460Sstevel@tonic-gate 	{ DACF_OPID_END,	NULL },
2470Sstevel@tonic-gate };
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate static dacf_opset_t opsets[] = {
2500Sstevel@tonic-gate 	{ "kb_config",	kbconfig_op },
2510Sstevel@tonic-gate 	{ "ms_config",	msconfig_op },
2520Sstevel@tonic-gate 	{ NULL,		NULL }
2530Sstevel@tonic-gate };
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate struct dacfsw dacfsw = {
2560Sstevel@tonic-gate 	DACF_MODREV_1,
2570Sstevel@tonic-gate 	opsets,
2580Sstevel@tonic-gate };
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate struct modldacf modldacf = {
2610Sstevel@tonic-gate 	&mod_dacfops,   /* Type of module */
2620Sstevel@tonic-gate 	"Consconfig DACF %I%",
2630Sstevel@tonic-gate 	&dacfsw
2640Sstevel@tonic-gate };
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate struct modlinkage modlinkage = {
2670Sstevel@tonic-gate 	MODREV_1, (void *)&modldacf, NULL
2680Sstevel@tonic-gate };
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate int
2710Sstevel@tonic-gate _init(void) {
2720Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate int
2760Sstevel@tonic-gate _fini(void)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * This modules state is held in the kernel by space.c
2800Sstevel@tonic-gate 	 * allowing this module to be unloaded.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate int
2860Sstevel@tonic-gate _info(struct modinfo *modinfop)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*PRINTFLIKE2*/
2920Sstevel@tonic-gate static void consconfig_dprintf(int, const char *, ...)
2930Sstevel@tonic-gate     __KPRINTFLIKE(2);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static void
2960Sstevel@tonic-gate consconfig_dprintf(int l, const char *fmt, ...)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	va_list ap;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate #ifndef DEBUG
3010Sstevel@tonic-gate 	if (!l) {
3020Sstevel@tonic-gate 		return;
3030Sstevel@tonic-gate 	}
3041253Slq150181 #endif /* DEBUG */
3050Sstevel@tonic-gate 	if (l < consconfig_errlevel) {
3060Sstevel@tonic-gate 		return;
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	va_start(ap, fmt);
3100Sstevel@tonic-gate 	(void) vprintf(fmt, ap);
3110Sstevel@tonic-gate 	va_end(ap);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Return a property name in /aliases.
3160Sstevel@tonic-gate  * The caller is responsible for freeing the memory.
3170Sstevel@tonic-gate  * The property value is NULL terminated string.
3180Sstevel@tonic-gate  * /aliases exists in OBP >= 2.4.
3190Sstevel@tonic-gate  */
3200Sstevel@tonic-gate char *
3210Sstevel@tonic-gate get_alias(char *alias, char *buf)
3220Sstevel@tonic-gate {
323789Sahrens 	pnode_t node;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/* OBP >= 2.4 has /aliases */
3260Sstevel@tonic-gate 	if ((node = prom_alias_node()) == OBP_BADNODE)
3270Sstevel@tonic-gate 		return (NULL);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (prom_getproplen(node, (caddr_t)alias) <= 0)
3300Sstevel@tonic-gate 		return (NULL);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	(void) prom_getprop(node, (caddr_t)alias, (caddr_t)buf);
3330Sstevel@tonic-gate 	prom_pathname(buf);
3340Sstevel@tonic-gate 	return (buf);
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * i_consconfig_createvp:
3390Sstevel@tonic-gate  *	This routine is a convenience routine that is passed a path and returns
3400Sstevel@tonic-gate  *	a vnode.
3410Sstevel@tonic-gate  */
3420Sstevel@tonic-gate static vnode_t *
3430Sstevel@tonic-gate i_consconfig_createvp(char *path)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate 	int error;
3460Sstevel@tonic-gate 	vnode_t *vp;
3470Sstevel@tonic-gate 	char *buf = NULL, *fullpath;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "i_consconfig_createvp: %s\n", path);
3500Sstevel@tonic-gate 	fullpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (strchr(path, ':') == NULL) {
3530Sstevel@tonic-gate 		/* convert an OBP path to a /devices path */
3540Sstevel@tonic-gate 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3550Sstevel@tonic-gate 		if (i_ddi_prompath_to_devfspath(path, buf) != DDI_SUCCESS) {
3560Sstevel@tonic-gate 			kmem_free(buf, MAXPATHLEN);
3570Sstevel@tonic-gate 			kmem_free(fullpath, MAXPATHLEN);
3580Sstevel@tonic-gate 			return (NULL);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		(void) snprintf(fullpath, MAXPATHLEN, "/devices%s", buf);
3610Sstevel@tonic-gate 		kmem_free(buf, MAXPATHLEN);
3620Sstevel@tonic-gate 	} else {
3630Sstevel@tonic-gate 		/* convert a devfs path to a /devices path */
3640Sstevel@tonic-gate 		(void) snprintf(fullpath, MAXPATHLEN, "/devices%s", path);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "lookupname(%s)\n", fullpath);
3680Sstevel@tonic-gate 	error = lookupname(fullpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
3690Sstevel@tonic-gate 	kmem_free(fullpath, MAXPATHLEN);
3700Sstevel@tonic-gate 	if (error)
3710Sstevel@tonic-gate 		return (NULL);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "create vnode = 0x%p - dev 0x%lx\n", vp, vp->v_rdev);
3740Sstevel@tonic-gate 	ASSERT(vn_matchops(vp, spec_getvnodeops()));
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	return (vp);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * consconfig_print_paths:
3810Sstevel@tonic-gate  *	Function to print out the various paths
3820Sstevel@tonic-gate  */
3830Sstevel@tonic-gate static void
3840Sstevel@tonic-gate consconfig_print_paths(void)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	char    *path;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if (usb_kb_path != NULL)
3890Sstevel@tonic-gate 		cmn_err(CE_WARN,
3900Sstevel@tonic-gate 		    "consconfig_dacf:usb_kb_path has been deprecated, "
3910Sstevel@tonic-gate 		    "use consconfig:usb_kb_path instead");
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (usb_ms_path != NULL)
3940Sstevel@tonic-gate 		cmn_err(CE_WARN,
3950Sstevel@tonic-gate 		    "consconfig_dacf:usb_ms_path has been deprecated, "
3960Sstevel@tonic-gate 		    "use consconfig:usb_ms_path instead");
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if (consconfig_errlevel > DPRINT_L0)
3990Sstevel@tonic-gate 		return;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	path = NULL;
4020Sstevel@tonic-gate 	if (consconfig_usb_kb_path != NULL)
4030Sstevel@tonic-gate 		path = consconfig_usb_kb_path;
4040Sstevel@tonic-gate 	else if (usb_kb_path != NULL)
4050Sstevel@tonic-gate 		path = usb_kb_path;
4060Sstevel@tonic-gate 	if (path != NULL)
4070Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "usb keyboard path = %s\n", path);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	path = plat_kbdpath();
4100Sstevel@tonic-gate 	if (path != NULL)
4110Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "keyboard path = %s\n", path);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	path = NULL;
4140Sstevel@tonic-gate 	if (consconfig_usb_ms_path != NULL)
4150Sstevel@tonic-gate 		path = consconfig_usb_ms_path;
4160Sstevel@tonic-gate 	else if (usb_ms_path != NULL)
4170Sstevel@tonic-gate 		path = usb_ms_path;
4180Sstevel@tonic-gate 	if (path != NULL)
4190Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "usb mouse path = %s\n", path);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	path = plat_mousepath();
4220Sstevel@tonic-gate 	if (path != NULL)
4230Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "mouse path = %s\n", path);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	path = plat_stdinpath();
4260Sstevel@tonic-gate 	if (path != NULL)
4270Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "stdin path = %s\n", path);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	path = plat_stdoutpath();
4300Sstevel@tonic-gate 	if (path != NULL)
4310Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "stdout path = %s\n", path);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	path = plat_fbpath();
4340Sstevel@tonic-gate 	if (path != NULL)
4350Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "fb path = %s\n", path);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate  * consconfig_kbd_abort_enable:
4400Sstevel@tonic-gate  * 	Send the CONSSETABORTENABLE ioctl to the lower layers.  This ioctl
4410Sstevel@tonic-gate  * 	will only be sent to the device if it is the console device.
4420Sstevel@tonic-gate  * 	This ioctl tells the device to pay attention to abort sequences.
4430Sstevel@tonic-gate  * 	In the case of kbtrans, this would tell the driver to pay attention
4440Sstevel@tonic-gate  * 	to the two key abort sequences like STOP-A.  In the case of the
4450Sstevel@tonic-gate  * 	serial keyboard, it would be an abort sequence like a break.
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate static int
4480Sstevel@tonic-gate consconfig_kbd_abort_enable(ldi_handle_t lh)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	int	err, rval;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consconfig_kbd_abort_enable\n");
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	err = ldi_ioctl(lh, CONSSETABORTENABLE, (uintptr_t)B_TRUE,
4550Sstevel@tonic-gate 	    FKIOCTL, kcred, &rval);
4560Sstevel@tonic-gate 	return (err);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate  * consconfig_kbd_abort_disable:
4610Sstevel@tonic-gate  * 	Send CONSSETABORTENABLE ioctl to lower layers.  This ioctl
4620Sstevel@tonic-gate  * 	will only be sent to the device if it is the console device.
4630Sstevel@tonic-gate  * 	This ioctl tells the physical device to ignore abort sequences,
4640Sstevel@tonic-gate  * 	and send the sequences up to virtual keyboard(conskbd) so that
4650Sstevel@tonic-gate  * 	STOP and A (or F1 and A) can be combined.
4660Sstevel@tonic-gate  */
4670Sstevel@tonic-gate static int
4680Sstevel@tonic-gate consconfig_kbd_abort_disable(ldi_handle_t lh)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	int	err, rval;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consconfig_kbd_abort_disable\n");
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	err = ldi_ioctl(lh, CONSSETABORTENABLE, (uintptr_t)B_FALSE,
4750Sstevel@tonic-gate 	    FKIOCTL, kcred, &rval);
4760Sstevel@tonic-gate 	return (err);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * consconfig_get_polledio:
4810Sstevel@tonic-gate  * 	Query the console with the CONSPOLLEDIO ioctl.
4820Sstevel@tonic-gate  * 	The polled I/O routines are used by debuggers to perform I/O while
4830Sstevel@tonic-gate  * 	interrupts and normal kernel services are disabled.
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate static cons_polledio_t *
4860Sstevel@tonic-gate consconfig_get_polledio(ldi_handle_t lh)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	int		err, rval;
4890Sstevel@tonic-gate 	struct strioctl	strioc;
4900Sstevel@tonic-gate 	cons_polledio_t	*polled_io;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/*
4930Sstevel@tonic-gate 	 * Setup the ioctl to be sent down to the lower driver.
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	strioc.ic_cmd = CONSOPENPOLLEDIO;
4960Sstevel@tonic-gate 	strioc.ic_timout = INFTIM;
4970Sstevel@tonic-gate 	strioc.ic_len = sizeof (polled_io);
4980Sstevel@tonic-gate 	strioc.ic_dp = (char *)&polled_io;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * Send the ioctl to the driver.  The ioctl will wait for
5020Sstevel@tonic-gate 	 * the response to come back from wc.  wc has already issued
5030Sstevel@tonic-gate 	 * the CONSOPENPOLLEDIO to the lower layer driver.
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	err = ldi_ioctl(lh, I_STR, (intptr_t)&strioc, FKIOCTL, kcred, &rval);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (err != 0) {
5080Sstevel@tonic-gate 		/*
5090Sstevel@tonic-gate 		 * If the lower driver does not support polled I/O, then
5100Sstevel@tonic-gate 		 * return NULL.  This will be the case if the driver does
5110Sstevel@tonic-gate 		 * not handle polled I/O, or OBP is going to handle polled
5120Sstevel@tonic-gate 		 * I/O for the device.
5130Sstevel@tonic-gate 		 */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		return (NULL);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Return the polled I/O structure.
5200Sstevel@tonic-gate 	 */
5210Sstevel@tonic-gate 	return (polled_io);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate  * consconfig_setup_polledio:
5260Sstevel@tonic-gate  * 	This routine does the setup work for polled I/O.  First we get
5270Sstevel@tonic-gate  * 	the polled_io structure from the lower layers
5280Sstevel@tonic-gate  * 	and then we register the polled I/O
5290Sstevel@tonic-gate  * 	callbacks with the debugger that will be using them.
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate static void
5320Sstevel@tonic-gate consconfig_setup_polledio(cons_state_t *sp, dev_t dev)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	cons_polledio_t		*polled_io;
5350Sstevel@tonic-gate 	ldi_handle_t		lh;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consconfig_setup_polledio: start\n");
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (ldi_open_by_dev(&dev, OTYP_CHR,
5410Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY, kcred, &lh, sp->cons_li) != 0)
5420Sstevel@tonic-gate 		return;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	/*
5460Sstevel@tonic-gate 	 * Get the polled io routines so that we can use this
5470Sstevel@tonic-gate 	 * device with the debuggers.
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	polled_io = consconfig_get_polledio(lh);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/*
5520Sstevel@tonic-gate 	 * If the get polledio failed, then we do not want to throw
5530Sstevel@tonic-gate 	 * the polled I/O switch.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if (polled_io == NULL) {
5560Sstevel@tonic-gate 		DPRINTF(DPRINT_L0,
5570Sstevel@tonic-gate 		    "consconfig_setup_polledio: get_polledio failed\n");
5580Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, kcred);
5590Sstevel@tonic-gate 		return;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	/* Initialize the polled input */
5630Sstevel@tonic-gate 	polled_io_init();
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/* Register the callbacks */
5660Sstevel@tonic-gate 	DPRINTF(DPRINT_L0,
5670Sstevel@tonic-gate 	    "consconfig_setup_polledio: registering callbacks\n");
5680Sstevel@tonic-gate 	(void) polled_io_register_callbacks(polled_io, 0);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, kcred);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consconfig_setup_polledio: end\n");
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static cons_state_t *
5760Sstevel@tonic-gate consconfig_state_init(void)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	cons_state_t	*sp;
5790Sstevel@tonic-gate 	int		rval;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/* Initialize console information */
5820Sstevel@tonic-gate 	sp = kmem_zalloc(sizeof (cons_state_t), KM_SLEEP);
5830Sstevel@tonic-gate 	sp->cons_keyboard_problem = B_FALSE;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	mutex_init(&sp->cons_lock, NULL, MUTEX_DRIVER, NULL);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/* check if consconfig:usb_kb_path is set in /etc/system */
5880Sstevel@tonic-gate 	consconfig_usb_kb_path = consconfig_get_usb_kb_path();
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/* check if consconfig:usb_ms_path is set in /etc/system */
5910Sstevel@tonic-gate 	consconfig_usb_ms_path = consconfig_get_usb_ms_path();
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	consconfig_print_paths();
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/* init external globals */
5960Sstevel@tonic-gate 	stdoutdev = NODEV;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/*
5990Sstevel@tonic-gate 	 * Find keyboard, mouse, stdin and stdout devices, if they
6000Sstevel@tonic-gate 	 * exist on this platform.
6010Sstevel@tonic-gate 	 */
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (consconfig_usb_kb_path != NULL) {
6040Sstevel@tonic-gate 		sp->cons_keyboard_path = consconfig_usb_kb_path;
6050Sstevel@tonic-gate 	} else if (usb_kb_path != NULL) {
6060Sstevel@tonic-gate 		sp->cons_keyboard_path = usb_kb_path;
6070Sstevel@tonic-gate 	} else {
6080Sstevel@tonic-gate 		sp->cons_keyboard_path = plat_kbdpath();
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (consconfig_usb_ms_path != NULL) {
6120Sstevel@tonic-gate 		sp->cons_mouse_path = consconfig_usb_ms_path;
6130Sstevel@tonic-gate 	} else if (usb_ms_path != NULL) {
6140Sstevel@tonic-gate 		sp->cons_mouse_path = usb_ms_path;
6150Sstevel@tonic-gate 	} else {
6160Sstevel@tonic-gate 		sp->cons_mouse_path = plat_mousepath();
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/* Identify the stdout driver */
6200Sstevel@tonic-gate 	sp->cons_stdout_path = plat_stdoutpath();
6210Sstevel@tonic-gate 	if (plat_stdout_is_framebuffer()) {
6220Sstevel@tonic-gate 		sp->cons_fb_path = sp->cons_stdout_path;
6230Sstevel@tonic-gate 	} else {
6240Sstevel@tonic-gate 		sp->cons_fb_path = plat_fbpath();
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
627267Scg149915 	if (plat_stdin_is_keyboard() &&
628539Sry162471 	    (usb_kb_path != NULL || consconfig_usb_kb_path != NULL))  {
6290Sstevel@tonic-gate 		sp->cons_stdin_path = sp->cons_keyboard_path;
6300Sstevel@tonic-gate 	} else {
6310Sstevel@tonic-gate 		/*
6320Sstevel@tonic-gate 		 * The standard in device may or may not be the same as
6330Sstevel@tonic-gate 		 * the keyboard. Even if the keyboard is not the
6340Sstevel@tonic-gate 		 * standard input, the keyboard console stream will
6350Sstevel@tonic-gate 		 * still be built if the keyboard alias provided by the
6360Sstevel@tonic-gate 		 * firmware exists and is valid.
6370Sstevel@tonic-gate 		 */
6380Sstevel@tonic-gate 		sp->cons_stdin_path = plat_stdinpath();
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	sp->cons_li = ldi_ident_from_anon();
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/* Save the pointer for retrieval by the dacf functions */
6440Sstevel@tonic-gate 	rval = space_store("consconfig", (uintptr_t)sp);
6450Sstevel@tonic-gate 	ASSERT(rval == 0);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	return (sp);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate static int
6510Sstevel@tonic-gate consconfig_relink_wc(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	int		err, rval;
6540Sstevel@tonic-gate 	ldi_handle_t	wc_lh;
6550Sstevel@tonic-gate 	dev_t		wc_dev;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	ASSERT(muxid != NULL);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/*
6600Sstevel@tonic-gate 	 * NOTE: we could be in a dacf callback context right now. normally
6610Sstevel@tonic-gate 	 * it's not legal to call any ldi_open_*() function from this context
6620Sstevel@tonic-gate 	 * because we're currently holding device tree locks and if the
6630Sstevel@tonic-gate 	 * ldi_open_*() call could try to acquire other device tree locks
6640Sstevel@tonic-gate 	 * (to attach the device we're trying to open.)  if this happens then
6650Sstevel@tonic-gate 	 * we could deadlock.  To avoid this situation, during initialization
6660Sstevel@tonic-gate 	 * we made sure to grab a hold on the dip of the device we plan to
6670Sstevel@tonic-gate 	 * open so that it can never be detached.  Then we use
6680Sstevel@tonic-gate 	 * ldi_open_by_dev() to actually open the device since it will see
6690Sstevel@tonic-gate 	 * that the device is already attached and held and instead of
6700Sstevel@tonic-gate 	 * acquire any locks it will only increase the reference count
6710Sstevel@tonic-gate 	 * on the device.
6720Sstevel@tonic-gate 	 */
6730Sstevel@tonic-gate 	wc_dev = sp->cons_wc_vp->v_rdev;
6740Sstevel@tonic-gate 	err = ldi_open_by_dev(&wc_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY,
6750Sstevel@tonic-gate 	    kcred, &wc_lh, sp->cons_li);
6760Sstevel@tonic-gate 	ASSERT(wc_dev == sp->cons_wc_vp->v_rdev);
6770Sstevel@tonic-gate 	if (err) {
6780Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig_relink_wc: "
6790Sstevel@tonic-gate 		    "unable to open wc device");
6800Sstevel@tonic-gate 		return (err);
6810Sstevel@tonic-gate 	}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (new_lh != NULL) {
6840Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "linking stream under wc\n");
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		err = ldi_ioctl(wc_lh, I_PLINK, (uintptr_t)new_lh,
6870Sstevel@tonic-gate 		    FKIOCTL, kcred, muxid);
6880Sstevel@tonic-gate 		if (err != 0) {
6890Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_wc: "
6900Sstevel@tonic-gate 			    "wc link failed, error %d", err);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 	} else {
6930Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "unlinking stream from under wc\n");
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 		err = ldi_ioctl(wc_lh, I_PUNLINK, *muxid,
6960Sstevel@tonic-gate 		    FKIOCTL, kcred, &rval);
6970Sstevel@tonic-gate 		if (err != 0) {
6980Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_wc: "
6990Sstevel@tonic-gate 			    "wc unlink failed, error %d", err);
7000Sstevel@tonic-gate 		} else {
7010Sstevel@tonic-gate 			*muxid = -1;
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	(void) ldi_close(wc_lh, FREAD|FWRITE, kcred);
7060Sstevel@tonic-gate 	return (err);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate static void
7100Sstevel@tonic-gate cons_build_upper_layer(cons_state_t *sp)
7110Sstevel@tonic-gate {
7121253Slq150181 	ldi_handle_t		wc_lh;
7131253Slq150181 	struct strioctl		strioc;
7141253Slq150181 	int			rval;
7151253Slq150181 	dev_t			wc_dev;
7161253Slq150181 	dev_t			dev;
7171253Slq150181 #ifdef _HAVE_TEM_FIRMWARE
7181253Slq150181 	dev_info_t		*dip;
7191253Slq150181 	int			*int_array;
7201253Slq150181 	uint_t			nint;
7211253Slq150181 	boolean_t		kfb_mode;
7221253Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
7231253Slq150181 
7240Sstevel@tonic-gate 	/*
7250Sstevel@tonic-gate 	 * Build the wc->conskbd portion of the keyboard console stream.
7260Sstevel@tonic-gate 	 * Even if no keyboard is attached to the system, the upper
7270Sstevel@tonic-gate 	 * layer of the stream will be built. If the user attaches
7280Sstevel@tonic-gate 	 * a keyboard after the system is booted, the keyboard driver
7290Sstevel@tonic-gate 	 * and module will be linked under conskbd.
7300Sstevel@tonic-gate 	 *
7310Sstevel@tonic-gate 	 * Errors are generally ignored here because conskbd and wc
7320Sstevel@tonic-gate 	 * are pseudo drivers and should be present on the system.
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/* open the console keyboard device.  will never be closed */
7360Sstevel@tonic-gate 	if (ldi_open_by_name(CONSKBD_PATH, FREAD|FWRITE|FNOCTTY, kcred,
7370Sstevel@tonic-gate 	    &sp->conskbd_lh, sp->cons_li) != 0)
7380Sstevel@tonic-gate 		cmn_err(CE_PANIC, "consconfig: "
7390Sstevel@tonic-gate 		    "unable to open conskbd device");
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "conskbd_lh = %p\n", sp->conskbd_lh);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	/* open the console mouse device.  will never be closed */
7440Sstevel@tonic-gate 	if (ldi_open_by_name(CONSMS_PATH, FREAD|FWRITE|FNOCTTY, kcred,
7450Sstevel@tonic-gate 	    &sp->consms_lh, sp->cons_li) != 0)
7460Sstevel@tonic-gate 		cmn_err(CE_PANIC, "consconfig: "
7470Sstevel@tonic-gate 		    "unable to open consms device");
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consms_lh = %p\n", sp->consms_lh);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * Get a vnode for the wc device and then grab a hold on the
7530Sstevel@tonic-gate 	 * device dip so it can never detach.  We need to do this now
7540Sstevel@tonic-gate 	 * because later we'll have to open the wc device in a context
7550Sstevel@tonic-gate 	 * were it isn't safe to acquire any device tree locks (ie, during
7560Sstevel@tonic-gate 	 * a dacf callback.)
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	sp->cons_wc_vp = i_consconfig_createvp(WC_PATH);
7590Sstevel@tonic-gate 	if (sp->cons_wc_vp == NULL)
7600Sstevel@tonic-gate 		panic("consconfig: unable to find wc device");
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (e_ddi_hold_devi_by_dev(sp->cons_wc_vp->v_rdev, 0) == NULL)
7630Sstevel@tonic-gate 		panic("consconfig: unable to hold wc device");
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * Build the wc->conskbd portion of the keyboard console stream.
7670Sstevel@tonic-gate 	 * Even if no keyboard is attached to the system, the upper
7680Sstevel@tonic-gate 	 * layer of the stream will be built. If the user attaches
7690Sstevel@tonic-gate 	 * a keyboard after the system is booted, the keyboard driver
7700Sstevel@tonic-gate 	 * and module will be linked under conskbd.
7710Sstevel@tonic-gate 	 *
7720Sstevel@tonic-gate 	 * The act of linking conskbd under wc will cause wc to
7730Sstevel@tonic-gate 	 * query the lower layers about their polled I/O routines
7740Sstevel@tonic-gate 	 * using CONSOPENPOLLEDIO.  This will fail on this link because
7750Sstevel@tonic-gate 	 * there is not a physical keyboard linked under conskbd.
7760Sstevel@tonic-gate 	 *
7770Sstevel@tonic-gate 	 * Since conskbd and wc are pseudo drivers, errors are
7780Sstevel@tonic-gate 	 * generally ignored when linking and unlinking them.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	(void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/*
7830Sstevel@tonic-gate 	 * Get a vnode for the redirection device.  (It has the
7840Sstevel@tonic-gate 	 * connection to the workstation console device wired into it,
7850Sstevel@tonic-gate 	 * so that it's not necessary to establish the connection
7860Sstevel@tonic-gate 	 * here.  If the redirection device is ever generalized to
7870Sstevel@tonic-gate 	 * handle multiple client devices, it won't be able to
7880Sstevel@tonic-gate 	 * establish the connection itself, and we'll have to do it
7890Sstevel@tonic-gate 	 * here.)
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	wsconsvp = i_consconfig_createvp(IWSCN_PATH);
7920Sstevel@tonic-gate 	if (wsconsvp == NULL)
7930Sstevel@tonic-gate 		cmn_err(CE_PANIC, "consconfig: "
7940Sstevel@tonic-gate 		    "unable to find iwscn device");
7950Sstevel@tonic-gate 
7961253Slq150181 	if (cons_tem_disable)
7971253Slq150181 		return;
7981253Slq150181 
7990Sstevel@tonic-gate 	if (sp->cons_fb_path == NULL) {
8001253Slq150181 #if defined(i386) || defined(__i386) || defined(__ia64)
8010Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig: no screen found");
8021253Slq150181 #endif /* defined(i386) || defined(__i386) || defined(__ia64) */
8031253Slq150181 		return;
8041253Slq150181 	}
8051253Slq150181 
8061253Slq150181 	/* make sure the frame buffer device exists */
8071253Slq150181 	dev = ddi_pathname_to_dev_t(sp->cons_fb_path);
8081253Slq150181 	if (dev == NODEV) {
8091253Slq150181 		cmn_err(CE_WARN, "consconfig: "
8101253Slq150181 		    "cannot find driver for screen device %s",
8111253Slq150181 		    sp->cons_fb_path);
8120Sstevel@tonic-gate 		return;
8131253Slq150181 	}
8140Sstevel@tonic-gate 
8151253Slq150181 #ifdef _HAVE_TEM_FIRMWARE
8161253Slq150181 	/*
8171253Slq150181 	 * Here we hold the driver and check "tem-support" property.
8181253Slq150181 	 * We're doing this with e_ddi_hold_devi_by_dev and
8191253Slq150181 	 * ddi_prop_lookup_int_array before opening the driver since
8201253Slq150181 	 * some video cards that don't support the kernel terminal
8211253Slq150181 	 * emulator could hang or crash if opened too early during
8221253Slq150181 	 * boot.
8231253Slq150181 	 */
8241253Slq150181 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
8251253Slq150181 		cmn_err(CE_WARN, "consconfig: cannot hold fb dev %s",
8261253Slq150181 		    sp->cons_fb_path);
8271253Slq150181 		return;
8281253Slq150181 	}
8290Sstevel@tonic-gate 
8301253Slq150181 	/*
8311253Slq150181 	 * Check the existance of the tem-support property AND that
8321253Slq150181 	 * it be equal to 1.
8331253Slq150181 	 */
8341253Slq150181 	kfb_mode = B_FALSE;
8351253Slq150181 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
8361253Slq150181 	    DDI_PROP_DONTPASS, "tem-support", &int_array, &nint) ==
8371253Slq150181 	    DDI_SUCCESS) {
8381253Slq150181 		if (nint > 0)
8391253Slq150181 			kfb_mode = int_array[0] == 1;
8401253Slq150181 		ddi_prop_free(int_array);
8411253Slq150181 	}
8421253Slq150181 	ddi_release_devi(dip);
8431253Slq150181 	if (!kfb_mode)
8441253Slq150181 		return;
8451253Slq150181 #endif /* _HAVE_TEM_FIRMWARE */
8460Sstevel@tonic-gate 
8471253Slq150181 	/* tell wc to open the frame buffer device */
8481253Slq150181 	wc_dev = sp->cons_wc_vp->v_rdev;
8491253Slq150181 	if (ldi_open_by_dev(&wc_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY, kcred,
8501253Slq150181 	    &wc_lh, sp->cons_li)) {
8511253Slq150181 		cmn_err(CE_PANIC, "cons_build_upper_layer: "
8521253Slq150181 		    "unable to open wc device");
8531253Slq150181 		return;
8541253Slq150181 	}
8551253Slq150181 	ASSERT(wc_dev == sp->cons_wc_vp->v_rdev);
8560Sstevel@tonic-gate 
8571253Slq150181 	strioc.ic_cmd = WC_OPEN_FB;
8581253Slq150181 	strioc.ic_timout = INFTIM;
8591253Slq150181 	strioc.ic_len = strlen(sp->cons_fb_path) + 1;
8601253Slq150181 	strioc.ic_dp = sp->cons_fb_path;
8610Sstevel@tonic-gate 
8621253Slq150181 	if (ldi_ioctl(wc_lh, I_STR, (intptr_t)&strioc,
8631253Slq150181 	    FKIOCTL, kcred, &rval) == 0)
8641253Slq150181 		consmode = CONS_KFB;
8651253Slq150181 	else
8661253Slq150181 		cmn_err(CE_WARN,
8671253Slq150181 		    "consconfig: terminal emulator failed to initialize");
8681253Slq150181 	(void) ldi_close(wc_lh, FREAD|FWRITE, kcred);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate static void
8720Sstevel@tonic-gate consconfig_load_drivers(cons_state_t *sp)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * Calling ddi_pathname_to_dev_t causes the drivers to be loaded.
8760Sstevel@tonic-gate 	 * The attaching of the drivers will cause the creation of the
8770Sstevel@tonic-gate 	 * keyboard and mouse minor nodes, which will in turn trigger the
8780Sstevel@tonic-gate 	 * dacf framework to call the keyboard and mouse configuration
8790Sstevel@tonic-gate 	 * tasks.  See PSARC/1998/212 for more details about the dacf
8800Sstevel@tonic-gate 	 * framework.
8810Sstevel@tonic-gate 	 *
8820Sstevel@tonic-gate 	 * on sparc, when the console is ttya, zs0 is stdin/stdout, and zs1
8830Sstevel@tonic-gate 	 * is kb/mouse.  zs0 must be attached before zs1. The zs driver
8840Sstevel@tonic-gate 	 * is written this way and the hardware may depend on this, too.
8850Sstevel@tonic-gate 	 * It would be better to enforce this by attaching zs in sibling
8860Sstevel@tonic-gate 	 * order with a driver property, such as ddi-attachall.
8870Sstevel@tonic-gate 	 */
8880Sstevel@tonic-gate 	if (sp->cons_stdin_path != NULL)
8890Sstevel@tonic-gate 		stdindev = ddi_pathname_to_dev_t(sp->cons_stdin_path);
8900Sstevel@tonic-gate 	if (stdindev == NODEV) {
8910Sstevel@tonic-gate 		DPRINTF(DPRINT_L0,
8920Sstevel@tonic-gate 		    "!fail to attach stdin: %s\n", sp->cons_stdin_path);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 	if (sp->cons_stdout_path != NULL)
8950Sstevel@tonic-gate 		stdoutdev = ddi_pathname_to_dev_t(sp->cons_stdout_path);
8960Sstevel@tonic-gate 	if (sp->cons_keyboard_path != NULL)
8970Sstevel@tonic-gate 		kbddev = ddi_pathname_to_dev_t(sp->cons_keyboard_path);
8980Sstevel@tonic-gate 	if (sp->cons_mouse_path != NULL)
8990Sstevel@tonic-gate 		mousedev =  ddi_pathname_to_dev_t(sp->cons_mouse_path);
9000Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "stdindev %lx, stdoutdev %lx, kbddev %lx, "
9010Sstevel@tonic-gate 	    "mousedev %lx\n", stdindev, stdoutdev, kbddev, mousedev);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate static void
9050Sstevel@tonic-gate consconfig_init_framebuffer(cons_state_t *sp)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	if (!plat_stdout_is_framebuffer())
9080Sstevel@tonic-gate 		return;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "stdout is framebuffer\n");
9110Sstevel@tonic-gate 	ASSERT(strcmp(sp->cons_fb_path, sp->cons_stdout_path) == 0);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	/*
9140Sstevel@tonic-gate 	 * Console output is a framebuffer.
9150Sstevel@tonic-gate 	 * Find the framebuffer driver if we can, and make
9160Sstevel@tonic-gate 	 * ourselves a shadow vnode to track it with.
9170Sstevel@tonic-gate 	 */
9180Sstevel@tonic-gate 	fbdev = stdoutdev;
9190Sstevel@tonic-gate 	if (fbdev == NODEV) {
9200Sstevel@tonic-gate 		DPRINTF(DPRINT_L3,
9210Sstevel@tonic-gate 		    "Can't find driver for console framebuffer\n");
9220Sstevel@tonic-gate 	} else {
9230Sstevel@tonic-gate 		/* stdoutdev is valid, of fbvp should exist */
9240Sstevel@tonic-gate 		fbvp = i_consconfig_createvp(sp->cons_stdout_path);
9250Sstevel@tonic-gate 		if (fbvp == NULL) {
9260Sstevel@tonic-gate 			panic("consconfig_load_drivers: "
9270Sstevel@tonic-gate 			    "unable to find frame buffer device");
9280Sstevel@tonic-gate 			/*NOTREACHED*/
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		ASSERT(fbvp->v_rdev == fbdev);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		/* console device is never released */
9330Sstevel@tonic-gate 		fbdip = e_ddi_hold_devi_by_dev(fbdev, 0);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 	pm_cfb_setup(sp->cons_stdout_path);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate  * consconfig_prepare_dev:
9400Sstevel@tonic-gate  * 	Flush the stream, push "pushmod" onto the stream.
9410Sstevel@tonic-gate  * 	for keyboard, issue the KIOCTRANSABLE ioctl, and
9420Sstevel@tonic-gate  *	possible enable abort.
9430Sstevel@tonic-gate  */
9440Sstevel@tonic-gate static void
9450Sstevel@tonic-gate consconfig_prepare_dev(
9460Sstevel@tonic-gate     ldi_handle_t	new_lh,
9470Sstevel@tonic-gate     const char		*pushmod,
9480Sstevel@tonic-gate     int			kbdtranslatable,
9490Sstevel@tonic-gate     int			input_type,
9500Sstevel@tonic-gate     int			dev_type)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	int err, rval;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	/* send a flush down the stream to the keyboard driver */
9550Sstevel@tonic-gate 	(void) ldi_ioctl(new_lh, I_FLUSH, (intptr_t)FLUSHRW,
9560Sstevel@tonic-gate 	    FKIOCTL, kcred, &rval);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	if (pushmod) {
9590Sstevel@tonic-gate 		err = ldi_ioctl(new_lh, I_PUSH, (intptr_t)pushmod,
9600Sstevel@tonic-gate 		    FKIOCTL, kcred, &rval);
9610Sstevel@tonic-gate 		if (err) {
9620Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_prepare_dev: "
9630Sstevel@tonic-gate 			    "can't push streams module \"%s\", error %d",
9640Sstevel@tonic-gate 			    pushmod, err);
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	if (dev_type == CONS_MS)
9690Sstevel@tonic-gate 		return;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	ASSERT(dev_type == CONS_KBD);
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	err = ldi_ioctl(new_lh, KIOCTRANSABLE,
9740Sstevel@tonic-gate 	    (intptr_t)&kbdtranslatable, FKIOCTL, kcred, &rval);
9750Sstevel@tonic-gate 	if (err) {
9760Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig_prepare_dev: "
9770Sstevel@tonic-gate 		    "KIOCTRANSABLE failed, error: %d", err);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * During boot, dynamic_console_config() will call the
9820Sstevel@tonic-gate 	 * function to enable abort on the console.  If the
9830Sstevel@tonic-gate 	 * keyboard is hotplugged after boot, check to see if
9840Sstevel@tonic-gate 	 * the keyboard is the console input.  If it is
9850Sstevel@tonic-gate 	 * enable abort on it.
9860Sstevel@tonic-gate 	 */
9870Sstevel@tonic-gate 	if (input_type == CONSOLE_LOCAL)
9880Sstevel@tonic-gate 		(void) consconfig_kbd_abort_enable(new_lh);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * consconfig_relink_conskbd:
9930Sstevel@tonic-gate  * 	If new_lh is not NULL it should represent a driver with a
9940Sstevel@tonic-gate  * 	keyboard module pushed on top of it. The driver is then linked
9950Sstevel@tonic-gate  * 	underneath conskbd.  the resulting stream will be
9960Sstevel@tonic-gate  *	wc->conskbd->"new_lh driver".
9970Sstevel@tonic-gate  *
9980Sstevel@tonic-gate  * 	If new_lh is NULL, then an unlink operation is done on conskbd
9990Sstevel@tonic-gate  * 	that attempts to unlink the stream specified by *muxid.
10000Sstevel@tonic-gate  *	the resulting stream will be wc->conskbd.
10010Sstevel@tonic-gate  */
10020Sstevel@tonic-gate static int
10030Sstevel@tonic-gate consconfig_relink_conskbd(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
10040Sstevel@tonic-gate {
10050Sstevel@tonic-gate 	int		err, rval;
10060Sstevel@tonic-gate 	int		conskbd_relink = 0;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	ASSERT(muxid != NULL);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consconfig_relink_conskbd: "
10110Sstevel@tonic-gate 	    "conskbd_lh = %p, new_lh = %p,  muxid = %x\n",
10120Sstevel@tonic-gate 	    sp->conskbd_lh, new_lh, *muxid);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * If conskbd is linked under wc then temporarily unlink it
10160Sstevel@tonic-gate 	 * from under wc so that the new_lh stream may be linked under
10170Sstevel@tonic-gate 	 * conskbd.  This has to be done because streams are built bottom
10180Sstevel@tonic-gate 	 * up and linking a stream under conskbd isn't allowed when
10190Sstevel@tonic-gate 	 * conskbd is linked under wc.
10200Sstevel@tonic-gate 	 */
10210Sstevel@tonic-gate 	if (sp->conskbd_muxid != -1) {
10220Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "unlinking conskbd from under wc\n");
10230Sstevel@tonic-gate 		conskbd_relink = 1;
10240Sstevel@tonic-gate 		err = consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid);
10250Sstevel@tonic-gate 		if (err) {
10260Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
10270Sstevel@tonic-gate 			    "wc unlink failed, error %d", err);
10280Sstevel@tonic-gate 			return (err);
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	if (new_lh != NULL) {
10330Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "linking keyboard under conskbd\n");
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		/* Link the stream represented by new_lh under conskbd */
10360Sstevel@tonic-gate 		err = ldi_ioctl(sp->conskbd_lh, I_PLINK, (uintptr_t)new_lh,
10370Sstevel@tonic-gate 				FKIOCTL, kcred, muxid);
10380Sstevel@tonic-gate 		if (err != 0) {
10390Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
10400Sstevel@tonic-gate 			    "conskbd link failed, error %d", err);
10410Sstevel@tonic-gate 			goto relink_failed;
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 	} else {
10440Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "unlinking keyboard from under conskbd\n");
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		/*
10470Sstevel@tonic-gate 		 * This will cause the keyboard driver to be closed,
10480Sstevel@tonic-gate 		 * all modules to be popped, and the keyboard vnode released.
10490Sstevel@tonic-gate 		 */
10500Sstevel@tonic-gate 		err = ldi_ioctl(sp->conskbd_lh, I_PUNLINK, *muxid,
10510Sstevel@tonic-gate 				FKIOCTL, kcred, &rval);
10520Sstevel@tonic-gate 		if (err != 0) {
10530Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
10540Sstevel@tonic-gate 			    "conskbd unlink failed, error %d", err);
10550Sstevel@tonic-gate 			goto relink_failed;
10560Sstevel@tonic-gate 		} else {
10570Sstevel@tonic-gate 			*muxid = -1;
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		consconfig_check_phys_kbd(sp);
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	if (!conskbd_relink)
10640Sstevel@tonic-gate 		return (err);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	/*
10670Sstevel@tonic-gate 	 * Link consbkd back under wc.
10680Sstevel@tonic-gate 	 *
10690Sstevel@tonic-gate 	 * The act of linking conskbd back under wc will cause wc
10700Sstevel@tonic-gate 	 * to query the lower lower layers about their polled I/O
10710Sstevel@tonic-gate 	 * routines.  This time the request will succeed because there
10720Sstevel@tonic-gate 	 * is a physical keyboard linked under conskbd.
10730Sstevel@tonic-gate 	 */
10740Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n");
10750Sstevel@tonic-gate 	err = consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
10760Sstevel@tonic-gate 	if (err) {
10770Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig_relink_conskbd: "
10780Sstevel@tonic-gate 		    "wc link failed, error %d", err);
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 	return (err);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate relink_failed:
10830Sstevel@tonic-gate 	if (!conskbd_relink)
10840Sstevel@tonic-gate 		return (err);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/* something went wrong, try to reconnect conskbd back under wc */
10870Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n");
10880Sstevel@tonic-gate 	(void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
10890Sstevel@tonic-gate 	return (err);
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate  * consconfig_relink_consms:
10940Sstevel@tonic-gate  * 	If new_lh is not NULL it should represent a driver with a
10950Sstevel@tonic-gate  * 	mouse module pushed on top of it. The driver is then linked
10960Sstevel@tonic-gate  * 	underneath consms.  the resulting stream will be
10970Sstevel@tonic-gate  *	consms->"new_lh driver".
10980Sstevel@tonic-gate  *
10990Sstevel@tonic-gate  * 	If new_lh is NULL, then an unlink operation is done on consms
11000Sstevel@tonic-gate  * 	that attempts to unlink the stream specified by *muxid.
11010Sstevel@tonic-gate  */
11020Sstevel@tonic-gate static int
11030Sstevel@tonic-gate consconfig_relink_consms(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate 	int		err, rval;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "consconfig_relink_consms: "
11080Sstevel@tonic-gate 	    "consms_lh = %p, new_lh = %p,  muxid = %x\n",
11090Sstevel@tonic-gate 	    (void *)sp->consms_lh, (void *)new_lh, *muxid);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	if (new_lh != NULL) {
11120Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "linking mouse under consms\n");
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 		/* Link ms/usbms stream underneath consms multiplexor. */
11150Sstevel@tonic-gate 		err = ldi_ioctl(sp->consms_lh, I_PLINK, (uintptr_t)new_lh,
11160Sstevel@tonic-gate 		    FKIOCTL, kcred, muxid);
11170Sstevel@tonic-gate 		if (err != 0) {
11180Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_consms: "
11190Sstevel@tonic-gate 			    "mouse link failed, error %d", err);
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate 	} else {
11220Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "unlinking mouse from under consms\n");
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		/* Tear down the mouse stream */
11250Sstevel@tonic-gate 		err = ldi_ioctl(sp->consms_lh, I_PUNLINK, *muxid,
11260Sstevel@tonic-gate 		    FKIOCTL, kcred, &rval);
11270Sstevel@tonic-gate 		if (err != 0) {
11280Sstevel@tonic-gate 			cmn_err(CE_WARN, "consconfig_relink_consms: "
11290Sstevel@tonic-gate 			    "mouse unlink failed, error %d", err);
11300Sstevel@tonic-gate 		} else {
11310Sstevel@tonic-gate 			*muxid = -1;
11320Sstevel@tonic-gate 		}
11330Sstevel@tonic-gate 	}
11340Sstevel@tonic-gate 	return (err);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate static int
11380Sstevel@tonic-gate cons_get_input_type(void)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	int type;
11410Sstevel@tonic-gate 	/*
11420Sstevel@tonic-gate 	 * Now that we know what all the devices are, we can figure out
11430Sstevel@tonic-gate 	 * what kind of console we have.
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	if (plat_stdin_is_keyboard())  {
11460Sstevel@tonic-gate 		/* Stdin is from the system keyboard */
11470Sstevel@tonic-gate 		type = CONSOLE_LOCAL;
11480Sstevel@tonic-gate 	} else if ((stdindev != NODEV) && (stdindev == stdoutdev)) {
11490Sstevel@tonic-gate 		/*
11500Sstevel@tonic-gate 		 * A reliable indicator that we are doing a remote console
11510Sstevel@tonic-gate 		 * is that stdin and stdout are the same.
11520Sstevel@tonic-gate 		 * This is probably a tip line.
11530Sstevel@tonic-gate 		 */
11540Sstevel@tonic-gate 		type = CONSOLE_TIP;
11550Sstevel@tonic-gate 	} else {
11560Sstevel@tonic-gate 		type = CONSOLE_SERIAL_KEYBOARD;
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	return (type);
11600Sstevel@tonic-gate }
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate static void
11630Sstevel@tonic-gate consconfig_init_input(cons_state_t *sp)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate 	ldi_handle_t	new_lh;
11660Sstevel@tonic-gate 	dev_t		cons_final_dev;
11670Sstevel@tonic-gate 	int		err;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	cons_final_dev = NODEV;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	switch (sp->cons_input_type) {
11720Sstevel@tonic-gate 	case CONSOLE_LOCAL:
11730Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "stdin is keyboard\n");
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 		/*
11760Sstevel@tonic-gate 		 * The machine is allowed to boot without a keyboard.
11770Sstevel@tonic-gate 		 * If a user attaches a keyboard later, the keyboard
11780Sstevel@tonic-gate 		 * will be hooked into the console stream with the dacf
11790Sstevel@tonic-gate 		 * functions.
11800Sstevel@tonic-gate 		 *
11810Sstevel@tonic-gate 		 * The only drivers that look at kbbdev are the
11820Sstevel@tonic-gate 		 * serial drivers, which looks at kbdev to see if
11830Sstevel@tonic-gate 		 * they should allow abort on a break. In the absence
11840Sstevel@tonic-gate 		 * of keyboard, the serial drivers won't be attached
11850Sstevel@tonic-gate 		 * for any keyboard instance.
11860Sstevel@tonic-gate 		 */
11870Sstevel@tonic-gate 		if (kbddev == NODEV) {
11880Sstevel@tonic-gate 			/*
11890Sstevel@tonic-gate 			 * If there is a problem with the keyboard
11900Sstevel@tonic-gate 			 * during the driver loading, then the polled
11910Sstevel@tonic-gate 			 * input won't get setup properly if polled
11920Sstevel@tonic-gate 			 * input is needed.  This means that if the
11930Sstevel@tonic-gate 			 * keyboard is hotplugged, the keyboard would
11940Sstevel@tonic-gate 			 * work normally, but going down to the
11950Sstevel@tonic-gate 			 * debugger would not work if polled input is
11960Sstevel@tonic-gate 			 * required.  This field is set here.  The next
11970Sstevel@tonic-gate 			 * time a keyboard is plugged in, the field is
11980Sstevel@tonic-gate 			 * checked in order to give the next keyboard a
11990Sstevel@tonic-gate 			 * chance at being registered for console
12000Sstevel@tonic-gate 			 * input.
12010Sstevel@tonic-gate 			 *
12020Sstevel@tonic-gate 			 * Although this code will rarely be needed,
12030Sstevel@tonic-gate 			 * USB keyboards can be flaky, so this code
12040Sstevel@tonic-gate 			 * will be useful on the occasion that the
12050Sstevel@tonic-gate 			 * keyboard doesn't enumerate when the drivers
12060Sstevel@tonic-gate 			 * are loaded.
12070Sstevel@tonic-gate 			 */
12080Sstevel@tonic-gate 			DPRINTF(DPRINT_L2, "Error with console keyboard\n");
12090Sstevel@tonic-gate 			sp->cons_keyboard_problem = B_TRUE;
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 		stdindev = kbddev;
12120Sstevel@tonic-gate 		cons_final_dev = sp->cons_wc_vp->v_rdev;
12130Sstevel@tonic-gate 		break;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	case CONSOLE_TIP:
12160Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "console input is tty (%s)\n",
12170Sstevel@tonic-gate 		    sp->cons_stdin_path);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		/*
12200Sstevel@tonic-gate 		 * Console device drivers must be able to output
12210Sstevel@tonic-gate 		 * after being closed.
12220Sstevel@tonic-gate 		 */
12230Sstevel@tonic-gate 		rconsvp = i_consconfig_createvp(sp->cons_stdin_path);
12240Sstevel@tonic-gate 		if (rconsvp == NULL)
12250Sstevel@tonic-gate 			cmn_err(CE_PANIC, "consconfig_init_input: "
12260Sstevel@tonic-gate 			    "unable to find stdin device (%s)",
12270Sstevel@tonic-gate 			    sp->cons_stdin_path);
12280Sstevel@tonic-gate 		rconsdev = rconsvp->v_rdev;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 		ASSERT(rconsdev == stdindev);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		cons_final_dev = rconsdev;
12330Sstevel@tonic-gate 		break;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	case CONSOLE_SERIAL_KEYBOARD:
12360Sstevel@tonic-gate 		DPRINTF(DPRINT_L0, "stdin is serial keyboard\n");
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 		/*
12390Sstevel@tonic-gate 		 * Non-keyboard input device, but not rconsdev.
12400Sstevel@tonic-gate 		 * This is known as the "serial keyboard" case - the
12410Sstevel@tonic-gate 		 * most likely use is someone has a keyboard attached
12420Sstevel@tonic-gate 		 * to a serial port (tip) and still has output on a
12430Sstevel@tonic-gate 		 * framebuffer.
12440Sstevel@tonic-gate 		 *
12450Sstevel@tonic-gate 		 * In this case, the serial driver must be linked
12460Sstevel@tonic-gate 		 * directly beneath wc.  Since conskbd was linked
12470Sstevel@tonic-gate 		 * underneath wc above, first we unlink conskbd.
12480Sstevel@tonic-gate 		 */
12490Sstevel@tonic-gate 		(void) consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid);
12500Sstevel@tonic-gate 		sp->conskbd_muxid = -1;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		/*
12530Sstevel@tonic-gate 		 * Open the serial keyboard, configure it,
12540Sstevel@tonic-gate 		 * and link it underneath wc.
12550Sstevel@tonic-gate 		 */
12560Sstevel@tonic-gate 		err = ldi_open_by_name(sp->cons_stdin_path,
12570Sstevel@tonic-gate 		    FREAD|FWRITE|FNOCTTY, kcred, &new_lh, sp->cons_li);
12580Sstevel@tonic-gate 		if (err == 0) {
12590Sstevel@tonic-gate 			struct termios	termios;
12600Sstevel@tonic-gate 			int		rval;
12610Sstevel@tonic-gate 			int		stdin_muxid;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 			consconfig_prepare_dev(new_lh,
12640Sstevel@tonic-gate 			    "kb", TR_CANNOT, sp->cons_input_type, CONS_KBD);
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 			/* Re-set baud rate */
12670Sstevel@tonic-gate 			(void) ldi_ioctl(new_lh, TCGETS, (intptr_t)&termios,
12680Sstevel@tonic-gate 						FKIOCTL, kcred, &rval);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 			/* Set baud rate */
12710Sstevel@tonic-gate 			if (consconfig_setmodes(stdindev, &termios) == 0) {
12720Sstevel@tonic-gate 				err = ldi_ioctl(new_lh,
12730Sstevel@tonic-gate 				    TCSETSF, (intptr_t)&termios,
12740Sstevel@tonic-gate 				    FKIOCTL, kcred, &rval);
12750Sstevel@tonic-gate 				if (err) {
12760Sstevel@tonic-gate 					cmn_err(CE_WARN,
12770Sstevel@tonic-gate 					    "consconfig_init_input: "
12780Sstevel@tonic-gate 					    "TCSETSF failed, error %d", err);
12790Sstevel@tonic-gate 				}
12800Sstevel@tonic-gate 			}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 			/*
12830Sstevel@tonic-gate 			 * Now link the serial keyboard direcly under wc
12840Sstevel@tonic-gate 			 * we don't save the returned muxid because we
12850Sstevel@tonic-gate 			 * don't support changing/hotplugging the console
12860Sstevel@tonic-gate 			 * keyboard when it is a serial keyboard.
12870Sstevel@tonic-gate 			 */
12880Sstevel@tonic-gate 			(void) consconfig_relink_wc(sp, new_lh, &stdin_muxid);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			(void) ldi_close(new_lh, FREAD|FWRITE, kcred);
12910Sstevel@tonic-gate 		}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 		cons_final_dev = sp->cons_wc_vp->v_rdev;
12940Sstevel@tonic-gate 		break;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	default:
12970Sstevel@tonic-gate 		panic("consconfig_init_input: "
12980Sstevel@tonic-gate 		    "unsupported console input/output combination");
12990Sstevel@tonic-gate 		/*NOTREACHED*/
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
13030Sstevel@tonic-gate 	 * Use the redirection device/workstation console pair as the "real"
13040Sstevel@tonic-gate 	 * console if the latter hasn't already been set.
13050Sstevel@tonic-gate 	 * The workstation console driver needs to see rwsconsvp, but
13060Sstevel@tonic-gate 	 * all other access should be through the redirecting driver.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	if (rconsvp == NULL) {
13090Sstevel@tonic-gate 		consconfig_dprintf(DPRINT_L0, "setup redirection driver\n");
13100Sstevel@tonic-gate 		rconsvp = wsconsvp;
13110Sstevel@tonic-gate 		rconsdev = wsconsvp->v_rdev;
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	ASSERT(cons_final_dev != NODEV);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	err = ldi_open_by_dev(&cons_final_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY,
13170Sstevel@tonic-gate 	    kcred, &new_lh, sp->cons_li);
13180Sstevel@tonic-gate 	if (err) {
13190Sstevel@tonic-gate 		panic("consconfig_init_input: "
13200Sstevel@tonic-gate 		    "unable to open console device");
13210Sstevel@tonic-gate 		/*NOTREACHED*/
13220Sstevel@tonic-gate 	}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/* Enable abort on the console */
13250Sstevel@tonic-gate 	(void) consconfig_kbd_abort_enable(new_lh);
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/* Now we must close it to make console logins happy */
13280Sstevel@tonic-gate 	(void) ldi_close(new_lh, FREAD|FWRITE, kcred);
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	/* Set up polled input if it is supported by the console device */
13310Sstevel@tonic-gate 	if (plat_use_polled_debug()) {
13320Sstevel@tonic-gate 		/*
13330Sstevel@tonic-gate 		 * In the debug case, register the keyboard polled entry
13340Sstevel@tonic-gate 		 * points, but don't throw the switch in the debugger.  This
13350Sstevel@tonic-gate 		 * allows the polled entry points to be checked by hand
13360Sstevel@tonic-gate 		 */
13370Sstevel@tonic-gate 		consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev);
13380Sstevel@tonic-gate 	} else {
13390Sstevel@tonic-gate 		consconfig_setup_polledio(sp, cons_final_dev);
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	kadb_uses_kernel();
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate  * This function kicks off the console configuration.
13470Sstevel@tonic-gate  * Configure keyboard and mouse. Main entry here.
13480Sstevel@tonic-gate  */
13490Sstevel@tonic-gate void
13500Sstevel@tonic-gate dynamic_console_config(void)
13510Sstevel@tonic-gate {
13520Sstevel@tonic-gate 	cons_state_t *sp;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	/* initialize space.c globals */
13550Sstevel@tonic-gate 	stdindev = NODEV;
13560Sstevel@tonic-gate 	mousedev = NODEV;
13570Sstevel@tonic-gate 	kbddev = NODEV;
13580Sstevel@tonic-gate 	fbdev = NODEV;
13590Sstevel@tonic-gate 	fbvp = NULL;
13600Sstevel@tonic-gate 	fbdip = NULL;
13610Sstevel@tonic-gate 	wsconsvp = NULL;
13620Sstevel@tonic-gate 	rwsconsvp = NULL;
13630Sstevel@tonic-gate 	rwsconsdev = NODEV;
13640Sstevel@tonic-gate 	rconsvp = NULL;
13650Sstevel@tonic-gate 	rconsdev = NODEV;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/* Initialize cons_state_t structure and console device paths */
13680Sstevel@tonic-gate 	sp = consconfig_state_init();
13690Sstevel@tonic-gate 	ASSERT(sp);
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/* Build upper layer of console stream */
13720Sstevel@tonic-gate 	cons_build_upper_layer(sp);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * Load keyboard/mouse drivers. The dacf routines will
13760Sstevel@tonic-gate 	 * plumb the devices into the console stream
13770Sstevel@tonic-gate 	 *
13780Sstevel@tonic-gate 	 * At the conclusion of the ddi_pathname_to_dev_t calls, the keyboard
13790Sstevel@tonic-gate 	 * and mouse drivers are linked into their respective console
13800Sstevel@tonic-gate 	 * streams if the pathnames are valid.
13810Sstevel@tonic-gate 	 */
13820Sstevel@tonic-gate 	consconfig_load_drivers(sp);
13830Sstevel@tonic-gate 	sp->cons_input_type = cons_get_input_type();
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	/*
13860Sstevel@tonic-gate 	 * This is legacy special case code for the "cool" virtual console
13870Sstevel@tonic-gate 	 * for the Starfire project.  Starfire has a dummy "ssp-serial"
13880Sstevel@tonic-gate 	 * node in the OBP device tree and cvc is a pseudo driver.
13890Sstevel@tonic-gate 	 */
13900Sstevel@tonic-gate 	if (sp->cons_stdout_path != NULL && stdindev == NODEV &&
13910Sstevel@tonic-gate 	    strstr(sp->cons_stdout_path, "ssp-serial")) {
13920Sstevel@tonic-gate 		/*
13930Sstevel@tonic-gate 		 * Setup the virtual console driver for Starfire
13940Sstevel@tonic-gate 		 * Note that console I/O will still go through prom for now
13950Sstevel@tonic-gate 		 * (notice we don't open the driver here). The cvc driver
13960Sstevel@tonic-gate 		 * will be activated when /dev/console is opened by init.
13970Sstevel@tonic-gate 		 * During that time, a cvcd daemon will be started that
13980Sstevel@tonic-gate 		 * will open the cvcredirection driver to facilitate
13990Sstevel@tonic-gate 		 * the redirection of console I/O from cvc to cvcd.
14000Sstevel@tonic-gate 		 */
14010Sstevel@tonic-gate 		rconsvp = i_consconfig_createvp(CVC_PATH);
14020Sstevel@tonic-gate 		if (rconsvp == NULL)
14030Sstevel@tonic-gate 			return;
14040Sstevel@tonic-gate 		rconsdev = rconsvp->v_rdev;
14050Sstevel@tonic-gate 		return;
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	rwsconsvp = sp->cons_wc_vp;
14090Sstevel@tonic-gate 	rwsconsdev = sp->cons_wc_vp->v_rdev;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	/* initialize framebuffer, console input, and redirection device  */
14130Sstevel@tonic-gate 	consconfig_init_framebuffer(sp);
14140Sstevel@tonic-gate 	consconfig_init_input(sp);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	DPRINTF(DPRINT_L0,
14170Sstevel@tonic-gate 		"mousedev %lx, kbddev %lx, fbdev %lx, rconsdev %lx\n",
14180Sstevel@tonic-gate 		    mousedev,  kbddev, fbdev, rconsdev);
1419*2191Sszhou 
1420*2191Sszhou 	flush_usb_serial_buf();
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate /*
14250Sstevel@tonic-gate  * Start of DACF interfaces
14260Sstevel@tonic-gate  */
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate /*
14290Sstevel@tonic-gate  * Do the real job for keyboard/mouse auto-configuration.
14300Sstevel@tonic-gate  */
14310Sstevel@tonic-gate static int
14320Sstevel@tonic-gate do_config(cons_state_t *sp, cons_prop_t *prop)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	ldi_handle_t	lh;
14350Sstevel@tonic-gate 	dev_t		dev;
14360Sstevel@tonic-gate 	int		error;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS));
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	dev = prop->cp_dev;
14410Sstevel@tonic-gate 	error = ldi_open_by_dev(&dev, OTYP_CHR,
14420Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY, kcred, &lh, sp->cons_li);
14430Sstevel@tonic-gate 	if (error) {
14440Sstevel@tonic-gate 		return (DACF_FAILURE);
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 	ASSERT(dev == prop->cp_dev);	/* clone not supported */
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * Prepare the new keyboard/mouse driver
14500Sstevel@tonic-gate 	 * to be linked under conskbd/consms.
14510Sstevel@tonic-gate 	 */
14520Sstevel@tonic-gate 	consconfig_prepare_dev(lh, prop->cp_pushmod, TR_CAN,
14530Sstevel@tonic-gate 	    sp->cons_input_type, prop->cp_type);
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	if (prop->cp_type == CONS_KBD) {
14560Sstevel@tonic-gate 		/*
14570Sstevel@tonic-gate 		 * Tell the physical keyboard driver to send
14580Sstevel@tonic-gate 		 * the abort sequences up to the virtual keyboard
14590Sstevel@tonic-gate 		 * driver so that STOP and A (or F1 and A)
14600Sstevel@tonic-gate 		 * can be applied to different keyboards.
14610Sstevel@tonic-gate 		 */
14620Sstevel@tonic-gate 		(void) consconfig_kbd_abort_disable(lh);
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 		/* Link the stream underneath conskbd */
14650Sstevel@tonic-gate 		error = consconfig_relink_conskbd(sp, lh, &prop->cp_muxid);
14660Sstevel@tonic-gate 	} else {
14670Sstevel@tonic-gate 		/* Link the stream underneath consms */
14680Sstevel@tonic-gate 		error = consconfig_relink_consms(sp, lh, &prop->cp_muxid);
14690Sstevel@tonic-gate 	}
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	/*
14720Sstevel@tonic-gate 	 * At this point, the stream is:
14730Sstevel@tonic-gate 	 *	for keyboard:	wc->conskbd->["pushmod"->"kbd_vp driver"]
14740Sstevel@tonic-gate 	 *	for mouse:	consms->["module_name"->]"mouse_avp driver"
14750Sstevel@tonic-gate 	 */
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	/* Close the driver stream, it will stay linked under conskbd */
14780Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, kcred);
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	if (error) {
14810Sstevel@tonic-gate 		return (DACF_FAILURE);
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	return (DACF_SUCCESS);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate static int
14880Sstevel@tonic-gate do_unconfig(cons_state_t *sp, cons_prop_t *prop)
14890Sstevel@tonic-gate {
14900Sstevel@tonic-gate 	ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS));
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	if (prop->cp_type == CONS_KBD)
14930Sstevel@tonic-gate 		return (consconfig_relink_conskbd(sp, NULL, &prop->cp_muxid));
14940Sstevel@tonic-gate 	else
14950Sstevel@tonic-gate 		return (consconfig_relink_consms(sp, NULL, &prop->cp_muxid));
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate static int
14990Sstevel@tonic-gate kb_ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int type)
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate 	major_t			major;
15020Sstevel@tonic-gate 	minor_t			minor;
15030Sstevel@tonic-gate 	dev_t			dev;
15040Sstevel@tonic-gate 	dev_info_t		*dip;
15050Sstevel@tonic-gate 	cons_state_t		*sp;
15060Sstevel@tonic-gate 	cons_prop_t		*prop;
15070Sstevel@tonic-gate 	const char		*pushmod;
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	/*
15100Sstevel@tonic-gate 	 * Retrieve the state information
15110Sstevel@tonic-gate 	 * Some platforms may use the old-style "consconfig" to configure
15120Sstevel@tonic-gate 	 * console stream modules but may also support devices that happen
15130Sstevel@tonic-gate 	 * to match a rule in /etc/dacf.conf.  This will cause a problem
15140Sstevel@tonic-gate 	 * since the console state structure will not be initialized.
15150Sstevel@tonic-gate 	 * In that case, these entry points should silently fail and
15160Sstevel@tonic-gate 	 * permit console to be plumbed later in boot.
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
15190Sstevel@tonic-gate 		return (DACF_FAILURE);
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	dip = dacf_devinfo_node(minor_hdl);
15220Sstevel@tonic-gate 	major = ddi_driver_major(dip);
15230Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
15240Sstevel@tonic-gate 	minor = dacf_minor_number(minor_hdl);
15250Sstevel@tonic-gate 	dev = makedevice(major, minor);
15260Sstevel@tonic-gate 	ASSERT(dev != NODEV);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	DPRINTF(DPRINT_L0, "driver name = \"%s\", dev = 0x%lx, major = 0x%x\n",
15290Sstevel@tonic-gate 	    (char *)dacf_driver_name(minor_hdl), dev, major);
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	/* Access to the global variables is synchronized */
15320Sstevel@tonic-gate 	mutex_enter(&sp->cons_lock);
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	/*
15350Sstevel@tonic-gate 	 * Check if the keyboard/mouse has already configured.
15360Sstevel@tonic-gate 	 */
15370Sstevel@tonic-gate 	if (consconfig_find_dev(sp, dev) != NULL) {
15380Sstevel@tonic-gate 		mutex_exit(&sp->cons_lock);
15390Sstevel@tonic-gate 		return (DACF_SUCCESS);
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	prop = kmem_zalloc(sizeof (cons_prop_t), KM_SLEEP);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	/* Config the new keyboard/mouse device */
15450Sstevel@tonic-gate 	prop->cp_dev = dev;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	pushmod = dacf_get_arg(arg_hdl, "pushmod");
15480Sstevel@tonic-gate 	prop->cp_pushmod = i_ddi_strdup((char *)pushmod, KM_SLEEP);
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	prop->cp_type = type;
15510Sstevel@tonic-gate 	if (do_config(sp, prop) != DACF_SUCCESS) {
15520Sstevel@tonic-gate 		/*
15530Sstevel@tonic-gate 		 * The keyboard/mouse node failed to open.
15540Sstevel@tonic-gate 		 * Set the major and minor numbers to 0 so
15550Sstevel@tonic-gate 		 * kb_unconfig/ms_unconfig won't unconfigure
15560Sstevel@tonic-gate 		 * this node if it is detached.
15570Sstevel@tonic-gate 		 */
15580Sstevel@tonic-gate 		mutex_exit(&sp->cons_lock);
15590Sstevel@tonic-gate 		consconfig_free_prop(prop);
15600Sstevel@tonic-gate 		return (DACF_FAILURE);
15610Sstevel@tonic-gate 	}
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	consconfig_add_dev(sp, prop);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	/*
15660Sstevel@tonic-gate 	 * See if there was a problem with the console keyboard during boot.
15670Sstevel@tonic-gate 	 * If so, try to register polled input for this keyboard.
15680Sstevel@tonic-gate 	 */
15690Sstevel@tonic-gate 	if ((type == CONS_KBD) && (sp->cons_keyboard_problem)) {
15700Sstevel@tonic-gate 		consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev);
15710Sstevel@tonic-gate 		sp->cons_keyboard_problem = B_FALSE;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/* Prevent autodetach due to memory pressure */
15750Sstevel@tonic-gate 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1);
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	mutex_exit(&sp->cons_lock);
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	return (DACF_SUCCESS);
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate static int
15830Sstevel@tonic-gate kb_ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl)
15840Sstevel@tonic-gate {
15850Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg_hdl))
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	major_t		major;
15880Sstevel@tonic-gate 	minor_t		minor;
15890Sstevel@tonic-gate 	dev_t		dev;
15900Sstevel@tonic-gate 	dev_info_t	*dip;
15910Sstevel@tonic-gate 	cons_state_t	*sp;
15920Sstevel@tonic-gate 	cons_prop_t	*prop;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	/*
15950Sstevel@tonic-gate 	 * Retrieve the state information
15960Sstevel@tonic-gate 	 * So if there isn't a state available, then this entry point just
15970Sstevel@tonic-gate 	 * returns.  See note in kb_config().
15980Sstevel@tonic-gate 	 */
15990Sstevel@tonic-gate 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
16000Sstevel@tonic-gate 		return (DACF_SUCCESS);
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	dip = dacf_devinfo_node(minor_hdl);
16030Sstevel@tonic-gate 	major = ddi_driver_major(dip);
16040Sstevel@tonic-gate 	ASSERT(major != (major_t)-1);
16050Sstevel@tonic-gate 	minor = dacf_minor_number(minor_hdl);
16060Sstevel@tonic-gate 	dev = makedevice(major, minor);
16070Sstevel@tonic-gate 	ASSERT(dev != NODEV);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	/*
16100Sstevel@tonic-gate 	 * Check if the keyboard/mouse that is being detached
16110Sstevel@tonic-gate 	 * is the console keyboard/mouse or not.
16120Sstevel@tonic-gate 	 */
16130Sstevel@tonic-gate 	mutex_enter(&sp->cons_lock);
16140Sstevel@tonic-gate 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
16150Sstevel@tonic-gate 		mutex_exit(&sp->cons_lock);
16160Sstevel@tonic-gate 		return (DACF_SUCCESS);
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * This dev may be opened physically and then hotplugged out.
16210Sstevel@tonic-gate 	 */
16220Sstevel@tonic-gate 	if (prop->cp_muxid != -1) {
16230Sstevel@tonic-gate 		(void) do_unconfig(sp, prop);
16240Sstevel@tonic-gate 		consconfig_rem_dev(sp, dev);
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	mutex_exit(&sp->cons_lock);
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	return (DACF_SUCCESS);
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate /*
16330Sstevel@tonic-gate  * This is the post-attach / pre-detach action function for the keyboard
16340Sstevel@tonic-gate  * and mouse. This function is associated with a node type in /etc/dacf.conf.
16350Sstevel@tonic-gate  */
16360Sstevel@tonic-gate static int
16370Sstevel@tonic-gate kb_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
16380Sstevel@tonic-gate {
16390Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	return (kb_ms_config(minor_hdl, arg_hdl, CONS_KBD));
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate static int
16450Sstevel@tonic-gate ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	return (kb_ms_config(minor_hdl, arg_hdl, CONS_MS));
16500Sstevel@tonic-gate }
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate static int
16530Sstevel@tonic-gate kb_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
16540Sstevel@tonic-gate {
16550Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	return (kb_ms_unconfig(minor_hdl, arg_hdl));
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate static int
16610Sstevel@tonic-gate ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
16620Sstevel@tonic-gate {
16630Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags))
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	return (kb_ms_unconfig(minor_hdl, arg_hdl));
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate  * consconfig_link and consconfig_unlink are provided to support
16700Sstevel@tonic-gate  * direct access to physical keyboard/mouse underlying conskbd/
16710Sstevel@tonic-gate  * consms.
16720Sstevel@tonic-gate  * When the keyboard/mouse is opened physically via its device
16730Sstevel@tonic-gate  * file, it will be unlinked from the virtual one, and when it
16740Sstevel@tonic-gate  * is closed physically, it will be linked back under the virtual
16750Sstevel@tonic-gate  * one.
16760Sstevel@tonic-gate  */
16770Sstevel@tonic-gate void
16780Sstevel@tonic-gate consconfig_link(major_t major, minor_t minor)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
16810Sstevel@tonic-gate 	dev_t		dev;
16820Sstevel@tonic-gate 	cons_state_t	*sp;
16830Sstevel@tonic-gate 	cons_prop_t	*prop;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
16860Sstevel@tonic-gate 		return;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	dev = makedevice(major, minor);
16890Sstevel@tonic-gate 	ASSERT(dev != NODEV);
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	mutex_enter(&sp->cons_lock);
16920Sstevel@tonic-gate 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
16930Sstevel@tonic-gate 		mutex_exit(&sp->cons_lock);
16940Sstevel@tonic-gate 		return;
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	if (do_config(sp, prop) != DACF_SUCCESS) {
16980Sstevel@tonic-gate 		(void) ddi_dev_pathname(dev, 0, buf);
16990Sstevel@tonic-gate 		if (prop->cp_type == CONS_KBD)
17000Sstevel@tonic-gate 			cmn_err(CE_WARN, "Failed to relink the keyboard "
17010Sstevel@tonic-gate 			    "(%s) underneath virtual keyboard", buf);
17020Sstevel@tonic-gate 		else
17030Sstevel@tonic-gate 			cmn_err(CE_WARN, "Failed to relink the mouse "
17040Sstevel@tonic-gate 			    "(%s) underneath virtual mouse", buf);
17050Sstevel@tonic-gate 		consconfig_rem_dev(sp, dev);
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	mutex_exit(&sp->cons_lock);
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate int
17130Sstevel@tonic-gate consconfig_unlink(major_t major, minor_t minor)
17140Sstevel@tonic-gate {
17150Sstevel@tonic-gate 	dev_t		dev;
17160Sstevel@tonic-gate 	cons_state_t	*sp;
17170Sstevel@tonic-gate 	cons_prop_t	*prop;
17180Sstevel@tonic-gate 	int		error;
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
17210Sstevel@tonic-gate 		return (DACF_SUCCESS);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	dev = makedevice(major, minor);
17240Sstevel@tonic-gate 	ASSERT(dev != NODEV);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	mutex_enter(&sp->cons_lock);
17270Sstevel@tonic-gate 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
17280Sstevel@tonic-gate 		mutex_exit(&sp->cons_lock);
17290Sstevel@tonic-gate 		return (DACF_FAILURE);
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	error = do_unconfig(sp, prop);
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	/*
17350Sstevel@tonic-gate 	 * Keep this dev on the list, for this dev is still online.
17360Sstevel@tonic-gate 	 */
17370Sstevel@tonic-gate 	mutex_exit(&sp->cons_lock);
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	return (error);
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate /*
17430Sstevel@tonic-gate  * Routine to set baud rate, bits-per-char, parity and stop bits
17440Sstevel@tonic-gate  * on the console line when necessary.
17450Sstevel@tonic-gate  */
17460Sstevel@tonic-gate static int
17470Sstevel@tonic-gate consconfig_setmodes(dev_t dev, struct termios *termiosp)
17480Sstevel@tonic-gate {
17490Sstevel@tonic-gate 	char buf[MAXPATHLEN];
17500Sstevel@tonic-gate 	int len = MAXPATHLEN;
17510Sstevel@tonic-gate 	char name[16];
17520Sstevel@tonic-gate 	int ppos, i, j;
17530Sstevel@tonic-gate 	char *path;
17540Sstevel@tonic-gate 	dev_t tdev;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	/*
17570Sstevel@tonic-gate 	 * First, search for a devalias which matches this dev_t.
17580Sstevel@tonic-gate 	 * Try all of ttya through ttyz until no such alias
17590Sstevel@tonic-gate 	 */
17600Sstevel@tonic-gate 	(void) strcpy(name, "ttya");
17610Sstevel@tonic-gate 	for (i = 0; i < ('z'-'a'); i++) {
17620Sstevel@tonic-gate 		name[3] = 'a' + i; /* increment device name */
17630Sstevel@tonic-gate 		path = get_alias(name, buf);
17640Sstevel@tonic-gate 		if (path == NULL)
17650Sstevel@tonic-gate 			return (1);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 		tdev = ddi_pathname_to_dev_t(path);
17680Sstevel@tonic-gate 		if (tdev == dev)
17690Sstevel@tonic-gate 			break;	/* Exit loop if found */
17700Sstevel@tonic-gate 	}
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	if (i >= ('z'-'a'))
17730Sstevel@tonic-gate 		return (1);		/* If we didn't find it, return */
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	/*
17760Sstevel@tonic-gate 	 * Now that we know which "tty" this corresponds to, retrieve
17770Sstevel@tonic-gate 	 * the "ttya-mode" options property, which tells us how to configure
17780Sstevel@tonic-gate 	 * the line.
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	(void) strcpy(name, "ttya-mode");	/* name of option we want */
17810Sstevel@tonic-gate 	name[3] = 'a' + i;			/* Adjust to correct line */
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	for (j = 0; j < sizeof (buf); j++)
17840Sstevel@tonic-gate 		buf[j] = 0; /* CROCK! */
17850Sstevel@tonic-gate 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_root_node(), 0, name,
17860Sstevel@tonic-gate 	    buf, &len) != DDI_PROP_SUCCESS)
17870Sstevel@tonic-gate 		return (1);	/* if no such option, just return */
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	/* Clear out options we will be setting */
17900Sstevel@tonic-gate 	termiosp->c_cflag &=
17910Sstevel@tonic-gate 	    ~(CSIZE | CBAUD | CBAUDEXT | PARODD | PARENB | CSTOPB);
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	/*
17940Sstevel@tonic-gate 	 * Now, parse the string. Wish I could use sscanf().
17950Sstevel@tonic-gate 	 * Format 9600,8,n,1,-
17960Sstevel@tonic-gate 	 * baud rate, bits-per-char, parity, stop-bits, ignored
17970Sstevel@tonic-gate 	 */
17980Sstevel@tonic-gate 	for (ppos = 0; ppos < (MAXPATHLEN-8); ppos++) { /* Find first comma */
17990Sstevel@tonic-gate 		if ((buf[ppos] == 0) || (buf[ppos] == ','))
18000Sstevel@tonic-gate 			break;
18010Sstevel@tonic-gate 	}
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	if (buf[ppos] != ',') {
18040Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig_setmodes: "
18050Sstevel@tonic-gate 		    "invalid mode string %s", buf);
18060Sstevel@tonic-gate 		return (1);
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	for (i = 0; i < MAX_SPEEDS; i++) {
18100Sstevel@tonic-gate 		if (strncmp(buf, speedtab[i].name, ppos) == 0)
18110Sstevel@tonic-gate 		    break;
18120Sstevel@tonic-gate 	}
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	if (i >= MAX_SPEEDS) {
18150Sstevel@tonic-gate 		cmn_err(CE_WARN,
18160Sstevel@tonic-gate 			"consconfig_setmodes: unrecognized speed in %s", buf);
18170Sstevel@tonic-gate 		return (1);
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	/* Found the baud rate, set it */
18210Sstevel@tonic-gate 	termiosp->c_cflag |= speedtab[i].code & CBAUD;
18220Sstevel@tonic-gate 	if (speedtab[i].code > 16) 			/* cfsetospeed! */
18230Sstevel@tonic-gate 		termiosp->c_cflag |= CBAUDEXT;
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	/* Set bits per character */
18260Sstevel@tonic-gate 	switch (buf[ppos+1]) {
18270Sstevel@tonic-gate 	case '8':
18280Sstevel@tonic-gate 		termiosp->c_cflag |= CS8;
18290Sstevel@tonic-gate 		break;
18300Sstevel@tonic-gate 	case '7':
18310Sstevel@tonic-gate 		termiosp->c_cflag |= CS7;
18320Sstevel@tonic-gate 		break;
18330Sstevel@tonic-gate 	default:
18340Sstevel@tonic-gate 		cmn_err(CE_WARN,
18350Sstevel@tonic-gate 		    "consconfig_setmodes: illegal bits-per-char %s", buf);
18360Sstevel@tonic-gate 		return (1);
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	/* Set parity */
18400Sstevel@tonic-gate 	switch (buf[ppos+3]) {
18410Sstevel@tonic-gate 	case 'o':
18420Sstevel@tonic-gate 		termiosp->c_cflag |= PARENB | PARODD;
18430Sstevel@tonic-gate 		break;
18440Sstevel@tonic-gate 	case 'e':
18450Sstevel@tonic-gate 		termiosp->c_cflag |= PARENB; /* enabled, not odd */
18460Sstevel@tonic-gate 		break;
18470Sstevel@tonic-gate 	case 'n':
18480Sstevel@tonic-gate 		break;	/* not enabled. */
18490Sstevel@tonic-gate 	default:
18500Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig_setmodes: illegal parity %s", buf);
18510Sstevel@tonic-gate 		return (1);
18520Sstevel@tonic-gate 	}
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 	/* Set stop bits */
18550Sstevel@tonic-gate 	switch (buf[ppos+5]) {
18560Sstevel@tonic-gate 	case '1':
18570Sstevel@tonic-gate 		break;	/* No extra stop bit */
18580Sstevel@tonic-gate 	case '2':
18590Sstevel@tonic-gate 		termiosp->c_cflag |= CSTOPB; /* 1 extra stop bit */
18600Sstevel@tonic-gate 		break;
18610Sstevel@tonic-gate 	default:
18620Sstevel@tonic-gate 		cmn_err(CE_WARN, "consconfig_setmodes: "
18630Sstevel@tonic-gate 		    "illegal stop bits %s", buf);
18640Sstevel@tonic-gate 		return (1);
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	return (0);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate /*
18710Sstevel@tonic-gate  * Check to see if underlying keyboard devices are still online,
18720Sstevel@tonic-gate  * if any one is offline now, unlink it.
18730Sstevel@tonic-gate  */
18740Sstevel@tonic-gate static void
18750Sstevel@tonic-gate consconfig_check_phys_kbd(cons_state_t *sp)
18760Sstevel@tonic-gate {
18770Sstevel@tonic-gate 	ldi_handle_t	kb_lh;
18780Sstevel@tonic-gate 	cons_prop_t	*prop;
18790Sstevel@tonic-gate 	int		error;
18800Sstevel@tonic-gate 	int		rval;
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) {
18830Sstevel@tonic-gate 		if ((prop->cp_type != CONS_KBD) || (prop->cp_muxid == -1))
18840Sstevel@tonic-gate 			continue;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		error = ldi_open_by_dev(&prop->cp_dev, OTYP_CHR,
18870Sstevel@tonic-gate 		    FREAD|FWRITE|FNOCTTY, kcred, &kb_lh, sp->cons_li);
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 		if (error) {
18900Sstevel@tonic-gate 			(void) ldi_ioctl(sp->conskbd_lh, I_PUNLINK,
18910Sstevel@tonic-gate 			    prop->cp_muxid, FKIOCTL, kcred, &rval);
18920Sstevel@tonic-gate 			prop->cp_dev = NODEV;
18930Sstevel@tonic-gate 		} else {
18940Sstevel@tonic-gate 			(void) ldi_close(kb_lh, FREAD|FWRITE, kcred);
18950Sstevel@tonic-gate 		}
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	/*
18990Sstevel@tonic-gate 	 * Remove all disconnected keyboards,
19000Sstevel@tonic-gate 	 * whose dev is turned into NODEV above.
19010Sstevel@tonic-gate 	 */
19020Sstevel@tonic-gate 	consconfig_rem_dev(sp, NODEV);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate  * Remove devices according to dev, which may be NODEV
19070Sstevel@tonic-gate  */
19080Sstevel@tonic-gate static void
19090Sstevel@tonic-gate consconfig_rem_dev(cons_state_t *sp, dev_t dev)
19100Sstevel@tonic-gate {
19110Sstevel@tonic-gate 	cons_prop_t *prop;
19120Sstevel@tonic-gate 	cons_prop_t *prev_prop;
19130Sstevel@tonic-gate 	cons_prop_t *tmp_prop;
19140Sstevel@tonic-gate 	cons_prop_t *head_prop;
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	head_prop = NULL;
19170Sstevel@tonic-gate 	prev_prop = NULL;
19180Sstevel@tonic-gate 	for (prop = sp->cons_km_prop; prop != NULL; ) {
19190Sstevel@tonic-gate 		if (prop->cp_dev == dev) {
19200Sstevel@tonic-gate 			tmp_prop = prop->cp_next;
19210Sstevel@tonic-gate 			consconfig_free_prop(prop);
19220Sstevel@tonic-gate 			prop = tmp_prop;
19230Sstevel@tonic-gate 			if (prev_prop)
19240Sstevel@tonic-gate 				prev_prop->cp_next = prop;
19250Sstevel@tonic-gate 		} else {
19260Sstevel@tonic-gate 			if (head_prop == NULL)
19270Sstevel@tonic-gate 				head_prop = prop;
19280Sstevel@tonic-gate 			prev_prop = prop;
19290Sstevel@tonic-gate 			prop = prop->cp_next;
19300Sstevel@tonic-gate 		}
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 	sp->cons_km_prop = head_prop;
19330Sstevel@tonic-gate }
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate /*
19360Sstevel@tonic-gate  * Add a dev according to prop
19370Sstevel@tonic-gate  */
19380Sstevel@tonic-gate static void
19390Sstevel@tonic-gate consconfig_add_dev(cons_state_t *sp, cons_prop_t *prop)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	prop->cp_next = sp->cons_km_prop;
19420Sstevel@tonic-gate 	sp->cons_km_prop = prop;
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate /*
19460Sstevel@tonic-gate  * Find a device from our list according to dev
19470Sstevel@tonic-gate  */
19480Sstevel@tonic-gate static cons_prop_t *
19490Sstevel@tonic-gate consconfig_find_dev(cons_state_t *sp, dev_t dev)
19500Sstevel@tonic-gate {
19510Sstevel@tonic-gate 	cons_prop_t *prop;
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) {
19540Sstevel@tonic-gate 		if (prop->cp_dev == dev)
19550Sstevel@tonic-gate 			break;
19560Sstevel@tonic-gate 	}
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	return (prop);
19590Sstevel@tonic-gate }
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate /*
19620Sstevel@tonic-gate  * Free a cons prop associated with a keyboard or mouse
19630Sstevel@tonic-gate  */
19640Sstevel@tonic-gate static void
19650Sstevel@tonic-gate consconfig_free_prop(cons_prop_t *prop)
19660Sstevel@tonic-gate {
19670Sstevel@tonic-gate 	if (prop->cp_pushmod)
19680Sstevel@tonic-gate 		kmem_free(prop->cp_pushmod, strlen(prop->cp_pushmod) + 1);
19690Sstevel@tonic-gate 	kmem_free(prop, sizeof (cons_prop_t));
19700Sstevel@tonic-gate }
1971*2191Sszhou 
1972*2191Sszhou /*
1973*2191Sszhou  * Boot code can't print to usb serial device. The early boot message
1974*2191Sszhou  * is saved in a buffer at address indicated by "usb-serial-buf".
1975*2191Sszhou  * This function flushes the message to the USB serial line
1976*2191Sszhou  */
1977*2191Sszhou static void
1978*2191Sszhou flush_usb_serial_buf(void)
1979*2191Sszhou {
1980*2191Sszhou 	int rval;
1981*2191Sszhou 	vnode_t *vp;
1982*2191Sszhou 	uint_t usbser_buf;
1983*2191Sszhou 	char *kc, *bc, *usbser_kern_buf;
1984*2191Sszhou 
1985*2191Sszhou 	usbser_buf = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(),
1986*2191Sszhou 	    DDI_PROP_DONTPASS, "usb-serial-buf", 0);
1987*2191Sszhou 
1988*2191Sszhou 	if (usbser_buf == 0)
1989*2191Sszhou 		return;
1990*2191Sszhou 
1991*2191Sszhou 	/*
1992*2191Sszhou 	 * After consconfig() and before userland opens /dev/sysmsg,
1993*2191Sszhou 	 * console I/O is goes to polled I/O entry points.
1994*2191Sszhou 	 *
1995*2191Sszhou 	 * If usb-serial doesn't implement polled I/O, we need
1996*2191Sszhou 	 * to open /dev/console now to get kernel console I/O to work.
1997*2191Sszhou 	 * We also push ttcompat and ldterm explicitly to get the
1998*2191Sszhou 	 * correct output format (autopush isn't set up yet). We
1999*2191Sszhou 	 * ignore push errors because they are non-fatal.
2000*2191Sszhou 	 * Note that opening /dev/console causes rconsvp to be
2001*2191Sszhou 	 * opened as well.
2002*2191Sszhou 	 */
2003*2191Sszhou 	if (cons_polledio == NULL) {
2004*2191Sszhou 		if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY,
2005*2191Sszhou 		    0, &vp, 0, 0) != 0)
2006*2191Sszhou 			return;
2007*2191Sszhou 
2008*2191Sszhou 		if (rconsvp) {
2009*2191Sszhou 			(void) strioctl(rconsvp, __I_PUSH_NOCTTY,
2010*2191Sszhou 			    (intptr_t)"ldterm", FKIOCTL, K_TO_K, kcred, &rval);
2011*2191Sszhou 			(void) strioctl(rconsvp, __I_PUSH_NOCTTY,
2012*2191Sszhou 			    (intptr_t)"ttcompat", FKIOCTL, K_TO_K,
2013*2191Sszhou 			    kcred, &rval);
2014*2191Sszhou 		}
2015*2191Sszhou 	}
2016*2191Sszhou 
2017*2191Sszhou 	/*
2018*2191Sszhou 	 * Copy message to a kernel buffer. Various kernel routines
2019*2191Sszhou 	 * expect buffer to be above kernelbase
2020*2191Sszhou 	 */
2021*2191Sszhou 	kc = usbser_kern_buf = (char *)kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
2022*2191Sszhou 	bc = (char *)(uintptr_t)usbser_buf;
2023*2191Sszhou 	while (*kc++ = *bc++)
2024*2191Sszhou 		;
2025*2191Sszhou 	console_printf("%s", usbser_kern_buf);
2026*2191Sszhou 
2027*2191Sszhou 	kmem_free(usbser_kern_buf, MMU_PAGESIZE);
2028*2191Sszhou }
2029