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 <sys/param.h>
31 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/taskqueue.h>
40 #include <sys/selinfo.h>
41 #include <sys/sysctl.h>
44 #include <sys/queue.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysproto.h>
47 #include <sys/systm.h>
48 #include <sys/mutex.h>
51 #include <dev/kbd/kbdreg.h>
52 #include <dev/kbd/kbdtables.h>
54 #include <dev/hyperv/include/hyperv.h>
55 #include <dev/hyperv/utilities/hv_utilreg.h>
56 #include <dev/hyperv/utilities/vmbus_icreg.h>
57 #include <dev/hyperv/utilities/vmbus_icvar.h>
58 #include <dev/hyperv/include/vmbus_xact.h>
60 #include "dev/hyperv/input/hv_kbdc.h"
63 #define HV_KBD_VER_MAJOR (1)
64 #define HV_KBD_VER_MINOR (0)
66 #define HV_KBD_VER (HV_KBD_VER_MINOR | (HV_KBD_VER_MAJOR) << 16)
68 #define HV_KBD_PROTO_ACCEPTED (1)
70 #define HV_BUFF_SIZE (4*PAGE_SIZE)
71 #define HV_KBD_RINGBUFF_SEND_SZ (10*PAGE_SIZE)
72 #define HV_KBD_RINGBUFF_RECV_SZ (10*PAGE_SIZE)
74 enum hv_kbd_msg_type_t {
75 HV_KBD_PROTO_REQUEST = 1,
76 HV_KBD_PROTO_RESPONSE = 2,
77 HV_KBD_PROTO_EVENT = 3,
78 HV_KBD_PROTO_LED_INDICATORS = 4,
81 typedef struct hv_kbd_msg_hdr_t {
85 typedef struct hv_kbd_msg_t {
90 typedef struct hv_kbd_proto_req_t {
95 typedef struct hv_kbd_proto_resp_t {
100 #define HV_KBD_PROTO_REQ_SZ (sizeof(hv_kbd_proto_req))
101 #define HV_KBD_PROTO_RESP_SZ (sizeof(hv_kbd_proto_resp))
104 * the struct in win host:
105 * typedef struct _HK_MESSAGE_KEYSTROKE
107 * HK_MESSAGE_HEADER Header;
109 * UINT32 IsUnicode:1;
113 * UINT32 Reserved:28;
114 * } HK_MESSAGE_KEYSTROKE
116 typedef struct hv_kbd_keystroke_t {
121 static const struct vmbus_ic_desc vmbus_kbd_descs[] = {
123 .ic_guid = { .hv_guid = {
124 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48,
125 0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84} },
126 .ic_desc = "Hyper-V KBD"
131 static int hv_kbd_attach(device_t dev);
132 static int hv_kbd_detach(device_t dev);
135 * return 1 if producer is ready
138 hv_kbd_prod_is_ready(hv_kbd_sc *sc)
141 mtx_lock(&sc->ks_mtx);
142 ret = !STAILQ_EMPTY(&sc->ks_queue);
143 mtx_unlock(&sc->ks_mtx);
148 hv_kbd_produce_ks(hv_kbd_sc *sc, const keystroke *ks)
152 mtx_lock(&sc->ks_mtx);
153 if (LIST_EMPTY(&sc->ks_free_list)) {
154 DEBUG_HVSC(sc, "NO buffer!\n");
157 ksi = LIST_FIRST(&sc->ks_free_list);
158 LIST_REMOVE(ksi, link);
160 STAILQ_INSERT_TAIL(&sc->ks_queue, ksi, slink);
162 mtx_unlock(&sc->ks_mtx);
167 * return 0 if successfully get the 1st item of queue without removing it
170 hv_kbd_fetch_top(hv_kbd_sc *sc, keystroke *result)
173 keystroke_info *ksi = NULL;
174 mtx_lock(&sc->ks_mtx);
175 if (STAILQ_EMPTY(&sc->ks_queue)) {
176 DEBUG_HVSC(sc, "Empty queue!\n");
179 ksi = STAILQ_FIRST(&sc->ks_queue);
182 mtx_unlock(&sc->ks_mtx);
187 * return 0 if successfully removing the top item
190 hv_kbd_remove_top(hv_kbd_sc *sc)
193 keystroke_info *ksi = NULL;
194 mtx_lock(&sc->ks_mtx);
195 if (STAILQ_EMPTY(&sc->ks_queue)) {
196 DEBUG_HVSC(sc, "Empty queue!\n");
199 ksi = STAILQ_FIRST(&sc->ks_queue);
200 STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
201 LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link);
203 mtx_unlock(&sc->ks_mtx);
208 * return 0 if successfully modify the 1st item of queue
211 hv_kbd_modify_top(hv_kbd_sc *sc, keystroke *top)
214 keystroke_info *ksi = NULL;
215 mtx_lock(&sc->ks_mtx);
216 if (STAILQ_EMPTY(&sc->ks_queue)) {
217 DEBUG_HVSC(sc, "Empty queue!\n");
220 ksi = STAILQ_FIRST(&sc->ks_queue);
223 mtx_unlock(&sc->ks_mtx);
228 hv_kbd_probe(device_t dev)
230 device_t bus = device_get_parent(dev);
231 const struct vmbus_ic_desc *d;
233 if (resource_disabled(device_get_name(dev), 0))
236 for (d = vmbus_kbd_descs; d->ic_desc != NULL; ++d) {
237 if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
238 device_set_desc(dev, d->ic_desc);
239 return (BUS_PROBE_DEFAULT);
246 hv_kbd_on_response(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
248 struct vmbus_xact_ctx *xact = sc->hs_xact_ctx;
250 DEBUG_HVSC(sc, "hvkbd is ready\n");
251 vmbus_xact_ctx_wakeup(xact, VMBUS_CHANPKT_CONST_DATA(pkt),
252 VMBUS_CHANPKT_DATALEN(pkt));
257 hv_kbd_on_received(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
260 const hv_kbd_msg *msg = VMBUS_CHANPKT_CONST_DATA(pkt);
261 const hv_kbd_proto_resp *resp =
262 VMBUS_CHANPKT_CONST_DATA(pkt);
263 const hv_kbd_keystroke *keystroke =
264 VMBUS_CHANPKT_CONST_DATA(pkt);
265 uint32_t msg_len = VMBUS_CHANPKT_DATALEN(pkt);
266 enum hv_kbd_msg_type_t msg_type;
270 if (msg_len <= sizeof(hv_kbd_msg)) {
271 device_printf(sc->dev, "Illegal packet\n");
274 msg_type = msg->hdr.type;
276 case HV_KBD_PROTO_RESPONSE:
277 hv_kbd_on_response(sc, pkt);
278 DEBUG_HVSC(sc, "keyboard resp: 0x%x\n",
281 case HV_KBD_PROTO_EVENT:
282 info = keystroke->ks.info;
283 scan_code = keystroke->ks.makecode;
284 DEBUG_HVSC(sc, "keystroke info: 0x%x, scan: 0x%x\n",
286 hv_kbd_produce_ks(sc, &keystroke->ks);
294 hv_kbd_read_channel(struct vmbus_channel *channel, void *context)
300 hv_kbd_sc *sc = (hv_kbd_sc*)context;
304 struct vmbus_chanpkt_hdr *pkt = (struct vmbus_chanpkt_hdr *)buf;
305 uint32_t rxed = buflen;
307 ret = vmbus_chan_recv_pkt(channel, pkt, &rxed);
308 if (__predict_false(ret == ENOBUFS)) {
309 buflen = sc->buflen * 2;
310 while (buflen < rxed)
312 buf = malloc(buflen, M_DEVBUF, M_WAITOK | M_ZERO);
313 device_printf(sc->dev, "expand recvbuf %d -> %d\n",
315 free(sc->buf, M_DEVBUF);
319 } else if (__predict_false(ret == EAGAIN)) {
320 /* No more channel packets; done! */
323 KASSERT(!ret, ("vmbus_chan_recv_pkt failed: %d", ret));
325 DEBUG_HVSC(sc, "event: 0x%x\n", pkt->cph_type);
326 switch (pkt->cph_type) {
327 case VMBUS_CHANPKT_TYPE_COMP:
328 case VMBUS_CHANPKT_TYPE_RXBUF:
329 device_printf(sc->dev, "unhandled event: %d\n",
332 case VMBUS_CHANPKT_TYPE_INBAND:
333 hv_kbd_on_received(sc, pkt);
336 device_printf(sc->dev, "unknown event: %d\n",
344 hv_kbd_connect_vsp(hv_kbd_sc *sc)
348 struct vmbus_xact *xact;
349 hv_kbd_proto_req *req;
350 const hv_kbd_proto_resp *resp;
352 xact = vmbus_xact_get(sc->hs_xact_ctx, sizeof(*req));
354 device_printf(sc->dev, "no xact for kbd init");
357 req = vmbus_xact_req_data(xact);
358 req->hdr.type = HV_KBD_PROTO_REQUEST;
359 req->ver = HV_KBD_VER;
361 vmbus_xact_activate(xact);
362 ret = vmbus_chan_send(sc->hs_chan,
363 VMBUS_CHANPKT_TYPE_INBAND,
364 VMBUS_CHANPKT_FLAG_RC,
365 req, sizeof(hv_kbd_proto_req),
366 (uint64_t)(uintptr_t)xact);
368 device_printf(sc->dev, "fail to send\n");
369 vmbus_xact_deactivate(xact);
372 resp = vmbus_chan_xact_wait(sc->hs_chan, xact, &resplen, true);
373 if (resplen < HV_KBD_PROTO_RESP_SZ) {
374 device_printf(sc->dev, "hv_kbd init communicate failed\n");
379 if (!(resp->status & HV_KBD_PROTO_ACCEPTED)) {
380 device_printf(sc->dev, "hv_kbd protocol request failed\n");
384 vmbus_xact_put(xact);
385 DEBUG_HVSC(sc, "finish connect vsp\n");
390 hv_kbd_attach1(device_t dev, vmbus_chan_callback_t cb)
395 sc = device_get_softc(dev);
396 sc->buflen = HV_BUFF_SIZE;
397 sc->buf = malloc(sc->buflen, M_DEVBUF, M_WAITOK | M_ZERO);
398 vmbus_chan_set_readbatch(sc->hs_chan, false);
399 ret = vmbus_chan_open(
401 HV_KBD_RINGBUFF_SEND_SZ,
402 HV_KBD_RINGBUFF_RECV_SZ,
407 free(sc->buf, M_DEVBUF);
413 hv_kbd_detach1(device_t dev)
415 hv_kbd_sc *sc = device_get_softc(dev);
416 vmbus_chan_close(vmbus_get_channel(dev));
417 free(sc->buf, M_DEVBUF);
422 hv_kbd_init(hv_kbd_sc *sc)
424 const int max_list = 16;
428 mtx_init(&sc->ks_mtx, "hv_kbdc mutex", NULL, MTX_DEF);
429 LIST_INIT(&sc->ks_free_list);
430 STAILQ_INIT(&sc->ks_queue);
431 for (i = 0; i < max_list; i++) {
432 ksi = malloc(sizeof(keystroke_info),
433 M_DEVBUF, M_WAITOK|M_ZERO);
434 LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link);
439 hv_kbd_fini(hv_kbd_sc *sc)
442 while (!LIST_EMPTY(&sc->ks_free_list)) {
443 ksi = LIST_FIRST(&sc->ks_free_list);
444 LIST_REMOVE(ksi, link);
447 while (!STAILQ_EMPTY(&sc->ks_queue)) {
448 ksi = STAILQ_FIRST(&sc->ks_queue);
449 STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
452 mtx_destroy(&sc->ks_mtx);
456 hv_kbd_sysctl(device_t dev)
458 struct sysctl_oid_list *child;
459 struct sysctl_ctx_list *ctx;
462 sc = device_get_softc(dev);
463 ctx = device_get_sysctl_ctx(dev);
464 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
465 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug", CTLFLAG_RW,
466 &sc->debug, 0, "debug hyperv keyboard");
470 hv_kbd_attach(device_t dev)
475 sc = device_get_softc(dev);
476 sc->hs_chan = vmbus_get_channel(dev);
479 sc->hs_xact_ctx = vmbus_xact_ctx_create(bus_get_dma_tag(dev),
480 HV_KBD_PROTO_REQ_SZ, HV_KBD_PROTO_RESP_SZ, 0);
481 if (sc->hs_xact_ctx == NULL) {
486 error = hv_kbd_attach1(dev, hv_kbd_read_channel);
489 error = hv_kbd_connect_vsp(sc);
493 error = hv_kbd_drv_attach(dev);
504 hv_kbd_detach(device_t dev)
507 hv_kbd_sc *sc = device_get_softc(dev);
509 if (sc->hs_xact_ctx != NULL)
510 vmbus_xact_ctx_destroy(sc->hs_xact_ctx);
511 ret = hv_kbd_detach1(dev);
513 device_printf(dev, "Fail to detach\n");
514 return hv_kbd_drv_detach(dev);
517 static device_method_t kbd_methods[] = {
518 /* Device interface */
519 DEVMETHOD(device_probe, hv_kbd_probe),
520 DEVMETHOD(device_attach, hv_kbd_attach),
521 DEVMETHOD(device_detach, hv_kbd_detach),
525 static driver_t kbd_driver = {HVKBD_DRIVER_NAME , kbd_methods, sizeof(hv_kbd_sc)};
527 static devclass_t kbd_devclass;
529 DRIVER_MODULE(hv_kbd, vmbus, kbd_driver, kbd_devclass, hvkbd_driver_load, NULL);
530 MODULE_VERSION(hv_kbd, 1);
531 MODULE_DEPEND(hv_kbd, vmbus, 1, 1, 1);