xref: /openbmc/linux/sound/virtio/virtio_pcm.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_PCM_H
7 #define VIRTIO_SND_PCM_H
8 
9 #include <linux/atomic.h>
10 #include <linux/virtio_config.h>
11 #include <sound/pcm.h>
12 
13 struct virtio_pcm;
14 struct virtio_pcm_msg;
15 
16 /**
17  * struct virtio_pcm_substream - VirtIO PCM substream.
18  * @snd: VirtIO sound device.
19  * @nid: Function group node identifier.
20  * @sid: Stream identifier.
21  * @direction: Stream data flow direction (SNDRV_PCM_STREAM_XXX).
22  * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX).
23  * @substream: Kernel ALSA substream.
24  * @hw: Kernel ALSA substream hardware descriptor.
25  * @elapsed_period: Kernel work to handle the elapsed period state.
26  * @lock: Spinlock that protects fields shared by interrupt handlers and
27  *        substream operators.
28  * @buffer_bytes: Current buffer size in bytes.
29  * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes).
30  * @xfer_enabled: Data transfer state (0 - off, 1 - on).
31  * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun).
32  * @msgs: Allocated I/O messages.
33  * @nmsgs: Number of allocated I/O messages.
34  * @msg_last_enqueued: Index of the last I/O message added to the virtqueue.
35  * @msg_count: Number of pending I/O messages in the virtqueue.
36  * @msg_empty: Notify when msg_count is zero.
37  */
38 struct virtio_pcm_substream {
39 	struct virtio_snd *snd;
40 	u32 nid;
41 	u32 sid;
42 	u32 direction;
43 	u32 features;
44 	struct snd_pcm_substream *substream;
45 	struct snd_pcm_hardware hw;
46 	struct work_struct elapsed_period;
47 	spinlock_t lock;
48 	size_t buffer_bytes;
49 	size_t hw_ptr;
50 	bool xfer_enabled;
51 	bool xfer_xrun;
52 	struct virtio_pcm_msg **msgs;
53 	unsigned int nmsgs;
54 	int msg_last_enqueued;
55 	unsigned int msg_count;
56 	wait_queue_head_t msg_empty;
57 };
58 
59 /**
60  * struct virtio_pcm_stream - VirtIO PCM stream.
61  * @substreams: VirtIO substreams belonging to the stream.
62  * @nsubstreams: Number of substreams.
63  */
64 struct virtio_pcm_stream {
65 	struct virtio_pcm_substream **substreams;
66 	u32 nsubstreams;
67 };
68 
69 /**
70  * struct virtio_pcm - VirtIO PCM device.
71  * @list: VirtIO PCM list entry.
72  * @nid: Function group node identifier.
73  * @pcm: Kernel PCM device.
74  * @streams: VirtIO PCM streams (playback and capture).
75  */
76 struct virtio_pcm {
77 	struct list_head list;
78 	u32 nid;
79 	struct snd_pcm *pcm;
80 	struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1];
81 };
82 
83 int virtsnd_pcm_validate(struct virtio_device *vdev);
84 
85 int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
86 
87 int virtsnd_pcm_build_devs(struct virtio_snd *snd);
88 
89 void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event);
90 
91 void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue);
92 
93 void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue);
94 
95 struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid);
96 
97 struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid);
98 
99 struct virtio_snd_msg *
100 virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
101 			  unsigned int command, gfp_t gfp);
102 
103 int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
104 			  unsigned int periods, unsigned int period_bytes);
105 
106 void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss);
107 
108 int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss);
109 
110 unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss);
111 
112 #endif /* VIRTIO_SND_PCM_H */
113