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_stdin_is_keyboard() && 622 usb_kb_path != NULL) { 623 sp->cons_stdin_path = sp->cons_keyboard_path; 624 } else { 625 /* 626 * The standard in device may or may not be the same as 627 * the keyboard. Even if the keyboard is not the 628 * standard input, the keyboard console stream will 629 * still be built if the keyboard alias provided by the 630 * firmware exists and is valid. 631 */ 632 sp->cons_stdin_path = plat_stdinpath(); 633 } 634 635 sp->cons_li = ldi_ident_from_anon(); 636 637 /* Save the pointer for retrieval by the dacf functions */ 638 rval = space_store("consconfig", (uintptr_t)sp); 639 ASSERT(rval == 0); 640 641 return (sp); 642 } 643 644 static int 645 consconfig_relink_wc(cons_state_t *sp, ldi_handle_t new_lh, int *muxid) 646 { 647 int err, rval; 648 ldi_handle_t wc_lh; 649 dev_t wc_dev; 650 651 ASSERT(muxid != NULL); 652 653 /* 654 * NOTE: we could be in a dacf callback context right now. normally 655 * it's not legal to call any ldi_open_*() function from this context 656 * because we're currently holding device tree locks and if the 657 * ldi_open_*() call could try to acquire other device tree locks 658 * (to attach the device we're trying to open.) if this happens then 659 * we could deadlock. To avoid this situation, during initialization 660 * we made sure to grab a hold on the dip of the device we plan to 661 * open so that it can never be detached. Then we use 662 * ldi_open_by_dev() to actually open the device since it will see 663 * that the device is already attached and held and instead of 664 * acquire any locks it will only increase the reference count 665 * on the device. 666 */ 667 wc_dev = sp->cons_wc_vp->v_rdev; 668 err = ldi_open_by_dev(&wc_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY, 669 kcred, &wc_lh, sp->cons_li); 670 ASSERT(wc_dev == sp->cons_wc_vp->v_rdev); 671 if (err) { 672 cmn_err(CE_WARN, "consconfig_relink_wc: " 673 "unable to open wc device"); 674 return (err); 675 } 676 677 if (new_lh != NULL) { 678 DPRINTF(DPRINT_L0, "linking stream under wc\n"); 679 680 err = ldi_ioctl(wc_lh, I_PLINK, (uintptr_t)new_lh, 681 FKIOCTL, kcred, muxid); 682 if (err != 0) { 683 cmn_err(CE_WARN, "consconfig_relink_wc: " 684 "wc link failed, error %d", err); 685 } 686 } else { 687 DPRINTF(DPRINT_L0, "unlinking stream from under wc\n"); 688 689 err = ldi_ioctl(wc_lh, I_PUNLINK, *muxid, 690 FKIOCTL, kcred, &rval); 691 if (err != 0) { 692 cmn_err(CE_WARN, "consconfig_relink_wc: " 693 "wc unlink failed, error %d", err); 694 } else { 695 *muxid = -1; 696 } 697 } 698 699 (void) ldi_close(wc_lh, FREAD|FWRITE, kcred); 700 return (err); 701 } 702 703 static void 704 cons_build_upper_layer(cons_state_t *sp) 705 { 706 /* 707 * Build the wc->conskbd portion of the keyboard console stream. 708 * Even if no keyboard is attached to the system, the upper 709 * layer of the stream will be built. If the user attaches 710 * a keyboard after the system is booted, the keyboard driver 711 * and module will be linked under conskbd. 712 * 713 * Errors are generally ignored here because conskbd and wc 714 * are pseudo drivers and should be present on the system. 715 */ 716 717 /* open the console keyboard device. will never be closed */ 718 if (ldi_open_by_name(CONSKBD_PATH, FREAD|FWRITE|FNOCTTY, kcred, 719 &sp->conskbd_lh, sp->cons_li) != 0) 720 cmn_err(CE_PANIC, "consconfig: " 721 "unable to open conskbd device"); 722 723 DPRINTF(DPRINT_L0, "conskbd_lh = %p\n", sp->conskbd_lh); 724 725 /* open the console mouse device. will never be closed */ 726 if (ldi_open_by_name(CONSMS_PATH, FREAD|FWRITE|FNOCTTY, kcred, 727 &sp->consms_lh, sp->cons_li) != 0) 728 cmn_err(CE_PANIC, "consconfig: " 729 "unable to open consms device"); 730 731 DPRINTF(DPRINT_L0, "consms_lh = %p\n", sp->consms_lh); 732 733 /* 734 * Get a vnode for the wc device and then grab a hold on the 735 * device dip so it can never detach. We need to do this now 736 * because later we'll have to open the wc device in a context 737 * were it isn't safe to acquire any device tree locks (ie, during 738 * a dacf callback.) 739 */ 740 sp->cons_wc_vp = i_consconfig_createvp(WC_PATH); 741 if (sp->cons_wc_vp == NULL) 742 panic("consconfig: unable to find wc device"); 743 744 if (e_ddi_hold_devi_by_dev(sp->cons_wc_vp->v_rdev, 0) == NULL) 745 panic("consconfig: unable to hold wc device"); 746 747 /* 748 * Build the wc->conskbd portion of the keyboard console stream. 749 * Even if no keyboard is attached to the system, the upper 750 * layer of the stream will be built. If the user attaches 751 * a keyboard after the system is booted, the keyboard driver 752 * and module will be linked under conskbd. 753 * 754 * The act of linking conskbd under wc will cause wc to 755 * query the lower layers about their polled I/O routines 756 * using CONSOPENPOLLEDIO. This will fail on this link because 757 * there is not a physical keyboard linked under conskbd. 758 * 759 * Since conskbd and wc are pseudo drivers, errors are 760 * generally ignored when linking and unlinking them. 761 */ 762 (void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid); 763 764 /* 765 * Get a vnode for the redirection device. (It has the 766 * connection to the workstation console device wired into it, 767 * so that it's not necessary to establish the connection 768 * here. If the redirection device is ever generalized to 769 * handle multiple client devices, it won't be able to 770 * establish the connection itself, and we'll have to do it 771 * here.) 772 */ 773 wsconsvp = i_consconfig_createvp(IWSCN_PATH); 774 if (wsconsvp == NULL) 775 cmn_err(CE_PANIC, "consconfig: " 776 "unable to find iwscn device"); 777 778 #if defined(_CONSOLE_OUTPUT_VIA_SOFTWARE) 779 if (sp->cons_fb_path == NULL) { 780 cmn_err(CE_WARN, "consconfig: no screen found"); 781 return; 782 } else { 783 ldi_handle_t wc_lh; 784 struct strioctl strioc; 785 dev_t wc_dev; 786 int err, rval; 787 788 /* make sure the frame buffer device exists */ 789 if (ddi_pathname_to_dev_t(sp->cons_fb_path) == NODEV) { 790 cmn_err(CE_NOTE, "consconfig: " 791 "cannot find driver for screen device %s", 792 sp->cons_fb_path); 793 return; 794 } 795 796 /* tell wc to open the frame buffer device */ 797 wc_dev = sp->cons_wc_vp->v_rdev; 798 err = ldi_open_by_dev(&wc_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY, 799 kcred, &wc_lh, sp->cons_li); 800 ASSERT(wc_dev == sp->cons_wc_vp->v_rdev); 801 if (err) { 802 panic("cons_build_upper_layer: " 803 "unable to open wc device"); 804 /*NOTREACHED*/ 805 } 806 807 strioc.ic_cmd = WC_OPEN_FB; 808 strioc.ic_timout = INFTIM; 809 strioc.ic_len = strlen(sp->cons_fb_path) + 1; 810 strioc.ic_dp = sp->cons_fb_path; 811 812 err = ldi_ioctl(wc_lh, I_STR, (intptr_t)&strioc, 813 FKIOCTL, kcred, &rval); 814 if (err) { 815 cmn_err(CE_WARN, "cons_build_upper_layer: " 816 "could not attach frame buffer, error %d", err); 817 } 818 819 (void) ldi_close(wc_lh, FREAD|FWRITE, kcred); 820 } 821 #endif /* _CONSOLE_OUTPUT_VIA_SOFTWARE */ 822 } 823 824 static void 825 consconfig_load_drivers(cons_state_t *sp) 826 { 827 /* 828 * Calling ddi_pathname_to_dev_t causes the drivers to be loaded. 829 * The attaching of the drivers will cause the creation of the 830 * keyboard and mouse minor nodes, which will in turn trigger the 831 * dacf framework to call the keyboard and mouse configuration 832 * tasks. See PSARC/1998/212 for more details about the dacf 833 * framework. 834 * 835 * on sparc, when the console is ttya, zs0 is stdin/stdout, and zs1 836 * is kb/mouse. zs0 must be attached before zs1. The zs driver 837 * is written this way and the hardware may depend on this, too. 838 * It would be better to enforce this by attaching zs in sibling 839 * order with a driver property, such as ddi-attachall. 840 */ 841 if (sp->cons_stdin_path != NULL) 842 stdindev = ddi_pathname_to_dev_t(sp->cons_stdin_path); 843 if (stdindev == NODEV) { 844 DPRINTF(DPRINT_L0, 845 "!fail to attach stdin: %s\n", sp->cons_stdin_path); 846 } 847 if (sp->cons_stdout_path != NULL) 848 stdoutdev = ddi_pathname_to_dev_t(sp->cons_stdout_path); 849 if (sp->cons_keyboard_path != NULL) 850 kbddev = ddi_pathname_to_dev_t(sp->cons_keyboard_path); 851 if (sp->cons_mouse_path != NULL) 852 mousedev = ddi_pathname_to_dev_t(sp->cons_mouse_path); 853 DPRINTF(DPRINT_L0, "stdindev %lx, stdoutdev %lx, kbddev %lx, " 854 "mousedev %lx\n", stdindev, stdoutdev, kbddev, mousedev); 855 } 856 857 static void 858 consconfig_init_framebuffer(cons_state_t *sp) 859 { 860 if (!plat_stdout_is_framebuffer()) 861 return; 862 863 DPRINTF(DPRINT_L0, "stdout is framebuffer\n"); 864 ASSERT(strcmp(sp->cons_fb_path, sp->cons_stdout_path) == 0); 865 866 /* 867 * Console output is a framebuffer. 868 * Find the framebuffer driver if we can, and make 869 * ourselves a shadow vnode to track it with. 870 */ 871 fbdev = stdoutdev; 872 if (fbdev == NODEV) { 873 DPRINTF(DPRINT_L3, 874 "Can't find driver for console framebuffer\n"); 875 } else { 876 /* stdoutdev is valid, of fbvp should exist */ 877 fbvp = i_consconfig_createvp(sp->cons_stdout_path); 878 if (fbvp == NULL) { 879 panic("consconfig_load_drivers: " 880 "unable to find frame buffer device"); 881 /*NOTREACHED*/ 882 } 883 ASSERT(fbvp->v_rdev == fbdev); 884 885 /* console device is never released */ 886 fbdip = e_ddi_hold_devi_by_dev(fbdev, 0); 887 } 888 pm_cfb_setup(sp->cons_stdout_path); 889 } 890 891 /* 892 * consconfig_prepare_dev: 893 * Flush the stream, push "pushmod" onto the stream. 894 * for keyboard, issue the KIOCTRANSABLE ioctl, and 895 * possible enable abort. 896 */ 897 static void 898 consconfig_prepare_dev( 899 ldi_handle_t new_lh, 900 const char *pushmod, 901 int kbdtranslatable, 902 int input_type, 903 int dev_type) 904 { 905 int err, rval; 906 907 /* send a flush down the stream to the keyboard driver */ 908 (void) ldi_ioctl(new_lh, I_FLUSH, (intptr_t)FLUSHRW, 909 FKIOCTL, kcred, &rval); 910 911 if (pushmod) { 912 err = ldi_ioctl(new_lh, I_PUSH, (intptr_t)pushmod, 913 FKIOCTL, kcred, &rval); 914 if (err) { 915 cmn_err(CE_WARN, "consconfig_prepare_dev: " 916 "can't push streams module \"%s\", error %d", 917 pushmod, err); 918 } 919 } 920 921 if (dev_type == CONS_MS) 922 return; 923 924 ASSERT(dev_type == CONS_KBD); 925 926 err = ldi_ioctl(new_lh, KIOCTRANSABLE, 927 (intptr_t)&kbdtranslatable, FKIOCTL, kcred, &rval); 928 if (err) { 929 cmn_err(CE_WARN, "consconfig_prepare_dev: " 930 "KIOCTRANSABLE failed, error: %d", err); 931 } 932 933 /* 934 * During boot, dynamic_console_config() will call the 935 * function to enable abort on the console. If the 936 * keyboard is hotplugged after boot, check to see if 937 * the keyboard is the console input. If it is 938 * enable abort on it. 939 */ 940 if (input_type == CONSOLE_LOCAL) 941 (void) consconfig_kbd_abort_enable(new_lh); 942 } 943 944 /* 945 * consconfig_relink_conskbd: 946 * If new_lh is not NULL it should represent a driver with a 947 * keyboard module pushed on top of it. The driver is then linked 948 * underneath conskbd. the resulting stream will be 949 * wc->conskbd->"new_lh driver". 950 * 951 * If new_lh is NULL, then an unlink operation is done on conskbd 952 * that attempts to unlink the stream specified by *muxid. 953 * the resulting stream will be wc->conskbd. 954 */ 955 static int 956 consconfig_relink_conskbd(cons_state_t *sp, ldi_handle_t new_lh, int *muxid) 957 { 958 int err, rval; 959 int conskbd_relink = 0; 960 961 ASSERT(muxid != NULL); 962 963 DPRINTF(DPRINT_L0, "consconfig_relink_conskbd: " 964 "conskbd_lh = %p, new_lh = %p, muxid = %x\n", 965 sp->conskbd_lh, new_lh, *muxid); 966 967 /* 968 * If conskbd is linked under wc then temporarily unlink it 969 * from under wc so that the new_lh stream may be linked under 970 * conskbd. This has to be done because streams are built bottom 971 * up and linking a stream under conskbd isn't allowed when 972 * conskbd is linked under wc. 973 */ 974 if (sp->conskbd_muxid != -1) { 975 DPRINTF(DPRINT_L0, "unlinking conskbd from under wc\n"); 976 conskbd_relink = 1; 977 err = consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid); 978 if (err) { 979 cmn_err(CE_WARN, "consconfig_relink_conskbd: " 980 "wc unlink failed, error %d", err); 981 return (err); 982 } 983 } 984 985 if (new_lh != NULL) { 986 DPRINTF(DPRINT_L0, "linking keyboard under conskbd\n"); 987 988 /* Link the stream represented by new_lh under conskbd */ 989 err = ldi_ioctl(sp->conskbd_lh, I_PLINK, (uintptr_t)new_lh, 990 FKIOCTL, kcred, muxid); 991 if (err != 0) { 992 cmn_err(CE_WARN, "consconfig_relink_conskbd: " 993 "conskbd link failed, error %d", err); 994 goto relink_failed; 995 } 996 } else { 997 DPRINTF(DPRINT_L0, "unlinking keyboard from under conskbd\n"); 998 999 /* 1000 * This will cause the keyboard driver to be closed, 1001 * all modules to be popped, and the keyboard vnode released. 1002 */ 1003 err = ldi_ioctl(sp->conskbd_lh, I_PUNLINK, *muxid, 1004 FKIOCTL, kcred, &rval); 1005 if (err != 0) { 1006 cmn_err(CE_WARN, "consconfig_relink_conskbd: " 1007 "conskbd unlink failed, error %d", err); 1008 goto relink_failed; 1009 } else { 1010 *muxid = -1; 1011 } 1012 1013 consconfig_check_phys_kbd(sp); 1014 } 1015 1016 if (!conskbd_relink) 1017 return (err); 1018 1019 /* 1020 * Link consbkd back under wc. 1021 * 1022 * The act of linking conskbd back under wc will cause wc 1023 * to query the lower lower layers about their polled I/O 1024 * routines. This time the request will succeed because there 1025 * is a physical keyboard linked under conskbd. 1026 */ 1027 DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n"); 1028 err = consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid); 1029 if (err) { 1030 cmn_err(CE_WARN, "consconfig_relink_conskbd: " 1031 "wc link failed, error %d", err); 1032 } 1033 return (err); 1034 1035 relink_failed: 1036 if (!conskbd_relink) 1037 return (err); 1038 1039 /* something went wrong, try to reconnect conskbd back under wc */ 1040 DPRINTF(DPRINT_L0, "re-linking conskbd under wc\n"); 1041 (void) consconfig_relink_wc(sp, sp->conskbd_lh, &sp->conskbd_muxid); 1042 return (err); 1043 } 1044 1045 /* 1046 * consconfig_relink_consms: 1047 * If new_lh is not NULL it should represent a driver with a 1048 * mouse module pushed on top of it. The driver is then linked 1049 * underneath consms. the resulting stream will be 1050 * consms->"new_lh driver". 1051 * 1052 * If new_lh is NULL, then an unlink operation is done on consms 1053 * that attempts to unlink the stream specified by *muxid. 1054 */ 1055 static int 1056 consconfig_relink_consms(cons_state_t *sp, ldi_handle_t new_lh, int *muxid) 1057 { 1058 int err, rval; 1059 1060 DPRINTF(DPRINT_L0, "consconfig_relink_consms: " 1061 "consms_lh = %p, new_lh = %p, muxid = %x\n", 1062 (void *)sp->consms_lh, (void *)new_lh, *muxid); 1063 1064 if (new_lh != NULL) { 1065 DPRINTF(DPRINT_L0, "linking mouse under consms\n"); 1066 1067 /* Link ms/usbms stream underneath consms multiplexor. */ 1068 err = ldi_ioctl(sp->consms_lh, I_PLINK, (uintptr_t)new_lh, 1069 FKIOCTL, kcred, muxid); 1070 if (err != 0) { 1071 cmn_err(CE_WARN, "consconfig_relink_consms: " 1072 "mouse link failed, error %d", err); 1073 } 1074 } else { 1075 DPRINTF(DPRINT_L0, "unlinking mouse from under consms\n"); 1076 1077 /* Tear down the mouse stream */ 1078 err = ldi_ioctl(sp->consms_lh, I_PUNLINK, *muxid, 1079 FKIOCTL, kcred, &rval); 1080 if (err != 0) { 1081 cmn_err(CE_WARN, "consconfig_relink_consms: " 1082 "mouse unlink failed, error %d", err); 1083 } else { 1084 *muxid = -1; 1085 } 1086 } 1087 return (err); 1088 } 1089 1090 static int 1091 cons_get_input_type(void) 1092 { 1093 int type; 1094 /* 1095 * Now that we know what all the devices are, we can figure out 1096 * what kind of console we have. 1097 */ 1098 if (plat_stdin_is_keyboard()) { 1099 /* Stdin is from the system keyboard */ 1100 type = CONSOLE_LOCAL; 1101 } else if ((stdindev != NODEV) && (stdindev == stdoutdev)) { 1102 /* 1103 * A reliable indicator that we are doing a remote console 1104 * is that stdin and stdout are the same. 1105 * This is probably a tip line. 1106 */ 1107 type = CONSOLE_TIP; 1108 } else { 1109 type = CONSOLE_SERIAL_KEYBOARD; 1110 } 1111 1112 return (type); 1113 } 1114 1115 static void 1116 consconfig_init_input(cons_state_t *sp) 1117 { 1118 ldi_handle_t new_lh; 1119 dev_t cons_final_dev; 1120 int err; 1121 1122 cons_final_dev = NODEV; 1123 1124 switch (sp->cons_input_type) { 1125 case CONSOLE_LOCAL: 1126 DPRINTF(DPRINT_L0, "stdin is keyboard\n"); 1127 1128 /* 1129 * The machine is allowed to boot without a keyboard. 1130 * If a user attaches a keyboard later, the keyboard 1131 * will be hooked into the console stream with the dacf 1132 * functions. 1133 * 1134 * The only drivers that look at kbbdev are the 1135 * serial drivers, which looks at kbdev to see if 1136 * they should allow abort on a break. In the absence 1137 * of keyboard, the serial drivers won't be attached 1138 * for any keyboard instance. 1139 */ 1140 if (kbddev == NODEV) { 1141 /* 1142 * If there is a problem with the keyboard 1143 * during the driver loading, then the polled 1144 * input won't get setup properly if polled 1145 * input is needed. This means that if the 1146 * keyboard is hotplugged, the keyboard would 1147 * work normally, but going down to the 1148 * debugger would not work if polled input is 1149 * required. This field is set here. The next 1150 * time a keyboard is plugged in, the field is 1151 * checked in order to give the next keyboard a 1152 * chance at being registered for console 1153 * input. 1154 * 1155 * Although this code will rarely be needed, 1156 * USB keyboards can be flaky, so this code 1157 * will be useful on the occasion that the 1158 * keyboard doesn't enumerate when the drivers 1159 * are loaded. 1160 */ 1161 DPRINTF(DPRINT_L2, "Error with console keyboard\n"); 1162 sp->cons_keyboard_problem = B_TRUE; 1163 } 1164 stdindev = kbddev; 1165 cons_final_dev = sp->cons_wc_vp->v_rdev; 1166 break; 1167 1168 case CONSOLE_TIP: 1169 DPRINTF(DPRINT_L0, "console input is tty (%s)\n", 1170 sp->cons_stdin_path); 1171 1172 /* 1173 * Console device drivers must be able to output 1174 * after being closed. 1175 */ 1176 rconsvp = i_consconfig_createvp(sp->cons_stdin_path); 1177 if (rconsvp == NULL) 1178 cmn_err(CE_PANIC, "consconfig_init_input: " 1179 "unable to find stdin device (%s)", 1180 sp->cons_stdin_path); 1181 rconsdev = rconsvp->v_rdev; 1182 1183 ASSERT(rconsdev == stdindev); 1184 1185 cons_final_dev = rconsdev; 1186 break; 1187 1188 case CONSOLE_SERIAL_KEYBOARD: 1189 DPRINTF(DPRINT_L0, "stdin is serial keyboard\n"); 1190 1191 /* 1192 * Non-keyboard input device, but not rconsdev. 1193 * This is known as the "serial keyboard" case - the 1194 * most likely use is someone has a keyboard attached 1195 * to a serial port (tip) and still has output on a 1196 * framebuffer. 1197 * 1198 * In this case, the serial driver must be linked 1199 * directly beneath wc. Since conskbd was linked 1200 * underneath wc above, first we unlink conskbd. 1201 */ 1202 (void) consconfig_relink_wc(sp, NULL, &sp->conskbd_muxid); 1203 sp->conskbd_muxid = -1; 1204 1205 /* 1206 * Open the serial keyboard, configure it, 1207 * and link it underneath wc. 1208 */ 1209 err = ldi_open_by_name(sp->cons_stdin_path, 1210 FREAD|FWRITE|FNOCTTY, kcred, &new_lh, sp->cons_li); 1211 if (err == 0) { 1212 struct termios termios; 1213 int rval; 1214 int stdin_muxid; 1215 1216 consconfig_prepare_dev(new_lh, 1217 "kb", TR_CANNOT, sp->cons_input_type, CONS_KBD); 1218 1219 /* Re-set baud rate */ 1220 (void) ldi_ioctl(new_lh, TCGETS, (intptr_t)&termios, 1221 FKIOCTL, kcred, &rval); 1222 1223 /* Set baud rate */ 1224 if (consconfig_setmodes(stdindev, &termios) == 0) { 1225 err = ldi_ioctl(new_lh, 1226 TCSETSF, (intptr_t)&termios, 1227 FKIOCTL, kcred, &rval); 1228 if (err) { 1229 cmn_err(CE_WARN, 1230 "consconfig_init_input: " 1231 "TCSETSF failed, error %d", err); 1232 } 1233 } 1234 1235 /* 1236 * Now link the serial keyboard direcly under wc 1237 * we don't save the returned muxid because we 1238 * don't support changing/hotplugging the console 1239 * keyboard when it is a serial keyboard. 1240 */ 1241 (void) consconfig_relink_wc(sp, new_lh, &stdin_muxid); 1242 1243 (void) ldi_close(new_lh, FREAD|FWRITE, kcred); 1244 } 1245 1246 cons_final_dev = sp->cons_wc_vp->v_rdev; 1247 break; 1248 1249 default: 1250 panic("consconfig_init_input: " 1251 "unsupported console input/output combination"); 1252 /*NOTREACHED*/ 1253 } 1254 1255 /* 1256 * Use the redirection device/workstation console pair as the "real" 1257 * console if the latter hasn't already been set. 1258 * The workstation console driver needs to see rwsconsvp, but 1259 * all other access should be through the redirecting driver. 1260 */ 1261 if (rconsvp == NULL) { 1262 consconfig_dprintf(DPRINT_L0, "setup redirection driver\n"); 1263 rconsvp = wsconsvp; 1264 rconsdev = wsconsvp->v_rdev; 1265 } 1266 1267 ASSERT(cons_final_dev != NODEV); 1268 1269 err = ldi_open_by_dev(&cons_final_dev, OTYP_CHR, FREAD|FWRITE|FNOCTTY, 1270 kcred, &new_lh, sp->cons_li); 1271 if (err) { 1272 panic("consconfig_init_input: " 1273 "unable to open console device"); 1274 /*NOTREACHED*/ 1275 } 1276 1277 /* Enable abort on the console */ 1278 (void) consconfig_kbd_abort_enable(new_lh); 1279 1280 /* Now we must close it to make console logins happy */ 1281 (void) ldi_close(new_lh, FREAD|FWRITE, kcred); 1282 1283 /* Set up polled input if it is supported by the console device */ 1284 if (plat_use_polled_debug()) { 1285 /* 1286 * In the debug case, register the keyboard polled entry 1287 * points, but don't throw the switch in the debugger. This 1288 * allows the polled entry points to be checked by hand 1289 */ 1290 consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev); 1291 } else { 1292 consconfig_setup_polledio(sp, cons_final_dev); 1293 } 1294 1295 kadb_uses_kernel(); 1296 } 1297 1298 /* 1299 * This function kicks off the console configuration. 1300 * Configure keyboard and mouse. Main entry here. 1301 */ 1302 void 1303 dynamic_console_config(void) 1304 { 1305 cons_state_t *sp; 1306 1307 /* initialize space.c globals */ 1308 stdindev = NODEV; 1309 mousedev = NODEV; 1310 kbddev = NODEV; 1311 fbdev = NODEV; 1312 fbvp = NULL; 1313 fbdip = NULL; 1314 wsconsvp = NULL; 1315 rwsconsvp = NULL; 1316 rwsconsdev = NODEV; 1317 rconsvp = NULL; 1318 rconsdev = NODEV; 1319 1320 /* Initialize cons_state_t structure and console device paths */ 1321 sp = consconfig_state_init(); 1322 ASSERT(sp); 1323 1324 /* Build upper layer of console stream */ 1325 cons_build_upper_layer(sp); 1326 1327 /* 1328 * Load keyboard/mouse drivers. The dacf routines will 1329 * plumb the devices into the console stream 1330 * 1331 * At the conclusion of the ddi_pathname_to_dev_t calls, the keyboard 1332 * and mouse drivers are linked into their respective console 1333 * streams if the pathnames are valid. 1334 */ 1335 consconfig_load_drivers(sp); 1336 sp->cons_input_type = cons_get_input_type(); 1337 1338 /* 1339 * This is legacy special case code for the "cool" virtual console 1340 * for the Starfire project. Starfire has a dummy "ssp-serial" 1341 * node in the OBP device tree and cvc is a pseudo driver. 1342 */ 1343 if (sp->cons_stdout_path != NULL && stdindev == NODEV && 1344 strstr(sp->cons_stdout_path, "ssp-serial")) { 1345 /* 1346 * Setup the virtual console driver for Starfire 1347 * Note that console I/O will still go through prom for now 1348 * (notice we don't open the driver here). The cvc driver 1349 * will be activated when /dev/console is opened by init. 1350 * During that time, a cvcd daemon will be started that 1351 * will open the cvcredirection driver to facilitate 1352 * the redirection of console I/O from cvc to cvcd. 1353 */ 1354 rconsvp = i_consconfig_createvp(CVC_PATH); 1355 if (rconsvp == NULL) 1356 return; 1357 rconsdev = rconsvp->v_rdev; 1358 return; 1359 } 1360 1361 rwsconsvp = sp->cons_wc_vp; 1362 rwsconsdev = sp->cons_wc_vp->v_rdev; 1363 1364 1365 /* initialize framebuffer, console input, and redirection device */ 1366 consconfig_init_framebuffer(sp); 1367 consconfig_init_input(sp); 1368 1369 DPRINTF(DPRINT_L0, 1370 "mousedev %lx, kbddev %lx, fbdev %lx, rconsdev %lx\n", 1371 mousedev, kbddev, fbdev, rconsdev); 1372 } 1373 1374 1375 /* 1376 * Start of DACF interfaces 1377 */ 1378 1379 /* 1380 * Do the real job for keyboard/mouse auto-configuration. 1381 */ 1382 static int 1383 do_config(cons_state_t *sp, cons_prop_t *prop) 1384 { 1385 ldi_handle_t lh; 1386 dev_t dev; 1387 int error; 1388 1389 ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS)); 1390 1391 dev = prop->cp_dev; 1392 error = ldi_open_by_dev(&dev, OTYP_CHR, 1393 FREAD|FWRITE|FNOCTTY, kcred, &lh, sp->cons_li); 1394 if (error) { 1395 return (DACF_FAILURE); 1396 } 1397 ASSERT(dev == prop->cp_dev); /* clone not supported */ 1398 1399 /* 1400 * Prepare the new keyboard/mouse driver 1401 * to be linked under conskbd/consms. 1402 */ 1403 consconfig_prepare_dev(lh, prop->cp_pushmod, TR_CAN, 1404 sp->cons_input_type, prop->cp_type); 1405 1406 if (prop->cp_type == CONS_KBD) { 1407 /* 1408 * Tell the physical keyboard driver to send 1409 * the abort sequences up to the virtual keyboard 1410 * driver so that STOP and A (or F1 and A) 1411 * can be applied to different keyboards. 1412 */ 1413 (void) consconfig_kbd_abort_disable(lh); 1414 1415 /* Link the stream underneath conskbd */ 1416 error = consconfig_relink_conskbd(sp, lh, &prop->cp_muxid); 1417 } else { 1418 /* Link the stream underneath consms */ 1419 error = consconfig_relink_consms(sp, lh, &prop->cp_muxid); 1420 } 1421 1422 /* 1423 * At this point, the stream is: 1424 * for keyboard: wc->conskbd->["pushmod"->"kbd_vp driver"] 1425 * for mouse: consms->["module_name"->]"mouse_avp driver" 1426 */ 1427 1428 /* Close the driver stream, it will stay linked under conskbd */ 1429 (void) ldi_close(lh, FREAD|FWRITE, kcred); 1430 1431 if (error) { 1432 return (DACF_FAILURE); 1433 } 1434 1435 return (DACF_SUCCESS); 1436 } 1437 1438 static int 1439 do_unconfig(cons_state_t *sp, cons_prop_t *prop) 1440 { 1441 ASSERT((prop->cp_type == CONS_KBD) || (prop->cp_type == CONS_MS)); 1442 1443 if (prop->cp_type == CONS_KBD) 1444 return (consconfig_relink_conskbd(sp, NULL, &prop->cp_muxid)); 1445 else 1446 return (consconfig_relink_consms(sp, NULL, &prop->cp_muxid)); 1447 } 1448 1449 static int 1450 kb_ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int type) 1451 { 1452 major_t major; 1453 minor_t minor; 1454 dev_t dev; 1455 dev_info_t *dip; 1456 cons_state_t *sp; 1457 cons_prop_t *prop; 1458 const char *pushmod; 1459 1460 /* 1461 * Retrieve the state information 1462 * Some platforms may use the old-style "consconfig" to configure 1463 * console stream modules but may also support devices that happen 1464 * to match a rule in /etc/dacf.conf. This will cause a problem 1465 * since the console state structure will not be initialized. 1466 * In that case, these entry points should silently fail and 1467 * permit console to be plumbed later in boot. 1468 */ 1469 if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL) 1470 return (DACF_FAILURE); 1471 1472 dip = dacf_devinfo_node(minor_hdl); 1473 major = ddi_driver_major(dip); 1474 ASSERT(major != (major_t)-1); 1475 minor = dacf_minor_number(minor_hdl); 1476 dev = makedevice(major, minor); 1477 ASSERT(dev != NODEV); 1478 1479 DPRINTF(DPRINT_L0, "driver name = \"%s\", dev = 0x%lx, major = 0x%x\n", 1480 (char *)dacf_driver_name(minor_hdl), dev, major); 1481 1482 /* Access to the global variables is synchronized */ 1483 mutex_enter(&sp->cons_lock); 1484 1485 /* 1486 * Check if the keyboard/mouse has already configured. 1487 */ 1488 if (consconfig_find_dev(sp, dev) != NULL) { 1489 mutex_exit(&sp->cons_lock); 1490 return (DACF_SUCCESS); 1491 } 1492 1493 prop = kmem_zalloc(sizeof (cons_prop_t), KM_SLEEP); 1494 1495 /* Config the new keyboard/mouse device */ 1496 prop->cp_dev = dev; 1497 1498 pushmod = dacf_get_arg(arg_hdl, "pushmod"); 1499 prop->cp_pushmod = i_ddi_strdup((char *)pushmod, KM_SLEEP); 1500 1501 prop->cp_type = type; 1502 if (do_config(sp, prop) != DACF_SUCCESS) { 1503 /* 1504 * The keyboard/mouse node failed to open. 1505 * Set the major and minor numbers to 0 so 1506 * kb_unconfig/ms_unconfig won't unconfigure 1507 * this node if it is detached. 1508 */ 1509 mutex_exit(&sp->cons_lock); 1510 consconfig_free_prop(prop); 1511 return (DACF_FAILURE); 1512 } 1513 1514 consconfig_add_dev(sp, prop); 1515 1516 /* 1517 * See if there was a problem with the console keyboard during boot. 1518 * If so, try to register polled input for this keyboard. 1519 */ 1520 if ((type == CONS_KBD) && (sp->cons_keyboard_problem)) { 1521 consconfig_setup_polledio(sp, sp->cons_wc_vp->v_rdev); 1522 sp->cons_keyboard_problem = B_FALSE; 1523 } 1524 1525 /* Prevent autodetach due to memory pressure */ 1526 (void) ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1); 1527 1528 mutex_exit(&sp->cons_lock); 1529 1530 return (DACF_SUCCESS); 1531 } 1532 1533 static int 1534 kb_ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl) 1535 { 1536 _NOTE(ARGUNUSED(arg_hdl)) 1537 1538 major_t major; 1539 minor_t minor; 1540 dev_t dev; 1541 dev_info_t *dip; 1542 cons_state_t *sp; 1543 cons_prop_t *prop; 1544 1545 /* 1546 * Retrieve the state information 1547 * So if there isn't a state available, then this entry point just 1548 * returns. See note in kb_config(). 1549 */ 1550 if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL) 1551 return (DACF_SUCCESS); 1552 1553 dip = dacf_devinfo_node(minor_hdl); 1554 major = ddi_driver_major(dip); 1555 ASSERT(major != (major_t)-1); 1556 minor = dacf_minor_number(minor_hdl); 1557 dev = makedevice(major, minor); 1558 ASSERT(dev != NODEV); 1559 1560 /* 1561 * Check if the keyboard/mouse that is being detached 1562 * is the console keyboard/mouse or not. 1563 */ 1564 mutex_enter(&sp->cons_lock); 1565 if ((prop = consconfig_find_dev(sp, dev)) == NULL) { 1566 mutex_exit(&sp->cons_lock); 1567 return (DACF_SUCCESS); 1568 } 1569 1570 /* 1571 * This dev may be opened physically and then hotplugged out. 1572 */ 1573 if (prop->cp_muxid != -1) { 1574 (void) do_unconfig(sp, prop); 1575 consconfig_rem_dev(sp, dev); 1576 } 1577 1578 mutex_exit(&sp->cons_lock); 1579 1580 return (DACF_SUCCESS); 1581 } 1582 1583 /* 1584 * This is the post-attach / pre-detach action function for the keyboard 1585 * and mouse. This function is associated with a node type in /etc/dacf.conf. 1586 */ 1587 static int 1588 kb_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags) 1589 { 1590 _NOTE(ARGUNUSED(flags)) 1591 1592 return (kb_ms_config(minor_hdl, arg_hdl, CONS_KBD)); 1593 } 1594 1595 static int 1596 ms_config(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags) 1597 { 1598 _NOTE(ARGUNUSED(flags)) 1599 1600 return (kb_ms_config(minor_hdl, arg_hdl, CONS_MS)); 1601 } 1602 1603 static int 1604 kb_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags) 1605 { 1606 _NOTE(ARGUNUSED(flags)) 1607 1608 return (kb_ms_unconfig(minor_hdl, arg_hdl)); 1609 } 1610 1611 static int 1612 ms_unconfig(dacf_infohdl_t minor_hdl, dacf_arghdl_t arg_hdl, int flags) 1613 { 1614 _NOTE(ARGUNUSED(flags)) 1615 1616 return (kb_ms_unconfig(minor_hdl, arg_hdl)); 1617 } 1618 1619 /* 1620 * consconfig_link and consconfig_unlink are provided to support 1621 * direct access to physical keyboard/mouse underlying conskbd/ 1622 * consms. 1623 * When the keyboard/mouse is opened physically via its device 1624 * file, it will be unlinked from the virtual one, and when it 1625 * is closed physically, it will be linked back under the virtual 1626 * one. 1627 */ 1628 void 1629 consconfig_link(major_t major, minor_t minor) 1630 { 1631 char buf[MAXPATHLEN]; 1632 dev_t dev; 1633 cons_state_t *sp; 1634 cons_prop_t *prop; 1635 1636 if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL) 1637 return; 1638 1639 dev = makedevice(major, minor); 1640 ASSERT(dev != NODEV); 1641 1642 mutex_enter(&sp->cons_lock); 1643 if ((prop = consconfig_find_dev(sp, dev)) == NULL) { 1644 mutex_exit(&sp->cons_lock); 1645 return; 1646 } 1647 1648 if (do_config(sp, prop) != DACF_SUCCESS) { 1649 (void) ddi_dev_pathname(dev, 0, buf); 1650 if (prop->cp_type == CONS_KBD) 1651 cmn_err(CE_WARN, "Failed to relink the keyboard " 1652 "(%s) underneath virtual keyboard", buf); 1653 else 1654 cmn_err(CE_WARN, "Failed to relink the mouse " 1655 "(%s) underneath virtual mouse", buf); 1656 consconfig_rem_dev(sp, dev); 1657 } 1658 1659 mutex_exit(&sp->cons_lock); 1660 } 1661 1662 1663 int 1664 consconfig_unlink(major_t major, minor_t minor) 1665 { 1666 dev_t dev; 1667 cons_state_t *sp; 1668 cons_prop_t *prop; 1669 int error; 1670 1671 if ((sp = (cons_state_t *)space_fetch("consconfig")) == NULL) 1672 return (DACF_SUCCESS); 1673 1674 dev = makedevice(major, minor); 1675 ASSERT(dev != NODEV); 1676 1677 mutex_enter(&sp->cons_lock); 1678 if ((prop = consconfig_find_dev(sp, dev)) == NULL) { 1679 mutex_exit(&sp->cons_lock); 1680 return (DACF_FAILURE); 1681 } 1682 1683 error = do_unconfig(sp, prop); 1684 1685 /* 1686 * Keep this dev on the list, for this dev is still online. 1687 */ 1688 mutex_exit(&sp->cons_lock); 1689 1690 return (error); 1691 } 1692 1693 /* 1694 * Routine to set baud rate, bits-per-char, parity and stop bits 1695 * on the console line when necessary. 1696 */ 1697 static int 1698 consconfig_setmodes(dev_t dev, struct termios *termiosp) 1699 { 1700 char buf[MAXPATHLEN]; 1701 int len = MAXPATHLEN; 1702 char name[16]; 1703 int ppos, i, j; 1704 char *path; 1705 dev_t tdev; 1706 1707 /* 1708 * First, search for a devalias which matches this dev_t. 1709 * Try all of ttya through ttyz until no such alias 1710 */ 1711 (void) strcpy(name, "ttya"); 1712 for (i = 0; i < ('z'-'a'); i++) { 1713 name[3] = 'a' + i; /* increment device name */ 1714 path = get_alias(name, buf); 1715 if (path == NULL) 1716 return (1); 1717 1718 tdev = ddi_pathname_to_dev_t(path); 1719 if (tdev == dev) 1720 break; /* Exit loop if found */ 1721 } 1722 1723 if (i >= ('z'-'a')) 1724 return (1); /* If we didn't find it, return */ 1725 1726 /* 1727 * Now that we know which "tty" this corresponds to, retrieve 1728 * the "ttya-mode" options property, which tells us how to configure 1729 * the line. 1730 */ 1731 (void) strcpy(name, "ttya-mode"); /* name of option we want */ 1732 name[3] = 'a' + i; /* Adjust to correct line */ 1733 1734 for (j = 0; j < sizeof (buf); j++) 1735 buf[j] = 0; /* CROCK! */ 1736 if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_root_node(), 0, name, 1737 buf, &len) != DDI_PROP_SUCCESS) 1738 return (1); /* if no such option, just return */ 1739 1740 /* Clear out options we will be setting */ 1741 termiosp->c_cflag &= 1742 ~(CSIZE | CBAUD | CBAUDEXT | PARODD | PARENB | CSTOPB); 1743 1744 /* 1745 * Now, parse the string. Wish I could use sscanf(). 1746 * Format 9600,8,n,1,- 1747 * baud rate, bits-per-char, parity, stop-bits, ignored 1748 */ 1749 for (ppos = 0; ppos < (MAXPATHLEN-8); ppos++) { /* Find first comma */ 1750 if ((buf[ppos] == 0) || (buf[ppos] == ',')) 1751 break; 1752 } 1753 1754 if (buf[ppos] != ',') { 1755 cmn_err(CE_WARN, "consconfig_setmodes: " 1756 "invalid mode string %s", buf); 1757 return (1); 1758 } 1759 1760 for (i = 0; i < MAX_SPEEDS; i++) { 1761 if (strncmp(buf, speedtab[i].name, ppos) == 0) 1762 break; 1763 } 1764 1765 if (i >= MAX_SPEEDS) { 1766 cmn_err(CE_WARN, 1767 "consconfig_setmodes: unrecognized speed in %s", buf); 1768 return (1); 1769 } 1770 1771 /* Found the baud rate, set it */ 1772 termiosp->c_cflag |= speedtab[i].code & CBAUD; 1773 if (speedtab[i].code > 16) /* cfsetospeed! */ 1774 termiosp->c_cflag |= CBAUDEXT; 1775 1776 /* Set bits per character */ 1777 switch (buf[ppos+1]) { 1778 case '8': 1779 termiosp->c_cflag |= CS8; 1780 break; 1781 case '7': 1782 termiosp->c_cflag |= CS7; 1783 break; 1784 default: 1785 cmn_err(CE_WARN, 1786 "consconfig_setmodes: illegal bits-per-char %s", buf); 1787 return (1); 1788 } 1789 1790 /* Set parity */ 1791 switch (buf[ppos+3]) { 1792 case 'o': 1793 termiosp->c_cflag |= PARENB | PARODD; 1794 break; 1795 case 'e': 1796 termiosp->c_cflag |= PARENB; /* enabled, not odd */ 1797 break; 1798 case 'n': 1799 break; /* not enabled. */ 1800 default: 1801 cmn_err(CE_WARN, "consconfig_setmodes: illegal parity %s", buf); 1802 return (1); 1803 } 1804 1805 /* Set stop bits */ 1806 switch (buf[ppos+5]) { 1807 case '1': 1808 break; /* No extra stop bit */ 1809 case '2': 1810 termiosp->c_cflag |= CSTOPB; /* 1 extra stop bit */ 1811 break; 1812 default: 1813 cmn_err(CE_WARN, "consconfig_setmodes: " 1814 "illegal stop bits %s", buf); 1815 return (1); 1816 } 1817 1818 return (0); 1819 } 1820 1821 /* 1822 * Check to see if underlying keyboard devices are still online, 1823 * if any one is offline now, unlink it. 1824 */ 1825 static void 1826 consconfig_check_phys_kbd(cons_state_t *sp) 1827 { 1828 ldi_handle_t kb_lh; 1829 cons_prop_t *prop; 1830 int error; 1831 int rval; 1832 1833 for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) { 1834 if ((prop->cp_type != CONS_KBD) || (prop->cp_muxid == -1)) 1835 continue; 1836 1837 error = ldi_open_by_dev(&prop->cp_dev, OTYP_CHR, 1838 FREAD|FWRITE|FNOCTTY, kcred, &kb_lh, sp->cons_li); 1839 1840 if (error) { 1841 (void) ldi_ioctl(sp->conskbd_lh, I_PUNLINK, 1842 prop->cp_muxid, FKIOCTL, kcred, &rval); 1843 prop->cp_dev = NODEV; 1844 } else { 1845 (void) ldi_close(kb_lh, FREAD|FWRITE, kcred); 1846 } 1847 } 1848 1849 /* 1850 * Remove all disconnected keyboards, 1851 * whose dev is turned into NODEV above. 1852 */ 1853 consconfig_rem_dev(sp, NODEV); 1854 } 1855 1856 /* 1857 * Remove devices according to dev, which may be NODEV 1858 */ 1859 static void 1860 consconfig_rem_dev(cons_state_t *sp, dev_t dev) 1861 { 1862 cons_prop_t *prop; 1863 cons_prop_t *prev_prop; 1864 cons_prop_t *tmp_prop; 1865 cons_prop_t *head_prop; 1866 1867 head_prop = NULL; 1868 prev_prop = NULL; 1869 for (prop = sp->cons_km_prop; prop != NULL; ) { 1870 if (prop->cp_dev == dev) { 1871 tmp_prop = prop->cp_next; 1872 consconfig_free_prop(prop); 1873 prop = tmp_prop; 1874 if (prev_prop) 1875 prev_prop->cp_next = prop; 1876 } else { 1877 if (head_prop == NULL) 1878 head_prop = prop; 1879 prev_prop = prop; 1880 prop = prop->cp_next; 1881 } 1882 } 1883 sp->cons_km_prop = head_prop; 1884 } 1885 1886 /* 1887 * Add a dev according to prop 1888 */ 1889 static void 1890 consconfig_add_dev(cons_state_t *sp, cons_prop_t *prop) 1891 { 1892 prop->cp_next = sp->cons_km_prop; 1893 sp->cons_km_prop = prop; 1894 } 1895 1896 /* 1897 * Find a device from our list according to dev 1898 */ 1899 static cons_prop_t * 1900 consconfig_find_dev(cons_state_t *sp, dev_t dev) 1901 { 1902 cons_prop_t *prop; 1903 1904 for (prop = sp->cons_km_prop; prop; prop = prop->cp_next) { 1905 if (prop->cp_dev == dev) 1906 break; 1907 } 1908 1909 return (prop); 1910 } 1911 1912 /* 1913 * Free a cons prop associated with a keyboard or mouse 1914 */ 1915 static void 1916 consconfig_free_prop(cons_prop_t *prop) 1917 { 1918 if (prop->cp_pushmod) 1919 kmem_free(prop->cp_pushmod, strlen(prop->cp_pushmod) + 1); 1920 kmem_free(prop, sizeof (cons_prop_t)); 1921 } 1922