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