1 /* Copyright (C) 2007-2008 The Android Open Source Project 2 ** 3 ** This software is licensed under the terms of the GNU General Public 4 ** License version 2, as published by the Free Software Foundation, and 5 ** may be copied, distributed, and modified under those terms. 6 ** 7 ** This program is distributed in the hope that it will be useful, 8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 ** GNU General Public License for more details. 11 */ 12 #include "hw/android/goldfish/device.h" 13 #include "migration/qemu-file.h" 14 #include "android/hw-events.h" 15 #include "android/charmap.h" 16 #include "android/globals.h" /* for android_hw */ 17 #include "android/multitouch-screen.h" 18 #include "exec/cpu-common.h" 19 #include "exec/hwaddr.h" 20 #include "hw/hw.h" 21 #include "hw/irq.h" 22 #include "android/user-events.h" 23 #include "ui/console.h" 24 25 #define MAX_EVENTS 256*4 26 27 enum { 28 REG_READ = 0x00, 29 REG_SET_PAGE = 0x00, 30 REG_LEN = 0x04, 31 REG_DATA = 0x08, 32 33 PAGE_NAME = 0x00000, 34 PAGE_EVBITS = 0x10000, 35 PAGE_ABSDATA = 0x20000 | EV_ABS, 36 }; 37 38 /* These corresponds to the state of the driver. 39 * Unfortunately, we have to buffer events coming 40 * from the UI, since the kernel driver is not 41 * capable of receiving them until XXXXXX 42 */ 43 enum { 44 STATE_INIT = 0, /* The device is initialized */ 45 STATE_BUFFERED, /* Events have been buffered, but no IRQ raised yet */ 46 STATE_LIVE /* Events can be sent directly to the kernel */ 47 }; 48 49 /* NOTE: The ev_bits arrays are used to indicate to the kernel 50 * which events can be sent by the emulated hardware. 51 */ 52 53 typedef struct 54 { 55 uint32_t base; 56 qemu_irq irq; 57 int pending; 58 int page; 59 60 unsigned events[MAX_EVENTS]; 61 unsigned first; 62 unsigned last; 63 unsigned state; 64 65 const char *name; 66 67 struct { 68 size_t len; 69 uint8_t *bits; 70 } ev_bits[EV_MAX + 1]; 71 72 int32_t *abs_info; 73 size_t abs_info_count; 74 } events_state; 75 76 /* An entry in the array of ABS_XXX values */ 77 typedef struct ABSEntry { 78 /* Minimum ABS_XXX value. */ 79 uint32_t min; 80 /* Maximum ABS_XXX value. */ 81 uint32_t max; 82 /* 'fuzz;, and 'flat' ABS_XXX values are always zero here. */ 83 uint32_t fuzz; 84 uint32_t flat; 85 } ABSEntry; 86 87 88 /* modify this each time you change the events_device structure. you 89 * will also need to upadte events_state_load and events_state_save 90 */ 91 #define EVENTS_STATE_SAVE_VERSION 2 92 93 #undef QFIELD_STRUCT 94 #define QFIELD_STRUCT events_state 95 96 QFIELD_BEGIN(events_state_fields) 97 QFIELD_INT32(pending), 98 QFIELD_INT32(page), 99 QFIELD_BUFFER(events), 100 QFIELD_INT32(first), 101 QFIELD_INT32(last), 102 QFIELD_INT32(state), 103 QFIELD_END 104 105 static void events_state_save(QEMUFile* f, void* opaque) 106 { 107 events_state* s = opaque; 108 109 qemu_put_struct(f, events_state_fields, s); 110 } 111 112 static int events_state_load(QEMUFile* f, void* opaque, int version_id) 113 { 114 events_state* s = opaque; 115 116 if (version_id != EVENTS_STATE_SAVE_VERSION) 117 return -1; 118 119 return qemu_get_struct(f, events_state_fields, s); 120 } 121 122 static void enqueue_event(events_state *s, unsigned int type, unsigned int code, int value) 123 { 124 int enqueued = s->last - s->first; 125 126 if (enqueued < 0) 127 enqueued += MAX_EVENTS; 128 129 if (enqueued + 3 > MAX_EVENTS) { 130 fprintf(stderr, "##KBD: Full queue, lose event\n"); 131 return; 132 } 133 134 if(s->first == s->last) { 135 if (s->state == STATE_LIVE) 136 qemu_irq_raise(s->irq); 137 else { 138 s->state = STATE_BUFFERED; 139 } 140 } 141 142 //fprintf(stderr, "##KBD: type=%d code=%d value=%d\n", type, code, value); 143 144 s->events[s->last] = type; 145 s->last = (s->last + 1) & (MAX_EVENTS-1); 146 s->events[s->last] = code; 147 s->last = (s->last + 1) & (MAX_EVENTS-1); 148 s->events[s->last] = value; 149 s->last = (s->last + 1) & (MAX_EVENTS-1); 150 } 151 152 static unsigned dequeue_event(events_state *s) 153 { 154 unsigned n; 155 156 if(s->first == s->last) { 157 return 0; 158 } 159 160 n = s->events[s->first]; 161 162 s->first = (s->first + 1) & (MAX_EVENTS - 1); 163 164 if(s->first == s->last) { 165 qemu_irq_lower(s->irq); 166 } 167 #ifdef TARGET_I386 168 /* 169 * Adding the logic to handle edge-triggered interrupts for x86 170 * because the exisiting goldfish events device basically provides 171 * level-trigger interrupts only. 172 * 173 * Logic: When an event (including the type/code/value) is fetched 174 * by the driver, if there is still another event in the event 175 * queue, the goldfish event device will re-assert the IRQ so that 176 * the driver can be notified to fetch the event again. 177 */ 178 else if (((s->first + 2) & (MAX_EVENTS - 1)) < s->last || 179 (s->first & (MAX_EVENTS - 1)) > s->last) { /* if there still is an event */ 180 qemu_irq_lower(s->irq); 181 qemu_irq_raise(s->irq); 182 } 183 #endif 184 return n; 185 } 186 187 static int get_page_len(events_state *s) 188 { 189 int page = s->page; 190 if (page == PAGE_NAME) { 191 const char* name = s->name; 192 return strlen(name); 193 } if (page >= PAGE_EVBITS && page <= PAGE_EVBITS + EV_MAX) 194 return s->ev_bits[page - PAGE_EVBITS].len; 195 if (page == PAGE_ABSDATA) 196 return s->abs_info_count * sizeof(s->abs_info[0]); 197 return 0; 198 } 199 200 static int get_page_data(events_state *s, int offset) 201 { 202 int page_len = get_page_len(s); 203 int page = s->page; 204 if (offset > page_len) 205 return 0; 206 if (page == PAGE_NAME) { 207 const char* name = s->name; 208 return name[offset]; 209 } if (page >= PAGE_EVBITS && page <= PAGE_EVBITS + EV_MAX) 210 return s->ev_bits[page - PAGE_EVBITS].bits[offset]; 211 if (page == PAGE_ABSDATA) { 212 return s->abs_info[offset / sizeof(s->abs_info[0])]; 213 } 214 return 0; 215 } 216 217 static uint32_t events_read(void *x, hwaddr off) 218 { 219 events_state *s = (events_state *) x; 220 int offset = off; // - s->base; 221 222 /* This gross hack below is used to ensure that we 223 * only raise the IRQ when the kernel driver is 224 * properly ready! If done before this, the driver 225 * becomes confused and ignores all input events 226 * as soon as one was buffered! 227 */ 228 if (offset == REG_LEN && s->page == PAGE_ABSDATA) { 229 if (s->state == STATE_BUFFERED) 230 qemu_irq_raise(s->irq); 231 s->state = STATE_LIVE; 232 } 233 234 if (offset == REG_READ) 235 return dequeue_event(s); 236 else if (offset == REG_LEN) 237 return get_page_len(s); 238 else if (offset >= REG_DATA) 239 return get_page_data(s, offset - REG_DATA); 240 return 0; // this shouldn't happen, if the driver does the right thing 241 } 242 243 static void events_write(void *x, hwaddr off, uint32_t val) 244 { 245 events_state *s = (events_state *) x; 246 int offset = off; // - s->base; 247 if (offset == REG_SET_PAGE) 248 s->page = val; 249 } 250 251 static CPUReadMemoryFunc *events_readfn[] = { 252 events_read, 253 events_read, 254 events_read 255 }; 256 257 static CPUWriteMemoryFunc *events_writefn[] = { 258 events_write, 259 events_write, 260 events_write 261 }; 262 263 static void events_put_keycode(void *x, int keycode) 264 { 265 events_state *s = (events_state *) x; 266 267 enqueue_event(s, EV_KEY, keycode&0x1ff, (keycode&0x200) ? 1 : 0); 268 } 269 270 static void events_put_mouse(void *opaque, int dx, int dy, int dz, int buttons_state) 271 { 272 events_state *s = (events_state *) opaque; 273 /* in the Android emulator, we use dz == 0 for touchscreen events, 274 * and dz == 1 for trackball events. See the kbd_mouse_event calls 275 * in android/skin/trackball.c and android/skin/window.c 276 */ 277 if (dz == 0) { 278 if (androidHwConfig_isScreenMultiTouch(android_hw)) { 279 /* Convert mouse event into multi-touch event */ 280 multitouch_update_pointer(MTES_MOUSE, 0, dx, dy, 281 (buttons_state & 1) ? 0x81 : 0); 282 } else if (androidHwConfig_isScreenTouch(android_hw)) { 283 enqueue_event(s, EV_ABS, ABS_X, dx); 284 enqueue_event(s, EV_ABS, ABS_Y, dy); 285 enqueue_event(s, EV_ABS, ABS_Z, dz); 286 enqueue_event(s, EV_KEY, BTN_TOUCH, buttons_state&1); 287 enqueue_event(s, EV_SYN, 0, 0); 288 } 289 } else { 290 enqueue_event(s, EV_REL, REL_X, dx); 291 enqueue_event(s, EV_REL, REL_Y, dy); 292 enqueue_event(s, EV_SYN, 0, 0); 293 } 294 } 295 296 static void events_put_generic(void* opaque, int type, int code, int value) 297 { 298 events_state *s = (events_state *) opaque; 299 300 enqueue_event(s, type, code, value); 301 } 302 303 /* set bits [bitl..bith] in the ev_bits[type] array 304 */ 305 static void 306 events_set_bits(events_state *s, int type, int bitl, int bith) 307 { 308 uint8_t *bits; 309 uint8_t maskl, maskh; 310 int il, ih; 311 il = bitl / 8; 312 ih = bith / 8; 313 if (ih >= s->ev_bits[type].len) { 314 bits = g_malloc0(ih + 1); 315 if (bits == NULL) 316 return; 317 memcpy(bits, s->ev_bits[type].bits, s->ev_bits[type].len); 318 g_free(s->ev_bits[type].bits); 319 s->ev_bits[type].bits = bits; 320 s->ev_bits[type].len = ih + 1; 321 } 322 else 323 bits = s->ev_bits[type].bits; 324 maskl = 0xffU << (bitl & 7); 325 maskh = 0xffU >> (7 - (bith & 7)); 326 if (il >= ih) 327 maskh &= maskl; 328 else { 329 bits[il] |= maskl; 330 while (++il < ih) 331 bits[il] = 0xff; 332 } 333 bits[ih] |= maskh; 334 } 335 336 static void 337 events_set_bit(events_state* s, int type, int bit) 338 { 339 events_set_bits(s, type, bit, bit); 340 } 341 342 static void 343 events_clr_bit(events_state* s, int type, int bit) 344 { 345 int ii = bit / 8; 346 if (ii < s->ev_bits[type].len) { 347 uint8_t* bits = s->ev_bits[type].bits; 348 uint8_t mask = 0x01U << (bit & 7); 349 bits[ii] &= ~mask; 350 } 351 } 352 353 void events_dev_init(uint32_t base, qemu_irq irq) 354 { 355 events_state *s; 356 int iomemtype; 357 AndroidHwConfig* config = android_hw; 358 359 s = (events_state *) g_malloc0(sizeof(events_state)); 360 361 /* now set the events capability bits depending on hardware configuration */ 362 /* apparently, the EV_SYN array is used to indicate which other 363 * event classes to consider. 364 */ 365 366 /* configure EV_KEY array 367 * 368 * All Android devices must have the following keys: 369 * KEY_HOME, KEY_BACK, KEY_SEND (Call), KEY_END (EndCall), 370 * KEY_SOFT1 (Menu), VOLUME_UP, VOLUME_DOWN 371 * 372 * Note that previous models also had a KEY_SOFT2, 373 * and a KEY_POWER which we still support here. 374 * 375 * Newer models have a KEY_SEARCH key, which we always 376 * enable here. 377 * 378 * A Dpad will send: KEY_DOWN / UP / LEFT / RIGHT / CENTER 379 * 380 * The KEY_CAMERA button isn't very useful if there is no camera. 381 * 382 * BTN_MOUSE is sent when the trackball is pressed 383 * BTN_TOUCH is sent when the touchscreen is pressed 384 */ 385 events_set_bit (s, EV_SYN, EV_KEY ); 386 387 events_set_bit(s, EV_KEY, KEY_HOME); 388 events_set_bit(s, EV_KEY, KEY_BACK); 389 events_set_bit(s, EV_KEY, KEY_SEND); 390 events_set_bit(s, EV_KEY, KEY_END); 391 events_set_bit(s, EV_KEY, KEY_SOFT1); 392 events_set_bit(s, EV_KEY, KEY_VOLUMEUP); 393 events_set_bit(s, EV_KEY, KEY_VOLUMEDOWN); 394 events_set_bit(s, EV_KEY, KEY_SOFT2); 395 events_set_bit(s, EV_KEY, KEY_POWER); 396 events_set_bit(s, EV_KEY, KEY_SEARCH); 397 398 if (config->hw_dPad) { 399 events_set_bit(s, EV_KEY, KEY_DOWN); 400 events_set_bit(s, EV_KEY, KEY_UP); 401 events_set_bit(s, EV_KEY, KEY_LEFT); 402 events_set_bit(s, EV_KEY, KEY_RIGHT); 403 events_set_bit(s, EV_KEY, KEY_CENTER); 404 } 405 406 if (config->hw_trackBall) { 407 events_set_bit(s, EV_KEY, BTN_MOUSE); 408 } 409 if (androidHwConfig_isScreenTouch(config)) { 410 events_set_bit(s, EV_KEY, BTN_TOUCH); 411 } 412 413 if (strcmp(config->hw_camera_back, "none") || 414 strcmp(config->hw_camera_front, "none")) { 415 /* Camera emulation is enabled. */ 416 events_set_bit(s, EV_KEY, KEY_CAMERA); 417 } 418 419 if (config->hw_keyboard) { 420 /* since we want to implement Unicode reverse-mapping 421 * allow any kind of key, even those not available on 422 * the skin. 423 * 424 * the previous code did set the [1..0x1ff] range, but 425 * we don't want to enable certain bits in the middle 426 * of the range that are registered for mouse/trackball/joystick 427 * events. 428 * 429 * see "linux_keycodes.h" for the list of events codes. 430 */ 431 events_set_bits(s, EV_KEY, 1, 0xff); 432 events_set_bits(s, EV_KEY, 0x160, 0x1ff); 433 434 /* If there is a keyboard, but no DPad, we need to clear the 435 * corresponding bits. Doing this is simpler than trying to exclude 436 * the DPad values from the ranges above. 437 */ 438 if (!config->hw_dPad) { 439 events_clr_bit(s, EV_KEY, KEY_DOWN); 440 events_clr_bit(s, EV_KEY, KEY_UP); 441 events_clr_bit(s, EV_KEY, KEY_LEFT); 442 events_clr_bit(s, EV_KEY, KEY_RIGHT); 443 events_clr_bit(s, EV_KEY, KEY_CENTER); 444 } 445 } 446 447 /* configure EV_REL array 448 * 449 * EV_REL events are sent when the trackball is moved 450 */ 451 if (config->hw_trackBall) { 452 events_set_bit (s, EV_SYN, EV_REL ); 453 events_set_bits(s, EV_REL, REL_X, REL_Y); 454 } 455 456 /* configure EV_ABS array. 457 * 458 * EV_ABS events are sent when the touchscreen is pressed 459 */ 460 if (!androidHwConfig_isScreenNoTouch(config)) { 461 ABSEntry* abs_values; 462 463 events_set_bit (s, EV_SYN, EV_ABS ); 464 events_set_bits(s, EV_ABS, ABS_X, ABS_Z); 465 /* Allocate the absinfo to report the min/max bounds for each 466 * absolute dimension. The array must contain 3, or ABS_MAX tuples 467 * of (min,max,fuzz,flat) 32-bit values. 468 * 469 * min and max are the bounds 470 * fuzz corresponds to the device's fuziness, we set it to 0 471 * flat corresponds to the flat position for JOEYDEV devices, 472 * we also set it to 0. 473 * 474 * There is no need to save/restore this array in a snapshot 475 * since the values only depend on the hardware configuration. 476 */ 477 s->abs_info_count = androidHwConfig_isScreenMultiTouch(config) ? ABS_MAX * 4 : 3 * 4; 478 const int abs_size = sizeof(uint32_t) * s->abs_info_count; 479 s->abs_info = malloc(abs_size); 480 memset(s->abs_info, 0, abs_size); 481 abs_values = (ABSEntry*)s->abs_info; 482 483 abs_values[ABS_X].max = config->hw_lcd_width-1; 484 abs_values[ABS_Y].max = config->hw_lcd_height-1; 485 abs_values[ABS_Z].max = 1; 486 487 if (androidHwConfig_isScreenMultiTouch(config)) { 488 /* 489 * Setup multitouch. 490 */ 491 events_set_bit(s, EV_ABS, ABS_MT_SLOT); 492 events_set_bit(s, EV_ABS, ABS_MT_POSITION_X); 493 events_set_bit(s, EV_ABS, ABS_MT_POSITION_Y); 494 events_set_bit(s, EV_ABS, ABS_MT_TRACKING_ID); 495 events_set_bit(s, EV_ABS, ABS_MT_TOUCH_MAJOR); 496 events_set_bit(s, EV_ABS, ABS_MT_PRESSURE); 497 498 abs_values[ABS_MT_SLOT].max = multitouch_get_max_slot(); 499 abs_values[ABS_MT_TRACKING_ID].max = abs_values[ABS_MT_SLOT].max + 1; 500 abs_values[ABS_MT_POSITION_X].max = abs_values[ABS_X].max; 501 abs_values[ABS_MT_POSITION_Y].max = abs_values[ABS_Y].max; 502 abs_values[ABS_MT_TOUCH_MAJOR].max = 0x7fffffff; // TODO: Make it less random 503 abs_values[ABS_MT_PRESSURE].max = 0x100; // TODO: Make it less random 504 } 505 } 506 507 /* configure EV_SW array 508 * 509 * EV_SW events are sent to indicate that the keyboard lid 510 * was closed or opened (done when we switch layouts through 511 * KP-7 or KP-9). 512 * 513 * We only support this when hw.keyboard.lid is true. 514 */ 515 if (config->hw_keyboard && config->hw_keyboard_lid) { 516 events_set_bit(s, EV_SYN, EV_SW); 517 events_set_bit(s, EV_SW, 0); 518 } 519 520 iomemtype = cpu_register_io_memory(events_readfn, events_writefn, s); 521 522 cpu_register_physical_memory(base, 0xfff, iomemtype); 523 524 qemu_add_kbd_event_handler(events_put_keycode, s); 525 qemu_add_mouse_event_handler(events_put_mouse, s, 1, "goldfish-events"); 526 527 s->base = base; 528 s->irq = irq; 529 530 s->first = 0; 531 s->last = 0; 532 s->state = STATE_INIT; 533 s->name = g_strdup(config->hw_keyboard_charmap); 534 535 /* This function migh fire buffered events to the device, so 536 * ensure that it is called after initialization is complete 537 */ 538 user_event_register_generic(s, events_put_generic); 539 540 register_savevm(NULL, 541 "events_state", 542 0, 543 EVENTS_STATE_SAVE_VERSION, 544 events_state_save, 545 events_state_load, 546 s); 547 } 548