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 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 DEFINE_PROP_END_OF_LIST(),
41 };
42
vu_snd_base_realize(DeviceState * dev,Error ** errp)43 static void vu_snd_base_realize(DeviceState *dev, Error **errp)
44 {
45 VHostUserBase *vub = VHOST_USER_BASE(dev);
46 VHostUserBaseClass *vubs = VHOST_USER_BASE_GET_CLASS(dev);
47 VirtIODevice *vdev = &vub->parent_obj;
48
49 vub->virtio_id = VIRTIO_ID_SOUND;
50 vub->num_vqs = 4;
51 vub->config_size = virtio_get_config_size(&cfg_size_params,
52 vdev->host_features);
53 vub->vq_size = 64;
54
55 vubs->parent_realize(dev, errp);
56 }
57
vu_snd_class_init(ObjectClass * klass,void * data)58 static void vu_snd_class_init(ObjectClass *klass, void *data)
59 {
60 DeviceClass *dc = DEVICE_CLASS(klass);
61 VHostUserBaseClass *vubc = VHOST_USER_BASE_CLASS(klass);
62
63 dc->vmsd = &vu_snd_vmstate;
64 device_class_set_props(dc, vsnd_properties);
65 device_class_set_parent_realize(dc, vu_snd_base_realize,
66 &vubc->parent_realize);
67
68 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
69 }
70
71 static const TypeInfo vu_snd_info = {
72 .name = TYPE_VHOST_USER_SND,
73 .parent = TYPE_VHOST_USER_BASE,
74 .instance_size = sizeof(VHostUserSound),
75 .class_init = vu_snd_class_init,
76 };
77
vu_snd_register_types(void)78 static void vu_snd_register_types(void)
79 {
80 type_register_static(&vu_snd_info);
81 }
82
83 type_init(vu_snd_register_types)
84