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