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