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