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