1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
*/
#include "qemu_file.h"
#include "goldfish_device.h"
#include "power_supply.h"
enum {
/* status register */
BATTERY_INT_STATUS = 0x00,
/* set this to enable IRQ */
BATTERY_INT_ENABLE = 0x04,
BATTERY_AC_ONLINE = 0x08,
BATTERY_STATUS = 0x0C,
BATTERY_HEALTH = 0x10,
BATTERY_PRESENT = 0x14,
BATTERY_CAPACITY = 0x18,
BATTERY_STATUS_CHANGED = 1U << 0,
AC_STATUS_CHANGED = 1U << 1,
BATTERY_INT_MASK = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
};
struct goldfish_battery_state {
struct goldfish_device dev;
// IRQs
uint32_t int_status;
// irq enable mask for int_status
uint32_t int_enable;
int ac_online;
int status;
int health;
int present;
int capacity;
};
/* update this each time you update the battery_state struct */
#define BATTERY_STATE_SAVE_VERSION 1
#define QFIELD_STRUCT struct goldfish_battery_state
QFIELD_BEGIN(goldfish_battery_fields)
QFIELD_INT32(int_status),
QFIELD_INT32(int_enable),
QFIELD_INT32(ac_online),
QFIELD_INT32(status),
QFIELD_INT32(health),
QFIELD_INT32(present),
QFIELD_INT32(capacity),
QFIELD_END
static void goldfish_battery_save(QEMUFile* f, void* opaque)
{
struct goldfish_battery_state* s = opaque;
qemu_put_struct(f, goldfish_battery_fields, s);
}
static int goldfish_battery_load(QEMUFile* f, void* opaque, int version_id)
{
struct goldfish_battery_state* s = opaque;
if (version_id != BATTERY_STATE_SAVE_VERSION)
return -1;
return qemu_get_struct(f, goldfish_battery_fields, s);
}
static struct goldfish_battery_state *battery_state;
static uint32_t goldfish_battery_read(void *opaque, target_phys_addr_t offset)
{
uint32_t ret;
struct goldfish_battery_state *s = opaque;
switch(offset) {
case BATTERY_INT_STATUS:
// return current buffer status flags
ret = s->int_status & s->int_enable;
if (ret) {
goldfish_device_set_irq(&s->dev, 0, 0);
s->int_status = 0;
}
return ret;
case BATTERY_INT_ENABLE:
return s->int_enable;
case BATTERY_AC_ONLINE:
return s->ac_online;
case BATTERY_STATUS:
return s->status;
case BATTERY_HEALTH:
return s->health;
case BATTERY_PRESENT:
return s->present;
case BATTERY_CAPACITY:
return s->capacity;
default:
cpu_abort (cpu_single_env, "goldfish_battery_read: Bad offset %x\n", offset);
return 0;
}
}
static void goldfish_battery_write(void *opaque, target_phys_addr_t offset, uint32_t val)
{
struct goldfish_battery_state *s = opaque;
switch(offset) {
case BATTERY_INT_ENABLE:
/* enable interrupts */
s->int_enable = val;
// s->int_status = (AUDIO_INT_WRITE_BUFFER_1_EMPTY | AUDIO_INT_WRITE_BUFFER_2_EMPTY);
// goldfish_device_set_irq(&s->dev, 0, (s->int_status & s->int_enable));
break;
default:
cpu_abort (cpu_single_env, "goldfish_audio_write: Bad offset %x\n", offset);
}
}
static CPUReadMemoryFunc *goldfish_battery_readfn[] = {
goldfish_battery_read,
goldfish_battery_read,
goldfish_battery_read
};
static CPUWriteMemoryFunc *goldfish_battery_writefn[] = {
goldfish_battery_write,
goldfish_battery_write,
goldfish_battery_write
};
void goldfish_battery_init()
{
struct goldfish_battery_state *s;
s = (struct goldfish_battery_state *)qemu_mallocz(sizeof(*s));
s->dev.name = "goldfish-battery";
s->dev.base = 0; // will be allocated dynamically
s->dev.size = 0x1000;
s->dev.irq_count = 1;
// default values for the battery
s->ac_online = 1;
s->status = POWER_SUPPLY_STATUS_CHARGING;
s->health = POWER_SUPPLY_HEALTH_GOOD;
s->present = 1; // battery is present
s->capacity = 50; // 50% charged
battery_state = s;
goldfish_device_add(&s->dev, goldfish_battery_readfn, goldfish_battery_writefn, s);
register_savevm( "battery_state", 0, BATTERY_STATE_SAVE_VERSION,
goldfish_battery_save, goldfish_battery_load, s);
}
void goldfish_battery_set_prop(int ac, int property, int value)
{
int new_status = (ac ? AC_STATUS_CHANGED : BATTERY_STATUS_CHANGED);
if (ac) {
switch (property) {
case POWER_SUPPLY_PROP_ONLINE:
battery_state->ac_online = value;
break;
}
} else {
switch (property) {
case POWER_SUPPLY_PROP_STATUS:
battery_state->status = value;
break;
case POWER_SUPPLY_PROP_HEALTH:
battery_state->health = value;
break;
case POWER_SUPPLY_PROP_PRESENT:
battery_state->present = value;
break;
case POWER_SUPPLY_PROP_CAPACITY:
battery_state->capacity = value;
break;
}
}
if (new_status != battery_state->int_status) {
battery_state->int_status |= new_status;
goldfish_device_set_irq(&battery_state->dev, 0, (battery_state->int_status & battery_state->int_enable));
}
}
void goldfish_battery_display(void (* callback)(void *data, const char* string), void *data)
{
char buffer[100];
const char* value;
sprintf(buffer, "AC: %s\r\n", (battery_state->ac_online ? "online" : "offline"));
callback(data, buffer);
switch (battery_state->status) {
case POWER_SUPPLY_STATUS_CHARGING:
value = "Charging";
break;
case POWER_SUPPLY_STATUS_DISCHARGING:
value = "Discharging";
break;
case POWER_SUPPLY_STATUS_NOT_CHARGING:
value = "Not charging";
break;
case POWER_SUPPLY_STATUS_FULL:
value = "Full";
break;
default:
value = "Unknown";
break;
}
sprintf(buffer, "status: %s\r\n", value);
callback(data, buffer);
switch (battery_state->health) {
case POWER_SUPPLY_HEALTH_GOOD:
value = "Good";
break;
case POWER_SUPPLY_HEALTH_OVERHEAT:
value = "Overhead";
break;
case POWER_SUPPLY_HEALTH_DEAD:
value = "Dead";
break;
case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
value = "Overvoltage";
break;
case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
value = "Unspecified failure";
break;
default:
value = "Unknown";
break;
}
sprintf(buffer, "health: %s\r\n", value);
callback(data, buffer);
sprintf(buffer, "present: %s\r\n", (battery_state->present ? "true" : "false"));
callback(data, buffer);
sprintf(buffer, "capacity: %d\r\n", battery_state->capacity);
callback(data, buffer);
}
|