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