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