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