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
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"
33 #include "qom/object.h"
35 /* debug only vmmouse */
36 //#define DEBUG_VMMOUSE
38 #define VMMOUSE_READ_ID 0x45414552
39 #define VMMOUSE_DISABLE 0x000000f5
40 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252
41 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152
43 #define VMMOUSE_QUEUE_SIZE 1024
45 #define VMMOUSE_VERSION 0x3442554a
48 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
50 #define DPRINTF(fmt, ...) do { } while (0)
53 #define TYPE_VMMOUSE "vmmouse"
54 OBJECT_DECLARE_SIMPLE_TYPE(VMMouseState
, VMMOUSE
)
59 uint32_t queue
[VMMOUSE_QUEUE_SIZE
];
64 QEMUPutMouseEntry
*entry
;
68 static void vmmouse_get_data(uint32_t *data
)
70 X86CPU
*cpu
= X86_CPU(current_cpu
);
71 CPUX86State
*env
= &cpu
->env
;
73 data
[0] = env
->regs
[R_EAX
]; data
[1] = env
->regs
[R_EBX
];
74 data
[2] = env
->regs
[R_ECX
]; data
[3] = env
->regs
[R_EDX
];
75 data
[4] = env
->regs
[R_ESI
]; data
[5] = env
->regs
[R_EDI
];
78 static void vmmouse_set_data(const uint32_t *data
)
80 X86CPU
*cpu
= X86_CPU(current_cpu
);
81 CPUX86State
*env
= &cpu
->env
;
83 env
->regs
[R_EAX
] = data
[0]; env
->regs
[R_EBX
] = data
[1];
84 env
->regs
[R_ECX
] = data
[2]; env
->regs
[R_EDX
] = data
[3];
85 env
->regs
[R_ESI
] = data
[4]; env
->regs
[R_EDI
] = data
[5];
88 static uint32_t vmmouse_get_status(VMMouseState
*s
)
90 DPRINTF("vmmouse_get_status()\n");
91 return (s
->status
<< 16) | s
->nb_queue
;
94 static void vmmouse_mouse_event(void *opaque
, int x
, int y
, int dz
, int buttons_state
)
96 VMMouseState
*s
= opaque
;
99 if (s
->nb_queue
> (VMMOUSE_QUEUE_SIZE
- 4))
102 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
103 x
, y
, dz
, buttons_state
);
105 if ((buttons_state
& MOUSE_EVENT_LBUTTON
))
107 if ((buttons_state
& MOUSE_EVENT_RBUTTON
))
109 if ((buttons_state
& MOUSE_EVENT_MBUTTON
))
117 s
->queue
[s
->nb_queue
++] = buttons
;
118 s
->queue
[s
->nb_queue
++] = x
;
119 s
->queue
[s
->nb_queue
++] = y
;
120 s
->queue
[s
->nb_queue
++] = dz
;
122 /* need to still generate PS2 events to notify driver to
124 i8042_isa_mouse_fake_event(s
->i8042
);
127 static void vmmouse_remove_handler(VMMouseState
*s
)
130 qemu_remove_mouse_event_handler(s
->entry
);
135 static void vmmouse_update_handler(VMMouseState
*s
, int absolute
)
137 if (s
->status
!= 0) {
140 if (s
->absolute
!= absolute
) {
141 s
->absolute
= absolute
;
142 vmmouse_remove_handler(s
);
144 if (s
->entry
== NULL
) {
145 s
->entry
= qemu_add_mouse_event_handler(vmmouse_mouse_event
,
148 qemu_activate_mouse_event_handler(s
->entry
);
152 static void vmmouse_read_id(VMMouseState
*s
)
154 DPRINTF("vmmouse_read_id()\n");
156 if (s
->nb_queue
== VMMOUSE_QUEUE_SIZE
)
159 s
->queue
[s
->nb_queue
++] = VMMOUSE_VERSION
;
161 vmmouse_update_handler(s
, s
->absolute
);
164 static void vmmouse_request_relative(VMMouseState
*s
)
166 DPRINTF("vmmouse_request_relative()\n");
167 vmmouse_update_handler(s
, 0);
170 static void vmmouse_request_absolute(VMMouseState
*s
)
172 DPRINTF("vmmouse_request_absolute()\n");
173 vmmouse_update_handler(s
, 1);
176 static void vmmouse_disable(VMMouseState
*s
)
178 DPRINTF("vmmouse_disable()\n");
180 vmmouse_remove_handler(s
);
183 static void vmmouse_data(VMMouseState
*s
, uint32_t *data
, uint32_t size
)
187 DPRINTF("vmmouse_data(%d)\n", size
);
189 if (size
== 0 || size
> 6 || size
> s
->nb_queue
) {
190 printf("vmmouse: driver requested too much data %d\n", size
);
192 vmmouse_remove_handler(s
);
196 for (i
= 0; i
< size
; i
++)
197 data
[i
] = s
->queue
[i
];
201 memmove(s
->queue
, &s
->queue
[size
], sizeof(s
->queue
[0]) * s
->nb_queue
);
204 static uint32_t vmmouse_ioport_read(void *opaque
, uint32_t addr
)
206 VMMouseState
*s
= opaque
;
210 vmmouse_get_data(data
);
212 command
= data
[2] & 0xFFFF;
215 case VMPORT_CMD_VMMOUSE_STATUS
:
216 data
[0] = vmmouse_get_status(s
);
218 case VMPORT_CMD_VMMOUSE_COMMAND
:
220 case VMMOUSE_DISABLE
:
223 case VMMOUSE_READ_ID
:
226 case VMMOUSE_REQUEST_RELATIVE
:
227 vmmouse_request_relative(s
);
229 case VMMOUSE_REQUEST_ABSOLUTE
:
230 vmmouse_request_absolute(s
);
233 printf("vmmouse: unknown command %x\n", data
[1]);
237 case VMPORT_CMD_VMMOUSE_DATA
:
238 vmmouse_data(s
, data
, data
[1]);
241 printf("vmmouse: unknown command %x\n", command
);
245 vmmouse_set_data(data
);
249 static int vmmouse_post_load(void *opaque
, int version_id
)
251 VMMouseState
*s
= opaque
;
253 vmmouse_remove_handler(s
);
254 vmmouse_update_handler(s
, s
->absolute
);
258 static const VMStateDescription vmstate_vmmouse
= {
261 .minimum_version_id
= 0,
262 .post_load
= vmmouse_post_load
,
263 .fields
= (VMStateField
[]) {
264 VMSTATE_INT32_EQUAL(queue_size
, VMMouseState
, NULL
),
265 VMSTATE_UINT32_ARRAY(queue
, VMMouseState
, VMMOUSE_QUEUE_SIZE
),
266 VMSTATE_UINT16(nb_queue
, VMMouseState
),
267 VMSTATE_UINT16(status
, VMMouseState
),
268 VMSTATE_UINT8(absolute
, VMMouseState
),
269 VMSTATE_END_OF_LIST()
273 static void vmmouse_reset(DeviceState
*d
)
275 VMMouseState
*s
= VMMOUSE(d
);
277 s
->queue_size
= VMMOUSE_QUEUE_SIZE
;
283 static void vmmouse_realizefn(DeviceState
*dev
, Error
**errp
)
285 VMMouseState
*s
= VMMOUSE(dev
);
287 DPRINTF("vmmouse_init\n");
290 error_setg(errp
, "'i8042' link is not set");
293 if (!object_resolve_path_type("", TYPE_VMPORT
, NULL
)) {
294 error_setg(errp
, "vmmouse needs a machine with vmport");
298 vmport_register(VMPORT_CMD_VMMOUSE_STATUS
, vmmouse_ioport_read
, s
);
299 vmport_register(VMPORT_CMD_VMMOUSE_COMMAND
, vmmouse_ioport_read
, s
);
300 vmport_register(VMPORT_CMD_VMMOUSE_DATA
, vmmouse_ioport_read
, s
);
303 static Property vmmouse_properties
[] = {
304 DEFINE_PROP_LINK("i8042", VMMouseState
, i8042
, TYPE_I8042
, ISAKBDState
*),
305 DEFINE_PROP_END_OF_LIST(),
308 static void vmmouse_class_initfn(ObjectClass
*klass
, void *data
)
310 DeviceClass
*dc
= DEVICE_CLASS(klass
);
312 dc
->realize
= vmmouse_realizefn
;
313 dc
->reset
= vmmouse_reset
;
314 dc
->vmsd
= &vmstate_vmmouse
;
315 device_class_set_props(dc
, vmmouse_properties
);
316 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
319 static const TypeInfo vmmouse_info
= {
320 .name
= TYPE_VMMOUSE
,
321 .parent
= TYPE_ISA_DEVICE
,
322 .instance_size
= sizeof(VMMouseState
),
323 .class_init
= vmmouse_class_initfn
,
326 static void vmmouse_register_types(void)
328 type_register_static(&vmmouse_info
);
331 type_init(vmmouse_register_types
)