xref: /openbmc/linux/sound/virtio/virtio_card.h (revision f40a2867)
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_pcm_substream;
22 
23 /**
24  * struct virtio_snd_queue - Virtqueue wrapper structure.
25  * @lock: Used to synchronize access to a virtqueue.
26  * @vqueue: Underlying virtqueue.
27  */
28 struct virtio_snd_queue {
29 	spinlock_t lock;
30 	struct virtqueue *vqueue;
31 };
32 
33 /**
34  * struct virtio_snd - VirtIO sound card device.
35  * @vdev: Underlying virtio device.
36  * @queues: Virtqueue wrappers.
37  * @card: ALSA sound card.
38  * @ctl_msgs: Pending control request list.
39  * @event_msgs: Device events.
40  * @pcm_list: VirtIO PCM device list.
41  * @substreams: VirtIO PCM substreams.
42  * @nsubstreams: Number of PCM substreams.
43  */
44 struct virtio_snd {
45 	struct virtio_device *vdev;
46 	struct virtio_snd_queue queues[VIRTIO_SND_VQ_MAX];
47 	struct snd_card *card;
48 	struct list_head ctl_msgs;
49 	struct virtio_snd_event *event_msgs;
50 	struct list_head pcm_list;
51 	struct virtio_pcm_substream *substreams;
52 	u32 nsubstreams;
53 };
54 
55 /* Message completion timeout in milliseconds (module parameter). */
56 extern u32 virtsnd_msg_timeout_ms;
57 
58 static inline struct virtio_snd_queue *
59 virtsnd_control_queue(struct virtio_snd *snd)
60 {
61 	return &snd->queues[VIRTIO_SND_VQ_CONTROL];
62 }
63 
64 static inline struct virtio_snd_queue *
65 virtsnd_event_queue(struct virtio_snd *snd)
66 {
67 	return &snd->queues[VIRTIO_SND_VQ_EVENT];
68 }
69 
70 static inline struct virtio_snd_queue *
71 virtsnd_tx_queue(struct virtio_snd *snd)
72 {
73 	return &snd->queues[VIRTIO_SND_VQ_TX];
74 }
75 
76 static inline struct virtio_snd_queue *
77 virtsnd_rx_queue(struct virtio_snd *snd)
78 {
79 	return &snd->queues[VIRTIO_SND_VQ_RX];
80 }
81 
82 static inline struct virtio_snd_queue *
83 virtsnd_pcm_queue(struct virtio_pcm_substream *vss)
84 {
85 	if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
86 		return virtsnd_tx_queue(vss->snd);
87 	else
88 		return virtsnd_rx_queue(vss->snd);
89 }
90 
91 #endif /* VIRTIO_SND_CARD_H */
92