xref: /openbmc/qemu/hw/i386/vmmouse.c (revision 65a117da)
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 
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "ui/console.h"
28 #include "hw/input/i8042.h"
29 #include "hw/qdev-properties.h"
30 #include "migration/vmstate.h"
31 #include "vmport.h"
32 #include "cpu.h"
33 
34 /* debug only vmmouse */
35 //#define DEBUG_VMMOUSE
36 
37 /* VMMouse Commands */
38 #define VMMOUSE_GETVERSION	10
39 #define VMMOUSE_DATA		39
40 #define VMMOUSE_STATUS		40
41 #define VMMOUSE_COMMAND		41
42 
43 #define VMMOUSE_READ_ID			0x45414552
44 #define VMMOUSE_DISABLE			0x000000f5
45 #define VMMOUSE_REQUEST_RELATIVE	0x4c455252
46 #define VMMOUSE_REQUEST_ABSOLUTE	0x53424152
47 
48 #define VMMOUSE_QUEUE_SIZE	1024
49 
50 #define VMMOUSE_VERSION		0x3442554a
51 
52 #ifdef DEBUG_VMMOUSE
53 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
54 #else
55 #define DPRINTF(fmt, ...) do { } while (0)
56 #endif
57 
58 #define TYPE_VMMOUSE "vmmouse"
59 #define VMMOUSE(obj) OBJECT_CHECK(VMMouseState, (obj), TYPE_VMMOUSE)
60 
61 typedef struct VMMouseState
62 {
63     ISADevice parent_obj;
64 
65     uint32_t queue[VMMOUSE_QUEUE_SIZE];
66     int32_t queue_size;
67     uint16_t nb_queue;
68     uint16_t status;
69     uint8_t absolute;
70     QEMUPutMouseEntry *entry;
71     ISAKBDState *i8042;
72 } VMMouseState;
73 
74 static void vmmouse_get_data(uint32_t *data)
75 {
76     X86CPU *cpu = X86_CPU(current_cpu);
77     CPUX86State *env = &cpu->env;
78 
79     data[0] = env->regs[R_EAX]; data[1] = env->regs[R_EBX];
80     data[2] = env->regs[R_ECX]; data[3] = env->regs[R_EDX];
81     data[4] = env->regs[R_ESI]; data[5] = env->regs[R_EDI];
82 }
83 
84 static void vmmouse_set_data(const uint32_t *data)
85 {
86     X86CPU *cpu = X86_CPU(current_cpu);
87     CPUX86State *env = &cpu->env;
88 
89     env->regs[R_EAX] = data[0]; env->regs[R_EBX] = data[1];
90     env->regs[R_ECX] = data[2]; env->regs[R_EDX] = data[3];
91     env->regs[R_ESI] = data[4]; env->regs[R_EDI] = data[5];
92 }
93 
94 static uint32_t vmmouse_get_status(VMMouseState *s)
95 {
96     DPRINTF("vmmouse_get_status()\n");
97     return (s->status << 16) | s->nb_queue;
98 }
99 
100 static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state)
101 {
102     VMMouseState *s = opaque;
103     int buttons = 0;
104 
105     if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4))
106         return;
107 
108     DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
109             x, y, dz, buttons_state);
110 
111     if ((buttons_state & MOUSE_EVENT_LBUTTON))
112         buttons |= 0x20;
113     if ((buttons_state & MOUSE_EVENT_RBUTTON))
114         buttons |= 0x10;
115     if ((buttons_state & MOUSE_EVENT_MBUTTON))
116         buttons |= 0x08;
117 
118     if (s->absolute) {
119         x <<= 1;
120         y <<= 1;
121     }
122 
123     s->queue[s->nb_queue++] = buttons;
124     s->queue[s->nb_queue++] = x;
125     s->queue[s->nb_queue++] = y;
126     s->queue[s->nb_queue++] = dz;
127 
128     /* need to still generate PS2 events to notify driver to
129        read from queue */
130     i8042_isa_mouse_fake_event(s->i8042);
131 }
132 
133 static void vmmouse_remove_handler(VMMouseState *s)
134 {
135     if (s->entry) {
136         qemu_remove_mouse_event_handler(s->entry);
137         s->entry = NULL;
138     }
139 }
140 
141 static void vmmouse_update_handler(VMMouseState *s, int absolute)
142 {
143     if (s->status != 0) {
144         return;
145     }
146     if (s->absolute != absolute) {
147         s->absolute = absolute;
148         vmmouse_remove_handler(s);
149     }
150     if (s->entry == NULL) {
151         s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event,
152                                                 s, s->absolute,
153                                                 "vmmouse");
154         qemu_activate_mouse_event_handler(s->entry);
155     }
156 }
157 
158 static void vmmouse_read_id(VMMouseState *s)
159 {
160     DPRINTF("vmmouse_read_id()\n");
161 
162     if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
163         return;
164 
165     s->queue[s->nb_queue++] = VMMOUSE_VERSION;
166     s->status = 0;
167 }
168 
169 static void vmmouse_request_relative(VMMouseState *s)
170 {
171     DPRINTF("vmmouse_request_relative()\n");
172     vmmouse_update_handler(s, 0);
173 }
174 
175 static void vmmouse_request_absolute(VMMouseState *s)
176 {
177     DPRINTF("vmmouse_request_absolute()\n");
178     vmmouse_update_handler(s, 1);
179 }
180 
181 static void vmmouse_disable(VMMouseState *s)
182 {
183     DPRINTF("vmmouse_disable()\n");
184     s->status = 0xffff;
185     vmmouse_remove_handler(s);
186 }
187 
188 static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size)
189 {
190     int i;
191 
192     DPRINTF("vmmouse_data(%d)\n", size);
193 
194     if (size == 0 || size > 6 || size > s->nb_queue) {
195         printf("vmmouse: driver requested too much data %d\n", size);
196         s->status = 0xffff;
197         vmmouse_remove_handler(s);
198         return;
199     }
200 
201     for (i = 0; i < size; i++)
202         data[i] = s->queue[i];
203 
204     s->nb_queue -= size;
205     if (s->nb_queue)
206         memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue);
207 }
208 
209 static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr)
210 {
211     VMMouseState *s = opaque;
212     uint32_t data[6];
213     uint16_t command;
214 
215     vmmouse_get_data(data);
216 
217     command = data[2] & 0xFFFF;
218 
219     switch (command) {
220     case VMMOUSE_STATUS:
221         data[0] = vmmouse_get_status(s);
222         break;
223     case VMMOUSE_COMMAND:
224         switch (data[1]) {
225         case VMMOUSE_DISABLE:
226             vmmouse_disable(s);
227             break;
228         case VMMOUSE_READ_ID:
229             vmmouse_read_id(s);
230             break;
231         case VMMOUSE_REQUEST_RELATIVE:
232             vmmouse_request_relative(s);
233             break;
234         case VMMOUSE_REQUEST_ABSOLUTE:
235             vmmouse_request_absolute(s);
236             break;
237         default:
238             printf("vmmouse: unknown command %x\n", data[1]);
239             break;
240         }
241         break;
242     case VMMOUSE_DATA:
243         vmmouse_data(s, data, data[1]);
244         break;
245     default:
246         printf("vmmouse: unknown command %x\n", command);
247         break;
248     }
249 
250     vmmouse_set_data(data);
251     return data[0];
252 }
253 
254 static int vmmouse_post_load(void *opaque, int version_id)
255 {
256     VMMouseState *s = opaque;
257 
258     vmmouse_remove_handler(s);
259     vmmouse_update_handler(s, s->absolute);
260     return 0;
261 }
262 
263 static const VMStateDescription vmstate_vmmouse = {
264     .name = "vmmouse",
265     .version_id = 0,
266     .minimum_version_id = 0,
267     .post_load = vmmouse_post_load,
268     .fields = (VMStateField[]) {
269         VMSTATE_INT32_EQUAL(queue_size, VMMouseState, NULL),
270         VMSTATE_UINT32_ARRAY(queue, VMMouseState, VMMOUSE_QUEUE_SIZE),
271         VMSTATE_UINT16(nb_queue, VMMouseState),
272         VMSTATE_UINT16(status, VMMouseState),
273         VMSTATE_UINT8(absolute, VMMouseState),
274         VMSTATE_END_OF_LIST()
275     }
276 };
277 
278 static void vmmouse_reset(DeviceState *d)
279 {
280     VMMouseState *s = VMMOUSE(d);
281 
282     s->queue_size = VMMOUSE_QUEUE_SIZE;
283     s->nb_queue = 0;
284 
285     vmmouse_disable(s);
286 }
287 
288 static void vmmouse_realizefn(DeviceState *dev, Error **errp)
289 {
290     VMMouseState *s = VMMOUSE(dev);
291 
292     DPRINTF("vmmouse_init\n");
293 
294     if (!object_resolve_path_type("", TYPE_VMPORT, NULL)) {
295         error_setg(errp, "vmmouse needs a machine with vmport");
296         return;
297     }
298 
299     vmport_register(VMMOUSE_STATUS, vmmouse_ioport_read, s);
300     vmport_register(VMMOUSE_COMMAND, vmmouse_ioport_read, s);
301     vmport_register(VMMOUSE_DATA, vmmouse_ioport_read, s);
302 }
303 
304 static Property vmmouse_properties[] = {
305     DEFINE_PROP_LINK("i8042", VMMouseState, i8042, TYPE_I8042, ISAKBDState *),
306     DEFINE_PROP_END_OF_LIST(),
307 };
308 
309 static void vmmouse_class_initfn(ObjectClass *klass, void *data)
310 {
311     DeviceClass *dc = DEVICE_CLASS(klass);
312 
313     dc->realize = vmmouse_realizefn;
314     dc->reset = vmmouse_reset;
315     dc->vmsd = &vmstate_vmmouse;
316     device_class_set_props(dc, vmmouse_properties);
317 }
318 
319 static const TypeInfo vmmouse_info = {
320     .name          = TYPE_VMMOUSE,
321     .parent        = TYPE_ISA_DEVICE,
322     .instance_size = sizeof(VMMouseState),
323     .class_init    = vmmouse_class_initfn,
324 };
325 
326 static void vmmouse_register_types(void)
327 {
328     type_register_static(&vmmouse_info);
329 }
330 
331 type_init(vmmouse_register_types)
332