]> git.proxmox.com Git - qemu.git/blob - hw/char/ipack.c
devices: Associate devices to their logical category
[qemu.git] / hw / char / ipack.c
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 "ipack.h"
12
13 IPackDevice *ipack_device_find(IPackBus *bus, int32_t slot)
14 {
15 BusChild *kid;
16
17 QTAILQ_FOREACH(kid, &BUS(bus)->children, sibling) {
18 DeviceState *qdev = kid->child;
19 IPackDevice *ip = IPACK_DEVICE(qdev);
20 if (ip->slot == slot) {
21 return ip;
22 }
23 }
24 return NULL;
25 }
26
27 void ipack_bus_new_inplace(IPackBus *bus, DeviceState *parent,
28 const char *name, uint8_t n_slots,
29 qemu_irq_handler handler)
30 {
31 qbus_create_inplace(&bus->qbus, TYPE_IPACK_BUS, parent, name);
32 bus->n_slots = n_slots;
33 bus->set_irq = handler;
34 }
35
36 static int ipack_device_dev_init(DeviceState *qdev)
37 {
38 IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(qdev));
39 IPackDevice *dev = IPACK_DEVICE(qdev);
40 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
41
42 if (dev->slot < 0) {
43 dev->slot = bus->free_slot;
44 }
45 if (dev->slot >= bus->n_slots) {
46 return -1;
47 }
48 bus->free_slot = dev->slot + 1;
49
50 dev->irq = qemu_allocate_irqs(bus->set_irq, dev, 2);
51
52 return k->init(dev);
53 }
54
55 static int ipack_device_dev_exit(DeviceState *qdev)
56 {
57 IPackDevice *dev = IPACK_DEVICE(qdev);
58 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
59
60 if (k->exit) {
61 k->exit(dev);
62 }
63
64 qemu_free_irqs(dev->irq);
65
66 return 0;
67 }
68
69 static Property ipack_device_props[] = {
70 DEFINE_PROP_INT32("slot", IPackDevice, slot, -1),
71 DEFINE_PROP_END_OF_LIST()
72 };
73
74 static void ipack_device_class_init(ObjectClass *klass, void *data)
75 {
76 DeviceClass *k = DEVICE_CLASS(klass);
77 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
78 k->bus_type = TYPE_IPACK_BUS;
79 k->init = ipack_device_dev_init;
80 k->exit = ipack_device_dev_exit;
81 k->props = ipack_device_props;
82 }
83
84 const VMStateDescription vmstate_ipack_device = {
85 .name = "ipack_device",
86 .version_id = 1,
87 .minimum_version_id = 1,
88 .minimum_version_id_old = 1,
89 .fields = (VMStateField[]) {
90 VMSTATE_INT32(slot, IPackDevice),
91 VMSTATE_END_OF_LIST()
92 }
93 };
94
95 static const TypeInfo ipack_device_info = {
96 .name = TYPE_IPACK_DEVICE,
97 .parent = TYPE_DEVICE,
98 .instance_size = sizeof(IPackDevice),
99 .class_size = sizeof(IPackDeviceClass),
100 .class_init = ipack_device_class_init,
101 .abstract = true,
102 };
103
104 static const TypeInfo ipack_bus_info = {
105 .name = TYPE_IPACK_BUS,
106 .parent = TYPE_BUS,
107 .instance_size = sizeof(IPackBus),
108 };
109
110 static void ipack_register_types(void)
111 {
112 type_register_static(&ipack_device_info);
113 type_register_static(&ipack_bus_info);
114 }
115
116 type_init(ipack_register_types)