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