1 /* SPDX-License-Identifier: GPL-2.0
2 *
3 * compress_driver.h - compress offload driver definations
4 *
5 * Copyright (C) 2011 Intel Corporation
6 * Authors: Vinod Koul <vinod.koul@linux.intel.com>
7 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
8 */
9
10 #ifndef __COMPRESS_DRIVER_H
11 #define __COMPRESS_DRIVER_H
12
13 #include <linux/types.h>
14 #include <linux/sched.h>
15 #include <linux/android_kabi.h>
16 #include <sound/core.h>
17 #include <sound/compress_offload.h>
18 #include <sound/asound.h>
19 #include <sound/pcm.h>
20
21 struct snd_compr_ops;
22
23 /**
24 * struct snd_compr_runtime: runtime stream description
25 * @state: stream state
26 * @ops: pointer to DSP callbacks
27 * @buffer: pointer to kernel buffer, valid only when not in mmap mode or
28 * DSP doesn't implement copy
29 * @buffer_size: size of the above buffer
30 * @fragment_size: size of buffer fragment in bytes
31 * @fragments: number of such fragments
32 * @total_bytes_available: cumulative number of bytes made available in
33 * the ring buffer
34 * @total_bytes_transferred: cumulative bytes transferred by offload DSP
35 * @sleep: poll sleep
36 * @private_data: driver private data pointer
37 * @dma_area: virtual buffer address
38 * @dma_addr: physical buffer address (not accessible from main CPU)
39 * @dma_bytes: size of DMA area
40 * @dma_buffer_p: runtime dma buffer pointer
41 */
42 struct snd_compr_runtime {
43 snd_pcm_state_t state;
44 struct snd_compr_ops *ops;
45 void *buffer;
46 u64 buffer_size;
47 u32 fragment_size;
48 u32 fragments;
49 u64 total_bytes_available;
50 u64 total_bytes_transferred;
51 wait_queue_head_t sleep;
52 void *private_data;
53
54 unsigned char *dma_area;
55 dma_addr_t dma_addr;
56 size_t dma_bytes;
57 struct snd_dma_buffer *dma_buffer_p;
58
59 ANDROID_KABI_RESERVE(1);
60 };
61
62 /**
63 * struct snd_compr_stream: compressed stream
64 * @name: device name
65 * @ops: pointer to DSP callbacks
66 * @runtime: pointer to runtime structure
67 * @device: device pointer
68 * @error_work: delayed work used when closing the stream due to an error
69 * @direction: stream direction, playback/recording
70 * @metadata_set: metadata set flag, true when set
71 * @next_track: has userspace signal next track transition, true when set
72 * @partial_drain: undergoing partial_drain for stream, true when set
73 * @pause_in_draining: paused during draining state, true when set
74 * @private_data: pointer to DSP private data
75 * @dma_buffer: allocated buffer if any
76 */
77 struct snd_compr_stream {
78 const char *name;
79 struct snd_compr_ops *ops;
80 struct snd_compr_runtime *runtime;
81 struct snd_compr *device;
82 struct delayed_work error_work;
83 enum snd_compr_direction direction;
84 bool metadata_set;
85 bool next_track;
86 bool partial_drain;
87 bool pause_in_draining;
88 void *private_data;
89 struct snd_dma_buffer dma_buffer;
90
91 ANDROID_KABI_RESERVE(1);
92 };
93
94 /**
95 * struct snd_compr_ops: compressed path DSP operations
96 * @open: Open the compressed stream
97 * This callback is mandatory and shall keep dsp ready to receive the stream
98 * parameter
99 * @free: Close the compressed stream, mandatory
100 * @set_params: Sets the compressed stream parameters, mandatory
101 * This can be called in during stream creation only to set codec params
102 * and the stream properties
103 * @get_params: retrieve the codec parameters, mandatory
104 * @set_metadata: Set the metadata values for a stream
105 * @get_metadata: retrieves the requested metadata values from stream
106 * @trigger: Trigger operations like start, pause, resume, drain, stop.
107 * This callback is mandatory
108 * @pointer: Retrieve current h/w pointer information. Mandatory
109 * @copy: Copy the compressed data to/from userspace, Optional
110 * Can't be implemented if DSP supports mmap
111 * @mmap: DSP mmap method to mmap DSP memory
112 * @ack: Ack for DSP when data is written to audio buffer, Optional
113 * Not valid if copy is implemented
114 * @get_caps: Retrieve DSP capabilities, mandatory
115 * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
116 */
117 struct snd_compr_ops {
118 int (*open)(struct snd_compr_stream *stream);
119 int (*free)(struct snd_compr_stream *stream);
120 int (*set_params)(struct snd_compr_stream *stream,
121 struct snd_compr_params *params);
122 int (*get_params)(struct snd_compr_stream *stream,
123 struct snd_codec *params);
124 int (*set_metadata)(struct snd_compr_stream *stream,
125 struct snd_compr_metadata *metadata);
126 int (*get_metadata)(struct snd_compr_stream *stream,
127 struct snd_compr_metadata *metadata);
128 int (*trigger)(struct snd_compr_stream *stream, int cmd);
129 int (*pointer)(struct snd_compr_stream *stream,
130 struct snd_compr_tstamp *tstamp);
131 int (*copy)(struct snd_compr_stream *stream, char __user *buf,
132 size_t count);
133 int (*mmap)(struct snd_compr_stream *stream,
134 struct vm_area_struct *vma);
135 int (*ack)(struct snd_compr_stream *stream, size_t bytes);
136 int (*get_caps) (struct snd_compr_stream *stream,
137 struct snd_compr_caps *caps);
138 int (*get_codec_caps) (struct snd_compr_stream *stream,
139 struct snd_compr_codec_caps *codec);
140
141 ANDROID_KABI_RESERVE(1);
142 };
143
144 /**
145 * struct snd_compr: Compressed device
146 * @name: DSP device name
147 * @dev: associated device instance
148 * @ops: pointer to DSP callbacks
149 * @private_data: pointer to DSP pvt data
150 * @card: sound card pointer
151 * @direction: Playback or capture direction
152 * @lock: device lock
153 * @device: device id
154 * @use_pause_in_draining: allow pause in draining, true when set
155 */
156 struct snd_compr {
157 const char *name;
158 struct device dev;
159 struct snd_compr_ops *ops;
160 void *private_data;
161 struct snd_card *card;
162 unsigned int direction;
163 struct mutex lock;
164 int device;
165 bool use_pause_in_draining;
166 #ifdef CONFIG_SND_VERBOSE_PROCFS
167 /* private: */
168 char id[64];
169 struct snd_info_entry *proc_root;
170 struct snd_info_entry *proc_info_entry;
171 #endif
172 ANDROID_KABI_RESERVE(1);
173 };
174
175 /* compress device register APIs */
176 int snd_compress_new(struct snd_card *card, int device,
177 int type, const char *id, struct snd_compr *compr);
178
179 /**
180 * snd_compr_use_pause_in_draining - Allow pause and resume in draining state
181 * @substream: compress substream to set
182 *
183 * Allow pause and resume in draining state.
184 * Only HW driver supports this transition can call this API.
185 */
snd_compr_use_pause_in_draining(struct snd_compr_stream * substream)186 static inline void snd_compr_use_pause_in_draining(struct snd_compr_stream *substream)
187 {
188 substream->device->use_pause_in_draining = true;
189 }
190
191 /* dsp driver callback apis
192 * For playback: driver should call snd_compress_fragment_elapsed() to let the
193 * framework know that a fragment has been consumed from the ring buffer
194 *
195 * For recording: we want to know when a frame is available or when
196 * at least one frame is available so snd_compress_frame_elapsed()
197 * callback should be called when a encodeded frame is available
198 */
snd_compr_fragment_elapsed(struct snd_compr_stream * stream)199 static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
200 {
201 wake_up(&stream->runtime->sleep);
202 }
203
snd_compr_drain_notify(struct snd_compr_stream * stream)204 static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
205 {
206 if (snd_BUG_ON(!stream))
207 return;
208
209 /* for partial_drain case we are back to running state on success */
210 if (stream->partial_drain) {
211 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
212 stream->partial_drain = false; /* clear this flag as well */
213 } else {
214 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
215 }
216
217 wake_up(&stream->runtime->sleep);
218 }
219
220 /**
221 * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
222 * @stream: compress stream to set
223 * @bufp: the buffer information, NULL to clear
224 *
225 * Copy the buffer information to runtime buffer when @bufp is non-NULL.
226 * Otherwise it clears the current buffer information.
227 */
228 static inline void
snd_compr_set_runtime_buffer(struct snd_compr_stream * stream,struct snd_dma_buffer * bufp)229 snd_compr_set_runtime_buffer(struct snd_compr_stream *stream,
230 struct snd_dma_buffer *bufp)
231 {
232 struct snd_compr_runtime *runtime = stream->runtime;
233
234 if (bufp) {
235 runtime->dma_buffer_p = bufp;
236 runtime->dma_area = bufp->area;
237 runtime->dma_addr = bufp->addr;
238 runtime->dma_bytes = bufp->bytes;
239 } else {
240 runtime->dma_buffer_p = NULL;
241 runtime->dma_area = NULL;
242 runtime->dma_addr = 0;
243 runtime->dma_bytes = 0;
244 }
245 }
246
247 int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size);
248 int snd_compr_free_pages(struct snd_compr_stream *stream);
249
250 int snd_compr_stop_error(struct snd_compr_stream *stream,
251 snd_pcm_state_t state);
252
253 #endif
254