hvkbd.c revision 1.3 1 /* $NetBSD: hvkbd.c,v 1.3 2019/10/01 18:00:08 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Microsoft Corp.
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 unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/hyperv/input/hv_kbd.c 317821 2017-05-05 03:28:30Z sephe $
29 * $FreeBSD: head/sys/dev/hyperv/input/hv_kbdc.c 320490 2017-06-30 03:01:22Z sephe $
30 * $FreeBSD: head/sys/dev/hyperv/input/hv_kbdc.h 316515 2017-04-05 05:01:23Z sephe $
31 */
32
33 #ifdef _KERNEL_OPT
34 #include "opt_pckbd_layout.h"
35 #include "opt_wsdisplay_compat.h"
36 #endif /* _KERNEL_OPT */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: hvkbd.c,v 1.3 2019/10/01 18:00:08 chs Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/mutex.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.h>
47 #include <sys/module.h>
48 #include <sys/pmf.h>
49 #include <sys/proc.h>
50 #include <sys/queue.h>
51
52 #include <dev/hyperv/vmbusvar.h>
53 #include <dev/hyperv/hvkbdvar.h>
54
55 #include <dev/wscons/wsconsio.h>
56 #include <dev/wscons/wskbdvar.h>
57 #include <dev/wscons/wsksymdef.h>
58 #include <dev/wscons/wsksymvar.h>
59 #include <dev/pckbport/wskbdmap_mfii.h>
60
61 #define HVKBD_BUFSIZE (4 * PAGE_SIZE)
62 #define HVKBD_TX_RING_SIZE (10 * PAGE_SIZE)
63 #define HVKBD_RX_RING_SIZE (10 * PAGE_SIZE)
64
65 #define HVKBD_VER_MAJOR (1)
66 #define HVKBD_VER_MINOR (0)
67 #define HVKBD_VERSION ((HVKBD_VER_MAJOR << 16) | HVKBD_VER_MINOR)
68
69 enum hvkbd_msg_type {
70 HVKBD_PROTO_REQUEST = 1,
71 HVKBD_PROTO_RESPONSE = 2,
72 HVKBD_PROTO_EVENT = 3,
73 HVKBD_PROTO_LED_INDICATORS = 4
74 };
75
76 struct hvkbd_msg_hdr {
77 uint32_t type;
78 } __packed;
79
80 struct hvkbd_proto_req {
81 struct hvkbd_msg_hdr hdr;
82 uint32_t ver;
83 } __packed;
84
85 struct hvkbd_proto_resp {
86 struct hvkbd_msg_hdr hdr;
87 uint32_t status;
88 #define RESP_STATUS_ACCEPTED __BIT(0)
89 } __packed;
90
91 struct keystroke {
92 uint16_t makecode;
93 uint16_t pad0;
94 uint32_t info;
95 #define KS_INFO_UNICODE __BIT(0)
96 #define KS_INFO_BREAK __BIT(1)
97 #define KS_INFO_E0 __BIT(2)
98 #define KS_INFO_E1 __BIT(3)
99 } __packed;
100
101 struct hvkbd_keystroke {
102 struct hvkbd_msg_hdr hdr;
103 struct keystroke ks;
104 } __packed;
105
106 struct hvkbd_keystroke_info {
107 LIST_ENTRY(hvkbd_keystroke_info) link;
108 STAILQ_ENTRY(hvkbd_keystroke_info) slink;
109 struct keystroke ks;
110 };
111
112 #define HVKBD_KEYBUF_SIZE 16
113
114 struct hvkbd_softc {
115 device_t sc_dev;
116
117 struct vmbus_channel *sc_chan;
118 void *sc_buf;
119
120 kmutex_t sc_ks_lock;
121 LIST_HEAD(, hvkbd_keystroke_info) sc_ks_free;
122 STAILQ_HEAD(, hvkbd_keystroke_info) sc_ks_queue;
123
124 int sc_enabled;
125 int sc_polling;
126 int sc_console_keyboard;
127 #if defined(WSDISPLAY_COMPAT_RAWKBD)
128 int sc_rawkbd;
129 #endif
130
131 int sc_connected;
132 uint32_t sc_connect_status;
133
134 device_t sc_wskbddev;
135 };
136
137 static int hvkbd_match(device_t, cfdata_t, void *);
138 static void hvkbd_attach(device_t, device_t, void *);
139
140 CFATTACH_DECL_NEW(hvkbd, sizeof(struct hvkbd_softc),
141 hvkbd_match, hvkbd_attach, NULL, NULL);
142
143 static int hvkbd_alloc_keybuf(struct hvkbd_softc *);
144 static void hvkbd_free_keybuf(struct hvkbd_softc *);
145
146 static int hvkbd_enable(void *, int);
147 static void hvkbd_set_leds(void *, int);
148 static int hvkbd_ioctl(void *, u_long, void *, int, struct lwp *);
149
150 static const struct wskbd_accessops hvkbd_accessops = {
151 hvkbd_enable,
152 hvkbd_set_leds,
153 hvkbd_ioctl,
154 };
155
156 static const struct wskbd_mapdata hvkbd_keymapdata = {
157 pckbd_keydesctab,
158 #if defined(PCKBD_LAYOUT)
159 PCKBD_LAYOUT,
160 #else
161 KB_US,
162 #endif
163 };
164
165 static int hvkbd_connect(struct hvkbd_softc *);
166 static void hvkbd_intr(void *);
167
168 static void hvkbd_cngetc(void *, u_int *, int *);
169 static void hvkbd_cnpollc(void *, int);
170
171 static const struct wskbd_consops hvkbd_consops = {
172 .getc = hvkbd_cngetc,
173 .pollc = hvkbd_cnpollc,
174 .bell = NULL,
175 };
176
177 static int hvkbd_is_console;
178
179 static int
180 hvkbd_match(device_t parent, cfdata_t cf, void *aux)
181 {
182 struct vmbus_attach_args *aa = aux;
183
184 if (memcmp(aa->aa_type, &hyperv_guid_kbd, sizeof(*aa->aa_type)) != 0)
185 return 0;
186
187 /* If hvkbd(4) is not console, we use pckbd(4) in Gen.1 VM. */
188 if (!hvkbd_is_console && hyperv_is_gen1())
189 return 0;
190
191 return 1;
192 }
193
194 static void
195 hvkbd_attach(device_t parent, device_t self, void *aux)
196 {
197 struct hvkbd_softc *sc = device_private(self);
198 struct vmbus_attach_args *aa = aux;
199 struct wskbddev_attach_args a;
200
201 sc->sc_dev = self;
202 sc->sc_chan = aa->aa_chan;
203
204 aprint_naive("\n");
205 aprint_normal(": Hyper-V Synthetic Keyboard\n");
206
207 mutex_init(&sc->sc_ks_lock, MUTEX_DEFAULT, IPL_TTY);
208 LIST_INIT(&sc->sc_ks_free);
209 STAILQ_INIT(&sc->sc_ks_queue);
210 hvkbd_alloc_keybuf(sc);
211
212 sc->sc_buf = kmem_zalloc(HVKBD_BUFSIZE, KM_SLEEP);
213 if (vmbus_channel_setdeferred(sc->sc_chan, device_xname(self))) {
214 aprint_error_dev(self,
215 "failed to create the interrupt thread\n");
216 goto free_buf;
217 }
218
219 sc->sc_chan->ch_flags &= ~CHF_BATCHED;
220 if (vmbus_channel_open(sc->sc_chan,
221 HVKBD_TX_RING_SIZE + HVKBD_RX_RING_SIZE, NULL, 0, hvkbd_intr, sc)) {
222 aprint_error_dev(self, "failed to open channel\n");
223 goto free_buf;
224 }
225
226 if (hvkbd_connect(sc))
227 goto free_buf;
228
229 if (!pmf_device_register(self, NULL, NULL))
230 aprint_error_dev(self, "couldn't establish power handler\n");
231
232 sc->sc_console_keyboard = hvkbd_is_console;
233 if (hvkbd_is_console)
234 hvkbd_is_console = 0;
235
236 if (sc->sc_console_keyboard) {
237 wskbd_cnattach(&hvkbd_consops, sc, &hvkbd_keymapdata);
238 hvkbd_enable(sc, 1);
239 }
240
241 a.console = sc->sc_console_keyboard;
242 a.keymap = &hvkbd_keymapdata;
243 a.accessops = &hvkbd_accessops;
244 a.accesscookie = sc;
245 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
246 return;
247
248 free_buf:
249 if (sc->sc_buf != NULL) {
250 kmem_free(sc->sc_buf, HVKBD_BUFSIZE);
251 sc->sc_buf = NULL;
252 }
253 hvkbd_free_keybuf(sc);
254 }
255
256 static int
257 hvkbd_alloc_keybuf(struct hvkbd_softc *sc)
258 {
259 struct hvkbd_keystroke_info *ksi;
260 int i;
261
262 for (i = 0; i < HVKBD_KEYBUF_SIZE; i++) {
263 ksi = kmem_zalloc(sizeof(*ksi), KM_SLEEP);
264 LIST_INSERT_HEAD(&sc->sc_ks_free, ksi, link);
265 }
266
267 return 0;
268 }
269
270 static void
271 hvkbd_free_keybuf(struct hvkbd_softc *sc)
272 {
273 struct hvkbd_keystroke_info *ksi;
274
275 while ((ksi = STAILQ_FIRST(&sc->sc_ks_queue)) != NULL) {
276 STAILQ_REMOVE(&sc->sc_ks_queue, ksi, hvkbd_keystroke_info,
277 slink);
278 kmem_free(ksi, sizeof(*ksi));
279 }
280 while ((ksi = LIST_FIRST(&sc->sc_ks_free)) != NULL) {
281 LIST_REMOVE(ksi, link);
282 kmem_free(ksi, sizeof(*ksi));
283 }
284 }
285
286 int
287 hvkbd_enable(void *v, int on)
288 {
289 struct hvkbd_softc *sc = v;
290
291 sc->sc_enabled = on;
292
293 return 0;
294 }
295
296 static void
297 hvkbd_set_leds(void *v, int leds)
298 {
299 }
300
301 static int
302 hvkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
303 {
304 #if defined(WSDISPLAY_COMPAT_RAWKBD)
305 struct hvkbd_softc *sc = v;
306 #endif
307
308 switch (cmd) {
309 case WSKBDIO_GTYPE:
310 *(int *)data = WSKBD_TYPE_HYPERV;
311 return 0;
312
313 case WSKBDIO_SETLEDS:
314 hvkbd_set_leds(v, *(int *)data);
315 return 0;
316
317 case WSKBDIO_GETLEDS:
318 *(int *)data = 0;
319 return 0;
320
321 #if defined(WSDISPLAY_COMPAT_RAWKBD)
322 case WSKBDIO_SETMODE:
323 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
324 return 0;
325 #endif
326 }
327
328 return EPASSTHROUGH;
329 }
330
331 static int
332 hvkbd_connect(struct hvkbd_softc *sc)
333 {
334 struct hvkbd_proto_req req;
335 int timo = 100;
336 int error, s;
337
338 sc->sc_connected = 0;
339
340 memset(&req, 0, sizeof(req));
341 req.hdr.type = HVKBD_PROTO_REQUEST;
342 req.ver = HVKBD_VERSION;
343 error = vmbus_channel_send(sc->sc_chan, &req, sizeof(req),
344 0, VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC);
345 if (error) {
346 aprint_error_dev(sc->sc_dev, "failed to send connect: %d\n",
347 error);
348 return error;
349 }
350
351 do {
352 if (cold)
353 delay(1000);
354 else
355 tsleep(sc, PRIBIO | PCATCH, "hvkbdcon", 1);
356 s = spltty();
357 hvkbd_intr(sc);
358 splx(s);
359 } while (--timo > 0 && sc->sc_connected == 0);
360
361 if (timo == 0 && sc->sc_connected == 0) {
362 aprint_error_dev(sc->sc_dev, "connect timed out\n");
363 return ETIMEDOUT;
364 }
365
366 if (!(sc->sc_connect_status & RESP_STATUS_ACCEPTED)) {
367 aprint_error_dev(sc->sc_dev, "protocol request failed\n");
368 return ENODEV;
369 }
370
371 return 0;
372 }
373
374 static int
375 hvkbd_keybuf_add_keystroke(struct hvkbd_softc *sc, const struct keystroke *ks)
376 {
377 struct hvkbd_keystroke_info *ksi;
378
379 mutex_enter(&sc->sc_ks_lock);
380 ksi = LIST_FIRST(&sc->sc_ks_free);
381 if (ksi != NULL) {
382 LIST_REMOVE(ksi, link);
383 ksi->ks = *ks;
384 STAILQ_INSERT_TAIL(&sc->sc_ks_queue, ksi, slink);
385 }
386 mutex_exit(&sc->sc_ks_lock);
387
388 return (ksi != NULL) ? 0 : 1;
389 }
390
391 static int
392 hvkbd_decode(struct hvkbd_softc *sc, u_int *type, int *scancode)
393 {
394 struct hvkbd_keystroke_info *ksi;
395 struct keystroke ks;
396
397 mutex_enter(&sc->sc_ks_lock);
398 ksi = STAILQ_FIRST(&sc->sc_ks_queue);
399 if (ksi != NULL) {
400 STAILQ_REMOVE_HEAD(&sc->sc_ks_queue, slink);
401 ks = ksi->ks;
402 LIST_INSERT_HEAD(&sc->sc_ks_free, ksi, link);
403 }
404 mutex_exit(&sc->sc_ks_lock);
405
406 if (ksi == NULL)
407 return 0;
408
409 /*
410 * XXX: Hyper-V host send unicode to VM through 'Type clipboard text',
411 * the mapping from unicode to scancode depends on the keymap.
412 * It is so complicated that we do not plan to support it yet.
413 */
414 if (ks.info & KS_INFO_UNICODE)
415 return 0;
416
417 *type = (ks.info & KS_INFO_BREAK) ?
418 WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
419 *scancode = ks.makecode;
420 return 1;
421 }
422
423 #if defined(WSDISPLAY_COMPAT_RAWKBD)
424 static int
425 hvkbd_encode(struct hvkbd_softc *sc, u_char *buf, int *len)
426 {
427 struct hvkbd_keystroke_info *ksi;
428 struct keystroke ks;
429 int i;
430
431 mutex_enter(&sc->sc_ks_lock);
432 ksi = STAILQ_FIRST(&sc->sc_ks_queue);
433 if (ksi != NULL) {
434 STAILQ_REMOVE_HEAD(&sc->sc_ks_queue, slink);
435 ks = ksi->ks;
436 LIST_INSERT_HEAD(&sc->sc_ks_free, ksi, link);
437 }
438 mutex_exit(&sc->sc_ks_lock);
439
440 if (ksi == NULL)
441 return 0;
442
443 /*
444 * XXX: Hyper-V host send unicode to VM through 'Type clipboard text',
445 * the mapping from unicode to scancode depends on the keymap.
446 * It is so complicated that we do not plan to support it yet.
447 */
448 if (ks.info & KS_INFO_UNICODE)
449 return 0;
450
451 i = 0;
452 if (ks.info & (KS_INFO_E0|KS_INFO_E1)) {
453 if (ks.info & KS_INFO_E0)
454 buf[i++] = 0xe0;
455 else
456 buf[i++] = 0xe1;
457 }
458 if (ks.info & KS_INFO_BREAK)
459 buf[i++] = (u_char)ks.makecode & 0x80;
460 else
461 buf[i++] = (u_char)ks.makecode;
462
463 KDASSERT(i <= *len);
464 *len = i;
465
466 return 1;
467 }
468 #endif
469
470 static void
471 hvkbd_intr(void *xsc)
472 {
473 struct hvkbd_softc *sc = xsc;
474 struct vmbus_chanpkt_hdr *cph;
475 const struct hvkbd_msg_hdr *hdr;
476 const struct hvkbd_proto_resp *rsp;
477 const struct hvkbd_keystroke *ks;
478 uint64_t rid;
479 uint32_t rlen;
480 u_int type;
481 int key, error;
482
483 for (;;) {
484 error = vmbus_channel_recv(sc->sc_chan, sc->sc_buf,
485 HVKBD_BUFSIZE, &rlen, &rid, 1);
486 if (error != 0 || rlen == 0) {
487 if (error != EAGAIN)
488 device_printf(sc->sc_dev,
489 "failed to receive a reply packet\n");
490 return;
491 }
492
493 cph = (struct vmbus_chanpkt_hdr *)sc->sc_buf;
494 switch (cph->cph_type) {
495 case VMBUS_CHANPKT_TYPE_INBAND:
496 hdr = VMBUS_CHANPKT_CONST_DATA(cph);
497 if (rlen < sizeof(*hdr)) {
498 device_printf(sc->sc_dev, "Illegal packet\n");
499 continue;
500 }
501
502 switch (hdr->type) {
503 case HVKBD_PROTO_RESPONSE:
504 if (!sc->sc_connected) {
505 rsp = VMBUS_CHANPKT_CONST_DATA(cph);
506 if (rlen < sizeof(*rsp)) {
507 device_printf(sc->sc_dev,
508 "Illegal resp packet\n");
509 break;
510 }
511 sc->sc_connect_status = rsp->status;
512 sc->sc_connected = 1;
513 wakeup(sc);
514 }
515 break;
516
517 case HVKBD_PROTO_EVENT:
518 if (sc->sc_wskbddev == NULL || !sc->sc_enabled)
519 break;
520
521 ks = VMBUS_CHANPKT_CONST_DATA(cph);
522 hvkbd_keybuf_add_keystroke(sc, &ks->ks);
523 if (sc->sc_polling)
524 break;
525
526 #if defined(WSDISPLAY_COMPAT_RAWKBD)
527 if (sc->sc_rawkbd) {
528 u_char buf[2];
529 int len;
530
531 len = sizeof(buf);
532 if (hvkbd_encode(sc, buf, &len)) {
533 wskbd_rawinput(sc->sc_wskbddev,
534 buf, len);
535 }
536 break;
537 }
538 #endif
539 if (hvkbd_decode(sc, &type, &key))
540 wskbd_input(sc->sc_wskbddev, type, key);
541 break;
542
543 case HVKBD_PROTO_REQUEST:
544 case HVKBD_PROTO_LED_INDICATORS:
545 device_printf(sc->sc_dev,
546 "unhandled message: %d\n", hdr->type);
547 break;
548
549 default:
550 device_printf(sc->sc_dev,
551 "unknown message: %d\n", hdr->type);
552 break;
553 }
554 break;
555
556 case VMBUS_CHANPKT_TYPE_COMP:
557 case VMBUS_CHANPKT_TYPE_RXBUF:
558 device_printf(sc->sc_dev, "unhandled event: %d\n",
559 cph->cph_type);
560 break;
561
562 default:
563 device_printf(sc->sc_dev, "unknown event: %d\n",
564 cph->cph_type);
565 break;
566 }
567 }
568 }
569
570 int
571 hvkbd_cnattach(void)
572 {
573
574 hvkbd_is_console = 1;
575
576 return 0;
577 }
578
579 static void
580 hvkbd_cngetc(void *v, u_int *type, int *data)
581 {
582 struct hvkbd_softc *sc = v;
583
584 while (!hvkbd_decode(sc, type, data))
585 hvkbd_intr(sc);
586 }
587
588 static void
589 hvkbd_cnpollc(void *v, int on)
590 {
591 struct hvkbd_softc *sc = v;
592
593 sc->sc_polling = on;
594 }
595
596 MODULE(MODULE_CLASS_DRIVER, hvkbd, "vmbus");
597
598 #ifdef _MODULE
599 #include "ioconf.c"
600 #endif
601
602 static int
603 hvkbd_modcmd(modcmd_t cmd, void *aux)
604 {
605 int error = 0;
606
607 switch (cmd) {
608 case MODULE_CMD_INIT:
609 #ifdef _MODULE
610 error = config_init_component(cfdriver_ioconf_hvkbd,
611 cfattach_ioconf_hvkbd, cfdata_ioconf_hvkbd);
612 #endif
613 break;
614
615 case MODULE_CMD_FINI:
616 #ifdef _MODULE
617 error = config_fini_component(cfdriver_ioconf_hvtkbd,
618 cfattach_ioconf_hvkbd, cfdata_ioconf_hvkbd);
619 #endif
620 break;
621
622 case MODULE_CMD_AUTOUNLOAD:
623 error = EBUSY;
624 break;
625
626 default:
627 error = ENOTTY;
628 break;
629 }
630
631 return error;
632 }
633