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 
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>
20 
21 #include "psmouse.h"
22 #include "vmmouse.h"
23 
24 /*
25  * Main commands supported by the vmmouse hypervisor port.
26  */
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
31 
32 /*
33  * Subcommands for VMWARE_CMD_ABSPOINTER_COMMAND
34  */
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
39 
40 #define VMMOUSE_ERROR				0xffff0000U
41 
42 #define VMMOUSE_VERSION_ID			0x3442554aU
43 
44 #define VMMOUSE_RELATIVE_PACKET			0x00010000U
45 
46 #define VMMOUSE_LEFT_BUTTON			0x20
47 #define VMMOUSE_RIGHT_BUTTON			0x10
48 #define VMMOUSE_MIDDLE_BUTTON			0x08
49 
50 /*
51  * VMMouse Restrict command
52  */
53 #define VMMOUSE_RESTRICT_ANY                    0x00
54 #define VMMOUSE_RESTRICT_CPL0                   0x01
55 #define VMMOUSE_RESTRICT_IOPL                   0x02
56 
57 #define VMMOUSE_MAX_X                           0xFFFF
58 #define VMMOUSE_MAX_Y                           0xFFFF
59 
60 #define VMMOUSE_VENDOR "VMware"
61 #define VMMOUSE_NAME   "VMMouse"
62 
63 /**
64  * struct vmmouse_data - private data structure for the vmmouse driver
65  *
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.
69  */
70 struct vmmouse_data {
71 	struct input_dev *abs_dev;
72 	char phys[32];
73 	char dev_name[128];
74 };
75 
76 /**
77  * vmmouse_report_button - report button state on the correct input device
78  *
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
83  * @code:     Button code
84  * @value:    Button value
85  *
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
88  * device.
89  */
vmmouse_report_button(struct psmouse * psmouse,struct input_dev * abs_dev,struct input_dev * rel_dev,struct input_dev * pref_dev,unsigned int code,int value)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)
95 {
96 	if (test_bit(code, abs_dev->key))
97 		pref_dev = abs_dev;
98 	else if (test_bit(code, rel_dev->key))
99 		pref_dev = rel_dev;
100 
101 	input_report_key(pref_dev, code, value);
102 }
103 
104 /**
105  * vmmouse_report_events - process events on the vmmouse communications channel
106  *
107  * @psmouse: Pointer to the psmouse struct
108  *
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.
115  */
vmmouse_report_events(struct psmouse * psmouse)116 static psmouse_ret_t vmmouse_report_events(struct psmouse *psmouse)
117 {
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;
122 	u32 status, x, y, z;
123 	unsigned int queue_length;
124 	unsigned int count = 255;
125 
126 	while (count--) {
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");
131 			/*
132 			 * After a few attempts this will result in
133 			 * reconnect.
134 			 */
135 			return PSMOUSE_BAD_DATA;
136 		}
137 
138 		queue_length = status & 0xffff;
139 		if (queue_length == 0)
140 			break;
141 
142 		if (queue_length % 4) {
143 			psmouse_err(psmouse, "invalid queue length\n");
144 			return PSMOUSE_BAD_DATA;
145 		}
146 
147 		/* Now get it */
148 		status = vmware_hypercall4(VMWARE_CMD_ABSPOINTER_DATA, 4,
149 					   &x, &y, &z);
150 
151 		/*
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.
157 		 */
158 		if (status & VMMOUSE_RELATIVE_PACKET) {
159 			pref_dev = rel_dev;
160 			input_report_rel(rel_dev, REL_X, (s32)x);
161 			input_report_rel(rel_dev, REL_Y, -(s32)y);
162 		} else {
163 			pref_dev = abs_dev;
164 			input_report_abs(abs_dev, ABS_X, x);
165 			input_report_abs(abs_dev, ABS_Y, y);
166 		}
167 
168 		/* Xorg seems to ignore wheel events on absolute devices */
169 		input_report_rel(rel_dev, REL_WHEEL, -(s8)((u8) z));
170 
171 		vmmouse_report_button(psmouse, abs_dev, rel_dev,
172 				      pref_dev, BTN_LEFT,
173 				      status & VMMOUSE_LEFT_BUTTON);
174 		vmmouse_report_button(psmouse, abs_dev, rel_dev,
175 				      pref_dev, BTN_RIGHT,
176 				      status & VMMOUSE_RIGHT_BUTTON);
177 		vmmouse_report_button(psmouse, abs_dev, rel_dev,
178 				      pref_dev, BTN_MIDDLE,
179 				      status & VMMOUSE_MIDDLE_BUTTON);
180 		input_sync(abs_dev);
181 		input_sync(rel_dev);
182 	}
183 
184 	return PSMOUSE_FULL_PACKET;
185 }
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  */
vmmouse_process_byte(struct psmouse * psmouse)197 static psmouse_ret_t vmmouse_process_byte(struct psmouse *psmouse)
198 {
199 	unsigned char *packet = psmouse->packet;
200 
201 	switch (psmouse->pktcnt) {
202 	case 1:
203 		return (packet[0] & 0x8) == 0x8 ?
204 			PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
205 
206 	case 2:
207 		return PSMOUSE_GOOD_DATA;
208 
209 	default:
210 		return vmmouse_report_events(psmouse);
211 	}
212 }
213 
214 /**
215  * vmmouse_disable - Disable vmmouse
216  *
217  * @psmouse: Pointer to the psmouse struct
218  *
219  * Tries to disable vmmouse mode.
220  */
vmmouse_disable(struct psmouse * psmouse)221 static void vmmouse_disable(struct psmouse *psmouse)
222 {
223 	u32 status;
224 
225 	vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND, VMMOUSE_CMD_DISABLE);
226 
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");
230 }
231 
232 /**
233  * vmmouse_enable - Enable vmmouse and request absolute mode.
234  *
235  * @psmouse: Pointer to the psmouse struct
236  *
237  * Tries to enable vmmouse mode. Performs basic checks and requests
238  * absolute vmmouse mode.
239  * Returns 0 on success, -ENODEV on failure.
240  */
vmmouse_enable(struct psmouse * psmouse)241 static int vmmouse_enable(struct psmouse *psmouse)
242 {
243 	u32 status, version;
244 
245 	/*
246 	 * Try enabling the device. If successful, we should be able to
247 	 * read valid version ID back from it.
248 	 */
249 	vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND, VMMOUSE_CMD_ENABLE);
250 
251 	/*
252 	 * See if version ID can be retrieved.
253 	 */
254 	status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0);
255 	if ((status & 0x0000ffff) == 0) {
256 		psmouse_dbg(psmouse, "empty flags - assuming no device\n");
257 		return -ENXIO;
258 	}
259 
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);
266 		return -ENXIO;
267 	}
268 
269 	/*
270 	 * Restrict ioport access, if possible.
271 	 */
272 	vmware_hypercall1(VMWARE_CMD_ABSPOINTER_RESTRICT,
273 			  VMMOUSE_RESTRICT_CPL0);
274 
275 	vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND,
276 			  VMMOUSE_CMD_REQUEST_ABSOLUTE);
277 
278 	return 0;
279 }
280 
281 /*
282  * Array of supported hypervisors.
283  */
284 static enum x86_hypervisor_type vmmouse_supported_hypervisors[] = {
285 	X86_HYPER_VMWARE,
286 	X86_HYPER_KVM,
287 };
288 
289 /**
290  * vmmouse_check_hypervisor - Check if we're running on a supported hypervisor
291  */
vmmouse_check_hypervisor(void)292 static bool vmmouse_check_hypervisor(void)
293 {
294 	int i;
295 
296 	for (i = 0; i < ARRAY_SIZE(vmmouse_supported_hypervisors); i++)
297 		if (vmmouse_supported_hypervisors[i] == x86_hyper_type)
298 			return true;
299 
300 	return false;
301 }
302 
303 /**
304  * vmmouse_detect - Probe whether vmmouse is available
305  *
306  * @psmouse: Pointer to the psmouse struct
307  * @set_properties: Whether to set psmouse name and vendor
308  *
309  * Returns 0 if vmmouse channel is available. Negative error code if not.
310  */
vmmouse_detect(struct psmouse * psmouse,bool set_properties)311 int vmmouse_detect(struct psmouse *psmouse, bool set_properties)
312 {
313 	u32 response, version, type;
314 
315 	if (!vmmouse_check_hypervisor()) {
316 		psmouse_dbg(psmouse,
317 			    "VMMouse not running on supported hypervisor.\n");
318 		return -ENXIO;
319 	}
320 
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)
325 		return -ENXIO;
326 
327 	if (set_properties) {
328 		psmouse->vendor = VMMOUSE_VENDOR;
329 		psmouse->name = VMMOUSE_NAME;
330 		psmouse->model = version;
331 	}
332 
333 	return 0;
334 }
335 
336 /**
337  * vmmouse_reset - Disable vmmouse and reset
338  *
339  * @psmouse: Pointer to the psmouse struct
340  *
341  * Tries to disable vmmouse mode before enter suspend.
342  */
vmmouse_reset(struct psmouse * psmouse)343 static void vmmouse_reset(struct psmouse *psmouse)
344 {
345 	vmmouse_disable(psmouse);
346 	psmouse_reset(psmouse);
347 }
348 
349 /**
350  * vmmouse_disconnect - Take down vmmouse driver
351  *
352  * @psmouse: Pointer to the psmouse struct
353  *
354  * Takes down vmmouse driver and frees resources set up in vmmouse_init().
355  */
vmmouse_disconnect(struct psmouse * psmouse)356 static void vmmouse_disconnect(struct psmouse *psmouse)
357 {
358 	struct vmmouse_data *priv = psmouse->private;
359 
360 	vmmouse_disable(psmouse);
361 	psmouse_reset(psmouse);
362 	input_unregister_device(priv->abs_dev);
363 	kfree(priv);
364 }
365 
366 /**
367  * vmmouse_reconnect - Reset the ps/2 - and vmmouse connections
368  *
369  * @psmouse: Pointer to the psmouse struct
370  *
371  * Attempts to reset the mouse connections. Returns 0 on success and
372  * -1 on failure.
373  */
vmmouse_reconnect(struct psmouse * psmouse)374 static int vmmouse_reconnect(struct psmouse *psmouse)
375 {
376 	int error;
377 
378 	psmouse_reset(psmouse);
379 	vmmouse_disable(psmouse);
380 	error = vmmouse_enable(psmouse);
381 	if (error) {
382 		psmouse_err(psmouse,
383 			    "Unable to re-enable mouse when reconnecting, err: %d\n",
384 			    error);
385 		return error;
386 	}
387 
388 	return 0;
389 }
390 
391 /**
392  * vmmouse_init - Initialize the vmmouse driver
393  *
394  * @psmouse: Pointer to the psmouse struct
395  *
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.
400  */
vmmouse_init(struct psmouse * psmouse)401 int vmmouse_init(struct psmouse *psmouse)
402 {
403 	struct vmmouse_data *priv;
404 	struct input_dev *rel_dev = psmouse->dev, *abs_dev;
405 	int error;
406 
407 	psmouse_reset(psmouse);
408 	error = vmmouse_enable(psmouse);
409 	if (error)
410 		return error;
411 
412 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
413 	abs_dev = input_allocate_device();
414 	if (!priv || !abs_dev) {
415 		error = -ENOMEM;
416 		goto init_fail;
417 	}
418 
419 	priv->abs_dev = abs_dev;
420 	psmouse->private = priv;
421 
422 	/* Set up and register absolute device */
423 	snprintf(priv->phys, sizeof(priv->phys), "%s/input1",
424 		 psmouse->ps2dev.serio->phys);
425 
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;
436 
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);
445 
446 	error = input_register_device(priv->abs_dev);
447 	if (error)
448 		goto init_fail;
449 
450 	/* Add wheel capability to the relative device */
451 	input_set_capability(rel_dev, EV_REL, REL_WHEEL);
452 
453 	psmouse->protocol_handler = vmmouse_process_byte;
454 	psmouse->disconnect = vmmouse_disconnect;
455 	psmouse->reconnect = vmmouse_reconnect;
456 	psmouse->cleanup = vmmouse_reset;
457 
458 	return 0;
459 
460 init_fail:
461 	vmmouse_disable(psmouse);
462 	psmouse_reset(psmouse);
463 	input_free_device(abs_dev);
464 	kfree(priv);
465 	psmouse->private = NULL;
466 
467 	return error;
468 }
469