2014-03-28 12:18:47 +04:00
|
|
|
/*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
* (at your option) any later version. See the COPYING file in the
|
|
|
|
* top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:07 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2014-03-28 12:18:47 +04:00
|
|
|
#include "qemu/sockets.h"
|
|
|
|
|
|
|
|
#include "hw/virtio/virtio.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2014-03-28 12:18:47 +04:00
|
|
|
#include "hw/virtio/virtio-input.h"
|
|
|
|
|
2015-07-14 14:44:12 +03:00
|
|
|
#include <sys/ioctl.h>
|
2014-03-28 12:18:47 +04:00
|
|
|
#include "standard-headers/linux/input.h"
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static struct virtio_input_config virtio_input_host_config[] = {
|
|
|
|
{ /* empty list */ },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_input_host_event(void *opaque)
|
|
|
|
{
|
|
|
|
VirtIOInputHost *vih = opaque;
|
|
|
|
VirtIOInput *vinput = VIRTIO_INPUT(vih);
|
|
|
|
struct virtio_input_event virtio;
|
|
|
|
struct input_event evdev;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
rc = read(vih->fd, &evdev, sizeof(evdev));
|
|
|
|
if (rc != sizeof(evdev)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtio.type = cpu_to_le16(evdev.type);
|
|
|
|
virtio.code = cpu_to_le16(evdev.code);
|
|
|
|
virtio.value = cpu_to_le32(evdev.value);
|
|
|
|
virtio_input_send(vinput, &virtio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_input_bits_config(VirtIOInputHost *vih,
|
|
|
|
int type, int count)
|
|
|
|
{
|
|
|
|
virtio_input_config bits;
|
|
|
|
int rc, i, size = 0;
|
|
|
|
|
|
|
|
memset(&bits, 0, sizeof(bits));
|
|
|
|
rc = ioctl(vih->fd, EVIOCGBIT(type, count/8), bits.u.bitmap);
|
|
|
|
if (rc < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count/8; i++) {
|
|
|
|
if (bits.u.bitmap[i]) {
|
|
|
|
size = i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (size == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits.select = VIRTIO_INPUT_CFG_EV_BITS;
|
|
|
|
bits.subsel = type;
|
|
|
|
bits.size = size;
|
|
|
|
virtio_input_add_config(VIRTIO_INPUT(vih), &bits);
|
|
|
|
}
|
|
|
|
|
2016-04-13 17:43:23 +03:00
|
|
|
static void virtio_input_abs_config(VirtIOInputHost *vih, int axis)
|
|
|
|
{
|
|
|
|
virtio_input_config config;
|
|
|
|
struct input_absinfo absinfo;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ioctl(vih->fd, EVIOCGABS(axis), &absinfo);
|
|
|
|
if (rc < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&config, 0, sizeof(config));
|
|
|
|
config.select = VIRTIO_INPUT_CFG_ABS_INFO;
|
|
|
|
config.subsel = axis;
|
|
|
|
config.size = sizeof(virtio_input_absinfo);
|
|
|
|
|
|
|
|
config.u.abs.min = cpu_to_le32(absinfo.minimum);
|
|
|
|
config.u.abs.max = cpu_to_le32(absinfo.maximum);
|
|
|
|
config.u.abs.fuzz = cpu_to_le32(absinfo.fuzz);
|
|
|
|
config.u.abs.flat = cpu_to_le32(absinfo.flat);
|
|
|
|
config.u.abs.res = cpu_to_le32(absinfo.resolution);
|
|
|
|
|
|
|
|
virtio_input_add_config(VIRTIO_INPUT(vih), &config);
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:18:47 +04:00
|
|
|
static void virtio_input_host_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
|
|
|
|
VirtIOInput *vinput = VIRTIO_INPUT(dev);
|
2016-04-13 17:43:23 +03:00
|
|
|
virtio_input_config id, *abs;
|
2014-03-28 12:18:47 +04:00
|
|
|
struct input_id ids;
|
2016-04-13 17:43:23 +03:00
|
|
|
int rc, ver, i, axis;
|
|
|
|
uint8_t byte;
|
2014-03-28 12:18:47 +04:00
|
|
|
|
|
|
|
if (!vih->evdev) {
|
|
|
|
error_setg(errp, "evdev property is required");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vih->fd = open(vih->evdev, O_RDWR);
|
|
|
|
if (vih->fd < 0) {
|
|
|
|
error_setg_file_open(errp, errno, vih->evdev);
|
|
|
|
return;
|
|
|
|
}
|
2022-04-25 16:39:06 +03:00
|
|
|
if (!g_unix_set_fd_nonblocking(vih->fd, true, NULL)) {
|
|
|
|
error_setg_errno(errp, errno, "Failed to set FD nonblocking");
|
|
|
|
goto err_close;
|
|
|
|
}
|
2014-03-28 12:18:47 +04:00
|
|
|
|
|
|
|
rc = ioctl(vih->fd, EVIOCGVERSION, &ver);
|
|
|
|
if (rc < 0) {
|
|
|
|
error_setg(errp, "%s: is not an evdev device", vih->evdev);
|
|
|
|
goto err_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ioctl(vih->fd, EVIOCGRAB, 1);
|
|
|
|
if (rc < 0) {
|
|
|
|
error_setg_errno(errp, errno, "%s: failed to get exclusive access",
|
|
|
|
vih->evdev);
|
|
|
|
goto err_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&id, 0, sizeof(id));
|
|
|
|
ioctl(vih->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string);
|
|
|
|
id.select = VIRTIO_INPUT_CFG_ID_NAME;
|
|
|
|
id.size = strlen(id.u.string);
|
|
|
|
virtio_input_add_config(vinput, &id);
|
|
|
|
|
|
|
|
if (ioctl(vih->fd, EVIOCGID, &ids) == 0) {
|
|
|
|
memset(&id, 0, sizeof(id));
|
|
|
|
id.select = VIRTIO_INPUT_CFG_ID_DEVIDS;
|
|
|
|
id.size = sizeof(struct virtio_input_devids);
|
|
|
|
id.u.ids.bustype = cpu_to_le16(ids.bustype);
|
|
|
|
id.u.ids.vendor = cpu_to_le16(ids.vendor);
|
|
|
|
id.u.ids.product = cpu_to_le16(ids.product);
|
|
|
|
id.u.ids.version = cpu_to_le16(ids.version);
|
|
|
|
virtio_input_add_config(vinput, &id);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtio_input_bits_config(vih, EV_KEY, KEY_CNT);
|
|
|
|
virtio_input_bits_config(vih, EV_REL, REL_CNT);
|
|
|
|
virtio_input_bits_config(vih, EV_ABS, ABS_CNT);
|
|
|
|
virtio_input_bits_config(vih, EV_MSC, MSC_CNT);
|
|
|
|
virtio_input_bits_config(vih, EV_SW, SW_CNT);
|
2016-03-31 12:53:48 +03:00
|
|
|
virtio_input_bits_config(vih, EV_LED, LED_CNT);
|
2014-03-28 12:18:47 +04:00
|
|
|
|
2016-04-13 17:43:23 +03:00
|
|
|
abs = virtio_input_find_config(VIRTIO_INPUT(vih),
|
|
|
|
VIRTIO_INPUT_CFG_EV_BITS, EV_ABS);
|
|
|
|
if (abs) {
|
|
|
|
for (i = 0; i < abs->size; i++) {
|
|
|
|
byte = abs->u.bitmap[i];
|
|
|
|
axis = 8 * i;
|
|
|
|
while (byte) {
|
|
|
|
if (byte & 1) {
|
|
|
|
virtio_input_abs_config(vih, axis);
|
|
|
|
}
|
|
|
|
axis++;
|
|
|
|
byte >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:18:47 +04:00
|
|
|
qemu_set_fd_handler(vih->fd, virtio_input_host_event, NULL, vih);
|
|
|
|
return;
|
|
|
|
|
|
|
|
err_close:
|
|
|
|
close(vih->fd);
|
|
|
|
vih->fd = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
static void virtio_input_host_unrealize(DeviceState *dev)
|
2014-03-28 12:18:47 +04:00
|
|
|
{
|
|
|
|
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
|
|
|
|
|
|
|
|
if (vih->fd > 0) {
|
|
|
|
qemu_set_fd_handler(vih->fd, NULL, NULL, NULL);
|
|
|
|
close(vih->fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 14:45:46 +03:00
|
|
|
static void virtio_input_host_handle_status(VirtIOInput *vinput,
|
|
|
|
virtio_input_event *event)
|
|
|
|
{
|
|
|
|
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(vinput);
|
|
|
|
struct input_event evdev;
|
2020-12-03 22:58:19 +03:00
|
|
|
struct timeval tval;
|
2016-04-01 14:45:46 +03:00
|
|
|
int rc;
|
|
|
|
|
2020-12-03 22:58:19 +03:00
|
|
|
if (gettimeofday(&tval, NULL)) {
|
2016-04-01 14:45:46 +03:00
|
|
|
perror("virtio_input_host_handle_status: gettimeofday");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:58:19 +03:00
|
|
|
evdev.input_event_sec = tval.tv_sec;
|
|
|
|
evdev.input_event_usec = tval.tv_usec;
|
2016-04-01 14:45:46 +03:00
|
|
|
evdev.type = le16_to_cpu(event->type);
|
|
|
|
evdev.code = le16_to_cpu(event->code);
|
|
|
|
evdev.value = le32_to_cpu(event->value);
|
|
|
|
|
|
|
|
rc = write(vih->fd, &evdev, sizeof(evdev));
|
|
|
|
if (rc == -1) {
|
|
|
|
perror("virtio_input_host_handle_status: write");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:18:47 +04:00
|
|
|
static const VMStateDescription vmstate_virtio_input_host = {
|
|
|
|
.name = "virtio-input-host",
|
|
|
|
.unmigratable = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static Property virtio_input_host_properties[] = {
|
|
|
|
DEFINE_PROP_STRING("evdev", VirtIOInputHost, evdev),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_input_host_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
dc->vmsd = &vmstate_virtio_input_host;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, virtio_input_host_properties);
|
2014-03-28 12:18:47 +04:00
|
|
|
vic->realize = virtio_input_host_realize;
|
|
|
|
vic->unrealize = virtio_input_host_unrealize;
|
2016-04-01 14:45:46 +03:00
|
|
|
vic->handle_status = virtio_input_host_handle_status;
|
2014-03-28 12:18:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_input_host_init(Object *obj)
|
|
|
|
{
|
|
|
|
VirtIOInput *vinput = VIRTIO_INPUT(obj);
|
|
|
|
|
|
|
|
virtio_input_init_config(vinput, virtio_input_host_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo virtio_input_host_info = {
|
|
|
|
.name = TYPE_VIRTIO_INPUT_HOST,
|
|
|
|
.parent = TYPE_VIRTIO_INPUT,
|
|
|
|
.instance_size = sizeof(VirtIOInputHost),
|
|
|
|
.instance_init = virtio_input_host_init,
|
|
|
|
.class_init = virtio_input_host_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void virtio_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&virtio_input_host_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(virtio_register_types)
|