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
69e60061a3SVinod Koul  * @private_data: pointer to DSP private data
70b9759ef2SCezary Rojewski  * @dma_buffer: allocated buffer if any
71e60061a3SVinod Koul  */
72e60061a3SVinod Koul struct snd_compr_stream {
73e60061a3SVinod Koul 	const char *name;
74e60061a3SVinod Koul 	struct snd_compr_ops *ops;
75e60061a3SVinod Koul 	struct snd_compr_runtime *runtime;
76e60061a3SVinod Koul 	struct snd_compr *device;
77a4f2d87cSCharles Keepax 	struct delayed_work error_work;
78e60061a3SVinod Koul 	enum snd_compr_direction direction;
799727b490SJeeja KP 	bool metadata_set;
809727b490SJeeja KP 	bool next_track;
81e60061a3SVinod Koul 	void *private_data;
82b9759ef2SCezary Rojewski 	struct snd_dma_buffer dma_buffer;
83e60061a3SVinod Koul };
84e60061a3SVinod Koul 
85e60061a3SVinod Koul /**
86e60061a3SVinod Koul  * struct snd_compr_ops: compressed path DSP operations
87e60061a3SVinod Koul  * @open: Open the compressed stream
88e60061a3SVinod Koul  * This callback is mandatory and shall keep dsp ready to receive the stream
89e60061a3SVinod Koul  * parameter
90e60061a3SVinod Koul  * @free: Close the compressed stream, mandatory
91e60061a3SVinod Koul  * @set_params: Sets the compressed stream parameters, mandatory
92e60061a3SVinod Koul  * This can be called in during stream creation only to set codec params
93e60061a3SVinod Koul  * and the stream properties
94e60061a3SVinod Koul  * @get_params: retrieve the codec parameters, mandatory
95859b2e37SVinod Koul  * @set_metadata: Set the metadata values for a stream
961a6ab46fSMasanari Iida  * @get_metadata: retrieves the requested metadata values from stream
97e60061a3SVinod Koul  * @trigger: Trigger operations like start, pause, resume, drain, stop.
98e60061a3SVinod Koul  * This callback is mandatory
99e60061a3SVinod Koul  * @pointer: Retrieve current h/w pointer information. Mandatory
100e60061a3SVinod Koul  * @copy: Copy the compressed data to/from userspace, Optional
101e60061a3SVinod Koul  * Can't be implemented if DSP supports mmap
102e60061a3SVinod Koul  * @mmap: DSP mmap method to mmap DSP memory
103e60061a3SVinod Koul  * @ack: Ack for DSP when data is written to audio buffer, Optional
104e60061a3SVinod Koul  * Not valid if copy is implemented
105e60061a3SVinod Koul  * @get_caps: Retrieve DSP capabilities, mandatory
106e60061a3SVinod Koul  * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
107e60061a3SVinod Koul  */
108e60061a3SVinod Koul struct snd_compr_ops {
109e60061a3SVinod Koul 	int (*open)(struct snd_compr_stream *stream);
110e60061a3SVinod Koul 	int (*free)(struct snd_compr_stream *stream);
111e60061a3SVinod Koul 	int (*set_params)(struct snd_compr_stream *stream,
112e60061a3SVinod Koul 			struct snd_compr_params *params);
113e60061a3SVinod Koul 	int (*get_params)(struct snd_compr_stream *stream,
114e60061a3SVinod Koul 			struct snd_codec *params);
1159727b490SJeeja KP 	int (*set_metadata)(struct snd_compr_stream *stream,
1169727b490SJeeja KP 			struct snd_compr_metadata *metadata);
1179727b490SJeeja KP 	int (*get_metadata)(struct snd_compr_stream *stream,
1189727b490SJeeja KP 			struct snd_compr_metadata *metadata);
119e60061a3SVinod Koul 	int (*trigger)(struct snd_compr_stream *stream, int cmd);
120e60061a3SVinod Koul 	int (*pointer)(struct snd_compr_stream *stream,
121e60061a3SVinod Koul 			struct snd_compr_tstamp *tstamp);
1224daf891cSCharles Keepax 	int (*copy)(struct snd_compr_stream *stream, char __user *buf,
123e60061a3SVinod Koul 		       size_t count);
124e60061a3SVinod Koul 	int (*mmap)(struct snd_compr_stream *stream,
125e60061a3SVinod Koul 			struct vm_area_struct *vma);
126e60061a3SVinod Koul 	int (*ack)(struct snd_compr_stream *stream, size_t bytes);
127e60061a3SVinod Koul 	int (*get_caps) (struct snd_compr_stream *stream,
128e60061a3SVinod Koul 			struct snd_compr_caps *caps);
129e60061a3SVinod Koul 	int (*get_codec_caps) (struct snd_compr_stream *stream,
130e60061a3SVinod Koul 			struct snd_compr_codec_caps *codec);
131e60061a3SVinod Koul };
132e60061a3SVinod Koul 
133e60061a3SVinod Koul /**
134e60061a3SVinod Koul  * struct snd_compr: Compressed device
135e60061a3SVinod Koul  * @name: DSP device name
13604c5d5a4STakashi Iwai  * @dev: associated device instance
137e60061a3SVinod Koul  * @ops: pointer to DSP callbacks
138e60061a3SVinod Koul  * @private_data: pointer to DSP pvt data
139e60061a3SVinod Koul  * @card: sound card pointer
140e60061a3SVinod Koul  * @direction: Playback or capture direction
141e60061a3SVinod Koul  * @lock: device lock
142e60061a3SVinod Koul  * @device: device id
143e60061a3SVinod Koul  */
144e60061a3SVinod Koul struct snd_compr {
145e60061a3SVinod Koul 	const char *name;
14604c5d5a4STakashi Iwai 	struct device dev;
147e60061a3SVinod Koul 	struct snd_compr_ops *ops;
148e60061a3SVinod Koul 	void *private_data;
149e60061a3SVinod Koul 	struct snd_card *card;
150e60061a3SVinod Koul 	unsigned int direction;
151e60061a3SVinod Koul 	struct mutex lock;
152e60061a3SVinod Koul 	int device;
15331742724SRichard Fitzgerald #ifdef CONFIG_SND_VERBOSE_PROCFS
154f84551e4STakashi Iwai 	/* private: */
15531742724SRichard Fitzgerald 	char id[64];
15631742724SRichard Fitzgerald 	struct snd_info_entry *proc_root;
15731742724SRichard Fitzgerald 	struct snd_info_entry *proc_info_entry;
15831742724SRichard Fitzgerald #endif
159e60061a3SVinod Koul };
160e60061a3SVinod Koul 
161e60061a3SVinod Koul /* compress device register APIs */
162e60061a3SVinod Koul int snd_compress_register(struct snd_compr *device);
163e60061a3SVinod Koul int snd_compress_deregister(struct snd_compr *device);
164e60061a3SVinod Koul int snd_compress_new(struct snd_card *card, int device,
165e5241a8cSRichard Fitzgerald 			int type, const char *id, struct snd_compr *compr);
166e60061a3SVinod Koul 
167e60061a3SVinod Koul /* dsp driver callback apis
168e60061a3SVinod Koul  * For playback: driver should call snd_compress_fragment_elapsed() to let the
169e60061a3SVinod Koul  * framework know that a fragment has been consumed from the ring buffer
170e60061a3SVinod Koul  *
171e60061a3SVinod Koul  * For recording: we want to know when a frame is available or when
172e60061a3SVinod Koul  * at least one frame is available so snd_compress_frame_elapsed()
173e60061a3SVinod Koul  * callback should be called when a encodeded frame is available
174e60061a3SVinod Koul  */
175e60061a3SVinod Koul static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
176e60061a3SVinod Koul {
177e60061a3SVinod Koul 	wake_up(&stream->runtime->sleep);
178e60061a3SVinod Koul }
179e60061a3SVinod Koul 
180917f4b5cSVinod Koul static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
181917f4b5cSVinod Koul {
182f44f2a54SVinod Koul 	if (snd_BUG_ON(!stream))
183f44f2a54SVinod Koul 		return;
184917f4b5cSVinod Koul 
185f44f2a54SVinod Koul 	stream->runtime->state = SNDRV_PCM_STATE_SETUP;
1864f2ab5e1SCharles Keepax 
187f44f2a54SVinod Koul 	wake_up(&stream->runtime->sleep);
188917f4b5cSVinod Koul }
189917f4b5cSVinod Koul 
190ba02eed9SSrinivas Kandagatla /**
191ba02eed9SSrinivas Kandagatla  * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
192386dd54bSCezary Rojewski  * @stream: compress stream to set
193ba02eed9SSrinivas Kandagatla  * @bufp: the buffer information, NULL to clear
194ba02eed9SSrinivas Kandagatla  *
195ba02eed9SSrinivas Kandagatla  * Copy the buffer information to runtime buffer when @bufp is non-NULL.
196ba02eed9SSrinivas Kandagatla  * Otherwise it clears the current buffer information.
197ba02eed9SSrinivas Kandagatla  */
198386dd54bSCezary Rojewski static inline void
199386dd54bSCezary Rojewski snd_compr_set_runtime_buffer(struct snd_compr_stream *stream,
200ba02eed9SSrinivas Kandagatla 			     struct snd_dma_buffer *bufp)
201ba02eed9SSrinivas Kandagatla {
202386dd54bSCezary Rojewski 	struct snd_compr_runtime *runtime = stream->runtime;
203ba02eed9SSrinivas Kandagatla 
204386dd54bSCezary Rojewski 	if (bufp) {
205ba02eed9SSrinivas Kandagatla 		runtime->dma_buffer_p = bufp;
206386dd54bSCezary Rojewski 		runtime->dma_area = bufp->area;
207386dd54bSCezary Rojewski 		runtime->dma_addr = bufp->addr;
208386dd54bSCezary Rojewski 		runtime->dma_bytes = bufp->bytes;
209386dd54bSCezary Rojewski 	} else {
210386dd54bSCezary Rojewski 		runtime->dma_buffer_p = NULL;
211386dd54bSCezary Rojewski 		runtime->dma_area = NULL;
212386dd54bSCezary Rojewski 		runtime->dma_addr = 0;
213386dd54bSCezary Rojewski 		runtime->dma_bytes = 0;
214386dd54bSCezary Rojewski 	}
215ba02eed9SSrinivas Kandagatla }
216ba02eed9SSrinivas Kandagatla 
217b9759ef2SCezary Rojewski int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size);
218b9759ef2SCezary Rojewski int snd_compr_free_pages(struct snd_compr_stream *stream);
219b9759ef2SCezary Rojewski 
220a4f2d87cSCharles Keepax int snd_compr_stop_error(struct snd_compr_stream *stream,
221a4f2d87cSCharles Keepax 			 snd_pcm_state_t state);
222a4f2d87cSCharles Keepax 
223e60061a3SVinod Koul #endif
224