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 *vqs[2];
  164. vq_callback_t *cbs[] = { virtinput_recv_events,
  165. virtinput_recv_status };
  166. static const char * const names[] = { "events", "status" };
  167. int err;
  168. err = virtio_find_vqs(vi->vdev, 2, vqs, cbs, names, NULL);
  169. if (err)
  170. return err;
  171. vi->evt = vqs[0];
  172. vi->sts = vqs[1];
  173. return 0;
  174. }
  175. static void virtinput_fill_evt(struct virtio_input *vi)
  176. {
  177. unsigned long flags;
  178. int i, size;
  179. spin_lock_irqsave(&vi->lock, flags);
  180. size = virtqueue_get_vring_size(vi->evt);
  181. if (size > ARRAY_SIZE(vi->evts))
  182. size = ARRAY_SIZE(vi->evts);
  183. for (i = 0; i < size; i++)
  184. virtinput_queue_evtbuf(vi, &vi->evts[i]);
  185. virtqueue_kick(vi->evt);
  186. spin_unlock_irqrestore(&vi->lock, flags);
  187. }
  188. static int virtinput_probe(struct virtio_device *vdev)
  189. {
  190. struct virtio_input *vi;
  191. unsigned long flags;
  192. size_t size;
  193. int abs, err, nslots;
  194. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  195. return -ENODEV;
  196. vi = kzalloc(sizeof(*vi), GFP_KERNEL);
  197. if (!vi)
  198. return -ENOMEM;
  199. vdev->priv = vi;
  200. vi->vdev = vdev;
  201. spin_lock_init(&vi->lock);
  202. err = virtinput_init_vqs(vi);
  203. if (err)
  204. goto err_init_vq;
  205. vi->idev = input_allocate_device();
  206. if (!vi->idev) {
  207. err = -ENOMEM;
  208. goto err_input_alloc;
  209. }
  210. input_set_drvdata(vi->idev, vi);
  211. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
  212. virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
  213. u.string),
  214. vi->name, min(size, sizeof(vi->name)));
  215. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_SERIAL, 0);
  216. virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
  217. u.string),
  218. vi->serial, min(size, sizeof(vi->serial)));
  219. snprintf(vi->phys, sizeof(vi->phys),
  220. "virtio%d/input0", vdev->index);
  221. vi->idev->name = vi->name;
  222. vi->idev->phys = vi->phys;
  223. vi->idev->uniq = vi->serial;
  224. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_DEVIDS, 0);
  225. if (size >= sizeof(struct virtio_input_devids)) {
  226. virtio_cread_le(vi->vdev, struct virtio_input_config,
  227. u.ids.bustype, &vi->idev->id.bustype);
  228. virtio_cread_le(vi->vdev, struct virtio_input_config,
  229. u.ids.vendor, &vi->idev->id.vendor);
  230. virtio_cread_le(vi->vdev, struct virtio_input_config,
  231. u.ids.product, &vi->idev->id.product);
  232. virtio_cread_le(vi->vdev, struct virtio_input_config,
  233. u.ids.version, &vi->idev->id.version);
  234. } else {
  235. vi->idev->id.bustype = BUS_VIRTUAL;
  236. }
  237. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_PROP_BITS, 0,
  238. vi->idev->propbit, INPUT_PROP_CNT);
  239. size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REP);
  240. if (size)
  241. __set_bit(EV_REP, vi->idev->evbit);
  242. vi->idev->dev.parent = &vdev->dev;
  243. vi->idev->event = virtinput_status;
  244. /* device -> kernel */
  245. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_KEY,
  246. vi->idev->keybit, KEY_CNT);
  247. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REL,
  248. vi->idev->relbit, REL_CNT);
  249. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_ABS,
  250. vi->idev->absbit, ABS_CNT);
  251. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_MSC,
  252. vi->idev->mscbit, MSC_CNT);
  253. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SW,
  254. vi->idev->swbit, SW_CNT);
  255. /* kernel -> device */
  256. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_LED,
  257. vi->idev->ledbit, LED_CNT);
  258. virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
  259. vi->idev->sndbit, SND_CNT);
  260. if (test_bit(EV_ABS, vi->idev->evbit)) {
  261. for (abs = 0; abs < ABS_CNT; abs++) {
  262. if (!test_bit(abs, vi->idev->absbit))
  263. continue;
  264. virtinput_cfg_abs(vi, abs);
  265. }
  266. if (test_bit(ABS_MT_SLOT, vi->idev->absbit)) {
  267. nslots = input_abs_get_max(vi->idev, ABS_MT_SLOT) + 1;
  268. err = input_mt_init_slots(vi->idev, nslots, 0);
  269. if (err)
  270. goto err_mt_init_slots;
  271. }
  272. }
  273. virtio_device_ready(vdev);
  274. vi->ready = true;
  275. err = input_register_device(vi->idev);
  276. if (err)
  277. goto err_input_register;
  278. virtinput_fill_evt(vi);
  279. return 0;
  280. err_input_register:
  281. spin_lock_irqsave(&vi->lock, flags);
  282. vi->ready = false;
  283. spin_unlock_irqrestore(&vi->lock, flags);
  284. err_mt_init_slots:
  285. input_free_device(vi->idev);
  286. err_input_alloc:
  287. vdev->config->del_vqs(vdev);
  288. err_init_vq:
  289. kfree(vi);
  290. return err;
  291. }
  292. static void virtinput_remove(struct virtio_device *vdev)
  293. {
  294. struct virtio_input *vi = vdev->priv;
  295. void *buf;
  296. unsigned long flags;
  297. spin_lock_irqsave(&vi->lock, flags);
  298. vi->ready = false;
  299. spin_unlock_irqrestore(&vi->lock, flags);
  300. input_unregister_device(vi->idev);
  301. virtio_reset_device(vdev);
  302. while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
  303. kfree(buf);
  304. vdev->config->del_vqs(vdev);
  305. kfree(vi);
  306. }
  307. #ifdef CONFIG_PM_SLEEP
  308. static int virtinput_freeze(struct virtio_device *vdev)
  309. {
  310. struct virtio_input *vi = vdev->priv;
  311. unsigned long flags;
  312. spin_lock_irqsave(&vi->lock, flags);
  313. vi->ready = false;
  314. spin_unlock_irqrestore(&vi->lock, flags);
  315. vdev->config->del_vqs(vdev);
  316. return 0;
  317. }
  318. static int virtinput_restore(struct virtio_device *vdev)
  319. {
  320. struct virtio_input *vi = vdev->priv;
  321. int err;
  322. err = virtinput_init_vqs(vi);
  323. if (err)
  324. return err;
  325. virtio_device_ready(vdev);
  326. vi->ready = true;
  327. virtinput_fill_evt(vi);
  328. return 0;
  329. }
  330. #endif
  331. static unsigned int features[] = {
  332. /* none */
  333. };
  334. static const struct virtio_device_id id_table[] = {
  335. { VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
  336. { 0 },
  337. };
  338. static struct virtio_driver virtio_input_driver = {
  339. .driver.name = KBUILD_MODNAME,
  340. .driver.owner = THIS_MODULE,
  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 <[email protected]>");