ALSA: virtio: add support for audio controls
Subsystems (3)
Anton Yakovlev
maintainercover ALSA: virtio: add support for audio controls
Implementation of support for audio controls in accordance with the proposed extension of the virtio sound device specification [1].
Design of virtual audio controls is based on and derived from ALSA audio controls. It allows the driver to perform all standard operations, such as reading and writing audio control value, as well as working with metadata (represented in the TLV form).
The driver part was tested on top of the Linux 5.10 kernel.
As a device part was used OpenSynergy proprietary implementation.
[1] https://lists.oasis-open.org/archives/virtio-comment/202104/msg00013.html
Anton Yakovlev (1):
ALSA: virtio: add support for audio controls
include/uapi/linux/virtio_snd.h | 154 +++++++++++ sound/virtio/Makefile | 1 + sound/virtio/virtio_card.c | 21 ++ sound/virtio/virtio_card.h | 22 ++ sound/virtio/virtio_kctl.c | 464 ++++++++++++++++++++++++++++++++ 5 files changed, 662 insertions(+) create mode 100644 sound/virtio/virtio_kctl.c
Signature · 6 lines
2.35.1 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Anton Yakovlev
maintainerPATCH 1/1 ALSA: virtio: add support for audio controls
Implementation of support for audio controls in accordance with the proposed extension of the virtio sound device specification [1].
The device can announce the VIRTIO_SND_F_CTLS feature. If the feature is negotiated, then an additional field appears in the configuration space:
struct virtio_snd_config {
...
/* number of available control elements */
__le32 controls;
};The driver can send the following requests to manage audio controls:
enum {
...
/* control element request types */
VIRTIO_SND_R_CTL_INFO = 0x0300,
VIRTIO_SND_R_CTL_ENUM_ITEMS,
VIRTIO_SND_R_CTL_READ,
VIRTIO_SND_R_CTL_WRITE,
VIRTIO_SND_R_CTL_TLV_READ,
VIRTIO_SND_R_CTL_TLV_WRITE,
VIRTIO_SND_R_CTL_TLV_COMMAND,
...
};And the device can send the following audio control event notification:
enum {
...
/* control element event types */
VIRTIO_SND_EVT_CTL_NOTIFY = 0x1200,
...
};See additional details in [1].
[1] https://lists.oasis-open.org/archives/virtio-comment/202104/msg00013.html
--- include/uapi/linux/virtio_snd.h | 154 +++++++++++ sound/virtio/Makefile | 1 + sound/virtio/virtio_card.c | 21 ++ sound/virtio/virtio_card.h | 22 ++ sound/virtio/virtio_kctl.c | 464 ++++++++++++++++++++++++++++++++ 5 files changed, 662 insertions(+) create mode 100644 sound/virtio/virtio_kctl.c
include/uapi/linux/virtio_snd.h +154 -0
@@ -7,6 +7,14 @@#include <linux/virtio_types.h> +/******************************************************************************* +* FEATURE BITS +*/ +enum { +/* device supports control elements */ +VIRTIO_SND_F_CTLS =0 +}; +/******************************************************************************* * CONFIGURATION SPACE */
@@ -17,6 +25,8 @@ struct virtio_snd_config {__le32 streams;/* # of available channel maps */ __le32 chmaps; +/* # of available control elements */ +__le32 controls; };enum {
@@ -55,6 +65,15 @@ enum {/* channel map control request types */ VIRTIO_SND_R_CHMAP_INFO =0x0200 , +/* control element request types */ +VIRTIO_SND_R_CTL_INFO =0x0300 , +VIRTIO_SND_R_CTL_ENUM_ITEMS , +VIRTIO_SND_R_CTL_READ , +VIRTIO_SND_R_CTL_WRITE , +VIRTIO_SND_R_CTL_TLV_READ , +VIRTIO_SND_R_CTL_TLV_WRITE , +VIRTIO_SND_R_CTL_TLV_COMMAND , +/* jack event types */ VIRTIO_SND_EVT_JACK_CONNECTED =0x1000 , VIRTIO_SND_EVT_JACK_DISCONNECTED,
@@ -63,6 +82,9 @@ enum {VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED =0x1100 ,VIRTIO_SND_EVT_PCM_XRUN , +/* control element event types */ +VIRTIO_SND_EVT_CTL_NOTIFY =0x1200 , +/* common status codes */ VIRTIO_SND_S_OK =0x8000 , VIRTIO_SND_S_BAD_MSG,
@@ -331,4 +353,136 @@ struct virtio_snd_chmap_info {__u8 positions[VIRTIO_SND_CHMAP_MAX_SIZE ]; }; +/******************************************************************************* +* CONTROL ELEMENTS MESSAGES +*/ +struct virtio_snd_ctl_hdr { +/* VIRTIO_SND_R_CTL_XXX */ +struct virtio_snd_hdr hdr; +/* 0 ... virtio_snd_config::controls - 1 */ +__le32 control_id; +}; + +/* supported roles for control elements */ +enum { +VIRTIO_SND_CTL_ROLE_UNDEFINED =0 , +VIRTIO_SND_CTL_ROLE_VOLUME , +VIRTIO_SND_CTL_ROLE_MUTE , +VIRTIO_SND_CTL_ROLE_GAIN +}; + +/* supported value types for control elements */ +enum { +VIRTIO_SND_CTL_TYPE_BOOLEAN =0 , +VIRTIO_SND_CTL_TYPE_INTEGER , +VIRTIO_SND_CTL_TYPE_INTEGER64 , +VIRTIO_SND_CTL_TYPE_ENUMERATED , +VIRTIO_SND_CTL_TYPE_BYTES , +VIRTIO_SND_CTL_TYPE_IEC958 +}; + +/* supported access rights for control elements */ +enum { +VIRTIO_SND_CTL_ACCESS_READ =0 , +VIRTIO_SND_CTL_ACCESS_WRITE , +VIRTIO_SND_CTL_ACCESS_VOLATILE , +VIRTIO_SND_CTL_ACCESS_INACTIVE , +VIRTIO_SND_CTL_ACCESS_TLV_READ , +VIRTIO_SND_CTL_ACCESS_TLV_WRITE , +VIRTIO_SND_CTL_ACCESS_TLV_COMMAND +}; + +struct virtio_snd_ctl_info { +/* common header */ +struct virtio_snd_info hdr; +/* element role (VIRTIO_SND_CTL_ROLE_XXX) */ +__le32 role; +/* element value type (VIRTIO_SND_CTL_TYPE_XXX) */ +__le32 type; +/* element access right bit map (1 << VIRTIO_SND_CTL_ACCESS_XXX) */ +__le32 access; +/* # of members in the element value */ +__le32 count; +/* index for an element with a non-unique name */ +__le32 index; +/* name identifier string for the element */ +__u8 name[44 ]; +/* additional information about the element's value */ +union { +/* VIRTIO_SND_CTL_TYPE_INTEGER */ +struct { +/* minimum supported value */ +__le32 min; +/* maximum supported value */ +__le32 max; +/* fixed step size for value (0 = variable size) */ +__le32 step; + } integer; +/* VIRTIO_SND_CTL_TYPE_INTEGER64 */ +struct { +/* minimum supported value */ +__le64 min; +/* maximum supported value */ +__le64 max; +/* fixed step size for value (0 = variable size) */ +__le64 step; + } integer64; +/* VIRTIO_SND_CTL_TYPE_ENUMERATED */ +struct { +/* # of options supported for value */ +__le32 items; + } enumerated; + } value; +}; + +struct virtio_snd_ctl_enum_item { +/* option name */ +__u8 item[64 ]; +}; + +struct virtio_snd_ctl_iec958 { +/* AES/IEC958 channel status bits */ +__u8 status[24 ]; +/* AES/IEC958 subcode bits */ +__u8 subcode[147 ]; +/* nothing */ +__u8 pad; +/* AES/IEC958 subframe bits */ +__u8 dig_subframe[4 ]; +}; + +struct virtio_snd_ctl_value { +union { +/* VIRTIO_SND_CTL_TYPE_BOOLEAN|INTEGER value */ +__le32 integer[128 ]; +/* VIRTIO_SND_CTL_TYPE_INTEGER64 value */ +__le64 integer64[64 ]; +/* VIRTIO_SND_CTL_TYPE_ENUMERATED value (option indexes) */ +__le32 enumerated[128 ]; +/* VIRTIO_SND_CTL_TYPE_BYTES value */ +__u8 bytes[512 ]; +/* VIRTIO_SND_CTL_TYPE_IEC958 value */ +struct virtio_snd_ctl_iec958 iec958; + } value; +}; + +/* supported event reason types */ +enum { +/* element's value has changed */ +VIRTIO_SND_CTL_EVT_MASK_VALUE =0 , +/* element's information has changed */ +VIRTIO_SND_CTL_EVT_MASK_INFO , +/* element's metadata has changed */ +VIRTIO_SND_CTL_EVT_MASK_TLV +}; + +struct virtio_snd_ctl_event { +/* VIRTIO_SND_EVT_CTL_NOTIFY */ +struct virtio_snd_hdr hdr; +/* 0 ... virtio_snd_config::controls - 1 */ +__le16 control_id; +/* event reason bit map (1 << VIRTIO_SND_CTL_EVT_MASK_XXX) */ +__le16 mask; +}; +#endif /* VIRTIO_SND_IF_H */
sound/virtio/Makefile +1 -0
@@ -7,6 +7,7 @@ virtio_snd-objs := \ virtio_chmap.o \ virtio_ctl_msg.o \ virtio_jack.o \ + virtio_kctl.o \ virtio_pcm.o \ virtio_pcm_msg.o \ virtio_pcm_ops.o
sound/virtio/virtio_card.c +21 -0
@@ -64,6 +64,9 @@ static void virtsnd_event_dispatch(struct virtio_snd *snd,case VIRTIO_SND_EVT_PCM_XRUN :virtsnd_pcm_event (snd, event);break ; +case VIRTIO_SND_EVT_CTL_NOTIFY : +virtsnd_kctl_event (snd, event); +break ; } }
@@ -235,6 +238,12 @@ static int virtsnd_build_devs(struct virtio_snd *snd)if (rc)return rc; +if (virtio_has_feature (vdev,VIRTIO_SND_F_CTLS )) { + rc =virtsnd_kctl_parse_cfg (snd); +if (rc) +return rc; + } +if (snd->njacks) { rc =virtsnd_jack_build_devs (snd);if (rc)
@@ -253,6 +262,12 @@ static int virtsnd_build_devs(struct virtio_snd *snd)return rc; } +if (snd->nkctls) { + rc =virtsnd_kctl_build_devs (snd); +if (rc) +return rc; + } +return snd_card_register (snd->card); }
@@ -419,10 +434,16 @@ static const struct virtio_device_id id_table[] = { { 0 }, }; +static unsigned int features[] = { +VIRTIO_SND_F_CTLS +}; +static struct virtio_driver virtsnd_driver = { .driver.name =KBUILD_MODNAME , .driver.owner =THIS_MODULE , .id_table = id_table, + .feature_table = features, + .feature_table_size =ARRAY_SIZE (features), .validate = virtsnd_validate, .probe = virtsnd_probe, .remove = virtsnd_remove,
sound/virtio/virtio_card.h +22 -0
@@ -31,6 +31,16 @@ struct virtio_snd_queue {struct virtqueue *vqueue; }; +/** +* struct virtio_kctl - VirtIO control element. +* @kctl: ALSA control element. +* @items: Items for the ENUMERATED element type. +*/ +struct virtio_kctl { +struct snd_kcontrol *kctl; +struct virtio_snd_ctl_enum_item *items; +}; +/** * struct virtio_snd - VirtIO sound card device. * @vdev: Underlying virtio device.
@@ -45,6 +55,9 @@ struct virtio_snd_queue {* @nsubstreams: Number of PCM substreams. * @chmaps: VirtIO channel maps. * @nchmaps: Number of channel maps. +* @kctl_infos: VirtIO control element information. +* @kctls: VirtIO control elements. +* @nkctls: Number of control elements. */ struct virtio_snd {struct virtio_device *vdev;
@@ -59,6 +72,9 @@ struct virtio_snd {u32 nsubstreams;struct virtio_snd_chmap_info *chmaps;u32 nchmaps; +struct virtio_snd_ctl_info *kctl_infos; +struct virtio_kctl *kctls; +u32 nkctls; };/* Message completion timeout in milliseconds (module parameter). */
@@ -108,4 +124,10 @@ int virtsnd_chmap_parse_cfg(struct virtio_snd *snd);int virtsnd_chmap_build_devs (struct virtio_snd *snd); +int virtsnd_kctl_parse_cfg (struct virtio_snd *snd); + +int virtsnd_kctl_build_devs (struct virtio_snd *snd); + +void virtsnd_kctl_event (struct virtio_snd *snd,struct virtio_snd_event *event); +#endif /* VIRTIO_SND_CARD_H */
sound/virtio/virtio_kctl.c new file +464 -0
464 lines not on this page · raw message
Signature · 6 lines
2.35.1 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
+/* supported roles for control elements */
+enum {
+ VIRTIO_SND_CTL_ROLE_UNDEFINED = 0, + VIRTIO_SND_CTL_ROLE_VOLUME, + VIRTIO_SND_CTL_ROLE_MUTE, + VIRTIO_SND_CTL_ROLE_GAIN
+};
Regarding "role" values:
1. While a volume-joined control can be represented using a control with "count"=1 and "role"=VIRTIO_SND_CTL_ROLE_VOLUME, it would not be clear on the guest side whether this is a volume control for a one channel sink or joined volume control for a multi channel sink; similar comment for switch-joined (corresponding role would be VIRTIO_SND_CTL_ROLE_MUTE). Is the guest driver supposed to look for a channel map with the same hda_fn_nid value and infer from the relation between audio control "count" value and channel map "channels" value, whether this is joined volume/mute control or not? Could this be addressed in a simpler fashion by having separate "role" bit mask values for joined volume and mute control? 2. There is no representation for a cswitch-exclusive control, so that the cswitch-exclusive capability can be recognized on the guest side 3. What is the difference between VIRTIO_SND_CTL_ROLE_VOLUME and VIRTIO_SND_CTL_ROLE_GAIN? 4. The posted guest driver implementation doesn't make any use of the "role" information provided by the host device as part of struct virtio_snd_ctl_info. Is there a plan to add that in a later revision of the guest driver?
+/* supported value types for control elements */
+enum {
+ VIRTIO_SND_CTL_TYPE_BOOLEAN = 0, + VIRTIO_SND_CTL_TYPE_INTEGER, + VIRTIO_SND_CTL_TYPE_INTEGER64, + VIRTIO_SND_CTL_TYPE_ENUMERATED, + VIRTIO_SND_CTL_TYPE_BYTES, + VIRTIO_SND_CTL_TYPE_IEC958
+};
Regarding relationship between audio control "role" and "type":
If certain data types are assumed for controls with role VIRTIO_SND_CTL_ROLE_VOLUME, VIRTIO_SND_CTL_ROLE_MUTE or VIRTIO_SND_CTL_ROLE_GAIN, it should be specified (e.g. BOOLEAN for VIRTIO_SND_CTL_ROLE_MUTE)
+/* supported access rights for control elements */ +enum {
+ VIRTIO_SND_CTL_ACCESS_READ = 0, + VIRTIO_SND_CTL_ACCESS_WRITE, + VIRTIO_SND_CTL_ACCESS_VOLATILE, + VIRTIO_SND_CTL_ACCESS_INACTIVE, + VIRTIO_SND_CTL_ACCESS_TLV_READ, + VIRTIO_SND_CTL_ACCESS_TLV_WRITE, + VIRTIO_SND_CTL_ACCESS_TLV_COMMAND
+};
Regarding TLVs:
1. The TLV type values are considered well known from ALSA; there are only a few common TLV type definitions (like db scale, db min/max, a few more); they could be defined in virtio-snd.h for the completeness of the specification. Otherwise a non ALSA based host that supports reporting db scale and db min/max would need to redefine these to match the ALSA definitions
2. The original extension proposal used to include the definition of struct virtio_snd_ctl_tlv. It is removed in the guest driver implementation and the guest driver seems to memcpy to/from the equivalent ALSA struct. For the completeness of the specification all data structures used should be described as part of the specification. This is particularly important when the host or guest don't use ALSA.
+struct virtio_snd_ctl_info {
+ /* common header */
+ struct virtio_snd_info hdr;
+ /* element role (VIRTIO_SND_CTL_ROLE_XXX) */
+ __le32 role;
+ /* element value type (VIRTIO_SND_CTL_TYPE_XXX) */
+ __le32 type;
+ /* element access right bit map (1 << VIRTIO_SND_CTL_ACCESS_XXX) */
+ __le32 access;
+ /* # of members in the element value */
+ __le32 count;
+ /* index for an element with a non-unique name */
+ __le32 index;
+ /* name identifier string for the element */
+ __u8 name[44];
+ /* additional information about the element's value */
+ union {
+ /* VIRTIO_SND_CTL_TYPE_INTEGER */
+ struct {
+ /* minimum supported value */
+ __le32 min;
+ /* maximum supported value */
+ __le32 max;
+ /* fixed step size for value (0 = variable size) */
+ __le32 step;
+ } integer;
+ /* VIRTIO_SND_CTL_TYPE_INTEGER64 */
+ struct {
+ /* minimum supported value */
+ __le64 min;
+ /* maximum supported value */
+ __le64 max;
+ /* fixed step size for value (0 = variable size) */
+ __le64 step;
+ } integer64;
+ /* VIRTIO_SND_CTL_TYPE_ENUMERATED */
+ struct {+ /* # of options supported for value */
+ __le32 items;
+ } enumerated;
+ } value;
+};
The field "name" of struct virtio_snd_ctl_info should have length 48 (or another multiple of 8), to ensure 64 bit alignment for the integer64 union member below, regardless of alignment and packing attributes.
----------------------------------------------------------------------
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.