xref: /qemu/hw/virtio/vhost-user-snd.c (revision 6ff5da16000f908140723e164d33a0b51a6c4162)
1 /*
2  * Vhost-user snd virtio device
3  *
4  * Copyright (c) 2023 Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
5  *
6  * Simple wrapper of the generic vhost-user-device.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "hw/qdev-properties.h"
14 #include "hw/virtio/virtio-bus.h"
15 #include "hw/virtio/vhost-user-snd.h"
16 #include "standard-headers/linux/virtio_ids.h"
17 #include "standard-headers/linux/virtio_snd.h"
18 
19 static const VirtIOFeature feature_sizes[] = {
20     {.flags = 1ULL << VIRTIO_SND_F_CTLS,
21     .end = endof(struct virtio_snd_config, controls)},
22     {}
23 };
24 
25 static const VirtIOConfigSizeParams cfg_size_params = {
26     .min_size = endof(struct virtio_snd_config, chmaps),
27     .max_size = sizeof(struct virtio_snd_config),
28     .feature_sizes = feature_sizes
29 };
30 
31 static const VMStateDescription vu_snd_vmstate = {
32     .name = "vhost-user-snd",
33     .unmigratable = 1,
34 };
35 
36 static const Property vsnd_properties[] = {
37     DEFINE_PROP_CHR("chardev", VHostUserBase, chardev),
38     DEFINE_PROP_BIT64("controls", VHostUserBase,
39                       parent_obj.host_features, VIRTIO_SND_F_CTLS, false),
40 };
41 
42 static void vu_snd_base_realize(DeviceState *dev, Error **errp)
43 {
44     VHostUserBase *vub = VHOST_USER_BASE(dev);
45     VHostUserBaseClass *vubs = VHOST_USER_BASE_GET_CLASS(dev);
46     VirtIODevice *vdev = &vub->parent_obj;
47 
48     vub->virtio_id = VIRTIO_ID_SOUND;
49     vub->num_vqs = 4;
50     vub->config_size = virtio_get_config_size(&cfg_size_params,
51                                               vdev->host_features);
52     vub->vq_size = 64;
53 
54     vubs->parent_realize(dev, errp);
55 }
56 
57 static void vu_snd_class_init(ObjectClass *klass, void *data)
58 {
59     DeviceClass *dc = DEVICE_CLASS(klass);
60     VHostUserBaseClass *vubc = VHOST_USER_BASE_CLASS(klass);
61 
62     dc->vmsd = &vu_snd_vmstate;
63     device_class_set_props(dc, vsnd_properties);
64     device_class_set_parent_realize(dc, vu_snd_base_realize,
65                                     &vubc->parent_realize);
66 
67     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
68 }
69 
70 static const TypeInfo vu_snd_info = {
71     .name = TYPE_VHOST_USER_SND,
72     .parent = TYPE_VHOST_USER_BASE,
73     .instance_size = sizeof(VHostUserSound),
74     .class_init = vu_snd_class_init,
75 };
76 
77 static void vu_snd_register_types(void)
78 {
79     type_register_static(&vu_snd_info);
80 }
81 
82 type_init(vu_snd_register_types)
83