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 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 23 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 24 /* All Rights Reserved */ 25 26 /* 27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/errno.h> 35 #include <sys/inline.h> 36 #include <sys/termio.h> 37 #include <sys/stropts.h> 38 #include <sys/termios.h> 39 #include <sys/stream.h> 40 #include <sys/strtty.h> 41 #include <sys/strsubr.h> 42 #include <sys/strsun.h> 43 #include <sys/ddi.h> 44 #include <sys/sunddi.h> 45 #include <sys/note.h> 46 #include "sys/consdev.h" 47 #include <sys/kbd.h> 48 #include <sys/kbtrans.h> 49 #include "kb8042.h" 50 51 #include <sys/i8042.h> 52 53 #include "sys/kbio.h" /* for KIOCSLAYOUT */ 54 #include "sys/stat.h" 55 #include "sys/reboot.h" 56 #include <sys/promif.h> 57 #include <sys/beep.h> 58 59 /* 60 * For any keyboard, there is a unique code describing the position 61 * of the key on a keyboard. We refer to the code as "station number". 62 * The following table is used to map the station numbers from ps2 63 * AT/XT keyboards to that of a USB one. 64 */ 65 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = { 66 /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35, 67 /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42, 68 /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24, 69 /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4, 70 /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15, 71 /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27, 72 /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56, 73 /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231, 74 /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0, 75 /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80, 76 /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0, 77 /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84, 78 /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91, 79 /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0, 80 /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65, 81 /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0, 82 /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0, 83 /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0, 84 /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0, 85 /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0, 86 /* 160 */ 0, 0, 0, 0, 0, 0, 0, 0, 87 /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0, 88 /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0, 89 /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0, 90 /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0, 91 /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0, 92 /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0, 93 /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0, 94 /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0, 95 /* 232 */ 0, 0, 0, 0, 0, 0, 0, 0, 96 /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0, 97 /* 248 */ 0, 0, 0, 0 98 }; 99 100 /* 101 * DEBUG (or KD_DEBUG for just this module) turns on a flag called 102 * kb8042_enable_debug_hotkey. If kb8042_enable_debug_hotkey is true, 103 * then the following hotkeys are enabled: 104 * F10 - turn on debugging "normal" translations 105 * F9 - turn on debugging "getchar" translations 106 * F8 - turn on "low level" debugging 107 * F7 - turn on terse press/release debugging 108 * F1 - turn off all debugging 109 * The default value for kb8042_enable_debug_hotkey is false, disabling 110 * these hotkeys. 111 */ 112 113 #if defined(DEBUG) || defined(lint) 114 #define KD_DEBUG 115 #endif 116 117 #ifdef KD_DEBUG 118 boolean_t kb8042_enable_debug_hotkey = B_FALSE; 119 boolean_t kb8042_debug = B_FALSE; 120 boolean_t kb8042_getchar_debug = B_FALSE; 121 boolean_t kb8042_low_level_debug = B_FALSE; 122 boolean_t kb8042_pressrelease_debug = B_FALSE; 123 static void kb8042_debug_hotkey(int scancode); 124 #endif 125 126 enum state_return { STATE_NORMAL, STATE_INTERNAL }; 127 128 static void kb8042_init(struct kb8042 *kb8042); 129 static uint_t kb8042_intr(caddr_t arg); 130 static void kb8042_wait_poweron(struct kb8042 *kb8042); 131 static void kb8042_start_state_machine(struct kb8042 *, boolean_t); 132 static enum state_return kb8042_state_machine(struct kb8042 *, int, boolean_t); 133 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t); 134 static int kb8042_xlate_leds(int); 135 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state); 136 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state); 137 static boolean_t kb8042_polled_keycheck( 138 struct kbtrans_hardware *hw, int *key, 139 enum keystate *state); 140 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *); 141 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos, 142 enum keystate state); 143 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd); 144 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *); 145 static void kb8042_iocdatamsg(queue_t *, mblk_t *); 146 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate); 147 static int kb8042_polled_ischar(struct cons_polledio_arg *arg); 148 static int kb8042_polled_getchar(struct cons_polledio_arg *arg); 149 150 static struct kbtrans_callbacks kb8042_callbacks = { 151 kb8042_streams_setled, 152 kb8042_polled_setled, 153 kb8042_polled_keycheck, 154 }; 155 156 extern struct keyboard keyindex_pc; 157 158 #define DRIVER_NAME(dip) ddi_driver_name(dip) 159 160 static char module_name[] = "kb8042"; 161 162 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, 163 cred_t *credp); 164 static int kb8042_close(queue_t *qp, int flag, cred_t *credp); 165 static int kb8042_wsrv(); 166 167 struct module_info kb8042_sinfo = { 168 42, /* Module ID */ 169 module_name, 170 0, 32, /* Minimum and maximum packet sizes */ 171 256, 128 /* High and low water marks */ 172 }; 173 174 static struct qinit kb8042_rinit = { 175 NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL 176 }; 177 178 static struct qinit kb8042_winit = { 179 putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL 180 }; 181 182 struct streamtab 183 kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL }; 184 185 struct kb8042 Kdws = {0}; 186 static dev_info_t *kb8042_dip; 187 188 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 189 void **result); 190 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t); 191 192 static struct cb_ops cb_kb8042_ops = { 193 nulldev, /* cb_open */ 194 nulldev, /* cb_close */ 195 nodev, /* cb_strategy */ 196 nodev, /* cb_print */ 197 nodev, /* cb_dump */ 198 nodev, /* cb_read */ 199 nodev, /* cb_write */ 200 nodev, /* cb_ioctl */ 201 nodev, /* cb_devmap */ 202 nodev, /* cb_mmap */ 203 nodev, /* cb_segmap */ 204 nochpoll, /* cb_chpoll */ 205 ddi_prop_op, /* cb_prop_op */ 206 &kb8042_str_info, /* cb_stream */ 207 D_MP 208 }; 209 210 struct dev_ops kb8042_ops = { 211 DEVO_REV, /* devo_rev */ 212 0, /* devo_refcnt */ 213 kb8042_getinfo, /* devo_getinfo */ 214 nulldev, /* devo_identify */ 215 nulldev, /* devo_probe */ 216 kb8042_attach, /* devo_attach */ 217 nodev, /* devo_detach */ 218 nodev, /* devo_reset */ 219 &cb_kb8042_ops, /* devo_cb_ops */ 220 (struct bus_ops *)NULL /* devo_bus_ops */ 221 }; 222 223 224 /* 225 * This is the loadable module wrapper. 226 */ 227 #include <sys/modctl.h> 228 229 /* 230 * Module linkage information for the kernel. 231 */ 232 static struct modldrv modldrv = { 233 &mod_driverops, /* Type of module. This one is a driver */ 234 "PS/2 Keyboard %I%, %E%", 235 &kb8042_ops, /* driver ops */ 236 }; 237 238 static struct modlinkage modlinkage = { 239 MODREV_1, 240 (void *) &modldrv, 241 NULL 242 }; 243 244 int 245 _init(void) 246 { 247 int rv; 248 249 rv = mod_install(&modlinkage); 250 return (rv); 251 } 252 253 254 int 255 _fini(void) 256 { 257 return (mod_remove(&modlinkage)); 258 } 259 260 int 261 _info(struct modinfo *modinfop) 262 { 263 return (mod_info(&modlinkage, modinfop)); 264 } 265 266 static int 267 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 268 { 269 int rc; 270 271 struct kb8042 *kb8042; 272 static ddi_device_acc_attr_t attr = { 273 DDI_DEVICE_ATTR_V0, 274 DDI_NEVERSWAP_ACC, 275 DDI_STRICTORDER_ACC, 276 }; 277 278 if (cmd != DDI_ATTACH) 279 return (DDI_FAILURE); 280 281 kb8042 = &Kdws; 282 283 KeyboardConvertScan_init(kb8042); 284 285 kb8042->debugger.mod1 = 58; /* Left Ctrl */ 286 kb8042->debugger.mod2 = 60; /* Left Alt */ 287 kb8042->debugger.trigger = 33; /* D */ 288 kb8042->debugger.mod1_down = B_FALSE; 289 kb8042->debugger.mod2_down = B_FALSE; 290 kb8042->debugger.enabled = B_FALSE; 291 292 kb8042->polled_synthetic_release_pending = B_FALSE; 293 294 if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0, 295 DDI_NT_KEYBOARD, 0) == DDI_FAILURE) { 296 ddi_remove_minor_node(devi, NULL); 297 return (-1); 298 } 299 kb8042_dip = devi; 300 301 rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr, 302 (offset_t)0, (offset_t)0, &attr, &kb8042->handle); 303 if (rc != DDI_SUCCESS) { 304 #if defined(KD_DEBUG) 305 cmn_err(CE_WARN, "kb8042_attach: can't map registers"); 306 #endif 307 return (rc); 308 } 309 310 if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) != 311 DDI_SUCCESS) { 312 cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie"); 313 return (DDI_FAILURE); 314 } 315 316 mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock); 317 318 kb8042_init(kb8042); 319 320 /* 321 * Turn on interrupts... 322 */ 323 if (ddi_add_intr(devi, 0, 324 &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL, 325 kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) { 326 cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt"); 327 return (DDI_FAILURE); 328 } 329 330 ddi_report_dev(devi); 331 #ifdef KD_DEBUG 332 cmn_err(CE_CONT, "?%s #%d: version %s\n", 333 DRIVER_NAME(devi), ddi_get_instance(devi), "%I% (%E%)"); 334 #endif 335 return (DDI_SUCCESS); 336 } 337 338 /*ARGSUSED*/ 339 static int 340 kb8042_getinfo( 341 dev_info_t *dip, 342 ddi_info_cmd_t infocmd, 343 void *arg, 344 void **result) 345 { 346 register int error; 347 348 switch (infocmd) { 349 case DDI_INFO_DEVT2DEVINFO: 350 if (kb8042_dip == NULL) { 351 error = DDI_FAILURE; 352 } else { 353 *result = (void *) kb8042_dip; 354 error = DDI_SUCCESS; 355 } 356 break; 357 case DDI_INFO_DEVT2INSTANCE: 358 *result = (void *)0; 359 error = DDI_SUCCESS; 360 break; 361 default: 362 error = DDI_FAILURE; 363 break; 364 } 365 return (error); 366 } 367 368 static void 369 kb8042_init(struct kb8042 *kb8042) 370 { 371 if (kb8042->w_init) 372 return; 373 374 kb8042->w_kblayout = 0; /* Default to US */ 375 376 kb8042->w_qp = (queue_t *)NULL; 377 378 kb8042_wait_poweron(kb8042); 379 380 kb8042->kb_old_key_pos = 0; 381 382 kb8042->simulated_kbd_type = KB_PC; 383 384 /* Set up the command state machine and start it running. */ 385 kb8042->leds.commanded = -1; /* Unknown initial state */ 386 kb8042->leds.desired = -1; /* Unknown initial state */ 387 kb8042->command_state = KB_COMMAND_STATE_WAIT; 388 kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE); 389 390 kb8042->w_init++; 391 392 (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */ 393 } 394 395 /*ARGSUSED2*/ 396 static int 397 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp) 398 { 399 struct kb8042 *kb8042; 400 int err; 401 int initial_leds; 402 int initial_led_mask; 403 404 kb8042 = &Kdws; 405 406 kb8042->w_dev = *devp; 407 408 if (qp->q_ptr) { 409 return (0); 410 } 411 qp->q_ptr = (caddr_t)kb8042; 412 WR(qp)->q_ptr = qp->q_ptr; 413 if (!kb8042->w_qp) 414 kb8042->w_qp = qp; 415 416 kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask); 417 err = kbtrans_streams_init(qp, sflag, credp, 418 (struct kbtrans_hardware *)kb8042, &kb8042_callbacks, 419 &kb8042->hw_kbtrans, 420 initial_leds, initial_led_mask); 421 if (err != 0) 422 return (err); 423 424 kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc); 425 426 kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1; 427 kb8042->polledio.cons_polledio_argument = 428 (struct cons_polledio_arg *)kb8042; 429 kb8042->polledio.cons_polledio_putchar = NULL; 430 kb8042->polledio.cons_polledio_getchar = 431 (int (*)(struct cons_polledio_arg *))kb8042_polled_getchar; 432 kb8042->polledio.cons_polledio_ischar = 433 (boolean_t (*)(struct cons_polledio_arg *))kb8042_polled_ischar; 434 kb8042->polledio.cons_polledio_enter = NULL; 435 kb8042->polledio.cons_polledio_exit = NULL; 436 kb8042->polledio.cons_polledio_setled = 437 (void (*)(struct cons_polledio_arg *, int))kb8042_polled_setled; 438 kb8042->polledio.cons_polledio_keycheck = 439 (boolean_t (*)(struct cons_polledio_arg *, int *, 440 enum keystate *))kb8042_polled_keycheck; 441 442 qprocson(qp); 443 444 kbtrans_streams_enable(kb8042->hw_kbtrans); 445 446 return (0); 447 } 448 449 /*ARGSUSED1*/ 450 static int 451 kb8042_close(queue_t *qp, int flag, cred_t *credp) 452 { 453 struct kb8042 *kb8042; 454 455 kb8042 = (struct kb8042 *)qp->q_ptr; 456 457 kb8042->w_qp = (queue_t *)NULL; 458 qprocsoff(qp); 459 460 return (0); 461 } 462 463 static int 464 kb8042_wsrv(queue_t *qp) 465 { 466 struct kb8042 *kb8042; 467 468 mblk_t *mp; 469 470 kb8042 = (struct kb8042 *)qp->q_ptr; 471 472 while ((mp = getq(qp))) { 473 switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) { 474 case KBTRANS_MESSAGE_HANDLED: 475 continue; 476 case KBTRANS_MESSAGE_NOT_HANDLED: 477 break; 478 } 479 switch (mp->b_datap->db_type) { 480 case M_IOCTL: 481 kb8042_ioctlmsg(kb8042, qp, mp); 482 continue; 483 case M_IOCDATA: 484 kb8042_iocdatamsg(qp, mp); 485 continue; 486 case M_DELAY: 487 case M_STARTI: 488 case M_STOPI: 489 case M_READ: /* ignore, no buffered data */ 490 freemsg(mp); 491 continue; 492 case M_FLUSH: 493 *mp->b_rptr &= ~FLUSHW; 494 if (*mp->b_rptr & FLUSHR) 495 qreply(qp, mp); 496 else 497 freemsg(mp); 498 continue; 499 default: 500 cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x", 501 mp->b_datap->db_type); 502 freemsg(mp); 503 continue; 504 } 505 } 506 return (0); 507 } 508 509 static void 510 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state) 511 { 512 struct kb8042 *kb8042 = (struct kb8042 *)hw; 513 514 kb8042->leds.desired = led_state; 515 mutex_enter(&kb8042->w_hw_mutex); 516 517 kb8042_start_state_machine(kb8042, B_FALSE); 518 519 mutex_exit(&kb8042->w_hw_mutex); 520 } 521 522 static void 523 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp) 524 { 525 struct iocblk *iocp; 526 mblk_t *datap; 527 int error; 528 int tmp; 529 530 iocp = (struct iocblk *)mp->b_rptr; 531 532 switch (iocp->ioc_cmd) { 533 534 case CONSOPENPOLLEDIO: 535 error = miocpullup(mp, sizeof (struct cons_polledio *)); 536 if (error != 0) { 537 miocnak(qp, mp, 0, error); 538 return; 539 } 540 541 /* 542 * We are given an appropriate-sized data block, 543 * and return a pointer to our structure in it. 544 */ 545 *(struct cons_polledio **)mp->b_cont->b_rptr = 546 &kb8042->polledio; 547 mp->b_datap->db_type = M_IOCACK; 548 iocp->ioc_error = 0; 549 qreply(qp, mp); 550 break; 551 552 case CONSCLOSEPOLLEDIO: 553 miocack(qp, mp, 0, 0); 554 break; 555 556 case CONSSETABORTENABLE: 557 if (iocp->ioc_count != TRANSPARENT) { 558 miocnak(qp, mp, 0, EINVAL); 559 return; 560 } 561 562 kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr; 563 miocack(qp, mp, 0, 0); 564 break; 565 566 /* 567 * Valid only in TR_UNTRANS_MODE mode. 568 */ 569 case CONSSETKBDTYPE: 570 error = miocpullup(mp, sizeof (int)); 571 if (error != 0) { 572 miocnak(qp, mp, 0, error); 573 return; 574 } 575 tmp = *(int *)mp->b_cont->b_rptr; 576 if (tmp != KB_PC && tmp != KB_USB) { 577 miocnak(qp, mp, 0, EINVAL); 578 break; 579 } 580 kb8042->simulated_kbd_type = tmp; 581 miocack(qp, mp, 0, 0); 582 break; 583 584 case KIOCLAYOUT: 585 if (kb8042->w_kblayout == -1) { 586 miocnak(qp, mp, 0, EINVAL); 587 return; 588 } 589 590 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) { 591 miocnak(qp, mp, 0, ENOMEM); 592 return; 593 } 594 595 if (kb8042->simulated_kbd_type == KB_USB) 596 *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT; 597 else 598 *(int *)datap->b_wptr = kb8042->w_kblayout; 599 600 datap->b_wptr += sizeof (int); 601 if (mp->b_cont) 602 freemsg(mp->b_cont); 603 mp->b_cont = datap; 604 iocp->ioc_count = sizeof (int); 605 mp->b_datap->db_type = M_IOCACK; 606 iocp->ioc_error = 0; 607 qreply(qp, mp); 608 break; 609 610 case KIOCSLAYOUT: 611 if (iocp->ioc_count != TRANSPARENT) { 612 miocnak(qp, mp, 0, EINVAL); 613 return; 614 } 615 616 kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr; 617 miocack(qp, mp, 0, 0); 618 break; 619 620 case KIOCCMD: 621 error = miocpullup(mp, sizeof (int)); 622 if (error != 0) { 623 miocnak(qp, mp, 0, error); 624 return; 625 } 626 627 kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr); 628 miocack(qp, mp, 0, 0); 629 break; 630 631 default: 632 #ifdef DEBUG1 633 cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd); 634 #endif 635 miocnak(qp, mp, 0, EINVAL); 636 return; 637 } 638 } 639 640 /* 641 * Process a byte received from the keyboard 642 */ 643 static void 644 kb8042_received_byte( 645 struct kb8042 *kb8042, 646 int scancode) /* raw scan code */ 647 { 648 boolean_t legit; /* is this a legit key pos'n? */ 649 int key_pos = -1; 650 enum keystate state; 651 boolean_t synthetic_release_needed; 652 653 #ifdef KD_DEBUG 654 kb8042_debug_hotkey(scancode); 655 #endif 656 657 if (!kb8042->w_init) /* can't do anything anyway */ 658 return; 659 660 legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state, 661 &synthetic_release_needed); 662 663 if (legit == 0) { 664 /* Eaten by translation */ 665 #ifdef KD_DEBUG 666 if (kb8042_debug) 667 prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode); 668 #endif 669 return; 670 } 671 672 #ifdef KD_DEBUG 673 if (kb8042_debug) { 674 prom_printf("kb8042_intr: 0x%x -> %s %d", 675 scancode, 676 state == KEY_RELEASED ? "released" : "pressed", 677 key_pos); 678 } 679 #endif 680 681 /* 682 * Don't know if we want this permanently, but it seems interesting 683 * for the moment. 684 */ 685 if (key_pos == kb8042->debugger.mod1) { 686 #ifdef KD_DEBUG 687 if (kb8042_debug) 688 prom_printf(" -> debug mod1"); 689 #endif 690 kb8042->debugger.mod1_down = (state == KEY_PRESSED); 691 } 692 if (key_pos == kb8042->debugger.mod2) { 693 #ifdef KD_DEBUG 694 if (kb8042_debug) 695 prom_printf(" -> debug mod2"); 696 #endif 697 kb8042->debugger.mod2_down = (state == KEY_PRESSED); 698 } 699 if (kb8042->debugger.enabled && 700 key_pos == kb8042->debugger.trigger && 701 kb8042->debugger.mod1_down && 702 kb8042->debugger.mod2_down) { 703 #ifdef KD_DEBUG 704 if (kb8042_debug) 705 prom_printf(" -> debugger\n"); 706 #endif 707 /* 708 * Require new presses of the modifiers. 709 */ 710 kb8042->debugger.mod1_down = B_FALSE; 711 kb8042->debugger.mod2_down = B_FALSE; 712 abort_sequence_enter(NULL); 713 return; 714 } 715 716 /* 717 * If there's no queue above us - as can happen if we've been 718 * attached but not opened - drop the keystroke. 719 * Note that we do this here instead of above so that 720 * Ctrl-Alt-D still works. 721 */ 722 if (kb8042->w_qp == NULL) { 723 #ifdef KD_DEBUG 724 if (kb8042_debug) 725 prom_printf(" -> nobody home\n"); 726 #endif 727 return; 728 } 729 730 /* 731 * This is to filter out auto repeat since it can't be 732 * turned off at the hardware. (Yeah, yeah, PS/2 keyboards 733 * can. Don't know whether they've taken over the world. 734 * Don't think so.) 735 */ 736 if (kb8042_autorepeat_detect(kb8042, key_pos, state)) { 737 #ifdef KD_DEBUG 738 if (kb8042_debug) 739 prom_printf(" -> autorepeat ignored\n"); 740 #endif 741 return; 742 } 743 744 #ifdef KD_DEBUG 745 if (kb8042_debug) 746 prom_printf(" -> OK\n"); 747 #endif 748 749 #if defined(KD_DEBUG) 750 if (kb8042_pressrelease_debug) { 751 prom_printf(" %s%d ", 752 state == KEY_PRESSED ? "+" : "-", 753 key_pos); 754 } 755 #endif 756 757 kb8042_process_key(kb8042, key_pos, state); 758 759 /* 760 * This is a total hack. For some stupid reason, the two additional 761 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press 762 * only. We synthesize a release immediately. 763 */ 764 if (synthetic_release_needed) { 765 #if defined(KD_DEBUG) 766 if (kb8042_debug) 767 prom_printf("synthetic release %d\n", key_pos); 768 if (kb8042_pressrelease_debug) 769 prom_printf(" -%d(s) ", key_pos); 770 #endif 771 (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED); 772 kb8042_process_key(kb8042, key_pos, state); 773 } 774 } 775 776 777 static void 778 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos, 779 enum keystate state) 780 { 781 kbtrans_key_t key; 782 783 ASSERT(key_pos >= 0 && key_pos <= 255); 784 if (kb8042->simulated_kbd_type == KB_PC) { 785 kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state); 786 } else if (kb8042->simulated_kbd_type == KB_USB) { 787 key = keytab_pc2usb[key_pos]; 788 if (key != 0) { 789 kbtrans_streams_key(kb8042->hw_kbtrans, key, state); 790 } 791 } 792 } 793 794 /* 795 * Called from interrupt handler when keyboard interrupt occurs. 796 */ 797 static uint_t 798 kb8042_intr(caddr_t arg) 799 { 800 uchar_t scancode; /* raw scan code */ 801 int rc; 802 struct kb8042 *kb8042 = (struct kb8042 *)arg; 803 804 rc = DDI_INTR_UNCLAIMED; 805 806 /* don't care if drv_setparm succeeds */ 807 (void) drv_setparm(SYSRINT, 1); 808 809 while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL) 810 != 0) { 811 rc = DDI_INTR_CLAIMED; 812 813 scancode = ddi_get8(kb8042->handle, 814 kb8042->addr + I8042_INT_INPUT_DATA); 815 816 #if defined(KD_DEBUG) 817 if (kb8042_low_level_debug) 818 prom_printf(" <K:%x ", scancode); 819 #endif 820 821 mutex_enter(&kb8042->w_hw_mutex); 822 823 if (kb8042_state_machine(kb8042, scancode, B_FALSE) != 824 STATE_NORMAL) { 825 mutex_exit(&kb8042->w_hw_mutex); 826 continue; 827 } 828 829 830 mutex_exit(&kb8042->w_hw_mutex); 831 832 kb8042_received_byte(kb8042, scancode); 833 } 834 835 return (rc); 836 } 837 838 static void 839 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp) 840 { 841 struct copyresp *csp; 842 843 csp = (struct copyresp *)mp->b_rptr; 844 if (csp->cp_rval) { 845 freemsg(mp); 846 return; 847 } 848 849 switch (csp->cp_cmd) { 850 default: 851 miocack(qp, mp, 0, 0); 852 break; 853 } 854 } 855 856 static boolean_t 857 kb8042_polled_keycheck( 858 struct kbtrans_hardware *hw, 859 int *key, 860 enum keystate *state) 861 { 862 struct kb8042 *kb8042 = (struct kb8042 *)hw; 863 int scancode; 864 boolean_t legit; 865 boolean_t synthetic_release_needed; 866 867 if (kb8042->polled_synthetic_release_pending) { 868 *key = kb8042->polled_synthetic_release_key; 869 *state = KEY_RELEASED; 870 kb8042->polled_synthetic_release_pending = B_FALSE; 871 #if defined(KD_DEBUG) 872 if (kb8042_getchar_debug) 873 prom_printf("synthetic release 0x%x\n", *key); 874 #endif 875 (void) kb8042_autorepeat_detect(kb8042, *key, *state); 876 return (B_TRUE); 877 } 878 879 for (;;) { 880 if (ddi_get8(kb8042->handle, 881 kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) { 882 return (B_FALSE); 883 } 884 885 scancode = ddi_get8(kb8042->handle, 886 kb8042->addr + I8042_POLL_INPUT_DATA); 887 888 #if defined(KD_DEBUG) 889 if (kb8042_low_level_debug) 890 prom_printf(" g<%x ", scancode); 891 #endif 892 893 if (kb8042_state_machine(kb8042, scancode, B_FALSE) != 894 STATE_NORMAL) { 895 continue; 896 } 897 898 #ifdef KD_DEBUG 899 kb8042_debug_hotkey(scancode); 900 if (kb8042_getchar_debug) 901 prom_printf("polled 0x%x", scancode); 902 #endif 903 904 legit = KeyboardConvertScan(kb8042, scancode, key, state, 905 &synthetic_release_needed); 906 if (!legit) { 907 #ifdef KD_DEBUG 908 if (kb8042_getchar_debug) 909 prom_printf(" -> ignored\n"); 910 #endif 911 continue; 912 } 913 #ifdef KD_DEBUG 914 if (kb8042_getchar_debug) { 915 prom_printf(" -> %s %d\n", 916 *state == KEY_PRESSED ? "pressed" : "released", 917 *key); 918 } 919 #endif 920 /* 921 * For the moment at least, we rely on hardware autorepeat 922 * for polled I/O autorepeat. However, for coordination 923 * with the interrupt-driven code, maintain the last key 924 * pressed. 925 */ 926 (void) kb8042_autorepeat_detect(kb8042, *key, *state); 927 928 /* 929 * This is a total hack to support two additional keys 930 * on Korean keyboards. They report only on press, and 931 * so we synthesize a release. Most likely this will 932 * never be important to polled I/O, but if I do it 933 * "right" the first time it _won't_ be an issue. 934 */ 935 if (synthetic_release_needed) { 936 kb8042->polled_synthetic_release_pending = B_TRUE; 937 kb8042->polled_synthetic_release_key = *key; 938 } 939 940 if (kb8042->simulated_kbd_type == KB_USB) { 941 *key = keytab_pc2usb[*key]; 942 } 943 return (B_TRUE); 944 } 945 } 946 947 static void 948 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state) 949 { 950 struct kb8042 *kb8042 = (struct kb8042 *)hw; 951 952 kb8042->leds.desired = led_state; 953 954 kb8042_start_state_machine(kb8042, B_TRUE); 955 } 956 957 static void 958 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled) 959 { 960 if (polled) { 961 ddi_put8(kb8042->handle, 962 kb8042->addr + I8042_POLL_OUTPUT_DATA, byte); 963 } else { 964 ddi_put8(kb8042->handle, 965 kb8042->addr + I8042_INT_OUTPUT_DATA, byte); 966 } 967 968 #if defined(KD_DEBUG) 969 if (kb8042_low_level_debug) 970 prom_printf(" >K:%x ", byte); 971 #endif 972 } 973 974 /* 975 * Wait until the keyboard is fully up, maybe. 976 * We may be the first person to talk to the keyboard, in which case 977 * it's patiently waiting to say "AA" to us to tell us it's up. 978 * In theory it sends the AA in 300ms < n < 9s, but it's a pretty 979 * good bet that we've already spent that long getting to that point, 980 * so we'll only wait long enough for the communications electronics to 981 * run. 982 */ 983 static void 984 kb8042_wait_poweron(struct kb8042 *kb8042) 985 { 986 int cnt; 987 int ready; 988 unsigned char byt; 989 990 /* wait for up to about a quarter-second for response */ 991 for (cnt = 0; cnt < 250; cnt++) { 992 ready = ddi_get8(kb8042->handle, 993 kb8042->addr + I8042_INT_INPUT_AVAIL); 994 if (ready != 0) 995 break; 996 drv_usecwait(1000); 997 } 998 999 /* 1000 * If there's something pending, read and discard it. If not, 1001 * assume things are OK anyway - maybe somebody else ate it 1002 * already. (On a PC, the BIOS almost certainly did.) 1003 */ 1004 if (ready != 0) { 1005 byt = ddi_get8(kb8042->handle, 1006 kb8042->addr + I8042_INT_INPUT_DATA); 1007 #if defined(KD_DEBUG) 1008 if (kb8042_low_level_debug) 1009 prom_printf(" <K:%x ", byt); 1010 #endif 1011 } 1012 } 1013 1014 static void 1015 kb8042_start_state_machine(struct kb8042 *kb8042, boolean_t polled) 1016 { 1017 if (kb8042->command_state == KB_COMMAND_STATE_IDLE) { 1018 if (kb8042->leds.desired != kb8042->leds.commanded) { 1019 kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled); 1020 kb8042->command_state = KB_COMMAND_STATE_LED; 1021 } 1022 } 1023 } 1024 1025 enum state_return 1026 kb8042_state_machine(struct kb8042 *kb8042, int scancode, boolean_t polled) 1027 { 1028 switch (kb8042->command_state) { 1029 case KB_COMMAND_STATE_IDLE: 1030 break; 1031 1032 case KB_COMMAND_STATE_LED: 1033 if (scancode == KB_ACK) { 1034 kb8042_send_to_keyboard(kb8042, 1035 kb8042_xlate_leds(kb8042->leds.desired), 1036 polled); 1037 kb8042->leds.commanded = kb8042->leds.desired; 1038 kb8042->command_state = KB_COMMAND_STATE_WAIT; 1039 return (STATE_INTERNAL); 1040 } 1041 /* Drop normal scan codes through. */ 1042 break; 1043 1044 case KB_COMMAND_STATE_WAIT: 1045 if (scancode == KB_ACK) { 1046 kb8042->command_state = KB_COMMAND_STATE_IDLE; 1047 kb8042_start_state_machine(kb8042, polled); 1048 return (STATE_INTERNAL); 1049 } 1050 /* Drop normal scan codes through. */ 1051 break; 1052 } 1053 return (STATE_NORMAL); 1054 } 1055 1056 static int 1057 kb8042_xlate_leds(int led) 1058 { 1059 int res; 1060 1061 res = 0; 1062 1063 if (led & LED_NUM_LOCK) 1064 res |= LED_NUM; 1065 if (led & LED_SCROLL_LOCK) 1066 res |= LED_SCR; 1067 if (led & LED_CAPS_LOCK) 1068 res |= LED_CAP; 1069 1070 return (res); 1071 } 1072 1073 /*ARGSUSED*/ 1074 static void 1075 kb8042_get_initial_leds( 1076 struct kb8042 *kb8042, 1077 int *initial_leds, 1078 int *initial_led_mask) 1079 { 1080 #if defined(i86pc) 1081 extern caddr_t p0_va; 1082 uint8_t bios_kb_flag; 1083 1084 bios_kb_flag = p0_va[BIOS_KB_FLAG]; 1085 1086 *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK; 1087 *initial_leds = 0; 1088 if (bios_kb_flag & BIOS_CAPS_STATE) 1089 *initial_leds |= LED_CAPS_LOCK; 1090 if (bios_kb_flag & BIOS_NUM_STATE) 1091 *initial_leds |= LED_NUM_LOCK; 1092 if (bios_kb_flag & BIOS_SCROLL_STATE) 1093 *initial_leds |= LED_SCROLL_LOCK; 1094 #else 1095 *initial_leds = 0; 1096 *initial_led_mask = 0; 1097 #endif 1098 } 1099 1100 #if defined(KD_DEBUG) 1101 static void 1102 kb8042_debug_hotkey(int scancode) 1103 { 1104 if (!kb8042_enable_debug_hotkey) 1105 return; 1106 1107 switch (scancode) { 1108 case 0x44: /* F10 in Scan Set 1 code. (Set 2 code is 0x09) */ 1109 if (!kb8042_debug) { 1110 prom_printf("\nKeyboard: normal debugging on\n"); 1111 kb8042_debug = B_TRUE; 1112 } 1113 break; 1114 case 0x43: /* F9 in Scan Set 1 code. (Set 2 code is 0x01) */ 1115 if (!kb8042_getchar_debug) { 1116 prom_printf("\nKeyboard: getchar debugging on\n"); 1117 kb8042_getchar_debug = B_TRUE; 1118 } 1119 break; 1120 case 0x42: /* F8 in Scan Set 1 code. (Set 2 code is 0x0a) */ 1121 if (!kb8042_low_level_debug) { 1122 prom_printf("\nKeyboard: low-level debugging on\n"); 1123 kb8042_low_level_debug = B_TRUE; 1124 } 1125 break; 1126 case 0x41: /* F7 in Scan Set 1 code. (Set 2 code is 0x83) */ 1127 if (!kb8042_pressrelease_debug) { 1128 prom_printf( 1129 "\nKeyboard: press/release debugging on\n"); 1130 kb8042_pressrelease_debug = B_TRUE; 1131 } 1132 break; 1133 case 0x3b: /* F1 in Scan Set 1 code. (Set 2 code is 0x05) */ 1134 if (kb8042_debug || 1135 kb8042_getchar_debug || 1136 kb8042_low_level_debug || 1137 kb8042_pressrelease_debug) { 1138 prom_printf("\nKeyboard: all debugging off\n"); 1139 kb8042_debug = B_FALSE; 1140 kb8042_getchar_debug = B_FALSE; 1141 kb8042_low_level_debug = B_FALSE; 1142 kb8042_pressrelease_debug = B_FALSE; 1143 } 1144 break; 1145 } 1146 } 1147 #endif 1148 1149 static boolean_t 1150 kb8042_autorepeat_detect( 1151 struct kb8042 *kb8042, 1152 int key_pos, 1153 enum keystate state) 1154 { 1155 if (state == KEY_RELEASED) { 1156 if (kb8042->kb_old_key_pos == key_pos) 1157 kb8042->kb_old_key_pos = 0; 1158 } else { 1159 if (kb8042->kb_old_key_pos == key_pos) { 1160 return (B_TRUE); 1161 } 1162 kb8042->kb_old_key_pos = key_pos; 1163 } 1164 return (B_FALSE); 1165 } 1166 1167 /* ARGSUSED */ 1168 static void 1169 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd) 1170 { 1171 switch (cmd) { 1172 case KBD_CMD_BELL: 1173 beeper_on(BEEP_TYPE4); 1174 break; 1175 case KBD_CMD_NOBELL: 1176 beeper_off(); 1177 break; 1178 } 1179 } 1180 1181 1182 /* 1183 * This is a pass-thru routine to get a character at poll time. 1184 */ 1185 static int 1186 kb8042_polled_getchar(struct cons_polledio_arg *arg) 1187 { 1188 struct kb8042 *kb8042; 1189 1190 kb8042 = (struct kb8042 *)arg; 1191 1192 return (kbtrans_getchar(kb8042->hw_kbtrans)); 1193 } 1194 1195 /* 1196 * This is a pass-thru routine to get a character at poll time. 1197 */ 1198 static int 1199 kb8042_polled_ischar(struct cons_polledio_arg *arg) 1200 { 1201 struct kb8042 *kb8042; 1202 1203 kb8042 = (struct kb8042 *)arg; 1204 1205 return (kbtrans_ischar(kb8042->hw_kbtrans)); 1206 } 1207