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