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