xref: /freebsd/usr.sbin/bhyve/pci_virtio_input.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * virtio input device emulation.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #ifndef WITHOUT_CAPSICUM
39 #include <sys/capsicum.h>
40 
41 #include <capsicum_helpers.h>
42 #endif
43 #include <sys/ioctl.h>
44 #include <sys/linker_set.h>
45 #include <sys/uio.h>
46 
47 #include <dev/evdev/input.h>
48 
49 #include <assert.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <pthread.h>
54 #include <stddef.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sysexits.h>
59 #include <unistd.h>
60 
61 #include "bhyverun.h"
62 #include "config.h"
63 #include "debug.h"
64 #include "mevent.h"
65 #include "pci_emul.h"
66 #include "virtio.h"
67 
68 #define VTINPUT_RINGSZ 64
69 
70 #define VTINPUT_MAX_PKT_LEN 10
71 
72 /*
73  * Queue definitions.
74  */
75 #define VTINPUT_EVENTQ 0
76 #define VTINPUT_STATUSQ 1
77 
78 #define VTINPUT_MAXQ 2
79 
80 static int pci_vtinput_debug;
81 #define DPRINTF(params)        \
82 	if (pci_vtinput_debug) \
83 	PRINTLN params
84 #define WPRINTF(params) PRINTLN params
85 
86 enum vtinput_config_select {
87 	VTINPUT_CFG_UNSET = 0x00,
88 	VTINPUT_CFG_ID_NAME = 0x01,
89 	VTINPUT_CFG_ID_SERIAL = 0x02,
90 	VTINPUT_CFG_ID_DEVIDS = 0x03,
91 	VTINPUT_CFG_PROP_BITS = 0x10,
92 	VTINPUT_CFG_EV_BITS = 0x11,
93 	VTINPUT_CFG_ABS_INFO = 0x12
94 };
95 
96 struct vtinput_absinfo {
97 	uint32_t min;
98 	uint32_t max;
99 	uint32_t fuzz;
100 	uint32_t flat;
101 	uint32_t res;
102 } __packed;
103 
104 struct vtinput_devids {
105 	uint16_t bustype;
106 	uint16_t vendor;
107 	uint16_t product;
108 	uint16_t version;
109 } __packed;
110 
111 struct vtinput_config {
112 	uint8_t select;
113 	uint8_t subsel;
114 	uint8_t size;
115 	uint8_t reserved[5];
116 	union {
117 		char string[128];
118 		uint8_t bitmap[128];
119 		struct vtinput_absinfo abs;
120 		struct vtinput_devids ids;
121 	} u;
122 } __packed;
123 
124 struct vtinput_event {
125 	uint16_t type;
126 	uint16_t code;
127 	uint32_t value;
128 } __packed;
129 
130 struct vtinput_event_elem {
131 	struct vtinput_event event;
132 	struct iovec iov;
133 	uint16_t idx;
134 };
135 
136 struct vtinput_eventqueue {
137 	struct vtinput_event_elem *events;
138 	uint32_t size;
139 	uint32_t idx;
140 };
141 
142 /*
143  * Per-device softc
144  */
145 struct pci_vtinput_softc {
146 	struct virtio_softc vsc_vs;
147 	struct vqueue_info vsc_queues[VTINPUT_MAXQ];
148 	pthread_mutex_t vsc_mtx;
149 	const char *vsc_evdev;
150 	int vsc_fd;
151 	struct vtinput_config vsc_config;
152 	int vsc_config_valid;
153 	struct mevent *vsc_evp;
154 	struct vtinput_eventqueue vsc_eventqueue;
155 };
156 
157 static void pci_vtinput_reset(void *);
158 static int pci_vtinput_cfgread(void *, int, int, uint32_t *);
159 static int pci_vtinput_cfgwrite(void *, int, int, uint32_t);
160 
161 static struct virtio_consts vtinput_vi_consts = {
162 	"vtinput",		       /* our name */
163 	VTINPUT_MAXQ,		       /* we support 1 virtqueue */
164 	sizeof(struct vtinput_config), /* config reg size */
165 	pci_vtinput_reset,	       /* reset */
166 	NULL,			       /* device-wide qnotify -- not used */
167 	pci_vtinput_cfgread,	       /* read virtio config */
168 	pci_vtinput_cfgwrite,	       /* write virtio config */
169 	NULL,			       /* apply negotiated features */
170 	0,			       /* our capabilities */
171 };
172 
173 static void
174 pci_vtinput_reset(void *vsc)
175 {
176 	struct pci_vtinput_softc *sc = vsc;
177 
178 	DPRINTF(("%s: device reset requested", __func__));
179 	vi_reset_dev(&sc->vsc_vs);
180 }
181 
182 static void
183 pci_vtinput_notify_eventq(void *vsc, struct vqueue_info *vq)
184 {
185 	DPRINTF(("%s", __func__));
186 }
187 
188 static void
189 pci_vtinput_notify_statusq(void *vsc, struct vqueue_info *vq)
190 {
191 	struct pci_vtinput_softc *sc = vsc;
192 
193 	while (vq_has_descs(vq)) {
194 		/* get descriptor chain */
195 		struct iovec iov;
196 		struct vi_req req;
197 		const int n = vq_getchain(vq, &iov, 1, &req);
198 		if (n <= 0) {
199 			WPRINTF(("%s: invalid descriptor: %d", __func__, n));
200 			return;
201 		}
202 
203 		/* get event */
204 		struct vtinput_event event;
205 		memcpy(&event, iov.iov_base, sizeof(event));
206 
207 		/*
208 		 * on multi touch devices:
209 		 * - host send EV_MSC to guest
210 		 * - guest sends EV_MSC back to host
211 		 * - host writes EV_MSC to evdev
212 		 * - evdev saves EV_MSC in it's event buffer
213 		 * - host receives an extra EV_MSC by reading the evdev event
214 		 *   buffer
215 		 * - frames become larger and larger
216 		 * avoid endless loops by ignoring EV_MSC
217 		 */
218 		if (event.type == EV_MSC) {
219 			vq_relchain(vq, req.idx, sizeof(event));
220 			continue;
221 		}
222 
223 		/* send event to evdev */
224 		struct input_event host_event;
225 		host_event.type = event.type;
226 		host_event.code = event.code;
227 		host_event.value = event.value;
228 		if (gettimeofday(&host_event.time, NULL) != 0) {
229 			WPRINTF(("%s: failed gettimeofday", __func__));
230 		}
231 		if (write(sc->vsc_fd, &host_event, sizeof(host_event)) == -1) {
232 			WPRINTF(("%s: failed to write host_event", __func__));
233 		}
234 
235 		vq_relchain(vq, req.idx, sizeof(event));
236 	}
237 	vq_endchains(vq, 1);
238 }
239 
240 static int
241 pci_vtinput_get_bitmap(struct pci_vtinput_softc *sc, int cmd, int count)
242 {
243 	if (count <= 0 || !sc) {
244 		return (-1);
245 	}
246 
247 	/* query bitmap */
248 	memset(sc->vsc_config.u.bitmap, 0, sizeof(sc->vsc_config.u.bitmap));
249 	if (ioctl(sc->vsc_fd, cmd, sc->vsc_config.u.bitmap) < 0) {
250 		return (-1);
251 	}
252 
253 	/* get number of set bytes in bitmap */
254 	for (int i = count - 1; i >= 0; i--) {
255 		if (sc->vsc_config.u.bitmap[i]) {
256 			return i + 1;
257 		}
258 	}
259 
260 	return (-1);
261 }
262 
263 static int
264 pci_vtinput_read_config_id_name(struct pci_vtinput_softc *sc)
265 {
266 	char name[128];
267 	if (ioctl(sc->vsc_fd, EVIOCGNAME(sizeof(name) - 1), name) < 0) {
268 		return (1);
269 	}
270 
271 	memcpy(sc->vsc_config.u.string, name, sizeof(name));
272 	sc->vsc_config.size = strnlen(name, sizeof(name));
273 
274 	return (0);
275 }
276 
277 static int
278 pci_vtinput_read_config_id_serial(struct pci_vtinput_softc *sc)
279 {
280 	/* serial isn't supported */
281 	sc->vsc_config.size = 0;
282 
283 	return (0);
284 }
285 
286 static int
287 pci_vtinput_read_config_id_devids(struct pci_vtinput_softc *sc)
288 {
289 	struct input_id devids;
290 	if (ioctl(sc->vsc_fd, EVIOCGID, &devids)) {
291 		return (1);
292 	}
293 
294 	sc->vsc_config.u.ids.bustype = devids.bustype;
295 	sc->vsc_config.u.ids.vendor = devids.vendor;
296 	sc->vsc_config.u.ids.product = devids.product;
297 	sc->vsc_config.u.ids.version = devids.version;
298 	sc->vsc_config.size = sizeof(struct vtinput_devids);
299 
300 	return (0);
301 }
302 
303 static int
304 pci_vtinput_read_config_prop_bits(struct pci_vtinput_softc *sc)
305 {
306 	/*
307 	 * Evdev bitmap countains 1 bit per count. Additionally evdev bitmaps
308 	 * are arrays of longs instead of chars. Calculate how many longs are
309 	 * required for evdev bitmap. Multiply that with sizeof(long) to get the
310 	 * number of elements.
311 	 */
312 	const int count = howmany(INPUT_PROP_CNT, sizeof(long) * 8) *
313 	    sizeof(long);
314 	const unsigned int cmd = EVIOCGPROP(count);
315 	const int size = pci_vtinput_get_bitmap(sc, cmd, count);
316 	if (size <= 0) {
317 		return (1);
318 	}
319 
320 	sc->vsc_config.size = size;
321 
322 	return (0);
323 }
324 
325 static int
326 pci_vtinput_read_config_ev_bits(struct pci_vtinput_softc *sc, uint8_t type)
327 {
328 	int count;
329 
330 	switch (type) {
331 	case EV_KEY:
332 		count = KEY_CNT;
333 		break;
334 	case EV_REL:
335 		count = REL_CNT;
336 		break;
337 	case EV_ABS:
338 		count = ABS_CNT;
339 		break;
340 	case EV_MSC:
341 		count = MSC_CNT;
342 		break;
343 	case EV_SW:
344 		count = SW_CNT;
345 		break;
346 	case EV_LED:
347 		count = LED_CNT;
348 		break;
349 	default:
350 		return (1);
351 	}
352 
353 	/*
354 	 * Evdev bitmap countains 1 bit per count. Additionally evdev bitmaps
355 	 * are arrays of longs instead of chars. Calculate how many longs are
356 	 * required for evdev bitmap. Multiply that with sizeof(long) to get the
357 	 * number of elements.
358 	 */
359 	count = howmany(count, sizeof(long) * 8) * sizeof(long);
360 	const unsigned int cmd = EVIOCGBIT(sc->vsc_config.subsel, count);
361 	const int size = pci_vtinput_get_bitmap(sc, cmd, count);
362 	if (size <= 0) {
363 		return (1);
364 	}
365 
366 	sc->vsc_config.size = size;
367 
368 	return (0);
369 }
370 
371 static int
372 pci_vtinput_read_config_abs_info(struct pci_vtinput_softc *sc)
373 {
374 	/* check if evdev has EV_ABS */
375 	if (!pci_vtinput_read_config_ev_bits(sc, EV_ABS)) {
376 		return (1);
377 	}
378 
379 	/* get abs information */
380 	struct input_absinfo abs;
381 	if (ioctl(sc->vsc_fd, EVIOCGABS(sc->vsc_config.subsel), &abs) < 0) {
382 		return (1);
383 	}
384 
385 	/* save abs information */
386 	sc->vsc_config.u.abs.min = abs.minimum;
387 	sc->vsc_config.u.abs.max = abs.maximum;
388 	sc->vsc_config.u.abs.fuzz = abs.fuzz;
389 	sc->vsc_config.u.abs.flat = abs.flat;
390 	sc->vsc_config.u.abs.res = abs.resolution;
391 	sc->vsc_config.size = sizeof(struct vtinput_absinfo);
392 
393 	return (0);
394 }
395 
396 static int
397 pci_vtinput_read_config(struct pci_vtinput_softc *sc)
398 {
399 	switch (sc->vsc_config.select) {
400 	case VTINPUT_CFG_UNSET:
401 		return (0);
402 	case VTINPUT_CFG_ID_NAME:
403 		return pci_vtinput_read_config_id_name(sc);
404 	case VTINPUT_CFG_ID_SERIAL:
405 		return pci_vtinput_read_config_id_serial(sc);
406 	case VTINPUT_CFG_ID_DEVIDS:
407 		return pci_vtinput_read_config_id_devids(sc);
408 	case VTINPUT_CFG_PROP_BITS:
409 		return pci_vtinput_read_config_prop_bits(sc);
410 	case VTINPUT_CFG_EV_BITS:
411 		return pci_vtinput_read_config_ev_bits(
412 		    sc, sc->vsc_config.subsel);
413 	case VTINPUT_CFG_ABS_INFO:
414 		return pci_vtinput_read_config_abs_info(sc);
415 	default:
416 		return (1);
417 	}
418 }
419 
420 static int
421 pci_vtinput_cfgread(void *vsc, int offset, int size, uint32_t *retval)
422 {
423 	struct pci_vtinput_softc *sc = vsc;
424 
425 	/* check for valid offset and size */
426 	if (offset + size > sizeof(struct vtinput_config)) {
427 		WPRINTF(("%s: read to invalid offset/size %d/%d", __func__,
428 		    offset, size));
429 		memset(retval, 0, size);
430 		return (0);
431 	}
432 
433 	/* read new config values, if select and subsel changed. */
434 	if (!sc->vsc_config_valid) {
435 		if (pci_vtinput_read_config(sc) != 0) {
436 			DPRINTF(("%s: could not read config %d/%d", __func__,
437 			    sc->vsc_config.select, sc->vsc_config.subsel));
438 			memset(retval, 0, size);
439 			return (0);
440 		}
441 		sc->vsc_config_valid = 1;
442 	}
443 
444 	uint8_t *ptr = (uint8_t *)&sc->vsc_config;
445 	memcpy(retval, ptr + offset, size);
446 
447 	return (0);
448 }
449 
450 static int
451 pci_vtinput_cfgwrite(void *vsc, int offset, int size, uint32_t value)
452 {
453 	struct pci_vtinput_softc *sc = vsc;
454 
455 	/* guest can only write to select and subsel fields */
456 	if (offset + size > 2) {
457 		WPRINTF(("%s: write to readonly reg %d", __func__, offset));
458 		return (1);
459 	}
460 
461 	/* copy value into config */
462 	uint8_t *ptr = (uint8_t *)&sc->vsc_config;
463 	memcpy(ptr + offset, &value, size);
464 
465 	/* select/subsel changed, query new config on next cfgread */
466 	sc->vsc_config_valid = 0;
467 
468 	return (0);
469 }
470 
471 static int
472 vtinput_eventqueue_add_event(
473     struct vtinput_eventqueue *queue, struct input_event *e)
474 {
475 	/* check if queue is full */
476 	if (queue->idx >= queue->size) {
477 		/* alloc new elements for queue */
478 		const uint32_t newSize = queue->idx;
479 		const void *newPtr = realloc(queue->events,
480 		    queue->size * sizeof(struct vtinput_event_elem));
481 		if (newPtr == NULL) {
482 			WPRINTF(("%s: realloc memory for eventqueue failed!",
483 			    __func__));
484 			return (1);
485 		}
486 		/* save new size and eventqueue */
487 		queue->events = (struct vtinput_event_elem *)newPtr;
488 		queue->size = newSize;
489 	}
490 
491 	/* save event */
492 	struct vtinput_event *event = &queue->events[queue->idx].event;
493 	event->type = e->type;
494 	event->code = e->code;
495 	event->value = e->value;
496 	queue->idx++;
497 
498 	return (0);
499 }
500 
501 static void
502 vtinput_eventqueue_clear(struct vtinput_eventqueue *queue)
503 {
504 	/* just reset index to clear queue */
505 	queue->idx = 0;
506 }
507 
508 static void
509 vtinput_eventqueue_send_events(
510     struct vtinput_eventqueue *queue, struct vqueue_info *vq)
511 {
512 	/*
513 	 * First iteration through eventqueue:
514 	 *   Get descriptor chains.
515 	 */
516 	for (uint32_t i = 0; i < queue->idx; ++i) {
517 		/* get descriptor */
518 		if (!vq_has_descs(vq)) {
519 			/*
520 			 * We don't have enough descriptors for all events.
521 			 * Return chains back to guest.
522 			 */
523 			vq_retchains(vq, i);
524 			WPRINTF((
525 			    "%s: not enough available descriptors, dropping %d events",
526 			    __func__, queue->idx));
527 			goto done;
528 		}
529 
530 		/* get descriptor chain */
531 		struct iovec iov;
532 		struct vi_req req;
533 		const int n = vq_getchain(vq, &iov, 1, &req);
534 		if (n <= 0) {
535 			WPRINTF(("%s: invalid descriptor: %d", __func__, n));
536 			return;
537 		}
538 		if (n != 1) {
539 			WPRINTF(
540 			    ("%s: invalid number of descriptors in chain: %d",
541 				__func__, n));
542 			/* release invalid chain */
543 			vq_relchain(vq, req.idx, 0);
544 			return;
545 		}
546 		if (iov.iov_len < sizeof(struct vtinput_event)) {
547 			WPRINTF(("%s: invalid descriptor length: %lu", __func__,
548 			    iov.iov_len));
549 			/* release invalid chain */
550 			vq_relchain(vq, req.idx, 0);
551 			return;
552 		}
553 
554 		/* save descriptor */
555 		queue->events[i].iov = iov;
556 		queue->events[i].idx = req.idx;
557 	}
558 
559 	/*
560 	 * Second iteration through eventqueue:
561 	 *   Send events to guest by releasing chains
562 	 */
563 	for (uint32_t i = 0; i < queue->idx; ++i) {
564 		struct vtinput_event_elem event = queue->events[i];
565 		memcpy(event.iov.iov_base, &event.event,
566 		    sizeof(struct vtinput_event));
567 		vq_relchain(vq, event.idx, sizeof(struct vtinput_event));
568 	}
569 done:
570 	/* clear queue and send interrupt to guest */
571 	vtinput_eventqueue_clear(queue);
572 	vq_endchains(vq, 1);
573 
574 	return;
575 }
576 
577 static int
578 vtinput_read_event_from_host(int fd, struct input_event *event)
579 {
580 	const int len = read(fd, event, sizeof(struct input_event));
581 	if (len != sizeof(struct input_event)) {
582 		if (len == -1 && errno != EAGAIN) {
583 			WPRINTF(("%s: event read failed! len = %d, errno = %d",
584 			    __func__, len, errno));
585 		}
586 
587 		/* host doesn't have more events for us */
588 		return (1);
589 	}
590 
591 	return (0);
592 }
593 
594 static void
595 vtinput_read_event(int fd __attribute((unused)),
596     enum ev_type t __attribute__((unused)), void *arg __attribute__((unused)))
597 {
598 	struct pci_vtinput_softc *sc = arg;
599 
600 	/* skip if driver isn't ready */
601 	if (!(sc->vsc_vs.vs_status & VIRTIO_CONFIG_STATUS_DRIVER_OK))
602 		return;
603 
604 	/* read all events from host */
605 	struct input_event event;
606 	while (vtinput_read_event_from_host(sc->vsc_fd, &event) == 0) {
607 		/* add events to our queue */
608 		vtinput_eventqueue_add_event(&sc->vsc_eventqueue, &event);
609 
610 		/* only send events to guest on EV_SYN or SYN_REPORT */
611 		if (event.type != EV_SYN || event.type != SYN_REPORT) {
612 			continue;
613 		}
614 
615 		/* send host events to guest */
616 		vtinput_eventqueue_send_events(
617 		    &sc->vsc_eventqueue, &sc->vsc_queues[VTINPUT_EVENTQ]);
618 	}
619 }
620 
621 static int
622 pci_vtinput_legacy_config(nvlist_t *nvl, const char *opts)
623 {
624 	if (opts == NULL)
625 		return (-1);
626 
627 	/*
628 	 * parse opts:
629 	 *   virtio-input,/dev/input/eventX
630 	 */
631 	char *cp = strchr(opts, ',');
632 	if (cp == NULL) {
633 		set_config_value_node(nvl, "path", opts);
634 		return (0);
635 	}
636 	char *path = strndup(opts, cp - opts);
637 	set_config_value_node(nvl, "path", path);
638 	free(path);
639 
640 	return (pci_parse_legacy_config(nvl, cp + 1));
641 }
642 
643 static int
644 pci_vtinput_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
645 {
646 	struct pci_vtinput_softc *sc;
647 
648 	/*
649 	 * Keep it here.
650 	 * Else it's possible to access it uninitialized by jumping to failed.
651 	 */
652 	pthread_mutexattr_t mtx_attr = NULL;
653 
654 	sc = calloc(1, sizeof(struct pci_vtinput_softc));
655 
656 	sc->vsc_evdev = get_config_value_node(nvl, "path");
657 	if (sc->vsc_evdev == NULL) {
658 		WPRINTF(("%s: missing required path config value", __func__));
659 		goto failed;
660 	}
661 
662 	/*
663 	 * open evdev by using non blocking I/O:
664 	 *   read from /dev/input/eventX would block our thread otherwise
665 	 */
666 	sc->vsc_fd = open(sc->vsc_evdev, O_RDWR | O_NONBLOCK);
667 	if (sc->vsc_fd < 0) {
668 		WPRINTF(("%s: failed to open %s", __func__, sc->vsc_evdev));
669 		goto failed;
670 	}
671 
672 	/* check if evdev is really a evdev */
673 	int evversion;
674 	int error = ioctl(sc->vsc_fd, EVIOCGVERSION, &evversion);
675 	if (error < 0) {
676 		WPRINTF(("%s: %s is no evdev", __func__, sc->vsc_evdev));
677 		goto failed;
678 	}
679 
680 	/* gain exclusive access to evdev */
681 	error = ioctl(sc->vsc_fd, EVIOCGRAB, 1);
682 	if (error < 0) {
683 		WPRINTF(("%s: failed to grab %s", __func__, sc->vsc_evdev));
684 		goto failed;
685 	}
686 
687 	if (pthread_mutexattr_init(&mtx_attr)) {
688 		WPRINTF(("%s: init mutexattr failed", __func__));
689 		goto failed;
690 	}
691 	if (pthread_mutexattr_settype(&mtx_attr, PTHREAD_MUTEX_RECURSIVE)) {
692 		WPRINTF(("%s: settype mutexattr failed", __func__));
693 		goto failed;
694 	}
695 	if (pthread_mutex_init(&sc->vsc_mtx, &mtx_attr)) {
696 		WPRINTF(("%s: init mutex failed", __func__));
697 		goto failed;
698 	}
699 
700 	/* init softc */
701 	sc->vsc_eventqueue.idx = 0;
702 	sc->vsc_eventqueue.size = VTINPUT_MAX_PKT_LEN;
703 	sc->vsc_eventqueue.events = calloc(
704 	    sc->vsc_eventqueue.size, sizeof(struct vtinput_event_elem));
705 	sc->vsc_config_valid = 0;
706 	if (sc->vsc_eventqueue.events == NULL) {
707 		WPRINTF(("%s: failed to alloc eventqueue", __func__));
708 		goto failed;
709 	}
710 
711 	/* register event handler */
712 	sc->vsc_evp = mevent_add(sc->vsc_fd, EVF_READ, vtinput_read_event, sc);
713 	if (sc->vsc_evp == NULL) {
714 		WPRINTF(("%s: could not register mevent", __func__));
715 		goto failed;
716 	}
717 
718 #ifndef WITHOUT_CAPSICUM
719 	cap_rights_t rights;
720 	cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ, CAP_WRITE);
721 	if (caph_rights_limit(sc->vsc_fd, &rights) == -1) {
722 		errx(EX_OSERR, "Unable to apply rights for sandbox");
723 	}
724 #endif
725 
726 	/* link virtio to softc */
727 	vi_softc_linkup(
728 	    &sc->vsc_vs, &vtinput_vi_consts, sc, pi, sc->vsc_queues);
729 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
730 
731 	/* init virtio queues */
732 	sc->vsc_queues[VTINPUT_EVENTQ].vq_qsize = VTINPUT_RINGSZ;
733 	sc->vsc_queues[VTINPUT_EVENTQ].vq_notify = pci_vtinput_notify_eventq;
734 	sc->vsc_queues[VTINPUT_STATUSQ].vq_qsize = VTINPUT_RINGSZ;
735 	sc->vsc_queues[VTINPUT_STATUSQ].vq_notify = pci_vtinput_notify_statusq;
736 
737 	/* initialize config space */
738 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_INPUT);
739 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
740 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_INPUTDEV);
741 	pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_INPUTDEV_OTHER);
742 	pci_set_cfgdata8(pi, PCIR_REVID, VIRTIO_REV_INPUT);
743 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_SUBDEV_INPUT);
744 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_SUBVEN_INPUT);
745 
746 	/* add MSI-X table BAR */
747 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
748 		goto failed;
749 	/* add virtio register */
750 	vi_set_io_bar(&sc->vsc_vs, 0);
751 
752 	return (0);
753 
754 failed:
755 	if (sc == NULL) {
756 		return (-1);
757 	}
758 
759 	if (sc->vsc_evp)
760 		mevent_delete(sc->vsc_evp);
761 	if (sc->vsc_eventqueue.events)
762 		free(sc->vsc_eventqueue.events);
763 	if (sc->vsc_mtx)
764 		pthread_mutex_destroy(&sc->vsc_mtx);
765 	if (mtx_attr)
766 		pthread_mutexattr_destroy(&mtx_attr);
767 	if (sc->vsc_fd)
768 		close(sc->vsc_fd);
769 
770 	free(sc);
771 
772 	return (-1);
773 }
774 
775 struct pci_devemu pci_de_vinput = {
776 	.pe_emu = "virtio-input",
777 	.pe_init = pci_vtinput_init,
778 	.pe_legacy_config = pci_vtinput_legacy_config,
779 	.pe_barwrite = vi_pci_write,
780 	.pe_barread = vi_pci_read,
781 };
782 PCI_EMUL_SET(pci_de_vinput);
783