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