virtio_input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/virtio.h>
  4. #include <linux/virtio_config.h>
  5. #include <linux/input.h>
  6. #include <linux/slab.h>
  7. #include <linux/dma-mapping.h>
  8. #include <uapi/linux/virtio_ids.h>
  9. #include <uapi/linux/virtio_input.h>
  10. #include <linux/input/mt.h>
  11. struct virtio_input {
  12. struct virtio_device *vdev;
  13. struct input_dev *idev;
  14. char name[64];
  15. char serial[64];
  16. char phys[64];
  17. struct virtqueue *evt, *sts;
  18. __dma_from_device_group_begin();
  19. struct virtio_input_event evts[64];
  20. __dma_from_device_group_end();
  21. spinlock_t lock;
  22. bool ready;
  23. };
  24. static void virtinput_queue_evtbuf(struct virtio_input *vi,
  25. struct virtio_input_event *evtbuf)
  26. {
  27. struct scatterlist sg[1];
  28. sg_init_one(sg, evtbuf, sizeof(*evtbuf));
  29. virtqueue_add_inbuf_cache_clean(vi->evt, sg, 1, evtbuf, GFP_ATOMIC);
  30. }
  31. static void virtinput_recv_events(struct virtqueue *vq)
  32. {
  33. struct virtio_input *vi = vq->vdev->priv;
  34. struct virtio_input_event *event;
  35. unsigned long flags;
  36. unsigned int len;
  37. spin_lock_irqsave(&vi->lock, flags);
  38. if (vi->ready) {
  39. while ((event = virtqueue_get_buf(vi->evt, &len)) != NULL) {
  40. spin_unlock_irqrestore(&vi->lock, flags);
  41. input_event(vi->idev,
  42. le16_to_cpu(event->type),
  43. le16_to_cpu(event->code),
  44. le32_to_cpu(event->value));
  45. spin_lock_irqsave(&vi->lock, flags);
  46. virtinput_queue_evtbuf(vi, event);
  47. }
  48. virtqueue_kick(vq);
  49. }
  50. spin_unlock_irqrestore(&vi->lock, flags);
  51. }
  52. /*
  53. * On error we are losing the status update, which isn't critical as
  54. * this is typically used for stuff like keyboard leds.
  55. */
  56. static int virtinput_send_status(struct virtio_input *vi,
  57. u16 type, u16 code, s32 value)
  58. {
  59. struct virtio_input_event *stsbuf;
  60. struct scatterlist sg[1];
  61. unsigned long flags;
  62. int rc;
  63. /*
  64. * Since 29cc309d8bf1 (HID: hid-multitouch: forward MSC_TIMESTAMP),
  65. * EV_MSC/MSC_TIMESTAMP is added to each before EV_SYN event.
  66. * EV_MSC is configured as INPUT_PASS_TO_ALL.
  67. * In case of touch device:
  68. * BE pass EV_MSC/MSC_TIMESTAMP to FE on receiving event from evdev.
  69. * FE pass EV_MSC/MSC_TIMESTAMP back to BE.
  70. * BE writes EV_MSC/MSC_TIMESTAMP to evdev due to INPUT_PASS_TO_ALL.
  71. * BE receives extra EV_MSC/MSC_TIMESTAMP and pass to FE.
  72. * >>> Each new frame becomes larger and larger.
  73. * Disable EV_MSC/MSC_TIMESTAMP forwarding for MT.
  74. */
  75. if (vi->idev->mt && type == EV_MSC && code == MSC_TIMESTAMP)
  76. return 0;
  77. stsbuf = kzalloc_obj(*stsbuf, GFP_ATOMIC);
  78. if (!stsbuf)
  79. return -ENOMEM;
  80. stsbuf->type = cpu_to_le16(type);
  81. stsbuf->code = cpu_to_le16(code);
  82. stsbuf->value = cpu_to_le32(value);
  83. sg_init_one(sg, stsbuf, sizeof(*stsbuf));
  84. spin_lock_irqsave(&vi->lock, flags);
  85. if (vi->ready) {
  86. rc = virtqueue_add_outbuf(vi->sts, sg, 1, stsbuf, GFP_ATOMIC);
  87. virtqueue_kick(vi->sts);
  88. } else {
  89. rc = -ENODEV;
  90. }
  91. spin_unlock_irqrestore(&vi->lock, flags);
  92. if (rc != 0)
  93. kfree(stsbuf);
  94. return rc;
  95. }
  96. static void virtinput_recv_status(struct virtqueue *vq)
  97. {
  98. struct virtio_input *vi = vq->vdev->priv;
  99. struct virtio_input_event *stsbuf;
  100. unsigned long flags;
  101. unsigned int len;
  102. spin_lock_irqsave(&vi->lock, flags);
  103. while ((stsbuf = virtqueue_get_buf(vi->sts, &len)) != NULL)
  104. kfree(stsbuf);
  105. spin_unlock_irqrestore(&vi->lock, flags);
  106. }
  107. static int virtinput_status(struct input_dev *idev, unsigned int type,
  108. unsigned int code, int value)
  109. {
  110. struct virtio_input *vi = input_get_drvdata(idev);
  111. return virtinput_send_status(vi, type, code, value);
  112. }
  113. static u8 virtinput_cfg_select(struct virtio_input *vi,
  114. u8 select, u8 subsel)
  115. {
  116. u8 size;
  117. virtio_cwrite_le(vi->vdev, struct virtio_input_config, select, &select);
  118. virtio_cwrite_le(vi->vdev, struct virtio_input_config, subsel, &subsel);
  119. virtio_cread_le(vi->vdev, struct virtio_input_config, size, &size);
  120. return size;
  121. }
  122. static void virtinput_cfg_bits(struct virtio_input *vi, int select, int subsel,
  123. unsigned long *bits, unsigned int bitcount)
  124. {
  125. unsigned int bit;
  126. u8 *virtio_bits;
  127. u8 bytes;
  128. bytes = virtinput_cfg_select(vi, select, subsel);
  129. if (!bytes)
  130. return;
  131. if (bitcount > bytes * 8)
  132. bitcount = bytes * 8;
  133. /*
  134. * Bitmap in virtio config space is a simple stream of bytes,
  135. * with the first byte carrying bits 0-7, second bits 8-15 and
  136. * so on.
  137. */
  138. virtio_bits = kzalloc(bytes, GFP_KERNEL);
  139. if (!virtio_bits)
  140. return;
  141. virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
  142. u.bitmap),
  143. virtio_bits, bytes);
  144. for (bit = 0; bit < bitcount; bit++) {
  145. if (virtio_bits[bit / 8] & (1 << (bit % 8)))
  146. __set_bit(bit, bits);
  147. }
  148. kfree(virtio_bits);
  149. if (select == VIRTIO_INPUT_CFG_EV_BITS)
  150. __set_bit(subsel, vi->idev->evbit);
  151. }
  152. static void virtinput_cfg_abs(struct virtio_input *vi, int abs)
  153. {
  154. u32 mi, ma, re, fu, fl;
  155. virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ABS_INFO, abs);
  156. virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.min, &mi);
  157. virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.max, &ma);
  158. virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.res, &re);
  159. virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.fuzz, &fu);
  160. virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.flat, &fl);
  161. input_set_abs_params(vi->idev, abs, mi, ma, fu, fl);
  162. input_abs_set_res(vi->idev, abs, re);
  163. }
  164. static int virtinput_init_vqs(struct virtio_input *vi)
  165. {
  166. struct virtqueue_info vqs_info[] = {
  167. { "events", virtinput_recv_events },
  168. { "status", virtinput_recv_status },
  169. };
  170. struct virtqueue *vqs[2];
  171. int err;
  172. err = virtio_find_vqs(vi->vdev, 2, vqs, vqs_info, NULL);
  173. if (err)
  174. return err;
  175. vi->evt = vqs[0];
  176. vi->sts = vqs[1];
  177. return 0;
  178. }
  179. static void virtinput_fill_evt(struct virtio_input *vi)
  180. {
  181. unsigned long flags;
  182. int i, size;
  183. spin_lock_irqsave(&vi->lock, flags);
  184. size = virtqueue_get_vring_size(vi->evt);
  185. if (size > ARRAY_SIZE(vi->evts))
  186. size = ARRAY_SIZE(vi->evts);
  187. for (i = 0; i < size; i++)
  188. virtinput_queue_evtbuf(vi, &vi->evts[i]);
  189. virtqueue_kick(vi->evt);
  190. spin_unlock_irqrestore(&vi->lock, flags);
  191. }
  192. static int virtinput_probe(struct virtio_device *vdev)
  193. {
  194. struct virtio_input *vi;
  195. unsigned long flags;
  196. size_t size;
  197. int abs, err, nslots;
  198. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  199. return -ENODEV;
  200. vi = kzalloc_obj(*vi);
  201. if (!vi)
  202. return -ENOMEM;
  203. vdev->priv = vi;
  204. vi->vdev = vdev;
  205. spin_lock_init(&vi->lock);
  206. err = virtinput_init_vqs(vi);
  207. if (err)
  208. goto err_init_vq;
  209. vi->idev = input_allocate_device();
  210. if (!vi->idev) {
  211. err = -ENOMEM;
  212. goto err_input_alloc;
  213. }
  214. input_set_drvdata(vi->idev, vi);
  215. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
  216. virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
  217. u.string),
  218. vi->name, min(size, sizeof(vi->name)));
  219. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_SERIAL, 0);
  220. virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
  221. u.string),
  222. vi->serial, min(size, sizeof(vi->serial)));
  223. snprintf(vi->phys, sizeof(vi->phys),
  224. "virtio%d/input0", vdev->index);
  225. vi->idev->name = vi->name;
  226. vi->idev->phys = vi->phys;
  227. vi->idev->uniq = vi->serial;
  228. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_DEVIDS, 0);
  229. if (size >= sizeof(struct virtio_input_devids)) {
  230. virtio_cread_le(vi->vdev, struct virtio_input_config,
  231. u.ids.bustype, &vi->idev->id.bustype);
  232. virtio_cread_le(vi->vdev, struct virtio_input_config,
  233. u.ids.vendor, &vi->idev->id.vendor);
  234. virtio_cread_le(vi->vdev, struct virtio_input_config,
  235. u.ids.product, &vi->idev->id.product);
  236. virtio_cread_le(vi->vdev, struct virtio_input_config,
  237. u.ids.version, &vi->idev->id.version);
  238. } else {
  239. vi->idev->id.bustype = BUS_VIRTUAL;
  240. }
  241. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_PROP_BITS, 0,
  242. vi->idev->propbit, INPUT_PROP_CNT);
  243. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REP);
  244. if (size)
  245. __set_bit(EV_REP, vi->idev->evbit);
  246. vi->idev->dev.parent = &vdev->dev;
  247. vi->idev->event = virtinput_status;
  248. /* device -> kernel */
  249. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_KEY,
  250. vi->idev->keybit, KEY_CNT);
  251. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REL,
  252. vi->idev->relbit, REL_CNT);
  253. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_ABS,
  254. vi->idev->absbit, ABS_CNT);
  255. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_MSC,
  256. vi->idev->mscbit, MSC_CNT);
  257. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SW,
  258. vi->idev->swbit, SW_CNT);
  259. /* kernel -> device */
  260. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_LED,
  261. vi->idev->ledbit, LED_CNT);
  262. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
  263. vi->idev->sndbit, SND_CNT);
  264. if (test_bit(EV_ABS, vi->idev->evbit)) {
  265. for (abs = 0; abs < ABS_CNT; abs++) {
  266. if (!test_bit(abs, vi->idev->absbit))
  267. continue;
  268. virtinput_cfg_abs(vi, abs);
  269. }
  270. if (test_bit(ABS_MT_SLOT, vi->idev->absbit)) {
  271. nslots = input_abs_get_max(vi->idev, ABS_MT_SLOT) + 1;
  272. err = input_mt_init_slots(vi->idev, nslots, 0);
  273. if (err)
  274. goto err_mt_init_slots;
  275. }
  276. }
  277. virtio_device_ready(vdev);
  278. vi->ready = true;
  279. err = input_register_device(vi->idev);
  280. if (err)
  281. goto err_input_register;
  282. virtinput_fill_evt(vi);
  283. return 0;
  284. err_input_register:
  285. spin_lock_irqsave(&vi->lock, flags);
  286. vi->ready = false;
  287. spin_unlock_irqrestore(&vi->lock, flags);
  288. err_mt_init_slots:
  289. input_free_device(vi->idev);
  290. err_input_alloc:
  291. vdev->config->del_vqs(vdev);
  292. err_init_vq:
  293. kfree(vi);
  294. return err;
  295. }
  296. static void virtinput_remove(struct virtio_device *vdev)
  297. {
  298. struct virtio_input *vi = vdev->priv;
  299. void *buf;
  300. unsigned long flags;
  301. spin_lock_irqsave(&vi->lock, flags);
  302. vi->ready = false;
  303. spin_unlock_irqrestore(&vi->lock, flags);
  304. input_unregister_device(vi->idev);
  305. virtio_reset_device(vdev);
  306. while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
  307. kfree(buf);
  308. vdev->config->del_vqs(vdev);
  309. kfree(vi);
  310. }
  311. #ifdef CONFIG_PM_SLEEP
  312. static int virtinput_freeze(struct virtio_device *vdev)
  313. {
  314. struct virtio_input *vi = vdev->priv;
  315. unsigned long flags;
  316. void *buf;
  317. spin_lock_irqsave(&vi->lock, flags);
  318. vi->ready = false;
  319. spin_unlock_irqrestore(&vi->lock, flags);
  320. virtio_reset_device(vdev);
  321. while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
  322. kfree(buf);
  323. vdev->config->del_vqs(vdev);
  324. return 0;
  325. }
  326. static int virtinput_restore(struct virtio_device *vdev)
  327. {
  328. struct virtio_input *vi = vdev->priv;
  329. int err;
  330. err = virtinput_init_vqs(vi);
  331. if (err)
  332. return err;
  333. virtio_device_ready(vdev);
  334. vi->ready = true;
  335. virtinput_fill_evt(vi);
  336. return 0;
  337. }
  338. #endif
  339. static unsigned int features[] = {
  340. /* none */
  341. };
  342. static const struct virtio_device_id id_table[] = {
  343. { VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
  344. { 0 },
  345. };
  346. static struct virtio_driver virtio_input_driver = {
  347. .driver.name = KBUILD_MODNAME,
  348. .feature_table = features,
  349. .feature_table_size = ARRAY_SIZE(features),
  350. .id_table = id_table,
  351. .probe = virtinput_probe,
  352. .remove = virtinput_remove,
  353. #ifdef CONFIG_PM_SLEEP
  354. .freeze = virtinput_freeze,
  355. .restore = virtinput_restore,
  356. #endif
  357. };
  358. module_virtio_driver(virtio_input_driver);
  359. MODULE_DEVICE_TABLE(virtio, id_table);
  360. MODULE_LICENSE("GPL");
  361. MODULE_DESCRIPTION("Virtio input device driver");
  362. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");