xref: /freebsd/sys/dev/hyperv/input/hv_kbd.c (revision b8bc6b7954e27edb449503004ef52b8b97585545)
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 #ifdef EVDEV_SUPPORT
273 	int keycode;
274 #endif
275 	HVKBD_LOCK_ASSERT();
276 
277 	if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc))
278 		return (NOKEY);
279 
280 next_code:
281 
282 	/* do we have a composed char to return? */
283 	if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char > 0) {
284 		action = sc->sc_composed_char;
285 		sc->sc_composed_char = 0;
286 		if (action > UCHAR_MAX) {
287 			return (ERRKEY);
288 		}
289 		return (action);
290 	}
291 
292 	if (hv_kbd_fetch_top(sc, &ks)) {
293 		return (NOKEY);
294 	}
295 	if ((ks.info & IS_E0) || (ks.info & IS_E1)) {
296 		/**
297 		 * Emulate the generation of E0 or E1 scancode,
298 		 * the real scancode will be consumed next time.
299 		 */
300 		if (ks.info & IS_E0) {
301 			scancode = XTKBD_EMUL0;
302 			ks.info &= ~IS_E0;
303 		} else if (ks.info & IS_E1) {
304 			scancode = XTKBD_EMUL1;
305 			ks.info &= ~IS_E1;
306 		}
307 		/**
308 		 * Change the top item to avoid encountering
309 		 * E0 or E1 twice.
310 		 */
311 		hv_kbd_modify_top(sc, &ks);
312 	} else if (ks.info & IS_UNICODE) {
313 		/**
314 		 * XXX: Hyperv host send unicode to VM through
315 		 * 'Type clipboard text', the mapping from
316 		 * unicode to scancode depends on the keymap.
317 		 * It is so complicated that we do not plan to
318 		 * support it yet.
319 		 */
320 		if (bootverbose)
321 			device_printf(sc->dev, "Unsupported unicode\n");
322 		hv_kbd_remove_top(sc);
323 		return (NOKEY);
324 	} else {
325 		scancode = ks.makecode;
326 		if (ks.info & IS_BREAK) {
327 			scancode |= XTKBD_RELEASE;
328 		}
329 		hv_kbd_remove_top(sc);
330 	}
331 #ifdef EVDEV_SUPPORT
332 	/* push evdev event */
333 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
334 	    sc->ks_evdev != NULL) {
335 		keycode = evdev_scancode2key(&sc->ks_evdev_state,
336 		    scancode);
337 
338 		if (keycode != KEY_RESERVED) {
339 			evdev_push_event(sc->ks_evdev, EV_KEY,
340 			    (uint16_t)keycode, scancode & 0x80 ? 0 : 1);
341 			evdev_sync(sc->ks_evdev);
342 		}
343 	}
344 #endif
345 	++kbd->kb_count;
346 	DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode);
347 
348 	/* return the byte as is for the K_RAW mode */
349 	if (sc->sc_mode == K_RAW)
350 		return scancode;
351 
352 	/* translate the scan code into a keycode */
353 	keycode = scancode & 0x7F;
354 	switch (sc->sc_prefix) {
355 	case 0x00:      /* normal scancode */
356 		switch(scancode) {
357 		case 0xB8:      /* left alt (compose key) released */
358 			if (sc->sc_flags & HVKBD_FLAG_COMPOSE) {
359 				sc->sc_flags &= ~HVKBD_FLAG_COMPOSE;
360 				if (sc->sc_composed_char > UCHAR_MAX)
361 					sc->sc_composed_char = 0;
362 			}
363 			break;
364 		case 0x38:      /* left alt (compose key) pressed */
365 			if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE)) {
366 				sc->sc_flags |= HVKBD_FLAG_COMPOSE;
367 				sc->sc_composed_char = 0;
368 			}
369 			break;
370 		case 0xE0:
371 		case 0xE1:
372 			sc->sc_prefix = scancode;
373 			goto next_code;
374 		}
375 		break;
376 	case 0xE0:		/* 0xE0 prefix */
377 		sc->sc_prefix = 0;
378 		switch (keycode) {
379 		case 0x1C:	/* right enter key */
380 			keycode = 0x59;
381 			break;
382 		case 0x1D:	/* right ctrl key */
383 			keycode = 0x5A;
384 			break;
385 		case 0x35:	/* keypad divide key */
386 			keycode = 0x5B;
387 			break;
388 		case 0x37:	/* print scrn key */
389 			keycode = 0x5C;
390 			break;
391 		case 0x38:	/* right alt key (alt gr) */
392 			keycode = 0x5D;
393 			break;
394 		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
395 			keycode = 0x68;
396 			break;
397 		case 0x47:	/* grey home key */
398 			keycode = 0x5E;
399 			break;
400 		case 0x48:	/* grey up arrow key */
401 			keycode = 0x5F;
402 			break;
403 		case 0x49:	/* grey page up key */
404 			keycode = 0x60;
405 			break;
406 		case 0x4B:	/* grey left arrow key */
407 			keycode = 0x61;
408 			break;
409 		case 0x4D:	/* grey right arrow key */
410 			keycode = 0x62;
411 			break;
412 		case 0x4F:	/* grey end key */
413 			keycode = 0x63;
414 			break;
415 		case 0x50:	/* grey down arrow key */
416 			keycode = 0x64;
417 			break;
418 		case 0x51:	/* grey page down key */
419 			keycode = 0x65;
420 			break;
421 		case 0x52:	/* grey insert key */
422 			keycode = 0x66;
423 			break;
424 		case 0x53:	/* grey delete key */
425 			keycode = 0x67;
426 			break;
427 			/* the following 3 are only used on the MS "Natural" keyboard */
428 		case 0x5b:	/* left Window key */
429 			keycode = 0x69;
430 			break;
431 		case 0x5c:	/* right Window key */
432 			keycode = 0x6a;
433 			break;
434 		case 0x5d:	/* menu key */
435 			keycode = 0x6b;
436 			break;
437 		case 0x5e:	/* power key */
438 			keycode = 0x6d;
439 			break;
440 		case 0x5f:	/* sleep key */
441 			keycode = 0x6e;
442 			break;
443 		case 0x63:	/* wake key */
444 			keycode = 0x6f;
445 			break;
446 		default:	/* ignore everything else */
447 			goto next_code;
448 		}
449 		break;
450 	case 0xE1:	/* 0xE1 prefix */
451 		/*
452 		 * The pause/break key on the 101 keyboard produces:
453 		 * E1-1D-45 E1-9D-C5
454 		 * Ctrl-pause/break produces:
455 		 * E0-46 E0-C6 (See above.)
456 		 */
457 		sc->sc_prefix = 0;
458 		if (keycode == 0x1D)
459 			sc->sc_prefix = 0x1D;
460 		goto next_code;
461 		/* NOT REACHED */
462 	case 0x1D:	/* pause / break */
463 		sc->sc_prefix = 0;
464 		if (keycode != 0x45)
465 			goto next_code;
466 		keycode = 0x68;
467 		break;
468 	}
469 
470 	/* XXX assume 101/102 keys AT keyboard */
471 	switch (keycode) {
472 	case 0x5c:      /* print screen */
473 		if (sc->sc_flags & ALTS)
474 			keycode = 0x54; /* sysrq */
475 		break;
476 	case 0x68:      /* pause/break */
477 		if (sc->sc_flags & CTLS)
478 			keycode = 0x6c; /* break */
479 		break;
480 	}
481 
482 	/* return the key code in the K_CODE mode */
483 	if (sc->sc_mode == K_CODE)
484 		return (keycode | (scancode & 0x80));
485 
486 	/* compose a character code */
487 	if (sc->sc_flags &  HVKBD_FLAG_COMPOSE) {
488 		switch (keycode | (scancode & 0x80)) {
489 		/* key pressed, process it */
490 		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
491 			sc->sc_composed_char *= 10;
492 			sc->sc_composed_char += keycode - 0x40;
493 			if (sc->sc_composed_char > UCHAR_MAX)
494 				return ERRKEY;
495 			goto next_code;
496 		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
497 			sc->sc_composed_char *= 10;
498 			sc->sc_composed_char += keycode - 0x47;
499 			if (sc->sc_composed_char > UCHAR_MAX)
500 				return ERRKEY;
501 			goto next_code;
502 		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
503 			sc->sc_composed_char *= 10;
504 			sc->sc_composed_char += keycode - 0x4E;
505 			if (sc->sc_composed_char > UCHAR_MAX)
506 				return ERRKEY;
507 			goto next_code;
508 		case 0x52:				/* keypad 0 */
509 			sc->sc_composed_char *= 10;
510 			if (sc->sc_composed_char > UCHAR_MAX)
511 				return ERRKEY;
512 			goto next_code;
513 
514 		/* key released, no interest here */
515 		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
516 		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
517 		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
518 		case 0xD2:				/* keypad 0 */
519 			goto next_code;
520 
521 		case 0x38:				/* left alt key */
522 			break;
523 
524 		default:
525 			if (sc->sc_composed_char > 0) {
526 				sc->sc_flags &= ~HVKBD_FLAG_COMPOSE;
527 				sc->sc_composed_char = 0;
528 				return (ERRKEY);
529 			}
530 			break;
531 		}
532 	}
533 
534 	/* keycode to key action */
535 	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
536 				  &sc->sc_state, &sc->sc_accents);
537 	if (action == NOKEY)
538 		goto next_code;
539 	else
540 		return (action);
541 }
542 
543 /* Currently wait is always false. */
544 static uint32_t
545 hvkbd_read_char(keyboard_t *kbd, int wait)
546 {
547 	uint32_t keycode;
548 
549 	HVKBD_LOCK();
550 	keycode = hvkbd_read_char_locked(kbd, wait);
551 	HVKBD_UNLOCK();
552 
553 	return (keycode);
554 }
555 
556 /* clear the internal state of the keyboard */
557 static void
558 hvkbd_clear_state(keyboard_t *kbd)
559 {
560 	hv_kbd_sc *sc = kbd->kb_data;
561 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
562 	sc->sc_flags &= ~(HVKBD_FLAG_POLLING | HVKBD_FLAG_COMPOSE);
563 	sc->sc_accents = 0;
564 	sc->sc_composed_char = 0;
565 }
566 
567 static int
568 hvkbd_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
569 {
570 	int i;
571 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
572     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
573         int ival;
574 #endif
575 	hv_kbd_sc *sc = kbd->kb_data;
576 	switch (cmd) {
577 	case KDGKBMODE:
578 		*(int *)arg = sc->sc_mode;
579 		break;
580 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
581     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
582 	case _IO('K', 7):
583 		ival = IOCPARM_IVAL(arg);
584 		arg = (caddr_t)&ival;
585 		/* FALLTHROUGH */
586 #endif
587 	case KDSKBMODE:		/* set keyboard mode */
588 		DEBUG_HVKBD(kbd, "expected mode: %x\n", *(int *)arg);
589 		switch (*(int *)arg) {
590 		case K_XLATE:
591 			if (sc->sc_mode != K_XLATE) {
592 				/* make lock key state and LED state match */
593 				sc->sc_state &= ~LOCK_MASK;
594 				sc->sc_state |= KBD_LED_VAL(kbd);
595 			}
596 			/* FALLTHROUGH */
597 		case K_RAW:
598 		case K_CODE:
599 			if (sc->sc_mode != *(int *)arg) {
600 				DEBUG_HVKBD(kbd, "mod changed to %x\n", *(int *)arg);
601 				if ((sc->sc_flags & HVKBD_FLAG_POLLING) == 0)
602 					hvkbd_clear_state(kbd);
603 				sc->sc_mode = *(int *)arg;
604 			}
605 			break;
606 		default:
607 			return (EINVAL);
608 		}
609 		break;
610 	case KDGKBSTATE:	/* get lock key state */
611 		*(int *)arg = sc->sc_state & LOCK_MASK;
612 		break;
613 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
614     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
615 	case _IO('K', 20):
616 		ival = IOCPARM_IVAL(arg);
617 		arg = (caddr_t)&ival;
618 		/* FALLTHROUGH */
619 #endif
620 	case KDSKBSTATE:		/* set lock key state */
621 		if (*(int *)arg & ~LOCK_MASK) {
622 			return (EINVAL);
623 		}
624 		sc->sc_state &= ~LOCK_MASK;
625 		sc->sc_state |= *(int *)arg;
626 		return hvkbd_ioctl_locked(kbd, KDSETLED, arg);
627 	case KDGETLED:			/* get keyboard LED */
628 		*(int *)arg = KBD_LED_VAL(kbd);
629 		break;
630 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
631     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
632 	case _IO('K', 66):
633 		ival = IOCPARM_IVAL(arg);
634 		arg = (caddr_t)&ival;
635 		/* FALLTHROUGH */
636 #endif
637 	case KDSETLED:			/* set keyboard LED */
638 		/* NOTE: lock key state in "sc_state" won't be changed */
639 		if (*(int *)arg & ~LOCK_MASK)
640 			return (EINVAL);
641 
642 		i = *(int *)arg;
643 
644 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
645 		if (sc->sc_mode == K_XLATE &&
646 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
647 			if (i & ALKED)
648 				i |= CLKED;
649 			else
650 				i &= ~CLKED;
651 		}
652 		if (KBD_HAS_DEVICE(kbd)) {
653 			DEBUG_HVSC(sc, "setled 0x%x\n", *(int *)arg);
654 		}
655 
656 #ifdef EVDEV_SUPPORT
657 		/* push LED states to evdev */
658 		if (sc->ks_evdev != NULL &&
659 		    evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
660 			evdev_push_leds(sc->ks_evdev, *(int *)arg);
661 #endif
662 		KBD_LED_VAL(kbd) = *(int *)arg;
663 		break;
664 	case PIO_KEYMAP:	/* set keyboard translation table */
665 	case OPIO_KEYMAP:	/* set keyboard translation table (compat) */
666 	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
667 	case PIO_DEADKEYMAP:	/* set accent key translation table */
668 		sc->sc_accents = 0;
669 		/* FALLTHROUGH */
670 	default:
671 		return (genkbd_commonioctl(kbd, cmd, arg));
672 	}
673 	return (0);
674 }
675 
676 /* some useful control functions */
677 static int
678 hvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
679 {
680 	DEBUG_HVKBD(kbd, "%s: %lx start\n", __func__, cmd);
681 	HVKBD_LOCK();
682 	int ret = hvkbd_ioctl_locked(kbd, cmd, arg);
683 	HVKBD_UNLOCK();
684 	DEBUG_HVKBD(kbd, "%s: %lx end %d\n", __func__, cmd, ret);
685 	return (ret);
686 }
687 
688 /* read one byte from the keyboard if it's allowed */
689 /* Currently unused. */
690 static int
691 hvkbd_read(keyboard_t *kbd, int wait)
692 {
693 	DEBUG_HVKBD(kbd, "%s\n", __func__);
694 	HVKBD_LOCK_ASSERT();
695 	if (!KBD_IS_ACTIVE(kbd))
696 		return (-1);
697 	return hvkbd_read_char_locked(kbd, wait);
698 }
699 
700 #ifdef EVDEV_SUPPORT
701 static void
702 hvkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
703     int32_t value)
704 {
705 	keyboard_t *kbd = evdev_get_softc(evdev);
706 
707 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
708 	(type == EV_LED || type == EV_REP)) {
709 		mtx_lock(&Giant);
710 		kbd_ev_event(kbd, type, code, value);
711 		mtx_unlock(&Giant);
712 	}
713 }
714 #endif
715 
716 static keyboard_switch_t hvkbdsw = {
717 	.probe =	hvkbd_probe,		/* not used */
718 	.init =		hvkbd_init,
719 	.term =		hvkbd_term,		/* not used */
720 	.intr =		hvkbd_intr,		/* not used */
721 	.test_if =	hvkbd_test_if,		/* not used */
722 	.enable =	hvkbd_enable,
723 	.disable =	hvkbd_disable,
724 	.read =		hvkbd_read,
725 	.check =	hvkbd_check,
726 	.read_char =	hvkbd_read_char,
727 	.check_char =	hvkbd_check_char,
728 	.ioctl =	hvkbd_ioctl,
729 	.lock =		hvkbd_lock,		/* not used */
730 	.clear_state =	hvkbd_clear_state,
731 	.get_state =	hvkbd_get_state,	/* not used */
732 	.set_state =	hvkbd_set_state,	/* not used */
733 	.poll =		hvkbd_poll,
734 };
735 
736 KEYBOARD_DRIVER(hvkbd, hvkbdsw, hvkbd_configure);
737 
738 void
739 hv_kbd_intr(hv_kbd_sc *sc)
740 {
741 	uint32_t c;
742 	if ((sc->sc_flags & HVKBD_FLAG_POLLING) != 0)
743 		return;
744 
745 	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
746 	    KBD_IS_BUSY(&sc->sc_kbd)) {
747 		/* let the callback function process the input */
748 		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
749 		    sc->sc_kbd.kb_callback.kc_arg);
750 	} else {
751 		/* read and discard the input, no one is waiting for it */
752 		do {
753 			c = hvkbd_read_char(&sc->sc_kbd, 0);
754 		} while (c != NOKEY);
755 	}
756 }
757 
758 int
759 hvkbd_driver_load(module_t mod, int what, void *arg)
760 {
761 	switch (what) {
762 	case MOD_LOAD:
763 		kbd_add_driver(&hvkbd_kbd_driver);
764 		break;
765 	case MOD_UNLOAD:
766 		kbd_delete_driver(&hvkbd_kbd_driver);
767 		break;
768 	}
769 	return (0);
770 }
771 
772 int
773 hv_kbd_drv_attach(device_t dev)
774 {
775 	hv_kbd_sc *sc = device_get_softc(dev);
776 	int unit = device_get_unit(dev);
777 	keyboard_t *kbd = &sc->sc_kbd;
778 	keyboard_switch_t *sw;
779 #ifdef EVDEV_SUPPORT
780 	struct evdev_dev *evdev;
781 #endif
782 
783 	sw = kbd_get_switch(HVKBD_DRIVER_NAME);
784 	if (sw == NULL) {
785 		return (ENXIO);
786 	}
787 
788 	kbd_init_struct(kbd, HVKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
789 	kbd->kb_data = (void *)sc;
790 	kbd_set_maps(kbd, &key_map, &accent_map, fkey_tab, nitems(fkey_tab));
791 	KBD_FOUND_DEVICE(kbd);
792 	hvkbd_clear_state(kbd);
793 	KBD_PROBE_DONE(kbd);
794 	KBD_INIT_DONE(kbd);
795 	sc->sc_mode = K_XLATE;
796 	(*sw->enable)(kbd);
797 
798 #ifdef EVDEV_SUPPORT
799 	evdev = evdev_alloc();
800 	evdev_set_name(evdev, "Hyper-V keyboard");
801 	evdev_set_phys(evdev, device_get_nameunit(dev));
802 	evdev_set_id(evdev, BUS_VIRTUAL, 0, 0, 0);
803 	evdev_set_methods(evdev, kbd, &hvkbd_evdev_methods);
804 	evdev_support_event(evdev, EV_SYN);
805 	evdev_support_event(evdev, EV_KEY);
806 	evdev_support_event(evdev, EV_LED);
807 	evdev_support_event(evdev, EV_REP);
808 	evdev_support_all_known_keys(evdev);
809 	evdev_support_led(evdev, LED_NUML);
810 	evdev_support_led(evdev, LED_CAPSL);
811 	evdev_support_led(evdev, LED_SCROLLL);
812 	if (evdev_register_mtx(evdev, &Giant))
813 		evdev_free(evdev);
814 	else
815 		sc->ks_evdev = evdev;
816 	sc->ks_evdev_state = 0;
817 #endif
818 
819 	if (kbd_register(kbd) < 0) {
820 		goto detach;
821 	}
822 	KBD_CONFIG_DONE(kbd);
823 #ifdef KBD_INSTALL_CDEV
824         if (kbd_attach(kbd)) {
825 		goto detach;
826 	}
827 #endif
828 	if (bootverbose) {
829 		kbdd_diag(kbd, bootverbose);
830 	}
831 	return (0);
832 detach:
833 	hv_kbd_drv_detach(dev);
834 	return (ENXIO);
835 }
836 
837 int
838 hv_kbd_drv_detach(device_t dev)
839 {
840 	int error = 0;
841 	hv_kbd_sc *sc = device_get_softc(dev);
842 	hvkbd_disable(&sc->sc_kbd);
843 #ifdef EVDEV_SUPPORT
844 	evdev_free(sc->ks_evdev);
845 #endif
846 	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
847 		error = kbd_unregister(&sc->sc_kbd);
848 		if (error) {
849 			device_printf(dev, "WARNING: kbd_unregister() "
850 			    "returned non-zero! (ignored)\n");
851 		}
852 	}
853 #ifdef KBD_INSTALL_CDEV
854 	error = kbd_detach(&sc->sc_kbd);
855 #endif
856 	return (error);
857 }
858 
859