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