// SPDX-License-Identifier: GPL-2.0-only /* * Driver for Virtual PS/2 Mouse on VMware and QEMU hypervisors. * * Copyright (C) 2014, VMware, Inc. All Rights Reserved. * * Twin device code is hugely inspired by the ALPS driver. * Authors: * Dmitry Torokhov <dmitry.torokhov@gmail.com> * Thomas Hellstrom <thellstrom@vmware.com>
*/
/** * struct vmmouse_data - private data structure for the vmmouse driver * * @abs_dev: "Absolute" device used to report absolute mouse movement. * @phys: Physical path for the absolute device. * @dev_name: Name attribute name for the absolute device.
*/ struct vmmouse_data { struct input_dev *abs_dev; char phys[32]; char dev_name[128];
};
/** * vmmouse_report_button - report button state on the correct input device * * @psmouse: Pointer to the psmouse struct * @abs_dev: The absolute input device * @rel_dev: The relative input device * @pref_dev: The preferred device for reporting * @code: Button code * @value: Button value * * Report @value and @code on @pref_dev, unless the button is already * pressed on the other device, in which case the state is reported on that * device.
*/ staticvoid vmmouse_report_button(struct psmouse *psmouse, struct input_dev *abs_dev, struct input_dev *rel_dev, struct input_dev *pref_dev, unsignedint code, int value)
{ if (test_bit(code, abs_dev->key))
pref_dev = abs_dev; elseif (test_bit(code, rel_dev->key))
pref_dev = rel_dev;
input_report_key(pref_dev, code, value);
}
/** * vmmouse_report_events - process events on the vmmouse communications channel * * @psmouse: Pointer to the psmouse struct * * This function pulls events from the vmmouse communications channel and * reports them on the correct (absolute or relative) input device. When the * communications channel is drained, or if we've processed more than 255 * psmouse commands, the function returns PSMOUSE_FULL_PACKET. If there is a * host- or synchronization error, the function returns PSMOUSE_BAD_DATA in * the hope that the caller will reset the communications channel.
*/ static psmouse_ret_t vmmouse_report_events(struct psmouse *psmouse)
{ struct input_dev *rel_dev = psmouse->dev; struct vmmouse_data *priv = psmouse->private; struct input_dev *abs_dev = priv->abs_dev; struct input_dev *pref_dev;
u32 status, x, y, z; unsignedint queue_length; unsignedint count = 255;
while (count--) { /* See if we have motion data. */
status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0); if ((status & VMMOUSE_ERROR) == VMMOUSE_ERROR) {
psmouse_err(psmouse, "failed to fetch status data\n"); /* * After a few attempts this will result in * reconnect.
*/ return PSMOUSE_BAD_DATA;
}
queue_length = status & 0xffff; if (queue_length == 0) break;
/* Now get it */
status = vmware_hypercall4(VMWARE_CMD_ABSPOINTER_DATA, 4,
&x, &y, &z);
/* * And report what we've got. Prefer to report button * events on the same device where we report motion events. * This doesn't work well with the mouse wheel, though. See * below. Ideally we would want to report that on the * preferred device as well.
*/ if (status & VMMOUSE_RELATIVE_PACKET) {
pref_dev = rel_dev;
input_report_rel(rel_dev, REL_X, (s32)x);
input_report_rel(rel_dev, REL_Y, -(s32)y);
} else {
pref_dev = abs_dev;
input_report_abs(abs_dev, ABS_X, x);
input_report_abs(abs_dev, ABS_Y, y);
}
/* Xorg seems to ignore wheel events on absolute devices */
input_report_rel(rel_dev, REL_WHEEL, -(s8)((u8) z));
vmmouse_report_button(psmouse, abs_dev, rel_dev,
pref_dev, BTN_LEFT,
status & VMMOUSE_LEFT_BUTTON);
vmmouse_report_button(psmouse, abs_dev, rel_dev,
pref_dev, BTN_RIGHT,
status & VMMOUSE_RIGHT_BUTTON);
vmmouse_report_button(psmouse, abs_dev, rel_dev,
pref_dev, BTN_MIDDLE,
status & VMMOUSE_MIDDLE_BUTTON);
input_sync(abs_dev);
input_sync(rel_dev);
}
return PSMOUSE_FULL_PACKET;
}
/** * vmmouse_process_byte - process data on the ps/2 channel * * @psmouse: Pointer to the psmouse struct * * When the ps/2 channel indicates that there is vmmouse data available, * call vmmouse channel processing. Otherwise, continue to accept bytes. If * there is a synchronization or communication data error, return * PSMOUSE_BAD_DATA in the hope that the caller will reset the mouse.
*/ static psmouse_ret_t vmmouse_process_byte(struct psmouse *psmouse)
{ unsignedchar *packet = psmouse->packet;
status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0); if ((status & VMMOUSE_ERROR) != VMMOUSE_ERROR)
psmouse_warn(psmouse, "failed to disable vmmouse device\n");
}
/** * vmmouse_enable - Enable vmmouse and request absolute mode. * * @psmouse: Pointer to the psmouse struct * * Tries to enable vmmouse mode. Performs basic checks and requests * absolute vmmouse mode. * Returns 0 on success, -ENODEV on failure.
*/ staticint vmmouse_enable(struct psmouse *psmouse)
{
u32 status, version;
/* * Try enabling the device. If successful, we should be able to * read valid version ID back from it.
*/
vmware_hypercall1(VMWARE_CMD_ABSPOINTER_COMMAND, VMMOUSE_CMD_ENABLE);
/* * See if version ID can be retrieved.
*/
status = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_STATUS, 0); if ((status & 0x0000ffff) == 0) {
psmouse_dbg(psmouse, "empty flags - assuming no device\n"); return -ENXIO;
}
version = vmware_hypercall1(VMWARE_CMD_ABSPOINTER_DATA,
1 /* single item */); if (version != VMMOUSE_VERSION_ID) {
psmouse_dbg(psmouse, "Unexpected version value: %u vs %u\n",
(unsigned) version, VMMOUSE_VERSION_ID);
vmmouse_disable(psmouse); return -ENXIO;
}
/* * Restrict ioport access, if possible.
*/
vmware_hypercall1(VMWARE_CMD_ABSPOINTER_RESTRICT,
VMMOUSE_RESTRICT_CPL0);
/** * vmmouse_check_hypervisor - Check if we're running on a supported hypervisor
*/ staticbool vmmouse_check_hypervisor(void)
{ int i;
for (i = 0; i < ARRAY_SIZE(vmmouse_supported_hypervisors); i++) if (vmmouse_supported_hypervisors[i] == x86_hyper_type) returntrue;
returnfalse;
}
/** * vmmouse_detect - Probe whether vmmouse is available * * @psmouse: Pointer to the psmouse struct * @set_properties: Whether to set psmouse name and vendor * * Returns 0 if vmmouse channel is available. Negative error code if not.
*/ int vmmouse_detect(struct psmouse *psmouse, bool set_properties)
{
u32 response, version, type;
if (!vmmouse_check_hypervisor()) {
psmouse_dbg(psmouse, "VMMouse not running on supported hypervisor.\n"); return -ENXIO;
}
/* Check if the device is present */
response = ~VMWARE_HYPERVISOR_MAGIC;
version = vmware_hypercall3(VMWARE_CMD_GETVERSION, 0, &response, &type); if (response != VMWARE_HYPERVISOR_MAGIC || version == 0xffffffffU) return -ENXIO;
/** * vmmouse_reset - Disable vmmouse and reset * * @psmouse: Pointer to the psmouse struct * * Tries to disable vmmouse mode before enter suspend.
*/ staticvoid vmmouse_reset(struct psmouse *psmouse)
{
vmmouse_disable(psmouse);
psmouse_reset(psmouse);
}
/** * vmmouse_disconnect - Take down vmmouse driver * * @psmouse: Pointer to the psmouse struct * * Takes down vmmouse driver and frees resources set up in vmmouse_init().
*/ staticvoid vmmouse_disconnect(struct psmouse *psmouse)
{ struct vmmouse_data *priv = psmouse->private;
/** * vmmouse_reconnect - Reset the ps/2 - and vmmouse connections * * @psmouse: Pointer to the psmouse struct * * Attempts to reset the mouse connections. Returns 0 on success and * -1 on failure.
*/ staticint vmmouse_reconnect(struct psmouse *psmouse)
{ int error;
psmouse_reset(psmouse);
vmmouse_disable(psmouse);
error = vmmouse_enable(psmouse); if (error) {
psmouse_err(psmouse, "Unable to re-enable mouse when reconnecting, err: %d\n",
error); return error;
}
return 0;
}
/** * vmmouse_init - Initialize the vmmouse driver * * @psmouse: Pointer to the psmouse struct * * Requests the device and tries to enable vmmouse mode. * If successful, sets up the input device for relative movement events. * It also allocates another input device and sets it up for absolute motion * events. Returns 0 on success and -1 on failure.
*/ int vmmouse_init(struct psmouse *psmouse)
{ struct vmmouse_data *priv; struct input_dev *rel_dev = psmouse->dev, *abs_dev; int error;
psmouse_reset(psmouse);
error = vmmouse_enable(psmouse); if (error) return error;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.