blob: 14897ae427dfd37da525fa8046f6aab5178fcaa0 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* 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
21struct snd_compr_ops;
22
23/**
24 * struct snd_compr_runtime: runtime stream description
25 * @state: stream state
26 * @ops: pointer to DSP callbacks
27 * @dma_buffer_p: runtime dma buffer pointer
28 * @buffer: pointer to kernel buffer, valid only when not in mmap mode or
29 * DSP doesn't implement copy
30 * @buffer_size: size of the above buffer
31 * @fragment_size: size of buffer fragment in bytes
32 * @fragments: number of such fragments
33 * @total_bytes_available: cumulative number of bytes made available in
34 * the ring buffer
35 * @total_bytes_transferred: cumulative bytes transferred by offload DSP
36 * @sleep: poll sleep
37 * @private_data: driver private data pointer
38 */
39struct snd_compr_runtime {
40 snd_pcm_state_t state;
41 struct snd_compr_ops *ops;
42 struct snd_dma_buffer *dma_buffer_p;
43 void *buffer;
44 u64 buffer_size;
45 u32 fragment_size;
46 u32 fragments;
47 u64 total_bytes_available;
48 u64 total_bytes_transferred;
49 wait_queue_head_t sleep;
50 void *private_data;
51
52 ANDROID_KABI_RESERVE(1);
53};
54
55/**
56 * struct snd_compr_stream: compressed stream
57 * @name: device name
58 * @ops: pointer to DSP callbacks
59 * @runtime: pointer to runtime structure
60 * @device: device pointer
61 * @error_work: delayed work used when closing the stream due to an error
62 * @direction: stream direction, playback/recording
63 * @metadata_set: metadata set flag, true when set
64 * @next_track: has userspace signal next track transition, true when set
65 * @partial_drain: undergoing partial_drain for stream, true when set
66 * @private_data: pointer to DSP private data
67 */
68struct snd_compr_stream {
69 const char *name;
70 struct snd_compr_ops *ops;
71 struct snd_compr_runtime *runtime;
72 struct snd_compr *device;
73 struct delayed_work error_work;
74 enum snd_compr_direction direction;
75 bool metadata_set;
76 bool next_track;
77 bool partial_drain;
78 void *private_data;
79
80 ANDROID_KABI_RESERVE(1);
81};
82
83/**
84 * struct snd_compr_ops: compressed path DSP operations
85 * @open: Open the compressed stream
86 * This callback is mandatory and shall keep dsp ready to receive the stream
87 * parameter
88 * @free: Close the compressed stream, mandatory
89 * @set_params: Sets the compressed stream parameters, mandatory
90 * This can be called in during stream creation only to set codec params
91 * and the stream properties
92 * @get_params: retrieve the codec parameters, mandatory
93 * @set_metadata: Set the metadata values for a stream
94 * @get_metadata: retrieves the requested metadata values from stream
95 * @trigger: Trigger operations like start, pause, resume, drain, stop.
96 * This callback is mandatory
97 * @pointer: Retrieve current h/w pointer information. Mandatory
98 * @copy: Copy the compressed data to/from userspace, Optional
99 * Can't be implemented if DSP supports mmap
100 * @mmap: DSP mmap method to mmap DSP memory
101 * @ack: Ack for DSP when data is written to audio buffer, Optional
102 * Not valid if copy is implemented
103 * @get_caps: Retrieve DSP capabilities, mandatory
104 * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
105 */
106struct snd_compr_ops {
107 int (*open)(struct snd_compr_stream *stream);
108 int (*free)(struct snd_compr_stream *stream);
109 int (*set_params)(struct snd_compr_stream *stream,
110 struct snd_compr_params *params);
111 int (*get_params)(struct snd_compr_stream *stream,
112 struct snd_codec *params);
113 int (*set_metadata)(struct snd_compr_stream *stream,
114 struct snd_compr_metadata *metadata);
115 int (*get_metadata)(struct snd_compr_stream *stream,
116 struct snd_compr_metadata *metadata);
117 int (*trigger)(struct snd_compr_stream *stream, int cmd);
118 int (*pointer)(struct snd_compr_stream *stream,
119 struct snd_compr_tstamp *tstamp);
120 int (*copy)(struct snd_compr_stream *stream, char __user *buf,
121 size_t count);
122 int (*mmap)(struct snd_compr_stream *stream,
123 struct vm_area_struct *vma);
124 int (*ack)(struct snd_compr_stream *stream, size_t bytes);
125 int (*get_caps) (struct snd_compr_stream *stream,
126 struct snd_compr_caps *caps);
127 int (*get_codec_caps) (struct snd_compr_stream *stream,
128 struct snd_compr_codec_caps *codec);
129
130 ANDROID_KABI_RESERVE(1);
131};
132
133/**
134 * struct snd_compr: Compressed device
135 * @name: DSP device name
136 * @dev: associated device instance
137 * @ops: pointer to DSP callbacks
138 * @private_data: pointer to DSP pvt data
139 * @card: sound card pointer
140 * @direction: Playback or capture direction
141 * @lock: device lock
142 * @device: device id
143 */
144struct snd_compr {
145 const char *name;
146 struct device dev;
147 struct snd_compr_ops *ops;
148 void *private_data;
149 struct snd_card *card;
150 unsigned int direction;
151 struct mutex lock;
152 int device;
153#ifdef CONFIG_SND_VERBOSE_PROCFS
154 /* private: */
155 char id[64];
156 struct snd_info_entry *proc_root;
157 struct snd_info_entry *proc_info_entry;
158#endif
159 ANDROID_KABI_RESERVE(1);
160};
161
162/* compress device register APIs */
163int snd_compress_register(struct snd_compr *device);
164int snd_compress_deregister(struct snd_compr *device);
165int snd_compress_new(struct snd_card *card, int device,
166 int type, const char *id, struct snd_compr *compr);
167
168/* dsp driver callback apis
169 * For playback: driver should call snd_compress_fragment_elapsed() to let the
170 * framework know that a fragment has been consumed from the ring buffer
171 *
172 * For recording: we want to know when a frame is available or when
173 * at least one frame is available so snd_compress_frame_elapsed()
174 * callback should be called when a encodeded frame is available
175 */
176static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
177{
178 wake_up(&stream->runtime->sleep);
179}
180
181static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
182{
183 if (snd_BUG_ON(!stream))
184 return;
185
186 /* for partial_drain case we are back to running state on success */
187 if (stream->partial_drain) {
188 stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
189 stream->partial_drain = false; /* clear this flag as well */
190 } else {
191 stream->runtime->state = SNDRV_PCM_STATE_SETUP;
192 }
193
194 wake_up(&stream->runtime->sleep);
195}
196
197/**
198 * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
199 * @substream: compress substream to set
200 * @bufp: the buffer information, NULL to clear
201 *
202 * Copy the buffer information to runtime buffer when @bufp is non-NULL.
203 * Otherwise it clears the current buffer information.
204 */
205static inline void snd_compr_set_runtime_buffer(
206 struct snd_compr_stream *substream,
207 struct snd_dma_buffer *bufp)
208{
209 struct snd_compr_runtime *runtime = substream->runtime;
210
211 runtime->dma_buffer_p = bufp;
212}
213
214int snd_compr_stop_error(struct snd_compr_stream *stream,
215 snd_pcm_state_t state);
216
217#endif