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