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