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 52typedef 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 59static SLIST_HEAD(, keyboard_driver) keyboard_drivers = 60 SLIST_HEAD_INITIALIZER(keyboard_drivers); 61 62SET_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 72static int keyboards = 1; 73static keyboard_t *kbd_ini; 74static keyboard_t **keyboard = &kbd_ini; 75static keyboard_switch_t *kbdsw_ini; 76 keyboard_switch_t **kbdsw = &kbdsw_ini; 77 78static int keymap_restrict_change; 79SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd"); 80SYSCTL_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 85static int 86kbd_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 */ 132void 133kbd_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 155void 156kbd_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 */ 166int 167kbd_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 175int 176kbd_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 */ 184int 185kbd_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 249int 250kbd_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 */ 282keyboard_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 */ 312int 313kbd_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 */ 336int 337kbd_find_keyboard(char *driver, int unit) 338{ 339 return (kbd_find_keyboard2(driver, unit, 0)); 340} 341 342/* allocate a keyboard */ 343int 344kbd_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 370int 371kbd_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 393int 394kbd_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 */ 417keyboard_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 435int 436kbd_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 464static d_open_t genkbdopen; 465static d_close_t genkbdclose; 466static d_read_t genkbdread; 467static d_write_t genkbdwrite; 468static d_ioctl_t genkbdioctl; 469static d_poll_t genkbdpoll; 470 471 472static 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 484int 485kbd_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 502int 503kbd_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 526static kbd_callback_func_t genkbd_event; 527 528static int 529genkbdopen(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 566static int 567genkbdclose(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 592static int 593genkbdread(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 645static int 646genkbdwrite(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 656static int 657genkbdioctl(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 671static int 672genkbdpoll(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 695static int 696genkbd_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 802static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *); 803static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *); 804static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *); 805static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *); 806#endif 807 808int 809genkbd_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 960static int 961key_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 998static int 999keymap_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 1011static int 1012accent_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 1041static int 1042fkey_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 */ 1056u_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 */ 1069static 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 1089void 1090genkbd_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 1113static u_int 1114save_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 1142static u_int 1143make_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 1169int 1170genkbd_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}
| 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 53typedef 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 60static SLIST_HEAD(, keyboard_driver) keyboard_drivers = 61 SLIST_HEAD_INITIALIZER(keyboard_drivers); 62 63SET_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 73static int keyboards = 1; 74static keyboard_t *kbd_ini; 75static keyboard_t **keyboard = &kbd_ini; 76static keyboard_switch_t *kbdsw_ini; 77 keyboard_switch_t **kbdsw = &kbdsw_ini; 78 79static int keymap_restrict_change; 80SYSCTL_NODE(_hw, OID_AUTO, kbd, CTLFLAG_RD, 0, "kbd"); 81SYSCTL_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 86static int 87kbd_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 */ 133void 134kbd_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 156void 157kbd_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 */ 167int 168kbd_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 176int 177kbd_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 */ 185int 186kbd_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 250int 251kbd_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 */ 283keyboard_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 */ 313int 314kbd_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 */ 337int 338kbd_find_keyboard(char *driver, int unit) 339{ 340 return (kbd_find_keyboard2(driver, unit, 0)); 341} 342 343/* allocate a keyboard */ 344int 345kbd_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 371int 372kbd_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 394int 395kbd_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 */ 418keyboard_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 436int 437kbd_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 465static d_open_t genkbdopen; 466static d_close_t genkbdclose; 467static d_read_t genkbdread; 468static d_write_t genkbdwrite; 469static d_ioctl_t genkbdioctl; 470static d_poll_t genkbdpoll; 471 472 473static 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 485int 486kbd_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 503int 504kbd_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 527static kbd_callback_func_t genkbd_event; 528 529static int 530genkbdopen(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 567static int 568genkbdclose(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 593static int 594genkbdread(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 646static int 647genkbdwrite(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 657static int 658genkbdioctl(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 672static int 673genkbdpoll(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 696static int 697genkbd_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 803static int key_change_ok(struct keyent_t *, struct keyent_t *, struct thread *); 804static int keymap_change_ok(keymap_t *, keymap_t *, struct thread *); 805static int accent_change_ok(accentmap_t *, accentmap_t *, struct thread *); 806static int fkey_change_ok(fkeytab_t *, fkeyarg_t *, struct thread *); 807#endif 808 809int 810genkbd_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 961static int 962key_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 999static int 1000keymap_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 1012static int 1013accent_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 1042static int 1043fkey_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 */ 1057u_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 */ 1070static 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 1090void 1091genkbd_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 1114static u_int 1115save_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 1143static u_int 1144make_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 1170int 1171genkbd_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}
|