2004-06-03 22:46:20 +04:00
|
|
|
/*
|
|
|
|
* QEMU ADB support
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-06-03 22:46:20 +04:00
|
|
|
* Copyright (c) 2004 Fabrice Bellard
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-06-03 22:46:20 +04:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 17:35:07 +03:00
|
|
|
|
2016-01-26 21:17:30 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/input/adb.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-06-23 23:49:21 +03:00
|
|
|
#include "qemu/timer.h"
|
2017-12-20 15:14:06 +03:00
|
|
|
#include "adb-internal.h"
|
2020-06-23 23:49:36 +03:00
|
|
|
#include "trace.h"
|
2004-06-03 22:46:20 +04:00
|
|
|
|
2004-07-13 00:15:26 +04:00
|
|
|
/* error codes */
|
|
|
|
#define ADB_RET_NOTPRESENT (-2)
|
|
|
|
|
2020-06-23 23:49:36 +03:00
|
|
|
static const char *adb_commands[] = {
|
|
|
|
"RESET", "FLUSH", "(Reserved 0x2)", "(Reserved 0x3)",
|
|
|
|
"Reserved (0x4)", "(Reserved 0x5)", "(Reserved 0x6)", "(Reserved 0x7)",
|
|
|
|
"LISTEN r0", "LISTEN r1", "LISTEN r2", "LISTEN r3",
|
|
|
|
"TALK r0", "TALK r1", "TALK r2", "TALK r3",
|
|
|
|
};
|
|
|
|
|
2013-01-24 03:04:04 +04:00
|
|
|
static void adb_device_reset(ADBDevice *d)
|
|
|
|
{
|
2022-12-16 18:55:27 +03:00
|
|
|
device_cold_reset(DEVICE(d));
|
2013-01-24 03:04:04 +04:00
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:28 +03:00
|
|
|
static int do_adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf,
|
|
|
|
int len)
|
2004-06-03 22:46:20 +04:00
|
|
|
{
|
|
|
|
ADBDevice *d;
|
2020-06-23 23:49:26 +03:00
|
|
|
ADBDeviceClass *adc;
|
2020-06-23 23:49:27 +03:00
|
|
|
int devaddr, cmd, olen, i;
|
2004-06-03 22:46:20 +04:00
|
|
|
|
2004-06-21 20:47:13 +04:00
|
|
|
cmd = buf[0] & 0xf;
|
2004-07-13 00:15:26 +04:00
|
|
|
if (cmd == ADB_BUSRESET) {
|
2020-06-23 23:49:15 +03:00
|
|
|
for (i = 0; i < s->nb_devices; i++) {
|
2013-01-24 03:04:04 +04:00
|
|
|
d = s->devices[i];
|
|
|
|
adb_device_reset(d);
|
2004-07-13 00:15:26 +04:00
|
|
|
}
|
2020-06-23 23:49:27 +03:00
|
|
|
s->status = 0;
|
2004-07-13 00:15:26 +04:00
|
|
|
return 0;
|
2004-06-03 22:46:20 +04:00
|
|
|
}
|
2020-06-23 23:49:26 +03:00
|
|
|
|
|
|
|
s->pending = 0;
|
|
|
|
for (i = 0; i < s->nb_devices; i++) {
|
|
|
|
d = s->devices[i];
|
|
|
|
adc = ADB_DEVICE_GET_CLASS(d);
|
|
|
|
|
|
|
|
if (adc->devhasdata(d)) {
|
|
|
|
s->pending |= (1 << d->devaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:27 +03:00
|
|
|
s->status = 0;
|
2004-07-13 00:15:26 +04:00
|
|
|
devaddr = buf[0] >> 4;
|
2020-06-23 23:49:15 +03:00
|
|
|
for (i = 0; i < s->nb_devices; i++) {
|
2013-01-24 03:04:04 +04:00
|
|
|
d = s->devices[i];
|
2020-06-23 23:49:26 +03:00
|
|
|
adc = ADB_DEVICE_GET_CLASS(d);
|
|
|
|
|
2004-06-03 22:46:20 +04:00
|
|
|
if (d->devaddr == devaddr) {
|
2020-06-23 23:49:27 +03:00
|
|
|
olen = adc->devreq(d, obuf, buf, len);
|
|
|
|
if (!olen) {
|
|
|
|
s->status |= ADB_STATUS_BUSTIMEOUT;
|
|
|
|
}
|
|
|
|
return olen;
|
2004-06-03 22:46:20 +04:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 23:49:26 +03:00
|
|
|
|
2020-06-23 23:49:27 +03:00
|
|
|
s->status |= ADB_STATUS_BUSTIMEOUT;
|
2004-07-13 00:15:26 +04:00
|
|
|
return ADB_RET_NOTPRESENT;
|
2004-06-22 02:46:10 +04:00
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:28 +03:00
|
|
|
int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
|
|
|
|
{
|
2020-06-23 23:49:36 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
trace_adb_bus_request(buf[0] >> 4, adb_commands[buf[0] & 0xf], len);
|
|
|
|
|
2020-06-23 23:49:34 +03:00
|
|
|
assert(s->autopoll_blocked);
|
|
|
|
|
2020-06-23 23:49:36 +03:00
|
|
|
ret = do_adb_request(s, obuf, buf, len);
|
|
|
|
|
|
|
|
trace_adb_bus_request_done(buf[0] >> 4, adb_commands[buf[0] & 0xf], ret);
|
|
|
|
return ret;
|
2020-06-23 23:49:28 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 23:34:08 +03:00
|
|
|
int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
|
2004-06-22 02:46:10 +04:00
|
|
|
{
|
|
|
|
ADBDevice *d;
|
|
|
|
int olen, i;
|
2004-07-13 00:15:26 +04:00
|
|
|
uint8_t buf[1];
|
2004-06-22 02:46:10 +04:00
|
|
|
|
|
|
|
olen = 0;
|
2020-06-23 23:49:15 +03:00
|
|
|
for (i = 0; i < s->nb_devices; i++) {
|
|
|
|
if (s->poll_index >= s->nb_devices) {
|
2004-06-22 02:46:10 +04:00
|
|
|
s->poll_index = 0;
|
2020-06-23 23:49:15 +03:00
|
|
|
}
|
2013-01-24 03:04:04 +04:00
|
|
|
d = s->devices[s->poll_index];
|
2016-02-07 23:34:08 +03:00
|
|
|
if ((1 << d->devaddr) & poll_mask) {
|
|
|
|
buf[0] = ADB_READREG | (d->devaddr << 4);
|
2020-06-23 23:49:28 +03:00
|
|
|
olen = do_adb_request(s, obuf + 1, buf, 1);
|
2016-02-07 23:34:08 +03:00
|
|
|
/* if there is data, we poll again the same device */
|
|
|
|
if (olen > 0) {
|
2020-06-23 23:49:27 +03:00
|
|
|
s->status |= ADB_STATUS_POLLREPLY;
|
2016-02-07 23:34:08 +03:00
|
|
|
obuf[0] = buf[0];
|
|
|
|
olen++;
|
2020-06-23 23:49:27 +03:00
|
|
|
return olen;
|
2016-02-07 23:34:08 +03:00
|
|
|
}
|
2004-07-13 00:15:26 +04:00
|
|
|
}
|
|
|
|
s->poll_index++;
|
2004-06-22 02:46:10 +04:00
|
|
|
}
|
|
|
|
return olen;
|
2004-06-03 22:46:20 +04:00
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:21 +03:00
|
|
|
void adb_set_autopoll_enabled(ADBBusState *s, bool enabled)
|
|
|
|
{
|
|
|
|
if (s->autopoll_enabled != enabled) {
|
|
|
|
s->autopoll_enabled = enabled;
|
|
|
|
if (s->autopoll_enabled) {
|
|
|
|
timer_mod(s->autopoll_timer,
|
|
|
|
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
s->autopoll_rate_ms);
|
|
|
|
} else {
|
|
|
|
timer_del(s->autopoll_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_set_autopoll_rate_ms(ADBBusState *s, int rate_ms)
|
|
|
|
{
|
|
|
|
s->autopoll_rate_ms = rate_ms;
|
|
|
|
|
|
|
|
if (s->autopoll_enabled) {
|
|
|
|
timer_mod(s->autopoll_timer,
|
|
|
|
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
s->autopoll_rate_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_set_autopoll_mask(ADBBusState *s, uint16_t mask)
|
|
|
|
{
|
|
|
|
if (s->autopoll_mask != mask) {
|
|
|
|
s->autopoll_mask = mask;
|
|
|
|
if (s->autopoll_enabled && s->autopoll_mask) {
|
|
|
|
timer_mod(s->autopoll_timer,
|
|
|
|
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
s->autopoll_rate_ms);
|
|
|
|
} else {
|
|
|
|
timer_del(s->autopoll_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:29 +03:00
|
|
|
void adb_autopoll_block(ADBBusState *s)
|
|
|
|
{
|
|
|
|
s->autopoll_blocked = true;
|
2020-06-23 23:49:36 +03:00
|
|
|
trace_adb_bus_autopoll_block(s->autopoll_blocked);
|
2020-06-23 23:49:29 +03:00
|
|
|
|
|
|
|
if (s->autopoll_enabled) {
|
|
|
|
timer_del(s->autopoll_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_autopoll_unblock(ADBBusState *s)
|
|
|
|
{
|
|
|
|
s->autopoll_blocked = false;
|
2020-06-23 23:49:36 +03:00
|
|
|
trace_adb_bus_autopoll_block(s->autopoll_blocked);
|
2020-06-23 23:49:29 +03:00
|
|
|
|
|
|
|
if (s->autopoll_enabled) {
|
|
|
|
timer_mod(s->autopoll_timer,
|
|
|
|
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
s->autopoll_rate_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:21 +03:00
|
|
|
static void adb_autopoll(void *opaque)
|
|
|
|
{
|
|
|
|
ADBBusState *s = opaque;
|
|
|
|
|
2020-06-23 23:49:34 +03:00
|
|
|
if (!s->autopoll_blocked) {
|
2020-06-23 23:49:36 +03:00
|
|
|
trace_adb_bus_autopoll_cb(s->autopoll_mask);
|
2020-06-23 23:49:34 +03:00
|
|
|
s->autopoll_cb(s->autopoll_cb_opaque);
|
2020-06-23 23:49:36 +03:00
|
|
|
trace_adb_bus_autopoll_cb_done(s->autopoll_mask);
|
2020-06-23 23:49:34 +03:00
|
|
|
}
|
2020-06-23 23:49:21 +03:00
|
|
|
|
|
|
|
timer_mod(s->autopoll_timer,
|
|
|
|
qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
|
|
|
|
s->autopoll_rate_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void adb_register_autopoll_callback(ADBBusState *s, void (*cb)(void *opaque),
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
s->autopoll_cb = cb;
|
|
|
|
s->autopoll_cb_opaque = opaque;
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:20 +03:00
|
|
|
static const VMStateDescription vmstate_adb_bus = {
|
|
|
|
.name = "adb_bus",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
2023-12-21 06:16:14 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2020-06-23 23:49:21 +03:00
|
|
|
VMSTATE_TIMER_PTR(autopoll_timer, ADBBusState),
|
|
|
|
VMSTATE_BOOL(autopoll_enabled, ADBBusState),
|
|
|
|
VMSTATE_UINT8(autopoll_rate_ms, ADBBusState),
|
|
|
|
VMSTATE_UINT16(autopoll_mask, ADBBusState),
|
2020-06-23 23:49:29 +03:00
|
|
|
VMSTATE_BOOL(autopoll_blocked, ADBBusState),
|
2020-06-23 23:49:20 +03:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-12 19:08:07 +03:00
|
|
|
static void adb_bus_reset_hold(Object *obj, ResetType type)
|
2020-06-23 23:49:21 +03:00
|
|
|
{
|
2024-01-19 19:35:10 +03:00
|
|
|
ADBBusState *adb_bus = ADB_BUS(obj);
|
2020-06-23 23:49:21 +03:00
|
|
|
|
|
|
|
adb_bus->autopoll_enabled = false;
|
|
|
|
adb_bus->autopoll_mask = 0xffff;
|
|
|
|
adb_bus->autopoll_rate_ms = 20;
|
|
|
|
}
|
|
|
|
|
2020-06-23 23:49:20 +03:00
|
|
|
static void adb_bus_realize(BusState *qbus, Error **errp)
|
|
|
|
{
|
|
|
|
ADBBusState *adb_bus = ADB_BUS(qbus);
|
|
|
|
|
2020-06-23 23:49:21 +03:00
|
|
|
adb_bus->autopoll_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, adb_autopoll,
|
|
|
|
adb_bus);
|
|
|
|
|
2023-10-20 12:07:20 +03:00
|
|
|
vmstate_register_any(NULL, &vmstate_adb_bus, adb_bus);
|
2020-06-23 23:49:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void adb_bus_unrealize(BusState *qbus)
|
|
|
|
{
|
|
|
|
ADBBusState *adb_bus = ADB_BUS(qbus);
|
|
|
|
|
2020-06-23 23:49:21 +03:00
|
|
|
timer_del(adb_bus->autopoll_timer);
|
|
|
|
|
2020-06-23 23:49:20 +03:00
|
|
|
vmstate_unregister(NULL, &vmstate_adb_bus, adb_bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adb_bus_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
BusClass *k = BUS_CLASS(klass);
|
2024-01-19 19:35:10 +03:00
|
|
|
ResettableClass *rc = RESETTABLE_CLASS(klass);
|
2020-06-23 23:49:20 +03:00
|
|
|
|
|
|
|
k->realize = adb_bus_realize;
|
|
|
|
k->unrealize = adb_bus_unrealize;
|
2024-01-19 19:35:10 +03:00
|
|
|
rc->phases.hold = adb_bus_reset_hold;
|
2020-06-23 23:49:20 +03:00
|
|
|
}
|
|
|
|
|
2013-01-24 03:04:03 +04:00
|
|
|
static const TypeInfo adb_bus_type_info = {
|
|
|
|
.name = TYPE_ADB_BUS,
|
|
|
|
.parent = TYPE_BUS,
|
|
|
|
.instance_size = sizeof(ADBBusState),
|
2020-06-23 23:49:20 +03:00
|
|
|
.class_init = adb_bus_class_init,
|
2013-01-24 03:04:03 +04:00
|
|
|
};
|
|
|
|
|
2017-12-20 15:14:06 +03:00
|
|
|
const VMStateDescription vmstate_adb_device = {
|
2015-02-10 01:40:45 +03:00
|
|
|
.name = "adb_device",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
2023-12-21 06:16:14 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2015-02-10 01:40:45 +03:00
|
|
|
VMSTATE_INT32(devaddr, ADBDevice),
|
|
|
|
VMSTATE_INT32(handler, ADBDevice),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-24 03:04:04 +04:00
|
|
|
static void adb_device_realizefn(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
ADBDevice *d = ADB_DEVICE(dev);
|
|
|
|
ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
|
|
|
|
|
|
|
|
if (bus->nb_devices >= MAX_ADB_DEVICES) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus->devices[bus->nb_devices++] = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adb_device_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
|
|
|
|
dc->realize = adb_device_realizefn;
|
|
|
|
dc->bus_type = TYPE_ADB_BUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo adb_device_type_info = {
|
|
|
|
.name = TYPE_ADB_DEVICE,
|
|
|
|
.parent = TYPE_DEVICE,
|
2020-08-25 13:28:51 +03:00
|
|
|
.class_size = sizeof(ADBDeviceClass),
|
2013-01-24 03:04:04 +04:00
|
|
|
.instance_size = sizeof(ADBDevice),
|
|
|
|
.abstract = true,
|
|
|
|
.class_init = adb_device_class_init,
|
|
|
|
};
|
|
|
|
|
2013-01-24 03:04:03 +04:00
|
|
|
static void adb_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&adb_bus_type_info);
|
2013-01-24 03:04:04 +04:00
|
|
|
type_register_static(&adb_device_type_info);
|
2013-01-24 03:04:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init(adb_register_types)
|