1 /*- 2 * (MPSAFE) 3 * 4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer as 12 * the first lines of this file unmodified. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $ 29 * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.23 2007/05/08 02:31:39 dillon Exp $ 30 */ 31 /* 32 * Generic keyboard driver. 33 * 34 * Interrupt note: keyboards use clist functions and since usb keyboard 35 * interrupts are not protected by spltty(), we must use a critical section 36 * to protect against corruption. 37 * XXX: this keyboard driver doesn't use clist functions anymore! 38 * 39 * MPSAFE NOTE: all keyboards could easily be put under a different global token. 40 */ 41 42 #include "opt_kbd.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/conf.h> 49 #include <sys/proc.h> 50 #include <sys/tty.h> 51 #include <sys/event.h> 52 #include <sys/vnode.h> 53 #include <sys/uio.h> 54 #include <sys/thread.h> 55 #include <sys/thread2.h> 56 57 #include <machine/console.h> 58 59 #include "kbdreg.h" 60 61 #if 0 62 #define lwkt_gettoken(x) 63 #define lwkt_reltoken(x) 64 #endif 65 66 #define KBD_INDEX(dev) minor(dev) 67 68 #define KB_QSIZE 512 69 #define KB_BUFSIZE 64 70 71 struct genkbd_softc { 72 int gkb_flags; /* flag/status bits */ 73 #define KB_ASLEEP (1 << 0) 74 struct kqinfo gkb_rkq; 75 char gkb_q[KB_QSIZE]; /* input queue */ 76 unsigned int gkb_q_start; 77 unsigned int gkb_q_length; 78 }; 79 80 typedef struct genkbd_softc *genkbd_softc_t; 81 82 static SLIST_HEAD(, keyboard_driver) keyboard_drivers = 83 SLIST_HEAD_INITIALIZER(keyboard_drivers); 84 85 SET_DECLARE(kbddriver_set, const keyboard_driver_t); 86 87 /* local arrays */ 88 89 /* 90 * We need at least one entry each in order to initialize a keyboard 91 * for the kernel console. The arrays will be increased dynamically 92 * when necessary. 93 */ 94 95 static int keyboards = 1; 96 static keyboard_t *kbd_ini; 97 static keyboard_t **keyboard = &kbd_ini; 98 static keyboard_switch_t *kbdsw_ini; 99 keyboard_switch_t **kbdsw = &kbdsw_ini; 100 101 #define ARRAY_DELTA 4 102 103 static int 104 kbd_realloc_array(void) 105 { 106 keyboard_t **new_kbd; 107 keyboard_switch_t **new_kbdsw; 108 int newsize; 109 110 lwkt_gettoken(&tty_token); 111 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA; 112 new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF, 113 M_WAITOK | M_ZERO); 114 new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF, 115 M_WAITOK | M_ZERO); 116 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards); 117 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards); 118 crit_enter(); 119 if (keyboards > 1) { 120 kfree(keyboard, M_DEVBUF); 121 kfree(kbdsw, M_DEVBUF); 122 } 123 keyboard = new_kbd; 124 kbdsw = new_kbdsw; 125 keyboards = newsize; 126 crit_exit(); 127 128 if (bootverbose) 129 kprintf("kbd: new array size %d\n", keyboards); 130 131 lwkt_reltoken(&tty_token); 132 return 0; 133 } 134 135 /* 136 * Low-level keyboard driver functions. 137 * 138 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard 139 * driver, call these functions to initialize the keyboard_t structure 140 * and register it to the virtual keyboard driver `kbd'. 141 * 142 * The reinit call is made when a driver has partially detached a keyboard 143 * but does not unregistered it, then wishes to reinitialize it later on. 144 * This is how the USB keyboard driver handles the 'default' keyboard, 145 * because unregistering the keyboard associated with the console will 146 * destroy its console association forever. 147 */ 148 void 149 kbd_reinit_struct(keyboard_t *kbd, int config, int pref) 150 { 151 lwkt_gettoken(&tty_token); 152 kbd->kb_flags |= KB_NO_DEVICE; /* device has not been found */ 153 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY; 154 kbd->kb_led = 0; /* unknown */ 155 kbd->kb_data = NULL; 156 kbd->kb_keymap = NULL; 157 kbd->kb_accentmap = NULL; 158 kbd->kb_fkeytab = NULL; 159 kbd->kb_fkeytab_size = 0; 160 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */ 161 kbd->kb_delay2 = KB_DELAY2; 162 kbd->kb_count = 0; 163 kbd->kb_pref = pref; 164 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact)); 165 lwkt_reltoken(&tty_token); 166 } 167 168 /* initialize the keyboard_t structure */ 169 void 170 kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config, 171 int pref, int port, int port_size) 172 { 173 lwkt_gettoken(&tty_token); 174 kbd->kb_flags = 0; 175 kbd->kb_name = name; 176 kbd->kb_type = type; 177 kbd->kb_unit = unit; 178 kbd->kb_io_base = port; 179 kbd->kb_io_size = port_size; 180 kbd_reinit_struct(kbd, config, pref); 181 lockinit(&kbd->kb_lock, name, 0, LK_CANRECURSE); 182 lwkt_reltoken(&tty_token); 183 } 184 185 void 186 kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap, 187 fkeytab_t *fkeymap, int fkeymap_size) 188 { 189 lwkt_gettoken(&tty_token); 190 kbd->kb_keymap = keymap; 191 kbd->kb_accentmap = accmap; 192 kbd->kb_fkeytab = fkeymap; 193 kbd->kb_fkeytab_size = fkeymap_size; 194 lwkt_reltoken(&tty_token); 195 } 196 197 /* declare a new keyboard driver */ 198 int 199 kbd_add_driver(keyboard_driver_t *driver) 200 { 201 lwkt_gettoken(&tty_token); 202 if (SLIST_NEXT(driver, link)) { 203 lwkt_reltoken(&tty_token); 204 return EINVAL; 205 } 206 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link); 207 lwkt_reltoken(&tty_token); 208 return 0; 209 } 210 211 int 212 kbd_delete_driver(keyboard_driver_t *driver) 213 { 214 lwkt_gettoken(&tty_token); 215 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link); 216 SLIST_NEXT(driver, link) = NULL; 217 lwkt_reltoken(&tty_token); 218 return 0; 219 } 220 221 /* register a keyboard and associate it with a function table */ 222 int 223 kbd_register(keyboard_t *kbd) 224 { 225 const keyboard_driver_t **list; 226 const keyboard_driver_t *p; 227 keyboard_t *mux; 228 keyboard_info_t ki; 229 int index; 230 231 lwkt_gettoken(&tty_token); 232 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1)); 233 234 for (index = 0; index < keyboards; ++index) { 235 if (keyboard[index] == NULL) 236 break; 237 } 238 if (index >= keyboards) { 239 if (kbd_realloc_array()) { 240 lwkt_reltoken(&tty_token); 241 return -1; 242 } 243 } 244 245 kbd->kb_index = index; 246 KBD_UNBUSY(kbd); 247 KBD_VALID(kbd); 248 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */ 249 kbd->kb_token = NULL; 250 kbd->kb_callback.kc_func = NULL; 251 kbd->kb_callback.kc_arg = NULL; 252 callout_init_mp(&kbd->kb_atkbd_timeout_ch); 253 254 SLIST_FOREACH(p, &keyboard_drivers, link) { 255 if (strcmp(p->name, kbd->kb_name) == 0) { 256 keyboard[index] = kbd; 257 kbdsw[index] = p->kbdsw; 258 259 if (mux != NULL) { 260 bzero(&ki, sizeof(ki)); 261 strcpy(ki.kb_name, kbd->kb_name); 262 ki.kb_unit = kbd->kb_unit; 263 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki); 264 } 265 266 lwkt_reltoken(&tty_token); 267 return index; 268 } 269 } 270 SET_FOREACH(list, kbddriver_set) { 271 p = *list; 272 if (strcmp(p->name, kbd->kb_name) == 0) { 273 keyboard[index] = kbd; 274 kbdsw[index] = p->kbdsw; 275 276 if (mux != NULL) { 277 bzero(&ki, sizeof(ki)); 278 strcpy(ki.kb_name, kbd->kb_name); 279 ki.kb_unit = kbd->kb_unit; 280 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki); 281 } 282 283 lwkt_reltoken(&tty_token); 284 return index; 285 } 286 } 287 288 lwkt_reltoken(&tty_token); 289 return -1; 290 } 291 292 int 293 kbd_unregister(keyboard_t *kbd) 294 { 295 int error; 296 297 lwkt_gettoken(&tty_token); 298 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards)) { 299 lwkt_reltoken(&tty_token); 300 return ENOENT; 301 } 302 if (keyboard[kbd->kb_index] != kbd) { 303 lwkt_reltoken(&tty_token); 304 return ENOENT; 305 } 306 307 crit_enter(); 308 callout_stop(&kbd->kb_atkbd_timeout_ch); 309 if (KBD_IS_BUSY(kbd)) { 310 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING, 311 kbd->kb_callback.kc_arg); 312 if (error) { 313 crit_exit(); 314 lwkt_reltoken(&tty_token); 315 return error; 316 } 317 if (KBD_IS_BUSY(kbd)) { 318 crit_exit(); 319 lwkt_reltoken(&tty_token); 320 return EBUSY; 321 } 322 } 323 KBD_CONFIG_LOST(kbd); 324 KBD_INVALID(kbd); 325 keyboard[kbd->kb_index] = NULL; 326 kbdsw[kbd->kb_index] = NULL; 327 328 KBD_ALWAYS_UNLOCK(kbd); 329 lockuninit(&kbd->kb_lock); 330 331 crit_exit(); 332 lwkt_reltoken(&tty_token); 333 return 0; 334 } 335 336 /* find a funciton table by the driver name */ 337 keyboard_switch_t * 338 kbd_get_switch(char *driver) 339 { 340 const keyboard_driver_t **list; 341 const keyboard_driver_t *p; 342 343 lwkt_gettoken(&tty_token); 344 345 SLIST_FOREACH(p, &keyboard_drivers, link) { 346 if (strcmp(p->name, driver) == 0) { 347 lwkt_reltoken(&tty_token); 348 return p->kbdsw; 349 } 350 } 351 SET_FOREACH(list, kbddriver_set) { 352 p = *list; 353 if (strcmp(p->name, driver) == 0) { 354 lwkt_reltoken(&tty_token); 355 return p->kbdsw; 356 } 357 } 358 359 lwkt_reltoken(&tty_token); 360 return NULL; 361 } 362 363 /* 364 * Keyboard client functions 365 * Keyboard clients, such as the console driver `syscons' and the keyboard 366 * cdev driver, use these functions to claim and release a keyboard for 367 * exclusive use. 368 */ 369 /* 370 * find the keyboard specified by a driver name and a unit number 371 * starting at given index 372 */ 373 int 374 kbd_find_keyboard2(char *driver, int unit, int index, int legacy) 375 { 376 int i; 377 int pref; 378 int pref_index; 379 380 pref = 0; 381 pref_index = -1; 382 383 lwkt_gettoken(&tty_token); 384 if ((index < 0) || (index >= keyboards)) { 385 lwkt_reltoken(&tty_token); 386 return (-1); 387 } 388 389 for (i = index; i < keyboards; ++i) { 390 if (keyboard[i] == NULL) 391 continue; 392 if (!KBD_IS_VALID(keyboard[i])) 393 continue; 394 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver)) 395 continue; 396 if ((unit != -1) && (keyboard[i]->kb_unit != unit)) 397 continue; 398 /* 399 * If we are in legacy mode, we do the old preference magic and 400 * don't return on the first found unit. 401 */ 402 if (legacy) { 403 if (pref <= keyboard[i]->kb_pref) { 404 pref = keyboard[i]->kb_pref; 405 pref_index = i; 406 } 407 } else { 408 lwkt_reltoken(&tty_token); 409 return i; 410 } 411 } 412 413 if (!legacy) 414 KKASSERT(pref_index == -1); 415 416 lwkt_reltoken(&tty_token); 417 return (pref_index); 418 } 419 420 /* find the keyboard specified by a driver name and a unit number */ 421 int 422 kbd_find_keyboard(char *driver, int unit) 423 { 424 return (kbd_find_keyboard2(driver, unit, 0, 1)); 425 } 426 427 /* allocate a keyboard */ 428 int 429 kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func, 430 void *arg) 431 { 432 int index; 433 434 if (func == NULL) 435 return -1; 436 437 crit_enter(); 438 lwkt_gettoken(&tty_token); 439 440 index = kbd_find_keyboard(driver, unit); 441 if (index >= 0) { 442 if (KBD_IS_BUSY(keyboard[index])) { 443 crit_exit(); 444 lwkt_reltoken(&tty_token); 445 return -1; 446 } 447 keyboard[index]->kb_token = id; 448 KBD_BUSY(keyboard[index]); 449 keyboard[index]->kb_callback.kc_func = func; 450 keyboard[index]->kb_callback.kc_arg = arg; 451 kbd_clear_state(keyboard[index]); 452 } 453 454 lwkt_reltoken(&tty_token); 455 crit_exit(); 456 return index; 457 } 458 459 int 460 kbd_release(keyboard_t *kbd, void *id) 461 { 462 int error; 463 464 crit_enter(); 465 lwkt_gettoken(&tty_token); 466 467 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) { 468 error = EINVAL; 469 } else if (kbd->kb_token != id) { 470 error = EPERM; 471 } else { 472 kbd->kb_token = NULL; 473 KBD_UNBUSY(kbd); 474 kbd->kb_callback.kc_func = NULL; 475 kbd->kb_callback.kc_arg = NULL; 476 kbd_clear_state(kbd); 477 error = 0; 478 } 479 480 lwkt_reltoken(&tty_token); 481 crit_exit(); 482 return error; 483 } 484 485 int 486 kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func, 487 void *arg) 488 { 489 int error; 490 491 crit_enter(); 492 lwkt_gettoken(&tty_token); 493 494 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) { 495 error = EINVAL; 496 } else if (kbd->kb_token != id) { 497 error = EPERM; 498 } else if (func == NULL) { 499 error = EINVAL; 500 } else { 501 kbd->kb_callback.kc_func = func; 502 kbd->kb_callback.kc_arg = arg; 503 error = 0; 504 } 505 506 lwkt_reltoken(&tty_token); 507 crit_exit(); 508 return error; 509 } 510 511 /* get a keyboard structure */ 512 keyboard_t * 513 kbd_get_keyboard(int index) 514 { 515 keyboard_t *kbd; 516 517 lwkt_gettoken(&tty_token); 518 if ((index < 0) || (index >= keyboards)) { 519 lwkt_reltoken(&tty_token); 520 return NULL; 521 } 522 if (keyboard[index] == NULL) { 523 lwkt_reltoken(&tty_token); 524 return NULL; 525 } 526 if (!KBD_IS_VALID(keyboard[index])) { 527 lwkt_reltoken(&tty_token); 528 return NULL; 529 } 530 kbd = keyboard[index]; 531 lwkt_reltoken(&tty_token); 532 533 return kbd; 534 } 535 536 /* 537 * The back door for the console driver; configure keyboards 538 * This function is for the kernel console to initialize keyboards 539 * at very early stage. 540 */ 541 542 int 543 kbd_configure(int flags) 544 { 545 const keyboard_driver_t **list; 546 const keyboard_driver_t *p; 547 548 lwkt_gettoken(&tty_token); 549 550 SLIST_FOREACH(p, &keyboard_drivers, link) { 551 if (p->configure != NULL) 552 (*p->configure)(flags); 553 } 554 SET_FOREACH(list, kbddriver_set) { 555 p = *list; 556 if (p->configure != NULL) 557 (*p->configure)(flags); 558 } 559 560 lwkt_reltoken(&tty_token); 561 return 0; 562 } 563 564 #ifdef KBD_INSTALL_CDEV 565 566 /* 567 * Virtual keyboard cdev driver functions 568 * The virtual keyboard driver dispatches driver functions to 569 * appropriate subdrivers. 570 */ 571 572 #define KBD_UNIT(dev) minor(dev) 573 574 static d_open_t genkbdopen; 575 static d_close_t genkbdclose; 576 static d_read_t genkbdread; 577 static d_write_t genkbdwrite; 578 static d_ioctl_t genkbdioctl; 579 static d_kqfilter_t genkbdkqfilter; 580 581 static void genkbdfiltdetach(struct knote *); 582 static int genkbdfilter(struct knote *, long); 583 584 #define CDEV_MAJOR 112 585 586 static struct dev_ops kbd_ops = { 587 { "kbd", CDEV_MAJOR, 0 }, 588 .d_open = genkbdopen, 589 .d_close = genkbdclose, 590 .d_read = genkbdread, 591 .d_write = genkbdwrite, 592 .d_ioctl = genkbdioctl, 593 .d_kqfilter = genkbdkqfilter 594 }; 595 596 /* 597 * Attach a keyboard. 598 * 599 * NOTE: The usb driver does not detach the default keyboard if it is 600 * unplugged, but calls kbd_attach() when it is plugged back in. 601 */ 602 int 603 kbd_attach(keyboard_t *kbd) 604 { 605 cdev_t dev; 606 607 lwkt_gettoken(&tty_token); 608 if (kbd->kb_index >= keyboards) { 609 lwkt_reltoken(&tty_token); 610 return EINVAL; 611 } 612 if (keyboard[kbd->kb_index] != kbd) { 613 lwkt_reltoken(&tty_token); 614 return EINVAL; 615 } 616 617 if (kbd->kb_dev == NULL) { 618 kbd->kb_dev = make_dev(&kbd_ops, kbd->kb_index, 619 UID_ROOT, GID_WHEEL, 0600, 620 "kbd%r", kbd->kb_index); 621 } 622 dev = kbd->kb_dev; 623 if (dev->si_drv1 == NULL) { 624 dev->si_drv1 = kmalloc(sizeof(struct genkbd_softc), M_DEVBUF, 625 M_WAITOK); 626 } 627 bzero(dev->si_drv1, sizeof(struct genkbd_softc)); 628 629 kprintf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit); 630 lwkt_reltoken(&tty_token); 631 return 0; 632 } 633 634 int 635 kbd_detach(keyboard_t *kbd) 636 { 637 cdev_t dev; 638 639 lwkt_gettoken(&tty_token); 640 641 if (kbd->kb_index >= keyboards) { 642 lwkt_reltoken(&tty_token); 643 return EINVAL; 644 } 645 if (keyboard[kbd->kb_index] != kbd) { 646 lwkt_reltoken(&tty_token); 647 return EINVAL; 648 } 649 650 if ((dev = kbd->kb_dev) != NULL) { 651 if (dev->si_drv1) { 652 kfree(dev->si_drv1, M_DEVBUF); 653 dev->si_drv1 = NULL; 654 } 655 kbd->kb_dev = NULL; 656 } 657 dev_ops_remove_minor(&kbd_ops, kbd->kb_index); 658 lwkt_reltoken(&tty_token); 659 return 0; 660 } 661 662 /* 663 * Generic keyboard cdev driver functions 664 * Keyboard subdrivers may call these functions to implement common 665 * driver functions. 666 */ 667 668 static void 669 genkbd_putc(genkbd_softc_t sc, char c) 670 { 671 unsigned int p; 672 673 lwkt_gettoken(&tty_token); 674 675 if (sc->gkb_q_length == KB_QSIZE) { 676 lwkt_reltoken(&tty_token); 677 return; 678 } 679 680 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE; 681 sc->gkb_q[p] = c; 682 sc->gkb_q_length++; 683 684 lwkt_reltoken(&tty_token); 685 } 686 687 static size_t 688 genkbd_getc(genkbd_softc_t sc, char *buf, size_t len) 689 { 690 691 lwkt_gettoken(&tty_token); 692 693 /* Determine copy size. */ 694 if (sc->gkb_q_length == 0) { 695 lwkt_reltoken(&tty_token); 696 return (0); 697 } 698 if (len >= sc->gkb_q_length) 699 len = sc->gkb_q_length; 700 if (len >= KB_QSIZE - sc->gkb_q_start) 701 len = KB_QSIZE - sc->gkb_q_start; 702 703 /* Copy out data and progress offset. */ 704 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len); 705 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE; 706 sc->gkb_q_length -= len; 707 708 lwkt_reltoken(&tty_token); 709 return (len); 710 } 711 712 static kbd_callback_func_t genkbd_event; 713 714 static int 715 genkbdopen(struct dev_open_args *ap) 716 { 717 cdev_t dev = ap->a_head.a_dev; 718 keyboard_t *kbd; 719 genkbd_softc_t sc; 720 int i; 721 722 crit_enter(); 723 lwkt_gettoken(&tty_token); 724 sc = dev->si_drv1; 725 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 726 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { 727 lwkt_reltoken(&tty_token); 728 crit_exit(); 729 return ENXIO; 730 } 731 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc, 732 genkbd_event, (void *)sc); 733 if (i < 0) { 734 lwkt_reltoken(&tty_token); 735 crit_exit(); 736 return EBUSY; 737 } 738 /* assert(i == kbd->kb_index) */ 739 /* assert(kbd == kbd_get_keyboard(i)) */ 740 741 /* 742 * NOTE: even when we have successfully claimed a keyboard, 743 * the device may still be missing (!KBD_HAS_DEVICE(kbd)). 744 */ 745 746 sc->gkb_q_length = 0; 747 lwkt_reltoken(&tty_token); 748 crit_exit(); 749 750 return 0; 751 } 752 753 static int 754 genkbdclose(struct dev_close_args *ap) 755 { 756 cdev_t dev = ap->a_head.a_dev; 757 keyboard_t *kbd; 758 genkbd_softc_t sc; 759 760 /* 761 * NOTE: the device may have already become invalid. 762 * kbd == NULL || !KBD_IS_VALID(kbd) 763 */ 764 crit_enter(); 765 lwkt_gettoken(&tty_token); 766 sc = dev->si_drv1; 767 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 768 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { 769 /* XXX: we shall be forgiving and don't report error... */ 770 } else { 771 kbd_release(kbd, (void *)sc); 772 } 773 lwkt_reltoken(&tty_token); 774 crit_exit(); 775 return 0; 776 } 777 778 static int 779 genkbdread(struct dev_read_args *ap) 780 { 781 cdev_t dev = ap->a_head.a_dev; 782 struct uio *uio = ap->a_uio; 783 keyboard_t *kbd; 784 genkbd_softc_t sc; 785 u_char buffer[KB_BUFSIZE]; 786 int len; 787 int error; 788 789 /* wait for input */ 790 crit_enter(); 791 lwkt_gettoken(&tty_token); 792 sc = dev->si_drv1; 793 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 794 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { 795 lwkt_reltoken(&tty_token); 796 crit_exit(); 797 return ENXIO; 798 } 799 while (sc->gkb_q_length == 0) { 800 if (ap->a_ioflag & IO_NDELAY) { /* O_NONBLOCK? */ 801 lwkt_reltoken(&tty_token); 802 crit_exit(); 803 return EWOULDBLOCK; 804 } 805 sc->gkb_flags |= KB_ASLEEP; 806 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0); 807 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 808 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) { 809 lwkt_reltoken(&tty_token); 810 crit_exit(); 811 return ENXIO; /* our keyboard has gone... */ 812 } 813 if (error) { 814 sc->gkb_flags &= ~KB_ASLEEP; 815 lwkt_reltoken(&tty_token); 816 crit_exit(); 817 return error; 818 } 819 } 820 lwkt_reltoken(&tty_token); 821 crit_exit(); 822 823 /* copy as much input as possible */ 824 error = 0; 825 while (uio->uio_resid > 0) { 826 len = (int)szmin(uio->uio_resid, sizeof(buffer)); 827 len = genkbd_getc(sc, buffer, len); 828 if (len <= 0) 829 break; 830 error = uiomove(buffer, (size_t)len, uio); 831 if (error) 832 break; 833 } 834 835 return error; 836 } 837 838 static int 839 genkbdwrite(struct dev_write_args *ap) 840 { 841 cdev_t dev = ap->a_head.a_dev; 842 keyboard_t *kbd; 843 844 lwkt_gettoken(&tty_token); 845 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 846 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) { 847 lwkt_reltoken(&tty_token); 848 return ENXIO; 849 } 850 lwkt_reltoken(&tty_token); 851 return ENODEV; 852 } 853 854 static int 855 genkbdioctl(struct dev_ioctl_args *ap) 856 { 857 cdev_t dev = ap->a_head.a_dev; 858 keyboard_t *kbd; 859 int error; 860 861 lwkt_gettoken(&tty_token); 862 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 863 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) { 864 lwkt_reltoken(&tty_token); 865 return ENXIO; 866 } 867 error = kbd_ioctl(kbd, ap->a_cmd, ap->a_data); 868 if (error == ENOIOCTL) 869 error = ENODEV; 870 871 lwkt_reltoken(&tty_token); 872 return error; 873 } 874 875 static struct filterops genkbdfiltops = 876 { FILTEROP_ISFD, NULL, genkbdfiltdetach, genkbdfilter }; 877 878 static int 879 genkbdkqfilter(struct dev_kqfilter_args *ap) 880 { 881 cdev_t dev = ap->a_head.a_dev; 882 struct knote *kn = ap->a_kn; 883 genkbd_softc_t sc; 884 struct klist *klist; 885 886 ap->a_result = 0; 887 888 switch (kn->kn_filter) { 889 case EVFILT_READ: 890 kn->kn_fop = &genkbdfiltops; 891 kn->kn_hook = (caddr_t)dev; 892 break; 893 default: 894 ap->a_result = EOPNOTSUPP; 895 return (0); 896 } 897 898 sc = dev->si_drv1; 899 klist = &sc->gkb_rkq.ki_note; 900 knote_insert(klist, kn); 901 902 return (0); 903 } 904 905 static void 906 genkbdfiltdetach(struct knote *kn) 907 { 908 cdev_t dev = (cdev_t)kn->kn_hook; 909 genkbd_softc_t sc; 910 struct klist *klist; 911 912 sc = dev->si_drv1; 913 klist = &sc->gkb_rkq.ki_note; 914 knote_remove(klist, kn); 915 } 916 917 static int 918 genkbdfilter(struct knote *kn, long hint) 919 { 920 cdev_t dev = (cdev_t)kn->kn_hook; 921 keyboard_t *kbd; 922 genkbd_softc_t sc; 923 int ready = 0; 924 925 crit_enter(); 926 lwkt_gettoken(&tty_token); 927 sc = dev->si_drv1; 928 kbd = kbd_get_keyboard(KBD_INDEX(dev)); 929 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) { 930 kn->kn_flags |= EV_EOF; /* the keyboard has gone */ 931 ready = 1; 932 } else { 933 if (sc->gkb_q_length > 0) 934 ready = 1; 935 } 936 lwkt_reltoken(&tty_token); 937 crit_exit(); 938 939 return (ready); 940 } 941 942 static int 943 genkbd_event(keyboard_t *kbd, int event, void *arg) 944 { 945 genkbd_softc_t sc; 946 size_t len; 947 u_char *cp; 948 int mode; 949 int c; 950 951 lwkt_gettoken(&tty_token); 952 /* assert(KBD_IS_VALID(kbd)) */ 953 sc = (genkbd_softc_t)arg; 954 955 switch (event) { 956 case KBDIO_KEYINPUT: 957 break; 958 case KBDIO_UNLOADING: 959 /* the keyboard is going... */ 960 kbd_release(kbd, (void *)sc); 961 if (sc->gkb_flags & KB_ASLEEP) { 962 sc->gkb_flags &= ~KB_ASLEEP; 963 wakeup((caddr_t)sc); 964 } 965 KNOTE(&sc->gkb_rkq.ki_note, 0); 966 lwkt_reltoken(&tty_token); 967 return 0; 968 default: 969 lwkt_reltoken(&tty_token); 970 return EINVAL; 971 } 972 973 /* obtain the current key input mode */ 974 if (kbd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode)) 975 mode = K_XLATE; 976 977 /* read all pending input */ 978 while (kbd_check_char(kbd)) { 979 c = kbd_read_char(kbd, FALSE); 980 if (c == NOKEY) 981 continue; 982 if (c == ERRKEY) /* XXX: ring bell? */ 983 continue; 984 if (!KBD_IS_BUSY(kbd)) 985 /* the device is not open, discard the input */ 986 continue; 987 988 /* store the byte as is for K_RAW and K_CODE modes */ 989 if (mode != K_XLATE) { 990 genkbd_putc(sc, KEYCHAR(c)); 991 continue; 992 } 993 994 /* K_XLATE */ 995 if (c & RELKEY) /* key release is ignored */ 996 continue; 997 998 /* process special keys; most of them are just ignored... */ 999 if (c & SPCLKEY) { 1000 switch (KEYCHAR(c)) { 1001 default: 1002 /* ignore them... */ 1003 continue; 1004 case BTAB: /* a backtab: ESC [ Z */ 1005 genkbd_putc(sc, 0x1b); 1006 genkbd_putc(sc, '['); 1007 genkbd_putc(sc, 'Z'); 1008 continue; 1009 } 1010 } 1011 1012 /* normal chars, normal chars with the META, function keys */ 1013 switch (KEYFLAGS(c)) { 1014 case 0: /* a normal char */ 1015 genkbd_putc(sc, KEYCHAR(c)); 1016 break; 1017 case MKEY: /* the META flag: prepend ESC */ 1018 genkbd_putc(sc, 0x1b); 1019 genkbd_putc(sc, KEYCHAR(c)); 1020 break; 1021 case FKEY | SPCLKEY: /* a function key, return string */ 1022 cp = kbd_get_fkeystr(kbd, KEYCHAR(c), &len); 1023 if (cp != NULL) { 1024 while (len-- > 0) 1025 genkbd_putc(sc, *cp++); 1026 } 1027 break; 1028 } 1029 } 1030 1031 /* wake up sleeping/polling processes */ 1032 if (sc->gkb_q_length > 0) { 1033 if (sc->gkb_flags & KB_ASLEEP) { 1034 sc->gkb_flags &= ~KB_ASLEEP; 1035 wakeup((caddr_t)sc); 1036 } 1037 KNOTE(&sc->gkb_rkq.ki_note, 0); 1038 } 1039 1040 lwkt_reltoken(&tty_token); 1041 return 0; 1042 } 1043 1044 #endif /* KBD_INSTALL_CDEV */ 1045 1046 /* 1047 * Generic low-level keyboard functions 1048 * The low-level functions in the keyboard subdriver may use these 1049 * functions. 1050 */ 1051 1052 int 1053 genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1054 { 1055 keyarg_t *keyp; 1056 fkeyarg_t *fkeyp; 1057 int i; 1058 1059 crit_enter(); 1060 lwkt_gettoken(&tty_token); 1061 switch (cmd) { 1062 1063 case KDGKBINFO: /* get keyboard information */ 1064 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index; 1065 i = imin(strlen(kbd->kb_name) + 1, 1066 sizeof(((keyboard_info_t *)arg)->kb_name)); 1067 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i); 1068 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit; 1069 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type; 1070 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config; 1071 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags; 1072 break; 1073 1074 case KDGKBTYPE: /* get keyboard type */ 1075 *(int *)arg = kbd->kb_type; 1076 break; 1077 1078 case KDGETREPEAT: /* get keyboard repeat rate */ 1079 ((int *)arg)[0] = kbd->kb_delay1; 1080 ((int *)arg)[1] = kbd->kb_delay2; 1081 break; 1082 1083 case GIO_KEYMAP: /* get keyboard translation table */ 1084 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap)); 1085 break; 1086 case PIO_KEYMAP: /* set keyboard translation table */ 1087 #ifndef KBD_DISABLE_KEYMAP_LOAD 1088 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap)); 1089 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap)); 1090 break; 1091 #else 1092 lwkt_reltoken(&tty_token); 1093 crit_exit(); 1094 return ENODEV; 1095 #endif 1096 1097 case GIO_KEYMAPENT: /* get keyboard translation table entry */ 1098 keyp = (keyarg_t *)arg; 1099 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) 1100 /sizeof(kbd->kb_keymap->key[0])) { 1101 lwkt_reltoken(&tty_token); 1102 crit_exit(); 1103 return EINVAL; 1104 } 1105 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key, 1106 sizeof(keyp->key)); 1107 break; 1108 case PIO_KEYMAPENT: /* set keyboard translation table entry */ 1109 #ifndef KBD_DISABLE_KEYMAP_LOAD 1110 keyp = (keyarg_t *)arg; 1111 if (keyp->keynum >= sizeof(kbd->kb_keymap->key) 1112 /sizeof(kbd->kb_keymap->key[0])) { 1113 lwkt_reltoken(&tty_token); 1114 crit_exit(); 1115 return EINVAL; 1116 } 1117 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum], 1118 sizeof(keyp->key)); 1119 break; 1120 #else 1121 lwkt_reltoken(&tty_token); 1122 crit_exit(); 1123 return ENODEV; 1124 #endif 1125 1126 case GIO_DEADKEYMAP: /* get accent key translation table */ 1127 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap)); 1128 break; 1129 case PIO_DEADKEYMAP: /* set accent key translation table */ 1130 #ifndef KBD_DISABLE_KEYMAP_LOAD 1131 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap)); 1132 break; 1133 #else 1134 lwkt_reltoken(&tty_token); 1135 crit_exit(); 1136 return ENODEV; 1137 #endif 1138 1139 case GETFKEY: /* get functionkey string */ 1140 fkeyp = (fkeyarg_t *)arg; 1141 if (fkeyp->keynum >= kbd->kb_fkeytab_size) { 1142 lwkt_reltoken(&tty_token); 1143 crit_exit(); 1144 return EINVAL; 1145 } 1146 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef, 1147 kbd->kb_fkeytab[fkeyp->keynum].len); 1148 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len; 1149 break; 1150 case SETFKEY: /* set functionkey string */ 1151 #ifndef KBD_DISABLE_KEYMAP_LOAD 1152 fkeyp = (fkeyarg_t *)arg; 1153 if (fkeyp->keynum >= kbd->kb_fkeytab_size) { 1154 lwkt_reltoken(&tty_token); 1155 crit_exit(); 1156 return EINVAL; 1157 } 1158 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK); 1159 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str, 1160 kbd->kb_fkeytab[fkeyp->keynum].len); 1161 break; 1162 #else 1163 lwkt_reltoken(&tty_token); 1164 crit_exit(); 1165 return ENODEV; 1166 #endif 1167 1168 default: 1169 lwkt_reltoken(&tty_token); 1170 crit_exit(); 1171 return ENOIOCTL; 1172 } 1173 1174 lwkt_reltoken(&tty_token); 1175 crit_exit(); 1176 return 0; 1177 } 1178 1179 /* get a pointer to the string associated with the given function key */ 1180 u_char * 1181 genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len) 1182 { 1183 u_char *ch; 1184 1185 if (kbd == NULL) 1186 return NULL; 1187 1188 lwkt_gettoken(&tty_token); 1189 fkey -= F_FN; 1190 if (fkey > kbd->kb_fkeytab_size) { 1191 lwkt_reltoken(&tty_token); 1192 return NULL; 1193 } 1194 *len = kbd->kb_fkeytab[fkey].len; 1195 ch = kbd->kb_fkeytab[fkey].str; 1196 1197 lwkt_reltoken(&tty_token); 1198 return ch; 1199 } 1200 1201 /* diagnostic dump */ 1202 static char * 1203 get_kbd_type_name(int type) 1204 { 1205 static struct { 1206 int type; 1207 char *name; 1208 } name_table[] = { 1209 { KB_84, "AT 84" }, 1210 { KB_101, "AT 101/102" }, 1211 { KB_OTHER, "generic" }, 1212 }; 1213 int i; 1214 1215 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) { 1216 if (type == name_table[i].type) 1217 return name_table[i].name; 1218 } 1219 return "unknown"; 1220 } 1221 1222 void 1223 genkbd_diag(keyboard_t *kbd, int level) 1224 { 1225 if (level > 0) { 1226 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x", 1227 kbd->kb_index, kbd->kb_name, kbd->kb_unit, 1228 get_kbd_type_name(kbd->kb_type), kbd->kb_type, 1229 kbd->kb_config, kbd->kb_flags); 1230 if (kbd->kb_io_base > 0) 1231 kprintf(", port:0x%x-0x%x", kbd->kb_io_base, 1232 kbd->kb_io_base + kbd->kb_io_size - 1); 1233 kprintf("\n"); 1234 } 1235 } 1236 1237 #define set_lockkey_state(k, s, l) \ 1238 if (!((s) & l ## DOWN)) { \ 1239 int i; \ 1240 (s) |= l ## DOWN; \ 1241 (s) ^= l ## ED; \ 1242 i = (s) & LOCK_MASK; \ 1243 kbd_ioctl((k), KDSETLED, (caddr_t)&i); \ 1244 } 1245 1246 static u_int 1247 save_accent_key(keyboard_t *kbd, u_int key, int *accents) 1248 { 1249 int i; 1250 1251 lwkt_gettoken(&tty_token); 1252 /* make an index into the accent map */ 1253 i = key - F_ACC + 1; 1254 if ((i > kbd->kb_accentmap->n_accs) 1255 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) { 1256 /* the index is out of range or pointing to an empty entry */ 1257 *accents = 0; 1258 lwkt_reltoken(&tty_token); 1259 return ERRKEY; 1260 } 1261 1262 /* 1263 * If the same accent key has been hit twice, produce the accent char 1264 * itself. 1265 */ 1266 if (i == *accents) { 1267 key = kbd->kb_accentmap->acc[i - 1].accchar; 1268 *accents = 0; 1269 lwkt_reltoken(&tty_token); 1270 return key; 1271 } 1272 1273 /* remember the index and wait for the next key */ 1274 *accents = i; 1275 lwkt_reltoken(&tty_token); 1276 return NOKEY; 1277 } 1278 1279 static u_int 1280 make_accent_char(keyboard_t *kbd, u_int ch, int *accents) 1281 { 1282 struct acc_t *acc; 1283 int i; 1284 1285 lwkt_gettoken(&tty_token); 1286 acc = &kbd->kb_accentmap->acc[*accents - 1]; 1287 *accents = 0; 1288 1289 /* 1290 * If the accent key is followed by the space key, 1291 * produce the accent char itself. 1292 */ 1293 if (ch == ' ') { 1294 lwkt_reltoken(&tty_token); 1295 return acc->accchar; 1296 } 1297 1298 /* scan the accent map */ 1299 for (i = 0; i < NUM_ACCENTCHARS; ++i) { 1300 if (acc->map[i][0] == 0) /* end of table */ 1301 break; 1302 if (acc->map[i][0] == ch) { 1303 lwkt_reltoken(&tty_token); 1304 return acc->map[i][1]; 1305 } 1306 } 1307 lwkt_reltoken(&tty_token); 1308 /* this char cannot be accented... */ 1309 return ERRKEY; 1310 } 1311 1312 int 1313 genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate, 1314 int *accents) 1315 { 1316 struct keyent_t *key; 1317 int state = *shiftstate; 1318 int action; 1319 int f; 1320 int i; 1321 1322 lwkt_gettoken(&tty_token); 1323 i = keycode; 1324 f = state & (AGRS | ALKED); 1325 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED)) 1326 i += ALTGR_OFFSET; 1327 key = &kbd->kb_keymap->key[i]; 1328 i = ((state & SHIFTS) ? 1 : 0) 1329 | ((state & CTLS) ? 2 : 0) 1330 | ((state & ALTS) ? 4 : 0); 1331 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED)) 1332 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) ) 1333 i ^= 1; 1334 1335 if (up) { /* break: key released */ 1336 action = kbd->kb_lastact[keycode]; 1337 kbd->kb_lastact[keycode] = NOP; 1338 switch (action) { 1339 case LSHA: 1340 if (state & SHIFTAON) { 1341 set_lockkey_state(kbd, state, ALK); 1342 state &= ~ALKDOWN; 1343 } 1344 action = LSH; 1345 /* FALL THROUGH */ 1346 case LSH: 1347 state &= ~SHIFTS1; 1348 break; 1349 case RSHA: 1350 if (state & SHIFTAON) { 1351 set_lockkey_state(kbd, state, ALK); 1352 state &= ~ALKDOWN; 1353 } 1354 action = RSH; 1355 /* FALL THROUGH */ 1356 case RSH: 1357 state &= ~SHIFTS2; 1358 break; 1359 case LCTRA: 1360 if (state & SHIFTAON) { 1361 set_lockkey_state(kbd, state, ALK); 1362 state &= ~ALKDOWN; 1363 } 1364 action = LCTR; 1365 /* FALL THROUGH */ 1366 case LCTR: 1367 state &= ~CTLS1; 1368 break; 1369 case RCTRA: 1370 if (state & SHIFTAON) { 1371 set_lockkey_state(kbd, state, ALK); 1372 state &= ~ALKDOWN; 1373 } 1374 action = RCTR; 1375 /* FALL THROUGH */ 1376 case RCTR: 1377 state &= ~CTLS2; 1378 break; 1379 case LALTA: 1380 if (state & SHIFTAON) { 1381 set_lockkey_state(kbd, state, ALK); 1382 state &= ~ALKDOWN; 1383 } 1384 action = LALT; 1385 /* FALL THROUGH */ 1386 case LALT: 1387 state &= ~ALTS1; 1388 break; 1389 case RALTA: 1390 if (state & SHIFTAON) { 1391 set_lockkey_state(kbd, state, ALK); 1392 state &= ~ALKDOWN; 1393 } 1394 action = RALT; 1395 /* FALL THROUGH */ 1396 case RALT: 1397 state &= ~ALTS2; 1398 break; 1399 case ASH: 1400 state &= ~AGRS1; 1401 break; 1402 case META: 1403 state &= ~METAS1; 1404 break; 1405 case NLK: 1406 state &= ~NLKDOWN; 1407 break; 1408 case CLK: 1409 state &= ~CLKDOWN; 1410 break; 1411 case SLK: 1412 state &= ~SLKDOWN; 1413 break; 1414 case ALK: 1415 state &= ~ALKDOWN; 1416 break; 1417 case NOP: 1418 /* release events of regular keys are not reported */ 1419 *shiftstate &= ~SHIFTAON; 1420 lwkt_reltoken(&tty_token); 1421 return NOKEY; 1422 } 1423 *shiftstate = state & ~SHIFTAON; 1424 lwkt_reltoken(&tty_token); 1425 return (SPCLKEY | RELKEY | action); 1426 } else { /* make: key pressed */ 1427 action = key->map[i]; 1428 state &= ~SHIFTAON; 1429 if (key->spcl & (0x80 >> i)) { 1430 /* special keys */ 1431 if (kbd->kb_lastact[keycode] == NOP) 1432 kbd->kb_lastact[keycode] = action; 1433 if (kbd->kb_lastact[keycode] != action) 1434 action = NOP; 1435 switch (action) { 1436 /* LOCKING KEYS */ 1437 case NLK: 1438 set_lockkey_state(kbd, state, NLK); 1439 break; 1440 case CLK: 1441 set_lockkey_state(kbd, state, CLK); 1442 break; 1443 case SLK: 1444 set_lockkey_state(kbd, state, SLK); 1445 break; 1446 case ALK: 1447 set_lockkey_state(kbd, state, ALK); 1448 break; 1449 /* NON-LOCKING KEYS */ 1450 case SPSC: case RBT: case SUSP: case STBY: 1451 case DBG: case NEXT: case PREV: case PNC: 1452 case HALT: case PDWN: 1453 *accents = 0; 1454 break; 1455 case BTAB: 1456 *accents = 0; 1457 action |= BKEY; 1458 break; 1459 case LSHA: 1460 state |= SHIFTAON; 1461 action = LSH; 1462 /* FALL THROUGH */ 1463 case LSH: 1464 state |= SHIFTS1; 1465 break; 1466 case RSHA: 1467 state |= SHIFTAON; 1468 action = RSH; 1469 /* FALL THROUGH */ 1470 case RSH: 1471 state |= SHIFTS2; 1472 break; 1473 case LCTRA: 1474 state |= SHIFTAON; 1475 action = LCTR; 1476 /* FALL THROUGH */ 1477 case LCTR: 1478 state |= CTLS1; 1479 break; 1480 case RCTRA: 1481 state |= SHIFTAON; 1482 action = RCTR; 1483 /* FALL THROUGH */ 1484 case RCTR: 1485 state |= CTLS2; 1486 break; 1487 case LALTA: 1488 state |= SHIFTAON; 1489 action = LALT; 1490 /* FALL THROUGH */ 1491 case LALT: 1492 state |= ALTS1; 1493 break; 1494 case RALTA: 1495 state |= SHIFTAON; 1496 action = RALT; 1497 /* FALL THROUGH */ 1498 case RALT: 1499 state |= ALTS2; 1500 break; 1501 case ASH: 1502 state |= AGRS1; 1503 break; 1504 case META: 1505 state |= METAS1; 1506 break; 1507 case NOP: 1508 *shiftstate = state; 1509 lwkt_reltoken(&tty_token); 1510 return NOKEY; 1511 default: 1512 /* is this an accent (dead) key? */ 1513 *shiftstate = state; 1514 if (action >= F_ACC && action <= L_ACC) { 1515 action = save_accent_key(kbd, action, 1516 accents); 1517 switch (action) { 1518 case NOKEY: 1519 case ERRKEY: 1520 lwkt_reltoken(&tty_token); 1521 return action; 1522 default: 1523 if (state & METAS) { 1524 lwkt_reltoken(&tty_token); 1525 return (action | MKEY); 1526 } else { 1527 lwkt_reltoken(&tty_token); 1528 return action; 1529 } 1530 } 1531 /* NOT REACHED */ 1532 } 1533 /* other special keys */ 1534 if (*accents > 0) { 1535 *accents = 0; 1536 lwkt_reltoken(&tty_token); 1537 return ERRKEY; 1538 } 1539 if (action >= F_FN && action <= L_FN) 1540 action |= FKEY; 1541 /* XXX: return fkey string for the FKEY? */ 1542 lwkt_reltoken(&tty_token); 1543 return (SPCLKEY | action); 1544 } 1545 *shiftstate = state; 1546 lwkt_reltoken(&tty_token); 1547 return (SPCLKEY | action); 1548 } else { 1549 /* regular keys */ 1550 kbd->kb_lastact[keycode] = NOP; 1551 *shiftstate = state; 1552 if (*accents > 0) { 1553 /* make an accented char */ 1554 action = make_accent_char(kbd, action, accents); 1555 if (action == ERRKEY) { 1556 lwkt_reltoken(&tty_token); 1557 return action; 1558 } 1559 } 1560 if (state & METAS) 1561 action |= MKEY; 1562 lwkt_reltoken(&tty_token); 1563 return action; 1564 } 1565 } 1566 /* NOT REACHED */ 1567 lwkt_reltoken(&tty_token); 1568 } 1569