1b3ed4c86SKuninori Morimoto /* SPDX-License-Identifier: GPL-2.0
2b3ed4c86SKuninori Morimoto  *
3e60061a3SVinod Koul  *  compress_driver.h - compress offload driver definations
4e60061a3SVinod Koul  *
5e60061a3SVinod Koul  *  Copyright (C) 2011 Intel Corporation
6e60061a3SVinod Koul  *  Authors:	Vinod Koul <vinod.koul@linux.intel.com>
7e60061a3SVinod Koul  *		Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
8e60061a3SVinod Koul  */
9b3ed4c86SKuninori Morimoto 
10e60061a3SVinod Koul #ifndef __COMPRESS_DRIVER_H
11e60061a3SVinod Koul #define __COMPRESS_DRIVER_H
12e60061a3SVinod Koul 
13e60061a3SVinod Koul #include <linux/types.h>
14e60061a3SVinod Koul #include <linux/sched.h>
157b617289SQais Yousef #include <sound/core.h>
16e60061a3SVinod Koul #include <sound/compress_offload.h>
17e60061a3SVinod Koul #include <sound/asound.h>
18e60061a3SVinod Koul #include <sound/pcm.h>
19e60061a3SVinod Koul 
20e60061a3SVinod Koul struct snd_compr_ops;
21e60061a3SVinod Koul 
22e60061a3SVinod Koul /**
23e60061a3SVinod Koul  * struct snd_compr_runtime: runtime stream description
24e60061a3SVinod Koul  * @state: stream state
25e60061a3SVinod Koul  * @ops: pointer to DSP callbacks
26e60061a3SVinod Koul  * @buffer: pointer to kernel buffer, valid only when not in mmap mode or
27e60061a3SVinod Koul  *	DSP doesn't implement copy
28e60061a3SVinod Koul  * @buffer_size: size of the above buffer
29e60061a3SVinod Koul  * @fragment_size: size of buffer fragment in bytes
30e60061a3SVinod Koul  * @fragments: number of such fragments
31e60061a3SVinod Koul  * @total_bytes_available: cumulative number of bytes made available in
32e60061a3SVinod Koul  *	the ring buffer
33e60061a3SVinod Koul  * @total_bytes_transferred: cumulative bytes transferred by offload DSP
34e60061a3SVinod Koul  * @sleep: poll sleep
35859b2e37SVinod Koul  * @private_data: driver private data pointer
36386dd54bSCezary Rojewski  * @dma_area: virtual buffer address
37386dd54bSCezary Rojewski  * @dma_addr: physical buffer address (not accessible from main CPU)
38386dd54bSCezary Rojewski  * @dma_bytes: size of DMA area
39386dd54bSCezary Rojewski  * @dma_buffer_p: runtime dma buffer pointer
40e60061a3SVinod Koul  */
41e60061a3SVinod Koul struct snd_compr_runtime {
42e60061a3SVinod Koul 	snd_pcm_state_t state;
43e60061a3SVinod Koul 	struct snd_compr_ops *ops;
44e60061a3SVinod Koul 	void *buffer;
45e60061a3SVinod Koul 	u64 buffer_size;
46e60061a3SVinod Koul 	u32 fragment_size;
47e60061a3SVinod Koul 	u32 fragments;
48e60061a3SVinod Koul 	u64 total_bytes_available;
49e60061a3SVinod Koul 	u64 total_bytes_transferred;
50e60061a3SVinod Koul 	wait_queue_head_t sleep;
5149681077SVinod Koul 	void *private_data;
52386dd54bSCezary Rojewski 
53386dd54bSCezary Rojewski 	unsigned char *dma_area;
54386dd54bSCezary Rojewski 	dma_addr_t dma_addr;
55386dd54bSCezary Rojewski 	size_t dma_bytes;
56386dd54bSCezary Rojewski 	struct snd_dma_buffer *dma_buffer_p;
57e60061a3SVinod Koul };
58e60061a3SVinod Koul 
59e60061a3SVinod Koul /**
60e60061a3SVinod Koul  * struct snd_compr_stream: compressed stream
61e60061a3SVinod Koul  * @name: device name
62e60061a3SVinod Koul  * @ops: pointer to DSP callbacks
63e60061a3SVinod Koul  * @runtime: pointer to runtime structure
64e60061a3SVinod Koul  * @device: device pointer
65a4f2d87cSCharles Keepax  * @error_work: delayed work used when closing the stream due to an error
66e60061a3SVinod Koul  * @direction: stream direction, playback/recording
679727b490SJeeja KP  * @metadata_set: metadata set flag, true when set
681a6ab46fSMasanari Iida  * @next_track: has userspace signal next track transition, true when set
69f79a732aSVinod Koul  * @partial_drain: undergoing partial_drain for stream, true when set
709be9f2d3SGyeongtaek Lee  * @pause_in_draining: paused during draining state, true when set
71e60061a3SVinod Koul  * @private_data: pointer to DSP private data
72b9759ef2SCezary Rojewski  * @dma_buffer: allocated buffer if any
73e60061a3SVinod Koul  */
74e60061a3SVinod Koul struct snd_compr_stream {
75e60061a3SVinod Koul 	const char *name;
76e60061a3SVinod Koul 	struct snd_compr_ops *ops;
77e60061a3SVinod Koul 	struct snd_compr_runtime *runtime;
78e60061a3SVinod Koul 	struct snd_compr *device;
79a4f2d87cSCharles Keepax 	struct delayed_work error_work;
80e60061a3SVinod Koul 	enum snd_compr_direction direction;
819727b490SJeeja KP 	bool metadata_set;
829727b490SJeeja KP 	bool next_track;
83f79a732aSVinod Koul 	bool partial_drain;
849be9f2d3SGyeongtaek Lee 	bool pause_in_draining;
85e60061a3SVinod Koul 	void *private_data;
86b9759ef2SCezary Rojewski 	struct snd_dma_buffer dma_buffer;
87e60061a3SVinod Koul };
88e60061a3SVinod Koul 
89e60061a3SVinod Koul /**
90e60061a3SVinod Koul  * struct snd_compr_ops: compressed path DSP operations
91e60061a3SVinod Koul  * @open: Open the compressed stream
92e60061a3SVinod Koul  * This callback is mandatory and shall keep dsp ready to receive the stream
93e60061a3SVinod Koul  * parameter
94e60061a3SVinod Koul  * @free: Close the compressed stream, mandatory
95e60061a3SVinod Koul  * @set_params: Sets the compressed stream parameters, mandatory
96e60061a3SVinod Koul  * This can be called in during stream creation only to set codec params
97e60061a3SVinod Koul  * and the stream properties
98e60061a3SVinod Koul  * @get_params: retrieve the codec parameters, mandatory
99859b2e37SVinod Koul  * @set_metadata: Set the metadata values for a stream
1001a6ab46fSMasanari Iida  * @get_metadata: retrieves the requested metadata values from stream
101e60061a3SVinod Koul  * @trigger: Trigger operations like start, pause, resume, drain, stop.
102e60061a3SVinod Koul  * This callback is mandatory
103e60061a3SVinod Koul  * @pointer: Retrieve current h/w pointer information. Mandatory
104e60061a3SVinod Koul  * @copy: Copy the compressed data to/from userspace, Optional
105e60061a3SVinod Koul  * Can't be implemented if DSP supports mmap
106e60061a3SVinod Koul  * @mmap: DSP mmap method to mmap DSP memory
107e60061a3SVinod Koul  * @ack: Ack for DSP when data is written to audio buffer, Optional
108e60061a3SVinod Koul  * Not valid if copy is implemented
109e60061a3SVinod Koul  * @get_caps: Retrieve DSP capabilities, mandatory
110e60061a3SVinod Koul  * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
111e60061a3SVinod Koul  */
112e60061a3SVinod Koul struct snd_compr_ops {
113e60061a3SVinod Koul 	int (*open)(struct snd_compr_stream *stream);
114e60061a3SVinod Koul 	int (*free)(struct snd_compr_stream *stream);
115e60061a3SVinod Koul 	int (*set_params)(struct snd_compr_stream *stream,
116e60061a3SVinod Koul 			struct snd_compr_params *params);
117e60061a3SVinod Koul 	int (*get_params)(struct snd_compr_stream *stream,
118e60061a3SVinod Koul 			struct snd_codec *params);
1199727b490SJeeja KP 	int (*set_metadata)(struct snd_compr_stream *stream,
1209727b490SJeeja KP 			struct snd_compr_metadata *metadata);
1219727b490SJeeja KP 	int (*get_metadata)(struct snd_compr_stream *stream,
1229727b490SJeeja KP 			struct snd_compr_metadata *metadata);
123e60061a3SVinod Koul 	int (*trigger)(struct snd_compr_stream *stream, int cmd);
124e60061a3SVinod Koul 	int (*pointer)(struct snd_compr_stream *stream,
125e60061a3SVinod Koul 			struct snd_compr_tstamp *tstamp);
1264daf891cSCharles Keepax 	int (*copy)(struct snd_compr_stream *stream, char __user *buf,
127e60061a3SVinod Koul 		       size_t count);
128e60061a3SVinod Koul 	int (*mmap)(struct snd_compr_stream *stream,
129e60061a3SVinod Koul 			struct vm_area_struct *vma);
130e60061a3SVinod Koul 	int (*ack)(struct snd_compr_stream *stream, size_t bytes);
131e60061a3SVinod Koul 	int (*get_caps) (struct snd_compr_stream *stream,
132e60061a3SVinod Koul 			struct snd_compr_caps *caps);
133e60061a3SVinod Koul 	int (*get_codec_caps) (struct snd_compr_stream *stream,
134e60061a3SVinod Koul 			struct snd_compr_codec_caps *codec);
135e60061a3SVinod Koul };
136e60061a3SVinod Koul 
137e60061a3SVinod Koul /**
138e60061a3SVinod Koul  * struct snd_compr: Compressed device
139e60061a3SVinod Koul  * @name: DSP device name
14004c5d5a4STakashi Iwai  * @dev: associated device instance
141e60061a3SVinod Koul  * @ops: pointer to DSP callbacks
142e60061a3SVinod Koul  * @private_data: pointer to DSP pvt data
143e60061a3SVinod Koul  * @card: sound card pointer
144e60061a3SVinod Koul  * @direction: Playback or capture direction
145e60061a3SVinod Koul  * @lock: device lock
146e60061a3SVinod Koul  * @device: device id
1479be9f2d3SGyeongtaek Lee  * @use_pause_in_draining: allow pause in draining, true when set
148e60061a3SVinod Koul  */
149e60061a3SVinod Koul struct snd_compr {
150e60061a3SVinod Koul 	const char *name;
151*b53a41eeSTakashi Iwai 	struct device *dev;
152e60061a3SVinod Koul 	struct snd_compr_ops *ops;
153e60061a3SVinod Koul 	void *private_data;
154e60061a3SVinod Koul 	struct snd_card *card;
155e60061a3SVinod Koul 	unsigned int direction;
156e60061a3SVinod Koul 	struct mutex lock;
157e60061a3SVinod Koul 	int device;
1589be9f2d3SGyeongtaek Lee 	bool use_pause_in_draining;
15931742724SRichard Fitzgerald #ifdef CONFIG_SND_VERBOSE_PROCFS
160f84551e4STakashi Iwai 	/* private: */
16131742724SRichard Fitzgerald 	char id[64];
16231742724SRichard Fitzgerald 	struct snd_info_entry *proc_root;
16331742724SRichard Fitzgerald 	struct snd_info_entry *proc_info_entry;
16431742724SRichard Fitzgerald #endif
165e60061a3SVinod Koul };
166e60061a3SVinod Koul 
167e60061a3SVinod Koul /* compress device register APIs */
168e60061a3SVinod Koul int snd_compress_new(struct snd_card *card, int device,
169e5241a8cSRichard Fitzgerald 			int type, const char *id, struct snd_compr *compr);
170e60061a3SVinod Koul 
1719be9f2d3SGyeongtaek Lee /**
1729be9f2d3SGyeongtaek Lee  * snd_compr_use_pause_in_draining - Allow pause and resume in draining state
1739be9f2d3SGyeongtaek Lee  * @substream: compress substream to set
1749be9f2d3SGyeongtaek Lee  *
1759be9f2d3SGyeongtaek Lee  * Allow pause and resume in draining state.
1769be9f2d3SGyeongtaek Lee  * Only HW driver supports this transition can call this API.
1779be9f2d3SGyeongtaek Lee  */
snd_compr_use_pause_in_draining(struct snd_compr_stream * substream)1789be9f2d3SGyeongtaek Lee static inline void snd_compr_use_pause_in_draining(struct snd_compr_stream *substream)
1799be9f2d3SGyeongtaek Lee {
1809be9f2d3SGyeongtaek Lee 	substream->device->use_pause_in_draining = true;
1819be9f2d3SGyeongtaek Lee }
1829be9f2d3SGyeongtaek Lee 
183e60061a3SVinod Koul /* dsp driver callback apis
184e60061a3SVinod Koul  * For playback: driver should call snd_compress_fragment_elapsed() to let the
185e60061a3SVinod Koul  * framework know that a fragment has been consumed from the ring buffer
186e60061a3SVinod Koul  *
187e60061a3SVinod Koul  * For recording: we want to know when a frame is available or when
188e60061a3SVinod Koul  * at least one frame is available so snd_compress_frame_elapsed()
189e60061a3SVinod Koul  * callback should be called when a encodeded frame is available
190e60061a3SVinod Koul  */
snd_compr_fragment_elapsed(struct snd_compr_stream * stream)191e60061a3SVinod Koul static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
192e60061a3SVinod Koul {
193e60061a3SVinod Koul 	wake_up(&stream->runtime->sleep);
194e60061a3SVinod Koul }
195e60061a3SVinod Koul 
snd_compr_drain_notify(struct snd_compr_stream * stream)196917f4b5cSVinod Koul static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
197917f4b5cSVinod Koul {
198f44f2a54SVinod Koul 	if (snd_BUG_ON(!stream))
199f44f2a54SVinod Koul 		return;
200917f4b5cSVinod Koul 
201f79a732aSVinod Koul 	/* for partial_drain case we are back to running state on success */
202f79a732aSVinod Koul 	if (stream->partial_drain) {
203f79a732aSVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
204f79a732aSVinod Koul 		stream->partial_drain = false; /* clear this flag as well */
205f79a732aSVinod Koul 	} else {
206f44f2a54SVinod Koul 		stream->runtime->state = SNDRV_PCM_STATE_SETUP;
207f79a732aSVinod Koul 	}
2084f2ab5e1SCharles Keepax 
209f44f2a54SVinod Koul 	wake_up(&stream->runtime->sleep);
210917f4b5cSVinod Koul }
211917f4b5cSVinod Koul 
212ba02eed9SSrinivas Kandagatla /**
213ba02eed9SSrinivas Kandagatla  * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
214386dd54bSCezary Rojewski  * @stream: compress stream to set
215ba02eed9SSrinivas Kandagatla  * @bufp: the buffer information, NULL to clear
216ba02eed9SSrinivas Kandagatla  *
217ba02eed9SSrinivas Kandagatla  * Copy the buffer information to runtime buffer when @bufp is non-NULL.
218ba02eed9SSrinivas Kandagatla  * Otherwise it clears the current buffer information.
219ba02eed9SSrinivas Kandagatla  */
220386dd54bSCezary Rojewski static inline void
snd_compr_set_runtime_buffer(struct snd_compr_stream * stream,struct snd_dma_buffer * bufp)221386dd54bSCezary Rojewski snd_compr_set_runtime_buffer(struct snd_compr_stream *stream,
222ba02eed9SSrinivas Kandagatla 			     struct snd_dma_buffer *bufp)
223ba02eed9SSrinivas Kandagatla {
224386dd54bSCezary Rojewski 	struct snd_compr_runtime *runtime = stream->runtime;
225ba02eed9SSrinivas Kandagatla 
226386dd54bSCezary Rojewski 	if (bufp) {
227ba02eed9SSrinivas Kandagatla 		runtime->dma_buffer_p = bufp;
228386dd54bSCezary Rojewski 		runtime->dma_area = bufp->area;
229386dd54bSCezary Rojewski 		runtime->dma_addr = bufp->addr;
230386dd54bSCezary Rojewski 		runtime->dma_bytes = bufp->bytes;
231386dd54bSCezary Rojewski 	} else {
232386dd54bSCezary Rojewski 		runtime->dma_buffer_p = NULL;
233386dd54bSCezary Rojewski 		runtime->dma_area = NULL;
234386dd54bSCezary Rojewski 		runtime->dma_addr = 0;
235386dd54bSCezary Rojewski 		runtime->dma_bytes = 0;
236386dd54bSCezary Rojewski 	}
237ba02eed9SSrinivas Kandagatla }
238ba02eed9SSrinivas Kandagatla 
239b9759ef2SCezary Rojewski int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size);
240b9759ef2SCezary Rojewski int snd_compr_free_pages(struct snd_compr_stream *stream);
241b9759ef2SCezary Rojewski 
242a4f2d87cSCharles Keepax int snd_compr_stop_error(struct snd_compr_stream *stream,
243a4f2d87cSCharles Keepax 			 snd_pcm_state_t state);
244a4f2d87cSCharles Keepax 
245e60061a3SVinod Koul #endif
246