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