From: Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Emmanouil Pitsidianakis" <manos.pitsidianakis@linaro.org>,
"Igor Skalkin" <Igor.Skalkin@opensynergy.com>,
"Anton Yakovlev" <Anton.Yakovlev@opensynergy.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Gerd Hoffmann" <kraxel@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Volker Rümelin" <vr_qemu@t-online.de>,
"Kővágó, Zoltán" <DirtY.iCE.hu@gmail.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [PATCH v7 04/12] virtio-sound: set PCM stream parameters
Date: Tue, 22 Aug 2023 22:18:27 +0300 [thread overview]
Message-ID: <34afff149a90bf1f7be183aa3907fbfe3b96daba.1692731646.git.manos.pitsidianakis@linaro.org> (raw)
In-Reply-To: <cover.1692731646.git.manos.pitsidianakis@linaro.org>
This commit sets the virtio-snd device's default PCM parameters in
virtio_snd_pcm_set_params_impl(). The same function will be used to set
parameters from the guest with VIRTIO_SND_R_PCM_SET_PARAMS in a
follow-up commit.
PCM parameters describe the sound card parameters that the guest's
kernel sees as an ALSA device.
Signed-off-by: Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
---
hw/virtio/virtio-snd.c | 260 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 251 insertions(+), 9 deletions(-)
diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c
index b23f8040e1..dec76ffcb1 100644
--- a/hw/virtio/virtio-snd.c
+++ b/hw/virtio/virtio-snd.c
@@ -30,6 +30,29 @@
#define VIRTIO_SOUND_CHMAP_DEFAULT 0
#define VIRTIO_SOUND_HDA_FN_NID 0
+static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
+ | BIT(VIRTIO_SND_PCM_FMT_U8)
+ | BIT(VIRTIO_SND_PCM_FMT_S16)
+ | BIT(VIRTIO_SND_PCM_FMT_U16)
+ | BIT(VIRTIO_SND_PCM_FMT_S32)
+ | BIT(VIRTIO_SND_PCM_FMT_U32)
+ | BIT(VIRTIO_SND_PCM_FMT_FLOAT);
+
+static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512)
+ | BIT(VIRTIO_SND_PCM_RATE_8000)
+ | BIT(VIRTIO_SND_PCM_RATE_11025)
+ | BIT(VIRTIO_SND_PCM_RATE_16000)
+ | BIT(VIRTIO_SND_PCM_RATE_22050)
+ | BIT(VIRTIO_SND_PCM_RATE_32000)
+ | BIT(VIRTIO_SND_PCM_RATE_44100)
+ | BIT(VIRTIO_SND_PCM_RATE_48000)
+ | BIT(VIRTIO_SND_PCM_RATE_64000)
+ | BIT(VIRTIO_SND_PCM_RATE_88200)
+ | BIT(VIRTIO_SND_PCM_RATE_96000)
+ | BIT(VIRTIO_SND_PCM_RATE_176400)
+ | BIT(VIRTIO_SND_PCM_RATE_192000)
+ | BIT(VIRTIO_SND_PCM_RATE_384000);
+
static const char *print_code(uint32_t code)
{
#define CASE(CODE) \
@@ -110,6 +133,206 @@ virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config)
memcpy(&s->snd_conf, sndconfig, sizeof(s->snd_conf));
}
+/*
+ * Get params for a specific stream.
+ *
+ * @s: VirtIOSound device
+ * @stream_id: stream id
+ */
+static VirtIOSoundPCMParams *virtio_snd_pcm_get_params(VirtIOSound *s,
+ uint32_t stream_id)
+{
+ return stream_id >= s->snd_conf.streams ? NULL
+ : &s->pcm->pcm_params[stream_id];
+}
+
+/*
+ * Set the given stream params.
+ * Called by both virtio_snd_handle_pcm_set_params and during device
+ * initialization.
+ * Returns the response status code. (VIRTIO_SND_S_*).
+ *
+ * @s: VirtIOSound device
+ * @params: The PCM params as defined in the virtio specification
+ */
+static
+uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s,
+ virtio_snd_pcm_set_params *params)
+{
+ VirtIOSoundPCMParams *st_params;
+ uint32_t stream_id = params->hdr.stream_id;
+
+ if (stream_id >= s->snd_conf.streams || !(s->pcm->pcm_params)) {
+ virtio_error(VIRTIO_DEVICE(s), "Streams have not been initialized.\n");
+ return VIRTIO_SND_S_BAD_MSG;
+ }
+
+ st_params = virtio_snd_pcm_get_params(s, stream_id);
+
+ if (params->channels < 1 || params->channels > AUDIO_MAX_CHANNELS) {
+ error_report("Number of channels is not supported.");
+ return VIRTIO_SND_S_NOT_SUPP;
+ }
+ if (!(supported_formats & BIT(params->format))) {
+ error_report("Stream format is not supported.");
+ return VIRTIO_SND_S_NOT_SUPP;
+ }
+ if (!(supported_rates & BIT(params->rate))) {
+ error_report("Stream rate is not supported.");
+ return VIRTIO_SND_S_NOT_SUPP;
+ }
+
+ st_params->buffer_bytes = params->buffer_bytes;
+ st_params->period_bytes = params->period_bytes;
+ st_params->features = params->features;
+ st_params->channels = params->channels;
+ st_params->format = params->format;
+ st_params->rate = params->rate;
+
+ return VIRTIO_SND_S_OK;
+}
+
+/*
+ * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
+ */
+static AudioFormat virtio_snd_get_qemu_format(uint32_t format)
+{
+ #define CASE(FMT) \
+ case VIRTIO_SND_PCM_FMT_##FMT: \
+ return AUDIO_FORMAT_##FMT;
+
+ switch (format) {
+ CASE(U8)
+ CASE(S8)
+ CASE(U16)
+ CASE(S16)
+ CASE(U32)
+ CASE(S32)
+ case VIRTIO_SND_PCM_FMT_FLOAT:
+ return AUDIO_FORMAT_F32;
+ default:
+ g_assert_not_reached();
+ }
+
+ #undef CASE
+}
+
+/*
+ * Get a QEMU Audiosystem compatible frequency value from a
+ * VIRTIO_SND_PCM_RATE_*
+ */
+static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
+{
+ #define CASE(RATE) \
+ case VIRTIO_SND_PCM_RATE_##RATE: \
+ return RATE;
+
+ switch (rate) {
+ CASE(5512)
+ CASE(8000)
+ CASE(11025)
+ CASE(16000)
+ CASE(22050)
+ CASE(32000)
+ CASE(44100)
+ CASE(48000)
+ CASE(64000)
+ CASE(88200)
+ CASE(96000)
+ CASE(176400)
+ CASE(192000)
+ CASE(384000)
+ default:
+ g_assert_not_reached();
+ }
+
+ #undef CASE
+}
+
+/*
+ * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
+ * params.
+ */
+static void virtio_snd_get_qemu_audsettings(audsettings *as,
+ VirtIOSoundPCMParams *params)
+{
+ as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
+ as->fmt = virtio_snd_get_qemu_format(params->format);
+ as->freq = virtio_snd_get_qemu_freq(params->rate);
+ as->endianness = AUDIO_HOST_ENDIANNESS;
+}
+
+/*
+ * Close a stream and free all its resources.
+ *
+ * @stream: VirtIOSoundPCMStream *stream
+ */
+static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
+{
+ if (stream) {
+ qemu_mutex_destroy(&stream->queue_mutex);
+ g_free(stream);
+ }
+}
+
+/*
+ * Prepares a VirtIOSound card stream.
+ * Returns the response status code. (VIRTIO_SND_S_*).
+ *
+ * @s: VirtIOSound device
+ * @stream_id: stream id
+ */
+static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
+{
+ audsettings as;
+ VirtIOSoundPCMParams *params;
+ VirtIOSoundPCMStream *stream;
+
+ if (!s->pcm->streams ||
+ !s->pcm->pcm_params ||
+ stream_id >= s->snd_conf.streams) {
+ return VIRTIO_SND_S_BAD_MSG;
+ }
+
+ params = virtio_snd_pcm_get_params(s, stream_id);
+ if (!params) {
+ return VIRTIO_SND_S_BAD_MSG;
+ }
+
+ virtio_snd_get_qemu_audsettings(&as, params);
+
+ virtio_snd_pcm_close(s->pcm->streams[stream_id]);
+
+ stream = g_new0(VirtIOSoundPCMStream, 1);
+
+ stream->id = stream_id;
+ stream->pcm = s->pcm;
+ stream->direction = stream_id < s->snd_conf.streams / 2 +
+ (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
+ stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
+ stream->features = 0;
+ stream->channels_min = 1;
+ stream->channels_max = as.nchannels;
+ stream->formats = supported_formats;
+ stream->rates = supported_rates;
+ stream->s = s;
+
+ stream->buffer_bytes = params->buffer_bytes;
+ stream->period_bytes = params->period_bytes;
+
+ stream->positions[0] = VIRTIO_SND_CHMAP_FL;
+ stream->positions[1] = VIRTIO_SND_CHMAP_FR;
+
+ stream->as = as;
+ stream->desired_as = stream->as;
+ qemu_mutex_init(&stream->queue_mutex);
+ QSIMPLEQ_INIT(&stream->queue);
+
+ s->pcm->streams[stream_id] = stream;
+
+ return VIRTIO_SND_S_OK;
+}
+
/*
* The actual processing done in virtio_snd_process_cmdq().
*
@@ -291,6 +514,8 @@ static void virtio_snd_common_realize(DeviceState *dev,
{
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
VirtIOSound *vsnd = VIRTIO_SND(dev);
+ virtio_snd_pcm_set_params default_params = { 0 };
+ uint32_t status;
virtio_snd_set_pcm(vsnd);
@@ -320,12 +545,37 @@ static void virtio_snd_common_realize(DeviceState *dev,
AUD_register_card("virtio-sound", &vsnd->card);
+ /* set default params for all streams */
+ default_params.features = 0;
+ default_params.buffer_bytes = 16384;
+ default_params.period_bytes = 4096;
+ default_params.channels = 2;
+ default_params.format = VIRTIO_SND_PCM_FMT_S16;
+ default_params.rate = VIRTIO_SND_PCM_RATE_48000;
vsnd->queues[VIRTIO_SND_VQ_CONTROL] = virtio_add_queue(vdev, 64, ctrl);
vsnd->queues[VIRTIO_SND_VQ_EVENT] = virtio_add_queue(vdev, 64, evt);
vsnd->queues[VIRTIO_SND_VQ_TX] = virtio_add_queue(vdev, 64, txq);
vsnd->queues[VIRTIO_SND_VQ_RX] = virtio_add_queue(vdev, 64, rxq);
qemu_mutex_init(&vsnd->cmdq_mutex);
QTAILQ_INIT(&vsnd->cmdq);
+
+ for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
+ default_params.hdr.stream_id = i;
+ status = virtio_snd_pcm_set_params_impl(vsnd, &default_params);
+ if (status != VIRTIO_SND_S_OK) {
+ error_setg(errp,
+ "Can't initalize stream params, device responded with %s.",
+ print_code(status));
+ return;
+ }
+ status = virtio_snd_pcm_prepare_impl(vsnd, i);
+ if (status != VIRTIO_SND_S_OK) {
+ error_setg(errp,
+ "Can't prepare streams, device responded with %s.",
+ print_code(status));
+ return;
+ }
+ }
}
static void
@@ -357,15 +607,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
errp);
}
-/*
- * Close the stream and free its resources.
- *
- * @stream: VirtIOSoundPCMStream *stream
- */
-static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
-{
-}
-
static void virtio_snd_unrealize(DeviceState *dev)
{
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -393,6 +634,7 @@ static void virtio_snd_unrealize(DeviceState *dev)
vsnd->pcm = NULL;
}
AUD_remove_card(&vsnd->card);
+ qemu_mutex_destroy(&vsnd->cmdq_mutex);
virtio_cleanup(vdev);
}
--
2.39.2
next prev parent reply other threads:[~2023-08-22 19:20 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-22 19:18 [PATCH v7 00/12] Add VIRTIO sound card Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 01/12] Add virtio-sound device stub Emmanouil Pitsidianakis
2023-08-24 17:44 ` Alex Bennée
2023-08-22 19:18 ` [PATCH v7 02/12] Add virtio-sound-pci device Emmanouil Pitsidianakis
2023-08-24 17:48 ` Alex Bennée
2023-08-22 19:18 ` [PATCH v7 03/12] virtio-sound: handle control messages and streams Emmanouil Pitsidianakis
2023-08-24 17:50 ` Alex Bennée
2023-08-22 19:18 ` Emmanouil Pitsidianakis [this message]
2023-08-22 19:18 ` [PATCH v7 05/12] virtio-sound: handle VIRTIO_SND_R_PCM_INFO request Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 06/12] virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP} Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 07/12] virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 08/12] virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 09/12] virtio-sound: handle VIRTIO_SND_PCM_RELEASE Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 10/12] virtio-sound: implement audio output (TX) Emmanouil Pitsidianakis
2023-08-22 22:17 ` Philippe Mathieu-Daudé
2023-08-23 8:42 ` Manos Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 11/12] virtio-sound: implement audio capture (RX) Emmanouil Pitsidianakis
2023-08-22 19:18 ` [PATCH v7 12/12] docs/system: add basic virtio-snd documentation Emmanouil Pitsidianakis
2023-08-24 18:44 ` [PATCH v7 00/12] Add VIRTIO sound card Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=34afff149a90bf1f7be183aa3907fbfe3b96daba.1692731646.git.manos.pitsidianakis@linaro.org \
--to=manos.pitsidianakis@linaro.org \
--cc=Anton.Yakovlev@opensynergy.com \
--cc=DirtY.iCE.hu@gmail.com \
--cc=Igor.Skalkin@opensynergy.com \
--cc=alex.bennee@linaro.org \
--cc=berrange@redhat.com \
--cc=eduardo@habkost.net \
--cc=kraxel@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=vr_qemu@t-online.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).