1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Virtual PS/2 Mouse on VMware and QEMU hypervisors.
5 * Copyright (C) 2014, VMware, Inc. All Rights Reserved.
7 * Twin device code is hugely inspired by the ALPS driver.
13 #include <linux/input.h>
14 #include <linux/serio.h>
15 #include <linux/libps2.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <asm/hypervisor.h>
19 #include <asm/vmware.h>
25 * Main commands supported by the vmmouse hypervisor port.
27 #define VMWARE_CMD_ABSPOINTER_DATA 39
28 #define VMWARE_CMD_ABSPOINTER_STATUS 40
29 #define VMWARE_CMD_ABSPOINTER_COMMAND 41
30 #define VMWARE_CMD_ABSPOINTER_RESTRICT 86
33 * Subcommands for VMWARE_CMD_ABSPOINTER_COMMAND
35 #define VMMOUSE_CMD_ENABLE 0x45414552U
36 #define VMMOUSE_CMD_DISABLE 0x000000f5U
37 #define VMMOUSE_CMD_REQUEST_RELATIVE 0x4c455252U
38 #define VMMOUSE_CMD_REQUEST_ABSOLUTE 0x53424152U
40 #define VMMOUSE_ERROR 0xffff0000U
42 #define VMMOUSE_VERSION_ID 0x3442554aU
44 #define VMMOUSE_RELATIVE_PACKET 0x00010000U
46 #define VMMOUSE_LEFT_BUTTON 0x20
47 #define VMMOUSE_RIGHT_BUTTON 0x10
48 #define VMMOUSE_MIDDLE_BUTTON 0x08
51 * VMMouse Restrict command
53 #define VMMOUSE_RESTRICT_ANY 0x00
54 #define VMMOUSE_RESTRICT_CPL0 0x01
55 #define VMMOUSE_RESTRICT_IOPL 0x02
57 #define VMMOUSE_MAX_X 0xFFFF
58 #define VMMOUSE_MAX_Y 0xFFFF
60 #define VMMOUSE_VENDOR "VMware"
61 #define VMMOUSE_NAME "VMMouse"
64 * struct vmmouse_data - private data structure for the vmmouse driver
66 * @abs_dev: "Absolute" device used to report absolute mouse movement.
67 * @phys: Physical path for the absolute device.
68 * @dev_name: Name attribute name for the absolute device.
71 struct input_dev *abs_dev;
77 * vmmouse_report_button - report button state on the correct input device
79 * @psmouse: Pointer to the psmouse struct
80 * @abs_dev: The absolute input device
81 * @rel_dev: The relative input device
82 * @pref_dev: The preferred device for reporting
84 * @value: Button value
86 * Report @value and @code on @pref_dev, unless the button is already
87 * pressed on the other device, in which case the state is reported on that
90 static void vmmouse_report_button(struct psmouse *psmouse,
91 struct input_dev *abs_dev,
92 struct input_dev *rel_dev,
93 struct input_dev *pref_dev,
94 unsigned int code, int value)
96 if (test_bit(code, abs_dev->key))
98 else if (test_bit(code, rel_dev->key))
101 input_report_key(pref_dev, code, value);
105 * vmmouse_report_events - process events on the vmmouse communications channel
107 * @psmouse: Pointer to the psmouse struct
109 * This function pulls events from the vmmouse communications channel and
110 * reports them on the correct (absolute or relative) input device. When the
111 * communications channel is drained, or if we've processed more than 255
112 * psmouse commands, the function returns PSMOUSE_FULL_PACKET. If there is a
113 * host- or synchronization error, the function returns PSMOUSE_BAD_DATA in
114 * the hope that the caller will reset the communications channel.
116 static psmouse_ret_t vmmouse_report_events(struct psmouse *psmouse)
118 struct input_dev *rel_dev = psmouse->dev;
119 struct vmmouse_data *priv = psmouse->private;
120 struct input_dev *abs_dev = priv->abs_dev;
121 struct input_dev *pref_dev;
123 unsigned int queue_length;
124 unsigned int count = 255;
127 /* See if we have motion data. */
128 status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0);
129 if ((status & VMMOUSE_ERROR) == VMMOUSE_ERROR) {
130 psmouse_err(psmouse, "failed to fetch status data\n");
132 * After a few attempts this will result in
135 return PSMOUSE_BAD_DATA;
138 queue_length = status & 0xffff;
139 if (queue_length == 0)
142 if (queue_length % 4) {
143 psmouse_err(psmouse, "invalid queue length\n");
144 return PSMOUSE_BAD_DATA;
148 status = vmware_hypercall4(VMWARE_CMD_ABSPOINTER_DATA, 4,
152 * And report what we've got. Prefer to report button
153 * events on the same device where we report motion events.
154 * This doesn't work well with the mouse wheel, though. See
155 * below. Ideally we would want to report that on the
156 * preferred device as well.
158 if (status & VMMOUSE_RELATIVE_PACKET) {
160 input_report_rel(rel_dev, REL_X, (s32)x);
161 input_report_rel(rel_dev, REL_Y, -(s32)y);
164 input_report_abs(abs_dev, ABS_X, x);
165 input_report_abs(abs_dev, ABS_Y, y);
168 /* Xorg seems to ignore wheel events on absolute devices */
169 input_report_rel(rel_dev, REL_WHEEL, -(s8)((u8) z));
171 vmmouse_report_button(psmouse, abs_dev, rel_dev,
173 status & VMMOUSE_LEFT_BUTTON);
174 vmmouse_report_button(psmouse, abs_dev, rel_dev,
176 status & VMMOUSE_RIGHT_BUTTON);
177 vmmouse_report_button(psmouse, abs_dev, rel_dev,
178 pref_dev, BTN_MIDDLE,
179 status & VMMOUSE_MIDDLE_BUTTON);
184 return PSMOUSE_FULL_PACKET;
188 * vmmouse_process_byte - process data on the ps/2 channel
190 * @psmouse: Pointer to the psmouse struct
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.
197 static psmouse_ret_t vmmouse_process_byte(struct psmouse *psmouse)
199 unsigned char *packet = psmouse->packet;
201 switch (psmouse->pktcnt) {
203 return (packet[0] & 0x8) == 0x8 ?
204 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
207 return PSMOUSE_GOOD_DATA;
210 return vmmouse_report_events(psmouse);
215 * vmmouse_disable - Disable vmmouse
217 * @psmouse: Pointer to the psmouse struct
219 * Tries to disable vmmouse mode.
221 static void vmmouse_disable(struct psmouse *psmouse)
225 vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND, VMMOUSE_CMD_DISABLE);
227 status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0);
228 if ((status & VMMOUSE_ERROR) != VMMOUSE_ERROR)
229 psmouse_warn(psmouse, "failed to disable vmmouse device\n");
233 * vmmouse_enable - Enable vmmouse and request absolute mode.
235 * @psmouse: Pointer to the psmouse struct
237 * Tries to enable vmmouse mode. Performs basic checks and requests
238 * absolute vmmouse mode.
239 * Returns 0 on success, -ENODEV on failure.
241 static int vmmouse_enable(struct psmouse *psmouse)
246 * Try enabling the device. If successful, we should be able to
247 * read valid version ID back from it.
249 vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND, VMMOUSE_CMD_ENABLE);
252 * See if version ID can be retrieved.
254 status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0);
255 if ((status & 0x0000ffff) == 0) {
256 psmouse_dbg(psmouse, "empty flags - assuming no device\n");
260 version = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_DATA,
261 1 /* single item */);
262 if (version != VMMOUSE_VERSION_ID) {
263 psmouse_dbg(psmouse, "Unexpected version value: %u vs %u\n",
264 (unsigned) version, VMMOUSE_VERSION_ID);
265 vmmouse_disable(psmouse);
270 * Restrict ioport access, if possible.
272 vmware_hypercall1(VMWARE_CMD_ABSPOINTER_RESTRICT,
273 VMMOUSE_RESTRICT_CPL0);
275 vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND,
276 VMMOUSE_CMD_REQUEST_ABSOLUTE);
282 * Array of supported hypervisors.
284 static enum x86_hypervisor_type vmmouse_supported_hypervisors[] = {
290 * vmmouse_check_hypervisor - Check if we're running on a supported hypervisor
292 static bool vmmouse_check_hypervisor(void)
296 for (i = 0; i < ARRAY_SIZE(vmmouse_supported_hypervisors); i++)
297 if (vmmouse_supported_hypervisors[i] == x86_hyper_type)
304 * vmmouse_detect - Probe whether vmmouse is available
306 * @psmouse: Pointer to the psmouse struct
307 * @set_properties: Whether to set psmouse name and vendor
309 * Returns 0 if vmmouse channel is available. Negative error code if not.
311 int vmmouse_detect(struct psmouse *psmouse, bool set_properties)
313 u32 response, version, type;
315 if (!vmmouse_check_hypervisor()) {
317 "VMMouse not running on supported hypervisor.\n");
321 /* Check if the device is present */
322 response = ~VMWARE_HYPERVISOR_MAGIC;
323 version = vmware_hypercall3(VMWARE_CMD_GETVERSION, 0, &response, &type);
324 if (response != VMWARE_HYPERVISOR_MAGIC || version == 0xffffffffU)
327 if (set_properties) {
328 psmouse->vendor = VMMOUSE_VENDOR;
329 psmouse->name = VMMOUSE_NAME;
330 psmouse->model = version;
337 * vmmouse_reset - Disable vmmouse and reset
339 * @psmouse: Pointer to the psmouse struct
341 * Tries to disable vmmouse mode before enter suspend.
343 static void vmmouse_reset(struct psmouse *psmouse)
345 vmmouse_disable(psmouse);
346 psmouse_reset(psmouse);
350 * vmmouse_disconnect - Take down vmmouse driver
352 * @psmouse: Pointer to the psmouse struct
354 * Takes down vmmouse driver and frees resources set up in vmmouse_init().
356 static void vmmouse_disconnect(struct psmouse *psmouse)
358 struct vmmouse_data *priv = psmouse->private;
360 vmmouse_disable(psmouse);
361 psmouse_reset(psmouse);
362 input_unregister_device(priv->abs_dev);
367 * vmmouse_reconnect - Reset the ps/2 - and vmmouse connections
369 * @psmouse: Pointer to the psmouse struct
371 * Attempts to reset the mouse connections. Returns 0 on success and
374 static int vmmouse_reconnect(struct psmouse *psmouse)
378 psmouse_reset(psmouse);
379 vmmouse_disable(psmouse);
380 error = vmmouse_enable(psmouse);
383 "Unable to re-enable mouse when reconnecting, err: %d\n",
392 * vmmouse_init - Initialize the vmmouse driver
394 * @psmouse: Pointer to the psmouse struct
396 * Requests the device and tries to enable vmmouse mode.
397 * If successful, sets up the input device for relative movement events.
398 * It also allocates another input device and sets it up for absolute motion
399 * events. Returns 0 on success and -1 on failure.
401 int vmmouse_init(struct psmouse *psmouse)
403 struct vmmouse_data *priv;
404 struct input_dev *rel_dev = psmouse->dev, *abs_dev;
407 psmouse_reset(psmouse);
408 error = vmmouse_enable(psmouse);
412 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
413 abs_dev = input_allocate_device();
414 if (!priv || !abs_dev) {
419 priv->abs_dev = abs_dev;
420 psmouse->private = priv;
422 /* Set up and register absolute device */
423 snprintf(priv->phys, sizeof(priv->phys), "%s/input1",
424 psmouse->ps2dev.serio->phys);
426 /* Mimic name setup for relative device in psmouse-base.c */
427 snprintf(priv->dev_name, sizeof(priv->dev_name), "%s %s %s",
428 VMMOUSE_PSNAME, VMMOUSE_VENDOR, VMMOUSE_NAME);
429 abs_dev->phys = priv->phys;
430 abs_dev->name = priv->dev_name;
431 abs_dev->id.bustype = BUS_I8042;
432 abs_dev->id.vendor = 0x0002;
433 abs_dev->id.product = PSMOUSE_VMMOUSE;
434 abs_dev->id.version = psmouse->model;
435 abs_dev->dev.parent = &psmouse->ps2dev.serio->dev;
437 /* Set absolute device capabilities */
438 input_set_capability(abs_dev, EV_KEY, BTN_LEFT);
439 input_set_capability(abs_dev, EV_KEY, BTN_RIGHT);
440 input_set_capability(abs_dev, EV_KEY, BTN_MIDDLE);
441 input_set_capability(abs_dev, EV_ABS, ABS_X);
442 input_set_capability(abs_dev, EV_ABS, ABS_Y);
443 input_set_abs_params(abs_dev, ABS_X, 0, VMMOUSE_MAX_X, 0, 0);
444 input_set_abs_params(abs_dev, ABS_Y, 0, VMMOUSE_MAX_Y, 0, 0);
446 error = input_register_device(priv->abs_dev);
450 /* Add wheel capability to the relative device */
451 input_set_capability(rel_dev, EV_REL, REL_WHEEL);
453 psmouse->protocol_handler = vmmouse_process_byte;
454 psmouse->disconnect = vmmouse_disconnect;
455 psmouse->reconnect = vmmouse_reconnect;
456 psmouse->cleanup = vmmouse_reset;
461 vmmouse_disable(psmouse);
462 psmouse_reset(psmouse);
463 input_free_device(abs_dev);
465 psmouse->private = NULL;