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 "qemu-char.h"
     14 #include "goldfish_device.h"
     15 #include "goldfish_vmem.h"
     16 
     17 enum {
     18     TTY_PUT_CHAR       = 0x00,
     19     TTY_BYTES_READY    = 0x04,
     20     TTY_CMD            = 0x08,
     21 
     22     TTY_DATA_PTR       = 0x10,
     23     TTY_DATA_LEN       = 0x14,
     24 
     25     TTY_CMD_INT_DISABLE    = 0,
     26     TTY_CMD_INT_ENABLE     = 1,
     27     TTY_CMD_WRITE_BUFFER   = 2,
     28     TTY_CMD_READ_BUFFER    = 3,
     29 };
     30 
     31 struct tty_state {
     32     struct goldfish_device dev;
     33     CharDriverState *cs;
     34     uint32_t ptr;
     35     uint32_t ptr_len;
     36     uint32_t ready;
     37     uint8_t data[128];
     38     uint32_t data_count;
     39 };
     40 
     41 #define  GOLDFISH_TTY_SAVE_VERSION  1
     42 
     43 static void  goldfish_tty_save(QEMUFile*  f, void*  opaque)
     44 {
     45     struct tty_state*  s = opaque;
     46 
     47     qemu_put_be32( f, s->ptr );
     48     qemu_put_be32( f, s->ptr_len );
     49     qemu_put_byte( f, s->ready );
     50     qemu_put_byte( f, s->data_count );
     51     qemu_put_buffer( f, s->data, s->data_count );
     52 }
     53 
     54 static int  goldfish_tty_load(QEMUFile*  f, void*  opaque, int  version_id)
     55 {
     56     struct tty_state*  s = opaque;
     57 
     58     if (version_id != GOLDFISH_TTY_SAVE_VERSION)
     59         return -1;
     60 
     61     s->ptr        = qemu_get_be32(f);
     62     s->ptr_len    = qemu_get_be32(f);
     63     s->ready      = qemu_get_byte(f);
     64     s->data_count = qemu_get_byte(f);
     65     qemu_get_buffer(f, s->data, s->data_count);
     66 
     67     return 0;
     68 }
     69 
     70 static uint32_t goldfish_tty_read(void *opaque, target_phys_addr_t offset)
     71 {
     72     struct tty_state *s = (struct tty_state *)opaque;
     73 
     74     //printf("goldfish_tty_read %x %x\n", offset, size);
     75 
     76     switch (offset) {
     77         case TTY_BYTES_READY:
     78             return s->data_count;
     79     default:
     80         cpu_abort (cpu_single_env, "goldfish_tty_read: Bad offset %x\n", offset);
     81         return 0;
     82     }
     83 }
     84 
     85 static void goldfish_tty_write(void *opaque, target_phys_addr_t offset, uint32_t value)
     86 {
     87     struct tty_state *s = (struct tty_state *)opaque;
     88 
     89     //printf("goldfish_tty_read %x %x %x\n", offset, value, size);
     90 
     91     switch(offset) {
     92         case TTY_PUT_CHAR: {
     93             uint8_t ch = value;
     94             if(s->cs)
     95                 qemu_chr_write(s->cs, &ch, 1);
     96         } break;
     97 
     98         case TTY_CMD:
     99             switch(value) {
    100                 case TTY_CMD_INT_DISABLE:
    101                     if(s->ready) {
    102                         if(s->data_count > 0)
    103                             goldfish_device_set_irq(&s->dev, 0, 0);
    104                         s->ready = 0;
    105                     }
    106                     break;
    107 
    108                 case TTY_CMD_INT_ENABLE:
    109                     if(!s->ready) {
    110                         if(s->data_count > 0)
    111                             goldfish_device_set_irq(&s->dev, 0, 1);
    112                         s->ready = 1;
    113                     }
    114                     break;
    115 
    116                 case TTY_CMD_WRITE_BUFFER:
    117                     if(s->cs) {
    118                         int len;
    119                         target_phys_addr_t  buf;
    120 
    121                         buf = s->ptr;
    122                         len = s->ptr_len;
    123 
    124                         while (len) {
    125                             char   temp[64];
    126                             int    to_write = sizeof(temp);
    127                             if (to_write > len)
    128                                 to_write = len;
    129 
    130                             safe_memory_rw_debug(cpu_single_env, buf, (uint8_t*)temp, to_write, 0);
    131                             qemu_chr_write(s->cs, (const uint8_t*)temp, to_write);
    132                             buf += to_write;
    133                             len -= to_write;
    134                         }
    135                         //printf("goldfish_tty_write: got %d bytes from %x\n", s->ptr_len, s->ptr);
    136                     }
    137                     break;
    138 
    139                 case TTY_CMD_READ_BUFFER:
    140                     if(s->ptr_len > s->data_count)
    141                         cpu_abort (cpu_single_env, "goldfish_tty_write: reading more data than available %d %d\n", s->ptr_len, s->data_count);
    142                     safe_memory_rw_debug(cpu_single_env,s->ptr, s->data, s->ptr_len,1);
    143                     //printf("goldfish_tty_write: read %d bytes to %x\n", s->ptr_len, s->ptr);
    144                     if(s->data_count > s->ptr_len)
    145                         memmove(s->data, s->data + s->ptr_len, s->data_count - s->ptr_len);
    146                     s->data_count -= s->ptr_len;
    147                     if(s->data_count == 0 && s->ready)
    148                         goldfish_device_set_irq(&s->dev, 0, 0);
    149                     break;
    150 
    151                 default:
    152                     cpu_abort (cpu_single_env, "goldfish_tty_write: Bad command %x\n", value);
    153             };
    154             break;
    155 
    156         case TTY_DATA_PTR:
    157             s->ptr = value;
    158             break;
    159 
    160         case TTY_DATA_LEN:
    161             s->ptr_len = value;
    162             break;
    163 
    164         default:
    165             cpu_abort (cpu_single_env, "goldfish_tty_write: Bad offset %x\n", offset);
    166     }
    167 }
    168 
    169 static int tty_can_receive(void *opaque)
    170 {
    171     struct tty_state *s = opaque;
    172 
    173     return (sizeof(s->data) - s->data_count);
    174 }
    175 
    176 static void tty_receive(void *opaque, const uint8_t *buf, int size)
    177 {
    178     struct tty_state *s = opaque;
    179 
    180     memcpy(s->data + s->data_count, buf, size);
    181     s->data_count += size;
    182     if(s->data_count > 0 && s->ready)
    183         goldfish_device_set_irq(&s->dev, 0, 1);
    184 }
    185 
    186 static CPUReadMemoryFunc *goldfish_tty_readfn[] = {
    187     goldfish_tty_read,
    188     goldfish_tty_read,
    189     goldfish_tty_read
    190 };
    191 
    192 static CPUWriteMemoryFunc *goldfish_tty_writefn[] = {
    193     goldfish_tty_write,
    194     goldfish_tty_write,
    195     goldfish_tty_write
    196 };
    197 
    198 int goldfish_tty_add(CharDriverState *cs, int id, uint32_t base, int irq)
    199 {
    200     int ret;
    201     struct tty_state *s;
    202     static int  instance_id = 0;
    203 
    204     s = qemu_mallocz(sizeof(*s));
    205     s->dev.name = "goldfish_tty";
    206     s->dev.id = id;
    207     s->dev.base = base;
    208     s->dev.size = 0x1000;
    209     s->dev.irq = irq;
    210     s->dev.irq_count = 1;
    211     s->cs = cs;
    212 
    213     if(cs) {
    214         qemu_chr_add_handlers(cs, tty_can_receive, tty_receive, NULL, s);
    215     }
    216 
    217     ret = goldfish_device_add(&s->dev, goldfish_tty_readfn, goldfish_tty_writefn, s);
    218     if(ret) {
    219         qemu_free(s);
    220     } else {
    221         register_savevm( "goldfish_tty", instance_id++, GOLDFISH_TTY_SAVE_VERSION,
    222                          goldfish_tty_save, goldfish_tty_load, s);
    223     }
    224     return ret;
    225 }
    226 
    227