xref: /freebsd/sys/dev/hyperv/input/hv_kbdc.c (revision 264104f26834fdb27974e0c5fdedf8f2f5a90383)
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 __FBSDID("$FreeBSD$");
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/lock.h>
39 #include <sys/taskqueue.h>
40 #include <sys/selinfo.h>
41 #include <sys/sysctl.h>
42 #include <sys/poll.h>
43 #include <sys/proc.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>
49 
50 #include <sys/kbio.h>
51 #include <dev/kbd/kbdreg.h>
52 #include <dev/kbd/kbdtables.h>
53 
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>
59 
60 #include "dev/hyperv/input/hv_kbdc.h"
61 #include "vmbus_if.h"
62 
63 #define HV_KBD_VER_MAJOR	(1)
64 #define HV_KBD_VER_MINOR	(0)
65 
66 #define HV_KBD_VER		(HV_KBD_VER_MINOR | (HV_KBD_VER_MAJOR) << 16)
67 
68 #define HV_KBD_PROTO_ACCEPTED	(1)
69 
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)
73 
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,
79 };
80 
81 typedef struct hv_kbd_msg_hdr_t {
82 	uint32_t type;
83 } hv_kbd_msg_hdr;
84 
85 typedef struct hv_kbd_msg_t {
86 	hv_kbd_msg_hdr hdr;
87 	char data[];
88 } hv_kbd_msg;
89 
90 typedef struct hv_kbd_proto_req_t {
91 	hv_kbd_msg_hdr	hdr;
92 	uint32_t	ver;
93 } hv_kbd_proto_req;
94 
95 typedef struct hv_kbd_proto_resp_t {
96 	hv_kbd_msg_hdr  hdr;
97 	uint32_t	status;
98 } hv_kbd_proto_resp;
99 
100 #define HV_KBD_PROTO_REQ_SZ	(sizeof(hv_kbd_proto_req))
101 #define HV_KBD_PROTO_RESP_SZ	(sizeof(hv_kbd_proto_resp))
102 
103 /**
104  * the struct in win host:
105  * typedef struct _HK_MESSAGE_KEYSTROKE
106  * {
107  *     HK_MESSAGE_HEADER Header;
108  *     UINT16 MakeCode;
109  *     UINT32 IsUnicode:1;
110  *     UINT32 IsBreak:1;
111  *     UINT32 IsE0:1;
112  *     UINT32 IsE1:1;
113  *     UINT32 Reserved:28;
114  * } HK_MESSAGE_KEYSTROKE
115  */
116 typedef struct hv_kbd_keystroke_t {
117 	hv_kbd_msg_hdr  hdr;
118 	keystroke	ks;
119 } hv_kbd_keystroke;
120 
121 static const struct vmbus_ic_desc vmbus_kbd_descs[] = {
122 	{
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"
127 	},
128 	VMBUS_IC_DESC_END
129 };
130 
131 static int hv_kbd_attach(device_t dev);
132 static int hv_kbd_detach(device_t dev);
133 
134 /**
135  * return 1 if producer is ready
136  */
137 int
138 hv_kbd_prod_is_ready(hv_kbd_sc *sc)
139 {
140 	int ret;
141 	mtx_lock(&sc->ks_mtx);
142 	ret = !STAILQ_EMPTY(&sc->ks_queue);
143 	mtx_unlock(&sc->ks_mtx);
144 	return (ret);
145 }
146 
147 int
148 hv_kbd_produce_ks(hv_kbd_sc *sc, const keystroke *ks)
149 {
150 	int ret = 0;
151 	keystroke_info *ksi;
152 	mtx_lock(&sc->ks_mtx);
153 	if (LIST_EMPTY(&sc->ks_free_list)) {
154 		DEBUG_HVSC(sc, "NO buffer!\n");
155 		ret = 1;
156 	} else {
157 		ksi = LIST_FIRST(&sc->ks_free_list);
158 		LIST_REMOVE(ksi, link);
159 		ksi->ks = *ks;
160 		STAILQ_INSERT_TAIL(&sc->ks_queue, ksi, slink);
161 	}
162 	mtx_unlock(&sc->ks_mtx);
163 	return (ret);
164 }
165 
166 /**
167  * return 0 if successfully get the 1st item of queue without removing it
168  */
169 int
170 hv_kbd_fetch_top(hv_kbd_sc *sc, keystroke *result)
171 {
172 	int ret = 0;
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");
177 		ret = 1;
178 	} else {
179 		ksi = STAILQ_FIRST(&sc->ks_queue);
180 		*result = ksi->ks;
181 	}
182 	mtx_unlock(&sc->ks_mtx);
183 	return (ret);
184 }
185 
186 /**
187  * return 0 if successfully removing the top item
188  */
189 int
190 hv_kbd_remove_top(hv_kbd_sc *sc)
191 {
192 	int ret = 0;
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");
197 		ret = 1;
198 	} else {
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);
202 	}
203 	mtx_unlock(&sc->ks_mtx);
204 	return (ret);
205 }
206 
207 /**
208  * return 0 if successfully modify the 1st item of queue
209  */
210 int
211 hv_kbd_modify_top(hv_kbd_sc *sc, keystroke *top)
212 {
213 	int ret = 0;
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");
218 		ret = 1;
219 	} else {
220 		ksi = STAILQ_FIRST(&sc->ks_queue);
221 		ksi->ks = *top;
222 	}
223 	mtx_unlock(&sc->ks_mtx);
224 	return (ret);
225 }
226 
227 static int
228 hv_kbd_probe(device_t dev)
229 {
230 	device_t bus = device_get_parent(dev);
231 	const struct vmbus_ic_desc *d;
232 
233 	if (resource_disabled(device_get_name(dev), 0))
234 		return (ENXIO);
235 
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);
240 		}
241 	}
242 	return (ENXIO);
243 }
244 
245 static void
246 hv_kbd_on_response(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
247 {
248 	struct vmbus_xact_ctx *xact = sc->hs_xact_ctx;
249 	if (xact != NULL) {
250 		DEBUG_HVSC(sc, "hvkbd is ready\n");
251 		vmbus_xact_ctx_wakeup(xact, VMBUS_CHANPKT_CONST_DATA(pkt),
252 		    VMBUS_CHANPKT_DATALEN(pkt));
253 	}
254 }
255 
256 static void
257 hv_kbd_on_received(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
258 {
259 
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;
267 	uint32_t info;
268 	uint16_t scan_code;
269 
270 	if (msg_len <= sizeof(hv_kbd_msg)) {
271 		device_printf(sc->dev, "Illegal packet\n");
272 		return;
273 	}
274 	msg_type = msg->hdr.type;
275 	switch (msg_type) {
276 		case HV_KBD_PROTO_RESPONSE:
277 			hv_kbd_on_response(sc, pkt);
278 			DEBUG_HVSC(sc, "keyboard resp: 0x%x\n",
279 			    resp->status);
280 			break;
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",
285 			    info, scan_code);
286 			hv_kbd_produce_ks(sc, &keystroke->ks);
287 			hv_kbd_intr(sc);
288 		default:
289 			break;
290 	}
291 }
292 
293 void
294 hv_kbd_read_channel(struct vmbus_channel *channel, void *context)
295 {
296 	uint8_t *buf;
297 	uint32_t buflen = 0;
298 	int ret = 0;
299 
300 	hv_kbd_sc *sc = (hv_kbd_sc*)context;
301 	buf = sc->buf;
302 	buflen = sc->buflen;
303 	for (;;) {
304 		struct vmbus_chanpkt_hdr *pkt = (struct vmbus_chanpkt_hdr *)buf;
305 		uint32_t rxed = buflen;
306 
307 		ret = vmbus_chan_recv_pkt(channel, pkt, &rxed);
308 		if (__predict_false(ret == ENOBUFS)) {
309 			buflen = sc->buflen * 2;
310 			while (buflen < rxed)
311 				buflen *= 2;
312 			buf = malloc(buflen, M_DEVBUF, M_WAITOK | M_ZERO);
313 			device_printf(sc->dev, "expand recvbuf %d -> %d\n",
314 			    sc->buflen, buflen);
315 			free(sc->buf, M_DEVBUF);
316 			sc->buf = buf;
317 			sc->buflen = buflen;
318 			continue;
319 		} else if (__predict_false(ret == EAGAIN)) {
320 			/* No more channel packets; done! */
321 			break;
322 		}
323 		KASSERT(!ret, ("vmbus_chan_recv_pkt failed: %d", ret));
324 
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",
330 			    pkt->cph_type);
331 			break;
332 		case VMBUS_CHANPKT_TYPE_INBAND:
333 			hv_kbd_on_received(sc, pkt);
334 			break;
335 		default:
336 			device_printf(sc->dev, "unknown event: %d\n",
337 			    pkt->cph_type);
338 			break;
339 		}
340 	}
341 }
342 
343 static int
344 hv_kbd_connect_vsp(hv_kbd_sc *sc)
345 {
346 	int ret;
347 	size_t resplen;
348 	struct vmbus_xact *xact;
349 	hv_kbd_proto_req *req;
350 	const hv_kbd_proto_resp *resp;
351 
352 	xact = vmbus_xact_get(sc->hs_xact_ctx, sizeof(*req));
353 	if (xact == NULL) {
354 		device_printf(sc->dev, "no xact for kbd init");
355 		return (ENODEV);
356 	}
357 	req = vmbus_xact_req_data(xact);
358 	req->hdr.type = HV_KBD_PROTO_REQUEST;
359 	req->ver = HV_KBD_VER;
360 
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);
367 	if (ret) {
368 		device_printf(sc->dev, "fail to send\n");
369 		vmbus_xact_deactivate(xact);
370 		return (ret);
371 	}
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");
375 		ret = ENODEV;
376 		goto clean;
377 	}
378 
379 	if (!(resp->status & HV_KBD_PROTO_ACCEPTED)) {
380 		device_printf(sc->dev, "hv_kbd protocol request failed\n");
381 		ret = ENODEV;
382 	}
383 clean:
384 	vmbus_xact_put(xact);
385 	DEBUG_HVSC(sc, "finish connect vsp\n");
386 	return (ret);
387 }
388 
389 static int
390 hv_kbd_attach1(device_t dev, vmbus_chan_callback_t cb)
391 {
392 	int ret;
393 	hv_kbd_sc *sc;
394 
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(
400 		sc->hs_chan,
401 		HV_KBD_RINGBUFF_SEND_SZ,
402 		HV_KBD_RINGBUFF_RECV_SZ,
403 		NULL, 0,
404 		cb,
405 		sc);
406 	if (ret != 0) {
407 		free(sc->buf, M_DEVBUF);
408 	}
409 	return (ret);
410 }
411 
412 static int
413 hv_kbd_detach1(device_t dev)
414 {
415 	hv_kbd_sc *sc = device_get_softc(dev);
416 	vmbus_chan_close(vmbus_get_channel(dev));
417 	free(sc->buf, M_DEVBUF);
418 	return (0);
419 }
420 
421 static void
422 hv_kbd_init(hv_kbd_sc *sc)
423 {
424 	const int max_list = 16;
425 	int i;
426 	keystroke_info *ksi;
427 
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);
435 	}
436 }
437 
438 static void
439 hv_kbd_fini(hv_kbd_sc *sc)
440 {
441 	keystroke_info *ksi;
442 	while (!LIST_EMPTY(&sc->ks_free_list)) {
443 		ksi = LIST_FIRST(&sc->ks_free_list);
444 		LIST_REMOVE(ksi, link);
445 		free(ksi, M_DEVBUF);
446 	}
447 	while (!STAILQ_EMPTY(&sc->ks_queue)) {
448 		ksi = STAILQ_FIRST(&sc->ks_queue);
449 		STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
450 		free(ksi, M_DEVBUF);
451 	}
452 	mtx_destroy(&sc->ks_mtx);
453 }
454 
455 static void
456 hv_kbd_sysctl(device_t dev)
457 {
458 	struct sysctl_oid_list *child;
459 	struct sysctl_ctx_list *ctx;
460 	hv_kbd_sc *sc;
461 
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");
467 }
468 
469 static int
470 hv_kbd_attach(device_t dev)
471 {
472 	int error = 0;
473 	hv_kbd_sc *sc;
474 
475 	sc = device_get_softc(dev);
476 	sc->hs_chan = vmbus_get_channel(dev);
477 	sc->dev = dev;
478 	hv_kbd_init(sc);
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) {
482 		error = ENOMEM;
483 		goto failed;
484 	}
485 
486 	error = hv_kbd_attach1(dev, hv_kbd_read_channel);
487 	if (error)
488 		goto failed;
489 	error = hv_kbd_connect_vsp(sc);
490 	if (error)
491 		goto failed;
492 
493 	error = hv_kbd_drv_attach(dev);
494 	if (error)
495 		goto failed;
496 	hv_kbd_sysctl(dev);
497 	return (0);
498 failed:
499 	hv_kbd_detach(dev);
500 	return (error);
501 }
502 
503 static int
504 hv_kbd_detach(device_t dev)
505 {
506 	int ret;
507 	hv_kbd_sc *sc = device_get_softc(dev);
508 	hv_kbd_fini(sc);
509 	if (sc->hs_xact_ctx != NULL)
510 		vmbus_xact_ctx_destroy(sc->hs_xact_ctx);
511 	ret = hv_kbd_detach1(dev);
512 	if (!ret)
513 		device_printf(dev, "Fail to detach\n");
514 	return hv_kbd_drv_detach(dev);
515 }
516 
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),
522 	{ 0, 0 }
523 };
524 
525 static driver_t kbd_driver = {HVKBD_DRIVER_NAME , kbd_methods, sizeof(hv_kbd_sc)};
526 
527 static devclass_t kbd_devclass;
528 
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);
532