1 /* 2 * QEMU VMMouse emulation 3 * 4 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws> 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 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. 23 */ 24 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" 32 #include "cpu.h" 33 #include "qom/object.h" 34 35 /* debug only vmmouse */ 36 //#define DEBUG_VMMOUSE 37 38 #define VMMOUSE_READ_ID 0x45414552 39 #define VMMOUSE_DISABLE 0x000000f5 40 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252 41 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152 42 43 #define VMMOUSE_QUEUE_SIZE 1024 44 45 #define VMMOUSE_VERSION 0x3442554a 46 47 #define VMMOUSE_RELATIVE_PACKET 0x00010000 48 49 #define VMMOUSE_LEFT_BUTTON 0x20 50 #define VMMOUSE_RIGHT_BUTTON 0x10 51 #define VMMOUSE_MIDDLE_BUTTON 0x08 52 53 #ifdef DEBUG_VMMOUSE 54 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) 55 #else 56 #define DPRINTF(fmt, ...) do { } while (0) 57 #endif 58 59 #define TYPE_VMMOUSE "vmmouse" 60 OBJECT_DECLARE_SIMPLE_TYPE(VMMouseState, VMMOUSE) 61 62 struct VMMouseState { 63 ISADevice parent_obj; 64 65 uint32_t queue[VMMOUSE_QUEUE_SIZE]; 66 int32_t queue_size; 67 uint16_t nb_queue; 68 uint16_t status; 69 uint8_t absolute; 70 QEMUPutMouseEntry *entry; 71 ISAKBDState *i8042; 72 }; 73 74 static void vmmouse_get_data(uint32_t *data) 75 { 76 X86CPU *cpu = X86_CPU(current_cpu); 77 CPUX86State *env = &cpu->env; 78 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]; 82 } 83 84 static void vmmouse_set_data(const uint32_t *data) 85 { 86 X86CPU *cpu = X86_CPU(current_cpu); 87 CPUX86State *env = &cpu->env; 88 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]; 92 } 93 94 static uint32_t vmmouse_get_status(VMMouseState *s) 95 { 96 DPRINTF("vmmouse_get_status()\n"); 97 return (s->status << 16) | s->nb_queue; 98 } 99 100 static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state) 101 { 102 VMMouseState *s = opaque; 103 int buttons = 0; 104 105 if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4)) 106 return; 107 108 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n", 109 x, y, dz, buttons_state); 110 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; 117 118 if (s->absolute) { 119 x <<= 1; 120 y <<= 1; 121 } else{ 122 /* add for guest vmmouse driver to judge this is a relative packet. */ 123 buttons |= VMMOUSE_RELATIVE_PACKET; 124 } 125 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; 130 131 /* need to still generate PS2 events to notify driver to 132 read from queue */ 133 i8042_isa_mouse_fake_event(s->i8042); 134 } 135 136 static void vmmouse_remove_handler(VMMouseState *s) 137 { 138 if (s->entry) { 139 qemu_remove_mouse_event_handler(s->entry); 140 s->entry = NULL; 141 } 142 } 143 144 static void vmmouse_update_handler(VMMouseState *s, int absolute) 145 { 146 if (s->status != 0) { 147 return; 148 } 149 if (s->absolute != absolute) { 150 s->absolute = absolute; 151 vmmouse_remove_handler(s); 152 } 153 if (s->entry == NULL) { 154 s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event, 155 s, s->absolute, 156 "vmmouse"); 157 qemu_activate_mouse_event_handler(s->entry); 158 } 159 } 160 161 static void vmmouse_read_id(VMMouseState *s) 162 { 163 DPRINTF("vmmouse_read_id()\n"); 164 165 if (s->nb_queue == VMMOUSE_QUEUE_SIZE) 166 return; 167 168 s->queue[s->nb_queue++] = VMMOUSE_VERSION; 169 s->status = 0; 170 vmmouse_update_handler(s, s->absolute); 171 } 172 173 static void vmmouse_request_relative(VMMouseState *s) 174 { 175 DPRINTF("vmmouse_request_relative()\n"); 176 vmmouse_update_handler(s, 0); 177 } 178 179 static void vmmouse_request_absolute(VMMouseState *s) 180 { 181 DPRINTF("vmmouse_request_absolute()\n"); 182 vmmouse_update_handler(s, 1); 183 } 184 185 static void vmmouse_disable(VMMouseState *s) 186 { 187 DPRINTF("vmmouse_disable()\n"); 188 s->status = 0xffff; 189 vmmouse_remove_handler(s); 190 } 191 192 static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size) 193 { 194 int i; 195 196 DPRINTF("vmmouse_data(%d)\n", size); 197 198 if (size == 0 || size > 6 || size > s->nb_queue) { 199 printf("vmmouse: driver requested too much data %d\n", size); 200 s->status = 0xffff; 201 vmmouse_remove_handler(s); 202 return; 203 } 204 205 for (i = 0; i < size; i++) 206 data[i] = s->queue[i]; 207 208 s->nb_queue -= size; 209 if (s->nb_queue) 210 memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue); 211 } 212 213 static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr) 214 { 215 VMMouseState *s = opaque; 216 uint32_t data[6]; 217 uint16_t command; 218 219 vmmouse_get_data(data); 220 221 command = data[2] & 0xFFFF; 222 223 switch (command) { 224 case VMPORT_CMD_VMMOUSE_STATUS: 225 data[0] = vmmouse_get_status(s); 226 break; 227 case VMPORT_CMD_VMMOUSE_COMMAND: 228 switch (data[1]) { 229 case VMMOUSE_DISABLE: 230 vmmouse_disable(s); 231 break; 232 case VMMOUSE_READ_ID: 233 vmmouse_read_id(s); 234 break; 235 case VMMOUSE_REQUEST_RELATIVE: 236 vmmouse_request_relative(s); 237 break; 238 case VMMOUSE_REQUEST_ABSOLUTE: 239 vmmouse_request_absolute(s); 240 break; 241 default: 242 printf("vmmouse: unknown command %x\n", data[1]); 243 break; 244 } 245 break; 246 case VMPORT_CMD_VMMOUSE_DATA: 247 vmmouse_data(s, data, data[1]); 248 break; 249 default: 250 printf("vmmouse: unknown command %x\n", command); 251 break; 252 } 253 254 vmmouse_set_data(data); 255 return data[0]; 256 } 257 258 static int vmmouse_post_load(void *opaque, int version_id) 259 { 260 VMMouseState *s = opaque; 261 262 vmmouse_remove_handler(s); 263 vmmouse_update_handler(s, s->absolute); 264 return 0; 265 } 266 267 static const VMStateDescription vmstate_vmmouse = { 268 .name = "vmmouse", 269 .version_id = 0, 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() 279 } 280 }; 281 282 static void vmmouse_reset(DeviceState *d) 283 { 284 VMMouseState *s = VMMOUSE(d); 285 286 s->queue_size = VMMOUSE_QUEUE_SIZE; 287 s->nb_queue = 0; 288 289 vmmouse_disable(s); 290 } 291 292 static void vmmouse_realizefn(DeviceState *dev, Error **errp) 293 { 294 VMMouseState *s = VMMOUSE(dev); 295 296 DPRINTF("vmmouse_init\n"); 297 298 if (!s->i8042) { 299 error_setg(errp, "'i8042' link is not set"); 300 return; 301 } 302 if (!object_resolve_path_type("", TYPE_VMPORT, NULL)) { 303 error_setg(errp, "vmmouse needs a machine with vmport"); 304 return; 305 } 306 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); 310 } 311 312 static Property vmmouse_properties[] = { 313 DEFINE_PROP_LINK("i8042", VMMouseState, i8042, TYPE_I8042, ISAKBDState *), 314 DEFINE_PROP_END_OF_LIST(), 315 }; 316 317 static void vmmouse_class_initfn(ObjectClass *klass, void *data) 318 { 319 DeviceClass *dc = DEVICE_CLASS(klass); 320 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); 326 } 327 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, 333 }; 334 335 static void vmmouse_register_types(void) 336 { 337 type_register_static(&vmmouse_info); 338 } 339 340 type_init(vmmouse_register_types) 341