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