xref: /titanic_50/usr/src/uts/common/io/consconfig_dacf.c (revision b3700b074e637f8c6991b70754c88a2cfffb246b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This module performs two functions.  First, it kicks off the driver loading
29  * of the console devices during boot in dynamic_console_config().
30  * The loading of the drivers for the console devices triggers the
31  * additional device autoconfiguration to link the drivers into the keyboard
32  * and mouse console streams.
33  *
34  * The second function of this module is to provide the dacf functions
35  * to be called after a driver has attached and before it detaches.  For
36  * example, a driver associated with the keyboard will have kb_config called
37  * after the driver attaches and kb_unconfig before it detaches.  Similar
38  * configuration actions are performed on behalf of minor nodes representing
39  * mice.  The configuration functions for the attach case take a module
40  * name as a parameter.  The module is pushed on top of the driver during
41  * the configuration.
42  *
43  * Although the dacf framework is used to configure all keyboards and mice,
44  * its primary function is to allow keyboard and mouse hotplugging.
45  *
46  * This module supports multiple keyboards and mice at the same time.
47  *
48  * From the kernel perspective, there are roughly three different possible
49  * console configurations.  Across these three configurations, the following
50  * elements are constant:
51  * 	wsconsvp = IWSCN_PATH
52  * 	rwsconsvp = WC_PATH
53  * 	consms -> msdev
54  *
55  * The "->" syntax indicates that the streams device on the right is
56  * linked under the streams device on the left.
57  *
58  * The following lists how the system is configured for different setups:
59  *
60  * stdin is a local keyboard.  use stdin and stdout as the console.
61  * 	sp->cons_input_type = CONSOLE_LOCAL
62  *	rconsvp = IWSCN_PATH
63  *	wc -> conskbd -> kbddev
64  *
65  * stdin is not a keyboard and stdin is the same as stdout.
66  * assume we running on a tip line and use stdin/stdout as the console.
67  * 	sp->cons_input_type = CONSOLE_TIP
68  *	rconsvp = (stdindev/stdoutdev)
69  *	wc -> conskbd -> kbddev
70  *
71  * stdin is not a keyboard device and it's not the same as stdout.
72  * assume we have a serial keyboard hooked up and use it along with
73  * stdout as the console.
74  * 	sp->cons_input_type = CONSOLE_SERIAL_KEYBOARD
75  *	rconsvp = IWSCN_PATH
76  *	wc -> stdindev
77  *	conskbd -> kbddev
78  *
79  * CAVEAT:
80  * 	The above is all true except for one possible Intel configuration.
81  * 	If stdin is set to a local keyboard but stdout is set to something
82  * 	other than the local display (a tip port for example) stdout will
83  * 	still go to the local display.  This is an artifact of the console
84  * 	implementation on intel.
85  */
86 
87 #include <sys/types.h>
88 #include <sys/param.h>
89 #include <sys/cmn_err.h>
90 #include <sys/user.h>
91 #include <sys/vfs.h>
92 #include <sys/vnode.h>
93 #include <sys/pathname.h>
94 #include <sys/systm.h>
95 #include <sys/file.h>
96 #include <sys/stropts.h>
97 #include <sys/stream.h>
98 #include <sys/strsubr.h>
99 
100 #include <sys/consdev.h>
101 #include <sys/console.h>
102 #include <sys/wscons.h>
103 #include <sys/kbio.h>
104 #include <sys/debug.h>
105 #include <sys/reboot.h>
106 #include <sys/termios.h>
107 
108 #include <sys/ddi.h>
109 #include <sys/sunddi.h>
110 #include <sys/sunldi.h>
111 #include <sys/sunndi.h>
112 #include <sys/ndi_impldefs.h>
113 #include <sys/modctl.h>
114 #include <sys/ddi_impldefs.h>
115 #include <sys/ddi_implfuncs.h>
116 #include <sys/promif.h>
117 #include <sys/fs/snode.h>
118 
119 #include <sys/errno.h>
120 #include <sys/devops.h>
121 #include <sys/note.h>
122 
123 #include <sys/tem_impl.h>
124 #include <sys/polled_io.h>
125 #include <sys/kmem.h>
126 #include <sys/dacf.h>
127 #include <sys/consconfig_dacf.h>
128 #include <sys/consplat.h>
129 #include <sys/log.h>
130 #include <sys/disp.h>
131 
132 /*
133  * External global variables
134  */
135 extern vnode_t		*rconsvp;
136 extern dev_t		rwsconsdev;
137 
138 /*
139  * External functions
140  */
141 extern uintptr_t	space_fetch(char *key);
142 extern int		space_store(char *key, uintptr_t ptr);
143 
144 /*
145  * Tasks
146  */
147 static int	kb_config(dacf_infohdl_t, dacf_arghdl_t, int);
148 static int	kb_unconfig(dacf_infohdl_t, dacf_arghdl_t, int);
149 static int	ms_config(dacf_infohdl_t, dacf_arghdl_t, int);
150 static int	ms_unconfig(dacf_infohdl_t, dacf_arghdl_t, int);
151 
152 /*
153  * Internal functions
154  */
155 static int	consconfig_setmodes(dev_t dev, struct termios *termiosp);
156 static void	consconfig_check_phys_kbd(cons_state_t *);
157 static void	consconfig_rem_dev(cons_state_t *, dev_t);
158 static void	consconfig_add_dev(cons_state_t *, cons_prop_t *);
159 static cons_prop_t *consconfig_find_dev(cons_state_t *, dev_t);
160 static void	consconfig_free_prop(cons_prop_t *prop);
161 static void	flush_deferred_console_buf(void);
162 
163 
164 /*
165  * On supported configurations, the firmware defines the keyboard and mouse
166  * paths.  However, during USB development, it is useful to be able to use
167  * the USB keyboard and mouse on machines without full USB firmware support.
168  * These variables may be set in /etc/system according to a machine's
169  * USB configuration.  This module will override the firmware's values
170  * with these.
171  *
172  * NOTE:
173  * The master copies of these variables in the misc/consconfig module.
174  * The reason for this is historic.  In versions of solaris up to and
175  * including solaris 9 the conscole configuration code was split into a
176  * seperate sparc and intel version.  These variables were defined
177  * in misc/consconfig on sparc and dacf/consconfig_dacf on intel.
178  *
179  * Unfortunatly the sparc variables were well documented.
180  * So to aviod breaking either sparc or intel we'll declare the variables
181  * in both modules.  This will allow any /etc/system entries that
182  * users may have to continue working.
183  *
184  * The variables in misc/consconfig will take precedence over the variables
185  * found in this file.  Since we eventually want to remove the variables
186  * local to this this file, if the user set them we'll emmit an error
187  * message telling them they need to set the variables in misc/consconfig
188  * instead.
189  */
190 static char *usb_kb_path = NULL;
191 static char *usb_ms_path = NULL;
192 
193 /*
194  * Access functions in the misc/consconfig module used to retrieve the
195  * values of it local usb_kb_path and usb_ms_path variables
196  */
197 extern char *consconfig_get_usb_kb_path();
198 extern char *consconfig_get_usb_ms_path();
199 
200 /*
201  * Local variables used to store the value of the usb_kb_path and
202  * usb_ms_path variables found in misc/consconfig
203  */
204 static char *consconfig_usb_kb_path = NULL;
205 static char *consconfig_usb_ms_path = NULL;
206 
207 /*
208  * Internal variables
209  */
210 static dev_t		stdoutdev;
211 static cons_state_t	*consconfig_sp;
212 
213 /*
214  * consconfig_errlevel:  debug verbosity; smaller numbers are more
215  * verbose.
216  */
217 int consconfig_errlevel = DPRINT_L3;
218 
219 /*
220  * Baud rate table
221  */
222 static struct speed {
223 	char *name;
224 	int code;
225 } speedtab[] = {
226 	{"0", B0},		{"50", B50},		{"75", B75},
227 	{"110", B110},		{"134", B134},		{"150", B150},
228 	{"200", B200},		{"300", B300},		{"600", B600},
229 	{"1200", B1200},	{"1800", B1800},	{"2400", B2400},
230 	{"4800", B4800},	{"9600", B9600},	{"19200", B19200},
231 	{"38400", B38400},	{"57600", B57600},	{"76800", B76800},
232 	{"115200", B115200},	{"153600", B153600},	{"230400", B230400},
233 	{"307200", B307200},	{"460800", B460800},	{"921600", B921600},
234 	{"", 0}
235 };
236 
237 static const int MAX_SPEEDS = sizeof (speedtab) / sizeof (speedtab[0]);
238 
239 static dacf_op_t kbconfig_op[] = {
240 	{ DACF_OPID_POSTATTACH,	kb_config },
241 	{ DACF_OPID_PREDETACH,	kb_unconfig },
242 	{ DACF_OPID_END,	NULL },
243 };
244 
245 static dacf_op_t msconfig_op[] = {
246 	{ DACF_OPID_POSTATTACH,	ms_config },
247 	{ DACF_OPID_PREDETACH,	ms_unconfig },
248 	{ DACF_OPID_END,	NULL },
249 };
250 
251 static dacf_opset_t opsets[] = {
252 	{ "kb_config",	kbconfig_op },
253 	{ "ms_config",	msconfig_op },
254 	{ NULL,		NULL }
255 };
256 
257 struct dacfsw dacfsw = {
258 	DACF_MODREV_1,
259 	opsets,
260 };
261 
262 static struct modldacf modldacf = {
263 	&mod_dacfops,   /* Type of module */
264 	"Consconfig DACF",
265 	&dacfsw
266 };
267 
268 static struct modlinkage modlinkage = {
269 	MODREV_1, (void *)&modldacf, NULL
270 };
271 
272 int
273 _init(void) {
274 	return (mod_install(&modlinkage));
275 }
276 
277 int
278 _fini(void)
279 {
280 	/*
281 	 * This modules state is held in the kernel by space.c
282 	 * allowing this module to be unloaded.
283 	 */
284 	return (mod_remove(&modlinkage));
285 }
286 
287 int
288 _info(struct modinfo *modinfop)
289 {
290 	return (mod_info(&modlinkage, modinfop));
291 }
292 
293 /*PRINTFLIKE2*/
294 static void consconfig_dprintf(int, const char *, ...)
295     __KPRINTFLIKE(2);
296 
297 static void
298 consconfig_dprintf(int l, const char *fmt, ...)
299 {
300 	va_list ap;
301 
302 #ifndef DEBUG
303 	if (!l) {
304 		return;
305 	}
306 #endif /* DEBUG */
307 	if (l < consconfig_errlevel) {
308 		return;
309 	}
310 
311 	va_start(ap, fmt);
312 	(void) vprintf(fmt, ap);
313 	va_end(ap);
314 }
315 
316 /*
317  * Return a property value for the specified alias in /aliases.
318  */
319 char *
320 get_alias(char *alias, char *buf)
321 {
322 	pnode_t node;
323 	int len;
324 
325 	/* The /aliases node only exists in OBP >= 2.4. */
326 	if ((node = prom_alias_node()) == OBP_BADNODE)
327 		return (NULL);
328 
329 	if ((len = prom_getproplen(node, (caddr_t)alias)) <= 0)
330 		return (NULL);
331 
332 	(void) prom_getprop(node, (caddr_t)alias, (caddr_t)buf);
333 
334 	/*
335 	 * The IEEE 1275 standard specifies that /aliases string property
336 	 * values should be null-terminated.  Unfortunatly the reality
337 	 * is that most aren't and the OBP can't easily be modified to
338 	 * add null termination to these strings.  So we'll add the
339 	 * null termination here.  If the string already contains a
340 	 * null termination character then that's ok too because we'll
341 	 * just be adding a second one.
342 	 */
343 	buf[len] = '\0';
344 
345 	prom_pathname(buf);
346 	return (buf);
347 }
348 
349 /*
350  * i_consconfig_createvp:
351  *	This routine is a convenience routine that is passed a path and returns
352  *	a vnode.
353  */
354 static vnode_t *
355 i_consconfig_createvp(char *path)
356 {
357 	int error;
358 	vnode_t *vp;
359 	char *buf = NULL, *fullpath;
360 
361 	DPRINTF(DPRINT_L0, "i_consconfig_createvp: %s\n", path);
362 	fullpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
363 
364 	if (strchr(path, ':') == NULL) {
365 		/* convert an OBP path to a /devices path */
366 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
367 		if (i_ddi_prompath_to_devfspath(path, buf) != DDI_SUCCESS) {
368 			kmem_free(buf, MAXPATHLEN);
369 			kmem_free(fullpath, MAXPATHLEN);
370 			return (NULL);
371 		}
372 		(void) snprintf(fullpath, MAXPATHLEN, "/devices%s", buf);
373 		kmem_free(buf, MAXPATHLEN);
374 	} else {
375 		/* convert a devfs path to a /devices path */
376 		(void) snprintf(fullpath, MAXPATHLEN, "/devices%s", path);
377 	}
378 
379 	DPRINTF(DPRINT_L0, "lookupname(%s)\n", fullpath);
380 	error = lookupname(fullpath, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
381 	kmem_free(fullpath, MAXPATHLEN);
382 	if (error)
383 		return (NULL);
384 
385 	DPRINTF(DPRINT_L0, "create vnode = 0x%p - dev 0x%lx\n", vp, vp->v_rdev);
386 	ASSERT(vn_matchops(vp, spec_getvnodeops()));
387 
388 	return (vp);
389 }
390 
391 /*
392  * consconfig_print_paths:
393  *	Function to print out the various paths
394  */
395 static void
396 consconfig_print_paths(void)
397 {
398 	char    *path;
399 
400 	if (usb_kb_path != NULL)
401 		cmn_err(CE_WARN,
402 		    "consconfig_dacf:usb_kb_path has been deprecated, "
403 		    "use consconfig:usb_kb_path instead");
404 
405 	if (usb_ms_path != NULL)
406 		cmn_err(CE_WARN,
407 		    "consconfig_dacf:usb_ms_path has been deprecated, "
408 		    "use consconfig:usb_ms_path instead");
409 
410 	if (consconfig_errlevel > DPRINT_L0)
411 		return;
412 
413 	path = NULL;
414 	if (consconfig_usb_kb_path != NULL)
415 		path = consconfig_usb_kb_path;
416 	else if (usb_kb_path != NULL)
417 		path = usb_kb_path;
418 	if (path != NULL)
419 		DPRINTF(DPRINT_L0, "usb keyboard path = %s\n", path);
420 
421 	path = plat_kbdpath();
422 	if (path != NULL)
423 		DPRINTF(DPRINT_L0, "keyboard path = %s\n", path);
424 
425 	path = NULL;
426 	if (consconfig_usb_ms_path != NULL)
427 		path = consconfig_usb_ms_path;
428 	else if (usb_ms_path != NULL)
429 		path = usb_ms_path;
430 	if (path != NULL)
431 		DPRINTF(DPRINT_L0, "usb mouse path = %s\n", path);
432 
433 	path = plat_mousepath();
434 	if (path != NULL)
435 		DPRINTF(DPRINT_L0, "mouse path = %s\n", path);
436 
437 	path = plat_stdinpath();
438 	if (path != NULL)
439 		DPRINTF(DPRINT_L0, "stdin path = %s\n", path);
440 
441 	path = plat_stdoutpath();
442 	if (path != NULL)
443 		DPRINTF(DPRINT_L0, "stdout path = %s\n", path);
444 
445 	path = plat_fbpath();
446 	if (path != NULL)
447 		DPRINTF(DPRINT_L0, "fb path = %s\n", path);
448 }
449 
450 /*
451  * consconfig_kbd_abort_enable:
452  * 	Send the CONSSETABORTENABLE ioctl to the lower layers.  This ioctl
453  * 	will only be sent to the device if it is the console device.
454  * 	This ioctl tells the device to pay attention to abort sequences.
455  * 	In the case of kbtrans, this would tell the driver to pay attention
456  * 	to the two key abort sequences like STOP-A.  In the case of the
457  * 	serial keyboard, it would be an abort sequence like a break.
458  */
459 static int
460 consconfig_kbd_abort_enable(ldi_handle_t lh)
461 {
462 	int	err, rval;
463 
464 	DPRINTF(DPRINT_L0, "consconfig_kbd_abort_enable\n");
465 
466 	err = ldi_ioctl(lh, CONSSETABORTENABLE, (uintptr_t)B_TRUE,
467 	    FKIOCTL, kcred, &rval);
468 	return (err);
469 }
470 
471 /*
472  * consconfig_kbd_abort_disable:
473  * 	Send CONSSETABORTENABLE ioctl to lower layers.  This ioctl
474  * 	will only be sent to the device if it is the console device.
475  * 	This ioctl tells the physical device to ignore abort sequences,
476  * 	and send the sequences up to virtual keyboard(conskbd) so that
477  * 	STOP and A (or F1 and A) can be combined.
478  */
479 static int
480 consconfig_kbd_abort_disable(ldi_handle_t lh)
481 {
482 	int	err, rval;
483 
484 	DPRINTF(DPRINT_L0, "consconfig_kbd_abort_disable\n");
485 
486 	err = ldi_ioctl(lh, CONSSETABORTENABLE, (uintptr_t)B_FALSE,
487 	    FKIOCTL, kcred, &rval);
488 	return (err);
489 }
490 
491 #ifdef _HAVE_TEM_FIRMWARE
492 static int
493 consconfig_tem_supported(cons_state_t *sp)
494 {
495 	dev_t			dev;
496 	dev_info_t		*dip;
497 	int			*int_array;
498 	uint_t			nint;
499 	int			rv = 0;
500 
501 	if (sp->cons_fb_path == NULL)
502 		return (0);
503 
504 	if ((dev = ddi_pathname_to_dev_t(sp->cons_fb_path)) == NODEV)
505 		return (0); /* warning printed later by common code */
506 
507 	/*
508 	 * Here we hold the driver and check "tem-support" property.
509 	 * We're doing this with e_ddi_hold_devi_by_dev and
510 	 * ddi_prop_lookup_int_array without opening the driver since
511 	 * some video cards that don't support the kernel terminal
512 	 * emulator could hang or crash if opened too early during
513 	 * boot.
514 	 */
515 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
516 		cmn_err(CE_WARN, "consconfig: cannot hold fb dev %s",
517 		    sp->cons_fb_path);
518 		return (0);
519 	}
520 
521 	/*
522 	 * Check that the tem-support property exists AND that
523 	 * it is equal to 1.
524 	 */
525 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
526 	    DDI_PROP_DONTPASS, "tem-support", &int_array, &nint) ==
527 	    DDI_SUCCESS) {
528 		if (nint > 0)
529 			rv = int_array[0] == 1;
530 		ddi_prop_free(int_array);
531 	}
532 
533 	ddi_release_devi(dip);
534 
535 	return (rv);
536 }
537 #endif /* _HAVE_TEM_FIRMWARE */
538 
539 /*
540  * consconfig_get_polledio:
541  * 	Query the console with the CONSPOLLEDIO ioctl.
542  * 	The polled I/O routines are used by debuggers to perform I/O while
543  * 	interrupts and normal kernel services are disabled.
544  */
545 static cons_polledio_t *
546 consconfig_get_polledio(ldi_handle_t lh)
547 {
548 	int		err, rval;
549 	struct strioctl	strioc;
550 	cons_polledio_t	*polled_io;
551 
552 	/*
553 	 * Setup the ioctl to be sent down to the lower driver.
554 	 */
555 	strioc.ic_cmd = CONSOPENPOLLEDIO;
556 	strioc.ic_timout = INFTIM;
557 	strioc.ic_len = sizeof (polled_io);
558 	strioc.ic_dp = (char *)&polled_io;
559 
560 	/*
561 	 * Send the ioctl to the driver.  The ioctl will wait for
562 	 * the response to come back from wc.  wc has already issued
563 	 * the CONSOPENPOLLEDIO to the lower layer driver.
564 	 */
565 	err = ldi_ioctl(lh, I_STR, (intptr_t)&strioc, FKIOCTL, kcred, &rval);
566 
567 	if (err != 0) {
568 		/*
569 		 * If the lower driver does not support polled I/O, then
570 		 * return NULL.  This will be the case if the driver does
571 		 * not handle polled I/O, or OBP is going to handle polled
572 		 * I/O for the device.
573 		 */
574 
575 		return (NULL);
576 	}
577 
578 	/*
579 	 * Return the polled I/O structure.
580 	 */
581 	return (polled_io);
582 }
583 
584 /*
585  * consconfig_setup_polledio:
586  * 	This routine does the setup work for polled I/O.  First we get
587  * 	the polled_io structure from the lower layers
588  * 	and then we register the polled I/O
589  * 	callbacks with the debugger that will be using them.
590  */
591 static void
592 consconfig_setup_polledio(cons_state_t *sp, dev_t dev)
593 {
594 	cons_polledio_t		*polled_io;
595 	ldi_handle_t		lh;
596 
597 	DPRINTF(DPRINT_L0, "consconfig_setup_polledio: start\n");
598 
599 
600 	if (ldi_open_by_dev(&dev, OTYP_CHR,
601 	    FREAD|FWRITE|FNOCTTY, kcred, &lh, sp->cons_li) != 0)
602 		return;
603 
604 
605 	/*
606 	 * Get the polled io routines so that we can use this
607 	 * device with the debuggers.
608 	 */
609 	polled_io = consconfig_get_polledio(lh);
610 
611 	/*
612 	 * If the get polledio failed, then we do not want to throw
613 	 * the polled I/O switch.
614 	 */
615 	if (polled_io == NULL) {
616 		DPRINTF(DPRINT_L0,
617 		    "consconfig_setup_polledio: get_polledio failed\n");
618 		(void) ldi_close(lh, FREAD|FWRITE, kcred);
619 		return;
620 	}
621 
622 	/* Initialize the polled input */
623 	polled_io_init();
624 
625 	/* Register the callbacks */
626 	DPRINTF(DPRINT_L0,
627 	    "consconfig_setup_polledio: registering callbacks\n");
628 	(void) polled_io_register_callbacks(polled_io, 0);
629 
630 	(void) ldi_close(lh, FREAD|FWRITE, kcred);
631 
632 	DPRINTF(DPRINT_L0, "consconfig_setup_polledio: end\n");
633 }
634 
635 static cons_state_t *
636 consconfig_state_init(void)
637 {
638 	cons_state_t	*sp;
639 	int		rval;
640 
641 	/* Initialize console information */
642 	sp = kmem_zalloc(sizeof (cons_state_t), KM_SLEEP);
643 	sp->cons_keyboard_problem = B_FALSE;
644 
645 	mutex_init(&sp->cons_lock, NULL, MUTEX_DRIVER, NULL);
646 
647 	/* check if consconfig:usb_kb_path is set in /etc/system */
648 	consconfig_usb_kb_path = consconfig_get_usb_kb_path();
649 
650 	/* check if consconfig:usb_ms_path is set in /etc/system */
651 	consconfig_usb_ms_path = consconfig_get_usb_ms_path();
652 
653 	consconfig_print_paths();
654 
655 	/* init external globals */
656 	stdoutdev = NODEV;
657 
658 	/*
659 	 * Find keyboard, mouse, stdin and stdout devices, if they
660 	 * exist on this platform.
661 	 */
662 
663 	if (consconfig_usb_kb_path != NULL) {
664 		sp->cons_keyboard_path = consconfig_usb_kb_path;
665 	} else if (usb_kb_path != NULL) {
666 		sp->cons_keyboard_path = usb_kb_path;
667 	} else {
668 		sp->cons_keyboard_path = plat_kbdpath();
669 	}
670 
671 	if (consconfig_usb_ms_path != NULL) {
672 		sp->cons_mouse_path = consconfig_usb_ms_path;
673 	} else if (usb_ms_path != NULL) {
674 		sp->cons_mouse_path = usb_ms_path;
675 	} else {
676 		sp->cons_mouse_path = plat_mousepath();
677 	}
678 
679 	/* Identify the stdout driver */
680 	sp->cons_stdout_path = plat_stdoutpath();
681 	sp->cons_stdout_is_fb = plat_stdout_is_framebuffer();
682 
683 	sp->cons_stdin_is_kbd = plat_stdin_is_keyboard();
684 
685 	if (sp->cons_stdin_is_kbd &&
686 	    (usb_kb_path != NULL || consconfig_usb_kb_path != NULL))  {
687 		sp->cons_stdin_path = sp->cons_keyboard_path;
688 	} else {
689 		/*
690 		 * The standard in device may or may not be the same as
691 		 * the keyboard. Even if the keyboard is not the
692 		 * standard input, the keyboard console stream will
693 		 * still be built if the keyboard alias provided by the
694 		 * firmware exists and is valid.
695 		 */
696 		sp->cons_stdin_path = plat_stdinpath();
697 	}
698 
699 	if (sp->cons_stdout_is_fb) {
700 		sp->cons_fb_path = sp->cons_stdout_path;
701 
702 #ifdef _HAVE_TEM_FIRMWARE
703 		sp->cons_tem_supported = consconfig_tem_supported(sp);
704 
705 		/*
706 		 * Systems which offer a virtual console must use that
707 		 * as a fallback whenever the fb doesn't support tem.
708 		 * Such systems cannot render characters to the screen
709 		 * using OBP.
710 		 */
711 		if (!sp->cons_tem_supported) {
712 			char *path;
713 
714 			if (plat_virtual_console_path(&path) >= 0) {
715 				sp->cons_stdin_is_kbd = 0;
716 				sp->cons_stdout_is_fb = 0;
717 				sp->cons_stdin_path = path;
718 				sp->cons_stdout_path = path;
719 				sp->cons_fb_path = plat_fbpath();
720 
721 				cmn_err(CE_WARN,
722 				    "%s doesn't support terminal emulation "
723 				    "mode; switching to virtual console.",
724 				    sp->cons_fb_path);
725 			}
726 		}
727 #endif /* _HAVE_TEM_FIRMWARE */
728 	} else {
729 		sp->cons_fb_path = plat_fbpath();
730 #ifdef _HAVE_TEM_FIRMWARE
731 		sp->cons_tem_supported = consconfig_tem_supported(sp);
732 #endif /* _HAVE_TEM_FIRMWARE */
733 	}
734 
735 	sp->cons_li = ldi_ident_from_anon();
736 
737 	/* Save the pointer for retrieval by the dacf functions */
738 	rval = space_store("consconfig", (uintptr_t)sp);
739 	ASSERT(rval == 0);
740 
741 	return (sp);
742 }
743 
744 static int
745 consconfig_relink_wc(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
746 {
747 	int		err, rval;
748 	ldi_handle_t	wc_lh;
749 	dev_t		wc_dev;
750 
751 	ASSERT(muxid != NULL);
752 
753 	/*
754 	 * NOTE: we could be in a dacf callback context right now. normally
755 	 * it's not legal to call any ldi_open_*() function from this context
756 	 * because we're currently holding device tree locks and if the
757 	 * ldi_open_*() call could try to acquire other device tree locks
758 	 * (to attach the device we're trying to open.)  if this happens then
759 	 * we could deadlock.  To avoid this situation, during initialization
760 	 * we made sure to grab a hold on the dip of the device we plan to
761 	 * open so that it can never be detached.  Then we use
762 	 * ldi_open_by_dev() to actually open the device since it will see
763 	 * that the device is already attached and held and instead of
764 	 * acquire any locks it will only increase the reference count
765 	 * on the device.
766 	 */
767 	wc_dev = sp->cons_wc_vp->v_rdev;
768 	err = ldi_open_by_dev(&wc_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY,
769 	    kcred, &wc_lh, sp->cons_li);
770 	ASSERT(wc_dev == sp->cons_wc_vp->v_rdev);
771 	if (err) {
772 		cmn_err(CE_WARN, "consconfig_relink_wc: "
773 		    "unable to open wc device");
774 		return (err);
775 	}
776 
777 	if (new_lh != NULL) {
778 		DPRINTF(DPRINT_L0, "linking stream under wc\n");
779 
780 		err = ldi_ioctl(wc_lh, I_PLINK, (uintptr_t)new_lh,
781 		    FKIOCTL, kcred, muxid);
782 		if (err != 0) {
783 			cmn_err(CE_WARN, "consconfig_relink_wc: "
784 			    "wc link failed, error %d", err);
785 		}
786 	} else {
787 		DPRINTF(DPRINT_L0, "unlinking stream from under wc\n");
788 
789 		err = ldi_ioctl(wc_lh, I_PUNLINK, *muxid,
790 		    FKIOCTL, kcred, &rval);
791 		if (err != 0) {
792 			cmn_err(CE_WARN, "consconfig_relink_wc: "
793 			    "wc unlink failed, error %d", err);
794 		} else {
795 			*muxid = -1;
796 		}
797 	}
798 
799 	(void) ldi_close(wc_lh, FREAD|FWRITE, kcred);
800 	return (err);
801 }
802 
803 static void
804 cons_build_upper_layer(cons_state_t *sp)
805 {
806 	ldi_handle_t		wc_lh;
807 	struct strioctl		strioc;
808 	int			rval;
809 	dev_t			dev;
810 	dev_t			wc_dev;
811 
812 	/*
813 	 * Build the wc->conskbd portion of the keyboard console stream.
814 	 * Even if no keyboard is attached to the system, the upper
815 	 * layer of the stream will be built. If the user attaches
816 	 * a keyboard after the system is booted, the keyboard driver
817 	 * and module will be linked under conskbd.
818 	 *
819 	 * Errors are generally ignored here because conskbd and wc
820 	 * are pseudo drivers and should be present on the system.
821 	 */
822 
823 	/* open the console keyboard device.  will never be closed */
824 	if (ldi_open_by_name(CONSKBD_PATH, FREAD|FWRITE|FNOCTTY,
825 	    kcred, &sp->conskbd_lh, sp->cons_li) != 0) {
826 		panic("consconfig: unable to open conskbd device");
827 		/*NOTREACHED*/
828 	}
829 
830 	DPRINTF(DPRINT_L0, "conskbd_lh = %p\n", sp->conskbd_lh);
831 
832 	/* open the console mouse device.  will never be closed */
833 	if (ldi_open_by_name(CONSMS_PATH, FREAD|FWRITE|FNOCTTY,
834 	    kcred, &sp->consms_lh, sp->cons_li) != 0) {
835 		panic("consconfig: unable to open consms device");
836 		/*NOTREACHED*/
837 	}
838 
839 	DPRINTF(DPRINT_L0, "consms_lh = %p\n", sp->consms_lh);
840 
841 	/*
842 	 * Get a vnode for the wc device and then grab a hold on the
843 	 * device dip so it can never detach.  We need to do this now
844 	 * because later we'll have to open the wc device in a context
845 	 * were it isn't safe to acquire any device tree locks (ie, during
846 	 * a dacf callback.)
847 	 */
848 	sp->cons_wc_vp = i_consconfig_createvp(WC_PATH);
849 	if (sp->cons_wc_vp == NULL)
850 		panic("consconfig: unable to find wc device");
851 
852 	if (e_ddi_hold_devi_by_dev(sp->cons_wc_vp->v_rdev, 0) == NULL)
853 		panic("consconfig: unable to hold wc device");
854 
855 	/*
856 	 * Build the wc->conskbd portion of the keyboard console stream.
857 	 * Even if no keyboard is attached to the system, the upper
858 	 * layer of the stream will be built. If the user attaches
859 	 * a keyboard after the system is booted, the keyboard driver
860 	 * and module will be linked under conskbd.
861 	 *
862 	 * The act of linking conskbd under wc will cause wc to
863 	 * query the lower layers about their polled I/O routines
864 	 * using CONSOPENPOLLEDIO.  This will fail on this link because
865 	 * there is not a physical keyboard linked under conskbd.
866 	 *
867 	 * Since conskbd and wc are pseudo drivers, errors are
868 	 * generally ignored when linking and unlinking them.
869 	 */
870 	(void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
871 
872 	/*
873 	 * Get a vnode for the redirection device.  (It has the
874 	 * connection to the workstation console device wired into it,
875 	 * so that it's not necessary to establish the connection
876 	 * here.  If the redirection device is ever generalized to
877 	 * handle multiple client devices, it won't be able to
878 	 * establish the connection itself, and we'll have to do it
879 	 * here.)
880 	 */
881 	wsconsvp = i_consconfig_createvp(IWSCN_PATH);
882 	if (wsconsvp == NULL) {
883 		panic("consconfig: unable to find iwscn device");
884 		/*NOTREACHED*/
885 	}
886 
887 	if (cons_tem_disable)
888 		return;
889 
890 	if (sp->cons_fb_path == NULL) {
891 #if defined(__x86)
892 		if (sp->cons_stdout_is_fb)
893 			cmn_err(CE_WARN, "consconfig: no screen found");
894 #endif
895 		return;
896 	}
897 
898 	/* make sure the frame buffer device exists */
899 	dev = ddi_pathname_to_dev_t(sp->cons_fb_path);
900 	if (dev == NODEV) {
901 		cmn_err(CE_WARN, "consconfig: "
902 		    "cannot find driver for screen device %s",
903 		    sp->cons_fb_path);
904 		return;
905 	}
906 
907 #ifdef _HAVE_TEM_FIRMWARE
908 	/*
909 	 * If the underlying fb device doesn't support terminal emulation,
910 	 * we don't want to open the wc device (below) because it depends
911 	 * on features which aren't available (polled mode io).
912 	 */
913 	if (!sp->cons_tem_supported)
914 		return;
915 #endif /* _HAVE_TEM_FIRMWARE */
916 
917 	/* tell wc to open the frame buffer device */
918 	wc_dev = sp->cons_wc_vp->v_rdev;
919 	if (ldi_open_by_dev(&wc_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY, kcred,
920 	    &wc_lh, sp->cons_li)) {
921 		cmn_err(CE_PANIC, "cons_build_upper_layer: "
922 		    "unable to open wc device");
923 		return;
924 	}
925 	ASSERT(wc_dev == sp->cons_wc_vp->v_rdev);
926 
927 	strioc.ic_cmd = WC_OPEN_FB;
928 	strioc.ic_timout = INFTIM;
929 	strioc.ic_len = strlen(sp->cons_fb_path) + 1;
930 	strioc.ic_dp = sp->cons_fb_path;
931 
932 	if (ldi_ioctl(wc_lh, I_STR, (intptr_t)&strioc,
933 	    FKIOCTL, kcred, &rval) == 0)
934 		consmode = CONS_KFB;
935 	else
936 		cmn_err(CE_WARN,
937 		    "consconfig: terminal emulator failed to initialize");
938 	(void) ldi_close(wc_lh, FREAD|FWRITE, kcred);
939 }
940 
941 static void
942 consconfig_load_drivers(cons_state_t *sp)
943 {
944 	/*
945 	 * Calling ddi_pathname_to_dev_t may cause the USB Host Controller
946 	 * drivers to be loaded. Here we make sure that EHCI is loaded
947 	 * earlier than {U, O}HCI. The order here is important. As
948 	 * we have observed many systems on which hangs occur if the
949 	 * {U,O}HCI companion controllers take over control from the BIOS
950 	 * before EHCI does.  These hangs are also caused by BIOSes leaving
951 	 * interrupt-on-port-change enabled in the ehci controller, so that
952 	 * when uhci/ohci reset themselves, it induces a port change on
953 	 * the ehci companion controller.  Since there's no interrupt handler
954 	 * installed at the time, the moment that interrupt is unmasked, an
955 	 * interrupt storm will occur.	All this is averted when ehci is
956 	 * loaded first.  And now you know..... the REST of the story.
957 	 *
958 	 * Regardless of platform, ehci needs to initialize first to avoid
959 	 * unnecessary connects and disconnects on the companion controller
960 	 * when ehci sets up the routing.
961 	 */
962 	(void) ddi_hold_installed_driver(ddi_name_to_major("ehci"));
963 	(void) ddi_hold_installed_driver(ddi_name_to_major("uhci"));
964 	(void) ddi_hold_installed_driver(ddi_name_to_major("ohci"));
965 
966 	/*
967 	 * The attaching of the drivers will cause the creation of the
968 	 * keyboard and mouse minor nodes, which will in turn trigger the
969 	 * dacf framework to call the keyboard and mouse configuration
970 	 * tasks.  See PSARC/1998/212 for more details about the dacf
971 	 * framework.
972 	 *
973 	 * on sparc, when the console is ttya, zs0 is stdin/stdout, and zs1
974 	 * is kb/mouse.  zs0 must be attached before zs1. The zs driver
975 	 * is written this way and the hardware may depend on this, too.
976 	 * It would be better to enforce this by attaching zs in sibling
977 	 * order with a driver property, such as ddi-attachall.
978 	 */
979 	if (sp->cons_stdin_path != NULL)
980 		stdindev = ddi_pathname_to_dev_t(sp->cons_stdin_path);
981 	if (stdindev == NODEV) {
982 		DPRINTF(DPRINT_L0,
983 		    "!fail to attach stdin: %s\n", sp->cons_stdin_path);
984 	}
985 	if (sp->cons_stdout_path != NULL)
986 		stdoutdev = ddi_pathname_to_dev_t(sp->cons_stdout_path);
987 	if (sp->cons_keyboard_path != NULL)
988 		kbddev = ddi_pathname_to_dev_t(sp->cons_keyboard_path);
989 	if (sp->cons_mouse_path != NULL)
990 		mousedev =  ddi_pathname_to_dev_t(sp->cons_mouse_path);
991 
992 	/*
993 	 * On x86, make sure the fb driver is loaded even if we don't use it
994 	 * for the console. This will ensure that we create a /dev/fb link
995 	 * which is required to start Xorg.
996 	 */
997 #if defined(__x86)
998 	if (sp->cons_fb_path != NULL)
999 		fbdev = ddi_pathname_to_dev_t(sp->cons_fb_path);
1000 #endif
1001 
1002 	DPRINTF(DPRINT_L0, "stdindev %lx, stdoutdev %lx, kbddev %lx, "
1003 	    "mousedev %lx\n", stdindev, stdoutdev, kbddev, mousedev);
1004 }
1005 
1006 #if !defined(__x86)
1007 void
1008 consconfig_virtual_console_vp(cons_state_t *sp)
1009 {
1010 	char    *virtual_cons_path;
1011 
1012 	if (plat_virtual_console_path(&virtual_cons_path) < 0)
1013 		return;
1014 
1015 	DPRINTF(DPRINT_L0, "consconfig_virtual_console_vp: "
1016 	    "virtual console device path %s\n", virtual_cons_path);
1017 
1018 	ASSERT(sp->cons_stdout_path != NULL);
1019 	if (strcmp(virtual_cons_path, sp->cons_stdout_path) == 0) {
1020 		/* virtual console already in use */
1021 		return;
1022 	}
1023 
1024 	vsconsvp = i_consconfig_createvp(virtual_cons_path);
1025 	if (vsconsvp == NULL) {
1026 		cmn_err(CE_WARN, "consconfig_virtual_console_vp: "
1027 		    "unable to find serial virtual console device %s",
1028 		    virtual_cons_path);
1029 			return;
1030 	}
1031 
1032 	(void) e_ddi_hold_devi_by_dev(vsconsvp->v_rdev, 0);
1033 }
1034 #endif
1035 
1036 static void
1037 consconfig_init_framebuffer(cons_state_t *sp)
1038 {
1039 	if (!sp->cons_stdout_is_fb)
1040 		return;
1041 
1042 	DPRINTF(DPRINT_L0, "stdout is framebuffer\n");
1043 	ASSERT(strcmp(sp->cons_fb_path, sp->cons_stdout_path) == 0);
1044 
1045 	/*
1046 	 * Console output is a framebuffer.
1047 	 * Find the framebuffer driver if we can, and make
1048 	 * ourselves a shadow vnode to track it with.
1049 	 */
1050 	fbdev = stdoutdev;
1051 	if (fbdev == NODEV) {
1052 		DPRINTF(DPRINT_L3,
1053 		    "Can't find driver for console framebuffer\n");
1054 	} else {
1055 		/* stdoutdev is valid, of fbvp should exist */
1056 		fbvp = i_consconfig_createvp(sp->cons_stdout_path);
1057 		if (fbvp == NULL) {
1058 			panic("consconfig_init_framebuffer: "
1059 			    "unable to find frame buffer device");
1060 			/*NOTREACHED*/
1061 		}
1062 		ASSERT(fbvp->v_rdev == fbdev);
1063 
1064 		/* console device is never released */
1065 		fbdip = e_ddi_hold_devi_by_dev(fbdev, 0);
1066 	}
1067 	pm_cfb_setup(sp->cons_stdout_path);
1068 }
1069 
1070 /*
1071  * consconfig_prepare_dev:
1072  * 	Flush the stream, push "pushmod" onto the stream.
1073  * 	for keyboard, issue the KIOCTRANSABLE ioctl, and
1074  *	possible enable abort.
1075  */
1076 static void
1077 consconfig_prepare_dev(
1078     ldi_handle_t	new_lh,
1079     const char		*pushmod,
1080     int			kbdtranslatable,
1081     int			input_type,
1082     int			dev_type)
1083 {
1084 	int err, rval;
1085 
1086 	/* send a flush down the stream to the keyboard driver */
1087 	(void) ldi_ioctl(new_lh, I_FLUSH, (intptr_t)FLUSHRW,
1088 	    FKIOCTL, kcred, &rval);
1089 
1090 	if (pushmod) {
1091 		err = ldi_ioctl(new_lh, I_PUSH, (intptr_t)pushmod,
1092 		    FKIOCTL, kcred, &rval);
1093 		if (err) {
1094 			cmn_err(CE_WARN, "consconfig_prepare_dev: "
1095 			    "can't push streams module \"%s\", error %d",
1096 			    pushmod, err);
1097 		}
1098 	}
1099 
1100 	if (dev_type == CONS_MS)
1101 		return;
1102 
1103 	ASSERT(dev_type == CONS_KBD);
1104 
1105 	err = ldi_ioctl(new_lh, KIOCTRANSABLE,
1106 	    (intptr_t)&kbdtranslatable, FKIOCTL, kcred, &rval);
1107 	if (err) {
1108 		cmn_err(CE_WARN, "consconfig_prepare_dev: "
1109 		    "KIOCTRANSABLE failed, error: %d", err);
1110 	}
1111 
1112 	/*
1113 	 * During boot, dynamic_console_config() will call the
1114 	 * function to enable abort on the console.  If the
1115 	 * keyboard is hotplugged after boot, check to see if
1116 	 * the keyboard is the console input.  If it is
1117 	 * enable abort on it.
1118 	 */
1119 	if (input_type == CONSOLE_LOCAL)
1120 		(void) consconfig_kbd_abort_enable(new_lh);
1121 }
1122 
1123 /*
1124  * consconfig_relink_conskbd:
1125  * 	If new_lh is not NULL it should represent a driver with a
1126  * 	keyboard module pushed on top of it. The driver is then linked
1127  * 	underneath conskbd.  the resulting stream will be
1128  *	wc->conskbd->"new_lh driver".
1129  *
1130  * 	If new_lh is NULL, then an unlink operation is done on conskbd
1131  * 	that attempts to unlink the stream specified by *muxid.
1132  *	the resulting stream will be wc->conskbd.
1133  */
1134 static int
1135 consconfig_relink_conskbd(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
1136 {
1137 	int		err, rval;
1138 	int		conskbd_relink = 0;
1139 
1140 	ASSERT(muxid != NULL);
1141 
1142 	DPRINTF(DPRINT_L0, "consconfig_relink_conskbd: "
1143 	    "conskbd_lh = %p, new_lh = %p,  muxid = %x\n",
1144 	    sp->conskbd_lh, new_lh, *muxid);
1145 
1146 	/*
1147 	 * If conskbd is linked under wc then temporarily unlink it
1148 	 * from under wc so that the new_lh stream may be linked under
1149 	 * conskbd.  This has to be done because streams are built bottom
1150 	 * up and linking a stream under conskbd isn't allowed when
1151 	 * conskbd is linked under wc.
1152 	 */
1153 	if (sp->conskbd_muxid != -1) {
1154 		DPRINTF(DPRINT_L0, "unlinking conskbd from under wc\n");
1155 		conskbd_relink = 1;
1156 		err = consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid);
1157 		if (err) {
1158 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1159 			    "wc unlink failed, error %d", err);
1160 			return (err);
1161 		}
1162 	}
1163 
1164 	if (new_lh != NULL) {
1165 		DPRINTF(DPRINT_L0, "linking keyboard under conskbd\n");
1166 
1167 		/* Link the stream represented by new_lh under conskbd */
1168 		err = ldi_ioctl(sp->conskbd_lh, I_PLINK, (uintptr_t)new_lh,
1169 		    FKIOCTL, kcred, muxid);
1170 		if (err != 0) {
1171 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1172 			    "conskbd link failed, error %d", err);
1173 			goto relink_failed;
1174 		}
1175 	} else {
1176 		DPRINTF(DPRINT_L0, "unlinking keyboard from under conskbd\n");
1177 
1178 		/*
1179 		 * This will cause the keyboard driver to be closed,
1180 		 * all modules to be popped, and the keyboard vnode released.
1181 		 */
1182 		err = ldi_ioctl(sp->conskbd_lh, I_PUNLINK, *muxid,
1183 		    FKIOCTL, kcred, &rval);
1184 		if (err != 0) {
1185 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1186 			    "conskbd unlink failed, error %d", err);
1187 			goto relink_failed;
1188 		} else {
1189 			*muxid = -1;
1190 		}
1191 
1192 		consconfig_check_phys_kbd(sp);
1193 	}
1194 
1195 	if (!conskbd_relink)
1196 		return (err);
1197 
1198 	/*
1199 	 * Link consbkd back under wc.
1200 	 *
1201 	 * The act of linking conskbd back under wc will cause wc
1202 	 * to query the lower lower layers about their polled I/O
1203 	 * routines.  This time the request will succeed because there
1204 	 * is a physical keyboard linked under conskbd.
1205 	 */
1206 	DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n");
1207 	err = consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
1208 	if (err) {
1209 		cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1210 		    "wc link failed, error %d", err);
1211 	}
1212 	return (err);
1213 
1214 relink_failed:
1215 	if (!conskbd_relink)
1216 		return (err);
1217 
1218 	/* something went wrong, try to reconnect conskbd back under wc */
1219 	DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n");
1220 	(void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
1221 	return (err);
1222 }
1223 
1224 /*
1225  * consconfig_relink_consms:
1226  * 	If new_lh is not NULL it should represent a driver with a
1227  * 	mouse module pushed on top of it. The driver is then linked
1228  * 	underneath consms.  the resulting stream will be
1229  *	consms->"new_lh driver".
1230  *
1231  * 	If new_lh is NULL, then an unlink operation is done on consms
1232  * 	that attempts to unlink the stream specified by *muxid.
1233  */
1234 static int
1235 consconfig_relink_consms(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
1236 {
1237 	int		err, rval;
1238 
1239 	DPRINTF(DPRINT_L0, "consconfig_relink_consms: "
1240 	    "consms_lh = %p, new_lh = %p,  muxid = %x\n",
1241 	    (void *)sp->consms_lh, (void *)new_lh, *muxid);
1242 
1243 	if (new_lh != NULL) {
1244 		DPRINTF(DPRINT_L0, "linking mouse under consms\n");
1245 
1246 		/* Link ms/usbms stream underneath consms multiplexor. */
1247 		err = ldi_ioctl(sp->consms_lh, I_PLINK, (uintptr_t)new_lh,
1248 		    FKIOCTL, kcred, muxid);
1249 		if (err != 0) {
1250 			cmn_err(CE_WARN, "consconfig_relink_consms: "
1251 			    "mouse link failed, error %d", err);
1252 		}
1253 	} else {
1254 		DPRINTF(DPRINT_L0, "unlinking mouse from under consms\n");
1255 
1256 		/* Tear down the mouse stream */
1257 		err = ldi_ioctl(sp->consms_lh, I_PUNLINK, *muxid,
1258 		    FKIOCTL, kcred, &rval);
1259 		if (err != 0) {
1260 			cmn_err(CE_WARN, "consconfig_relink_consms: "
1261 			    "mouse unlink failed, error %d", err);
1262 		} else {
1263 			*muxid = -1;
1264 		}
1265 	}
1266 	return (err);
1267 }
1268 
1269 static int
1270 cons_get_input_type(cons_state_t *sp)
1271 {
1272 	int type;
1273 
1274 	/*
1275 	 * Now that we know what all the devices are, we can figure out
1276 	 * what kind of console we have.
1277 	 */
1278 	if (sp->cons_stdin_is_kbd)  {
1279 		/* Stdin is from the system keyboard */
1280 		type = CONSOLE_LOCAL;
1281 	} else if ((stdindev != NODEV) && (stdindev == stdoutdev)) {
1282 		/*
1283 		 * A reliable indicator that we are doing a remote console
1284 		 * is that stdin and stdout are the same.
1285 		 * This is probably a tip line.
1286 		 */
1287 		type = CONSOLE_TIP;
1288 	} else {
1289 		type = CONSOLE_SERIAL_KEYBOARD;
1290 	}
1291 
1292 	return (type);
1293 }
1294 
1295 static void
1296 consconfig_init_input(cons_state_t *sp)
1297 {
1298 	ldi_handle_t	new_lh;
1299 	dev_t		cons_final_dev;
1300 	int		err;
1301 
1302 	cons_final_dev = NODEV;
1303 
1304 	switch (sp->cons_input_type) {
1305 	case CONSOLE_LOCAL:
1306 		DPRINTF(DPRINT_L0, "stdin is keyboard\n");
1307 
1308 		/*
1309 		 * The machine is allowed to boot without a keyboard.
1310 		 * If a user attaches a keyboard later, the keyboard
1311 		 * will be hooked into the console stream with the dacf
1312 		 * functions.
1313 		 *
1314 		 * The only drivers that look at kbbdev are the
1315 		 * serial drivers, which looks at kbdev to see if
1316 		 * they should allow abort on a break. In the absence
1317 		 * of keyboard, the serial drivers won't be attached
1318 		 * for any keyboard instance.
1319 		 */
1320 		if (kbddev == NODEV) {
1321 			/*
1322 			 * If there is a problem with the keyboard
1323 			 * during the driver loading, then the polled
1324 			 * input won't get setup properly if polled
1325 			 * input is needed.  This means that if the
1326 			 * keyboard is hotplugged, the keyboard would
1327 			 * work normally, but going down to the
1328 			 * debugger would not work if polled input is
1329 			 * required.  This field is set here.  The next
1330 			 * time a keyboard is plugged in, the field is
1331 			 * checked in order to give the next keyboard a
1332 			 * chance at being registered for console
1333 			 * input.
1334 			 *
1335 			 * Although this code will rarely be needed,
1336 			 * USB keyboards can be flaky, so this code
1337 			 * will be useful on the occasion that the
1338 			 * keyboard doesn't enumerate when the drivers
1339 			 * are loaded.
1340 			 */
1341 			DPRINTF(DPRINT_L2, "Error with console keyboard\n");
1342 			sp->cons_keyboard_problem = B_TRUE;
1343 		}
1344 		stdindev = kbddev;
1345 		cons_final_dev = sp->cons_wc_vp->v_rdev;
1346 		break;
1347 
1348 	case CONSOLE_TIP:
1349 		DPRINTF(DPRINT_L0, "console input is tty (%s)\n",
1350 		    sp->cons_stdin_path);
1351 
1352 		/*
1353 		 * Console device drivers must be able to output
1354 		 * after being closed.
1355 		 */
1356 		rconsvp = i_consconfig_createvp(sp->cons_stdin_path);
1357 		if (rconsvp == NULL) {
1358 			panic("consconfig_init_input: "
1359 			    "unable to find stdin device (%s)",
1360 			    sp->cons_stdin_path);
1361 			/*NOTREACHED*/
1362 		}
1363 		rconsdev = rconsvp->v_rdev;
1364 
1365 		ASSERT(rconsdev == stdindev);
1366 
1367 		cons_final_dev = rconsdev;
1368 		break;
1369 
1370 	case CONSOLE_SERIAL_KEYBOARD:
1371 		DPRINTF(DPRINT_L0, "stdin is serial keyboard\n");
1372 
1373 		/*
1374 		 * Non-keyboard input device, but not rconsdev.
1375 		 * This is known as the "serial keyboard" case - the
1376 		 * most likely use is someone has a keyboard attached
1377 		 * to a serial port (tip) and still has output on a
1378 		 * framebuffer.
1379 		 *
1380 		 * In this case, the serial driver must be linked
1381 		 * directly beneath wc.  Since conskbd was linked
1382 		 * underneath wc above, first we unlink conskbd.
1383 		 */
1384 		(void) consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid);
1385 		sp->conskbd_muxid = -1;
1386 
1387 		/*
1388 		 * Open the serial keyboard, configure it,
1389 		 * and link it underneath wc.
1390 		 */
1391 		err = ldi_open_by_name(sp->cons_stdin_path,
1392 		    FREAD|FWRITE|FNOCTTY, kcred, &new_lh, sp->cons_li);
1393 		if (err == 0) {
1394 			struct termios	termios;
1395 			int		rval;
1396 			int		stdin_muxid;
1397 
1398 			consconfig_prepare_dev(new_lh,
1399 			    "kb", TR_CANNOT, sp->cons_input_type, CONS_KBD);
1400 
1401 			/* Re-set baud rate */
1402 			(void) ldi_ioctl(new_lh, TCGETS, (intptr_t)&termios,
1403 			    FKIOCTL, kcred, &rval);
1404 
1405 			/* Set baud rate */
1406 			if (consconfig_setmodes(stdindev, &termios) == 0) {
1407 				err = ldi_ioctl(new_lh,
1408 				    TCSETSF, (intptr_t)&termios,
1409 				    FKIOCTL, kcred, &rval);
1410 				if (err) {
1411 					cmn_err(CE_WARN,
1412 					    "consconfig_init_input: "
1413 					    "TCSETSF failed, error %d", err);
1414 				}
1415 			}
1416 
1417 			/*
1418 			 * Now link the serial keyboard direcly under wc
1419 			 * we don't save the returned muxid because we
1420 			 * don't support changing/hotplugging the console
1421 			 * keyboard when it is a serial keyboard.
1422 			 */
1423 			(void) consconfig_relink_wc(sp, new_lh, &stdin_muxid);
1424 
1425 			(void) ldi_close(new_lh, FREAD|FWRITE, kcred);
1426 		}
1427 
1428 		cons_final_dev = sp->cons_wc_vp->v_rdev;
1429 		break;
1430 
1431 	default:
1432 		panic("consconfig_init_input: "
1433 		    "unsupported console input/output combination");
1434 		/*NOTREACHED*/
1435 	}
1436 
1437 	/*
1438 	 * Use the redirection device/workstation console pair as the "real"
1439 	 * console if the latter hasn't already been set.
1440 	 * The workstation console driver needs to see rwsconsvp, but
1441 	 * all other access should be through the redirecting driver.
1442 	 */
1443 	if (rconsvp == NULL) {
1444 		consconfig_dprintf(DPRINT_L0, "setup redirection driver\n");
1445 		rconsvp = wsconsvp;
1446 		rconsdev = wsconsvp->v_rdev;
1447 	}
1448 
1449 	ASSERT(cons_final_dev != NODEV);
1450 
1451 	err = ldi_open_by_dev(&cons_final_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY,
1452 	    kcred, &new_lh, sp->cons_li);
1453 	if (err) {
1454 		panic("consconfig_init_input: "
1455 		    "unable to open console device");
1456 		/*NOTREACHED*/
1457 	}
1458 
1459 	/* Enable abort on the console */
1460 	(void) consconfig_kbd_abort_enable(new_lh);
1461 
1462 	/* Now we must close it to make console logins happy */
1463 	(void) ldi_close(new_lh, FREAD|FWRITE, kcred);
1464 
1465 	/* Set up polled input if it is supported by the console device */
1466 	if (plat_use_polled_debug()) {
1467 		/*
1468 		 * In the debug case, register the keyboard polled entry
1469 		 * points, but don't throw the switch in the debugger.  This
1470 		 * allows the polled entry points to be checked by hand
1471 		 */
1472 		consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev);
1473 	} else {
1474 		consconfig_setup_polledio(sp, cons_final_dev);
1475 	}
1476 
1477 	kadb_uses_kernel();
1478 }
1479 
1480 /*
1481  * This function kicks off the console configuration.
1482  * Configure keyboard and mouse. Main entry here.
1483  */
1484 void
1485 dynamic_console_config(void)
1486 {
1487 	/* initialize space.c globals */
1488 	stdindev = NODEV;
1489 	mousedev = NODEV;
1490 	kbddev = NODEV;
1491 	fbdev = NODEV;
1492 	fbvp = NULL;
1493 	fbdip = NULL;
1494 	wsconsvp = NULL;
1495 	rwsconsvp = NULL;
1496 	rwsconsdev = NODEV;
1497 	rconsvp = NULL;
1498 	rconsdev = NODEV;
1499 
1500 	/* Initialize cons_state_t structure and console device paths */
1501 	consconfig_sp = consconfig_state_init();
1502 	ASSERT(consconfig_sp);
1503 
1504 	/* Build upper layer of console stream */
1505 	cons_build_upper_layer(consconfig_sp);
1506 
1507 	/*
1508 	 * Load keyboard/mouse drivers. The dacf routines will
1509 	 * plumb the devices into the console stream
1510 	 *
1511 	 * At the conclusion of the ddi_pathname_to_dev_t calls, the keyboard
1512 	 * and mouse drivers are linked into their respective console
1513 	 * streams if the pathnames are valid.
1514 	 */
1515 	consconfig_load_drivers(consconfig_sp);
1516 	consconfig_sp->cons_input_type = cons_get_input_type(consconfig_sp);
1517 
1518 	/*
1519 	 * This is legacy special case code for the "cool" virtual console
1520 	 * for the Starfire project.  Starfire has a dummy "ssp-serial"
1521 	 * node in the OBP device tree and cvc is a pseudo driver.
1522 	 */
1523 	if (consconfig_sp->cons_stdout_path != NULL && stdindev == NODEV &&
1524 	    strstr(consconfig_sp->cons_stdout_path, "ssp-serial")) {
1525 		/*
1526 		 * Setup the virtual console driver for Starfire
1527 		 * Note that console I/O will still go through prom for now
1528 		 * (notice we don't open the driver here). The cvc driver
1529 		 * will be activated when /dev/console is opened by init.
1530 		 * During that time, a cvcd daemon will be started that
1531 		 * will open the cvcredirection driver to facilitate
1532 		 * the redirection of console I/O from cvc to cvcd.
1533 		 */
1534 		rconsvp = i_consconfig_createvp(CVC_PATH);
1535 		if (rconsvp == NULL)
1536 			goto done;
1537 		rconsdev = rconsvp->v_rdev;
1538 		goto done;
1539 	}
1540 
1541 	rwsconsvp = consconfig_sp->cons_wc_vp;
1542 	rwsconsdev = consconfig_sp->cons_wc_vp->v_rdev;
1543 
1544 
1545 	/* initialize framebuffer, console input, and redirection device  */
1546 	consconfig_init_framebuffer(consconfig_sp);
1547 	consconfig_init_input(consconfig_sp);
1548 
1549 #if !defined(__x86)
1550 	/* initialize virtual console vp for logging if needed */
1551 	consconfig_virtual_console_vp(consconfig_sp);
1552 #endif
1553 
1554 	DPRINTF(DPRINT_L0,
1555 	    "mousedev %lx, kbddev %lx, fbdev %lx, rconsdev %lx\n",
1556 	    mousedev,  kbddev, fbdev, rconsdev);
1557 
1558 	flush_deferred_console_buf();
1559 done:
1560 	consconfig_sp->cons_initialized = B_TRUE;
1561 }
1562 
1563 
1564 /*
1565  * Start of DACF interfaces
1566  */
1567 
1568 /*
1569  * Do the real job for keyboard/mouse auto-configuration.
1570  */
1571 static int
1572 do_config(cons_state_t *sp, cons_prop_t *prop)
1573 {
1574 	ldi_handle_t	lh;
1575 	dev_t		dev;
1576 	int		error;
1577 
1578 	ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS));
1579 
1580 	dev = prop->cp_dev;
1581 	error = ldi_open_by_dev(&dev, OTYP_CHR,
1582 	    FREAD|FWRITE|FNOCTTY, kcred, &lh, sp->cons_li);
1583 	if (error) {
1584 		return (DACF_FAILURE);
1585 	}
1586 	ASSERT(dev == prop->cp_dev);	/* clone not supported */
1587 
1588 	/*
1589 	 * Prepare the new keyboard/mouse driver
1590 	 * to be linked under conskbd/consms.
1591 	 */
1592 	consconfig_prepare_dev(lh, prop->cp_pushmod, TR_CAN,
1593 	    sp->cons_input_type, prop->cp_type);
1594 
1595 	if (prop->cp_type == CONS_KBD) {
1596 		/*
1597 		 * Tell the physical keyboard driver to send
1598 		 * the abort sequences up to the virtual keyboard
1599 		 * driver so that STOP and A (or F1 and A)
1600 		 * can be applied to different keyboards.
1601 		 */
1602 		(void) consconfig_kbd_abort_disable(lh);
1603 
1604 		/* Link the stream underneath conskbd */
1605 		error = consconfig_relink_conskbd(sp, lh, &prop->cp_muxid);
1606 	} else {
1607 		/* Link the stream underneath consms */
1608 		error = consconfig_relink_consms(sp, lh, &prop->cp_muxid);
1609 	}
1610 
1611 	/*
1612 	 * At this point, the stream is:
1613 	 *	for keyboard:	wc->conskbd->["pushmod"->"kbd_vp driver"]
1614 	 *	for mouse:	consms->["module_name"->]"mouse_avp driver"
1615 	 */
1616 
1617 	/* Close the driver stream, it will stay linked under conskbd */
1618 	(void) ldi_close(lh, FREAD|FWRITE, kcred);
1619 
1620 	if (error) {
1621 		return (DACF_FAILURE);
1622 	}
1623 
1624 	return (DACF_SUCCESS);
1625 }
1626 
1627 static int
1628 do_unconfig(cons_state_t *sp, cons_prop_t *prop)
1629 {
1630 	ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS));
1631 
1632 	if (prop->cp_type == CONS_KBD)
1633 		return (consconfig_relink_conskbd(sp, NULL, &prop->cp_muxid));
1634 	else
1635 		return (consconfig_relink_consms(sp, NULL, &prop->cp_muxid));
1636 }
1637 
1638 static int
1639 kb_ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int type)
1640 {
1641 	major_t			major;
1642 	minor_t			minor;
1643 	dev_t			dev;
1644 	dev_info_t		*dip;
1645 	cons_state_t		*sp;
1646 	cons_prop_t		*prop;
1647 	const char		*pushmod;
1648 
1649 	/*
1650 	 * Retrieve the state information
1651 	 * Some platforms may use the old-style "consconfig" to configure
1652 	 * console stream modules but may also support devices that happen
1653 	 * to match a rule in /etc/dacf.conf.  This will cause a problem
1654 	 * since the console state structure will not be initialized.
1655 	 * In that case, these entry points should silently fail and
1656 	 * permit console to be plumbed later in boot.
1657 	 */
1658 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1659 		return (DACF_FAILURE);
1660 
1661 	dip = dacf_devinfo_node(minor_hdl);
1662 	major = ddi_driver_major(dip);
1663 	ASSERT(major != DDI_MAJOR_T_NONE);
1664 	minor = dacf_minor_number(minor_hdl);
1665 	dev = makedevice(major, minor);
1666 	ASSERT(dev != NODEV);
1667 
1668 	DPRINTF(DPRINT_L0, "driver name = \"%s\", dev = 0x%lx, major = 0x%x\n",
1669 	    (char *)dacf_driver_name(minor_hdl), dev, major);
1670 
1671 	/* Access to the global variables is synchronized */
1672 	mutex_enter(&sp->cons_lock);
1673 
1674 	/*
1675 	 * Check if the keyboard/mouse has already configured.
1676 	 */
1677 	if (consconfig_find_dev(sp, dev) != NULL) {
1678 		mutex_exit(&sp->cons_lock);
1679 		return (DACF_SUCCESS);
1680 	}
1681 
1682 	prop = kmem_zalloc(sizeof (cons_prop_t), KM_SLEEP);
1683 
1684 	/* Config the new keyboard/mouse device */
1685 	prop->cp_dev = dev;
1686 
1687 	pushmod = dacf_get_arg(arg_hdl, "pushmod");
1688 	prop->cp_pushmod = i_ddi_strdup((char *)pushmod, KM_SLEEP);
1689 
1690 	prop->cp_type = type;
1691 	if (do_config(sp, prop) != DACF_SUCCESS) {
1692 		/*
1693 		 * The keyboard/mouse node failed to open.
1694 		 * Set the major and minor numbers to 0 so
1695 		 * kb_unconfig/ms_unconfig won't unconfigure
1696 		 * this node if it is detached.
1697 		 */
1698 		mutex_exit(&sp->cons_lock);
1699 		consconfig_free_prop(prop);
1700 		return (DACF_FAILURE);
1701 	}
1702 
1703 	consconfig_add_dev(sp, prop);
1704 
1705 	/*
1706 	 * See if there was a problem with the console keyboard during boot.
1707 	 * If so, try to register polled input for this keyboard.
1708 	 */
1709 	if ((type == CONS_KBD) && (sp->cons_keyboard_problem)) {
1710 		consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev);
1711 		sp->cons_keyboard_problem = B_FALSE;
1712 	}
1713 
1714 	/* Prevent autodetach due to memory pressure */
1715 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1);
1716 
1717 	mutex_exit(&sp->cons_lock);
1718 
1719 	return (DACF_SUCCESS);
1720 }
1721 
1722 static int
1723 kb_ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl)
1724 {
1725 	_NOTE(ARGUNUSED(arg_hdl))
1726 
1727 	major_t		major;
1728 	minor_t		minor;
1729 	dev_t		dev;
1730 	dev_info_t	*dip;
1731 	cons_state_t	*sp;
1732 	cons_prop_t	*prop;
1733 
1734 	/*
1735 	 * Retrieve the state information
1736 	 * So if there isn't a state available, then this entry point just
1737 	 * returns.  See note in kb_config().
1738 	 */
1739 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1740 		return (DACF_SUCCESS);
1741 
1742 	dip = dacf_devinfo_node(minor_hdl);
1743 	major = ddi_driver_major(dip);
1744 	ASSERT(major != DDI_MAJOR_T_NONE);
1745 	minor = dacf_minor_number(minor_hdl);
1746 	dev = makedevice(major, minor);
1747 	ASSERT(dev != NODEV);
1748 
1749 	/*
1750 	 * Check if the keyboard/mouse that is being detached
1751 	 * is the console keyboard/mouse or not.
1752 	 */
1753 	mutex_enter(&sp->cons_lock);
1754 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
1755 		mutex_exit(&sp->cons_lock);
1756 		return (DACF_SUCCESS);
1757 	}
1758 
1759 	/*
1760 	 * This dev may be opened physically and then hotplugged out.
1761 	 */
1762 	if (prop->cp_muxid != -1) {
1763 		(void) do_unconfig(sp, prop);
1764 		consconfig_rem_dev(sp, dev);
1765 	}
1766 
1767 	mutex_exit(&sp->cons_lock);
1768 
1769 	return (DACF_SUCCESS);
1770 }
1771 
1772 /*
1773  * This is the post-attach / pre-detach action function for the keyboard
1774  * and mouse. This function is associated with a node type in /etc/dacf.conf.
1775  */
1776 static int
1777 kb_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1778 {
1779 	_NOTE(ARGUNUSED(flags))
1780 
1781 	return (kb_ms_config(minor_hdl, arg_hdl, CONS_KBD));
1782 }
1783 
1784 static int
1785 ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1786 {
1787 	_NOTE(ARGUNUSED(flags))
1788 
1789 	return (kb_ms_config(minor_hdl, arg_hdl, CONS_MS));
1790 }
1791 
1792 static int
1793 kb_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1794 {
1795 	_NOTE(ARGUNUSED(flags))
1796 
1797 	return (kb_ms_unconfig(minor_hdl, arg_hdl));
1798 }
1799 
1800 static int
1801 ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1802 {
1803 	_NOTE(ARGUNUSED(flags))
1804 
1805 	return (kb_ms_unconfig(minor_hdl, arg_hdl));
1806 }
1807 
1808 /*
1809  * consconfig_link and consconfig_unlink are provided to support
1810  * direct access to physical keyboard/mouse underlying conskbd/
1811  * consms.
1812  * When the keyboard/mouse is opened physically via its device
1813  * file, it will be unlinked from the virtual one, and when it
1814  * is closed physically, it will be linked back under the virtual
1815  * one.
1816  */
1817 void
1818 consconfig_link(major_t major, minor_t minor)
1819 {
1820 	char		buf[MAXPATHLEN];
1821 	dev_t		dev;
1822 	cons_state_t	*sp;
1823 	cons_prop_t	*prop;
1824 
1825 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1826 		return;
1827 
1828 	dev = makedevice(major, minor);
1829 	ASSERT(dev != NODEV);
1830 
1831 	mutex_enter(&sp->cons_lock);
1832 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
1833 		mutex_exit(&sp->cons_lock);
1834 		return;
1835 	}
1836 
1837 	if (do_config(sp, prop) != DACF_SUCCESS) {
1838 		(void) ddi_dev_pathname(dev, 0, buf);
1839 		if (prop->cp_type == CONS_KBD)
1840 			cmn_err(CE_WARN, "Failed to relink the keyboard "
1841 			    "(%s) underneath virtual keyboard", buf);
1842 		else
1843 			cmn_err(CE_WARN, "Failed to relink the mouse "
1844 			    "(%s) underneath virtual mouse", buf);
1845 		consconfig_rem_dev(sp, dev);
1846 	}
1847 
1848 	mutex_exit(&sp->cons_lock);
1849 }
1850 
1851 
1852 int
1853 consconfig_unlink(major_t major, minor_t minor)
1854 {
1855 	dev_t		dev;
1856 	cons_state_t	*sp;
1857 	cons_prop_t	*prop;
1858 	int		error;
1859 
1860 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1861 		return (DACF_SUCCESS);
1862 
1863 	dev = makedevice(major, minor);
1864 	ASSERT(dev != NODEV);
1865 
1866 	mutex_enter(&sp->cons_lock);
1867 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
1868 		mutex_exit(&sp->cons_lock);
1869 		return (DACF_FAILURE);
1870 	}
1871 
1872 	error = do_unconfig(sp, prop);
1873 
1874 	/*
1875 	 * Keep this dev on the list, for this dev is still online.
1876 	 */
1877 	mutex_exit(&sp->cons_lock);
1878 
1879 	return (error);
1880 }
1881 
1882 /*
1883  * Routine to set baud rate, bits-per-char, parity and stop bits
1884  * on the console line when necessary.
1885  */
1886 static int
1887 consconfig_setmodes(dev_t dev, struct termios *termiosp)
1888 {
1889 	char buf[MAXPATHLEN];
1890 	int len = MAXPATHLEN;
1891 	char name[16];
1892 	int ppos, i;
1893 	char *path;
1894 	dev_t tdev;
1895 
1896 	/*
1897 	 * First, search for a devalias which matches this dev_t.
1898 	 * Try all of ttya through ttyz until no such alias
1899 	 */
1900 	(void) strcpy(name, "ttya");
1901 	for (i = 0; i < ('z'-'a'); i++) {
1902 		name[3] = 'a' + i; /* increment device name */
1903 		path = get_alias(name, buf);
1904 		if (path == NULL)
1905 			return (1);
1906 
1907 		tdev = ddi_pathname_to_dev_t(path);
1908 		if (tdev == dev)
1909 			break;	/* Exit loop if found */
1910 	}
1911 
1912 	if (i >= ('z'-'a'))
1913 		return (1);		/* If we didn't find it, return */
1914 
1915 	/*
1916 	 * Now that we know which "tty" this corresponds to, retrieve
1917 	 * the "ttya-mode" options property, which tells us how to configure
1918 	 * the line.
1919 	 */
1920 	(void) strcpy(name, "ttya-mode");	/* name of option we want */
1921 	name[3] = 'a' + i;			/* Adjust to correct line */
1922 
1923 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_root_node(), 0, name,
1924 	    buf, &len) != DDI_PROP_SUCCESS)
1925 		return (1);	/* if no such option, just return */
1926 
1927 	/*
1928 	 * The IEEE 1275 standard specifies that /aliases string property
1929 	 * values should be null-terminated.  Unfortunately the reality
1930 	 * is that most aren't and the OBP can't easily be modified to
1931 	 * add null termination to these strings.  So we'll add the
1932 	 * null termination here.  If the string already contains a
1933 	 * null termination character then that's ok too because we'll
1934 	 * just be adding a second one.
1935 	 */
1936 	buf[len] = '\0';
1937 
1938 	/* Clear out options we will be setting */
1939 	termiosp->c_cflag &=
1940 	    ~(CSIZE | CBAUD | CBAUDEXT | PARODD | PARENB | CSTOPB);
1941 
1942 	/* Clear options which potentially conflict with new settings */
1943 	termiosp->c_cflag &= ~(CIBAUD | CIBAUDEXT);
1944 
1945 	/*
1946 	 * Now, parse the string. Wish I could use sscanf().
1947 	 * Format 9600,8,n,1,-
1948 	 * baud rate, bits-per-char, parity, stop-bits, ignored
1949 	 */
1950 	for (ppos = 0; ppos < (MAXPATHLEN-8); ppos++) { /* Find first comma */
1951 		if ((buf[ppos] == 0) || (buf[ppos] == ','))
1952 			break;
1953 	}
1954 
1955 	if (buf[ppos] != ',') {
1956 		cmn_err(CE_WARN, "consconfig_setmodes: "
1957 		    "invalid mode string %s", buf);
1958 		return (1);
1959 	}
1960 
1961 	for (i = 0; i < MAX_SPEEDS; i++) {
1962 		if (strncmp(buf, speedtab[i].name, ppos) == 0)
1963 			break;
1964 	}
1965 
1966 	if (i >= MAX_SPEEDS) {
1967 		cmn_err(CE_WARN,
1968 		    "consconfig_setmodes: unrecognized speed in %s", buf);
1969 		return (1);
1970 	}
1971 
1972 	/* Found the baud rate, set it */
1973 	termiosp->c_cflag |= speedtab[i].code & CBAUD;
1974 	if (speedtab[i].code > 16) 			/* cfsetospeed! */
1975 		termiosp->c_cflag |= CBAUDEXT;
1976 
1977 	/* Set bits per character */
1978 	switch (buf[ppos+1]) {
1979 	case '8':
1980 		termiosp->c_cflag |= CS8;
1981 		break;
1982 	case '7':
1983 		termiosp->c_cflag |= CS7;
1984 		break;
1985 	default:
1986 		cmn_err(CE_WARN,
1987 		    "consconfig_setmodes: illegal bits-per-char %s", buf);
1988 		return (1);
1989 	}
1990 
1991 	/* Set parity */
1992 	switch (buf[ppos+3]) {
1993 	case 'o':
1994 		termiosp->c_cflag |= PARENB | PARODD;
1995 		break;
1996 	case 'e':
1997 		termiosp->c_cflag |= PARENB; /* enabled, not odd */
1998 		break;
1999 	case 'n':
2000 		break;	/* not enabled. */
2001 	default:
2002 		cmn_err(CE_WARN, "consconfig_setmodes: illegal parity %s", buf);
2003 		return (1);
2004 	}
2005 
2006 	/* Set stop bits */
2007 	switch (buf[ppos+5]) {
2008 	case '1':
2009 		break;	/* No extra stop bit */
2010 	case '2':
2011 		termiosp->c_cflag |= CSTOPB; /* 1 extra stop bit */
2012 		break;
2013 	default:
2014 		cmn_err(CE_WARN, "consconfig_setmodes: "
2015 		    "illegal stop bits %s", buf);
2016 		return (1);
2017 	}
2018 
2019 	return (0);
2020 }
2021 
2022 /*
2023  * Check to see if underlying keyboard devices are still online,
2024  * if any one is offline now, unlink it.
2025  */
2026 static void
2027 consconfig_check_phys_kbd(cons_state_t *sp)
2028 {
2029 	ldi_handle_t	kb_lh;
2030 	cons_prop_t	*prop;
2031 	int		error;
2032 	int		rval;
2033 
2034 	for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) {
2035 		if ((prop->cp_type != CONS_KBD) || (prop->cp_muxid == -1))
2036 			continue;
2037 
2038 		error = ldi_open_by_dev(&prop->cp_dev, OTYP_CHR,
2039 		    FREAD|FWRITE|FNOCTTY, kcred, &kb_lh, sp->cons_li);
2040 
2041 		if (error) {
2042 			(void) ldi_ioctl(sp->conskbd_lh, I_PUNLINK,
2043 			    prop->cp_muxid, FKIOCTL, kcred, &rval);
2044 			prop->cp_dev = NODEV;
2045 		} else {
2046 			(void) ldi_close(kb_lh, FREAD|FWRITE, kcred);
2047 		}
2048 	}
2049 
2050 	/*
2051 	 * Remove all disconnected keyboards,
2052 	 * whose dev is turned into NODEV above.
2053 	 */
2054 	consconfig_rem_dev(sp, NODEV);
2055 }
2056 
2057 /*
2058  * Remove devices according to dev, which may be NODEV
2059  */
2060 static void
2061 consconfig_rem_dev(cons_state_t *sp, dev_t dev)
2062 {
2063 	cons_prop_t *prop;
2064 	cons_prop_t *prev_prop;
2065 	cons_prop_t *tmp_prop;
2066 	cons_prop_t *head_prop;
2067 
2068 	head_prop = NULL;
2069 	prev_prop = NULL;
2070 	for (prop = sp->cons_km_prop; prop != NULL; ) {
2071 		if (prop->cp_dev == dev) {
2072 			tmp_prop = prop->cp_next;
2073 			consconfig_free_prop(prop);
2074 			prop = tmp_prop;
2075 			if (prev_prop)
2076 				prev_prop->cp_next = prop;
2077 		} else {
2078 			if (head_prop == NULL)
2079 				head_prop = prop;
2080 			prev_prop = prop;
2081 			prop = prop->cp_next;
2082 		}
2083 	}
2084 	sp->cons_km_prop = head_prop;
2085 }
2086 
2087 /*
2088  * Add a dev according to prop
2089  */
2090 static void
2091 consconfig_add_dev(cons_state_t *sp, cons_prop_t *prop)
2092 {
2093 	prop->cp_next = sp->cons_km_prop;
2094 	sp->cons_km_prop = prop;
2095 }
2096 
2097 /*
2098  * Find a device from our list according to dev
2099  */
2100 static cons_prop_t *
2101 consconfig_find_dev(cons_state_t *sp, dev_t dev)
2102 {
2103 	cons_prop_t *prop;
2104 
2105 	for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) {
2106 		if (prop->cp_dev == dev)
2107 			break;
2108 	}
2109 
2110 	return (prop);
2111 }
2112 
2113 /*
2114  * Free a cons prop associated with a keyboard or mouse
2115  */
2116 static void
2117 consconfig_free_prop(cons_prop_t *prop)
2118 {
2119 	if (prop->cp_pushmod)
2120 		kmem_free(prop->cp_pushmod, strlen(prop->cp_pushmod) + 1);
2121 	kmem_free(prop, sizeof (cons_prop_t));
2122 }
2123 
2124 /*
2125  * The early boot code can't print to a usb serial device or the
2126  * graphical boot screen.
2127  *
2128  * The early boot messages are saved in a buffer at the address indicated
2129  * by "deferred-console-buf" This function flushes the message to the
2130  * current console now that it is set up.
2131  */
2132 static void
2133 flush_deferred_console_buf(void)
2134 {
2135 	int rval;
2136 	vnode_t *vp;
2137 	uint_t defcons_buf;
2138 	char *kc, *bc, *defcons_kern_buf;
2139 
2140 	/* defcons_buf is in low memory, so an int works here */
2141 	defcons_buf = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(),
2142 	    DDI_PROP_DONTPASS, "deferred-console-buf", 0);
2143 
2144 	if (defcons_buf == 0)
2145 		return;
2146 
2147 	/*
2148 	 * After consconfig() and before userland opens /dev/sysmsg,
2149 	 * console I/O is goes to polled I/O entry points.
2150 	 *
2151 	 * If usb-serial doesn't implement polled I/O, we need
2152 	 * to open /dev/console now to get kernel console I/O to work.
2153 	 * We also push ttcompat and ldterm explicitly to get the
2154 	 * correct output format (autopush isn't set up yet). We
2155 	 * ignore push errors because they are non-fatal.
2156 	 * Note that opening /dev/console causes rconsvp to be
2157 	 * opened as well.
2158 	 */
2159 	if (cons_polledio == NULL) {
2160 		if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY,
2161 		    0, &vp, 0, 0) != 0)
2162 			return;
2163 
2164 		if (rconsvp) {
2165 			(void) strioctl(rconsvp, __I_PUSH_NOCTTY,
2166 			    (intptr_t)"ldterm", FKIOCTL, K_TO_K, kcred, &rval);
2167 			(void) strioctl(rconsvp, __I_PUSH_NOCTTY,
2168 			    (intptr_t)"ttcompat", FKIOCTL, K_TO_K,
2169 			    kcred, &rval);
2170 		}
2171 	}
2172 
2173 	/*
2174 	 * Copy message to a kernel buffer. Various kernel routines
2175 	 * expect buffer to be above kernelbase
2176 	 */
2177 	kc = defcons_kern_buf = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
2178 	bc = (char *)(uintptr_t)defcons_buf;
2179 	while (*kc++ = *bc++)
2180 		;
2181 	console_printf("%s", defcons_kern_buf);
2182 
2183 	kmem_free(defcons_kern_buf, MMU_PAGESIZE);
2184 }
2185 
2186 boolean_t
2187 consconfig_console_is_tipline(void)
2188 {
2189 	cons_state_t	*sp;
2190 
2191 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
2192 		return (B_FALSE);
2193 
2194 	if (sp->cons_input_type == CONSOLE_TIP)
2195 		return (B_TRUE);
2196 
2197 	return (B_FALSE);
2198 }
2199 
2200 boolean_t
2201 consconfig_dacf_initialized(void)
2202 {
2203 	cons_state_t	*sp;
2204 
2205 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
2206 		return (B_FALSE);
2207 
2208 	return (sp->cons_initialized);
2209 }
2210