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