blob: 59bfe2869e0e1ab7d1f9b3a9db91ad4dfb362e34 [file] [log] [blame]
Alberto Garcia9c16fa72013-01-11 18:25:29 +01001/*
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
Andreas Färber1f9c4cf2013-08-02 00:48:40 +020011#include "hw/ipack/ipack.h"
Alberto Garcia9c16fa72013-01-11 18:25:29 +010012
13IPackDevice *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
Andreas Färber77cbb282013-08-23 20:07:28 +020027void ipack_bus_new_inplace(IPackBus *bus, size_t bus_size,
28 DeviceState *parent,
Alberto Garcia9c16fa72013-01-11 18:25:29 +010029 const char *name, uint8_t n_slots,
30 qemu_irq_handler handler)
31{
Andreas Färberfb17dfe2013-08-24 00:02:27 +020032 qbus_create_inplace(bus, bus_size, TYPE_IPACK_BUS, parent, name);
Alberto Garcia9c16fa72013-01-11 18:25:29 +010033 bus->n_slots = n_slots;
34 bus->set_irq = handler;
35}
36
Andreas Färber5c570902013-08-01 18:45:02 +020037static void ipack_device_realize(DeviceState *dev, Error **errp)
Alberto Garcia9c16fa72013-01-11 18:25:29 +010038{
Andreas Färber5c570902013-08-01 18:45:02 +020039 IPackDevice *idev = IPACK_DEVICE(dev);
40 IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(dev));
Alberto Garcia9c16fa72013-01-11 18:25:29 +010041 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
42
Andreas Färber5c570902013-08-01 18:45:02 +020043 if (idev->slot < 0) {
44 idev->slot = bus->free_slot;
Alberto Garcia9c16fa72013-01-11 18:25:29 +010045 }
Andreas Färber5c570902013-08-01 18:45:02 +020046 if (idev->slot >= bus->n_slots) {
47 error_setg(errp, "Only %" PRIu8 " slots available.", bus->n_slots);
48 return;
Alberto Garcia9c16fa72013-01-11 18:25:29 +010049 }
Andreas Färber5c570902013-08-01 18:45:02 +020050 bus->free_slot = idev->slot + 1;
Alberto Garcia9c16fa72013-01-11 18:25:29 +010051
Andreas Färber5c570902013-08-01 18:45:02 +020052 idev->irq = qemu_allocate_irqs(bus->set_irq, idev, 2);
Alberto Garcia9c16fa72013-01-11 18:25:29 +010053
Andreas Färber5c570902013-08-01 18:45:02 +020054 k->realize(dev, errp);
Alberto Garcia9c16fa72013-01-11 18:25:29 +010055}
56
Andreas Färber5c570902013-08-01 18:45:02 +020057static void ipack_device_unrealize(DeviceState *dev, Error **errp)
Alberto Garcia9c16fa72013-01-11 18:25:29 +010058{
Andreas Färber5c570902013-08-01 18:45:02 +020059 IPackDevice *idev = IPACK_DEVICE(dev);
Alberto Garcia9c16fa72013-01-11 18:25:29 +010060 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(dev);
Andreas Färber5c570902013-08-01 18:45:02 +020061 Error *err = NULL;
Alberto Garcia9c16fa72013-01-11 18:25:29 +010062
Andreas Färber5c570902013-08-01 18:45:02 +020063 if (k->unrealize) {
64 k->unrealize(dev, &err);
65 error_propagate(errp, err);
66 return;
Alberto Garcia9c16fa72013-01-11 18:25:29 +010067 }
68
Peter Crosthwaitef173d572014-06-18 00:56:31 -070069 qemu_free_irqs(idev->irq, 2);
Alberto Garcia9c16fa72013-01-11 18:25:29 +010070}
71
72static Property ipack_device_props[] = {
73 DEFINE_PROP_INT32("slot", IPackDevice, slot, -1),
74 DEFINE_PROP_END_OF_LIST()
75};
76
77static void ipack_device_class_init(ObjectClass *klass, void *data)
78{
79 DeviceClass *k = DEVICE_CLASS(klass);
Andreas Färber5c570902013-08-01 18:45:02 +020080
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +030081 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
Alberto Garcia9c16fa72013-01-11 18:25:29 +010082 k->bus_type = TYPE_IPACK_BUS;
Andreas Färber5c570902013-08-01 18:45:02 +020083 k->realize = ipack_device_realize;
84 k->unrealize = ipack_device_unrealize;
Alberto Garcia9c16fa72013-01-11 18:25:29 +010085 k->props = ipack_device_props;
86}
87
88const VMStateDescription vmstate_ipack_device = {
89 .name = "ipack_device",
90 .version_id = 1,
91 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +020092 .fields = (VMStateField[]) {
Alberto Garcia9c16fa72013-01-11 18:25:29 +010093 VMSTATE_INT32(slot, IPackDevice),
94 VMSTATE_END_OF_LIST()
95 }
96};
97
98static 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
107static const TypeInfo ipack_bus_info = {
108 .name = TYPE_IPACK_BUS,
109 .parent = TYPE_BUS,
110 .instance_size = sizeof(IPackBus),
111};
112
113static void ipack_register_types(void)
114{
115 type_register_static(&ipack_device_info);
116 type_register_static(&ipack_bus_info);
117}
118
119type_init(ipack_register_types)