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