1 /*- 2 * (MPSAFE) 3 * 4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer as 12 * the first lines of this file unmodified. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: src/sys/dev/kbd/atkbd.c,v 1.25.2.4 2002/04/08 19:21:38 asmodai Exp $ 29 */ 30 /* 31 * NOTE: All locks are handled by the kbd wrappers. 32 */ 33 34 #include "opt_kbd.h" 35 #include "opt_atkbd.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/bus.h> 41 #include <sys/proc.h> 42 #include <sys/malloc.h> 43 #include <sys/thread2.h> 44 45 #ifdef __i386__ 46 #include <machine/md_var.h> 47 #include <machine/psl.h> 48 #include <machine/vm86.h> 49 #include <machine/pc/bios.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 #endif /* __i386__ */ 54 55 #include <sys/kbio.h> 56 #include "kbdreg.h" 57 #include "atkbdreg.h" 58 #include "atkbdcreg.h" 59 60 #include <bus/isa/isareg.h> 61 62 static timeout_t atkbd_timeout; 63 64 typedef struct atkbd_state { 65 KBDC kbdc; /* keyboard controller */ 66 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 67 int ks_flags; /* flags */ 68 #define COMPOSE (1 << 0) 69 int ks_polling; 70 int ks_state; /* shift/lock key state */ 71 int ks_accents; /* accent key index (> 0) */ 72 u_int ks_composed_char; /* composed char code (> 0) */ 73 u_char ks_prefix; /* AT scan code prefix */ 74 struct callout ks_timer; 75 } atkbd_state_t; 76 77 static int atkbd_reset(KBDC kbdc, int flags, int c); 78 79 #define HAS_QUIRK(p, q) (((atkbdc_softc_t *)(p))->quirks & q) 80 #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED) 81 82 #define DEFAULT_DELAY 0x1 /* 500ms */ 83 #define DEFAULT_RATE 0x10 /* 14Hz */ 84 85 86 #if 0 87 static int atkbd_setmuxmode(KBDC kbdc, int value, int *mux_version); 88 #endif 89 90 int 91 atkbd_probe_unit(int unit, int ctlr, int irq, int flags) 92 { 93 keyboard_switch_t *sw; 94 int args[2]; 95 int error; 96 97 sw = kbd_get_switch(ATKBD_DRIVER_NAME); 98 if (sw == NULL) { 99 return ENXIO; 100 } 101 102 args[0] = ctlr; 103 args[1] = irq; 104 error = (*sw->probe)(unit, args, flags); 105 106 if (error) 107 return error; 108 return 0; 109 } 110 111 int 112 atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags) 113 { 114 keyboard_switch_t *sw; 115 int args[2]; 116 int error; 117 atkbd_state_t *state; 118 119 sw = kbd_get_switch(ATKBD_DRIVER_NAME); 120 if (sw == NULL) { 121 return ENXIO; 122 } 123 124 /* reset, initialize and enable the device */ 125 args[0] = ctlr; 126 args[1] = irq; 127 *kbd = NULL; 128 error = (*sw->probe)(unit, args, flags); 129 if (error) { 130 return error; 131 } 132 error = (*sw->init)(unit, kbd, args, flags); 133 if (error) { 134 return error; 135 } 136 (*sw->enable)(*kbd); 137 138 #ifdef KBD_INSTALL_CDEV 139 /* attach a virtual keyboard cdev */ 140 error = kbd_attach(*kbd); 141 if (error) { 142 return error; 143 } 144 #endif 145 146 /* 147 * This is a kludge to compensate for lost keyboard interrupts. 148 * A similar code used to be in syscons. See below. XXX 149 */ 150 state = (atkbd_state_t *)(*kbd)->kb_data; 151 callout_init(&state->ks_timer); 152 atkbd_timeout(*kbd); 153 154 if (bootverbose) 155 (*sw->diag)(*kbd, bootverbose); 156 157 return 0; 158 } 159 160 static void 161 atkbd_timeout(void *arg) 162 { 163 atkbd_state_t *state; 164 keyboard_t *kbd; 165 166 /* 167 * The original text of the following comments are extracted 168 * from syscons.c (1.287) 169 * 170 * With release 2.1 of the Xaccel server, the keyboard is left 171 * hanging pretty often. Apparently an interrupt from the 172 * keyboard is lost, and I don't know why (yet). 173 * This ugly hack calls the low-level interrupt routine if input 174 * is ready for the keyboard and conveniently hides the problem. XXX 175 * 176 * Try removing anything stuck in the keyboard controller; whether 177 * it's a keyboard scan code or mouse data. The low-level 178 * interrupt routine doesn't read the mouse data directly, 179 * but the keyboard controller driver will, as a side effect. 180 */ 181 /* 182 * And here is bde's original comment about this: 183 * 184 * This is necessary to handle edge triggered interrupts - if we 185 * returned when our IRQ is high due to unserviced input, then there 186 * would be no more keyboard IRQs until the keyboard is reset by 187 * external powers. 188 * 189 * The keyboard apparently unwedges the irq in most cases. 190 */ 191 crit_enter(); 192 kbd = (keyboard_t *)arg; 193 if (kbd_lock(kbd, TRUE)) { 194 /* 195 * We have seen the lock flag is not set. Let's reset 196 * the flag early, otherwise the LED update routine fails 197 * which may want the lock during the interrupt routine. 198 */ 199 kbd_lock(kbd, FALSE); 200 if (kbd_check_char(kbd)) 201 kbd_intr(kbd, NULL); 202 } 203 crit_exit(); 204 state = (atkbd_state_t *)kbd->kb_data; 205 callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg); 206 } 207 208 /* LOW-LEVEL */ 209 210 #include <machine/limits.h> 211 #include <machine/clock.h> 212 213 #define ATKBD_DEFAULT 0 214 215 216 /* keyboard driver declaration */ 217 static int atkbd_configure(int flags); 218 static kbd_probe_t atkbd_probe; 219 static kbd_init_t atkbd_init; 220 static kbd_term_t atkbd_term; 221 static kbd_intr_t atkbd_intr; 222 static kbd_test_if_t atkbd_test_if; 223 static kbd_enable_t atkbd_enable; 224 static kbd_disable_t atkbd_disable; 225 static kbd_read_t atkbd_read; 226 static kbd_check_t atkbd_check; 227 static kbd_read_char_t atkbd_read_char; 228 static kbd_check_char_t atkbd_check_char; 229 static kbd_ioctl_t atkbd_ioctl; 230 static kbd_lock_t atkbd_lock; 231 static kbd_clear_state_t atkbd_clear_state; 232 static kbd_get_state_t atkbd_get_state; 233 static kbd_set_state_t atkbd_set_state; 234 static kbd_poll_mode_t atkbd_poll; 235 236 keyboard_switch_t atkbdsw = { 237 atkbd_probe, 238 atkbd_init, 239 atkbd_term, 240 atkbd_intr, 241 atkbd_test_if, 242 atkbd_enable, 243 atkbd_disable, 244 atkbd_read, 245 atkbd_check, 246 atkbd_read_char, 247 atkbd_check_char, 248 atkbd_ioctl, 249 atkbd_lock, 250 atkbd_clear_state, 251 atkbd_get_state, 252 atkbd_set_state, 253 genkbd_get_fkeystr, 254 atkbd_poll, 255 genkbd_diag, 256 }; 257 258 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure); 259 260 /* local functions */ 261 static int set_typematic(keyboard_t *kbd); /* replaces get_typematic() */ 262 /* static int get_typematic(keyboard_t *kbd); */ 263 static int setup_kbd_port(KBDC kbdc, int port, int intr); 264 static int get_kbd_echo(KBDC kbdc); 265 static int probe_keyboard(KBDC kbdc, int flags); 266 static int init_keyboard(KBDC kbdc, int *type, int flags); 267 static int write_kbd(KBDC kbdc, int command, int data); 268 static int get_kbd_id(KBDC kbdc, int cmd); 269 static int typematic(int delay, int rate); 270 static int typematic_delay(int delay); 271 static int typematic_rate(int rate); 272 273 /* local variables */ 274 275 /* the initial key map, accent map and fkey strings */ 276 #ifdef ATKBD_DFLT_KEYMAP 277 #define KBD_DFLT_KEYMAP 278 #include "atkbdmap.h" 279 #endif 280 #include "kbdtables.h" 281 282 /* structures for the default keyboard */ 283 static keyboard_t default_kbd; 284 static atkbd_state_t default_kbd_state; 285 static keymap_t default_keymap; 286 static accentmap_t default_accentmap; 287 static fkeytab_t default_fkeytab[NUM_FKEYS]; 288 289 /* 290 * The back door to the keyboard driver! 291 * This function is called by the console driver, via the kbdio module, 292 * to tickle keyboard drivers when the low-level console is being initialized. 293 * Almost nothing in the kernel has been initialied yet. Try to probe 294 * keyboards if possible. 295 * NOTE: because of the way the low-level console is initialized, this routine 296 * may be called more than once!! 297 */ 298 static int 299 atkbd_configure(int flags) 300 { 301 keyboard_t *kbd; 302 int arg[2]; 303 int i; 304 305 /* 306 * Probe the keyboard controller, if not present or if the driver 307 * is disabled, unregister the keyboard if any. 308 */ 309 if (atkbdc_configure() != 0 || 310 resource_disabled("atkbd", ATKBD_DEFAULT)) { 311 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT); 312 if (i >= 0) { 313 kbd = kbd_get_keyboard(i); 314 KBD_ALWAYS_LOCK(kbd); 315 kbd_unregister(kbd); 316 kbd->kb_flags &= ~KB_REGISTERED; 317 } 318 return 0; 319 } 320 321 /* XXX: a kludge to obtain the device configuration flags */ 322 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0) 323 flags |= i; 324 325 /* probe the default keyboard */ 326 arg[0] = -1; 327 arg[1] = -1; 328 kbd = NULL; 329 if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) { 330 return 0; 331 } 332 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) { 333 return 0; 334 } 335 336 /* return the number of found keyboards */ 337 return 1; 338 } 339 340 /* low-level functions */ 341 342 /* detect a keyboard */ 343 static int 344 atkbd_probe(int unit, void *arg, int flags) 345 { 346 KBDC kbdc; 347 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ 348 349 350 if (unit == ATKBD_DEFAULT) { 351 if (KBD_IS_PROBED(&default_kbd)) { 352 return 0; 353 } 354 } 355 356 kbdc = atkbdc_open(data[0]); 357 if (kbdc == NULL) { 358 return ENXIO; 359 } 360 if (probe_keyboard(kbdc, flags)) { 361 if (flags & KB_CONF_FAIL_IF_NO_KBD) { 362 return ENXIO; 363 } 364 } 365 366 return 0; 367 } 368 369 /* reset and initialize the device */ 370 static int 371 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 372 { 373 keyboard_t *kbd; 374 atkbd_state_t *state; 375 keymap_t *keymap; 376 accentmap_t *accmap; 377 fkeytab_t *fkeymap; 378 int fkeymap_size; 379 int delay[2]; 380 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ 381 int error, needfree; 382 383 /* XXX */ 384 if (unit == ATKBD_DEFAULT) { 385 *kbdp = kbd = &default_kbd; 386 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) { 387 return 0; 388 } 389 state = &default_kbd_state; 390 keymap = &default_keymap; 391 accmap = &default_accentmap; 392 fkeymap = default_fkeytab; 393 fkeymap_size = NELEM(default_fkeytab); 394 needfree = 0; 395 } else if (*kbdp == NULL) { 396 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_WAITOK|M_ZERO); 397 state = kmalloc(sizeof(*state), M_DEVBUF, M_WAITOK|M_ZERO); 398 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_WAITOK); 399 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_WAITOK); 400 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_WAITOK); 401 fkeymap_size = NELEM(fkey_tab); 402 needfree = 1; 403 if ((kbd == NULL) || (state == NULL) || (keymap == NULL) 404 || (accmap == NULL) || (fkeymap == NULL)) { 405 error = ENOMEM; 406 goto bad; 407 } 408 409 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { 410 return 0; 411 } else { 412 kbd = *kbdp; 413 state = (atkbd_state_t *)kbd->kb_data; 414 bzero(state, sizeof(*state)); 415 keymap = kbd->kb_keymap; 416 accmap = kbd->kb_accentmap; 417 fkeymap = kbd->kb_fkeytab; 418 fkeymap_size = kbd->kb_fkeytab_size; 419 needfree = 0; 420 } 421 422 if (!KBD_IS_PROBED(kbd)) { 423 state->kbdc = atkbdc_open(data[0]); 424 if (state->kbdc == NULL) { 425 error = ENXIO; 426 goto bad; 427 } 428 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags, 429 KB_PRI_ATKBD, 0, 0); 430 bcopy(&key_map, keymap, sizeof(key_map)); 431 bcopy(&accent_map, accmap, sizeof(accent_map)); 432 bcopy(fkey_tab, fkeymap, 433 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab))); 434 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); 435 kbd->kb_data = (void *)state; 436 437 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */ 438 if (flags & KB_CONF_FAIL_IF_NO_KBD) { 439 error = ENXIO; 440 goto bad; 441 } 442 } else { 443 KBD_FOUND_DEVICE(kbd); 444 } 445 atkbd_clear_state(kbd); 446 state->ks_mode = K_XLATE; 447 /* 448 * FIXME: set the initial value for lock keys in ks_state 449 * according to the BIOS data? 450 */ 451 KBD_PROBE_DONE(kbd); 452 } 453 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { 454 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; 455 if (!KBD_HAS_DEVICE(kbd) 456 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config) 457 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) { 458 kbd_unregister(kbd); 459 error = ENXIO; 460 goto bad; 461 } 462 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 463 set_typematic(kbd); 464 delay[0] = kbd->kb_delay1; 465 delay[1] = kbd->kb_delay2; 466 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 467 KBD_FOUND_DEVICE(kbd); 468 KBD_INIT_DONE(kbd); 469 } 470 if (!KBD_IS_CONFIGURED(kbd)) { 471 if (kbd_register(kbd) < 0) { 472 error = ENXIO; 473 goto bad; 474 } 475 KBD_CONFIG_DONE(kbd); 476 } 477 478 return 0; 479 bad: 480 if (needfree) { 481 if (state != NULL) 482 kfree(state, M_DEVBUF); 483 if (keymap != NULL) 484 kfree(keymap, M_DEVBUF); 485 if (accmap != NULL) 486 kfree(accmap, M_DEVBUF); 487 if (fkeymap != NULL) 488 kfree(fkeymap, M_DEVBUF); 489 if (kbd != NULL) { 490 kfree(kbd, M_DEVBUF); 491 *kbdp = NULL; /* insure ref doesn't leak to caller */ 492 } 493 } 494 return error; 495 496 } 497 498 /* finish using this keyboard */ 499 static int 500 atkbd_term(keyboard_t *kbd) 501 { 502 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; 503 504 kbd_unregister(kbd); 505 callout_stop_sync(&state->ks_timer); 506 return 0; 507 } 508 509 /* keyboard interrupt routine */ 510 static int 511 atkbd_intr(keyboard_t *kbd, void *arg) 512 { 513 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; 514 int delay[2]; 515 int c; 516 517 if (!KBD_HAS_DEVICE(kbd)) { 518 /* 519 * The keyboard was not detected before; 520 * it must have been reconnected! 521 */ 522 init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config); 523 KBD_FOUND_DEVICE(kbd); 524 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 525 set_typematic(kbd); 526 delay[0] = kbd->kb_delay1; 527 delay[1] = kbd->kb_delay2; 528 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 529 } 530 531 if (state->ks_polling) 532 return 0; 533 534 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { 535 /* let the callback function to process the input */ 536 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, 537 kbd->kb_callback.kc_arg); 538 } else { 539 /* read and discard the input; no one is waiting for input */ 540 do { 541 c = atkbd_read_char(kbd, FALSE); 542 } while (c != NOKEY); 543 } 544 return 0; 545 546 #if 0 547 atkbd_state_t *state; 548 int delay[2]; 549 int c; 550 551 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { 552 /* let the callback function to process the input */ 553 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, 554 kbd->kb_callback.kc_arg); 555 } else { 556 /* read and discard the input; no one is waiting for input */ 557 do { 558 c = atkbd_read_char(kbd, FALSE); 559 } while (c != NOKEY); 560 561 if (!KBD_HAS_DEVICE(kbd)) { 562 /* 563 * The keyboard was not detected before; 564 * it must have been reconnected! 565 */ 566 state = (atkbd_state_t *)kbd->kb_data; 567 init_keyboard(state->kbdc, &kbd->kb_type, 568 kbd->kb_config); 569 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 570 get_typematic(kbd); 571 delay[0] = kbd->kb_delay1; 572 delay[1] = kbd->kb_delay2; 573 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 574 KBD_FOUND_DEVICE(kbd); 575 } 576 } 577 578 return 0; 579 #endif 580 } 581 582 583 /* test the interface to the device */ 584 static int 585 atkbd_test_if(keyboard_t *kbd) 586 { 587 int error; 588 589 error = 0; 590 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10); 591 crit_enter(); 592 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc)) 593 error = EIO; 594 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0) 595 error = EIO; 596 crit_exit(); 597 598 return error; 599 } 600 601 /* 602 * Enable the access to the device; until this function is called, 603 * the client cannot read from the keyboard. 604 */ 605 static int 606 atkbd_enable(keyboard_t *kbd) 607 { 608 crit_enter(); 609 KBD_ACTIVATE(kbd); 610 crit_exit(); 611 return 0; 612 } 613 614 /* disallow the access to the device */ 615 static int 616 atkbd_disable(keyboard_t *kbd) 617 { 618 crit_enter(); 619 KBD_DEACTIVATE(kbd); 620 crit_exit(); 621 return 0; 622 } 623 624 /* read one byte from the keyboard if it's allowed */ 625 static int 626 atkbd_read(keyboard_t *kbd, int wait) 627 { 628 int c, ret; 629 630 if (wait) 631 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc); 632 else 633 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc); 634 if (c != -1) 635 ++kbd->kb_count; 636 637 ret = (KBD_IS_ACTIVE(kbd) ? c : -1); 638 639 return ret; 640 } 641 642 /* check if data is waiting */ 643 static int 644 atkbd_check(keyboard_t *kbd) 645 { 646 int ret; 647 648 if (!KBD_IS_ACTIVE(kbd)) { 649 return FALSE; 650 } 651 ret = kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc); 652 653 return ret; 654 } 655 656 /* read char from the keyboard */ 657 static u_int 658 atkbd_read_char(keyboard_t *kbd, int wait) 659 { 660 atkbd_state_t *state; 661 u_int action; 662 int scancode; 663 int keycode; 664 665 state = (atkbd_state_t *)kbd->kb_data; 666 next_code: 667 /* do we have a composed char to return? */ 668 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { 669 action = state->ks_composed_char; 670 state->ks_composed_char = 0; 671 if (action > UCHAR_MAX) { 672 return ERRKEY; 673 } 674 return action; 675 } 676 677 /* see if there is something in the keyboard port */ 678 if (wait) { 679 do { 680 scancode = read_kbd_data(state->kbdc); 681 } while (scancode == -1); 682 } else { 683 scancode = read_kbd_data_no_wait(state->kbdc); 684 if (scancode == -1) { 685 return NOKEY; 686 } 687 } 688 ++kbd->kb_count; 689 690 #if KBDIO_DEBUG >= 10 691 kprintf("atkbd_read_char(): scancode:0x%x\n", scancode); 692 #endif 693 694 /* return the byte as is for the K_RAW mode */ 695 if (state->ks_mode == K_RAW) { 696 return scancode; 697 } 698 699 /* translate the scan code into a keycode */ 700 keycode = scancode & 0x7F; 701 switch (state->ks_prefix) { 702 case 0x00: /* normal scancode */ 703 switch(scancode) { 704 case 0xB8: /* left alt (compose key) released */ 705 if (state->ks_flags & COMPOSE) { 706 state->ks_flags &= ~COMPOSE; 707 if (state->ks_composed_char > UCHAR_MAX) 708 state->ks_composed_char = 0; 709 } 710 break; 711 case 0x38: /* left alt (compose key) pressed */ 712 if (!(state->ks_flags & COMPOSE)) { 713 state->ks_flags |= COMPOSE; 714 state->ks_composed_char = 0; 715 } 716 break; 717 case 0xE0: 718 case 0xE1: 719 state->ks_prefix = scancode; 720 goto next_code; 721 } 722 break; 723 case 0xE0: /* 0xE0 prefix */ 724 state->ks_prefix = 0; 725 switch (keycode) { 726 case 0x1C: /* right enter key */ 727 keycode = 0x59; 728 break; 729 case 0x1D: /* right ctrl key */ 730 keycode = 0x5A; 731 break; 732 case 0x35: /* keypad divide key */ 733 keycode = 0x5B; 734 break; 735 case 0x37: /* print scrn key */ 736 keycode = 0x5C; 737 break; 738 case 0x38: /* right alt key (alt gr) */ 739 keycode = 0x5D; 740 break; 741 case 0x46: /* ctrl-pause/break on AT 101 (see below) */ 742 keycode = 0x68; 743 break; 744 case 0x47: /* grey home key */ 745 keycode = 0x5E; 746 break; 747 case 0x48: /* grey up arrow key */ 748 keycode = 0x5F; 749 break; 750 case 0x49: /* grey page up key */ 751 keycode = 0x60; 752 break; 753 case 0x4B: /* grey left arrow key */ 754 keycode = 0x61; 755 break; 756 case 0x4D: /* grey right arrow key */ 757 keycode = 0x62; 758 break; 759 case 0x4F: /* grey end key */ 760 keycode = 0x63; 761 break; 762 case 0x50: /* grey down arrow key */ 763 keycode = 0x64; 764 break; 765 case 0x51: /* grey page down key */ 766 keycode = 0x65; 767 break; 768 case 0x52: /* grey insert key */ 769 keycode = 0x66; 770 break; 771 case 0x53: /* grey delete key */ 772 keycode = 0x67; 773 break; 774 /* the following 3 are only used on the MS "Natural" keyboard */ 775 case 0x5b: /* left Window key */ 776 keycode = 0x69; 777 break; 778 case 0x5c: /* right Window key */ 779 keycode = 0x6a; 780 break; 781 case 0x5d: /* menu key */ 782 keycode = 0x6b; 783 break; 784 case 0x5e: /* power key */ 785 keycode = 0x6d; 786 break; 787 case 0x5f: /* sleep key */ 788 keycode = 0x6e; 789 break; 790 case 0x63: /* wake key */ 791 keycode = 0x6f; 792 break; 793 794 default: /* ignore everything else */ 795 goto next_code; 796 } 797 break; 798 case 0xE1: /* 0xE1 prefix */ 799 /* 800 * The pause/break key on the 101 keyboard produces: 801 * E1-1D-45 E1-9D-C5 802 * Ctrl-pause/break produces: 803 * E0-46 E0-C6 (See above.) 804 */ 805 state->ks_prefix = 0; 806 if (keycode == 0x1D) 807 state->ks_prefix = 0x1D; 808 goto next_code; 809 /* NOT REACHED */ 810 case 0x1D: /* pause / break */ 811 state->ks_prefix = 0; 812 if (keycode != 0x45) 813 goto next_code; 814 keycode = 0x68; 815 break; 816 } 817 818 if (kbd->kb_type == KB_84) { 819 switch (keycode) { 820 case 0x37: /* *(numpad)/print screen */ 821 if (state->ks_flags & SHIFTS) 822 keycode = 0x5c; /* print screen */ 823 break; 824 case 0x45: /* num lock/pause */ 825 if (state->ks_flags & CTLS) 826 keycode = 0x68; /* pause */ 827 break; 828 case 0x46: /* scroll lock/break */ 829 if (state->ks_flags & CTLS) 830 keycode = 0x6c; /* break */ 831 break; 832 } 833 } else if (kbd->kb_type == KB_101) { 834 switch (keycode) { 835 case 0x5c: /* print screen */ 836 if (state->ks_flags & ALTS) 837 keycode = 0x54; /* sysrq */ 838 break; 839 case 0x68: /* pause/break */ 840 if (state->ks_flags & CTLS) 841 keycode = 0x6c; /* break */ 842 break; 843 } 844 } 845 846 /* return the key code in the K_CODE mode */ 847 if (state->ks_mode == K_CODE) { 848 return (keycode | (scancode & 0x80)); 849 } 850 851 /* compose a character code */ 852 if (state->ks_flags & COMPOSE) { 853 switch (keycode | (scancode & 0x80)) { 854 /* key pressed, process it */ 855 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ 856 state->ks_composed_char *= 10; 857 state->ks_composed_char += keycode - 0x40; 858 if (state->ks_composed_char > UCHAR_MAX) 859 return ERRKEY; 860 goto next_code; 861 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ 862 state->ks_composed_char *= 10; 863 state->ks_composed_char += keycode - 0x47; 864 if (state->ks_composed_char > UCHAR_MAX) 865 return ERRKEY; 866 goto next_code; 867 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ 868 state->ks_composed_char *= 10; 869 state->ks_composed_char += keycode - 0x4E; 870 if (state->ks_composed_char > UCHAR_MAX) 871 return ERRKEY; 872 goto next_code; 873 case 0x52: /* keypad 0 */ 874 state->ks_composed_char *= 10; 875 if (state->ks_composed_char > UCHAR_MAX) 876 return ERRKEY; 877 goto next_code; 878 879 /* key released, no interest here */ 880 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ 881 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ 882 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ 883 case 0xD2: /* keypad 0 */ 884 goto next_code; 885 886 case 0x38: /* left alt key */ 887 break; 888 889 default: 890 if (state->ks_composed_char > 0) { 891 state->ks_flags &= ~COMPOSE; 892 state->ks_composed_char = 0; 893 return ERRKEY; 894 } 895 break; 896 } 897 } 898 899 /* keycode to key action */ 900 action = genkbd_keyaction(kbd, keycode, scancode & 0x80, 901 &state->ks_state, &state->ks_accents); 902 if (action == NOKEY) { 903 goto next_code; 904 } else { 905 return action; 906 } 907 } 908 909 /* check if char is waiting */ 910 static int 911 atkbd_check_char(keyboard_t *kbd) 912 { 913 atkbd_state_t *state; 914 int ret; 915 916 if (!KBD_IS_ACTIVE(kbd)) { 917 return FALSE; 918 } 919 state = (atkbd_state_t *)kbd->kb_data; 920 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { 921 return TRUE; 922 } 923 ret = kbdc_data_ready(state->kbdc); 924 return ret; 925 } 926 927 /* some useful control functions */ 928 static int 929 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 930 { 931 /* trasnlate LED_XXX bits into the device specific bits */ 932 static u_char ledmap[8] = { 933 0, 4, 2, 6, 1, 5, 3, 7, 934 }; 935 atkbd_state_t *state = kbd->kb_data; 936 int error; 937 int i; 938 939 crit_enter(); 940 941 switch (cmd) { 942 943 case KDGKBMODE: /* get keyboard mode */ 944 *(int *)arg = state->ks_mode; 945 break; 946 case KDSKBMODE: /* set keyboard mode */ 947 switch (*(int *)arg) { 948 case K_XLATE: 949 if (state->ks_mode != K_XLATE) { 950 /* make lock key state and LED state match */ 951 state->ks_state &= ~LOCK_MASK; 952 state->ks_state |= KBD_LED_VAL(kbd); 953 } 954 /* FALL THROUGH */ 955 case K_RAW: 956 case K_CODE: 957 if (state->ks_mode != *(int *)arg) { 958 atkbd_clear_state(kbd); 959 state->ks_mode = *(int *)arg; 960 } 961 break; 962 default: 963 crit_exit(); 964 return EINVAL; 965 } 966 break; 967 968 case KDGETLED: /* get keyboard LED */ 969 *(int *)arg = KBD_LED_VAL(kbd); 970 break; 971 case KDSETLED: /* set keyboard LED */ 972 /* NOTE: lock key state in ks_state won't be changed */ 973 if (*(int *)arg & ~LOCK_MASK) { 974 crit_exit(); 975 return EINVAL; 976 } 977 i = *(int *)arg; 978 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 979 if (state->ks_mode == K_XLATE && 980 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 981 if (i & ALKED) 982 i |= CLKED; 983 else 984 i &= ~CLKED; 985 } 986 if (KBD_HAS_DEVICE(kbd)) { 987 error = write_kbd(state->kbdc, KBDC_SET_LEDS, 988 ledmap[i & LED_MASK]); 989 if (error) { 990 crit_exit(); 991 return error; 992 } 993 } 994 KBD_LED_VAL(kbd) = *(int *)arg; 995 break; 996 997 case KDGKBSTATE: /* get lock key state */ 998 *(int *)arg = state->ks_state & LOCK_MASK; 999 break; 1000 case KDSKBSTATE: /* set lock key state */ 1001 if (*(int *)arg & ~LOCK_MASK) { 1002 crit_exit(); 1003 return EINVAL; 1004 } 1005 state->ks_state &= ~LOCK_MASK; 1006 state->ks_state |= *(int *)arg; 1007 crit_exit(); 1008 /* set LEDs and quit */ 1009 return atkbd_ioctl(kbd, KDSETLED, arg); 1010 1011 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ 1012 crit_exit(); 1013 if (!KBD_HAS_DEVICE(kbd)) { 1014 return 0; 1015 } 1016 i = typematic(((int *)arg)[0], ((int *)arg)[1]); 1017 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i); 1018 if (error == 0) { 1019 kbd->kb_delay1 = typematic_delay(i); 1020 kbd->kb_delay2 = typematic_rate(i); 1021 } 1022 return error; 1023 1024 case KDSETRAD: /* set keyboard repeat rate (old interface) */ 1025 crit_exit(); 1026 if (!KBD_HAS_DEVICE(kbd)) { 1027 return 0; 1028 } 1029 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg); 1030 if (error == 0) { 1031 kbd->kb_delay1 = typematic_delay(*(int *)arg); 1032 kbd->kb_delay2 = typematic_rate(*(int *)arg); 1033 } 1034 return error; 1035 1036 case PIO_KEYMAP: /* set keyboard translation table */ 1037 case PIO_KEYMAPENT: /* set keyboard translation table entry */ 1038 case PIO_DEADKEYMAP: /* set accent key translation table */ 1039 state->ks_accents = 0; 1040 /* FALL THROUGH */ 1041 default: 1042 crit_exit(); 1043 return genkbd_commonioctl(kbd, cmd, arg); 1044 } 1045 1046 crit_exit(); 1047 return 0; 1048 } 1049 1050 /* lock the access to the keyboard */ 1051 static int 1052 atkbd_lock(keyboard_t *kbd, int lock) 1053 { 1054 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock); 1055 } 1056 1057 /* clear the internal state of the keyboard */ 1058 static void 1059 atkbd_clear_state(keyboard_t *kbd) 1060 { 1061 atkbd_state_t *state; 1062 1063 state = (atkbd_state_t *)kbd->kb_data; 1064 state->ks_flags = 0; 1065 state->ks_polling = 0; 1066 state->ks_state &= LOCK_MASK; /* preserve locking key state */ 1067 state->ks_accents = 0; 1068 state->ks_composed_char = 0; 1069 #if 0 1070 state->ks_prefix = 0; /* XXX */ 1071 #endif 1072 } 1073 1074 /* save the internal state */ 1075 static int 1076 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1077 { 1078 if (len == 0) 1079 return sizeof(atkbd_state_t); 1080 if (len < sizeof(atkbd_state_t)) 1081 return -1; 1082 1083 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t)); 1084 return 0; 1085 } 1086 1087 /* set the internal state */ 1088 static int 1089 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1090 { 1091 if (len < sizeof(atkbd_state_t)) 1092 return ENOMEM; 1093 if (((atkbd_state_t *)kbd->kb_data)->kbdc 1094 != ((atkbd_state_t *)buf)->kbdc) 1095 return ENOMEM; 1096 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t)); 1097 return 0; 1098 } 1099 1100 static int 1101 atkbd_poll(keyboard_t *kbd, int on) 1102 { 1103 atkbd_state_t *state; 1104 1105 state = (atkbd_state_t *)kbd->kb_data; 1106 crit_enter(); 1107 if (on) 1108 state->ks_polling = 1; 1109 else 1110 state->ks_polling = 0; 1111 crit_exit(); 1112 return 0; 1113 } 1114 1115 static int 1116 atkbd_reset(KBDC kbdc, int flags, int c) 1117 { 1118 /* reset keyboard hardware */ 1119 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) { 1120 /* 1121 * KEYBOARD ERROR 1122 * Keyboard reset may fail either because the keyboard 1123 * doen't exist, or because the keyboard doesn't pass 1124 * the self-test, or the keyboard controller on the 1125 * motherboard and the keyboard somehow fail to shake hands. 1126 * It is just possible, particularly in the last case, 1127 * that the keyboard controller may be left in a hung state. 1128 * test_controller() and test_kbd_port() appear to bring 1129 * the keyboard controller back (I don't know why and how, 1130 * though.) 1131 */ 1132 empty_both_buffers(kbdc, 10); 1133 test_controller(kbdc); 1134 test_kbd_port(kbdc); 1135 /* 1136 * We could disable the keyboard port and interrupt... but, 1137 * the keyboard may still exist (see above). 1138 */ 1139 set_controller_command_byte(kbdc, 1140 ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c); 1141 if (bootverbose) 1142 kprintf("atkbd: failed to reset the keyboard.\n"); 1143 return (EIO); 1144 } 1145 return (0); 1146 } 1147 1148 1149 /* local functions */ 1150 1151 #if 0 /* replaced by set_typematic() */ 1152 static int 1153 get_typematic(keyboard_t *kbd) 1154 { 1155 #ifdef __i386__ 1156 /* 1157 * Only some systems allow us to retrieve the keyboard repeat 1158 * rate previously set via the BIOS... 1159 */ 1160 struct vm86frame vmf; 1161 u_int32_t p; 1162 1163 bzero(&vmf, sizeof(vmf)); 1164 vmf.vmf_ax = 0xc000; 1165 vm86_intcall(0x15, &vmf); 1166 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah) 1167 return ENODEV; 1168 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx); 1169 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */ 1170 return ENODEV; 1171 vmf.vmf_ax = 0x0900; 1172 vm86_intcall(0x16, &vmf); 1173 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */ 1174 return ENODEV; 1175 vmf.vmf_ax = 0x0306; 1176 vm86_intcall(0x16, &vmf); 1177 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5); 1178 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl); 1179 return 0; 1180 #else 1181 return ENODEV; 1182 #endif /* __i386__ */ 1183 } 1184 #endif 1185 1186 static int 1187 set_typematic(keyboard_t *kbd) 1188 { 1189 int val, error; 1190 atkbd_state_t *state = kbd->kb_data; 1191 1192 val = typematic(DEFAULT_DELAY, DEFAULT_RATE); 1193 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val); 1194 if (error == 0) { 1195 kbd->kb_delay1 = typematic_delay(val); 1196 kbd->kb_delay2 = typematic_rate(val); 1197 } 1198 1199 return (error); 1200 } 1201 1202 static int 1203 setup_kbd_port(KBDC kbdc, int port, int intr) 1204 { 1205 if (!set_controller_command_byte(kbdc, 1206 KBD_KBD_CONTROL_BITS, 1207 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT) | 1208 ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT))) 1209 return 1; 1210 return 0; 1211 } 1212 1213 static int 1214 get_kbd_echo(KBDC kbdc) 1215 { 1216 /* enable the keyboard port, but disable the keyboard intr. */ 1217 if (setup_kbd_port(kbdc, TRUE, FALSE)) 1218 /* CONTROLLER ERROR: there is very little we can do... */ 1219 return ENXIO; 1220 1221 /* see if something is present */ 1222 write_kbd_command(kbdc, KBDC_ECHO); 1223 if (read_kbd_data(kbdc) != KBD_ECHO) { 1224 empty_both_buffers(kbdc, 10); 1225 test_controller(kbdc); 1226 test_kbd_port(kbdc); 1227 return ENXIO; 1228 } 1229 1230 /* enable the keyboard port and intr. */ 1231 if (setup_kbd_port(kbdc, TRUE, TRUE)) { 1232 /* 1233 * CONTROLLER ERROR 1234 * This is serious; the keyboard intr is left disabled! 1235 */ 1236 return ENXIO; 1237 } 1238 1239 return 0; 1240 } 1241 1242 static int 1243 probe_keyboard(KBDC kbdc, int flags) 1244 { 1245 /* 1246 * Don't try to print anything in this function. The low-level 1247 * console may not have been initialized yet... 1248 */ 1249 int err; 1250 int c; 1251 int m; 1252 1253 if (!kbdc_lock(kbdc, TRUE)) { 1254 /* driver error? */ 1255 return ENXIO; 1256 } 1257 1258 /* 1259 * XXX block data transmission from the keyboard. This can cause 1260 * the keyboard to stop sending keystrokes even when re-enabled 1261 * under certain circumstances if not followed by a full reset. 1262 */ 1263 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); 1264 1265 /* flush any noise in the buffer */ 1266 empty_both_buffers(kbdc, 100); 1267 1268 /* save the current keyboard controller command byte */ 1269 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS; 1270 c = get_controller_command_byte(kbdc); 1271 if (c == -1) { 1272 /* CONTROLLER ERROR */ 1273 kbdc_set_device_mask(kbdc, m); 1274 kbdc_lock(kbdc, FALSE); 1275 return ENXIO; 1276 } 1277 1278 /* 1279 * The keyboard may have been screwed up by the boot block. 1280 * We may just be able to recover from error by testing the controller 1281 * and the keyboard port. The controller command byte needs to be 1282 * saved before this recovery operation, as some controllers seem 1283 * to set the command byte to particular values. 1284 */ 1285 test_controller(kbdc); 1286 if (!(flags & KB_CONF_NO_PROBE_TEST)) 1287 test_kbd_port(kbdc); 1288 1289 err = get_kbd_echo(kbdc); 1290 1291 /* 1292 * Even if the keyboard doesn't seem to be present (err != 0), 1293 * we shall enable the keyboard port and interrupt so that 1294 * the driver will be operable when the keyboard is attached 1295 * to the system later. It is NOT recommended to hot-plug 1296 * the AT keyboard, but many people do so... 1297 */ 1298 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); 1299 setup_kbd_port(kbdc, TRUE, TRUE); 1300 #if 0 1301 if (err) { 1302 /* try to restore the command byte as before */ 1303 set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c); 1304 } 1305 #endif 1306 1307 kbdc_lock(kbdc, FALSE); 1308 return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err); 1309 } 1310 1311 static int 1312 init_keyboard(KBDC kbdc, int *type, int flags) 1313 { 1314 int codeset; 1315 int id; 1316 int c; 1317 int mux_version; 1318 int mux_mask; 1319 int mux_val; 1320 1321 if (!kbdc_lock(kbdc, TRUE)) { 1322 /* driver error? */ 1323 return EIO; 1324 } 1325 1326 /* 1327 * XXX block data transmission from the keyboard. This can cause 1328 * the keyboard to stop sending keystrokes even when re-enabled 1329 * under certain circumstances if not followed by a full reset. 1330 */ 1331 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); 1332 1333 #if 0 1334 if (atkbd_setmuxmode(kbdc, 1, &mux_version)) { 1335 kprintf("atkbd: no mux\n"); 1336 mux_version = -1; 1337 } else { 1338 kprintf("atkbd: mux present version %d\n", mux_version); 1339 } 1340 #else 1341 mux_version = -1; 1342 #endif 1343 1344 /* save the current controller command byte */ 1345 empty_both_buffers(kbdc, 200); 1346 c = get_controller_command_byte(kbdc); 1347 if (c == -1) { 1348 /* CONTROLLER ERROR */ 1349 kbdc_lock(kbdc, FALSE); 1350 kprintf("atkbd: unable to get the current command byte value.\n"); 1351 return EIO; 1352 } 1353 if (bootverbose) 1354 kprintf("atkbd: the current kbd controller command byte %04x\n", 1355 c); 1356 #if 0 1357 /* override the keyboard lock switch */ 1358 c |= KBD_OVERRIDE_KBD_LOCK; 1359 #endif 1360 1361 /* enable the keyboard port, but disable the keyboard intr. */ 1362 if (setup_kbd_port(kbdc, TRUE, FALSE)) { 1363 /* CONTROLLER ERROR: there is very little we can do... */ 1364 kprintf("atkbd: unable to set the command byte.\n"); 1365 kbdc_lock(kbdc, FALSE); 1366 return EIO; 1367 } 1368 1369 if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) && 1370 atkbd_reset(kbdc, flags, c)) { 1371 kbdc_lock(kbdc, FALSE); 1372 return EIO; 1373 } 1374 1375 /* default codeset */ 1376 codeset = -1; 1377 1378 /* reset keyboard hardware */ 1379 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) { 1380 /* 1381 * KEYBOARD ERROR 1382 * Keyboard reset may fail either because the keyboard 1383 * doen't exist, or because the keyboard doesn't pass 1384 * the self-test, or the keyboard controller on the 1385 * motherboard and the keyboard somehow fail to shake hands. 1386 * It is just possible, particularly in the last case, 1387 * that the keyoard controller may be left in a hung state. 1388 * test_controller() and test_kbd_port() appear to bring 1389 * the keyboard controller back (I don't know why and how, 1390 * though.) 1391 */ 1392 empty_both_buffers(kbdc, 10); 1393 test_controller(kbdc); 1394 test_kbd_port(kbdc); 1395 /* 1396 * We could disable the keyboard port and interrupt... but, 1397 * the keyboard may still exist (see above). 1398 */ 1399 set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c); 1400 kbdc_lock(kbdc, FALSE); 1401 if (bootverbose) 1402 kprintf("atkbd: failed to reset the keyboard.\n"); 1403 return EIO; 1404 } 1405 1406 /* 1407 * Check if we have an XT keyboard before we attempt to reset it. 1408 * The procedure assumes that the keyboard and the controller have 1409 * been set up properly by BIOS and have not been messed up 1410 * during the boot process. 1411 */ 1412 codeset = -1; 1413 if (flags & KB_CONF_ALT_SCANCODESET) 1414 /* the user says there is a XT keyboard */ 1415 codeset = 1; 1416 #ifdef KBD_DETECT_XT_KEYBOARD 1417 else if ((c & KBD_TRANSLATION) == 0) { 1418 /* SET_SCANCODE_SET is not always supported; ignore error */ 1419 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0) 1420 == KBD_ACK) 1421 codeset = read_kbd_data(kbdc); 1422 } 1423 #endif /* KBD_DETECT_XT_KEYBOARD */ 1424 if (bootverbose) 1425 kprintf("atkbd: scancode set %d\n", codeset); 1426 1427 /* 1428 * Get the keyboard id. 1429 */ 1430 *type = KB_OTHER; 1431 id = get_kbd_id(kbdc, ATKBD_CMD_GETID); 1432 switch(id) { 1433 case 0x41ab: /* 101/102/... Enhanced */ 1434 case 0x83ab: /* ditto */ 1435 case 0x54ab: /* SpaceSaver */ 1436 case 0x84ab: /* ditto */ 1437 #if 0 1438 case 0x90ab: /* 'G' */ 1439 case 0x91ab: /* 'P' */ 1440 case 0x92ab: /* 'A' */ 1441 #endif 1442 *type = KB_101; 1443 break; 1444 case -1: /* AT 84 keyboard doesn't return ID */ 1445 *type = KB_84; 1446 break; 1447 default: 1448 break; 1449 } 1450 if (bootverbose) 1451 kprintf("atkbd: keyboard ID 0x%x (%d)\n", id, *type); 1452 1453 if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) && 1454 atkbd_reset(kbdc, flags, c)) { 1455 kbdc_lock(kbdc, FALSE); 1456 return EIO; 1457 } 1458 1459 /* 1460 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards 1461 * such as those on the IBM ThinkPad laptop computers can be used 1462 * with the standard console driver. 1463 */ 1464 if (codeset == 1) { 1465 if (send_kbd_command_and_data(kbdc, 1466 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) { 1467 /* XT kbd doesn't need scan code translation */ 1468 c &= ~KBD_TRANSLATION; 1469 } else { 1470 /* 1471 * KEYBOARD ERROR 1472 * The XT kbd isn't usable unless the proper scan 1473 * code set is selected. 1474 */ 1475 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) 1476 ? 0xff : KBD_KBD_CONTROL_BITS, c); 1477 kbdc_lock(kbdc, FALSE); 1478 kprintf("atkbd: unable to set the XT keyboard mode.\n"); 1479 return EIO; 1480 } 1481 } 1482 1483 #if 0 1484 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_EX_ENABLE, 0x71) != KBD_ACK) 1485 kprintf("atkbd: can't CMD_EX_ENABLE\n"); 1486 1487 if (send_kbd_command(kbdc, ATKBD_CMD_SETALL_MB) != KBD_ACK) 1488 kprintf("atkbd: can't SETALL_MB\n"); 1489 if (send_kbd_command(kbdc, ATKBD_CMD_SETALL_MBR) != KBD_ACK) 1490 kprintf("atkbd: can't SETALL_MBR\n"); 1491 #endif 1492 #if 0 1493 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SSCANSET, 2) != KBD_ACK) 1494 kprintf("atkbd: can't SSCANSET\n"); 1495 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_GSCANSET, 0) != KBD_ACK) 1496 kprintf("atkbd: can't SSCANSET\n"); 1497 else 1498 kprintf("atkbd: scanset %d\n", read_kbd_data(kbdc)); 1499 #endif 1500 #if 0 1501 kprintf("atkbd: id %04x\n", get_kbd_id(kbdc, ATKBD_CMD_OK_GETID)); 1502 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETLEDS, 0) != KBD_ACK) 1503 kprintf("atkbd: setleds failed\n"); 1504 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETREP, 255) != KBD_ACK) 1505 kprintf("atkbd: setrep failed\n"); 1506 if (send_kbd_command(kbdc, ATKBD_CMD_RESEND) != KBD_ACK) 1507 kprintf("atkbd: resend failed\n"); 1508 #endif 1509 /* 1510 * Some keyboards require a SETLEDS command to be sent after 1511 * the reset command before they will send keystrokes to us 1512 * (Acer C720). 1513 */ 1514 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETLEDS, 0) != KBD_ACK) 1515 kprintf("atkbd: setleds failed\n"); 1516 if (!ALLOW_DISABLE_KBD(kbdc)) 1517 send_kbd_command(kbdc, KBDC_ENABLE_KBD); 1518 /* used to be: send_kbd_command(kbdc, ATKBD_CMD_ENABLE); */ 1519 1520 #if 0 1521 /* DEBUGGING */ 1522 { 1523 int retry; 1524 int c; 1525 kprintf("atkbd: waiting for keypress"); 1526 for (retry = 0; retry < 10; ++retry) { 1527 c = read_kbd_data_no_wait(kbdc); 1528 kprintf(" %d", c); 1529 tsleep(&c, 0, "wait", hz); 1530 } 1531 kprintf("\n"); 1532 } 1533 #endif 1534 1535 if (mux_version == -1) { 1536 mux_mask = 0; 1537 mux_val = 0; 1538 } else { 1539 mux_mask = KBD_AUX_CONTROL_BITS; 1540 mux_val = 0; 1541 kprintf("atkbd: setaux for multiplexer\n"); 1542 } 1543 1544 /* enable the keyboard port and intr. */ 1545 if (!set_controller_command_byte(kbdc, 1546 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | 1547 KBD_OVERRIDE_KBD_LOCK | mux_mask, 1548 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK)) 1549 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT | mux_val)) { 1550 /* 1551 * CONTROLLER ERROR 1552 * This is serious; we are left with the disabled 1553 * keyboard intr. 1554 */ 1555 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc) 1556 ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | 1557 KBD_OVERRIDE_KBD_LOCK | mux_mask), c); 1558 kbdc_lock(kbdc, FALSE); 1559 kprintf("atkbd: unable to enable the keyboard port and intr.\n"); 1560 return EIO; 1561 } 1562 1563 kbdc_lock(kbdc, FALSE); 1564 return 0; 1565 } 1566 1567 #if 0 1568 1569 static 1570 int 1571 atkbd_setmuxmode(KBDC kbdc, int enable, int *mux_version) 1572 { 1573 int param; 1574 int val; 1575 int i; 1576 1577 kbdc->mux_active = 0; 1578 empty_both_buffers(kbdc, 100); 1579 val = 0xf0; 1580 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) { 1581 kprintf("setmuxmode: fail1\n"); 1582 return(-1); 1583 } 1584 val = enable ? 0x56 : 0xf6; 1585 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) { 1586 kprintf("setmuxmode: fail2\n"); 1587 return(-1); 1588 } 1589 val = enable ? 0xa4 : 0xa5; 1590 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) { 1591 kprintf("setmuxmode: fail3\n"); 1592 return(-1); 1593 } 1594 kprintf("mux version %02x\n", param); 1595 if (param == 0xac) { 1596 kprintf("setmuxmode: fail4\n"); 1597 return(-1); 1598 } 1599 1600 if (enable) { 1601 for (i = 0; i < KBD_NUM_MUX_PORTS; ++i) { 1602 write_controller_command(kbdc, KBDC_MUX_PFX + i); 1603 write_controller_command(kbdc, KBDC_ENABLE_AUX_PORT); 1604 1605 } 1606 } 1607 kbdc->mux_active = 1; 1608 if (mux_version) 1609 *mux_version = param; 1610 return 0; 1611 } 1612 1613 #endif 1614 1615 static int 1616 write_kbd(KBDC kbdc, int command, int data) 1617 { 1618 /* prevent the timeout routine from polling the keyboard */ 1619 if (!kbdc_lock(kbdc, TRUE)) 1620 return EBUSY; 1621 1622 /* disable the keyboard and mouse interrupt */ 1623 crit_enter(); 1624 1625 #if 0 1626 /* 1627 * XXX NOTE: We can't just disable the KBD port any more, even 1628 * temporarily, without blowing up some BIOS emulations 1629 * if not followed by a full reset. 1630 */ 1631 c = get_controller_command_byte(kbdc); 1632 if ((c == -1) || 1633 !set_controller_command_byte(kbdc, 1634 KBD_KBD_CONTROL_BITS, 1635 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT)) { 1636 /* CONTROLLER ERROR */ 1637 kbdc_lock(kbdc, FALSE); 1638 crit_exit(); 1639 return EIO; 1640 } 1641 /* 1642 * Now that the keyboard controller is told not to generate 1643 * the keyboard and mouse interrupts, call `splx()' to allow 1644 * the other tty interrupts. The clock interrupt may also occur, 1645 * but the timeout routine (`scrn_timer()') will be blocked 1646 * by the lock flag set via `kbdc_lock()' 1647 */ 1648 crit_exit(); 1649 #endif 1650 1651 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK) 1652 send_kbd_command(kbdc, KBDC_ENABLE_KBD); 1653 1654 #if 0 1655 /* restore the interrupts */ 1656 if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c)) { 1657 /* CONTROLLER ERROR */ 1658 } 1659 #else 1660 crit_exit(); 1661 #endif 1662 kbdc_lock(kbdc, FALSE); 1663 1664 return 0; 1665 } 1666 1667 static int 1668 get_kbd_id(KBDC kbdc, int cmd) 1669 { 1670 int id1, id2; 1671 1672 empty_both_buffers(kbdc, 10); 1673 id1 = id2 = -1; 1674 if (send_kbd_command(kbdc, cmd) != KBD_ACK) 1675 return -1; 1676 1677 DELAY(10000); /* 10 msec delay */ 1678 id1 = read_kbd_data(kbdc); 1679 if (id1 != -1) 1680 id2 = read_kbd_data(kbdc); 1681 1682 if ((id1 == -1) || (id2 == -1)) { 1683 empty_both_buffers(kbdc, 10); 1684 test_controller(kbdc); 1685 test_kbd_port(kbdc); 1686 return -1; 1687 } 1688 return ((id2 << 8) | id1); 1689 } 1690 1691 static int delays[] = { 250, 500, 750, 1000 }; 1692 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 1693 68, 76, 84, 92, 100, 110, 118, 126, 1694 136, 152, 168, 184, 200, 220, 236, 252, 1695 272, 304, 336, 368, 400, 440, 472, 504 }; 1696 1697 static int 1698 typematic_delay(int i) 1699 { 1700 return delays[(i >> 5) & 3]; 1701 } 1702 1703 static int 1704 typematic_rate(int i) 1705 { 1706 return rates[i & 0x1f]; 1707 } 1708 1709 static int 1710 typematic(int delay, int rate) 1711 { 1712 int value; 1713 int i; 1714 1715 for (i = NELEM(delays) - 1; i > 0; --i) { 1716 if (delay >= delays[i]) 1717 break; 1718 } 1719 value = i << 5; 1720 for (i = NELEM(rates) - 1; i > 0; --i) { 1721 if (rate >= rates[i]) 1722 break; 1723 } 1724 value |= i; 1725 return value; 1726 } 1727