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