xref: /titanic_50/usr/src/uts/common/io/consconfig_dacf.c (revision 40b706cf0ea54322e61c57be046ae27fde135222)
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 #if !defined(__x86)
986 void
987 consconfig_virtual_console_vp(cons_state_t *sp)
988 {
989 	char    *virtual_cons_path;
990 
991 	if (plat_virtual_console_path(&virtual_cons_path) < 0)
992 		return;
993 
994 	DPRINTF(DPRINT_L0, "consconfig_virtual_console_vp: "
995 	    "virtual console device path %s\n", virtual_cons_path);
996 
997 	ASSERT(sp->cons_stdout_path != NULL);
998 	if (strcmp(virtual_cons_path, sp->cons_stdout_path) == 0) {
999 		/* virtual console already in use */
1000 		return;
1001 	}
1002 
1003 	vsconsvp = i_consconfig_createvp(virtual_cons_path);
1004 	if (vsconsvp == NULL) {
1005 		cmn_err(CE_WARN, "consconfig_virtual_console_vp: "
1006 		    "unable to find serial virtual console device %s",
1007 		    virtual_cons_path);
1008 			return;
1009 	}
1010 
1011 	(void) e_ddi_hold_devi_by_dev(vsconsvp->v_rdev, 0);
1012 }
1013 #endif
1014 
1015 static void
1016 consconfig_init_framebuffer(cons_state_t *sp)
1017 {
1018 	if (!sp->cons_stdout_is_fb)
1019 		return;
1020 
1021 	DPRINTF(DPRINT_L0, "stdout is framebuffer\n");
1022 	ASSERT(strcmp(sp->cons_fb_path, sp->cons_stdout_path) == 0);
1023 
1024 	/*
1025 	 * Console output is a framebuffer.
1026 	 * Find the framebuffer driver if we can, and make
1027 	 * ourselves a shadow vnode to track it with.
1028 	 */
1029 	fbdev = stdoutdev;
1030 	if (fbdev == NODEV) {
1031 		DPRINTF(DPRINT_L3,
1032 		    "Can't find driver for console framebuffer\n");
1033 	} else {
1034 		/* stdoutdev is valid, of fbvp should exist */
1035 		fbvp = i_consconfig_createvp(sp->cons_stdout_path);
1036 		if (fbvp == NULL) {
1037 			panic("consconfig_init_framebuffer: "
1038 			    "unable to find frame buffer device");
1039 			/*NOTREACHED*/
1040 		}
1041 		ASSERT(fbvp->v_rdev == fbdev);
1042 
1043 		/* console device is never released */
1044 		fbdip = e_ddi_hold_devi_by_dev(fbdev, 0);
1045 	}
1046 	pm_cfb_setup(sp->cons_stdout_path);
1047 }
1048 
1049 /*
1050  * consconfig_prepare_dev:
1051  * 	Flush the stream, push "pushmod" onto the stream.
1052  * 	for keyboard, issue the KIOCTRANSABLE ioctl, and
1053  *	possible enable abort.
1054  */
1055 static void
1056 consconfig_prepare_dev(
1057     ldi_handle_t	new_lh,
1058     const char		*pushmod,
1059     int			kbdtranslatable,
1060     int			input_type,
1061     int			dev_type)
1062 {
1063 	int err, rval;
1064 
1065 	/* send a flush down the stream to the keyboard driver */
1066 	(void) ldi_ioctl(new_lh, I_FLUSH, (intptr_t)FLUSHRW,
1067 	    FKIOCTL, kcred, &rval);
1068 
1069 	if (pushmod) {
1070 		err = ldi_ioctl(new_lh, I_PUSH, (intptr_t)pushmod,
1071 		    FKIOCTL, kcred, &rval);
1072 		if (err) {
1073 			cmn_err(CE_WARN, "consconfig_prepare_dev: "
1074 			    "can't push streams module \"%s\", error %d",
1075 			    pushmod, err);
1076 		}
1077 	}
1078 
1079 	if (dev_type == CONS_MS)
1080 		return;
1081 
1082 	ASSERT(dev_type == CONS_KBD);
1083 
1084 	err = ldi_ioctl(new_lh, KIOCTRANSABLE,
1085 	    (intptr_t)&kbdtranslatable, FKIOCTL, kcred, &rval);
1086 	if (err) {
1087 		cmn_err(CE_WARN, "consconfig_prepare_dev: "
1088 		    "KIOCTRANSABLE failed, error: %d", err);
1089 	}
1090 
1091 	/*
1092 	 * During boot, dynamic_console_config() will call the
1093 	 * function to enable abort on the console.  If the
1094 	 * keyboard is hotplugged after boot, check to see if
1095 	 * the keyboard is the console input.  If it is
1096 	 * enable abort on it.
1097 	 */
1098 	if (input_type == CONSOLE_LOCAL)
1099 		(void) consconfig_kbd_abort_enable(new_lh);
1100 }
1101 
1102 /*
1103  * consconfig_relink_conskbd:
1104  * 	If new_lh is not NULL it should represent a driver with a
1105  * 	keyboard module pushed on top of it. The driver is then linked
1106  * 	underneath conskbd.  the resulting stream will be
1107  *	wc->conskbd->"new_lh driver".
1108  *
1109  * 	If new_lh is NULL, then an unlink operation is done on conskbd
1110  * 	that attempts to unlink the stream specified by *muxid.
1111  *	the resulting stream will be wc->conskbd.
1112  */
1113 static int
1114 consconfig_relink_conskbd(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
1115 {
1116 	int		err, rval;
1117 	int		conskbd_relink = 0;
1118 
1119 	ASSERT(muxid != NULL);
1120 
1121 	DPRINTF(DPRINT_L0, "consconfig_relink_conskbd: "
1122 	    "conskbd_lh = %p, new_lh = %p,  muxid = %x\n",
1123 	    sp->conskbd_lh, new_lh, *muxid);
1124 
1125 	/*
1126 	 * If conskbd is linked under wc then temporarily unlink it
1127 	 * from under wc so that the new_lh stream may be linked under
1128 	 * conskbd.  This has to be done because streams are built bottom
1129 	 * up and linking a stream under conskbd isn't allowed when
1130 	 * conskbd is linked under wc.
1131 	 */
1132 	if (sp->conskbd_muxid != -1) {
1133 		DPRINTF(DPRINT_L0, "unlinking conskbd from under wc\n");
1134 		conskbd_relink = 1;
1135 		err = consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid);
1136 		if (err) {
1137 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1138 			    "wc unlink failed, error %d", err);
1139 			return (err);
1140 		}
1141 	}
1142 
1143 	if (new_lh != NULL) {
1144 		DPRINTF(DPRINT_L0, "linking keyboard under conskbd\n");
1145 
1146 		/* Link the stream represented by new_lh under conskbd */
1147 		err = ldi_ioctl(sp->conskbd_lh, I_PLINK, (uintptr_t)new_lh,
1148 		    FKIOCTL, kcred, muxid);
1149 		if (err != 0) {
1150 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1151 			    "conskbd link failed, error %d", err);
1152 			goto relink_failed;
1153 		}
1154 	} else {
1155 		DPRINTF(DPRINT_L0, "unlinking keyboard from under conskbd\n");
1156 
1157 		/*
1158 		 * This will cause the keyboard driver to be closed,
1159 		 * all modules to be popped, and the keyboard vnode released.
1160 		 */
1161 		err = ldi_ioctl(sp->conskbd_lh, I_PUNLINK, *muxid,
1162 		    FKIOCTL, kcred, &rval);
1163 		if (err != 0) {
1164 			cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1165 			    "conskbd unlink failed, error %d", err);
1166 			goto relink_failed;
1167 		} else {
1168 			*muxid = -1;
1169 		}
1170 
1171 		consconfig_check_phys_kbd(sp);
1172 	}
1173 
1174 	if (!conskbd_relink)
1175 		return (err);
1176 
1177 	/*
1178 	 * Link consbkd back under wc.
1179 	 *
1180 	 * The act of linking conskbd back under wc will cause wc
1181 	 * to query the lower lower layers about their polled I/O
1182 	 * routines.  This time the request will succeed because there
1183 	 * is a physical keyboard linked under conskbd.
1184 	 */
1185 	DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n");
1186 	err = consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
1187 	if (err) {
1188 		cmn_err(CE_WARN, "consconfig_relink_conskbd: "
1189 		    "wc link failed, error %d", err);
1190 	}
1191 	return (err);
1192 
1193 relink_failed:
1194 	if (!conskbd_relink)
1195 		return (err);
1196 
1197 	/* something went wrong, try to reconnect conskbd back under wc */
1198 	DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n");
1199 	(void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid);
1200 	return (err);
1201 }
1202 
1203 /*
1204  * consconfig_relink_consms:
1205  * 	If new_lh is not NULL it should represent a driver with a
1206  * 	mouse module pushed on top of it. The driver is then linked
1207  * 	underneath consms.  the resulting stream will be
1208  *	consms->"new_lh driver".
1209  *
1210  * 	If new_lh is NULL, then an unlink operation is done on consms
1211  * 	that attempts to unlink the stream specified by *muxid.
1212  */
1213 static int
1214 consconfig_relink_consms(cons_state_t *sp, ldi_handle_t new_lh, int *muxid)
1215 {
1216 	int		err, rval;
1217 
1218 	DPRINTF(DPRINT_L0, "consconfig_relink_consms: "
1219 	    "consms_lh = %p, new_lh = %p,  muxid = %x\n",
1220 	    (void *)sp->consms_lh, (void *)new_lh, *muxid);
1221 
1222 	if (new_lh != NULL) {
1223 		DPRINTF(DPRINT_L0, "linking mouse under consms\n");
1224 
1225 		/* Link ms/usbms stream underneath consms multiplexor. */
1226 		err = ldi_ioctl(sp->consms_lh, I_PLINK, (uintptr_t)new_lh,
1227 		    FKIOCTL, kcred, muxid);
1228 		if (err != 0) {
1229 			cmn_err(CE_WARN, "consconfig_relink_consms: "
1230 			    "mouse link failed, error %d", err);
1231 		}
1232 	} else {
1233 		DPRINTF(DPRINT_L0, "unlinking mouse from under consms\n");
1234 
1235 		/* Tear down the mouse stream */
1236 		err = ldi_ioctl(sp->consms_lh, I_PUNLINK, *muxid,
1237 		    FKIOCTL, kcred, &rval);
1238 		if (err != 0) {
1239 			cmn_err(CE_WARN, "consconfig_relink_consms: "
1240 			    "mouse unlink failed, error %d", err);
1241 		} else {
1242 			*muxid = -1;
1243 		}
1244 	}
1245 	return (err);
1246 }
1247 
1248 static int
1249 cons_get_input_type(cons_state_t *sp)
1250 {
1251 	int type;
1252 
1253 	/*
1254 	 * Now that we know what all the devices are, we can figure out
1255 	 * what kind of console we have.
1256 	 */
1257 	if (sp->cons_stdin_is_kbd)  {
1258 		/* Stdin is from the system keyboard */
1259 		type = CONSOLE_LOCAL;
1260 	} else if ((stdindev != NODEV) && (stdindev == stdoutdev)) {
1261 		/*
1262 		 * A reliable indicator that we are doing a remote console
1263 		 * is that stdin and stdout are the same.
1264 		 * This is probably a tip line.
1265 		 */
1266 		type = CONSOLE_TIP;
1267 	} else {
1268 		type = CONSOLE_SERIAL_KEYBOARD;
1269 	}
1270 
1271 	return (type);
1272 }
1273 
1274 static void
1275 consconfig_init_input(cons_state_t *sp)
1276 {
1277 	ldi_handle_t	new_lh;
1278 	dev_t		cons_final_dev;
1279 	int		err;
1280 
1281 	cons_final_dev = NODEV;
1282 
1283 	switch (sp->cons_input_type) {
1284 	case CONSOLE_LOCAL:
1285 		DPRINTF(DPRINT_L0, "stdin is keyboard\n");
1286 
1287 		/*
1288 		 * The machine is allowed to boot without a keyboard.
1289 		 * If a user attaches a keyboard later, the keyboard
1290 		 * will be hooked into the console stream with the dacf
1291 		 * functions.
1292 		 *
1293 		 * The only drivers that look at kbbdev are the
1294 		 * serial drivers, which looks at kbdev to see if
1295 		 * they should allow abort on a break. In the absence
1296 		 * of keyboard, the serial drivers won't be attached
1297 		 * for any keyboard instance.
1298 		 */
1299 		if (kbddev == NODEV) {
1300 			/*
1301 			 * If there is a problem with the keyboard
1302 			 * during the driver loading, then the polled
1303 			 * input won't get setup properly if polled
1304 			 * input is needed.  This means that if the
1305 			 * keyboard is hotplugged, the keyboard would
1306 			 * work normally, but going down to the
1307 			 * debugger would not work if polled input is
1308 			 * required.  This field is set here.  The next
1309 			 * time a keyboard is plugged in, the field is
1310 			 * checked in order to give the next keyboard a
1311 			 * chance at being registered for console
1312 			 * input.
1313 			 *
1314 			 * Although this code will rarely be needed,
1315 			 * USB keyboards can be flaky, so this code
1316 			 * will be useful on the occasion that the
1317 			 * keyboard doesn't enumerate when the drivers
1318 			 * are loaded.
1319 			 */
1320 			DPRINTF(DPRINT_L2, "Error with console keyboard\n");
1321 			sp->cons_keyboard_problem = B_TRUE;
1322 		}
1323 		stdindev = kbddev;
1324 		cons_final_dev = sp->cons_wc_vp->v_rdev;
1325 		break;
1326 
1327 	case CONSOLE_TIP:
1328 		DPRINTF(DPRINT_L0, "console input is tty (%s)\n",
1329 		    sp->cons_stdin_path);
1330 
1331 		/*
1332 		 * Console device drivers must be able to output
1333 		 * after being closed.
1334 		 */
1335 		rconsvp = i_consconfig_createvp(sp->cons_stdin_path);
1336 		if (rconsvp == NULL) {
1337 			panic("consconfig_init_input: "
1338 			    "unable to find stdin device (%s)",
1339 			    sp->cons_stdin_path);
1340 			/*NOTREACHED*/
1341 		}
1342 		rconsdev = rconsvp->v_rdev;
1343 
1344 		ASSERT(rconsdev == stdindev);
1345 
1346 		cons_final_dev = rconsdev;
1347 		break;
1348 
1349 	case CONSOLE_SERIAL_KEYBOARD:
1350 		DPRINTF(DPRINT_L0, "stdin is serial keyboard\n");
1351 
1352 		/*
1353 		 * Non-keyboard input device, but not rconsdev.
1354 		 * This is known as the "serial keyboard" case - the
1355 		 * most likely use is someone has a keyboard attached
1356 		 * to a serial port (tip) and still has output on a
1357 		 * framebuffer.
1358 		 *
1359 		 * In this case, the serial driver must be linked
1360 		 * directly beneath wc.  Since conskbd was linked
1361 		 * underneath wc above, first we unlink conskbd.
1362 		 */
1363 		(void) consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid);
1364 		sp->conskbd_muxid = -1;
1365 
1366 		/*
1367 		 * Open the serial keyboard, configure it,
1368 		 * and link it underneath wc.
1369 		 */
1370 		err = ldi_open_by_name(sp->cons_stdin_path,
1371 		    FREAD|FWRITE|FNOCTTY, kcred, &new_lh, sp->cons_li);
1372 		if (err == 0) {
1373 			struct termios	termios;
1374 			int		rval;
1375 			int		stdin_muxid;
1376 
1377 			consconfig_prepare_dev(new_lh,
1378 			    "kb", TR_CANNOT, sp->cons_input_type, CONS_KBD);
1379 
1380 			/* Re-set baud rate */
1381 			(void) ldi_ioctl(new_lh, TCGETS, (intptr_t)&termios,
1382 			    FKIOCTL, kcred, &rval);
1383 
1384 			/* Set baud rate */
1385 			if (consconfig_setmodes(stdindev, &termios) == 0) {
1386 				err = ldi_ioctl(new_lh,
1387 				    TCSETSF, (intptr_t)&termios,
1388 				    FKIOCTL, kcred, &rval);
1389 				if (err) {
1390 					cmn_err(CE_WARN,
1391 					    "consconfig_init_input: "
1392 					    "TCSETSF failed, error %d", err);
1393 				}
1394 			}
1395 
1396 			/*
1397 			 * Now link the serial keyboard direcly under wc
1398 			 * we don't save the returned muxid because we
1399 			 * don't support changing/hotplugging the console
1400 			 * keyboard when it is a serial keyboard.
1401 			 */
1402 			(void) consconfig_relink_wc(sp, new_lh, &stdin_muxid);
1403 
1404 			(void) ldi_close(new_lh, FREAD|FWRITE, kcred);
1405 		}
1406 
1407 		cons_final_dev = sp->cons_wc_vp->v_rdev;
1408 		break;
1409 
1410 	default:
1411 		panic("consconfig_init_input: "
1412 		    "unsupported console input/output combination");
1413 		/*NOTREACHED*/
1414 	}
1415 
1416 	/*
1417 	 * Use the redirection device/workstation console pair as the "real"
1418 	 * console if the latter hasn't already been set.
1419 	 * The workstation console driver needs to see rwsconsvp, but
1420 	 * all other access should be through the redirecting driver.
1421 	 */
1422 	if (rconsvp == NULL) {
1423 		consconfig_dprintf(DPRINT_L0, "setup redirection driver\n");
1424 		rconsvp = wsconsvp;
1425 		rconsdev = wsconsvp->v_rdev;
1426 	}
1427 
1428 	ASSERT(cons_final_dev != NODEV);
1429 
1430 	err = ldi_open_by_dev(&cons_final_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY,
1431 	    kcred, &new_lh, sp->cons_li);
1432 	if (err) {
1433 		panic("consconfig_init_input: "
1434 		    "unable to open console device");
1435 		/*NOTREACHED*/
1436 	}
1437 
1438 	/* Enable abort on the console */
1439 	(void) consconfig_kbd_abort_enable(new_lh);
1440 
1441 	/* Now we must close it to make console logins happy */
1442 	(void) ldi_close(new_lh, FREAD|FWRITE, kcred);
1443 
1444 	/* Set up polled input if it is supported by the console device */
1445 	if (plat_use_polled_debug()) {
1446 		/*
1447 		 * In the debug case, register the keyboard polled entry
1448 		 * points, but don't throw the switch in the debugger.  This
1449 		 * allows the polled entry points to be checked by hand
1450 		 */
1451 		consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev);
1452 	} else {
1453 		consconfig_setup_polledio(sp, cons_final_dev);
1454 	}
1455 
1456 	kadb_uses_kernel();
1457 }
1458 
1459 /*
1460  * This function kicks off the console configuration.
1461  * Configure keyboard and mouse. Main entry here.
1462  */
1463 void
1464 dynamic_console_config(void)
1465 {
1466 	/* initialize space.c globals */
1467 	stdindev = NODEV;
1468 	mousedev = NODEV;
1469 	kbddev = NODEV;
1470 	fbdev = NODEV;
1471 	fbvp = NULL;
1472 	fbdip = NULL;
1473 	wsconsvp = NULL;
1474 	rwsconsvp = NULL;
1475 	rwsconsdev = NODEV;
1476 	rconsvp = NULL;
1477 	rconsdev = NODEV;
1478 
1479 	/* Initialize cons_state_t structure and console device paths */
1480 	consconfig_sp = consconfig_state_init();
1481 	ASSERT(consconfig_sp);
1482 
1483 	/* Build upper layer of console stream */
1484 	cons_build_upper_layer(consconfig_sp);
1485 
1486 	/*
1487 	 * Load keyboard/mouse drivers. The dacf routines will
1488 	 * plumb the devices into the console stream
1489 	 *
1490 	 * At the conclusion of the ddi_pathname_to_dev_t calls, the keyboard
1491 	 * and mouse drivers are linked into their respective console
1492 	 * streams if the pathnames are valid.
1493 	 */
1494 	consconfig_load_drivers(consconfig_sp);
1495 	consconfig_sp->cons_input_type = cons_get_input_type(consconfig_sp);
1496 
1497 	/*
1498 	 * This is legacy special case code for the "cool" virtual console
1499 	 * for the Starfire project.  Starfire has a dummy "ssp-serial"
1500 	 * node in the OBP device tree and cvc is a pseudo driver.
1501 	 */
1502 	if (consconfig_sp->cons_stdout_path != NULL && stdindev == NODEV &&
1503 	    strstr(consconfig_sp->cons_stdout_path, "ssp-serial")) {
1504 		/*
1505 		 * Setup the virtual console driver for Starfire
1506 		 * Note that console I/O will still go through prom for now
1507 		 * (notice we don't open the driver here). The cvc driver
1508 		 * will be activated when /dev/console is opened by init.
1509 		 * During that time, a cvcd daemon will be started that
1510 		 * will open the cvcredirection driver to facilitate
1511 		 * the redirection of console I/O from cvc to cvcd.
1512 		 */
1513 		rconsvp = i_consconfig_createvp(CVC_PATH);
1514 		if (rconsvp == NULL)
1515 			return;
1516 		rconsdev = rconsvp->v_rdev;
1517 		return;
1518 	}
1519 
1520 	rwsconsvp = consconfig_sp->cons_wc_vp;
1521 	rwsconsdev = consconfig_sp->cons_wc_vp->v_rdev;
1522 
1523 
1524 	/* initialize framebuffer, console input, and redirection device  */
1525 	consconfig_init_framebuffer(consconfig_sp);
1526 	consconfig_init_input(consconfig_sp);
1527 
1528 #if !defined(__x86)
1529 	/* initialize virtual console vp for logging if needed */
1530 	consconfig_virtual_console_vp(consconfig_sp);
1531 #endif
1532 
1533 	DPRINTF(DPRINT_L0,
1534 	    "mousedev %lx, kbddev %lx, fbdev %lx, rconsdev %lx\n",
1535 	    mousedev,  kbddev, fbdev, rconsdev);
1536 
1537 	flush_deferred_console_buf();
1538 }
1539 
1540 
1541 /*
1542  * Start of DACF interfaces
1543  */
1544 
1545 /*
1546  * Do the real job for keyboard/mouse auto-configuration.
1547  */
1548 static int
1549 do_config(cons_state_t *sp, cons_prop_t *prop)
1550 {
1551 	ldi_handle_t	lh;
1552 	dev_t		dev;
1553 	int		error;
1554 
1555 	ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS));
1556 
1557 	dev = prop->cp_dev;
1558 	error = ldi_open_by_dev(&dev, OTYP_CHR,
1559 	    FREAD|FWRITE|FNOCTTY, kcred, &lh, sp->cons_li);
1560 	if (error) {
1561 		return (DACF_FAILURE);
1562 	}
1563 	ASSERT(dev == prop->cp_dev);	/* clone not supported */
1564 
1565 	/*
1566 	 * Prepare the new keyboard/mouse driver
1567 	 * to be linked under conskbd/consms.
1568 	 */
1569 	consconfig_prepare_dev(lh, prop->cp_pushmod, TR_CAN,
1570 	    sp->cons_input_type, prop->cp_type);
1571 
1572 	if (prop->cp_type == CONS_KBD) {
1573 		/*
1574 		 * Tell the physical keyboard driver to send
1575 		 * the abort sequences up to the virtual keyboard
1576 		 * driver so that STOP and A (or F1 and A)
1577 		 * can be applied to different keyboards.
1578 		 */
1579 		(void) consconfig_kbd_abort_disable(lh);
1580 
1581 		/* Link the stream underneath conskbd */
1582 		error = consconfig_relink_conskbd(sp, lh, &prop->cp_muxid);
1583 	} else {
1584 		/* Link the stream underneath consms */
1585 		error = consconfig_relink_consms(sp, lh, &prop->cp_muxid);
1586 	}
1587 
1588 	/*
1589 	 * At this point, the stream is:
1590 	 *	for keyboard:	wc->conskbd->["pushmod"->"kbd_vp driver"]
1591 	 *	for mouse:	consms->["module_name"->]"mouse_avp driver"
1592 	 */
1593 
1594 	/* Close the driver stream, it will stay linked under conskbd */
1595 	(void) ldi_close(lh, FREAD|FWRITE, kcred);
1596 
1597 	if (error) {
1598 		return (DACF_FAILURE);
1599 	}
1600 
1601 	return (DACF_SUCCESS);
1602 }
1603 
1604 static int
1605 do_unconfig(cons_state_t *sp, cons_prop_t *prop)
1606 {
1607 	ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS));
1608 
1609 	if (prop->cp_type == CONS_KBD)
1610 		return (consconfig_relink_conskbd(sp, NULL, &prop->cp_muxid));
1611 	else
1612 		return (consconfig_relink_consms(sp, NULL, &prop->cp_muxid));
1613 }
1614 
1615 static int
1616 kb_ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int type)
1617 {
1618 	major_t			major;
1619 	minor_t			minor;
1620 	dev_t			dev;
1621 	dev_info_t		*dip;
1622 	cons_state_t		*sp;
1623 	cons_prop_t		*prop;
1624 	const char		*pushmod;
1625 
1626 	/*
1627 	 * Retrieve the state information
1628 	 * Some platforms may use the old-style "consconfig" to configure
1629 	 * console stream modules but may also support devices that happen
1630 	 * to match a rule in /etc/dacf.conf.  This will cause a problem
1631 	 * since the console state structure will not be initialized.
1632 	 * In that case, these entry points should silently fail and
1633 	 * permit console to be plumbed later in boot.
1634 	 */
1635 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1636 		return (DACF_FAILURE);
1637 
1638 	dip = dacf_devinfo_node(minor_hdl);
1639 	major = ddi_driver_major(dip);
1640 	ASSERT(major != DDI_MAJOR_T_NONE);
1641 	minor = dacf_minor_number(minor_hdl);
1642 	dev = makedevice(major, minor);
1643 	ASSERT(dev != NODEV);
1644 
1645 	DPRINTF(DPRINT_L0, "driver name = \"%s\", dev = 0x%lx, major = 0x%x\n",
1646 	    (char *)dacf_driver_name(minor_hdl), dev, major);
1647 
1648 	/* Access to the global variables is synchronized */
1649 	mutex_enter(&sp->cons_lock);
1650 
1651 	/*
1652 	 * Check if the keyboard/mouse has already configured.
1653 	 */
1654 	if (consconfig_find_dev(sp, dev) != NULL) {
1655 		mutex_exit(&sp->cons_lock);
1656 		return (DACF_SUCCESS);
1657 	}
1658 
1659 	prop = kmem_zalloc(sizeof (cons_prop_t), KM_SLEEP);
1660 
1661 	/* Config the new keyboard/mouse device */
1662 	prop->cp_dev = dev;
1663 
1664 	pushmod = dacf_get_arg(arg_hdl, "pushmod");
1665 	prop->cp_pushmod = i_ddi_strdup((char *)pushmod, KM_SLEEP);
1666 
1667 	prop->cp_type = type;
1668 	if (do_config(sp, prop) != DACF_SUCCESS) {
1669 		/*
1670 		 * The keyboard/mouse node failed to open.
1671 		 * Set the major and minor numbers to 0 so
1672 		 * kb_unconfig/ms_unconfig won't unconfigure
1673 		 * this node if it is detached.
1674 		 */
1675 		mutex_exit(&sp->cons_lock);
1676 		consconfig_free_prop(prop);
1677 		return (DACF_FAILURE);
1678 	}
1679 
1680 	consconfig_add_dev(sp, prop);
1681 
1682 	/*
1683 	 * See if there was a problem with the console keyboard during boot.
1684 	 * If so, try to register polled input for this keyboard.
1685 	 */
1686 	if ((type == CONS_KBD) && (sp->cons_keyboard_problem)) {
1687 		consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev);
1688 		sp->cons_keyboard_problem = B_FALSE;
1689 	}
1690 
1691 	/* Prevent autodetach due to memory pressure */
1692 	(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1);
1693 
1694 	mutex_exit(&sp->cons_lock);
1695 
1696 	return (DACF_SUCCESS);
1697 }
1698 
1699 static int
1700 kb_ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl)
1701 {
1702 	_NOTE(ARGUNUSED(arg_hdl))
1703 
1704 	major_t		major;
1705 	minor_t		minor;
1706 	dev_t		dev;
1707 	dev_info_t	*dip;
1708 	cons_state_t	*sp;
1709 	cons_prop_t	*prop;
1710 
1711 	/*
1712 	 * Retrieve the state information
1713 	 * So if there isn't a state available, then this entry point just
1714 	 * returns.  See note in kb_config().
1715 	 */
1716 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1717 		return (DACF_SUCCESS);
1718 
1719 	dip = dacf_devinfo_node(minor_hdl);
1720 	major = ddi_driver_major(dip);
1721 	ASSERT(major != DDI_MAJOR_T_NONE);
1722 	minor = dacf_minor_number(minor_hdl);
1723 	dev = makedevice(major, minor);
1724 	ASSERT(dev != NODEV);
1725 
1726 	/*
1727 	 * Check if the keyboard/mouse that is being detached
1728 	 * is the console keyboard/mouse or not.
1729 	 */
1730 	mutex_enter(&sp->cons_lock);
1731 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
1732 		mutex_exit(&sp->cons_lock);
1733 		return (DACF_SUCCESS);
1734 	}
1735 
1736 	/*
1737 	 * This dev may be opened physically and then hotplugged out.
1738 	 */
1739 	if (prop->cp_muxid != -1) {
1740 		(void) do_unconfig(sp, prop);
1741 		consconfig_rem_dev(sp, dev);
1742 	}
1743 
1744 	mutex_exit(&sp->cons_lock);
1745 
1746 	return (DACF_SUCCESS);
1747 }
1748 
1749 /*
1750  * This is the post-attach / pre-detach action function for the keyboard
1751  * and mouse. This function is associated with a node type in /etc/dacf.conf.
1752  */
1753 static int
1754 kb_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1755 {
1756 	_NOTE(ARGUNUSED(flags))
1757 
1758 	return (kb_ms_config(minor_hdl, arg_hdl, CONS_KBD));
1759 }
1760 
1761 static int
1762 ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1763 {
1764 	_NOTE(ARGUNUSED(flags))
1765 
1766 	return (kb_ms_config(minor_hdl, arg_hdl, CONS_MS));
1767 }
1768 
1769 static int
1770 kb_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1771 {
1772 	_NOTE(ARGUNUSED(flags))
1773 
1774 	return (kb_ms_unconfig(minor_hdl, arg_hdl));
1775 }
1776 
1777 static int
1778 ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags)
1779 {
1780 	_NOTE(ARGUNUSED(flags))
1781 
1782 	return (kb_ms_unconfig(minor_hdl, arg_hdl));
1783 }
1784 
1785 /*
1786  * consconfig_link and consconfig_unlink are provided to support
1787  * direct access to physical keyboard/mouse underlying conskbd/
1788  * consms.
1789  * When the keyboard/mouse is opened physically via its device
1790  * file, it will be unlinked from the virtual one, and when it
1791  * is closed physically, it will be linked back under the virtual
1792  * one.
1793  */
1794 void
1795 consconfig_link(major_t major, minor_t minor)
1796 {
1797 	char		buf[MAXPATHLEN];
1798 	dev_t		dev;
1799 	cons_state_t	*sp;
1800 	cons_prop_t	*prop;
1801 
1802 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1803 		return;
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;
1812 	}
1813 
1814 	if (do_config(sp, prop) != DACF_SUCCESS) {
1815 		(void) ddi_dev_pathname(dev, 0, buf);
1816 		if (prop->cp_type == CONS_KBD)
1817 			cmn_err(CE_WARN, "Failed to relink the keyboard "
1818 			    "(%s) underneath virtual keyboard", buf);
1819 		else
1820 			cmn_err(CE_WARN, "Failed to relink the mouse "
1821 			    "(%s) underneath virtual mouse", buf);
1822 		consconfig_rem_dev(sp, dev);
1823 	}
1824 
1825 	mutex_exit(&sp->cons_lock);
1826 }
1827 
1828 
1829 int
1830 consconfig_unlink(major_t major, minor_t minor)
1831 {
1832 	dev_t		dev;
1833 	cons_state_t	*sp;
1834 	cons_prop_t	*prop;
1835 	int		error;
1836 
1837 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
1838 		return (DACF_SUCCESS);
1839 
1840 	dev = makedevice(major, minor);
1841 	ASSERT(dev != NODEV);
1842 
1843 	mutex_enter(&sp->cons_lock);
1844 	if ((prop = consconfig_find_dev(sp, dev)) == NULL) {
1845 		mutex_exit(&sp->cons_lock);
1846 		return (DACF_FAILURE);
1847 	}
1848 
1849 	error = do_unconfig(sp, prop);
1850 
1851 	/*
1852 	 * Keep this dev on the list, for this dev is still online.
1853 	 */
1854 	mutex_exit(&sp->cons_lock);
1855 
1856 	return (error);
1857 }
1858 
1859 /*
1860  * Routine to set baud rate, bits-per-char, parity and stop bits
1861  * on the console line when necessary.
1862  */
1863 static int
1864 consconfig_setmodes(dev_t dev, struct termios *termiosp)
1865 {
1866 	char buf[MAXPATHLEN];
1867 	int len = MAXPATHLEN;
1868 	char name[16];
1869 	int ppos, i;
1870 	char *path;
1871 	dev_t tdev;
1872 
1873 	/*
1874 	 * First, search for a devalias which matches this dev_t.
1875 	 * Try all of ttya through ttyz until no such alias
1876 	 */
1877 	(void) strcpy(name, "ttya");
1878 	for (i = 0; i < ('z'-'a'); i++) {
1879 		name[3] = 'a' + i; /* increment device name */
1880 		path = get_alias(name, buf);
1881 		if (path == NULL)
1882 			return (1);
1883 
1884 		tdev = ddi_pathname_to_dev_t(path);
1885 		if (tdev == dev)
1886 			break;	/* Exit loop if found */
1887 	}
1888 
1889 	if (i >= ('z'-'a'))
1890 		return (1);		/* If we didn't find it, return */
1891 
1892 	/*
1893 	 * Now that we know which "tty" this corresponds to, retrieve
1894 	 * the "ttya-mode" options property, which tells us how to configure
1895 	 * the line.
1896 	 */
1897 	(void) strcpy(name, "ttya-mode");	/* name of option we want */
1898 	name[3] = 'a' + i;			/* Adjust to correct line */
1899 
1900 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_root_node(), 0, name,
1901 	    buf, &len) != DDI_PROP_SUCCESS)
1902 		return (1);	/* if no such option, just return */
1903 
1904 	/*
1905 	 * The IEEE 1275 standard specifies that /aliases string property
1906 	 * values should be null-terminated.  Unfortunately the reality
1907 	 * is that most aren't and the OBP can't easily be modified to
1908 	 * add null termination to these strings.  So we'll add the
1909 	 * null termination here.  If the string already contains a
1910 	 * null termination character then that's ok too because we'll
1911 	 * just be adding a second one.
1912 	 */
1913 	buf[len] = '\0';
1914 
1915 	/* Clear out options we will be setting */
1916 	termiosp->c_cflag &=
1917 	    ~(CSIZE | CBAUD | CBAUDEXT | PARODD | PARENB | CSTOPB);
1918 
1919 	/* Clear options which potentially conflict with new settings */
1920 	termiosp->c_cflag &= ~(CIBAUD | CIBAUDEXT);
1921 
1922 	/*
1923 	 * Now, parse the string. Wish I could use sscanf().
1924 	 * Format 9600,8,n,1,-
1925 	 * baud rate, bits-per-char, parity, stop-bits, ignored
1926 	 */
1927 	for (ppos = 0; ppos < (MAXPATHLEN-8); ppos++) { /* Find first comma */
1928 		if ((buf[ppos] == 0) || (buf[ppos] == ','))
1929 			break;
1930 	}
1931 
1932 	if (buf[ppos] != ',') {
1933 		cmn_err(CE_WARN, "consconfig_setmodes: "
1934 		    "invalid mode string %s", buf);
1935 		return (1);
1936 	}
1937 
1938 	for (i = 0; i < MAX_SPEEDS; i++) {
1939 		if (strncmp(buf, speedtab[i].name, ppos) == 0)
1940 			break;
1941 	}
1942 
1943 	if (i >= MAX_SPEEDS) {
1944 		cmn_err(CE_WARN,
1945 		    "consconfig_setmodes: unrecognized speed in %s", buf);
1946 		return (1);
1947 	}
1948 
1949 	/* Found the baud rate, set it */
1950 	termiosp->c_cflag |= speedtab[i].code & CBAUD;
1951 	if (speedtab[i].code > 16) 			/* cfsetospeed! */
1952 		termiosp->c_cflag |= CBAUDEXT;
1953 
1954 	/* Set bits per character */
1955 	switch (buf[ppos+1]) {
1956 	case '8':
1957 		termiosp->c_cflag |= CS8;
1958 		break;
1959 	case '7':
1960 		termiosp->c_cflag |= CS7;
1961 		break;
1962 	default:
1963 		cmn_err(CE_WARN,
1964 		    "consconfig_setmodes: illegal bits-per-char %s", buf);
1965 		return (1);
1966 	}
1967 
1968 	/* Set parity */
1969 	switch (buf[ppos+3]) {
1970 	case 'o':
1971 		termiosp->c_cflag |= PARENB | PARODD;
1972 		break;
1973 	case 'e':
1974 		termiosp->c_cflag |= PARENB; /* enabled, not odd */
1975 		break;
1976 	case 'n':
1977 		break;	/* not enabled. */
1978 	default:
1979 		cmn_err(CE_WARN, "consconfig_setmodes: illegal parity %s", buf);
1980 		return (1);
1981 	}
1982 
1983 	/* Set stop bits */
1984 	switch (buf[ppos+5]) {
1985 	case '1':
1986 		break;	/* No extra stop bit */
1987 	case '2':
1988 		termiosp->c_cflag |= CSTOPB; /* 1 extra stop bit */
1989 		break;
1990 	default:
1991 		cmn_err(CE_WARN, "consconfig_setmodes: "
1992 		    "illegal stop bits %s", buf);
1993 		return (1);
1994 	}
1995 
1996 	return (0);
1997 }
1998 
1999 /*
2000  * Check to see if underlying keyboard devices are still online,
2001  * if any one is offline now, unlink it.
2002  */
2003 static void
2004 consconfig_check_phys_kbd(cons_state_t *sp)
2005 {
2006 	ldi_handle_t	kb_lh;
2007 	cons_prop_t	*prop;
2008 	int		error;
2009 	int		rval;
2010 
2011 	for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) {
2012 		if ((prop->cp_type != CONS_KBD) || (prop->cp_muxid == -1))
2013 			continue;
2014 
2015 		error = ldi_open_by_dev(&prop->cp_dev, OTYP_CHR,
2016 		    FREAD|FWRITE|FNOCTTY, kcred, &kb_lh, sp->cons_li);
2017 
2018 		if (error) {
2019 			(void) ldi_ioctl(sp->conskbd_lh, I_PUNLINK,
2020 			    prop->cp_muxid, FKIOCTL, kcred, &rval);
2021 			prop->cp_dev = NODEV;
2022 		} else {
2023 			(void) ldi_close(kb_lh, FREAD|FWRITE, kcred);
2024 		}
2025 	}
2026 
2027 	/*
2028 	 * Remove all disconnected keyboards,
2029 	 * whose dev is turned into NODEV above.
2030 	 */
2031 	consconfig_rem_dev(sp, NODEV);
2032 }
2033 
2034 /*
2035  * Remove devices according to dev, which may be NODEV
2036  */
2037 static void
2038 consconfig_rem_dev(cons_state_t *sp, dev_t dev)
2039 {
2040 	cons_prop_t *prop;
2041 	cons_prop_t *prev_prop;
2042 	cons_prop_t *tmp_prop;
2043 	cons_prop_t *head_prop;
2044 
2045 	head_prop = NULL;
2046 	prev_prop = NULL;
2047 	for (prop = sp->cons_km_prop; prop != NULL; ) {
2048 		if (prop->cp_dev == dev) {
2049 			tmp_prop = prop->cp_next;
2050 			consconfig_free_prop(prop);
2051 			prop = tmp_prop;
2052 			if (prev_prop)
2053 				prev_prop->cp_next = prop;
2054 		} else {
2055 			if (head_prop == NULL)
2056 				head_prop = prop;
2057 			prev_prop = prop;
2058 			prop = prop->cp_next;
2059 		}
2060 	}
2061 	sp->cons_km_prop = head_prop;
2062 }
2063 
2064 /*
2065  * Add a dev according to prop
2066  */
2067 static void
2068 consconfig_add_dev(cons_state_t *sp, cons_prop_t *prop)
2069 {
2070 	prop->cp_next = sp->cons_km_prop;
2071 	sp->cons_km_prop = prop;
2072 }
2073 
2074 /*
2075  * Find a device from our list according to dev
2076  */
2077 static cons_prop_t *
2078 consconfig_find_dev(cons_state_t *sp, dev_t dev)
2079 {
2080 	cons_prop_t *prop;
2081 
2082 	for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) {
2083 		if (prop->cp_dev == dev)
2084 			break;
2085 	}
2086 
2087 	return (prop);
2088 }
2089 
2090 /*
2091  * Free a cons prop associated with a keyboard or mouse
2092  */
2093 static void
2094 consconfig_free_prop(cons_prop_t *prop)
2095 {
2096 	if (prop->cp_pushmod)
2097 		kmem_free(prop->cp_pushmod, strlen(prop->cp_pushmod) + 1);
2098 	kmem_free(prop, sizeof (cons_prop_t));
2099 }
2100 
2101 /*
2102  * The early boot code can't print to a usb serial device or the
2103  * graphical boot screen.
2104  *
2105  * The early boot messages are saved in a buffer at the address indicated
2106  * by "deferred-console-buf" This function flushes the message to the
2107  * current console now that it is set up.
2108  */
2109 static void
2110 flush_deferred_console_buf(void)
2111 {
2112 	int rval;
2113 	vnode_t *vp;
2114 	uint_t defcons_buf;
2115 	char *kc, *bc, *defcons_kern_buf;
2116 
2117 	/* defcons_buf is in low memory, so an int works here */
2118 	defcons_buf = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(),
2119 	    DDI_PROP_DONTPASS, "deferred-console-buf", 0);
2120 
2121 	if (defcons_buf == 0)
2122 		return;
2123 
2124 	/*
2125 	 * After consconfig() and before userland opens /dev/sysmsg,
2126 	 * console I/O is goes to polled I/O entry points.
2127 	 *
2128 	 * If usb-serial doesn't implement polled I/O, we need
2129 	 * to open /dev/console now to get kernel console I/O to work.
2130 	 * We also push ttcompat and ldterm explicitly to get the
2131 	 * correct output format (autopush isn't set up yet). We
2132 	 * ignore push errors because they are non-fatal.
2133 	 * Note that opening /dev/console causes rconsvp to be
2134 	 * opened as well.
2135 	 */
2136 	if (cons_polledio == NULL) {
2137 		if (vn_open("/dev/console", UIO_SYSSPACE, FWRITE | FNOCTTY,
2138 		    0, &vp, 0, 0) != 0)
2139 			return;
2140 
2141 		if (rconsvp) {
2142 			(void) strioctl(rconsvp, __I_PUSH_NOCTTY,
2143 			    (intptr_t)"ldterm", FKIOCTL, K_TO_K, kcred, &rval);
2144 			(void) strioctl(rconsvp, __I_PUSH_NOCTTY,
2145 			    (intptr_t)"ttcompat", FKIOCTL, K_TO_K,
2146 			    kcred, &rval);
2147 		}
2148 	}
2149 
2150 	/*
2151 	 * Copy message to a kernel buffer. Various kernel routines
2152 	 * expect buffer to be above kernelbase
2153 	 */
2154 	kc = defcons_kern_buf = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
2155 	bc = (char *)(uintptr_t)defcons_buf;
2156 	while (*kc++ = *bc++)
2157 		;
2158 	console_printf("%s", defcons_kern_buf);
2159 
2160 	kmem_free(defcons_kern_buf, MMU_PAGESIZE);
2161 }
2162 
2163 boolean_t
2164 consconfig_console_is_tipline(void)
2165 {
2166 	cons_state_t	*sp;
2167 
2168 	if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL)
2169 		return (B_FALSE);
2170 
2171 	if (sp->cons_input_type == CONSOLE_TIP)
2172 		return (B_TRUE);
2173 
2174 	return (B_FALSE);
2175 }
2176