1 /* 2 * QEMU ADB support 3 * 4 * Copyright (c) 2004 Fabrice Bellard 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 #include "qemu/osdep.h" 25 #include "hw/hw.h" 26 #include "hw/input/adb.h" 27 #include "ui/console.h" 28 #include "include/hw/input/adb-keys.h" 29 #include "ui/input.h" 30 #include "sysemu/sysemu.h" 31 32 /* debug ADB */ 33 //#define DEBUG_ADB 34 35 #ifdef DEBUG_ADB 36 #define ADB_DPRINTF(fmt, ...) \ 37 do { printf("ADB: " fmt , ## __VA_ARGS__); } while (0) 38 #else 39 #define ADB_DPRINTF(fmt, ...) 40 #endif 41 42 /* ADB commands */ 43 #define ADB_BUSRESET 0x00 44 #define ADB_FLUSH 0x01 45 #define ADB_WRITEREG 0x08 46 #define ADB_READREG 0x0c 47 48 /* ADB device commands */ 49 #define ADB_CMD_SELF_TEST 0xff 50 #define ADB_CMD_CHANGE_ID 0xfe 51 #define ADB_CMD_CHANGE_ID_AND_ACT 0xfd 52 #define ADB_CMD_CHANGE_ID_AND_ENABLE 0x00 53 54 /* ADB default device IDs (upper 4 bits of ADB command byte) */ 55 #define ADB_DEVID_DONGLE 1 56 #define ADB_DEVID_KEYBOARD 2 57 #define ADB_DEVID_MOUSE 3 58 #define ADB_DEVID_TABLET 4 59 #define ADB_DEVID_MODEM 5 60 #define ADB_DEVID_MISC 7 61 62 /* error codes */ 63 #define ADB_RET_NOTPRESENT (-2) 64 65 /* The adb keyboard doesn't have every key imaginable */ 66 #define NO_KEY 0xff 67 68 static void adb_device_reset(ADBDevice *d) 69 { 70 qdev_reset_all(DEVICE(d)); 71 } 72 73 int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len) 74 { 75 ADBDevice *d; 76 int devaddr, cmd, i; 77 78 cmd = buf[0] & 0xf; 79 if (cmd == ADB_BUSRESET) { 80 for(i = 0; i < s->nb_devices; i++) { 81 d = s->devices[i]; 82 adb_device_reset(d); 83 } 84 return 0; 85 } 86 devaddr = buf[0] >> 4; 87 for(i = 0; i < s->nb_devices; i++) { 88 d = s->devices[i]; 89 if (d->devaddr == devaddr) { 90 ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d); 91 return adc->devreq(d, obuf, buf, len); 92 } 93 } 94 return ADB_RET_NOTPRESENT; 95 } 96 97 /* XXX: move that to cuda ? */ 98 int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask) 99 { 100 ADBDevice *d; 101 int olen, i; 102 uint8_t buf[1]; 103 104 olen = 0; 105 for(i = 0; i < s->nb_devices; i++) { 106 if (s->poll_index >= s->nb_devices) 107 s->poll_index = 0; 108 d = s->devices[s->poll_index]; 109 if ((1 << d->devaddr) & poll_mask) { 110 buf[0] = ADB_READREG | (d->devaddr << 4); 111 olen = adb_request(s, obuf + 1, buf, 1); 112 /* if there is data, we poll again the same device */ 113 if (olen > 0) { 114 obuf[0] = buf[0]; 115 olen++; 116 break; 117 } 118 } 119 s->poll_index++; 120 } 121 return olen; 122 } 123 124 static const TypeInfo adb_bus_type_info = { 125 .name = TYPE_ADB_BUS, 126 .parent = TYPE_BUS, 127 .instance_size = sizeof(ADBBusState), 128 }; 129 130 static const VMStateDescription vmstate_adb_device = { 131 .name = "adb_device", 132 .version_id = 0, 133 .minimum_version_id = 0, 134 .fields = (VMStateField[]) { 135 VMSTATE_INT32(devaddr, ADBDevice), 136 VMSTATE_INT32(handler, ADBDevice), 137 VMSTATE_END_OF_LIST() 138 } 139 }; 140 141 static void adb_device_realizefn(DeviceState *dev, Error **errp) 142 { 143 ADBDevice *d = ADB_DEVICE(dev); 144 ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev)); 145 146 if (bus->nb_devices >= MAX_ADB_DEVICES) { 147 return; 148 } 149 150 bus->devices[bus->nb_devices++] = d; 151 } 152 153 static void adb_device_class_init(ObjectClass *oc, void *data) 154 { 155 DeviceClass *dc = DEVICE_CLASS(oc); 156 157 dc->realize = adb_device_realizefn; 158 dc->bus_type = TYPE_ADB_BUS; 159 } 160 161 static const TypeInfo adb_device_type_info = { 162 .name = TYPE_ADB_DEVICE, 163 .parent = TYPE_DEVICE, 164 .instance_size = sizeof(ADBDevice), 165 .abstract = true, 166 .class_init = adb_device_class_init, 167 }; 168 169 /***************************************************************/ 170 /* Keyboard ADB device */ 171 172 #define ADB_KEYBOARD(obj) OBJECT_CHECK(KBDState, (obj), TYPE_ADB_KEYBOARD) 173 174 typedef struct KBDState { 175 /*< private >*/ 176 ADBDevice parent_obj; 177 /*< public >*/ 178 179 uint8_t data[128]; 180 int rptr, wptr, count; 181 } KBDState; 182 183 #define ADB_KEYBOARD_CLASS(class) \ 184 OBJECT_CLASS_CHECK(ADBKeyboardClass, (class), TYPE_ADB_KEYBOARD) 185 #define ADB_KEYBOARD_GET_CLASS(obj) \ 186 OBJECT_GET_CLASS(ADBKeyboardClass, (obj), TYPE_ADB_KEYBOARD) 187 188 typedef struct ADBKeyboardClass { 189 /*< private >*/ 190 ADBDeviceClass parent_class; 191 /*< public >*/ 192 193 DeviceRealize parent_realize; 194 } ADBKeyboardClass; 195 196 int qcode_to_adb_keycode[] = { 197 /* Make sure future additions are automatically set to NO_KEY */ 198 [0 ... 0xff] = NO_KEY, 199 200 [Q_KEY_CODE_SHIFT] = ADB_KEY_LEFT_SHIFT, 201 [Q_KEY_CODE_SHIFT_R] = ADB_KEY_RIGHT_SHIFT, 202 [Q_KEY_CODE_ALT] = ADB_KEY_LEFT_OPTION, 203 [Q_KEY_CODE_ALT_R] = ADB_KEY_RIGHT_OPTION, 204 [Q_KEY_CODE_ALTGR] = ADB_KEY_RIGHT_OPTION, 205 [Q_KEY_CODE_CTRL] = ADB_KEY_LEFT_CONTROL, 206 [Q_KEY_CODE_CTRL_R] = ADB_KEY_RIGHT_CONTROL, 207 [Q_KEY_CODE_META_L] = ADB_KEY_COMMAND, 208 [Q_KEY_CODE_META_R] = ADB_KEY_COMMAND, 209 [Q_KEY_CODE_SPC] = ADB_KEY_SPACEBAR, 210 211 [Q_KEY_CODE_ESC] = ADB_KEY_ESC, 212 [Q_KEY_CODE_1] = ADB_KEY_1, 213 [Q_KEY_CODE_2] = ADB_KEY_2, 214 [Q_KEY_CODE_3] = ADB_KEY_3, 215 [Q_KEY_CODE_4] = ADB_KEY_4, 216 [Q_KEY_CODE_5] = ADB_KEY_5, 217 [Q_KEY_CODE_6] = ADB_KEY_6, 218 [Q_KEY_CODE_7] = ADB_KEY_7, 219 [Q_KEY_CODE_8] = ADB_KEY_8, 220 [Q_KEY_CODE_9] = ADB_KEY_9, 221 [Q_KEY_CODE_0] = ADB_KEY_0, 222 [Q_KEY_CODE_MINUS] = ADB_KEY_MINUS, 223 [Q_KEY_CODE_EQUAL] = ADB_KEY_EQUAL, 224 [Q_KEY_CODE_BACKSPACE] = ADB_KEY_DELETE, 225 [Q_KEY_CODE_TAB] = ADB_KEY_TAB, 226 [Q_KEY_CODE_Q] = ADB_KEY_Q, 227 [Q_KEY_CODE_W] = ADB_KEY_W, 228 [Q_KEY_CODE_E] = ADB_KEY_E, 229 [Q_KEY_CODE_R] = ADB_KEY_R, 230 [Q_KEY_CODE_T] = ADB_KEY_T, 231 [Q_KEY_CODE_Y] = ADB_KEY_Y, 232 [Q_KEY_CODE_U] = ADB_KEY_U, 233 [Q_KEY_CODE_I] = ADB_KEY_I, 234 [Q_KEY_CODE_O] = ADB_KEY_O, 235 [Q_KEY_CODE_P] = ADB_KEY_P, 236 [Q_KEY_CODE_BRACKET_LEFT] = ADB_KEY_LEFT_BRACKET, 237 [Q_KEY_CODE_BRACKET_RIGHT] = ADB_KEY_RIGHT_BRACKET, 238 [Q_KEY_CODE_RET] = ADB_KEY_RETURN, 239 [Q_KEY_CODE_A] = ADB_KEY_A, 240 [Q_KEY_CODE_S] = ADB_KEY_S, 241 [Q_KEY_CODE_D] = ADB_KEY_D, 242 [Q_KEY_CODE_F] = ADB_KEY_F, 243 [Q_KEY_CODE_G] = ADB_KEY_G, 244 [Q_KEY_CODE_H] = ADB_KEY_H, 245 [Q_KEY_CODE_J] = ADB_KEY_J, 246 [Q_KEY_CODE_K] = ADB_KEY_K, 247 [Q_KEY_CODE_L] = ADB_KEY_L, 248 [Q_KEY_CODE_SEMICOLON] = ADB_KEY_SEMICOLON, 249 [Q_KEY_CODE_APOSTROPHE] = ADB_KEY_APOSTROPHE, 250 [Q_KEY_CODE_GRAVE_ACCENT] = ADB_KEY_GRAVE_ACCENT, 251 [Q_KEY_CODE_BACKSLASH] = ADB_KEY_BACKSLASH, 252 [Q_KEY_CODE_Z] = ADB_KEY_Z, 253 [Q_KEY_CODE_X] = ADB_KEY_X, 254 [Q_KEY_CODE_C] = ADB_KEY_C, 255 [Q_KEY_CODE_V] = ADB_KEY_V, 256 [Q_KEY_CODE_B] = ADB_KEY_B, 257 [Q_KEY_CODE_N] = ADB_KEY_N, 258 [Q_KEY_CODE_M] = ADB_KEY_M, 259 [Q_KEY_CODE_COMMA] = ADB_KEY_COMMA, 260 [Q_KEY_CODE_DOT] = ADB_KEY_PERIOD, 261 [Q_KEY_CODE_SLASH] = ADB_KEY_FORWARD_SLASH, 262 [Q_KEY_CODE_ASTERISK] = ADB_KEY_KP_MULTIPLY, 263 [Q_KEY_CODE_CAPS_LOCK] = ADB_KEY_CAPS_LOCK, 264 265 [Q_KEY_CODE_F1] = ADB_KEY_F1, 266 [Q_KEY_CODE_F2] = ADB_KEY_F2, 267 [Q_KEY_CODE_F3] = ADB_KEY_F3, 268 [Q_KEY_CODE_F4] = ADB_KEY_F4, 269 [Q_KEY_CODE_F5] = ADB_KEY_F5, 270 [Q_KEY_CODE_F6] = ADB_KEY_F6, 271 [Q_KEY_CODE_F7] = ADB_KEY_F7, 272 [Q_KEY_CODE_F8] = ADB_KEY_F8, 273 [Q_KEY_CODE_F9] = ADB_KEY_F9, 274 [Q_KEY_CODE_F10] = ADB_KEY_F10, 275 [Q_KEY_CODE_F11] = ADB_KEY_F11, 276 [Q_KEY_CODE_F12] = ADB_KEY_F12, 277 [Q_KEY_CODE_PRINT] = ADB_KEY_F13, 278 [Q_KEY_CODE_SYSRQ] = ADB_KEY_F13, 279 [Q_KEY_CODE_SCROLL_LOCK] = ADB_KEY_F14, 280 [Q_KEY_CODE_PAUSE] = ADB_KEY_F15, 281 282 [Q_KEY_CODE_NUM_LOCK] = ADB_KEY_KP_CLEAR, 283 [Q_KEY_CODE_KP_EQUALS] = ADB_KEY_KP_EQUAL, 284 [Q_KEY_CODE_KP_DIVIDE] = ADB_KEY_KP_DIVIDE, 285 [Q_KEY_CODE_KP_MULTIPLY] = ADB_KEY_KP_MULTIPLY, 286 [Q_KEY_CODE_KP_SUBTRACT] = ADB_KEY_KP_SUBTRACT, 287 [Q_KEY_CODE_KP_ADD] = ADB_KEY_KP_PLUS, 288 [Q_KEY_CODE_KP_ENTER] = ADB_KEY_KP_ENTER, 289 [Q_KEY_CODE_KP_DECIMAL] = ADB_KEY_KP_PERIOD, 290 [Q_KEY_CODE_KP_0] = ADB_KEY_KP_0, 291 [Q_KEY_CODE_KP_1] = ADB_KEY_KP_1, 292 [Q_KEY_CODE_KP_2] = ADB_KEY_KP_2, 293 [Q_KEY_CODE_KP_3] = ADB_KEY_KP_3, 294 [Q_KEY_CODE_KP_4] = ADB_KEY_KP_4, 295 [Q_KEY_CODE_KP_5] = ADB_KEY_KP_5, 296 [Q_KEY_CODE_KP_6] = ADB_KEY_KP_6, 297 [Q_KEY_CODE_KP_7] = ADB_KEY_KP_7, 298 [Q_KEY_CODE_KP_8] = ADB_KEY_KP_8, 299 [Q_KEY_CODE_KP_9] = ADB_KEY_KP_9, 300 301 [Q_KEY_CODE_UP] = ADB_KEY_UP, 302 [Q_KEY_CODE_DOWN] = ADB_KEY_DOWN, 303 [Q_KEY_CODE_LEFT] = ADB_KEY_LEFT, 304 [Q_KEY_CODE_RIGHT] = ADB_KEY_RIGHT, 305 306 [Q_KEY_CODE_HELP] = ADB_KEY_HELP, 307 [Q_KEY_CODE_INSERT] = ADB_KEY_HELP, 308 [Q_KEY_CODE_DELETE] = ADB_KEY_FORWARD_DELETE, 309 [Q_KEY_CODE_HOME] = ADB_KEY_HOME, 310 [Q_KEY_CODE_END] = ADB_KEY_END, 311 [Q_KEY_CODE_PGUP] = ADB_KEY_PAGE_UP, 312 [Q_KEY_CODE_PGDN] = ADB_KEY_PAGE_DOWN, 313 314 [Q_KEY_CODE_POWER] = ADB_KEY_POWER 315 }; 316 317 static void adb_kbd_put_keycode(void *opaque, int keycode) 318 { 319 KBDState *s = opaque; 320 321 if (s->count < sizeof(s->data)) { 322 s->data[s->wptr] = keycode; 323 if (++s->wptr == sizeof(s->data)) 324 s->wptr = 0; 325 s->count++; 326 } 327 } 328 329 static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf) 330 { 331 KBDState *s = ADB_KEYBOARD(d); 332 int keycode; 333 int olen; 334 335 olen = 0; 336 if (s->count == 0) { 337 return 0; 338 } 339 keycode = s->data[s->rptr]; 340 s->rptr++; 341 if (s->rptr == sizeof(s->data)) { 342 s->rptr = 0; 343 } 344 s->count--; 345 /* 346 * The power key is the only two byte value key, so it is a special case. 347 * Since 0x7f is not a used keycode for ADB we overload it to indicate the 348 * power button when we're storing keycodes in our internal buffer, and 349 * expand it out to two bytes when we send to the guest. 350 */ 351 if (keycode == 0x7f) { 352 obuf[0] = 0x7f; 353 obuf[1] = 0x7f; 354 olen = 2; 355 } else { 356 obuf[0] = keycode; 357 /* NOTE: the power key key-up is the two byte sequence 0xff 0xff; 358 * otherwise we could in theory send a second keycode in the second 359 * byte, but choose not to bother. 360 */ 361 obuf[1] = 0xff; 362 olen = 2; 363 } 364 365 return olen; 366 } 367 368 static int adb_kbd_request(ADBDevice *d, uint8_t *obuf, 369 const uint8_t *buf, int len) 370 { 371 KBDState *s = ADB_KEYBOARD(d); 372 int cmd, reg, olen; 373 374 if ((buf[0] & 0x0f) == ADB_FLUSH) { 375 /* flush keyboard fifo */ 376 s->wptr = s->rptr = s->count = 0; 377 return 0; 378 } 379 380 cmd = buf[0] & 0xc; 381 reg = buf[0] & 0x3; 382 olen = 0; 383 switch(cmd) { 384 case ADB_WRITEREG: 385 switch(reg) { 386 case 2: 387 /* LED status */ 388 break; 389 case 3: 390 switch(buf[2]) { 391 case ADB_CMD_SELF_TEST: 392 break; 393 case ADB_CMD_CHANGE_ID: 394 case ADB_CMD_CHANGE_ID_AND_ACT: 395 case ADB_CMD_CHANGE_ID_AND_ENABLE: 396 d->devaddr = buf[1] & 0xf; 397 break; 398 default: 399 /* XXX: check this */ 400 d->devaddr = buf[1] & 0xf; 401 d->handler = buf[2]; 402 break; 403 } 404 } 405 break; 406 case ADB_READREG: 407 switch(reg) { 408 case 0: 409 olen = adb_kbd_poll(d, obuf); 410 break; 411 case 1: 412 break; 413 case 2: 414 obuf[0] = 0x00; /* XXX: check this */ 415 obuf[1] = 0x07; /* led status */ 416 olen = 2; 417 break; 418 case 3: 419 obuf[0] = d->handler; 420 obuf[1] = d->devaddr; 421 olen = 2; 422 break; 423 } 424 break; 425 } 426 return olen; 427 } 428 429 /* This is where keyboard events enter this file */ 430 static void adb_keyboard_event(DeviceState *dev, QemuConsole *src, 431 InputEvent *evt) 432 { 433 KBDState *s = (KBDState *)dev; 434 int qcode, keycode; 435 436 qcode = qemu_input_key_value_to_qcode(evt->u.key.data->key); 437 if (qcode >= ARRAY_SIZE(qcode_to_adb_keycode)) { 438 return; 439 } 440 keycode = qcode_to_adb_keycode[qcode]; 441 if (keycode == NO_KEY) { /* We don't want to send this to the guest */ 442 ADB_DPRINTF("Ignoring NO_KEY\n"); 443 return; 444 } 445 if (evt->u.key.data->down == false) { /* if key release event */ 446 keycode = keycode | 0x80; /* create keyboard break code */ 447 } 448 449 adb_kbd_put_keycode(s, keycode); 450 } 451 452 static const VMStateDescription vmstate_adb_kbd = { 453 .name = "adb_kbd", 454 .version_id = 2, 455 .minimum_version_id = 2, 456 .fields = (VMStateField[]) { 457 VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice), 458 VMSTATE_BUFFER(data, KBDState), 459 VMSTATE_INT32(rptr, KBDState), 460 VMSTATE_INT32(wptr, KBDState), 461 VMSTATE_INT32(count, KBDState), 462 VMSTATE_END_OF_LIST() 463 } 464 }; 465 466 static void adb_kbd_reset(DeviceState *dev) 467 { 468 ADBDevice *d = ADB_DEVICE(dev); 469 KBDState *s = ADB_KEYBOARD(dev); 470 471 d->handler = 1; 472 d->devaddr = ADB_DEVID_KEYBOARD; 473 memset(s->data, 0, sizeof(s->data)); 474 s->rptr = 0; 475 s->wptr = 0; 476 s->count = 0; 477 } 478 479 static QemuInputHandler adb_keyboard_handler = { 480 .name = "QEMU ADB Keyboard", 481 .mask = INPUT_EVENT_MASK_KEY, 482 .event = adb_keyboard_event, 483 }; 484 485 static void adb_kbd_realizefn(DeviceState *dev, Error **errp) 486 { 487 ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev); 488 akc->parent_realize(dev, errp); 489 qemu_input_handler_register(dev, &adb_keyboard_handler); 490 } 491 492 static void adb_kbd_initfn(Object *obj) 493 { 494 ADBDevice *d = ADB_DEVICE(obj); 495 496 d->devaddr = ADB_DEVID_KEYBOARD; 497 } 498 499 static void adb_kbd_class_init(ObjectClass *oc, void *data) 500 { 501 DeviceClass *dc = DEVICE_CLASS(oc); 502 ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc); 503 ADBKeyboardClass *akc = ADB_KEYBOARD_CLASS(oc); 504 505 akc->parent_realize = dc->realize; 506 dc->realize = adb_kbd_realizefn; 507 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 508 509 adc->devreq = adb_kbd_request; 510 dc->reset = adb_kbd_reset; 511 dc->vmsd = &vmstate_adb_kbd; 512 } 513 514 static const TypeInfo adb_kbd_type_info = { 515 .name = TYPE_ADB_KEYBOARD, 516 .parent = TYPE_ADB_DEVICE, 517 .instance_size = sizeof(KBDState), 518 .instance_init = adb_kbd_initfn, 519 .class_init = adb_kbd_class_init, 520 .class_size = sizeof(ADBKeyboardClass), 521 }; 522 523 /***************************************************************/ 524 /* Mouse ADB device */ 525 526 #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE) 527 528 typedef struct MouseState { 529 /*< public >*/ 530 ADBDevice parent_obj; 531 /*< private >*/ 532 533 int buttons_state, last_buttons_state; 534 int dx, dy, dz; 535 } MouseState; 536 537 #define ADB_MOUSE_CLASS(class) \ 538 OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE) 539 #define ADB_MOUSE_GET_CLASS(obj) \ 540 OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE) 541 542 typedef struct ADBMouseClass { 543 /*< public >*/ 544 ADBDeviceClass parent_class; 545 /*< private >*/ 546 547 DeviceRealize parent_realize; 548 } ADBMouseClass; 549 550 static void adb_mouse_event(void *opaque, 551 int dx1, int dy1, int dz1, int buttons_state) 552 { 553 MouseState *s = opaque; 554 555 s->dx += dx1; 556 s->dy += dy1; 557 s->dz += dz1; 558 s->buttons_state = buttons_state; 559 } 560 561 562 static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf) 563 { 564 MouseState *s = ADB_MOUSE(d); 565 int dx, dy; 566 567 if (s->last_buttons_state == s->buttons_state && 568 s->dx == 0 && s->dy == 0) 569 return 0; 570 571 dx = s->dx; 572 if (dx < -63) 573 dx = -63; 574 else if (dx > 63) 575 dx = 63; 576 577 dy = s->dy; 578 if (dy < -63) 579 dy = -63; 580 else if (dy > 63) 581 dy = 63; 582 583 s->dx -= dx; 584 s->dy -= dy; 585 s->last_buttons_state = s->buttons_state; 586 587 dx &= 0x7f; 588 dy &= 0x7f; 589 590 if (!(s->buttons_state & MOUSE_EVENT_LBUTTON)) 591 dy |= 0x80; 592 if (!(s->buttons_state & MOUSE_EVENT_RBUTTON)) 593 dx |= 0x80; 594 595 obuf[0] = dy; 596 obuf[1] = dx; 597 return 2; 598 } 599 600 static int adb_mouse_request(ADBDevice *d, uint8_t *obuf, 601 const uint8_t *buf, int len) 602 { 603 MouseState *s = ADB_MOUSE(d); 604 int cmd, reg, olen; 605 606 if ((buf[0] & 0x0f) == ADB_FLUSH) { 607 /* flush mouse fifo */ 608 s->buttons_state = s->last_buttons_state; 609 s->dx = 0; 610 s->dy = 0; 611 s->dz = 0; 612 return 0; 613 } 614 615 cmd = buf[0] & 0xc; 616 reg = buf[0] & 0x3; 617 olen = 0; 618 switch(cmd) { 619 case ADB_WRITEREG: 620 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]); 621 switch(reg) { 622 case 2: 623 break; 624 case 3: 625 switch(buf[2]) { 626 case ADB_CMD_SELF_TEST: 627 break; 628 case ADB_CMD_CHANGE_ID: 629 case ADB_CMD_CHANGE_ID_AND_ACT: 630 case ADB_CMD_CHANGE_ID_AND_ENABLE: 631 d->devaddr = buf[1] & 0xf; 632 break; 633 default: 634 /* XXX: check this */ 635 d->devaddr = buf[1] & 0xf; 636 break; 637 } 638 } 639 break; 640 case ADB_READREG: 641 switch(reg) { 642 case 0: 643 olen = adb_mouse_poll(d, obuf); 644 break; 645 case 1: 646 break; 647 case 3: 648 obuf[0] = d->handler; 649 obuf[1] = d->devaddr; 650 olen = 2; 651 break; 652 } 653 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg, 654 obuf[0], obuf[1]); 655 break; 656 } 657 return olen; 658 } 659 660 static void adb_mouse_reset(DeviceState *dev) 661 { 662 ADBDevice *d = ADB_DEVICE(dev); 663 MouseState *s = ADB_MOUSE(dev); 664 665 d->handler = 2; 666 d->devaddr = ADB_DEVID_MOUSE; 667 s->last_buttons_state = s->buttons_state = 0; 668 s->dx = s->dy = s->dz = 0; 669 } 670 671 static const VMStateDescription vmstate_adb_mouse = { 672 .name = "adb_mouse", 673 .version_id = 2, 674 .minimum_version_id = 2, 675 .fields = (VMStateField[]) { 676 VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device, 677 ADBDevice), 678 VMSTATE_INT32(buttons_state, MouseState), 679 VMSTATE_INT32(last_buttons_state, MouseState), 680 VMSTATE_INT32(dx, MouseState), 681 VMSTATE_INT32(dy, MouseState), 682 VMSTATE_INT32(dz, MouseState), 683 VMSTATE_END_OF_LIST() 684 } 685 }; 686 687 static void adb_mouse_realizefn(DeviceState *dev, Error **errp) 688 { 689 MouseState *s = ADB_MOUSE(dev); 690 ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev); 691 692 amc->parent_realize(dev, errp); 693 694 qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse"); 695 } 696 697 static void adb_mouse_initfn(Object *obj) 698 { 699 ADBDevice *d = ADB_DEVICE(obj); 700 701 d->devaddr = ADB_DEVID_MOUSE; 702 } 703 704 static void adb_mouse_class_init(ObjectClass *oc, void *data) 705 { 706 DeviceClass *dc = DEVICE_CLASS(oc); 707 ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc); 708 ADBMouseClass *amc = ADB_MOUSE_CLASS(oc); 709 710 amc->parent_realize = dc->realize; 711 dc->realize = adb_mouse_realizefn; 712 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 713 714 adc->devreq = adb_mouse_request; 715 dc->reset = adb_mouse_reset; 716 dc->vmsd = &vmstate_adb_mouse; 717 } 718 719 static const TypeInfo adb_mouse_type_info = { 720 .name = TYPE_ADB_MOUSE, 721 .parent = TYPE_ADB_DEVICE, 722 .instance_size = sizeof(MouseState), 723 .instance_init = adb_mouse_initfn, 724 .class_init = adb_mouse_class_init, 725 .class_size = sizeof(ADBMouseClass), 726 }; 727 728 729 static void adb_register_types(void) 730 { 731 type_register_static(&adb_bus_type_info); 732 type_register_static(&adb_device_type_info); 733 type_register_static(&adb_kbd_type_info); 734 type_register_static(&adb_mouse_type_info); 735 } 736 737 type_init(adb_register_types) 738