net/announce: Add HMP optional interface list
[qemu/kevin.git] / hw / i386 / vmmouse.c
blob5d2d278be4cae7b60c9ec0bb8ce3cf049f83a25e
1 /*
2 * QEMU VMMouse emulation
4 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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"
31 /* debug only vmmouse */
32 //#define DEBUG_VMMOUSE
34 /* VMMouse Commands */
35 #define VMMOUSE_GETVERSION 10
36 #define VMMOUSE_DATA 39
37 #define VMMOUSE_STATUS 40
38 #define VMMOUSE_COMMAND 41
40 #define VMMOUSE_READ_ID 0x45414552
41 #define VMMOUSE_DISABLE 0x000000f5
42 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252
43 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152
45 #define VMMOUSE_QUEUE_SIZE 1024
47 #define VMMOUSE_VERSION 0x3442554a
49 #ifdef DEBUG_VMMOUSE
50 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
51 #else
52 #define DPRINTF(fmt, ...) do { } while (0)
53 #endif
55 #define TYPE_VMMOUSE "vmmouse"
56 #define VMMOUSE(obj) OBJECT_CHECK(VMMouseState, (obj), TYPE_VMMOUSE)
58 typedef struct VMMouseState
60 ISADevice parent_obj;
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;
71 static uint32_t vmmouse_get_status(VMMouseState *s)
73 DPRINTF("vmmouse_get_status()\n");
74 return (s->status << 16) | s->nb_queue;
77 static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state)
79 VMMouseState *s = opaque;
80 int buttons = 0;
82 if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4))
83 return;
85 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
86 x, y, dz, buttons_state);
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;
95 if (s->absolute) {
96 x <<= 1;
97 y <<= 1;
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;
105 /* need to still generate PS2 events to notify driver to
106 read from queue */
107 i8042_isa_mouse_fake_event(s->ps2_mouse);
110 static void vmmouse_remove_handler(VMMouseState *s)
112 if (s->entry) {
113 qemu_remove_mouse_event_handler(s->entry);
114 s->entry = NULL;
118 static void vmmouse_update_handler(VMMouseState *s, int absolute)
120 if (s->status != 0) {
121 return;
123 if (s->absolute != absolute) {
124 s->absolute = absolute;
125 vmmouse_remove_handler(s);
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);
135 static void vmmouse_read_id(VMMouseState *s)
137 DPRINTF("vmmouse_read_id()\n");
139 if (s->nb_queue == VMMOUSE_QUEUE_SIZE)
140 return;
142 s->queue[s->nb_queue++] = VMMOUSE_VERSION;
143 s->status = 0;
146 static void vmmouse_request_relative(VMMouseState *s)
148 DPRINTF("vmmouse_request_relative()\n");
149 vmmouse_update_handler(s, 0);
152 static void vmmouse_request_absolute(VMMouseState *s)
154 DPRINTF("vmmouse_request_absolute()\n");
155 vmmouse_update_handler(s, 1);
158 static void vmmouse_disable(VMMouseState *s)
160 DPRINTF("vmmouse_disable()\n");
161 s->status = 0xffff;
162 vmmouse_remove_handler(s);
165 static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size)
167 int i;
169 DPRINTF("vmmouse_data(%d)\n", size);
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;
178 for (i = 0; i < size; i++)
179 data[i] = s->queue[i];
181 s->nb_queue -= size;
182 if (s->nb_queue)
183 memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue);
186 static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr)
188 VMMouseState *s = opaque;
189 uint32_t data[6];
190 uint16_t command;
192 vmmouse_get_data(data);
194 command = data[2] & 0xFFFF;
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;
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;
227 vmmouse_set_data(data);
228 return data[0];
231 static int vmmouse_post_load(void *opaque, int version_id)
233 VMMouseState *s = opaque;
235 vmmouse_remove_handler(s);
236 vmmouse_update_handler(s, s->absolute);
237 return 0;
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()
255 static void vmmouse_reset(DeviceState *d)
257 VMMouseState *s = VMMOUSE(d);
259 s->queue_size = VMMOUSE_QUEUE_SIZE;
261 vmmouse_disable(s);
264 static void vmmouse_realizefn(DeviceState *dev, Error **errp)
266 VMMouseState *s = VMMOUSE(dev);
268 DPRINTF("vmmouse_init\n");
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);
275 static Property vmmouse_properties[] = {
276 DEFINE_PROP_PTR("ps2_mouse", VMMouseState, ps2_mouse),
277 DEFINE_PROP_END_OF_LIST(),
280 static void vmmouse_class_initfn(ObjectClass *klass, void *data)
282 DeviceClass *dc = DEVICE_CLASS(klass);
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;
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,
299 static void vmmouse_register_types(void)
301 type_register_static(&vmmouse_info);
304 type_init(vmmouse_register_types)