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