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#include "qemu/osdep.h"
26#include "hw/input/adb.h"
27#include "qemu/module.h"
28#include "adb-internal.h"
29
30
31#define ADB_RET_NOTPRESENT (-2)
32
33static void adb_device_reset(ADBDevice *d)
34{
35 qdev_reset_all(DEVICE(d));
36}
37
38int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
39{
40 ADBDevice *d;
41 int devaddr, cmd, i;
42
43 cmd = buf[0] & 0xf;
44 if (cmd == ADB_BUSRESET) {
45 for(i = 0; i < s->nb_devices; i++) {
46 d = s->devices[i];
47 adb_device_reset(d);
48 }
49 return 0;
50 }
51 devaddr = buf[0] >> 4;
52 for(i = 0; i < s->nb_devices; i++) {
53 d = s->devices[i];
54 if (d->devaddr == devaddr) {
55 ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d);
56 return adc->devreq(d, obuf, buf, len);
57 }
58 }
59 return ADB_RET_NOTPRESENT;
60}
61
62
63int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
64{
65 ADBDevice *d;
66 int olen, i;
67 uint8_t buf[1];
68
69 olen = 0;
70 for(i = 0; i < s->nb_devices; i++) {
71 if (s->poll_index >= s->nb_devices)
72 s->poll_index = 0;
73 d = s->devices[s->poll_index];
74 if ((1 << d->devaddr) & poll_mask) {
75 buf[0] = ADB_READREG | (d->devaddr << 4);
76 olen = adb_request(s, obuf + 1, buf, 1);
77
78 if (olen > 0) {
79 obuf[0] = buf[0];
80 olen++;
81 break;
82 }
83 }
84 s->poll_index++;
85 }
86 return olen;
87}
88
89static const TypeInfo adb_bus_type_info = {
90 .name = TYPE_ADB_BUS,
91 .parent = TYPE_BUS,
92 .instance_size = sizeof(ADBBusState),
93};
94
95const VMStateDescription vmstate_adb_device = {
96 .name = "adb_device",
97 .version_id = 0,
98 .minimum_version_id = 0,
99 .fields = (VMStateField[]) {
100 VMSTATE_INT32(devaddr, ADBDevice),
101 VMSTATE_INT32(handler, ADBDevice),
102 VMSTATE_END_OF_LIST()
103 }
104};
105
106static void adb_device_realizefn(DeviceState *dev, Error **errp)
107{
108 ADBDevice *d = ADB_DEVICE(dev);
109 ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
110
111 if (bus->nb_devices >= MAX_ADB_DEVICES) {
112 return;
113 }
114
115 bus->devices[bus->nb_devices++] = d;
116}
117
118static Property adb_device_properties[] = {
119 DEFINE_PROP_BOOL("disable-direct-reg3-writes", ADBDevice,
120 disable_direct_reg3_writes, false),
121 DEFINE_PROP_END_OF_LIST(),
122};
123
124static void adb_device_class_init(ObjectClass *oc, void *data)
125{
126 DeviceClass *dc = DEVICE_CLASS(oc);
127
128 dc->realize = adb_device_realizefn;
129 dc->props = adb_device_properties;
130 dc->bus_type = TYPE_ADB_BUS;
131}
132
133static const TypeInfo adb_device_type_info = {
134 .name = TYPE_ADB_DEVICE,
135 .parent = TYPE_DEVICE,
136 .instance_size = sizeof(ADBDevice),
137 .abstract = true,
138 .class_init = adb_device_class_init,
139};
140
141static void adb_register_types(void)
142{
143 type_register_static(&adb_bus_type_info);
144 type_register_static(&adb_device_type_info);
145}
146
147type_init(adb_register_types)
148