vmmouse.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Driver for Virtual PS/2 Mouse on VMware and QEMU hypervisors.
  3. *
  4. * Copyright (C) 2014, VMware, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * Twin device code is hugely inspired by the ALPS driver.
  11. * Authors:
  12. * Dmitry Torokhov <dmitry.torokhov@gmail.com>
  13. * Thomas Hellstrom <thellstrom@vmware.com>
  14. */
  15. #include <linux/input.h>
  16. #include <linux/serio.h>
  17. #include <linux/libps2.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <asm/hypervisor.h>
  21. #include "psmouse.h"
  22. #include "vmmouse.h"
  23. #define VMMOUSE_PROTO_MAGIC 0x564D5868U
  24. #define VMMOUSE_PROTO_PORT 0x5658
  25. /*
  26. * Main commands supported by the vmmouse hypervisor port.
  27. */
  28. #define VMMOUSE_PROTO_CMD_GETVERSION 10
  29. #define VMMOUSE_PROTO_CMD_ABSPOINTER_DATA 39
  30. #define VMMOUSE_PROTO_CMD_ABSPOINTER_STATUS 40
  31. #define VMMOUSE_PROTO_CMD_ABSPOINTER_COMMAND 41
  32. #define VMMOUSE_PROTO_CMD_ABSPOINTER_RESTRICT 86
  33. /*
  34. * Subcommands for VMMOUSE_PROTO_CMD_ABSPOINTER_COMMAND
  35. */
  36. #define VMMOUSE_CMD_ENABLE 0x45414552U
  37. #define VMMOUSE_CMD_DISABLE 0x000000f5U
  38. #define VMMOUSE_CMD_REQUEST_RELATIVE 0x4c455252U
  39. #define VMMOUSE_CMD_REQUEST_ABSOLUTE 0x53424152U
  40. #define VMMOUSE_ERROR 0xffff0000U
  41. #define VMMOUSE_VERSION_ID 0x3442554aU
  42. #define VMMOUSE_RELATIVE_PACKET 0x00010000U
  43. #define VMMOUSE_LEFT_BUTTON 0x20
  44. #define VMMOUSE_RIGHT_BUTTON 0x10
  45. #define VMMOUSE_MIDDLE_BUTTON 0x08
  46. /*
  47. * VMMouse Restrict command
  48. */
  49. #define VMMOUSE_RESTRICT_ANY 0x00
  50. #define VMMOUSE_RESTRICT_CPL0 0x01
  51. #define VMMOUSE_RESTRICT_IOPL 0x02
  52. #define VMMOUSE_MAX_X 0xFFFF
  53. #define VMMOUSE_MAX_Y 0xFFFF
  54. #define VMMOUSE_VENDOR "VMware"
  55. #define VMMOUSE_NAME "VMMouse"
  56. /**
  57. * struct vmmouse_data - private data structure for the vmmouse driver
  58. *
  59. * @abs_dev: "Absolute" device used to report absolute mouse movement.
  60. * @phys: Physical path for the absolute device.
  61. * @dev_name: Name attribute name for the absolute device.
  62. */
  63. struct vmmouse_data {
  64. struct input_dev *abs_dev;
  65. char phys[32];
  66. char dev_name[128];
  67. };
  68. /**
  69. * Hypervisor-specific bi-directional communication channel
  70. * implementing the vmmouse protocol. Should never execute on
  71. * bare metal hardware.
  72. */
  73. #define VMMOUSE_CMD(cmd, in1, out1, out2, out3, out4) \
  74. ({ \
  75. unsigned long __dummy1, __dummy2; \
  76. __asm__ __volatile__ ("inl %%dx" : \
  77. "=a"(out1), \
  78. "=b"(out2), \
  79. "=c"(out3), \
  80. "=d"(out4), \
  81. "=S"(__dummy1), \
  82. "=D"(__dummy2) : \
  83. "a"(VMMOUSE_PROTO_MAGIC), \
  84. "b"(in1), \
  85. "c"(VMMOUSE_PROTO_CMD_##cmd), \
  86. "d"(VMMOUSE_PROTO_PORT) : \
  87. "memory"); \
  88. })
  89. /**
  90. * vmmouse_report_button - report button state on the correct input device
  91. *
  92. * @psmouse: Pointer to the psmouse struct
  93. * @abs_dev: The absolute input device
  94. * @rel_dev: The relative input device
  95. * @pref_dev: The preferred device for reporting
  96. * @code: Button code
  97. * @value: Button value
  98. *
  99. * Report @value and @code on @pref_dev, unless the button is already
  100. * pressed on the other device, in which case the state is reported on that
  101. * device.
  102. */
  103. static void vmmouse_report_button(struct psmouse *psmouse,
  104. struct input_dev *abs_dev,
  105. struct input_dev *rel_dev,
  106. struct input_dev *pref_dev,
  107. unsigned int code, int value)
  108. {
  109. if (test_bit(code, abs_dev->key))
  110. pref_dev = abs_dev;
  111. else if (test_bit(code, rel_dev->key))
  112. pref_dev = rel_dev;
  113. input_report_key(pref_dev, code, value);
  114. }
  115. /**
  116. * vmmouse_report_events - process events on the vmmouse communications channel
  117. *
  118. * @psmouse: Pointer to the psmouse struct
  119. *
  120. * This function pulls events from the vmmouse communications channel and
  121. * reports them on the correct (absolute or relative) input device. When the
  122. * communications channel is drained, or if we've processed more than 255
  123. * psmouse commands, the function returns PSMOUSE_FULL_PACKET. If there is a
  124. * host- or synchronization error, the function returns PSMOUSE_BAD_DATA in
  125. * the hope that the caller will reset the communications channel.
  126. */
  127. static psmouse_ret_t vmmouse_report_events(struct psmouse *psmouse)
  128. {
  129. struct input_dev *rel_dev = psmouse->dev;
  130. struct vmmouse_data *priv = psmouse->private;
  131. struct input_dev *abs_dev = priv->abs_dev;
  132. struct input_dev *pref_dev;
  133. u32 status, x, y, z;
  134. u32 dummy1, dummy2, dummy3;
  135. unsigned int queue_length;
  136. unsigned int count = 255;
  137. while (count--) {
  138. /* See if we have motion data. */
  139. VMMOUSE_CMD(ABSPOINTER_STATUS, 0,
  140. status, dummy1, dummy2, dummy3);
  141. if ((status & VMMOUSE_ERROR) == VMMOUSE_ERROR) {
  142. psmouse_err(psmouse, "failed to fetch status data\n");
  143. /*
  144. * After a few attempts this will result in
  145. * reconnect.
  146. */
  147. return PSMOUSE_BAD_DATA;
  148. }
  149. queue_length = status & 0xffff;
  150. if (queue_length == 0)
  151. break;
  152. if (queue_length % 4) {
  153. psmouse_err(psmouse, "invalid queue length\n");
  154. return PSMOUSE_BAD_DATA;
  155. }
  156. /* Now get it */
  157. VMMOUSE_CMD(ABSPOINTER_DATA, 4, status, x, y, z);
  158. /*
  159. * And report what we've got. Prefer to report button
  160. * events on the same device where we report motion events.
  161. * This doesn't work well with the mouse wheel, though. See
  162. * below. Ideally we would want to report that on the
  163. * preferred device as well.
  164. */
  165. if (status & VMMOUSE_RELATIVE_PACKET) {
  166. pref_dev = rel_dev;
  167. input_report_rel(rel_dev, REL_X, (s32)x);
  168. input_report_rel(rel_dev, REL_Y, -(s32)y);
  169. } else {
  170. pref_dev = abs_dev;
  171. input_report_abs(abs_dev, ABS_X, x);
  172. input_report_abs(abs_dev, ABS_Y, y);
  173. }
  174. /* Xorg seems to ignore wheel events on absolute devices */
  175. input_report_rel(rel_dev, REL_WHEEL, -(s8)((u8) z));
  176. vmmouse_report_button(psmouse, abs_dev, rel_dev,
  177. pref_dev, BTN_LEFT,
  178. status & VMMOUSE_LEFT_BUTTON);
  179. vmmouse_report_button(psmouse, abs_dev, rel_dev,
  180. pref_dev, BTN_RIGHT,
  181. status & VMMOUSE_RIGHT_BUTTON);
  182. vmmouse_report_button(psmouse, abs_dev, rel_dev,
  183. pref_dev, BTN_MIDDLE,
  184. status & VMMOUSE_MIDDLE_BUTTON);
  185. input_sync(abs_dev);
  186. input_sync(rel_dev);
  187. }
  188. return PSMOUSE_FULL_PACKET;
  189. }
  190. /**
  191. * vmmouse_process_byte - process data on the ps/2 channel
  192. *
  193. * @psmouse: Pointer to the psmouse struct
  194. *
  195. * When the ps/2 channel indicates that there is vmmouse data available,
  196. * call vmmouse channel processing. Otherwise, continue to accept bytes. If
  197. * there is a synchronization or communication data error, return
  198. * PSMOUSE_BAD_DATA in the hope that the caller will reset the mouse.
  199. */
  200. static psmouse_ret_t vmmouse_process_byte(struct psmouse *psmouse)
  201. {
  202. unsigned char *packet = psmouse->packet;
  203. switch (psmouse->pktcnt) {
  204. case 1:
  205. return (packet[0] & 0x8) == 0x8 ?
  206. PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
  207. case 2:
  208. return PSMOUSE_GOOD_DATA;
  209. default:
  210. return vmmouse_report_events(psmouse);
  211. }
  212. }
  213. /**
  214. * vmmouse_disable - Disable vmmouse
  215. *
  216. * @psmouse: Pointer to the psmouse struct
  217. *
  218. * Tries to disable vmmouse mode.
  219. */
  220. static void vmmouse_disable(struct psmouse *psmouse)
  221. {
  222. u32 status;
  223. u32 dummy1, dummy2, dummy3, dummy4;
  224. VMMOUSE_CMD(ABSPOINTER_COMMAND, VMMOUSE_CMD_DISABLE,
  225. dummy1, dummy2, dummy3, dummy4);
  226. VMMOUSE_CMD(ABSPOINTER_STATUS, 0,
  227. status, dummy1, dummy2, dummy3);
  228. if ((status & VMMOUSE_ERROR) != VMMOUSE_ERROR)
  229. psmouse_warn(psmouse, "failed to disable vmmouse device\n");
  230. }
  231. /**
  232. * vmmouse_enable - Enable vmmouse and request absolute mode.
  233. *
  234. * @psmouse: Pointer to the psmouse struct
  235. *
  236. * Tries to enable vmmouse mode. Performs basic checks and requests
  237. * absolute vmmouse mode.
  238. * Returns 0 on success, -ENODEV on failure.
  239. */
  240. static int vmmouse_enable(struct psmouse *psmouse)
  241. {
  242. u32 status, version;
  243. u32 dummy1, dummy2, dummy3, dummy4;
  244. /*
  245. * Try enabling the device. If successful, we should be able to
  246. * read valid version ID back from it.
  247. */
  248. VMMOUSE_CMD(ABSPOINTER_COMMAND, VMMOUSE_CMD_ENABLE,
  249. dummy1, dummy2, dummy3, dummy4);
  250. /*
  251. * See if version ID can be retrieved.
  252. */
  253. VMMOUSE_CMD(ABSPOINTER_STATUS, 0, status, dummy1, dummy2, dummy3);
  254. if ((status & 0x0000ffff) == 0) {
  255. psmouse_dbg(psmouse, "empty flags - assuming no device\n");
  256. return -ENXIO;
  257. }
  258. VMMOUSE_CMD(ABSPOINTER_DATA, 1 /* single item */,
  259. version, dummy1, dummy2, dummy3);
  260. if (version != VMMOUSE_VERSION_ID) {
  261. psmouse_dbg(psmouse, "Unexpected version value: %u vs %u\n",
  262. (unsigned) version, VMMOUSE_VERSION_ID);
  263. vmmouse_disable(psmouse);
  264. return -ENXIO;
  265. }
  266. /*
  267. * Restrict ioport access, if possible.
  268. */
  269. VMMOUSE_CMD(ABSPOINTER_RESTRICT, VMMOUSE_RESTRICT_CPL0,
  270. dummy1, dummy2, dummy3, dummy4);
  271. VMMOUSE_CMD(ABSPOINTER_COMMAND, VMMOUSE_CMD_REQUEST_ABSOLUTE,
  272. dummy1, dummy2, dummy3, dummy4);
  273. return 0;
  274. }
  275. /*
  276. * Array of supported hypervisors.
  277. */
  278. static const struct hypervisor_x86 *vmmouse_supported_hypervisors[] = {
  279. &x86_hyper_vmware,
  280. #ifdef CONFIG_KVM_GUEST
  281. &x86_hyper_kvm,
  282. #endif
  283. };
  284. /**
  285. * vmmouse_check_hypervisor - Check if we're running on a supported hypervisor
  286. */
  287. static bool vmmouse_check_hypervisor(void)
  288. {
  289. int i;
  290. for (i = 0; i < ARRAY_SIZE(vmmouse_supported_hypervisors); i++)
  291. if (vmmouse_supported_hypervisors[i] == x86_hyper)
  292. return true;
  293. return false;
  294. }
  295. /**
  296. * vmmouse_detect - Probe whether vmmouse is available
  297. *
  298. * @psmouse: Pointer to the psmouse struct
  299. * @set_properties: Whether to set psmouse name and vendor
  300. *
  301. * Returns 0 if vmmouse channel is available. Negative error code if not.
  302. */
  303. int vmmouse_detect(struct psmouse *psmouse, bool set_properties)
  304. {
  305. u32 response, version, dummy1, dummy2;
  306. if (!vmmouse_check_hypervisor()) {
  307. psmouse_dbg(psmouse,
  308. "VMMouse not running on supported hypervisor.\n");
  309. return -ENXIO;
  310. }
  311. /* Check if the device is present */
  312. response = ~VMMOUSE_PROTO_MAGIC;
  313. VMMOUSE_CMD(GETVERSION, 0, version, response, dummy1, dummy2);
  314. if (response != VMMOUSE_PROTO_MAGIC || version == 0xffffffffU)
  315. return -ENXIO;
  316. if (set_properties) {
  317. psmouse->vendor = VMMOUSE_VENDOR;
  318. psmouse->name = VMMOUSE_NAME;
  319. psmouse->model = version;
  320. }
  321. return 0;
  322. }
  323. /**
  324. * vmmouse_disconnect - Take down vmmouse driver
  325. *
  326. * @psmouse: Pointer to the psmouse struct
  327. *
  328. * Takes down vmmouse driver and frees resources set up in vmmouse_init().
  329. */
  330. static void vmmouse_disconnect(struct psmouse *psmouse)
  331. {
  332. struct vmmouse_data *priv = psmouse->private;
  333. vmmouse_disable(psmouse);
  334. psmouse_reset(psmouse);
  335. input_unregister_device(priv->abs_dev);
  336. kfree(priv);
  337. }
  338. /**
  339. * vmmouse_reconnect - Reset the ps/2 - and vmmouse connections
  340. *
  341. * @psmouse: Pointer to the psmouse struct
  342. *
  343. * Attempts to reset the mouse connections. Returns 0 on success and
  344. * -1 on failure.
  345. */
  346. static int vmmouse_reconnect(struct psmouse *psmouse)
  347. {
  348. int error;
  349. psmouse_reset(psmouse);
  350. vmmouse_disable(psmouse);
  351. error = vmmouse_enable(psmouse);
  352. if (error) {
  353. psmouse_err(psmouse,
  354. "Unable to re-enable mouse when reconnecting, err: %d\n",
  355. error);
  356. return error;
  357. }
  358. return 0;
  359. }
  360. /**
  361. * vmmouse_init - Initialize the vmmouse driver
  362. *
  363. * @psmouse: Pointer to the psmouse struct
  364. *
  365. * Requests the device and tries to enable vmmouse mode.
  366. * If successful, sets up the input device for relative movement events.
  367. * It also allocates another input device and sets it up for absolute motion
  368. * events. Returns 0 on success and -1 on failure.
  369. */
  370. int vmmouse_init(struct psmouse *psmouse)
  371. {
  372. struct vmmouse_data *priv;
  373. struct input_dev *rel_dev = psmouse->dev, *abs_dev;
  374. int error;
  375. psmouse_reset(psmouse);
  376. error = vmmouse_enable(psmouse);
  377. if (error)
  378. return error;
  379. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  380. abs_dev = input_allocate_device();
  381. if (!priv || !abs_dev) {
  382. error = -ENOMEM;
  383. goto init_fail;
  384. }
  385. priv->abs_dev = abs_dev;
  386. psmouse->private = priv;
  387. /* Set up and register absolute device */
  388. snprintf(priv->phys, sizeof(priv->phys), "%s/input1",
  389. psmouse->ps2dev.serio->phys);
  390. /* Mimic name setup for relative device in psmouse-base.c */
  391. snprintf(priv->dev_name, sizeof(priv->dev_name), "%s %s %s",
  392. VMMOUSE_PSNAME, VMMOUSE_VENDOR, VMMOUSE_NAME);
  393. abs_dev->phys = priv->phys;
  394. abs_dev->name = priv->dev_name;
  395. abs_dev->id.bustype = BUS_I8042;
  396. abs_dev->id.vendor = 0x0002;
  397. abs_dev->id.product = PSMOUSE_VMMOUSE;
  398. abs_dev->id.version = psmouse->model;
  399. abs_dev->dev.parent = &psmouse->ps2dev.serio->dev;
  400. /* Set absolute device capabilities */
  401. input_set_capability(abs_dev, EV_KEY, BTN_LEFT);
  402. input_set_capability(abs_dev, EV_KEY, BTN_RIGHT);
  403. input_set_capability(abs_dev, EV_KEY, BTN_MIDDLE);
  404. input_set_capability(abs_dev, EV_ABS, ABS_X);
  405. input_set_capability(abs_dev, EV_ABS, ABS_Y);
  406. input_set_abs_params(abs_dev, ABS_X, 0, VMMOUSE_MAX_X, 0, 0);
  407. input_set_abs_params(abs_dev, ABS_Y, 0, VMMOUSE_MAX_Y, 0, 0);
  408. error = input_register_device(priv->abs_dev);
  409. if (error)
  410. goto init_fail;
  411. /* Add wheel capability to the relative device */
  412. input_set_capability(rel_dev, EV_REL, REL_WHEEL);
  413. psmouse->protocol_handler = vmmouse_process_byte;
  414. psmouse->disconnect = vmmouse_disconnect;
  415. psmouse->reconnect = vmmouse_reconnect;
  416. return 0;
  417. init_fail:
  418. vmmouse_disable(psmouse);
  419. psmouse_reset(psmouse);
  420. input_free_device(abs_dev);
  421. kfree(priv);
  422. psmouse->private = NULL;
  423. return error;
  424. }