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