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 596 if (*kbdp == NULL) { 597 *kbdp = kbd = malloc(sizeof(*kbd), M_VKBD, M_NOWAIT | M_ZERO); 598 state = malloc(sizeof(*state), M_VKBD, M_NOWAIT | M_ZERO); 599 keymap = malloc(sizeof(key_map), M_VKBD, M_NOWAIT); 600 accmap = malloc(sizeof(accent_map), M_VKBD, M_NOWAIT); 601 fkeymap = malloc(sizeof(fkey_tab), M_VKBD, M_NOWAIT); 602 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]); 603 if ((kbd == NULL) || (state == NULL) || (keymap == NULL) || 604 (accmap == NULL) || (fkeymap == NULL)) { 605 if (state != NULL) 606 free(state, M_VKBD); 607 if (keymap != NULL) 608 free(keymap, M_VKBD); 609 if (accmap != NULL) 610 free(accmap, M_VKBD); 611 if (fkeymap != NULL) 612 free(fkeymap, M_VKBD); 613 if (kbd != NULL) 614 free(kbd, M_VKBD); 615 return (ENOMEM); 616 } 617 618 VKBD_LOCK_INIT(state); 619 state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; 620 TASK_INIT(&state->ks_task, 0, vkbd_dev_intr, (void *) kbd); 621 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { 622 return (0); 623 } else { 624 kbd = *kbdp; 625 state = (vkbd_state_t *) kbd->kb_data; 626 keymap = kbd->kb_keymap; 627 accmap = kbd->kb_accentmap; 628 fkeymap = kbd->kb_fkeytab; 629 fkeymap_size = kbd->kb_fkeytab_size; 630 } 631 632 if (!KBD_IS_PROBED(kbd)) { 633 kbd_init_struct(kbd, KEYBOARD_NAME, KB_OTHER, unit, flags, 0, 0); 634 bcopy(&key_map, keymap, sizeof(key_map)); 635 bcopy(&accent_map, accmap, sizeof(accent_map)); 636 bcopy(fkey_tab, fkeymap, 637 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab))); 638 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); 639 kbd->kb_data = (void *)state; 640 641 KBD_FOUND_DEVICE(kbd); 642 KBD_PROBE_DONE(kbd); 643 644 VKBD_LOCK(state); 645 vkbd_clear_state_locked(state); 646 state->ks_mode = K_XLATE; 647 /* FIXME: set the initial value for lock keys in ks_state */ 648 VKBD_UNLOCK(state); 649 } 650 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { 651 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; 652 653 vkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 654 delay[0] = kbd->kb_delay1; 655 delay[1] = kbd->kb_delay2; 656 vkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 657 658 KBD_INIT_DONE(kbd); 659 } 660 if (!KBD_IS_CONFIGURED(kbd)) { 661 if (kbd_register(kbd) < 0) 662 return (ENXIO); 663 KBD_CONFIG_DONE(kbd); 664 } 665 666 return (0); 667 } 668 669 /* Finish using this keyboard */ 670 static int 671 vkbd_term(keyboard_t *kbd) 672 { 673 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 674 675 kbd_unregister(kbd); 676 677 VKBD_LOCK_DESTROY(state); 678 bzero(state, sizeof(*state)); 679 free(state, M_VKBD); 680 681 free(kbd->kb_keymap, M_VKBD); 682 free(kbd->kb_accentmap, M_VKBD); 683 free(kbd->kb_fkeytab, M_VKBD); 684 free(kbd, M_VKBD); 685 686 return (0); 687 } 688 689 /* Keyboard interrupt routine */ 690 static int 691 vkbd_intr(keyboard_t *kbd, void *arg) 692 { 693 int c; 694 695 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { 696 /* let the callback function to process the input */ 697 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, 698 kbd->kb_callback.kc_arg); 699 } else { 700 /* read and discard the input; no one is waiting for input */ 701 do { 702 c = vkbd_read_char(kbd, FALSE); 703 } while (c != NOKEY); 704 } 705 706 return (0); 707 } 708 709 /* Test the interface to the device */ 710 static int 711 vkbd_test_if(keyboard_t *kbd) 712 { 713 return (0); 714 } 715 716 /* 717 * Enable the access to the device; until this function is called, 718 * the client cannot read from the keyboard. 719 */ 720 721 static int 722 vkbd_enable(keyboard_t *kbd) 723 { 724 KBD_ACTIVATE(kbd); 725 return (0); 726 } 727 728 /* Disallow the access to the device */ 729 static int 730 vkbd_disable(keyboard_t *kbd) 731 { 732 KBD_DEACTIVATE(kbd); 733 return (0); 734 } 735 736 /* Read one byte from the keyboard if it's allowed */ 737 static int 738 vkbd_read(keyboard_t *kbd, int wait) 739 { 740 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 741 int c; 742 743 VKBD_LOCK(state); 744 c = vkbd_data_read(state, wait); 745 VKBD_UNLOCK(state); 746 747 if (c != -1) 748 kbd->kb_count ++; 749 750 return (KBD_IS_ACTIVE(kbd)? c : -1); 751 } 752 753 /* Check if data is waiting */ 754 static int 755 vkbd_check(keyboard_t *kbd) 756 { 757 vkbd_state_t *state = NULL; 758 int ready; 759 760 if (!KBD_IS_ACTIVE(kbd)) 761 return (FALSE); 762 763 state = (vkbd_state_t *) kbd->kb_data; 764 765 VKBD_LOCK(state); 766 ready = vkbd_data_ready(state); 767 VKBD_UNLOCK(state); 768 769 return (ready); 770 } 771 772 /* Read char from the keyboard (stolen from atkbd.c) */ 773 static u_int 774 vkbd_read_char(keyboard_t *kbd, int wait) 775 { 776 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 777 u_int action; 778 int scancode, keycode; 779 780 VKBD_LOCK(state); 781 782 next_code: 783 784 /* do we have a composed char to return? */ 785 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { 786 action = state->ks_composed_char; 787 state->ks_composed_char = 0; 788 if (action > UCHAR_MAX) { 789 VKBD_UNLOCK(state); 790 return (ERRKEY); 791 } 792 793 VKBD_UNLOCK(state); 794 return (action); 795 } 796 797 /* see if there is something in the keyboard port */ 798 scancode = vkbd_data_read(state, wait); 799 if (scancode == -1) { 800 VKBD_UNLOCK(state); 801 return (NOKEY); 802 } 803 /* XXX FIXME: check for -1 if wait == 1! */ 804 805 kbd->kb_count ++; 806 807 /* return the byte as is for the K_RAW mode */ 808 if (state->ks_mode == K_RAW) { 809 VKBD_UNLOCK(state); 810 return (scancode); 811 } 812 813 /* translate the scan code into a keycode */ 814 keycode = scancode & 0x7F; 815 switch (state->ks_prefix) { 816 case 0x00: /* normal scancode */ 817 switch(scancode) { 818 case 0xB8: /* left alt (compose key) released */ 819 if (state->ks_flags & COMPOSE) { 820 state->ks_flags &= ~COMPOSE; 821 if (state->ks_composed_char > UCHAR_MAX) 822 state->ks_composed_char = 0; 823 } 824 break; 825 case 0x38: /* left alt (compose key) pressed */ 826 if (!(state->ks_flags & COMPOSE)) { 827 state->ks_flags |= COMPOSE; 828 state->ks_composed_char = 0; 829 } 830 break; 831 case 0xE0: 832 case 0xE1: 833 state->ks_prefix = scancode; 834 goto next_code; 835 } 836 break; 837 case 0xE0: /* 0xE0 prefix */ 838 state->ks_prefix = 0; 839 switch (keycode) { 840 case 0x1C: /* right enter key */ 841 keycode = 0x59; 842 break; 843 case 0x1D: /* right ctrl key */ 844 keycode = 0x5A; 845 break; 846 case 0x35: /* keypad divide key */ 847 keycode = 0x5B; 848 break; 849 case 0x37: /* print scrn key */ 850 keycode = 0x5C; 851 break; 852 case 0x38: /* right alt key (alt gr) */ 853 keycode = 0x5D; 854 break; 855 case 0x46: /* ctrl-pause/break on AT 101 (see below) */ 856 keycode = 0x68; 857 break; 858 case 0x47: /* grey home key */ 859 keycode = 0x5E; 860 break; 861 case 0x48: /* grey up arrow key */ 862 keycode = 0x5F; 863 break; 864 case 0x49: /* grey page up key */ 865 keycode = 0x60; 866 break; 867 case 0x4B: /* grey left arrow key */ 868 keycode = 0x61; 869 break; 870 case 0x4D: /* grey right arrow key */ 871 keycode = 0x62; 872 break; 873 case 0x4F: /* grey end key */ 874 keycode = 0x63; 875 break; 876 case 0x50: /* grey down arrow key */ 877 keycode = 0x64; 878 break; 879 case 0x51: /* grey page down key */ 880 keycode = 0x65; 881 break; 882 case 0x52: /* grey insert key */ 883 keycode = 0x66; 884 break; 885 case 0x53: /* grey delete key */ 886 keycode = 0x67; 887 break; 888 /* the following 3 are only used on the MS "Natural" keyboard */ 889 case 0x5b: /* left Window key */ 890 keycode = 0x69; 891 break; 892 case 0x5c: /* right Window key */ 893 keycode = 0x6a; 894 break; 895 case 0x5d: /* menu key */ 896 keycode = 0x6b; 897 break; 898 case 0x5e: /* power key */ 899 keycode = 0x6d; 900 break; 901 case 0x5f: /* sleep key */ 902 keycode = 0x6e; 903 break; 904 case 0x63: /* wake key */ 905 keycode = 0x6f; 906 break; 907 default: /* ignore everything else */ 908 goto next_code; 909 } 910 break; 911 case 0xE1: /* 0xE1 prefix */ 912 /* 913 * The pause/break key on the 101 keyboard produces: 914 * E1-1D-45 E1-9D-C5 915 * Ctrl-pause/break produces: 916 * E0-46 E0-C6 (See above.) 917 */ 918 state->ks_prefix = 0; 919 if (keycode == 0x1D) 920 state->ks_prefix = 0x1D; 921 goto next_code; 922 /* NOT REACHED */ 923 case 0x1D: /* pause / break */ 924 state->ks_prefix = 0; 925 if (keycode != 0x45) 926 goto next_code; 927 keycode = 0x68; 928 break; 929 } 930 931 if (kbd->kb_type == KB_84) { 932 switch (keycode) { 933 case 0x37: /* *(numpad)/print screen */ 934 if (state->ks_flags & SHIFTS) 935 keycode = 0x5c; /* print screen */ 936 break; 937 case 0x45: /* num lock/pause */ 938 if (state->ks_flags & CTLS) 939 keycode = 0x68; /* pause */ 940 break; 941 case 0x46: /* scroll lock/break */ 942 if (state->ks_flags & CTLS) 943 keycode = 0x6c; /* break */ 944 break; 945 } 946 } else if (kbd->kb_type == KB_101) { 947 switch (keycode) { 948 case 0x5c: /* print screen */ 949 if (state->ks_flags & ALTS) 950 keycode = 0x54; /* sysrq */ 951 break; 952 case 0x68: /* pause/break */ 953 if (state->ks_flags & CTLS) 954 keycode = 0x6c; /* break */ 955 break; 956 } 957 } 958 959 /* return the key code in the K_CODE mode */ 960 if (state->ks_mode == K_CODE) { 961 VKBD_UNLOCK(state); 962 return (keycode | (scancode & 0x80)); 963 } 964 965 /* compose a character code */ 966 if (state->ks_flags & COMPOSE) { 967 switch (keycode | (scancode & 0x80)) { 968 /* key pressed, process it */ 969 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ 970 state->ks_composed_char *= 10; 971 state->ks_composed_char += keycode - 0x40; 972 if (state->ks_composed_char > UCHAR_MAX) { 973 VKBD_UNLOCK(state); 974 return (ERRKEY); 975 } 976 goto next_code; 977 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ 978 state->ks_composed_char *= 10; 979 state->ks_composed_char += keycode - 0x47; 980 if (state->ks_composed_char > UCHAR_MAX) { 981 VKBD_UNLOCK(state); 982 return (ERRKEY); 983 } 984 goto next_code; 985 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ 986 state->ks_composed_char *= 10; 987 state->ks_composed_char += keycode - 0x4E; 988 if (state->ks_composed_char > UCHAR_MAX) { 989 VKBD_UNLOCK(state); 990 return (ERRKEY); 991 } 992 goto next_code; 993 case 0x52: /* keypad 0 */ 994 state->ks_composed_char *= 10; 995 if (state->ks_composed_char > UCHAR_MAX) { 996 VKBD_UNLOCK(state); 997 return (ERRKEY); 998 } 999 goto next_code; 1000 1001 /* key released, no interest here */ 1002 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ 1003 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ 1004 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ 1005 case 0xD2: /* keypad 0 */ 1006 goto next_code; 1007 1008 case 0x38: /* left alt key */ 1009 break; 1010 1011 default: 1012 if (state->ks_composed_char > 0) { 1013 state->ks_flags &= ~COMPOSE; 1014 state->ks_composed_char = 0; 1015 VKBD_UNLOCK(state); 1016 return (ERRKEY); 1017 } 1018 break; 1019 } 1020 } 1021 1022 /* keycode to key action */ 1023 action = genkbd_keyaction(kbd, keycode, scancode & 0x80, 1024 &state->ks_state, &state->ks_accents); 1025 if (action == NOKEY) 1026 goto next_code; 1027 1028 VKBD_UNLOCK(state); 1029 1030 return (action); 1031 } 1032 1033 /* Check if char is waiting */ 1034 static int 1035 vkbd_check_char(keyboard_t *kbd) 1036 { 1037 vkbd_state_t *state = NULL; 1038 int ready; 1039 1040 if (!KBD_IS_ACTIVE(kbd)) 1041 return (FALSE); 1042 1043 state = (vkbd_state_t *) kbd->kb_data; 1044 1045 VKBD_LOCK(state); 1046 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) 1047 ready = TRUE; 1048 else 1049 ready = vkbd_data_ready(state); 1050 VKBD_UNLOCK(state); 1051 1052 return (ready); 1053 } 1054 1055 /* Some useful control functions (stolen from atkbd.c) */ 1056 static int 1057 vkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 1058 { 1059 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 1060 int i; 1061 1062 VKBD_LOCK(state); 1063 1064 switch (cmd) { 1065 case KDGKBMODE: /* get keyboard mode */ 1066 *(int *)arg = state->ks_mode; 1067 break; 1068 1069 case KDSKBMODE: /* set keyboard mode */ 1070 switch (*(int *)arg) { 1071 case K_XLATE: 1072 if (state->ks_mode != K_XLATE) { 1073 /* make lock key state and LED state match */ 1074 state->ks_state &= ~LOCK_MASK; 1075 state->ks_state |= KBD_LED_VAL(kbd); 1076 vkbd_status_changed(state); 1077 } 1078 /* FALLTHROUGH */ 1079 1080 case K_RAW: 1081 case K_CODE: 1082 if (state->ks_mode != *(int *)arg) { 1083 vkbd_clear_state_locked(state); 1084 state->ks_mode = *(int *)arg; 1085 vkbd_status_changed(state); 1086 } 1087 break; 1088 1089 default: 1090 VKBD_UNLOCK(state); 1091 return (EINVAL); 1092 } 1093 break; 1094 1095 case KDGETLED: /* get keyboard LED */ 1096 *(int *)arg = KBD_LED_VAL(kbd); 1097 break; 1098 1099 case KDSETLED: /* set keyboard LED */ 1100 /* NOTE: lock key state in ks_state won't be changed */ 1101 if (*(int *)arg & ~LOCK_MASK) { 1102 VKBD_UNLOCK(state); 1103 return (EINVAL); 1104 } 1105 1106 i = *(int *)arg; 1107 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 1108 if (state->ks_mode == K_XLATE && 1109 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 1110 if (i & ALKED) 1111 i |= CLKED; 1112 else 1113 i &= ~CLKED; 1114 } 1115 1116 KBD_LED_VAL(kbd) = *(int *)arg; 1117 vkbd_status_changed(state); 1118 break; 1119 1120 case KDGKBSTATE: /* get lock key state */ 1121 *(int *)arg = state->ks_state & LOCK_MASK; 1122 break; 1123 1124 case KDSKBSTATE: /* set lock key state */ 1125 if (*(int *)arg & ~LOCK_MASK) { 1126 VKBD_UNLOCK(state); 1127 return (EINVAL); 1128 } 1129 state->ks_state &= ~LOCK_MASK; 1130 state->ks_state |= *(int *)arg; 1131 vkbd_status_changed(state); 1132 VKBD_UNLOCK(state); 1133 /* set LEDs and quit */ 1134 return (vkbd_ioctl(kbd, KDSETLED, arg)); 1135 1136 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ 1137 i = typematic(((int *)arg)[0], ((int *)arg)[1]); 1138 kbd->kb_delay1 = typematic_delay(i); 1139 kbd->kb_delay2 = typematic_rate(i); 1140 vkbd_status_changed(state); 1141 break; 1142 1143 case KDSETRAD: /* set keyboard repeat rate (old interface) */ 1144 kbd->kb_delay1 = typematic_delay(*(int *)arg); 1145 kbd->kb_delay2 = typematic_rate(*(int *)arg); 1146 vkbd_status_changed(state); 1147 break; 1148 1149 case PIO_KEYMAP: /* set keyboard translation table */ 1150 case PIO_KEYMAPENT: /* set keyboard translation table entry */ 1151 case PIO_DEADKEYMAP: /* set accent key translation table */ 1152 state->ks_accents = 0; 1153 /* FALLTHROUGH */ 1154 1155 default: 1156 VKBD_UNLOCK(state); 1157 return (genkbd_commonioctl(kbd, cmd, arg)); 1158 } 1159 1160 VKBD_UNLOCK(state); 1161 1162 return (0); 1163 } 1164 1165 /* Lock the access to the keyboard */ 1166 static int 1167 vkbd_lock(keyboard_t *kbd, int lock) 1168 { 1169 return (1); /* XXX */ 1170 } 1171 1172 /* Clear the internal state of the keyboard */ 1173 static void 1174 vkbd_clear_state_locked(vkbd_state_t *state) 1175 { 1176 VKBD_LOCK_ASSERT(state, MA_OWNED); 1177 1178 state->ks_flags = 0; 1179 state->ks_polling = 0; 1180 state->ks_state &= LOCK_MASK; /* preserve locking key state */ 1181 state->ks_accents = 0; 1182 state->ks_composed_char = 0; 1183 /* state->ks_prefix = 0; XXX */ 1184 1185 /* flush ks_inq and wakeup writers/poll()ers */ 1186 state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; 1187 selwakeuppri(&state->ks_wsel, PZERO + 1); 1188 wakeup(&state->ks_inq); 1189 } 1190 1191 static void 1192 vkbd_clear_state(keyboard_t *kbd) 1193 { 1194 vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; 1195 1196 VKBD_LOCK(state); 1197 vkbd_clear_state_locked(state); 1198 VKBD_UNLOCK(state); 1199 } 1200 1201 /* Save the internal state */ 1202 static int 1203 vkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 1204 { 1205 if (len == 0) 1206 return (sizeof(vkbd_state_t)); 1207 if (len < sizeof(vkbd_state_t)) 1208 return (-1); 1209 bcopy(kbd->kb_data, buf, sizeof(vkbd_state_t)); /* XXX locking? */ 1210 return (0); 1211 } 1212 1213 /* Set the internal state */ 1214 static int 1215 vkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 1216 { 1217 if (len < sizeof(vkbd_state_t)) 1218 return (ENOMEM); 1219 bcopy(buf, kbd->kb_data, sizeof(vkbd_state_t)); /* XXX locking? */ 1220 return (0); 1221 } 1222 1223 /* Set polling */ 1224 static int 1225 vkbd_poll(keyboard_t *kbd, int on) 1226 { 1227 vkbd_state_t *state = NULL; 1228 1229 state = (vkbd_state_t *) kbd->kb_data; 1230 1231 VKBD_LOCK(state); 1232 1233 if (on) 1234 state->ks_polling ++; 1235 else 1236 state->ks_polling --; 1237 1238 VKBD_UNLOCK(state); 1239 1240 return (0); 1241 } 1242 1243 /* 1244 * Local functions 1245 */ 1246 1247 static int delays[] = { 250, 500, 750, 1000 }; 1248 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 1249 68, 76, 84, 92, 100, 110, 118, 126, 1250 136, 152, 168, 184, 200, 220, 236, 252, 1251 272, 304, 336, 368, 400, 440, 472, 504 }; 1252 1253 static int 1254 typematic_delay(int i) 1255 { 1256 return (delays[(i >> 5) & 3]); 1257 } 1258 1259 static int 1260 typematic_rate(int i) 1261 { 1262 return (rates[i & 0x1f]); 1263 } 1264 1265 static int 1266 typematic(int delay, int rate) 1267 { 1268 int value; 1269 int i; 1270 1271 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; i --) { 1272 if (delay >= delays[i]) 1273 break; 1274 } 1275 value = i << 5; 1276 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; i --) { 1277 if (rate >= rates[i]) 1278 break; 1279 } 1280 value |= i; 1281 return (value); 1282 } 1283 1284 /***************************************************************************** 1285 ***************************************************************************** 1286 ** Module 1287 ***************************************************************************** 1288 *****************************************************************************/ 1289 1290 KEYBOARD_DRIVER(vkbd, vkbdsw, vkbd_configure); 1291 1292 static int 1293 vkbd_modevent(module_t mod, int type, void *data) 1294 { 1295 static eventhandler_tag tag; 1296 1297 switch (type) { 1298 case MOD_LOAD: 1299 clone_setup(&vkbd_dev_clones); 1300 tag = EVENTHANDLER_REGISTER(dev_clone, vkbd_dev_clone, 0, 1000); 1301 if (tag == NULL) { 1302 clone_cleanup(&vkbd_dev_clones); 1303 return (ENOMEM); 1304 } 1305 kbd_add_driver(&vkbd_kbd_driver); 1306 break; 1307 1308 case MOD_UNLOAD: 1309 kbd_delete_driver(&vkbd_kbd_driver); 1310 EVENTHANDLER_DEREGISTER(dev_clone, tag); 1311 clone_cleanup(&vkbd_dev_clones); 1312 break; 1313 1314 default: 1315 return (EOPNOTSUPP); 1316 } 1317 1318 return (0); 1319 } 1320 1321 DEV_MODULE(vkbd, vkbd_modevent, NULL); 1322 1323