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