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