tests: Avoid 'do/while(false); ' in vhost-user-bridge
[qemu/ar7.git] / hw / input / adb.c
blob23ae6f0d751331c0cd0891cfbc7b70ee284bf8b0
1 /*
2 * QEMU ADB support
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "hw/input/adb.h"
26 #include "adb-internal.h"
28 /* error codes */
29 #define ADB_RET_NOTPRESENT (-2)
31 static void adb_device_reset(ADBDevice *d)
33 qdev_reset_all(DEVICE(d));
36 int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len)
38 ADBDevice *d;
39 int devaddr, cmd, i;
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);
47 return 0;
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);
57 return ADB_RET_NOTPRESENT;
60 /* XXX: move that to cuda ? */
61 int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask)
63 ADBDevice *d;
64 int olen, i;
65 uint8_t buf[1];
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 /* if there is data, we poll again the same device */
76 if (olen > 0) {
77 obuf[0] = buf[0];
78 olen++;
79 break;
82 s->poll_index++;
84 return olen;
87 static const TypeInfo adb_bus_type_info = {
88 .name = TYPE_ADB_BUS,
89 .parent = TYPE_BUS,
90 .instance_size = sizeof(ADBBusState),
93 const 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()
104 static void adb_device_realizefn(DeviceState *dev, Error **errp)
106 ADBDevice *d = ADB_DEVICE(dev);
107 ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev));
109 if (bus->nb_devices >= MAX_ADB_DEVICES) {
110 return;
113 bus->devices[bus->nb_devices++] = d;
116 static void adb_device_class_init(ObjectClass *oc, void *data)
118 DeviceClass *dc = DEVICE_CLASS(oc);
120 dc->realize = adb_device_realizefn;
121 dc->bus_type = TYPE_ADB_BUS;
124 static const TypeInfo adb_device_type_info = {
125 .name = TYPE_ADB_DEVICE,
126 .parent = TYPE_DEVICE,
127 .instance_size = sizeof(ADBDevice),
128 .abstract = true,
129 .class_init = adb_device_class_init,
132 static void adb_register_types(void)
134 type_register_static(&adb_bus_type_info);
135 type_register_static(&adb_device_type_info);
138 type_init(adb_register_types)