Statistics
| Branch: | Revision:

root / hw / vmmouse.c @ 87ecb68b

History | View | Annotate | Download (7.6 kB)

1 548df2ac ths
/*
2 548df2ac ths
 * QEMU VMMouse emulation
3 5fafdf24 ths
 *
4 548df2ac ths
 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
5 5fafdf24 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 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "console.h"
26 87ecb68b pbrook
#include "ps2.h"
27 87ecb68b pbrook
#include "pc.h"
28 548df2ac ths
29 548df2ac ths
/* debug only vmmouse */
30 548df2ac ths
//#define DEBUG_VMMOUSE
31 548df2ac ths
32 548df2ac ths
/* VMMouse Commands */
33 548df2ac ths
#define VMMOUSE_GETVERSION        10
34 548df2ac ths
#define VMMOUSE_DATA                39
35 548df2ac ths
#define VMMOUSE_STATUS                40
36 548df2ac ths
#define VMMOUSE_COMMAND                41
37 548df2ac ths
38 548df2ac ths
#define VMMOUSE_READ_ID                        0x45414552
39 548df2ac ths
#define VMMOUSE_DISABLE                        0x000000f5
40 548df2ac ths
#define VMMOUSE_REQUEST_RELATIVE        0x4c455252
41 548df2ac ths
#define VMMOUSE_REQUEST_ABSOLUTE        0x53424152
42 548df2ac ths
43 548df2ac ths
#define VMMOUSE_QUEUE_SIZE        1024
44 548df2ac ths
45 548df2ac ths
#define VMMOUSE_VERSION                0x3442554a
46 548df2ac ths
47 548df2ac ths
#ifdef DEBUG_VMMOUSE
48 548df2ac ths
#define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
49 548df2ac ths
#else
50 548df2ac ths
#define DPRINTF(fmt, ...) do { } while (0)
51 548df2ac ths
#endif
52 548df2ac ths
53 548df2ac ths
typedef struct _VMMouseState
54 548df2ac ths
{
55 548df2ac ths
    uint32_t queue[VMMOUSE_QUEUE_SIZE];
56 548df2ac ths
    uint16_t nb_queue;
57 548df2ac ths
    uint16_t status;
58 548df2ac ths
    uint8_t absolute;
59 548df2ac ths
    QEMUPutMouseEntry *entry;
60 548df2ac ths
    void *ps2_mouse;
61 548df2ac ths
} VMMouseState;
62 548df2ac ths
63 548df2ac ths
static uint32_t vmmouse_get_status(VMMouseState *s)
64 548df2ac ths
{
65 548df2ac ths
    DPRINTF("vmmouse_get_status()\n");
66 548df2ac ths
    return (s->status << 16) | s->nb_queue;
67 548df2ac ths
}
68 548df2ac ths
69 548df2ac ths
static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state)
70 548df2ac ths
{
71 548df2ac ths
    VMMouseState *s = opaque;
72 548df2ac ths
    int buttons = 0;
73 548df2ac ths
74 548df2ac ths
    if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4))
75 4ed59aa5 ths
        return;
76 548df2ac ths
77 548df2ac ths
    DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
78 4ed59aa5 ths
            x, y, dz, buttons_state);
79 548df2ac ths
80 548df2ac ths
    if ((buttons_state & MOUSE_EVENT_LBUTTON))
81 4ed59aa5 ths
        buttons |= 0x20;
82 548df2ac ths
    if ((buttons_state & MOUSE_EVENT_RBUTTON))
83 4ed59aa5 ths
        buttons |= 0x10;
84 548df2ac ths
    if ((buttons_state & MOUSE_EVENT_MBUTTON))
85 4ed59aa5 ths
        buttons |= 0x08;
86 548df2ac ths
87 548df2ac ths
    if (s->absolute) {
88 4ed59aa5 ths
        x <<= 1;
89 4ed59aa5 ths
        y <<= 1;
90 548df2ac ths
    }
91 548df2ac ths
92 548df2ac ths
    s->queue[s->nb_queue++] = buttons;
93 548df2ac ths
    s->queue[s->nb_queue++] = x;
94 548df2ac ths
    s->queue[s->nb_queue++] = y;
95 548df2ac ths
    s->queue[s->nb_queue++] = dz;
96 548df2ac ths
97 548df2ac ths
    /* need to still generate PS2 events to notify driver to
98 548df2ac ths
       read from queue */
99 548df2ac ths
    ps2_mouse_fake_event(s->ps2_mouse);
100 548df2ac ths
}
101 548df2ac ths
102 548df2ac ths
static void vmmouse_update_handler(VMMouseState *s)
103 548df2ac ths
{
104 548df2ac ths
    if (s->entry) {
105 4ed59aa5 ths
        qemu_remove_mouse_event_handler(s->entry);
106 4ed59aa5 ths
        s->entry = NULL;
107 548df2ac ths
    }
108 548df2ac ths
    if (s->status == 0)
109 4ed59aa5 ths
        s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event,
110 4ed59aa5 ths
                                                s, s->absolute,
111 4ed59aa5 ths
                                                "vmmouse");
112 548df2ac ths
}
113 548df2ac ths
114 548df2ac ths
static void vmmouse_read_id(VMMouseState *s)
115 548df2ac ths
{
116 548df2ac ths
    DPRINTF("vmmouse_read_id()\n");
117 548df2ac ths
118 548df2ac ths
    if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
119 4ed59aa5 ths
        return;
120 548df2ac ths
121 548df2ac ths
    s->queue[s->nb_queue++] = VMMOUSE_VERSION;
122 548df2ac ths
    s->status = 0;
123 548df2ac ths
    vmmouse_update_handler(s);
124 548df2ac ths
}
125 548df2ac ths
126 548df2ac ths
static void vmmouse_request_relative(VMMouseState *s)
127 548df2ac ths
{
128 548df2ac ths
    DPRINTF("vmmouse_request_relative()\n");
129 548df2ac ths
    s->absolute = 0;
130 548df2ac ths
    vmmouse_update_handler(s);
131 548df2ac ths
}
132 548df2ac ths
133 548df2ac ths
static void vmmouse_request_absolute(VMMouseState *s)
134 548df2ac ths
{
135 548df2ac ths
    DPRINTF("vmmouse_request_absolute()\n");
136 548df2ac ths
    s->absolute = 1;
137 548df2ac ths
    vmmouse_update_handler(s);
138 548df2ac ths
}
139 548df2ac ths
140 548df2ac ths
static void vmmouse_disable(VMMouseState *s)
141 548df2ac ths
{
142 548df2ac ths
    DPRINTF("vmmouse_disable()\n");
143 548df2ac ths
    s->status = 0xffff;
144 548df2ac ths
    vmmouse_update_handler(s);
145 548df2ac ths
}
146 548df2ac ths
147 548df2ac ths
static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size)
148 548df2ac ths
{
149 548df2ac ths
    int i;
150 548df2ac ths
151 548df2ac ths
    DPRINTF("vmmouse_data(%d)\n", size);
152 548df2ac ths
153 548df2ac ths
    if (size == 0 || size > 6 || size > s->nb_queue) {
154 4ed59aa5 ths
        printf("vmmouse: driver requested too much data %d\n", size);
155 4ed59aa5 ths
        s->status = 0xffff;
156 4ed59aa5 ths
        vmmouse_update_handler(s);
157 4ed59aa5 ths
        return;
158 548df2ac ths
    }
159 548df2ac ths
160 548df2ac ths
    for (i = 0; i < size; i++)
161 4ed59aa5 ths
        data[i] = s->queue[i];
162 548df2ac ths
163 548df2ac ths
    s->nb_queue -= size;
164 548df2ac ths
    if (s->nb_queue)
165 4ed59aa5 ths
        memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue);
166 548df2ac ths
}
167 548df2ac ths
168 548df2ac ths
static void vmmouse_get_data(uint32_t *data)
169 548df2ac ths
{
170 548df2ac ths
    CPUState *env = cpu_single_env;
171 548df2ac ths
172 548df2ac ths
    data[0] = env->regs[R_EAX]; data[1] = env->regs[R_EBX];
173 548df2ac ths
    data[2] = env->regs[R_ECX]; data[3] = env->regs[R_EDX];
174 548df2ac ths
    data[4] = env->regs[R_ESI]; data[5] = env->regs[R_EDI];
175 548df2ac ths
176 548df2ac ths
    DPRINTF("get_data = {%x, %x, %x, %x, %x, %x}\n",
177 4ed59aa5 ths
            data[0], data[1], data[2], data[3], data[4], data[5]);
178 548df2ac ths
}
179 548df2ac ths
180 548df2ac ths
static void vmmouse_set_data(const uint32_t *data)
181 548df2ac ths
{
182 548df2ac ths
    CPUState *env = cpu_single_env;
183 548df2ac ths
184 548df2ac ths
    DPRINTF("set_data = {%x, %x, %x, %x, %x, %x}\n",
185 4ed59aa5 ths
            data[0], data[1], data[2], data[3], data[4], data[5]);
186 548df2ac ths
187 548df2ac ths
    env->regs[R_EAX] = data[0]; env->regs[R_EBX] = data[1];
188 548df2ac ths
    env->regs[R_ECX] = data[2]; env->regs[R_EDX] = data[3];
189 548df2ac ths
    env->regs[R_ESI] = data[4]; env->regs[R_EDI] = data[5];
190 548df2ac ths
}
191 548df2ac ths
192 548df2ac ths
static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr)
193 548df2ac ths
{
194 548df2ac ths
    VMMouseState *s = opaque;
195 548df2ac ths
    uint32_t data[6];
196 548df2ac ths
    uint16_t command;
197 548df2ac ths
198 548df2ac ths
    vmmouse_get_data(data);
199 548df2ac ths
200 548df2ac ths
    command = data[2] & 0xFFFF;
201 548df2ac ths
202 548df2ac ths
    switch (command) {
203 548df2ac ths
    case VMMOUSE_STATUS:
204 4ed59aa5 ths
        data[0] = vmmouse_get_status(s);
205 4ed59aa5 ths
        break;
206 548df2ac ths
    case VMMOUSE_COMMAND:
207 4ed59aa5 ths
        switch (data[1]) {
208 4ed59aa5 ths
        case VMMOUSE_DISABLE:
209 4ed59aa5 ths
            vmmouse_disable(s);
210 4ed59aa5 ths
            break;
211 4ed59aa5 ths
        case VMMOUSE_READ_ID:
212 4ed59aa5 ths
            vmmouse_read_id(s);
213 4ed59aa5 ths
            break;
214 4ed59aa5 ths
        case VMMOUSE_REQUEST_RELATIVE:
215 4ed59aa5 ths
            vmmouse_request_relative(s);
216 4ed59aa5 ths
            break;
217 4ed59aa5 ths
        case VMMOUSE_REQUEST_ABSOLUTE:
218 4ed59aa5 ths
            vmmouse_request_absolute(s);
219 4ed59aa5 ths
            break;
220 4ed59aa5 ths
        default:
221 4ed59aa5 ths
            printf("vmmouse: unknown command %x\n", data[1]);
222 4ed59aa5 ths
            break;
223 4ed59aa5 ths
        }
224 4ed59aa5 ths
        break;
225 548df2ac ths
    case VMMOUSE_DATA:
226 4ed59aa5 ths
        vmmouse_data(s, data, data[1]);
227 4ed59aa5 ths
        break;
228 548df2ac ths
    default:
229 4ed59aa5 ths
        printf("vmmouse: unknown command %x\n", command);
230 4ed59aa5 ths
        break;
231 548df2ac ths
    }
232 548df2ac ths
233 548df2ac ths
    vmmouse_set_data(data);
234 548df2ac ths
    return data[0];
235 548df2ac ths
}
236 548df2ac ths
237 548df2ac ths
static void vmmouse_save(QEMUFile *f, void *opaque)
238 548df2ac ths
{
239 548df2ac ths
    VMMouseState *s = opaque;
240 548df2ac ths
    int i;
241 548df2ac ths
242 548df2ac ths
    qemu_put_be32(f, VMMOUSE_QUEUE_SIZE);
243 548df2ac ths
    for (i = 0; i < VMMOUSE_QUEUE_SIZE; i++)
244 4ed59aa5 ths
        qemu_put_be32s(f, &s->queue[i]);
245 548df2ac ths
    qemu_put_be16s(f, &s->nb_queue);
246 548df2ac ths
    qemu_put_be16s(f, &s->status);
247 548df2ac ths
    qemu_put_8s(f, &s->absolute);
248 548df2ac ths
}
249 548df2ac ths
250 548df2ac ths
static int vmmouse_load(QEMUFile *f, void *opaque, int version_id)
251 548df2ac ths
{
252 548df2ac ths
    VMMouseState *s = opaque;
253 548df2ac ths
    int i;
254 548df2ac ths
255 548df2ac ths
    if (version_id != 0)
256 548df2ac ths
        return -EINVAL;
257 548df2ac ths
258 548df2ac ths
    if (qemu_get_be32(f) != VMMOUSE_QUEUE_SIZE)
259 4ed59aa5 ths
        return -EINVAL;
260 548df2ac ths
    for (i = 0; i < VMMOUSE_QUEUE_SIZE; i++)
261 4ed59aa5 ths
        qemu_get_be32s(f, &s->queue[i]);
262 548df2ac ths
    qemu_get_be16s(f, &s->nb_queue);
263 548df2ac ths
    qemu_get_be16s(f, &s->status);
264 548df2ac ths
    qemu_get_8s(f, &s->absolute);
265 548df2ac ths
266 548df2ac ths
    vmmouse_update_handler(s);
267 548df2ac ths
268 548df2ac ths
    return 0;
269 548df2ac ths
}
270 548df2ac ths
271 548df2ac ths
void *vmmouse_init(void *m)
272 548df2ac ths
{
273 548df2ac ths
    VMMouseState *s = NULL;
274 548df2ac ths
275 548df2ac ths
    DPRINTF("vmmouse_init\n");
276 548df2ac ths
277 548df2ac ths
    s = qemu_mallocz(sizeof(VMMouseState));
278 548df2ac ths
    if (!s)
279 4ed59aa5 ths
        return NULL;
280 548df2ac ths
281 548df2ac ths
    s->status = 0xffff;
282 548df2ac ths
    s->ps2_mouse = m;
283 548df2ac ths
284 93342807 ths
    vmport_register(VMMOUSE_STATUS, vmmouse_ioport_read, s);
285 93342807 ths
    vmport_register(VMMOUSE_COMMAND, vmmouse_ioport_read, s);
286 93342807 ths
    vmport_register(VMMOUSE_DATA, vmmouse_ioport_read, s);
287 548df2ac ths
    register_savevm("vmmouse", 0, 0, vmmouse_save, vmmouse_load, s);
288 548df2ac ths
289 548df2ac ths
    return s;
290 548df2ac ths
}