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