xref: /freebsd/usr.sbin/bhyve/pci_virtio_input.c (revision 38a52bd3)
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 	.vc_name =	"vtinput",
163 	.vc_nvq =	VTINPUT_MAXQ,
164 	.vc_cfgsize =	sizeof(struct vtinput_config),
165 	.vc_reset =	pci_vtinput_reset,
166 	.vc_cfgread =	pci_vtinput_cfgread,
167 	.vc_cfgwrite =	pci_vtinput_cfgwrite,
168 	.vc_hv_caps =	0,
169 };
170 
171 static void
172 pci_vtinput_reset(void *vsc)
173 {
174 	struct pci_vtinput_softc *sc = vsc;
175 
176 	DPRINTF(("%s: device reset requested", __func__));
177 	vi_reset_dev(&sc->vsc_vs);
178 }
179 
180 static void
181 pci_vtinput_notify_eventq(void *vsc __unused, struct vqueue_info *vq __unused)
182 {
183 	DPRINTF(("%s", __func__));
184 }
185 
186 static void
187 pci_vtinput_notify_statusq(void *vsc, struct vqueue_info *vq)
188 {
189 	struct pci_vtinput_softc *sc = vsc;
190 
191 	while (vq_has_descs(vq)) {
192 		/* get descriptor chain */
193 		struct iovec iov;
194 		struct vi_req req;
195 		const int n = vq_getchain(vq, &iov, 1, &req);
196 		if (n <= 0) {
197 			WPRINTF(("%s: invalid descriptor: %d", __func__, n));
198 			return;
199 		}
200 
201 		/* get event */
202 		struct vtinput_event event;
203 		memcpy(&event, iov.iov_base, sizeof(event));
204 
205 		/*
206 		 * on multi touch devices:
207 		 * - host send EV_MSC to guest
208 		 * - guest sends EV_MSC back to host
209 		 * - host writes EV_MSC to evdev
210 		 * - evdev saves EV_MSC in it's event buffer
211 		 * - host receives an extra EV_MSC by reading the evdev event
212 		 *   buffer
213 		 * - frames become larger and larger
214 		 * avoid endless loops by ignoring EV_MSC
215 		 */
216 		if (event.type == EV_MSC) {
217 			vq_relchain(vq, req.idx, sizeof(event));
218 			continue;
219 		}
220 
221 		/* send event to evdev */
222 		struct input_event host_event;
223 		host_event.type = event.type;
224 		host_event.code = event.code;
225 		host_event.value = event.value;
226 		if (gettimeofday(&host_event.time, NULL) != 0) {
227 			WPRINTF(("%s: failed gettimeofday", __func__));
228 		}
229 		if (write(sc->vsc_fd, &host_event, sizeof(host_event)) == -1) {
230 			WPRINTF(("%s: failed to write host_event", __func__));
231 		}
232 
233 		vq_relchain(vq, req.idx, sizeof(event));
234 	}
235 	vq_endchains(vq, 1);
236 }
237 
238 static int
239 pci_vtinput_get_bitmap(struct pci_vtinput_softc *sc, int cmd, int count)
240 {
241 	if (count <= 0 || !sc) {
242 		return (-1);
243 	}
244 
245 	/* query bitmap */
246 	memset(sc->vsc_config.u.bitmap, 0, sizeof(sc->vsc_config.u.bitmap));
247 	if (ioctl(sc->vsc_fd, cmd, sc->vsc_config.u.bitmap) < 0) {
248 		return (-1);
249 	}
250 
251 	/* get number of set bytes in bitmap */
252 	for (int i = count - 1; i >= 0; i--) {
253 		if (sc->vsc_config.u.bitmap[i]) {
254 			return i + 1;
255 		}
256 	}
257 
258 	return (-1);
259 }
260 
261 static int
262 pci_vtinput_read_config_id_name(struct pci_vtinput_softc *sc)
263 {
264 	char name[128];
265 	if (ioctl(sc->vsc_fd, EVIOCGNAME(sizeof(name) - 1), name) < 0) {
266 		return (1);
267 	}
268 
269 	memcpy(sc->vsc_config.u.string, name, sizeof(name));
270 	sc->vsc_config.size = strnlen(name, sizeof(name));
271 
272 	return (0);
273 }
274 
275 static int
276 pci_vtinput_read_config_id_serial(struct pci_vtinput_softc *sc)
277 {
278 	/* serial isn't supported */
279 	sc->vsc_config.size = 0;
280 
281 	return (0);
282 }
283 
284 static int
285 pci_vtinput_read_config_id_devids(struct pci_vtinput_softc *sc)
286 {
287 	struct input_id devids;
288 	if (ioctl(sc->vsc_fd, EVIOCGID, &devids)) {
289 		return (1);
290 	}
291 
292 	sc->vsc_config.u.ids.bustype = devids.bustype;
293 	sc->vsc_config.u.ids.vendor = devids.vendor;
294 	sc->vsc_config.u.ids.product = devids.product;
295 	sc->vsc_config.u.ids.version = devids.version;
296 	sc->vsc_config.size = sizeof(struct vtinput_devids);
297 
298 	return (0);
299 }
300 
301 static int
302 pci_vtinput_read_config_prop_bits(struct pci_vtinput_softc *sc)
303 {
304 	/*
305 	 * Evdev bitmap countains 1 bit per count. Additionally evdev bitmaps
306 	 * are arrays of longs instead of chars. Calculate how many longs are
307 	 * required for evdev bitmap. Multiply that with sizeof(long) to get the
308 	 * number of elements.
309 	 */
310 	const int count = howmany(INPUT_PROP_CNT, sizeof(long) * 8) *
311 	    sizeof(long);
312 	const unsigned int cmd = EVIOCGPROP(count);
313 	const int size = pci_vtinput_get_bitmap(sc, cmd, count);
314 	if (size <= 0) {
315 		return (1);
316 	}
317 
318 	sc->vsc_config.size = size;
319 
320 	return (0);
321 }
322 
323 static int
324 pci_vtinput_read_config_ev_bits(struct pci_vtinput_softc *sc, uint8_t type)
325 {
326 	int count;
327 
328 	switch (type) {
329 	case EV_KEY:
330 		count = KEY_CNT;
331 		break;
332 	case EV_REL:
333 		count = REL_CNT;
334 		break;
335 	case EV_ABS:
336 		count = ABS_CNT;
337 		break;
338 	case EV_MSC:
339 		count = MSC_CNT;
340 		break;
341 	case EV_SW:
342 		count = SW_CNT;
343 		break;
344 	case EV_LED:
345 		count = LED_CNT;
346 		break;
347 	default:
348 		return (1);
349 	}
350 
351 	/*
352 	 * Evdev bitmap countains 1 bit per count. Additionally evdev bitmaps
353 	 * are arrays of longs instead of chars. Calculate how many longs are
354 	 * required for evdev bitmap. Multiply that with sizeof(long) to get the
355 	 * number of elements.
356 	 */
357 	count = howmany(count, sizeof(long) * 8) * sizeof(long);
358 	const unsigned int cmd = EVIOCGBIT(sc->vsc_config.subsel, count);
359 	const int size = pci_vtinput_get_bitmap(sc, cmd, count);
360 	if (size <= 0) {
361 		return (1);
362 	}
363 
364 	sc->vsc_config.size = size;
365 
366 	return (0);
367 }
368 
369 static int
370 pci_vtinput_read_config_abs_info(struct pci_vtinput_softc *sc)
371 {
372 	/* check if evdev has EV_ABS */
373 	if (!pci_vtinput_read_config_ev_bits(sc, EV_ABS)) {
374 		return (1);
375 	}
376 
377 	/* get abs information */
378 	struct input_absinfo abs;
379 	if (ioctl(sc->vsc_fd, EVIOCGABS(sc->vsc_config.subsel), &abs) < 0) {
380 		return (1);
381 	}
382 
383 	/* save abs information */
384 	sc->vsc_config.u.abs.min = abs.minimum;
385 	sc->vsc_config.u.abs.max = abs.maximum;
386 	sc->vsc_config.u.abs.fuzz = abs.fuzz;
387 	sc->vsc_config.u.abs.flat = abs.flat;
388 	sc->vsc_config.u.abs.res = abs.resolution;
389 	sc->vsc_config.size = sizeof(struct vtinput_absinfo);
390 
391 	return (0);
392 }
393 
394 static int
395 pci_vtinput_read_config(struct pci_vtinput_softc *sc)
396 {
397 	switch (sc->vsc_config.select) {
398 	case VTINPUT_CFG_UNSET:
399 		return (0);
400 	case VTINPUT_CFG_ID_NAME:
401 		return pci_vtinput_read_config_id_name(sc);
402 	case VTINPUT_CFG_ID_SERIAL:
403 		return pci_vtinput_read_config_id_serial(sc);
404 	case VTINPUT_CFG_ID_DEVIDS:
405 		return pci_vtinput_read_config_id_devids(sc);
406 	case VTINPUT_CFG_PROP_BITS:
407 		return pci_vtinput_read_config_prop_bits(sc);
408 	case VTINPUT_CFG_EV_BITS:
409 		return pci_vtinput_read_config_ev_bits(
410 		    sc, sc->vsc_config.subsel);
411 	case VTINPUT_CFG_ABS_INFO:
412 		return pci_vtinput_read_config_abs_info(sc);
413 	default:
414 		return (1);
415 	}
416 }
417 
418 static int
419 pci_vtinput_cfgread(void *vsc, int offset, int size, uint32_t *retval)
420 {
421 	struct pci_vtinput_softc *sc = vsc;
422 
423 	/* check for valid offset and size */
424 	if (offset + size > sizeof(struct vtinput_config)) {
425 		WPRINTF(("%s: read to invalid offset/size %d/%d", __func__,
426 		    offset, size));
427 		memset(retval, 0, size);
428 		return (0);
429 	}
430 
431 	/* read new config values, if select and subsel changed. */
432 	if (!sc->vsc_config_valid) {
433 		if (pci_vtinput_read_config(sc) != 0) {
434 			DPRINTF(("%s: could not read config %d/%d", __func__,
435 			    sc->vsc_config.select, sc->vsc_config.subsel));
436 			memset(retval, 0, size);
437 			return (0);
438 		}
439 		sc->vsc_config_valid = 1;
440 	}
441 
442 	uint8_t *ptr = (uint8_t *)&sc->vsc_config;
443 	memcpy(retval, ptr + offset, size);
444 
445 	return (0);
446 }
447 
448 static int
449 pci_vtinput_cfgwrite(void *vsc, int offset, int size, uint32_t value)
450 {
451 	struct pci_vtinput_softc *sc = vsc;
452 
453 	/* guest can only write to select and subsel fields */
454 	if (offset + size > 2) {
455 		WPRINTF(("%s: write to readonly reg %d", __func__, offset));
456 		return (1);
457 	}
458 
459 	/* copy value into config */
460 	uint8_t *ptr = (uint8_t *)&sc->vsc_config;
461 	memcpy(ptr + offset, &value, size);
462 
463 	/* select/subsel changed, query new config on next cfgread */
464 	sc->vsc_config_valid = 0;
465 
466 	return (0);
467 }
468 
469 static int
470 vtinput_eventqueue_add_event(
471     struct vtinput_eventqueue *queue, struct input_event *e)
472 {
473 	/* check if queue is full */
474 	if (queue->idx >= queue->size) {
475 		/* alloc new elements for queue */
476 		const uint32_t newSize = queue->idx;
477 		const void *newPtr = realloc(queue->events,
478 		    queue->size * sizeof(struct vtinput_event_elem));
479 		if (newPtr == NULL) {
480 			WPRINTF(("%s: realloc memory for eventqueue failed!",
481 			    __func__));
482 			return (1);
483 		}
484 		/* save new size and eventqueue */
485 		queue->events = (struct vtinput_event_elem *)newPtr;
486 		queue->size = newSize;
487 	}
488 
489 	/* save event */
490 	struct vtinput_event *event = &queue->events[queue->idx].event;
491 	event->type = e->type;
492 	event->code = e->code;
493 	event->value = e->value;
494 	queue->idx++;
495 
496 	return (0);
497 }
498 
499 static void
500 vtinput_eventqueue_clear(struct vtinput_eventqueue *queue)
501 {
502 	/* just reset index to clear queue */
503 	queue->idx = 0;
504 }
505 
506 static void
507 vtinput_eventqueue_send_events(
508     struct vtinput_eventqueue *queue, struct vqueue_info *vq)
509 {
510 	/*
511 	 * First iteration through eventqueue:
512 	 *   Get descriptor chains.
513 	 */
514 	for (uint32_t i = 0; i < queue->idx; ++i) {
515 		/* get descriptor */
516 		if (!vq_has_descs(vq)) {
517 			/*
518 			 * We don't have enough descriptors for all events.
519 			 * Return chains back to guest.
520 			 */
521 			vq_retchains(vq, i);
522 			WPRINTF((
523 			    "%s: not enough available descriptors, dropping %d events",
524 			    __func__, queue->idx));
525 			goto done;
526 		}
527 
528 		/* get descriptor chain */
529 		struct iovec iov;
530 		struct vi_req req;
531 		const int n = vq_getchain(vq, &iov, 1, &req);
532 		if (n <= 0) {
533 			WPRINTF(("%s: invalid descriptor: %d", __func__, n));
534 			return;
535 		}
536 		if (n != 1) {
537 			WPRINTF(
538 			    ("%s: invalid number of descriptors in chain: %d",
539 				__func__, n));
540 			/* release invalid chain */
541 			vq_relchain(vq, req.idx, 0);
542 			return;
543 		}
544 		if (iov.iov_len < sizeof(struct vtinput_event)) {
545 			WPRINTF(("%s: invalid descriptor length: %lu", __func__,
546 			    iov.iov_len));
547 			/* release invalid chain */
548 			vq_relchain(vq, req.idx, 0);
549 			return;
550 		}
551 
552 		/* save descriptor */
553 		queue->events[i].iov = iov;
554 		queue->events[i].idx = req.idx;
555 	}
556 
557 	/*
558 	 * Second iteration through eventqueue:
559 	 *   Send events to guest by releasing chains
560 	 */
561 	for (uint32_t i = 0; i < queue->idx; ++i) {
562 		struct vtinput_event_elem event = queue->events[i];
563 		memcpy(event.iov.iov_base, &event.event,
564 		    sizeof(struct vtinput_event));
565 		vq_relchain(vq, event.idx, sizeof(struct vtinput_event));
566 	}
567 done:
568 	/* clear queue and send interrupt to guest */
569 	vtinput_eventqueue_clear(queue);
570 	vq_endchains(vq, 1);
571 
572 	return;
573 }
574 
575 static int
576 vtinput_read_event_from_host(int fd, struct input_event *event)
577 {
578 	const int len = read(fd, event, sizeof(struct input_event));
579 	if (len != sizeof(struct input_event)) {
580 		if (len == -1 && errno != EAGAIN) {
581 			WPRINTF(("%s: event read failed! len = %d, errno = %d",
582 			    __func__, len, errno));
583 		}
584 
585 		/* host doesn't have more events for us */
586 		return (1);
587 	}
588 
589 	return (0);
590 }
591 
592 static void
593 vtinput_read_event(int fd __attribute((unused)),
594     enum ev_type t __attribute__((unused)), void *arg __attribute__((unused)))
595 {
596 	struct pci_vtinput_softc *sc = arg;
597 
598 	/* skip if driver isn't ready */
599 	if (!(sc->vsc_vs.vs_status & VIRTIO_CONFIG_STATUS_DRIVER_OK))
600 		return;
601 
602 	/* read all events from host */
603 	struct input_event event;
604 	while (vtinput_read_event_from_host(sc->vsc_fd, &event) == 0) {
605 		/* add events to our queue */
606 		vtinput_eventqueue_add_event(&sc->vsc_eventqueue, &event);
607 
608 		/* only send events to guest on EV_SYN or SYN_REPORT */
609 		if (event.type != EV_SYN || event.type != SYN_REPORT) {
610 			continue;
611 		}
612 
613 		/* send host events to guest */
614 		vtinput_eventqueue_send_events(
615 		    &sc->vsc_eventqueue, &sc->vsc_queues[VTINPUT_EVENTQ]);
616 	}
617 }
618 
619 static int
620 pci_vtinput_legacy_config(nvlist_t *nvl, const char *opts)
621 {
622 	if (opts == NULL)
623 		return (-1);
624 
625 	/*
626 	 * parse opts:
627 	 *   virtio-input,/dev/input/eventX
628 	 */
629 	char *cp = strchr(opts, ',');
630 	if (cp == NULL) {
631 		set_config_value_node(nvl, "path", opts);
632 		return (0);
633 	}
634 	char *path = strndup(opts, cp - opts);
635 	set_config_value_node(nvl, "path", path);
636 	free(path);
637 
638 	return (pci_parse_legacy_config(nvl, cp + 1));
639 }
640 
641 static int
642 pci_vtinput_init(struct vmctx *ctx __unused, struct pci_devinst *pi,
643     nvlist_t *nvl)
644 {
645 	struct pci_vtinput_softc *sc;
646 
647 	/*
648 	 * Keep it here.
649 	 * Else it's possible to access it uninitialized by jumping to failed.
650 	 */
651 	pthread_mutexattr_t mtx_attr = NULL;
652 
653 	sc = calloc(1, sizeof(struct pci_vtinput_softc));
654 
655 	sc->vsc_evdev = get_config_value_node(nvl, "path");
656 	if (sc->vsc_evdev == NULL) {
657 		WPRINTF(("%s: missing required path config value", __func__));
658 		goto failed;
659 	}
660 
661 	/*
662 	 * open evdev by using non blocking I/O:
663 	 *   read from /dev/input/eventX would block our thread otherwise
664 	 */
665 	sc->vsc_fd = open(sc->vsc_evdev, O_RDWR | O_NONBLOCK);
666 	if (sc->vsc_fd < 0) {
667 		WPRINTF(("%s: failed to open %s", __func__, sc->vsc_evdev));
668 		goto failed;
669 	}
670 
671 	/* check if evdev is really a evdev */
672 	int evversion;
673 	int error = ioctl(sc->vsc_fd, EVIOCGVERSION, &evversion);
674 	if (error < 0) {
675 		WPRINTF(("%s: %s is no evdev", __func__, sc->vsc_evdev));
676 		goto failed;
677 	}
678 
679 	/* gain exclusive access to evdev */
680 	error = ioctl(sc->vsc_fd, EVIOCGRAB, 1);
681 	if (error < 0) {
682 		WPRINTF(("%s: failed to grab %s", __func__, sc->vsc_evdev));
683 		goto failed;
684 	}
685 
686 	if (pthread_mutexattr_init(&mtx_attr)) {
687 		WPRINTF(("%s: init mutexattr failed", __func__));
688 		goto failed;
689 	}
690 	if (pthread_mutexattr_settype(&mtx_attr, PTHREAD_MUTEX_RECURSIVE)) {
691 		WPRINTF(("%s: settype mutexattr failed", __func__));
692 		goto failed;
693 	}
694 	if (pthread_mutex_init(&sc->vsc_mtx, &mtx_attr)) {
695 		WPRINTF(("%s: init mutex failed", __func__));
696 		goto failed;
697 	}
698 
699 	/* init softc */
700 	sc->vsc_eventqueue.idx = 0;
701 	sc->vsc_eventqueue.size = VTINPUT_MAX_PKT_LEN;
702 	sc->vsc_eventqueue.events = calloc(
703 	    sc->vsc_eventqueue.size, sizeof(struct vtinput_event_elem));
704 	sc->vsc_config_valid = 0;
705 	if (sc->vsc_eventqueue.events == NULL) {
706 		WPRINTF(("%s: failed to alloc eventqueue", __func__));
707 		goto failed;
708 	}
709 
710 	/* register event handler */
711 	sc->vsc_evp = mevent_add(sc->vsc_fd, EVF_READ, vtinput_read_event, sc);
712 	if (sc->vsc_evp == NULL) {
713 		WPRINTF(("%s: could not register mevent", __func__));
714 		goto failed;
715 	}
716 
717 #ifndef WITHOUT_CAPSICUM
718 	cap_rights_t rights;
719 	cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ, CAP_WRITE);
720 	if (caph_rights_limit(sc->vsc_fd, &rights) == -1) {
721 		errx(EX_OSERR, "Unable to apply rights for sandbox");
722 	}
723 #endif
724 
725 	/* link virtio to softc */
726 	vi_softc_linkup(
727 	    &sc->vsc_vs, &vtinput_vi_consts, sc, pi, sc->vsc_queues);
728 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
729 
730 	/* init virtio queues */
731 	sc->vsc_queues[VTINPUT_EVENTQ].vq_qsize = VTINPUT_RINGSZ;
732 	sc->vsc_queues[VTINPUT_EVENTQ].vq_notify = pci_vtinput_notify_eventq;
733 	sc->vsc_queues[VTINPUT_STATUSQ].vq_qsize = VTINPUT_RINGSZ;
734 	sc->vsc_queues[VTINPUT_STATUSQ].vq_notify = pci_vtinput_notify_statusq;
735 
736 	/* initialize config space */
737 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_INPUT);
738 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
739 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_INPUTDEV);
740 	pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_INPUTDEV_OTHER);
741 	pci_set_cfgdata8(pi, PCIR_REVID, VIRTIO_REV_INPUT);
742 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_SUBDEV_INPUT);
743 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_SUBVEN_INPUT);
744 
745 	/* add MSI-X table BAR */
746 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
747 		goto failed;
748 	/* add virtio register */
749 	vi_set_io_bar(&sc->vsc_vs, 0);
750 
751 	return (0);
752 
753 failed:
754 	if (sc == NULL) {
755 		return (-1);
756 	}
757 
758 	if (sc->vsc_evp)
759 		mevent_delete(sc->vsc_evp);
760 	if (sc->vsc_eventqueue.events)
761 		free(sc->vsc_eventqueue.events);
762 	if (sc->vsc_mtx)
763 		pthread_mutex_destroy(&sc->vsc_mtx);
764 	if (mtx_attr)
765 		pthread_mutexattr_destroy(&mtx_attr);
766 	if (sc->vsc_fd)
767 		close(sc->vsc_fd);
768 
769 	free(sc);
770 
771 	return (-1);
772 }
773 
774 static const struct pci_devemu pci_de_vinput = {
775 	.pe_emu = "virtio-input",
776 	.pe_init = pci_vtinput_init,
777 	.pe_legacy_config = pci_vtinput_legacy_config,
778 	.pe_barwrite = vi_pci_write,
779 	.pe_barread = vi_pci_read,
780 };
781 PCI_EMUL_SET(pci_de_vinput);
782