virtio_input.c 10 KB

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