]>
Commit | Line | Data |
---|---|---|
9c16fa79 AG |
1 | /* |
2 | * QEMU IndustryPack emulation | |
3 | * | |
4 | * Copyright (C) 2012 Igalia, S.L. | |
b996aed5 | 5 | * Author: Alberto Garcia <berto@igalia.com> |
9c16fa79 AG |
6 | * |
7 | * This code is licensed under the GNU GPL v2 or (at your option) any | |
8 | * later version. | |
9 | */ | |
10 | ||
0430891c | 11 | #include "qemu/osdep.h" |
da34e65c | 12 | #include "qapi/error.h" |
0b8fa32f | 13 | #include "qemu/module.h" |
1f9c4cfd | 14 | #include "hw/ipack/ipack.h" |
9c16fa79 AG |
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 | ||
77cbb28a AF |
30 | void ipack_bus_new_inplace(IPackBus *bus, size_t bus_size, |
31 | DeviceState *parent, | |
9c16fa79 AG |
32 | const char *name, uint8_t n_slots, |
33 | qemu_irq_handler handler) | |
34 | { | |
fb17dfe0 | 35 | qbus_create_inplace(bus, bus_size, TYPE_IPACK_BUS, parent, name); |
9c16fa79 AG |
36 | bus->n_slots = n_slots; |
37 | bus->set_irq = handler; | |
38 | } | |
39 | ||
5c570902 | 40 | static void ipack_device_realize(DeviceState *dev, Error **errp) |
9c16fa79 | 41 | { |
5c570902 AF |
42 | IPackDevice *idev = IPACK_DEVICE(dev); |
43 | IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(dev)); | |
9c16fa79 AG |
44 | IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev); |
45 | ||
5c570902 AF |
46 | if (idev->slot < 0) { |
47 | idev->slot = bus->free_slot; | |
9c16fa79 | 48 | } |
5c570902 AF |
49 | if (idev->slot >= bus->n_slots) { |
50 | error_setg(errp, "Only %" PRIu8 " slots available.", bus->n_slots); | |
51 | return; | |
9c16fa79 | 52 | } |
5c570902 | 53 | bus->free_slot = idev->slot + 1; |
9c16fa79 | 54 | |
5c570902 | 55 | idev->irq = qemu_allocate_irqs(bus->set_irq, idev, 2); |
9c16fa79 | 56 | |
5c570902 | 57 | k->realize(dev, errp); |
9c16fa79 AG |
58 | } |
59 | ||
5c570902 | 60 | static void ipack_device_unrealize(DeviceState *dev, Error **errp) |
9c16fa79 | 61 | { |
5c570902 | 62 | IPackDevice *idev = IPACK_DEVICE(dev); |
9c16fa79 | 63 | IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev); |
5c570902 | 64 | Error *err = NULL; |
9c16fa79 | 65 | |
5c570902 AF |
66 | if (k->unrealize) { |
67 | k->unrealize(dev, &err); | |
68 | error_propagate(errp, err); | |
69 | return; | |
9c16fa79 AG |
70 | } |
71 | ||
f173d57a | 72 | qemu_free_irqs(idev->irq, 2); |
9c16fa79 AG |
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); | |
5c570902 | 83 | |
125ee0ed | 84 | set_bit(DEVICE_CATEGORY_INPUT, k->categories); |
9c16fa79 | 85 | k->bus_type = TYPE_IPACK_BUS; |
5c570902 AF |
86 | k->realize = ipack_device_realize; |
87 | k->unrealize = ipack_device_unrealize; | |
9c16fa79 AG |
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, | |
35d08458 | 95 | .fields = (VMStateField[]) { |
9c16fa79 AG |
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) |