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