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