virtio-input-host.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/module.h"
  9. #include "qemu/sockets.h"
  10. #include "hw/virtio/virtio.h"
  11. #include "hw/qdev-properties.h"
  12. #include "hw/virtio/virtio-input.h"
  13. #include <sys/ioctl.h>
  14. #include "standard-headers/linux/input.h"
  15. /* ----------------------------------------------------------------- */
  16. static struct virtio_input_config virtio_input_host_config[] = {
  17. { /* empty list */ },
  18. };
  19. static void virtio_input_host_event(void *opaque)
  20. {
  21. VirtIOInputHost *vih = opaque;
  22. VirtIOInput *vinput = VIRTIO_INPUT(vih);
  23. struct virtio_input_event virtio;
  24. struct input_event evdev;
  25. int rc;
  26. for (;;) {
  27. rc = read(vih->fd, &evdev, sizeof(evdev));
  28. if (rc != sizeof(evdev)) {
  29. break;
  30. }
  31. virtio.type = cpu_to_le16(evdev.type);
  32. virtio.code = cpu_to_le16(evdev.code);
  33. virtio.value = cpu_to_le32(evdev.value);
  34. virtio_input_send(vinput, &virtio);
  35. }
  36. }
  37. static void virtio_input_bits_config(VirtIOInputHost *vih,
  38. int type, int count)
  39. {
  40. virtio_input_config bits;
  41. int rc, i, size = 0;
  42. memset(&bits, 0, sizeof(bits));
  43. rc = ioctl(vih->fd, EVIOCGBIT(type, count/8), bits.u.bitmap);
  44. if (rc < 0) {
  45. return;
  46. }
  47. for (i = 0; i < count/8; i++) {
  48. if (bits.u.bitmap[i]) {
  49. size = i+1;
  50. }
  51. }
  52. if (size == 0) {
  53. return;
  54. }
  55. bits.select = VIRTIO_INPUT_CFG_EV_BITS;
  56. bits.subsel = type;
  57. bits.size = size;
  58. virtio_input_add_config(VIRTIO_INPUT(vih), &bits);
  59. }
  60. static void virtio_input_abs_config(VirtIOInputHost *vih, int axis)
  61. {
  62. virtio_input_config config;
  63. struct input_absinfo absinfo;
  64. int rc;
  65. rc = ioctl(vih->fd, EVIOCGABS(axis), &absinfo);
  66. if (rc < 0) {
  67. return;
  68. }
  69. memset(&config, 0, sizeof(config));
  70. config.select = VIRTIO_INPUT_CFG_ABS_INFO;
  71. config.subsel = axis;
  72. config.size = sizeof(virtio_input_absinfo);
  73. config.u.abs.min = cpu_to_le32(absinfo.minimum);
  74. config.u.abs.max = cpu_to_le32(absinfo.maximum);
  75. config.u.abs.fuzz = cpu_to_le32(absinfo.fuzz);
  76. config.u.abs.flat = cpu_to_le32(absinfo.flat);
  77. config.u.abs.res = cpu_to_le32(absinfo.resolution);
  78. virtio_input_add_config(VIRTIO_INPUT(vih), &config);
  79. }
  80. static void virtio_input_host_realize(DeviceState *dev, Error **errp)
  81. {
  82. VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
  83. VirtIOInput *vinput = VIRTIO_INPUT(dev);
  84. virtio_input_config id, *abs;
  85. struct input_id ids;
  86. int rc, ver, i, axis;
  87. uint8_t byte;
  88. if (!vih->evdev) {
  89. error_setg(errp, "evdev property is required");
  90. return;
  91. }
  92. vih->fd = open(vih->evdev, O_RDWR);
  93. if (vih->fd < 0) {
  94. error_setg_file_open(errp, errno, vih->evdev);
  95. return;
  96. }
  97. qemu_set_nonblock(vih->fd);
  98. rc = ioctl(vih->fd, EVIOCGVERSION, &ver);
  99. if (rc < 0) {
  100. error_setg(errp, "%s: is not an evdev device", vih->evdev);
  101. goto err_close;
  102. }
  103. rc = ioctl(vih->fd, EVIOCGRAB, 1);
  104. if (rc < 0) {
  105. error_setg_errno(errp, errno, "%s: failed to get exclusive access",
  106. vih->evdev);
  107. goto err_close;
  108. }
  109. memset(&id, 0, sizeof(id));
  110. ioctl(vih->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string);
  111. id.select = VIRTIO_INPUT_CFG_ID_NAME;
  112. id.size = strlen(id.u.string);
  113. virtio_input_add_config(vinput, &id);
  114. if (ioctl(vih->fd, EVIOCGID, &ids) == 0) {
  115. memset(&id, 0, sizeof(id));
  116. id.select = VIRTIO_INPUT_CFG_ID_DEVIDS;
  117. id.size = sizeof(struct virtio_input_devids);
  118. id.u.ids.bustype = cpu_to_le16(ids.bustype);
  119. id.u.ids.vendor = cpu_to_le16(ids.vendor);
  120. id.u.ids.product = cpu_to_le16(ids.product);
  121. id.u.ids.version = cpu_to_le16(ids.version);
  122. virtio_input_add_config(vinput, &id);
  123. }
  124. virtio_input_bits_config(vih, EV_KEY, KEY_CNT);
  125. virtio_input_bits_config(vih, EV_REL, REL_CNT);
  126. virtio_input_bits_config(vih, EV_ABS, ABS_CNT);
  127. virtio_input_bits_config(vih, EV_MSC, MSC_CNT);
  128. virtio_input_bits_config(vih, EV_SW, SW_CNT);
  129. virtio_input_bits_config(vih, EV_LED, LED_CNT);
  130. abs = virtio_input_find_config(VIRTIO_INPUT(vih),
  131. VIRTIO_INPUT_CFG_EV_BITS, EV_ABS);
  132. if (abs) {
  133. for (i = 0; i < abs->size; i++) {
  134. byte = abs->u.bitmap[i];
  135. axis = 8 * i;
  136. while (byte) {
  137. if (byte & 1) {
  138. virtio_input_abs_config(vih, axis);
  139. }
  140. axis++;
  141. byte >>= 1;
  142. }
  143. }
  144. }
  145. qemu_set_fd_handler(vih->fd, virtio_input_host_event, NULL, vih);
  146. return;
  147. err_close:
  148. close(vih->fd);
  149. vih->fd = -1;
  150. return;
  151. }
  152. static void virtio_input_host_unrealize(DeviceState *dev, Error **errp)
  153. {
  154. VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
  155. if (vih->fd > 0) {
  156. qemu_set_fd_handler(vih->fd, NULL, NULL, NULL);
  157. close(vih->fd);
  158. }
  159. }
  160. static void virtio_input_host_handle_status(VirtIOInput *vinput,
  161. virtio_input_event *event)
  162. {
  163. VirtIOInputHost *vih = VIRTIO_INPUT_HOST(vinput);
  164. struct input_event evdev;
  165. int rc;
  166. if (gettimeofday(&evdev.time, NULL)) {
  167. perror("virtio_input_host_handle_status: gettimeofday");
  168. return;
  169. }
  170. evdev.type = le16_to_cpu(event->type);
  171. evdev.code = le16_to_cpu(event->code);
  172. evdev.value = le32_to_cpu(event->value);
  173. rc = write(vih->fd, &evdev, sizeof(evdev));
  174. if (rc == -1) {
  175. perror("virtio_input_host_handle_status: write");
  176. }
  177. }
  178. static const VMStateDescription vmstate_virtio_input_host = {
  179. .name = "virtio-input-host",
  180. .unmigratable = 1,
  181. };
  182. static Property virtio_input_host_properties[] = {
  183. DEFINE_PROP_STRING("evdev", VirtIOInputHost, evdev),
  184. DEFINE_PROP_END_OF_LIST(),
  185. };
  186. static void virtio_input_host_class_init(ObjectClass *klass, void *data)
  187. {
  188. VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
  189. DeviceClass *dc = DEVICE_CLASS(klass);
  190. dc->vmsd = &vmstate_virtio_input_host;
  191. dc->props = virtio_input_host_properties;
  192. vic->realize = virtio_input_host_realize;
  193. vic->unrealize = virtio_input_host_unrealize;
  194. vic->handle_status = virtio_input_host_handle_status;
  195. }
  196. static void virtio_input_host_init(Object *obj)
  197. {
  198. VirtIOInput *vinput = VIRTIO_INPUT(obj);
  199. virtio_input_init_config(vinput, virtio_input_host_config);
  200. }
  201. static const TypeInfo virtio_input_host_info = {
  202. .name = TYPE_VIRTIO_INPUT_HOST,
  203. .parent = TYPE_VIRTIO_INPUT,
  204. .instance_size = sizeof(VirtIOInputHost),
  205. .instance_init = virtio_input_host_init,
  206. .class_init = virtio_input_host_class_init,
  207. };
  208. /* ----------------------------------------------------------------- */
  209. static void virtio_register_types(void)
  210. {
  211. type_register_static(&virtio_input_host_info);
  212. }
  213. type_init(virtio_register_types)