vhost-user-input.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. */
  6. #include "qemu/osdep.h"
  7. #include "qemu/error-report.h"
  8. #include "qapi/error.h"
  9. #include "hw/virtio/virtio-input.h"
  10. static int vhost_input_config_change(struct vhost_dev *dev)
  11. {
  12. error_report("vhost-user-input: unhandled backend config change");
  13. return -1;
  14. }
  15. static const VhostDevConfigOps config_ops = {
  16. .vhost_dev_config_notifier = vhost_input_config_change,
  17. };
  18. static void vhost_input_realize(DeviceState *dev, Error **errp)
  19. {
  20. VHostUserInput *vhi = VHOST_USER_INPUT(dev);
  21. VirtIOInput *vinput = VIRTIO_INPUT(dev);
  22. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  23. vhost_dev_set_config_notifier(&vhi->vhost->dev, &config_ops);
  24. vinput->cfg_size = sizeof_field(virtio_input_config, u);
  25. if (vhost_user_backend_dev_init(vhi->vhost, vdev, 2, errp) == -1) {
  26. return;
  27. }
  28. }
  29. static void vhost_input_change_active(VirtIOInput *vinput)
  30. {
  31. VHostUserInput *vhi = VHOST_USER_INPUT(vinput);
  32. if (vinput->active) {
  33. vhost_user_backend_start(vhi->vhost);
  34. } else {
  35. vhost_user_backend_stop(vhi->vhost);
  36. }
  37. }
  38. static void vhost_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
  39. {
  40. VirtIOInput *vinput = VIRTIO_INPUT(vdev);
  41. VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
  42. Error *local_err = NULL;
  43. int ret;
  44. memset(config_data, 0, vinput->cfg_size);
  45. ret = vhost_dev_get_config(&vhi->vhost->dev, config_data, vinput->cfg_size,
  46. &local_err);
  47. if (ret) {
  48. error_report_err(local_err);
  49. return;
  50. }
  51. }
  52. static void vhost_input_set_config(VirtIODevice *vdev,
  53. const uint8_t *config_data)
  54. {
  55. VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
  56. int ret;
  57. ret = vhost_dev_set_config(&vhi->vhost->dev, config_data,
  58. 0, sizeof(virtio_input_config),
  59. VHOST_SET_CONFIG_TYPE_MASTER);
  60. if (ret) {
  61. error_report("vhost-user-input: set device config space failed");
  62. return;
  63. }
  64. virtio_notify_config(vdev);
  65. }
  66. static struct vhost_dev *vhost_input_get_vhost(VirtIODevice *vdev)
  67. {
  68. VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
  69. return &vhi->vhost->dev;
  70. }
  71. static const VMStateDescription vmstate_vhost_input = {
  72. .name = "vhost-user-input",
  73. .unmigratable = 1,
  74. };
  75. static void vhost_input_class_init(ObjectClass *klass, void *data)
  76. {
  77. VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
  78. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  79. DeviceClass *dc = DEVICE_CLASS(klass);
  80. dc->vmsd = &vmstate_vhost_input;
  81. vdc->get_config = vhost_input_get_config;
  82. vdc->set_config = vhost_input_set_config;
  83. vdc->get_vhost = vhost_input_get_vhost;
  84. vic->realize = vhost_input_realize;
  85. vic->change_active = vhost_input_change_active;
  86. }
  87. static void vhost_input_init(Object *obj)
  88. {
  89. VHostUserInput *vhi = VHOST_USER_INPUT(obj);
  90. vhi->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
  91. object_property_add_alias(obj, "chardev",
  92. OBJECT(vhi->vhost), "chardev");
  93. }
  94. static void vhost_input_finalize(Object *obj)
  95. {
  96. VHostUserInput *vhi = VHOST_USER_INPUT(obj);
  97. object_unref(OBJECT(vhi->vhost));
  98. }
  99. static const TypeInfo vhost_input_info = {
  100. .name = TYPE_VHOST_USER_INPUT,
  101. .parent = TYPE_VIRTIO_INPUT,
  102. .instance_size = sizeof(VHostUserInput),
  103. .instance_init = vhost_input_init,
  104. .instance_finalize = vhost_input_finalize,
  105. .class_init = vhost_input_class_init,
  106. };
  107. static void vhost_input_register_types(void)
  108. {
  109. type_register_static(&vhost_input_info);
  110. }
  111. type_init(vhost_input_register_types)