2 * Copyright (c) 2017 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_evdev.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/limits.h>
42 #include <sys/taskqueue.h>
43 #include <sys/selinfo.h>
44 #include <sys/sysctl.h>
47 #include <sys/queue.h>
48 #include <sys/kthread.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysproto.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>
59 #include <dev/kbd/kbdreg.h>
60 #include <dev/kbd/kbdtables.h>
63 #include <dev/evdev/evdev.h>
64 #include <dev/evdev/input.h>
67 #include "dev/hyperv/input/hv_kbdc.h"
69 #define HVKBD_MTX_LOCK(_m) do { \
73 #define HVKBD_MTX_UNLOCK(_m) do { \
77 #define HVKBD_MTX_ASSERT(_m, _t) do { \
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)
85 #define HVKBD_FLAG_COMPOSE 0x00000001 /* compose char flag */
86 #define HVKBD_FLAG_POLLING 0x00000002
89 static evdev_event_t hvkbd_ev_event;
91 static const struct evdev_methods hvkbd_evdev_methods = {
92 .ev_event = hvkbd_ev_event,
96 /* early keyboard probe, not supported */
98 hvkbd_configure(int flags)
103 /* detect a keyboard, not used */
105 hvkbd_probe(int unit, void *arg, int flags)
110 /* reset and initialize the device, not used */
112 hvkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
114 DEBUG_HVKBD(*kbdp, "%s\n", __func__);
118 /* test the interface to the device, not used */
120 hvkbd_test_if(keyboard_t *kbd)
122 DEBUG_HVKBD(kbd, "%s\n", __func__);
126 /* finish using this keyboard, not used */
128 hvkbd_term(keyboard_t *kbd)
130 DEBUG_HVKBD(kbd, "%s\n", __func__);
134 /* keyboard interrupt routine, not used */
136 hvkbd_intr(keyboard_t *kbd, void *arg)
138 DEBUG_HVKBD(kbd, "%s\n", __func__);
142 /* lock the access to the keyboard, not used */
144 hvkbd_lock(keyboard_t *kbd, int lock)
146 DEBUG_HVKBD(kbd, "%s\n", __func__);
150 /* save the internal state, not used */
152 hvkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
154 DEBUG_HVKBD(kbd,"%s\n", __func__);
155 return (len == 0) ? 1 : -1;
158 /* set the internal state, not used */
160 hvkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
162 DEBUG_HVKBD(kbd, "%s\n", __func__);
167 hvkbd_poll(keyboard_t *kbd, int on)
169 hv_kbd_sc *sc = kbd->kb_data;
173 * Keep a reference count on polling to allow recursive
174 * cngrab() during a panic for example.
178 else if (sc->sc_polling > 0)
181 if (sc->sc_polling != 0) {
182 sc->sc_flags |= HVKBD_FLAG_POLLING;
184 sc->sc_flags &= ~HVKBD_FLAG_POLLING;
191 * Enable the access to the device; until this function is called,
192 * the client cannot read from the keyboard.
195 hvkbd_enable(keyboard_t *kbd)
203 /* disallow the access to the device */
205 hvkbd_disable(keyboard_t *kbd)
207 DEBUG_HVKBD(kbd, "%s\n", __func__);
215 hvkbd_do_poll(hv_kbd_sc *sc, uint8_t wait)
217 while (!hv_kbd_prod_is_ready(sc)) {
218 hv_kbd_read_channel(sc->hs_chan, sc);
224 /* check if data is waiting */
225 /* Currently unused. */
227 hvkbd_check(keyboard_t *kbd)
229 DEBUG_HVKBD(kbd, "%s\n", __func__);
233 /* check if char is waiting */
235 hvkbd_check_char_locked(keyboard_t *kbd)
238 if (!KBD_IS_ACTIVE(kbd))
241 hv_kbd_sc *sc = kbd->kb_data;
242 if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char != 0)
244 if (sc->sc_flags & HVKBD_FLAG_POLLING)
245 hvkbd_do_poll(sc, 0);
246 if (hv_kbd_prod_is_ready(sc)) {
253 hvkbd_check_char(keyboard_t *kbd)
258 result = hvkbd_check_char_locked(kbd);
264 /* read char from the keyboard */
266 hvkbd_read_char_locked(keyboard_t *kbd, int wait)
268 uint32_t scancode = NOKEY;
271 hv_kbd_sc *sc = kbd->kb_data;
276 if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc))
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) {
291 if (hv_kbd_fetch_top(sc, &ks)) {
294 if ((ks.info & IS_E0) || (ks.info & IS_E1)) {
296 * Emulate the generation of E0 or E1 scancode,
297 * the real scancode will be consumed next time.
299 if (ks.info & IS_E0) {
300 scancode = XTKBD_EMUL0;
302 } else if (ks.info & IS_E1) {
303 scancode = XTKBD_EMUL1;
307 * Change the top item to avoid encountering
310 hv_kbd_modify_top(sc, &ks);
311 } else if (ks.info & IS_UNICODE) {
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
320 device_printf(sc->dev, "Unsupported unicode\n");
321 hv_kbd_remove_top(sc);
324 scancode = ks.makecode;
325 if (ks.info & IS_BREAK) {
326 scancode |= XTKBD_RELEASE;
328 hv_kbd_remove_top(sc);
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,
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);
345 DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode);
347 /* return the byte as is for the K_RAW mode */
348 if (sc->sc_mode == K_RAW)
351 /* translate the scan code into a keycode */
352 keycode = scancode & 0x7F;
353 switch (sc->sc_prefix) {
354 case 0x00: /* normal 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;
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;
371 sc->sc_prefix = scancode;
375 case 0xE0: /* 0xE0 prefix */
378 case 0x1C: /* right enter key */
381 case 0x1D: /* right ctrl key */
384 case 0x35: /* keypad divide key */
387 case 0x37: /* print scrn key */
390 case 0x38: /* right alt key (alt gr) */
393 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
396 case 0x47: /* grey home key */
399 case 0x48: /* grey up arrow key */
402 case 0x49: /* grey page up key */
405 case 0x4B: /* grey left arrow key */
408 case 0x4D: /* grey right arrow key */
411 case 0x4F: /* grey end key */
414 case 0x50: /* grey down arrow key */
417 case 0x51: /* grey page down key */
420 case 0x52: /* grey insert key */
423 case 0x53: /* grey delete key */
426 /* the following 3 are only used on the MS "Natural" keyboard */
427 case 0x5b: /* left Window key */
430 case 0x5c: /* right Window key */
433 case 0x5d: /* menu key */
436 case 0x5e: /* power key */
439 case 0x5f: /* sleep key */
442 case 0x63: /* wake key */
445 default: /* ignore everything else */
449 case 0xE1: /* 0xE1 prefix */
451 * The pause/break key on the 101 keyboard produces:
453 * Ctrl-pause/break produces:
454 * E0-46 E0-C6 (See above.)
458 sc->sc_prefix = 0x1D;
461 case 0x1D: /* pause / break */
469 /* XXX assume 101/102 keys AT keyboard */
471 case 0x5c: /* print screen */
472 if (sc->sc_flags & ALTS)
473 keycode = 0x54; /* sysrq */
475 case 0x68: /* pause/break */
476 if (sc->sc_flags & CTLS)
477 keycode = 0x6c; /* break */
481 /* return the key code in the K_CODE mode */
482 if (sc->sc_mode == K_CODE)
483 return (keycode | (scancode & 0x80));
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)
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)
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)
507 case 0x52: /* keypad 0 */
508 sc->sc_composed_char *= 10;
509 if (sc->sc_composed_char > UCHAR_MAX)
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 */
520 case 0x38: /* left alt key */
524 if (sc->sc_composed_char > 0) {
525 sc->sc_flags &= ~HVKBD_FLAG_COMPOSE;
526 sc->sc_composed_char = 0;
533 /* keycode to key action */
534 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
535 &sc->sc_state, &sc->sc_accents);
542 /* Currently wait is always false. */
544 hvkbd_read_char(keyboard_t *kbd, int wait)
549 keycode = hvkbd_read_char_locked(kbd, wait);
555 /* clear the internal state of the keyboard */
557 hvkbd_clear_state(keyboard_t *kbd)
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);
563 sc->sc_composed_char = 0;
567 hvkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
570 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
571 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
574 hv_kbd_sc *sc = kbd->kb_data;
577 *(int *)arg = sc->sc_mode;
579 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
580 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
582 ival = IOCPARM_IVAL(arg);
583 arg = (caddr_t)&ival;
586 case KDSKBMODE: /* set keyboard mode */
587 DEBUG_HVKBD(kbd, "expected mode: %x\n", *(int *)arg);
588 switch (*(int *)arg) {
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);
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;
609 case KDGKBSTATE: /* get lock key state */
610 *(int *)arg = sc->sc_state & LOCK_MASK;
612 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
613 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
615 ival = IOCPARM_IVAL(arg);
616 arg = (caddr_t)&ival;
619 case KDSKBSTATE: /* set lock key state */
620 if (*(int *)arg & ~LOCK_MASK) {
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);
629 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
630 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
632 ival = IOCPARM_IVAL(arg);
633 arg = (caddr_t)&ival;
636 case KDSETLED: /* set keyboard LED */
637 /* NOTE: lock key state in "sc_state" won't be changed */
638 if (*(int *)arg & ~LOCK_MASK)
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) {
651 if (KBD_HAS_DEVICE(kbd)) {
652 DEBUG_HVSC(sc, "setled 0x%x\n", *(int *)arg);
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);
661 KBD_LED_VAL(kbd) = *(int *)arg;
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 */
670 return (genkbd_commonioctl(kbd, cmd, arg));
675 /* some useful control functions */
677 hvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
679 DEBUG_HVKBD(kbd, "%s: %lx start\n", __func__, cmd);
681 int ret = hvkbd_ioctl_locked(kbd, cmd, arg);
683 DEBUG_HVKBD(kbd, "%s: %lx end %d\n", __func__, cmd, ret);
687 /* read one byte from the keyboard if it's allowed */
688 /* Currently unused. */
690 hvkbd_read(keyboard_t *kbd, int wait)
692 DEBUG_HVKBD(kbd, "%s\n", __func__);
694 if (!KBD_IS_ACTIVE(kbd))
696 return hvkbd_read_char_locked(kbd, wait);
701 hvkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
704 keyboard_t *kbd = evdev_get_softc(evdev);
706 if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
707 (type == EV_LED || type == EV_REP)) {
709 kbd_ev_event(kbd, type, code, value);
715 static keyboard_switch_t hvkbdsw = {
716 .probe = hvkbd_probe, /* not used */
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,
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 */
735 KEYBOARD_DRIVER(hvkbd, hvkbdsw, hvkbd_configure);
738 hv_kbd_intr(hv_kbd_sc *sc)
741 if ((sc->sc_flags & HVKBD_FLAG_POLLING) != 0)
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);
750 /* read and discard the input, no one is waiting for it */
752 c = hvkbd_read_char(&sc->sc_kbd, 0);
753 } while (c != NOKEY);
758 hvkbd_driver_load(module_t mod, int what, void *arg)
762 kbd_add_driver(&hvkbd_kbd_driver);
765 kbd_delete_driver(&hvkbd_kbd_driver);
772 hv_kbd_drv_attach(device_t dev)
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;
779 struct evdev_dev *evdev;
782 sw = kbd_get_switch(HVKBD_DRIVER_NAME);
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);
794 sc->sc_mode = K_XLATE;
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))
814 sc->ks_evdev = evdev;
815 sc->ks_evdev_state = 0;
818 if (kbd_register(kbd) < 0) {
821 KBD_CONFIG_DONE(kbd);
822 #ifdef KBD_INSTALL_CDEV
823 if (kbd_attach(kbd)) {
828 kbdd_diag(kbd, bootverbose);
832 hv_kbd_drv_detach(dev);
837 hv_kbd_drv_detach(device_t dev)
840 hv_kbd_sc *sc = device_get_softc(dev);
841 hvkbd_disable(&sc->sc_kbd);
843 evdev_free(sc->ks_evdev);
845 if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
846 error = kbd_unregister(&sc->sc_kbd);
848 device_printf(dev, "WARNING: kbd_unregister() "
849 "returned non-zero! (ignored)\n");
852 #ifdef KBD_INSTALL_CDEV
853 error = kbd_detach(&sc->sc_kbd);