virtio_input.c 11 KB

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