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