Statistics
| Branch: | Revision:

root / hw / vmmouse.c @ 5fafdf24

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