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"
37 /* debug only vmmouse */
38 //#define DEBUG_VMMOUSE
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 #define VMMOUSE_RELATIVE_PACKET 0x00010000
51 #define VMMOUSE_LEFT_BUTTON 0x20
52 #define VMMOUSE_RIGHT_BUTTON 0x10
53 #define VMMOUSE_MIDDLE_BUTTON 0x08
55 #define VMMOUSE_MIN_X 0
56 #define VMMOUSE_MIN_Y 0
57 #define VMMOUSE_MAX_X 0xFFFF
58 #define VMMOUSE_MAX_Y 0xFFFF
60 #define TYPE_VMMOUSE "vmmouse"
61 OBJECT_DECLARE_SIMPLE_TYPE(VMMouseState
, VMMOUSE
)
66 uint32_t queue
[VMMOUSE_QUEUE_SIZE
];
71 QEMUPutMouseEntry
*entry
;
75 static void vmmouse_get_data(uint32_t *data
)
77 X86CPU
*cpu
= X86_CPU(current_cpu
);
78 CPUX86State
*env
= &cpu
->env
;
80 data
[0] = env
->regs
[R_EAX
]; data
[1] = env
->regs
[R_EBX
];
81 data
[2] = env
->regs
[R_ECX
]; data
[3] = env
->regs
[R_EDX
];
82 data
[4] = env
->regs
[R_ESI
]; data
[5] = env
->regs
[R_EDI
];
85 static void vmmouse_set_data(const uint32_t *data
)
87 X86CPU
*cpu
= X86_CPU(current_cpu
);
88 CPUX86State
*env
= &cpu
->env
;
90 env
->regs
[R_EAX
] = data
[0]; env
->regs
[R_EBX
] = data
[1];
91 env
->regs
[R_ECX
] = data
[2]; env
->regs
[R_EDX
] = data
[3];
92 env
->regs
[R_ESI
] = data
[4]; env
->regs
[R_EDI
] = data
[5];
95 static uint32_t vmmouse_get_status(VMMouseState
*s
)
97 trace_vmmouse_get_status();
99 return (s
->status
<< 16) | s
->nb_queue
;
102 static void vmmouse_mouse_event(void *opaque
, int x
, int y
, int dz
, int buttons_state
)
104 VMMouseState
*s
= opaque
;
107 if (s
->nb_queue
> (VMMOUSE_QUEUE_SIZE
- 4))
110 trace_vmmouse_mouse_event(x
, y
, dz
, buttons_state
);
112 if ((buttons_state
& MOUSE_EVENT_LBUTTON
))
113 buttons
|= VMMOUSE_LEFT_BUTTON
;
114 if ((buttons_state
& MOUSE_EVENT_RBUTTON
))
115 buttons
|= VMMOUSE_RIGHT_BUTTON
;
116 if ((buttons_state
& MOUSE_EVENT_MBUTTON
))
117 buttons
|= VMMOUSE_MIDDLE_BUTTON
;
120 x
= qemu_input_scale_axis(x
,
121 INPUT_EVENT_ABS_MIN
, INPUT_EVENT_ABS_MAX
,
122 VMMOUSE_MIN_X
, VMMOUSE_MAX_X
);
123 y
= qemu_input_scale_axis(y
,
124 INPUT_EVENT_ABS_MIN
, INPUT_EVENT_ABS_MAX
,
125 VMMOUSE_MIN_Y
, VMMOUSE_MAX_Y
);
127 /* add for guest vmmouse driver to judge this is a relative packet. */
128 buttons
|= VMMOUSE_RELATIVE_PACKET
;
131 s
->queue
[s
->nb_queue
++] = buttons
;
132 s
->queue
[s
->nb_queue
++] = x
;
133 s
->queue
[s
->nb_queue
++] = y
;
134 s
->queue
[s
->nb_queue
++] = dz
;
136 /* need to still generate PS2 events to notify driver to
138 i8042_isa_mouse_fake_event(s
->i8042
);
141 static void vmmouse_remove_handler(VMMouseState
*s
)
144 qemu_remove_mouse_event_handler(s
->entry
);
149 static void vmmouse_update_handler(VMMouseState
*s
, int absolute
)
151 if (s
->status
!= 0) {
154 if (s
->absolute
!= absolute
) {
155 s
->absolute
= absolute
;
156 vmmouse_remove_handler(s
);
158 if (s
->entry
== NULL
) {
159 s
->entry
= qemu_add_mouse_event_handler(vmmouse_mouse_event
,
162 qemu_activate_mouse_event_handler(s
->entry
);
166 static void vmmouse_read_id(VMMouseState
*s
)
168 trace_vmmouse_read_id();
170 if (s
->nb_queue
== VMMOUSE_QUEUE_SIZE
)
173 s
->queue
[s
->nb_queue
++] = VMMOUSE_VERSION
;
175 vmmouse_update_handler(s
, s
->absolute
);
178 static void vmmouse_request_relative(VMMouseState
*s
)
180 trace_vmmouse_request_relative();
182 vmmouse_update_handler(s
, 0);
185 static void vmmouse_request_absolute(VMMouseState
*s
)
187 trace_vmmouse_request_absolute();
189 vmmouse_update_handler(s
, 1);
192 static void vmmouse_disable(VMMouseState
*s
)
194 trace_vmmouse_disable();
197 vmmouse_remove_handler(s
);
200 static void vmmouse_data(VMMouseState
*s
, uint32_t *data
, uint32_t size
)
204 trace_vmmouse_data(size
);
206 if (size
== 0 || size
> 6 || size
> s
->nb_queue
) {
207 printf("vmmouse: driver requested too much data %d\n", size
);
209 vmmouse_remove_handler(s
);
213 for (i
= 0; i
< size
; i
++)
214 data
[i
] = s
->queue
[i
];
218 memmove(s
->queue
, &s
->queue
[size
], sizeof(s
->queue
[0]) * s
->nb_queue
);
221 static uint32_t vmmouse_ioport_read(void *opaque
, uint32_t addr
)
223 VMMouseState
*s
= opaque
;
227 vmmouse_get_data(data
);
229 command
= data
[2] & 0xFFFF;
232 case VMPORT_CMD_VMMOUSE_STATUS
:
233 data
[0] = vmmouse_get_status(s
);
235 case VMPORT_CMD_VMMOUSE_COMMAND
:
237 case VMMOUSE_DISABLE
:
240 case VMMOUSE_READ_ID
:
243 case VMMOUSE_REQUEST_RELATIVE
:
244 vmmouse_request_relative(s
);
246 case VMMOUSE_REQUEST_ABSOLUTE
:
247 vmmouse_request_absolute(s
);
250 printf("vmmouse: unknown command %x\n", data
[1]);
254 case VMPORT_CMD_VMMOUSE_DATA
:
255 vmmouse_data(s
, data
, data
[1]);
258 printf("vmmouse: unknown command %x\n", command
);
262 vmmouse_set_data(data
);
266 static int vmmouse_post_load(void *opaque
, int version_id
)
268 VMMouseState
*s
= opaque
;
270 vmmouse_remove_handler(s
);
271 vmmouse_update_handler(s
, s
->absolute
);
275 static const VMStateDescription vmstate_vmmouse
= {
278 .minimum_version_id
= 0,
279 .post_load
= vmmouse_post_load
,
280 .fields
= (const VMStateField
[]) {
281 VMSTATE_INT32_EQUAL(queue_size
, VMMouseState
, NULL
),
282 VMSTATE_UINT32_ARRAY(queue
, VMMouseState
, VMMOUSE_QUEUE_SIZE
),
283 VMSTATE_UINT16(nb_queue
, VMMouseState
),
284 VMSTATE_UINT16(status
, VMMouseState
),
285 VMSTATE_UINT8(absolute
, VMMouseState
),
286 VMSTATE_END_OF_LIST()
290 static void vmmouse_reset(DeviceState
*d
)
292 VMMouseState
*s
= VMMOUSE(d
);
294 s
->queue_size
= VMMOUSE_QUEUE_SIZE
;
300 static void vmmouse_realizefn(DeviceState
*dev
, Error
**errp
)
302 VMMouseState
*s
= VMMOUSE(dev
);
304 trace_vmmouse_init();
307 error_setg(errp
, "'i8042' link is not set");
310 if (!object_resolve_path_type("", TYPE_VMPORT
, NULL
)) {
311 error_setg(errp
, "vmmouse needs a machine with vmport");
315 vmport_register(VMPORT_CMD_VMMOUSE_STATUS
, vmmouse_ioport_read
, s
);
316 vmport_register(VMPORT_CMD_VMMOUSE_COMMAND
, vmmouse_ioport_read
, s
);
317 vmport_register(VMPORT_CMD_VMMOUSE_DATA
, vmmouse_ioport_read
, s
);
320 static Property vmmouse_properties
[] = {
321 DEFINE_PROP_LINK("i8042", VMMouseState
, i8042
, TYPE_I8042
, ISAKBDState
*),
322 DEFINE_PROP_END_OF_LIST(),
325 static void vmmouse_class_initfn(ObjectClass
*klass
, void *data
)
327 DeviceClass
*dc
= DEVICE_CLASS(klass
);
329 dc
->realize
= vmmouse_realizefn
;
330 dc
->reset
= vmmouse_reset
;
331 dc
->vmsd
= &vmstate_vmmouse
;
332 device_class_set_props(dc
, vmmouse_properties
);
333 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
336 static const TypeInfo vmmouse_info
= {
337 .name
= TYPE_VMMOUSE
,
338 .parent
= TYPE_ISA_DEVICE
,
339 .instance_size
= sizeof(VMMouseState
),
340 .class_init
= vmmouse_class_initfn
,
343 static void vmmouse_register_types(void)
345 type_register_static(&vmmouse_info
);
348 type_init(vmmouse_register_types
)