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