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