virtio-bus: remove vdev field
[qemu/cris-port.git] / hw / virtio / virtio-bus.c
blob17dd06e1a18d87ac2e6f88588d7ddf9b3ed93d9b
1 /*
2 * VirtioBus
4 * Copyright (C) 2012 : GreenSocs Ltd
5 * http://www.greensocs.com/ , email: info@greensocs.com
7 * Developed by :
8 * Frederic Konrad <fred.konrad@greensocs.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "hw/hw.h"
26 #include "qemu/error-report.h"
27 #include "hw/qdev.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/virtio/virtio.h"
31 /* #define DEBUG_VIRTIO_BUS */
33 #ifdef DEBUG_VIRTIO_BUS
34 #define DPRINTF(fmt, ...) \
35 do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
36 #else
37 #define DPRINTF(fmt, ...) do { } while (0)
38 #endif
40 /* Plug the VirtIODevice */
41 int virtio_bus_plug_device(VirtIODevice *vdev)
43 DeviceState *qdev = DEVICE(vdev);
44 BusState *qbus = BUS(qdev_get_parent_bus(qdev));
45 VirtioBusState *bus = VIRTIO_BUS(qbus);
46 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
47 DPRINTF("%s: plug device.\n", qbus->name);
49 if (klass->device_plugged != NULL) {
50 klass->device_plugged(qbus->parent);
53 return 0;
56 /* Reset the virtio_bus */
57 void virtio_bus_reset(VirtioBusState *bus)
59 VirtIODevice *vdev = virtio_bus_get_device(bus);
61 DPRINTF("%s: reset device.\n", qbus->name);
62 if (vdev != NULL) {
63 virtio_reset(vdev);
67 /* Destroy the VirtIODevice */
68 void virtio_bus_destroy_device(VirtioBusState *bus)
70 BusState *qbus = BUS(bus);
71 VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
72 VirtIODevice *vdev = virtio_bus_get_device(bus);
74 DPRINTF("%s: remove device.\n", qbus->name);
76 if (vdev != NULL) {
77 if (klass->device_unplug != NULL) {
78 klass->device_unplug(qbus->parent);
80 object_unparent(OBJECT(vdev));
84 /* Get the device id of the plugged device. */
85 uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
87 VirtIODevice *vdev = virtio_bus_get_device(bus);
88 assert(vdev != NULL);
89 return vdev->device_id;
92 /* Get the config_len field of the plugged device. */
93 size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
95 VirtIODevice *vdev = virtio_bus_get_device(bus);
96 assert(vdev != NULL);
97 return vdev->config_len;
100 /* Get the features of the plugged device. */
101 uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
102 uint32_t requested_features)
104 VirtIODevice *vdev = virtio_bus_get_device(bus);
105 VirtioDeviceClass *k;
107 assert(vdev != NULL);
108 k = VIRTIO_DEVICE_GET_CLASS(vdev);
109 assert(k->get_features != NULL);
110 return k->get_features(vdev, requested_features);
113 /* Set the features of the plugged device. */
114 void virtio_bus_set_vdev_features(VirtioBusState *bus,
115 uint32_t requested_features)
117 VirtIODevice *vdev = virtio_bus_get_device(bus);
118 VirtioDeviceClass *k;
120 assert(vdev != NULL);
121 k = VIRTIO_DEVICE_GET_CLASS(vdev);
122 if (k->set_features != NULL) {
123 k->set_features(vdev, requested_features);
127 /* Get bad features of the plugged device. */
128 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
130 VirtIODevice *vdev = virtio_bus_get_device(bus);
131 VirtioDeviceClass *k;
133 assert(vdev != NULL);
134 k = VIRTIO_DEVICE_GET_CLASS(vdev);
135 if (k->bad_features != NULL) {
136 return k->bad_features(vdev);
137 } else {
138 return 0;
142 /* Get config of the plugged device. */
143 void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
145 VirtIODevice *vdev = virtio_bus_get_device(bus);
146 VirtioDeviceClass *k;
148 assert(vdev != NULL);
149 k = VIRTIO_DEVICE_GET_CLASS(vdev);
150 if (k->get_config != NULL) {
151 k->get_config(vdev, config);
155 /* Set config of the plugged device. */
156 void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config)
158 VirtIODevice *vdev = virtio_bus_get_device(bus);
159 VirtioDeviceClass *k;
161 assert(vdev != NULL);
162 k = VIRTIO_DEVICE_GET_CLASS(vdev);
163 if (k->set_config != NULL) {
164 k->set_config(vdev, config);
168 static char *virtio_bus_get_dev_path(DeviceState *dev)
170 BusState *bus = qdev_get_parent_bus(dev);
171 DeviceState *proxy = DEVICE(bus->parent);
172 return qdev_get_dev_path(proxy);
175 static char *virtio_bus_get_fw_dev_path(DeviceState *dev)
177 return NULL;
180 static void virtio_bus_class_init(ObjectClass *klass, void *data)
182 BusClass *bus_class = BUS_CLASS(klass);
183 bus_class->get_dev_path = virtio_bus_get_dev_path;
184 bus_class->get_fw_dev_path = virtio_bus_get_fw_dev_path;
187 static const TypeInfo virtio_bus_info = {
188 .name = TYPE_VIRTIO_BUS,
189 .parent = TYPE_BUS,
190 .instance_size = sizeof(VirtioBusState),
191 .abstract = true,
192 .class_size = sizeof(VirtioBusClass),
193 .class_init = virtio_bus_class_init
196 static void virtio_register_types(void)
198 type_register_static(&virtio_bus_info);
201 type_init(virtio_register_types)