1 /*- 2 * vkbd.c 3 * 4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: vkbd.c,v 1.20 2004/11/15 23:53:30 max Exp $ 29 * $FreeBSD$ 30 */ 31 32 #include "opt_kbd.h" 33 34 #include <sys/param.h> 35 #include <sys/conf.h> 36 #include <sys/fcntl.h> 37 #include <sys/kbio.h> 38 #include <sys/kernel.h> 39 #include <sys/limits.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/poll.h> 45 #include <sys/proc.h> 46 #include <sys/queue.h> 47 #include <sys/selinfo.h> 48 #include <sys/systm.h> 49 #include <sys/taskqueue.h> 50 #include <sys/uio.h> 51 #include <dev/kbd/kbdreg.h> 52 #include <dev/kbd/kbdtables.h> 53 #include <dev/vkbd/vkbd_var.h> 54 55 #define DEVICE_NAME "vkbdctl" 56 #define KEYBOARD_NAME "vkbd" 57 58 MALLOC_DECLARE(M_VKBD); 59 MALLOC_DEFINE(M_VKBD, KEYBOARD_NAME, "Virtual AT keyboard"); 60 61 /***************************************************************************** 62 ***************************************************************************** 63 ** Keyboard state 64 ***************************************************************************** 65 *****************************************************************************/ 66 67 #define VKBD_LOCK_DECL struct mtx ks_lock 68 #define VKBD_LOCK_INIT(s) mtx_init(&(s)->ks_lock, NULL, NULL, MTX_DEF) 69 #define VKBD_LOCK_DESTROY(s) mtx_destroy(&(s)->ks_lock) 70 #define VKBD_LOCK(s) mtx_lock(&(s)->ks_lock) 71 #define VKBD_UNLOCK(s) mtx_unlock(&(s)->ks_lock) 72 #define VKBD_LOCK_ASSERT(s, w) mtx_assert(&(s)->ks_lock, w) 73 #define VKBD_SLEEP(s, f, d, t) \ 74 msleep(&(s)->f, &(s)->ks_lock, PCATCH | (PZERO + 1), d, t) 75 76 #define VKBD_KEYBOARD(d) \ 77 kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, dev2unit(d))) 78 79 /* vkbd queue */ 80 struct vkbd_queue 81 { 82 int q[VKBD_Q_SIZE]; /* queue */ 83 int head; /* index of the first code */ 84 int tail; /* index of the last code */ 85 int cc; /* number of codes in queue */ 86 }; 87 88 typedef struct vkbd_queue vkbd_queue_t; 89 90 /* vkbd state */ 91 struct vkbd_state 92 { 93 struct cdev *ks_dev; /* control device */ 94 95 struct selinfo ks_rsel; /* select(2) */ 96 struct selinfo ks_wsel; 97 98 vkbd_queue_t ks_inq; /* input key codes queue */ 99 struct task ks_task; /* interrupt task */ 100 101 int ks_flags; /* flags */ 102 #define OPEN (1 << 0) /* control device is open */ 103 #define COMPOSE (1 << 1) /* compose flag */ 104 #define STATUS (1 << 2) /* status has changed */ 105 #define TASK (1 << 3) /* interrupt task queued */ 106 #define READ (1 << 4) /* read pending */ 107 #define WRITE (1 << 5) /* write pending */ 108 109 int ks_mode; /* K_XLATE, K_RAW, K_CODE */ 110 int ks_polling; /* polling flag */ 111 int ks_state; /* shift/lock key state */ 112 int ks_accents; /* accent key index (> 0) */ 113 u_int ks_composed_char; /* composed char code */ 114 u_char ks_prefix; /* AT scan code prefix */ 115 116 VKBD_LOCK_DECL; 117 }; 118 119 typedef struct vkbd_state vkbd_state_t; 120 121 /***************************************************************************** 122 ***************************************************************************** 123 ** Character device 124 ***************************************************************************** 125 *****************************************************************************/ 126 127 static void vkbd_dev_clone(void *, char *, int, struct cdev **); 128 static d_open_t vkbd_dev_open; 129 static d_close_t vkbd_dev_close; 130 static d_read_t vkbd_dev_read; 131 static d_write_t vkbd_dev_write; 132 static d_ioctl_t vkbd_dev_ioctl; 133 static d_poll_t vkbd_dev_poll; 134 static void vkbd_dev_intr(void *, int); 135 static void vkbd_status_changed(vkbd_state_t *); 136 static int vkbd_data_ready(vkbd_state_t *); 137 static int vkbd_data_read(vkbd_state_t *, int); 138 139 static struct cdevsw vkbd_dev_cdevsw = { 140 .d_version = D_VERSION, 141 .d_flags = D_PSEUDO | D_NEEDGIANT, 142 .d_open = vkbd_dev_open, 143 .d_close = vkbd_dev_close, 144 .d_read = vkbd_dev_read, 145 .d_write = vkbd_dev_write, 146 .d_ioctl = vkbd_dev_ioctl, 147 .d_poll = vkbd_dev_poll, 148 .d_name = DEVICE_NAME, 149 }; 150 151 static struct clonedevs *vkbd_dev_clones = NULL; 152 153 /* Clone device */ 154 static void 155 vkbd_dev_clone(void *arg, char *name, int namelen, struct cdev **dev) 156 { 157 int unit; 158 159 if (*dev != NULL) 160 return; 161 162 if (strcmp(name, DEVICE_NAME) == 0) 163 unit = -1; 164 else if (dev_stdclone(name, NULL, DEVICE_NAME, &unit) != 1) 165 return; /* don't recognize the name */ 166 167 /* find any existing device, or allocate new unit number */ 168 if (clone_create(&vkbd_dev_clones, &vkbd_dev_cdevsw, &unit, dev, 0)) { 169 *dev = make_dev(&vkbd_dev_cdevsw, unit2minor(unit), 170 UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME "%d", unit); 171 if (*dev != NULL) 172 (*dev)->si_flags |= SI_CHEAPCLONE; 173 } 174 } 175 176 /* Open device */ 177 static int 178 vkbd_dev_open(struct cdev *dev, int flag, int mode, struct thread *td) 179 { 180 int unit = dev2unit(dev), error; 181 keyboard_switch_t *sw = NULL; 182 keyboard_t *kbd = NULL; 183 vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; 184 185 /* XXX FIXME: dev->si_drv1 locking */ 186 if (state == NULL) { 187 if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL) 188 return (ENXIO); 189 190 if ((error = (*sw->probe)(unit, NULL, 0)) != 0 || 191 (error = (*sw->init)(unit, &kbd, NULL, 0)) != 0) 192 return (error); 193 194 state = (vkbd_state_t *) kbd->kb_data; 195 196 if ((error = (*sw->enable)(kbd)) != 0) { 197 (*sw->term)(kbd); 198 return (error); 199 } 200 201 #ifdef KBD_INSTALL_CDEV 202 if ((error = kbd_attach(kbd)) != 0) { 203 (*sw->disable)(kbd); 204 (*sw->term)(kbd); 205 return (error); 206 } 207 #endif /* def KBD_INSTALL_CDEV */ 208 209 dev->si_drv1 = kbd->kb_data; 210 } 211 212 VKBD_LOCK(state); 213 214 if (state->ks_flags & OPEN) { 215 VKBD_UNLOCK(state); 216 return (EBUSY); 217 } 218 219 state->ks_flags |= OPEN; 220 state->ks_dev = dev; 221 222 VKBD_UNLOCK(state); 223 224 return (0); 225 } 226 227 /* Close device */ 228 static int 229 vkbd_dev_close(struct cdev *dev, int foo, int bar, struct thread *td) 230 { 231 keyboard_t *kbd = VKBD_KEYBOARD(dev); 232 vkbd_state_t *state = NULL; 233 234 if (kbd == NULL) 235 return (ENXIO); 236 237 if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) 238 panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); 239 240 state = (vkbd_state_t *) kbd->kb_data; 241 242 VKBD_LOCK(state); 243 244 /* wait for interrupt task */ 245 while (state->ks_flags & TASK) 246 VKBD_SLEEP(state, ks_task, "vkbdc", 0); 247 248 /* wakeup poll()ers */ 249 selwakeuppri(&state->ks_rsel, PZERO + 1); 250 selwakeuppri(&state->ks_wsel, PZERO + 1); 251 252 state->ks_flags &= ~OPEN; 253 state->ks_dev = NULL; 254 state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; 255 256 VKBD_UNLOCK(state); 257 258 (*kbdsw[kbd->kb_index]->disable)(kbd); 259 #ifdef KBD_INSTALL_CDEV 260 kbd_detach(kbd); 261 #endif /* def KBD_INSTALL_CDEV */ 262 (*kbdsw[kbd->kb_index]->term)(kbd); 263 264 /* XXX FIXME: dev->si_drv1 locking */ 265 dev->si_drv1 = NULL; 266 267 return (0); 268 } 269 270 /* Read status */ 271 static int 272 vkbd_dev_read(struct cdev *dev, struct uio *uio, int flag) 273 { 274 keyboard_t *kbd = VKBD_KEYBOARD(dev); 275 vkbd_state_t *state = NULL; 276 vkbd_status_t status; 277 int error; 278 279 if (kbd == NULL) 280 return (ENXIO); 281 282 if (uio->uio_resid != sizeof(status)) 283 return (EINVAL); 284 285 if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) 286 panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); 287 288 state = (vkbd_state_t *) kbd->kb_data; 289 290 VKBD_LOCK(state); 291 292 if (state->ks_flags & READ) { 293 VKBD_UNLOCK(state); 294 return (EALREADY); 295 } 296 297 state->ks_flags |= READ; 298 again: 299 if (state->ks_flags & STATUS) { 300 state->ks_flags &= ~STATUS; 301 302 status.mode = state->ks_mode; 303 status.leds = KBD_LED_VAL(kbd); 304 status.lock = state->ks_state & LOCK_MASK; 305 status.delay = kbd->kb_delay1; 306 status.rate = kbd->kb_delay2; 307 bzero(status.reserved, sizeof(status.reserved)); 308 309 error = uiomove(&status, sizeof(status), uio); 310 } else { 311 if (flag & O_NONBLOCK) { 312 error = EWOULDBLOCK; 313 goto done; 314 } 315 316 error = VKBD_SLEEP(state, ks_flags, "vkbdr", 0); 317 if (error != 0) 318 goto done; 319 320 goto again; 321 } 322 done: 323 state->ks_flags &= ~READ; 324 325 VKBD_UNLOCK(state); 326 327 return (error); 328 } 329 330 /* Write scancodes */ 331 static int 332 vkbd_dev_write(struct cdev *dev, struct uio *uio, int flag) 333 { 334 keyboard_t *kbd = VKBD_KEYBOARD(dev); 335 vkbd_state_t *state = NULL; 336 vkbd_queue_t *q = NULL; 337 int error, avail, bytes; 338 339 if (kbd == NULL) 340 return (ENXIO); 341 342 if (uio->uio_resid <= 0) 343 return (EINVAL); 344 345 if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) 346 panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); 347 348 state = (vkbd_state_t *) kbd->kb_data; 349 350 VKBD_LOCK(state); 351 352 if (state->ks_flags & WRITE) { 353 VKBD_UNLOCK(state); 354 return (EALREADY); 355 } 356 357 state->ks_flags |= WRITE; 358 error = 0; 359 q = &state->ks_inq; 360 361 while (uio->uio_resid >= sizeof(q->q[0])) { 362 if (q->head == q->tail) { 363 if (q->cc == 0) 364 avail = sizeof(q->q)/sizeof(q->q[0]) - q->head; 365 else 366 avail = 0; /* queue must be full */ 367 } else if (q->head < q->tail) 368 avail = sizeof(q->q)/sizeof(q->q[0]) - q->tail; 369 else 370 avail = q->head - q->tail; 371 372 if (avail == 0) { 373 if (flag & O_NONBLOCK) { 374 error = EWOULDBLOCK; 375 break; 376 } 377 378 error = VKBD_SLEEP(state, ks_inq, "vkbdw", 0); 379 if (error != 0) 380 break; 381 } else { 382 bytes = avail * sizeof(q->q[0]); 383 if (bytes > uio->uio_resid) { 384 avail = uio->uio_resid / sizeof(q->q[0]); 385 bytes = avail * sizeof(q->q[0]); 386 } 387 388 error = uiomove((void *) &q->q[q->tail], bytes, uio); 389 if (error != 0) 390 break; 391 392 q->cc += avail; 393 q->tail += avail; 394 if (q->tail == sizeof(q->q)/sizeof(q->q[0])) 395 q->tail = 0; 396 397 /* queue interrupt task if needed */ 398 if (!(state->ks_flags & TASK) && 399 taskqueue_enqueue(taskqueue_swi_giant, &state->ks_task) == 0) 400 state->ks_flags |= TASK; 401 } 402 } 403 404 state->ks_flags &= ~WRITE; 405 406 VKBD_UNLOCK(state); 407 408 return (error); 409 } 410 411 /* Process ioctl */ 412 static int 413 vkbd_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 414 { 415 keyboard_t *kbd = VKBD_KEYBOARD(dev); 416 417 return ((kbd == NULL)? ENXIO : 418 (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, data)); 419 } 420 421 /* Poll device */ 422 static int 423 vkbd_dev_poll(struct cdev *dev, int events, struct thread *td) 424 { 425 vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; 426 vkbd_queue_t *q = NULL; 427 int revents = 0; 428 429 if (state == NULL) 430 return (ENXIO); 431 432 VKBD_LOCK(state); 433 434 q = &state->ks_inq; 435 436 if (events & (POLLIN | POLLRDNORM)) { 437 if (state->ks_flags & STATUS) 438 revents |= events & (POLLIN | POLLRDNORM); 439 else 440 selrecord(td, &state->ks_rsel); 441 } 442 443 if (events & (POLLOUT | POLLWRNORM)) { 444 if (q->cc < sizeof(q->q)/sizeof(q->q[0])) 445 revents |= events & (POLLOUT | POLLWRNORM); 446 else 447 selrecord(td, &state->ks_wsel); 448 } 449 450 VKBD_UNLOCK(state); 451 452 return (revents); 453 } 454 455 /* Interrupt handler */ 456 void 457 vkbd_dev_intr(void *xkbd, int pending) 458 { 459 keyboard_t *kbd = (keyboard_t *) xkbd; 460 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 461 462 (*kbdsw[kbd->kb_index]->intr)(kbd, NULL); 463 464 VKBD_LOCK(state); 465 466 state->ks_flags &= ~TASK; 467 wakeup(&state->ks_task); 468 469 VKBD_UNLOCK(state); 470 } 471 472 /* Set status change flags */ 473 static void 474 vkbd_status_changed(vkbd_state_t *state) 475 { 476 VKBD_LOCK_ASSERT(state, MA_OWNED); 477 478 if (!(state->ks_flags & STATUS)) { 479 state->ks_flags |= STATUS; 480 selwakeuppri(&state->ks_rsel, PZERO + 1); 481 wakeup(&state->ks_flags); 482 } 483 } 484 485 /* Check if we have data in the input queue */ 486 static int 487 vkbd_data_ready(vkbd_state_t *state) 488 { 489 VKBD_LOCK_ASSERT(state, MA_OWNED); 490 491 return (state->ks_inq.cc > 0); 492 } 493 494 /* Read one code from the input queue */ 495 static int 496 vkbd_data_read(vkbd_state_t *state, int wait) 497 { 498 vkbd_queue_t *q = &state->ks_inq; 499 int c; 500 501 VKBD_LOCK_ASSERT(state, MA_OWNED); 502 503 if (q->cc == 0) 504 return (-1); 505 506 /* get first code from the queue */ 507 q->cc --; 508 c = q->q[q->head ++]; 509 if (q->head == sizeof(q->q)/sizeof(q->q[0])) 510 q->head = 0; 511 512 /* wakeup ks_inq writers/poll()ers */ 513 selwakeuppri(&state->ks_wsel, PZERO + 1); 514 wakeup(q); 515 516 return (c); 517 } 518 519 /**************************************************************************** 520 **************************************************************************** 521 ** Keyboard driver 522 **************************************************************************** 523 ****************************************************************************/ 524 525 static int vkbd_configure(int flags); 526 static kbd_probe_t vkbd_probe; 527 static kbd_init_t vkbd_init; 528 static kbd_term_t vkbd_term; 529 static kbd_intr_t vkbd_intr; 530 static kbd_test_if_t vkbd_test_if; 531 static kbd_enable_t vkbd_enable; 532 static kbd_disable_t vkbd_disable; 533 static kbd_read_t vkbd_read; 534 static kbd_check_t vkbd_check; 535 static kbd_read_char_t vkbd_read_char; 536 static kbd_check_char_t vkbd_check_char; 537 static kbd_ioctl_t vkbd_ioctl; 538 static kbd_lock_t vkbd_lock; 539 static void vkbd_clear_state_locked(vkbd_state_t *state); 540 static kbd_clear_state_t vkbd_clear_state; 541 static kbd_get_state_t vkbd_get_state; 542 static kbd_set_state_t vkbd_set_state; 543 static kbd_poll_mode_t vkbd_poll; 544 545 static keyboard_switch_t vkbdsw = { 546 .probe = vkbd_probe, 547 .init = vkbd_init, 548 .term = vkbd_term, 549 .intr = vkbd_intr, 550 .test_if = vkbd_test_if, 551 .enable = vkbd_enable, 552 .disable = vkbd_disable, 553 .read = vkbd_read, 554 .check = vkbd_check, 555 .read_char = vkbd_read_char, 556 .check_char = vkbd_check_char, 557 .ioctl = vkbd_ioctl, 558 .lock = vkbd_lock, 559 .clear_state = vkbd_clear_state, 560 .get_state = vkbd_get_state, 561 .set_state = vkbd_set_state, 562 .get_fkeystr = genkbd_get_fkeystr, 563 .poll = vkbd_poll, 564 .diag = genkbd_diag, 565 }; 566 567 static int typematic(int delay, int rate); 568 static int typematic_delay(int delay); 569 static int typematic_rate(int rate); 570 571 /* Return the number of found keyboards */ 572 static int 573 vkbd_configure(int flags) 574 { 575 return (1); 576 } 577 578 /* Detect a keyboard */ 579 static int 580 vkbd_probe(int unit, void *arg, int flags) 581 { 582 return (0); 583 } 584 585 /* Reset and initialize the keyboard (stolen from atkbd.c) */ 586 static int 587 vkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 588 { 589 keyboard_t *kbd = NULL; 590 vkbd_state_t *state = NULL; 591 keymap_t *keymap = NULL; 592 accentmap_t *accmap = NULL; 593 fkeytab_t *fkeymap = NULL; 594 int fkeymap_size, delay[2]; 595 int error, needfree; 596 597 if (*kbdp == NULL) { 598 *kbdp = kbd = malloc(sizeof(*kbd), M_VKBD, M_NOWAIT | M_ZERO); 599 state = malloc(sizeof(*state), M_VKBD, M_NOWAIT | M_ZERO); 600 keymap = malloc(sizeof(key_map), M_VKBD, M_NOWAIT); 601 accmap = malloc(sizeof(accent_map), M_VKBD, M_NOWAIT); 602 fkeymap = malloc(sizeof(fkey_tab), M_VKBD, M_NOWAIT); 603 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]); 604 needfree = 1; 605 if ((kbd == NULL) || (state == NULL) || (keymap == NULL) || 606 (accmap == NULL) || (fkeymap == NULL)) { 607 error = ENOMEM; 608 goto bad; 609 } 610 611 VKBD_LOCK_INIT(state); 612 state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; 613 TASK_INIT(&state->ks_task, 0, vkbd_dev_intr, (void *) kbd); 614 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { 615 return (0); 616 } else { 617 kbd = *kbdp; 618 state = (vkbd_state_t *) kbd->kb_data; 619 keymap = kbd->kb_keymap; 620 accmap = kbd->kb_accentmap; 621 fkeymap = kbd->kb_fkeytab; 622 fkeymap_size = kbd->kb_fkeytab_size; 623 needfree = 0; 624 } 625 626 if (!KBD_IS_PROBED(kbd)) { 627 kbd_init_struct(kbd, KEYBOARD_NAME, KB_OTHER, unit, flags, 0, 0); 628 bcopy(&key_map, keymap, sizeof(key_map)); 629 bcopy(&accent_map, accmap, sizeof(accent_map)); 630 bcopy(fkey_tab, fkeymap, 631 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab))); 632 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); 633 kbd->kb_data = (void *)state; 634 635 KBD_FOUND_DEVICE(kbd); 636 KBD_PROBE_DONE(kbd); 637 638 VKBD_LOCK(state); 639 vkbd_clear_state_locked(state); 640 state->ks_mode = K_XLATE; 641 /* FIXME: set the initial value for lock keys in ks_state */ 642 VKBD_UNLOCK(state); 643 } 644 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { 645 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; 646 647 vkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 648 delay[0] = kbd->kb_delay1; 649 delay[1] = kbd->kb_delay2; 650 vkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 651 652 KBD_INIT_DONE(kbd); 653 } 654 if (!KBD_IS_CONFIGURED(kbd)) { 655 if (kbd_register(kbd) < 0) { 656 error = ENXIO; 657 goto bad; 658 } 659 KBD_CONFIG_DONE(kbd); 660 } 661 662 return (0); 663 bad: 664 if (needfree) { 665 if (state != NULL) 666 free(state, M_VKBD); 667 if (keymap != NULL) 668 free(keymap, M_VKBD); 669 if (accmap != NULL) 670 free(accmap, M_VKBD); 671 if (fkeymap != NULL) 672 free(fkeymap, M_VKBD); 673 if (kbd != NULL) { 674 free(kbd, M_DEVBUF); 675 *kbdp = NULL; /* insure ref doesn't leak to caller */ 676 } 677 } 678 return (error); 679 } 680 681 /* Finish using this keyboard */ 682 static int 683 vkbd_term(keyboard_t *kbd) 684 { 685 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 686 687 kbd_unregister(kbd); 688 689 VKBD_LOCK_DESTROY(state); 690 bzero(state, sizeof(*state)); 691 free(state, M_VKBD); 692 693 free(kbd->kb_keymap, M_VKBD); 694 free(kbd->kb_accentmap, M_VKBD); 695 free(kbd->kb_fkeytab, M_VKBD); 696 free(kbd, M_VKBD); 697 698 return (0); 699 } 700 701 /* Keyboard interrupt routine */ 702 static int 703 vkbd_intr(keyboard_t *kbd, void *arg) 704 { 705 int c; 706 707 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { 708 /* let the callback function to process the input */ 709 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, 710 kbd->kb_callback.kc_arg); 711 } else { 712 /* read and discard the input; no one is waiting for input */ 713 do { 714 c = vkbd_read_char(kbd, FALSE); 715 } while (c != NOKEY); 716 } 717 718 return (0); 719 } 720 721 /* Test the interface to the device */ 722 static int 723 vkbd_test_if(keyboard_t *kbd) 724 { 725 return (0); 726 } 727 728 /* 729 * Enable the access to the device; until this function is called, 730 * the client cannot read from the keyboard. 731 */ 732 733 static int 734 vkbd_enable(keyboard_t *kbd) 735 { 736 KBD_ACTIVATE(kbd); 737 return (0); 738 } 739 740 /* Disallow the access to the device */ 741 static int 742 vkbd_disable(keyboard_t *kbd) 743 { 744 KBD_DEACTIVATE(kbd); 745 return (0); 746 } 747 748 /* Read one byte from the keyboard if it's allowed */ 749 static int 750 vkbd_read(keyboard_t *kbd, int wait) 751 { 752 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 753 int c; 754 755 VKBD_LOCK(state); 756 c = vkbd_data_read(state, wait); 757 VKBD_UNLOCK(state); 758 759 if (c != -1) 760 kbd->kb_count ++; 761 762 return (KBD_IS_ACTIVE(kbd)? c : -1); 763 } 764 765 /* Check if data is waiting */ 766 static int 767 vkbd_check(keyboard_t *kbd) 768 { 769 vkbd_state_t *state = NULL; 770 int ready; 771 772 if (!KBD_IS_ACTIVE(kbd)) 773 return (FALSE); 774 775 state = (vkbd_state_t *) kbd->kb_data; 776 777 VKBD_LOCK(state); 778 ready = vkbd_data_ready(state); 779 VKBD_UNLOCK(state); 780 781 return (ready); 782 } 783 784 /* Read char from the keyboard (stolen from atkbd.c) */ 785 static u_int 786 vkbd_read_char(keyboard_t *kbd, int wait) 787 { 788 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 789 u_int action; 790 int scancode, keycode; 791 792 VKBD_LOCK(state); 793 794 next_code: 795 796 /* do we have a composed char to return? */ 797 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { 798 action = state->ks_composed_char; 799 state->ks_composed_char = 0; 800 if (action > UCHAR_MAX) { 801 VKBD_UNLOCK(state); 802 return (ERRKEY); 803 } 804 805 VKBD_UNLOCK(state); 806 return (action); 807 } 808 809 /* see if there is something in the keyboard port */ 810 scancode = vkbd_data_read(state, wait); 811 if (scancode == -1) { 812 VKBD_UNLOCK(state); 813 return (NOKEY); 814 } 815 /* XXX FIXME: check for -1 if wait == 1! */ 816 817 kbd->kb_count ++; 818 819 /* return the byte as is for the K_RAW mode */ 820 if (state->ks_mode == K_RAW) { 821 VKBD_UNLOCK(state); 822 return (scancode); 823 } 824 825 /* translate the scan code into a keycode */ 826 keycode = scancode & 0x7F; 827 switch (state->ks_prefix) { 828 case 0x00: /* normal scancode */ 829 switch(scancode) { 830 case 0xB8: /* left alt (compose key) released */ 831 if (state->ks_flags & COMPOSE) { 832 state->ks_flags &= ~COMPOSE; 833 if (state->ks_composed_char > UCHAR_MAX) 834 state->ks_composed_char = 0; 835 } 836 break; 837 case 0x38: /* left alt (compose key) pressed */ 838 if (!(state->ks_flags & COMPOSE)) { 839 state->ks_flags |= COMPOSE; 840 state->ks_composed_char = 0; 841 } 842 break; 843 case 0xE0: 844 case 0xE1: 845 state->ks_prefix = scancode; 846 goto next_code; 847 } 848 break; 849 case 0xE0: /* 0xE0 prefix */ 850 state->ks_prefix = 0; 851 switch (keycode) { 852 case 0x1C: /* right enter key */ 853 keycode = 0x59; 854 break; 855 case 0x1D: /* right ctrl key */ 856 keycode = 0x5A; 857 break; 858 case 0x35: /* keypad divide key */ 859 keycode = 0x5B; 860 break; 861 case 0x37: /* print scrn key */ 862 keycode = 0x5C; 863 break; 864 case 0x38: /* right alt key (alt gr) */ 865 keycode = 0x5D; 866 break; 867 case 0x46: /* ctrl-pause/break on AT 101 (see below) */ 868 keycode = 0x68; 869 break; 870 case 0x47: /* grey home key */ 871 keycode = 0x5E; 872 break; 873 case 0x48: /* grey up arrow key */ 874 keycode = 0x5F; 875 break; 876 case 0x49: /* grey page up key */ 877 keycode = 0x60; 878 break; 879 case 0x4B: /* grey left arrow key */ 880 keycode = 0x61; 881 break; 882 case 0x4D: /* grey right arrow key */ 883 keycode = 0x62; 884 break; 885 case 0x4F: /* grey end key */ 886 keycode = 0x63; 887 break; 888 case 0x50: /* grey down arrow key */ 889 keycode = 0x64; 890 break; 891 case 0x51: /* grey page down key */ 892 keycode = 0x65; 893 break; 894 case 0x52: /* grey insert key */ 895 keycode = 0x66; 896 break; 897 case 0x53: /* grey delete key */ 898 keycode = 0x67; 899 break; 900 /* the following 3 are only used on the MS "Natural" keyboard */ 901 case 0x5b: /* left Window key */ 902 keycode = 0x69; 903 break; 904 case 0x5c: /* right Window key */ 905 keycode = 0x6a; 906 break; 907 case 0x5d: /* menu key */ 908 keycode = 0x6b; 909 break; 910 case 0x5e: /* power key */ 911 keycode = 0x6d; 912 break; 913 case 0x5f: /* sleep key */ 914 keycode = 0x6e; 915 break; 916 case 0x63: /* wake key */ 917 keycode = 0x6f; 918 break; 919 default: /* ignore everything else */ 920 goto next_code; 921 } 922 break; 923 case 0xE1: /* 0xE1 prefix */ 924 /* 925 * The pause/break key on the 101 keyboard produces: 926 * E1-1D-45 E1-9D-C5 927 * Ctrl-pause/break produces: 928 * E0-46 E0-C6 (See above.) 929 */ 930 state->ks_prefix = 0; 931 if (keycode == 0x1D) 932 state->ks_prefix = 0x1D; 933 goto next_code; 934 /* NOT REACHED */ 935 case 0x1D: /* pause / break */ 936 state->ks_prefix = 0; 937 if (keycode != 0x45) 938 goto next_code; 939 keycode = 0x68; 940 break; 941 } 942 943 if (kbd->kb_type == KB_84) { 944 switch (keycode) { 945 case 0x37: /* *(numpad)/print screen */ 946 if (state->ks_flags & SHIFTS) 947 keycode = 0x5c; /* print screen */ 948 break; 949 case 0x45: /* num lock/pause */ 950 if (state->ks_flags & CTLS) 951 keycode = 0x68; /* pause */ 952 break; 953 case 0x46: /* scroll lock/break */ 954 if (state->ks_flags & CTLS) 955 keycode = 0x6c; /* break */ 956 break; 957 } 958 } else if (kbd->kb_type == KB_101) { 959 switch (keycode) { 960 case 0x5c: /* print screen */ 961 if (state->ks_flags & ALTS) 962 keycode = 0x54; /* sysrq */ 963 break; 964 case 0x68: /* pause/break */ 965 if (state->ks_flags & CTLS) 966 keycode = 0x6c; /* break */ 967 break; 968 } 969 } 970 971 /* return the key code in the K_CODE mode */ 972 if (state->ks_mode == K_CODE) { 973 VKBD_UNLOCK(state); 974 return (keycode | (scancode & 0x80)); 975 } 976 977 /* compose a character code */ 978 if (state->ks_flags & COMPOSE) { 979 switch (keycode | (scancode & 0x80)) { 980 /* key pressed, process it */ 981 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ 982 state->ks_composed_char *= 10; 983 state->ks_composed_char += keycode - 0x40; 984 if (state->ks_composed_char > UCHAR_MAX) { 985 VKBD_UNLOCK(state); 986 return (ERRKEY); 987 } 988 goto next_code; 989 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ 990 state->ks_composed_char *= 10; 991 state->ks_composed_char += keycode - 0x47; 992 if (state->ks_composed_char > UCHAR_MAX) { 993 VKBD_UNLOCK(state); 994 return (ERRKEY); 995 } 996 goto next_code; 997 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ 998 state->ks_composed_char *= 10; 999 state->ks_composed_char += keycode - 0x4E; 1000 if (state->ks_composed_char > UCHAR_MAX) { 1001 VKBD_UNLOCK(state); 1002 return (ERRKEY); 1003 } 1004 goto next_code; 1005 case 0x52: /* keypad 0 */ 1006 state->ks_composed_char *= 10; 1007 if (state->ks_composed_char > UCHAR_MAX) { 1008 VKBD_UNLOCK(state); 1009 return (ERRKEY); 1010 } 1011 goto next_code; 1012 1013 /* key released, no interest here */ 1014 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ 1015 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ 1016 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ 1017 case 0xD2: /* keypad 0 */ 1018 goto next_code; 1019 1020 case 0x38: /* left alt key */ 1021 break; 1022 1023 default: 1024 if (state->ks_composed_char > 0) { 1025 state->ks_flags &= ~COMPOSE; 1026 state->ks_composed_char = 0; 1027 VKBD_UNLOCK(state); 1028 return (ERRKEY); 1029 } 1030 break; 1031 } 1032 } 1033 1034 /* keycode to key action */ 1035 action = genkbd_keyaction(kbd, keycode, scancode & 0x80, 1036 &state->ks_state, &state->ks_accents); 1037 if (action == NOKEY) 1038 goto next_code; 1039 1040 VKBD_UNLOCK(state); 1041 1042 return (action); 1043 } 1044 1045 /* Check if char is waiting */ 1046 static int 1047 vkbd_check_char(keyboard_t *kbd) 1048 { 1049 vkbd_state_t *state = NULL; 1050 int ready; 1051 1052 if (!KBD_IS_ACTIVE(kbd)) 1053 return (FALSE); 1054 1055 state = (vkbd_state_t *) kbd->kb_data; 1056 1057 VKBD_LOCK(state); 1058 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) 1059 ready = TRUE; 1060 else 1061 ready = vkbd_data_ready(state); 1062 VKBD_UNLOCK(state); 1063 1064 return (ready); 1065 } 1066 1067 /* Some useful control functions (stolen from atkbd.c) */ 1068 static int 1069 vkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1070 { 1071 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 1072 int i; 1073 1074 VKBD_LOCK(state); 1075 1076 switch (cmd) { 1077 case KDGKBMODE: /* get keyboard mode */ 1078 *(int *)arg = state->ks_mode; 1079 break; 1080 1081 case KDSKBMODE: /* set keyboard mode */ 1082 switch (*(int *)arg) { 1083 case K_XLATE: 1084 if (state->ks_mode != K_XLATE) { 1085 /* make lock key state and LED state match */ 1086 state->ks_state &= ~LOCK_MASK; 1087 state->ks_state |= KBD_LED_VAL(kbd); 1088 vkbd_status_changed(state); 1089 } 1090 /* FALLTHROUGH */ 1091 1092 case K_RAW: 1093 case K_CODE: 1094 if (state->ks_mode != *(int *)arg) { 1095 vkbd_clear_state_locked(state); 1096 state->ks_mode = *(int *)arg; 1097 vkbd_status_changed(state); 1098 } 1099 break; 1100 1101 default: 1102 VKBD_UNLOCK(state); 1103 return (EINVAL); 1104 } 1105 break; 1106 1107 case KDGETLED: /* get keyboard LED */ 1108 *(int *)arg = KBD_LED_VAL(kbd); 1109 break; 1110 1111 case KDSETLED: /* set keyboard LED */ 1112 /* NOTE: lock key state in ks_state won't be changed */ 1113 if (*(int *)arg & ~LOCK_MASK) { 1114 VKBD_UNLOCK(state); 1115 return (EINVAL); 1116 } 1117 1118 i = *(int *)arg; 1119 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1120 if (state->ks_mode == K_XLATE && 1121 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1122 if (i & ALKED) 1123 i |= CLKED; 1124 else 1125 i &= ~CLKED; 1126 } 1127 1128 KBD_LED_VAL(kbd) = *(int *)arg; 1129 vkbd_status_changed(state); 1130 break; 1131 1132 case KDGKBSTATE: /* get lock key state */ 1133 *(int *)arg = state->ks_state & LOCK_MASK; 1134 break; 1135 1136 case KDSKBSTATE: /* set lock key state */ 1137 if (*(int *)arg & ~LOCK_MASK) { 1138 VKBD_UNLOCK(state); 1139 return (EINVAL); 1140 } 1141 state->ks_state &= ~LOCK_MASK; 1142 state->ks_state |= *(int *)arg; 1143 vkbd_status_changed(state); 1144 VKBD_UNLOCK(state); 1145 /* set LEDs and quit */ 1146 return (vkbd_ioctl(kbd, KDSETLED, arg)); 1147 1148 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ 1149 i = typematic(((int *)arg)[0], ((int *)arg)[1]); 1150 kbd->kb_delay1 = typematic_delay(i); 1151 kbd->kb_delay2 = typematic_rate(i); 1152 vkbd_status_changed(state); 1153 break; 1154 1155 case KDSETRAD: /* set keyboard repeat rate (old interface) */ 1156 kbd->kb_delay1 = typematic_delay(*(int *)arg); 1157 kbd->kb_delay2 = typematic_rate(*(int *)arg); 1158 vkbd_status_changed(state); 1159 break; 1160 1161 case PIO_KEYMAP: /* set keyboard translation table */ 1162 case PIO_KEYMAPENT: /* set keyboard translation table entry */ 1163 case PIO_DEADKEYMAP: /* set accent key translation table */ 1164 state->ks_accents = 0; 1165 /* FALLTHROUGH */ 1166 1167 default: 1168 VKBD_UNLOCK(state); 1169 return (genkbd_commonioctl(kbd, cmd, arg)); 1170 } 1171 1172 VKBD_UNLOCK(state); 1173 1174 return (0); 1175 } 1176 1177 /* Lock the access to the keyboard */ 1178 static int 1179 vkbd_lock(keyboard_t *kbd, int lock) 1180 { 1181 return (1); /* XXX */ 1182 } 1183 1184 /* Clear the internal state of the keyboard */ 1185 static void 1186 vkbd_clear_state_locked(vkbd_state_t *state) 1187 { 1188 VKBD_LOCK_ASSERT(state, MA_OWNED); 1189 1190 state->ks_flags = 0; 1191 state->ks_polling = 0; 1192 state->ks_state &= LOCK_MASK; /* preserve locking key state */ 1193 state->ks_accents = 0; 1194 state->ks_composed_char = 0; 1195 /* state->ks_prefix = 0; XXX */ 1196 1197 /* flush ks_inq and wakeup writers/poll()ers */ 1198 state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; 1199 selwakeuppri(&state->ks_wsel, PZERO + 1); 1200 wakeup(&state->ks_inq); 1201 } 1202 1203 static void 1204 vkbd_clear_state(keyboard_t *kbd) 1205 { 1206 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 1207 1208 VKBD_LOCK(state); 1209 vkbd_clear_state_locked(state); 1210 VKBD_UNLOCK(state); 1211 } 1212 1213 /* Save the internal state */ 1214 static int 1215 vkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1216 { 1217 if (len == 0) 1218 return (sizeof(vkbd_state_t)); 1219 if (len < sizeof(vkbd_state_t)) 1220 return (-1); 1221 bcopy(kbd->kb_data, buf, sizeof(vkbd_state_t)); /* XXX locking? */ 1222 return (0); 1223 } 1224 1225 /* Set the internal state */ 1226 static int 1227 vkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1228 { 1229 if (len < sizeof(vkbd_state_t)) 1230 return (ENOMEM); 1231 bcopy(buf, kbd->kb_data, sizeof(vkbd_state_t)); /* XXX locking? */ 1232 return (0); 1233 } 1234 1235 /* Set polling */ 1236 static int 1237 vkbd_poll(keyboard_t *kbd, int on) 1238 { 1239 vkbd_state_t *state = NULL; 1240 1241 state = (vkbd_state_t *) kbd->kb_data; 1242 1243 VKBD_LOCK(state); 1244 1245 if (on) 1246 state->ks_polling ++; 1247 else 1248 state->ks_polling --; 1249 1250 VKBD_UNLOCK(state); 1251 1252 return (0); 1253 } 1254 1255 /* 1256 * Local functions 1257 */ 1258 1259 static int delays[] = { 250, 500, 750, 1000 }; 1260 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 1261 68, 76, 84, 92, 100, 110, 118, 126, 1262 136, 152, 168, 184, 200, 220, 236, 252, 1263 272, 304, 336, 368, 400, 440, 472, 504 }; 1264 1265 static int 1266 typematic_delay(int i) 1267 { 1268 return (delays[(i >> 5) & 3]); 1269 } 1270 1271 static int 1272 typematic_rate(int i) 1273 { 1274 return (rates[i & 0x1f]); 1275 } 1276 1277 static int 1278 typematic(int delay, int rate) 1279 { 1280 int value; 1281 int i; 1282 1283 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; i --) { 1284 if (delay >= delays[i]) 1285 break; 1286 } 1287 value = i << 5; 1288 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; i --) { 1289 if (rate >= rates[i]) 1290 break; 1291 } 1292 value |= i; 1293 return (value); 1294 } 1295 1296 /***************************************************************************** 1297 ***************************************************************************** 1298 ** Module 1299 ***************************************************************************** 1300 *****************************************************************************/ 1301 1302 KEYBOARD_DRIVER(vkbd, vkbdsw, vkbd_configure); 1303 1304 static int 1305 vkbd_modevent(module_t mod, int type, void *data) 1306 { 1307 static eventhandler_tag tag; 1308 1309 switch (type) { 1310 case MOD_LOAD: 1311 clone_setup(&vkbd_dev_clones); 1312 tag = EVENTHANDLER_REGISTER(dev_clone, vkbd_dev_clone, 0, 1000); 1313 if (tag == NULL) { 1314 clone_cleanup(&vkbd_dev_clones); 1315 return (ENOMEM); 1316 } 1317 kbd_add_driver(&vkbd_kbd_driver); 1318 break; 1319 1320 case MOD_UNLOAD: 1321 kbd_delete_driver(&vkbd_kbd_driver); 1322 EVENTHANDLER_DEREGISTER(dev_clone, tag); 1323 clone_cleanup(&vkbd_dev_clones); 1324 break; 1325 1326 default: 1327 return (EOPNOTSUPP); 1328 } 1329 1330 return (0); 1331 } 1332 1333 DEV_MODULE(vkbd, vkbd_modevent, NULL); 1334 1335