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