virtio-input.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 "qapi/error.h"
  8. #include "qemu/iov.h"
  9. #include "qemu/module.h"
  10. #include "trace.h"
  11. #include "hw/virtio/virtio.h"
  12. #include "hw/qdev-properties.h"
  13. #include "hw/virtio/virtio-input.h"
  14. #include "standard-headers/linux/input.h"
  15. #define VIRTIO_INPUT_VM_VERSION 1
  16. /* ----------------------------------------------------------------- */
  17. void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
  18. {
  19. VirtQueueElement *elem;
  20. int i, len;
  21. if (!vinput->active) {
  22. return;
  23. }
  24. /* queue up events ... */
  25. if (vinput->qindex == vinput->qsize) {
  26. vinput->qsize++;
  27. vinput->queue = g_realloc(vinput->queue, vinput->qsize *
  28. sizeof(vinput->queue[0]));
  29. }
  30. vinput->queue[vinput->qindex++].event = *event;
  31. /* ... until we see a report sync ... */
  32. if (event->type != cpu_to_le16(EV_SYN) ||
  33. event->code != cpu_to_le16(SYN_REPORT)) {
  34. return;
  35. }
  36. /* ... then check available space ... */
  37. for (i = 0; i < vinput->qindex; i++) {
  38. elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement));
  39. if (!elem) {
  40. while (--i >= 0) {
  41. virtqueue_unpop(vinput->evt, vinput->queue[i].elem, 0);
  42. }
  43. vinput->qindex = 0;
  44. trace_virtio_input_queue_full();
  45. return;
  46. }
  47. vinput->queue[i].elem = elem;
  48. }
  49. /* ... and finally pass them to the guest */
  50. for (i = 0; i < vinput->qindex; i++) {
  51. elem = vinput->queue[i].elem;
  52. len = iov_from_buf(elem->in_sg, elem->in_num,
  53. 0, &vinput->queue[i].event, sizeof(virtio_input_event));
  54. virtqueue_push(vinput->evt, elem, len);
  55. g_free(elem);
  56. }
  57. virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);
  58. vinput->qindex = 0;
  59. }
  60. static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq)
  61. {
  62. /* nothing */
  63. }
  64. static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq)
  65. {
  66. VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
  67. VirtIOInput *vinput = VIRTIO_INPUT(vdev);
  68. virtio_input_event event;
  69. VirtQueueElement *elem;
  70. int len;
  71. for (;;) {
  72. elem = virtqueue_pop(vinput->sts, sizeof(VirtQueueElement));
  73. if (!elem) {
  74. break;
  75. }
  76. memset(&event, 0, sizeof(event));
  77. len = iov_to_buf(elem->out_sg, elem->out_num,
  78. 0, &event, sizeof(event));
  79. if (vic->handle_status) {
  80. vic->handle_status(vinput, &event);
  81. }
  82. virtqueue_push(vinput->sts, elem, len);
  83. g_free(elem);
  84. }
  85. virtio_notify(vdev, vinput->sts);
  86. }
  87. virtio_input_config *virtio_input_find_config(VirtIOInput *vinput,
  88. uint8_t select,
  89. uint8_t subsel)
  90. {
  91. VirtIOInputConfig *cfg;
  92. QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
  93. if (select == cfg->config.select &&
  94. subsel == cfg->config.subsel) {
  95. return &cfg->config;
  96. }
  97. }
  98. return NULL;
  99. }
  100. void virtio_input_add_config(VirtIOInput *vinput,
  101. virtio_input_config *config)
  102. {
  103. VirtIOInputConfig *cfg;
  104. if (virtio_input_find_config(vinput, config->select, config->subsel)) {
  105. /* should not happen */
  106. fprintf(stderr, "%s: duplicate config: %d/%d\n",
  107. __func__, config->select, config->subsel);
  108. abort();
  109. }
  110. cfg = g_new0(VirtIOInputConfig, 1);
  111. cfg->config = *config;
  112. QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node);
  113. }
  114. void virtio_input_init_config(VirtIOInput *vinput,
  115. virtio_input_config *config)
  116. {
  117. int i = 0;
  118. QTAILQ_INIT(&vinput->cfg_list);
  119. while (config[i].select) {
  120. virtio_input_add_config(vinput, config + i);
  121. i++;
  122. }
  123. }
  124. void virtio_input_idstr_config(VirtIOInput *vinput,
  125. uint8_t select, const char *string)
  126. {
  127. virtio_input_config id;
  128. if (!string) {
  129. return;
  130. }
  131. memset(&id, 0, sizeof(id));
  132. id.select = select;
  133. id.size = snprintf(id.u.string, sizeof(id.u.string), "%s", string);
  134. virtio_input_add_config(vinput, &id);
  135. }
  136. static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
  137. {
  138. VirtIOInput *vinput = VIRTIO_INPUT(vdev);
  139. virtio_input_config *config;
  140. config = virtio_input_find_config(vinput, vinput->cfg_select,
  141. vinput->cfg_subsel);
  142. if (config) {
  143. memcpy(config_data, config, vinput->cfg_size);
  144. } else {
  145. memset(config_data, 0, vinput->cfg_size);
  146. }
  147. }
  148. static void virtio_input_set_config(VirtIODevice *vdev,
  149. const uint8_t *config_data)
  150. {
  151. VirtIOInput *vinput = VIRTIO_INPUT(vdev);
  152. virtio_input_config *config = (virtio_input_config *)config_data;
  153. vinput->cfg_select = config->select;
  154. vinput->cfg_subsel = config->subsel;
  155. virtio_notify_config(vdev);
  156. }
  157. static uint64_t virtio_input_get_features(VirtIODevice *vdev, uint64_t f,
  158. Error **errp)
  159. {
  160. return f;
  161. }
  162. static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
  163. {
  164. VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
  165. VirtIOInput *vinput = VIRTIO_INPUT(vdev);
  166. if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
  167. if (!vinput->active) {
  168. vinput->active = true;
  169. if (vic->change_active) {
  170. vic->change_active(vinput);
  171. }
  172. }
  173. }
  174. }
  175. static void virtio_input_reset(VirtIODevice *vdev)
  176. {
  177. VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
  178. VirtIOInput *vinput = VIRTIO_INPUT(vdev);
  179. if (vinput->active) {
  180. vinput->active = false;
  181. if (vic->change_active) {
  182. vic->change_active(vinput);
  183. }
  184. }
  185. }
  186. static int virtio_input_post_load(void *opaque, int version_id)
  187. {
  188. VirtIOInput *vinput = opaque;
  189. VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vinput);
  190. VirtIODevice *vdev = VIRTIO_DEVICE(vinput);
  191. vinput->active = vdev->status & VIRTIO_CONFIG_S_DRIVER_OK;
  192. if (vic->change_active) {
  193. vic->change_active(vinput);
  194. }
  195. return 0;
  196. }
  197. static void virtio_input_device_realize(DeviceState *dev, Error **errp)
  198. {
  199. VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
  200. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  201. VirtIOInput *vinput = VIRTIO_INPUT(dev);
  202. VirtIOInputConfig *cfg;
  203. Error *local_err = NULL;
  204. if (vic->realize) {
  205. vic->realize(dev, &local_err);
  206. if (local_err) {
  207. error_propagate(errp, local_err);
  208. return;
  209. }
  210. }
  211. virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
  212. vinput->serial);
  213. QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
  214. if (vinput->cfg_size < cfg->config.size) {
  215. vinput->cfg_size = cfg->config.size;
  216. }
  217. }
  218. vinput->cfg_size += 8;
  219. assert(vinput->cfg_size <= sizeof(virtio_input_config));
  220. virtio_init(vdev, VIRTIO_ID_INPUT, vinput->cfg_size);
  221. vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
  222. vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
  223. }
  224. static void virtio_input_finalize(Object *obj)
  225. {
  226. VirtIOInput *vinput = VIRTIO_INPUT(obj);
  227. VirtIOInputConfig *cfg, *next;
  228. QTAILQ_FOREACH_SAFE(cfg, &vinput->cfg_list, node, next) {
  229. QTAILQ_REMOVE(&vinput->cfg_list, cfg, node);
  230. g_free(cfg);
  231. }
  232. g_free(vinput->queue);
  233. }
  234. static void virtio_input_device_unrealize(DeviceState *dev)
  235. {
  236. VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
  237. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  238. VirtIOInput *vinput = VIRTIO_INPUT(dev);
  239. if (vic->unrealize) {
  240. vic->unrealize(dev);
  241. }
  242. virtio_delete_queue(vinput->evt);
  243. virtio_delete_queue(vinput->sts);
  244. virtio_cleanup(vdev);
  245. }
  246. static const VMStateDescription vmstate_virtio_input = {
  247. .name = "virtio-input",
  248. .minimum_version_id = VIRTIO_INPUT_VM_VERSION,
  249. .version_id = VIRTIO_INPUT_VM_VERSION,
  250. .fields = (VMStateField[]) {
  251. VMSTATE_VIRTIO_DEVICE,
  252. VMSTATE_END_OF_LIST()
  253. },
  254. .post_load = virtio_input_post_load,
  255. };
  256. static Property virtio_input_properties[] = {
  257. DEFINE_PROP_STRING("serial", VirtIOInput, serial),
  258. DEFINE_PROP_END_OF_LIST(),
  259. };
  260. static void virtio_input_class_init(ObjectClass *klass, void *data)
  261. {
  262. DeviceClass *dc = DEVICE_CLASS(klass);
  263. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  264. device_class_set_props(dc, virtio_input_properties);
  265. dc->vmsd = &vmstate_virtio_input;
  266. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  267. vdc->realize = virtio_input_device_realize;
  268. vdc->unrealize = virtio_input_device_unrealize;
  269. vdc->get_config = virtio_input_get_config;
  270. vdc->set_config = virtio_input_set_config;
  271. vdc->get_features = virtio_input_get_features;
  272. vdc->set_status = virtio_input_set_status;
  273. vdc->reset = virtio_input_reset;
  274. }
  275. static const TypeInfo virtio_input_info = {
  276. .name = TYPE_VIRTIO_INPUT,
  277. .parent = TYPE_VIRTIO_DEVICE,
  278. .instance_size = sizeof(VirtIOInput),
  279. .class_size = sizeof(VirtIOInputClass),
  280. .class_init = virtio_input_class_init,
  281. .abstract = true,
  282. .instance_finalize = virtio_input_finalize,
  283. };
  284. /* ----------------------------------------------------------------- */
  285. static void virtio_register_types(void)
  286. {
  287. type_register_static(&virtio_input_info);
  288. }
  289. type_init(virtio_register_types)