xref: /freebsd/sys/dev/hyperv/input/hv_kbd.c (revision 61e21613)
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 #include "opt_evdev.h"
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/conf.h>
33 #include <sys/uio.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/module.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/taskqueue.h>
41 #include <sys/selinfo.h>
42 #include <sys/sysctl.h>
43 #include <sys/poll.h>
44 #include <sys/proc.h>
45 #include <sys/queue.h>
46 #include <sys/kthread.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysproto.h>
49 #include <sys/sema.h>
50 #include <sys/signal.h>
51 #include <sys/syslog.h>
52 #include <sys/systm.h>
53 #include <sys/mutex.h>
54 #include <sys/callout.h>
55 
56 #include <sys/kbio.h>
57 #include <dev/kbd/kbdreg.h>
58 #include <dev/kbd/kbdtables.h>
59 
60 #ifdef EVDEV_SUPPORT
61 #include <dev/evdev/evdev.h>
62 #include <dev/evdev/input.h>
63 #endif
64 
65 #include "dev/hyperv/input/hv_kbdc.h"
66 
67 #define HVKBD_MTX_LOCK(_m) do {		\
68 	mtx_lock(_m);			\
69 } while (0)
70 
71 #define HVKBD_MTX_UNLOCK(_m) do {	\
72 	mtx_unlock(_m);			\
73 } while (0)
74 
75 #define HVKBD_MTX_ASSERT(_m, _t) do {	\
76 	mtx_assert(_m, _t);		\
77 } while (0)
78 
79 #define	HVKBD_LOCK()		HVKBD_MTX_LOCK(&Giant)
80 #define	HVKBD_UNLOCK()		HVKBD_MTX_UNLOCK(&Giant)
81 #define	HVKBD_LOCK_ASSERT()	HVKBD_MTX_ASSERT(&Giant, MA_OWNED)
82 
83 #define	HVKBD_FLAG_COMPOSE	0x00000001	/* compose char flag */
84 #define HVKBD_FLAG_POLLING	0x00000002
85 
86 #ifdef EVDEV_SUPPORT
87 static evdev_event_t hvkbd_ev_event;
88 
89 static const struct evdev_methods hvkbd_evdev_methods = {
90 	.ev_event = hvkbd_ev_event,
91 };
92 #endif
93 
94 /* early keyboard probe, not supported */
95 static int
96 hvkbd_configure(int flags)
97 {
98 	return (0);
99 }
100 
101 /* detect a keyboard, not used */
102 static int
103 hvkbd_probe(int unit, void *arg, int flags)
104 {
105 	return (ENXIO);
106 }
107 
108 /* reset and initialize the device, not used */
109 static int
110 hvkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
111 {
112 	DEBUG_HVKBD(*kbdp, "%s\n", __func__);
113 	return (ENXIO);
114 }
115 
116 /* test the interface to the device, not used */
117 static int
118 hvkbd_test_if(keyboard_t *kbd)
119 {
120 	DEBUG_HVKBD(kbd, "%s\n", __func__);
121 	return (0);
122 }
123 
124 /* finish using this keyboard, not used */
125 static int
126 hvkbd_term(keyboard_t *kbd)
127 {
128 	DEBUG_HVKBD(kbd, "%s\n", __func__);
129 	return (ENXIO);
130 }
131 
132 /* keyboard interrupt routine, not used */
133 static int
134 hvkbd_intr(keyboard_t *kbd, void *arg)
135 {
136 	DEBUG_HVKBD(kbd, "%s\n", __func__);
137 	return (0);
138 }
139 
140 /* lock the access to the keyboard, not used */
141 static int
142 hvkbd_lock(keyboard_t *kbd, int lock)
143 {
144 	DEBUG_HVKBD(kbd, "%s\n", __func__);
145 	return (1);
146 }
147 
148 /* save the internal state, not used */
149 static int
150 hvkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
151 {
152 	DEBUG_HVKBD(kbd,"%s\n",  __func__);
153 	return (len == 0) ? 1 : -1;
154 }
155 
156 /* set the internal state, not used */
157 static int
158 hvkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
159 {
160 	DEBUG_HVKBD(kbd, "%s\n", __func__);
161 	return (EINVAL);
162 }
163 
164 static int
165 hvkbd_poll(keyboard_t *kbd, int on)
166 {
167 	hv_kbd_sc *sc = kbd->kb_data;
168 
169 	HVKBD_LOCK();
170 	/*
171 	 * Keep a reference count on polling to allow recursive
172 	 * cngrab() during a panic for example.
173 	 */
174 	if (on)
175 		sc->sc_polling++;
176 	else if (sc->sc_polling > 0)
177 		sc->sc_polling--;
178 
179 	if (sc->sc_polling != 0) {
180 		sc->sc_flags |= HVKBD_FLAG_POLLING;
181 	} else {
182 		sc->sc_flags &= ~HVKBD_FLAG_POLLING;
183 	}
184 	HVKBD_UNLOCK();
185 	return (0);
186 }
187 
188 /*
189  * Enable the access to the device; until this function is called,
190  * the client cannot read from the keyboard.
191  */
192 static int
193 hvkbd_enable(keyboard_t *kbd)
194 {
195 	HVKBD_LOCK();
196 	KBD_ACTIVATE(kbd);
197 	HVKBD_UNLOCK();
198 	return (0);
199 }
200 
201 /* disallow the access to the device */
202 static int
203 hvkbd_disable(keyboard_t *kbd)
204 {
205 	DEBUG_HVKBD(kbd, "%s\n", __func__);
206 	HVKBD_LOCK();
207 	KBD_DEACTIVATE(kbd);
208 	HVKBD_UNLOCK();
209 	return (0);
210 }
211 
212 static void
213 hvkbd_do_poll(hv_kbd_sc *sc, uint8_t wait)
214 {
215 	while (!hv_kbd_prod_is_ready(sc)) {
216 		hv_kbd_read_channel(sc->hs_chan, sc);
217 		if (!wait)
218 			break;
219 	}
220 }
221 
222 /* check if data is waiting */
223 /* Currently unused. */
224 static int
225 hvkbd_check(keyboard_t *kbd)
226 {
227 	DEBUG_HVKBD(kbd, "%s\n", __func__);
228 	return (0);
229 }
230 
231 /* check if char is waiting */
232 static int
233 hvkbd_check_char_locked(keyboard_t *kbd)
234 {
235 	HVKBD_LOCK_ASSERT();
236 	if (!KBD_IS_ACTIVE(kbd))
237 		return (FALSE);
238 
239 	hv_kbd_sc *sc = kbd->kb_data;
240 	if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char != 0)
241 		return (TRUE);
242 	if (sc->sc_flags & HVKBD_FLAG_POLLING)
243 		hvkbd_do_poll(sc, 0);
244 	if (hv_kbd_prod_is_ready(sc)) {
245 		return (TRUE);
246 	}
247 	return (FALSE);
248 }
249 
250 static int
251 hvkbd_check_char(keyboard_t *kbd)
252 {
253 	int result;
254 
255 	HVKBD_LOCK();
256 	result = hvkbd_check_char_locked(kbd);
257 	HVKBD_UNLOCK();
258 
259 	return (result);
260 }
261 
262 /* read char from the keyboard */
263 static uint32_t
264 hvkbd_read_char_locked(keyboard_t *kbd, int wait)
265 {
266 	uint32_t scancode = NOKEY;
267 	uint32_t action;
268 	keystroke ks;
269 	hv_kbd_sc *sc = kbd->kb_data;
270 	int keycode;
271 
272 	HVKBD_LOCK_ASSERT();
273 
274 	if (!KBD_IS_ACTIVE(kbd) || !hv_kbd_prod_is_ready(sc))
275 		return (NOKEY);
276 
277 next_code:
278 
279 	/* do we have a composed char to return? */
280 	if (!(sc->sc_flags & HVKBD_FLAG_COMPOSE) && sc->sc_composed_char > 0) {
281 		action = sc->sc_composed_char;
282 		sc->sc_composed_char = 0;
283 		if (action > UCHAR_MAX) {
284 			return (ERRKEY);
285 		}
286 		return (action);
287 	}
288 
289 	if (hv_kbd_fetch_top(sc, &ks)) {
290 		return (NOKEY);
291 	}
292 	if ((ks.info & IS_E0) || (ks.info & IS_E1)) {
293 		/**
294 		 * Emulate the generation of E0 or E1 scancode,
295 		 * the real scancode will be consumed next time.
296 		 */
297 		if (ks.info & IS_E0) {
298 			scancode = XTKBD_EMUL0;
299 			ks.info &= ~IS_E0;
300 		} else if (ks.info & IS_E1) {
301 			scancode = XTKBD_EMUL1;
302 			ks.info &= ~IS_E1;
303 		}
304 		/**
305 		 * Change the top item to avoid encountering
306 		 * E0 or E1 twice.
307 		 */
308 		hv_kbd_modify_top(sc, &ks);
309 	} else if (ks.info & IS_UNICODE) {
310 		/**
311 		 * XXX: Hyperv host send unicode to VM through
312 		 * 'Type clipboard text', the mapping from
313 		 * unicode to scancode depends on the keymap.
314 		 * It is so complicated that we do not plan to
315 		 * support it yet.
316 		 */
317 		if (bootverbose)
318 			device_printf(sc->dev, "Unsupported unicode\n");
319 		hv_kbd_remove_top(sc);
320 		return (NOKEY);
321 	} else {
322 		scancode = ks.makecode;
323 		if (ks.info & IS_BREAK) {
324 			scancode |= XTKBD_RELEASE;
325 		}
326 		hv_kbd_remove_top(sc);
327 	}
328 #ifdef EVDEV_SUPPORT
329 	/* push evdev event */
330 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
331 	    sc->ks_evdev != NULL) {
332 		keycode = evdev_scancode2key(&sc->ks_evdev_state,
333 		    scancode);
334 
335 		if (keycode != KEY_RESERVED) {
336 			evdev_push_event(sc->ks_evdev, EV_KEY,
337 			    (uint16_t)keycode, scancode & 0x80 ? 0 : 1);
338 			evdev_sync(sc->ks_evdev);
339 		}
340 	}
341 	if (sc->ks_evdev != NULL && evdev_is_grabbed(sc->ks_evdev))
342 		return (NOKEY);
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 PIO_KEYMAPENT:	/* set keyboard translation table entry */
665 	case PIO_DEADKEYMAP:	/* set accent key translation table */
666 #ifdef COMPAT_FREEBSD13
667 	case OPIO_KEYMAP:	/* set keyboard translation table (compat) */
668 	case OPIO_DEADKEYMAP:	/* set accent key translation table (compat) */
669 #endif /* COMPAT_FREEBSD13 */
670 		sc->sc_accents = 0;
671 		/* FALLTHROUGH */
672 	default:
673 		return (genkbd_commonioctl(kbd, cmd, arg));
674 	}
675 	return (0);
676 }
677 
678 /* some useful control functions */
679 static int
680 hvkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
681 {
682 	DEBUG_HVKBD(kbd, "%s: %lx start\n", __func__, cmd);
683 	HVKBD_LOCK();
684 	int ret = hvkbd_ioctl_locked(kbd, cmd, arg);
685 	HVKBD_UNLOCK();
686 	DEBUG_HVKBD(kbd, "%s: %lx end %d\n", __func__, cmd, ret);
687 	return (ret);
688 }
689 
690 /* read one byte from the keyboard if it's allowed */
691 /* Currently unused. */
692 static int
693 hvkbd_read(keyboard_t *kbd, int wait)
694 {
695 	DEBUG_HVKBD(kbd, "%s\n", __func__);
696 	HVKBD_LOCK_ASSERT();
697 	if (!KBD_IS_ACTIVE(kbd))
698 		return (-1);
699 	return hvkbd_read_char_locked(kbd, wait);
700 }
701 
702 #ifdef EVDEV_SUPPORT
703 static void
704 hvkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
705     int32_t value)
706 {
707 	keyboard_t *kbd = evdev_get_softc(evdev);
708 
709 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
710 	(type == EV_LED || type == EV_REP)) {
711 		mtx_lock(&Giant);
712 		kbd_ev_event(kbd, type, code, value);
713 		mtx_unlock(&Giant);
714 	}
715 }
716 #endif
717 
718 static keyboard_switch_t hvkbdsw = {
719 	.probe =	hvkbd_probe,		/* not used */
720 	.init =		hvkbd_init,
721 	.term =		hvkbd_term,		/* not used */
722 	.intr =		hvkbd_intr,		/* not used */
723 	.test_if =	hvkbd_test_if,		/* not used */
724 	.enable =	hvkbd_enable,
725 	.disable =	hvkbd_disable,
726 	.read =		hvkbd_read,
727 	.check =	hvkbd_check,
728 	.read_char =	hvkbd_read_char,
729 	.check_char =	hvkbd_check_char,
730 	.ioctl =	hvkbd_ioctl,
731 	.lock =		hvkbd_lock,		/* not used */
732 	.clear_state =	hvkbd_clear_state,
733 	.get_state =	hvkbd_get_state,	/* not used */
734 	.set_state =	hvkbd_set_state,	/* not used */
735 	.poll =		hvkbd_poll,
736 };
737 
738 KEYBOARD_DRIVER(hvkbd, hvkbdsw, hvkbd_configure);
739 
740 void
741 hv_kbd_intr(hv_kbd_sc *sc)
742 {
743 	uint32_t c;
744 	if ((sc->sc_flags & HVKBD_FLAG_POLLING) != 0)
745 		return;
746 
747 	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
748 	    KBD_IS_BUSY(&sc->sc_kbd)) {
749 		/* let the callback function process the input */
750 		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
751 		    sc->sc_kbd.kb_callback.kc_arg);
752 	} else {
753 		/* read and discard the input, no one is waiting for it */
754 		do {
755 			c = hvkbd_read_char(&sc->sc_kbd, 0);
756 		} while (c != NOKEY);
757 	}
758 }
759 
760 int
761 hvkbd_driver_load(module_t mod, int what, void *arg)
762 {
763 	switch (what) {
764 	case MOD_LOAD:
765 		kbd_add_driver(&hvkbd_kbd_driver);
766 		break;
767 	case MOD_UNLOAD:
768 		kbd_delete_driver(&hvkbd_kbd_driver);
769 		break;
770 	}
771 	return (0);
772 }
773 
774 int
775 hv_kbd_drv_attach(device_t dev)
776 {
777 	hv_kbd_sc *sc = device_get_softc(dev);
778 	int unit = device_get_unit(dev);
779 	keyboard_t *kbd = &sc->sc_kbd;
780 	keyboard_switch_t *sw;
781 #ifdef EVDEV_SUPPORT
782 	struct evdev_dev *evdev;
783 #endif
784 
785 	sw = kbd_get_switch(HVKBD_DRIVER_NAME);
786 	if (sw == NULL) {
787 		return (ENXIO);
788 	}
789 
790 	kbd_init_struct(kbd, HVKBD_DRIVER_NAME, KB_OTHER, unit, 0, 0, 0);
791 	kbd->kb_data = (void *)sc;
792 	kbd_set_maps(kbd, &key_map, &accent_map, fkey_tab, nitems(fkey_tab));
793 	KBD_FOUND_DEVICE(kbd);
794 	hvkbd_clear_state(kbd);
795 	KBD_PROBE_DONE(kbd);
796 	KBD_INIT_DONE(kbd);
797 	sc->sc_mode = K_XLATE;
798 	(*sw->enable)(kbd);
799 
800 #ifdef EVDEV_SUPPORT
801 	evdev = evdev_alloc();
802 	evdev_set_name(evdev, "Hyper-V keyboard");
803 	evdev_set_phys(evdev, device_get_nameunit(dev));
804 	evdev_set_id(evdev, BUS_VIRTUAL, 0, 0, 0);
805 	evdev_set_methods(evdev, kbd, &hvkbd_evdev_methods);
806 	evdev_support_event(evdev, EV_SYN);
807 	evdev_support_event(evdev, EV_KEY);
808 	evdev_support_event(evdev, EV_LED);
809 	evdev_support_event(evdev, EV_REP);
810 	evdev_support_all_known_keys(evdev);
811 	evdev_support_led(evdev, LED_NUML);
812 	evdev_support_led(evdev, LED_CAPSL);
813 	evdev_support_led(evdev, LED_SCROLLL);
814 	if (evdev_register_mtx(evdev, &Giant))
815 		evdev_free(evdev);
816 	else
817 		sc->ks_evdev = evdev;
818 	sc->ks_evdev_state = 0;
819 #endif
820 
821 	if (kbd_register(kbd) < 0) {
822 		goto detach;
823 	}
824 	KBD_CONFIG_DONE(kbd);
825 #ifdef KBD_INSTALL_CDEV
826         if (kbd_attach(kbd)) {
827 		goto detach;
828 	}
829 #endif
830 	if (bootverbose) {
831 		kbdd_diag(kbd, bootverbose);
832 	}
833 	return (0);
834 detach:
835 	hv_kbd_drv_detach(dev);
836 	return (ENXIO);
837 }
838 
839 int
840 hv_kbd_drv_detach(device_t dev)
841 {
842 	int error = 0;
843 	hv_kbd_sc *sc = device_get_softc(dev);
844 	hvkbd_disable(&sc->sc_kbd);
845 #ifdef EVDEV_SUPPORT
846 	evdev_free(sc->ks_evdev);
847 #endif
848 	if (KBD_IS_CONFIGURED(&sc->sc_kbd)) {
849 		error = kbd_unregister(&sc->sc_kbd);
850 		if (error) {
851 			device_printf(dev, "WARNING: kbd_unregister() "
852 			    "returned non-zero! (ignored)\n");
853 		}
854 	}
855 #ifdef KBD_INSTALL_CDEV
856 	error = kbd_detach(&sc->sc_kbd);
857 #endif
858 	return (error);
859 }
860 
861