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_task_runtime: task runtime description
25 * @list: list of all managed tasks
26 * @input: input DMA buffer
27 * @output: output DMA buffer
28 * @seqno: sequence number
29 * @input_size: really used data in the input buffer
30 * @output_size: really used data in the output buffer
31 * @flags: see SND_COMPRESS_TFLG_*
32 * @state: actual task state
33 * @private_value: used by the lowlevel driver (opaque)
34 */
35 struct snd_compr_task_runtime {
36 struct list_head list;
37 struct dma_buf *input;
38 struct dma_buf *output;
39 u64 seqno;
40 u64 input_size;
41 u64 output_size;
42 u32 flags;
43 u8 state;
44 void *private_value;
45 };
46
47 /**
48 * struct snd_compr_runtime: runtime stream description
49 * @state: stream state
50 * @ops: pointer to DSP callbacks
51 * @buffer: pointer to kernel buffer, valid only when not in mmap mode or
52 * DSP doesn't implement copy
53 * @buffer_size: size of the above buffer
54 * @fragment_size: size of buffer fragment in bytes
55 * @fragments: number of such fragments
56 * @total_bytes_available: cumulative number of bytes made available in
57 * the ring buffer
58 * @total_bytes_transferred: cumulative bytes transferred by offload DSP
59 * @sleep: poll sleep
60 * @private_data: driver private data pointer
61 * @dma_area: virtual buffer address
62 * @dma_addr: physical buffer address (not accessible from main CPU)
63 * @dma_bytes: size of DMA area
64 * @dma_buffer_p: runtime dma buffer pointer
65 * @active_tasks: count of active tasks
66 * @total_tasks: count of all tasks
67 * @task_seqno: last task sequence number (!= 0)
68 * @tasks: list of all tasks
69 */
70 struct snd_compr_runtime {
71 snd_pcm_state_t state;
72 struct snd_compr_ops *ops;
73 void *buffer;
74 u64 buffer_size;
75 u32 fragment_size;
76 u32 fragments;
77 u64 total_bytes_available;
78 u64 total_bytes_transferred;
79 wait_queue_head_t sleep;
80 void *private_data;
81
82 unsigned char *dma_area;
83 dma_addr_t dma_addr;
84 size_t dma_bytes;
85 struct snd_dma_buffer *dma_buffer_p;
86
87 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
88 u32 active_tasks;
89 u32 total_tasks;
90 u64 task_seqno;
91 struct list_head tasks;
92 #endif
93 ANDROID_KABI_RESERVE(1);
94 };
95
96 /**
97 * struct snd_compr_stream: compressed stream
98 * @name: device name
99 * @ops: pointer to DSP callbacks
100 * @runtime: pointer to runtime structure
101 * @device: device pointer
102 * @error_work: delayed work used when closing the stream due to an error
103 * @direction: stream direction, playback/recording
104 * @metadata_set: metadata set flag, true when set
105 * @next_track: has userspace signal next track transition, true when set
106 * @partial_drain: undergoing partial_drain for stream, true when set
107 * @pause_in_draining: paused during draining state, true when set
108 * @private_data: pointer to DSP private data
109 * @dma_buffer: allocated buffer if any
110 */
111 struct snd_compr_stream {
112 const char *name;
113 struct snd_compr_ops *ops;
114 struct snd_compr_runtime *runtime;
115 struct snd_compr *device;
116 struct delayed_work error_work;
117 enum snd_compr_direction direction;
118 bool metadata_set;
119 bool next_track;
120 bool partial_drain;
121 bool pause_in_draining;
122 void *private_data;
123 struct snd_dma_buffer dma_buffer;
124
125 ANDROID_KABI_RESERVE(1);
126 };
127
128 /**
129 * struct snd_compr_ops: compressed path DSP operations
130 * @open: Open the compressed stream
131 * This callback is mandatory and shall keep dsp ready to receive the stream
132 * parameter
133 * @free: Close the compressed stream, mandatory
134 * @set_params: Sets the compressed stream parameters, mandatory
135 * This can be called in during stream creation only to set codec params
136 * and the stream properties
137 * @get_params: retrieve the codec parameters, mandatory
138 * @set_metadata: Set the metadata values for a stream
139 * @get_metadata: retrieves the requested metadata values from stream
140 * @trigger: Trigger operations like start, pause, resume, drain, stop.
141 * This callback is mandatory
142 * @pointer: Retrieve current h/w pointer information. Mandatory
143 * @copy: Copy the compressed data to/from userspace, Optional
144 * Can't be implemented if DSP supports mmap
145 * @mmap: DSP mmap method to mmap DSP memory
146 * @ack: Ack for DSP when data is written to audio buffer, Optional
147 * Not valid if copy is implemented
148 * @get_caps: Retrieve DSP capabilities, mandatory
149 * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
150 * @task_create: Create a set of input/output buffers for accel operations
151 * @task_start: Start (queue) a task for accel operations
152 * @task_stop: Stop (dequeue) a task for accel operations
153 * @task_free: Free a set of input/output buffers for accel operations
154 */
155 struct snd_compr_ops {
156 int (*open)(struct snd_compr_stream *stream);
157 int (*free)(struct snd_compr_stream *stream);
158 int (*set_params)(struct snd_compr_stream *stream,
159 struct snd_compr_params *params);
160 int (*get_params)(struct snd_compr_stream *stream,
161 struct snd_codec *params);
162 int (*set_metadata)(struct snd_compr_stream *stream,
163 struct snd_compr_metadata *metadata);
164 int (*get_metadata)(struct snd_compr_stream *stream,
165 struct snd_compr_metadata *metadata);
166 int (*trigger)(struct snd_compr_stream *stream, int cmd);
167 int (*pointer)(struct snd_compr_stream *stream,
168 struct snd_compr_tstamp *tstamp);
169 int (*copy)(struct snd_compr_stream *stream, char __user *buf,
170 size_t count);
171 int (*mmap)(struct snd_compr_stream *stream,
172 struct vm_area_struct *vma);
173 int (*ack)(struct snd_compr_stream *stream, size_t bytes);
174 int (*get_caps) (struct snd_compr_stream *stream,
175 struct snd_compr_caps *caps);
176 int (*get_codec_caps) (struct snd_compr_stream *stream,
177 struct snd_compr_codec_caps *codec);
178 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
179 int (*task_create) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
180 int (*task_start) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
181 int (*task_stop) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
182 int (*task_free) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
183 #endif
184
185 ANDROID_KABI_RESERVE(1);
186 };
187
188 /**
189 * struct snd_compr: Compressed device
190 * @name: DSP device name
191 * @dev: associated device instance
192 * @ops: pointer to DSP callbacks
193 * @private_data: pointer to DSP pvt data
194 * @card: sound card pointer
195 * @direction: Playback or capture direction
196 * @lock: device lock
197 * @device: device id
198 * @use_pause_in_draining: allow pause in draining, true when set
199 */
200 struct snd_compr {
201 const char *name;
202 struct device *dev;
203 struct snd_compr_ops *ops;
204 void *private_data;
205 struct snd_card *card;
206 unsigned int direction;
207 struct mutex lock;
208 int device;
209 bool use_pause_in_draining;
210 #ifdef CONFIG_SND_VERBOSE_PROCFS
211 /* private: */
212 char id[64];
213 struct snd_info_entry *proc_root;
214 struct snd_info_entry *proc_info_entry;
215 #endif
216 ANDROID_KABI_RESERVE(1);
217 };
218
219 /* compress device register APIs */
220 int snd_compress_new(struct snd_card *card, int device,
221 int type, const char *id, struct snd_compr *compr);
222
223 /**
224 * snd_compr_use_pause_in_draining - Allow pause and resume in draining state
225 * @substream: compress substream to set
226 *
227 * Allow pause and resume in draining state.
228 * Only HW driver supports this transition can call this API.
229 */
snd_compr_use_pause_in_draining(struct snd_compr_stream * substream)230 static inline void snd_compr_use_pause_in_draining(struct snd_compr_stream *substream)
231 {
232 substream->device->use_pause_in_draining = true;
233 }
234
235 /* dsp driver callback apis
236 * For playback: driver should call snd_compress_fragment_elapsed() to let the
237 * framework know that a fragment has been consumed from the ring buffer
238 *
239 * For recording: we want to know when a frame is available or when
240 * at least one frame is available so snd_compress_frame_elapsed()
241 * callback should be called when a encodeded frame is available
242 */
snd_compr_fragment_elapsed(struct snd_compr_stream * stream)243 static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
244 {
245 wake_up(&stream->runtime->sleep);
246 }
247
snd_compr_drain_notify(struct snd_compr_stream * stream)248 static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
249 {
250 if (snd_BUG_ON(!stream))
251 return;
252
253 /* for partial_drain case we are back to running state on success */
254 if (stream->partial_drain) {
255 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
256 stream->partial_drain = false; /* clear this flag as well */
257 } else {
258 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
259 }
260
261 wake_up(&stream->runtime->sleep);
262 }
263
264 /**
265 * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
266 * @stream: compress stream to set
267 * @bufp: the buffer information, NULL to clear
268 *
269 * Copy the buffer information to runtime buffer when @bufp is non-NULL.
270 * Otherwise it clears the current buffer information.
271 */
272 static inline void
snd_compr_set_runtime_buffer(struct snd_compr_stream * stream,struct snd_dma_buffer * bufp)273 snd_compr_set_runtime_buffer(struct snd_compr_stream *stream,
274 struct snd_dma_buffer *bufp)
275 {
276 struct snd_compr_runtime *runtime = stream->runtime;
277
278 if (bufp) {
279 runtime->dma_buffer_p = bufp;
280 runtime->dma_area = bufp->area;
281 runtime->dma_addr = bufp->addr;
282 runtime->dma_bytes = bufp->bytes;
283 } else {
284 runtime->dma_buffer_p = NULL;
285 runtime->dma_area = NULL;
286 runtime->dma_addr = 0;
287 runtime->dma_bytes = 0;
288 }
289 }
290
291 int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size);
292 int snd_compr_free_pages(struct snd_compr_stream *stream);
293
294 int snd_compr_stop_error(struct snd_compr_stream *stream,
295 snd_pcm_state_t state);
296
297 #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
298 void snd_compr_task_finished(struct snd_compr_stream *stream,
299 struct snd_compr_task_runtime *task);
300 #endif
301
302 #endif
303