vmmouse.c 12 KB

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