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