xen-kbdfront.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Xen para-virtual input device
  3. *
  4. * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
  5. * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
  6. *
  7. * Based on linux/drivers/input/mouse/sermouse.c
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file COPYING in the main directory of this archive for
  11. * more details.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/input.h>
  18. #include <linux/slab.h>
  19. #include <asm/xen/hypervisor.h>
  20. #include <xen/xen.h>
  21. #include <xen/events.h>
  22. #include <xen/page.h>
  23. #include <xen/grant_table.h>
  24. #include <xen/interface/grant_table.h>
  25. #include <xen/interface/io/fbif.h>
  26. #include <xen/interface/io/kbdif.h>
  27. #include <xen/xenbus.h>
  28. #include <xen/platform_pci.h>
  29. struct xenkbd_info {
  30. struct input_dev *kbd;
  31. struct input_dev *ptr;
  32. struct xenkbd_page *page;
  33. int gref;
  34. int irq;
  35. struct xenbus_device *xbdev;
  36. char phys[32];
  37. };
  38. static int xenkbd_remove(struct xenbus_device *);
  39. static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
  40. static void xenkbd_disconnect_backend(struct xenkbd_info *);
  41. /*
  42. * Note: if you need to send out events, see xenfb_do_update() for how
  43. * to do that.
  44. */
  45. static irqreturn_t input_handler(int rq, void *dev_id)
  46. {
  47. struct xenkbd_info *info = dev_id;
  48. struct xenkbd_page *page = info->page;
  49. __u32 cons, prod;
  50. prod = page->in_prod;
  51. if (prod == page->in_cons)
  52. return IRQ_HANDLED;
  53. rmb(); /* ensure we see ring contents up to prod */
  54. for (cons = page->in_cons; cons != prod; cons++) {
  55. union xenkbd_in_event *event;
  56. struct input_dev *dev;
  57. event = &XENKBD_IN_RING_REF(page, cons);
  58. dev = info->ptr;
  59. switch (event->type) {
  60. case XENKBD_TYPE_MOTION:
  61. input_report_rel(dev, REL_X, event->motion.rel_x);
  62. input_report_rel(dev, REL_Y, event->motion.rel_y);
  63. if (event->motion.rel_z)
  64. input_report_rel(dev, REL_WHEEL,
  65. -event->motion.rel_z);
  66. break;
  67. case XENKBD_TYPE_KEY:
  68. dev = NULL;
  69. if (test_bit(event->key.keycode, info->kbd->keybit))
  70. dev = info->kbd;
  71. if (test_bit(event->key.keycode, info->ptr->keybit))
  72. dev = info->ptr;
  73. if (dev)
  74. input_report_key(dev, event->key.keycode,
  75. event->key.pressed);
  76. else
  77. pr_warning("unhandled keycode 0x%x\n",
  78. event->key.keycode);
  79. break;
  80. case XENKBD_TYPE_POS:
  81. input_report_abs(dev, ABS_X, event->pos.abs_x);
  82. input_report_abs(dev, ABS_Y, event->pos.abs_y);
  83. if (event->pos.rel_z)
  84. input_report_rel(dev, REL_WHEEL,
  85. -event->pos.rel_z);
  86. break;
  87. }
  88. if (dev)
  89. input_sync(dev);
  90. }
  91. mb(); /* ensure we got ring contents */
  92. page->in_cons = cons;
  93. notify_remote_via_irq(info->irq);
  94. return IRQ_HANDLED;
  95. }
  96. static int xenkbd_probe(struct xenbus_device *dev,
  97. const struct xenbus_device_id *id)
  98. {
  99. int ret, i, abs;
  100. struct xenkbd_info *info;
  101. struct input_dev *kbd, *ptr;
  102. info = kzalloc(sizeof(*info), GFP_KERNEL);
  103. if (!info) {
  104. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  105. return -ENOMEM;
  106. }
  107. dev_set_drvdata(&dev->dev, info);
  108. info->xbdev = dev;
  109. info->irq = -1;
  110. info->gref = -1;
  111. snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
  112. info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  113. if (!info->page)
  114. goto error_nomem;
  115. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-abs-pointer", "%d", &abs) < 0)
  116. abs = 0;
  117. if (abs) {
  118. ret = xenbus_write(XBT_NIL, dev->nodename,
  119. "request-abs-pointer", "1");
  120. if (ret) {
  121. pr_warning("xenkbd: can't request abs-pointer");
  122. abs = 0;
  123. }
  124. }
  125. /* keyboard */
  126. kbd = input_allocate_device();
  127. if (!kbd)
  128. goto error_nomem;
  129. kbd->name = "Xen Virtual Keyboard";
  130. kbd->phys = info->phys;
  131. kbd->id.bustype = BUS_PCI;
  132. kbd->id.vendor = 0x5853;
  133. kbd->id.product = 0xffff;
  134. __set_bit(EV_KEY, kbd->evbit);
  135. for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
  136. __set_bit(i, kbd->keybit);
  137. for (i = KEY_OK; i < KEY_MAX; i++)
  138. __set_bit(i, kbd->keybit);
  139. ret = input_register_device(kbd);
  140. if (ret) {
  141. input_free_device(kbd);
  142. xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
  143. goto error;
  144. }
  145. info->kbd = kbd;
  146. /* pointing device */
  147. ptr = input_allocate_device();
  148. if (!ptr)
  149. goto error_nomem;
  150. ptr->name = "Xen Virtual Pointer";
  151. ptr->phys = info->phys;
  152. ptr->id.bustype = BUS_PCI;
  153. ptr->id.vendor = 0x5853;
  154. ptr->id.product = 0xfffe;
  155. if (abs) {
  156. __set_bit(EV_ABS, ptr->evbit);
  157. input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
  158. input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
  159. } else {
  160. input_set_capability(ptr, EV_REL, REL_X);
  161. input_set_capability(ptr, EV_REL, REL_Y);
  162. }
  163. input_set_capability(ptr, EV_REL, REL_WHEEL);
  164. __set_bit(EV_KEY, ptr->evbit);
  165. for (i = BTN_LEFT; i <= BTN_TASK; i++)
  166. __set_bit(i, ptr->keybit);
  167. ret = input_register_device(ptr);
  168. if (ret) {
  169. input_free_device(ptr);
  170. xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
  171. goto error;
  172. }
  173. info->ptr = ptr;
  174. ret = xenkbd_connect_backend(dev, info);
  175. if (ret < 0)
  176. goto error;
  177. return 0;
  178. error_nomem:
  179. ret = -ENOMEM;
  180. xenbus_dev_fatal(dev, ret, "allocating device memory");
  181. error:
  182. xenkbd_remove(dev);
  183. return ret;
  184. }
  185. static int xenkbd_resume(struct xenbus_device *dev)
  186. {
  187. struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
  188. xenkbd_disconnect_backend(info);
  189. memset(info->page, 0, PAGE_SIZE);
  190. return xenkbd_connect_backend(dev, info);
  191. }
  192. static int xenkbd_remove(struct xenbus_device *dev)
  193. {
  194. struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
  195. xenkbd_disconnect_backend(info);
  196. if (info->kbd)
  197. input_unregister_device(info->kbd);
  198. if (info->ptr)
  199. input_unregister_device(info->ptr);
  200. free_page((unsigned long)info->page);
  201. kfree(info);
  202. return 0;
  203. }
  204. static int xenkbd_connect_backend(struct xenbus_device *dev,
  205. struct xenkbd_info *info)
  206. {
  207. int ret, evtchn;
  208. struct xenbus_transaction xbt;
  209. ret = gnttab_grant_foreign_access(dev->otherend_id,
  210. virt_to_gfn(info->page), 0);
  211. if (ret < 0)
  212. return ret;
  213. info->gref = ret;
  214. ret = xenbus_alloc_evtchn(dev, &evtchn);
  215. if (ret)
  216. goto error_grant;
  217. ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
  218. 0, dev->devicetype, info);
  219. if (ret < 0) {
  220. xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
  221. goto error_evtchan;
  222. }
  223. info->irq = ret;
  224. again:
  225. ret = xenbus_transaction_start(&xbt);
  226. if (ret) {
  227. xenbus_dev_fatal(dev, ret, "starting transaction");
  228. goto error_irqh;
  229. }
  230. ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
  231. virt_to_gfn(info->page));
  232. if (ret)
  233. goto error_xenbus;
  234. ret = xenbus_printf(xbt, dev->nodename, "page-gref", "%u", info->gref);
  235. if (ret)
  236. goto error_xenbus;
  237. ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  238. evtchn);
  239. if (ret)
  240. goto error_xenbus;
  241. ret = xenbus_transaction_end(xbt, 0);
  242. if (ret) {
  243. if (ret == -EAGAIN)
  244. goto again;
  245. xenbus_dev_fatal(dev, ret, "completing transaction");
  246. goto error_irqh;
  247. }
  248. xenbus_switch_state(dev, XenbusStateInitialised);
  249. return 0;
  250. error_xenbus:
  251. xenbus_transaction_end(xbt, 1);
  252. xenbus_dev_fatal(dev, ret, "writing xenstore");
  253. error_irqh:
  254. unbind_from_irqhandler(info->irq, info);
  255. info->irq = -1;
  256. error_evtchan:
  257. xenbus_free_evtchn(dev, evtchn);
  258. error_grant:
  259. gnttab_end_foreign_access(info->gref, 0, 0UL);
  260. info->gref = -1;
  261. return ret;
  262. }
  263. static void xenkbd_disconnect_backend(struct xenkbd_info *info)
  264. {
  265. if (info->irq >= 0)
  266. unbind_from_irqhandler(info->irq, info);
  267. info->irq = -1;
  268. if (info->gref >= 0)
  269. gnttab_end_foreign_access(info->gref, 0, 0UL);
  270. info->gref = -1;
  271. }
  272. static void xenkbd_backend_changed(struct xenbus_device *dev,
  273. enum xenbus_state backend_state)
  274. {
  275. struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
  276. int ret, val;
  277. switch (backend_state) {
  278. case XenbusStateInitialising:
  279. case XenbusStateInitialised:
  280. case XenbusStateReconfiguring:
  281. case XenbusStateReconfigured:
  282. case XenbusStateUnknown:
  283. break;
  284. case XenbusStateInitWait:
  285. InitWait:
  286. ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  287. "feature-abs-pointer", "%d", &val);
  288. if (ret < 0)
  289. val = 0;
  290. if (val) {
  291. ret = xenbus_write(XBT_NIL, info->xbdev->nodename,
  292. "request-abs-pointer", "1");
  293. if (ret)
  294. pr_warning("xenkbd: can't request abs-pointer");
  295. }
  296. xenbus_switch_state(dev, XenbusStateConnected);
  297. break;
  298. case XenbusStateConnected:
  299. /*
  300. * Work around xenbus race condition: If backend goes
  301. * through InitWait to Connected fast enough, we can
  302. * get Connected twice here.
  303. */
  304. if (dev->state != XenbusStateConnected)
  305. goto InitWait; /* no InitWait seen yet, fudge it */
  306. /* Set input abs params to match backend screen res */
  307. if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  308. "width", "%d", &val) > 0)
  309. input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0);
  310. if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
  311. "height", "%d", &val) > 0)
  312. input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0);
  313. break;
  314. case XenbusStateClosed:
  315. if (dev->state == XenbusStateClosed)
  316. break;
  317. /* Missed the backend's CLOSING state -- fallthrough */
  318. case XenbusStateClosing:
  319. xenbus_frontend_closed(dev);
  320. break;
  321. }
  322. }
  323. static const struct xenbus_device_id xenkbd_ids[] = {
  324. { "vkbd" },
  325. { "" }
  326. };
  327. static struct xenbus_driver xenkbd_driver = {
  328. .ids = xenkbd_ids,
  329. .probe = xenkbd_probe,
  330. .remove = xenkbd_remove,
  331. .resume = xenkbd_resume,
  332. .otherend_changed = xenkbd_backend_changed,
  333. };
  334. static int __init xenkbd_init(void)
  335. {
  336. if (!xen_domain())
  337. return -ENODEV;
  338. /* Nothing to do if running in dom0. */
  339. if (xen_initial_domain())
  340. return -ENODEV;
  341. if (!xen_has_pv_devices())
  342. return -ENODEV;
  343. return xenbus_register_frontend(&xenkbd_driver);
  344. }
  345. static void __exit xenkbd_cleanup(void)
  346. {
  347. xenbus_unregister_driver(&xenkbd_driver);
  348. }
  349. module_init(xenkbd_init);
  350. module_exit(xenkbd_cleanup);
  351. MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
  352. MODULE_LICENSE("GPL");
  353. MODULE_ALIAS("xen:vkbd");