1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef __SOUND_PCM_H
3 #define __SOUND_PCM_H
4 
5 /*
6  *  Digital Audio (PCM) abstract layer
7  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8  *                   Abramo Bagnara <abramo@alsa-project.org>
9  */
10 
11 #include <sound/asound.h>
12 #include <sound/memalloc.h>
13 #include <sound/minors.h>
14 #include <linux/poll.h>
15 #include <linux/mm.h>
16 #include <linux/bitops.h>
17 #include <linux/pm_qos.h>
18 #include <linux/refcount.h>
19 #include <linux/uio.h>
20 #include <linux/android_kabi.h>
21 
22 #define snd_pcm_substream_chip(substream) ((substream)->private_data)
23 #define snd_pcm_chip(pcm) ((pcm)->private_data)
24 
25 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
26 #include <sound/pcm_oss.h>
27 #endif
28 
29 /*
30  *  Hardware (lowlevel) section
31  */
32 
33 struct snd_pcm_hardware {
34 	unsigned int info;		/* SNDRV_PCM_INFO_* */
35 	u64 formats;			/* SNDRV_PCM_FMTBIT_* */
36 	u32 subformats;			/* for S32_LE, SNDRV_PCM_SUBFMTBIT_* */
37 	unsigned int rates;		/* SNDRV_PCM_RATE_* */
38 	unsigned int rate_min;		/* min rate */
39 	unsigned int rate_max;		/* max rate */
40 	unsigned int channels_min;	/* min channels */
41 	unsigned int channels_max;	/* max channels */
42 	size_t buffer_bytes_max;	/* max buffer size */
43 	size_t period_bytes_min;	/* min period size */
44 	size_t period_bytes_max;	/* max period size */
45 	unsigned int periods_min;	/* min # of periods */
46 	unsigned int periods_max;	/* max # of periods */
47 	size_t fifo_size;		/* fifo size in bytes */
48 };
49 
50 struct snd_pcm_status64;
51 struct snd_pcm_substream;
52 
53 struct snd_pcm_audio_tstamp_config; /* definitions further down */
54 struct snd_pcm_audio_tstamp_report;
55 
56 struct snd_pcm_ops {
57 	int (*open)(struct snd_pcm_substream *substream);
58 	int (*close)(struct snd_pcm_substream *substream);
59 	int (*ioctl)(struct snd_pcm_substream * substream,
60 		     unsigned int cmd, void *arg);
61 	int (*hw_params)(struct snd_pcm_substream *substream,
62 			 struct snd_pcm_hw_params *params);
63 	int (*hw_free)(struct snd_pcm_substream *substream);
64 	int (*prepare)(struct snd_pcm_substream *substream);
65 	int (*trigger)(struct snd_pcm_substream *substream, int cmd);
66 	int (*sync_stop)(struct snd_pcm_substream *substream);
67 	snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
68 	int (*get_time_info)(struct snd_pcm_substream *substream,
69 			struct timespec64 *system_ts, struct timespec64 *audio_ts,
70 			struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
71 			struct snd_pcm_audio_tstamp_report *audio_tstamp_report);
72 	int (*fill_silence)(struct snd_pcm_substream *substream, int channel,
73 			    unsigned long pos, unsigned long bytes);
74 	int (*copy)(struct snd_pcm_substream *substream, int channel,
75 		    unsigned long pos, struct iov_iter *iter, unsigned long bytes);
76 	struct page *(*page)(struct snd_pcm_substream *substream,
77 			     unsigned long offset);
78 	int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct *vma);
79 	int (*ack)(struct snd_pcm_substream *substream);
80 
81 	ANDROID_KABI_RESERVE(1);
82 };
83 
84 /*
85  *
86  */
87 
88 #if defined(CONFIG_SND_DYNAMIC_MINORS)
89 #define SNDRV_PCM_DEVICES	(SNDRV_OS_MINORS-2)
90 #else
91 #define SNDRV_PCM_DEVICES	8
92 #endif
93 
94 #define SNDRV_PCM_IOCTL1_RESET		0
95 /* 1 is absent slot. */
96 #define SNDRV_PCM_IOCTL1_CHANNEL_INFO	2
97 /* 3 is absent slot. */
98 #define SNDRV_PCM_IOCTL1_FIFO_SIZE	4
99 #define SNDRV_PCM_IOCTL1_SYNC_ID	5
100 
101 #define SNDRV_PCM_TRIGGER_STOP		0
102 #define SNDRV_PCM_TRIGGER_START		1
103 #define SNDRV_PCM_TRIGGER_PAUSE_PUSH	3
104 #define SNDRV_PCM_TRIGGER_PAUSE_RELEASE	4
105 #define SNDRV_PCM_TRIGGER_SUSPEND	5
106 #define SNDRV_PCM_TRIGGER_RESUME	6
107 #define SNDRV_PCM_TRIGGER_DRAIN		7
108 
109 #define SNDRV_PCM_POS_XRUN		((snd_pcm_uframes_t)-1)
110 
111 /* If you change this don't forget to change rates[] table in pcm_native.c */
112 #define SNDRV_PCM_RATE_5512		(1U<<0)		/* 5512Hz */
113 #define SNDRV_PCM_RATE_8000		(1U<<1)		/* 8000Hz */
114 #define SNDRV_PCM_RATE_11025		(1U<<2)		/* 11025Hz */
115 #define SNDRV_PCM_RATE_16000		(1U<<3)		/* 16000Hz */
116 #define SNDRV_PCM_RATE_22050		(1U<<4)		/* 22050Hz */
117 #define SNDRV_PCM_RATE_32000		(1U<<5)		/* 32000Hz */
118 #define SNDRV_PCM_RATE_44100		(1U<<6)		/* 44100Hz */
119 #define SNDRV_PCM_RATE_48000		(1U<<7)		/* 48000Hz */
120 #define SNDRV_PCM_RATE_64000		(1U<<8)		/* 64000Hz */
121 #define SNDRV_PCM_RATE_88200		(1U<<9)		/* 88200Hz */
122 #define SNDRV_PCM_RATE_96000		(1U<<10)	/* 96000Hz */
123 #define SNDRV_PCM_RATE_176400		(1U<<11)	/* 176400Hz */
124 #define SNDRV_PCM_RATE_192000		(1U<<12)	/* 192000Hz */
125 #define SNDRV_PCM_RATE_352800		(1U<<13)	/* 352800Hz */
126 #define SNDRV_PCM_RATE_384000		(1U<<14)	/* 384000Hz */
127 #define SNDRV_PCM_RATE_705600		(1U<<15)	/* 705600Hz */
128 #define SNDRV_PCM_RATE_768000		(1U<<16)	/* 768000Hz */
129 /* extended rates since 6.12 */
130 #define SNDRV_PCM_RATE_12000		(1U<<17)	/* 12000Hz */
131 #define SNDRV_PCM_RATE_24000		(1U<<18)	/* 24000Hz */
132 #define SNDRV_PCM_RATE_128000		(1U<<19)	/* 128000Hz */
133 
134 #define SNDRV_PCM_RATE_CONTINUOUS	(1U<<30)	/* continuous range */
135 #define SNDRV_PCM_RATE_KNOT		(1U<<31)	/* supports more non-continuous rates */
136 
137 #define SNDRV_PCM_RATE_8000_44100	(SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_11025|\
138 					 SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_22050|\
139 					 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100)
140 #define SNDRV_PCM_RATE_8000_48000	(SNDRV_PCM_RATE_8000_44100|SNDRV_PCM_RATE_48000)
141 #define SNDRV_PCM_RATE_8000_96000	(SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_64000|\
142 					 SNDRV_PCM_RATE_88200|SNDRV_PCM_RATE_96000)
143 #define SNDRV_PCM_RATE_8000_192000	(SNDRV_PCM_RATE_8000_96000|SNDRV_PCM_RATE_176400|\
144 					 SNDRV_PCM_RATE_192000)
145 #define SNDRV_PCM_RATE_8000_384000	(SNDRV_PCM_RATE_8000_192000|\
146 					 SNDRV_PCM_RATE_352800|\
147 					 SNDRV_PCM_RATE_384000)
148 #define SNDRV_PCM_RATE_8000_768000	(SNDRV_PCM_RATE_8000_384000|\
149 					 SNDRV_PCM_RATE_705600|\
150 					 SNDRV_PCM_RATE_768000)
151 #define _SNDRV_PCM_FMTBIT(fmt)		(1ULL << (__force int)SNDRV_PCM_FORMAT_##fmt)
152 #define SNDRV_PCM_FMTBIT_S8		_SNDRV_PCM_FMTBIT(S8)
153 #define SNDRV_PCM_FMTBIT_U8		_SNDRV_PCM_FMTBIT(U8)
154 #define SNDRV_PCM_FMTBIT_S16_LE		_SNDRV_PCM_FMTBIT(S16_LE)
155 #define SNDRV_PCM_FMTBIT_S16_BE		_SNDRV_PCM_FMTBIT(S16_BE)
156 #define SNDRV_PCM_FMTBIT_U16_LE		_SNDRV_PCM_FMTBIT(U16_LE)
157 #define SNDRV_PCM_FMTBIT_U16_BE		_SNDRV_PCM_FMTBIT(U16_BE)
158 #define SNDRV_PCM_FMTBIT_S24_LE		_SNDRV_PCM_FMTBIT(S24_LE)
159 #define SNDRV_PCM_FMTBIT_S24_BE		_SNDRV_PCM_FMTBIT(S24_BE)
160 #define SNDRV_PCM_FMTBIT_U24_LE		_SNDRV_PCM_FMTBIT(U24_LE)
161 #define SNDRV_PCM_FMTBIT_U24_BE		_SNDRV_PCM_FMTBIT(U24_BE)
162 // For S32/U32 formats, 'msbits' hardware parameter is often used to deliver information about the
163 // available bit count in most significant bit. It's for the case of so-called 'left-justified' or
164 // `right-padding` sample which has less width than 32 bit.
165 #define SNDRV_PCM_FMTBIT_S32_LE		_SNDRV_PCM_FMTBIT(S32_LE)
166 #define SNDRV_PCM_FMTBIT_S32_BE		_SNDRV_PCM_FMTBIT(S32_BE)
167 #define SNDRV_PCM_FMTBIT_U32_LE		_SNDRV_PCM_FMTBIT(U32_LE)
168 #define SNDRV_PCM_FMTBIT_U32_BE		_SNDRV_PCM_FMTBIT(U32_BE)
169 #define SNDRV_PCM_FMTBIT_FLOAT_LE	_SNDRV_PCM_FMTBIT(FLOAT_LE)
170 #define SNDRV_PCM_FMTBIT_FLOAT_BE	_SNDRV_PCM_FMTBIT(FLOAT_BE)
171 #define SNDRV_PCM_FMTBIT_FLOAT64_LE	_SNDRV_PCM_FMTBIT(FLOAT64_LE)
172 #define SNDRV_PCM_FMTBIT_FLOAT64_BE	_SNDRV_PCM_FMTBIT(FLOAT64_BE)
173 #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_LE)
174 #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_BE)
175 #define SNDRV_PCM_FMTBIT_MU_LAW		_SNDRV_PCM_FMTBIT(MU_LAW)
176 #define SNDRV_PCM_FMTBIT_A_LAW		_SNDRV_PCM_FMTBIT(A_LAW)
177 #define SNDRV_PCM_FMTBIT_IMA_ADPCM	_SNDRV_PCM_FMTBIT(IMA_ADPCM)
178 #define SNDRV_PCM_FMTBIT_MPEG		_SNDRV_PCM_FMTBIT(MPEG)
179 #define SNDRV_PCM_FMTBIT_GSM		_SNDRV_PCM_FMTBIT(GSM)
180 #define SNDRV_PCM_FMTBIT_S20_LE	_SNDRV_PCM_FMTBIT(S20_LE)
181 #define SNDRV_PCM_FMTBIT_U20_LE	_SNDRV_PCM_FMTBIT(U20_LE)
182 #define SNDRV_PCM_FMTBIT_S20_BE	_SNDRV_PCM_FMTBIT(S20_BE)
183 #define SNDRV_PCM_FMTBIT_U20_BE	_SNDRV_PCM_FMTBIT(U20_BE)
184 #define SNDRV_PCM_FMTBIT_SPECIAL	_SNDRV_PCM_FMTBIT(SPECIAL)
185 #define SNDRV_PCM_FMTBIT_S24_3LE	_SNDRV_PCM_FMTBIT(S24_3LE)
186 #define SNDRV_PCM_FMTBIT_U24_3LE	_SNDRV_PCM_FMTBIT(U24_3LE)
187 #define SNDRV_PCM_FMTBIT_S24_3BE	_SNDRV_PCM_FMTBIT(S24_3BE)
188 #define SNDRV_PCM_FMTBIT_U24_3BE	_SNDRV_PCM_FMTBIT(U24_3BE)
189 #define SNDRV_PCM_FMTBIT_S20_3LE	_SNDRV_PCM_FMTBIT(S20_3LE)
190 #define SNDRV_PCM_FMTBIT_U20_3LE	_SNDRV_PCM_FMTBIT(U20_3LE)
191 #define SNDRV_PCM_FMTBIT_S20_3BE	_SNDRV_PCM_FMTBIT(S20_3BE)
192 #define SNDRV_PCM_FMTBIT_U20_3BE	_SNDRV_PCM_FMTBIT(U20_3BE)
193 #define SNDRV_PCM_FMTBIT_S18_3LE	_SNDRV_PCM_FMTBIT(S18_3LE)
194 #define SNDRV_PCM_FMTBIT_U18_3LE	_SNDRV_PCM_FMTBIT(U18_3LE)
195 #define SNDRV_PCM_FMTBIT_S18_3BE	_SNDRV_PCM_FMTBIT(S18_3BE)
196 #define SNDRV_PCM_FMTBIT_U18_3BE	_SNDRV_PCM_FMTBIT(U18_3BE)
197 #define SNDRV_PCM_FMTBIT_G723_24	_SNDRV_PCM_FMTBIT(G723_24)
198 #define SNDRV_PCM_FMTBIT_G723_24_1B	_SNDRV_PCM_FMTBIT(G723_24_1B)
199 #define SNDRV_PCM_FMTBIT_G723_40	_SNDRV_PCM_FMTBIT(G723_40)
200 #define SNDRV_PCM_FMTBIT_G723_40_1B	_SNDRV_PCM_FMTBIT(G723_40_1B)
201 #define SNDRV_PCM_FMTBIT_DSD_U8		_SNDRV_PCM_FMTBIT(DSD_U8)
202 #define SNDRV_PCM_FMTBIT_DSD_U16_LE	_SNDRV_PCM_FMTBIT(DSD_U16_LE)
203 #define SNDRV_PCM_FMTBIT_DSD_U32_LE	_SNDRV_PCM_FMTBIT(DSD_U32_LE)
204 #define SNDRV_PCM_FMTBIT_DSD_U16_BE	_SNDRV_PCM_FMTBIT(DSD_U16_BE)
205 #define SNDRV_PCM_FMTBIT_DSD_U32_BE	_SNDRV_PCM_FMTBIT(DSD_U32_BE)
206 
207 #ifdef SNDRV_LITTLE_ENDIAN
208 #define SNDRV_PCM_FMTBIT_S16		SNDRV_PCM_FMTBIT_S16_LE
209 #define SNDRV_PCM_FMTBIT_U16		SNDRV_PCM_FMTBIT_U16_LE
210 #define SNDRV_PCM_FMTBIT_S24		SNDRV_PCM_FMTBIT_S24_LE
211 #define SNDRV_PCM_FMTBIT_U24		SNDRV_PCM_FMTBIT_U24_LE
212 #define SNDRV_PCM_FMTBIT_S32		SNDRV_PCM_FMTBIT_S32_LE
213 #define SNDRV_PCM_FMTBIT_U32		SNDRV_PCM_FMTBIT_U32_LE
214 #define SNDRV_PCM_FMTBIT_FLOAT		SNDRV_PCM_FMTBIT_FLOAT_LE
215 #define SNDRV_PCM_FMTBIT_FLOAT64	SNDRV_PCM_FMTBIT_FLOAT64_LE
216 #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
217 #define SNDRV_PCM_FMTBIT_S20		SNDRV_PCM_FMTBIT_S20_LE
218 #define SNDRV_PCM_FMTBIT_U20		SNDRV_PCM_FMTBIT_U20_LE
219 #endif
220 #ifdef SNDRV_BIG_ENDIAN
221 #define SNDRV_PCM_FMTBIT_S16		SNDRV_PCM_FMTBIT_S16_BE
222 #define SNDRV_PCM_FMTBIT_U16		SNDRV_PCM_FMTBIT_U16_BE
223 #define SNDRV_PCM_FMTBIT_S24		SNDRV_PCM_FMTBIT_S24_BE
224 #define SNDRV_PCM_FMTBIT_U24		SNDRV_PCM_FMTBIT_U24_BE
225 #define SNDRV_PCM_FMTBIT_S32		SNDRV_PCM_FMTBIT_S32_BE
226 #define SNDRV_PCM_FMTBIT_U32		SNDRV_PCM_FMTBIT_U32_BE
227 #define SNDRV_PCM_FMTBIT_FLOAT		SNDRV_PCM_FMTBIT_FLOAT_BE
228 #define SNDRV_PCM_FMTBIT_FLOAT64	SNDRV_PCM_FMTBIT_FLOAT64_BE
229 #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
230 #define SNDRV_PCM_FMTBIT_S20		SNDRV_PCM_FMTBIT_S20_BE
231 #define SNDRV_PCM_FMTBIT_U20		SNDRV_PCM_FMTBIT_U20_BE
232 #endif
233 
234 #define _SNDRV_PCM_SUBFMTBIT(fmt)	BIT((__force int)SNDRV_PCM_SUBFORMAT_##fmt)
235 #define SNDRV_PCM_SUBFMTBIT_STD		_SNDRV_PCM_SUBFMTBIT(STD)
236 #define SNDRV_PCM_SUBFMTBIT_MSBITS_MAX	_SNDRV_PCM_SUBFMTBIT(MSBITS_MAX)
237 #define SNDRV_PCM_SUBFMTBIT_MSBITS_20	_SNDRV_PCM_SUBFMTBIT(MSBITS_20)
238 #define SNDRV_PCM_SUBFMTBIT_MSBITS_24	_SNDRV_PCM_SUBFMTBIT(MSBITS_24)
239 
240 struct snd_pcm_file {
241 	struct snd_pcm_substream *substream;
242 	int no_compat_mmap;
243 	unsigned int user_pversion;	/* supported protocol version */
244 };
245 
246 struct snd_pcm_hw_rule;
247 typedef int (*snd_pcm_hw_rule_func_t)(struct snd_pcm_hw_params *params,
248 				      struct snd_pcm_hw_rule *rule);
249 
250 struct snd_pcm_hw_rule {
251 	unsigned int cond;
252 	int var;
253 	int deps[5];
254 
255 	snd_pcm_hw_rule_func_t func;
256 	void *private;
257 };
258 
259 struct snd_pcm_hw_constraints {
260 	struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK -
261 			 SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
262 	struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
263 			     SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
264 	unsigned int rules_num;
265 	unsigned int rules_all;
266 	struct snd_pcm_hw_rule *rules;
267 };
268 
constrs_mask(struct snd_pcm_hw_constraints * constrs,snd_pcm_hw_param_t var)269 static inline struct snd_mask *constrs_mask(struct snd_pcm_hw_constraints *constrs,
270 					    snd_pcm_hw_param_t var)
271 {
272 	return &constrs->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
273 }
274 
constrs_interval(struct snd_pcm_hw_constraints * constrs,snd_pcm_hw_param_t var)275 static inline struct snd_interval *constrs_interval(struct snd_pcm_hw_constraints *constrs,
276 						    snd_pcm_hw_param_t var)
277 {
278 	return &constrs->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
279 }
280 
281 struct snd_ratnum {
282 	unsigned int num;
283 	unsigned int den_min, den_max, den_step;
284 };
285 
286 struct snd_ratden {
287 	unsigned int num_min, num_max, num_step;
288 	unsigned int den;
289 };
290 
291 struct snd_pcm_hw_constraint_ratnums {
292 	int nrats;
293 	const struct snd_ratnum *rats;
294 };
295 
296 struct snd_pcm_hw_constraint_ratdens {
297 	int nrats;
298 	const struct snd_ratden *rats;
299 };
300 
301 struct snd_pcm_hw_constraint_list {
302 	const unsigned int *list;
303 	unsigned int count;
304 	unsigned int mask;
305 };
306 
307 struct snd_pcm_hw_constraint_ranges {
308 	unsigned int count;
309 	const struct snd_interval *ranges;
310 	unsigned int mask;
311 };
312 
313 /*
314  * userspace-provided audio timestamp config to kernel,
315  * structure is for internal use only and filled with dedicated unpack routine
316  */
317 struct snd_pcm_audio_tstamp_config {
318 	/* 5 of max 16 bits used */
319 	u32 type_requested:4;
320 	u32 report_delay:1; /* add total delay to A/D or D/A */
321 };
322 
snd_pcm_unpack_audio_tstamp_config(__u32 data,struct snd_pcm_audio_tstamp_config * config)323 static inline void snd_pcm_unpack_audio_tstamp_config(__u32 data,
324 						struct snd_pcm_audio_tstamp_config *config)
325 {
326 	config->type_requested = data & 0xF;
327 	config->report_delay = (data >> 4) & 1;
328 }
329 
330 /*
331  * kernel-provided audio timestamp report to user-space
332  * structure is for internal use only and read by dedicated pack routine
333  */
334 struct snd_pcm_audio_tstamp_report {
335 	/* 6 of max 16 bits used for bit-fields */
336 
337 	/* for backwards compatibility */
338 	u32 valid:1;
339 
340 	/* actual type if hardware could not support requested timestamp */
341 	u32 actual_type:4;
342 
343 	/* accuracy represented in ns units */
344 	u32 accuracy_report:1; /* 0 if accuracy unknown, 1 if accuracy field is valid */
345 	u32 accuracy; /* up to 4.29s, will be packed in separate field  */
346 };
347 
snd_pcm_pack_audio_tstamp_report(__u32 * data,__u32 * accuracy,const struct snd_pcm_audio_tstamp_report * report)348 static inline void snd_pcm_pack_audio_tstamp_report(__u32 *data, __u32 *accuracy,
349 						const struct snd_pcm_audio_tstamp_report *report)
350 {
351 	u32 tmp;
352 
353 	tmp = report->accuracy_report;
354 	tmp <<= 4;
355 	tmp |= report->actual_type;
356 	tmp <<= 1;
357 	tmp |= report->valid;
358 
359 	*data &= 0xffff; /* zero-clear MSBs */
360 	*data |= (tmp << 16);
361 	*accuracy = report->accuracy;
362 }
363 
364 
365 struct snd_pcm_runtime {
366 	/* -- Status -- */
367 	snd_pcm_state_t state;		/* stream state */
368 	snd_pcm_state_t suspended_state; /* suspended stream state */
369 	struct snd_pcm_substream *trigger_master;
370 	struct timespec64 trigger_tstamp;	/* trigger timestamp */
371 	bool trigger_tstamp_latched;     /* trigger timestamp latched in low-level driver/hardware */
372 	int overrange;
373 	snd_pcm_uframes_t avail_max;
374 	snd_pcm_uframes_t hw_ptr_base;	/* Position at buffer restart */
375 	snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
376 	unsigned long hw_ptr_jiffies;	/* Time when hw_ptr is updated */
377 	unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
378 	snd_pcm_sframes_t delay;	/* extra delay; typically FIFO size */
379 	u64 hw_ptr_wrap;                /* offset for hw_ptr due to boundary wrap-around */
380 
381 	/* -- HW params -- */
382 	snd_pcm_access_t access;	/* access mode */
383 	snd_pcm_format_t format;	/* SNDRV_PCM_FORMAT_* */
384 	snd_pcm_subformat_t subformat;	/* subformat */
385 	unsigned int rate;		/* rate in Hz */
386 	unsigned int channels;		/* channels */
387 	snd_pcm_uframes_t period_size;	/* period size */
388 	unsigned int periods;		/* periods */
389 	snd_pcm_uframes_t buffer_size;	/* buffer size */
390 	snd_pcm_uframes_t min_align;	/* Min alignment for the format */
391 	size_t byte_align;
392 	unsigned int frame_bits;
393 	unsigned int sample_bits;
394 	unsigned int info;
395 	unsigned int rate_num;
396 	unsigned int rate_den;
397 	unsigned int no_period_wakeup: 1;
398 
399 	/* -- SW params; see struct snd_pcm_sw_params for comments -- */
400 	int tstamp_mode;
401   	unsigned int period_step;
402 	snd_pcm_uframes_t start_threshold;
403 	snd_pcm_uframes_t stop_threshold;
404 	snd_pcm_uframes_t silence_threshold;
405 	snd_pcm_uframes_t silence_size;
406 	snd_pcm_uframes_t boundary;
407 
408 	/* internal data of auto-silencer */
409 	snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
410 	snd_pcm_uframes_t silence_filled; /* already filled part of silence area */
411 
412 	bool std_sync_id;		/* hardware synchronization - standard per card ID */
413 
414 	/* -- mmap -- */
415 	struct snd_pcm_mmap_status *status;
416 	struct snd_pcm_mmap_control *control;
417 
418 	/* -- locking / scheduling -- */
419 	snd_pcm_uframes_t twake; 	/* do transfer (!poll) wakeup if non-zero */
420 	wait_queue_head_t sleep;	/* poll sleep */
421 	wait_queue_head_t tsleep;	/* transfer sleep */
422 	struct snd_fasync *fasync;
423 	bool stop_operating;		/* sync_stop will be called */
424 	struct mutex buffer_mutex;	/* protect for buffer changes */
425 	atomic_t buffer_accessing;	/* >0: in r/w operation, <0: blocked */
426 
427 	/* -- private section -- */
428 	void *private_data;
429 	void (*private_free)(struct snd_pcm_runtime *runtime);
430 
431 	/* -- hardware description -- */
432 	struct snd_pcm_hardware hw;
433 	struct snd_pcm_hw_constraints hw_constraints;
434 
435 	/* -- timer -- */
436 	unsigned int timer_resolution;	/* timer resolution */
437 	int tstamp_type;		/* timestamp type */
438 
439 	/* -- DMA -- */
440 	unsigned char *dma_area;	/* DMA area */
441 	dma_addr_t dma_addr;		/* physical bus address (not accessible from main CPU) */
442 	size_t dma_bytes;		/* size of DMA area */
443 
444 	struct snd_dma_buffer *dma_buffer_p;	/* allocated buffer */
445 	unsigned int buffer_changed:1;	/* buffer allocation changed; set only in managed mode */
446 
447 	/* -- audio timestamp config -- */
448 	struct snd_pcm_audio_tstamp_config audio_tstamp_config;
449 	struct snd_pcm_audio_tstamp_report audio_tstamp_report;
450 	struct timespec64 driver_tstamp;
451 
452 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
453 	/* -- OSS things -- */
454 	struct snd_pcm_oss_runtime oss;
455 #endif
456 
457 	ANDROID_KABI_RESERVE(1);
458 	ANDROID_KABI_RESERVE(2);
459 };
460 
461 struct snd_pcm_group {		/* keep linked substreams */
462 	spinlock_t lock;
463 	struct mutex mutex;
464 	struct list_head substreams;
465 	refcount_t refs;
466 };
467 
468 struct pid;
469 
470 struct snd_pcm_substream {
471 	struct snd_pcm *pcm;
472 	struct snd_pcm_str *pstr;
473 	void *private_data;		/* copied from pcm->private_data */
474 	int number;
475 	char name[32];			/* substream name */
476 	int stream;			/* stream (direction) */
477 	struct pm_qos_request latency_pm_qos_req; /* pm_qos request */
478 	size_t buffer_bytes_max;	/* limit ring buffer size */
479 	struct snd_dma_buffer dma_buffer;
480 	size_t dma_max;
481 	/* -- hardware operations -- */
482 	const struct snd_pcm_ops *ops;
483 	/* -- runtime information -- */
484 	struct snd_pcm_runtime *runtime;
485         /* -- timer section -- */
486 	struct snd_timer *timer;		/* timer */
487 	unsigned timer_running: 1;	/* time is running */
488 	long wait_time;	/* time in ms for R/W to wait for avail */
489 	/* -- next substream -- */
490 	struct snd_pcm_substream *next;
491 	/* -- linked substreams -- */
492 	struct list_head link_list;	/* linked list member */
493 	struct snd_pcm_group self_group;	/* fake group for non linked substream (with substream lock inside) */
494 	struct snd_pcm_group *group;		/* pointer to current group */
495 	/* -- assigned files -- */
496 	int ref_count;
497 	atomic_t mmap_count;
498 	unsigned int f_flags;
499 	void (*pcm_release)(struct snd_pcm_substream *);
500 	struct pid *pid;
501 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
502 	/* -- OSS things -- */
503 	struct snd_pcm_oss_substream oss;
504 #endif
505 #ifdef CONFIG_SND_VERBOSE_PROCFS
506 	struct snd_info_entry *proc_root;
507 #endif /* CONFIG_SND_VERBOSE_PROCFS */
508 	/* misc flags */
509 	unsigned int hw_opened: 1;
510 	unsigned int managed_buffer_alloc:1;
511 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
512 	unsigned int xrun_counter; /* number of times xrun happens */
513 #endif /* CONFIG_SND_PCM_XRUN_DEBUG */
514 
515 	ANDROID_KABI_RESERVE(1);
516 };
517 
518 #define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
519 
520 
521 struct snd_pcm_str {
522 	int stream;				/* stream (direction) */
523 	struct snd_pcm *pcm;
524 	/* -- substreams -- */
525 	unsigned int substream_count;
526 	unsigned int substream_opened;
527 	struct snd_pcm_substream *substream;
528 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
529 	/* -- OSS things -- */
530 	struct snd_pcm_oss_stream oss;
531 #endif
532 #ifdef CONFIG_SND_VERBOSE_PROCFS
533 	struct snd_info_entry *proc_root;
534 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
535 	unsigned int xrun_debug;	/* 0 = disabled, 1 = verbose, 2 = stacktrace */
536 #endif
537 #endif
538 	struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
539 	struct device *dev;
540 
541 	ANDROID_KABI_RESERVE(1);
542 };
543 
544 struct snd_pcm {
545 	struct snd_card *card;
546 	struct list_head list;
547 	int device; /* device number */
548 	unsigned int info_flags;
549 	unsigned short dev_class;
550 	unsigned short dev_subclass;
551 	char id[64];
552 	char name[80];
553 	struct snd_pcm_str streams[2];
554 	struct mutex open_mutex;
555 	wait_queue_head_t open_wait;
556 	void *private_data;
557 	void (*private_free) (struct snd_pcm *pcm);
558 	bool internal; /* pcm is for internal use only */
559 	bool nonatomic; /* whole PCM operations are in non-atomic context */
560 	bool no_device_suspend; /* don't invoke device PM suspend */
561 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
562 	struct snd_pcm_oss oss;
563 #endif
564 
565 	ANDROID_KABI_RESERVE(1);
566 };
567 
568 /*
569  *  Registering
570  */
571 
572 extern const struct file_operations snd_pcm_f_ops[2];
573 
574 int snd_pcm_new(struct snd_card *card, const char *id, int device,
575 		int playback_count, int capture_count,
576 		struct snd_pcm **rpcm);
577 int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
578 		int playback_count, int capture_count,
579 		struct snd_pcm **rpcm);
580 int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count);
581 
582 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
583 struct snd_pcm_notify {
584 	int (*n_register) (struct snd_pcm * pcm);
585 	int (*n_disconnect) (struct snd_pcm * pcm);
586 	int (*n_unregister) (struct snd_pcm * pcm);
587 	struct list_head list;
588 };
589 int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
590 #endif
591 
592 /*
593  *  Native I/O
594  */
595 
596 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info);
597 int snd_pcm_info_user(struct snd_pcm_substream *substream,
598 		      struct snd_pcm_info __user *info);
599 int snd_pcm_status64(struct snd_pcm_substream *substream,
600 		     struct snd_pcm_status64 *status);
601 int snd_pcm_start(struct snd_pcm_substream *substream);
602 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
603 int snd_pcm_drain_done(struct snd_pcm_substream *substream);
604 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
605 #ifdef CONFIG_PM
606 int snd_pcm_suspend_all(struct snd_pcm *pcm);
607 #else
snd_pcm_suspend_all(struct snd_pcm * pcm)608 static inline int snd_pcm_suspend_all(struct snd_pcm *pcm)
609 {
610 	return 0;
611 }
612 #endif
613 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
614 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
615 			   struct snd_pcm_substream **rsubstream);
616 void snd_pcm_release_substream(struct snd_pcm_substream *substream);
617 int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
618 			     struct snd_pcm_substream **rsubstream);
619 void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
620 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
621 
622 
623 #ifdef CONFIG_SND_DEBUG
624 void snd_pcm_debug_name(struct snd_pcm_substream *substream,
625 			   char *name, size_t len);
626 #else
627 static inline void
snd_pcm_debug_name(struct snd_pcm_substream * substream,char * buf,size_t size)628 snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
629 {
630 	*buf = 0;
631 }
632 #endif
633 
634 /*
635  *  PCM library
636  */
637 
638 /**
639  * snd_pcm_stream_linked - Check whether the substream is linked with others
640  * @substream: substream to check
641  *
642  * Return: true if the given substream is being linked with others
643  */
snd_pcm_stream_linked(struct snd_pcm_substream * substream)644 static inline int snd_pcm_stream_linked(struct snd_pcm_substream *substream)
645 {
646 	return substream->group != &substream->self_group;
647 }
648 
649 void snd_pcm_stream_lock(struct snd_pcm_substream *substream);
650 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream);
651 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream);
652 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream);
653 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream);
654 unsigned long _snd_pcm_stream_lock_irqsave_nested(struct snd_pcm_substream *substream);
655 
656 /**
657  * snd_pcm_stream_lock_irqsave - Lock the PCM stream
658  * @substream: PCM substream
659  * @flags: irq flags
660  *
661  * This locks the PCM stream like snd_pcm_stream_lock() but with the local
662  * IRQ (only when nonatomic is false).  In nonatomic case, this is identical
663  * as snd_pcm_stream_lock().
664  */
665 #define snd_pcm_stream_lock_irqsave(substream, flags)		 \
666 	do {							 \
667 		typecheck(unsigned long, flags);		 \
668 		flags = _snd_pcm_stream_lock_irqsave(substream); \
669 	} while (0)
670 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
671 				      unsigned long flags);
672 
673 /**
674  * snd_pcm_stream_lock_irqsave_nested - Single-nested PCM stream locking
675  * @substream: PCM substream
676  * @flags: irq flags
677  *
678  * This locks the PCM stream like snd_pcm_stream_lock_irqsave() but with
679  * the single-depth lockdep subclass.
680  */
681 #define snd_pcm_stream_lock_irqsave_nested(substream, flags)		\
682 	do {								\
683 		typecheck(unsigned long, flags);			\
684 		flags = _snd_pcm_stream_lock_irqsave_nested(substream); \
685 	} while (0)
686 
687 /* definitions for guard(); use like guard(pcm_stream_lock) */
688 DEFINE_LOCK_GUARD_1(pcm_stream_lock, struct snd_pcm_substream,
689 		    snd_pcm_stream_lock(_T->lock),
690 		    snd_pcm_stream_unlock(_T->lock))
691 DEFINE_LOCK_GUARD_1(pcm_stream_lock_irq, struct snd_pcm_substream,
692 		    snd_pcm_stream_lock_irq(_T->lock),
693 		    snd_pcm_stream_unlock_irq(_T->lock))
694 DEFINE_LOCK_GUARD_1(pcm_stream_lock_irqsave, struct snd_pcm_substream,
695 		    snd_pcm_stream_lock_irqsave(_T->lock, _T->flags),
696 		    snd_pcm_stream_unlock_irqrestore(_T->lock, _T->flags),
697 		    unsigned long flags)
698 
699 /**
700  * snd_pcm_group_for_each_entry - iterate over the linked substreams
701  * @s: the iterator
702  * @substream: the substream
703  *
704  * Iterate over the all linked substreams to the given @substream.
705  * When @substream isn't linked with any others, this gives returns @substream
706  * itself once.
707  */
708 #define snd_pcm_group_for_each_entry(s, substream) \
709 	list_for_each_entry(s, &substream->group->substreams, link_list)
710 
711 #define for_each_pcm_streams(stream)			\
712 	for (stream  = SNDRV_PCM_STREAM_PLAYBACK;	\
713 	     stream <= SNDRV_PCM_STREAM_LAST;		\
714 	     stream++)
715 
716 /**
717  * snd_pcm_running - Check whether the substream is in a running state
718  * @substream: substream to check
719  *
720  * Return: true if the given substream is in the state RUNNING, or in the
721  * state DRAINING for playback.
722  */
snd_pcm_running(struct snd_pcm_substream * substream)723 static inline int snd_pcm_running(struct snd_pcm_substream *substream)
724 {
725 	return (substream->runtime->state == SNDRV_PCM_STATE_RUNNING ||
726 		(substream->runtime->state == SNDRV_PCM_STATE_DRAINING &&
727 		 substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
728 }
729 
730 /**
731  * __snd_pcm_set_state - Change the current PCM state
732  * @runtime: PCM runtime to set
733  * @state: the current state to set
734  *
735  * Call within the stream lock
736  */
__snd_pcm_set_state(struct snd_pcm_runtime * runtime,snd_pcm_state_t state)737 static inline void __snd_pcm_set_state(struct snd_pcm_runtime *runtime,
738 				       snd_pcm_state_t state)
739 {
740 	runtime->state = state;
741 	runtime->status->state = state; /* copy for mmap */
742 }
743 
744 /**
745  * bytes_to_samples - Unit conversion of the size from bytes to samples
746  * @runtime: PCM runtime instance
747  * @size: size in bytes
748  *
749  * Return: the size in samples
750  */
bytes_to_samples(struct snd_pcm_runtime * runtime,ssize_t size)751 static inline ssize_t bytes_to_samples(struct snd_pcm_runtime *runtime, ssize_t size)
752 {
753 	return size * 8 / runtime->sample_bits;
754 }
755 
756 /**
757  * bytes_to_frames - Unit conversion of the size from bytes to frames
758  * @runtime: PCM runtime instance
759  * @size: size in bytes
760  *
761  * Return: the size in frames
762  */
bytes_to_frames(struct snd_pcm_runtime * runtime,ssize_t size)763 static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)
764 {
765 	return size * 8 / runtime->frame_bits;
766 }
767 
768 /**
769  * samples_to_bytes - Unit conversion of the size from samples to bytes
770  * @runtime: PCM runtime instance
771  * @size: size in samples
772  *
773  * Return: the byte size
774  */
samples_to_bytes(struct snd_pcm_runtime * runtime,ssize_t size)775 static inline ssize_t samples_to_bytes(struct snd_pcm_runtime *runtime, ssize_t size)
776 {
777 	return size * runtime->sample_bits / 8;
778 }
779 
780 /**
781  * frames_to_bytes - Unit conversion of the size from frames to bytes
782  * @runtime: PCM runtime instance
783  * @size: size in frames
784  *
785  * Return: the byte size
786  */
frames_to_bytes(struct snd_pcm_runtime * runtime,snd_pcm_sframes_t size)787 static inline ssize_t frames_to_bytes(struct snd_pcm_runtime *runtime, snd_pcm_sframes_t size)
788 {
789 	return size * runtime->frame_bits / 8;
790 }
791 
792 /**
793  * frame_aligned - Check whether the byte size is aligned to frames
794  * @runtime: PCM runtime instance
795  * @bytes: size in bytes
796  *
797  * Return: true if aligned, or false if not
798  */
frame_aligned(struct snd_pcm_runtime * runtime,ssize_t bytes)799 static inline int frame_aligned(struct snd_pcm_runtime *runtime, ssize_t bytes)
800 {
801 	return bytes % runtime->byte_align == 0;
802 }
803 
804 /**
805  * snd_pcm_lib_buffer_bytes - Get the buffer size of the current PCM in bytes
806  * @substream: PCM substream
807  *
808  * Return: buffer byte size
809  */
snd_pcm_lib_buffer_bytes(struct snd_pcm_substream * substream)810 static inline size_t snd_pcm_lib_buffer_bytes(struct snd_pcm_substream *substream)
811 {
812 	struct snd_pcm_runtime *runtime = substream->runtime;
813 	return frames_to_bytes(runtime, runtime->buffer_size);
814 }
815 
816 /**
817  * snd_pcm_lib_period_bytes - Get the period size of the current PCM in bytes
818  * @substream: PCM substream
819  *
820  * Return: period byte size
821  */
snd_pcm_lib_period_bytes(struct snd_pcm_substream * substream)822 static inline size_t snd_pcm_lib_period_bytes(struct snd_pcm_substream *substream)
823 {
824 	struct snd_pcm_runtime *runtime = substream->runtime;
825 	return frames_to_bytes(runtime, runtime->period_size);
826 }
827 
828 /**
829  * snd_pcm_playback_avail - Get the available (writable) space for playback
830  * @runtime: PCM runtime instance
831  *
832  * Result is between 0 ... (boundary - 1)
833  *
834  * Return: available frame size
835  */
snd_pcm_playback_avail(struct snd_pcm_runtime * runtime)836 static inline snd_pcm_uframes_t snd_pcm_playback_avail(struct snd_pcm_runtime *runtime)
837 {
838 	snd_pcm_sframes_t avail = runtime->status->hw_ptr + runtime->buffer_size - runtime->control->appl_ptr;
839 	if (avail < 0)
840 		avail += runtime->boundary;
841 	else if ((snd_pcm_uframes_t) avail >= runtime->boundary)
842 		avail -= runtime->boundary;
843 	return avail;
844 }
845 
846 /**
847  * snd_pcm_capture_avail - Get the available (readable) space for capture
848  * @runtime: PCM runtime instance
849  *
850  * Result is between 0 ... (boundary - 1)
851  *
852  * Return: available frame size
853  */
snd_pcm_capture_avail(struct snd_pcm_runtime * runtime)854 static inline snd_pcm_uframes_t snd_pcm_capture_avail(struct snd_pcm_runtime *runtime)
855 {
856 	snd_pcm_sframes_t avail = runtime->status->hw_ptr - runtime->control->appl_ptr;
857 	if (avail < 0)
858 		avail += runtime->boundary;
859 	return avail;
860 }
861 
862 /**
863  * snd_pcm_playback_hw_avail - Get the queued space for playback
864  * @runtime: PCM runtime instance
865  *
866  * Return: available frame size
867  */
snd_pcm_playback_hw_avail(struct snd_pcm_runtime * runtime)868 static inline snd_pcm_sframes_t snd_pcm_playback_hw_avail(struct snd_pcm_runtime *runtime)
869 {
870 	return runtime->buffer_size - snd_pcm_playback_avail(runtime);
871 }
872 
873 /**
874  * snd_pcm_capture_hw_avail - Get the free space for capture
875  * @runtime: PCM runtime instance
876  *
877  * Return: available frame size
878  */
snd_pcm_capture_hw_avail(struct snd_pcm_runtime * runtime)879 static inline snd_pcm_sframes_t snd_pcm_capture_hw_avail(struct snd_pcm_runtime *runtime)
880 {
881 	return runtime->buffer_size - snd_pcm_capture_avail(runtime);
882 }
883 
884 /**
885  * snd_pcm_playback_ready - check whether the playback buffer is available
886  * @substream: the pcm substream instance
887  *
888  * Checks whether enough free space is available on the playback buffer.
889  *
890  * Return: Non-zero if available, or zero if not.
891  */
snd_pcm_playback_ready(struct snd_pcm_substream * substream)892 static inline int snd_pcm_playback_ready(struct snd_pcm_substream *substream)
893 {
894 	struct snd_pcm_runtime *runtime = substream->runtime;
895 	return snd_pcm_playback_avail(runtime) >= runtime->control->avail_min;
896 }
897 
898 /**
899  * snd_pcm_capture_ready - check whether the capture buffer is available
900  * @substream: the pcm substream instance
901  *
902  * Checks whether enough capture data is available on the capture buffer.
903  *
904  * Return: Non-zero if available, or zero if not.
905  */
snd_pcm_capture_ready(struct snd_pcm_substream * substream)906 static inline int snd_pcm_capture_ready(struct snd_pcm_substream *substream)
907 {
908 	struct snd_pcm_runtime *runtime = substream->runtime;
909 	return snd_pcm_capture_avail(runtime) >= runtime->control->avail_min;
910 }
911 
912 /**
913  * snd_pcm_playback_data - check whether any data exists on the playback buffer
914  * @substream: the pcm substream instance
915  *
916  * Checks whether any data exists on the playback buffer.
917  *
918  * Return: Non-zero if any data exists, or zero if not. If stop_threshold
919  * is bigger or equal to boundary, then this function returns always non-zero.
920  */
snd_pcm_playback_data(struct snd_pcm_substream * substream)921 static inline int snd_pcm_playback_data(struct snd_pcm_substream *substream)
922 {
923 	struct snd_pcm_runtime *runtime = substream->runtime;
924 
925 	if (runtime->stop_threshold >= runtime->boundary)
926 		return 1;
927 	return snd_pcm_playback_avail(runtime) < runtime->buffer_size;
928 }
929 
930 /**
931  * snd_pcm_playback_empty - check whether the playback buffer is empty
932  * @substream: the pcm substream instance
933  *
934  * Checks whether the playback buffer is empty.
935  *
936  * Return: Non-zero if empty, or zero if not.
937  */
snd_pcm_playback_empty(struct snd_pcm_substream * substream)938 static inline int snd_pcm_playback_empty(struct snd_pcm_substream *substream)
939 {
940 	struct snd_pcm_runtime *runtime = substream->runtime;
941 	return snd_pcm_playback_avail(runtime) >= runtime->buffer_size;
942 }
943 
944 /**
945  * snd_pcm_capture_empty - check whether the capture buffer is empty
946  * @substream: the pcm substream instance
947  *
948  * Checks whether the capture buffer is empty.
949  *
950  * Return: Non-zero if empty, or zero if not.
951  */
snd_pcm_capture_empty(struct snd_pcm_substream * substream)952 static inline int snd_pcm_capture_empty(struct snd_pcm_substream *substream)
953 {
954 	struct snd_pcm_runtime *runtime = substream->runtime;
955 	return snd_pcm_capture_avail(runtime) == 0;
956 }
957 
958 /**
959  * snd_pcm_trigger_done - Mark the master substream
960  * @substream: the pcm substream instance
961  * @master: the linked master substream
962  *
963  * When multiple substreams of the same card are linked and the hardware
964  * supports the single-shot operation, the driver calls this in the loop
965  * in snd_pcm_group_for_each_entry() for marking the substream as "done".
966  * Then most of trigger operations are performed only to the given master
967  * substream.
968  *
969  * The trigger_master mark is cleared at timestamp updates at the end
970  * of trigger operations.
971  */
snd_pcm_trigger_done(struct snd_pcm_substream * substream,struct snd_pcm_substream * master)972 static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream,
973 					struct snd_pcm_substream *master)
974 {
975 	substream->runtime->trigger_master = master;
976 }
977 
hw_is_mask(int var)978 static inline int hw_is_mask(int var)
979 {
980 	return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
981 		var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
982 }
983 
hw_is_interval(int var)984 static inline int hw_is_interval(int var)
985 {
986 	return var >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
987 		var <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
988 }
989 
hw_param_mask(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)990 static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
991 				     snd_pcm_hw_param_t var)
992 {
993 	return ¶ms->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
994 }
995 
hw_param_interval(struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)996 static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
997 					     snd_pcm_hw_param_t var)
998 {
999 	return ¶ms->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
1000 }
1001 
hw_param_mask_c(const struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)1002 static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
1003 					     snd_pcm_hw_param_t var)
1004 {
1005 	return ¶ms->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
1006 }
1007 
hw_param_interval_c(const struct snd_pcm_hw_params * params,snd_pcm_hw_param_t var)1008 static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
1009 						     snd_pcm_hw_param_t var)
1010 {
1011 	return ¶ms->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
1012 }
1013 
1014 /**
1015  * params_channels - Get the number of channels from the hw params
1016  * @p: hw params
1017  *
1018  * Return: the number of channels
1019  */
params_channels(const struct snd_pcm_hw_params * p)1020 static inline unsigned int params_channels(const struct snd_pcm_hw_params *p)
1021 {
1022 	return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_CHANNELS)->min;
1023 }
1024 
1025 /**
1026  * params_rate - Get the sample rate from the hw params
1027  * @p: hw params
1028  *
1029  * Return: the sample rate
1030  */
params_rate(const struct snd_pcm_hw_params * p)1031 static inline unsigned int params_rate(const struct snd_pcm_hw_params *p)
1032 {
1033 	return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_RATE)->min;
1034 }
1035 
1036 /**
1037  * params_period_size - Get the period size (in frames) from the hw params
1038  * @p: hw params
1039  *
1040  * Return: the period size in frames
1041  */
params_period_size(const struct snd_pcm_hw_params * p)1042 static inline unsigned int params_period_size(const struct snd_pcm_hw_params *p)
1043 {
1044 	return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIOD_SIZE)->min;
1045 }
1046 
1047 /**
1048  * params_periods - Get the number of periods from the hw params
1049  * @p: hw params
1050  *
1051  * Return: the number of periods
1052  */
params_periods(const struct snd_pcm_hw_params * p)1053 static inline unsigned int params_periods(const struct snd_pcm_hw_params *p)
1054 {
1055 	return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIODS)->min;
1056 }
1057 
1058 /**
1059  * params_buffer_size - Get the buffer size (in frames) from the hw params
1060  * @p: hw params
1061  *
1062  * Return: the buffer size in frames
1063  */
params_buffer_size(const struct snd_pcm_hw_params * p)1064 static inline unsigned int params_buffer_size(const struct snd_pcm_hw_params *p)
1065 {
1066 	return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)->min;
1067 }
1068 
1069 /**
1070  * params_buffer_bytes - Get the buffer size (in bytes) from the hw params
1071  * @p: hw params
1072  *
1073  * Return: the buffer size in bytes
1074  */
params_buffer_bytes(const struct snd_pcm_hw_params * p)1075 static inline unsigned int params_buffer_bytes(const struct snd_pcm_hw_params *p)
1076 {
1077 	return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)->min;
1078 }
1079 
1080 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v);
1081 int snd_interval_list(struct snd_interval *i, unsigned int count,
1082 		      const unsigned int *list, unsigned int mask);
1083 int snd_interval_ranges(struct snd_interval *i, unsigned int count,
1084 			const struct snd_interval *list, unsigned int mask);
1085 int snd_interval_ratnum(struct snd_interval *i,
1086 			unsigned int rats_count, const struct snd_ratnum *rats,
1087 			unsigned int *nump, unsigned int *denp);
1088 
1089 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params);
1090 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var);
1091 
1092 int snd_pcm_hw_refine(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
1093 
1094 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1095 				 u_int64_t mask);
1096 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1097 				 unsigned int min, unsigned int max);
1098 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var);
1099 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1100 			       unsigned int cond,
1101 			       snd_pcm_hw_param_t var,
1102 			       const struct snd_pcm_hw_constraint_list *l);
1103 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
1104 				 unsigned int cond,
1105 				 snd_pcm_hw_param_t var,
1106 				 const struct snd_pcm_hw_constraint_ranges *r);
1107 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1108 				  unsigned int cond,
1109 				  snd_pcm_hw_param_t var,
1110 				  const struct snd_pcm_hw_constraint_ratnums *r);
1111 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1112 				  unsigned int cond,
1113 				  snd_pcm_hw_param_t var,
1114 				  const struct snd_pcm_hw_constraint_ratdens *r);
1115 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1116 				 unsigned int cond,
1117 				 unsigned int width,
1118 				 unsigned int msbits);
1119 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1120 			       unsigned int cond,
1121 			       snd_pcm_hw_param_t var,
1122 			       unsigned long step);
1123 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1124 			       unsigned int cond,
1125 			       snd_pcm_hw_param_t var);
1126 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
1127 			       unsigned int base_rate);
1128 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime,
1129 			unsigned int cond,
1130 			int var,
1131 			snd_pcm_hw_rule_func_t func, void *private,
1132 			int dep, ...);
1133 
1134 /**
1135  * snd_pcm_hw_constraint_single() - Constrain parameter to a single value
1136  * @runtime: PCM runtime instance
1137  * @var: The hw_params variable to constrain
1138  * @val: The value to constrain to
1139  *
1140  * Return: Positive if the value is changed, zero if it's not changed, or a
1141  * negative error code.
1142  */
snd_pcm_hw_constraint_single(struct snd_pcm_runtime * runtime,snd_pcm_hw_param_t var,unsigned int val)1143 static inline int snd_pcm_hw_constraint_single(
1144 	struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1145 	unsigned int val)
1146 {
1147 	return snd_pcm_hw_constraint_minmax(runtime, var, val, val);
1148 }
1149 
1150 int snd_pcm_format_signed(snd_pcm_format_t format);
1151 int snd_pcm_format_unsigned(snd_pcm_format_t format);
1152 int snd_pcm_format_linear(snd_pcm_format_t format);
1153 int snd_pcm_format_little_endian(snd_pcm_format_t format);
1154 int snd_pcm_format_big_endian(snd_pcm_format_t format);
1155 #if 0 /* just for kernel-doc */
1156 /**
1157  * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
1158  * @format: the format to check
1159  *
1160  * Return: 1 if the given PCM format is CPU-endian, 0 if
1161  * opposite, or a negative error code if endian not specified.
1162  */
1163 int snd_pcm_format_cpu_endian(snd_pcm_format_t format);
1164 #endif /* DocBook */
1165 #ifdef SNDRV_LITTLE_ENDIAN
1166 #define snd_pcm_format_cpu_endian(format) snd_pcm_format_little_endian(format)
1167 #else
1168 #define snd_pcm_format_cpu_endian(format) snd_pcm_format_big_endian(format)
1169 #endif
1170 int snd_pcm_format_width(snd_pcm_format_t format);			/* in bits */
1171 int snd_pcm_format_physical_width(snd_pcm_format_t format);		/* in bits */
1172 ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples);
1173 const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format);
1174 int snd_pcm_format_set_silence(snd_pcm_format_t format, void *buf, unsigned int frames);
1175 
1176 void snd_pcm_set_ops(struct snd_pcm * pcm, int direction,
1177 		     const struct snd_pcm_ops *ops);
1178 void snd_pcm_set_sync_per_card(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params,
1179 			       const unsigned char *id, unsigned int len);
1180 /**
1181  * snd_pcm_set_sync - set the PCM sync id
1182  * @substream: the pcm substream
1183  *
1184  * Use the default PCM sync identifier for the specific card.
1185  */
snd_pcm_set_sync(struct snd_pcm_substream * substream)1186 static inline void snd_pcm_set_sync(struct snd_pcm_substream *substream)
1187 {
1188 	substream->runtime->std_sync_id = true;
1189 }
1190 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1191 		      unsigned int cmd, void *arg);
1192 void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream *substream);
1193 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
1194 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
1195 				     void *buf, bool interleaved,
1196 				     snd_pcm_uframes_t frames, bool in_kernel);
1197 
1198 static inline snd_pcm_sframes_t
snd_pcm_lib_write(struct snd_pcm_substream * substream,const void __user * buf,snd_pcm_uframes_t frames)1199 snd_pcm_lib_write(struct snd_pcm_substream *substream,
1200 		  const void __user *buf, snd_pcm_uframes_t frames)
1201 {
1202 	return __snd_pcm_lib_xfer(substream, (void __force *)buf, true, frames, false);
1203 }
1204 
1205 static inline snd_pcm_sframes_t
snd_pcm_lib_read(struct snd_pcm_substream * substream,void __user * buf,snd_pcm_uframes_t frames)1206 snd_pcm_lib_read(struct snd_pcm_substream *substream,
1207 		 void __user *buf, snd_pcm_uframes_t frames)
1208 {
1209 	return __snd_pcm_lib_xfer(substream, (void __force *)buf, true, frames, false);
1210 }
1211 
1212 static inline snd_pcm_sframes_t
snd_pcm_lib_writev(struct snd_pcm_substream * substream,void __user ** bufs,snd_pcm_uframes_t frames)1213 snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1214 		   void __user **bufs, snd_pcm_uframes_t frames)
1215 {
1216 	return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames, false);
1217 }
1218 
1219 static inline snd_pcm_sframes_t
snd_pcm_lib_readv(struct snd_pcm_substream * substream,void __user ** bufs,snd_pcm_uframes_t frames)1220 snd_pcm_lib_readv(struct snd_pcm_substream *substream,
1221 		  void __user **bufs, snd_pcm_uframes_t frames)
1222 {
1223 	return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames, false);
1224 }
1225 
1226 static inline snd_pcm_sframes_t
snd_pcm_kernel_write(struct snd_pcm_substream * substream,const void * buf,snd_pcm_uframes_t frames)1227 snd_pcm_kernel_write(struct snd_pcm_substream *substream,
1228 		     const void *buf, snd_pcm_uframes_t frames)
1229 {
1230 	return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames, true);
1231 }
1232 
1233 static inline snd_pcm_sframes_t
snd_pcm_kernel_read(struct snd_pcm_substream * substream,void * buf,snd_pcm_uframes_t frames)1234 snd_pcm_kernel_read(struct snd_pcm_substream *substream,
1235 		    void *buf, snd_pcm_uframes_t frames)
1236 {
1237 	return __snd_pcm_lib_xfer(substream, buf, true, frames, true);
1238 }
1239 
1240 static inline snd_pcm_sframes_t
snd_pcm_kernel_writev(struct snd_pcm_substream * substream,void ** bufs,snd_pcm_uframes_t frames)1241 snd_pcm_kernel_writev(struct snd_pcm_substream *substream,
1242 		      void **bufs, snd_pcm_uframes_t frames)
1243 {
1244 	return __snd_pcm_lib_xfer(substream, bufs, false, frames, true);
1245 }
1246 
1247 static inline snd_pcm_sframes_t
snd_pcm_kernel_readv(struct snd_pcm_substream * substream,void ** bufs,snd_pcm_uframes_t frames)1248 snd_pcm_kernel_readv(struct snd_pcm_substream *substream,
1249 		     void **bufs, snd_pcm_uframes_t frames)
1250 {
1251 	return __snd_pcm_lib_xfer(substream, bufs, false, frames, true);
1252 }
1253 
1254 int snd_pcm_hw_limit_rates(struct snd_pcm_hardware *hw);
1255 
1256 static inline int
snd_pcm_limit_hw_rates(struct snd_pcm_runtime * runtime)1257 snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
1258 {
1259 	return snd_pcm_hw_limit_rates(&runtime->hw);
1260 }
1261 
1262 unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate);
1263 unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit);
1264 unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
1265 					 unsigned int rates_b);
1266 unsigned int snd_pcm_rate_range_to_bits(unsigned int rate_min,
1267 					unsigned int rate_max);
1268 
1269 /**
1270  * snd_pcm_set_runtime_buffer - Set the PCM runtime buffer
1271  * @substream: PCM substream to set
1272  * @bufp: the buffer information, NULL to clear
1273  *
1274  * Copy the buffer information to runtime->dma_buffer when @bufp is non-NULL.
1275  * Otherwise it clears the current buffer information.
1276  */
snd_pcm_set_runtime_buffer(struct snd_pcm_substream * substream,struct snd_dma_buffer * bufp)1277 static inline void snd_pcm_set_runtime_buffer(struct snd_pcm_substream *substream,
1278 					      struct snd_dma_buffer *bufp)
1279 {
1280 	struct snd_pcm_runtime *runtime = substream->runtime;
1281 	if (bufp) {
1282 		runtime->dma_buffer_p = bufp;
1283 		runtime->dma_area = bufp->area;
1284 		runtime->dma_addr = bufp->addr;
1285 		runtime->dma_bytes = bufp->bytes;
1286 	} else {
1287 		runtime->dma_buffer_p = NULL;
1288 		runtime->dma_area = NULL;
1289 		runtime->dma_addr = 0;
1290 		runtime->dma_bytes = 0;
1291 	}
1292 }
1293 
1294 /**
1295  * snd_pcm_gettime - Fill the timespec64 depending on the timestamp mode
1296  * @runtime: PCM runtime instance
1297  * @tv: timespec64 to fill
1298  */
snd_pcm_gettime(struct snd_pcm_runtime * runtime,struct timespec64 * tv)1299 static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
1300 				   struct timespec64 *tv)
1301 {
1302 	switch (runtime->tstamp_type) {
1303 	case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
1304 		ktime_get_ts64(tv);
1305 		break;
1306 	case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
1307 		ktime_get_raw_ts64(tv);
1308 		break;
1309 	default:
1310 		ktime_get_real_ts64(tv);
1311 		break;
1312 	}
1313 }
1314 
1315 /*
1316  *  Memory
1317  */
1318 
1319 void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream);
1320 void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm);
1321 void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
1322 				  int type, struct device *data,
1323 				  size_t size, size_t max);
1324 void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
1325 					  int type, void *data,
1326 					  size_t size, size_t max);
1327 int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
1328 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
1329 
1330 int snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
1331 			       struct device *data, size_t size, size_t max);
1332 int snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
1333 				   struct device *data,
1334 				   size_t size, size_t max);
1335 
1336 /**
1337  * snd_pcm_set_fixed_buffer - Preallocate and set up the fixed size PCM buffer
1338  * @substream: the pcm substream instance
1339  * @type: DMA type (SNDRV_DMA_TYPE_*)
1340  * @data: DMA type dependent data
1341  * @size: the requested pre-allocation size in bytes
1342  *
1343  * This is a variant of snd_pcm_set_managed_buffer(), but this pre-allocates
1344  * only the given sized buffer and doesn't allow re-allocation nor dynamic
1345  * allocation of a larger buffer unlike the standard one.
1346  * The function may return -ENOMEM error, hence the caller must check it.
1347  *
1348  * Return: zero if successful, or a negative error code
1349  */
1350 static inline int __must_check
snd_pcm_set_fixed_buffer(struct snd_pcm_substream * substream,int type,struct device * data,size_t size)1351 snd_pcm_set_fixed_buffer(struct snd_pcm_substream *substream, int type,
1352 				 struct device *data, size_t size)
1353 {
1354 	return snd_pcm_set_managed_buffer(substream, type, data, size, 0);
1355 }
1356 
1357 /**
1358  * snd_pcm_set_fixed_buffer_all - Preallocate and set up the fixed size PCM buffer
1359  * @pcm: the pcm instance
1360  * @type: DMA type (SNDRV_DMA_TYPE_*)
1361  * @data: DMA type dependent data
1362  * @size: the requested pre-allocation size in bytes
1363  *
1364  * Apply the set up of the fixed buffer via snd_pcm_set_fixed_buffer() for
1365  * all substream.  If any of allocation fails, it returns -ENOMEM, hence the
1366  * caller must check the return value.
1367  *
1368  * Return: zero if successful, or a negative error code
1369  */
1370 static inline int __must_check
snd_pcm_set_fixed_buffer_all(struct snd_pcm * pcm,int type,struct device * data,size_t size)1371 snd_pcm_set_fixed_buffer_all(struct snd_pcm *pcm, int type,
1372 			     struct device *data, size_t size)
1373 {
1374 	return snd_pcm_set_managed_buffer_all(pcm, type, data, size, 0);
1375 }
1376 
1377 #define snd_pcm_get_dma_buf(substream) ((substream)->runtime->dma_buffer_p)
1378 
1379 /**
1380  * snd_pcm_sgbuf_get_addr - Get the DMA address at the corresponding offset
1381  * @substream: PCM substream
1382  * @ofs: byte offset
1383  *
1384  * Return: DMA address
1385  */
1386 static inline dma_addr_t
snd_pcm_sgbuf_get_addr(struct snd_pcm_substream * substream,unsigned int ofs)1387 snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
1388 {
1389 	return snd_sgbuf_get_addr(snd_pcm_get_dma_buf(substream), ofs);
1390 }
1391 
1392 /**
1393  * snd_pcm_sgbuf_get_chunk_size - Compute the max size that fits within the
1394  * contig. page from the given size
1395  * @substream: PCM substream
1396  * @ofs: byte offset
1397  * @size: byte size to examine
1398  *
1399  * Return: chunk size
1400  */
1401 static inline unsigned int
snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream * substream,unsigned int ofs,unsigned int size)1402 snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
1403 			     unsigned int ofs, unsigned int size)
1404 {
1405 	return snd_sgbuf_get_chunk_size(snd_pcm_get_dma_buf(substream), ofs, size);
1406 }
1407 
1408 /**
1409  * snd_pcm_mmap_data_open - increase the mmap counter
1410  * @area: VMA
1411  *
1412  * PCM mmap callback should handle this counter properly
1413  */
snd_pcm_mmap_data_open(struct vm_area_struct * area)1414 static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)
1415 {
1416 	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
1417 	atomic_inc(&substream->mmap_count);
1418 }
1419 
1420 /**
1421  * snd_pcm_mmap_data_close - decrease the mmap counter
1422  * @area: VMA
1423  *
1424  * PCM mmap callback should handle this counter properly
1425  */
snd_pcm_mmap_data_close(struct vm_area_struct * area)1426 static inline void snd_pcm_mmap_data_close(struct vm_area_struct *area)
1427 {
1428 	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
1429 	atomic_dec(&substream->mmap_count);
1430 }
1431 
1432 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
1433 			     struct vm_area_struct *area);
1434 /* mmap for io-memory area */
1435 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
1436 #define SNDRV_PCM_INFO_MMAP_IOMEM	SNDRV_PCM_INFO_MMAP
1437 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, struct vm_area_struct *area);
1438 #else
1439 #define SNDRV_PCM_INFO_MMAP_IOMEM	0
1440 #define snd_pcm_lib_mmap_iomem	NULL
1441 #endif
1442 
1443 void snd_pcm_runtime_buffer_set_silence(struct snd_pcm_runtime *runtime);
1444 
1445 /**
1446  * snd_pcm_limit_isa_dma_size - Get the max size fitting with ISA DMA transfer
1447  * @dma: DMA number
1448  * @max: pointer to store the max size
1449  */
snd_pcm_limit_isa_dma_size(int dma,size_t * max)1450 static inline void snd_pcm_limit_isa_dma_size(int dma, size_t *max)
1451 {
1452 	*max = dma < 4 ? 64 * 1024 : 128 * 1024;
1453 }
1454 
1455 /*
1456  *  Misc
1457  */
1458 
1459 #define SNDRV_PCM_DEFAULT_CON_SPDIF	(IEC958_AES0_CON_EMPHASIS_NONE|\
1460 					 (IEC958_AES1_CON_ORIGINAL<<8)|\
1461 					 (IEC958_AES1_CON_PCM_CODER<<8)|\
1462 					 (IEC958_AES3_CON_FS_48000<<24))
1463 
1464 const char *snd_pcm_format_name(snd_pcm_format_t format);
1465 
1466 /**
1467  * snd_pcm_direction_name - Get a string naming the direction of a stream
1468  * @direction: Stream's direction, one of SNDRV_PCM_STREAM_XXX
1469  *
1470  * Returns a string naming the direction of the stream.
1471  */
snd_pcm_direction_name(int direction)1472 static inline const char *snd_pcm_direction_name(int direction)
1473 {
1474 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1475 		return "Playback";
1476 	else
1477 		return "Capture";
1478 }
1479 
1480 /**
1481  * snd_pcm_stream_str - Get a string naming the direction of a stream
1482  * @substream: the pcm substream instance
1483  *
1484  * Return: A string naming the direction of the stream.
1485  */
snd_pcm_stream_str(struct snd_pcm_substream * substream)1486 static inline const char *snd_pcm_stream_str(struct snd_pcm_substream *substream)
1487 {
1488 	return snd_pcm_direction_name(substream->stream);
1489 }
1490 
1491 /*
1492  * PCM channel-mapping control API
1493  */
1494 /* array element of channel maps */
1495 struct snd_pcm_chmap_elem {
1496 	unsigned char channels;
1497 	unsigned char map[15];
1498 };
1499 
1500 /* channel map information; retrieved via snd_kcontrol_chip() */
1501 struct snd_pcm_chmap {
1502 	struct snd_pcm *pcm;	/* assigned PCM instance */
1503 	int stream;		/* PLAYBACK or CAPTURE */
1504 	struct snd_kcontrol *kctl;
1505 	const struct snd_pcm_chmap_elem *chmap;
1506 	unsigned int max_channels;
1507 	unsigned int channel_mask;	/* optional: active channels bitmask */
1508 	void *private_data;	/* optional: private data pointer */
1509 };
1510 
1511 /**
1512  * snd_pcm_chmap_substream - get the PCM substream assigned to the given chmap info
1513  * @info: chmap information
1514  * @idx: the substream number index
1515  *
1516  * Return: the matched PCM substream, or NULL if not found
1517  */
1518 static inline struct snd_pcm_substream *
snd_pcm_chmap_substream(struct snd_pcm_chmap * info,unsigned int idx)1519 snd_pcm_chmap_substream(struct snd_pcm_chmap *info, unsigned int idx)
1520 {
1521 	struct snd_pcm_substream *s;
1522 	for (s = info->pcm->streams[info->stream].substream; s; s = s->next)
1523 		if (s->number == idx)
1524 			return s;
1525 	return NULL;
1526 }
1527 
1528 /* ALSA-standard channel maps (RL/RR prior to C/LFE) */
1529 extern const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[];
1530 /* Other world's standard channel maps (C/LFE prior to RL/RR) */
1531 extern const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[];
1532 
1533 /* bit masks to be passed to snd_pcm_chmap.channel_mask field */
1534 #define SND_PCM_CHMAP_MASK_24	((1U << 2) | (1U << 4))
1535 #define SND_PCM_CHMAP_MASK_246	(SND_PCM_CHMAP_MASK_24 | (1U << 6))
1536 #define SND_PCM_CHMAP_MASK_2468	(SND_PCM_CHMAP_MASK_246 | (1U << 8))
1537 
1538 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
1539 			   const struct snd_pcm_chmap_elem *chmap,
1540 			   int max_channels,
1541 			   unsigned long private_value,
1542 			   struct snd_pcm_chmap **info_ret);
1543 
1544 /**
1545  * pcm_format_to_bits - Strong-typed conversion of pcm_format to bitwise
1546  * @pcm_format: PCM format
1547  *
1548  * Return: 64bit mask corresponding to the given PCM format
1549  */
pcm_format_to_bits(snd_pcm_format_t pcm_format)1550 static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
1551 {
1552 	return 1ULL << (__force int) pcm_format;
1553 }
1554 
1555 /**
1556  * pcm_for_each_format - helper to iterate for each format type
1557  * @f: the iterator variable in snd_pcm_format_t type
1558  */
1559 #define pcm_for_each_format(f)						\
1560 	for ((f) = SNDRV_PCM_FORMAT_FIRST;				\
1561 	     (__force int)(f) <= (__force int)SNDRV_PCM_FORMAT_LAST;	\
1562 	     (f) = (__force snd_pcm_format_t)((__force int)(f) + 1))
1563 
1564 /* printk helpers */
1565 #define pcm_err(pcm, fmt, args...) \
1566 	dev_err((pcm)->card->dev, fmt, ##args)
1567 #define pcm_warn(pcm, fmt, args...) \
1568 	dev_warn((pcm)->card->dev, fmt, ##args)
1569 #define pcm_dbg(pcm, fmt, args...) \
1570 	dev_dbg((pcm)->card->dev, fmt, ##args)
1571 
1572 /* helpers for copying between iov_iter and iomem */
1573 int copy_to_iter_fromio(struct iov_iter *itert, const void __iomem *src,
1574 			size_t count);
1575 int copy_from_iter_toio(void __iomem *dst, struct iov_iter *iter, size_t count);
1576 
1577 struct snd_pcm_status64 {
1578 	snd_pcm_state_t state;		/* stream state */
1579 	u8 rsvd[4];
1580 	s64 trigger_tstamp_sec;		/* time when stream was started/stopped/paused */
1581 	s64 trigger_tstamp_nsec;
1582 	s64 tstamp_sec;			/* reference timestamp */
1583 	s64 tstamp_nsec;
1584 	snd_pcm_uframes_t appl_ptr;	/* appl ptr */
1585 	snd_pcm_uframes_t hw_ptr;	/* hw ptr */
1586 	snd_pcm_sframes_t delay;	/* current delay in frames */
1587 	snd_pcm_uframes_t avail;	/* number of frames available */
1588 	snd_pcm_uframes_t avail_max;	/* max frames available on hw since last status */
1589 	snd_pcm_uframes_t overrange;	/* count of ADC (capture) overrange detections from last status */
1590 	snd_pcm_state_t suspended_state; /* suspended stream state */
1591 	__u32 audio_tstamp_data;	 /* needed for 64-bit alignment, used for configs/report to/from userspace */
1592 	s64 audio_tstamp_sec;		/* sample counter, wall clock, PHC or on-demand sync'ed */
1593 	s64 audio_tstamp_nsec;
1594 	s64 driver_tstamp_sec;		/* useful in case reference system tstamp is reported with delay */
1595 	s64 driver_tstamp_nsec;
1596 	__u32 audio_tstamp_accuracy;	/* in ns units, only valid if indicated in audio_tstamp_data */
1597 	unsigned char reserved[52-4*sizeof(s64)]; /* must be filled with zero */
1598 };
1599 
1600 #define SNDRV_PCM_IOCTL_STATUS64	_IOR('A', 0x20, struct snd_pcm_status64)
1601 #define SNDRV_PCM_IOCTL_STATUS_EXT64	_IOWR('A', 0x24, struct snd_pcm_status64)
1602 
1603 struct snd_pcm_status32 {
1604 	snd_pcm_state_t state;		/* stream state */
1605 	s32 trigger_tstamp_sec;	/* time when stream was started/stopped/paused */
1606 	s32 trigger_tstamp_nsec;
1607 	s32 tstamp_sec;		/* reference timestamp */
1608 	s32 tstamp_nsec;
1609 	u32 appl_ptr;		/* appl ptr */
1610 	u32 hw_ptr;		/* hw ptr */
1611 	s32 delay;		/* current delay in frames */
1612 	u32 avail;		/* number of frames available */
1613 	u32 avail_max;		/* max frames available on hw since last status */
1614 	u32 overrange;		/* count of ADC (capture) overrange detections from last status */
1615 	snd_pcm_state_t suspended_state;	/* suspended stream state */
1616 	u32 audio_tstamp_data;	/* needed for 64-bit alignment, used for configs/report to/from userspace */
1617 	s32 audio_tstamp_sec;	/* sample counter, wall clock, PHC or on-demand sync'ed */
1618 	s32 audio_tstamp_nsec;
1619 	s32 driver_tstamp_sec;	/* useful in case reference system tstamp is reported with delay */
1620 	s32 driver_tstamp_nsec;
1621 	u32 audio_tstamp_accuracy;	/* in ns units, only valid if indicated in audio_tstamp_data */
1622 	unsigned char reserved[52-4*sizeof(s32)]; /* must be filled with zero */
1623 };
1624 
1625 #define SNDRV_PCM_IOCTL_STATUS32	_IOR('A', 0x20, struct snd_pcm_status32)
1626 #define SNDRV_PCM_IOCTL_STATUS_EXT32	_IOWR('A', 0x24, struct snd_pcm_status32)
1627 
1628 #endif /* __SOUND_PCM_H */
1629