diff --git a/hw/audio/intel-hda.c b/hw/audio/intel-hda.c
index a363993..5f18595 100644
--- a/hw/audio/intel-hda.c
+++ b/hw/audio/intel-hda.c
@@ -1327,7 +1327,7 @@ static void hda_codec_device_class_init(ObjectClass *klass, void *data)
     k->realize = hda_codec_dev_realize;
     k->exit = hda_codec_dev_exit;
     set_bit(DEVICE_CATEGORY_SOUND, k->categories);
-    k->bus_type = TYPE_HDA_BUS;
+    device_class_set_bus_type(k, TYPE_HDA_BUS);
     k->props = hda_props;
 }
 
diff --git a/hw/block/fdc.c b/hw/block/fdc.c
index 09bfdda..da655a6 100644
--- a/hw/block/fdc.c
+++ b/hw/block/fdc.c
@@ -593,7 +593,7 @@ static void floppy_drive_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
     k->init = floppy_drive_init;
     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
-    k->bus_type = TYPE_FLOPPY_BUS;
+    device_class_set_bus_type(k, TYPE_FLOPPY_BUS);
     k->props = floppy_drive_properties;
     k->desc = "virtual floppy drive";
 }
diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c
index 215e180..ec0906a 100644
--- a/hw/char/virtio-serial-bus.c
+++ b/hw/char/virtio-serial-bus.c
@@ -1089,7 +1089,7 @@ static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
 
     set_bit(DEVICE_CATEGORY_INPUT, k->categories);
-    k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
+    device_class_set_bus_type(k, TYPE_VIRTIO_SERIAL_BUS);
     k->realize = virtser_port_device_realize;
     k->unrealize = virtser_port_device_unrealize;
     k->props = virtser_props;
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 5783442..65ed738 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -1080,6 +1080,14 @@ static void device_finalize(Object *obj)
     }
 }
 
+void device_class_set_bus_type(DeviceClass *dc, const char *bus_type)
+{
+    BusClass *bc = BUS_CLASS(object_class_by_name(bus_type));
+
+    assert(object_class_dynamic_cast(OBJECT_CLASS(dc), bc->device_type));
+    dc->bus_type = bus_type;
+}
+
 static void device_class_base_init(ObjectClass *class, void *data)
 {
     DeviceClass *klass = DEVICE_CLASS(class);
diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index c065eb8..ee3b768 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -326,7 +326,7 @@ static void sysbus_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
     k->init = sysbus_device_init;
-    k->bus_type = TYPE_SYSTEM_BUS;
+    device_class_set_bus_type(k, TYPE_SYSTEM_BUS);
 }
 
 static const TypeInfo sysbus_device_type_info = {
diff --git a/hw/i2c/core.c b/hw/i2c/core.c
index 81b6ae4..66254ae 100644
--- a/hw/i2c/core.c
+++ b/hw/i2c/core.c
@@ -285,7 +285,7 @@ static void i2c_slave_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
     k->init = i2c_slave_qdev_init;
     set_bit(DEVICE_CATEGORY_MISC, k->categories);
-    k->bus_type = TYPE_I2C_BUS;
+    device_class_set_bus_type(k, TYPE_I2C_BUS);
     k->props = i2c_props;
 }
 
diff --git a/hw/ide/qdev.c b/hw/ide/qdev.c
index b526f85..35a65a0 100644
--- a/hw/ide/qdev.c
+++ b/hw/ide/qdev.c
@@ -365,7 +365,7 @@ static void ide_device_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
     k->init = ide_qdev_init;
     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
-    k->bus_type = TYPE_IDE_BUS;
+    device_class_set_bus_type(k, TYPE_IDE_BUS);
     k->unrealize = idebus_unrealize;
     k->props = ide_props;
 }
diff --git a/hw/input/adb.c b/hw/input/adb.c
index c6a2b31..0e64b1c 100644
--- a/hw/input/adb.c
+++ b/hw/input/adb.c
@@ -162,7 +162,7 @@ static void adb_device_class_init(ObjectClass *oc, void *data)
     DeviceClass *dc = DEVICE_CLASS(oc);
 
     dc->realize = adb_device_realizefn;
-    dc->bus_type = TYPE_ADB_BUS;
+    device_class_set_bus_type(dc, TYPE_ADB_BUS);
 }
 
 static const TypeInfo adb_device_type_info = {
diff --git a/hw/ipack/ipack.c b/hw/ipack/ipack.c
index f22c504..8a7fb63 100644
--- a/hw/ipack/ipack.c
+++ b/hw/ipack/ipack.c
@@ -81,7 +81,7 @@ static void ipack_device_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
 
     set_bit(DEVICE_CATEGORY_INPUT, k->categories);
-    k->bus_type = TYPE_IPACK_BUS;
+    device_class_set_bus_type(k, TYPE_IPACK_BUS);
     k->realize = ipack_device_realize;
     k->unrealize = ipack_device_unrealize;
     k->props = ipack_device_props;
diff --git a/hw/isa/isa-bus.c b/hw/isa/isa-bus.c
index aad7669..f97fe52 100644
--- a/hw/isa/isa-bus.c
+++ b/hw/isa/isa-bus.c
@@ -233,7 +233,7 @@ static const TypeInfo isabus_bridge_info = {
 static void isa_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
-    k->bus_type = TYPE_ISA_BUS;
+    device_class_set_bus_type(k, TYPE_ISA_BUS);
 }
 
 static const TypeInfo isa_device_type_info = {
diff --git a/hw/misc/auxbus.c b/hw/misc/auxbus.c
index 4b0d565..b310dc4 100644
--- a/hw/misc/auxbus.c
+++ b/hw/misc/auxbus.c
@@ -272,7 +272,7 @@ static void aux_slave_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
 
     set_bit(DEVICE_CATEGORY_MISC, k->categories);
-    k->bus_type = TYPE_AUX_BUS;
+    device_class_set_bus_type(k, TYPE_AUX_BUS);
 }
 
 static const TypeInfo aux_slave_type_info = {
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 1b0b7d3..be8ed2e 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -2504,7 +2504,7 @@ static void pci_device_class_init(ObjectClass *klass, void *data)
 
     k->realize = pci_qdev_realize;
     k->unrealize = pci_qdev_unrealize;
-    k->bus_type = TYPE_PCI_BUS;
+    device_class_set_bus_type(k, TYPE_PCI_BUS);
     k->props = pci_props;
     pc->realize = pci_default_realize;
 }
diff --git a/hw/ppc/spapr_vio.c b/hw/ppc/spapr_vio.c
index c70b9b9..53cb87c 100644
--- a/hw/ppc/spapr_vio.c
+++ b/hw/ppc/spapr_vio.c
@@ -586,7 +586,7 @@ static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
     DeviceClass *k = DEVICE_CLASS(klass);
     k->realize = spapr_vio_busdev_realize;
     k->reset = spapr_vio_busdev_reset;
-    k->bus_type = TYPE_SPAPR_VIO_BUS;
+    device_class_set_bus_type(k, TYPE_SPAPR_VIO_BUS);
     k->props = spapr_vio_props;
 }
 
diff --git a/hw/s390x/event-facility.c b/hw/s390x/event-facility.c
index f2562ce..527ba36 100644
--- a/hw/s390x/event-facility.c
+++ b/hw/s390x/event-facility.c
@@ -426,7 +426,7 @@ static void event_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
 
-    dc->bus_type = TYPE_SCLP_EVENTS_BUS;
+    device_class_set_bus_type(dc, TYPE_SCLP_EVENTS_BUS);
     dc->realize = event_realize;
     dc->unrealize = event_unrealize;
 }
diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
index 7470fdd..a29c729 100644
--- a/hw/s390x/s390-pci-bus.c
+++ b/hw/s390x/s390-pci-bus.c
@@ -939,7 +939,7 @@ static void s390_pci_device_class_init(ObjectClass *klass, void *data)
 
     dc->desc = "zpci device";
     dc->reset = s390_pci_device_reset;
-    dc->bus_type = TYPE_S390_PCI_BUS;
+    device_class_set_bus_type(dc, TYPE_S390_PCI_BUS);
     dc->realize = s390_pci_device_realize;
     dc->props = s390_pci_device_properties;
 }
diff --git a/hw/s390x/virtio-ccw.c b/hw/s390x/virtio-ccw.c
index f5c1d98..b4ce290 100644
--- a/hw/s390x/virtio-ccw.c
+++ b/hw/s390x/virtio-ccw.c
@@ -1550,7 +1550,7 @@ static void virtio_ccw_device_class_init(ObjectClass *klass, void *data)
     k->unplug = virtio_ccw_busdev_unplug;
     dc->realize = virtio_ccw_busdev_realize;
     dc->exit = virtio_ccw_busdev_exit;
-    dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
+    device_class_set_bus_type(dc, TYPE_VIRTUAL_CSS_BUS);
 }
 
 static const TypeInfo virtio_ccw_device_info = {
diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c
index 9faf45f..b60079a 100644
--- a/hw/scsi/scsi-bus.c
+++ b/hw/scsi/scsi-bus.c
@@ -2067,7 +2067,7 @@ static void scsi_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
-    k->bus_type  = TYPE_SCSI_BUS;
+    device_class_set_bus_type(k, TYPE_SCSI_BUS);
     k->realize   = scsi_qdev_realize;
     k->unrealize = scsi_qdev_unrealize;
     k->props     = scsi_props;
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 8e88e83..b1e5bc6 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1917,7 +1917,7 @@ static void sd_class_init(ObjectClass *klass, void *data)
     dc->props = sd_properties;
     dc->vmsd = &sd_vmstate;
     dc->reset = sd_reset;
-    dc->bus_type = TYPE_SD_BUS;
+    device_class_set_bus_type(dc, TYPE_SD_BUS);
 
     sc->do_command = sd_do_command;
     sc->write_data = sd_write_data;
diff --git a/hw/ssi/ssi.c b/hw/ssi/ssi.c
index c48920d..b97d726 100644
--- a/hw/ssi/ssi.c
+++ b/hw/ssi/ssi.c
@@ -80,7 +80,7 @@ static void ssi_slave_class_init(ObjectClass *klass, void *data)
     DeviceClass *dc = DEVICE_CLASS(klass);
 
     dc->realize = ssi_slave_realize;
-    dc->bus_type = TYPE_SSI_BUS;
+    device_class_set_bus_type(dc, TYPE_SSI_BUS);
     if (!ssc->transfer_raw) {
         ssc->transfer_raw = ssi_transfer_raw_default;
     }
diff --git a/hw/usb/bus.c b/hw/usb/bus.c
index 2cb7fa4..978d9d5 100644
--- a/hw/usb/bus.c
+++ b/hw/usb/bus.c
@@ -789,7 +789,7 @@ static void usb_device_instance_init(Object *obj)
 static void usb_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
-    k->bus_type = TYPE_USB_BUS;
+    device_class_set_bus_type(k, TYPE_USB_BUS);
     k->realize  = usb_qdev_realize;
     k->unrealize = usb_qdev_unrealize;
     k->props    = usb_props;
diff --git a/hw/usb/dev-smartcard-reader.c b/hw/usb/dev-smartcard-reader.c
index 2721f0c..a7cf0f9 100644
--- a/hw/usb/dev-smartcard-reader.c
+++ b/hw/usb/dev-smartcard-reader.c
@@ -1487,7 +1487,7 @@ static const TypeInfo ccid_info = {
 static void ccid_card_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
-    k->bus_type = TYPE_CCID_BUS;
+    device_class_set_bus_type(k, TYPE_CCID_BUS);
     k->init = ccid_card_init;
     k->exit = ccid_card_exit;
     k->props = ccid_props;
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 1af2de2..9dfab8a 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -2247,7 +2247,7 @@ static void virtio_device_class_init(ObjectClass *klass, void *data)
 
     dc->realize = virtio_device_realize;
     dc->unrealize = virtio_device_unrealize;
-    dc->bus_type = TYPE_VIRTIO_BUS;
+    device_class_set_bus_type(dc, TYPE_VIRTIO_BUS);
     dc->props = virtio_properties;
     vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
     vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 7c34f14..2f82950 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -406,4 +406,6 @@ static inline bool qbus_is_hotpluggable(BusState *bus)
 void device_listener_register(DeviceListener *listener);
 void device_listener_unregister(DeviceListener *listener);
 
+void device_class_set_bus_type(DeviceClass *dc, const char *bus_type);
+
 #endif
