4 * Copyright (C) 2012 : GreenSocs Ltd
5 * http://www.greensocs.com/ , email: info@greensocs.com
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 "qemu/osdep.h"
27 #include "qemu/error-report.h"
29 #include "hw/virtio/virtio-bus.h"
30 #include "hw/virtio/virtio.h"
32 /* #define DEBUG_VIRTIO_BUS */
34 #ifdef DEBUG_VIRTIO_BUS
35 #define DPRINTF(fmt, ...) \
36 do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
38 #define DPRINTF(fmt, ...) do { } while (0)
41 /* A VirtIODevice is being plugged */
42 void virtio_bus_device_plugged(VirtIODevice
*vdev
, Error
**errp
)
44 DeviceState
*qdev
= DEVICE(vdev
);
45 BusState
*qbus
= BUS(qdev_get_parent_bus(qdev
));
46 VirtioBusState
*bus
= VIRTIO_BUS(qbus
);
47 VirtioBusClass
*klass
= VIRTIO_BUS_GET_CLASS(bus
);
48 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
50 DPRINTF("%s: plug device.\n", qbus
->name
);
52 if (klass
->device_plugged
!= NULL
) {
53 klass
->device_plugged(qbus
->parent
, errp
);
56 /* Get the features of the plugged device. */
57 assert(vdc
->get_features
!= NULL
);
58 vdev
->host_features
= vdc
->get_features(vdev
, vdev
->host_features
,
60 if (klass
->post_plugged
!= NULL
) {
61 klass
->post_plugged(qbus
->parent
, errp
);
65 /* Reset the virtio_bus */
66 void virtio_bus_reset(VirtioBusState
*bus
)
68 VirtIODevice
*vdev
= virtio_bus_get_device(bus
);
70 DPRINTF("%s: reset device.\n", BUS(bus
)->name
);
76 /* A VirtIODevice is being unplugged */
77 void virtio_bus_device_unplugged(VirtIODevice
*vdev
)
79 DeviceState
*qdev
= DEVICE(vdev
);
80 BusState
*qbus
= BUS(qdev_get_parent_bus(qdev
));
81 VirtioBusClass
*klass
= VIRTIO_BUS_GET_CLASS(qbus
);
83 DPRINTF("%s: remove device.\n", qbus
->name
);
86 if (klass
->device_unplugged
!= NULL
) {
87 klass
->device_unplugged(qbus
->parent
);
92 /* Get the device id of the plugged device. */
93 uint16_t virtio_bus_get_vdev_id(VirtioBusState
*bus
)
95 VirtIODevice
*vdev
= virtio_bus_get_device(bus
);
97 return vdev
->device_id
;
100 /* Get the config_len field of the plugged device. */
101 size_t virtio_bus_get_vdev_config_len(VirtioBusState
*bus
)
103 VirtIODevice
*vdev
= virtio_bus_get_device(bus
);
104 assert(vdev
!= NULL
);
105 return vdev
->config_len
;
108 /* Get bad features of the plugged device. */
109 uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState
*bus
)
111 VirtIODevice
*vdev
= virtio_bus_get_device(bus
);
112 VirtioDeviceClass
*k
;
114 assert(vdev
!= NULL
);
115 k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
116 if (k
->bad_features
!= NULL
) {
117 return k
->bad_features(vdev
);
123 /* Get config of the plugged device. */
124 void virtio_bus_get_vdev_config(VirtioBusState
*bus
, uint8_t *config
)
126 VirtIODevice
*vdev
= virtio_bus_get_device(bus
);
127 VirtioDeviceClass
*k
;
129 assert(vdev
!= NULL
);
130 k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
131 if (k
->get_config
!= NULL
) {
132 k
->get_config(vdev
, config
);
136 /* Set config of the plugged device. */
137 void virtio_bus_set_vdev_config(VirtioBusState
*bus
, uint8_t *config
)
139 VirtIODevice
*vdev
= virtio_bus_get_device(bus
);
140 VirtioDeviceClass
*k
;
142 assert(vdev
!= NULL
);
143 k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
144 if (k
->set_config
!= NULL
) {
145 k
->set_config(vdev
, config
);
149 static char *virtio_bus_get_dev_path(DeviceState
*dev
)
151 BusState
*bus
= qdev_get_parent_bus(dev
);
152 DeviceState
*proxy
= DEVICE(bus
->parent
);
153 return qdev_get_dev_path(proxy
);
156 static char *virtio_bus_get_fw_dev_path(DeviceState
*dev
)
161 static void virtio_bus_class_init(ObjectClass
*klass
, void *data
)
163 BusClass
*bus_class
= BUS_CLASS(klass
);
164 bus_class
->get_dev_path
= virtio_bus_get_dev_path
;
165 bus_class
->get_fw_dev_path
= virtio_bus_get_fw_dev_path
;
168 static const TypeInfo virtio_bus_info
= {
169 .name
= TYPE_VIRTIO_BUS
,
171 .instance_size
= sizeof(VirtioBusState
),
173 .class_size
= sizeof(VirtioBusClass
),
174 .class_init
= virtio_bus_class_init
177 static void virtio_register_types(void)
179 type_register_static(&virtio_bus_info
);
182 type_init(virtio_register_types
)