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