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