4
* Copyright (c) 2004 Fabrice Bellard
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to deal
8
* in the Software without restriction, including without limitation the rights
9
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
#include "qemu/osdep.h"
26
#include "hw/input/adb.h"
27
#include "hw/qdev-properties.h"
28
#include "migration/vmstate.h"
29
#include "qemu/module.h"
30
#include "qemu/timer.h"
31
#include "adb-internal.h"
35
#define ADB_RET_NOTPRESENT (-2)
37
static const char *adb_commands[] = {
38
"RESET", "FLUSH", "(Reserved 0x2)", "(Reserved 0x3)",
39
"Reserved (0x4)", "(Reserved 0x5)", "(Reserved 0x6)", "(Reserved 0x7)",
40
"LISTEN r0", "LISTEN r1", "LISTEN r2", "LISTEN r3",
41
"TALK r0", "TALK r1", "TALK r2", "TALK r3",
44
static void adb_device_reset(ADBDevice *d)
46
device_cold_reset(DEVICE(d));
49
static int do_adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf,
54
int devaddr, cmd, olen, i;
57
if (cmd == ADB_BUSRESET) {
58
for (i = 0; i < s->nb_devices; i++) {
67
for (i = 0; i < s->nb_devices; i++) {
69
adc = ADB_DEVICE_GET_CLASS(d);
71
if (adc->devhasdata(d)) {
72
s->pending |= (1 << d->devaddr);
77
devaddr = buf[0] >> 4;
78
for (i = 0; i < s->nb_devices; i++) {
80
adc = ADB_DEVICE_GET_CLASS(d);
82
if (d->devaddr == devaddr) {
83
olen = adc->devreq(d, obuf, buf, len);
85
s->status |= ADB_STATUS_BUSTIMEOUT;
91
s->status |= ADB_STATUS_BUSTIMEOUT;
92
return ADB_RET_NOTPRESENT;
95
int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
99
trace_adb_bus_request(buf[0] >> 4, adb_commands[buf[0] & 0xf], len);
101
assert(s->autopoll_blocked);
103
ret = do_adb_request(s, obuf, buf, len);
105
trace_adb_bus_request_done(buf[0] >> 4, adb_commands[buf[0] & 0xf], ret);
109
int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
116
for (i = 0; i < s->nb_devices; i++) {
117
if (s->poll_index >= s->nb_devices) {
120
d = s->devices[s->poll_index];
121
if ((1 << d->devaddr) & poll_mask) {
122
buf[0] = ADB_READREG | (d->devaddr << 4);
123
olen = do_adb_request(s, obuf + 1, buf, 1);
124
/* if there is data, we poll again the same device */
126
s->status |= ADB_STATUS_POLLREPLY;
137
void adb_set_autopoll_enabled(ADBBusState *s, bool enabled)
139
if (s->autopoll_enabled != enabled) {
140
s->autopoll_enabled = enabled;
141
if (s->autopoll_enabled) {
142
timer_mod(s->autopoll_timer,
143
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
144
s->autopoll_rate_ms);
146
timer_del(s->autopoll_timer);
151
void adb_set_autopoll_rate_ms(ADBBusState *s, int rate_ms)
153
s->autopoll_rate_ms = rate_ms;
155
if (s->autopoll_enabled) {
156
timer_mod(s->autopoll_timer,
157
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
158
s->autopoll_rate_ms);
162
void adb_set_autopoll_mask(ADBBusState *s, uint16_t mask)
164
if (s->autopoll_mask != mask) {
165
s->autopoll_mask = mask;
166
if (s->autopoll_enabled && s->autopoll_mask) {
167
timer_mod(s->autopoll_timer,
168
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
169
s->autopoll_rate_ms);
171
timer_del(s->autopoll_timer);
176
void adb_autopoll_block(ADBBusState *s)
178
s->autopoll_blocked = true;
179
trace_adb_bus_autopoll_block(s->autopoll_blocked);
181
if (s->autopoll_enabled) {
182
timer_del(s->autopoll_timer);
186
void adb_autopoll_unblock(ADBBusState *s)
188
s->autopoll_blocked = false;
189
trace_adb_bus_autopoll_block(s->autopoll_blocked);
191
if (s->autopoll_enabled) {
192
timer_mod(s->autopoll_timer,
193
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
194
s->autopoll_rate_ms);
198
static void adb_autopoll(void *opaque)
200
ADBBusState *s = opaque;
202
if (!s->autopoll_blocked) {
203
trace_adb_bus_autopoll_cb(s->autopoll_mask);
204
s->autopoll_cb(s->autopoll_cb_opaque);
205
trace_adb_bus_autopoll_cb_done(s->autopoll_mask);
208
timer_mod(s->autopoll_timer,
209
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
210
s->autopoll_rate_ms);
213
void adb_register_autopoll_callback(ADBBusState *s, void (*cb)(void *opaque),
217
s->autopoll_cb_opaque = opaque;
220
static const VMStateDescription vmstate_adb_bus = {
223
.minimum_version_id = 0,
224
.fields = (const VMStateField[]) {
225
VMSTATE_TIMER_PTR(autopoll_timer, ADBBusState),
226
VMSTATE_BOOL(autopoll_enabled, ADBBusState),
227
VMSTATE_UINT8(autopoll_rate_ms, ADBBusState),
228
VMSTATE_UINT16(autopoll_mask, ADBBusState),
229
VMSTATE_BOOL(autopoll_blocked, ADBBusState),
230
VMSTATE_END_OF_LIST()
234
static void adb_bus_reset_hold(Object *obj, ResetType type)
236
ADBBusState *adb_bus = ADB_BUS(obj);
238
adb_bus->autopoll_enabled = false;
239
adb_bus->autopoll_mask = 0xffff;
240
adb_bus->autopoll_rate_ms = 20;
243
static void adb_bus_realize(BusState *qbus, Error **errp)
245
ADBBusState *adb_bus = ADB_BUS(qbus);
247
adb_bus->autopoll_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, adb_autopoll,
250
vmstate_register_any(NULL, &vmstate_adb_bus, adb_bus);
253
static void adb_bus_unrealize(BusState *qbus)
255
ADBBusState *adb_bus = ADB_BUS(qbus);
257
timer_del(adb_bus->autopoll_timer);
259
vmstate_unregister(NULL, &vmstate_adb_bus, adb_bus);
262
static void adb_bus_class_init(ObjectClass *klass, void *data)
264
BusClass *k = BUS_CLASS(klass);
265
ResettableClass *rc = RESETTABLE_CLASS(klass);
267
k->realize = adb_bus_realize;
268
k->unrealize = adb_bus_unrealize;
269
rc->phases.hold = adb_bus_reset_hold;
272
static const TypeInfo adb_bus_type_info = {
273
.name = TYPE_ADB_BUS,
275
.instance_size = sizeof(ADBBusState),
276
.class_init = adb_bus_class_init,
279
const VMStateDescription vmstate_adb_device = {
280
.name = "adb_device",
282
.minimum_version_id = 0,
283
.fields = (const VMStateField[]) {
284
VMSTATE_INT32(devaddr, ADBDevice),
285
VMSTATE_INT32(handler, ADBDevice),
286
VMSTATE_END_OF_LIST()
290
static void adb_device_realizefn(DeviceState *dev, Error **errp)
292
ADBDevice *d = ADB_DEVICE(dev);
293
ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
295
if (bus->nb_devices >= MAX_ADB_DEVICES) {
299
bus->devices[bus->nb_devices++] = d;
302
static void adb_device_class_init(ObjectClass *oc, void *data)
304
DeviceClass *dc = DEVICE_CLASS(oc);
306
dc->realize = adb_device_realizefn;
307
dc->bus_type = TYPE_ADB_BUS;
310
static const TypeInfo adb_device_type_info = {
311
.name = TYPE_ADB_DEVICE,
312
.parent = TYPE_DEVICE,
313
.class_size = sizeof(ADBDeviceClass),
314
.instance_size = sizeof(ADBDevice),
316
.class_init = adb_device_class_init,
319
static void adb_register_types(void)
321
type_register_static(&adb_bus_type_info);
322
type_register_static(&adb_device_type_info);
325
type_init(adb_register_types)