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