xref: /qemu/hw/audio/virtio-snd-pci.c (revision 940bb5fa)
1 /*
2  * VIRTIO Sound Device PCI Bindings
3  *
4  * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or
7  * (at your option) any later version.  See the COPYING file in the
8  * top-level directory.
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qom/object.h"
13 #include "qapi/error.h"
14 #include "hw/audio/soundhw.h"
15 #include "hw/virtio/virtio-pci.h"
16 #include "hw/audio/virtio-snd.h"
17 
18 /*
19  * virtio-snd-pci: This extends VirtioPCIProxy.
20  */
21 #define TYPE_VIRTIO_SND_PCI "virtio-sound-pci"
22 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSoundPCI, VIRTIO_SND_PCI)
23 
24 struct VirtIOSoundPCI {
25     VirtIOPCIProxy parent_obj;
26 
27     VirtIOSound vdev;
28 };
29 
30 static Property virtio_snd_pci_properties[] = {
31     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
32                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
33     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
34     DEFINE_PROP_END_OF_LIST(),
35 };
36 
37 static void virtio_snd_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
38 {
39     VirtIOSoundPCI *dev = VIRTIO_SND_PCI(vpci_dev);
40     DeviceState *vdev = DEVICE(&dev->vdev);
41 
42     virtio_pci_force_virtio_1(vpci_dev);
43     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
44 }
45 
46 static void virtio_snd_pci_class_init(ObjectClass *klass, void *data)
47 {
48     DeviceClass *dc = DEVICE_CLASS(klass);
49     VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
50 
51     device_class_set_props(dc, virtio_snd_pci_properties);
52     dc->desc = "Virtio Sound";
53     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
54 
55     vpciklass->realize = virtio_snd_pci_realize;
56 }
57 
58 static void virtio_snd_pci_instance_init(Object *obj)
59 {
60     VirtIOSoundPCI *dev = VIRTIO_SND_PCI(obj);
61 
62     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
63                                 TYPE_VIRTIO_SND);
64 }
65 
66 static const VirtioPCIDeviceTypeInfo virtio_snd_pci_info = {
67     .generic_name  = TYPE_VIRTIO_SND_PCI,
68     .instance_size = sizeof(VirtIOSoundPCI),
69     .instance_init = virtio_snd_pci_instance_init,
70     .class_init    = virtio_snd_pci_class_init,
71 };
72 
73 /* Create a Virtio Sound PCI device, so '-audio driver,model=virtio' works. */
74 static int virtio_snd_pci_init(PCIBus *bus, const char *audiodev)
75 {
76     DeviceState *vdev = NULL;
77     VirtIOSoundPCI *dev = NULL;
78 
79     vdev = qdev_new(TYPE_VIRTIO_SND_PCI);
80     assert(vdev);
81     dev = VIRTIO_SND_PCI(vdev);
82     qdev_prop_set_string(DEVICE(&dev->vdev), "audiodev", audiodev);
83     qdev_realize_and_unref(vdev, BUS(bus), &error_fatal);
84     return 0;
85 }
86 
87 static void virtio_snd_pci_register(void)
88 {
89     virtio_pci_types_register(&virtio_snd_pci_info);
90     pci_register_soundhw("virtio", "Virtio Sound", virtio_snd_pci_init);
91 }
92 
93 type_init(virtio_snd_pci_register);
94