2
0

main.c 11 KB

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