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
47 #define VMMOUSE_RELATIVE_PACKET 0x00010000
49 #define VMMOUSE_LEFT_BUTTON 0x20
50 #define VMMOUSE_RIGHT_BUTTON 0x10
51 #define VMMOUSE_MIDDLE_BUTTON 0x08
54 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
56 #define DPRINTF(fmt, ...) do { } while (0)
59 #define TYPE_VMMOUSE "vmmouse"
60 OBJECT_DECLARE_SIMPLE_TYPE(VMMouseState
, VMMOUSE
)
65 uint32_t queue
[VMMOUSE_QUEUE_SIZE
];
70 QEMUPutMouseEntry
*entry
;
74 static void vmmouse_get_data(uint32_t *data
)
76 X86CPU
*cpu
= X86_CPU(current_cpu
);
77 CPUX86State
*env
= &cpu
->env
;
79 data
[0] = env
->regs
[R_EAX
]; data
[1] = env
->regs
[R_EBX
];
80 data
[2] = env
->regs
[R_ECX
]; data
[3] = env
->regs
[R_EDX
];
81 data
[4] = env
->regs
[R_ESI
]; data
[5] = env
->regs
[R_EDI
];
84 static void vmmouse_set_data(const uint32_t *data
)
86 X86CPU
*cpu
= X86_CPU(current_cpu
);
87 CPUX86State
*env
= &cpu
->env
;
89 env
->regs
[R_EAX
] = data
[0]; env
->regs
[R_EBX
] = data
[1];
90 env
->regs
[R_ECX
] = data
[2]; env
->regs
[R_EDX
] = data
[3];
91 env
->regs
[R_ESI
] = data
[4]; env
->regs
[R_EDI
] = data
[5];
94 static uint32_t vmmouse_get_status(VMMouseState
*s
)
96 DPRINTF("vmmouse_get_status()\n");
97 return (s
->status
<< 16) | s
->nb_queue
;
100 static void vmmouse_mouse_event(void *opaque
, int x
, int y
, int dz
, int buttons_state
)
102 VMMouseState
*s
= opaque
;
105 if (s
->nb_queue
> (VMMOUSE_QUEUE_SIZE
- 4))
108 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n",
109 x
, y
, dz
, buttons_state
);
111 if ((buttons_state
& MOUSE_EVENT_LBUTTON
))
112 buttons
|= VMMOUSE_LEFT_BUTTON
;
113 if ((buttons_state
& MOUSE_EVENT_RBUTTON
))
114 buttons
|= VMMOUSE_RIGHT_BUTTON
;
115 if ((buttons_state
& MOUSE_EVENT_MBUTTON
))
116 buttons
|= VMMOUSE_MIDDLE_BUTTON
;
122 /* add for guest vmmouse driver to judge this is a relative packet. */
123 buttons
|= VMMOUSE_RELATIVE_PACKET
;
126 s
->queue
[s
->nb_queue
++] = buttons
;
127 s
->queue
[s
->nb_queue
++] = x
;
128 s
->queue
[s
->nb_queue
++] = y
;
129 s
->queue
[s
->nb_queue
++] = dz
;
131 /* need to still generate PS2 events to notify driver to
133 i8042_isa_mouse_fake_event(s
->i8042
);
136 static void vmmouse_remove_handler(VMMouseState
*s
)
139 qemu_remove_mouse_event_handler(s
->entry
);
144 static void vmmouse_update_handler(VMMouseState
*s
, int absolute
)
146 if (s
->status
!= 0) {
149 if (s
->absolute
!= absolute
) {
150 s
->absolute
= absolute
;
151 vmmouse_remove_handler(s
);
153 if (s
->entry
== NULL
) {
154 s
->entry
= qemu_add_mouse_event_handler(vmmouse_mouse_event
,
157 qemu_activate_mouse_event_handler(s
->entry
);
161 static void vmmouse_read_id(VMMouseState
*s
)
163 DPRINTF("vmmouse_read_id()\n");
165 if (s
->nb_queue
== VMMOUSE_QUEUE_SIZE
)
168 s
->queue
[s
->nb_queue
++] = VMMOUSE_VERSION
;
170 vmmouse_update_handler(s
, s
->absolute
);
173 static void vmmouse_request_relative(VMMouseState
*s
)
175 DPRINTF("vmmouse_request_relative()\n");
176 vmmouse_update_handler(s
, 0);
179 static void vmmouse_request_absolute(VMMouseState
*s
)
181 DPRINTF("vmmouse_request_absolute()\n");
182 vmmouse_update_handler(s
, 1);
185 static void vmmouse_disable(VMMouseState
*s
)
187 DPRINTF("vmmouse_disable()\n");
189 vmmouse_remove_handler(s
);
192 static void vmmouse_data(VMMouseState
*s
, uint32_t *data
, uint32_t size
)
196 DPRINTF("vmmouse_data(%d)\n", size
);
198 if (size
== 0 || size
> 6 || size
> s
->nb_queue
) {
199 printf("vmmouse: driver requested too much data %d\n", size
);
201 vmmouse_remove_handler(s
);
205 for (i
= 0; i
< size
; i
++)
206 data
[i
] = s
->queue
[i
];
210 memmove(s
->queue
, &s
->queue
[size
], sizeof(s
->queue
[0]) * s
->nb_queue
);
213 static uint32_t vmmouse_ioport_read(void *opaque
, uint32_t addr
)
215 VMMouseState
*s
= opaque
;
219 vmmouse_get_data(data
);
221 command
= data
[2] & 0xFFFF;
224 case VMPORT_CMD_VMMOUSE_STATUS
:
225 data
[0] = vmmouse_get_status(s
);
227 case VMPORT_CMD_VMMOUSE_COMMAND
:
229 case VMMOUSE_DISABLE
:
232 case VMMOUSE_READ_ID
:
235 case VMMOUSE_REQUEST_RELATIVE
:
236 vmmouse_request_relative(s
);
238 case VMMOUSE_REQUEST_ABSOLUTE
:
239 vmmouse_request_absolute(s
);
242 printf("vmmouse: unknown command %x\n", data
[1]);
246 case VMPORT_CMD_VMMOUSE_DATA
:
247 vmmouse_data(s
, data
, data
[1]);
250 printf("vmmouse: unknown command %x\n", command
);
254 vmmouse_set_data(data
);
258 static int vmmouse_post_load(void *opaque
, int version_id
)
260 VMMouseState
*s
= opaque
;
262 vmmouse_remove_handler(s
);
263 vmmouse_update_handler(s
, s
->absolute
);
267 static const VMStateDescription vmstate_vmmouse
= {
270 .minimum_version_id
= 0,
271 .post_load
= vmmouse_post_load
,
272 .fields
= (VMStateField
[]) {
273 VMSTATE_INT32_EQUAL(queue_size
, VMMouseState
, NULL
),
274 VMSTATE_UINT32_ARRAY(queue
, VMMouseState
, VMMOUSE_QUEUE_SIZE
),
275 VMSTATE_UINT16(nb_queue
, VMMouseState
),
276 VMSTATE_UINT16(status
, VMMouseState
),
277 VMSTATE_UINT8(absolute
, VMMouseState
),
278 VMSTATE_END_OF_LIST()
282 static void vmmouse_reset(DeviceState
*d
)
284 VMMouseState
*s
= VMMOUSE(d
);
286 s
->queue_size
= VMMOUSE_QUEUE_SIZE
;
292 static void vmmouse_realizefn(DeviceState
*dev
, Error
**errp
)
294 VMMouseState
*s
= VMMOUSE(dev
);
296 DPRINTF("vmmouse_init\n");
299 error_setg(errp
, "'i8042' link is not set");
302 if (!object_resolve_path_type("", TYPE_VMPORT
, NULL
)) {
303 error_setg(errp
, "vmmouse needs a machine with vmport");
307 vmport_register(VMPORT_CMD_VMMOUSE_STATUS
, vmmouse_ioport_read
, s
);
308 vmport_register(VMPORT_CMD_VMMOUSE_COMMAND
, vmmouse_ioport_read
, s
);
309 vmport_register(VMPORT_CMD_VMMOUSE_DATA
, vmmouse_ioport_read
, s
);
312 static Property vmmouse_properties
[] = {
313 DEFINE_PROP_LINK("i8042", VMMouseState
, i8042
, TYPE_I8042
, ISAKBDState
*),
314 DEFINE_PROP_END_OF_LIST(),
317 static void vmmouse_class_initfn(ObjectClass
*klass
, void *data
)
319 DeviceClass
*dc
= DEVICE_CLASS(klass
);
321 dc
->realize
= vmmouse_realizefn
;
322 dc
->reset
= vmmouse_reset
;
323 dc
->vmsd
= &vmstate_vmmouse
;
324 device_class_set_props(dc
, vmmouse_properties
);
325 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
328 static const TypeInfo vmmouse_info
= {
329 .name
= TYPE_VMMOUSE
,
330 .parent
= TYPE_ISA_DEVICE
,
331 .instance_size
= sizeof(VMMouseState
),
332 .class_init
= vmmouse_class_initfn
,
335 static void vmmouse_register_types(void)
337 type_register_static(&vmmouse_info
);
340 type_init(vmmouse_register_types
)