xref: /openbmc/qemu/hw/i386/vmmouse.c (revision 885f380f7bec732b3615d2698e493fab617d46de)
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 TYPE_VMMOUSE "vmmouse"
56 OBJECT_DECLARE_SIMPLE_TYPE(VMMouseState, 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     trace_vmmouse_get_status();
93 
94     return (s->status << 16) | s->nb_queue;
95 }
96 
97 static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state)
98 {
99     VMMouseState *s = opaque;
100     int buttons = 0;
101 
102     if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4))
103         return;
104 
105     trace_vmmouse_mouse_event(x, y, dz, buttons_state);
106 
107     if ((buttons_state & MOUSE_EVENT_LBUTTON))
108         buttons |= VMMOUSE_LEFT_BUTTON;
109     if ((buttons_state & MOUSE_EVENT_RBUTTON))
110         buttons |= VMMOUSE_RIGHT_BUTTON;
111     if ((buttons_state & MOUSE_EVENT_MBUTTON))
112         buttons |= VMMOUSE_MIDDLE_BUTTON;
113 
114     if (s->absolute) {
115         x <<= 1;
116         y <<= 1;
117     } else{
118         /* add for guest vmmouse driver to judge this is a relative packet. */
119         buttons |= VMMOUSE_RELATIVE_PACKET;
120     }
121 
122     s->queue[s->nb_queue++] = buttons;
123     s->queue[s->nb_queue++] = x;
124     s->queue[s->nb_queue++] = y;
125     s->queue[s->nb_queue++] = dz;
126 
127     /* need to still generate PS2 events to notify driver to
128        read from queue */
129     i8042_isa_mouse_fake_event(s->i8042);
130 }
131 
132 static void vmmouse_remove_handler(VMMouseState *s)
133 {
134     if (s->entry) {
135         qemu_remove_mouse_event_handler(s->entry);
136         s->entry = NULL;
137     }
138 }
139 
140 static void vmmouse_update_handler(VMMouseState *s, int absolute)
141 {
142     if (s->status != 0) {
143         return;
144     }
145     if (s->absolute != absolute) {
146         s->absolute = absolute;
147         vmmouse_remove_handler(s);
148     }
149     if (s->entry == NULL) {
150         s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event,
151                                                 s, s->absolute,
152                                                 "vmmouse");
153         qemu_activate_mouse_event_handler(s->entry);
154     }
155 }
156 
157 static void vmmouse_read_id(VMMouseState *s)
158 {
159     trace_vmmouse_read_id();
160 
161     if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
162         return;
163 
164     s->queue[s->nb_queue++] = VMMOUSE_VERSION;
165     s->status = 0;
166     vmmouse_update_handler(s, s->absolute);
167 }
168 
169 static void vmmouse_request_relative(VMMouseState *s)
170 {
171     trace_vmmouse_request_relative();
172 
173     vmmouse_update_handler(s, 0);
174 }
175 
176 static void vmmouse_request_absolute(VMMouseState *s)
177 {
178     trace_vmmouse_request_absolute();
179 
180     vmmouse_update_handler(s, 1);
181 }
182 
183 static void vmmouse_disable(VMMouseState *s)
184 {
185     trace_vmmouse_disable();
186 
187     s->status = 0xffff;
188     vmmouse_remove_handler(s);
189 }
190 
191 static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size)
192 {
193     int i;
194 
195     trace_vmmouse_data(size);
196 
197     if (size == 0 || size > 6 || size > s->nb_queue) {
198         printf("vmmouse: driver requested too much data %d\n", size);
199         s->status = 0xffff;
200         vmmouse_remove_handler(s);
201         return;
202     }
203 
204     for (i = 0; i < size; i++)
205         data[i] = s->queue[i];
206 
207     s->nb_queue -= size;
208     if (s->nb_queue)
209         memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue);
210 }
211 
212 static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr)
213 {
214     VMMouseState *s = opaque;
215     uint32_t data[6];
216     uint16_t command;
217 
218     vmmouse_get_data(data);
219 
220     command = data[2] & 0xFFFF;
221 
222     switch (command) {
223     case VMPORT_CMD_VMMOUSE_STATUS:
224         data[0] = vmmouse_get_status(s);
225         break;
226     case VMPORT_CMD_VMMOUSE_COMMAND:
227         switch (data[1]) {
228         case VMMOUSE_DISABLE:
229             vmmouse_disable(s);
230             break;
231         case VMMOUSE_READ_ID:
232             vmmouse_read_id(s);
233             break;
234         case VMMOUSE_REQUEST_RELATIVE:
235             vmmouse_request_relative(s);
236             break;
237         case VMMOUSE_REQUEST_ABSOLUTE:
238             vmmouse_request_absolute(s);
239             break;
240         default:
241             printf("vmmouse: unknown command %x\n", data[1]);
242             break;
243         }
244         break;
245     case VMPORT_CMD_VMMOUSE_DATA:
246         vmmouse_data(s, data, data[1]);
247         break;
248     default:
249         printf("vmmouse: unknown command %x\n", command);
250         break;
251     }
252 
253     vmmouse_set_data(data);
254     return data[0];
255 }
256 
257 static int vmmouse_post_load(void *opaque, int version_id)
258 {
259     VMMouseState *s = opaque;
260 
261     vmmouse_remove_handler(s);
262     vmmouse_update_handler(s, s->absolute);
263     return 0;
264 }
265 
266 static const VMStateDescription vmstate_vmmouse = {
267     .name = "vmmouse",
268     .version_id = 0,
269     .minimum_version_id = 0,
270     .post_load = vmmouse_post_load,
271     .fields = (VMStateField[]) {
272         VMSTATE_INT32_EQUAL(queue_size, VMMouseState, NULL),
273         VMSTATE_UINT32_ARRAY(queue, VMMouseState, VMMOUSE_QUEUE_SIZE),
274         VMSTATE_UINT16(nb_queue, VMMouseState),
275         VMSTATE_UINT16(status, VMMouseState),
276         VMSTATE_UINT8(absolute, VMMouseState),
277         VMSTATE_END_OF_LIST()
278     }
279 };
280 
281 static void vmmouse_reset(DeviceState *d)
282 {
283     VMMouseState *s = VMMOUSE(d);
284 
285     s->queue_size = VMMOUSE_QUEUE_SIZE;
286     s->nb_queue = 0;
287 
288     vmmouse_disable(s);
289 }
290 
291 static void vmmouse_realizefn(DeviceState *dev, Error **errp)
292 {
293     VMMouseState *s = VMMOUSE(dev);
294 
295     trace_vmmouse_init();
296 
297     if (!s->i8042) {
298         error_setg(errp, "'i8042' link is not set");
299         return;
300     }
301     if (!object_resolve_path_type("", TYPE_VMPORT, NULL)) {
302         error_setg(errp, "vmmouse needs a machine with vmport");
303         return;
304     }
305 
306     vmport_register(VMPORT_CMD_VMMOUSE_STATUS, vmmouse_ioport_read, s);
307     vmport_register(VMPORT_CMD_VMMOUSE_COMMAND, vmmouse_ioport_read, s);
308     vmport_register(VMPORT_CMD_VMMOUSE_DATA, vmmouse_ioport_read, s);
309 }
310 
311 static Property vmmouse_properties[] = {
312     DEFINE_PROP_LINK("i8042", VMMouseState, i8042, TYPE_I8042, ISAKBDState *),
313     DEFINE_PROP_END_OF_LIST(),
314 };
315 
316 static void vmmouse_class_initfn(ObjectClass *klass, void *data)
317 {
318     DeviceClass *dc = DEVICE_CLASS(klass);
319 
320     dc->realize = vmmouse_realizefn;
321     dc->reset = vmmouse_reset;
322     dc->vmsd = &vmstate_vmmouse;
323     device_class_set_props(dc, vmmouse_properties);
324     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
325 }
326 
327 static const TypeInfo vmmouse_info = {
328     .name          = TYPE_VMMOUSE,
329     .parent        = TYPE_ISA_DEVICE,
330     .instance_size = sizeof(VMMouseState),
331     .class_init    = vmmouse_class_initfn,
332 };
333 
334 static void vmmouse_register_types(void)
335 {
336     type_register_static(&vmmouse_info);
337 }
338 
339 type_init(vmmouse_register_types)
340