1 /* 2 * QEMU IndustryPack emulation 3 * 4 * Copyright (C) 2012 Igalia, S.L. 5 * Author: Alberto Garcia <berto@igalia.com> 6 * 7 * This code is licensed under the GNU GPL v2 or (at your option) any 8 * later version. 9 */ 10 11 #include "qemu/osdep.h" 12 #include "qapi/error.h" 13 #include "qemu/module.h" 14 #include "hw/ipack/ipack.h" 15 16 IPackDevice *ipack_device_find(IPackBus *bus, int32_t slot) 17 { 18 BusChild *kid; 19 20 QTAILQ_FOREACH(kid, &BUS(bus)->children, sibling) { 21 DeviceState *qdev = kid->child; 22 IPackDevice *ip = IPACK_DEVICE(qdev); 23 if (ip->slot == slot) { 24 return ip; 25 } 26 } 27 return NULL; 28 } 29 30 void ipack_bus_new_inplace(IPackBus *bus, size_t bus_size, 31 DeviceState *parent, 32 const char *name, uint8_t n_slots, 33 qemu_irq_handler handler) 34 { 35 qbus_create_inplace(bus, bus_size, TYPE_IPACK_BUS, parent, name); 36 bus->n_slots = n_slots; 37 bus->set_irq = handler; 38 } 39 40 static void ipack_device_realize(DeviceState *dev, Error **errp) 41 { 42 IPackDevice *idev = IPACK_DEVICE(dev); 43 IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(dev)); 44 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev); 45 46 if (idev->slot < 0) { 47 idev->slot = bus->free_slot; 48 } 49 if (idev->slot >= bus->n_slots) { 50 error_setg(errp, "Only %" PRIu8 " slots available.", bus->n_slots); 51 return; 52 } 53 bus->free_slot = idev->slot + 1; 54 55 idev->irq = qemu_allocate_irqs(bus->set_irq, idev, 2); 56 57 k->realize(dev, errp); 58 } 59 60 static void ipack_device_unrealize(DeviceState *dev, Error **errp) 61 { 62 IPackDevice *idev = IPACK_DEVICE(dev); 63 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev); 64 Error *err = NULL; 65 66 if (k->unrealize) { 67 k->unrealize(dev, &err); 68 error_propagate(errp, err); 69 return; 70 } 71 72 qemu_free_irqs(idev->irq, 2); 73 } 74 75 static Property ipack_device_props[] = { 76 DEFINE_PROP_INT32("slot", IPackDevice, slot, -1), 77 DEFINE_PROP_END_OF_LIST() 78 }; 79 80 static void ipack_device_class_init(ObjectClass *klass, void *data) 81 { 82 DeviceClass *k = DEVICE_CLASS(klass); 83 84 set_bit(DEVICE_CATEGORY_INPUT, k->categories); 85 k->bus_type = TYPE_IPACK_BUS; 86 k->realize = ipack_device_realize; 87 k->unrealize = ipack_device_unrealize; 88 k->props = ipack_device_props; 89 } 90 91 const VMStateDescription vmstate_ipack_device = { 92 .name = "ipack_device", 93 .version_id = 1, 94 .minimum_version_id = 1, 95 .fields = (VMStateField[]) { 96 VMSTATE_INT32(slot, IPackDevice), 97 VMSTATE_END_OF_LIST() 98 } 99 }; 100 101 static const TypeInfo ipack_device_info = { 102 .name = TYPE_IPACK_DEVICE, 103 .parent = TYPE_DEVICE, 104 .instance_size = sizeof(IPackDevice), 105 .class_size = sizeof(IPackDeviceClass), 106 .class_init = ipack_device_class_init, 107 .abstract = true, 108 }; 109 110 static const TypeInfo ipack_bus_info = { 111 .name = TYPE_IPACK_BUS, 112 .parent = TYPE_BUS, 113 .instance_size = sizeof(IPackBus), 114 }; 115 116 static void ipack_register_types(void) 117 { 118 type_register_static(&ipack_device_info); 119 type_register_static(&ipack_bus_info); 120 } 121 122 type_init(ipack_register_types) 123