xref: /openbmc/linux/sound/virtio/virtio_card.h (revision f7af616c)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * virtio-snd: Virtio sound device
4  * Copyright (C) 2021 OpenSynergy GmbH
5  */
6 #ifndef VIRTIO_SND_CARD_H
7 #define VIRTIO_SND_CARD_H
8 
9 #include <linux/slab.h>
10 #include <linux/virtio.h>
11 #include <sound/core.h>
12 #include <uapi/linux/virtio_snd.h>
13 
14 #include "virtio_ctl_msg.h"
15 #include "virtio_pcm.h"
16 
17 #define VIRTIO_SND_CARD_DRIVER	"virtio-snd"
18 #define VIRTIO_SND_CARD_NAME	"VirtIO SoundCard"
19 #define VIRTIO_SND_PCM_NAME	"VirtIO PCM"
20 
21 struct virtio_jack;
22 struct virtio_pcm_substream;
23 
24 /**
25  * struct virtio_snd_queue - Virtqueue wrapper structure.
26  * @lock: Used to synchronize access to a virtqueue.
27  * @vqueue: Underlying virtqueue.
28  */
29 struct virtio_snd_queue {
30 	spinlock_t lock;
31 	struct virtqueue *vqueue;
32 };
33 
34 /**
35  * struct virtio_snd - VirtIO sound card device.
36  * @vdev: Underlying virtio device.
37  * @queues: Virtqueue wrappers.
38  * @card: ALSA sound card.
39  * @ctl_msgs: Pending control request list.
40  * @event_msgs: Device events.
41  * @pcm_list: VirtIO PCM device list.
42  * @jacks: VirtIO jacks.
43  * @njacks: Number of jacks.
44  * @substreams: VirtIO PCM substreams.
45  * @nsubstreams: Number of PCM substreams.
46  * @chmaps: VirtIO channel maps.
47  * @nchmaps: Number of channel maps.
48  */
49 struct virtio_snd {
50 	struct virtio_device *vdev;
51 	struct virtio_snd_queue queues[VIRTIO_SND_VQ_MAX];
52 	struct snd_card *card;
53 	struct list_head ctl_msgs;
54 	struct virtio_snd_event *event_msgs;
55 	struct list_head pcm_list;
56 	struct virtio_jack *jacks;
57 	u32 njacks;
58 	struct virtio_pcm_substream *substreams;
59 	u32 nsubstreams;
60 	struct virtio_snd_chmap_info *chmaps;
61 	u32 nchmaps;
62 };
63 
64 /* Message completion timeout in milliseconds (module parameter). */
65 extern u32 virtsnd_msg_timeout_ms;
66 
67 static inline struct virtio_snd_queue *
68 virtsnd_control_queue(struct virtio_snd *snd)
69 {
70 	return &snd->queues[VIRTIO_SND_VQ_CONTROL];
71 }
72 
73 static inline struct virtio_snd_queue *
74 virtsnd_event_queue(struct virtio_snd *snd)
75 {
76 	return &snd->queues[VIRTIO_SND_VQ_EVENT];
77 }
78 
79 static inline struct virtio_snd_queue *
80 virtsnd_tx_queue(struct virtio_snd *snd)
81 {
82 	return &snd->queues[VIRTIO_SND_VQ_TX];
83 }
84 
85 static inline struct virtio_snd_queue *
86 virtsnd_rx_queue(struct virtio_snd *snd)
87 {
88 	return &snd->queues[VIRTIO_SND_VQ_RX];
89 }
90 
91 static inline struct virtio_snd_queue *
92 virtsnd_pcm_queue(struct virtio_pcm_substream *vss)
93 {
94 	if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
95 		return virtsnd_tx_queue(vss->snd);
96 	else
97 		return virtsnd_rx_queue(vss->snd);
98 }
99 
100 int virtsnd_jack_parse_cfg(struct virtio_snd *snd);
101 
102 int virtsnd_jack_build_devs(struct virtio_snd *snd);
103 
104 void virtsnd_jack_event(struct virtio_snd *snd,
105 			struct virtio_snd_event *event);
106 
107 int virtsnd_chmap_parse_cfg(struct virtio_snd *snd);
108 
109 int virtsnd_chmap_build_devs(struct virtio_snd *snd);
110 
111 #endif /* VIRTIO_SND_CARD_H */
112