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