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 <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/conf.h>
31 #include <sys/uio.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/mbuf.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/taskqueue.h>
38 #include <sys/selinfo.h>
39 #include <sys/sysctl.h>
40 #include <sys/poll.h>
41 #include <sys/proc.h>
42 #include <sys/queue.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysproto.h>
45 #include <sys/systm.h>
46 #include <sys/mutex.h>
47
48 #include <sys/kbio.h>
49 #include <dev/kbd/kbdreg.h>
50
51 #include <dev/hyperv/include/hyperv.h>
52 #include <dev/hyperv/utilities/hv_utilreg.h>
53 #include <dev/hyperv/utilities/vmbus_icreg.h>
54 #include <dev/hyperv/utilities/vmbus_icvar.h>
55 #include <dev/hyperv/include/vmbus_xact.h>
56
57 #include "dev/hyperv/input/hv_kbdc.h"
58 #include "vmbus_if.h"
59
60 #define HV_KBD_VER_MAJOR (1)
61 #define HV_KBD_VER_MINOR (0)
62
63 #define HV_KBD_VER (HV_KBD_VER_MINOR | (HV_KBD_VER_MAJOR) << 16)
64
65 #define HV_KBD_PROTO_ACCEPTED (1)
66
67 #define HV_BUFF_SIZE (4*PAGE_SIZE)
68 #define HV_KBD_RINGBUFF_SEND_SZ (10*PAGE_SIZE)
69 #define HV_KBD_RINGBUFF_RECV_SZ (10*PAGE_SIZE)
70
71 enum hv_kbd_msg_type_t {
72 HV_KBD_PROTO_REQUEST = 1,
73 HV_KBD_PROTO_RESPONSE = 2,
74 HV_KBD_PROTO_EVENT = 3,
75 HV_KBD_PROTO_LED_INDICATORS = 4,
76 };
77
78 typedef struct hv_kbd_msg_hdr_t {
79 uint32_t type;
80 } hv_kbd_msg_hdr;
81
82 typedef struct hv_kbd_msg_t {
83 hv_kbd_msg_hdr hdr;
84 char data[];
85 } hv_kbd_msg;
86
87 typedef struct hv_kbd_proto_req_t {
88 hv_kbd_msg_hdr hdr;
89 uint32_t ver;
90 } hv_kbd_proto_req;
91
92 typedef struct hv_kbd_proto_resp_t {
93 hv_kbd_msg_hdr hdr;
94 uint32_t status;
95 } hv_kbd_proto_resp;
96
97 #define HV_KBD_PROTO_REQ_SZ (sizeof(hv_kbd_proto_req))
98 #define HV_KBD_PROTO_RESP_SZ (sizeof(hv_kbd_proto_resp))
99
100 /**
101 * the struct in win host:
102 * typedef struct _HK_MESSAGE_KEYSTROKE
103 * {
104 * HK_MESSAGE_HEADER Header;
105 * UINT16 MakeCode;
106 * UINT32 IsUnicode:1;
107 * UINT32 IsBreak:1;
108 * UINT32 IsE0:1;
109 * UINT32 IsE1:1;
110 * UINT32 Reserved:28;
111 * } HK_MESSAGE_KEYSTROKE
112 */
113 typedef struct hv_kbd_keystroke_t {
114 hv_kbd_msg_hdr hdr;
115 keystroke ks;
116 } hv_kbd_keystroke;
117
118 static const struct vmbus_ic_desc vmbus_kbd_descs[] = {
119 {
120 .ic_guid = { .hv_guid = {
121 0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48,
122 0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76, 0x84} },
123 .ic_desc = "Hyper-V KBD"
124 },
125 VMBUS_IC_DESC_END
126 };
127
128 static int hv_kbd_attach(device_t dev);
129 static int hv_kbd_detach(device_t dev);
130
131 /**
132 * return 1 if producer is ready
133 */
134 int
hv_kbd_prod_is_ready(hv_kbd_sc * sc)135 hv_kbd_prod_is_ready(hv_kbd_sc *sc)
136 {
137 int ret;
138 mtx_lock(&sc->ks_mtx);
139 ret = !STAILQ_EMPTY(&sc->ks_queue);
140 mtx_unlock(&sc->ks_mtx);
141 return (ret);
142 }
143
144 int
hv_kbd_produce_ks(hv_kbd_sc * sc,const keystroke * ks)145 hv_kbd_produce_ks(hv_kbd_sc *sc, const keystroke *ks)
146 {
147 int ret = 0;
148 keystroke_info *ksi;
149 mtx_lock(&sc->ks_mtx);
150 if (LIST_EMPTY(&sc->ks_free_list)) {
151 DEBUG_HVSC(sc, "NO buffer!\n");
152 ret = 1;
153 } else {
154 ksi = LIST_FIRST(&sc->ks_free_list);
155 LIST_REMOVE(ksi, link);
156 ksi->ks = *ks;
157 STAILQ_INSERT_TAIL(&sc->ks_queue, ksi, slink);
158 }
159 mtx_unlock(&sc->ks_mtx);
160 return (ret);
161 }
162
163 /**
164 * return 0 if successfully get the 1st item of queue without removing it
165 */
166 int
hv_kbd_fetch_top(hv_kbd_sc * sc,keystroke * result)167 hv_kbd_fetch_top(hv_kbd_sc *sc, keystroke *result)
168 {
169 int ret = 0;
170 keystroke_info *ksi = NULL;
171 mtx_lock(&sc->ks_mtx);
172 if (STAILQ_EMPTY(&sc->ks_queue)) {
173 DEBUG_HVSC(sc, "Empty queue!\n");
174 ret = 1;
175 } else {
176 ksi = STAILQ_FIRST(&sc->ks_queue);
177 *result = ksi->ks;
178 }
179 mtx_unlock(&sc->ks_mtx);
180 return (ret);
181 }
182
183 /**
184 * return 0 if successfully removing the top item
185 */
186 int
hv_kbd_remove_top(hv_kbd_sc * sc)187 hv_kbd_remove_top(hv_kbd_sc *sc)
188 {
189 int ret = 0;
190 keystroke_info *ksi = NULL;
191 mtx_lock(&sc->ks_mtx);
192 if (STAILQ_EMPTY(&sc->ks_queue)) {
193 DEBUG_HVSC(sc, "Empty queue!\n");
194 ret = 1;
195 } else {
196 ksi = STAILQ_FIRST(&sc->ks_queue);
197 STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
198 LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link);
199 }
200 mtx_unlock(&sc->ks_mtx);
201 return (ret);
202 }
203
204 /**
205 * return 0 if successfully modify the 1st item of queue
206 */
207 int
hv_kbd_modify_top(hv_kbd_sc * sc,keystroke * top)208 hv_kbd_modify_top(hv_kbd_sc *sc, keystroke *top)
209 {
210 int ret = 0;
211 keystroke_info *ksi = NULL;
212 mtx_lock(&sc->ks_mtx);
213 if (STAILQ_EMPTY(&sc->ks_queue)) {
214 DEBUG_HVSC(sc, "Empty queue!\n");
215 ret = 1;
216 } else {
217 ksi = STAILQ_FIRST(&sc->ks_queue);
218 ksi->ks = *top;
219 }
220 mtx_unlock(&sc->ks_mtx);
221 return (ret);
222 }
223
224 static int
hv_kbd_probe(device_t dev)225 hv_kbd_probe(device_t dev)
226 {
227 device_t bus = device_get_parent(dev);
228 const struct vmbus_ic_desc *d;
229
230 if (resource_disabled(device_get_name(dev), 0))
231 return (ENXIO);
232
233 for (d = vmbus_kbd_descs; d->ic_desc != NULL; ++d) {
234 if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
235 device_set_desc(dev, d->ic_desc);
236 return (BUS_PROBE_DEFAULT);
237 }
238 }
239 return (ENXIO);
240 }
241
242 static void
hv_kbd_on_response(hv_kbd_sc * sc,struct vmbus_chanpkt_hdr * pkt)243 hv_kbd_on_response(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
244 {
245 struct vmbus_xact_ctx *xact = sc->hs_xact_ctx;
246 if (xact != NULL) {
247 DEBUG_HVSC(sc, "hvkbd is ready\n");
248 vmbus_xact_ctx_wakeup(xact, VMBUS_CHANPKT_CONST_DATA(pkt),
249 VMBUS_CHANPKT_DATALEN(pkt));
250 }
251 }
252
253 static void
hv_kbd_on_received(hv_kbd_sc * sc,struct vmbus_chanpkt_hdr * pkt)254 hv_kbd_on_received(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
255 {
256
257 const hv_kbd_msg *msg = VMBUS_CHANPKT_CONST_DATA(pkt);
258 const hv_kbd_proto_resp *resp =
259 VMBUS_CHANPKT_CONST_DATA(pkt);
260 const hv_kbd_keystroke *keystroke =
261 VMBUS_CHANPKT_CONST_DATA(pkt);
262 uint32_t msg_len = VMBUS_CHANPKT_DATALEN(pkt);
263 enum hv_kbd_msg_type_t msg_type;
264 uint32_t info;
265 uint16_t scan_code;
266
267 if (msg_len <= sizeof(hv_kbd_msg)) {
268 device_printf(sc->dev, "Illegal packet\n");
269 return;
270 }
271 msg_type = msg->hdr.type;
272 switch (msg_type) {
273 case HV_KBD_PROTO_RESPONSE:
274 hv_kbd_on_response(sc, pkt);
275 DEBUG_HVSC(sc, "keyboard resp: 0x%x\n",
276 resp->status);
277 break;
278 case HV_KBD_PROTO_EVENT:
279 info = keystroke->ks.info;
280 scan_code = keystroke->ks.makecode;
281 DEBUG_HVSC(sc, "keystroke info: 0x%x, scan: 0x%x\n",
282 info, scan_code);
283 hv_kbd_produce_ks(sc, &keystroke->ks);
284 hv_kbd_intr(sc);
285 default:
286 break;
287 }
288 }
289
290 void
hv_kbd_read_channel(struct vmbus_channel * channel,void * context)291 hv_kbd_read_channel(struct vmbus_channel *channel, void *context)
292 {
293 uint8_t *buf;
294 uint32_t buflen = 0;
295 int ret = 0;
296
297 hv_kbd_sc *sc = (hv_kbd_sc*)context;
298 buf = sc->buf;
299 buflen = sc->buflen;
300 for (;;) {
301 struct vmbus_chanpkt_hdr *pkt = (struct vmbus_chanpkt_hdr *)buf;
302 uint32_t rxed = buflen;
303
304 ret = vmbus_chan_recv_pkt(channel, pkt, &rxed);
305 if (__predict_false(ret == ENOBUFS)) {
306 buflen = sc->buflen * 2;
307 while (buflen < rxed)
308 buflen *= 2;
309 buf = malloc(buflen, M_DEVBUF, M_WAITOK | M_ZERO);
310 device_printf(sc->dev, "expand recvbuf %d -> %d\n",
311 sc->buflen, buflen);
312 free(sc->buf, M_DEVBUF);
313 sc->buf = buf;
314 sc->buflen = buflen;
315 continue;
316 } else if (__predict_false(ret == EAGAIN)) {
317 /* No more channel packets; done! */
318 break;
319 }
320 KASSERT(!ret, ("vmbus_chan_recv_pkt failed: %d", ret));
321
322 DEBUG_HVSC(sc, "event: 0x%x\n", pkt->cph_type);
323 switch (pkt->cph_type) {
324 case VMBUS_CHANPKT_TYPE_COMP:
325 case VMBUS_CHANPKT_TYPE_RXBUF:
326 device_printf(sc->dev, "unhandled event: %d\n",
327 pkt->cph_type);
328 break;
329 case VMBUS_CHANPKT_TYPE_INBAND:
330 hv_kbd_on_received(sc, pkt);
331 break;
332 default:
333 device_printf(sc->dev, "unknown event: %d\n",
334 pkt->cph_type);
335 break;
336 }
337 }
338 }
339
340 static int
hv_kbd_connect_vsp(hv_kbd_sc * sc)341 hv_kbd_connect_vsp(hv_kbd_sc *sc)
342 {
343 int ret;
344 size_t resplen;
345 struct vmbus_xact *xact;
346 hv_kbd_proto_req *req;
347 const hv_kbd_proto_resp *resp;
348
349 xact = vmbus_xact_get(sc->hs_xact_ctx, sizeof(*req));
350 if (xact == NULL) {
351 device_printf(sc->dev, "no xact for kbd init");
352 return (ENODEV);
353 }
354 req = vmbus_xact_req_data(xact);
355 req->hdr.type = HV_KBD_PROTO_REQUEST;
356 req->ver = HV_KBD_VER;
357
358 vmbus_xact_activate(xact);
359 ret = vmbus_chan_send(sc->hs_chan,
360 VMBUS_CHANPKT_TYPE_INBAND,
361 VMBUS_CHANPKT_FLAG_RC,
362 req, sizeof(hv_kbd_proto_req),
363 (uint64_t)(uintptr_t)xact);
364 if (ret) {
365 device_printf(sc->dev, "fail to send\n");
366 vmbus_xact_deactivate(xact);
367 return (ret);
368 }
369 resp = vmbus_chan_xact_wait(sc->hs_chan, xact, &resplen, true);
370 if (resplen < HV_KBD_PROTO_RESP_SZ) {
371 device_printf(sc->dev, "hv_kbd init communicate failed\n");
372 ret = ENODEV;
373 goto clean;
374 }
375
376 if (!(resp->status & HV_KBD_PROTO_ACCEPTED)) {
377 device_printf(sc->dev, "hv_kbd protocol request failed\n");
378 ret = ENODEV;
379 }
380 clean:
381 vmbus_xact_put(xact);
382 DEBUG_HVSC(sc, "finish connect vsp\n");
383 return (ret);
384 }
385
386 static int
hv_kbd_attach1(device_t dev,vmbus_chan_callback_t cb)387 hv_kbd_attach1(device_t dev, vmbus_chan_callback_t cb)
388 {
389 int ret;
390 hv_kbd_sc *sc;
391
392 sc = device_get_softc(dev);
393 sc->buflen = HV_BUFF_SIZE;
394 sc->buf = malloc(sc->buflen, M_DEVBUF, M_WAITOK | M_ZERO);
395 vmbus_chan_set_readbatch(sc->hs_chan, false);
396 ret = vmbus_chan_open(
397 sc->hs_chan,
398 HV_KBD_RINGBUFF_SEND_SZ,
399 HV_KBD_RINGBUFF_RECV_SZ,
400 NULL, 0,
401 cb,
402 sc);
403 if (ret != 0) {
404 free(sc->buf, M_DEVBUF);
405 }
406 return (ret);
407 }
408
409 static int
hv_kbd_detach1(device_t dev)410 hv_kbd_detach1(device_t dev)
411 {
412 hv_kbd_sc *sc = device_get_softc(dev);
413 vmbus_chan_close(vmbus_get_channel(dev));
414 free(sc->buf, M_DEVBUF);
415 return (0);
416 }
417
418 static void
hv_kbd_init(hv_kbd_sc * sc)419 hv_kbd_init(hv_kbd_sc *sc)
420 {
421 const int max_list = 16;
422 int i;
423 keystroke_info *ksi;
424
425 mtx_init(&sc->ks_mtx, "hv_kbdc mutex", NULL, MTX_DEF);
426 LIST_INIT(&sc->ks_free_list);
427 STAILQ_INIT(&sc->ks_queue);
428 for (i = 0; i < max_list; i++) {
429 ksi = malloc(sizeof(keystroke_info),
430 M_DEVBUF, M_WAITOK|M_ZERO);
431 LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link);
432 }
433 }
434
435 static void
hv_kbd_fini(hv_kbd_sc * sc)436 hv_kbd_fini(hv_kbd_sc *sc)
437 {
438 keystroke_info *ksi;
439 while (!LIST_EMPTY(&sc->ks_free_list)) {
440 ksi = LIST_FIRST(&sc->ks_free_list);
441 LIST_REMOVE(ksi, link);
442 free(ksi, M_DEVBUF);
443 }
444 while (!STAILQ_EMPTY(&sc->ks_queue)) {
445 ksi = STAILQ_FIRST(&sc->ks_queue);
446 STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
447 free(ksi, M_DEVBUF);
448 }
449 mtx_destroy(&sc->ks_mtx);
450 }
451
452 static void
hv_kbd_sysctl(device_t dev)453 hv_kbd_sysctl(device_t dev)
454 {
455 struct sysctl_oid_list *child;
456 struct sysctl_ctx_list *ctx;
457 hv_kbd_sc *sc;
458
459 sc = device_get_softc(dev);
460 ctx = device_get_sysctl_ctx(dev);
461 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
462 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug", CTLFLAG_RW,
463 &sc->debug, 0, "debug hyperv keyboard");
464 }
465
466 static int
hv_kbd_attach(device_t dev)467 hv_kbd_attach(device_t dev)
468 {
469 int error = 0;
470 hv_kbd_sc *sc;
471
472 sc = device_get_softc(dev);
473 sc->hs_chan = vmbus_get_channel(dev);
474 sc->dev = dev;
475 hv_kbd_init(sc);
476 sc->hs_xact_ctx = vmbus_xact_ctx_create(bus_get_dma_tag(dev),
477 HV_KBD_PROTO_REQ_SZ, HV_KBD_PROTO_RESP_SZ, 0);
478 if (sc->hs_xact_ctx == NULL) {
479 error = ENOMEM;
480 goto failed;
481 }
482
483 error = hv_kbd_attach1(dev, hv_kbd_read_channel);
484 if (error)
485 goto failed;
486 error = hv_kbd_connect_vsp(sc);
487 if (error)
488 goto failed;
489
490 error = hv_kbd_drv_attach(dev);
491 if (error)
492 goto failed;
493 hv_kbd_sysctl(dev);
494 return (0);
495 failed:
496 hv_kbd_detach(dev);
497 return (error);
498 }
499
500 static int
hv_kbd_detach(device_t dev)501 hv_kbd_detach(device_t dev)
502 {
503 int ret;
504 hv_kbd_sc *sc = device_get_softc(dev);
505 hv_kbd_fini(sc);
506 if (sc->hs_xact_ctx != NULL)
507 vmbus_xact_ctx_destroy(sc->hs_xact_ctx);
508 ret = hv_kbd_detach1(dev);
509 if (!ret)
510 device_printf(dev, "Fail to detach\n");
511 return hv_kbd_drv_detach(dev);
512 }
513
514 static device_method_t kbd_methods[] = {
515 /* Device interface */
516 DEVMETHOD(device_probe, hv_kbd_probe),
517 DEVMETHOD(device_attach, hv_kbd_attach),
518 DEVMETHOD(device_detach, hv_kbd_detach),
519 { 0, 0 }
520 };
521
522 static driver_t kbd_driver = {HVKBD_DRIVER_NAME , kbd_methods, sizeof(hv_kbd_sc)};
523
524 DRIVER_MODULE(hv_kbd, vmbus, kbd_driver, hvkbd_driver_load, NULL);
525 MODULE_VERSION(hv_kbd, 1);
526 MODULE_DEPEND(hv_kbd, vmbus, 1, 1, 1);
527