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 "ui/console.h" 27 #include "hw/i386/pc.h" 28 #include "hw/input/i8042.h" 29 #include "hw/qdev-properties.h" 30 #include "migration/vmstate.h" 31 32 /* debug only vmmouse */ 33 //#define DEBUG_VMMOUSE 34 35 /* VMMouse Commands */ 36 #define VMMOUSE_GETVERSION 10 37 #define VMMOUSE_DATA 39 38 #define VMMOUSE_STATUS 40 39 #define VMMOUSE_COMMAND 41 40 41 #define VMMOUSE_READ_ID 0x45414552 42 #define VMMOUSE_DISABLE 0x000000f5 43 #define VMMOUSE_REQUEST_RELATIVE 0x4c455252 44 #define VMMOUSE_REQUEST_ABSOLUTE 0x53424152 45 46 #define VMMOUSE_QUEUE_SIZE 1024 47 48 #define VMMOUSE_VERSION 0x3442554a 49 50 #ifdef DEBUG_VMMOUSE 51 #define DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) 52 #else 53 #define DPRINTF(fmt, ...) do { } while (0) 54 #endif 55 56 #define TYPE_VMMOUSE "vmmouse" 57 #define VMMOUSE(obj) OBJECT_CHECK(VMMouseState, (obj), TYPE_VMMOUSE) 58 59 typedef struct VMMouseState 60 { 61 ISADevice parent_obj; 62 63 uint32_t queue[VMMOUSE_QUEUE_SIZE]; 64 int32_t queue_size; 65 uint16_t nb_queue; 66 uint16_t status; 67 uint8_t absolute; 68 QEMUPutMouseEntry *entry; 69 void *ps2_mouse; 70 } VMMouseState; 71 72 static uint32_t vmmouse_get_status(VMMouseState *s) 73 { 74 DPRINTF("vmmouse_get_status()\n"); 75 return (s->status << 16) | s->nb_queue; 76 } 77 78 static void vmmouse_mouse_event(void *opaque, int x, int y, int dz, int buttons_state) 79 { 80 VMMouseState *s = opaque; 81 int buttons = 0; 82 83 if (s->nb_queue > (VMMOUSE_QUEUE_SIZE - 4)) 84 return; 85 86 DPRINTF("vmmouse_mouse_event(%d, %d, %d, %d)\n", 87 x, y, dz, buttons_state); 88 89 if ((buttons_state & MOUSE_EVENT_LBUTTON)) 90 buttons |= 0x20; 91 if ((buttons_state & MOUSE_EVENT_RBUTTON)) 92 buttons |= 0x10; 93 if ((buttons_state & MOUSE_EVENT_MBUTTON)) 94 buttons |= 0x08; 95 96 if (s->absolute) { 97 x <<= 1; 98 y <<= 1; 99 } 100 101 s->queue[s->nb_queue++] = buttons; 102 s->queue[s->nb_queue++] = x; 103 s->queue[s->nb_queue++] = y; 104 s->queue[s->nb_queue++] = dz; 105 106 /* need to still generate PS2 events to notify driver to 107 read from queue */ 108 i8042_isa_mouse_fake_event(s->ps2_mouse); 109 } 110 111 static void vmmouse_remove_handler(VMMouseState *s) 112 { 113 if (s->entry) { 114 qemu_remove_mouse_event_handler(s->entry); 115 s->entry = NULL; 116 } 117 } 118 119 static void vmmouse_update_handler(VMMouseState *s, int absolute) 120 { 121 if (s->status != 0) { 122 return; 123 } 124 if (s->absolute != absolute) { 125 s->absolute = absolute; 126 vmmouse_remove_handler(s); 127 } 128 if (s->entry == NULL) { 129 s->entry = qemu_add_mouse_event_handler(vmmouse_mouse_event, 130 s, s->absolute, 131 "vmmouse"); 132 qemu_activate_mouse_event_handler(s->entry); 133 } 134 } 135 136 static void vmmouse_read_id(VMMouseState *s) 137 { 138 DPRINTF("vmmouse_read_id()\n"); 139 140 if (s->nb_queue == VMMOUSE_QUEUE_SIZE) 141 return; 142 143 s->queue[s->nb_queue++] = VMMOUSE_VERSION; 144 s->status = 0; 145 } 146 147 static void vmmouse_request_relative(VMMouseState *s) 148 { 149 DPRINTF("vmmouse_request_relative()\n"); 150 vmmouse_update_handler(s, 0); 151 } 152 153 static void vmmouse_request_absolute(VMMouseState *s) 154 { 155 DPRINTF("vmmouse_request_absolute()\n"); 156 vmmouse_update_handler(s, 1); 157 } 158 159 static void vmmouse_disable(VMMouseState *s) 160 { 161 DPRINTF("vmmouse_disable()\n"); 162 s->status = 0xffff; 163 vmmouse_remove_handler(s); 164 } 165 166 static void vmmouse_data(VMMouseState *s, uint32_t *data, uint32_t size) 167 { 168 int i; 169 170 DPRINTF("vmmouse_data(%d)\n", size); 171 172 if (size == 0 || size > 6 || size > s->nb_queue) { 173 printf("vmmouse: driver requested too much data %d\n", size); 174 s->status = 0xffff; 175 vmmouse_remove_handler(s); 176 return; 177 } 178 179 for (i = 0; i < size; i++) 180 data[i] = s->queue[i]; 181 182 s->nb_queue -= size; 183 if (s->nb_queue) 184 memmove(s->queue, &s->queue[size], sizeof(s->queue[0]) * s->nb_queue); 185 } 186 187 static uint32_t vmmouse_ioport_read(void *opaque, uint32_t addr) 188 { 189 VMMouseState *s = opaque; 190 uint32_t data[6]; 191 uint16_t command; 192 193 vmmouse_get_data(data); 194 195 command = data[2] & 0xFFFF; 196 197 switch (command) { 198 case VMMOUSE_STATUS: 199 data[0] = vmmouse_get_status(s); 200 break; 201 case VMMOUSE_COMMAND: 202 switch (data[1]) { 203 case VMMOUSE_DISABLE: 204 vmmouse_disable(s); 205 break; 206 case VMMOUSE_READ_ID: 207 vmmouse_read_id(s); 208 break; 209 case VMMOUSE_REQUEST_RELATIVE: 210 vmmouse_request_relative(s); 211 break; 212 case VMMOUSE_REQUEST_ABSOLUTE: 213 vmmouse_request_absolute(s); 214 break; 215 default: 216 printf("vmmouse: unknown command %x\n", data[1]); 217 break; 218 } 219 break; 220 case VMMOUSE_DATA: 221 vmmouse_data(s, data, data[1]); 222 break; 223 default: 224 printf("vmmouse: unknown command %x\n", command); 225 break; 226 } 227 228 vmmouse_set_data(data); 229 return data[0]; 230 } 231 232 static int vmmouse_post_load(void *opaque, int version_id) 233 { 234 VMMouseState *s = opaque; 235 236 vmmouse_remove_handler(s); 237 vmmouse_update_handler(s, s->absolute); 238 return 0; 239 } 240 241 static const VMStateDescription vmstate_vmmouse = { 242 .name = "vmmouse", 243 .version_id = 0, 244 .minimum_version_id = 0, 245 .post_load = vmmouse_post_load, 246 .fields = (VMStateField[]) { 247 VMSTATE_INT32_EQUAL(queue_size, VMMouseState, NULL), 248 VMSTATE_UINT32_ARRAY(queue, VMMouseState, VMMOUSE_QUEUE_SIZE), 249 VMSTATE_UINT16(nb_queue, VMMouseState), 250 VMSTATE_UINT16(status, VMMouseState), 251 VMSTATE_UINT8(absolute, VMMouseState), 252 VMSTATE_END_OF_LIST() 253 } 254 }; 255 256 static void vmmouse_reset(DeviceState *d) 257 { 258 VMMouseState *s = VMMOUSE(d); 259 260 s->queue_size = VMMOUSE_QUEUE_SIZE; 261 s->nb_queue = 0; 262 263 vmmouse_disable(s); 264 } 265 266 static void vmmouse_realizefn(DeviceState *dev, Error **errp) 267 { 268 VMMouseState *s = VMMOUSE(dev); 269 270 DPRINTF("vmmouse_init\n"); 271 272 vmport_register(VMMOUSE_STATUS, vmmouse_ioport_read, s); 273 vmport_register(VMMOUSE_COMMAND, vmmouse_ioport_read, s); 274 vmport_register(VMMOUSE_DATA, vmmouse_ioport_read, s); 275 } 276 277 static Property vmmouse_properties[] = { 278 DEFINE_PROP_PTR("ps2_mouse", VMMouseState, ps2_mouse), 279 DEFINE_PROP_END_OF_LIST(), 280 }; 281 282 static void vmmouse_class_initfn(ObjectClass *klass, void *data) 283 { 284 DeviceClass *dc = DEVICE_CLASS(klass); 285 286 dc->realize = vmmouse_realizefn; 287 dc->reset = vmmouse_reset; 288 dc->vmsd = &vmstate_vmmouse; 289 dc->props = vmmouse_properties; 290 /* Reason: pointer property "ps2_mouse" */ 291 dc->user_creatable = false; 292 } 293 294 static const TypeInfo vmmouse_info = { 295 .name = TYPE_VMMOUSE, 296 .parent = TYPE_ISA_DEVICE, 297 .instance_size = sizeof(VMMouseState), 298 .class_init = vmmouse_class_initfn, 299 }; 300 301 static void vmmouse_register_types(void) 302 { 303 type_register_static(&vmmouse_info); 304 } 305 306 type_init(vmmouse_register_types) 307