xen-kbdfront.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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/input/mt.h>
  19. #include <linux/slab.h>
  20. #include <asm/xen/hypervisor.h>
  21. #include <xen/xen.h>
  22. #include <xen/events.h>
  23. #include <xen/page.h>
  24. #include <xen/grant_table.h>
  25. #include <xen/interface/grant_table.h>
  26. #include <xen/interface/io/fbif.h>
  27. #include <xen/interface/io/kbdif.h>
  28. #include <xen/xenbus.h>
  29. #include <xen/platform_pci.h>
  30. struct xenkbd_info {
  31. struct input_dev *kbd;
  32. struct input_dev *ptr;
  33. struct input_dev *mtouch;
  34. struct xenkbd_page *page;
  35. int gref;
  36. int irq;
  37. struct xenbus_device *xbdev;
  38. char phys[32];
  39. /* current MT slot/contact ID we are injecting events in */
  40. int mtouch_cur_contact_id;
  41. };
  42. enum { KPARAM_X, KPARAM_Y, KPARAM_CNT };
  43. static int ptr_size[KPARAM_CNT] = { XENFB_WIDTH, XENFB_HEIGHT };
  44. module_param_array(ptr_size, int, NULL, 0444);
  45. MODULE_PARM_DESC(ptr_size,
  46. "Pointing device width, height in pixels (default 800,600)");
  47. static int xenkbd_remove(struct xenbus_device *);
  48. static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
  49. static void xenkbd_disconnect_backend(struct xenkbd_info *);
  50. /*
  51. * Note: if you need to send out events, see xenfb_do_update() for how
  52. * to do that.
  53. */
  54. static void xenkbd_handle_motion_event(struct xenkbd_info *info,
  55. struct xenkbd_motion *motion)
  56. {
  57. input_report_rel(info->ptr, REL_X, motion->rel_x);
  58. input_report_rel(info->ptr, REL_Y, motion->rel_y);
  59. if (motion->rel_z)
  60. input_report_rel(info->ptr, REL_WHEEL, -motion->rel_z);
  61. input_sync(info->ptr);
  62. }
  63. static void xenkbd_handle_position_event(struct xenkbd_info *info,
  64. struct xenkbd_position *pos)
  65. {
  66. input_report_abs(info->ptr, ABS_X, pos->abs_x);
  67. input_report_abs(info->ptr, ABS_Y, pos->abs_y);
  68. if (pos->rel_z)
  69. input_report_rel(info->ptr, REL_WHEEL, -pos->rel_z);
  70. input_sync(info->ptr);
  71. }
  72. static void xenkbd_handle_key_event(struct xenkbd_info *info,
  73. struct xenkbd_key *key)
  74. {
  75. struct input_dev *dev;
  76. int value = key->pressed;
  77. if (test_bit(key->keycode, info->ptr->keybit)) {
  78. dev = info->ptr;
  79. } else if (test_bit(key->keycode, info->kbd->keybit)) {
  80. dev = info->kbd;
  81. if (key->pressed && test_bit(key->keycode, info->kbd->key))
  82. value = 2; /* Mark as autorepeat */
  83. } else {
  84. pr_warn("unhandled keycode 0x%x\n", key->keycode);
  85. return;
  86. }
  87. input_event(dev, EV_KEY, key->keycode, value);
  88. input_sync(dev);
  89. }
  90. static void xenkbd_handle_mt_event(struct xenkbd_info *info,
  91. struct xenkbd_mtouch *mtouch)
  92. {
  93. if (unlikely(!info->mtouch))
  94. return;
  95. if (mtouch->contact_id != info->mtouch_cur_contact_id) {
  96. info->mtouch_cur_contact_id = mtouch->contact_id;
  97. input_mt_slot(info->mtouch, mtouch->contact_id);
  98. }
  99. switch (mtouch->event_type) {
  100. case XENKBD_MT_EV_DOWN:
  101. input_mt_report_slot_state(info->mtouch, MT_TOOL_FINGER, true);
  102. /* fall through */
  103. case XENKBD_MT_EV_MOTION:
  104. input_report_abs(info->mtouch, ABS_MT_POSITION_X,
  105. mtouch->u.pos.abs_x);
  106. input_report_abs(info->mtouch, ABS_MT_POSITION_Y,
  107. mtouch->u.pos.abs_y);
  108. break;
  109. case XENKBD_MT_EV_SHAPE:
  110. input_report_abs(info->mtouch, ABS_MT_TOUCH_MAJOR,
  111. mtouch->u.shape.major);
  112. input_report_abs(info->mtouch, ABS_MT_TOUCH_MINOR,
  113. mtouch->u.shape.minor);
  114. break;
  115. case XENKBD_MT_EV_ORIENT:
  116. input_report_abs(info->mtouch, ABS_MT_ORIENTATION,
  117. mtouch->u.orientation);
  118. break;
  119. case XENKBD_MT_EV_UP:
  120. input_mt_report_slot_state(info->mtouch, MT_TOOL_FINGER, false);
  121. break;
  122. case XENKBD_MT_EV_SYN:
  123. input_mt_sync_frame(info->mtouch);
  124. input_sync(info->mtouch);
  125. break;
  126. }
  127. }
  128. static void xenkbd_handle_event(struct xenkbd_info *info,
  129. union xenkbd_in_event *event)
  130. {
  131. switch (event->type) {
  132. case XENKBD_TYPE_MOTION:
  133. xenkbd_handle_motion_event(info, &event->motion);
  134. break;
  135. case XENKBD_TYPE_KEY:
  136. xenkbd_handle_key_event(info, &event->key);
  137. break;
  138. case XENKBD_TYPE_POS:
  139. xenkbd_handle_position_event(info, &event->pos);
  140. break;
  141. case XENKBD_TYPE_MTOUCH:
  142. xenkbd_handle_mt_event(info, &event->mtouch);
  143. break;
  144. }
  145. }
  146. static irqreturn_t input_handler(int rq, void *dev_id)
  147. {
  148. struct xenkbd_info *info = dev_id;
  149. struct xenkbd_page *page = info->page;
  150. __u32 cons, prod;
  151. prod = page->in_prod;
  152. if (prod == page->in_cons)
  153. return IRQ_HANDLED;
  154. rmb(); /* ensure we see ring contents up to prod */
  155. for (cons = page->in_cons; cons != prod; cons++)
  156. xenkbd_handle_event(info, &XENKBD_IN_RING_REF(page, cons));
  157. mb(); /* ensure we got ring contents */
  158. page->in_cons = cons;
  159. notify_remote_via_irq(info->irq);
  160. return IRQ_HANDLED;
  161. }
  162. static int xenkbd_probe(struct xenbus_device *dev,
  163. const struct xenbus_device_id *id)
  164. {
  165. int ret, i;
  166. unsigned int abs, touch;
  167. struct xenkbd_info *info;
  168. struct input_dev *kbd, *ptr, *mtouch;
  169. info = kzalloc(sizeof(*info), GFP_KERNEL);
  170. if (!info) {
  171. xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
  172. return -ENOMEM;
  173. }
  174. dev_set_drvdata(&dev->dev, info);
  175. info->xbdev = dev;
  176. info->irq = -1;
  177. info->gref = -1;
  178. snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
  179. info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  180. if (!info->page)
  181. goto error_nomem;
  182. /* Set input abs params to match backend screen res */
  183. abs = xenbus_read_unsigned(dev->otherend,
  184. XENKBD_FIELD_FEAT_ABS_POINTER, 0);
  185. ptr_size[KPARAM_X] = xenbus_read_unsigned(dev->otherend,
  186. XENKBD_FIELD_WIDTH,
  187. ptr_size[KPARAM_X]);
  188. ptr_size[KPARAM_Y] = xenbus_read_unsigned(dev->otherend,
  189. XENKBD_FIELD_HEIGHT,
  190. ptr_size[KPARAM_Y]);
  191. if (abs) {
  192. ret = xenbus_write(XBT_NIL, dev->nodename,
  193. XENKBD_FIELD_REQ_ABS_POINTER, "1");
  194. if (ret) {
  195. pr_warn("xenkbd: can't request abs-pointer\n");
  196. abs = 0;
  197. }
  198. }
  199. touch = xenbus_read_unsigned(dev->nodename,
  200. XENKBD_FIELD_FEAT_MTOUCH, 0);
  201. if (touch) {
  202. ret = xenbus_write(XBT_NIL, dev->nodename,
  203. XENKBD_FIELD_REQ_MTOUCH, "1");
  204. if (ret) {
  205. pr_warn("xenkbd: can't request multi-touch");
  206. touch = 0;
  207. }
  208. }
  209. /* keyboard */
  210. kbd = input_allocate_device();
  211. if (!kbd)
  212. goto error_nomem;
  213. kbd->name = "Xen Virtual Keyboard";
  214. kbd->phys = info->phys;
  215. kbd->id.bustype = BUS_PCI;
  216. kbd->id.vendor = 0x5853;
  217. kbd->id.product = 0xffff;
  218. __set_bit(EV_KEY, kbd->evbit);
  219. for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
  220. __set_bit(i, kbd->keybit);
  221. for (i = KEY_OK; i < KEY_MAX; i++)
  222. __set_bit(i, kbd->keybit);
  223. ret = input_register_device(kbd);
  224. if (ret) {
  225. input_free_device(kbd);
  226. xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
  227. goto error;
  228. }
  229. info->kbd = kbd;
  230. /* pointing device */
  231. ptr = input_allocate_device();
  232. if (!ptr)
  233. goto error_nomem;
  234. ptr->name = "Xen Virtual Pointer";
  235. ptr->phys = info->phys;
  236. ptr->id.bustype = BUS_PCI;
  237. ptr->id.vendor = 0x5853;
  238. ptr->id.product = 0xfffe;
  239. if (abs) {
  240. __set_bit(EV_ABS, ptr->evbit);
  241. input_set_abs_params(ptr, ABS_X, 0, ptr_size[KPARAM_X], 0, 0);
  242. input_set_abs_params(ptr, ABS_Y, 0, ptr_size[KPARAM_Y], 0, 0);
  243. } else {
  244. input_set_capability(ptr, EV_REL, REL_X);
  245. input_set_capability(ptr, EV_REL, REL_Y);
  246. }
  247. input_set_capability(ptr, EV_REL, REL_WHEEL);
  248. __set_bit(EV_KEY, ptr->evbit);
  249. for (i = BTN_LEFT; i <= BTN_TASK; i++)
  250. __set_bit(i, ptr->keybit);
  251. ret = input_register_device(ptr);
  252. if (ret) {
  253. input_free_device(ptr);
  254. xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
  255. goto error;
  256. }
  257. info->ptr = ptr;
  258. /* multi-touch device */
  259. if (touch) {
  260. int num_cont, width, height;
  261. mtouch = input_allocate_device();
  262. if (!mtouch)
  263. goto error_nomem;
  264. num_cont = xenbus_read_unsigned(info->xbdev->nodename,
  265. XENKBD_FIELD_MT_NUM_CONTACTS,
  266. 1);
  267. width = xenbus_read_unsigned(info->xbdev->nodename,
  268. XENKBD_FIELD_MT_WIDTH,
  269. XENFB_WIDTH);
  270. height = xenbus_read_unsigned(info->xbdev->nodename,
  271. XENKBD_FIELD_MT_HEIGHT,
  272. XENFB_HEIGHT);
  273. mtouch->name = "Xen Virtual Multi-touch";
  274. mtouch->phys = info->phys;
  275. mtouch->id.bustype = BUS_PCI;
  276. mtouch->id.vendor = 0x5853;
  277. mtouch->id.product = 0xfffd;
  278. input_set_abs_params(mtouch, ABS_MT_TOUCH_MAJOR,
  279. 0, 255, 0, 0);
  280. input_set_abs_params(mtouch, ABS_MT_POSITION_X,
  281. 0, width, 0, 0);
  282. input_set_abs_params(mtouch, ABS_MT_POSITION_Y,
  283. 0, height, 0, 0);
  284. input_set_abs_params(mtouch, ABS_MT_PRESSURE,
  285. 0, 255, 0, 0);
  286. ret = input_mt_init_slots(mtouch, num_cont, INPUT_MT_DIRECT);
  287. if (ret) {
  288. input_free_device(mtouch);
  289. xenbus_dev_fatal(info->xbdev, ret,
  290. "input_mt_init_slots");
  291. goto error;
  292. }
  293. ret = input_register_device(mtouch);
  294. if (ret) {
  295. input_free_device(mtouch);
  296. xenbus_dev_fatal(info->xbdev, ret,
  297. "input_register_device(mtouch)");
  298. goto error;
  299. }
  300. info->mtouch_cur_contact_id = -1;
  301. info->mtouch = mtouch;
  302. }
  303. ret = xenkbd_connect_backend(dev, info);
  304. if (ret < 0)
  305. goto error;
  306. return 0;
  307. error_nomem:
  308. ret = -ENOMEM;
  309. xenbus_dev_fatal(dev, ret, "allocating device memory");
  310. error:
  311. xenkbd_remove(dev);
  312. return ret;
  313. }
  314. static int xenkbd_resume(struct xenbus_device *dev)
  315. {
  316. struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
  317. xenkbd_disconnect_backend(info);
  318. memset(info->page, 0, PAGE_SIZE);
  319. return xenkbd_connect_backend(dev, info);
  320. }
  321. static int xenkbd_remove(struct xenbus_device *dev)
  322. {
  323. struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
  324. xenkbd_disconnect_backend(info);
  325. if (info->kbd)
  326. input_unregister_device(info->kbd);
  327. if (info->ptr)
  328. input_unregister_device(info->ptr);
  329. if (info->mtouch)
  330. input_unregister_device(info->mtouch);
  331. free_page((unsigned long)info->page);
  332. kfree(info);
  333. return 0;
  334. }
  335. static int xenkbd_connect_backend(struct xenbus_device *dev,
  336. struct xenkbd_info *info)
  337. {
  338. int ret, evtchn;
  339. struct xenbus_transaction xbt;
  340. ret = gnttab_grant_foreign_access(dev->otherend_id,
  341. virt_to_gfn(info->page), 0);
  342. if (ret < 0)
  343. return ret;
  344. info->gref = ret;
  345. ret = xenbus_alloc_evtchn(dev, &evtchn);
  346. if (ret)
  347. goto error_grant;
  348. ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
  349. 0, dev->devicetype, info);
  350. if (ret < 0) {
  351. xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
  352. goto error_evtchan;
  353. }
  354. info->irq = ret;
  355. again:
  356. ret = xenbus_transaction_start(&xbt);
  357. if (ret) {
  358. xenbus_dev_fatal(dev, ret, "starting transaction");
  359. goto error_irqh;
  360. }
  361. ret = xenbus_printf(xbt, dev->nodename, XENKBD_FIELD_RING_REF, "%lu",
  362. virt_to_gfn(info->page));
  363. if (ret)
  364. goto error_xenbus;
  365. ret = xenbus_printf(xbt, dev->nodename, XENKBD_FIELD_RING_GREF,
  366. "%u", info->gref);
  367. if (ret)
  368. goto error_xenbus;
  369. ret = xenbus_printf(xbt, dev->nodename, XENKBD_FIELD_EVT_CHANNEL, "%u",
  370. evtchn);
  371. if (ret)
  372. goto error_xenbus;
  373. ret = xenbus_transaction_end(xbt, 0);
  374. if (ret) {
  375. if (ret == -EAGAIN)
  376. goto again;
  377. xenbus_dev_fatal(dev, ret, "completing transaction");
  378. goto error_irqh;
  379. }
  380. xenbus_switch_state(dev, XenbusStateInitialised);
  381. return 0;
  382. error_xenbus:
  383. xenbus_transaction_end(xbt, 1);
  384. xenbus_dev_fatal(dev, ret, "writing xenstore");
  385. error_irqh:
  386. unbind_from_irqhandler(info->irq, info);
  387. info->irq = -1;
  388. error_evtchan:
  389. xenbus_free_evtchn(dev, evtchn);
  390. error_grant:
  391. gnttab_end_foreign_access(info->gref, 0, 0UL);
  392. info->gref = -1;
  393. return ret;
  394. }
  395. static void xenkbd_disconnect_backend(struct xenkbd_info *info)
  396. {
  397. if (info->irq >= 0)
  398. unbind_from_irqhandler(info->irq, info);
  399. info->irq = -1;
  400. if (info->gref >= 0)
  401. gnttab_end_foreign_access(info->gref, 0, 0UL);
  402. info->gref = -1;
  403. }
  404. static void xenkbd_backend_changed(struct xenbus_device *dev,
  405. enum xenbus_state backend_state)
  406. {
  407. switch (backend_state) {
  408. case XenbusStateInitialising:
  409. case XenbusStateInitialised:
  410. case XenbusStateReconfiguring:
  411. case XenbusStateReconfigured:
  412. case XenbusStateUnknown:
  413. break;
  414. case XenbusStateInitWait:
  415. xenbus_switch_state(dev, XenbusStateConnected);
  416. break;
  417. case XenbusStateConnected:
  418. /*
  419. * Work around xenbus race condition: If backend goes
  420. * through InitWait to Connected fast enough, we can
  421. * get Connected twice here.
  422. */
  423. if (dev->state != XenbusStateConnected)
  424. xenbus_switch_state(dev, XenbusStateConnected);
  425. break;
  426. case XenbusStateClosed:
  427. if (dev->state == XenbusStateClosed)
  428. break;
  429. /* Missed the backend's CLOSING state -- fallthrough */
  430. case XenbusStateClosing:
  431. xenbus_frontend_closed(dev);
  432. break;
  433. }
  434. }
  435. static const struct xenbus_device_id xenkbd_ids[] = {
  436. { XENKBD_DRIVER_NAME },
  437. { "" }
  438. };
  439. static struct xenbus_driver xenkbd_driver = {
  440. .ids = xenkbd_ids,
  441. .probe = xenkbd_probe,
  442. .remove = xenkbd_remove,
  443. .resume = xenkbd_resume,
  444. .otherend_changed = xenkbd_backend_changed,
  445. };
  446. static int __init xenkbd_init(void)
  447. {
  448. if (!xen_domain())
  449. return -ENODEV;
  450. /* Nothing to do if running in dom0. */
  451. if (xen_initial_domain())
  452. return -ENODEV;
  453. if (!xen_has_pv_devices())
  454. return -ENODEV;
  455. return xenbus_register_frontend(&xenkbd_driver);
  456. }
  457. static void __exit xenkbd_cleanup(void)
  458. {
  459. xenbus_unregister_driver(&xenkbd_driver);
  460. }
  461. module_init(xenkbd_init);
  462. module_exit(xenkbd_cleanup);
  463. MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
  464. MODULE_LICENSE("GPL");
  465. MODULE_ALIAS("xen:" XENKBD_DRIVER_NAME);