]>
Commit | Line | Data |
---|---|---|
267002cd FB |
1 | /* |
2 | * QEMU ADB support | |
5fafdf24 | 3 | * |
267002cd | 4 | * Copyright (c) 2004 Fabrice Bellard |
5fafdf24 | 5 | * |
267002cd FB |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
0b8fa32f | 24 | |
0430891c | 25 | #include "qemu/osdep.h" |
0d09e41a | 26 | #include "hw/input/adb.h" |
a27bd6c7 | 27 | #include "hw/qdev-properties.h" |
d6454270 | 28 | #include "migration/vmstate.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
77cb0f5a | 30 | #include "adb-internal.h" |
267002cd | 31 | |
bec9d989 FB |
32 | /* error codes */ |
33 | #define ADB_RET_NOTPRESENT (-2) | |
34 | ||
2e4a7c9c AF |
35 | static void adb_device_reset(ADBDevice *d) |
36 | { | |
37 | qdev_reset_all(DEVICE(d)); | |
38 | } | |
39 | ||
e2733d20 | 40 | int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len) |
267002cd FB |
41 | { |
42 | ADBDevice *d; | |
43 | int devaddr, cmd, i; | |
267002cd | 44 | |
819e712b | 45 | cmd = buf[0] & 0xf; |
bec9d989 FB |
46 | if (cmd == ADB_BUSRESET) { |
47 | for(i = 0; i < s->nb_devices; i++) { | |
2e4a7c9c AF |
48 | d = s->devices[i]; |
49 | adb_device_reset(d); | |
bec9d989 FB |
50 | } |
51 | return 0; | |
267002cd | 52 | } |
bec9d989 | 53 | devaddr = buf[0] >> 4; |
267002cd | 54 | for(i = 0; i < s->nb_devices; i++) { |
2e4a7c9c | 55 | d = s->devices[i]; |
267002cd | 56 | if (d->devaddr == devaddr) { |
2e4a7c9c AF |
57 | ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d); |
58 | return adc->devreq(d, obuf, buf, len); | |
267002cd FB |
59 | } |
60 | } | |
bec9d989 | 61 | return ADB_RET_NOTPRESENT; |
e2733d20 FB |
62 | } |
63 | ||
bec9d989 | 64 | /* XXX: move that to cuda ? */ |
216c906e | 65 | int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask) |
e2733d20 FB |
66 | { |
67 | ADBDevice *d; | |
68 | int olen, i; | |
bec9d989 | 69 | uint8_t buf[1]; |
e2733d20 FB |
70 | |
71 | olen = 0; | |
72 | for(i = 0; i < s->nb_devices; i++) { | |
73 | if (s->poll_index >= s->nb_devices) | |
74 | s->poll_index = 0; | |
2e4a7c9c | 75 | d = s->devices[s->poll_index]; |
216c906e HP |
76 | if ((1 << d->devaddr) & poll_mask) { |
77 | buf[0] = ADB_READREG | (d->devaddr << 4); | |
78 | olen = adb_request(s, obuf + 1, buf, 1); | |
79 | /* if there is data, we poll again the same device */ | |
80 | if (olen > 0) { | |
81 | obuf[0] = buf[0]; | |
82 | olen++; | |
83 | break; | |
84 | } | |
bec9d989 FB |
85 | } |
86 | s->poll_index++; | |
e2733d20 FB |
87 | } |
88 | return olen; | |
267002cd FB |
89 | } |
90 | ||
84ede329 AF |
91 | static const TypeInfo adb_bus_type_info = { |
92 | .name = TYPE_ADB_BUS, | |
93 | .parent = TYPE_BUS, | |
94 | .instance_size = sizeof(ADBBusState), | |
95 | }; | |
96 | ||
77cb0f5a | 97 | const VMStateDescription vmstate_adb_device = { |
e5dffaa5 MCA |
98 | .name = "adb_device", |
99 | .version_id = 0, | |
100 | .minimum_version_id = 0, | |
101 | .fields = (VMStateField[]) { | |
102 | VMSTATE_INT32(devaddr, ADBDevice), | |
103 | VMSTATE_INT32(handler, ADBDevice), | |
104 | VMSTATE_END_OF_LIST() | |
105 | } | |
106 | }; | |
107 | ||
2e4a7c9c AF |
108 | static void adb_device_realizefn(DeviceState *dev, Error **errp) |
109 | { | |
110 | ADBDevice *d = ADB_DEVICE(dev); | |
111 | ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev)); | |
112 | ||
113 | if (bus->nb_devices >= MAX_ADB_DEVICES) { | |
114 | return; | |
115 | } | |
116 | ||
117 | bus->devices[bus->nb_devices++] = d; | |
118 | } | |
119 | ||
84051eb4 MCA |
120 | static Property adb_device_properties[] = { |
121 | DEFINE_PROP_BOOL("disable-direct-reg3-writes", ADBDevice, | |
122 | disable_direct_reg3_writes, false), | |
123 | DEFINE_PROP_END_OF_LIST(), | |
124 | }; | |
125 | ||
2e4a7c9c AF |
126 | static void adb_device_class_init(ObjectClass *oc, void *data) |
127 | { | |
128 | DeviceClass *dc = DEVICE_CLASS(oc); | |
129 | ||
130 | dc->realize = adb_device_realizefn; | |
84051eb4 | 131 | dc->props = adb_device_properties; |
2e4a7c9c AF |
132 | dc->bus_type = TYPE_ADB_BUS; |
133 | } | |
134 | ||
135 | static const TypeInfo adb_device_type_info = { | |
136 | .name = TYPE_ADB_DEVICE, | |
137 | .parent = TYPE_DEVICE, | |
138 | .instance_size = sizeof(ADBDevice), | |
139 | .abstract = true, | |
140 | .class_init = adb_device_class_init, | |
141 | }; | |
142 | ||
84ede329 AF |
143 | static void adb_register_types(void) |
144 | { | |
145 | type_register_static(&adb_bus_type_info); | |
2e4a7c9c | 146 | type_register_static(&adb_device_type_info); |
84ede329 AF |
147 | } |
148 | ||
149 | type_init(adb_register_types) |