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 "goldfish_device.h"
     14 #include "audio/audio.h"
     15 #include "qemu_debug.h"
     16 #include "android/globals.h"
     17 
     18 #define  DEBUG  1
     19 
     20 #if DEBUG
     21 #  define  D(...)  VERBOSE_PRINT(audio,__VA_ARGS__)
     22 #else
     23 #  define  D(...)  ((void)0)
     24 #endif
     25 
     26 extern void  dprint(const char*  fmt, ...);
     27 
     28 /* define USE_QEMU_AUDIO_IN to 1 to use QEMU's audio subsystem to
     29  * implement the audio input. if 0, this will try to read a .wav file
     30  * directly...
     31  */
     32 #define  USE_QEMU_AUDIO_IN  1
     33 
     34 enum {
     35 	/* audio status register */
     36 	AUDIO_INT_STATUS	= 0x00,
     37 	/* set this to enable IRQ */
     38 	AUDIO_INT_ENABLE	= 0x04,
     39 	/* set these to specify buffer addresses */
     40 	AUDIO_SET_WRITE_BUFFER_1 = 0x08,
     41 	AUDIO_SET_WRITE_BUFFER_2 = 0x0C,
     42 	/* set number of bytes in buffer to write */
     43 	AUDIO_WRITE_BUFFER_1  = 0x10,
     44 	AUDIO_WRITE_BUFFER_2  = 0x14,
     45 
     46 	/* true if audio input is supported */
     47 	AUDIO_READ_SUPPORTED = 0x18,
     48 	/* buffer to use for audio input */
     49 	AUDIO_SET_READ_BUFFER = 0x1C,
     50 
     51 	/* driver writes number of bytes to read */
     52 	AUDIO_START_READ  = 0x20,
     53 
     54 	/* number of bytes available in read buffer */
     55 	AUDIO_READ_BUFFER_AVAILABLE  = 0x24,
     56 
     57 	/* AUDIO_INT_STATUS bits */
     58 
     59 	/* this bit set when it is safe to write more bytes to the buffer */
     60 	AUDIO_INT_WRITE_BUFFER_1_EMPTY	= 1U << 0,
     61 	AUDIO_INT_WRITE_BUFFER_2_EMPTY	= 1U << 1,
     62 	AUDIO_INT_READ_BUFFER_FULL      = 1U << 2,
     63 };
     64 
     65 struct goldfish_audio_buff {
     66     uint32_t  address;
     67     uint32_t  length;
     68     uint8*    data;
     69     uint32_t  capacity;
     70     uint32_t  offset;
     71 };
     72 
     73 
     74 struct goldfish_audio_state {
     75     struct goldfish_device dev;
     76     // buffer flags
     77     uint32_t int_status;
     78     // irq enable mask for int_status
     79     uint32_t int_enable;
     80 
     81 #ifndef USE_QEMU_AUDIO_IN
     82     // address of the read buffer
     83     uint32_t read_buffer;
     84     // path to file or device to use for input
     85     const char* input_source;
     86     // true if input is a wav file
     87     int input_is_wav;
     88     // true if we need to convert stereo -> mono
     89     int input_is_stereo;
     90     // file descriptor to use for input
     91     int input_fd;
     92 #endif
     93 
     94     // number of bytes available in the read buffer
     95     int read_buffer_available;
     96 
     97     // set to 1 or 2 to indicate which buffer we are writing from, or zero if both buffers are empty
     98     int current_buffer;
     99 
    100     // current data to write
    101     struct goldfish_audio_buff  out_buff1[1];
    102     struct goldfish_audio_buff  out_buff2[1];
    103     struct goldfish_audio_buff  in_buff[1];
    104 
    105     // for QEMU sound output
    106     QEMUSoundCard card;
    107     SWVoiceOut *voice;
    108 #if USE_QEMU_AUDIO_IN
    109     SWVoiceIn*  voicein;
    110 #endif
    111 };
    112 
    113 static void
    114 goldfish_audio_buff_init( struct goldfish_audio_buff*  b )
    115 {
    116     b->address  = 0;
    117     b->length   = 0;
    118     b->data     = NULL;
    119     b->capacity = 0;
    120     b->offset   = 0;
    121 }
    122 
    123 static void
    124 goldfish_audio_buff_reset( struct goldfish_audio_buff*  b )
    125 {
    126     b->offset = 0;
    127     b->length = 0;
    128 }
    129 
    130 static uint32_t
    131 goldfish_audio_buff_length( struct goldfish_audio_buff*  b )
    132 {
    133     return b->length;
    134 }
    135 
    136 static void
    137 goldfish_audio_buff_ensure( struct goldfish_audio_buff*  b, uint32_t  size )
    138 {
    139     if (b->capacity < size) {
    140         b->data     = qemu_realloc(b->data, size);
    141         b->capacity = size;
    142     }
    143 }
    144 
    145 static void
    146 goldfish_audio_buff_set_address( struct goldfish_audio_buff*  b, uint32_t  addr )
    147 {
    148     b->address = addr;
    149 }
    150 
    151 static void
    152 goldfish_audio_buff_set_length( struct goldfish_audio_buff*  b, uint32_t  len )
    153 {
    154     b->length = len;
    155     b->offset = 0;
    156     goldfish_audio_buff_ensure(b, len);
    157 }
    158 
    159 static void
    160 goldfish_audio_buff_read( struct goldfish_audio_buff*  b )
    161 {
    162     cpu_physical_memory_read(b->address, b->data, b->length);
    163 }
    164 
    165 static void
    166 goldfish_audio_buff_write( struct goldfish_audio_buff*  b )
    167 {
    168     cpu_physical_memory_write(b->address, b->data, b->length);
    169 }
    170 
    171 static int
    172 goldfish_audio_buff_send( struct goldfish_audio_buff*  b, int  free, struct goldfish_audio_state*  s )
    173 {
    174     int  ret, write = b->length;
    175 
    176     if (write > free)
    177         write = free;
    178 
    179     ret = AUD_write(s->voice, b->data + b->offset, write);
    180     b->offset += ret;
    181     b->length -= ret;
    182     return ret;
    183 }
    184 
    185 static int
    186 goldfish_audio_buff_available( struct goldfish_audio_buff*  b )
    187 {
    188     return b->length - b->offset;
    189 }
    190 
    191 static int
    192 goldfish_audio_buff_recv( struct goldfish_audio_buff*  b, int  avail, struct goldfish_audio_state*  s )
    193 {
    194     int     missing = b->length - b->offset;
    195     int     avail2 = (avail > missing) ? missing : avail;
    196     int     read;
    197 
    198     read = AUD_read(s->voicein, b->data + b->offset, avail2 );
    199     if (read == 0)
    200         return 0;
    201 
    202     if (avail2 > 0)
    203         D("%s: AUD_read(%d) returned %d", __FUNCTION__, avail2, read);
    204 
    205     cpu_physical_memory_write( b->address + b->offset, b->data, read );
    206     b->offset += read;
    207 
    208     return read;
    209 }
    210 
    211 static void
    212 goldfish_audio_buff_put( struct goldfish_audio_buff*  b, QEMUFile*  f )
    213 {
    214     qemu_put_be32(f, b->address );
    215     qemu_put_be32(f, b->length );
    216     qemu_put_be32(f, b->offset );
    217     qemu_put_buffer(f, b->data, b->length );
    218 }
    219 
    220 static void
    221 goldfish_audio_buff_get( struct goldfish_audio_buff*  b, QEMUFile*  f )
    222 {
    223     b->address = qemu_get_be32(f);
    224     b->length  = qemu_get_be32(f);
    225     b->offset  = qemu_get_be32(f);
    226     goldfish_audio_buff_ensure(b, b->length);
    227     qemu_get_buffer(f, b->data, b->length);
    228 }
    229 
    230 /* update this whenever you change the goldfish_audio_state structure */
    231 #define  AUDIO_STATE_SAVE_VERSION  2
    232 
    233 #define  QFIELD_STRUCT   struct goldfish_audio_state
    234 QFIELD_BEGIN(audio_state_fields)
    235     QFIELD_INT32(int_status),
    236     QFIELD_INT32(int_enable),
    237     QFIELD_INT32(read_buffer_available),
    238     QFIELD_INT32(current_buffer),
    239 QFIELD_END
    240 
    241 static void  audio_state_save( QEMUFile*  f, void* opaque )
    242 {
    243     struct goldfish_audio_state*  s = opaque;
    244 
    245     qemu_put_struct(f, audio_state_fields, s);
    246 
    247     goldfish_audio_buff_put (s->out_buff1, f);
    248     goldfish_audio_buff_put (s->out_buff2, f);
    249     goldfish_audio_buff_put (s->in_buff, f);
    250 }
    251 
    252 static int   audio_state_load( QEMUFile*  f, void*  opaque, int  version_id )
    253 {
    254     struct goldfish_audio_state*  s = opaque;
    255     int                           ret;
    256 
    257     if (version_id != AUDIO_STATE_SAVE_VERSION)
    258         return -1;
    259 
    260     ret = qemu_get_struct(f, audio_state_fields, s);
    261     if (!ret) {
    262         goldfish_audio_buff_get( s->out_buff1, f );
    263         goldfish_audio_buff_get( s->out_buff2, f );
    264         goldfish_audio_buff_get (s->in_buff, f);
    265     }
    266     return ret;
    267 }
    268 
    269 static void enable_audio(struct goldfish_audio_state *s, int enable)
    270 {
    271     // enable or disable the output voice
    272     if (s->voice != NULL) {
    273         AUD_set_active_out(s->voice,   (enable & (AUDIO_INT_WRITE_BUFFER_1_EMPTY | AUDIO_INT_WRITE_BUFFER_2_EMPTY)) != 0);
    274         goldfish_audio_buff_reset( s->out_buff1 );
    275         goldfish_audio_buff_reset( s->out_buff2 );
    276     }
    277 
    278     if (s->voicein) {
    279         AUD_set_active_in (s->voicein, (enable & AUDIO_INT_READ_BUFFER_FULL) != 0);
    280         goldfish_audio_buff_reset( s->in_buff );
    281     }
    282     s->current_buffer = 0;
    283 }
    284 
    285 #if USE_QEMU_AUDIO_IN
    286 static void start_read(struct goldfish_audio_state *s, uint32_t count)
    287 {
    288     //printf( "... goldfish audio start_read, count=%d\n", count );
    289     goldfish_audio_buff_set_length( s->in_buff, count );
    290     s->read_buffer_available = count;
    291 }
    292 #else
    293 static void start_read(struct goldfish_audio_state *s, uint32_t count)
    294 {
    295     uint8   wav_header[44];
    296     int result;
    297 
    298     if (!s->input_source) return;
    299 
    300     if (s->input_fd < 0) {
    301         s->input_fd = open(s->input_source, O_BINARY | O_RDONLY);
    302 
    303         if (s->input_fd < 0) {
    304             fprintf(stderr, "goldfish_audio could not open %s for audio input\n", s->input_source);
    305             s->input_source = NULL; // set to to avoid endless retries
    306             return;
    307         }
    308 
    309         // skip WAV header if we have a WAV file
    310         if (s->input_is_wav) {
    311             if (read(s->input_fd, wav_header, sizeof(wav_header)) != sizeof(wav_header)) {
    312                 fprintf(stderr, "goldfish_audio could not read WAV file header %s\n", s->input_source);
    313                 s->input_fd = -1;
    314                 s->input_source = NULL; // set to to avoid endless retries
    315                 return;
    316             }
    317 
    318             // is the WAV file stereo?
    319             s->input_is_stereo = (wav_header[22] == 2);
    320         } else {
    321             // assume input from an audio device is stereo
    322             s->input_is_stereo = 1;
    323         }
    324     }
    325 
    326     uint8* buffer = (uint8*)phys_ram_base + s->read_buffer;
    327     if (s->input_is_stereo) {
    328         // need to read twice as much data
    329         count *= 2;
    330     }
    331 
    332 try_again:
    333     result = read(s->input_fd, buffer, count);
    334     if (result == 0 && s->input_is_wav) {
    335         // end of file, so seek back to the beginning
    336        lseek(s->input_fd, sizeof(wav_header), SEEK_SET);
    337        goto try_again;
    338     }
    339 
    340     if (result > 0 && s->input_is_stereo) {
    341         // we need to convert stereo to mono
    342         uint8* src  = (uint8*)buffer;
    343         uint8* dest = src;
    344         int count = result/2;
    345         while (count-- > 0) {
    346             int  sample1 = src[0] | (src[1] << 8);
    347             int  sample2 = src[2] | (src[3] << 8);
    348             int  sample  = (sample1 + sample2) >> 1;
    349             dst[0] = (uint8_t) sample;
    350             dst[1] = (uint8_t)(sample >> 8);
    351             src   += 4;
    352             dst   += 2;
    353         }
    354 
    355         // we reduced the number of bytes by 2
    356         result /= 2;
    357     }
    358 
    359     s->read_buffer_available = (result > 0 ? result : 0);
    360     s->int_status |= AUDIO_INT_READ_BUFFER_FULL;
    361     goldfish_device_set_irq(&s->dev, 0, (s->int_status & s->int_enable));
    362 }
    363 #endif
    364 
    365 static uint32_t goldfish_audio_read(void *opaque, target_phys_addr_t offset)
    366 {
    367     uint32_t ret;
    368     struct goldfish_audio_state *s = opaque;
    369     switch(offset) {
    370         case AUDIO_INT_STATUS:
    371             // return current buffer status flags
    372             ret = s->int_status & s->int_enable;
    373             if(ret) {
    374                 goldfish_device_set_irq(&s->dev, 0, 0);
    375             }
    376             return ret;
    377 
    378 	case AUDIO_READ_SUPPORTED:
    379 #if USE_QEMU_AUDIO_IN
    380             D("%s: AUDIO_READ_SUPPORTED returns %d", __FUNCTION__,
    381               (s->voicein != NULL));
    382             return (s->voicein != NULL);
    383 #else
    384             return (s->input_source ? 1 : 0);
    385 #endif
    386 
    387 	case AUDIO_READ_BUFFER_AVAILABLE:
    388             D("%s: AUDIO_READ_BUFFER_AVAILABLE returns %d", __FUNCTION__,
    389                s->read_buffer_available);
    390             goldfish_audio_buff_write( s->in_buff );
    391 	    return s->read_buffer_available;
    392 
    393         default:
    394             cpu_abort (cpu_single_env, "goldfish_audio_read: Bad offset %x\n", offset);
    395             return 0;
    396     }
    397 }
    398 
    399 static void goldfish_audio_write(void *opaque, target_phys_addr_t offset, uint32_t val)
    400 {
    401     struct goldfish_audio_state *s = opaque;
    402 
    403     switch(offset) {
    404         case AUDIO_INT_ENABLE:
    405             /* enable buffer empty interrupts */
    406             D("%s: AUDIO_INT_ENABLE %d", __FUNCTION__, val );
    407             enable_audio(s, val);
    408             s->int_enable = val;
    409             s->int_status = (AUDIO_INT_WRITE_BUFFER_1_EMPTY | AUDIO_INT_WRITE_BUFFER_2_EMPTY);
    410             goldfish_device_set_irq(&s->dev, 0, (s->int_status & s->int_enable));
    411             break;
    412         case AUDIO_SET_WRITE_BUFFER_1:
    413             /* save pointer to buffer 1 */
    414             D( "%s: AUDIO_SET_WRITE_BUFFER_1 %08x", __FUNCTION__, val);
    415             goldfish_audio_buff_set_address( s->out_buff1, val );
    416             break;
    417         case AUDIO_SET_WRITE_BUFFER_2:
    418             /* save pointer to buffer 2 */
    419             D( "%s: AUDIO_SET_WRITE_BUFFER_2 %08x", __FUNCTION__, val);
    420             goldfish_audio_buff_set_address( s->out_buff2, val );
    421             break;
    422         case AUDIO_WRITE_BUFFER_1:
    423             /* record that data in buffer 1 is ready to write */
    424             //D( "%s: AUDIO_WRITE_BUFFER_1 %08x", __FUNCTION__, val);
    425             if (s->current_buffer == 0) s->current_buffer = 1;
    426             goldfish_audio_buff_set_length( s->out_buff1, val );
    427             goldfish_audio_buff_read( s->out_buff1 );
    428             s->int_status &= ~AUDIO_INT_WRITE_BUFFER_1_EMPTY;
    429             break;
    430         case AUDIO_WRITE_BUFFER_2:
    431             /* record that data in buffer 2 is ready to write */
    432             //D( "%s: AUDIO_WRITE_BUFFER_2 %08x", __FUNCTION__, val);
    433             if (s->current_buffer == 0) s->current_buffer = 2;
    434             goldfish_audio_buff_set_length( s->out_buff2, val );
    435             goldfish_audio_buff_read( s->out_buff2 );
    436             s->int_status &= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY;
    437             break;
    438 
    439         case AUDIO_SET_READ_BUFFER:
    440             /* save pointer to the read buffer */
    441             goldfish_audio_buff_set_address( s->in_buff, val );
    442             D( "%s: AUDIO_SET_READ_BUFFER %08x", __FUNCTION__, val );
    443             break;
    444 
    445         case AUDIO_START_READ:
    446             D( "%s: AUDIO_START_READ %d", __FUNCTION__, val );
    447             start_read(s, val);
    448             s->int_status &= ~AUDIO_INT_READ_BUFFER_FULL;
    449             goldfish_device_set_irq(&s->dev, 0, (s->int_status & s->int_enable));
    450             break;
    451 
    452         default:
    453             cpu_abort (cpu_single_env, "goldfish_audio_write: Bad offset %x\n", offset);
    454     }
    455 }
    456 
    457 static void goldfish_audio_callback(void *opaque, int free)
    458 {
    459     struct goldfish_audio_state *s = opaque;
    460     int new_status = 0;
    461 
    462     /* loop until free is zero or both buffers are empty */
    463     while (free && s->current_buffer) {
    464 
    465         /* write data in buffer 1 */
    466         while (free && s->current_buffer == 1) {
    467             int  written = goldfish_audio_buff_send( s->out_buff1, free, s );
    468             if (written) {
    469                 D("%s: sent %5d bytes to audio output (buffer 1)", __FUNCTION__, written);
    470                 free -= written;
    471 
    472                 if (goldfish_audio_buff_length( s->out_buff1 ) == 0) {
    473                     new_status |= AUDIO_INT_WRITE_BUFFER_1_EMPTY;
    474                     s->current_buffer = (goldfish_audio_buff_length( s->out_buff2 ) ? 2 : 0);
    475                 }
    476             } else {
    477                 break;
    478             }
    479         }
    480 
    481         /* write data in buffer 2 */
    482         while (free && s->current_buffer == 2) {
    483             int  written = goldfish_audio_buff_send( s->out_buff2, free, s );
    484             if (written) {
    485                 D("%s: sent %5d bytes to audio output (buffer 2)", __FUNCTION__, written);
    486                 free -= written;
    487 
    488                 if (goldfish_audio_buff_length( s->out_buff2 ) == 0) {
    489                     new_status |= AUDIO_INT_WRITE_BUFFER_2_EMPTY;
    490                     s->current_buffer = (goldfish_audio_buff_length( s->out_buff1 ) ? 1 : 0);
    491                 }
    492             } else {
    493                 break;
    494             }
    495         }
    496     }
    497 
    498     if (new_status && new_status != s->int_status) {
    499         s->int_status |= new_status;
    500         goldfish_device_set_irq(&s->dev, 0, (s->int_status & s->int_enable));
    501     }
    502 }
    503 
    504 #if USE_QEMU_AUDIO_IN
    505 static void
    506 goldfish_audio_in_callback(void *opaque, int avail)
    507 {
    508     struct goldfish_audio_state *s = opaque;
    509     int new_status = 0;
    510 
    511     if (goldfish_audio_buff_available( s->in_buff ) == 0 )
    512         return;
    513 
    514     while (avail > 0) {
    515         int  read = goldfish_audio_buff_recv( s->in_buff, avail, s );
    516         if (read == 0)
    517             break;
    518 
    519         avail -= read;
    520 
    521         if (goldfish_audio_buff_available( s->in_buff) == 0) {
    522             new_status |= AUDIO_INT_READ_BUFFER_FULL;
    523             D("%s: AUDIO_INT_READ_BUFFER_FULL available=%d",
    524               __FUNCTION__, goldfish_audio_buff_length( s->in_buff ));
    525             break;
    526         }
    527     }
    528 
    529     if (new_status && new_status != s->int_status) {
    530         s->int_status |= new_status;
    531         goldfish_device_set_irq(&s->dev, 0, (s->int_status & s->int_enable));
    532     }
    533 }
    534 #endif /* USE_QEMU_AUDIO_IN */
    535 
    536 static CPUReadMemoryFunc *goldfish_audio_readfn[] = {
    537    goldfish_audio_read,
    538    goldfish_audio_read,
    539    goldfish_audio_read
    540 };
    541 
    542 static CPUWriteMemoryFunc *goldfish_audio_writefn[] = {
    543    goldfish_audio_write,
    544    goldfish_audio_write,
    545    goldfish_audio_write
    546 };
    547 
    548 void goldfish_audio_init(uint32_t base, int id, const char* input_source)
    549 {
    550     struct goldfish_audio_state *s;
    551     struct audsettings as;
    552 
    553     /* nothing to do if no audio input and output */
    554     if (!android_hw->hw_audioOutput && !android_hw->hw_audioInput)
    555         return;
    556 
    557     s = (struct goldfish_audio_state *)qemu_mallocz(sizeof(*s));
    558     s->dev.name = "goldfish_audio";
    559     s->dev.id = id;
    560     s->dev.base = base;
    561     s->dev.size = 0x1000;
    562     s->dev.irq_count = 1;
    563 
    564 #ifndef USE_QEMU_AUDIO_IN
    565     s->input_fd = -1;
    566     if (input_source) {
    567         s->input_source = input_source;
    568         char* extension = strrchr(input_source, '.');
    569         if (extension && strcasecmp(extension, ".wav") == 0) {
    570             s->input_is_wav = 1;
    571         }
    572      }
    573 #endif
    574 
    575     AUD_register_card( "goldfish_audio", &s->card);
    576 
    577     as.freq = 44100;
    578     as.nchannels = 2;
    579     as.fmt = AUD_FMT_S16;
    580     as.endianness = AUDIO_HOST_ENDIANNESS;
    581 
    582     if (android_hw->hw_audioOutput) {
    583         s->voice = AUD_open_out (
    584             &s->card,
    585             NULL,
    586             "goldfish_audio",
    587             s,
    588             goldfish_audio_callback,
    589             &as
    590             );
    591         if (!s->voice) {
    592             dprint("warning: opening audio output failed\n");
    593             return;
    594         }
    595     }
    596 
    597 #if USE_QEMU_AUDIO_IN
    598     as.freq       = 8000;
    599     as.nchannels  = 1;
    600     as.fmt        = AUD_FMT_S16;
    601     as.endianness = AUDIO_HOST_ENDIANNESS;
    602 
    603     if (android_hw->hw_audioInput) {
    604         s->voicein = AUD_open_in (
    605             &s->card,
    606             NULL,
    607             "goldfish_audio_in",
    608             s,
    609             goldfish_audio_in_callback,
    610             &as
    611             );
    612         if (!s->voicein) {
    613             dprint("warning: opening audio input failed\n");
    614         }
    615     }
    616 #endif
    617 
    618     goldfish_audio_buff_init( s->out_buff1 );
    619     goldfish_audio_buff_init( s->out_buff2 );
    620     goldfish_audio_buff_init( s->in_buff );
    621 
    622     goldfish_device_add(&s->dev, goldfish_audio_readfn, goldfish_audio_writefn, s);
    623 
    624     register_savevm( "audio_state", 0, AUDIO_STATE_SAVE_VERSION,
    625                      audio_state_save, audio_state_load, s );
    626 }
    627 
    628