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