aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/hyperv/input/hv_kbdc.c
blob: 7065ff3057a7780e9b494f3339e1df3941438c69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*-
 * Copyright (c) 2017 Microsoft Corp.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/bus.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/taskqueue.h>
#include <sys/selinfo.h>
#include <sys/sysctl.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/syscallsubr.h>
#include <sys/sysproto.h>
#include <sys/systm.h>
#include <sys/mutex.h>

#include <sys/kbio.h>
#include <dev/kbd/kbdreg.h>

#include <dev/hyperv/include/hyperv.h>
#include <dev/hyperv/utilities/hv_utilreg.h>
#include <dev/hyperv/utilities/vmbus_icreg.h>
#include <dev/hyperv/utilities/vmbus_icvar.h>
#include <dev/hyperv/include/vmbus_xact.h>

#include "dev/hyperv/input/hv_kbdc.h"
#include "vmbus_if.h"

#define HV_KBD_VER_MAJOR	(1)
#define HV_KBD_VER_MINOR	(0)

#define HV_KBD_VER		(HV_KBD_VER_MINOR | (HV_KBD_VER_MAJOR) << 16)

#define HV_KBD_PROTO_ACCEPTED	(1)

#define HV_BUFF_SIZE		(4*PAGE_SIZE)
#define HV_KBD_RINGBUFF_SEND_SZ	(10*PAGE_SIZE)
#define HV_KBD_RINGBUFF_RECV_SZ (10*PAGE_SIZE)

enum hv_kbd_msg_type_t {
	HV_KBD_PROTO_REQUEST        = 1,
	HV_KBD_PROTO_RESPONSE       = 2,
	HV_KBD_PROTO_EVENT          = 3,
	HV_KBD_PROTO_LED_INDICATORS = 4,
};

typedef struct hv_kbd_msg_hdr_t {
	uint32_t type;
} hv_kbd_msg_hdr;

typedef struct hv_kbd_msg_t {
	hv_kbd_msg_hdr hdr;
	char data[];
} hv_kbd_msg;

typedef struct hv_kbd_proto_req_t {
	hv_kbd_msg_hdr	hdr;
	uint32_t	ver;
} hv_kbd_proto_req;

typedef struct hv_kbd_proto_resp_t {
	hv_kbd_msg_hdr  hdr;
	uint32_t	status;
} hv_kbd_proto_resp;

#define HV_KBD_PROTO_REQ_SZ	(sizeof(hv_kbd_proto_req))
#define HV_KBD_PROTO_RESP_SZ	(sizeof(hv_kbd_proto_resp))

/**
 * the struct in win host:
 * typedef struct _HK_MESSAGE_KEYSTROKE
 * {
 *     HK_MESSAGE_HEADER Header;
 *     UINT16 MakeCode;
 *     UINT32 IsUnicode:1;
 *     UINT32 IsBreak:1;
 *     UINT32 IsE0:1;
 *     UINT32 IsE1:1;
 *     UINT32 Reserved:28;
 * } HK_MESSAGE_KEYSTROKE
 */
typedef struct hv_kbd_keystroke_t {
	hv_kbd_msg_hdr  hdr;
	keystroke	ks;
} hv_kbd_keystroke;

static const struct vmbus_ic_desc vmbus_kbd_descs[] = {
	{
		.ic_guid = { .hv_guid = {
		    0x6d, 0xad, 0x12, 0xf9, 0x17, 0x2b, 0xea, 0x48,
		    0xbd, 0x65, 0xf9, 0x27, 0xa6, 0x1c, 0x76,  0x84} },
		.ic_desc = "Hyper-V KBD"
	},
	VMBUS_IC_DESC_END
};

static int hv_kbd_attach(device_t dev);
static int hv_kbd_detach(device_t dev);

/**
 * return 1 if producer is ready
 */
int
hv_kbd_prod_is_ready(hv_kbd_sc *sc)
{
	int ret;
	mtx_lock(&sc->ks_mtx);
	ret = !STAILQ_EMPTY(&sc->ks_queue);
	mtx_unlock(&sc->ks_mtx);
	return (ret);
}

int
hv_kbd_produce_ks(hv_kbd_sc *sc, const keystroke *ks)
{
	int ret = 0;
	keystroke_info *ksi;
	mtx_lock(&sc->ks_mtx);
	if (LIST_EMPTY(&sc->ks_free_list)) {
		DEBUG_HVSC(sc, "NO buffer!\n");
		ret = 1;
	} else {
		ksi = LIST_FIRST(&sc->ks_free_list);
		LIST_REMOVE(ksi, link);
		ksi->ks = *ks;
		STAILQ_INSERT_TAIL(&sc->ks_queue, ksi, slink);
	}
	mtx_unlock(&sc->ks_mtx);
	return (ret);
}

/**
 * return 0 if successfully get the 1st item of queue without removing it
 */
int
hv_kbd_fetch_top(hv_kbd_sc *sc, keystroke *result)
{
	int ret = 0;
	keystroke_info *ksi = NULL;
	mtx_lock(&sc->ks_mtx);
	if (STAILQ_EMPTY(&sc->ks_queue)) {
		DEBUG_HVSC(sc, "Empty queue!\n");
		ret = 1;
	} else {
		ksi = STAILQ_FIRST(&sc->ks_queue);
		*result = ksi->ks;
	}
	mtx_unlock(&sc->ks_mtx);
	return (ret);
}

/**
 * return 0 if successfully removing the top item
 */
int
hv_kbd_remove_top(hv_kbd_sc *sc)
{
	int ret = 0;
	keystroke_info *ksi = NULL;
	mtx_lock(&sc->ks_mtx);
	if (STAILQ_EMPTY(&sc->ks_queue)) {
		DEBUG_HVSC(sc, "Empty queue!\n");
		ret = 1;
	} else {
		ksi = STAILQ_FIRST(&sc->ks_queue);
		STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
		LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link);
	}
	mtx_unlock(&sc->ks_mtx);
	return (ret);
}

/**
 * return 0 if successfully modify the 1st item of queue
 */
int
hv_kbd_modify_top(hv_kbd_sc *sc, keystroke *top)
{
	int ret = 0;
	keystroke_info *ksi = NULL;
	mtx_lock(&sc->ks_mtx);
	if (STAILQ_EMPTY(&sc->ks_queue)) {
		DEBUG_HVSC(sc, "Empty queue!\n");
		ret = 1;
	} else {
		ksi = STAILQ_FIRST(&sc->ks_queue);
		ksi->ks = *top;
	}
	mtx_unlock(&sc->ks_mtx);
	return (ret);
}

static int
hv_kbd_probe(device_t dev)
{
	device_t bus = device_get_parent(dev);
	const struct vmbus_ic_desc *d;

	if (resource_disabled(device_get_name(dev), 0))
		return (ENXIO);

	for (d = vmbus_kbd_descs; d->ic_desc != NULL; ++d) {
		if (VMBUS_PROBE_GUID(bus, dev, &d->ic_guid) == 0) {
			device_set_desc(dev, d->ic_desc);
			return (BUS_PROBE_DEFAULT);
		}
	}
	return (ENXIO);
}

static void
hv_kbd_on_response(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
{
	struct vmbus_xact_ctx *xact = sc->hs_xact_ctx;
	if (xact != NULL) {
		DEBUG_HVSC(sc, "hvkbd is ready\n");
		vmbus_xact_ctx_wakeup(xact, VMBUS_CHANPKT_CONST_DATA(pkt),
		    VMBUS_CHANPKT_DATALEN(pkt));
	}
}

static void
hv_kbd_on_received(hv_kbd_sc *sc, struct vmbus_chanpkt_hdr *pkt)
{

	const hv_kbd_msg *msg = VMBUS_CHANPKT_CONST_DATA(pkt);
	const hv_kbd_proto_resp *resp =
	    VMBUS_CHANPKT_CONST_DATA(pkt);
	const hv_kbd_keystroke *keystroke =
	    VMBUS_CHANPKT_CONST_DATA(pkt);
	uint32_t msg_len = VMBUS_CHANPKT_DATALEN(pkt);
	enum hv_kbd_msg_type_t msg_type;
	uint32_t info;
	uint16_t scan_code;

	if (msg_len <= sizeof(hv_kbd_msg)) {
		device_printf(sc->dev, "Illegal packet\n");
		return;
	}
	msg_type = msg->hdr.type;
	switch (msg_type) {
		case HV_KBD_PROTO_RESPONSE:
			hv_kbd_on_response(sc, pkt);
			DEBUG_HVSC(sc, "keyboard resp: 0x%x\n",
			    resp->status);
			break;
		case HV_KBD_PROTO_EVENT:
			info = keystroke->ks.info;
			scan_code = keystroke->ks.makecode;
			DEBUG_HVSC(sc, "keystroke info: 0x%x, scan: 0x%x\n",
			    info, scan_code);
			hv_kbd_produce_ks(sc, &keystroke->ks);
			hv_kbd_intr(sc);
		default:
			break;
	}
}

void 
hv_kbd_read_channel(struct vmbus_channel *channel, void *context)
{
	uint8_t *buf;
	uint32_t buflen = 0;
	int ret = 0;

	hv_kbd_sc *sc = (hv_kbd_sc*)context;
	buf = sc->buf;
	buflen = sc->buflen;
	for (;;) {
		struct vmbus_chanpkt_hdr *pkt = (struct vmbus_chanpkt_hdr *)buf;
		uint32_t rxed = buflen;

		ret = vmbus_chan_recv_pkt(channel, pkt, &rxed);
		if (__predict_false(ret == ENOBUFS)) {
			buflen = sc->buflen * 2;
			while (buflen < rxed)
				buflen *= 2;
			buf = malloc(buflen, M_DEVBUF, M_WAITOK | M_ZERO);
			device_printf(sc->dev, "expand recvbuf %d -> %d\n",
			    sc->buflen, buflen);
			free(sc->buf, M_DEVBUF);
			sc->buf = buf;
			sc->buflen = buflen;
			continue;
		} else if (__predict_false(ret == EAGAIN)) {
			/* No more channel packets; done! */
			break;
		}
		KASSERT(!ret, ("vmbus_chan_recv_pkt failed: %d", ret));

		DEBUG_HVSC(sc, "event: 0x%x\n", pkt->cph_type);
		switch (pkt->cph_type) {
		case VMBUS_CHANPKT_TYPE_COMP:
		case VMBUS_CHANPKT_TYPE_RXBUF:
			device_printf(sc->dev, "unhandled event: %d\n",
			    pkt->cph_type);
			break;
		case VMBUS_CHANPKT_TYPE_INBAND:
			hv_kbd_on_received(sc, pkt);
			break;
		default:
			device_printf(sc->dev, "unknown event: %d\n",
			    pkt->cph_type);
			break;
		}
	}
}

static int
hv_kbd_connect_vsp(hv_kbd_sc *sc)
{
	int ret;
	size_t resplen;
	struct vmbus_xact *xact;
	hv_kbd_proto_req *req;
	const hv_kbd_proto_resp *resp;

	xact = vmbus_xact_get(sc->hs_xact_ctx, sizeof(*req));
	if (xact == NULL) {
		device_printf(sc->dev, "no xact for kbd init");
		return (ENODEV);
	}
	req = vmbus_xact_req_data(xact);
	req->hdr.type = HV_KBD_PROTO_REQUEST;
	req->ver = HV_KBD_VER;

	vmbus_xact_activate(xact);
	ret = vmbus_chan_send(sc->hs_chan,
		VMBUS_CHANPKT_TYPE_INBAND,
		VMBUS_CHANPKT_FLAG_RC,
		req, sizeof(hv_kbd_proto_req),
		(uint64_t)(uintptr_t)xact);
	if (ret) {
		device_printf(sc->dev, "fail to send\n");
		vmbus_xact_deactivate(xact);
		return (ret);
	}
	resp = vmbus_chan_xact_wait(sc->hs_chan, xact, &resplen, true);
	if (resplen < HV_KBD_PROTO_RESP_SZ) {
		device_printf(sc->dev, "hv_kbd init communicate failed\n");
		ret = ENODEV;
		goto clean;
	}

	if (!(resp->status & HV_KBD_PROTO_ACCEPTED)) {
		device_printf(sc->dev, "hv_kbd protocol request failed\n");
		ret = ENODEV;
	}
clean:
	vmbus_xact_put(xact);
	DEBUG_HVSC(sc, "finish connect vsp\n");
	return (ret);
}

static int
hv_kbd_attach1(device_t dev, vmbus_chan_callback_t cb)
{
	int ret;
	hv_kbd_sc *sc;

        sc = device_get_softc(dev);
	sc->buflen = HV_BUFF_SIZE;
	sc->buf = malloc(sc->buflen, M_DEVBUF, M_WAITOK | M_ZERO);
	vmbus_chan_set_readbatch(sc->hs_chan, false);
	ret = vmbus_chan_open(
		sc->hs_chan,
		HV_KBD_RINGBUFF_SEND_SZ,
		HV_KBD_RINGBUFF_RECV_SZ,
		NULL, 0,
		cb,
		sc);
	if (ret != 0) {
		free(sc->buf, M_DEVBUF);
	}
	return (ret);
}

static int
hv_kbd_detach1(device_t dev)
{
	hv_kbd_sc *sc = device_get_softc(dev);
	vmbus_chan_close(vmbus_get_channel(dev));
	free(sc->buf, M_DEVBUF);
	return (0);
}

static void
hv_kbd_init(hv_kbd_sc *sc)
{
	const int max_list = 16;
	int i;
	keystroke_info *ksi;

	mtx_init(&sc->ks_mtx, "hv_kbdc mutex", NULL, MTX_DEF);
	LIST_INIT(&sc->ks_free_list);
	STAILQ_INIT(&sc->ks_queue);
	for (i = 0; i < max_list; i++) {
		ksi = malloc(sizeof(keystroke_info),
		    M_DEVBUF, M_WAITOK|M_ZERO);
		LIST_INSERT_HEAD(&sc->ks_free_list, ksi, link);
	}
}

static void
hv_kbd_fini(hv_kbd_sc *sc)
{
	keystroke_info *ksi;
	while (!LIST_EMPTY(&sc->ks_free_list)) {
		ksi = LIST_FIRST(&sc->ks_free_list);
		LIST_REMOVE(ksi, link);
		free(ksi, M_DEVBUF);
	}
	while (!STAILQ_EMPTY(&sc->ks_queue)) {
		ksi = STAILQ_FIRST(&sc->ks_queue);
		STAILQ_REMOVE_HEAD(&sc->ks_queue, slink);
		free(ksi, M_DEVBUF);
	}
	mtx_destroy(&sc->ks_mtx);
}

static void
hv_kbd_sysctl(device_t dev)
{
	struct sysctl_oid_list *child;
	struct sysctl_ctx_list *ctx;
	hv_kbd_sc *sc;

	sc = device_get_softc(dev);
	ctx = device_get_sysctl_ctx(dev);
	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "debug", CTLFLAG_RW,
	    &sc->debug, 0, "debug hyperv keyboard");
}

static int
hv_kbd_attach(device_t dev)
{
	int error = 0;
	hv_kbd_sc *sc;

	sc = device_get_softc(dev);
	sc->hs_chan = vmbus_get_channel(dev);
	sc->dev = dev;
	hv_kbd_init(sc);
	sc->hs_xact_ctx = vmbus_xact_ctx_create(bus_get_dma_tag(dev),
	    HV_KBD_PROTO_REQ_SZ, HV_KBD_PROTO_RESP_SZ, 0);
	if (sc->hs_xact_ctx == NULL) {
		error = ENOMEM;
		goto failed;
	}

	error = hv_kbd_attach1(dev, hv_kbd_read_channel);
	if (error)
		goto failed;
	error = hv_kbd_connect_vsp(sc);
	if (error)
		goto failed;

	error = hv_kbd_drv_attach(dev);
	if (error)
		goto failed;
	hv_kbd_sysctl(dev);
	return (0);
failed:
	hv_kbd_detach(dev);
	return (error);
}

static int
hv_kbd_detach(device_t dev)
{
	int ret;
	hv_kbd_sc *sc = device_get_softc(dev);
	hv_kbd_fini(sc);
	if (sc->hs_xact_ctx != NULL)
		vmbus_xact_ctx_destroy(sc->hs_xact_ctx);
	ret = hv_kbd_detach1(dev);
	if (!ret)
		device_printf(dev, "Fail to detach\n");
	return hv_kbd_drv_detach(dev);
}

static device_method_t kbd_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe, hv_kbd_probe),
	DEVMETHOD(device_attach, hv_kbd_attach),
	DEVMETHOD(device_detach, hv_kbd_detach),
	{ 0, 0 }
};

static driver_t kbd_driver = {HVKBD_DRIVER_NAME , kbd_methods, sizeof(hv_kbd_sc)};

static devclass_t kbd_devclass;

DRIVER_MODULE(hv_kbd, vmbus, kbd_driver, kbd_devclass, hvkbd_driver_load, NULL);
MODULE_VERSION(hv_kbd, 1);
MODULE_DEPEND(hv_kbd, vmbus, 1, 1, 1);