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