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