2
0

main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 <glib.h>
  8. #include <linux/input.h>
  9. #include "qemu/iov.h"
  10. #include "qemu/bswap.h"
  11. #include "qemu/sockets.h"
  12. #include "contrib/libvhost-user/libvhost-user.h"
  13. #include "contrib/libvhost-user/libvhost-user-glib.h"
  14. #include "standard-headers/linux/virtio_input.h"
  15. #include "qapi/error.h"
  16. enum {
  17. VHOST_USER_INPUT_MAX_QUEUES = 2,
  18. };
  19. typedef struct virtio_input_event virtio_input_event;
  20. typedef struct virtio_input_config virtio_input_config;
  21. typedef struct VuInput {
  22. VugDev dev;
  23. GSource *evsrc;
  24. int evdevfd;
  25. GArray *config;
  26. virtio_input_config *sel_config;
  27. struct {
  28. virtio_input_event event;
  29. VuVirtqElement *elem;
  30. } *queue;
  31. uint32_t qindex, qsize;
  32. } VuInput;
  33. static void vi_input_send(VuInput *vi, struct virtio_input_event *event)
  34. {
  35. VuDev *dev = &vi->dev.parent;
  36. VuVirtq *vq = vu_get_queue(dev, 0);
  37. VuVirtqElement *elem;
  38. int i, len;
  39. /* queue up events ... */
  40. if (vi->qindex == vi->qsize) {
  41. vi->qsize++;
  42. vi->queue = g_realloc_n(vi->queue, vi->qsize, sizeof(vi->queue[0]));
  43. }
  44. vi->queue[vi->qindex++].event = *event;
  45. /* ... until we see a report sync ... */
  46. if (event->type != htole16(EV_SYN) ||
  47. event->code != htole16(SYN_REPORT)) {
  48. return;
  49. }
  50. /* ... then check available space ... */
  51. for (i = 0; i < vi->qindex; i++) {
  52. elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
  53. if (!elem) {
  54. while (--i >= 0) {
  55. vu_queue_unpop(dev, vq, vi->queue[i].elem, 0);
  56. }
  57. vi->qindex = 0;
  58. g_warning("virtio-input queue full");
  59. return;
  60. }
  61. vi->queue[i].elem = elem;
  62. }
  63. /* ... and finally pass them to the guest */
  64. for (i = 0; i < vi->qindex; i++) {
  65. elem = vi->queue[i].elem;
  66. len = iov_from_buf(elem->in_sg, elem->in_num,
  67. 0, &vi->queue[i].event, sizeof(virtio_input_event));
  68. vu_queue_push(dev, vq, elem, len);
  69. free(elem);
  70. }
  71. vu_queue_notify(&vi->dev.parent, vq);
  72. vi->qindex = 0;
  73. }
  74. static void
  75. vi_evdev_watch(VuDev *dev, int condition, void *data)
  76. {
  77. VuInput *vi = data;
  78. int fd = vi->evdevfd;
  79. g_debug("Got evdev condition %x", condition);
  80. struct virtio_input_event virtio;
  81. struct input_event evdev;
  82. int rc;
  83. for (;;) {
  84. rc = read(fd, &evdev, sizeof(evdev));
  85. if (rc != sizeof(evdev)) {
  86. break;
  87. }
  88. g_debug("input %d %d %d", evdev.type, evdev.code, evdev.value);
  89. virtio.type = htole16(evdev.type);
  90. virtio.code = htole16(evdev.code);
  91. virtio.value = htole32(evdev.value);
  92. vi_input_send(vi, &virtio);
  93. }
  94. }
  95. static void vi_handle_status(VuInput *vi, virtio_input_event *event)
  96. {
  97. struct input_event evdev;
  98. int rc;
  99. if (gettimeofday(&evdev.time, NULL)) {
  100. perror("vi_handle_status: gettimeofday");
  101. return;
  102. }
  103. evdev.type = le16toh(event->type);
  104. evdev.code = le16toh(event->code);
  105. evdev.value = le32toh(event->value);
  106. rc = write(vi->evdevfd, &evdev, sizeof(evdev));
  107. if (rc == -1) {
  108. perror("vi_host_handle_status: write");
  109. }
  110. }
  111. static void vi_handle_sts(VuDev *dev, int qidx)
  112. {
  113. VuInput *vi = container_of(dev, VuInput, dev.parent);
  114. VuVirtq *vq = vu_get_queue(dev, qidx);
  115. virtio_input_event event;
  116. VuVirtqElement *elem;
  117. int len;
  118. g_debug("%s", G_STRFUNC);
  119. for (;;) {
  120. elem = vu_queue_pop(dev, vq, sizeof(VuVirtqElement));
  121. if (!elem) {
  122. break;
  123. }
  124. memset(&event, 0, sizeof(event));
  125. len = iov_to_buf(elem->out_sg, elem->out_num,
  126. 0, &event, sizeof(event));
  127. vi_handle_status(vi, &event);
  128. vu_queue_push(dev, vq, elem, len);
  129. free(elem);
  130. }
  131. vu_queue_notify(&vi->dev.parent, vq);
  132. }
  133. static void
  134. vi_panic(VuDev *dev, const char *msg)
  135. {
  136. g_critical("%s\n", msg);
  137. exit(EXIT_FAILURE);
  138. }
  139. static void
  140. vi_queue_set_started(VuDev *dev, int qidx, bool started)
  141. {
  142. VuInput *vi = container_of(dev, VuInput, dev.parent);
  143. VuVirtq *vq = vu_get_queue(dev, qidx);
  144. g_debug("queue started %d:%d", qidx, started);
  145. if (qidx == 1) {
  146. vu_set_queue_handler(dev, vq, started ? vi_handle_sts : NULL);
  147. }
  148. started = vu_queue_started(dev, vu_get_queue(dev, 0)) &&
  149. vu_queue_started(dev, vu_get_queue(dev, 1));
  150. if (started && !vi->evsrc) {
  151. vi->evsrc = vug_source_new(&vi->dev, vi->evdevfd,
  152. G_IO_IN, vi_evdev_watch, vi);
  153. }
  154. if (!started && vi->evsrc) {
  155. g_source_destroy(vi->evsrc);
  156. vi->evsrc = NULL;
  157. }
  158. }
  159. static virtio_input_config *
  160. vi_find_config(VuInput *vi, uint8_t select, uint8_t subsel)
  161. {
  162. virtio_input_config *cfg;
  163. int i;
  164. for (i = 0; i < vi->config->len; i++) {
  165. cfg = &g_array_index(vi->config, virtio_input_config, i);
  166. if (select == cfg->select && subsel == cfg->subsel) {
  167. return cfg;
  168. }
  169. }
  170. return NULL;
  171. }
  172. static int vi_get_config(VuDev *dev, uint8_t *config, uint32_t len)
  173. {
  174. VuInput *vi = container_of(dev, VuInput, dev.parent);
  175. g_return_val_if_fail(len <= sizeof(*vi->sel_config), -1);
  176. if (vi->sel_config) {
  177. memcpy(config, vi->sel_config, len);
  178. } else {
  179. memset(config, 0, len);
  180. }
  181. return 0;
  182. }
  183. static int vi_set_config(VuDev *dev, const uint8_t *data,
  184. uint32_t offset, uint32_t size,
  185. uint32_t flags)
  186. {
  187. VuInput *vi = container_of(dev, VuInput, dev.parent);
  188. virtio_input_config *config = (virtio_input_config *)data;
  189. vi->sel_config = vi_find_config(vi, config->select, config->subsel);
  190. return 0;
  191. }
  192. static const VuDevIface vuiface = {
  193. .queue_set_started = vi_queue_set_started,
  194. .get_config = vi_get_config,
  195. .set_config = vi_set_config,
  196. };
  197. static void
  198. vi_bits_config(VuInput *vi, int type, int count)
  199. {
  200. virtio_input_config bits;
  201. int rc, i, size = 0;
  202. memset(&bits, 0, sizeof(bits));
  203. rc = ioctl(vi->evdevfd, EVIOCGBIT(type, count / 8), bits.u.bitmap);
  204. if (rc < 0) {
  205. return;
  206. }
  207. for (i = 0; i < count / 8; i++) {
  208. if (bits.u.bitmap[i]) {
  209. size = i + 1;
  210. }
  211. }
  212. if (size == 0) {
  213. return;
  214. }
  215. bits.select = VIRTIO_INPUT_CFG_EV_BITS;
  216. bits.subsel = type;
  217. bits.size = size;
  218. g_array_append_val(vi->config, bits);
  219. }
  220. static char *opt_evdev;
  221. static int opt_fdnum = -1;
  222. static char *opt_socket_path;
  223. static gboolean opt_nograb;
  224. static gboolean opt_print_caps;
  225. static GOptionEntry entries[] = {
  226. { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &opt_print_caps,
  227. "Print capabilities", NULL },
  228. { "no-grab", 'n', 0, G_OPTION_ARG_NONE, &opt_nograb,
  229. "Don't grab device", NULL },
  230. { "fd", 'f', 0, G_OPTION_ARG_INT, &opt_fdnum,
  231. "Use inherited fd socket", "FDNUM" },
  232. { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &opt_socket_path,
  233. "Use UNIX socket path", "PATH" },
  234. { "evdev-path", 'p', 0, G_OPTION_ARG_FILENAME, &opt_evdev,
  235. "evdev input device path", "PATH" },
  236. { NULL, }
  237. };
  238. int
  239. main(int argc, char *argv[])
  240. {
  241. GMainLoop *loop = NULL;
  242. VuInput vi = { 0, };
  243. int rc, ver, fd;
  244. virtio_input_config id;
  245. struct input_id ids;
  246. GError *error = NULL;
  247. GOptionContext *context;
  248. context = g_option_context_new(NULL);
  249. g_option_context_add_main_entries(context, entries, NULL);
  250. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  251. g_printerr("Option parsing failed: %s\n", error->message);
  252. exit(EXIT_FAILURE);
  253. }
  254. if (opt_print_caps) {
  255. g_print("{\n");
  256. g_print(" \"type\": \"input\",\n");
  257. g_print(" \"features\": [\n");
  258. g_print(" \"evdev-path\",\n");
  259. g_print(" \"no-grab\"\n");
  260. g_print(" ]\n");
  261. g_print("}\n");
  262. exit(EXIT_SUCCESS);
  263. }
  264. if (!opt_evdev) {
  265. g_printerr("Please specify an evdev path\n");
  266. exit(EXIT_FAILURE);
  267. }
  268. if ((!!opt_socket_path + (opt_fdnum != -1)) != 1) {
  269. g_printerr("Please specify either --fd or --socket-path\n");
  270. exit(EXIT_FAILURE);
  271. }
  272. vi.evdevfd = open(opt_evdev, O_RDWR);
  273. if (vi.evdevfd < 0) {
  274. g_printerr("Failed to open evdev: %s\n", g_strerror(errno));
  275. exit(EXIT_FAILURE);
  276. }
  277. rc = ioctl(vi.evdevfd, EVIOCGVERSION, &ver);
  278. if (rc < 0) {
  279. g_printerr("%s: is not an evdev device\n", argv[1]);
  280. exit(EXIT_FAILURE);
  281. }
  282. if (!opt_nograb) {
  283. rc = ioctl(vi.evdevfd, EVIOCGRAB, 1);
  284. if (rc < 0) {
  285. g_printerr("Failed to grab device\n");
  286. exit(EXIT_FAILURE);
  287. }
  288. }
  289. vi.config = g_array_new(false, false, sizeof(virtio_input_config));
  290. memset(&id, 0, sizeof(id));
  291. if (ioctl(vi.evdevfd, EVIOCGNAME(sizeof(id.u.string) - 1),
  292. id.u.string) < 0) {
  293. g_printerr("Failed to get evdev name: %s\n", g_strerror(errno));
  294. exit(EXIT_FAILURE);
  295. }
  296. id.select = VIRTIO_INPUT_CFG_ID_NAME;
  297. id.size = strlen(id.u.string);
  298. g_array_append_val(vi.config, id);
  299. if (ioctl(vi.evdevfd, EVIOCGID, &ids) == 0) {
  300. memset(&id, 0, sizeof(id));
  301. id.select = VIRTIO_INPUT_CFG_ID_DEVIDS;
  302. id.size = sizeof(struct virtio_input_devids);
  303. id.u.ids.bustype = cpu_to_le16(ids.bustype);
  304. id.u.ids.vendor = cpu_to_le16(ids.vendor);
  305. id.u.ids.product = cpu_to_le16(ids.product);
  306. id.u.ids.version = cpu_to_le16(ids.version);
  307. g_array_append_val(vi.config, id);
  308. }
  309. vi_bits_config(&vi, EV_KEY, KEY_CNT);
  310. vi_bits_config(&vi, EV_REL, REL_CNT);
  311. vi_bits_config(&vi, EV_ABS, ABS_CNT);
  312. vi_bits_config(&vi, EV_MSC, MSC_CNT);
  313. vi_bits_config(&vi, EV_SW, SW_CNT);
  314. g_debug("config length: %u", vi.config->len);
  315. if (opt_socket_path) {
  316. int lsock = unix_listen(opt_socket_path, &error_fatal);
  317. if (lsock < 0) {
  318. g_printerr("Failed to listen on %s.\n", opt_socket_path);
  319. exit(EXIT_FAILURE);
  320. }
  321. fd = accept(lsock, NULL, NULL);
  322. close(lsock);
  323. } else {
  324. fd = opt_fdnum;
  325. }
  326. if (fd == -1) {
  327. g_printerr("Invalid vhost-user socket.\n");
  328. exit(EXIT_FAILURE);
  329. }
  330. if (!vug_init(&vi.dev, VHOST_USER_INPUT_MAX_QUEUES, fd, vi_panic,
  331. &vuiface)) {
  332. g_printerr("Failed to initialize libvhost-user-glib.\n");
  333. exit(EXIT_FAILURE);
  334. }
  335. loop = g_main_loop_new(NULL, FALSE);
  336. g_main_loop_run(loop);
  337. g_main_loop_unref(loop);
  338. vug_deinit(&vi.dev);
  339. if (vi.evsrc) {
  340. g_source_unref(vi.evsrc);
  341. }
  342. g_array_free(vi.config, TRUE);
  343. g_free(vi.queue);
  344. return 0;
  345. }