vmmouse.c 13 KB

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