qemu/hw/vmmouse.c
<<
>>
Prefs
   1/*
   2 * QEMU VMMouse emulation
   3 *
   4 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24#include "hw.h"
  25#include "console.h"
  26#include "ps2.h"
  27#include "pc.h"
  28
  29/* debug only vmmouse */
  30//#define DEBUG_VMMOUSE
  31
  32/* VMMouse Commands */
  33#define VMMOUSE_GETVERSION      10
  34#define VMMOUSE_DATA            39
  35#define VMMOUSE_STATUS          40
  36#define VMMOUSE_COMMAND         41
  37
  38#define VMMOUSE_READ_ID                 0x45414552
  39#define VMMOUSE_DISABLE                 0x000000f5
  40#define VMMOUSE_REQUEST_RELATIVE        0x4c455252
  41#define VMMOUSE_REQUEST_ABSOLUTE        0x53424152
  42
  43#define VMMOUSE_QUEUE_SIZE      1024
  44
  45#define VMMOUSE_VERSION         0x3442554a
  46
  47#ifdef DEBUG_VMMOUSE
  48#define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
  49#else
  50#define DPRINTF(fmt, ...) do { } while (0)
  51#endif
  52
  53typedef struct _VMMouseState
  54{
  55    uint32_t queue[VMMOUSE_QUEUE_SIZE];
  56    int32_t queue_size;
  57    uint16_t nb_queue;
  58    uint16_t status;
  59    uint8_t absolute;
  60    QEMUPutMouseEntry *entry;
  61    void *ps2_mouse;
  62} VMMouseState;
  63
  64static uint32_t vmmouse_get_status(VMMouseState *s)
  65{
  66    DPRINTF("vmmouse_get_status()\n");
  67    return (s->status << 16) | s->nb_queue;
  68}
  69
  70static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state)
  71{
  72    VMMouseState *s = opaque;
  73    int buttons = 0;
  74
  75    if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4))
  76        return;
  77
  78    DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
  79            x, y, dz, buttons_state);
  80
  81    if ((buttons_state & MOUSE_EVENT_LBUTTON))
  82        buttons |= 0x20;
  83    if ((buttons_state & MOUSE_EVENT_RBUTTON))
  84        buttons |= 0x10;
  85    if ((buttons_state & MOUSE_EVENT_MBUTTON))
  86        buttons |= 0x08;
  87
  88    if (s->absolute) {
  89        x <<= 1;
  90        y <<= 1;
  91    }
  92
  93    s->queue[s->nb_queue++] = buttons;
  94    s->queue[s->nb_queue++] = x;
  95    s->queue[s->nb_queue++] = y;
  96    s->queue[s->nb_queue++] = dz;
  97
  98    /* need to still generate PS2 events to notify driver to
  99       read from queue */
 100    i8042_isa_mouse_fake_event(s->ps2_mouse);
 101}
 102
 103static void vmmouse_remove_handler(VMMouseState *s)
 104{
 105    if (s->entry) {
 106        qemu_remove_mouse_event_handler(s->entry);
 107        s->entry = NULL;
 108    }
 109}
 110
 111static void vmmouse_update_handler(VMMouseState *s, int absolute)
 112{
 113    if (s->status != 0) {
 114        return;
 115    }
 116    if (s->absolute != absolute) {
 117        s->absolute = absolute;
 118        vmmouse_remove_handler(s);
 119    }
 120    if (s->entry == NULL) {
 121        s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event,
 122                                                s, s->absolute,
 123                                                "vmmouse");
 124        qemu_activate_mouse_event_handler(s->entry);
 125    }
 126}
 127
 128static void vmmouse_read_id(VMMouseState *s)
 129{
 130    DPRINTF("vmmouse_read_id()\n");
 131
 132    if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
 133        return;
 134
 135    s->queue[s->nb_queue++] = VMMOUSE_VERSION;
 136    s->status = 0;
 137}
 138
 139static void vmmouse_request_relative(VMMouseState *s)
 140{
 141    DPRINTF("vmmouse_request_relative()\n");
 142    vmmouse_update_handler(s, 0);
 143}
 144
 145static void vmmouse_request_absolute(VMMouseState *s)
 146{
 147    DPRINTF("vmmouse_request_absolute()\n");
 148    vmmouse_update_handler(s, 1);
 149}
 150
 151static void vmmouse_disable(VMMouseState *s)
 152{
 153    DPRINTF("vmmouse_disable()\n");
 154    s->status = 0xffff;
 155    vmmouse_remove_handler(s);
 156}
 157
 158static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size)
 159{
 160    int i;
 161
 162    DPRINTF("vmmouse_data(%d)\n", size);
 163
 164    if (size == 0 || size > 6 || size > s->nb_queue) {
 165        printf("vmmouse: driver requested too much data %d\n", size);
 166        s->status = 0xffff;
 167        vmmouse_remove_handler(s);
 168        return;
 169    }
 170
 171    for (i = 0; i < size; i++)
 172        data[i] = s->queue[i];
 173
 174    s->nb_queue -= size;
 175    if (s->nb_queue)
 176        memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue);
 177}
 178
 179static void vmmouse_get_data(uint32_t *data)
 180{
 181    CPUState *env = cpu_single_env;
 182
 183    data[0] = env->regs[R_EAX]; data[1] = env->regs[R_EBX];
 184    data[2] = env->regs[R_ECX]; data[3] = env->regs[R_EDX];
 185    data[4] = env->regs[R_ESI]; data[5] = env->regs[R_EDI];
 186
 187    DPRINTF("get_data = {%x, %x, %x, %x, %x, %x}\n",
 188            data[0], data[1], data[2], data[3], data[4], data[5]);
 189}
 190
 191static void vmmouse_set_data(const uint32_t *data)
 192{
 193    CPUState *env = cpu_single_env;
 194
 195    DPRINTF("set_data = {%x, %x, %x, %x, %x, %x}\n",
 196            data[0], data[1], data[2], data[3], data[4], data[5]);
 197
 198    env->regs[R_EAX] = data[0]; env->regs[R_EBX] = data[1];
 199    env->regs[R_ECX] = data[2]; env->regs[R_EDX] = data[3];
 200    env->regs[R_ESI] = data[4]; env->regs[R_EDI] = data[5];
 201}
 202
 203static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr)
 204{
 205    VMMouseState *s = opaque;
 206    uint32_t data[6];
 207    uint16_t command;
 208
 209    vmmouse_get_data(data);
 210
 211    command = data[2] & 0xFFFF;
 212
 213    switch (command) {
 214    case VMMOUSE_STATUS:
 215        data[0] = vmmouse_get_status(s);
 216        break;
 217    case VMMOUSE_COMMAND:
 218        switch (data[1]) {
 219        case VMMOUSE_DISABLE:
 220            vmmouse_disable(s);
 221            break;
 222        case VMMOUSE_READ_ID:
 223            vmmouse_read_id(s);
 224            break;
 225        case VMMOUSE_REQUEST_RELATIVE:
 226            vmmouse_request_relative(s);
 227            break;
 228        case VMMOUSE_REQUEST_ABSOLUTE:
 229            vmmouse_request_absolute(s);
 230            break;
 231        default:
 232            printf("vmmouse: unknown command %x\n", data[1]);
 233            break;
 234        }
 235        break;
 236    case VMMOUSE_DATA:
 237        vmmouse_data(s, data, data[1]);
 238        break;
 239    default:
 240        printf("vmmouse: unknown command %x\n", command);
 241        break;
 242    }
 243
 244    vmmouse_set_data(data);
 245    return data[0];
 246}
 247
 248static int vmmouse_post_load(void *opaque, int version_id)
 249{
 250    VMMouseState *s = opaque;
 251
 252    vmmouse_remove_handler(s);
 253    vmmouse_update_handler(s, s->absolute);
 254    return 0;
 255}
 256
 257static const VMStateDescription vmstate_vmmouse = {
 258    .name = "vmmouse",
 259    .version_id = 0,
 260    .minimum_version_id = 0,
 261    .minimum_version_id_old = 0,
 262    .post_load = vmmouse_post_load,
 263    .fields      = (VMStateField []) {
 264        VMSTATE_INT32_EQUAL(queue_size, VMMouseState),
 265        VMSTATE_UINT32_ARRAY(queue, VMMouseState, VMMOUSE_QUEUE_SIZE),
 266        VMSTATE_UINT16(nb_queue, VMMouseState),
 267        VMSTATE_UINT16(status, VMMouseState),
 268        VMSTATE_UINT8(absolute, VMMouseState),
 269        VMSTATE_END_OF_LIST()
 270    }
 271};
 272
 273void *vmmouse_init(void *m)
 274{
 275    VMMouseState *s = NULL;
 276
 277    DPRINTF("vmmouse_init\n");
 278
 279    s = qemu_mallocz(sizeof(VMMouseState));
 280
 281    s->status = 0xffff;
 282    s->ps2_mouse = m;
 283    s->queue_size = VMMOUSE_QUEUE_SIZE;
 284
 285    vmport_register(VMMOUSE_STATUS, vmmouse_ioport_read, s);
 286    vmport_register(VMMOUSE_COMMAND, vmmouse_ioport_read, s);
 287    vmport_register(VMMOUSE_DATA, vmmouse_ioport_read, s);
 288    vmstate_register(NULL, 0, &vmstate_vmmouse, s);
 289
 290    return s;
 291}
 292