vhost-user: more master/slave things
[qemu.git] / hw / input / vhost-user-input.c
blob1352e372ff39cd3ccf043f947203074ea5831259
1 /*
2 * This work is licensed under the terms of the GNU GPL, version 2 or
3 * (at your option) any later version. See the COPYING file in the
4 * top-level directory.
5 */
7 #include "qemu/osdep.h"
8 #include "qemu/error-report.h"
9 #include "qapi/error.h"
11 #include "hw/virtio/virtio-input.h"
13 static int vhost_input_config_change(struct vhost_dev *dev)
15 error_report("vhost-user-input: unhandled backend config change");
16 return -1;
19 static const VhostDevConfigOps config_ops = {
20 .vhost_dev_config_notifier = vhost_input_config_change,
23 static void vhost_input_realize(DeviceState *dev, Error **errp)
25 VHostUserInput *vhi = VHOST_USER_INPUT(dev);
26 VirtIOInput *vinput = VIRTIO_INPUT(dev);
27 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
29 vhost_dev_set_config_notifier(&vhi->vhost->dev, &config_ops);
30 vinput->cfg_size = sizeof_field(virtio_input_config, u);
31 if (vhost_user_backend_dev_init(vhi->vhost, vdev, 2, errp) == -1) {
32 return;
36 static void vhost_input_change_active(VirtIOInput *vinput)
38 VHostUserInput *vhi = VHOST_USER_INPUT(vinput);
40 if (vinput->active) {
41 vhost_user_backend_start(vhi->vhost);
42 } else {
43 vhost_user_backend_stop(vhi->vhost);
47 static void vhost_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
49 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
50 VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
51 Error *local_err = NULL;
52 int ret;
54 memset(config_data, 0, vinput->cfg_size);
56 ret = vhost_dev_get_config(&vhi->vhost->dev, config_data, vinput->cfg_size,
57 &local_err);
58 if (ret) {
59 error_report_err(local_err);
60 return;
64 static void vhost_input_set_config(VirtIODevice *vdev,
65 const uint8_t *config_data)
67 VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
68 int ret;
70 ret = vhost_dev_set_config(&vhi->vhost->dev, config_data,
71 0, sizeof(virtio_input_config),
72 VHOST_SET_CONFIG_TYPE_MASTER);
73 if (ret) {
74 error_report("vhost-user-input: set device config space failed");
75 return;
78 virtio_notify_config(vdev);
81 static struct vhost_dev *vhost_input_get_vhost(VirtIODevice *vdev)
83 VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
84 return &vhi->vhost->dev;
87 static const VMStateDescription vmstate_vhost_input = {
88 .name = "vhost-user-input",
89 .unmigratable = 1,
92 static void vhost_input_class_init(ObjectClass *klass, void *data)
94 VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
95 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
96 DeviceClass *dc = DEVICE_CLASS(klass);
98 dc->vmsd = &vmstate_vhost_input;
99 vdc->get_config = vhost_input_get_config;
100 vdc->set_config = vhost_input_set_config;
101 vdc->get_vhost = vhost_input_get_vhost;
102 vic->realize = vhost_input_realize;
103 vic->change_active = vhost_input_change_active;
106 static void vhost_input_init(Object *obj)
108 VHostUserInput *vhi = VHOST_USER_INPUT(obj);
110 vhi->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
111 object_property_add_alias(obj, "chardev",
112 OBJECT(vhi->vhost), "chardev");
115 static void vhost_input_finalize(Object *obj)
117 VHostUserInput *vhi = VHOST_USER_INPUT(obj);
119 object_unref(OBJECT(vhi->vhost));
122 static const TypeInfo vhost_input_info = {
123 .name = TYPE_VHOST_USER_INPUT,
124 .parent = TYPE_VIRTIO_INPUT,
125 .instance_size = sizeof(VHostUserInput),
126 .instance_init = vhost_input_init,
127 .instance_finalize = vhost_input_finalize,
128 .class_init = vhost_input_class_init,
131 static void vhost_input_register_types(void)
133 type_register_static(&vhost_input_info);
136 type_init(vhost_input_register_types)