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