1 /*- 2 * Copyright (c) 2017 Microsoft Corp. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_evdev.h" 31 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/conf.h> 35 #include <sys/uio.h> 36 #include <sys/bus.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/module.h> 40 #include <sys/limits.h> 41 #include <sys/lock.h> 42 #include <sys/taskqueue.h> 43 #include <sys/selinfo.h> 44 #include <sys/sysctl.h> 45 #include <sys/poll.h> 46 #include <sys/proc.h> 47 #include <sys/queue.h> 48 #include <sys/kthread.h> 49 #include <sys/syscallsubr.h> 50 #include <sys/sysproto.h> 51 #include <sys/sema.h> 52 #include <sys/signal.h> 53 #include <sys/syslog.h> 54 #include <sys/systm.h> 55 #include <sys/mutex.h> 56 #include <sys/callout.h> 57 58 #include <sys/kbio.h> 59 #include <dev/kbd/kbdreg.h> 60 #include <dev/kbd/kbdtables.h> 61 62 #ifdef EVDEV_SUPPORT 63 #include <dev/evdev/evdev.h> 64 #include <dev/evdev/input.h> 65 #endif 66 67 #include "dev/hyperv/input/hv_kbdc.h" 68 69 #define HVKBD_MTX_LOCK(_m) do { \ 70 mtx_lock(_m); \ 71 } while (0) 72 73 #define HVKBD_MTX_UNLOCK(_m) do { \ 74 mtx_unlock(_m); \ 75 } while (0) 76 77 #define HVKBD_MTX_ASSERT(_m, _t) do { \ 78 mtx_assert(_m, _t); \ 79 } while (0) 80 81 #define HVKBD_LOCK() HVKBD_MTX_LOCK(&Giant) 82 #define HVKBD_UNLOCK() HVKBD_MTX_UNLOCK(&Giant) 83 #define HVKBD_LOCK_ASSERT() HVKBD_MTX_ASSERT(&Giant, MA_OWNED) 84 85 #define HVKBD_FLAG_COMPOSE 0x00000001 /* compose char flag */ 86 #define HVKBD_FLAG_POLLING 0x00000002 87 88 #ifdef EVDEV_SUPPORT 89 static evdev_event_t hvkbd_ev_event; 90 91 static const struct evdev_methods hvkbd_evdev_methods = { 92 .ev_event = hvkbd_ev_event, 93 }; 94 #endif 95 96 /* early keyboard probe, not supported */ 97 static int 98 hvkbd_configure(int flags) 99 { 100 return (0); 101 } 102 103 /* detect a keyboard, not used */ 104 static int 105 hvkbd_probe(int unit, void *arg, int flags) 106 { 107 return (ENXIO); 108 } 109 110 /* reset and initialize the device, not used */ 111 static int 112 hvkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 113 { 114 DEBUG_HVKBD(*kbdp, "%s\n", __func__); 115 return (ENXIO); 116 } 117 118 /* test the interface to the device, not used */ 119 static int 120 hvkbd_test_if(keyboard_t *kbd) 121 { 122 DEBUG_HVKBD(kbd, "%s\n", __func__); 123 return (0); 124 } 125 126 /* finish using this keyboard, not used */ 127 static int 128 hvkbd_term(keyboard_t *kbd) 129 { 130 DEBUG_HVKBD(kbd, "%s\n", __func__); 131 return (ENXIO); 132 } 133 134 /* keyboard interrupt routine, not used */ 135 static int 136 hvkbd_intr(keyboard_t *kbd, void *arg) 137 { 138 DEBUG_HVKBD(kbd, "%s\n", __func__); 139 return (0); 140 } 141 142 /* lock the access to the keyboard, not used */ 143 static int 144 hvkbd_lock(keyboard_t *kbd, int lock) 145 { 146 DEBUG_HVKBD(kbd, "%s\n", __func__); 147 return (1); 148 } 149 150 /* save the internal state, not used */ 151 static int 152 hvkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 153 { 154 DEBUG_HVKBD(kbd,"%s\n", __func__); 155 return (len == 0) ? 1 : -1; 156 } 157 158 /* set the internal state, not used */ 159 static int 160 hvkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 161 { 162 DEBUG_HVKBD(kbd, "%s\n", __func__); 163 return (EINVAL); 164 } 165 166 static int 167 hvkbd_poll(keyboard_t *kbd, int on) 168 { 169 hv_kbd_sc *sc = kbd->kb_data; 170 171 HVKBD_LOCK(); 172 /* 173 * Keep a reference count on polling to allow recursive 174 * cngrab() during a panic for example. 175 */ 176 if (on) 177 sc->sc_polling++; 178 else if (sc->sc_polling > 0) 179 sc->sc_polling--; 180 181 if (sc->sc_polling != 0) { 182 sc->sc_flags |= HVKBD_FLAG_POLLING; 183 } else { 184 sc->sc_flags &= ~HVKBD_FLAG_POLLING; 185 } 186 HVKBD_UNLOCK(); 187 return (0); 188 } 189 190 /* 191 * Enable the access to the device; until this function is called, 192 * the client cannot read from the keyboard. 193 */ 194 static int 195 hvkbd_enable(keyboard_t *kbd) 196 { 197 HVKBD_LOCK(); 198 KBD_ACTIVATE(kbd); 199 HVKBD_UNLOCK(); 200 return (0); 201 } 202 203 /* disallow the access to the device */ 204 static int 205 hvkbd_disable(keyboard_t *kbd) 206 { 207 DEBUG_HVKBD(kbd, "%s\n", __func__); 208 HVKBD_LOCK(); 209 KBD_DEACTIVATE(kbd); 210 HVKBD_UNLOCK(); 211 return (0); 212 } 213 214 static void 215 hvkbd_do_poll(hv_kbd_sc *sc, uint8_t wait) 216 { 217 while (!hv_kbd_prod_is_ready(sc)) { 218 hv_kbd_read_channel(sc->hs_chan, sc); 219 if (!wait) 220 break; 221 } 222 } 223 224 /* check if data is waiting */ 225 /* Currently unused. */ 226 static int 227 hvkbd_check(keyboard_t *kbd) 228 { 229 DEBUG_HVKBD(kbd, "%s\n", __func__); 230 return (0); 231 } 232 233 /* check if char is waiting */ 234 static int 235 hvkbd_check_char_locked(keyboard_t *kbd) 236 { 237 HVKBD_LOCK_ASSERT(); 238 if (!KBD_IS_ACTIVE(kbd)) 239 return (FALSE); 240 241 hv_kbd_sc *sc = kbd->kb_data; 242 if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char != 0) 243 return (TRUE); 244 if (sc->sc_flags & HVKBD_FLAG_POLLING) 245 hvkbd_do_poll(sc, 0); 246 if (hv_kbd_prod_is_ready(sc)) { 247 return (TRUE); 248 } 249 return (FALSE); 250 } 251 252 static int 253 hvkbd_check_char(keyboard_t *kbd) 254 { 255 int result; 256 257 HVKBD_LOCK(); 258 result = hvkbd_check_char_locked(kbd); 259 HVKBD_UNLOCK(); 260 261 return (result); 262 } 263 264 /* read char from the keyboard */ 265 static uint32_t 266 hvkbd_read_char_locked(keyboard_t *kbd, int wait) 267 { 268 uint32_t scancode = NOKEY; 269 uint32_t action; 270 keystroke ks; 271 hv_kbd_sc *sc = kbd->kb_data; 272 int keycode; 273 274 HVKBD_LOCK_ASSERT(); 275 276 if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc)) 277 return (NOKEY); 278 279 next_code: 280 281 /* do we have a composed char to return? */ 282 if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char > 0) { 283 action = sc->sc_composed_char; 284 sc->sc_composed_char = 0; 285 if (action > UCHAR_MAX) { 286 return (ERRKEY); 287 } 288 return (action); 289 } 290 291 if (hv_kbd_fetch_top(sc, &ks)) { 292 return (NOKEY); 293 } 294 if ((ks.info & IS_E0) || (ks.info & IS_E1)) { 295 /** 296 * Emulate the generation of E0 or E1 scancode, 297 * the real scancode will be consumed next time. 298 */ 299 if (ks.info & IS_E0) { 300 scancode = XTKBD_EMUL0; 301 ks.info &= ~IS_E0; 302 } else if (ks.info & IS_E1) { 303 scancode = XTKBD_EMUL1; 304 ks.info &= ~IS_E1; 305 } 306 /** 307 * Change the top item to avoid encountering 308 * E0 or E1 twice. 309 */ 310 hv_kbd_modify_top(sc, &ks); 311 } else if (ks.info & IS_UNICODE) { 312 /** 313 * XXX: Hyperv host send unicode to VM through 314 * 'Type clipboard text', the mapping from 315 * unicode to scancode depends on the keymap. 316 * It is so complicated that we do not plan to 317 * support it yet. 318 */ 319 if (bootverbose) 320 device_printf(sc->dev, "Unsupported unicode\n"); 321 hv_kbd_remove_top(sc); 322 return (NOKEY); 323 } else { 324 scancode = ks.makecode; 325 if (ks.info & IS_BREAK) { 326 scancode |= XTKBD_RELEASE; 327 } 328 hv_kbd_remove_top(sc); 329 } 330 #ifdef EVDEV_SUPPORT 331 /* push evdev event */ 332 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && 333 sc->ks_evdev != NULL) { 334 keycode = evdev_scancode2key(&sc->ks_evdev_state, 335 scancode); 336 337 if (keycode != KEY_RESERVED) { 338 evdev_push_event(sc->ks_evdev, EV_KEY, 339 (uint16_t)keycode, scancode & 0x80 ? 0 : 1); 340 evdev_sync(sc->ks_evdev); 341 } 342 } 343 #endif 344 ++kbd->kb_count; 345 DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode); 346 347 /* return the byte as is for the K_RAW mode */ 348 if (sc->sc_mode == K_RAW) 349 return scancode; 350 351 /* translate the scan code into a keycode */ 352 keycode = scancode & 0x7F; 353 switch (sc->sc_prefix) { 354 case 0x00: /* normal scancode */ 355 switch(scancode) { 356 case 0xB8: /* left alt (compose key) released */ 357 if (sc->sc_flags & HVKBD_FLAG_COMPOSE) { 358 sc->sc_flags &= ~HVKBD_FLAG_COMPOSE; 359 if (sc->sc_composed_char > UCHAR_MAX) 360 sc->sc_composed_char = 0; 361 } 362 break; 363 case 0x38: /* left alt (compose key) pressed */ 364 if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE)) { 365 sc->sc_flags |= HVKBD_FLAG_COMPOSE; 366 sc->sc_composed_char = 0; 367 } 368 break; 369 case 0xE0: 370 case 0xE1: 371 sc->sc_prefix = scancode; 372 goto next_code; 373 } 374 break; 375 case 0xE0: /* 0xE0 prefix */ 376 sc->sc_prefix = 0; 377 switch (keycode) { 378 case 0x1C: /* right enter key */ 379 keycode = 0x59; 380 break; 381 case 0x1D: /* right ctrl key */ 382 keycode = 0x5A; 383 break; 384 case 0x35: /* keypad divide key */ 385 keycode = 0x5B; 386 break; 387 case 0x37: /* print scrn key */ 388 keycode = 0x5C; 389 break; 390 case 0x38: /* right alt key (alt gr) */ 391 keycode = 0x5D; 392 break; 393 case 0x46: /* ctrl-pause/break on AT 101 (see below) */ 394 keycode = 0x68; 395 break; 396 case 0x47: /* grey home key */ 397 keycode = 0x5E; 398 break; 399 case 0x48: /* grey up arrow key */ 400 keycode = 0x5F; 401 break; 402 case 0x49: /* grey page up key */ 403 keycode = 0x60; 404 break; 405 case 0x4B: /* grey left arrow key */ 406 keycode = 0x61; 407 break; 408 case 0x4D: /* grey right arrow key */ 409 keycode = 0x62; 410 break; 411 case 0x4F: /* grey end key */ 412 keycode = 0x63; 413 break; 414 case 0x50: /* grey down arrow key */ 415 keycode = 0x64; 416 break; 417 case 0x51: /* grey page down key */ 418 keycode = 0x65; 419 break; 420 case 0x52: /* grey insert key */ 421 keycode = 0x66; 422 break; 423 case 0x53: /* grey delete key */ 424 keycode = 0x67; 425 break; 426 /* the following 3 are only used on the MS "Natural" keyboard */ 427 case 0x5b: /* left Window key */ 428 keycode = 0x69; 429 break; 430 case 0x5c: /* right Window key */ 431 keycode = 0x6a; 432 break; 433 case 0x5d: /* menu key */ 434 keycode = 0x6b; 435 break; 436 case 0x5e: /* power key */ 437 keycode = 0x6d; 438 break; 439 case 0x5f: /* sleep key */ 440 keycode = 0x6e; 441 break; 442 case 0x63: /* wake key */ 443 keycode = 0x6f; 444 break; 445 default: /* ignore everything else */ 446 goto next_code; 447 } 448 break; 449 case 0xE1: /* 0xE1 prefix */ 450 /* 451 * The pause/break key on the 101 keyboard produces: 452 * E1-1D-45 E1-9D-C5 453 * Ctrl-pause/break produces: 454 * E0-46 E0-C6 (See above.) 455 */ 456 sc->sc_prefix = 0; 457 if (keycode == 0x1D) 458 sc->sc_prefix = 0x1D; 459 goto next_code; 460 /* NOT REACHED */ 461 case 0x1D: /* pause / break */ 462 sc->sc_prefix = 0; 463 if (keycode != 0x45) 464 goto next_code; 465 keycode = 0x68; 466 break; 467 } 468 469 /* XXX assume 101/102 keys AT keyboard */ 470 switch (keycode) { 471 case 0x5c: /* print screen */ 472 if (sc->sc_flags & ALTS) 473 keycode = 0x54; /* sysrq */ 474 break; 475 case 0x68: /* pause/break */ 476 if (sc->sc_flags & CTLS) 477 keycode = 0x6c; /* break */ 478 break; 479 } 480 481 /* return the key code in the K_CODE mode */ 482 if (sc->sc_mode == K_CODE) 483 return (keycode | (scancode & 0x80)); 484 485 /* compose a character code */ 486 if (sc->sc_flags & HVKBD_FLAG_COMPOSE) { 487 switch (keycode | (scancode & 0x80)) { 488 /* key pressed, process it */ 489 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ 490 sc->sc_composed_char *= 10; 491 sc->sc_composed_char += keycode - 0x40; 492 if (sc->sc_composed_char > UCHAR_MAX) 493 return ERRKEY; 494 goto next_code; 495 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ 496 sc->sc_composed_char *= 10; 497 sc->sc_composed_char += keycode - 0x47; 498 if (sc->sc_composed_char > UCHAR_MAX) 499 return ERRKEY; 500 goto next_code; 501 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ 502 sc->sc_composed_char *= 10; 503 sc->sc_composed_char += keycode - 0x4E; 504 if (sc->sc_composed_char > UCHAR_MAX) 505 return ERRKEY; 506 goto next_code; 507 case 0x52: /* keypad 0 */ 508 sc->sc_composed_char *= 10; 509 if (sc->sc_composed_char > UCHAR_MAX) 510 return ERRKEY; 511 goto next_code; 512 513 /* key released, no interest here */ 514 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ 515 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ 516 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ 517 case 0xD2: /* keypad 0 */ 518 goto next_code; 519 520 case 0x38: /* left alt key */ 521 break; 522 523 default: 524 if (sc->sc_composed_char > 0) { 525 sc->sc_flags &= ~HVKBD_FLAG_COMPOSE; 526 sc->sc_composed_char = 0; 527 return (ERRKEY); 528 } 529 break; 530 } 531 } 532 533 /* keycode to key action */ 534 action = genkbd_keyaction(kbd, keycode, scancode & 0x80, 535 &sc->sc_state, &sc->sc_accents); 536 if (action == NOKEY) 537 goto next_code; 538 else 539 return (action); 540 } 541 542 /* Currently wait is always false. */ 543 static uint32_t 544 hvkbd_read_char(keyboard_t *kbd, int wait) 545 { 546 uint32_t keycode; 547 548 HVKBD_LOCK(); 549 keycode = hvkbd_read_char_locked(kbd, wait); 550 HVKBD_UNLOCK(); 551 552 return (keycode); 553 } 554 555 /* clear the internal state of the keyboard */ 556 static void 557 hvkbd_clear_state(keyboard_t *kbd) 558 { 559 hv_kbd_sc *sc = kbd->kb_data; 560 sc->sc_state &= LOCK_MASK; /* preserve locking key state */ 561 sc->sc_flags &= ~(HVKBD_FLAG_POLLING | HVKBD_FLAG_COMPOSE); 562 sc->sc_accents = 0; 563 sc->sc_composed_char = 0; 564 } 565 566 static int 567 hvkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg) 568 { 569 int i; 570 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 571 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 572 int ival; 573 #endif 574 hv_kbd_sc *sc = kbd->kb_data; 575 switch (cmd) { 576 case KDGKBMODE: 577 *(int *)arg = sc->sc_mode; 578 break; 579 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 580 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 581 case _IO('K', 7): 582 ival = IOCPARM_IVAL(arg); 583 arg = (caddr_t)&ival; 584 /* FALLTHROUGH */ 585 #endif 586 case KDSKBMODE: /* set keyboard mode */ 587 DEBUG_HVKBD(kbd, "expected mode: %x\n", *(int *)arg); 588 switch (*(int *)arg) { 589 case K_XLATE: 590 if (sc->sc_mode != K_XLATE) { 591 /* make lock key state and LED state match */ 592 sc->sc_state &= ~LOCK_MASK; 593 sc->sc_state |= KBD_LED_VAL(kbd); 594 } 595 /* FALLTHROUGH */ 596 case K_RAW: 597 case K_CODE: 598 if (sc->sc_mode != *(int *)arg) { 599 DEBUG_HVKBD(kbd, "mod changed to %x\n", *(int *)arg); 600 if ((sc->sc_flags & HVKBD_FLAG_POLLING) == 0) 601 hvkbd_clear_state(kbd); 602 sc->sc_mode = *(int *)arg; 603 } 604 break; 605 default: 606 return (EINVAL); 607 } 608 break; 609 case KDGKBSTATE: /* get lock key state */ 610 *(int *)arg = sc->sc_state & LOCK_MASK; 611 break; 612 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 613 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 614 case _IO('K', 20): 615 ival = IOCPARM_IVAL(arg); 616 arg = (caddr_t)&ival; 617 /* FALLTHROUGH */ 618 #endif 619 case KDSKBSTATE: /* set lock key state */ 620 if (*(int *)arg & ~LOCK_MASK) { 621 return (EINVAL); 622 } 623 sc->sc_state &= ~LOCK_MASK; 624 sc->sc_state |= *(int *)arg; 625 return hvkbd_ioctl_locked(kbd, KDSETLED, arg); 626 case KDGETLED: /* get keyboard LED */ 627 *(int *)arg = KBD_LED_VAL(kbd); 628 break; 629 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 630 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 631 case _IO('K', 66): 632 ival = IOCPARM_IVAL(arg); 633 arg = (caddr_t)&ival; 634 /* FALLTHROUGH */ 635 #endif 636 case KDSETLED: /* set keyboard LED */ 637 /* NOTE: lock key state in "sc_state" won't be changed */ 638 if (*(int *)arg & ~LOCK_MASK) 639 return (EINVAL); 640 641 i = *(int *)arg; 642 643 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 644 if (sc->sc_mode == K_XLATE && 645 kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 646 if (i & ALKED) 647 i |= CLKED; 648 else 649 i &= ~CLKED; 650 } 651 if (KBD_HAS_DEVICE(kbd)) { 652 DEBUG_HVSC(sc, "setled 0x%x\n", *(int *)arg); 653 } 654 655 #ifdef EVDEV_SUPPORT 656 /* push LED states to evdev */ 657 if (sc->ks_evdev != NULL && 658 evdev_rcpt_mask & EVDEV_RCPT_HW_KBD) 659 evdev_push_leds(sc->ks_evdev, *(int *)arg); 660 #endif 661 KBD_LED_VAL(kbd) = *(int *)arg; 662 break; 663 case PIO_KEYMAP: /* set keyboard translation table */ 664 case OPIO_KEYMAP: /* set keyboard translation table (compat) */ 665 case PIO_KEYMAPENT: /* set keyboard translation table entry */ 666 case PIO_DEADKEYMAP: /* set accent key translation table */ 667 sc->sc_accents = 0; 668 /* FALLTHROUGH */ 669 default: 670 return (genkbd_commonioctl(kbd, cmd, arg)); 671 } 672 return (0); 673 } 674 675 /* some useful control functions */ 676 static int 677 hvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 678 { 679 DEBUG_HVKBD(kbd, "%s: %lx start\n", __func__, cmd); 680 HVKBD_LOCK(); 681 int ret = hvkbd_ioctl_locked(kbd, cmd, arg); 682 HVKBD_UNLOCK(); 683 DEBUG_HVKBD(kbd, "%s: %lx end %d\n", __func__, cmd, ret); 684 return (ret); 685 } 686 687 /* read one byte from the keyboard if it's allowed */ 688 /* Currently unused. */ 689 static int 690 hvkbd_read(keyboard_t *kbd, int wait) 691 { 692 DEBUG_HVKBD(kbd, "%s\n", __func__); 693 HVKBD_LOCK_ASSERT(); 694 if (!KBD_IS_ACTIVE(kbd)) 695 return (-1); 696 return hvkbd_read_char_locked(kbd, wait); 697 } 698 699 #ifdef EVDEV_SUPPORT 700 static void 701 hvkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 702 int32_t value) 703 { 704 keyboard_t *kbd = evdev_get_softc(evdev); 705 706 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && 707 (type == EV_LED || type == EV_REP)) { 708 mtx_lock(&Giant); 709 kbd_ev_event(kbd, type, code, value); 710 mtx_unlock(&Giant); 711 } 712 } 713 #endif 714 715 static keyboard_switch_t hvkbdsw = { 716 .probe = hvkbd_probe, /* not used */ 717 .init = hvkbd_init, 718 .term = hvkbd_term, /* not used */ 719 .intr = hvkbd_intr, /* not used */ 720 .test_if = hvkbd_test_if, /* not used */ 721 .enable = hvkbd_enable, 722 .disable = hvkbd_disable, 723 .read = hvkbd_read, 724 .check = hvkbd_check, 725 .read_char = hvkbd_read_char, 726 .check_char = hvkbd_check_char, 727 .ioctl = hvkbd_ioctl, 728 .lock = hvkbd_lock, /* not used */ 729 .clear_state = hvkbd_clear_state, 730 .get_state = hvkbd_get_state, /* not used */ 731 .set_state = hvkbd_set_state, /* not used */ 732 .poll = hvkbd_poll, 733 }; 734 735 KEYBOARD_DRIVER(hvkbd, hvkbdsw, hvkbd_configure); 736 737 void 738 hv_kbd_intr(hv_kbd_sc *sc) 739 { 740 uint32_t c; 741 if ((sc->sc_flags & HVKBD_FLAG_POLLING) != 0) 742 return; 743 744 if (KBD_IS_ACTIVE(&sc->sc_kbd) && 745 KBD_IS_BUSY(&sc->sc_kbd)) { 746 /* let the callback function process the input */ 747 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT, 748 sc->sc_kbd.kb_callback.kc_arg); 749 } else { 750 /* read and discard the input, no one is waiting for it */ 751 do { 752 c = hvkbd_read_char(&sc->sc_kbd, 0); 753 } while (c != NOKEY); 754 } 755 } 756 757 int 758 hvkbd_driver_load(module_t mod, int what, void *arg) 759 { 760 switch (what) { 761 case MOD_LOAD: 762 kbd_add_driver(&hvkbd_kbd_driver); 763 break; 764 case MOD_UNLOAD: 765 kbd_delete_driver(&hvkbd_kbd_driver); 766 break; 767 } 768 return (0); 769 } 770 771 int 772 hv_kbd_drv_attach(device_t dev) 773 { 774 hv_kbd_sc *sc = device_get_softc(dev); 775 int unit = device_get_unit(dev); 776 keyboard_t *kbd = &sc->sc_kbd; 777 keyboard_switch_t *sw; 778 #ifdef EVDEV_SUPPORT 779 struct evdev_dev *evdev; 780 #endif 781 782 sw = kbd_get_switch(HVKBD_DRIVER_NAME); 783 if (sw == NULL) { 784 return (ENXIO); 785 } 786 787 kbd_init_struct(kbd, HVKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0); 788 kbd->kb_data = (void *)sc; 789 kbd_set_maps(kbd, &key_map, &accent_map, fkey_tab, nitems(fkey_tab)); 790 KBD_FOUND_DEVICE(kbd); 791 hvkbd_clear_state(kbd); 792 KBD_PROBE_DONE(kbd); 793 KBD_INIT_DONE(kbd); 794 sc->sc_mode = K_XLATE; 795 (*sw->enable)(kbd); 796 797 #ifdef EVDEV_SUPPORT 798 evdev = evdev_alloc(); 799 evdev_set_name(evdev, "Hyper-V keyboard"); 800 evdev_set_phys(evdev, device_get_nameunit(dev)); 801 evdev_set_id(evdev, BUS_VIRTUAL, 0, 0, 0); 802 evdev_set_methods(evdev, kbd, &hvkbd_evdev_methods); 803 evdev_support_event(evdev, EV_SYN); 804 evdev_support_event(evdev, EV_KEY); 805 evdev_support_event(evdev, EV_LED); 806 evdev_support_event(evdev, EV_REP); 807 evdev_support_all_known_keys(evdev); 808 evdev_support_led(evdev, LED_NUML); 809 evdev_support_led(evdev, LED_CAPSL); 810 evdev_support_led(evdev, LED_SCROLLL); 811 if (evdev_register_mtx(evdev, &Giant)) 812 evdev_free(evdev); 813 else 814 sc->ks_evdev = evdev; 815 sc->ks_evdev_state = 0; 816 #endif 817 818 if (kbd_register(kbd) < 0) { 819 goto detach; 820 } 821 KBD_CONFIG_DONE(kbd); 822 #ifdef KBD_INSTALL_CDEV 823 if (kbd_attach(kbd)) { 824 goto detach; 825 } 826 #endif 827 if (bootverbose) { 828 kbdd_diag(kbd, bootverbose); 829 } 830 return (0); 831 detach: 832 hv_kbd_drv_detach(dev); 833 return (ENXIO); 834 } 835 836 int 837 hv_kbd_drv_detach(device_t dev) 838 { 839 int error = 0; 840 hv_kbd_sc *sc = device_get_softc(dev); 841 hvkbd_disable(&sc->sc_kbd); 842 #ifdef EVDEV_SUPPORT 843 evdev_free(sc->ks_evdev); 844 #endif 845 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) { 846 error = kbd_unregister(&sc->sc_kbd); 847 if (error) { 848 device_printf(dev, "WARNING: kbd_unregister() " 849 "returned non-zero! (ignored)\n"); 850 } 851 } 852 #ifdef KBD_INSTALL_CDEV 853 error = kbd_detach(&sc->sc_kbd); 854 #endif 855 return (error); 856 } 857 858