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