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