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