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