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