2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * virtio input device emulation.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #ifndef WITHOUT_CAPSICUM
39 #include <sys/capsicum.h>
41 #include <capsicum_helpers.h>
43 #include <sys/ioctl.h>
44 #include <sys/linker_set.h>
47 #include <dev/evdev/input.h>
68 #define VTINPUT_RINGSZ 64
70 #define VTINPUT_MAX_PKT_LEN 10
75 #define VTINPUT_EVENTQ 0
76 #define VTINPUT_STATUSQ 1
78 #define VTINPUT_MAXQ 2
80 static int pci_vtinput_debug;
81 #define DPRINTF(params) \
82 if (pci_vtinput_debug) \
84 #define WPRINTF(params) PRINTLN params
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
96 struct vtinput_absinfo {
104 struct vtinput_devids {
111 struct vtinput_config {
119 struct vtinput_absinfo abs;
120 struct vtinput_devids ids;
124 struct vtinput_event {
130 struct vtinput_event_elem {
131 struct vtinput_event event;
136 struct vtinput_eventqueue {
137 struct vtinput_event_elem *events;
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;
151 struct vtinput_config vsc_config;
152 int vsc_config_valid;
153 struct mevent *vsc_evp;
154 struct vtinput_eventqueue vsc_eventqueue;
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);
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,
172 pci_vtinput_reset(void *vsc)
174 struct pci_vtinput_softc *sc = vsc;
176 DPRINTF(("%s: device reset requested", __func__));
177 vi_reset_dev(&sc->vsc_vs);
181 pci_vtinput_notify_eventq(void *vsc __unused, struct vqueue_info *vq __unused)
183 DPRINTF(("%s", __func__));
187 pci_vtinput_notify_statusq(void *vsc, struct vqueue_info *vq)
189 struct pci_vtinput_softc *sc = vsc;
191 while (vq_has_descs(vq)) {
192 /* get descriptor chain */
195 const int n = vq_getchain(vq, &iov, 1, &req);
197 WPRINTF(("%s: invalid descriptor: %d", __func__, n));
202 struct vtinput_event event;
203 memcpy(&event, iov.iov_base, sizeof(event));
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
213 * - frames become larger and larger
214 * avoid endless loops by ignoring EV_MSC
216 if (event.type == EV_MSC) {
217 vq_relchain(vq, req.idx, sizeof(event));
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__));
229 if (write(sc->vsc_fd, &host_event, sizeof(host_event)) == -1) {
230 WPRINTF(("%s: failed to write host_event", __func__));
233 vq_relchain(vq, req.idx, sizeof(event));
239 pci_vtinput_get_bitmap(struct pci_vtinput_softc *sc, int cmd, int count)
241 if (count <= 0 || !sc) {
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) {
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]) {
262 pci_vtinput_read_config_id_name(struct pci_vtinput_softc *sc)
265 if (ioctl(sc->vsc_fd, EVIOCGNAME(sizeof(name) - 1), name) < 0) {
269 memcpy(sc->vsc_config.u.string, name, sizeof(name));
270 sc->vsc_config.size = strnlen(name, sizeof(name));
276 pci_vtinput_read_config_id_serial(struct pci_vtinput_softc *sc)
278 /* serial isn't supported */
279 sc->vsc_config.size = 0;
285 pci_vtinput_read_config_id_devids(struct pci_vtinput_softc *sc)
287 struct input_id devids;
288 if (ioctl(sc->vsc_fd, EVIOCGID, &devids)) {
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);
302 pci_vtinput_read_config_prop_bits(struct pci_vtinput_softc *sc)
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.
310 const int count = howmany(INPUT_PROP_CNT, sizeof(long) * 8) *
312 const unsigned int cmd = EVIOCGPROP(count);
313 const int size = pci_vtinput_get_bitmap(sc, cmd, count);
318 sc->vsc_config.size = size;
324 pci_vtinput_read_config_ev_bits(struct pci_vtinput_softc *sc, uint8_t type)
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.
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);
364 sc->vsc_config.size = size;
370 pci_vtinput_read_config_abs_info(struct pci_vtinput_softc *sc)
372 /* check if evdev has EV_ABS */
373 if (!pci_vtinput_read_config_ev_bits(sc, EV_ABS)) {
377 /* get abs information */
378 struct input_absinfo abs;
379 if (ioctl(sc->vsc_fd, EVIOCGABS(sc->vsc_config.subsel), &abs) < 0) {
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);
395 pci_vtinput_read_config(struct pci_vtinput_softc *sc)
397 switch (sc->vsc_config.select) {
398 case VTINPUT_CFG_UNSET:
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);
419 pci_vtinput_cfgread(void *vsc, int offset, int size, uint32_t *retval)
421 struct pci_vtinput_softc *sc = vsc;
423 /* check for valid offset and size */
424 if (offset + size > (int)sizeof(struct vtinput_config)) {
425 WPRINTF(("%s: read to invalid offset/size %d/%d", __func__,
427 memset(retval, 0, size);
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);
439 sc->vsc_config_valid = 1;
442 uint8_t *ptr = (uint8_t *)&sc->vsc_config;
443 memcpy(retval, ptr + offset, size);
449 pci_vtinput_cfgwrite(void *vsc, int offset, int size, uint32_t value)
451 struct pci_vtinput_softc *sc = vsc;
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));
459 /* copy value into config */
460 uint8_t *ptr = (uint8_t *)&sc->vsc_config;
461 memcpy(ptr + offset, &value, size);
463 /* select/subsel changed, query new config on next cfgread */
464 sc->vsc_config_valid = 0;
470 vtinput_eventqueue_add_event(
471 struct vtinput_eventqueue *queue, struct input_event *e)
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 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!",
484 queue->events = newPtr;
485 queue->size = newSize;
489 struct vtinput_event *event = &queue->events[queue->idx].event;
490 event->type = e->type;
491 event->code = e->code;
492 event->value = e->value;
499 vtinput_eventqueue_clear(struct vtinput_eventqueue *queue)
501 /* just reset index to clear queue */
506 vtinput_eventqueue_send_events(
507 struct vtinput_eventqueue *queue, struct vqueue_info *vq)
510 * First iteration through eventqueue:
511 * Get descriptor chains.
513 for (uint32_t i = 0; i < queue->idx; ++i) {
515 if (!vq_has_descs(vq)) {
517 * We don't have enough descriptors for all events.
518 * Return chains back to guest.
522 "%s: not enough available descriptors, dropping %d events",
523 __func__, queue->idx));
527 /* get descriptor chain */
530 const int n = vq_getchain(vq, &iov, 1, &req);
532 WPRINTF(("%s: invalid descriptor: %d", __func__, n));
537 ("%s: invalid number of descriptors in chain: %d",
539 /* release invalid chain */
540 vq_relchain(vq, req.idx, 0);
543 if (iov.iov_len < sizeof(struct vtinput_event)) {
544 WPRINTF(("%s: invalid descriptor length: %lu", __func__,
546 /* release invalid chain */
547 vq_relchain(vq, req.idx, 0);
551 /* save descriptor */
552 queue->events[i].iov = iov;
553 queue->events[i].idx = req.idx;
557 * Second iteration through eventqueue:
558 * Send events to guest by releasing chains
560 for (uint32_t i = 0; i < queue->idx; ++i) {
561 struct vtinput_event_elem event = queue->events[i];
562 memcpy(event.iov.iov_base, &event.event,
563 sizeof(struct vtinput_event));
564 vq_relchain(vq, event.idx, sizeof(struct vtinput_event));
567 /* clear queue and send interrupt to guest */
568 vtinput_eventqueue_clear(queue);
573 vtinput_read_event_from_host(int fd, struct input_event *event)
575 const int len = read(fd, event, sizeof(struct input_event));
576 if (len != sizeof(struct input_event)) {
577 if (len == -1 && errno != EAGAIN) {
578 WPRINTF(("%s: event read failed! len = %d, errno = %d",
579 __func__, len, errno));
582 /* host doesn't have more events for us */
590 vtinput_read_event(int fd __attribute((unused)),
591 enum ev_type t __attribute__((unused)), void *arg __attribute__((unused)))
593 struct pci_vtinput_softc *sc = arg;
595 /* skip if driver isn't ready */
596 if (!(sc->vsc_vs.vs_status & VIRTIO_CONFIG_STATUS_DRIVER_OK))
599 /* read all events from host */
600 struct input_event event;
601 while (vtinput_read_event_from_host(sc->vsc_fd, &event) == 0) {
602 /* add events to our queue */
603 vtinput_eventqueue_add_event(&sc->vsc_eventqueue, &event);
605 /* only send events to guest on EV_SYN or SYN_REPORT */
606 if (event.type != EV_SYN || event.type != SYN_REPORT) {
610 /* send host events to guest */
611 vtinput_eventqueue_send_events(
612 &sc->vsc_eventqueue, &sc->vsc_queues[VTINPUT_EVENTQ]);
617 pci_vtinput_legacy_config(nvlist_t *nvl, const char *opts)
624 * virtio-input,/dev/input/eventX
626 char *cp = strchr(opts, ',');
628 set_config_value_node(nvl, "path", opts);
631 char *path = strndup(opts, cp - opts);
632 set_config_value_node(nvl, "path", path);
635 return (pci_parse_legacy_config(nvl, cp + 1));
639 pci_vtinput_init(struct pci_devinst *pi, nvlist_t *nvl)
641 struct pci_vtinput_softc *sc;
645 * Else it's possible to access it uninitialized by jumping to failed.
647 pthread_mutexattr_t mtx_attr = NULL;
649 sc = calloc(1, sizeof(struct pci_vtinput_softc));
651 sc->vsc_evdev = get_config_value_node(nvl, "path");
652 if (sc->vsc_evdev == NULL) {
653 WPRINTF(("%s: missing required path config value", __func__));
658 * open evdev by using non blocking I/O:
659 * read from /dev/input/eventX would block our thread otherwise
661 sc->vsc_fd = open(sc->vsc_evdev, O_RDWR | O_NONBLOCK);
662 if (sc->vsc_fd < 0) {
663 WPRINTF(("%s: failed to open %s", __func__, sc->vsc_evdev));
667 /* check if evdev is really a evdev */
669 int error = ioctl(sc->vsc_fd, EVIOCGVERSION, &evversion);
671 WPRINTF(("%s: %s is no evdev", __func__, sc->vsc_evdev));
675 /* gain exclusive access to evdev */
676 error = ioctl(sc->vsc_fd, EVIOCGRAB, 1);
678 WPRINTF(("%s: failed to grab %s", __func__, sc->vsc_evdev));
682 if (pthread_mutexattr_init(&mtx_attr)) {
683 WPRINTF(("%s: init mutexattr failed", __func__));
686 if (pthread_mutexattr_settype(&mtx_attr, PTHREAD_MUTEX_RECURSIVE)) {
687 WPRINTF(("%s: settype mutexattr failed", __func__));
690 if (pthread_mutex_init(&sc->vsc_mtx, &mtx_attr)) {
691 WPRINTF(("%s: init mutex failed", __func__));
696 sc->vsc_eventqueue.idx = 0;
697 sc->vsc_eventqueue.size = VTINPUT_MAX_PKT_LEN;
698 sc->vsc_eventqueue.events = calloc(
699 sc->vsc_eventqueue.size, sizeof(struct vtinput_event_elem));
700 sc->vsc_config_valid = 0;
701 if (sc->vsc_eventqueue.events == NULL) {
702 WPRINTF(("%s: failed to alloc eventqueue", __func__));
706 /* register event handler */
707 sc->vsc_evp = mevent_add(sc->vsc_fd, EVF_READ, vtinput_read_event, sc);
708 if (sc->vsc_evp == NULL) {
709 WPRINTF(("%s: could not register mevent", __func__));
713 #ifndef WITHOUT_CAPSICUM
715 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ, CAP_WRITE);
716 if (caph_rights_limit(sc->vsc_fd, &rights) == -1) {
717 errx(EX_OSERR, "Unable to apply rights for sandbox");
721 /* link virtio to softc */
723 &sc->vsc_vs, &vtinput_vi_consts, sc, pi, sc->vsc_queues);
724 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
726 /* init virtio queues */
727 sc->vsc_queues[VTINPUT_EVENTQ].vq_qsize = VTINPUT_RINGSZ;
728 sc->vsc_queues[VTINPUT_EVENTQ].vq_notify = pci_vtinput_notify_eventq;
729 sc->vsc_queues[VTINPUT_STATUSQ].vq_qsize = VTINPUT_RINGSZ;
730 sc->vsc_queues[VTINPUT_STATUSQ].vq_notify = pci_vtinput_notify_statusq;
732 /* initialize config space */
733 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_INPUT);
734 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
735 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_INPUTDEV);
736 pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_INPUTDEV_OTHER);
737 pci_set_cfgdata8(pi, PCIR_REVID, VIRTIO_REV_INPUT);
738 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_SUBDEV_INPUT);
739 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_SUBVEN_INPUT);
741 /* add MSI-X table BAR */
742 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
744 /* add virtio register */
745 vi_set_io_bar(&sc->vsc_vs, 0);
755 mevent_delete(sc->vsc_evp);
756 if (sc->vsc_eventqueue.events)
757 free(sc->vsc_eventqueue.events);
759 pthread_mutex_destroy(&sc->vsc_mtx);
761 pthread_mutexattr_destroy(&mtx_attr);
770 static const struct pci_devemu pci_de_vinput = {
771 .pe_emu = "virtio-input",
772 .pe_init = pci_vtinput_init,
773 .pe_legacy_config = pci_vtinput_legacy_config,
774 .pe_barwrite = vi_pci_write,
775 .pe_barread = vi_pci_read,
777 PCI_EMUL_SET(pci_de_vinput);