Statistics
| Branch: | Revision:

root / hw / vmmouse.c @ fcb4a419

History | View | Annotate | Download (7.3 kB)

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