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