1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 23 /* All Rights Reserved */ 24 25 /* 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/errno.h> 32 #include <sys/inline.h> 33 #include <sys/termio.h> 34 #include <sys/stropts.h> 35 #include <sys/termios.h> 36 #include <sys/stream.h> 37 #include <sys/strtty.h> 38 #include <sys/strsubr.h> 39 #include <sys/strsun.h> 40 #include <sys/ddi.h> 41 #include <sys/sunddi.h> 42 #include <sys/note.h> 43 #include "sys/consdev.h" 44 #include <sys/kbd.h> 45 #include <sys/kbtrans.h> 46 #include "kb8042.h" 47 48 #include <sys/i8042.h> 49 50 #include "sys/kbio.h" /* for KIOCSLAYOUT */ 51 #include "sys/stat.h" 52 #include "sys/reboot.h" 53 #include <sys/promif.h> 54 #include <sys/beep.h> 55 #include <sys/inttypes.h> 56 57 /* 58 * For any keyboard, there is a unique code describing the position 59 * of the key on a keyboard. We refer to the code as "station number". 60 * The following table is used to map the station numbers from ps2 61 * AT/XT keyboards to that of a USB one. 62 * 63 * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which 64 * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry 65 * when in KB_PC mode. Note that this does not need to be made conditional 66 * on the architecture for which this module is complied because there are no 67 * keys that map to K8042_STOP on non-SPARC platforms. 68 */ 69 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = { 70 /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35, 71 /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42, 72 /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24, 73 /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4, 74 /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15, 75 /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27, 76 /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56, 77 /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231, 78 /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0, 79 /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80, 80 /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0, 81 /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84, 82 /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91, 83 /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0, 84 /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65, 85 /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0, 86 /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0, 87 /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0, 88 /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0, 89 /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0, 90 /* 160 */ 120, 0, 0, 0, 0, 0, 0, 0, 91 /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0, 92 /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0, 93 /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0, 94 /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0, 95 /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0, 96 /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0, 97 /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0, 98 /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0, 99 /* 232 */ 0, 0, 0, 0, 0, 0, 0, 0, 100 /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0, 101 /* 248 */ 0, 0, 0, 0 102 }; 103 104 /* 105 * DEBUG (or KD_DEBUG for just this module) turns on a flag called 106 * kb8042_enable_debug_hotkey. If kb8042_enable_debug_hotkey is true, 107 * then the following hotkeys are enabled: 108 * F10 - turn on debugging "normal" translations 109 * F9 - turn on debugging "getchar" translations 110 * F8 - turn on "low level" debugging 111 * F7 - turn on terse press/release debugging 112 * F1 - turn off all debugging 113 * The default value for kb8042_enable_debug_hotkey is false, disabling 114 * these hotkeys. 115 */ 116 117 #if defined(DEBUG) || defined(lint) 118 #define KD_DEBUG 119 #endif 120 121 #ifdef KD_DEBUG 122 boolean_t kb8042_enable_debug_hotkey = B_FALSE; 123 boolean_t kb8042_debug = B_FALSE; 124 boolean_t kb8042_getchar_debug = B_FALSE; 125 boolean_t kb8042_low_level_debug = B_FALSE; 126 boolean_t kb8042_pressrelease_debug = B_FALSE; 127 static void kb8042_debug_hotkey(int scancode); 128 #endif 129 130 #ifdef __sparc 131 #define USECS_PER_WAIT 100 132 #define MAX_WAIT_USECS 100000 /* in usecs = 100 ms */ 133 #define MIN_DELAY_USECS USECS_PER_WAIT 134 135 boolean_t kb8042_warn_unknown_scanset = B_TRUE; 136 int kb8042_default_scanset = 2; 137 138 #endif 139 140 enum state_return { STATE_NORMAL, STATE_INTERNAL }; 141 142 static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume); 143 static uint_t kb8042_intr(caddr_t arg); 144 static void kb8042_wait_poweron(struct kb8042 *kb8042); 145 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t); 146 static int kb8042_xlate_leds(int); 147 static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled); 148 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state); 149 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state); 150 static boolean_t kb8042_polled_keycheck( 151 struct kbtrans_hardware *hw, int *key, 152 enum keystate *state); 153 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *); 154 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos, 155 enum keystate state); 156 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd); 157 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *); 158 static void kb8042_iocdatamsg(queue_t *, mblk_t *); 159 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate); 160 static int kb8042_polled_ischar(cons_polledio_arg_t arg); 161 static int kb8042_polled_getchar(cons_polledio_arg_t arg); 162 static void kb8042_cleanup(struct kb8042 *kb8042); 163 164 static struct kbtrans_callbacks kb8042_callbacks = { 165 kb8042_streams_setled, 166 kb8042_polled_setled, 167 kb8042_polled_keycheck, 168 }; 169 170 extern struct keyboard keyindex_pc; 171 172 #define DRIVER_NAME(dip) ddi_driver_name(dip) 173 174 static char module_name[] = "kb8042"; 175 176 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, 177 cred_t *credp); 178 static int kb8042_close(queue_t *qp, int flag, cred_t *credp); 179 static int kb8042_wsrv(); 180 181 struct module_info kb8042_sinfo = { 182 42, /* Module ID */ 183 module_name, 184 0, 32, /* Minimum and maximum packet sizes */ 185 256, 128 /* High and low water marks */ 186 }; 187 188 static struct qinit kb8042_rinit = { 189 NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL 190 }; 191 192 static struct qinit kb8042_winit = { 193 putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL 194 }; 195 196 struct streamtab 197 kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL }; 198 199 struct kb8042 Kdws = {0}; 200 static dev_info_t *kb8042_dip = NULL; 201 202 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 203 void **result); 204 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t); 205 static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t); 206 207 static struct cb_ops cb_kb8042_ops = { 208 nulldev, /* cb_open */ 209 nulldev, /* cb_close */ 210 nodev, /* cb_strategy */ 211 nodev, /* cb_print */ 212 nodev, /* cb_dump */ 213 nodev, /* cb_read */ 214 nodev, /* cb_write */ 215 nodev, /* cb_ioctl */ 216 nodev, /* cb_devmap */ 217 nodev, /* cb_mmap */ 218 nodev, /* cb_segmap */ 219 nochpoll, /* cb_chpoll */ 220 ddi_prop_op, /* cb_prop_op */ 221 &kb8042_str_info, /* cb_stream */ 222 D_MP 223 }; 224 225 struct dev_ops kb8042_ops = { 226 DEVO_REV, /* devo_rev */ 227 0, /* devo_refcnt */ 228 kb8042_getinfo, /* devo_getinfo */ 229 nulldev, /* devo_identify */ 230 nulldev, /* devo_probe */ 231 kb8042_attach, /* devo_attach */ 232 kb8042_detach, /* devo_detach */ 233 nodev, /* devo_reset */ 234 &cb_kb8042_ops, /* devo_cb_ops */ 235 (struct bus_ops *)NULL, /* devo_bus_ops */ 236 NULL, /* devo_power */ 237 ddi_quiesce_not_needed, /* devo_quiesce */ 238 }; 239 240 241 /* 242 * This is the loadable module wrapper. 243 */ 244 #include <sys/modctl.h> 245 246 /* 247 * Module linkage information for the kernel. 248 */ 249 static struct modldrv modldrv = { 250 &mod_driverops, /* Type of module. This one is a driver */ 251 "PS/2 keyboard driver", 252 &kb8042_ops, /* driver ops */ 253 }; 254 255 static struct modlinkage modlinkage = { 256 MODREV_1, 257 (void *) &modldrv, 258 NULL 259 }; 260 261 int 262 _init(void) 263 { 264 int rv; 265 266 rv = mod_install(&modlinkage); 267 return (rv); 268 } 269 270 271 int 272 _fini(void) 273 { 274 return (mod_remove(&modlinkage)); 275 } 276 277 int 278 _info(struct modinfo *modinfop) 279 { 280 return (mod_info(&modlinkage, modinfop)); 281 } 282 283 #ifdef __sparc 284 static boolean_t 285 kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled) 286 { 287 int i; 288 int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL : 289 I8042_INT_INPUT_AVAIL; 290 int reps = timeout_usec / USECS_PER_WAIT; 291 292 for (i = 0; i < reps; i++) { 293 if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0) 294 return (B_TRUE); 295 296 if (i < (reps - 1)) 297 drv_usecwait(USECS_PER_WAIT); 298 } 299 return (B_FALSE); 300 } 301 302 static void 303 kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled) 304 { 305 int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA : 306 I8042_INT_INPUT_DATA; 307 308 while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) { 309 (void) ddi_get8(kb8042->handle, kb8042->addr + port); 310 } 311 } 312 313 static boolean_t 314 kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect, 315 boolean_t polled, int timeout, int *error, uint8_t *got) 316 { 317 int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA : 318 I8042_INT_INPUT_DATA; 319 uint8_t datab; 320 int err; 321 boolean_t rval; 322 323 kb8042_send_to_keyboard(kb8042, send, polled); 324 325 if (kb8042_is_input_avail(kb8042, timeout, polled)) { 326 err = 0; 327 datab = ddi_get8(kb8042->handle, kb8042->addr + port); 328 rval = ((datab == expect) ? B_TRUE : B_FALSE); 329 } else { 330 err = EAGAIN; 331 rval = B_FALSE; 332 } 333 334 if (error != NULL) 335 *error = err; 336 if (got != NULL) 337 *got = datab; 338 return (rval); 339 } 340 341 static const char * 342 kb8042_error_string(int errcode) 343 { 344 switch (errcode) { 345 case EAGAIN: 346 return ("Timed out"); 347 default: 348 return ("Unknown error"); 349 } 350 } 351 352 static int 353 kb8042_read_scanset(struct kb8042 *kb8042, boolean_t polled) 354 { 355 int scanset = -1; 356 int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA : 357 I8042_INT_INPUT_DATA; 358 int err; 359 uint8_t got; 360 361 kb8042_clear_input_buffer(kb8042, B_TRUE); 362 363 /* 364 * Send a "change scan code set" command to the keyboard. 365 * It should respond with an ACK. 366 */ 367 if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, polled, 368 MAX_WAIT_USECS, &err, &got) != B_TRUE) { 369 goto fail_read_scanset; 370 } 371 372 /* 373 * Send a 0. The keyboard should ACK the 0, then it should send the 374 * scan code set in use. 375 */ 376 if (kb8042_send_and_expect(kb8042, 0, KB_ACK, polled, 377 MAX_WAIT_USECS, &err, &got) != B_TRUE) { 378 goto fail_read_scanset; 379 } 380 381 /* 382 * The next input byte from the keyboard should be the scan code 383 * set in use, though some keyboards like to send a few more acks 384 * just for fun, so blow past those to get the keyboard scan code. 385 */ 386 while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) && 387 (scanset = ddi_get8(kb8042->handle, kb8042->addr + port)) 388 == KB_ACK) 389 ; 390 391 #ifdef KD_DEBUG 392 cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.", 393 scanset); 394 #endif 395 396 return (scanset); 397 398 fail_read_scanset: 399 #ifdef KD_DEBUG 400 if (err == 0) 401 cmn_err(CE_NOTE, "Could not read current scan set from " 402 "keyboard: %s. (Expected 0x%x, but got 0x%x).", 403 kb8042_error_string(err), KB_ACK, got); 404 else 405 cmn_err(CE_NOTE, "Could not read current scan set from " 406 "keyboard: %s.", kb8042_error_string(err)); 407 #endif 408 return (-1); 409 } 410 #endif 411 412 static int 413 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 414 { 415 int rc; 416 int scanset; 417 int leds; 418 419 struct kb8042 *kb8042 = &Kdws; 420 static ddi_device_acc_attr_t attr = { 421 DDI_DEVICE_ATTR_V0, 422 DDI_NEVERSWAP_ACC, 423 DDI_STRICTORDER_ACC, 424 }; 425 426 switch (cmd) { 427 case DDI_RESUME: 428 leds = kb8042->leds.commanded; 429 kb8042->w_init = 0; 430 kb8042_init(kb8042, B_TRUE); 431 kb8042_setled(kb8042, leds, B_FALSE); 432 mutex_enter(&kb8042->w_hw_mutex); 433 kb8042->suspended = B_FALSE; 434 if (kb8042->w_qp != NULL) { 435 enableok(WR(kb8042->w_qp)); 436 qenable(WR(kb8042->w_qp)); 437 } 438 cv_broadcast(&kb8042->suspend_cv); 439 mutex_exit(&kb8042->w_hw_mutex); 440 return (DDI_SUCCESS); 441 442 case DDI_ATTACH: 443 if (kb8042_dip != NULL) 444 return (DDI_FAILURE); 445 /* The rest of the function is for attach */ 446 break; 447 448 default: 449 return (DDI_FAILURE); 450 } 451 452 kb8042->debugger.mod1 = 58; /* Left Ctrl */ 453 kb8042->debugger.mod2 = 60; /* Left Alt */ 454 kb8042->debugger.trigger = 33; /* D */ 455 kb8042->debugger.mod1_down = B_FALSE; 456 kb8042->debugger.mod2_down = B_FALSE; 457 kb8042->debugger.enabled = B_FALSE; 458 459 kb8042_dip = devi; 460 kb8042->init_state = KB8042_UNINITIALIZED; 461 462 kb8042->polled_synthetic_release_pending = B_FALSE; 463 464 if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0, 465 DDI_NT_KEYBOARD, 0) == DDI_FAILURE) { 466 goto failure; 467 } 468 469 kb8042->init_state |= KB8042_MINOR_NODE_CREATED; 470 471 rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr, 472 (offset_t)0, (offset_t)0, &attr, &kb8042->handle); 473 if (rc != DDI_SUCCESS) { 474 #if defined(KD_DEBUG) 475 cmn_err(CE_WARN, "kb8042_attach: can't map registers"); 476 #endif 477 goto failure; 478 } 479 480 kb8042->init_state |= KB8042_REGS_MAPPED; 481 482 if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) != 483 DDI_SUCCESS) { 484 cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie"); 485 goto failure; 486 } 487 488 mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock); 489 cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL); 490 cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL); 491 kb8042->init_state |= KB8042_HW_MUTEX_INITTED; 492 493 kb8042_init(kb8042, B_FALSE); 494 495 #ifdef __sparc 496 /* Detect the scan code set currently in use */ 497 scanset = kb8042_read_scanset(kb8042, B_TRUE); 498 499 if (scanset < 0 && kb8042_warn_unknown_scanset) { 500 501 cmn_err(CE_WARN, "Cannot determine keyboard scan code set "); 502 cmn_err(CE_CONT, "(is the keyboard plugged in?). "); 503 cmn_err(CE_CONT, "Defaulting to scan code set %d. If the " 504 "keyboard does not ", kb8042_default_scanset); 505 cmn_err(CE_CONT, "work properly, add " 506 "`set kb8042:kb8042_default_scanset=%d' to /etc/system ", 507 (kb8042_default_scanset == 1) ? 2 : 1); 508 cmn_err(CE_CONT, "(via network or with a USB keyboard) and " 509 "restart the system. If you "); 510 cmn_err(CE_CONT, "do not want to see this message in the " 511 "future, add "); 512 cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' " 513 "to /etc/system.\n"); 514 515 /* Use the default scan code set. */ 516 scanset = kb8042_default_scanset; 517 } 518 #else 519 /* x86 systems use scan code set 1 -- no detection required */ 520 scanset = 1; 521 #endif 522 if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) { 523 cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: " 524 "Unknown scan code set `%d'.", scanset); 525 /* Scan code set is not supported */ 526 goto failure; 527 } 528 529 /* 530 * Turn on interrupts... 531 */ 532 if (ddi_add_intr(devi, 0, 533 &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL, 534 kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) { 535 cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt"); 536 goto failure; 537 } 538 539 kb8042->init_state |= KB8042_INTR_ADDED; 540 541 ddi_report_dev(devi); 542 543 #ifdef KD_DEBUG 544 cmn_err(CE_CONT, "?%s instance #%d READY\n", 545 DRIVER_NAME(devi), ddi_get_instance(devi)); 546 #endif 547 548 return (DDI_SUCCESS); 549 550 failure: 551 kb8042_cleanup(kb8042); 552 return (DDI_FAILURE); 553 } 554 555 static int 556 kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 557 { 558 struct kb8042 *kb8042 = &Kdws; 559 560 switch (cmd) { 561 case DDI_SUSPEND: 562 mutex_enter(&kb8042->w_hw_mutex); 563 ASSERT(kb8042->ops >= 0); 564 while (kb8042->ops > 0) 565 cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex); 566 kb8042->suspended = B_TRUE; 567 mutex_exit(&kb8042->w_hw_mutex); 568 return (DDI_SUCCESS); 569 570 case DDI_DETACH: 571 /* If someone has a stream open, fail to detach */ 572 if (kb8042->w_qp != NULL) 573 return (DDI_FAILURE); 574 575 ASSERT(kb8042_dip == dip); 576 577 kb8042_cleanup(kb8042); 578 579 return (DDI_SUCCESS); 580 581 default: 582 return (DDI_FAILURE); 583 } 584 } 585 586 /*ARGSUSED*/ 587 static int 588 kb8042_getinfo( 589 dev_info_t *dip, 590 ddi_info_cmd_t infocmd, 591 void *arg, 592 void **result) 593 { 594 register int error; 595 596 switch (infocmd) { 597 case DDI_INFO_DEVT2DEVINFO: 598 if (kb8042_dip == NULL) { 599 error = DDI_FAILURE; 600 } else { 601 *result = (void *) kb8042_dip; 602 error = DDI_SUCCESS; 603 } 604 break; 605 case DDI_INFO_DEVT2INSTANCE: 606 *result = (void *)0; 607 error = DDI_SUCCESS; 608 break; 609 default: 610 error = DDI_FAILURE; 611 break; 612 } 613 return (error); 614 } 615 616 static void 617 kb8042_cleanup(struct kb8042 *kb8042) 618 { 619 ASSERT(kb8042_dip != NULL); 620 621 if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) { 622 cv_destroy(&kb8042->suspend_cv); 623 cv_destroy(&kb8042->ops_cv); 624 mutex_destroy(&kb8042->w_hw_mutex); 625 } 626 627 if (kb8042->init_state & KB8042_INTR_ADDED) 628 ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock); 629 630 if (kb8042->init_state & KB8042_REGS_MAPPED) 631 ddi_regs_map_free(&kb8042->handle); 632 633 if (kb8042->init_state & KB8042_MINOR_NODE_CREATED) 634 ddi_remove_minor_node(kb8042_dip, NULL); 635 636 kb8042->init_state = KB8042_UNINITIALIZED; 637 kb8042_dip = NULL; 638 } 639 640 static void 641 kb8042_init(struct kb8042 *kb8042, boolean_t from_resume) 642 { 643 if (kb8042->w_init) 644 return; 645 646 if (!from_resume) { 647 kb8042->w_kblayout = 0; /* Default to US */ 648 kb8042->w_qp = (queue_t *)NULL; 649 kb8042->simulated_kbd_type = KB_PC; 650 kb8042->leds.commanded = -1; /* Unknown initial state */ 651 kb8042->leds.desired = -1; /* Unknown initial state */ 652 } 653 654 kb8042_wait_poweron(kb8042); 655 656 kb8042->kb_old_key_pos = 0; 657 658 /* Set up the command state machine and start it running. */ 659 kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE); 660 661 kb8042->w_init++; 662 663 (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */ 664 } 665 666 /*ARGSUSED2*/ 667 static int 668 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp) 669 { 670 struct kb8042 *kb8042; 671 int err; 672 int initial_leds; 673 int initial_led_mask; 674 675 kb8042 = &Kdws; 676 677 mutex_enter(&kb8042->w_hw_mutex); 678 while (kb8042->suspended) { 679 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) == 680 0) { 681 mutex_exit(&kb8042->w_hw_mutex); 682 return (EINTR); 683 } 684 } 685 686 kb8042->w_dev = *devp; 687 688 if (qp->q_ptr) { 689 mutex_exit(&kb8042->w_hw_mutex); 690 return (0); 691 } 692 qp->q_ptr = (caddr_t)kb8042; 693 WR(qp)->q_ptr = qp->q_ptr; 694 if (!kb8042->w_qp) 695 kb8042->w_qp = qp; 696 697 ASSERT(kb8042->ops >= 0); 698 kb8042->ops++; 699 mutex_exit(&kb8042->w_hw_mutex); 700 701 kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask); 702 err = kbtrans_streams_init(qp, sflag, credp, 703 (struct kbtrans_hardware *)kb8042, &kb8042_callbacks, 704 &kb8042->hw_kbtrans, 705 initial_leds, initial_led_mask); 706 if (err != 0) 707 return (err); 708 709 kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc); 710 711 kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1; 712 kb8042->polledio.cons_polledio_argument = 713 (cons_polledio_arg_t)kb8042; 714 kb8042->polledio.cons_polledio_putchar = NULL; 715 kb8042->polledio.cons_polledio_getchar = 716 (int (*)(cons_polledio_arg_t))kb8042_polled_getchar; 717 kb8042->polledio.cons_polledio_ischar = 718 (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar; 719 kb8042->polledio.cons_polledio_enter = NULL; 720 kb8042->polledio.cons_polledio_exit = NULL; 721 kb8042->polledio.cons_polledio_setled = 722 (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled; 723 kb8042->polledio.cons_polledio_keycheck = 724 (boolean_t (*)(cons_polledio_arg_t, int *, 725 enum keystate *))kb8042_polled_keycheck; 726 727 qprocson(qp); 728 729 kbtrans_streams_enable(kb8042->hw_kbtrans); 730 731 mutex_enter(&kb8042->w_hw_mutex); 732 ASSERT(kb8042->ops > 0); 733 kb8042->ops--; 734 if (kb8042->ops == 0) 735 cv_broadcast(&kb8042->ops_cv); 736 mutex_exit(&kb8042->w_hw_mutex); 737 738 return (0); 739 } 740 741 /*ARGSUSED1*/ 742 static int 743 kb8042_close(queue_t *qp, int flag, cred_t *credp) 744 { 745 struct kb8042 *kb8042; 746 747 /* If a beep is in progress, stop that */ 748 (void) beeper_off(); 749 750 kb8042 = (struct kb8042 *)qp->q_ptr; 751 752 mutex_enter(&kb8042->w_hw_mutex); 753 while (kb8042->suspended) { 754 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) == 755 0) { 756 mutex_exit(&kb8042->w_hw_mutex); 757 return (EINTR); 758 } 759 } 760 761 ASSERT(kb8042->ops >= 0); 762 kb8042->ops++; 763 mutex_exit(&kb8042->w_hw_mutex); 764 765 (void) kbtrans_streams_fini(kb8042->hw_kbtrans); 766 767 kb8042->w_qp = (queue_t *)NULL; 768 qprocsoff(qp); 769 770 mutex_enter(&kb8042->w_hw_mutex); 771 ASSERT(kb8042->ops > 0); 772 kb8042->ops--; 773 if (kb8042->ops == 0) 774 cv_broadcast(&kb8042->ops_cv); 775 mutex_exit(&kb8042->w_hw_mutex); 776 777 return (0); 778 } 779 780 static int 781 kb8042_wsrv(queue_t *qp) 782 { 783 struct kb8042 *kb8042; 784 785 mblk_t *mp; 786 boolean_t suspended; 787 788 kb8042 = (struct kb8042 *)qp->q_ptr; 789 790 mutex_enter(&kb8042->w_hw_mutex); 791 suspended = kb8042->suspended; 792 ASSERT(kb8042->ops >= 0); 793 if (!suspended) 794 kb8042->ops++; 795 mutex_exit(&kb8042->w_hw_mutex); 796 797 #ifdef NO_KB_DEBUG 798 while (!suspended && (mp = getq(qp)) != NULL) { 799 #else 800 /* 801 * Not taking keyboard input while suspending can make debugging 802 * difficult. However, we still do the ops counting so that we 803 * don't suspend at a bad time. 804 */ 805 while ((mp = getq(qp))) { 806 #endif 807 switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) { 808 case KBTRANS_MESSAGE_HANDLED: 809 continue; 810 case KBTRANS_MESSAGE_NOT_HANDLED: 811 break; 812 } 813 switch (mp->b_datap->db_type) { 814 case M_IOCTL: 815 kb8042_ioctlmsg(kb8042, qp, mp); 816 continue; 817 case M_IOCDATA: 818 kb8042_iocdatamsg(qp, mp); 819 continue; 820 case M_DELAY: 821 case M_STARTI: 822 case M_STOPI: 823 case M_READ: /* ignore, no buffered data */ 824 freemsg(mp); 825 continue; 826 case M_FLUSH: 827 *mp->b_rptr &= ~FLUSHW; 828 if (*mp->b_rptr & FLUSHR) 829 qreply(qp, mp); 830 else 831 freemsg(mp); 832 continue; 833 default: 834 cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x", 835 mp->b_datap->db_type); 836 freemsg(mp); 837 continue; 838 } 839 } 840 841 mutex_enter(&kb8042->w_hw_mutex); 842 if (!suspended) { 843 ASSERT(kb8042->ops > 0); 844 kb8042->ops--; 845 if (kb8042->ops == 0) 846 cv_broadcast(&kb8042->ops_cv); 847 } 848 mutex_exit(&kb8042->w_hw_mutex); 849 850 return (0); 851 } 852 853 static void 854 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp) 855 { 856 struct iocblk *iocp; 857 mblk_t *datap; 858 int error; 859 int tmp; 860 int cycles; 861 int frequency; 862 int msecs; 863 864 iocp = (struct iocblk *)mp->b_rptr; 865 866 switch (iocp->ioc_cmd) { 867 868 case CONSOPENPOLLEDIO: 869 error = miocpullup(mp, sizeof (struct cons_polledio *)); 870 if (error != 0) { 871 miocnak(qp, mp, 0, error); 872 return; 873 } 874 875 /* 876 * We are given an appropriate-sized data block, 877 * and return a pointer to our structure in it. 878 */ 879 *(struct cons_polledio **)mp->b_cont->b_rptr = 880 &kb8042->polledio; 881 mp->b_datap->db_type = M_IOCACK; 882 iocp->ioc_error = 0; 883 qreply(qp, mp); 884 break; 885 886 case CONSCLOSEPOLLEDIO: 887 miocack(qp, mp, 0, 0); 888 break; 889 890 case CONSSETABORTENABLE: 891 if (iocp->ioc_count != TRANSPARENT) { 892 miocnak(qp, mp, 0, EINVAL); 893 return; 894 } 895 896 kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr; 897 miocack(qp, mp, 0, 0); 898 break; 899 900 /* 901 * Valid only in TR_UNTRANS_MODE mode. 902 */ 903 case CONSSETKBDTYPE: 904 error = miocpullup(mp, sizeof (int)); 905 if (error != 0) { 906 miocnak(qp, mp, 0, error); 907 return; 908 } 909 tmp = *(int *)mp->b_cont->b_rptr; 910 if (tmp != KB_PC && tmp != KB_USB) { 911 miocnak(qp, mp, 0, EINVAL); 912 break; 913 } 914 kb8042->simulated_kbd_type = tmp; 915 miocack(qp, mp, 0, 0); 916 break; 917 918 case KIOCLAYOUT: 919 if (kb8042->w_kblayout == -1) { 920 miocnak(qp, mp, 0, EINVAL); 921 return; 922 } 923 924 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) { 925 miocnak(qp, mp, 0, ENOMEM); 926 return; 927 } 928 929 if (kb8042->simulated_kbd_type == KB_USB) 930 *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT; 931 else 932 *(int *)datap->b_wptr = kb8042->w_kblayout; 933 934 datap->b_wptr += sizeof (int); 935 if (mp->b_cont) 936 freemsg(mp->b_cont); 937 mp->b_cont = datap; 938 iocp->ioc_count = sizeof (int); 939 mp->b_datap->db_type = M_IOCACK; 940 iocp->ioc_error = 0; 941 qreply(qp, mp); 942 break; 943 944 case KIOCSLAYOUT: 945 if (iocp->ioc_count != TRANSPARENT) { 946 miocnak(qp, mp, 0, EINVAL); 947 return; 948 } 949 950 kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr; 951 miocack(qp, mp, 0, 0); 952 break; 953 954 case KIOCCMD: 955 error = miocpullup(mp, sizeof (int)); 956 if (error != 0) { 957 miocnak(qp, mp, 0, error); 958 return; 959 } 960 961 kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr); 962 miocack(qp, mp, 0, 0); 963 break; 964 965 case KIOCMKTONE: 966 if (iocp->ioc_count != TRANSPARENT) { 967 miocnak(qp, mp, 0, EINVAL); 968 return; 969 } 970 971 tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr); 972 cycles = tmp & 0xffff; 973 msecs = (tmp >> 16) & 0xffff; 974 975 if (cycles == 0) 976 frequency = UINT16_MAX; 977 else if (cycles == UINT16_MAX) 978 frequency = 0; 979 else { 980 frequency = (PIT_HZ + cycles / 2) / cycles; 981 if (frequency > UINT16_MAX) 982 frequency = UINT16_MAX; 983 } 984 985 error = beep_mktone(frequency, msecs); 986 if (error != 0) 987 miocnak(qp, mp, 0, error); 988 else 989 miocack(qp, mp, 0, 0); 990 break; 991 992 default: 993 #ifdef DEBUG1 994 cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd); 995 #endif 996 miocnak(qp, mp, 0, EINVAL); 997 return; 998 } 999 } 1000 1001 /* 1002 * Process a byte received from the keyboard 1003 */ 1004 static void 1005 kb8042_received_byte( 1006 struct kb8042 *kb8042, 1007 int scancode) /* raw scan code */ 1008 { 1009 boolean_t legit; /* is this a legit key pos'n? */ 1010 int key_pos = -1; 1011 enum keystate state; 1012 boolean_t synthetic_release_needed; 1013 1014 #ifdef KD_DEBUG 1015 kb8042_debug_hotkey(scancode); 1016 #endif 1017 1018 if (!kb8042->w_init) /* can't do anything anyway */ 1019 return; 1020 1021 legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state, 1022 &synthetic_release_needed); 1023 1024 if (legit == 0) { 1025 /* Eaten by translation */ 1026 #ifdef KD_DEBUG 1027 if (kb8042_debug) 1028 prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode); 1029 #endif 1030 return; 1031 } 1032 1033 #ifdef KD_DEBUG 1034 if (kb8042_debug) { 1035 prom_printf("kb8042_intr: 0x%x -> %s %d", 1036 scancode, 1037 state == KEY_RELEASED ? "released" : "pressed", 1038 key_pos); 1039 } 1040 #endif 1041 1042 /* 1043 * Don't know if we want this permanently, but it seems interesting 1044 * for the moment. 1045 */ 1046 if (key_pos == kb8042->debugger.mod1) { 1047 #ifdef KD_DEBUG 1048 if (kb8042_debug) 1049 prom_printf(" -> debug mod1"); 1050 #endif 1051 kb8042->debugger.mod1_down = (state == KEY_PRESSED); 1052 } 1053 if (key_pos == kb8042->debugger.mod2) { 1054 #ifdef KD_DEBUG 1055 if (kb8042_debug) 1056 prom_printf(" -> debug mod2"); 1057 #endif 1058 kb8042->debugger.mod2_down = (state == KEY_PRESSED); 1059 } 1060 if (kb8042->debugger.enabled && 1061 key_pos == kb8042->debugger.trigger && 1062 kb8042->debugger.mod1_down && 1063 kb8042->debugger.mod2_down) { 1064 #ifdef KD_DEBUG 1065 if (kb8042_debug) 1066 prom_printf(" -> debugger\n"); 1067 #endif 1068 /* 1069 * Require new presses of the modifiers. 1070 */ 1071 kb8042->debugger.mod1_down = B_FALSE; 1072 kb8042->debugger.mod2_down = B_FALSE; 1073 abort_sequence_enter(NULL); 1074 return; 1075 } 1076 1077 /* 1078 * If there's no queue above us - as can happen if we've been 1079 * attached but not opened - drop the keystroke. 1080 * Note that we do this here instead of above so that 1081 * Ctrl-Alt-D still works. 1082 */ 1083 if (kb8042->w_qp == NULL) { 1084 #ifdef KD_DEBUG 1085 if (kb8042_debug) 1086 prom_printf(" -> nobody home\n"); 1087 #endif 1088 return; 1089 } 1090 1091 /* 1092 * This is to filter out auto repeat since it can't be 1093 * turned off at the hardware. (Yeah, yeah, PS/2 keyboards 1094 * can. Don't know whether they've taken over the world. 1095 * Don't think so.) 1096 */ 1097 if (kb8042_autorepeat_detect(kb8042, key_pos, state)) { 1098 #ifdef KD_DEBUG 1099 if (kb8042_debug) 1100 prom_printf(" -> autorepeat ignored\n"); 1101 #endif 1102 return; 1103 } 1104 1105 #ifdef KD_DEBUG 1106 if (kb8042_debug) 1107 prom_printf(" -> OK\n"); 1108 #endif 1109 1110 #if defined(KD_DEBUG) 1111 if (kb8042_pressrelease_debug) { 1112 prom_printf(" %s%d ", 1113 state == KEY_PRESSED ? "+" : "-", 1114 key_pos); 1115 } 1116 #endif 1117 1118 kb8042_process_key(kb8042, key_pos, state); 1119 1120 /* 1121 * This is a total hack. For some stupid reason, the two additional 1122 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press 1123 * only. We synthesize a release immediately. 1124 */ 1125 if (synthetic_release_needed) { 1126 #if defined(KD_DEBUG) 1127 if (kb8042_debug) 1128 prom_printf("synthetic release %d\n", key_pos); 1129 if (kb8042_pressrelease_debug) 1130 prom_printf(" -%d(s) ", key_pos); 1131 #endif 1132 (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED); 1133 kb8042_process_key(kb8042, key_pos, state); 1134 } 1135 } 1136 1137 1138 static void 1139 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos, 1140 enum keystate state) 1141 { 1142 kbtrans_key_t key; 1143 1144 ASSERT(key_pos >= 0 && key_pos <= 255); 1145 if (kb8042->simulated_kbd_type == KB_PC) { 1146 kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state); 1147 } else if (kb8042->simulated_kbd_type == KB_USB) { 1148 key = keytab_pc2usb[key_pos]; 1149 if (key != 0) { 1150 kbtrans_streams_key(kb8042->hw_kbtrans, key, state); 1151 } 1152 } 1153 } 1154 1155 /* 1156 * Called from interrupt handler when keyboard interrupt occurs. 1157 */ 1158 static uint_t 1159 kb8042_intr(caddr_t arg) 1160 { 1161 uchar_t scancode; /* raw scan code */ 1162 int rc; 1163 struct kb8042 *kb8042 = (struct kb8042 *)arg; 1164 1165 rc = DDI_INTR_UNCLAIMED; 1166 1167 if (kb8042->init_state == KB8042_UNINITIALIZED) 1168 return (DDI_INTR_UNCLAIMED); 1169 1170 /* don't care if drv_setparm succeeds */ 1171 (void) drv_setparm(SYSRINT, 1); 1172 1173 while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL) 1174 != 0) { 1175 rc = DDI_INTR_CLAIMED; 1176 1177 scancode = ddi_get8(kb8042->handle, 1178 kb8042->addr + I8042_INT_INPUT_DATA); 1179 1180 #if defined(KD_DEBUG) 1181 if (kb8042_low_level_debug) 1182 prom_printf(" <K:%x ", scancode); 1183 #endif 1184 1185 kb8042_received_byte(kb8042, scancode); 1186 } 1187 1188 return (rc); 1189 } 1190 1191 static void 1192 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp) 1193 { 1194 struct copyresp *csp; 1195 1196 csp = (struct copyresp *)mp->b_rptr; 1197 if (csp->cp_rval) { 1198 freemsg(mp); 1199 return; 1200 } 1201 1202 switch (csp->cp_cmd) { 1203 default: 1204 miocack(qp, mp, 0, 0); 1205 break; 1206 } 1207 } 1208 1209 static boolean_t 1210 kb8042_polled_keycheck( 1211 struct kbtrans_hardware *hw, 1212 int *key, 1213 enum keystate *state) 1214 { 1215 struct kb8042 *kb8042 = (struct kb8042 *)hw; 1216 int scancode; 1217 boolean_t legit; 1218 boolean_t synthetic_release_needed; 1219 1220 if (kb8042->polled_synthetic_release_pending) { 1221 *key = kb8042->polled_synthetic_release_key; 1222 *state = KEY_RELEASED; 1223 kb8042->polled_synthetic_release_pending = B_FALSE; 1224 #if defined(KD_DEBUG) 1225 if (kb8042_getchar_debug) 1226 prom_printf("synthetic release 0x%x\n", *key); 1227 #endif 1228 (void) kb8042_autorepeat_detect(kb8042, *key, *state); 1229 return (B_TRUE); 1230 } 1231 1232 for (;;) { 1233 if (ddi_get8(kb8042->handle, 1234 kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) { 1235 return (B_FALSE); 1236 } 1237 1238 scancode = ddi_get8(kb8042->handle, 1239 kb8042->addr + I8042_POLL_INPUT_DATA); 1240 1241 #if defined(KD_DEBUG) 1242 if (kb8042_low_level_debug) 1243 prom_printf(" g<%x ", scancode); 1244 #endif 1245 1246 #ifdef KD_DEBUG 1247 kb8042_debug_hotkey(scancode); 1248 if (kb8042_getchar_debug) 1249 prom_printf("polled 0x%x", scancode); 1250 #endif 1251 1252 legit = KeyboardConvertScan(kb8042, scancode, key, state, 1253 &synthetic_release_needed); 1254 if (!legit) { 1255 #ifdef KD_DEBUG 1256 if (kb8042_getchar_debug) 1257 prom_printf(" -> ignored\n"); 1258 #endif 1259 continue; 1260 } 1261 #ifdef KD_DEBUG 1262 if (kb8042_getchar_debug) { 1263 prom_printf(" -> %s %d\n", 1264 *state == KEY_PRESSED ? "pressed" : "released", 1265 *key); 1266 } 1267 #endif 1268 /* 1269 * For the moment at least, we rely on hardware autorepeat 1270 * for polled I/O autorepeat. However, for coordination 1271 * with the interrupt-driven code, maintain the last key 1272 * pressed. 1273 */ 1274 (void) kb8042_autorepeat_detect(kb8042, *key, *state); 1275 1276 /* 1277 * This is a total hack to support two additional keys 1278 * on Korean keyboards. They report only on press, and 1279 * so we synthesize a release. Most likely this will 1280 * never be important to polled I/O, but if I do it 1281 * "right" the first time it _won't_ be an issue. 1282 */ 1283 if (synthetic_release_needed) { 1284 kb8042->polled_synthetic_release_pending = B_TRUE; 1285 kb8042->polled_synthetic_release_key = *key; 1286 } 1287 1288 if (kb8042->simulated_kbd_type == KB_USB) { 1289 *key = keytab_pc2usb[*key]; 1290 } 1291 return (B_TRUE); 1292 } 1293 } 1294 1295 static void 1296 kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled) 1297 { 1298 kb8042->leds.desired = led_state; 1299 1300 if (!polled) 1301 mutex_enter(&kb8042->w_hw_mutex); 1302 1303 if (kb8042->leds.desired != kb8042->leds.commanded) { 1304 kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled); 1305 } 1306 1307 if (!polled) 1308 mutex_exit(&kb8042->w_hw_mutex); 1309 } 1310 1311 static void 1312 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state) 1313 { 1314 struct kb8042 *kb8042 = (struct kb8042 *)hw; 1315 kb8042_setled(kb8042, led_state, B_TRUE); 1316 } 1317 1318 static void 1319 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state) 1320 { 1321 struct kb8042 *kb8042 = (struct kb8042 *)hw; 1322 kb8042_setled(kb8042, led_state, B_FALSE); 1323 } 1324 1325 static void 1326 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled) 1327 { 1328 uint8_t led_cmd[3]; 1329 1330 /* 1331 * KB_SET_LED and KB_ENABLE are special commands for which the nexus 1332 * driver is requested to wait for responses before proceeding. 1333 * KB_SET_LED also provides an option byte for the nexus to send to 1334 * the keyboard after the acknowledgement. 1335 * 1336 * Other commands/data are sent using the single put8 I/O access 1337 * function. 1338 */ 1339 if (byte == KB_SET_LED) { 1340 /* 1341 * Initialize the buffer used with _rep_put8. We 1342 * expect an ACK after the SET_LED command, at which point 1343 * the LED byte should be sent to the keyboard. 1344 */ 1345 led_cmd[0] = KB_SET_LED; 1346 led_cmd[1] = KB_ACK; 1347 led_cmd[2] = kb8042_xlate_leds(kb8042->leds.desired); 1348 if (polled) { 1349 ddi_rep_put8(kb8042->handle, &led_cmd[0], 1350 kb8042->addr + I8042_POLL_CMD_PLUS_PARAM, 3, 0); 1351 } else { 1352 ddi_rep_put8(kb8042->handle, &led_cmd[0], 1353 kb8042->addr + I8042_INT_CMD_PLUS_PARAM, 3, 0); 1354 } 1355 kb8042->leds.commanded = kb8042->leds.desired; 1356 1357 } else if (byte == KB_ENABLE) { 1358 1359 /* 1360 * Initialize the buffer used with _rep_put8. We 1361 * expect an ACK after the KB_ENABLE command. 1362 */ 1363 led_cmd[0] = KB_ENABLE; 1364 led_cmd[1] = KB_ACK; 1365 if (polled) { 1366 ddi_rep_put8(kb8042->handle, &led_cmd[0], 1367 kb8042->addr + I8042_POLL_CMD_PLUS_PARAM, 2, 0); 1368 } else { 1369 ddi_rep_put8(kb8042->handle, &led_cmd[0], 1370 kb8042->addr + I8042_INT_CMD_PLUS_PARAM, 2, 0); 1371 } 1372 } else { 1373 /* All other commands use the "normal" virtual output port */ 1374 if (polled) { 1375 ddi_put8(kb8042->handle, 1376 kb8042->addr + I8042_POLL_OUTPUT_DATA, byte); 1377 } else { 1378 ddi_put8(kb8042->handle, 1379 kb8042->addr + I8042_INT_OUTPUT_DATA, byte); 1380 } 1381 } 1382 1383 #if defined(KD_DEBUG) 1384 if (kb8042_low_level_debug) 1385 prom_printf(" >K:%x ", byte); 1386 #endif 1387 } 1388 1389 /* 1390 * Wait until the keyboard is fully up, maybe. 1391 * We may be the first person to talk to the keyboard, in which case 1392 * it's patiently waiting to say "AA" to us to tell us it's up. 1393 * In theory it sends the AA in 300ms < n < 9s, but it's a pretty 1394 * good bet that we've already spent that long getting to that point, 1395 * so we'll only wait long enough for the communications electronics to 1396 * run. 1397 */ 1398 static void 1399 kb8042_wait_poweron(struct kb8042 *kb8042) 1400 { 1401 int cnt; 1402 int ready; 1403 unsigned char byt; 1404 1405 /* wait for up to 250 ms for a response */ 1406 for (cnt = 0; cnt < 250; cnt++) { 1407 ready = ddi_get8(kb8042->handle, 1408 kb8042->addr + I8042_INT_INPUT_AVAIL); 1409 if (ready != 0) 1410 break; 1411 drv_usecwait(1000); 1412 } 1413 1414 /* 1415 * If there's something pending, read and discard it. If not, 1416 * assume things are OK anyway - maybe somebody else ate it 1417 * already. (On a PC, the BIOS almost certainly did.) 1418 */ 1419 if (ready != 0) { 1420 byt = ddi_get8(kb8042->handle, 1421 kb8042->addr + I8042_INT_INPUT_DATA); 1422 #if defined(KD_DEBUG) 1423 if (kb8042_low_level_debug) 1424 prom_printf(" <K:%x ", byt); 1425 #endif 1426 } 1427 } 1428 1429 static int 1430 kb8042_xlate_leds(int led) 1431 { 1432 int res; 1433 1434 res = 0; 1435 1436 if (led & LED_NUM_LOCK) 1437 res |= LED_NUM; 1438 if (led & LED_SCROLL_LOCK) 1439 res |= LED_SCR; 1440 if (led & LED_CAPS_LOCK) 1441 res |= LED_CAP; 1442 1443 return (res); 1444 } 1445 1446 /*ARGSUSED*/ 1447 static void 1448 kb8042_get_initial_leds( 1449 struct kb8042 *kb8042, 1450 int *initial_leds, 1451 int *initial_led_mask) 1452 { 1453 #if defined(__i386) || defined(__amd64) 1454 extern caddr_t p0_va; 1455 uint8_t bios_kb_flag; 1456 1457 bios_kb_flag = p0_va[BIOS_KB_FLAG]; 1458 1459 *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK; 1460 *initial_leds = 0; 1461 if (bios_kb_flag & BIOS_CAPS_STATE) 1462 *initial_leds |= LED_CAPS_LOCK; 1463 if (bios_kb_flag & BIOS_NUM_STATE) 1464 *initial_leds |= LED_NUM_LOCK; 1465 if (bios_kb_flag & BIOS_SCROLL_STATE) 1466 *initial_leds |= LED_SCROLL_LOCK; 1467 #else 1468 *initial_leds = 0; 1469 *initial_led_mask = 0; 1470 #endif 1471 } 1472 1473 #if defined(KD_DEBUG) 1474 static void 1475 kb8042_debug_hotkey(int scancode) 1476 { 1477 if (!kb8042_enable_debug_hotkey) 1478 return; 1479 1480 switch (scancode) { 1481 case 0x44: /* F10 in Scan Set 1 code. (Set 2 code is 0x09) */ 1482 if (!kb8042_debug) { 1483 prom_printf("\nKeyboard: normal debugging on\n"); 1484 kb8042_debug = B_TRUE; 1485 } 1486 break; 1487 case 0x43: /* F9 in Scan Set 1 code. (Set 2 code is 0x01) */ 1488 if (!kb8042_getchar_debug) { 1489 prom_printf("\nKeyboard: getchar debugging on\n"); 1490 kb8042_getchar_debug = B_TRUE; 1491 } 1492 break; 1493 case 0x42: /* F8 in Scan Set 1 code. (Set 2 code is 0x0a) */ 1494 if (!kb8042_low_level_debug) { 1495 prom_printf("\nKeyboard: low-level debugging on\n"); 1496 kb8042_low_level_debug = B_TRUE; 1497 } 1498 break; 1499 case 0x41: /* F7 in Scan Set 1 code. (Set 2 code is 0x83) */ 1500 if (!kb8042_pressrelease_debug) { 1501 prom_printf( 1502 "\nKeyboard: press/release debugging on\n"); 1503 kb8042_pressrelease_debug = B_TRUE; 1504 } 1505 break; 1506 case 0x3b: /* F1 in Scan Set 1 code. (Set 2 code is 0x05) */ 1507 if (kb8042_debug || 1508 kb8042_getchar_debug || 1509 kb8042_low_level_debug || 1510 kb8042_pressrelease_debug) { 1511 prom_printf("\nKeyboard: all debugging off\n"); 1512 kb8042_debug = B_FALSE; 1513 kb8042_getchar_debug = B_FALSE; 1514 kb8042_low_level_debug = B_FALSE; 1515 kb8042_pressrelease_debug = B_FALSE; 1516 } 1517 break; 1518 } 1519 } 1520 #endif 1521 1522 static boolean_t 1523 kb8042_autorepeat_detect( 1524 struct kb8042 *kb8042, 1525 int key_pos, 1526 enum keystate state) 1527 { 1528 if (state == KEY_RELEASED) { 1529 if (kb8042->kb_old_key_pos == key_pos) 1530 kb8042->kb_old_key_pos = 0; 1531 } else { 1532 if (kb8042->kb_old_key_pos == key_pos) { 1533 return (B_TRUE); 1534 } 1535 kb8042->kb_old_key_pos = key_pos; 1536 } 1537 return (B_FALSE); 1538 } 1539 1540 /* ARGSUSED */ 1541 static void 1542 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd) 1543 { 1544 switch (cmd) { 1545 case KBD_CMD_BELL: 1546 (void) beeper_on(BEEP_TYPE4); 1547 break; 1548 case KBD_CMD_NOBELL: 1549 (void) beeper_off(); 1550 break; 1551 } 1552 } 1553 1554 1555 /* 1556 * This is a pass-thru routine to get a character at poll time. 1557 */ 1558 static int 1559 kb8042_polled_getchar(cons_polledio_arg_t arg) 1560 { 1561 struct kb8042 *kb8042; 1562 1563 kb8042 = (struct kb8042 *)arg; 1564 1565 return (kbtrans_getchar(kb8042->hw_kbtrans)); 1566 } 1567 1568 /* 1569 * This is a pass-thru routine to get a character at poll time. 1570 */ 1571 static int 1572 kb8042_polled_ischar(cons_polledio_arg_t arg) 1573 { 1574 struct kb8042 *kb8042; 1575 1576 kb8042 = (struct kb8042 *)arg; 1577 1578 return (kbtrans_ischar(kb8042->hw_kbtrans)); 1579 } 1580