1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "hw/hw.h"
26#include "ui/console.h"
27#include "hw/input/ps2.h"
28#include "hw/i386/pc.h"
29#include "hw/qdev.h"
30
31
32
33
34
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
58typedef 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
71static uint32_t vmmouse_get_status(VMMouseState *s)
72{
73 DPRINTF("vmmouse_get_status()\n");
74 return (s->status << 16) | s->nb_queue;
75}
76
77static 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
106
107 i8042_isa_mouse_fake_event(s->ps2_mouse);
108}
109
110static 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
118static 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
135static 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
146static void vmmouse_request_relative(VMMouseState *s)
147{
148 DPRINTF("vmmouse_request_relative()\n");
149 vmmouse_update_handler(s, 0);
150}
151
152static void vmmouse_request_absolute(VMMouseState *s)
153{
154 DPRINTF("vmmouse_request_absolute()\n");
155 vmmouse_update_handler(s, 1);
156}
157
158static void vmmouse_disable(VMMouseState *s)
159{
160 DPRINTF("vmmouse_disable()\n");
161 s->status = 0xffff;
162 vmmouse_remove_handler(s);
163}
164
165static 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
186static 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
231static 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
240static 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),
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
255static 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
264static 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
275static Property vmmouse_properties[] = {
276 DEFINE_PROP_PTR("ps2_mouse", VMMouseState, ps2_mouse),
277 DEFINE_PROP_END_OF_LIST(),
278};
279
280static 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
289 dc->cannot_instantiate_with_device_add_yet = true;
290}
291
292static 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
299static void vmmouse_register_types(void)
300{
301 type_register_static(&vmmouse_info);
302}
303
304type_init(vmmouse_register_types)
305