hw/i386/vmmouse: Fix crash when using the vmmouse on a machine without vmport
[qemu/kevin.git] / hw / ssi / ssi.c
blobc6415eb6e329ee78f822f67231921e08646f124d
1 /*
2 * QEMU Synchronous Serial Interface support
4 * Copyright (c) 2009 CodeSourcery.
5 * Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com)
6 * Copyright (c) 2012 PetaLogix Pty Ltd.
7 * Written by Paul Brook
9 * This code is licensed under the GNU GPL v2.
11 * Contributions after 2012-01-13 are licensed under the terms of the
12 * GNU GPL, version 2 or (at your option) any later version.
15 #include "qemu/osdep.h"
16 #include "hw/ssi/ssi.h"
17 #include "migration/vmstate.h"
18 #include "qemu/module.h"
20 struct SSIBus {
21 BusState parent_obj;
24 #define TYPE_SSI_BUS "SSI"
25 #define SSI_BUS(obj) OBJECT_CHECK(SSIBus, (obj), TYPE_SSI_BUS)
27 static const TypeInfo ssi_bus_info = {
28 .name = TYPE_SSI_BUS,
29 .parent = TYPE_BUS,
30 .instance_size = sizeof(SSIBus),
33 static void ssi_cs_default(void *opaque, int n, int level)
35 SSISlave *s = SSI_SLAVE(opaque);
36 bool cs = !!level;
37 assert(n == 0);
38 if (s->cs != cs) {
39 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
40 if (ssc->set_cs) {
41 ssc->set_cs(s, cs);
44 s->cs = cs;
47 static uint32_t ssi_transfer_raw_default(SSISlave *dev, uint32_t val)
49 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(dev);
51 if ((dev->cs && ssc->cs_polarity == SSI_CS_HIGH) ||
52 (!dev->cs && ssc->cs_polarity == SSI_CS_LOW) ||
53 ssc->cs_polarity == SSI_CS_NONE) {
54 return ssc->transfer(dev, val);
56 return 0;
59 static void ssi_slave_realize(DeviceState *dev, Error **errp)
61 SSISlave *s = SSI_SLAVE(dev);
62 SSISlaveClass *ssc = SSI_SLAVE_GET_CLASS(s);
64 if (ssc->transfer_raw == ssi_transfer_raw_default &&
65 ssc->cs_polarity != SSI_CS_NONE) {
66 qdev_init_gpio_in_named(dev, ssi_cs_default, SSI_GPIO_CS, 1);
69 ssc->realize(s, errp);
72 static void ssi_slave_class_init(ObjectClass *klass, void *data)
74 SSISlaveClass *ssc = SSI_SLAVE_CLASS(klass);
75 DeviceClass *dc = DEVICE_CLASS(klass);
77 dc->realize = ssi_slave_realize;
78 dc->bus_type = TYPE_SSI_BUS;
79 if (!ssc->transfer_raw) {
80 ssc->transfer_raw = ssi_transfer_raw_default;
84 static const TypeInfo ssi_slave_info = {
85 .name = TYPE_SSI_SLAVE,
86 .parent = TYPE_DEVICE,
87 .class_init = ssi_slave_class_init,
88 .class_size = sizeof(SSISlaveClass),
89 .abstract = true,
92 DeviceState *ssi_create_slave_no_init(SSIBus *bus, const char *name)
94 return qdev_create(BUS(bus), name);
97 DeviceState *ssi_create_slave(SSIBus *bus, const char *name)
99 DeviceState *dev = ssi_create_slave_no_init(bus, name);
101 qdev_init_nofail(dev);
102 return dev;
105 SSIBus *ssi_create_bus(DeviceState *parent, const char *name)
107 BusState *bus;
108 bus = qbus_create(TYPE_SSI_BUS, parent, name);
109 return SSI_BUS(bus);
112 uint32_t ssi_transfer(SSIBus *bus, uint32_t val)
114 BusState *b = BUS(bus);
115 BusChild *kid;
116 SSISlaveClass *ssc;
117 uint32_t r = 0;
119 QTAILQ_FOREACH(kid, &b->children, sibling) {
120 SSISlave *slave = SSI_SLAVE(kid->child);
121 ssc = SSI_SLAVE_GET_CLASS(slave);
122 r |= ssc->transfer_raw(slave, val);
125 return r;
128 const VMStateDescription vmstate_ssi_slave = {
129 .name = "SSISlave",
130 .version_id = 1,
131 .minimum_version_id = 1,
132 .fields = (VMStateField[]) {
133 VMSTATE_BOOL(cs, SSISlave),
134 VMSTATE_END_OF_LIST()
138 static void ssi_slave_register_types(void)
140 type_register_static(&ssi_bus_info);
141 type_register_static(&ssi_slave_info);
144 type_init(ssi_slave_register_types)
146 typedef struct SSIAutoConnectArg {
147 qemu_irq **cs_linep;
148 SSIBus *bus;
149 } SSIAutoConnectArg;
151 static int ssi_auto_connect_slave(Object *child, void *opaque)
153 SSIAutoConnectArg *arg = opaque;
154 SSISlave *dev = (SSISlave *)object_dynamic_cast(child, TYPE_SSI_SLAVE);
155 qemu_irq cs_line;
157 if (!dev) {
158 return 0;
161 cs_line = qdev_get_gpio_in_named(DEVICE(dev), SSI_GPIO_CS, 0);
162 qdev_set_parent_bus(DEVICE(dev), BUS(arg->bus));
163 **arg->cs_linep = cs_line;
164 (*arg->cs_linep)++;
165 return 0;
168 void ssi_auto_connect_slaves(DeviceState *parent, qemu_irq *cs_line,
169 SSIBus *bus)
171 SSIAutoConnectArg arg = {
172 .cs_linep = &cs_line,
173 .bus = bus
176 object_child_foreach(OBJECT(parent), ssi_auto_connect_slave, &arg);