From patchwork Sat Jul 29 10:36:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333100 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2DB25C001DC for ; Sat, 29 Jul 2023 10:46:54 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLQ-0007pJ-74; Sat, 29 Jul 2023 06:38:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLO-0007p9-4I for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:42 -0400 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLL-000124-Ss for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:41 -0400 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3fbc244d384so29749295e9.0 for ; Sat, 29 Jul 2023 03:38:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627118; x=1691231918; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=z0rsg/4CHy+Ws6fVrneeBm7op2f0oHurRox9Ob3BVQI=; b=kMEDn7lwp94F5+AMHzCcHoz9Ud1ye28inTabaS9hhBvMNReRHuqGYNfZNn8pUOG/1A FbLlR7STo+k2eoAJKtGYz6Vfnkjs97Bl3vrgWklJgX4u2JVTUsC9vrTDZrJPjThNvRDe h/tg/efcLm1FnjMgj4WRhl0Dqk4iqBFaOlBX49ZV0YsZCGYpmyVM/BC3/uo9fCrzjuY9 /JqzHd0HVtI/CUeg1x4Rc+wBq+E4Nr+tdE/JII4LaSCRZzi+RVts9agj1K0F3SqmZKxL FVzIUlah2/vW8J1oMYulN+AzycI1L7jYeXYYd/mTnbW06NkiwuQaO2lgCoL42EsSGR1C 441w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627118; x=1691231918; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z0rsg/4CHy+Ws6fVrneeBm7op2f0oHurRox9Ob3BVQI=; b=fTpzif1SqzJih9GGS7F72QLEb/BjZ/aIx9Pe6auqSjJbQnZZawnf4YjPSY4k7lPkcz N1GzthM9sWsevwXZIn9PTdIqB20DVrXW8xkrAAdQWE7Ju+aPveaFT3lHTditj3NC+fW+ ik+PLlPI1EqJIOaBtBFkxS7k1B6b/5LrMdQBnLkK3as9KeM7ldN6l1H9gWdJ7FIxqaRo 6Um1HSkmRL08tD3niaSrzV3Y0Jm5h3bxJNkFbXI2zACBCaYcJZTjHjPYIcLuqNc8CL7M Y5aoLjCEHR4WZ7CrczOvPVxYIbhoSXiNPXfQWHjuhT9wzp+6wt1aATdY49tYOKmmSNDB OT6Q== X-Gm-Message-State: ABy/qLYMITxIUT8xmG8t4ceKQ8hnK6b69E9veOzmXpliPyNNtZF9FLZ5 kYme2Hu5XjU750sYCAe2RbcwckdoA5l9OCFQCOiQoQ== X-Google-Smtp-Source: APBJJlHh2dbudnKVCaxknAk0Hv48aBDUgu3MTAmlYvlMXO0t1lFVcemOhSSZ2FioVRJL/yAqiVPmsA== X-Received: by 2002:a1c:4c11:0:b0:3fe:19cf:93c9 with SMTP id z17-20020a1c4c11000000b003fe19cf93c9mr582599wmf.1.1690627118091; Sat, 29 Jul 2023 03:38:38 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:38:37 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 01/12] Add virtio-sound device stub Date: Sat, 29 Jul 2023 13:36:53 +0300 Message-Id: <899b9a06bbd00cbad30ffa13d44d11c660a0a650.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x32c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Add a new VIRTIO device for the virtio sound device id. Functionality will be added in the following commits. Signed-off-by: Emmanouil Pitsidianakis --- MAINTAINERS | 6 + hw/virtio/Kconfig | 5 + hw/virtio/meson.build | 1 + hw/virtio/trace-events | 9 ++ hw/virtio/virtio-snd.c | 231 +++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-snd.h | 80 ++++++++++++ 6 files changed, 332 insertions(+) create mode 100644 hw/virtio/virtio-snd.c create mode 100644 include/hw/virtio/virtio-snd.h diff --git a/MAINTAINERS b/MAINTAINERS index 12e59b6b27..2bed60f9c1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2245,6 +2245,12 @@ F: hw/virtio/virtio-mem-pci.h F: hw/virtio/virtio-mem-pci.c F: include/hw/virtio/virtio-mem.h +virtio-snd +M: Manos Pitsidianakis +S: Supported +F: hw/virtio/virtio-snd*.c +F: include/hw/virtio/virtio-snd.h + nvme M: Keith Busch M: Klaus Jensen diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig index 92c9cf6c96..d6f20657b3 100644 --- a/hw/virtio/Kconfig +++ b/hw/virtio/Kconfig @@ -17,6 +17,11 @@ config VIRTIO_PCI depends on PCI select VIRTIO +config VIRTIO_SND + bool + default y + depends on VIRTIO + config VIRTIO_MMIO bool select VIRTIO diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 13e7c6c272..120d4bfa0a 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -31,6 +31,7 @@ specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c')) specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c')) +specific_virtio_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c')) specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c')) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 7109cf1a3b..3ed7da35f2 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -154,3 +154,12 @@ virtio_pmem_flush_done(int type) "fsync return=%d" virtio_gpio_start(void) "start" virtio_gpio_stop(void) "stop" virtio_gpio_set_status(uint8_t status) "0x%x" + +#virtio-snd.c +virtio_snd_get_config(void *vdev, uint32_t jacks, uint32_t streams, uint32_t chmaps) "snd %p: get_config jacks=%"PRIu32" streams=%"PRIu32" chmaps=%"PRIu32"" +virtio_snd_set_config(void *vdev, uint32_t jacks, uint32_t new_jacks, uint32_t streams, uint32_t new_streams, uint32_t chmaps, uint32_t new_chmaps) "snd %p: set_config jacks from %"PRIu32"->%"PRIu32", streams from %"PRIu32"->%"PRIu32", chmaps from %"PRIu32"->%"PRIu32 +virtio_snd_get_features(void *vdev, uint64_t features) "snd %p: get_features 0x%"PRIx64 +virtio_snd_vm_state_running(void) "vm state running" +virtio_snd_vm_state_stopped(void) "vm state stopped" +virtio_snd_realize(void *snd) "snd %p: realize" +virtio_snd_unrealize(void *snd) "snd %p: unrealize" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c new file mode 100644 index 0000000000..f301054541 --- /dev/null +++ b/hw/virtio/virtio-snd.c @@ -0,0 +1,231 @@ +/* + * VIRTIO Sound Device conforming to + * + * "Virtual I/O Device (VIRTIO) Version 1.2 + * Committee Specification Draft 01 + * 09 May 2022" + * + * + * + * Copyright (c) 2023 Emmanouil Pitsidianakis + * Copyright (C) 2019 OpenSynergy GmbH + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ + +#include "qemu/osdep.h" +#include "qemu/iov.h" +#include "qemu/log.h" +#include "include/qemu/lockable.h" +#include "sysemu/runstate.h" +#include "trace.h" +#include "qapi/error.h" +#include "hw/virtio/virtio-snd.h" + +#define VIRTIO_SOUND_VM_VERSION 1 +#define VIRTIO_SOUND_JACK_DEFAULT 0 +#define VIRTIO_SOUND_STREAM_DEFAULT 1 +#define VIRTIO_SOUND_CHMAP_DEFAULT 0 +#define VIRTIO_SOUND_HDA_FN_NID 0 + +static const VMStateDescription vmstate_virtio_snd_device = { + .name = TYPE_VIRTIO_SND, + .version_id = VIRTIO_SOUND_VM_VERSION, + .minimum_version_id = VIRTIO_SOUND_VM_VERSION, +}; + +static const VMStateDescription vmstate_virtio_snd = { + .name = "virtio-sound", + .minimum_version_id = VIRTIO_SOUND_VM_VERSION, + .version_id = VIRTIO_SOUND_VM_VERSION, + .fields = (VMStateField[]) { + VMSTATE_VIRTIO_DEVICE, + VMSTATE_END_OF_LIST() + }, +}; + +static Property virtio_snd_properties[] = { + DEFINE_AUDIO_PROPERTIES(VirtIOSound, card), + DEFINE_PROP_UINT32("jacks", VirtIOSound, snd_conf.jacks, + VIRTIO_SOUND_JACK_DEFAULT), + DEFINE_PROP_UINT32("streams", VirtIOSound, snd_conf.streams, + VIRTIO_SOUND_STREAM_DEFAULT), + DEFINE_PROP_UINT32("chmaps", VirtIOSound, snd_conf.chmaps, + VIRTIO_SOUND_CHMAP_DEFAULT), + DEFINE_PROP_END_OF_LIST(), +}; + +static void +virtio_snd_get_config(VirtIODevice *vdev, uint8_t *config) +{ + VirtIOSound *s = VIRTIO_SND(vdev); + trace_virtio_snd_get_config(vdev, + s->snd_conf.jacks, + s->snd_conf.streams, + s->snd_conf.chmaps); + + memcpy(config, &s->snd_conf, sizeof(s->snd_conf)); +} + +static void +virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config) +{ + VirtIOSound *s = VIRTIO_SND(vdev); + const virtio_snd_config *sndconfig = + (const virtio_snd_config *)config; + + + trace_virtio_snd_set_config(vdev, + s->snd_conf.jacks, + sndconfig->jacks, + s->snd_conf.streams, + sndconfig->streams, + s->snd_conf.chmaps, + sndconfig->chmaps); + + memcpy(&s->snd_conf, sndconfig, sizeof(s->snd_conf)); +} + +/* + * Queue handler stub. + * + * @vdev: VirtIOSound device + * @vq: virtqueue + */ +static void virtio_snd_handle_queue(VirtIODevice *vdev, VirtQueue *vq) {} + +static uint64_t get_features(VirtIODevice *vdev, uint64_t features, + Error **errp) +{ + /* + * virtio-v1.2-csd01, 5.14.3, + * Feature Bits + * None currently defined. + */ + VirtIOSound *s = VIRTIO_SND(vdev); + features |= s->features; + + trace_virtio_snd_get_features(vdev, features); + + return features; +} + +static void virtio_snd_common_realize(DeviceState *dev, + VirtIOHandleOutput ctrl, + VirtIOHandleOutput evt, + VirtIOHandleOutput txq, + VirtIOHandleOutput rxq, + Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSound *vsnd = VIRTIO_SND(dev); + + virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config)); + virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1); + + /* set number of jacks and streams */ + if (vsnd->snd_conf.jacks > 8) { + error_setg(errp, + "Invalid number of jacks: %"PRIu32, + vsnd->snd_conf.jacks); + return; + } + if (vsnd->snd_conf.streams < 1 || vsnd->snd_conf.streams > 10) { + error_setg(errp, + "Invalid number of streams: %"PRIu32, + vsnd->snd_conf.streams); + return; + } + + if (vsnd->snd_conf.chmaps > VIRTIO_SND_CHMAP_MAX_SIZE) { + error_setg(errp, + "Invalid number of channel maps: %"PRIu32, + vsnd->snd_conf.chmaps); + return; + } + + AUD_register_card("virtio-sound", &vsnd->card); + + 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); +} + +static void +virtio_snd_vm_state_change(void *, bool running, RunState) +{ + if (running) { + trace_virtio_snd_vm_state_running(); + } else { + trace_virtio_snd_vm_state_stopped(); + } +} + +static void virtio_snd_realize(DeviceState *dev, Error **errp) +{ + ERRP_GUARD(); + VirtIOSound *vsnd = VIRTIO_SND(dev); + + vsnd->vmstate = + qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd); + + trace_virtio_snd_realize(vsnd); + + virtio_snd_common_realize(dev, + virtio_snd_handle_queue, + virtio_snd_handle_queue, + virtio_snd_handle_queue, + virtio_snd_handle_queue, + errp); +} + +static void virtio_snd_unrealize(DeviceState *dev) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIOSound *vsnd = VIRTIO_SND(dev); + + qemu_del_vm_change_state_handler(vsnd->vmstate); + virtio_del_queue(vdev, 0); + + trace_virtio_snd_unrealize(vsnd); + + AUD_remove_card(&vsnd->card); + virtio_cleanup(vdev); +} + + +static void virtio_snd_reset(VirtIODevice *vdev) {} + +static void virtio_snd_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + + + set_bit(DEVICE_CATEGORY_SOUND, dc->categories); + device_class_set_props(dc, virtio_snd_properties); + + dc->vmsd = &vmstate_virtio_snd; + vdc->vmsd = &vmstate_virtio_snd_device; + vdc->realize = virtio_snd_realize; + vdc->unrealize = virtio_snd_unrealize; + vdc->get_config = virtio_snd_get_config; + vdc->set_config = virtio_snd_set_config; + vdc->get_features = get_features; + vdc->reset = virtio_snd_reset; + vdc->legacy_features = 0; +} + +static const TypeInfo virtio_snd_types[] = { + { + .name = TYPE_VIRTIO_SND, + .parent = TYPE_VIRTIO_DEVICE, + .instance_size = sizeof(VirtIOSound), + .class_init = virtio_snd_class_init, + } +}; + +DEFINE_TYPES(virtio_snd_types) diff --git a/include/hw/virtio/virtio-snd.h b/include/hw/virtio/virtio-snd.h new file mode 100644 index 0000000000..0a7d8b0f77 --- /dev/null +++ b/include/hw/virtio/virtio-snd.h @@ -0,0 +1,80 @@ +/* + * VIRTIO Sound Device conforming to + * + * "Virtual I/O Device (VIRTIO) Version 1.2 + * Committee Specification Draft 01 + * 09 May 2022" + * + * Copyright (c) 2023 Emmanouil Pitsidianakis + * Copyright (C) 2019 OpenSynergy GmbH + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ + +#ifndef QEMU_VIRTIO_SOUND_H +#define QEMU_VIRTIO_SOUND_H + +#include "hw/virtio/virtio.h" +#include "audio/audio.h" +#include "standard-headers/linux/virtio_ids.h" +#include "standard-headers/linux/virtio_snd.h" + +#define TYPE_VIRTIO_SND "virtio-sound-device" +#define VIRTIO_SND(obj) \ + OBJECT_CHECK(VirtIOSound, (obj), TYPE_VIRTIO_SND) + +/* CONFIGURATION SPACE */ + +typedef struct virtio_snd_config virtio_snd_config; + +/* COMMON DEFINITIONS */ + +/* common header for request/response*/ +typedef struct virtio_snd_hdr virtio_snd_hdr; + +/* event notification */ +typedef struct virtio_snd_event virtio_snd_event; + +/* common control request to query an item information */ +typedef struct virtio_snd_query_info virtio_snd_query_info; + +/* JACK CONTROL MESSAGES */ + +typedef struct virtio_snd_jack_hdr virtio_snd_jack_hdr; + +/* jack information structure */ +typedef struct virtio_snd_jack_info virtio_snd_jack_info; + +/* jack remapping control request */ +typedef struct virtio_snd_jack_remap virtio_snd_jack_remap; + +typedef struct virtio_snd_jack virtio_snd_jack; + +/* + * PCM CONTROL MESSAGES + */ +typedef struct virtio_snd_pcm_hdr virtio_snd_pcm_hdr; + +/* PCM stream info structure */ +typedef struct virtio_snd_pcm_info virtio_snd_pcm_info; + +/* set PCM stream params */ +typedef struct virtio_snd_pcm_set_params virtio_snd_pcm_set_params; + +/* I/O request header */ +typedef struct virtio_snd_pcm_xfer virtio_snd_pcm_xfer; + +/* I/O request status */ +typedef struct virtio_snd_pcm_status virtio_snd_pcm_status; + +typedef struct VirtIOSound { + VirtIODevice parent_obj; + VirtQueue *queues[VIRTIO_SND_VQ_MAX]; + uint64_t features; + QEMUSoundCard card; + VMChangeStateEntry *vmstate; + virtio_snd_config snd_conf; +} VirtIOSound; +#endif From patchwork Sat Jul 29 10:36:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333112 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 90CC7C0015E for ; Sat, 29 Jul 2023 11:18:00 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLW-0007rK-OT; Sat, 29 Jul 2023 06:38:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLV-0007pg-Lb for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:49 -0400 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLT-000138-SK for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:49 -0400 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fbc244d384so29749715e9.0 for ; Sat, 29 Jul 2023 03:38:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627125; x=1691231925; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TK1fciw7LUKtOLqmUperb6ROHdP0V6/TZkVSUmHfwKY=; b=FKlX/BbmvW2qc/KGcDWazV0rFsrcwWHoOpWP+KGqZqHmBbgHarhYnzzcI+VPX8jktJ G5tD5AN2ri4/1PjpbkZhy09X/hvxK1xYpqImdqZfQKQhIlnnOTwycPGFPlhbQxDh1Z/A YKhH6XVtx/U98lnzQlKWtvNahtvxmfJStI4NcrMX7Mg+UsWef5DvHCf6qiG0CQnjuMAn KV1I41ar41DznTGLQO8h9GdkrJw7lqAbnIay+O5X232ymndCwXfIOsmkSFzYeKRm7GeM RpAg/8sdgyh2LL8X+CMHIDJ1KdC++Q2OH52B1jManhUJwpqw9gP90MJUGZVlbKaSjdvV 45CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627125; x=1691231925; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TK1fciw7LUKtOLqmUperb6ROHdP0V6/TZkVSUmHfwKY=; b=fjXa3eJleviMjFJbYHqM5nLx3wkLhvcLFjVLj/YWo4mkyFZ9WeLfzKGORgY1rrobNY TZOxk41IXSbNhNlhcVQ3ihy2//XlJyad84k/XSpda8TT+pW/MfSD++g0FduHu+tcTryl NFCUmqlg3Cs0Ci0ZFt0uIuEC01TE8uYZ7oaairbKtHMYEYGPn25G2kuJnwW8adeewJv7 mlNMAxmPRzT/G3iw22u9fZgpcmx3znno8Oro34JxZrUAoRzqQnjSJ8qZ9m3h9lVMtTaj qUcpCJfy8KRJC9tx243qIULd7wi31v+kuNBJc3AHMOAprYFsLJrw/jlJMsMIIlYDkef2 rJXA== X-Gm-Message-State: ABy/qLa0t+Ex0ypsRRfgmLAes6xT6yL35SXrZm2ekCHBNaV1OUnRa7/e WSkjYcJw9CFjx7+kt78yLxNv27/vMVjgMlb2ECoraw== X-Google-Smtp-Source: APBJJlFXhqVkVK8KsFlozDxL4Ebp0Fr9dZZK14r8i+MyGHL168od+keZsV4+T7sG0VBUtq65iwX8pg== X-Received: by 2002:a5d:5247:0:b0:317:61de:abb5 with SMTP id k7-20020a5d5247000000b0031761deabb5mr3028560wrc.69.1690627124402; Sat, 29 Jul 2023 03:38:44 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.38.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:38:43 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 02/12] Add virtio-sound-pci device Date: Sat, 29 Jul 2023 13:36:54 +0300 Message-Id: <035be510f829a871c237d91f10c5aa3d532d1bbf.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32f; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x32f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This patch adds a PCI wrapper device for the virtio-sound device. It is necessary to instantiate a virtio-snd device in a guest. All sound logic will be added to the virtio-snd device in the following commits. To add this device with a guest, you'll need a >=5.13 kernel compiled with CONFIG_SND_VIRTIO=y, which at the time of writing most distros have off by default. Use with following flags in the invocation: -device virtio-sound-pci,disable-legacy=on And an audio backend listed with `-audio driver=help` that works on your host machine, e.g.: Pulseaudio: -audio driver=pa,model=virtio-sound or -audio driver=pa,model=virtio-sound,server=/run/user/1000/pulse/native sdl: -audio driver=sdl,model=virtio-sound coreaudio (macos/darwin): -audio driver=coreaudio,model=virtio-sound etc. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/meson.build | 1 + hw/virtio/virtio-snd-pci.c | 91 ++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 1 + softmmu/qdev-monitor.c | 1 + 4 files changed, 94 insertions(+) create mode 100644 hw/virtio/virtio-snd-pci.c diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 120d4bfa0a..5e5a83a4ee 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -63,6 +63,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SERIAL', if_true: files('virtio-serial-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem-pci.c')) +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VHOST_VDPA_DEV', if_true: files('vdpa-dev-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MD', if_true: files('virtio-md-pci.c')) diff --git a/hw/virtio/virtio-snd-pci.c b/hw/virtio/virtio-snd-pci.c new file mode 100644 index 0000000000..53070b85f6 --- /dev/null +++ b/hw/virtio/virtio-snd-pci.c @@ -0,0 +1,91 @@ +/* + * VIRTIO Sound Device PCI Bindings + * + * Copyright (c) 2023 Emmanouil Pitsidianakis + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/audio/soundhw.h" +#include "hw/virtio/virtio-pci.h" +#include "hw/virtio/virtio-snd.h" + +typedef struct VirtIOSoundPCI VirtIOSoundPCI; + +/* + * virtio-snd-pci: This extends VirtioPCIProxy. + */ +#define TYPE_VIRTIO_SND_PCI "virtio-sound-pci-base" +DECLARE_INSTANCE_CHECKER(VirtIOSoundPCI, VIRTIO_SOUND_PCI, + TYPE_VIRTIO_SND_PCI) + +struct VirtIOSoundPCI { + VirtIOPCIProxy parent; + VirtIOSound vdev; +}; + +static Property virtio_snd_pci_properties[] = { + DEFINE_PROP_END_OF_LIST(), +}; + +static const char *audiodev_id; + +static int virtio_snd_init_pci(PCIBus *init_bus, const char *audiodev) +{ + audiodev_id = audiodev; + return 0; +} + +static void virtio_snd_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) +{ + VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(vpci_dev); + DeviceState *vdev = DEVICE(&dev->vdev); + + qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus), errp); + qdev_prop_set_string(vdev, "audiodev", audiodev_id); + object_property_set_bool(OBJECT(vdev), "realized", true, errp); +} + +static void virtio_snd_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); + PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); + + vpciklass->realize = virtio_snd_pci_realize; + set_bit(DEVICE_CATEGORY_SOUND, dc->categories); + + pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SND; + pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; + pcidev_k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO; + device_class_set_props(dc, virtio_snd_pci_properties); +} + +static void virtio_snd_pci_instance_init(Object *obj) +{ + VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VIRTIO_SND); +} + +static const VirtioPCIDeviceTypeInfo virtio_snd_pci_info = { + .base_name = TYPE_VIRTIO_SND_PCI, + .generic_name = "virtio-sound-pci", + .instance_size = sizeof(VirtIOSoundPCI), + .instance_init = virtio_snd_pci_instance_init, + .class_init = virtio_snd_pci_class_init, +}; + +static void virtio_snd_pci_register(void) +{ + virtio_pci_types_register(&virtio_snd_pci_info); + pci_register_soundhw("virtio-sound", "Virtio Sound Device", + virtio_snd_init_pci); +} + +type_init(virtio_snd_pci_register); diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index abdc1ef103..3cd5712035 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -85,6 +85,7 @@ extern bool pci_available; #define PCI_DEVICE_ID_VIRTIO_RNG 0x1005 #define PCI_DEVICE_ID_VIRTIO_9P 0x1009 #define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 +#define PCI_DEVICE_ID_VIRTIO_SND 0x1019 /* * modern virtio-pci devices get their id assigned automatically, diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c index 74f4e41338..2e9835ad88 100644 --- a/softmmu/qdev-monitor.c +++ b/softmmu/qdev-monitor.c @@ -108,6 +108,7 @@ static const QDevAlias qdev_alias_table[] = { { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO }, { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW }, { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI}, + { "virtio-sound-pci", "virtio-sound", QEMU_ARCH_VIRTIO_PCI}, { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO }, { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW }, { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI }, From patchwork Sat Jul 29 10:36:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333110 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1F9B3C0015E for ; Sat, 29 Jul 2023 11:14:42 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLa-0007xa-98; Sat, 29 Jul 2023 06:38:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLY-0007us-OY for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:52 -0400 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLW-00013y-NE for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:52 -0400 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3172144c084so3064689f8f.1 for ; Sat, 29 Jul 2023 03:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627129; x=1691231929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bIplqeqnlPnNjqYb3o46pvPaOA2xEJui0krDQluV7OM=; b=UdFBCGY4pUnzmaVOr7rGGY+n1hITXNrBDpY/6DmpMwCCLRYAiYNASTKVlz+kNOFJ// dOvaVDOmiMkealwcK2tGYVTkA8L1LVsloRmu/9ad4k7MZ5KXt8MeiTkeiFKBLKqKDiL4 vKtrrF2gzIy+suYejKCv0QKbK9+Am/uqsw4TITl2RrJDLK50U5RYBw/HTXOr3SdEdOM1 JJ1xtA1MIsA5alHcQioNALpt6BtalJRzSx7vMl/uheNb/7u0LrSPBJHkGRoEC9/HmuQ9 rg3D8LQhyRV35iiwCijIGkbNCwkpsy7YzI+i29pJZWyWDPhXLxFs3nbsoMNNp+lhvMI6 KfkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627129; x=1691231929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bIplqeqnlPnNjqYb3o46pvPaOA2xEJui0krDQluV7OM=; b=NNhki+qbkhcs52mf0DmOXvB1/yszGua3ncgk9f0M6ob3bROHvEpAXEUVU8/gIh4JY3 NLdfYnSXI6vUDXj+7czi834cc+UOfN+y06BoLPSyirtND2QpmXMpol3vyDd0mSwNKHya Ht+ll2HJrCKkCIE/Clu2GJ9MTl/BS2SqmiHC+5aAOa2xWH2G7XeWH/La3Q9SG52rv+3c LtFpryGvqow+R1W+2T/o68ogwVdlIrCMpazPae+lnQ9BWxXOqp6iwwH7lMLbNFLdtvSA Td1ngxfzQDusBwf/9yzgO0gYsU/vp1RMj943cd0PJncdWrYKSedlxWEAhQF95pAcJhef SB/A== X-Gm-Message-State: ABy/qLaQ7aXyzcFv3hRhdEelsfzkZ0R4AK8bVptGWNlxnGvhQba+ka23 isvWPgJtqUytIEXMtbofo8yDluRTH9NGkMjR55VhVA== X-Google-Smtp-Source: APBJJlF8Pgzchv2K/7JFm65iIu5qbJtRG0S5GXV6IbWbBjkB9EZqd0ZisR3eXiBkF34lvLCfnvCjrQ== X-Received: by 2002:a5d:574d:0:b0:314:748:d59d with SMTP id q13-20020a5d574d000000b003140748d59dmr3981157wrw.27.1690627129140; Sat, 29 Jul 2023 03:38:49 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:38:48 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 03/12] virtio-sound: handle control messages and streams Date: Sat, 29 Jul 2023 13:36:55 +0300 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wr1-x42d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Receive guest requests in the control (CTRL) queue of the virtio sound device and reply with a NOT SUPPORTED error to all control commands. The receiving handler is virtio_snd_handle_ctrl(). It stores all control messages in the queue in the device's command queue. Then it calls virtio_snd_process_cmdq() to handle each message. The handler is process_cmd() which replies with VIRTIO_SND_S_NOT_SUPP. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 4 + hw/virtio/virtio-snd.c | 221 +++++++++++++++++++++++++++++++-- include/hw/virtio/virtio-snd.h | 71 ++++++++++- 3 files changed, 287 insertions(+), 9 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 3ed7da35f2..8a223e36e9 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -163,3 +163,7 @@ virtio_snd_vm_state_running(void) "vm state running" virtio_snd_vm_state_stopped(void) "vm state stopped" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" +virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" +virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" +virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" +virtio_snd_handle_event(void) "event queue callback called" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index f301054541..c49c17ea76 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 const char *print_code(uint32_t code) +{ + #define CASE(CODE) \ + case VIRTIO_SND_R_##CODE: \ + return "VIRTIO_SND_R_"#CODE + + switch (code) { + CASE(JACK_INFO); + CASE(JACK_REMAP); + CASE(PCM_INFO); + CASE(PCM_SET_PARAMS); + CASE(PCM_PREPARE); + CASE(PCM_RELEASE); + CASE(PCM_START); + CASE(PCM_STOP); + CASE(CHMAP_INFO); + default: + return "invalid code"; + } + + #undef CASE +}; + static const VMStateDescription vmstate_virtio_snd_device = { .name = TYPE_VIRTIO_SND, .version_id = VIRTIO_SOUND_VM_VERSION, @@ -89,12 +112,148 @@ virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config) } /* - * Queue handler stub. + * The actual processing done in virtio_snd_process_cmdq(). + * + * @s: VirtIOSound device + * @cmd: control command request + */ +static inline void +process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) +{ + size_t sz = iov_to_buf(cmd->elem->out_sg, + cmd->elem->out_num, + 0, + &cmd->ctrl, + sizeof(cmd->ctrl)); + if (sz != sizeof(cmd->ctrl)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: virtio-snd command size incorrect %zu vs \ + %zu\n", __func__, sz, sizeof(cmd->ctrl)); + return; + } + + trace_virtio_snd_handle_code(cmd->ctrl.code, + print_code(cmd->ctrl.code)); + + switch (cmd->ctrl.code) { + case VIRTIO_SND_R_JACK_INFO: + case VIRTIO_SND_R_JACK_REMAP: + qemu_log_mask(LOG_UNIMP, + "virtio_snd: jack functionality is unimplemented."); + cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; + break; + case VIRTIO_SND_R_PCM_INFO: + case VIRTIO_SND_R_PCM_SET_PARAMS: + case VIRTIO_SND_R_PCM_PREPARE: + case VIRTIO_SND_R_PCM_START: + case VIRTIO_SND_R_PCM_STOP: + case VIRTIO_SND_R_PCM_RELEASE: + cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; + break; + case VIRTIO_SND_R_CHMAP_INFO: + qemu_log_mask(LOG_UNIMP, + "virtio_snd: chmap info functionality is unimplemented."); + trace_virtio_snd_handle_chmap_info(); + cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; + break; + default: + /* error */ + error_report("virtio snd header not recognized: %"PRIu32, + cmd->ctrl.code); + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + } + + iov_from_buf(cmd->elem->in_sg, + cmd->elem->in_num, + 0, + &cmd->resp, + sizeof(cmd->resp)); + virtqueue_push(cmd->vq, cmd->elem, sizeof(cmd->elem)); + virtio_notify(VIRTIO_DEVICE(s), cmd->vq); +} + +/* + * Consume all elements in command queue. + * + * @s: VirtIOSound device + */ +static void virtio_snd_process_cmdq(VirtIOSound *s) +{ + virtio_snd_ctrl_command *cmd; + + if (unlikely(qatomic_read(&s->processing_cmdq))) { + return; + } + + WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) { + qatomic_set(&s->processing_cmdq, true); + while (!QTAILQ_EMPTY(&s->cmdq)) { + cmd = QTAILQ_FIRST(&s->cmdq); + + /* process command */ + process_cmd(s, cmd); + + QTAILQ_REMOVE(&s->cmdq, cmd, next); + + g_free(cmd); + } + qatomic_set(&s->processing_cmdq, false); + } +} + +/* + * The control message handler. Pops an element from the control virtqueue, + * and stores them to VirtIOSound's cmdq queue and finally calls + * virtio_snd_process_cmdq() for processing. + * + * @vdev: VirtIOSound device + * @vq: Control virtqueue + */ +static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOSound *s = VIRTIO_SND(vdev); + VirtQueueElement *elem; + virtio_snd_ctrl_command *cmd; + + trace_virtio_snd_handle_ctrl(vdev, vq); + + if (!virtio_queue_ready(vq)) { + return; + } + + elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); + while (elem) { + cmd = g_new0(virtio_snd_ctrl_command, 1); + cmd->elem = elem; + cmd->vq = vq; + cmd->resp.code = VIRTIO_SND_S_OK; + QTAILQ_INSERT_TAIL(&s->cmdq, cmd, next); + elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); + } + + virtio_snd_process_cmdq(s); +} + +/* + * The event virtqueue handler. + * Not implemented yet. + * + * @vdev: VirtIOSound device + * @vq: event vq + */ +static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq) +{ + qemu_log_mask(LOG_UNIMP, "virtio_snd: event queue is unimplemented."); + trace_virtio_snd_handle_event(); +} + +/* + * Stub buffer virtqueue handler. * * @vdev: VirtIOSound device * @vq: virtqueue */ -static void virtio_snd_handle_queue(VirtIODevice *vdev, VirtQueue *vq) {} +static void virtio_snd_handle_xfer(VirtIODevice *vdev, VirtQueue *vq) {} static uint64_t get_features(VirtIODevice *vdev, uint64_t features, Error **errp) @@ -112,6 +271,20 @@ static uint64_t get_features(VirtIODevice *vdev, uint64_t features, return features; } +static void virtio_snd_set_pcm(VirtIOSound *snd) +{ + VirtIOSoundPCM *pcm; + + pcm = g_new0(VirtIOSoundPCM, 1); + pcm->snd = snd; + + pcm->streams = g_new0(VirtIOSoundPCMStream *, snd->snd_conf.streams); + pcm->pcm_params = g_new0(VirtIOSoundPCMParams *, snd->snd_conf.streams); + pcm->jacks = g_new0(virtio_snd_jack *, snd->snd_conf.jacks); + + snd->pcm = pcm; +} + static void virtio_snd_common_realize(DeviceState *dev, VirtIOHandleOutput ctrl, VirtIOHandleOutput evt, @@ -122,6 +295,8 @@ static void virtio_snd_common_realize(DeviceState *dev, VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIOSound *vsnd = VIRTIO_SND(dev); + virtio_snd_set_pcm(vsnd); + virtio_init(vdev, VIRTIO_ID_SOUND, sizeof(virtio_snd_config)); virtio_add_feature(&vsnd->features, VIRTIO_F_VERSION_1); @@ -152,6 +327,8 @@ static void virtio_snd_common_realize(DeviceState *dev, 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); } static void @@ -169,35 +346,65 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) ERRP_GUARD(); VirtIOSound *vsnd = VIRTIO_SND(dev); + vsnd->pcm = NULL; vsnd->vmstate = qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd); trace_virtio_snd_realize(vsnd); virtio_snd_common_realize(dev, - virtio_snd_handle_queue, - virtio_snd_handle_queue, - virtio_snd_handle_queue, - virtio_snd_handle_queue, + virtio_snd_handle_ctrl, + virtio_snd_handle_event, + virtio_snd_handle_xfer, + virtio_snd_handle_xfer, errp); } +/* + * Close the sound card. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream) +{ + virtio_snd_process_cmdq(stream->s); +} + static void virtio_snd_unrealize(DeviceState *dev) { VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIOSound *vsnd = VIRTIO_SND(dev); + VirtIOSoundPCMStream *stream; qemu_del_vm_change_state_handler(vsnd->vmstate); virtio_del_queue(vdev, 0); trace_virtio_snd_unrealize(vsnd); + for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { + stream = vsnd->pcm->streams[i]; + virtio_snd_pcm_close(stream); + g_free(stream); + } AUD_remove_card(&vsnd->card); + g_free(vsnd->pcm); virtio_cleanup(vdev); } -static void virtio_snd_reset(VirtIODevice *vdev) {} +static void virtio_snd_reset(VirtIODevice *vdev) +{ + VirtIOSound *s = VIRTIO_SND(vdev); + virtio_snd_ctrl_command *cmd; + + WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) { + while (!QTAILQ_EMPTY(&s->cmdq)) { + cmd = QTAILQ_FIRST(&s->cmdq); + QTAILQ_REMOVE(&s->cmdq, cmd, next); + g_free(cmd); + } + } +} static void virtio_snd_class_init(ObjectClass *klass, void *data) { diff --git a/include/hw/virtio/virtio-snd.h b/include/hw/virtio/virtio-snd.h index 0a7d8b0f77..7cc5061fa8 100644 --- a/include/hw/virtio/virtio-snd.h +++ b/include/hw/virtio/virtio-snd.h @@ -69,12 +69,79 @@ typedef struct virtio_snd_pcm_xfer virtio_snd_pcm_xfer; /* I/O request status */ typedef struct virtio_snd_pcm_status virtio_snd_pcm_status; -typedef struct VirtIOSound { +/* device structs */ + +typedef struct VirtIOSound VirtIOSound; + +typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream; + +typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command; + +typedef struct VirtIOSoundPCMParams VirtIOSoundPCMParams; + +typedef struct VirtIOSoundPCM VirtIOSoundPCM; + +/* Stream params */ +struct VirtIOSoundPCMParams { + uint32_t features; + uint32_t buffer_bytes; /* size of hardware buffer in bytes */ + uint32_t period_bytes; /* size of hardware period in bytes */ + uint8_t channels; + uint8_t format; + uint8_t rate; +}; + +struct VirtIOSoundPCM { + VirtIOSound *snd; + VirtIOSoundPCMParams **pcm_params; + VirtIOSoundPCMStream **streams; + virtio_snd_jack **jacks; +}; + +struct VirtIOSoundPCMStream { + VirtIOSoundPCM *pcm; + virtio_snd_pcm_info info; + uint32_t id; + uint32_t buffer_bytes; + uint32_t period_bytes; + /* channel position values (VIRTIO_SND_CHMAP_XXX) */ + uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE]; + VirtIOSound *s; + uint32_t features; /* 1 << VIRTIO_SND_PCM_F_XXX */ + uint64_t formats; /* 1 << VIRTIO_SND_PCM_FMT_XXX */ + uint64_t rates; /* 1 << VIRTIO_SND_PCM_RATE_XXX */ + uint8_t direction; + uint8_t channels_min; + uint8_t channels_max; + bool flushing; + audsettings as; + audsettings desired_as; + union { + SWVoiceIn *in; + SWVoiceOut *out; + } voice; + QemuMutex queue_mutex; + QSIMPLEQ_HEAD(, VirtIOSoundPCMBlock) queue; +}; + +struct VirtIOSound { VirtIODevice parent_obj; VirtQueue *queues[VIRTIO_SND_VQ_MAX]; uint64_t features; + VirtIOSoundPCM *pcm; QEMUSoundCard card; VMChangeStateEntry *vmstate; virtio_snd_config snd_conf; -} VirtIOSound; + QemuMutex cmdq_mutex; + QTAILQ_HEAD(, virtio_snd_ctrl_command) cmdq; + bool processing_cmdq; +}; + +struct virtio_snd_ctrl_command { + VirtQueueElement *elem; + VirtQueue *vq; + virtio_snd_hdr ctrl; + virtio_snd_hdr resp; + QTAILQ_ENTRY(virtio_snd_ctrl_command) next; +}; #endif From patchwork Sat Jul 29 10:36:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333115 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D278C001DC for ; Sat, 29 Jul 2023 11:31:08 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLf-0008Ik-4b; Sat, 29 Jul 2023 06:38:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLd-00089n-N8 for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:57 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLc-00015M-0D for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:38:57 -0400 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3fe110de3b6so8741435e9.1 for ; Sat, 29 Jul 2023 03:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627134; x=1691231934; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YztM4kDBmLmGNGlh3rIVws7vtRqKyCmzIKShHvHPY2o=; b=AnAYZ47lCYcYS8zBj5kMTNVRe2RLgiv5k6b3XYHAl/t3xtxuGDbtCuUAm3GN7IOqfT UhNBbG9l3+Nqc0hWdd8d7RcrtIaWs1G0j+2R9399WLO2lUvD1xMgwEEwN/Y30n0RZsIZ u4rP+Yiy6lXwTGkv3K1dlPPYvYHxgnZ+3rz6Qx1seBOqUFpnUjOQAPxHeSsjYOyCmrNQ hfahMg86BfyqEGOAWnBi1SAKm0qkvjUX3Hn7emYPEo3b8lOhJec2meeVVzXOpnZs46HL xDpGmp8POl1HBowaiOSbifkTQCo3nTgl7Gdo96PVfArK2T9DjBkDYp2becssV7Jlia7d 7pAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627134; x=1691231934; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YztM4kDBmLmGNGlh3rIVws7vtRqKyCmzIKShHvHPY2o=; b=HE3Vkgz68GGRLe9xTUE+ymSPqiRV3GY1IkUxKWwbtrBJ/0XUIZN3/8vHub1y5viQA5 L5VJdiKNzihySKjIUVUxYhKj0VNGtESQVCTb6P8MhPtmC/kapJAzQUQl6Qq/VM6OTGGS kYPnWllMsxuGucbEQJutXDJGYVBRB55hr6erHC2STsTU/fl+Ta3BzFxm2J7k23Io0/hJ h2no6CE2Tsn8Ez79w9p2Fq7Hb8NuuGc/F0xa3LA4Ks4cdotW/IFO2CbWaJlRu9W80jze wZQEvKhgIKiE6OEC5PqPALj7AVR4ToSUXlgi6GGKDwu6bwqUKLXjEgNnZ74+mC/HZpqD lxhA== X-Gm-Message-State: ABy/qLa7+SEOLCDjGhC6uyaFGi8fw52g2NWhVGg+FpqBTNjEQK8dLhrf xZ1K4Y0ARgi3mLePaDILjrxrMRwE7gxbblXt9uBaDw== X-Google-Smtp-Source: APBJJlEk3C6gg+gHQS77YGxoWxnX/vG0+PQ9uTBSKmCzgY2VCD2VrbFD2AmlpJ8aQx/IdLVfln+k0w== X-Received: by 2002:a05:600c:4f45:b0:3fa:8c8b:716 with SMTP id m5-20020a05600c4f4500b003fa8c8b0716mr6721741wmq.1.1690627134238; Sat, 29 Jul 2023 03:38:54 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.38.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:38:53 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 04/12] virtio-sound: set PCM stream parameters Date: Sat, 29 Jul 2023 13:36:56 +0300 Message-Id: <82c80e2ae4ff684ca9e1c245ccb23e4d7f88106b.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.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 --- hw/virtio/virtio-snd.c | 109 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index c49c17ea76..dda724f3ce 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) \ @@ -111,6 +134,72 @@ 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; + } + + if (!s->pcm->pcm_params[stream_id]) { + s->pcm->pcm_params[stream_id] = g_new0(VirtIOSoundPCMParams, 1); + } + st_params = virtio_snd_pcm_get_params(s, stream_id); + + st_params->features = params->features; + st_params->buffer_bytes = params->buffer_bytes; + st_params->period_bytes = params->period_bytes; + + if (params->channels < 1 || params->channels > AUDIO_MAX_CHANNELS) { + error_report("Number of channels is not supported."); + return VIRTIO_SND_S_NOT_SUPP; + } + st_params->channels = params->channels; + + if (!(supported_formats & BIT(params->format))) { + error_report("Stream format is not supported."); + return VIRTIO_SND_S_NOT_SUPP; + } + st_params->format = params->format; + + if (!(supported_rates & BIT(params->rate))) { + error_report("Stream rate is not supported."); + return VIRTIO_SND_S_NOT_SUPP; + } + st_params->rate = params->rate; + st_params->period_bytes = params->period_bytes; + st_params->buffer_bytes = params->buffer_bytes; + + return VIRTIO_SND_S_OK; +} + /* * The actual processing done in virtio_snd_process_cmdq(). * @@ -294,6 +383,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; + uint32_t status; virtio_snd_set_pcm(vsnd); @@ -323,12 +414,30 @@ 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 = 8192; + default_params.period_bytes = 4096; + default_params.channels = 2; + default_params.format = VIRTIO_SND_PCM_FMT_S16; + default_params.rate = VIRTIO_SND_PCM_RATE_44100; 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; + } + } } static void From patchwork Sat Jul 29 10:36:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5A570C001DC for ; Sat, 29 Jul 2023 11:31:58 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLj-00005i-SY; Sat, 29 Jul 2023 06:39:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLh-0008Pe-QI for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:01 -0400 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLg-000166-7B for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:01 -0400 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-316feb137a7so3086333f8f.1 for ; Sat, 29 Jul 2023 03:38:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627138; x=1691231938; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qMqK+wZaQpSLsT7aCCcs1/xfHis4zgE5fANu8jf6wDQ=; b=lhgKhwYEcr2s1Y9NfLf/QeAczCAFuuLIEKuW58Xx5ELEW1J6niwPpIHuY7gEAp+MMt CJHcVq47mjkAKHWRplpnHv8BfWor32Tme0Mra81GaPioXbE8AeGamYCw1T1V6xyoxDNn hezs2KPLvmvGotkEm+p2NzyZkyq/iMHCub9tNZBHOGAsaZPoAztV4UPQRSpe7/OVvKnh i0WBg2KrjiWt8xG0/yoicpcDkwKT2UinY74sEULNlkdNVYlXCv0fXD26d5SSde9ddpGZ nO1iD1UWrYuOIWnuPbVB4x9GjLF7GssKAN4lSahzczhC9pXeEB7klu4xqOWT1+Gg/iUO +U5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627138; x=1691231938; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qMqK+wZaQpSLsT7aCCcs1/xfHis4zgE5fANu8jf6wDQ=; b=jaJ5sM9WjPvb2o627SRi+wkdmMYQ8EfxAnChC9kmlP17acgUvXoZiesgBswE45xtVE YHhmBWtW0fJRrxk3skM4P0DxPYh5r1ZKYengsejUuMNUV3ez3F7ZjJwsjz+giQFFBBA+ J5pdtgfS4F60/lTtXZD197IyDHZfSGbMFWsN3jc7XlUZZzr6xXIgxL45X6l+0w8PaEdv Spu/lJ6TdOWFSlKeDolVV47nLKN+ulAf+zioz85k6kOyfzBTmQET5CfvZgliVa4p74Kp rp80lbOJFcK78lD0nn7SK9qQaSNKnSmuWPaK6ggGnRSerhZUiOO217SyHzYFt/t1YOHZ ST3w== X-Gm-Message-State: ABy/qLaTTvYO1xoRjaKixS6hE42ZWIJdX3ZV+Z1KT9qzubZVBGmkmfKK TazDYi7iDPao0CjCDLto4moRmV2VvYgWjkhU/OnUng== X-Google-Smtp-Source: APBJJlGVRYqvDT/2CfDYFAHalTQooP2iB3N4PD57GHqRwPGvVuD400clXVx+CYWVsSvIaESTTptmhg== X-Received: by 2002:adf:ec8f:0:b0:313:e741:1caa with SMTP id z15-20020adfec8f000000b00313e7411caamr4029668wrn.25.1690627138696; Sat, 29 Jul 2023 03:38:58 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.38.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:38:58 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 05/12] virtio-sound: prepare PCM streams Date: Sat, 29 Jul 2023 13:36:57 +0300 Message-Id: <19d95e841157a6a6d88e60e749025f487f010d22.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42b; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wr1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org After setting PCM parameters, instantiate ("prepare") each stream in virtio_snd_pcm_prepare_impl(). Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/virtio-snd.c | 133 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index dda724f3ce..4d69f2003a 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -200,6 +200,132 @@ uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s, 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; +} + +/* + * 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 || + !s->pcm->pcm_params[stream_id]) { + 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); + + 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(). * @@ -437,6 +563,13 @@ static void virtio_snd_common_realize(DeviceState *dev, 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; + } } } From patchwork Sat Jul 29 10:36:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333119 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 69FA7C001DC for ; Sat, 29 Jul 2023 11:48:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLn-0000GB-SG; Sat, 29 Jul 2023 06:39:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLl-00005p-Q8 for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:05 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLk-000179-82 for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:05 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3fbef8ad9bbso32834685e9.0 for ; Sat, 29 Jul 2023 03:39:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627142; x=1691231942; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wk0psUlwpn5O2EJ9JAYIFQ5jJ/30Ny4RuRauf3NTG8A=; b=lIQVdSsFcz6dbBcColSJR2tvESG6y+1PaBGxFtk1Uo5z7sg7KaWoS1nJC2FFgX0Dgc 9ofzzekBUlCmyMf08sfcTS+pWHob/LU6n1kDivGIsRPHtwwt9ulyvniF7YMFrP1Og6tD xmbgNJydqqzP3+oHS+kwniXOGnVflK1hKtOwgxFmme6E6It3v+aw61AQoeSkyiBTfU1k NCPUazo2rCsh5JRsmObGDSbQPZFl4y5fBAeHgrglg5l8TW6ootn46gdIXODS6h7eVbON 8j/AjAnDWfTlZlIBBGfDcpxB7/ecF3K3h/qJyKspo9fEw/wxg6h4K6pWw7IA6QKk8NNo EfUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627142; x=1691231942; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wk0psUlwpn5O2EJ9JAYIFQ5jJ/30Ny4RuRauf3NTG8A=; b=kTWrm7vOcVTsupzvWt9rqP7OPWyLE9AsbTpL139uImPIrOUT/W+tU5vwjoKXEbDMhh OcpK+ODX3EVOHRtyVZMOBc1FB1IbIjAO5zE9ZSyK4mrBy5YBWPkt/mWpS9CTUC8BqJ4g BkUhEetEvicf3weISPjph8u6lvHtAY23lxA8CzuZAcNLC/PbfQEitf8gpm+tVxE+TrC/ HbL/oRHKtFoxG69blBSNUkwJiB6m9bPwnDrwYx/K+szKpAKYIfKVewZ+uoCulAH4XhZz ILNxTg96AL58Bb68pTEDyFFFgMDT2pFKQIQEFoppGaD8MbdP6HsfrcHEIkspiOA6l+vk OZmQ== X-Gm-Message-State: ABy/qLabWaCq3oIGtxP08CsvmZpRz/8qAb76n4i65z0GMzMQ4raACFgO EizOaDOGcgXFk3HhTBXzNSsWTaDukK4IDXl8uCaoGw== X-Google-Smtp-Source: APBJJlE6gyOb6O2PmwEGfVQJLmIuPrHKVvOgFKm5AiNdHroHb+18wwfidgJhgrm1+11uMCSZlhKTvw== X-Received: by 2002:a05:600c:3657:b0:3fd:e86b:f098 with SMTP id y23-20020a05600c365700b003fde86bf098mr4134852wmq.4.1690627142603; Sat, 29 Jul 2023 03:39:02 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:02 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 06/12] virtio-sound: handle VIRTIO_SND_R_PCM_INFO request Date: Sat, 29 Jul 2023 13:36:58 +0300 Message-Id: <78777fb46fa86180fa0654bb82c8ff976635cd86.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x335.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Respond to the VIRTIO_SND_R_PCM_INFO control request with the parameters of each requested PCM stream. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 8a223e36e9..3e619f778b 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -164,6 +164,7 @@ virtio_snd_vm_state_stopped(void) "vm state stopped" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" +virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index 4d69f2003a..eccb156e44 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -134,6 +134,19 @@ virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config) memcpy(&s->snd_conf, sndconfig, sizeof(s->snd_conf)); } +/* + * Get a specific stream from the virtio sound card device. + * Returns NULL if @stream_id is invalid or not allocated. + * + * @s: VirtIOSound device + * @stream_id: stream id + */ +static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s, + uint32_t stream_id) +{ + return stream_id >= s->snd_conf.streams ? NULL : s->pcm->streams[stream_id]; +} + /* * Get params for a specific stream. * @@ -147,6 +160,69 @@ static VirtIOSoundPCMParams *virtio_snd_pcm_get_params(VirtIOSound *s, : s->pcm->pcm_params[stream_id]; } +/* + * Handle the VIRTIO_SND_R_PCM_INFO request. + * The function writes the info structs to the request element. + * + * @s: VirtIOSound device + * @cmd: The request command queue element from VirtIOSound cmdq field + */ +static void virtio_snd_handle_pcm_info(VirtIOSound *s, + virtio_snd_ctrl_command *cmd) +{ + virtio_snd_query_info req; + VirtIOSoundPCMStream *stream = NULL; + g_autofree virtio_snd_pcm_info *pcm_info = NULL; + size_t sz = iov_to_buf(cmd->elem->out_sg, + cmd->elem->out_num, + 0, + &req, + sizeof(req)); + if (sz != sizeof(virtio_snd_query_info)) { + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + + if (iov_size(cmd->elem->in_sg, cmd->elem->in_num) < + sizeof(virtio_snd_hdr) + req.size * req.count) { + error_report("pcm info: buffer too small, got: %zu, needed: %zu", + iov_size(cmd->elem->in_sg, cmd->elem->in_num), + sizeof(virtio_snd_pcm_info)); + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + + pcm_info = g_new0(virtio_snd_pcm_info, req.count); + for (uint32_t i = req.start_id; i < req.start_id + req.count; i++) { + trace_virtio_snd_handle_pcm_info(i); + stream = virtio_snd_pcm_get_stream(s, i); + + if (!stream) { + error_report("Invalid stream id: %"PRIu32, i); + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + + pcm_info[i - req.start_id].hdr.hda_fn_nid = stream->info.hdr.hda_fn_nid; + pcm_info[i - req.start_id].features = stream->features; + pcm_info[i - req.start_id].formats = stream->formats; + pcm_info[i - req.start_id].rates = stream->rates; + pcm_info[i - req.start_id].direction = stream->direction; + pcm_info[i - req.start_id].channels_min = stream->channels_min; + pcm_info[i - req.start_id].channels_max = stream->channels_max; + + memset(&pcm_info[i].padding, 0, sizeof(pcm_info[i].padding)); + } + + cmd->resp.code = VIRTIO_SND_S_OK; + + iov_from_buf(cmd->elem->in_sg, + cmd->elem->in_num, + sizeof(virtio_snd_hdr), + pcm_info, + sizeof(virtio_snd_pcm_info) * req.count); +} + /* * Set the given stream params. * Called by both virtio_snd_handle_pcm_set_params and during device @@ -358,6 +434,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; break; case VIRTIO_SND_R_PCM_INFO: + virtio_snd_handle_pcm_info(s, cmd); + break; case VIRTIO_SND_R_PCM_SET_PARAMS: case VIRTIO_SND_R_PCM_PREPARE: case VIRTIO_SND_R_PCM_START: From patchwork Sat Jul 29 10:36:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333101 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F1E40C0015E for ; Sat, 29 Jul 2023 10:59:47 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLr-0000QI-IX; Sat, 29 Jul 2023 06:39:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLq-0000IH-A6 for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:10 -0400 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLo-00017o-Qp for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:10 -0400 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3fde57684d7so28857075e9.2 for ; Sat, 29 Jul 2023 03:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627147; x=1691231947; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SFgI0VBM4/EkVfi0Gwqqn06sAlissiVMCynNkPCR8Nw=; b=BQqUDW0cMVyPEmMlt6moWQscfs/eH1864sgjRKgMmSSJmweeMbRFiDmIRif9u5LhwS VeO/XqDy/pWzk+LuQRW4GdmDqumVDwcmtnXmNu6psG21I4uL28ahDOOVNw5iLp4N/dgB zMuxdP3recqXevtfdzJ3we3gerB7GJCZrR8Ep+0ojJSNZl+gP/He67wYJC3HCP9oCBWG 3BeFXd5J+r2AXCZQm9J7hYWhLkVHRVVSojan+Sq2Isk07Tw4cLV75saU2V67V0gD2Chp 9NsAT4CQLc9yAYNDMjY9BsguOd8dy4LU+sLHJ2XkO5GdtOuN76R27STtKLh/thJx8uvn rqiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627147; x=1691231947; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SFgI0VBM4/EkVfi0Gwqqn06sAlissiVMCynNkPCR8Nw=; b=krInmvXvIwQPYi4odO2xQA7zEPdSoYroZVD6zAUaenSrhinO2vaSR8Jt9Y+RgLD+Wk pxM0LaEsMFwItb63umo24tjDQi7T+VDl9t0riUc77vt2iUiz97l23GsWuJwUFxuxo2kt dCwI2Hy2Hvn/fCRpz+5pPLFsVhVH59XlkN5lpAhp6WJNJPlH5sLYf6zPV7wMWJR1GEi+ oDNrOHdkjG/3vdU4KUr58StIGp0tGgqFiqyx2Bb21rSqeRHrjfpJVkSnc7QfDcfm/s5x SAVzHXHIybsPNPgUwrV57teurYNMihnEy7JhRyMuQTiElK3t0IrW6sORZEcZQZfLeB/o W2RA== X-Gm-Message-State: ABy/qLbN9CbDnG1y/MRQTHfuvptkFgbN27halK/s6amfiYpsplYYCk6H kK+BBkjN7rLikZu2syDd3kudARxWXie9qiXhslkOUw== X-Google-Smtp-Source: APBJJlGm9YuGDZoHLsIEMuCdl/zLntfryizjtWr1gxLvcIY8feylagK/qlLDCRJ1wCN+i7EEtaHnrQ== X-Received: by 2002:a1c:7416:0:b0:3fe:ad4:27b1 with SMTP id p22-20020a1c7416000000b003fe0ad427b1mr3086159wmc.28.1690627146926; Sat, 29 Jul 2023 03:39:06 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:06 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 07/12] virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP} Date: Sat, 29 Jul 2023 13:36:59 +0300 Message-Id: <7641e21ee8e50dbcc045caed774082ecca455640.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::331; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x331.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Handle the start and stop control messages for a stream_id. This request does nothing at the moment except for replying to it. Audio playback or capture will be started/stopped here in follow-up commits. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 3e619f778b..8eae1bf881 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -165,6 +165,7 @@ virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 +virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index eccb156e44..c8adc7614c 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -402,6 +402,40 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id) return VIRTIO_SND_S_OK; } +/* + * Handles VIRTIO_SND_R_PCM_START. + * + * @s: VirtIOSound device + * @cmd: The request command queue element from VirtIOSound cmdq field + * @start: whether to start or stop the device + */ +static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s, + virtio_snd_ctrl_command *cmd, + bool start) +{ + VirtIOSoundPCMStream *stream; + virtio_snd_pcm_hdr req; + size_t sz = iov_to_buf(cmd->elem->out_sg, + cmd->elem->out_num, + 0, + &req, + sizeof(req)); + if (sz != sizeof(virtio_snd_pcm_hdr)) { + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + + cmd->resp.code = VIRTIO_SND_S_OK; + trace_virtio_snd_handle_pcm_start_stop(start ? "VIRTIO_SND_R_PCM_START" : + "VIRTIO_SND_R_PCM_STOP", req.stream_id); + + stream = virtio_snd_pcm_get_stream(s, req.stream_id); + if (!stream) { + error_report("Invalid stream id: %"PRIu32, req.stream_id); + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + } +} + /* * The actual processing done in virtio_snd_process_cmdq(). * @@ -436,10 +470,14 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) case VIRTIO_SND_R_PCM_INFO: virtio_snd_handle_pcm_info(s, cmd); break; - case VIRTIO_SND_R_PCM_SET_PARAMS: - case VIRTIO_SND_R_PCM_PREPARE: case VIRTIO_SND_R_PCM_START: + virtio_snd_handle_pcm_start_stop(s, cmd, true); + break; case VIRTIO_SND_R_PCM_STOP: + virtio_snd_handle_pcm_start_stop(s, cmd, false); + break; + case VIRTIO_SND_R_PCM_SET_PARAMS: + case VIRTIO_SND_R_PCM_PREPARE: case VIRTIO_SND_R_PCM_RELEASE: cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; break; From patchwork Sat Jul 29 10:37:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333109 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DBF47C0015E for ; Sat, 29 Jul 2023 11:13:47 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLv-0000aD-Cs; Sat, 29 Jul 2023 06:39:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLu-0000a5-6V for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:14 -0400 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLs-00018u-Ju for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:13 -0400 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3fbf1b82d9cso28620275e9.2 for ; Sat, 29 Jul 2023 03:39:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627151; x=1691231951; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=luWiixk055IHbxaWLp3VrLOmzNxb5Lg5G+xPDpAlQSE=; b=MF0NAqUnnwKUbuiMdnWGDHRZv+EFrIqIk0kiNxhxE3xQX8WS1UYtd+JEfd/Je+/BvR ZT25li2i36uHdSi4fko40zrAYaw/d/aeGY1Fub+k41cTnUae3W1eVT6C0ttkERAgjW3A aVyBfhehjBCbVRSqqqPGTsh1Z6kaEBYp5ETbc0T1rQbEzk3/SXucf5sP4whCWRyhb+t+ 77obk+6K5dFgteJbkiSLdmRqs5Nm3zsxkGmCBUku81j/NxMJM5vgEXvEMZYzCr7Go3op G/ej2EfhOvCkkeYRRqGRXIMSCN9Pl2xeRopRuPx/5dzOzt6BmlxRigmFwMUZAxzJliWQ yIQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627151; x=1691231951; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=luWiixk055IHbxaWLp3VrLOmzNxb5Lg5G+xPDpAlQSE=; b=A4/eYixZ6xLGMZRKsrdD5DRId0diMGQatvW62HFzBroQ+ZEcmFRiglO53I10okCZwz fK3N/W7kx+OS996Xdt4Ysqgl1Q9JnfX+ayNWt9zKizN0aAFhyTKNXTlfYv0EFV52EiQs uUxydoLzBEwFTBPkSfn0Obni+t/X71tPqszOAE+dKhUuCfpRj+JF9BH/6MjupgIspzzC N3J48ziVLS93Qs6YEgXLRqrLGLDtC+EQH2yromSTaZEKSgEXr2gHKBA4m28x3Oy1Kh+E WKBS9mtduSzZnHDKtk8jAVokU5UxBvBcrdlCZnmsszIbwWxVhXxPAQUZPzaC+v67TM2A ON0A== X-Gm-Message-State: ABy/qLbh/dgrLkRKFTw0vN+vRoaBRVqy5HpCbX8yh/ClUrgslOP2Efzd yAVnV+Mi0xtvsZVOFZ4xsqvg+tquxrv1jcgqtIdwyw== X-Google-Smtp-Source: APBJJlEBSHgvhKVq83+150vtysOz5/TshCijWBgJNJ5wtA6QOhmfcrsiQ7OI8y+ZVxf93jYwW3OyNw== X-Received: by 2002:a1c:7917:0:b0:3fe:16f4:d865 with SMTP id l23-20020a1c7917000000b003fe16f4d865mr876213wme.23.1690627151129; Sat, 29 Jul 2023 03:39:11 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:10 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 08/12] virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS Date: Sat, 29 Jul 2023 13:37:00 +0300 Message-Id: <48f9b776f5b90c560b8731ed96747e5429e781ec.1690626150.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x330.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Handle the set parameters control request. It reconfigures a stream based on a guest's preference if the values are valid and supported. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 8eae1bf881..f70cde4f01 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -163,6 +163,7 @@ virtio_snd_vm_state_running(void) "vm state running" virtio_snd_vm_state_stopped(void) "vm state stopped" virtio_snd_realize(void *snd) "snd %p: realize" virtio_snd_unrealize(void *snd) "snd %p: unrealize" +virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARAMS called for stream %"PRIu32 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32 diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index c8adc7614c..85a0565543 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -276,6 +276,30 @@ uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s, return VIRTIO_SND_S_OK; } +/* + * Handles the VIRTIO_SND_R_PCM_SET_PARAMS request. + * + * @s: VirtIOSound device + * @cmd: The request command queue element from VirtIOSound cmdq field + */ +static void virtio_snd_handle_pcm_set_params(VirtIOSound *s, + virtio_snd_ctrl_command *cmd) +{ + virtio_snd_pcm_set_params req; + size_t sz = iov_to_buf(cmd->elem->out_sg, + cmd->elem->out_num, + 0, + &req, + sizeof(req)); + if (sz != sizeof(virtio_snd_pcm_set_params)) { + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + + trace_virtio_snd_handle_pcm_set_params(req.hdr.stream_id); + cmd->resp.code = virtio_snd_pcm_set_params_impl(s, &req); +} + /* * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_* */ @@ -477,6 +501,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) virtio_snd_handle_pcm_start_stop(s, cmd, false); break; case VIRTIO_SND_R_PCM_SET_PARAMS: + virtio_snd_handle_pcm_set_params(s, cmd); + break; case VIRTIO_SND_R_PCM_PREPARE: case VIRTIO_SND_R_PCM_RELEASE: cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; From patchwork Sat Jul 29 10:37:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333114 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F0D09C001E0 for ; Sat, 29 Jul 2023 11:28:48 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhLy-0000gE-Up; Sat, 29 Jul 2023 06:39:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhLy-0000g5-7F for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:18 -0400 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhLw-00019V-OY for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:17 -0400 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-314172bac25so2730119f8f.3 for ; Sat, 29 Jul 2023 03:39:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627155; x=1691231955; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QeWkGnZPslfwAExkWiwvEET430rj7LZsI1bWG/GhhK4=; b=dW19i1iwf5jA7MO5+SdAiW6BROzVFNpFgqsbTm3045ByhHlwx8Ml9arPxKkdkAdRAL 9MxRyY9JDsX1CJ7E+3hyeQbBTSC5stAkDJTShXP5R6znew5ey2QTs3sg0140HWbns8rd BOddFj1VNcNPiRf8Difm20ViuSn8I83jLuHJqDVp9wYoOheSJRviEqAnI26Os6INYedB 2biUXs/Hj0LSdgjvMgWtGxGw05RBqrsD6NT3dk7PBFVmEkQsUSk4XQahaeSJwFcbEZF6 hUjIke/tTEFVDd1dCLxSnlhIbN8nTPEBiN1XITwIZqRGgnYF7+D+LwFms2ew5AkheG6y TTQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627155; x=1691231955; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QeWkGnZPslfwAExkWiwvEET430rj7LZsI1bWG/GhhK4=; b=eoze7zXPmZz2KMwjFygm+fKVbm96bNIG4zsGL4JvYjxDFQqcdWXuT4rULUN9GiEyiH enu01zfrpLVZi95uJxgo3NZN/7mDoZrMBSyVOmbWbuhP75XWmgx6B0jxCEIHUccRrr4T pNeiOgtUfxuCzh6CSQQ9rpT4LpKiPQUTqFbU8umkmvt0AvVxkZDlxl1245kRen9q2rbW 3ddE2UXKWu3VGib4dHmR48kNG5HKgJHVYLVzTo755fV3aAidzas+LZxSYc2DIeu0ZM1Y vlBjnR0r1yc+EfnJ8MAQh6/zy0oCKmYBUINynFdOOnqnwBUUHQ1tpYlt7QwbfN8TxYqN FcPw== X-Gm-Message-State: ABy/qLZ6BXua/7hnVfxCpO3Iu9bSobUgDco1+TodeQYn68mQqoaXlUXc wfZyppIb2vFaqK9eoALblmlIsEG7VOJu7fYlWw/shg== X-Google-Smtp-Source: APBJJlHDTQH73glKMgm1enzChvANP3xYavync5GIBrgISQlszf6d6jhtbFvRsxbGKPxyoQy6cbP0aw== X-Received: by 2002:adf:e807:0:b0:317:5e58:3fd with SMTP id o7-20020adfe807000000b003175e5803fdmr3962927wrm.47.1690627155084; Sat, 29 Jul 2023 03:39:15 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:14 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 09/12] virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE Date: Sat, 29 Jul 2023 13:37:01 +0300 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wr1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Handles the PCM prepare control request. It initializes a PCM stream when the guests asks for it. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/virtio-snd.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index 85a0565543..a1cccf8ac8 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -426,6 +426,27 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id) return VIRTIO_SND_S_OK; } +/* + * Handles VIRTIO_SND_R_PCM_PREPARE. + * + * @s: VirtIOSound device + * @cmd: The request command queue element from VirtIOSound cmdq field + */ +static void virtio_snd_handle_pcm_prepare(VirtIOSound *s, + virtio_snd_ctrl_command *cmd) +{ + uint32_t stream_id; + size_t sz = iov_to_buf(cmd->elem->out_sg, + cmd->elem->out_num, + sizeof(virtio_snd_hdr), + &stream_id, + sizeof(stream_id)); + + cmd->resp.code = sz == sizeof(uint32_t) + ? virtio_snd_pcm_prepare_impl(s, stream_id) + : VIRTIO_SND_S_BAD_MSG; +} + /* * Handles VIRTIO_SND_R_PCM_START. * @@ -504,6 +525,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) virtio_snd_handle_pcm_set_params(s, cmd); break; case VIRTIO_SND_R_PCM_PREPARE: + virtio_snd_handle_pcm_prepare(s, cmd); + break; case VIRTIO_SND_R_PCM_RELEASE: cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; break; From patchwork Sat Jul 29 10:37:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333118 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36FDEC001DC for ; Sat, 29 Jul 2023 11:44:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhM4-0000ho-Eq; Sat, 29 Jul 2023 06:39:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhM3-0000gZ-42 for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:23 -0400 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhM1-0001Ac-Kx for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:22 -0400 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-4fe0d5f719dso4995857e87.2 for ; Sat, 29 Jul 2023 03:39:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627159; x=1691231959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bx39DvTsg/ajjQUQLb6RWvFaCKu2g+7rrEAwijI0XxI=; b=PxcK8+FHn9MFvMEEtcZhfxQuaOuTj8+SZ7lLek7Nff0GWsYyRrUgaMa/oVNyhGDcmY vI9MpkoJ1tUUkVv6ApY2+lnzM3t5OIfcRPcHHmosMF3xYZ8LscqbPkLYsdNJ9nRxBSP4 WRvPPUDGXxvAaSVy5IWyapahZKIIScqhBCzEk4YmRtz8s5GOfUP+InW2uiRLObLOLFkC 1MRYX3R0uDjsf6Q766fTV7YTUDZN9fEmtEWbKAWu0dZn6n+Ri3OOHqPGMz06RiIz18Gh R+LwqcfV3zdJvqyAUZ3ZsJfsZiDFZXP5SoNuTgDzH49Lwe1GHYq1XU2yDD3qXbI1ot39 oMUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627159; x=1691231959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bx39DvTsg/ajjQUQLb6RWvFaCKu2g+7rrEAwijI0XxI=; b=EIqW7L1W8+2OTLaZ+3I3g9Kub23h/ALW34HAETNkNi5fb4m+S37PwhrvzlsQzWIL40 aLDKwgoomOybvE4K7or6NsYwuRNTS1awhAlrW7QaG5xZp5NG3Nl2RAAbwM2PWARPWFlK RlWAd+uVVqFR/1RyuvH4Ezak1jcebCVrjAnWSy6/VNnOjFjC51MxwRDVFV9LIeQ+eQ74 /IQwjmjgbu0uxqNirRBSlbCyQtLyScT51X+jDcQFAovXTSCj60N5dWXc2+nl/vUdyKlo hElRdQXHJwrgEdc2GNH8AIDLo9ZQRTJoyxESBBAG3PyY8z1yAm0SsiQ/Myv4suX/1OeH BhpA== X-Gm-Message-State: ABy/qLZCo1LRzLOqHZrU/Pj2/WV2JKIOUEMtcR+p30LpmfBVN+FEoWEa GwwCosoqUC+TleZ0gCXSrlkCBH03l+rKlEQMwu6rlg== X-Google-Smtp-Source: APBJJlEIRrU+u6ytWpwRoQ6n4OfjVeEu/EsokYKkbJYwW43Do27/FTkj5UZcOifsl/o1uKsF9NLsQw== X-Received: by 2002:a19:ca16:0:b0:4fb:89cd:9616 with SMTP id a22-20020a19ca16000000b004fb89cd9616mr3093382lfg.0.1690627159614; Sat, 29 Jul 2023 03:39:19 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:19 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 10/12] virtio-sound: handle VIRTIO_SND_PCM_RELEASE Date: Sat, 29 Jul 2023 13:37:02 +0300 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::135; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-lf1-x135.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Handle the PCM release control request, which is necessary for flushing pending sound IO. No IO is handled yet so currently it only replies to the request. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 1 + hw/virtio/virtio-snd.c | 51 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index f70cde4f01..60ab62a80d 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -167,6 +167,7 @@ virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARAMS cal virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p" virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32 virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32 +virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE called for stream %"PRIu32 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index a1cccf8ac8..5065d62e40 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -481,6 +481,55 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s, } } +/* + * Releases the buffer resources allocated to a stream. Seperated from the + * handler so that the code can be reused in the unrealize function. Returns + * the response status code. (VIRTIO_SND_S_*). + * + * @stream: VirtIOSoundPCMStream stream + * @stream_id: stream id + */ +static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundPCMStream *stream, + uint32_t stream_id) +{ + return VIRTIO_SND_S_OK; +} + +/* + * Handles VIRTIO_SND_R_PCM_RELEASE. + * + * @s: VirtIOSound device + * @cmd: The request command queue element from VirtIOSound cmdq field + */ +static void virtio_snd_handle_pcm_release(VirtIOSound *s, + virtio_snd_ctrl_command *cmd) +{ + uint32_t stream_id; + VirtIOSoundPCMStream *stream; + size_t sz = iov_to_buf(cmd->elem->out_sg, + cmd->elem->out_num, + sizeof(virtio_snd_hdr), + &stream_id, + sizeof(stream_id)); + if (sz != sizeof(uint32_t)) { + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + + trace_virtio_snd_handle_pcm_release(stream_id); + + stream = virtio_snd_pcm_get_stream(s, stream_id); + if (!stream) { + error_report("already released stream %"PRIu32, stream_id); + virtio_error(VIRTIO_DEVICE(s), + "already released stream %"PRIu32, + stream_id); + cmd->resp.code = VIRTIO_SND_S_BAD_MSG; + return; + } + cmd->resp.code = virtio_snd_pcm_release_impl(stream, stream_id); +} + /* * The actual processing done in virtio_snd_process_cmdq(). * @@ -528,7 +577,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) virtio_snd_handle_pcm_prepare(s, cmd); break; case VIRTIO_SND_R_PCM_RELEASE: - cmd->resp.code = VIRTIO_SND_S_NOT_SUPP; + virtio_snd_handle_pcm_release(s, cmd); break; case VIRTIO_SND_R_CHMAP_INFO: qemu_log_mask(LOG_UNIMP, From patchwork Sat Jul 29 10:37:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333113 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7D1B1C0015E for ; Sat, 29 Jul 2023 11:24:33 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhM9-0000mJ-2g; Sat, 29 Jul 2023 06:39:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhM7-0000m6-CZ for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:27 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhM5-0001BP-CA for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:27 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3fe110de46dso11350085e9.1 for ; Sat, 29 Jul 2023 03:39:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627164; x=1691231964; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6tnLdt2QM+N2hyryCHvRbS6Fo4+6i2BUhEirVgE1n9o=; b=GO80mN7QDwmlpVSKLDDVQERhURI1DvvuTh7lRKPC9xzjqXuue26kTlWEB1lzK+TfFg 0a5tIWey38L+0Y5FiSA4R1IBuP4+rJcY3soeFmdztilVYYZTG0GbZftCpM/42TuQpJ1X 8l6p1i+tx9hXoAgzbnjX+CMriQQwwbM1eDWPT+BailWukxxauRSSywMO9ZBXbIrHAwOn +U47nw7eN4Ymnn6bkiGqHiNQbUcVMpuEhjKf3+xha9XBMS3lZGEP65zWXmqfQFa5nUim OT4dqWjdnRI+/1Tup6CvVWuyG8dZpu6NL4NwzstcpAiDIqzsCATgiDpqHh/24fPkujIp kNPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627164; x=1691231964; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6tnLdt2QM+N2hyryCHvRbS6Fo4+6i2BUhEirVgE1n9o=; b=hxplJSjmfa12TvS1/K4x4AVdGihbsfIFVbf+/IEVSOPJdvFiSWZQuG13tRbCGy+++B 2n6T8mx8Rux1R/uLJOktBoYhOGnoQP1wQTIMDB7CqAw7Pmb3ZkNfe4Ux6nNAyYTEEVBQ CN79N1jyfKz4HAiC4Js/mA9RvkarNf+MRpvi9/0+DIBbiJ1ygvntgb/63NC+W6DI/phX M+Hm1hY1N42Vt9mqvmRSALmV3dcvi+g0JVRh7MBIePeN/mSPmRz6SNYLsZkg5vzH7q/3 xx8oK4JlT1jk424um4kGcfr0GoCHcfNlqfOqn5MsP5VtpSUtSIMg91G2WTrkzw6jDTwR 4pHQ== X-Gm-Message-State: ABy/qLbmPbjzSzJj1wQrre7rMGosY47kFort+ZNDUXgxCSqnIPUUmBGd /8YRkuehDhlfpwYI+2gtDOs0AznnHM/12RHMZXyRFg== X-Google-Smtp-Source: APBJJlHTxPcLayjkZB7LG7Gr3OhQx9/s6JIsrObVBcbZysli1ZUQeGwPtCPCRhWHRVYhkuQhydlatg== X-Received: by 2002:a7b:c44b:0:b0:3fb:c9f4:150e with SMTP id l11-20020a7bc44b000000b003fbc9f4150emr3256807wmi.14.1690627163870; Sat, 29 Jul 2023 03:39:23 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:23 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 11/12] virtio-sound: implement audio output (TX) Date: Sat, 29 Jul 2023 13:37:03 +0300 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x335.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Handle output IO messages in the transmit (TX) virtqueue. It allocates a VirtIOSoundPCMBlock for each IO message and copies the data buffer to it. When the IO buffer is written to the host's sound card, the guest will be notified that it has been consumed. The lifetime of an IO message is: 1. Guest sends IO message to TX virtqueue. 2. QEMU adds it to the appropriate stream's IO buffer queue. 3. Sometime later, the host audio backend calls the output callback, virtio_snd_pcm_out_cb(), which is defined with an AUD_open_out() call. The callback gets an available number of bytes the backend can receive. Then it writes data from the IO buffer queue to the backend. If at any time a buffer is exhausted, it is returned to the guest as completed. 4. If the guest releases the stream, its buffer queue is flushed by attempting to write any leftover data to the audio backend and releasing all IO messages back to the guest. This is how according to the spec the guest knows the release was successful. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 2 + hw/virtio/virtio-snd.c | 254 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-snd.h | 11 ++ 3 files changed, 260 insertions(+), 7 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 60ab62a80d..3b95e745c2 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -171,3 +171,5 @@ virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE called fo virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s" virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" +virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32 +virtio_snd_handle_xfer(void) "tx/rx queue callback called" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index 5065d62e40..28cbec47f3 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -30,6 +30,15 @@ #define VIRTIO_SOUND_CHMAP_DEFAULT 0 #define VIRTIO_SOUND_HDA_FN_NID 0 +static void virtio_snd_pcm_out_cb(void *data, int available); +static void virtio_snd_process_cmdq(VirtIOSound *s); +static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream); +static uint32_t +virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element, + bool read); + static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8) | BIT(VIRTIO_SND_PCM_FMT_U8) | BIT(VIRTIO_SND_PCM_FMT_S16) @@ -416,6 +425,17 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id) stream->positions[0] = VIRTIO_SND_CHMAP_FL; stream->positions[1] = VIRTIO_SND_CHMAP_FR; + if (stream->direction == VIRTIO_SND_D_OUTPUT) { + stream->voice.out = AUD_open_out(&s->card, + stream->voice.out, + "virtio_snd_card", + stream, + virtio_snd_pcm_out_cb, + &as); + } else { + qemu_log_mask(LOG_UNIMP, "virtio_snd: input/capture is unimplemented."); + } + stream->as = as; stream->desired_as = stream->as; qemu_mutex_init(&stream->queue_mutex); @@ -475,12 +495,35 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s, "VIRTIO_SND_R_PCM_STOP", req.stream_id); stream = virtio_snd_pcm_get_stream(s, req.stream_id); - if (!stream) { + if (stream) { + if (stream->direction == VIRTIO_SND_D_OUTPUT) { + AUD_set_active_out(stream->voice.out, start); + } + } else { error_report("Invalid stream id: %"PRIu32, req.stream_id); cmd->resp.code = VIRTIO_SND_S_BAD_MSG; } } +/* + * Returns the number of bytes that have not been passed to AUD_write yet. + * + * @stream: VirtIOSoundPCMStream + */ +static size_t virtio_snd_pcm_get_pending_bytes(VirtIOSoundPCMStream *stream) +{ + VirtIOSoundPCMBlock *block; + VirtIOSoundPCMBlock *next; + size_t size = 0; + + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { + size += block->size; + } + } + return size; +} + /* * Releases the buffer resources allocated to a stream. Seperated from the * handler so that the code can be reused in the unrealize function. Returns @@ -492,6 +535,21 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s, static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundPCMStream *stream, uint32_t stream_id) { + if (virtio_snd_pcm_get_pending_bytes(stream)) { + /* + * virtio-v1.2-csd01, 5.14.6.6.5.1, + * Device Requirements: Stream Release + * + * - The device MUST complete all pending I/O messages for the + * specified stream ID. + * - The device MUST NOT complete the control request while there + * are pending I/O messages for the specified stream ID. + */ + virtio_snd_process_cmdq(stream->s); + trace_virtio_snd_pcm_stream_flush(stream_id); + virtio_snd_pcm_flush(stream); + } + return VIRTIO_SND_S_OK; } @@ -676,6 +734,79 @@ static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq) trace_virtio_snd_handle_event(); } +/* + * The tx virtqueue handler. Makes the buffers available to their respective + * streams for consumption. + * + * @vdev: VirtIOSound device + * @vq: tx virtqueue + */ +static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOSound *s = VIRTIO_SND(vdev); + VirtIOSoundPCMStream *stream = NULL; + VirtQueueElement *elem; + size_t sz; + virtio_snd_pcm_xfer hdr; + virtio_snd_pcm_status resp = { 0 }; + + trace_virtio_snd_handle_xfer(); + + for (;;) { + elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!elem) { + break; + } + /* get the message hdr object */ + sz = iov_to_buf(elem->out_sg, + elem->out_num, + 0, + &hdr, + sizeof(hdr)); + if (sz != sizeof(hdr) + || hdr.stream_id >= s->snd_conf.streams + || !s->pcm->streams[hdr.stream_id]) { + goto tx_err; + } + + stream = s->pcm->streams[hdr.stream_id]; + if (stream->direction == VIRTIO_SND_D_INPUT) { + goto tx_err; + } + + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + virtio_snd_pcm_read_write(stream, + vq, + elem, + hdr.stream_id == VIRTIO_SND_D_INPUT); + + resp.status = VIRTIO_SND_S_OK; + iov_from_buf(elem->in_sg, + elem->in_num, + 0, + &resp, + sizeof(resp)); + } + continue; + +tx_err: + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + resp.status = VIRTIO_SND_S_BAD_MSG; + iov_from_buf(elem->in_sg, + elem->in_num, + 0, + &resp, + sizeof(resp)); + } + } + + /* + * Notify vq about virtio_snd_pcm_status responses. + * Buffer responses must be notified separately later. + */ + virtio_notify(VIRTIO_DEVICE(s), vq); +} + /* * Stub buffer virtqueue handler. * @@ -811,11 +942,78 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) virtio_snd_common_realize(dev, virtio_snd_handle_ctrl, virtio_snd_handle_event, - virtio_snd_handle_xfer, + virtio_snd_handle_tx, virtio_snd_handle_xfer, errp); } +/* + * AUD_* output callback. + * + * @data: VirtIOSoundPCMStream stream + * @available: number of bytes that can be written with AUD_write() + */ +static void virtio_snd_pcm_out_cb(void *data, int available) +{ + VirtIOSoundPCMStream *stream = data; + VirtIOSoundPCMBlock *block; + VirtIOSoundPCMBlock *next; + size_t size; + + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { + for (;;) { + size = MIN(block->size, available); + size = AUD_write(stream->voice.out, + block->data + block->offset, + size); + block->size -= size; + block->offset += size; + if (!block->size) { + virtqueue_push(block->vq, + block->elem, + sizeof(block->elem)); + virtio_notify(VIRTIO_DEVICE(stream->s), + block->vq); + QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry); + g_free(block); + available -= size; + break; + } + + available -= size; + if (!available) { + break; + } + } + if (!available) { + break; + } + } + } +} + +/* + * Flush all buffer data from this stream's queue into the driver's virtual + * queue. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream) +{ + VirtIOSoundPCMBlock *block; + VirtIOSoundPCMBlock *next; + + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { + AUD_write(stream->voice.out, block->data, block->size); + virtqueue_push(block->vq, block->elem, sizeof(block->elem)); + virtio_notify(VIRTIO_DEVICE(stream->s), block->vq); + QSIMPLEQ_REMOVE(&stream->queue, block, VirtIOSoundPCMBlock, entry); + } + } +} + /* * Close the sound card. * @@ -824,6 +1022,10 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream) { virtio_snd_process_cmdq(stream->s); + if (stream->direction == VIRTIO_SND_D_OUTPUT) { + AUD_close_out(&stream->pcm->snd->card, stream->voice.out); + stream->voice.out = NULL; + } } static void virtio_snd_unrealize(DeviceState *dev) @@ -837,17 +1039,55 @@ static void virtio_snd_unrealize(DeviceState *dev) trace_virtio_snd_unrealize(vsnd); - for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { - stream = vsnd->pcm->streams[i]; - virtio_snd_pcm_close(stream); - g_free(stream); + if (vsnd->pcm) { + if (vsnd->pcm->streams) { + for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { + stream = vsnd->pcm->streams[i]; + if (stream) { + virtio_snd_pcm_close(stream); + g_free(stream); + } + } + g_free(vsnd->pcm->streams); + g_free(vsnd->pcm->pcm_params); + g_free(vsnd->pcm->jacks); + } + + g_free(vsnd->pcm); } + + vsnd->pcm = NULL; AUD_remove_card(&vsnd->card); - g_free(vsnd->pcm); virtio_cleanup(vdev); } +static uint32_t +virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element, + bool read) +{ + VirtIOSoundPCMBlock *fragment; + size_t size = iov_size(element->out_sg, element->out_num) - + sizeof(virtio_snd_pcm_xfer); + + fragment = g_malloc0(sizeof(VirtIOSoundPCMBlock) + size); + fragment->elem = element; + fragment->vq = vq; + fragment->size = size; + fragment->offset = 0; + + iov_to_buf(element->out_sg, element->out_num, + sizeof(virtio_snd_pcm_xfer), + fragment->data, + size); + + QSIMPLEQ_INSERT_TAIL(&stream->queue, fragment, entry); + + return fragment->size; +} + static void virtio_snd_reset(VirtIODevice *vdev) { VirtIOSound *s = VIRTIO_SND(vdev); diff --git a/include/hw/virtio/virtio-snd.h b/include/hw/virtio/virtio-snd.h index 7cc5061fa8..6665c8a703 100644 --- a/include/hw/virtio/virtio-snd.h +++ b/include/hw/virtio/virtio-snd.h @@ -81,6 +81,8 @@ typedef struct VirtIOSoundPCMParams VirtIOSoundPCMParams; typedef struct VirtIOSoundPCM VirtIOSoundPCM; +typedef struct VirtIOSoundPCMBlock VirtIOSoundPCMBlock; + /* Stream params */ struct VirtIOSoundPCMParams { uint32_t features; @@ -91,6 +93,15 @@ struct VirtIOSoundPCMParams { uint8_t rate; }; +struct VirtIOSoundPCMBlock { + QSIMPLEQ_ENTRY(VirtIOSoundPCMBlock) entry; + VirtQueueElement *elem; + VirtQueue *vq; + size_t size; + uint64_t offset; + uint8_t data[]; +}; + struct VirtIOSoundPCM { VirtIOSound *snd; VirtIOSoundPCMParams **pcm_params; From patchwork Sat Jul 29 10:37:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manos Pitsidianakis X-Patchwork-Id: 13333111 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3FBECC0015E for ; Sat, 29 Jul 2023 11:16:24 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qPhMD-0000me-Jv; Sat, 29 Jul 2023 06:39:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPhMB-0000mV-IO for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:31 -0400 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qPhM9-0001De-Jc for qemu-devel@nongnu.org; Sat, 29 Jul 2023 06:39:31 -0400 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3fbc0609cd6so29247705e9.1 for ; Sat, 29 Jul 2023 03:39:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690627168; x=1691231968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f0fGzg2Z3WTCndm2Mm9WOZ8jXGNNLrDMwYsbvobnzvg=; b=bKtDOMq5eBr9jzUPggE6vHhmN7i9S+HfhBhykznGnd5qu3WcLbNeUInWoFbMzSxvne fCed7jofnVqpVi/eY3/72YRCwffswdS1klcKhrmEuWmY9Dar9mTWgUcuKK+oQmjoe6Dx M3St7nnWLHQWph+ZcNo7AwYEuHC4DjP8v9A5/rVrIdkcDxk0rr57EmLOJBDfZm9o8FNq 5RRPl0DrWfwDKe+DTIb8Pew/zM/h+bnHhK20Y01glsT37inrGqQo4hMSpzJ+lJ1V+HW2 ee9FJ6rTaQXV6OhRCyg7BW5VJeE1+C94oB7toduwuvr2w93j0td9/0XiXTde0p9MRVVz /RJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690627168; x=1691231968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f0fGzg2Z3WTCndm2Mm9WOZ8jXGNNLrDMwYsbvobnzvg=; b=J8kElTH3527a+qzgLiYnvAdBdl8VthE6ZZyBMpilRSihtQ/SrpCYNNxHtOEL3Zwjww LZdm+NmHnF6Fb3hd9KJ+oL5IduTbPybNrELKfDCcZmQ5lXTS63nTdJ7wBeU6szgmqLvO 4XkG3YSarf1tsisKdPl5uaZI8cqZ1ZbZux5qBTymgOsO2A8CUF4oylQ12UBcYNLdGSe+ x+iLbQmxHBib8OpVbDfrzHtwRPrK+81HWPsygZjgG+hYCNxcb8lRCoIYERQ8PSfdRWmX SR05Hl4yYUwfIsjzjI6IQdjpWXZS3cd+CzpSUDDcj8YKLRALmQY/JBM4DkVTrCbhnhe0 pjMg== X-Gm-Message-State: ABy/qLb/Lxtyvw4CEeSu/YsDuP/V6oy1mYzp0tmH4tfQ3/BfoyTFS/O3 d8aMKNs3ZR9OQJ+l2Lx/VXj/t3XZ4PTd9wDY4TE2rg== X-Google-Smtp-Source: APBJJlF5nRZKqxyWwUUEFy/iWEq3PLLnb7Rb9jWTSOhT+bVfYy3wnM0rCCGZ6iuhmO5TjfeywcuS2w== X-Received: by 2002:a1c:7c0b:0:b0:3fc:5606:c246 with SMTP id x11-20020a1c7c0b000000b003fc5606c246mr3360507wmc.22.1690627168033; Sat, 29 Jul 2023 03:39:28 -0700 (PDT) Received: from localhost.localdomain (adsl-200.37.6.161.tellas.gr. [37.6.161.200]) by smtp.gmail.com with ESMTPSA id x20-20020a1c7c14000000b003fc01f7b415sm8984777wmc.39.2023.07.29.03.39.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 03:39:27 -0700 (PDT) From: Emmanouil Pitsidianakis To: qemu-devel@nongnu.org Cc: Emmanouil Pitsidianakis , "Igor Skalkin" , "Anton Yakovlev" , "Paolo Bonzini" , "Gerd Hoffmann" , "Michael S. Tsirkin" , "Marcel Apfelbaum" , =?utf-8?q?Daniel_P=2E_Berr?= =?utf-8?q?ang=C3=A9?= , "Eduardo Habkost" , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Volker_R=C3=BCmelin?= , =?utf-8?b?S8WRdsOh?= =?utf-8?b?Z8OzLCBab2x0w6Fu?= , "Alex Bennee" , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v5 12/12] virtio-sound: implement audio capture (RX) Date: Sat, 29 Jul 2023 13:37:04 +0300 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x32e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org To perform audio capture we duplicate the TX logic of the previous commit with the following difference: we receive data from the QEMU audio backend and write it in the virt queue IO buffers the guest sends to QEMU. When they are full (i.e. they have `period_bytes` amount of data) or when recording stops in QEMU's audio backend, the buffer is returned to the guest by notifying it. Signed-off-by: Emmanouil Pitsidianakis --- hw/virtio/trace-events | 3 +- hw/virtio/virtio-snd.c | 230 +++++++++++++++++++++++++++++++++++------ 2 files changed, 200 insertions(+), 33 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 3b95e745c2..a5829b112c 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -172,4 +172,5 @@ virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PR virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called" virtio_snd_handle_event(void) "event queue callback called" virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32 -virtio_snd_handle_xfer(void) "tx/rx queue callback called" +virtio_snd_handle_tx_xfer(void) "tx queue callback called" +virtio_snd_handle_rx_xfer(void) "tx queue callback called" diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c index 28cbec47f3..c28ace1f24 100644 --- a/hw/virtio/virtio-snd.c +++ b/hw/virtio/virtio-snd.c @@ -26,18 +26,21 @@ #define VIRTIO_SOUND_VM_VERSION 1 #define VIRTIO_SOUND_JACK_DEFAULT 0 -#define VIRTIO_SOUND_STREAM_DEFAULT 1 +#define VIRTIO_SOUND_STREAM_DEFAULT 2 #define VIRTIO_SOUND_CHMAP_DEFAULT 0 #define VIRTIO_SOUND_HDA_FN_NID 0 static void virtio_snd_pcm_out_cb(void *data, int available); static void virtio_snd_process_cmdq(VirtIOSound *s); -static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream); -static uint32_t -virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, - VirtQueue *vq, - VirtQueueElement *element, - bool read); +static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream); +static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream); +static void virtio_snd_pcm_in_cb(void *data, int available); +static uint32_t virtio_snd_pcm_write(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element); +static uint32_t virtio_snd_pcm_read(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element); static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8) | BIT(VIRTIO_SND_PCM_FMT_U8) @@ -433,7 +436,12 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id) virtio_snd_pcm_out_cb, &as); } else { - qemu_log_mask(LOG_UNIMP, "virtio_snd: input/capture is unimplemented."); + stream->voice.in = AUD_open_in(&s->card, + stream->voice.in, + "virtio_snd_card", + stream, + virtio_snd_pcm_in_cb, + &as); } stream->as = as; @@ -498,6 +506,8 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s, if (stream) { if (stream->direction == VIRTIO_SND_D_OUTPUT) { AUD_set_active_out(stream->voice.out, start); + } else { + AUD_set_active_in(stream->voice.in, start); } } else { error_report("Invalid stream id: %"PRIu32, req.stream_id); @@ -547,7 +557,11 @@ static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundPCMStream *stream, */ virtio_snd_process_cmdq(stream->s); trace_virtio_snd_pcm_stream_flush(stream_id); - virtio_snd_pcm_flush(stream); + if (stream->direction == VIRTIO_SND_D_OUTPUT) { + virtio_snd_pcm_out_flush(stream); + } else { + virtio_snd_pcm_in_flush(stream); + } } return VIRTIO_SND_S_OK; @@ -741,7 +755,7 @@ static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq) * @vdev: VirtIOSound device * @vq: tx virtqueue */ -static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq) +static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) { VirtIOSound *s = VIRTIO_SND(vdev); VirtIOSoundPCMStream *stream = NULL; @@ -750,7 +764,7 @@ static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq) virtio_snd_pcm_xfer hdr; virtio_snd_pcm_status resp = { 0 }; - trace_virtio_snd_handle_xfer(); + trace_virtio_snd_handle_tx_xfer(); for (;;) { elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); @@ -774,11 +788,11 @@ static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } + assert(hdr.stream_id != VIRTIO_SND_D_INPUT); WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - virtio_snd_pcm_read_write(stream, + virtio_snd_pcm_write(stream, vq, - elem, - hdr.stream_id == VIRTIO_SND_D_INPUT); + elem); resp.status = VIRTIO_SND_S_OK; iov_from_buf(elem->in_sg, @@ -808,12 +822,54 @@ tx_err: } /* - * Stub buffer virtqueue handler. + * The rx virtqueue handler. Makes the buffers available to their respective + * streams for consumption. * * @vdev: VirtIOSound device - * @vq: virtqueue + * @vq: tx virtqueue */ -static void virtio_snd_handle_xfer(VirtIODevice *vdev, VirtQueue *vq) {} +static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIOSound *s = VIRTIO_SND(vdev); + VirtIOSoundPCMStream *stream = NULL; + VirtQueueElement *elem; + size_t sz; + virtio_snd_pcm_xfer hdr; + + trace_virtio_snd_handle_rx_xfer(); + + for (;;) { + elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); + if (!elem) { + break; + } + /* get the message hdr object */ + sz = iov_to_buf(elem->out_sg, + elem->out_num, + 0, + &hdr, + sizeof(hdr)); + if (sz != sizeof(hdr) + || hdr.stream_id >= s->snd_conf.streams + || !s->pcm->streams[hdr.stream_id]) { + continue; + } + + stream = s->pcm->streams[hdr.stream_id]; + if (stream->direction == VIRTIO_SND_D_OUTPUT) { + continue; + } + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { + virtio_snd_pcm_read(stream, vq, elem); + } + } + + /* + * Notify vq about virtio_snd_pcm_status responses. + * Buffer responses must be notified separately later. + */ + virtio_notify(VIRTIO_DEVICE(s), vq); +} static uint64_t get_features(VirtIODevice *vdev, uint64_t features, Error **errp) @@ -942,8 +998,8 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) virtio_snd_common_realize(dev, virtio_snd_handle_ctrl, virtio_snd_handle_event, - virtio_snd_handle_tx, - virtio_snd_handle_xfer, + virtio_snd_handle_tx_xfer, + virtio_snd_handle_rx_xfer, errp); } @@ -994,26 +1050,119 @@ static void virtio_snd_pcm_out_cb(void *data, int available) } /* - * Flush all buffer data from this stream's queue into the driver's virtual - * queue. + * AUD_* input callback. * - * @stream: VirtIOSoundPCMStream *stream + * @data: VirtIOSoundPCMStream stream + * @available: number of bytes that can be read with AUD_read() */ -static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream) +static void virtio_snd_pcm_in_cb(void *data, int available) { + VirtIOSoundPCMStream *stream = data; VirtIOSoundPCMBlock *block; - VirtIOSoundPCMBlock *next; + uint32_t sz; + virtio_snd_pcm_status resp = { 0 }; + size_t size; WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { - AUD_write(stream->voice.out, block->data, block->size); - virtqueue_push(block->vq, block->elem, sizeof(block->elem)); - virtio_notify(VIRTIO_DEVICE(stream->s), block->vq); - QSIMPLEQ_REMOVE(&stream->queue, block, VirtIOSoundPCMBlock, entry); + while (!QSIMPLEQ_EMPTY(&stream->queue)) { + block = QSIMPLEQ_FIRST(&stream->queue); + + for (;;) { + size = AUD_read(stream->voice.in, + block->data + block->offset, + MIN(stream->period_bytes - block->offset, available)); + block->offset += size; + block->size += size; + if (size == 0 || block->size >= stream->period_bytes) { + resp.status = VIRTIO_SND_S_OK; + sz = iov_from_buf(block->elem->in_sg, + block->elem->in_num, + 0, + &resp, + sizeof(resp)); + + /* Copy data -if any- to guest */ + if (block->size) { + iov_from_buf(block->elem->in_sg, + block->elem->in_num, + sz, + &block->data, + MIN(stream->period_bytes, block->size)); + } + virtqueue_push(block->vq, + block->elem, + sizeof(block->elem)); + virtio_notify(VIRTIO_DEVICE(stream->s), + block->vq); + QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry); + g_free(block); + available -= size; + break; + } + + available -= size; + if (!available) { + break; + } + } + if (!available) { + break; + } } } } +#define virtio_snd_pcm_flush(AUD_CB) \ + VirtIOSoundPCMBlock *block; \ + VirtIOSoundPCMBlock *next; \ + WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { \ + QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) { \ + do { \ + AUD_CB; \ + } while (0) \ + ; \ + virtqueue_push(block->vq, block->elem, sizeof(block->elem));\ + virtio_notify(VIRTIO_DEVICE(stream->s), block->vq); \ + QSIMPLEQ_REMOVE(&stream->queue, \ + block, \ + VirtIOSoundPCMBlock, \ + entry); \ + } \ + } \ + + +/* + * Flush all buffer data from this output stream's queue into the driver's + * virtual queue. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream) +{ + virtio_snd_pcm_flush( + AUD_write(stream->voice.out, + block->data, + block->size); + ); +} + +/* + * Flush all buffer data from this input stream's queue into the driver's + * virtual queue. + * + * @stream: VirtIOSoundPCMStream *stream + */ +static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream) +{ + virtio_snd_pcm_flush( + iov_from_buf(block->elem->in_sg, + block->elem->in_num, + sizeof(virtio_snd_pcm_info), + block->data, + block->offset); + ); +} + /* * Close the sound card. * @@ -1063,10 +1212,9 @@ static void virtio_snd_unrealize(DeviceState *dev) static uint32_t -virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, +virtio_snd_pcm_write(VirtIOSoundPCMStream *stream, VirtQueue *vq, - VirtQueueElement *element, - bool read) + VirtQueueElement *element) { VirtIOSoundPCMBlock *fragment; size_t size = iov_size(element->out_sg, element->out_num) - @@ -1088,6 +1236,24 @@ virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream, return fragment->size; } +static uint32_t +virtio_snd_pcm_read(VirtIOSoundPCMStream *stream, + VirtQueue *vq, + VirtQueueElement *element) +{ + VirtIOSoundPCMBlock *fragment; + + fragment = g_malloc0(sizeof(VirtIOSoundPCMBlock) + stream->period_bytes); + fragment->elem = element; + fragment->vq = vq; + fragment->size = 0; + fragment->offset = 0; + + QSIMPLEQ_INSERT_TAIL(&stream->queue, fragment, entry); + + return fragment->size; +} + static void virtio_snd_reset(VirtIODevice *vdev) { VirtIOSound *s = VIRTIO_SND(vdev);