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