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 "hw/input/adb-keys.h" 28 #include "ui/console.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_CTRL] = ADB_KEY_LEFT_CONTROL, 205 [Q_KEY_CODE_CTRL_R] = ADB_KEY_RIGHT_CONTROL, 206 [Q_KEY_CODE_META_L] = ADB_KEY_COMMAND, 207 [Q_KEY_CODE_META_R] = ADB_KEY_COMMAND, 208 [Q_KEY_CODE_SPC] = ADB_KEY_SPACEBAR, 209 210 [Q_KEY_CODE_ESC] = ADB_KEY_ESC, 211 [Q_KEY_CODE_1] = ADB_KEY_1, 212 [Q_KEY_CODE_2] = ADB_KEY_2, 213 [Q_KEY_CODE_3] = ADB_KEY_3, 214 [Q_KEY_CODE_4] = ADB_KEY_4, 215 [Q_KEY_CODE_5] = ADB_KEY_5, 216 [Q_KEY_CODE_6] = ADB_KEY_6, 217 [Q_KEY_CODE_7] = ADB_KEY_7, 218 [Q_KEY_CODE_8] = ADB_KEY_8, 219 [Q_KEY_CODE_9] = ADB_KEY_9, 220 [Q_KEY_CODE_0] = ADB_KEY_0, 221 [Q_KEY_CODE_MINUS] = ADB_KEY_MINUS, 222 [Q_KEY_CODE_EQUAL] = ADB_KEY_EQUAL, 223 [Q_KEY_CODE_BACKSPACE] = ADB_KEY_DELETE, 224 [Q_KEY_CODE_TAB] = ADB_KEY_TAB, 225 [Q_KEY_CODE_Q] = ADB_KEY_Q, 226 [Q_KEY_CODE_W] = ADB_KEY_W, 227 [Q_KEY_CODE_E] = ADB_KEY_E, 228 [Q_KEY_CODE_R] = ADB_KEY_R, 229 [Q_KEY_CODE_T] = ADB_KEY_T, 230 [Q_KEY_CODE_Y] = ADB_KEY_Y, 231 [Q_KEY_CODE_U] = ADB_KEY_U, 232 [Q_KEY_CODE_I] = ADB_KEY_I, 233 [Q_KEY_CODE_O] = ADB_KEY_O, 234 [Q_KEY_CODE_P] = ADB_KEY_P, 235 [Q_KEY_CODE_BRACKET_LEFT] = ADB_KEY_LEFT_BRACKET, 236 [Q_KEY_CODE_BRACKET_RIGHT] = ADB_KEY_RIGHT_BRACKET, 237 [Q_KEY_CODE_RET] = ADB_KEY_RETURN, 238 [Q_KEY_CODE_A] = ADB_KEY_A, 239 [Q_KEY_CODE_S] = ADB_KEY_S, 240 [Q_KEY_CODE_D] = ADB_KEY_D, 241 [Q_KEY_CODE_F] = ADB_KEY_F, 242 [Q_KEY_CODE_G] = ADB_KEY_G, 243 [Q_KEY_CODE_H] = ADB_KEY_H, 244 [Q_KEY_CODE_J] = ADB_KEY_J, 245 [Q_KEY_CODE_K] = ADB_KEY_K, 246 [Q_KEY_CODE_L] = ADB_KEY_L, 247 [Q_KEY_CODE_SEMICOLON] = ADB_KEY_SEMICOLON, 248 [Q_KEY_CODE_APOSTROPHE] = ADB_KEY_APOSTROPHE, 249 [Q_KEY_CODE_GRAVE_ACCENT] = ADB_KEY_GRAVE_ACCENT, 250 [Q_KEY_CODE_BACKSLASH] = ADB_KEY_BACKSLASH, 251 [Q_KEY_CODE_Z] = ADB_KEY_Z, 252 [Q_KEY_CODE_X] = ADB_KEY_X, 253 [Q_KEY_CODE_C] = ADB_KEY_C, 254 [Q_KEY_CODE_V] = ADB_KEY_V, 255 [Q_KEY_CODE_B] = ADB_KEY_B, 256 [Q_KEY_CODE_N] = ADB_KEY_N, 257 [Q_KEY_CODE_M] = ADB_KEY_M, 258 [Q_KEY_CODE_COMMA] = ADB_KEY_COMMA, 259 [Q_KEY_CODE_DOT] = ADB_KEY_PERIOD, 260 [Q_KEY_CODE_SLASH] = ADB_KEY_FORWARD_SLASH, 261 [Q_KEY_CODE_ASTERISK] = ADB_KEY_KP_MULTIPLY, 262 [Q_KEY_CODE_CAPS_LOCK] = ADB_KEY_CAPS_LOCK, 263 264 [Q_KEY_CODE_F1] = ADB_KEY_F1, 265 [Q_KEY_CODE_F2] = ADB_KEY_F2, 266 [Q_KEY_CODE_F3] = ADB_KEY_F3, 267 [Q_KEY_CODE_F4] = ADB_KEY_F4, 268 [Q_KEY_CODE_F5] = ADB_KEY_F5, 269 [Q_KEY_CODE_F6] = ADB_KEY_F6, 270 [Q_KEY_CODE_F7] = ADB_KEY_F7, 271 [Q_KEY_CODE_F8] = ADB_KEY_F8, 272 [Q_KEY_CODE_F9] = ADB_KEY_F9, 273 [Q_KEY_CODE_F10] = ADB_KEY_F10, 274 [Q_KEY_CODE_F11] = ADB_KEY_F11, 275 [Q_KEY_CODE_F12] = ADB_KEY_F12, 276 [Q_KEY_CODE_PRINT] = ADB_KEY_F13, 277 [Q_KEY_CODE_SYSRQ] = ADB_KEY_F13, 278 [Q_KEY_CODE_SCROLL_LOCK] = ADB_KEY_F14, 279 [Q_KEY_CODE_PAUSE] = ADB_KEY_F15, 280 281 [Q_KEY_CODE_NUM_LOCK] = ADB_KEY_KP_CLEAR, 282 [Q_KEY_CODE_KP_EQUALS] = ADB_KEY_KP_EQUAL, 283 [Q_KEY_CODE_KP_DIVIDE] = ADB_KEY_KP_DIVIDE, 284 [Q_KEY_CODE_KP_MULTIPLY] = ADB_KEY_KP_MULTIPLY, 285 [Q_KEY_CODE_KP_SUBTRACT] = ADB_KEY_KP_SUBTRACT, 286 [Q_KEY_CODE_KP_ADD] = ADB_KEY_KP_PLUS, 287 [Q_KEY_CODE_KP_ENTER] = ADB_KEY_KP_ENTER, 288 [Q_KEY_CODE_KP_DECIMAL] = ADB_KEY_KP_PERIOD, 289 [Q_KEY_CODE_KP_0] = ADB_KEY_KP_0, 290 [Q_KEY_CODE_KP_1] = ADB_KEY_KP_1, 291 [Q_KEY_CODE_KP_2] = ADB_KEY_KP_2, 292 [Q_KEY_CODE_KP_3] = ADB_KEY_KP_3, 293 [Q_KEY_CODE_KP_4] = ADB_KEY_KP_4, 294 [Q_KEY_CODE_KP_5] = ADB_KEY_KP_5, 295 [Q_KEY_CODE_KP_6] = ADB_KEY_KP_6, 296 [Q_KEY_CODE_KP_7] = ADB_KEY_KP_7, 297 [Q_KEY_CODE_KP_8] = ADB_KEY_KP_8, 298 [Q_KEY_CODE_KP_9] = ADB_KEY_KP_9, 299 300 [Q_KEY_CODE_UP] = ADB_KEY_UP, 301 [Q_KEY_CODE_DOWN] = ADB_KEY_DOWN, 302 [Q_KEY_CODE_LEFT] = ADB_KEY_LEFT, 303 [Q_KEY_CODE_RIGHT] = ADB_KEY_RIGHT, 304 305 [Q_KEY_CODE_HELP] = ADB_KEY_HELP, 306 [Q_KEY_CODE_INSERT] = ADB_KEY_HELP, 307 [Q_KEY_CODE_DELETE] = ADB_KEY_FORWARD_DELETE, 308 [Q_KEY_CODE_HOME] = ADB_KEY_HOME, 309 [Q_KEY_CODE_END] = ADB_KEY_END, 310 [Q_KEY_CODE_PGUP] = ADB_KEY_PAGE_UP, 311 [Q_KEY_CODE_PGDN] = ADB_KEY_PAGE_DOWN, 312 313 [Q_KEY_CODE_POWER] = ADB_KEY_POWER 314 }; 315 316 static void adb_kbd_put_keycode(void *opaque, int keycode) 317 { 318 KBDState *s = opaque; 319 320 if (s->count < sizeof(s->data)) { 321 s->data[s->wptr] = keycode; 322 if (++s->wptr == sizeof(s->data)) 323 s->wptr = 0; 324 s->count++; 325 } 326 } 327 328 static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf) 329 { 330 KBDState *s = ADB_KEYBOARD(d); 331 int keycode; 332 int olen; 333 334 olen = 0; 335 if (s->count == 0) { 336 return 0; 337 } 338 keycode = s->data[s->rptr]; 339 s->rptr++; 340 if (s->rptr == sizeof(s->data)) { 341 s->rptr = 0; 342 } 343 s->count--; 344 /* 345 * The power key is the only two byte value key, so it is a special case. 346 * Since 0x7f is not a used keycode for ADB we overload it to indicate the 347 * power button when we're storing keycodes in our internal buffer, and 348 * expand it out to two bytes when we send to the guest. 349 */ 350 if (keycode == 0x7f) { 351 obuf[0] = 0x7f; 352 obuf[1] = 0x7f; 353 olen = 2; 354 } else { 355 obuf[0] = keycode; 356 /* NOTE: the power key key-up is the two byte sequence 0xff 0xff; 357 * otherwise we could in theory send a second keycode in the second 358 * byte, but choose not to bother. 359 */ 360 obuf[1] = 0xff; 361 olen = 2; 362 } 363 364 return olen; 365 } 366 367 static int adb_kbd_request(ADBDevice *d, uint8_t *obuf, 368 const uint8_t *buf, int len) 369 { 370 KBDState *s = ADB_KEYBOARD(d); 371 int cmd, reg, olen; 372 373 if ((buf[0] & 0x0f) == ADB_FLUSH) { 374 /* flush keyboard fifo */ 375 s->wptr = s->rptr = s->count = 0; 376 return 0; 377 } 378 379 cmd = buf[0] & 0xc; 380 reg = buf[0] & 0x3; 381 olen = 0; 382 switch(cmd) { 383 case ADB_WRITEREG: 384 switch(reg) { 385 case 2: 386 /* LED status */ 387 break; 388 case 3: 389 switch(buf[2]) { 390 case ADB_CMD_SELF_TEST: 391 break; 392 case ADB_CMD_CHANGE_ID: 393 case ADB_CMD_CHANGE_ID_AND_ACT: 394 case ADB_CMD_CHANGE_ID_AND_ENABLE: 395 d->devaddr = buf[1] & 0xf; 396 break; 397 default: 398 d->devaddr = buf[1] & 0xf; 399 /* we support handlers: 400 * 1: Apple Standard Keyboard 401 * 2: Apple Extended Keyboard (LShift = RShift) 402 * 3: Apple Extended Keyboard (LShift != RShift) 403 */ 404 if (buf[2] == 1 || buf[2] == 2 || buf[2] == 3) { 405 d->handler = buf[2]; 406 } 407 break; 408 } 409 } 410 break; 411 case ADB_READREG: 412 switch(reg) { 413 case 0: 414 olen = adb_kbd_poll(d, obuf); 415 break; 416 case 1: 417 break; 418 case 2: 419 obuf[0] = 0x00; /* XXX: check this */ 420 obuf[1] = 0x07; /* led status */ 421 olen = 2; 422 break; 423 case 3: 424 obuf[0] = d->handler; 425 obuf[1] = d->devaddr; 426 olen = 2; 427 break; 428 } 429 break; 430 } 431 return olen; 432 } 433 434 /* This is where keyboard events enter this file */ 435 static void adb_keyboard_event(DeviceState *dev, QemuConsole *src, 436 InputEvent *evt) 437 { 438 KBDState *s = (KBDState *)dev; 439 int qcode, keycode; 440 441 qcode = qemu_input_key_value_to_qcode(evt->u.key.data->key); 442 if (qcode >= ARRAY_SIZE(qcode_to_adb_keycode)) { 443 return; 444 } 445 /* FIXME: take handler into account when translating qcode */ 446 keycode = qcode_to_adb_keycode[qcode]; 447 if (keycode == NO_KEY) { /* We don't want to send this to the guest */ 448 ADB_DPRINTF("Ignoring NO_KEY\n"); 449 return; 450 } 451 if (evt->u.key.data->down == false) { /* if key release event */ 452 keycode = keycode | 0x80; /* create keyboard break code */ 453 } 454 455 adb_kbd_put_keycode(s, keycode); 456 } 457 458 static const VMStateDescription vmstate_adb_kbd = { 459 .name = "adb_kbd", 460 .version_id = 2, 461 .minimum_version_id = 2, 462 .fields = (VMStateField[]) { 463 VMSTATE_STRUCT(parent_obj, KBDState, 0, vmstate_adb_device, ADBDevice), 464 VMSTATE_BUFFER(data, KBDState), 465 VMSTATE_INT32(rptr, KBDState), 466 VMSTATE_INT32(wptr, KBDState), 467 VMSTATE_INT32(count, KBDState), 468 VMSTATE_END_OF_LIST() 469 } 470 }; 471 472 static void adb_kbd_reset(DeviceState *dev) 473 { 474 ADBDevice *d = ADB_DEVICE(dev); 475 KBDState *s = ADB_KEYBOARD(dev); 476 477 d->handler = 1; 478 d->devaddr = ADB_DEVID_KEYBOARD; 479 memset(s->data, 0, sizeof(s->data)); 480 s->rptr = 0; 481 s->wptr = 0; 482 s->count = 0; 483 } 484 485 static QemuInputHandler adb_keyboard_handler = { 486 .name = "QEMU ADB Keyboard", 487 .mask = INPUT_EVENT_MASK_KEY, 488 .event = adb_keyboard_event, 489 }; 490 491 static void adb_kbd_realizefn(DeviceState *dev, Error **errp) 492 { 493 ADBKeyboardClass *akc = ADB_KEYBOARD_GET_CLASS(dev); 494 akc->parent_realize(dev, errp); 495 qemu_input_handler_register(dev, &adb_keyboard_handler); 496 } 497 498 static void adb_kbd_initfn(Object *obj) 499 { 500 ADBDevice *d = ADB_DEVICE(obj); 501 502 d->devaddr = ADB_DEVID_KEYBOARD; 503 } 504 505 static void adb_kbd_class_init(ObjectClass *oc, void *data) 506 { 507 DeviceClass *dc = DEVICE_CLASS(oc); 508 ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc); 509 ADBKeyboardClass *akc = ADB_KEYBOARD_CLASS(oc); 510 511 akc->parent_realize = dc->realize; 512 dc->realize = adb_kbd_realizefn; 513 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 514 515 adc->devreq = adb_kbd_request; 516 dc->reset = adb_kbd_reset; 517 dc->vmsd = &vmstate_adb_kbd; 518 } 519 520 static const TypeInfo adb_kbd_type_info = { 521 .name = TYPE_ADB_KEYBOARD, 522 .parent = TYPE_ADB_DEVICE, 523 .instance_size = sizeof(KBDState), 524 .instance_init = adb_kbd_initfn, 525 .class_init = adb_kbd_class_init, 526 .class_size = sizeof(ADBKeyboardClass), 527 }; 528 529 /***************************************************************/ 530 /* Mouse ADB device */ 531 532 #define ADB_MOUSE(obj) OBJECT_CHECK(MouseState, (obj), TYPE_ADB_MOUSE) 533 534 typedef struct MouseState { 535 /*< public >*/ 536 ADBDevice parent_obj; 537 /*< private >*/ 538 539 int buttons_state, last_buttons_state; 540 int dx, dy, dz; 541 } MouseState; 542 543 #define ADB_MOUSE_CLASS(class) \ 544 OBJECT_CLASS_CHECK(ADBMouseClass, (class), TYPE_ADB_MOUSE) 545 #define ADB_MOUSE_GET_CLASS(obj) \ 546 OBJECT_GET_CLASS(ADBMouseClass, (obj), TYPE_ADB_MOUSE) 547 548 typedef struct ADBMouseClass { 549 /*< public >*/ 550 ADBDeviceClass parent_class; 551 /*< private >*/ 552 553 DeviceRealize parent_realize; 554 } ADBMouseClass; 555 556 static void adb_mouse_event(void *opaque, 557 int dx1, int dy1, int dz1, int buttons_state) 558 { 559 MouseState *s = opaque; 560 561 s->dx += dx1; 562 s->dy += dy1; 563 s->dz += dz1; 564 s->buttons_state = buttons_state; 565 } 566 567 568 static int adb_mouse_poll(ADBDevice *d, uint8_t *obuf) 569 { 570 MouseState *s = ADB_MOUSE(d); 571 int dx, dy; 572 573 if (s->last_buttons_state == s->buttons_state && 574 s->dx == 0 && s->dy == 0) 575 return 0; 576 577 dx = s->dx; 578 if (dx < -63) 579 dx = -63; 580 else if (dx > 63) 581 dx = 63; 582 583 dy = s->dy; 584 if (dy < -63) 585 dy = -63; 586 else if (dy > 63) 587 dy = 63; 588 589 s->dx -= dx; 590 s->dy -= dy; 591 s->last_buttons_state = s->buttons_state; 592 593 dx &= 0x7f; 594 dy &= 0x7f; 595 596 if (!(s->buttons_state & MOUSE_EVENT_LBUTTON)) 597 dy |= 0x80; 598 if (!(s->buttons_state & MOUSE_EVENT_RBUTTON)) 599 dx |= 0x80; 600 601 obuf[0] = dy; 602 obuf[1] = dx; 603 return 2; 604 } 605 606 static int adb_mouse_request(ADBDevice *d, uint8_t *obuf, 607 const uint8_t *buf, int len) 608 { 609 MouseState *s = ADB_MOUSE(d); 610 int cmd, reg, olen; 611 612 if ((buf[0] & 0x0f) == ADB_FLUSH) { 613 /* flush mouse fifo */ 614 s->buttons_state = s->last_buttons_state; 615 s->dx = 0; 616 s->dy = 0; 617 s->dz = 0; 618 return 0; 619 } 620 621 cmd = buf[0] & 0xc; 622 reg = buf[0] & 0x3; 623 olen = 0; 624 switch(cmd) { 625 case ADB_WRITEREG: 626 ADB_DPRINTF("write reg %d val 0x%2.2x\n", reg, buf[1]); 627 switch(reg) { 628 case 2: 629 break; 630 case 3: 631 switch(buf[2]) { 632 case ADB_CMD_SELF_TEST: 633 break; 634 case ADB_CMD_CHANGE_ID: 635 case ADB_CMD_CHANGE_ID_AND_ACT: 636 case ADB_CMD_CHANGE_ID_AND_ENABLE: 637 d->devaddr = buf[1] & 0xf; 638 break; 639 default: 640 d->devaddr = buf[1] & 0xf; 641 /* we support handlers: 642 * 0x01: Classic Apple Mouse Protocol / 100 cpi operations 643 * 0x02: Classic Apple Mouse Protocol / 200 cpi operations 644 * we don't support handlers (at least): 645 * 0x03: Mouse systems A3 trackball 646 * 0x04: Extended Apple Mouse Protocol 647 * 0x2f: Microspeed mouse 648 * 0x42: Macally 649 * 0x5f: Microspeed mouse 650 * 0x66: Microspeed mouse 651 */ 652 if (buf[2] == 1 || buf[2] == 2) { 653 d->handler = buf[2]; 654 } 655 break; 656 } 657 } 658 break; 659 case ADB_READREG: 660 switch(reg) { 661 case 0: 662 olen = adb_mouse_poll(d, obuf); 663 break; 664 case 1: 665 break; 666 case 3: 667 obuf[0] = d->handler; 668 obuf[1] = d->devaddr; 669 olen = 2; 670 break; 671 } 672 ADB_DPRINTF("read reg %d obuf[0] 0x%2.2x obuf[1] 0x%2.2x\n", reg, 673 obuf[0], obuf[1]); 674 break; 675 } 676 return olen; 677 } 678 679 static void adb_mouse_reset(DeviceState *dev) 680 { 681 ADBDevice *d = ADB_DEVICE(dev); 682 MouseState *s = ADB_MOUSE(dev); 683 684 d->handler = 2; 685 d->devaddr = ADB_DEVID_MOUSE; 686 s->last_buttons_state = s->buttons_state = 0; 687 s->dx = s->dy = s->dz = 0; 688 } 689 690 static const VMStateDescription vmstate_adb_mouse = { 691 .name = "adb_mouse", 692 .version_id = 2, 693 .minimum_version_id = 2, 694 .fields = (VMStateField[]) { 695 VMSTATE_STRUCT(parent_obj, MouseState, 0, vmstate_adb_device, 696 ADBDevice), 697 VMSTATE_INT32(buttons_state, MouseState), 698 VMSTATE_INT32(last_buttons_state, MouseState), 699 VMSTATE_INT32(dx, MouseState), 700 VMSTATE_INT32(dy, MouseState), 701 VMSTATE_INT32(dz, MouseState), 702 VMSTATE_END_OF_LIST() 703 } 704 }; 705 706 static void adb_mouse_realizefn(DeviceState *dev, Error **errp) 707 { 708 MouseState *s = ADB_MOUSE(dev); 709 ADBMouseClass *amc = ADB_MOUSE_GET_CLASS(dev); 710 711 amc->parent_realize(dev, errp); 712 713 qemu_add_mouse_event_handler(adb_mouse_event, s, 0, "QEMU ADB Mouse"); 714 } 715 716 static void adb_mouse_initfn(Object *obj) 717 { 718 ADBDevice *d = ADB_DEVICE(obj); 719 720 d->devaddr = ADB_DEVID_MOUSE; 721 } 722 723 static void adb_mouse_class_init(ObjectClass *oc, void *data) 724 { 725 DeviceClass *dc = DEVICE_CLASS(oc); 726 ADBDeviceClass *adc = ADB_DEVICE_CLASS(oc); 727 ADBMouseClass *amc = ADB_MOUSE_CLASS(oc); 728 729 amc->parent_realize = dc->realize; 730 dc->realize = adb_mouse_realizefn; 731 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 732 733 adc->devreq = adb_mouse_request; 734 dc->reset = adb_mouse_reset; 735 dc->vmsd = &vmstate_adb_mouse; 736 } 737 738 static const TypeInfo adb_mouse_type_info = { 739 .name = TYPE_ADB_MOUSE, 740 .parent = TYPE_ADB_DEVICE, 741 .instance_size = sizeof(MouseState), 742 .instance_init = adb_mouse_initfn, 743 .class_init = adb_mouse_class_init, 744 .class_size = sizeof(ADBMouseClass), 745 }; 746 747 748 static void adb_register_types(void) 749 { 750 type_register_static(&adb_bus_type_info); 751 type_register_static(&adb_device_type_info); 752 type_register_static(&adb_kbd_type_info); 753 type_register_static(&adb_mouse_type_info); 754 } 755 756 type_init(adb_register_types) 757