]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Digital Audio (PCM) abstract layer | |
c1017a4c | 4 | * Copyright (c) by Jaroslav Kysela <[email protected]> |
1da177e4 LT |
5 | */ |
6 | ||
09d94175 | 7 | #include <linux/compat.h> |
1da177e4 | 8 | #include <linux/mm.h> |
da155d5b | 9 | #include <linux/module.h> |
1da177e4 LT |
10 | #include <linux/file.h> |
11 | #include <linux/slab.h> | |
174cd4b1 | 12 | #include <linux/sched/signal.h> |
1da177e4 | 13 | #include <linux/time.h> |
e8db0be1 | 14 | #include <linux/pm_qos.h> |
6cbbfe1c | 15 | #include <linux/io.h> |
657b1989 | 16 | #include <linux/dma-mapping.h> |
7e8edae3 | 17 | #include <linux/vmalloc.h> |
1da177e4 LT |
18 | #include <sound/core.h> |
19 | #include <sound/control.h> | |
20 | #include <sound/info.h> | |
21 | #include <sound/pcm.h> | |
22 | #include <sound/pcm_params.h> | |
23 | #include <sound/timer.h> | |
24 | #include <sound/minors.h> | |
e2e40f2c | 25 | #include <linux/uio.h> |
b888a5f7 | 26 | #include <linux/delay.h> |
1da177e4 | 27 | |
2c4842d3 TS |
28 | #include "pcm_local.h" |
29 | ||
37567c55 | 30 | #ifdef CONFIG_SND_DEBUG |
be4e31da TS |
31 | #define CREATE_TRACE_POINTS |
32 | #include "pcm_param_trace.h" | |
37567c55 TS |
33 | #else |
34 | #define trace_hw_mask_param_enabled() 0 | |
35 | #define trace_hw_interval_param_enabled() 0 | |
36 | #define trace_hw_mask_param(substream, type, index, prev, curr) | |
37 | #define trace_hw_interval_param(substream, type, index, prev, curr) | |
38 | #endif | |
be4e31da | 39 | |
1da177e4 LT |
40 | /* |
41 | * Compatibility | |
42 | */ | |
43 | ||
877211f5 | 44 | struct snd_pcm_hw_params_old { |
1da177e4 LT |
45 | unsigned int flags; |
46 | unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT - | |
47 | SNDRV_PCM_HW_PARAM_ACCESS + 1]; | |
877211f5 | 48 | struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME - |
1da177e4 LT |
49 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1]; |
50 | unsigned int rmask; | |
51 | unsigned int cmask; | |
52 | unsigned int info; | |
53 | unsigned int msbits; | |
54 | unsigned int rate_num; | |
55 | unsigned int rate_den; | |
877211f5 | 56 | snd_pcm_uframes_t fifo_size; |
1da177e4 LT |
57 | unsigned char reserved[64]; |
58 | }; | |
59 | ||
59d48582 | 60 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
877211f5 TI |
61 | #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old) |
62 | #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old) | |
1da177e4 | 63 | |
877211f5 TI |
64 | static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, |
65 | struct snd_pcm_hw_params_old __user * _oparams); | |
66 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, | |
67 | struct snd_pcm_hw_params_old __user * _oparams); | |
59d48582 | 68 | #endif |
f87135f5 | 69 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream); |
1da177e4 LT |
70 | |
71 | /* | |
72 | * | |
73 | */ | |
74 | ||
7af142f7 | 75 | static DECLARE_RWSEM(snd_pcm_link_rwsem); |
1da177e4 | 76 | |
73365cb1 | 77 | void snd_pcm_group_init(struct snd_pcm_group *group) |
67ec1072 | 78 | { |
73365cb1 TI |
79 | spin_lock_init(&group->lock); |
80 | mutex_init(&group->mutex); | |
81 | INIT_LIST_HEAD(&group->substreams); | |
0e279dce | 82 | refcount_set(&group->refs, 1); |
67ec1072 TI |
83 | } |
84 | ||
f57f3df0 TI |
85 | /* define group lock helpers */ |
86 | #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \ | |
87 | static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \ | |
88 | { \ | |
89 | if (nonatomic) \ | |
90 | mutex_ ## mutex_action(&group->mutex); \ | |
91 | else \ | |
92 | spin_ ## action(&group->lock); \ | |
10aa7cad AMG |
93 | } |
94 | ||
f57f3df0 TI |
95 | DEFINE_PCM_GROUP_LOCK(lock, lock); |
96 | DEFINE_PCM_GROUP_LOCK(unlock, unlock); | |
97 | DEFINE_PCM_GROUP_LOCK(lock_irq, lock); | |
98 | DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock); | |
10aa7cad | 99 | |
30b771cf TI |
100 | /** |
101 | * snd_pcm_stream_lock - Lock the PCM stream | |
102 | * @substream: PCM substream | |
103 | * | |
104 | * This locks the PCM stream's spinlock or mutex depending on the nonatomic | |
105 | * flag of the given substream. This also takes the global link rw lock | |
106 | * (or rw sem), too, for avoiding the race with linked streams. | |
107 | */ | |
7af142f7 TI |
108 | void snd_pcm_stream_lock(struct snd_pcm_substream *substream) |
109 | { | |
ef2056b8 | 110 | snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic); |
7af142f7 TI |
111 | } |
112 | EXPORT_SYMBOL_GPL(snd_pcm_stream_lock); | |
113 | ||
30b771cf | 114 | /** |
f7b6603c | 115 | * snd_pcm_stream_unlock - Unlock the PCM stream |
30b771cf TI |
116 | * @substream: PCM substream |
117 | * | |
118 | * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock(). | |
119 | */ | |
7af142f7 TI |
120 | void snd_pcm_stream_unlock(struct snd_pcm_substream *substream) |
121 | { | |
ef2056b8 | 122 | snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic); |
7af142f7 TI |
123 | } |
124 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock); | |
125 | ||
30b771cf TI |
126 | /** |
127 | * snd_pcm_stream_lock_irq - Lock the PCM stream | |
128 | * @substream: PCM substream | |
129 | * | |
130 | * This locks the PCM stream like snd_pcm_stream_lock() and disables the local | |
131 | * IRQ (only when nonatomic is false). In nonatomic case, this is identical | |
132 | * as snd_pcm_stream_lock(). | |
133 | */ | |
7af142f7 TI |
134 | void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream) |
135 | { | |
ef2056b8 TI |
136 | snd_pcm_group_lock_irq(&substream->self_group, |
137 | substream->pcm->nonatomic); | |
7af142f7 TI |
138 | } |
139 | EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq); | |
140 | ||
e18035cf MM |
141 | static void snd_pcm_stream_lock_nested(struct snd_pcm_substream *substream) |
142 | { | |
143 | struct snd_pcm_group *group = &substream->self_group; | |
144 | ||
145 | if (substream->pcm->nonatomic) | |
146 | mutex_lock_nested(&group->mutex, SINGLE_DEPTH_NESTING); | |
147 | else | |
148 | spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING); | |
149 | } | |
150 | ||
30b771cf TI |
151 | /** |
152 | * snd_pcm_stream_unlock_irq - Unlock the PCM stream | |
153 | * @substream: PCM substream | |
154 | * | |
155 | * This is a counter-part of snd_pcm_stream_lock_irq(). | |
156 | */ | |
7af142f7 TI |
157 | void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream) |
158 | { | |
ef2056b8 TI |
159 | snd_pcm_group_unlock_irq(&substream->self_group, |
160 | substream->pcm->nonatomic); | |
7af142f7 TI |
161 | } |
162 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq); | |
163 | ||
164 | unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream) | |
165 | { | |
ef2056b8 TI |
166 | unsigned long flags = 0; |
167 | if (substream->pcm->nonatomic) | |
168 | mutex_lock(&substream->self_group.mutex); | |
169 | else | |
170 | spin_lock_irqsave(&substream->self_group.lock, flags); | |
171 | return flags; | |
7af142f7 TI |
172 | } |
173 | EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave); | |
174 | ||
3c75c0ea TI |
175 | unsigned long _snd_pcm_stream_lock_irqsave_nested(struct snd_pcm_substream *substream) |
176 | { | |
177 | unsigned long flags = 0; | |
178 | if (substream->pcm->nonatomic) | |
179 | mutex_lock_nested(&substream->self_group.mutex, | |
180 | SINGLE_DEPTH_NESTING); | |
181 | else | |
182 | spin_lock_irqsave_nested(&substream->self_group.lock, flags, | |
183 | SINGLE_DEPTH_NESTING); | |
184 | return flags; | |
185 | } | |
186 | EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave_nested); | |
187 | ||
30b771cf TI |
188 | /** |
189 | * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream | |
190 | * @substream: PCM substream | |
191 | * @flags: irq flags | |
192 | * | |
193 | * This is a counter-part of snd_pcm_stream_lock_irqsave(). | |
194 | */ | |
7af142f7 TI |
195 | void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream, |
196 | unsigned long flags) | |
197 | { | |
ef2056b8 TI |
198 | if (substream->pcm->nonatomic) |
199 | mutex_unlock(&substream->self_group.mutex); | |
200 | else | |
201 | spin_unlock_irqrestore(&substream->self_group.lock, flags); | |
7af142f7 TI |
202 | } |
203 | EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore); | |
1da177e4 | 204 | |
fc033cbf TI |
205 | /* Run PCM ioctl ops */ |
206 | static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream, | |
207 | unsigned cmd, void *arg) | |
208 | { | |
209 | if (substream->ops->ioctl) | |
210 | return substream->ops->ioctl(substream, cmd, arg); | |
211 | else | |
212 | return snd_pcm_lib_ioctl(substream, cmd, arg); | |
213 | } | |
214 | ||
877211f5 | 215 | int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info) |
1da177e4 | 216 | { |
877211f5 TI |
217 | struct snd_pcm *pcm = substream->pcm; |
218 | struct snd_pcm_str *pstr = substream->pstr; | |
1da177e4 | 219 | |
1da177e4 LT |
220 | memset(info, 0, sizeof(*info)); |
221 | info->card = pcm->card->number; | |
222 | info->device = pcm->device; | |
223 | info->stream = substream->stream; | |
224 | info->subdevice = substream->number; | |
75b1a8f9 JP |
225 | strscpy(info->id, pcm->id, sizeof(info->id)); |
226 | strscpy(info->name, pcm->name, sizeof(info->name)); | |
1da177e4 LT |
227 | info->dev_class = pcm->dev_class; |
228 | info->dev_subclass = pcm->dev_subclass; | |
229 | info->subdevices_count = pstr->substream_count; | |
230 | info->subdevices_avail = pstr->substream_count - pstr->substream_opened; | |
75b1a8f9 | 231 | strscpy(info->subname, substream->name, sizeof(info->subname)); |
e11f0f90 | 232 | |
1da177e4 LT |
233 | return 0; |
234 | } | |
235 | ||
877211f5 TI |
236 | int snd_pcm_info_user(struct snd_pcm_substream *substream, |
237 | struct snd_pcm_info __user * _info) | |
1da177e4 | 238 | { |
ae921398 | 239 | struct snd_pcm_info *info __free(kfree) = NULL; |
1da177e4 LT |
240 | int err; |
241 | ||
242 | info = kmalloc(sizeof(*info), GFP_KERNEL); | |
243 | if (! info) | |
244 | return -ENOMEM; | |
245 | err = snd_pcm_info(substream, info); | |
246 | if (err >= 0) { | |
247 | if (copy_to_user(_info, info, sizeof(*info))) | |
248 | err = -EFAULT; | |
249 | } | |
1da177e4 LT |
250 | return err; |
251 | } | |
252 | ||
f9b0c053 TI |
253 | /* macro for simplified cast */ |
254 | #define PARAM_MASK_BIT(b) (1U << (__force int)(b)) | |
255 | ||
63825f3a TI |
256 | static bool hw_support_mmap(struct snd_pcm_substream *substream) |
257 | { | |
cbea6e5a TI |
258 | struct snd_dma_buffer *dmabuf; |
259 | ||
63825f3a TI |
260 | if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP)) |
261 | return false; | |
425da159 | 262 | |
852a8a97 | 263 | if (substream->ops->mmap || substream->ops->page) |
425da159 CH |
264 | return true; |
265 | ||
cbea6e5a TI |
266 | dmabuf = snd_pcm_get_dma_buf(substream); |
267 | if (!dmabuf) | |
268 | dmabuf = &substream->dma_buffer; | |
269 | switch (dmabuf->dev.type) { | |
c4824ae7 | 270 | case SNDRV_DMA_TYPE_UNKNOWN: |
dc0dc8a7 TI |
271 | /* we can't know the device, so just assume that the driver does |
272 | * everything right | |
273 | */ | |
274 | return true; | |
c4824ae7 TI |
275 | case SNDRV_DMA_TYPE_CONTINUOUS: |
276 | case SNDRV_DMA_TYPE_VMALLOC: | |
277 | return true; | |
278 | default: | |
cbea6e5a | 279 | return dma_can_mmap(dmabuf->dev.dev); |
c4824ae7 | 280 | } |
63825f3a TI |
281 | } |
282 | ||
561e1cad TS |
283 | static int constrain_mask_params(struct snd_pcm_substream *substream, |
284 | struct snd_pcm_hw_params *params) | |
1da177e4 | 285 | { |
561e1cad TS |
286 | struct snd_pcm_hw_constraints *constrs = |
287 | &substream->runtime->hw_constraints; | |
288 | struct snd_mask *m; | |
1da177e4 | 289 | unsigned int k; |
3827597a | 290 | struct snd_mask old_mask __maybe_unused; |
561e1cad | 291 | int changed; |
1da177e4 LT |
292 | |
293 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { | |
294 | m = hw_param_mask(params, k); | |
295 | if (snd_mask_empty(m)) | |
296 | return -EINVAL; | |
d81052f9 TS |
297 | |
298 | /* This parameter is not requested to change by a caller. */ | |
f9b0c053 | 299 | if (!(params->rmask & PARAM_MASK_BIT(k))) |
1da177e4 | 300 | continue; |
561e1cad TS |
301 | |
302 | if (trace_hw_mask_param_enabled()) | |
303 | old_mask = *m; | |
304 | ||
1da177e4 | 305 | changed = snd_mask_refine(m, constrs_mask(constrs, k)); |
1da177e4 LT |
306 | if (changed < 0) |
307 | return changed; | |
82e7d501 TS |
308 | if (changed == 0) |
309 | continue; | |
561e1cad | 310 | |
d81052f9 | 311 | /* Set corresponding flag so that the caller gets it. */ |
82e7d501 | 312 | trace_hw_mask_param(substream, k, 0, &old_mask, m); |
f9b0c053 | 313 | params->cmask |= PARAM_MASK_BIT(k); |
1da177e4 LT |
314 | } |
315 | ||
561e1cad TS |
316 | return 0; |
317 | } | |
318 | ||
3432fa04 TS |
319 | static int constrain_interval_params(struct snd_pcm_substream *substream, |
320 | struct snd_pcm_hw_params *params) | |
321 | { | |
322 | struct snd_pcm_hw_constraints *constrs = | |
323 | &substream->runtime->hw_constraints; | |
324 | struct snd_interval *i; | |
325 | unsigned int k; | |
3827597a | 326 | struct snd_interval old_interval __maybe_unused; |
3432fa04 TS |
327 | int changed; |
328 | ||
1da177e4 LT |
329 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { |
330 | i = hw_param_interval(params, k); | |
331 | if (snd_interval_empty(i)) | |
332 | return -EINVAL; | |
d81052f9 TS |
333 | |
334 | /* This parameter is not requested to change by a caller. */ | |
f9b0c053 | 335 | if (!(params->rmask & PARAM_MASK_BIT(k))) |
1da177e4 | 336 | continue; |
3432fa04 TS |
337 | |
338 | if (trace_hw_interval_param_enabled()) | |
339 | old_interval = *i; | |
340 | ||
1da177e4 | 341 | changed = snd_interval_refine(i, constrs_interval(constrs, k)); |
1da177e4 LT |
342 | if (changed < 0) |
343 | return changed; | |
82e7d501 TS |
344 | if (changed == 0) |
345 | continue; | |
3432fa04 | 346 | |
d81052f9 | 347 | /* Set corresponding flag so that the caller gets it. */ |
82e7d501 | 348 | trace_hw_interval_param(substream, k, 0, &old_interval, i); |
f9b0c053 | 349 | params->cmask |= PARAM_MASK_BIT(k); |
1da177e4 LT |
350 | } |
351 | ||
3432fa04 TS |
352 | return 0; |
353 | } | |
354 | ||
9cc07f55 TS |
355 | static int constrain_params_by_rules(struct snd_pcm_substream *substream, |
356 | struct snd_pcm_hw_params *params) | |
1da177e4 | 357 | { |
9cc07f55 TS |
358 | struct snd_pcm_hw_constraints *constrs = |
359 | &substream->runtime->hw_constraints; | |
1da177e4 | 360 | unsigned int k; |
ae921398 | 361 | unsigned int *rstamps __free(kfree) = NULL; |
1da177e4 | 362 | unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1]; |
d81052f9 | 363 | unsigned int stamp; |
a1c06e39 TS |
364 | struct snd_pcm_hw_rule *r; |
365 | unsigned int d; | |
3827597a KM |
366 | struct snd_mask old_mask __maybe_unused; |
367 | struct snd_interval old_interval __maybe_unused; | |
a1c06e39 | 368 | bool again; |
5730f9f7 | 369 | int changed, err = 0; |
1da177e4 | 370 | |
d81052f9 TS |
371 | /* |
372 | * Each application of rule has own sequence number. | |
373 | * | |
374 | * Each member of 'rstamps' array represents the sequence number of | |
375 | * recent application of corresponding rule. | |
376 | */ | |
5730f9f7 TI |
377 | rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL); |
378 | if (!rstamps) | |
379 | return -ENOMEM; | |
d81052f9 TS |
380 | |
381 | /* | |
382 | * Each member of 'vstamps' array represents the sequence number of | |
383 | * recent application of rule in which corresponding parameters were | |
384 | * changed. | |
385 | * | |
386 | * In initial state, elements corresponding to parameters requested by | |
387 | * a caller is 1. For unrequested parameters, corresponding members | |
388 | * have 0 so that the parameters are never changed anymore. | |
389 | */ | |
9cc07f55 | 390 | for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) |
f9b0c053 | 391 | vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0; |
d81052f9 TS |
392 | |
393 | /* Due to the above design, actual sequence number starts at 2. */ | |
394 | stamp = 2; | |
0d4e3999 | 395 | retry: |
d81052f9 | 396 | /* Apply all rules in order. */ |
a1c06e39 | 397 | again = false; |
0d4e3999 | 398 | for (k = 0; k < constrs->rules_num; k++) { |
a1c06e39 | 399 | r = &constrs->rules[k]; |
d81052f9 TS |
400 | |
401 | /* | |
402 | * Check condition bits of this rule. When the rule has | |
403 | * some condition bits, parameter without the bits is | |
404 | * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP | |
405 | * is an example of the condition bits. | |
406 | */ | |
0d4e3999 TS |
407 | if (r->cond && !(r->cond & params->flags)) |
408 | continue; | |
d81052f9 TS |
409 | |
410 | /* | |
23b53d44 TI |
411 | * The 'deps' array includes maximum four dependencies |
412 | * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth | |
d81052f9 TS |
413 | * member of this array is a sentinel and should be |
414 | * negative value. | |
415 | * | |
416 | * This rule should be processed in this time when dependent | |
417 | * parameters were changed at former applications of the other | |
418 | * rules. | |
419 | */ | |
0d4e3999 | 420 | for (d = 0; r->deps[d] >= 0; d++) { |
d656b4a6 | 421 | if (vstamps[r->deps[d]] > rstamps[k]) |
0d4e3999 | 422 | break; |
0d4e3999 | 423 | } |
d656b4a6 | 424 | if (r->deps[d] < 0) |
0d4e3999 | 425 | continue; |
be4e31da | 426 | |
0d4e3999 TS |
427 | if (trace_hw_mask_param_enabled()) { |
428 | if (hw_is_mask(r->var)) | |
429 | old_mask = *hw_param_mask(params, r->var); | |
430 | } | |
431 | if (trace_hw_interval_param_enabled()) { | |
432 | if (hw_is_interval(r->var)) | |
433 | old_interval = *hw_param_interval(params, r->var); | |
434 | } | |
c6706de0 | 435 | |
0d4e3999 | 436 | changed = r->func(params, r); |
ae921398 TI |
437 | if (changed < 0) |
438 | return changed; | |
c6706de0 | 439 | |
d81052f9 | 440 | /* |
82e7d501 | 441 | * When the parameter is changed, notify it to the caller |
d81052f9 TS |
442 | * by corresponding returned bit, then preparing for next |
443 | * iteration. | |
444 | */ | |
0d4e3999 | 445 | if (changed && r->var >= 0) { |
82e7d501 TS |
446 | if (hw_is_mask(r->var)) { |
447 | trace_hw_mask_param(substream, r->var, | |
448 | k + 1, &old_mask, | |
449 | hw_param_mask(params, r->var)); | |
1da177e4 | 450 | } |
82e7d501 TS |
451 | if (hw_is_interval(r->var)) { |
452 | trace_hw_interval_param(substream, r->var, | |
453 | k + 1, &old_interval, | |
454 | hw_param_interval(params, r->var)); | |
1da177e4 | 455 | } |
82e7d501 | 456 | |
f9b0c053 | 457 | params->cmask |= PARAM_MASK_BIT(r->var); |
0d4e3999 | 458 | vstamps[r->var] = stamp; |
a1c06e39 | 459 | again = true; |
1da177e4 | 460 | } |
f74ae15f | 461 | |
82e7d501 | 462 | rstamps[k] = stamp++; |
0d4e3999 TS |
463 | } |
464 | ||
d81052f9 | 465 | /* Iterate to evaluate all rules till no parameters are changed. */ |
0d4e3999 TS |
466 | if (again) |
467 | goto retry; | |
9cc07f55 | 468 | |
5730f9f7 | 469 | return err; |
9cc07f55 TS |
470 | } |
471 | ||
f9a076bf TS |
472 | static int fixup_unreferenced_params(struct snd_pcm_substream *substream, |
473 | struct snd_pcm_hw_params *params) | |
474 | { | |
475 | const struct snd_interval *i; | |
476 | const struct snd_mask *m; | |
2112aa03 | 477 | struct snd_mask *m_rw; |
f9a076bf TS |
478 | int err; |
479 | ||
1da177e4 | 480 | if (!params->msbits) { |
f9a076bf | 481 | i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); |
1da177e4 LT |
482 | if (snd_interval_single(i)) |
483 | params->msbits = snd_interval_value(i); | |
85df6b5a JK |
484 | m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT); |
485 | if (snd_mask_single(m)) { | |
486 | snd_pcm_format_t format = (__force snd_pcm_format_t)snd_mask_min(m); | |
487 | params->msbits = snd_pcm_format_width(format); | |
488 | } | |
1da177e4 LT |
489 | } |
490 | ||
2112aa03 JK |
491 | if (params->msbits) { |
492 | m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
493 | if (snd_mask_single(m)) { | |
494 | snd_pcm_format_t format = (__force snd_pcm_format_t)snd_mask_min(m); | |
495 | ||
496 | if (snd_pcm_format_linear(format) && | |
497 | snd_pcm_format_width(format) != params->msbits) { | |
498 | m_rw = hw_param_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT); | |
499 | snd_mask_reset(m_rw, | |
500 | (__force unsigned)SNDRV_PCM_SUBFORMAT_MSBITS_MAX); | |
501 | if (snd_mask_empty(m_rw)) | |
502 | return -EINVAL; | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
1da177e4 | 507 | if (!params->rate_den) { |
f9a076bf | 508 | i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); |
1da177e4 LT |
509 | if (snd_interval_single(i)) { |
510 | params->rate_num = snd_interval_value(i); | |
511 | params->rate_den = 1; | |
512 | } | |
513 | } | |
514 | ||
f9a076bf TS |
515 | if (!params->fifo_size) { |
516 | m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
517 | i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
518 | if (snd_mask_single(m) && snd_interval_single(i)) { | |
fc033cbf TI |
519 | err = snd_pcm_ops_ioctl(substream, |
520 | SNDRV_PCM_IOCTL1_FIFO_SIZE, | |
521 | params); | |
f9a076bf TS |
522 | if (err < 0) |
523 | return err; | |
524 | } | |
525 | } | |
526 | ||
63825f3a | 527 | if (!params->info) { |
7802fb52 TS |
528 | params->info = substream->runtime->hw.info; |
529 | params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES | | |
530 | SNDRV_PCM_INFO_DRAIN_TRIGGER); | |
63825f3a TI |
531 | if (!hw_support_mmap(substream)) |
532 | params->info &= ~(SNDRV_PCM_INFO_MMAP | | |
533 | SNDRV_PCM_INFO_MMAP_VALID); | |
534 | } | |
7802fb52 | 535 | |
f05c1ffc JK |
536 | err = snd_pcm_ops_ioctl(substream, |
537 | SNDRV_PCM_IOCTL1_SYNC_ID, | |
538 | params); | |
539 | if (err < 0) | |
540 | return err; | |
541 | ||
f9a076bf TS |
542 | return 0; |
543 | } | |
544 | ||
9cc07f55 TS |
545 | int snd_pcm_hw_refine(struct snd_pcm_substream *substream, |
546 | struct snd_pcm_hw_params *params) | |
547 | { | |
9cc07f55 TS |
548 | int err; |
549 | ||
550 | params->info = 0; | |
551 | params->fifo_size = 0; | |
f9b0c053 | 552 | if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS)) |
9cc07f55 | 553 | params->msbits = 0; |
f9b0c053 | 554 | if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) { |
9cc07f55 TS |
555 | params->rate_num = 0; |
556 | params->rate_den = 0; | |
8bea869c | 557 | } |
9cc07f55 TS |
558 | |
559 | err = constrain_mask_params(substream, params); | |
560 | if (err < 0) | |
561 | return err; | |
562 | ||
563 | err = constrain_interval_params(substream, params); | |
564 | if (err < 0) | |
565 | return err; | |
566 | ||
567 | err = constrain_params_by_rules(substream, params); | |
568 | if (err < 0) | |
569 | return err; | |
570 | ||
1da177e4 | 571 | params->rmask = 0; |
7802fb52 | 572 | |
1da177e4 LT |
573 | return 0; |
574 | } | |
e88e8ae6 TI |
575 | EXPORT_SYMBOL(snd_pcm_hw_refine); |
576 | ||
877211f5 TI |
577 | static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream, |
578 | struct snd_pcm_hw_params __user * _params) | |
1da177e4 | 579 | { |
ae921398 | 580 | struct snd_pcm_hw_params *params __free(kfree) = NULL; |
1da177e4 LT |
581 | int err; |
582 | ||
ef44a1ec LZ |
583 | params = memdup_user(_params, sizeof(*params)); |
584 | if (IS_ERR(params)) | |
40a024b8 | 585 | return PTR_ERR(params); |
ef44a1ec | 586 | |
1da177e4 | 587 | err = snd_pcm_hw_refine(substream, params); |
f74ae15f | 588 | if (err < 0) |
ae921398 | 589 | return err; |
ef44a1ec | 590 | |
f74ae15f TS |
591 | err = fixup_unreferenced_params(substream, params); |
592 | if (err < 0) | |
ae921398 | 593 | return err; |
ef44a1ec | 594 | |
f74ae15f | 595 | if (copy_to_user(_params, params, sizeof(*params))) |
ae921398 TI |
596 | return -EFAULT; |
597 | return 0; | |
1da177e4 LT |
598 | } |
599 | ||
9442e691 TI |
600 | static int period_to_usecs(struct snd_pcm_runtime *runtime) |
601 | { | |
602 | int usecs; | |
603 | ||
604 | if (! runtime->rate) | |
605 | return -1; /* invalid */ | |
606 | ||
607 | /* take 75% of period time as the deadline */ | |
608 | usecs = (750000 / runtime->rate) * runtime->period_size; | |
609 | usecs += ((750000 % runtime->rate) * runtime->period_size) / | |
610 | runtime->rate; | |
611 | ||
612 | return usecs; | |
613 | } | |
614 | ||
cb639a42 TI |
615 | static void snd_pcm_set_state(struct snd_pcm_substream *substream, |
616 | snd_pcm_state_t state) | |
9b0573c0 | 617 | { |
650224fe | 618 | guard(pcm_stream_lock_irq)(substream); |
f0061c18 TI |
619 | if (substream->runtime->state != SNDRV_PCM_STATE_DISCONNECTED) |
620 | __snd_pcm_set_state(substream->runtime, state); | |
9b0573c0 TI |
621 | } |
622 | ||
90bbaf66 JY |
623 | static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream, |
624 | int event) | |
625 | { | |
626 | #ifdef CONFIG_SND_PCM_TIMER | |
627 | if (substream->timer) | |
628 | snd_timer_notify(substream->timer, event, | |
629 | &substream->runtime->trigger_tstamp); | |
630 | #endif | |
631 | } | |
632 | ||
29bb274e | 633 | void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq) |
1e850bee | 634 | { |
29bb274e | 635 | if (substream->runtime && substream->runtime->stop_operating) { |
1e850bee | 636 | substream->runtime->stop_operating = false; |
29bb274e | 637 | if (substream->ops && substream->ops->sync_stop) |
1e850bee | 638 | substream->ops->sync_stop(substream); |
29bb274e | 639 | else if (sync_irq && substream->pcm->card->sync_irq > 0) |
fabb26dc | 640 | synchronize_irq(substream->pcm->card->sync_irq); |
1e850bee TI |
641 | } |
642 | } | |
643 | ||
60f96aae | 644 | /** |
f7b6603c | 645 | * snd_pcm_hw_params_choose - choose a configuration defined by @params |
60f96aae TS |
646 | * @pcm: PCM instance |
647 | * @params: the hw_params instance | |
648 | * | |
649 | * Choose one configuration from configuration space defined by @params. | |
650 | * The configuration chosen is that obtained fixing in this order: | |
651 | * first access, first format, first subformat, min channels, | |
652 | * min rate, min period time, max buffer size, min tick time | |
653 | * | |
654 | * Return: Zero if successful, or a negative error code on failure. | |
655 | */ | |
656 | static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, | |
657 | struct snd_pcm_hw_params *params) | |
658 | { | |
659 | static const int vars[] = { | |
660 | SNDRV_PCM_HW_PARAM_ACCESS, | |
661 | SNDRV_PCM_HW_PARAM_FORMAT, | |
662 | SNDRV_PCM_HW_PARAM_SUBFORMAT, | |
663 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
664 | SNDRV_PCM_HW_PARAM_RATE, | |
665 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
666 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
667 | SNDRV_PCM_HW_PARAM_TICK_TIME, | |
668 | -1 | |
669 | }; | |
670 | const int *v; | |
3827597a KM |
671 | struct snd_mask old_mask __maybe_unused; |
672 | struct snd_interval old_interval __maybe_unused; | |
82e7d501 | 673 | int changed; |
60f96aae TS |
674 | |
675 | for (v = vars; *v != -1; v++) { | |
7b8a54af TS |
676 | /* Keep old parameter to trace. */ |
677 | if (trace_hw_mask_param_enabled()) { | |
678 | if (hw_is_mask(*v)) | |
679 | old_mask = *hw_param_mask(params, *v); | |
680 | } | |
681 | if (trace_hw_interval_param_enabled()) { | |
682 | if (hw_is_interval(*v)) | |
683 | old_interval = *hw_param_interval(params, *v); | |
684 | } | |
60f96aae | 685 | if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE) |
82e7d501 | 686 | changed = snd_pcm_hw_param_first(pcm, params, *v, NULL); |
60f96aae | 687 | else |
82e7d501 | 688 | changed = snd_pcm_hw_param_last(pcm, params, *v, NULL); |
e1a3a981 | 689 | if (changed < 0) |
82e7d501 TS |
690 | return changed; |
691 | if (changed == 0) | |
692 | continue; | |
7b8a54af | 693 | |
82e7d501 | 694 | /* Trace the changed parameter. */ |
7b8a54af TS |
695 | if (hw_is_mask(*v)) { |
696 | trace_hw_mask_param(pcm, *v, 0, &old_mask, | |
697 | hw_param_mask(params, *v)); | |
698 | } | |
699 | if (hw_is_interval(*v)) { | |
700 | trace_hw_interval_param(pcm, *v, 0, &old_interval, | |
701 | hw_param_interval(params, *v)); | |
702 | } | |
60f96aae | 703 | } |
7b8a54af | 704 | |
60f96aae TS |
705 | return 0; |
706 | } | |
707 | ||
bc55cfd5 TI |
708 | /* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise |
709 | * block the further r/w operations | |
710 | */ | |
711 | static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime) | |
712 | { | |
713 | if (!atomic_dec_unless_positive(&runtime->buffer_accessing)) | |
714 | return -EBUSY; | |
715 | mutex_lock(&runtime->buffer_mutex); | |
716 | return 0; /* keep buffer_mutex, unlocked by below */ | |
717 | } | |
718 | ||
719 | /* release buffer_mutex and clear r/w access flag */ | |
720 | static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime) | |
721 | { | |
722 | mutex_unlock(&runtime->buffer_mutex); | |
723 | atomic_inc(&runtime->buffer_accessing); | |
724 | } | |
725 | ||
92ee3c60 TI |
726 | #if IS_ENABLED(CONFIG_SND_PCM_OSS) |
727 | #define is_oss_stream(substream) ((substream)->oss.oss) | |
728 | #else | |
729 | #define is_oss_stream(substream) false | |
730 | #endif | |
731 | ||
877211f5 TI |
732 | static int snd_pcm_hw_params(struct snd_pcm_substream *substream, |
733 | struct snd_pcm_hw_params *params) | |
1da177e4 | 734 | { |
877211f5 | 735 | struct snd_pcm_runtime *runtime; |
bc55cfd5 | 736 | int err, usecs; |
1da177e4 LT |
737 | unsigned int bits; |
738 | snd_pcm_uframes_t frames; | |
739 | ||
7eaa943c TI |
740 | if (PCM_RUNTIME_CHECK(substream)) |
741 | return -ENXIO; | |
1da177e4 | 742 | runtime = substream->runtime; |
bc55cfd5 TI |
743 | err = snd_pcm_buffer_access_lock(runtime); |
744 | if (err < 0) | |
745 | return err; | |
650224fe TI |
746 | scoped_guard(pcm_stream_lock_irq, substream) { |
747 | switch (runtime->state) { | |
748 | case SNDRV_PCM_STATE_OPEN: | |
749 | case SNDRV_PCM_STATE_SETUP: | |
750 | case SNDRV_PCM_STATE_PREPARED: | |
751 | if (!is_oss_stream(substream) && | |
752 | atomic_read(&substream->mmap_count)) | |
753 | err = -EBADFD; | |
754 | break; | |
755 | default: | |
92ee3c60 | 756 | err = -EBADFD; |
650224fe TI |
757 | break; |
758 | } | |
1da177e4 | 759 | } |
92ee3c60 TI |
760 | if (err) |
761 | goto unlock; | |
1da177e4 | 762 | |
29bb274e | 763 | snd_pcm_sync_stop(substream, true); |
1e850bee | 764 | |
1da177e4 LT |
765 | params->rmask = ~0U; |
766 | err = snd_pcm_hw_refine(substream, params); | |
767 | if (err < 0) | |
768 | goto _error; | |
769 | ||
770 | err = snd_pcm_hw_params_choose(substream, params); | |
771 | if (err < 0) | |
772 | goto _error; | |
773 | ||
f9a076bf TS |
774 | err = fixup_unreferenced_params(substream, params); |
775 | if (err < 0) | |
776 | goto _error; | |
777 | ||
0dba808e TI |
778 | if (substream->managed_buffer_alloc) { |
779 | err = snd_pcm_lib_malloc_pages(substream, | |
780 | params_buffer_bytes(params)); | |
781 | if (err < 0) | |
782 | goto _error; | |
783 | runtime->buffer_changed = err > 0; | |
784 | } | |
785 | ||
1da177e4 LT |
786 | if (substream->ops->hw_params != NULL) { |
787 | err = substream->ops->hw_params(substream, params); | |
788 | if (err < 0) | |
789 | goto _error; | |
790 | } | |
791 | ||
792 | runtime->access = params_access(params); | |
793 | runtime->format = params_format(params); | |
794 | runtime->subformat = params_subformat(params); | |
795 | runtime->channels = params_channels(params); | |
796 | runtime->rate = params_rate(params); | |
797 | runtime->period_size = params_period_size(params); | |
798 | runtime->periods = params_periods(params); | |
799 | runtime->buffer_size = params_buffer_size(params); | |
1da177e4 LT |
800 | runtime->info = params->info; |
801 | runtime->rate_num = params->rate_num; | |
802 | runtime->rate_den = params->rate_den; | |
ab69a490 CL |
803 | runtime->no_period_wakeup = |
804 | (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) && | |
805 | (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP); | |
1da177e4 LT |
806 | |
807 | bits = snd_pcm_format_physical_width(runtime->format); | |
808 | runtime->sample_bits = bits; | |
809 | bits *= runtime->channels; | |
810 | runtime->frame_bits = bits; | |
811 | frames = 1; | |
812 | while (bits % 8 != 0) { | |
813 | bits *= 2; | |
814 | frames *= 2; | |
815 | } | |
816 | runtime->byte_align = bits / 8; | |
817 | runtime->min_align = frames; | |
818 | ||
819 | /* Default sw params */ | |
820 | runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; | |
821 | runtime->period_step = 1; | |
1da177e4 | 822 | runtime->control->avail_min = runtime->period_size; |
1da177e4 LT |
823 | runtime->start_threshold = 1; |
824 | runtime->stop_threshold = runtime->buffer_size; | |
825 | runtime->silence_threshold = 0; | |
826 | runtime->silence_size = 0; | |
ead4046b CL |
827 | runtime->boundary = runtime->buffer_size; |
828 | while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size) | |
829 | runtime->boundary *= 2; | |
1da177e4 | 830 | |
add9d56d | 831 | /* clear the buffer for avoiding possible kernel info leaks */ |
6c0217b1 | 832 | if (runtime->dma_area && !substream->ops->copy) { |
618de0f4 TI |
833 | size_t size = runtime->dma_bytes; |
834 | ||
835 | if (runtime->info & SNDRV_PCM_INFO_MMAP) | |
836 | size = PAGE_ALIGN(size); | |
837 | memset(runtime->dma_area, 0, size); | |
838 | } | |
add9d56d | 839 | |
1da177e4 | 840 | snd_pcm_timer_resolution_change(substream); |
9b0573c0 | 841 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP); |
9442e691 | 842 | |
5371a79b RW |
843 | if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req)) |
844 | cpu_latency_qos_remove_request(&substream->latency_pm_qos_req); | |
137c171c TI |
845 | usecs = period_to_usecs(runtime); |
846 | if (usecs >= 0) | |
5371a79b RW |
847 | cpu_latency_qos_add_request(&substream->latency_pm_qos_req, |
848 | usecs); | |
92ee3c60 | 849 | err = 0; |
1da177e4 | 850 | _error: |
92ee3c60 TI |
851 | if (err) { |
852 | /* hardware might be unusable from this time, | |
853 | * so we force application to retry to set | |
854 | * the correct hardware parameter settings | |
855 | */ | |
856 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); | |
857 | if (substream->ops->hw_free != NULL) | |
858 | substream->ops->hw_free(substream); | |
859 | if (substream->managed_buffer_alloc) | |
860 | snd_pcm_lib_free_pages(substream); | |
861 | } | |
862 | unlock: | |
bc55cfd5 | 863 | snd_pcm_buffer_access_unlock(runtime); |
1da177e4 LT |
864 | return err; |
865 | } | |
866 | ||
877211f5 TI |
867 | static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream, |
868 | struct snd_pcm_hw_params __user * _params) | |
1da177e4 | 869 | { |
ae921398 | 870 | struct snd_pcm_hw_params *params __free(kfree) = NULL; |
1da177e4 LT |
871 | int err; |
872 | ||
ef44a1ec LZ |
873 | params = memdup_user(_params, sizeof(*params)); |
874 | if (IS_ERR(params)) | |
40a024b8 | 875 | return PTR_ERR(params); |
ef44a1ec | 876 | |
1da177e4 | 877 | err = snd_pcm_hw_params(substream, params); |
f74ae15f | 878 | if (err < 0) |
ae921398 | 879 | return err; |
ef44a1ec | 880 | |
f74ae15f | 881 | if (copy_to_user(_params, params, sizeof(*params))) |
ae921398 | 882 | return -EFAULT; |
1da177e4 LT |
883 | return err; |
884 | } | |
885 | ||
66f2d19f TI |
886 | static int do_hw_free(struct snd_pcm_substream *substream) |
887 | { | |
888 | int result = 0; | |
889 | ||
29bb274e | 890 | snd_pcm_sync_stop(substream, true); |
66f2d19f TI |
891 | if (substream->ops->hw_free) |
892 | result = substream->ops->hw_free(substream); | |
893 | if (substream->managed_buffer_alloc) | |
894 | snd_pcm_lib_free_pages(substream); | |
895 | return result; | |
896 | } | |
897 | ||
877211f5 | 898 | static int snd_pcm_hw_free(struct snd_pcm_substream *substream) |
1da177e4 | 899 | { |
877211f5 | 900 | struct snd_pcm_runtime *runtime; |
92ee3c60 | 901 | int result = 0; |
1da177e4 | 902 | |
7eaa943c TI |
903 | if (PCM_RUNTIME_CHECK(substream)) |
904 | return -ENXIO; | |
1da177e4 | 905 | runtime = substream->runtime; |
bc55cfd5 TI |
906 | result = snd_pcm_buffer_access_lock(runtime); |
907 | if (result < 0) | |
908 | return result; | |
650224fe TI |
909 | scoped_guard(pcm_stream_lock_irq, substream) { |
910 | switch (runtime->state) { | |
911 | case SNDRV_PCM_STATE_SETUP: | |
912 | case SNDRV_PCM_STATE_PREPARED: | |
913 | if (atomic_read(&substream->mmap_count)) | |
914 | result = -EBADFD; | |
915 | break; | |
916 | default: | |
92ee3c60 | 917 | result = -EBADFD; |
650224fe TI |
918 | break; |
919 | } | |
1da177e4 | 920 | } |
92ee3c60 TI |
921 | if (result) |
922 | goto unlock; | |
66f2d19f | 923 | result = do_hw_free(substream); |
9b0573c0 | 924 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); |
5371a79b | 925 | cpu_latency_qos_remove_request(&substream->latency_pm_qos_req); |
92ee3c60 | 926 | unlock: |
bc55cfd5 | 927 | snd_pcm_buffer_access_unlock(runtime); |
1da177e4 LT |
928 | return result; |
929 | } | |
930 | ||
877211f5 TI |
931 | static int snd_pcm_sw_params(struct snd_pcm_substream *substream, |
932 | struct snd_pcm_sw_params *params) | |
1da177e4 | 933 | { |
877211f5 | 934 | struct snd_pcm_runtime *runtime; |
1250932e | 935 | int err; |
1da177e4 | 936 | |
7eaa943c TI |
937 | if (PCM_RUNTIME_CHECK(substream)) |
938 | return -ENXIO; | |
1da177e4 | 939 | runtime = substream->runtime; |
650224fe TI |
940 | scoped_guard(pcm_stream_lock_irq, substream) { |
941 | if (runtime->state == SNDRV_PCM_STATE_OPEN) | |
942 | return -EBADFD; | |
1da177e4 | 943 | } |
1da177e4 | 944 | |
145d92e7 DC |
945 | if (params->tstamp_mode < 0 || |
946 | params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST) | |
1da177e4 | 947 | return -EINVAL; |
58900810 TI |
948 | if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) && |
949 | params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST) | |
5646eda5 | 950 | return -EINVAL; |
1da177e4 LT |
951 | if (params->avail_min == 0) |
952 | return -EINVAL; | |
1da177e4 LT |
953 | if (params->silence_size >= runtime->boundary) { |
954 | if (params->silence_threshold != 0) | |
955 | return -EINVAL; | |
956 | } else { | |
957 | if (params->silence_size > params->silence_threshold) | |
958 | return -EINVAL; | |
959 | if (params->silence_threshold > runtime->buffer_size) | |
960 | return -EINVAL; | |
961 | } | |
1250932e | 962 | err = 0; |
650224fe TI |
963 | scoped_guard(pcm_stream_lock_irq, substream) { |
964 | runtime->tstamp_mode = params->tstamp_mode; | |
965 | if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12)) | |
966 | runtime->tstamp_type = params->tstamp_type; | |
967 | runtime->period_step = params->period_step; | |
968 | runtime->control->avail_min = params->avail_min; | |
969 | runtime->start_threshold = params->start_threshold; | |
970 | runtime->stop_threshold = params->stop_threshold; | |
971 | runtime->silence_threshold = params->silence_threshold; | |
972 | runtime->silence_size = params->silence_size; | |
973 | params->boundary = runtime->boundary; | |
974 | if (snd_pcm_running(substream)) { | |
975 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
976 | runtime->silence_size > 0) | |
977 | snd_pcm_playback_silence(substream, ULONG_MAX); | |
978 | err = snd_pcm_update_state(substream, runtime); | |
979 | } | |
1da177e4 | 980 | } |
1250932e | 981 | return err; |
1da177e4 LT |
982 | } |
983 | ||
877211f5 TI |
984 | static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream, |
985 | struct snd_pcm_sw_params __user * _params) | |
1da177e4 | 986 | { |
877211f5 | 987 | struct snd_pcm_sw_params params; |
1da177e4 LT |
988 | int err; |
989 | if (copy_from_user(¶ms, _params, sizeof(params))) | |
990 | return -EFAULT; | |
991 | err = snd_pcm_sw_params(substream, ¶ms); | |
992 | if (copy_to_user(_params, ¶ms, sizeof(params))) | |
993 | return -EFAULT; | |
994 | return err; | |
995 | } | |
996 | ||
c99c5a3b TI |
997 | static inline snd_pcm_uframes_t |
998 | snd_pcm_calc_delay(struct snd_pcm_substream *substream) | |
999 | { | |
1000 | snd_pcm_uframes_t delay; | |
1001 | ||
1002 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
1003 | delay = snd_pcm_playback_hw_avail(substream->runtime); | |
1004 | else | |
1005 | delay = snd_pcm_capture_avail(substream->runtime); | |
1006 | return delay + substream->runtime->delay; | |
1007 | } | |
1008 | ||
3ddee7f8 BW |
1009 | int snd_pcm_status64(struct snd_pcm_substream *substream, |
1010 | struct snd_pcm_status64 *status) | |
1da177e4 | 1011 | { |
877211f5 | 1012 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1013 | |
650224fe | 1014 | guard(pcm_stream_lock_irq)(substream); |
3179f620 PLB |
1015 | |
1016 | snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data, | |
1017 | &runtime->audio_tstamp_config); | |
1018 | ||
1019 | /* backwards compatible behavior */ | |
1020 | if (runtime->audio_tstamp_config.type_requested == | |
1021 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) { | |
1022 | if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK) | |
1023 | runtime->audio_tstamp_config.type_requested = | |
1024 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK; | |
1025 | else | |
1026 | runtime->audio_tstamp_config.type_requested = | |
1027 | SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT; | |
1028 | runtime->audio_tstamp_report.valid = 0; | |
1029 | } else | |
1030 | runtime->audio_tstamp_report.valid = 1; | |
1031 | ||
f0061c18 TI |
1032 | status->state = runtime->state; |
1033 | status->suspended_state = runtime->suspended_state; | |
1da177e4 | 1034 | if (status->state == SNDRV_PCM_STATE_OPEN) |
650224fe | 1035 | return 0; |
3ddee7f8 BW |
1036 | status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec; |
1037 | status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec; | |
8c121586 | 1038 | if (snd_pcm_running(substream)) { |
1da177e4 | 1039 | snd_pcm_update_hw_ptr(substream); |
8c121586 | 1040 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
3ddee7f8 BW |
1041 | status->tstamp_sec = runtime->status->tstamp.tv_sec; |
1042 | status->tstamp_nsec = | |
1043 | runtime->status->tstamp.tv_nsec; | |
1044 | status->driver_tstamp_sec = | |
1045 | runtime->driver_tstamp.tv_sec; | |
1046 | status->driver_tstamp_nsec = | |
1047 | runtime->driver_tstamp.tv_nsec; | |
1048 | status->audio_tstamp_sec = | |
1049 | runtime->status->audio_tstamp.tv_sec; | |
1050 | status->audio_tstamp_nsec = | |
1051 | runtime->status->audio_tstamp.tv_nsec; | |
3179f620 PLB |
1052 | if (runtime->audio_tstamp_report.valid == 1) |
1053 | /* backwards compatibility, no report provided in COMPAT mode */ | |
1054 | snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data, | |
1055 | &status->audio_tstamp_accuracy, | |
1056 | &runtime->audio_tstamp_report); | |
1057 | ||
8c121586 JK |
1058 | goto _tstamp_end; |
1059 | } | |
0d59b814 PLB |
1060 | } else { |
1061 | /* get tstamp only in fallback mode and only if enabled */ | |
fcae40c9 BW |
1062 | if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { |
1063 | struct timespec64 tstamp; | |
1064 | ||
1065 | snd_pcm_gettime(runtime, &tstamp); | |
3ddee7f8 BW |
1066 | status->tstamp_sec = tstamp.tv_sec; |
1067 | status->tstamp_nsec = tstamp.tv_nsec; | |
fcae40c9 | 1068 | } |
8c121586 | 1069 | } |
8c121586 | 1070 | _tstamp_end: |
1da177e4 LT |
1071 | status->appl_ptr = runtime->control->appl_ptr; |
1072 | status->hw_ptr = runtime->status->hw_ptr; | |
763e5067 | 1073 | status->avail = snd_pcm_avail(substream); |
c99c5a3b TI |
1074 | status->delay = snd_pcm_running(substream) ? |
1075 | snd_pcm_calc_delay(substream) : 0; | |
1da177e4 LT |
1076 | status->avail_max = runtime->avail_max; |
1077 | status->overrange = runtime->overrange; | |
1078 | runtime->avail_max = 0; | |
1079 | runtime->overrange = 0; | |
1da177e4 LT |
1080 | return 0; |
1081 | } | |
1082 | ||
3ddee7f8 BW |
1083 | static int snd_pcm_status_user64(struct snd_pcm_substream *substream, |
1084 | struct snd_pcm_status64 __user * _status, | |
1085 | bool ext) | |
1da177e4 | 1086 | { |
3ddee7f8 | 1087 | struct snd_pcm_status64 status; |
1da177e4 | 1088 | int res; |
38ca2a4d | 1089 | |
1da177e4 | 1090 | memset(&status, 0, sizeof(status)); |
38ca2a4d PLB |
1091 | /* |
1092 | * with extension, parameters are read/write, | |
1093 | * get audio_tstamp_data from user, | |
1094 | * ignore rest of status structure | |
1095 | */ | |
1096 | if (ext && get_user(status.audio_tstamp_data, | |
1097 | (u32 __user *)(&_status->audio_tstamp_data))) | |
1098 | return -EFAULT; | |
3ddee7f8 | 1099 | res = snd_pcm_status64(substream, &status); |
1da177e4 LT |
1100 | if (res < 0) |
1101 | return res; | |
1102 | if (copy_to_user(_status, &status, sizeof(status))) | |
1103 | return -EFAULT; | |
1104 | return 0; | |
1105 | } | |
1106 | ||
3ddee7f8 BW |
1107 | static int snd_pcm_status_user32(struct snd_pcm_substream *substream, |
1108 | struct snd_pcm_status32 __user * _status, | |
1109 | bool ext) | |
1110 | { | |
1111 | struct snd_pcm_status64 status64; | |
1112 | struct snd_pcm_status32 status32; | |
1113 | int res; | |
1114 | ||
1115 | memset(&status64, 0, sizeof(status64)); | |
1116 | memset(&status32, 0, sizeof(status32)); | |
1117 | /* | |
1118 | * with extension, parameters are read/write, | |
1119 | * get audio_tstamp_data from user, | |
1120 | * ignore rest of status structure | |
1121 | */ | |
1122 | if (ext && get_user(status64.audio_tstamp_data, | |
1123 | (u32 __user *)(&_status->audio_tstamp_data))) | |
1124 | return -EFAULT; | |
1125 | res = snd_pcm_status64(substream, &status64); | |
1126 | if (res < 0) | |
1127 | return res; | |
1128 | ||
1129 | status32 = (struct snd_pcm_status32) { | |
1130 | .state = status64.state, | |
1131 | .trigger_tstamp_sec = status64.trigger_tstamp_sec, | |
1132 | .trigger_tstamp_nsec = status64.trigger_tstamp_nsec, | |
1133 | .tstamp_sec = status64.tstamp_sec, | |
1134 | .tstamp_nsec = status64.tstamp_nsec, | |
1135 | .appl_ptr = status64.appl_ptr, | |
1136 | .hw_ptr = status64.hw_ptr, | |
1137 | .delay = status64.delay, | |
1138 | .avail = status64.avail, | |
1139 | .avail_max = status64.avail_max, | |
1140 | .overrange = status64.overrange, | |
1141 | .suspended_state = status64.suspended_state, | |
1142 | .audio_tstamp_data = status64.audio_tstamp_data, | |
1143 | .audio_tstamp_sec = status64.audio_tstamp_sec, | |
1144 | .audio_tstamp_nsec = status64.audio_tstamp_nsec, | |
1145 | .driver_tstamp_sec = status64.audio_tstamp_sec, | |
1146 | .driver_tstamp_nsec = status64.audio_tstamp_nsec, | |
1147 | .audio_tstamp_accuracy = status64.audio_tstamp_accuracy, | |
1148 | }; | |
1149 | ||
1150 | if (copy_to_user(_status, &status32, sizeof(status32))) | |
1151 | return -EFAULT; | |
1152 | ||
1153 | return 0; | |
1154 | } | |
1155 | ||
877211f5 TI |
1156 | static int snd_pcm_channel_info(struct snd_pcm_substream *substream, |
1157 | struct snd_pcm_channel_info * info) | |
1da177e4 | 1158 | { |
877211f5 | 1159 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
1160 | unsigned int channel; |
1161 | ||
1da177e4 LT |
1162 | channel = info->channel; |
1163 | runtime = substream->runtime; | |
650224fe TI |
1164 | scoped_guard(pcm_stream_lock_irq, substream) { |
1165 | if (runtime->state == SNDRV_PCM_STATE_OPEN) | |
1166 | return -EBADFD; | |
1da177e4 | 1167 | } |
1da177e4 LT |
1168 | if (channel >= runtime->channels) |
1169 | return -EINVAL; | |
1170 | memset(info, 0, sizeof(*info)); | |
1171 | info->channel = channel; | |
fc033cbf | 1172 | return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info); |
1da177e4 LT |
1173 | } |
1174 | ||
877211f5 TI |
1175 | static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream, |
1176 | struct snd_pcm_channel_info __user * _info) | |
1da177e4 | 1177 | { |
877211f5 | 1178 | struct snd_pcm_channel_info info; |
1da177e4 LT |
1179 | int res; |
1180 | ||
1181 | if (copy_from_user(&info, _info, sizeof(info))) | |
1182 | return -EFAULT; | |
1183 | res = snd_pcm_channel_info(substream, &info); | |
1184 | if (res < 0) | |
1185 | return res; | |
1186 | if (copy_to_user(_info, &info, sizeof(info))) | |
1187 | return -EFAULT; | |
1188 | return 0; | |
1189 | } | |
1190 | ||
877211f5 | 1191 | static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream) |
1da177e4 | 1192 | { |
877211f5 | 1193 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1194 | if (runtime->trigger_master == NULL) |
1195 | return; | |
1196 | if (runtime->trigger_master == substream) { | |
2b79d7a6 PLB |
1197 | if (!runtime->trigger_tstamp_latched) |
1198 | snd_pcm_gettime(runtime, &runtime->trigger_tstamp); | |
1da177e4 LT |
1199 | } else { |
1200 | snd_pcm_trigger_tstamp(runtime->trigger_master); | |
1201 | runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp; | |
1202 | } | |
1203 | runtime->trigger_master = NULL; | |
1204 | } | |
1205 | ||
cb639a42 TI |
1206 | #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0 |
1207 | ||
1da177e4 | 1208 | struct action_ops { |
cb639a42 TI |
1209 | int (*pre_action)(struct snd_pcm_substream *substream, |
1210 | snd_pcm_state_t state); | |
1211 | int (*do_action)(struct snd_pcm_substream *substream, | |
1212 | snd_pcm_state_t state); | |
1213 | void (*undo_action)(struct snd_pcm_substream *substream, | |
1214 | snd_pcm_state_t state); | |
1215 | void (*post_action)(struct snd_pcm_substream *substream, | |
1216 | snd_pcm_state_t state); | |
1da177e4 LT |
1217 | }; |
1218 | ||
1219 | /* | |
1220 | * this functions is core for handling of linked stream | |
1221 | * Note: the stream state might be changed also on failure | |
1222 | * Note2: call with calling stream lock + link lock | |
1223 | */ | |
b17154cf | 1224 | static int snd_pcm_action_group(const struct action_ops *ops, |
877211f5 | 1225 | struct snd_pcm_substream *substream, |
cb639a42 | 1226 | snd_pcm_state_t state, |
3c3201f8 | 1227 | bool stream_lock) |
1da177e4 | 1228 | { |
877211f5 TI |
1229 | struct snd_pcm_substream *s = NULL; |
1230 | struct snd_pcm_substream *s1; | |
dde1c652 | 1231 | int res = 0, depth = 1; |
1da177e4 | 1232 | |
ef991b95 | 1233 | snd_pcm_group_for_each_entry(s, substream) { |
3c3201f8 TI |
1234 | if (s != substream) { |
1235 | if (!stream_lock) | |
1236 | mutex_lock_nested(&s->runtime->buffer_mutex, depth); | |
1237 | else if (s->pcm->nonatomic) | |
dde1c652 | 1238 | mutex_lock_nested(&s->self_group.mutex, depth); |
257f8cce | 1239 | else |
dde1c652 TI |
1240 | spin_lock_nested(&s->self_group.lock, depth); |
1241 | depth++; | |
257f8cce | 1242 | } |
1da177e4 LT |
1243 | res = ops->pre_action(s, state); |
1244 | if (res < 0) | |
1245 | goto _unlock; | |
1246 | } | |
ef991b95 | 1247 | snd_pcm_group_for_each_entry(s, substream) { |
1da177e4 LT |
1248 | res = ops->do_action(s, state); |
1249 | if (res < 0) { | |
1250 | if (ops->undo_action) { | |
ef991b95 | 1251 | snd_pcm_group_for_each_entry(s1, substream) { |
1da177e4 LT |
1252 | if (s1 == s) /* failed stream */ |
1253 | break; | |
1254 | ops->undo_action(s1, state); | |
1255 | } | |
1256 | } | |
1257 | s = NULL; /* unlock all */ | |
1258 | goto _unlock; | |
1259 | } | |
1260 | } | |
ef991b95 | 1261 | snd_pcm_group_for_each_entry(s, substream) { |
1da177e4 LT |
1262 | ops->post_action(s, state); |
1263 | } | |
1264 | _unlock: | |
3c3201f8 TI |
1265 | /* unlock streams */ |
1266 | snd_pcm_group_for_each_entry(s1, substream) { | |
1267 | if (s1 != substream) { | |
1268 | if (!stream_lock) | |
1269 | mutex_unlock(&s1->runtime->buffer_mutex); | |
1270 | else if (s1->pcm->nonatomic) | |
1271 | mutex_unlock(&s1->self_group.mutex); | |
1272 | else | |
1273 | spin_unlock(&s1->self_group.lock); | |
1da177e4 | 1274 | } |
3c3201f8 TI |
1275 | if (s1 == s) /* end */ |
1276 | break; | |
1da177e4 LT |
1277 | } |
1278 | return res; | |
1279 | } | |
1280 | ||
1281 | /* | |
1282 | * Note: call with stream lock | |
1283 | */ | |
b17154cf | 1284 | static int snd_pcm_action_single(const struct action_ops *ops, |
877211f5 | 1285 | struct snd_pcm_substream *substream, |
cb639a42 | 1286 | snd_pcm_state_t state) |
1da177e4 LT |
1287 | { |
1288 | int res; | |
1289 | ||
1290 | res = ops->pre_action(substream, state); | |
1291 | if (res < 0) | |
1292 | return res; | |
1293 | res = ops->do_action(substream, state); | |
1294 | if (res == 0) | |
1295 | ops->post_action(substream, state); | |
1296 | else if (ops->undo_action) | |
1297 | ops->undo_action(substream, state); | |
1298 | return res; | |
1299 | } | |
1300 | ||
a41c4cb9 TI |
1301 | static void snd_pcm_group_assign(struct snd_pcm_substream *substream, |
1302 | struct snd_pcm_group *new_group) | |
1303 | { | |
1304 | substream->group = new_group; | |
1305 | list_move(&substream->link_list, &new_group->substreams); | |
1306 | } | |
1307 | ||
f57f3df0 TI |
1308 | /* |
1309 | * Unref and unlock the group, but keep the stream lock; | |
1310 | * when the group becomes empty and no longer referred, destroy itself | |
1311 | */ | |
1312 | static void snd_pcm_group_unref(struct snd_pcm_group *group, | |
1313 | struct snd_pcm_substream *substream) | |
1314 | { | |
1315 | bool do_free; | |
1316 | ||
1317 | if (!group) | |
1318 | return; | |
0e279dce | 1319 | do_free = refcount_dec_and_test(&group->refs); |
f57f3df0 TI |
1320 | snd_pcm_group_unlock(group, substream->pcm->nonatomic); |
1321 | if (do_free) | |
1322 | kfree(group); | |
1323 | } | |
1324 | ||
1325 | /* | |
1326 | * Lock the group inside a stream lock and reference it; | |
1327 | * return the locked group object, or NULL if not linked | |
1328 | */ | |
1329 | static struct snd_pcm_group * | |
1330 | snd_pcm_stream_group_ref(struct snd_pcm_substream *substream) | |
1331 | { | |
1332 | bool nonatomic = substream->pcm->nonatomic; | |
1333 | struct snd_pcm_group *group; | |
1334 | bool trylock; | |
1335 | ||
1336 | for (;;) { | |
1337 | if (!snd_pcm_stream_linked(substream)) | |
1338 | return NULL; | |
1339 | group = substream->group; | |
1340 | /* block freeing the group object */ | |
1341 | refcount_inc(&group->refs); | |
1342 | ||
1343 | trylock = nonatomic ? mutex_trylock(&group->mutex) : | |
1344 | spin_trylock(&group->lock); | |
1345 | if (trylock) | |
1346 | break; /* OK */ | |
1347 | ||
1348 | /* re-lock for avoiding ABBA deadlock */ | |
1349 | snd_pcm_stream_unlock(substream); | |
1350 | snd_pcm_group_lock(group, nonatomic); | |
1351 | snd_pcm_stream_lock(substream); | |
1352 | ||
1353 | /* check the group again; the above opens a small race window */ | |
1354 | if (substream->group == group) | |
1355 | break; /* OK */ | |
1356 | /* group changed, try again */ | |
1357 | snd_pcm_group_unref(group, substream); | |
1358 | } | |
1359 | return group; | |
1360 | } | |
1361 | ||
aa8edd8c TI |
1362 | /* |
1363 | * Note: call with stream lock | |
1364 | */ | |
b17154cf | 1365 | static int snd_pcm_action(const struct action_ops *ops, |
aa8edd8c | 1366 | struct snd_pcm_substream *substream, |
cb639a42 | 1367 | snd_pcm_state_t state) |
257f8cce | 1368 | { |
f57f3df0 | 1369 | struct snd_pcm_group *group; |
257f8cce TI |
1370 | int res; |
1371 | ||
f57f3df0 TI |
1372 | group = snd_pcm_stream_group_ref(substream); |
1373 | if (group) | |
cb639a42 | 1374 | res = snd_pcm_action_group(ops, substream, state, true); |
f57f3df0 TI |
1375 | else |
1376 | res = snd_pcm_action_single(ops, substream, state); | |
1377 | snd_pcm_group_unref(group, substream); | |
1da177e4 LT |
1378 | return res; |
1379 | } | |
1380 | ||
1381 | /* | |
1382 | * Note: don't use any locks before | |
1383 | */ | |
b17154cf | 1384 | static int snd_pcm_action_lock_irq(const struct action_ops *ops, |
877211f5 | 1385 | struct snd_pcm_substream *substream, |
cb639a42 | 1386 | snd_pcm_state_t state) |
1da177e4 | 1387 | { |
650224fe TI |
1388 | guard(pcm_stream_lock_irq)(substream); |
1389 | return snd_pcm_action(ops, substream, state); | |
1da177e4 LT |
1390 | } |
1391 | ||
1392 | /* | |
1393 | */ | |
b17154cf | 1394 | static int snd_pcm_action_nonatomic(const struct action_ops *ops, |
877211f5 | 1395 | struct snd_pcm_substream *substream, |
cb639a42 | 1396 | snd_pcm_state_t state) |
1da177e4 LT |
1397 | { |
1398 | int res; | |
1399 | ||
f57f3df0 | 1400 | /* Guarantee the group members won't change during non-atomic action */ |
dd0da75b | 1401 | guard(rwsem_read)(&snd_pcm_link_rwsem); |
bc55cfd5 TI |
1402 | res = snd_pcm_buffer_access_lock(substream->runtime); |
1403 | if (res < 0) | |
dd0da75b | 1404 | return res; |
1da177e4 | 1405 | if (snd_pcm_stream_linked(substream)) |
cb639a42 | 1406 | res = snd_pcm_action_group(ops, substream, state, false); |
1da177e4 LT |
1407 | else |
1408 | res = snd_pcm_action_single(ops, substream, state); | |
bc55cfd5 | 1409 | snd_pcm_buffer_access_unlock(substream->runtime); |
1da177e4 LT |
1410 | return res; |
1411 | } | |
1412 | ||
1413 | /* | |
1414 | * start callbacks | |
1415 | */ | |
cb639a42 TI |
1416 | static int snd_pcm_pre_start(struct snd_pcm_substream *substream, |
1417 | snd_pcm_state_t state) | |
1da177e4 | 1418 | { |
877211f5 | 1419 | struct snd_pcm_runtime *runtime = substream->runtime; |
f0061c18 | 1420 | if (runtime->state != SNDRV_PCM_STATE_PREPARED) |
1da177e4 LT |
1421 | return -EBADFD; |
1422 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
1423 | !snd_pcm_playback_data(substream)) | |
1424 | return -EPIPE; | |
2b79d7a6 | 1425 | runtime->trigger_tstamp_latched = false; |
1da177e4 LT |
1426 | runtime->trigger_master = substream; |
1427 | return 0; | |
1428 | } | |
1429 | ||
cb639a42 TI |
1430 | static int snd_pcm_do_start(struct snd_pcm_substream *substream, |
1431 | snd_pcm_state_t state) | |
1da177e4 | 1432 | { |
e661c488 TI |
1433 | int err; |
1434 | ||
1da177e4 LT |
1435 | if (substream->runtime->trigger_master != substream) |
1436 | return 0; | |
e661c488 TI |
1437 | err = substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START); |
1438 | /* XRUN happened during the start */ | |
1439 | if (err == -EPIPE) | |
1440 | __snd_pcm_set_state(substream->runtime, SNDRV_PCM_STATE_XRUN); | |
1441 | return err; | |
1da177e4 LT |
1442 | } |
1443 | ||
cb639a42 TI |
1444 | static void snd_pcm_undo_start(struct snd_pcm_substream *substream, |
1445 | snd_pcm_state_t state) | |
1da177e4 | 1446 | { |
5c8cc93b | 1447 | if (substream->runtime->trigger_master == substream) { |
1da177e4 | 1448 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
5c8cc93b TI |
1449 | substream->runtime->stop_operating = true; |
1450 | } | |
1da177e4 LT |
1451 | } |
1452 | ||
cb639a42 TI |
1453 | static void snd_pcm_post_start(struct snd_pcm_substream *substream, |
1454 | snd_pcm_state_t state) | |
1da177e4 | 1455 | { |
877211f5 | 1456 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1457 | snd_pcm_trigger_tstamp(substream); |
6af3fb72 | 1458 | runtime->hw_ptr_jiffies = jiffies; |
bd76af0f JK |
1459 | runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) / |
1460 | runtime->rate; | |
f0061c18 | 1461 | __snd_pcm_set_state(runtime, state); |
1da177e4 LT |
1462 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && |
1463 | runtime->silence_size > 0) | |
d7f5dd97 | 1464 | snd_pcm_playback_silence(substream, ULONG_MAX); |
90bbaf66 | 1465 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); |
1da177e4 LT |
1466 | } |
1467 | ||
b17154cf | 1468 | static const struct action_ops snd_pcm_action_start = { |
1da177e4 LT |
1469 | .pre_action = snd_pcm_pre_start, |
1470 | .do_action = snd_pcm_do_start, | |
1471 | .undo_action = snd_pcm_undo_start, | |
1472 | .post_action = snd_pcm_post_start | |
1473 | }; | |
1474 | ||
1475 | /** | |
1c85cc64 | 1476 | * snd_pcm_start - start all linked streams |
df8db936 | 1477 | * @substream: the PCM substream instance |
eb7c06e8 YB |
1478 | * |
1479 | * Return: Zero if successful, or a negative error code. | |
c2c86a97 | 1480 | * The stream lock must be acquired before calling this function. |
1da177e4 | 1481 | */ |
877211f5 | 1482 | int snd_pcm_start(struct snd_pcm_substream *substream) |
1da177e4 | 1483 | { |
877211f5 TI |
1484 | return snd_pcm_action(&snd_pcm_action_start, substream, |
1485 | SNDRV_PCM_STATE_RUNNING); | |
1da177e4 LT |
1486 | } |
1487 | ||
c2c86a97 TI |
1488 | /* take the stream lock and start the streams */ |
1489 | static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream) | |
1490 | { | |
1491 | return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, | |
1492 | SNDRV_PCM_STATE_RUNNING); | |
1493 | } | |
1494 | ||
1da177e4 LT |
1495 | /* |
1496 | * stop callbacks | |
1497 | */ | |
cb639a42 TI |
1498 | static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, |
1499 | snd_pcm_state_t state) | |
1da177e4 | 1500 | { |
877211f5 | 1501 | struct snd_pcm_runtime *runtime = substream->runtime; |
f0061c18 | 1502 | if (runtime->state == SNDRV_PCM_STATE_OPEN) |
1da177e4 LT |
1503 | return -EBADFD; |
1504 | runtime->trigger_master = substream; | |
1505 | return 0; | |
1506 | } | |
1507 | ||
cb639a42 TI |
1508 | static int snd_pcm_do_stop(struct snd_pcm_substream *substream, |
1509 | snd_pcm_state_t state) | |
1da177e4 LT |
1510 | { |
1511 | if (substream->runtime->trigger_master == substream && | |
700cb707 | 1512 | snd_pcm_running(substream)) { |
1da177e4 | 1513 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP); |
700cb707 TI |
1514 | substream->runtime->stop_operating = true; |
1515 | } | |
583770e8 | 1516 | return 0; /* unconditionally stop all substreams */ |
1da177e4 LT |
1517 | } |
1518 | ||
cb639a42 TI |
1519 | static void snd_pcm_post_stop(struct snd_pcm_substream *substream, |
1520 | snd_pcm_state_t state) | |
1da177e4 | 1521 | { |
877211f5 | 1522 | struct snd_pcm_runtime *runtime = substream->runtime; |
f0061c18 | 1523 | if (runtime->state != state) { |
1da177e4 | 1524 | snd_pcm_trigger_tstamp(substream); |
f0061c18 | 1525 | __snd_pcm_set_state(runtime, state); |
90bbaf66 | 1526 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP); |
1da177e4 LT |
1527 | } |
1528 | wake_up(&runtime->sleep); | |
c91a988d | 1529 | wake_up(&runtime->tsleep); |
1da177e4 LT |
1530 | } |
1531 | ||
b17154cf | 1532 | static const struct action_ops snd_pcm_action_stop = { |
1da177e4 LT |
1533 | .pre_action = snd_pcm_pre_stop, |
1534 | .do_action = snd_pcm_do_stop, | |
1535 | .post_action = snd_pcm_post_stop | |
1536 | }; | |
1537 | ||
1538 | /** | |
1c85cc64 | 1539 | * snd_pcm_stop - try to stop all running streams in the substream group |
df8db936 TI |
1540 | * @substream: the PCM substream instance |
1541 | * @state: PCM state after stopping the stream | |
1da177e4 | 1542 | * |
1c85cc64 | 1543 | * The state of each stream is then changed to the given state unconditionally. |
eb7c06e8 | 1544 | * |
0a11458c | 1545 | * Return: Zero if successful, or a negative error code. |
1da177e4 | 1546 | */ |
fea952e5 | 1547 | int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state) |
1da177e4 LT |
1548 | { |
1549 | return snd_pcm_action(&snd_pcm_action_stop, substream, state); | |
1550 | } | |
e88e8ae6 TI |
1551 | EXPORT_SYMBOL(snd_pcm_stop); |
1552 | ||
1da177e4 | 1553 | /** |
1c85cc64 | 1554 | * snd_pcm_drain_done - stop the DMA only when the given stream is playback |
df8db936 | 1555 | * @substream: the PCM substream |
1da177e4 | 1556 | * |
1c85cc64 | 1557 | * After stopping, the state is changed to SETUP. |
1da177e4 | 1558 | * Unlike snd_pcm_stop(), this affects only the given stream. |
eb7c06e8 | 1559 | * |
583770e8 | 1560 | * Return: Zero if successful, or a negative error code. |
1da177e4 | 1561 | */ |
877211f5 | 1562 | int snd_pcm_drain_done(struct snd_pcm_substream *substream) |
1da177e4 | 1563 | { |
877211f5 TI |
1564 | return snd_pcm_action_single(&snd_pcm_action_stop, substream, |
1565 | SNDRV_PCM_STATE_SETUP); | |
1da177e4 LT |
1566 | } |
1567 | ||
1fb8510c TI |
1568 | /** |
1569 | * snd_pcm_stop_xrun - stop the running streams as XRUN | |
1570 | * @substream: the PCM substream instance | |
1fb8510c TI |
1571 | * |
1572 | * This stops the given running substream (and all linked substreams) as XRUN. | |
1573 | * Unlike snd_pcm_stop(), this function takes the substream lock by itself. | |
1574 | * | |
1575 | * Return: Zero if successful, or a negative error code. | |
1576 | */ | |
1577 | int snd_pcm_stop_xrun(struct snd_pcm_substream *substream) | |
1578 | { | |
650224fe | 1579 | guard(pcm_stream_lock_irqsave)(substream); |
e647f5a5 | 1580 | if (substream->runtime && snd_pcm_running(substream)) |
9cd641ed | 1581 | __snd_pcm_xrun(substream); |
9cd641ed | 1582 | return 0; |
1fb8510c TI |
1583 | } |
1584 | EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun); | |
1585 | ||
1da177e4 | 1586 | /* |
cb639a42 | 1587 | * pause callbacks: pass boolean (to start pause or resume) as state argument |
1da177e4 | 1588 | */ |
cb639a42 TI |
1589 | #define pause_pushed(state) (__force bool)(state) |
1590 | ||
1591 | static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, | |
1592 | snd_pcm_state_t state) | |
1da177e4 | 1593 | { |
877211f5 | 1594 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1595 | if (!(runtime->info & SNDRV_PCM_INFO_PAUSE)) |
1596 | return -ENOSYS; | |
cb639a42 | 1597 | if (pause_pushed(state)) { |
f0061c18 | 1598 | if (runtime->state != SNDRV_PCM_STATE_RUNNING) |
1da177e4 | 1599 | return -EBADFD; |
f0061c18 | 1600 | } else if (runtime->state != SNDRV_PCM_STATE_PAUSED) |
1da177e4 LT |
1601 | return -EBADFD; |
1602 | runtime->trigger_master = substream; | |
1603 | return 0; | |
1604 | } | |
1605 | ||
cb639a42 TI |
1606 | static int snd_pcm_do_pause(struct snd_pcm_substream *substream, |
1607 | snd_pcm_state_t state) | |
1da177e4 LT |
1608 | { |
1609 | if (substream->runtime->trigger_master != substream) | |
1610 | return 0; | |
6af3fb72 | 1611 | /* The jiffies check in snd_pcm_update_hw_ptr*() is done by |
b595076a | 1612 | * a delta between the current jiffies, this gives a large enough |
6af3fb72 TI |
1613 | * delta, effectively to skip the check once. |
1614 | */ | |
1615 | substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000; | |
1da177e4 | 1616 | return substream->ops->trigger(substream, |
cb639a42 TI |
1617 | pause_pushed(state) ? |
1618 | SNDRV_PCM_TRIGGER_PAUSE_PUSH : | |
1619 | SNDRV_PCM_TRIGGER_PAUSE_RELEASE); | |
1da177e4 LT |
1620 | } |
1621 | ||
cb639a42 TI |
1622 | static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, |
1623 | snd_pcm_state_t state) | |
1da177e4 LT |
1624 | { |
1625 | if (substream->runtime->trigger_master == substream) | |
1626 | substream->ops->trigger(substream, | |
cb639a42 TI |
1627 | pause_pushed(state) ? |
1628 | SNDRV_PCM_TRIGGER_PAUSE_RELEASE : | |
1da177e4 LT |
1629 | SNDRV_PCM_TRIGGER_PAUSE_PUSH); |
1630 | } | |
1631 | ||
cb639a42 TI |
1632 | static void snd_pcm_post_pause(struct snd_pcm_substream *substream, |
1633 | snd_pcm_state_t state) | |
1da177e4 | 1634 | { |
877211f5 | 1635 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1636 | snd_pcm_trigger_tstamp(substream); |
cb639a42 | 1637 | if (pause_pushed(state)) { |
f0061c18 | 1638 | __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_PAUSED); |
90bbaf66 | 1639 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE); |
1da177e4 | 1640 | wake_up(&runtime->sleep); |
c91a988d | 1641 | wake_up(&runtime->tsleep); |
1da177e4 | 1642 | } else { |
f0061c18 | 1643 | __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_RUNNING); |
90bbaf66 | 1644 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE); |
1da177e4 LT |
1645 | } |
1646 | } | |
1647 | ||
b17154cf | 1648 | static const struct action_ops snd_pcm_action_pause = { |
1da177e4 LT |
1649 | .pre_action = snd_pcm_pre_pause, |
1650 | .do_action = snd_pcm_do_pause, | |
1651 | .undo_action = snd_pcm_undo_pause, | |
1652 | .post_action = snd_pcm_post_pause | |
1653 | }; | |
1654 | ||
1655 | /* | |
1656 | * Push/release the pause for all linked streams. | |
1657 | */ | |
cb639a42 | 1658 | static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push) |
1da177e4 | 1659 | { |
cb639a42 TI |
1660 | return snd_pcm_action(&snd_pcm_action_pause, substream, |
1661 | (__force snd_pcm_state_t)push); | |
1662 | } | |
1663 | ||
1664 | static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream, | |
1665 | bool push) | |
1666 | { | |
1667 | return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream, | |
1668 | (__force snd_pcm_state_t)push); | |
1da177e4 LT |
1669 | } |
1670 | ||
1671 | #ifdef CONFIG_PM | |
cb639a42 | 1672 | /* suspend callback: state argument ignored */ |
1da177e4 | 1673 | |
cb639a42 TI |
1674 | static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, |
1675 | snd_pcm_state_t state) | |
1da177e4 | 1676 | { |
877211f5 | 1677 | struct snd_pcm_runtime *runtime = substream->runtime; |
f0061c18 | 1678 | switch (runtime->state) { |
113ce081 | 1679 | case SNDRV_PCM_STATE_SUSPENDED: |
1da177e4 | 1680 | return -EBUSY; |
113ce081 TI |
1681 | /* unresumable PCM state; return -EBUSY for skipping suspend */ |
1682 | case SNDRV_PCM_STATE_OPEN: | |
1683 | case SNDRV_PCM_STATE_SETUP: | |
1684 | case SNDRV_PCM_STATE_DISCONNECTED: | |
1685 | return -EBUSY; | |
1686 | } | |
1da177e4 LT |
1687 | runtime->trigger_master = substream; |
1688 | return 0; | |
1689 | } | |
1690 | ||
cb639a42 TI |
1691 | static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, |
1692 | snd_pcm_state_t state) | |
1da177e4 | 1693 | { |
877211f5 | 1694 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1695 | if (runtime->trigger_master != substream) |
1696 | return 0; | |
1697 | if (! snd_pcm_running(substream)) | |
1698 | return 0; | |
1699 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); | |
2c87c1a4 | 1700 | runtime->stop_operating = true; |
1da177e4 LT |
1701 | return 0; /* suspend unconditionally */ |
1702 | } | |
1703 | ||
cb639a42 TI |
1704 | static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, |
1705 | snd_pcm_state_t state) | |
1da177e4 | 1706 | { |
877211f5 | 1707 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1708 | snd_pcm_trigger_tstamp(substream); |
f0061c18 TI |
1709 | runtime->suspended_state = runtime->state; |
1710 | runtime->status->suspended_state = runtime->suspended_state; | |
1711 | __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SUSPENDED); | |
90bbaf66 | 1712 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND); |
1da177e4 | 1713 | wake_up(&runtime->sleep); |
c91a988d | 1714 | wake_up(&runtime->tsleep); |
1da177e4 LT |
1715 | } |
1716 | ||
b17154cf | 1717 | static const struct action_ops snd_pcm_action_suspend = { |
1da177e4 LT |
1718 | .pre_action = snd_pcm_pre_suspend, |
1719 | .do_action = snd_pcm_do_suspend, | |
1720 | .post_action = snd_pcm_post_suspend | |
1721 | }; | |
1722 | ||
ce7f93e2 | 1723 | /* |
1c85cc64 | 1724 | * snd_pcm_suspend - trigger SUSPEND to all linked streams |
df8db936 | 1725 | * @substream: the PCM substream |
1da177e4 | 1726 | * |
1da177e4 | 1727 | * After this call, all streams are changed to SUSPENDED state. |
eb7c06e8 | 1728 | * |
ce7f93e2 | 1729 | * Return: Zero if successful, or a negative error code. |
1da177e4 | 1730 | */ |
ce7f93e2 | 1731 | static int snd_pcm_suspend(struct snd_pcm_substream *substream) |
1da177e4 | 1732 | { |
650224fe TI |
1733 | guard(pcm_stream_lock_irqsave)(substream); |
1734 | return snd_pcm_action(&snd_pcm_action_suspend, substream, | |
1735 | ACTION_ARG_IGNORE); | |
1da177e4 | 1736 | } |
e88e8ae6 | 1737 | |
1da177e4 | 1738 | /** |
1c85cc64 | 1739 | * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm |
df8db936 | 1740 | * @pcm: the PCM instance |
1da177e4 | 1741 | * |
1da177e4 | 1742 | * After this call, all streams are changed to SUSPENDED state. |
eb7c06e8 YB |
1743 | * |
1744 | * Return: Zero if successful (or @pcm is %NULL), or a negative error code. | |
1da177e4 | 1745 | */ |
877211f5 | 1746 | int snd_pcm_suspend_all(struct snd_pcm *pcm) |
1da177e4 | 1747 | { |
877211f5 | 1748 | struct snd_pcm_substream *substream; |
1da177e4 LT |
1749 | int stream, err = 0; |
1750 | ||
603bf524 TI |
1751 | if (! pcm) |
1752 | return 0; | |
1753 | ||
8d19b4e0 TI |
1754 | for_each_pcm_substream(pcm, stream, substream) { |
1755 | /* FIXME: the open/close code should lock this as well */ | |
1756 | if (!substream->runtime) | |
1757 | continue; | |
1758 | ||
1759 | /* | |
1760 | * Skip BE dai link PCM's that are internal and may | |
1761 | * not have their substream ops set. | |
1762 | */ | |
1763 | if (!substream->ops) | |
1764 | continue; | |
1765 | ||
1766 | err = snd_pcm_suspend(substream); | |
1767 | if (err < 0 && err != -EBUSY) | |
1768 | return err; | |
1da177e4 | 1769 | } |
2c87c1a4 | 1770 | |
8d19b4e0 TI |
1771 | for_each_pcm_substream(pcm, stream, substream) |
1772 | snd_pcm_sync_stop(substream, false); | |
2c87c1a4 | 1773 | |
1da177e4 LT |
1774 | return 0; |
1775 | } | |
e88e8ae6 TI |
1776 | EXPORT_SYMBOL(snd_pcm_suspend_all); |
1777 | ||
cb639a42 | 1778 | /* resume callbacks: state argument ignored */ |
1da177e4 | 1779 | |
cb639a42 TI |
1780 | static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, |
1781 | snd_pcm_state_t state) | |
1da177e4 | 1782 | { |
877211f5 | 1783 | struct snd_pcm_runtime *runtime = substream->runtime; |
1225675c TI |
1784 | if (runtime->state != SNDRV_PCM_STATE_SUSPENDED) |
1785 | return -EBADFD; | |
1da177e4 LT |
1786 | if (!(runtime->info & SNDRV_PCM_INFO_RESUME)) |
1787 | return -ENOSYS; | |
1788 | runtime->trigger_master = substream; | |
1789 | return 0; | |
1790 | } | |
1791 | ||
cb639a42 TI |
1792 | static int snd_pcm_do_resume(struct snd_pcm_substream *substream, |
1793 | snd_pcm_state_t state) | |
1da177e4 | 1794 | { |
877211f5 | 1795 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 LT |
1796 | if (runtime->trigger_master != substream) |
1797 | return 0; | |
1798 | /* DMA not running previously? */ | |
f0061c18 TI |
1799 | if (runtime->suspended_state != SNDRV_PCM_STATE_RUNNING && |
1800 | (runtime->suspended_state != SNDRV_PCM_STATE_DRAINING || | |
1da177e4 LT |
1801 | substream->stream != SNDRV_PCM_STREAM_PLAYBACK)) |
1802 | return 0; | |
1803 | return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME); | |
1804 | } | |
1805 | ||
cb639a42 TI |
1806 | static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, |
1807 | snd_pcm_state_t state) | |
1da177e4 LT |
1808 | { |
1809 | if (substream->runtime->trigger_master == substream && | |
1810 | snd_pcm_running(substream)) | |
1811 | substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND); | |
1812 | } | |
1813 | ||
cb639a42 TI |
1814 | static void snd_pcm_post_resume(struct snd_pcm_substream *substream, |
1815 | snd_pcm_state_t state) | |
1da177e4 | 1816 | { |
877211f5 | 1817 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1818 | snd_pcm_trigger_tstamp(substream); |
f0061c18 | 1819 | __snd_pcm_set_state(runtime, runtime->suspended_state); |
90bbaf66 | 1820 | snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME); |
1da177e4 LT |
1821 | } |
1822 | ||
b17154cf | 1823 | static const struct action_ops snd_pcm_action_resume = { |
1da177e4 LT |
1824 | .pre_action = snd_pcm_pre_resume, |
1825 | .do_action = snd_pcm_do_resume, | |
1826 | .undo_action = snd_pcm_undo_resume, | |
1827 | .post_action = snd_pcm_post_resume | |
1828 | }; | |
1829 | ||
877211f5 | 1830 | static int snd_pcm_resume(struct snd_pcm_substream *substream) |
1da177e4 | 1831 | { |
cb639a42 TI |
1832 | return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, |
1833 | ACTION_ARG_IGNORE); | |
1da177e4 LT |
1834 | } |
1835 | ||
1836 | #else | |
1837 | ||
877211f5 | 1838 | static int snd_pcm_resume(struct snd_pcm_substream *substream) |
1da177e4 LT |
1839 | { |
1840 | return -ENOSYS; | |
1841 | } | |
1842 | ||
1843 | #endif /* CONFIG_PM */ | |
1844 | ||
1845 | /* | |
1846 | * xrun ioctl | |
1847 | * | |
1848 | * Change the RUNNING stream(s) to XRUN state. | |
1849 | */ | |
877211f5 | 1850 | static int snd_pcm_xrun(struct snd_pcm_substream *substream) |
1da177e4 | 1851 | { |
877211f5 | 1852 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1853 | |
650224fe | 1854 | guard(pcm_stream_lock_irq)(substream); |
f0061c18 | 1855 | switch (runtime->state) { |
1da177e4 | 1856 | case SNDRV_PCM_STATE_XRUN: |
650224fe | 1857 | return 0; /* already there */ |
1da177e4 | 1858 | case SNDRV_PCM_STATE_RUNNING: |
9cd641ed | 1859 | __snd_pcm_xrun(substream); |
650224fe | 1860 | return 0; |
1da177e4 | 1861 | default: |
650224fe | 1862 | return -EBADFD; |
1da177e4 | 1863 | } |
1da177e4 LT |
1864 | } |
1865 | ||
1866 | /* | |
1867 | * reset ioctl | |
1868 | */ | |
cb639a42 TI |
1869 | /* reset callbacks: state argument ignored */ |
1870 | static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, | |
1871 | snd_pcm_state_t state) | |
1da177e4 | 1872 | { |
877211f5 | 1873 | struct snd_pcm_runtime *runtime = substream->runtime; |
f0061c18 | 1874 | switch (runtime->state) { |
1da177e4 LT |
1875 | case SNDRV_PCM_STATE_RUNNING: |
1876 | case SNDRV_PCM_STATE_PREPARED: | |
1877 | case SNDRV_PCM_STATE_PAUSED: | |
1878 | case SNDRV_PCM_STATE_SUSPENDED: | |
1879 | return 0; | |
1880 | default: | |
1881 | return -EBADFD; | |
1882 | } | |
1883 | } | |
1884 | ||
cb639a42 TI |
1885 | static int snd_pcm_do_reset(struct snd_pcm_substream *substream, |
1886 | snd_pcm_state_t state) | |
1da177e4 | 1887 | { |
877211f5 | 1888 | struct snd_pcm_runtime *runtime = substream->runtime; |
fc033cbf | 1889 | int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL); |
1da177e4 LT |
1890 | if (err < 0) |
1891 | return err; | |
650224fe | 1892 | guard(pcm_stream_lock_irq)(substream); |
1da177e4 | 1893 | runtime->hw_ptr_base = 0; |
e7636925 JK |
1894 | runtime->hw_ptr_interrupt = runtime->status->hw_ptr - |
1895 | runtime->status->hw_ptr % runtime->period_size; | |
1da177e4 LT |
1896 | runtime->silence_start = runtime->status->hw_ptr; |
1897 | runtime->silence_filled = 0; | |
1898 | return 0; | |
1899 | } | |
1900 | ||
cb639a42 TI |
1901 | static void snd_pcm_post_reset(struct snd_pcm_substream *substream, |
1902 | snd_pcm_state_t state) | |
1da177e4 | 1903 | { |
877211f5 | 1904 | struct snd_pcm_runtime *runtime = substream->runtime; |
650224fe | 1905 | guard(pcm_stream_lock_irq)(substream); |
1da177e4 LT |
1906 | runtime->control->appl_ptr = runtime->status->hw_ptr; |
1907 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && | |
1908 | runtime->silence_size > 0) | |
d7f5dd97 | 1909 | snd_pcm_playback_silence(substream, ULONG_MAX); |
1da177e4 LT |
1910 | } |
1911 | ||
b17154cf | 1912 | static const struct action_ops snd_pcm_action_reset = { |
1da177e4 LT |
1913 | .pre_action = snd_pcm_pre_reset, |
1914 | .do_action = snd_pcm_do_reset, | |
1915 | .post_action = snd_pcm_post_reset | |
1916 | }; | |
1917 | ||
877211f5 | 1918 | static int snd_pcm_reset(struct snd_pcm_substream *substream) |
1da177e4 | 1919 | { |
cb639a42 TI |
1920 | return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, |
1921 | ACTION_ARG_IGNORE); | |
1da177e4 LT |
1922 | } |
1923 | ||
1924 | /* | |
1925 | * prepare ioctl | |
1926 | */ | |
cb639a42 | 1927 | /* pass f_flags as state argument */ |
0df63e44 | 1928 | static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, |
cb639a42 | 1929 | snd_pcm_state_t state) |
1da177e4 | 1930 | { |
877211f5 | 1931 | struct snd_pcm_runtime *runtime = substream->runtime; |
cb639a42 TI |
1932 | int f_flags = (__force int)state; |
1933 | ||
f0061c18 TI |
1934 | if (runtime->state == SNDRV_PCM_STATE_OPEN || |
1935 | runtime->state == SNDRV_PCM_STATE_DISCONNECTED) | |
1da177e4 LT |
1936 | return -EBADFD; |
1937 | if (snd_pcm_running(substream)) | |
1938 | return -EBUSY; | |
0df63e44 | 1939 | substream->f_flags = f_flags; |
1da177e4 LT |
1940 | return 0; |
1941 | } | |
1942 | ||
cb639a42 TI |
1943 | static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, |
1944 | snd_pcm_state_t state) | |
1da177e4 LT |
1945 | { |
1946 | int err; | |
29bb274e | 1947 | snd_pcm_sync_stop(substream, true); |
1da177e4 LT |
1948 | err = substream->ops->prepare(substream); |
1949 | if (err < 0) | |
1950 | return err; | |
cb639a42 | 1951 | return snd_pcm_do_reset(substream, state); |
1da177e4 LT |
1952 | } |
1953 | ||
cb639a42 TI |
1954 | static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, |
1955 | snd_pcm_state_t state) | |
1da177e4 | 1956 | { |
877211f5 | 1957 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 1958 | runtime->control->appl_ptr = runtime->status->hw_ptr; |
9b0573c0 | 1959 | snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED); |
1da177e4 LT |
1960 | } |
1961 | ||
b17154cf | 1962 | static const struct action_ops snd_pcm_action_prepare = { |
1da177e4 LT |
1963 | .pre_action = snd_pcm_pre_prepare, |
1964 | .do_action = snd_pcm_do_prepare, | |
1965 | .post_action = snd_pcm_post_prepare | |
1966 | }; | |
1967 | ||
1968 | /** | |
1c85cc64 | 1969 | * snd_pcm_prepare - prepare the PCM substream to be triggerable |
df8db936 | 1970 | * @substream: the PCM substream instance |
0df63e44 | 1971 | * @file: file to refer f_flags |
eb7c06e8 YB |
1972 | * |
1973 | * Return: Zero if successful, or a negative error code. | |
1da177e4 | 1974 | */ |
0df63e44 TI |
1975 | static int snd_pcm_prepare(struct snd_pcm_substream *substream, |
1976 | struct file *file) | |
1da177e4 | 1977 | { |
0df63e44 TI |
1978 | int f_flags; |
1979 | ||
1980 | if (file) | |
1981 | f_flags = file->f_flags; | |
1982 | else | |
1983 | f_flags = substream->f_flags; | |
1da177e4 | 1984 | |
650224fe TI |
1985 | scoped_guard(pcm_stream_lock_irq, substream) { |
1986 | switch (substream->runtime->state) { | |
1987 | case SNDRV_PCM_STATE_PAUSED: | |
1988 | snd_pcm_pause(substream, false); | |
1989 | fallthrough; | |
1990 | case SNDRV_PCM_STATE_SUSPENDED: | |
1991 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); | |
1992 | break; | |
1993 | } | |
1b745cd9 | 1994 | } |
1b745cd9 | 1995 | |
68b4acd3 | 1996 | return snd_pcm_action_nonatomic(&snd_pcm_action_prepare, |
cb639a42 TI |
1997 | substream, |
1998 | (__force snd_pcm_state_t)f_flags); | |
1da177e4 LT |
1999 | } |
2000 | ||
2001 | /* | |
2002 | * drain ioctl | |
2003 | */ | |
2004 | ||
cb639a42 TI |
2005 | /* drain init callbacks: state argument ignored */ |
2006 | static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, | |
2007 | snd_pcm_state_t state) | |
1da177e4 | 2008 | { |
4f7c39dc | 2009 | struct snd_pcm_runtime *runtime = substream->runtime; |
f0061c18 | 2010 | switch (runtime->state) { |
4f7c39dc TI |
2011 | case SNDRV_PCM_STATE_OPEN: |
2012 | case SNDRV_PCM_STATE_DISCONNECTED: | |
2013 | case SNDRV_PCM_STATE_SUSPENDED: | |
2014 | return -EBADFD; | |
2015 | } | |
2016 | runtime->trigger_master = substream; | |
1da177e4 LT |
2017 | return 0; |
2018 | } | |
2019 | ||
cb639a42 TI |
2020 | static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, |
2021 | snd_pcm_state_t state) | |
1da177e4 | 2022 | { |
877211f5 | 2023 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 2024 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
f0061c18 | 2025 | switch (runtime->state) { |
1da177e4 LT |
2026 | case SNDRV_PCM_STATE_PREPARED: |
2027 | /* start playback stream if possible */ | |
2028 | if (! snd_pcm_playback_empty(substream)) { | |
2029 | snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING); | |
2030 | snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING); | |
70372a75 | 2031 | } else { |
f0061c18 | 2032 | __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SETUP); |
1da177e4 LT |
2033 | } |
2034 | break; | |
2035 | case SNDRV_PCM_STATE_RUNNING: | |
f0061c18 | 2036 | __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_DRAINING); |
1da177e4 | 2037 | break; |
4f7c39dc | 2038 | case SNDRV_PCM_STATE_XRUN: |
f0061c18 | 2039 | __snd_pcm_set_state(runtime, SNDRV_PCM_STATE_SETUP); |
4f7c39dc | 2040 | break; |
1da177e4 LT |
2041 | default: |
2042 | break; | |
2043 | } | |
2044 | } else { | |
2045 | /* stop running stream */ | |
f0061c18 | 2046 | if (runtime->state == SNDRV_PCM_STATE_RUNNING) { |
cb639a42 TI |
2047 | snd_pcm_state_t new_state; |
2048 | ||
2049 | new_state = snd_pcm_capture_avail(runtime) > 0 ? | |
1da177e4 | 2050 | SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP; |
b05e5787 MŚ |
2051 | snd_pcm_do_stop(substream, new_state); |
2052 | snd_pcm_post_stop(substream, new_state); | |
1da177e4 LT |
2053 | } |
2054 | } | |
48d88297 | 2055 | |
f0061c18 | 2056 | if (runtime->state == SNDRV_PCM_STATE_DRAINING && |
48d88297 LY |
2057 | runtime->trigger_master == substream && |
2058 | (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER)) | |
2059 | return substream->ops->trigger(substream, | |
2060 | SNDRV_PCM_TRIGGER_DRAIN); | |
2061 | ||
1da177e4 LT |
2062 | return 0; |
2063 | } | |
2064 | ||
cb639a42 TI |
2065 | static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, |
2066 | snd_pcm_state_t state) | |
1da177e4 LT |
2067 | { |
2068 | } | |
2069 | ||
b17154cf | 2070 | static const struct action_ops snd_pcm_action_drain_init = { |
1da177e4 LT |
2071 | .pre_action = snd_pcm_pre_drain_init, |
2072 | .do_action = snd_pcm_do_drain_init, | |
2073 | .post_action = snd_pcm_post_drain_init | |
2074 | }; | |
2075 | ||
1da177e4 LT |
2076 | /* |
2077 | * Drain the stream(s). | |
2078 | * When the substream is linked, sync until the draining of all playback streams | |
2079 | * is finished. | |
2080 | * After this call, all streams are supposed to be either SETUP or DRAINING | |
2081 | * (capture only) state. | |
2082 | */ | |
4cdc115f TI |
2083 | static int snd_pcm_drain(struct snd_pcm_substream *substream, |
2084 | struct file *file) | |
1da177e4 | 2085 | { |
877211f5 TI |
2086 | struct snd_card *card; |
2087 | struct snd_pcm_runtime *runtime; | |
ef991b95 | 2088 | struct snd_pcm_substream *s; |
f57f3df0 | 2089 | struct snd_pcm_group *group; |
ac6424b9 | 2090 | wait_queue_entry_t wait; |
1da177e4 | 2091 | int result = 0; |
4cdc115f | 2092 | int nonblock = 0; |
1da177e4 | 2093 | |
1da177e4 LT |
2094 | card = substream->pcm->card; |
2095 | runtime = substream->runtime; | |
2096 | ||
f0061c18 | 2097 | if (runtime->state == SNDRV_PCM_STATE_OPEN) |
1da177e4 LT |
2098 | return -EBADFD; |
2099 | ||
4cdc115f TI |
2100 | if (file) { |
2101 | if (file->f_flags & O_NONBLOCK) | |
2102 | nonblock = 1; | |
2103 | } else if (substream->f_flags & O_NONBLOCK) | |
2104 | nonblock = 1; | |
2105 | ||
21cb2a2e TI |
2106 | snd_pcm_stream_lock_irq(substream); |
2107 | /* resume pause */ | |
f0061c18 | 2108 | if (runtime->state == SNDRV_PCM_STATE_PAUSED) |
cb639a42 | 2109 | snd_pcm_pause(substream, false); |
21cb2a2e TI |
2110 | |
2111 | /* pre-start/stop - all running streams are changed to DRAINING state */ | |
cb639a42 TI |
2112 | result = snd_pcm_action(&snd_pcm_action_drain_init, substream, |
2113 | ACTION_ARG_IGNORE); | |
4cdc115f TI |
2114 | if (result < 0) |
2115 | goto unlock; | |
2116 | /* in non-blocking, we don't wait in ioctl but let caller poll */ | |
2117 | if (nonblock) { | |
2118 | result = -EAGAIN; | |
2119 | goto unlock; | |
21cb2a2e | 2120 | } |
1da177e4 LT |
2121 | |
2122 | for (;;) { | |
2123 | long tout; | |
d3a7dcfe | 2124 | struct snd_pcm_runtime *to_check; |
1da177e4 LT |
2125 | if (signal_pending(current)) { |
2126 | result = -ERESTARTSYS; | |
2127 | break; | |
2128 | } | |
d3a7dcfe TI |
2129 | /* find a substream to drain */ |
2130 | to_check = NULL; | |
f57f3df0 | 2131 | group = snd_pcm_stream_group_ref(substream); |
d3a7dcfe TI |
2132 | snd_pcm_group_for_each_entry(s, substream) { |
2133 | if (s->stream != SNDRV_PCM_STREAM_PLAYBACK) | |
2134 | continue; | |
2135 | runtime = s->runtime; | |
f0061c18 | 2136 | if (runtime->state == SNDRV_PCM_STATE_DRAINING) { |
d3a7dcfe | 2137 | to_check = runtime; |
21cb2a2e | 2138 | break; |
d3a7dcfe | 2139 | } |
21cb2a2e | 2140 | } |
f57f3df0 | 2141 | snd_pcm_group_unref(group, substream); |
d3a7dcfe TI |
2142 | if (!to_check) |
2143 | break; /* all drained */ | |
2144 | init_waitqueue_entry(&wait, current); | |
37151a41 | 2145 | set_current_state(TASK_INTERRUPTIBLE); |
d3a7dcfe | 2146 | add_wait_queue(&to_check->sleep, &wait); |
1da177e4 | 2147 | snd_pcm_stream_unlock_irq(substream); |
f2b3614c TI |
2148 | if (runtime->no_period_wakeup) |
2149 | tout = MAX_SCHEDULE_TIMEOUT; | |
2150 | else { | |
3ed2b549 | 2151 | tout = 100; |
f2b3614c | 2152 | if (runtime->rate) { |
3ed2b549 | 2153 | long t = runtime->buffer_size * 1100 / runtime->rate; |
f2b3614c TI |
2154 | tout = max(t, tout); |
2155 | } | |
3ed2b549 | 2156 | tout = msecs_to_jiffies(tout); |
f2b3614c | 2157 | } |
37151a41 | 2158 | tout = schedule_timeout(tout); |
f57f3df0 | 2159 | |
1da177e4 | 2160 | snd_pcm_stream_lock_irq(substream); |
f57f3df0 TI |
2161 | group = snd_pcm_stream_group_ref(substream); |
2162 | snd_pcm_group_for_each_entry(s, substream) { | |
2163 | if (s->runtime == to_check) { | |
2164 | remove_wait_queue(&to_check->sleep, &wait); | |
2165 | break; | |
2166 | } | |
2167 | } | |
2168 | snd_pcm_group_unref(group, substream); | |
2169 | ||
0914f796 TI |
2170 | if (card->shutdown) { |
2171 | result = -ENODEV; | |
2172 | break; | |
2173 | } | |
1da177e4 | 2174 | if (tout == 0) { |
f0061c18 | 2175 | if (substream->runtime->state == SNDRV_PCM_STATE_SUSPENDED) |
1da177e4 LT |
2176 | result = -ESTRPIPE; |
2177 | else { | |
09e56df8 | 2178 | dev_dbg(substream->pcm->card->dev, |
3ed2b549 | 2179 | "playback drain timeout (DMA or IRQ trouble?)\n"); |
1da177e4 LT |
2180 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); |
2181 | result = -EIO; | |
2182 | } | |
2183 | break; | |
2184 | } | |
1da177e4 | 2185 | } |
21cb2a2e | 2186 | |
4cdc115f | 2187 | unlock: |
21cb2a2e | 2188 | snd_pcm_stream_unlock_irq(substream); |
1da177e4 LT |
2189 | |
2190 | return result; | |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * drop ioctl | |
2195 | * | |
2196 | * Immediately put all linked substreams into SETUP state. | |
2197 | */ | |
877211f5 | 2198 | static int snd_pcm_drop(struct snd_pcm_substream *substream) |
1da177e4 | 2199 | { |
877211f5 | 2200 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
2201 | int result = 0; |
2202 | ||
7eaa943c TI |
2203 | if (PCM_RUNTIME_CHECK(substream)) |
2204 | return -ENXIO; | |
1da177e4 | 2205 | runtime = substream->runtime; |
1da177e4 | 2206 | |
f0061c18 TI |
2207 | if (runtime->state == SNDRV_PCM_STATE_OPEN || |
2208 | runtime->state == SNDRV_PCM_STATE_DISCONNECTED) | |
1da177e4 LT |
2209 | return -EBADFD; |
2210 | ||
650224fe | 2211 | guard(pcm_stream_lock_irq)(substream); |
1da177e4 | 2212 | /* resume pause */ |
f0061c18 | 2213 | if (runtime->state == SNDRV_PCM_STATE_PAUSED) |
cb639a42 | 2214 | snd_pcm_pause(substream, false); |
1da177e4 LT |
2215 | |
2216 | snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP); | |
2217 | /* runtime->control->appl_ptr = runtime->status->hw_ptr; */ | |
24e8fc49 | 2218 | |
1da177e4 LT |
2219 | return result; |
2220 | } | |
2221 | ||
2222 | ||
0888c321 | 2223 | static bool is_pcm_file(struct file *file) |
1da177e4 | 2224 | { |
0888c321 | 2225 | struct inode *inode = file_inode(file); |
d819fb21 | 2226 | struct snd_pcm *pcm; |
f87135f5 CL |
2227 | unsigned int minor; |
2228 | ||
0888c321 AV |
2229 | if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major) |
2230 | return false; | |
1da177e4 | 2231 | minor = iminor(inode); |
d819fb21 TI |
2232 | pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK); |
2233 | if (!pcm) | |
2234 | pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE); | |
2235 | if (!pcm) | |
2236 | return false; | |
2237 | snd_card_unref(pcm->card); | |
2238 | return true; | |
1da177e4 LT |
2239 | } |
2240 | ||
2241 | /* | |
2242 | * PCM link handling | |
2243 | */ | |
877211f5 | 2244 | static int snd_pcm_link(struct snd_pcm_substream *substream, int fd) |
1da177e4 | 2245 | { |
877211f5 TI |
2246 | struct snd_pcm_file *pcm_file; |
2247 | struct snd_pcm_substream *substream1; | |
ae921398 TI |
2248 | struct snd_pcm_group *group __free(kfree) = NULL; |
2249 | struct snd_pcm_group *target_group; | |
f57f3df0 | 2250 | bool nonatomic = substream->pcm->nonatomic; |
d90950c6 | 2251 | CLASS(fd, f)(fd); |
1da177e4 | 2252 | |
38052c2d | 2253 | if (fd_empty(f)) |
1da177e4 | 2254 | return -EBADFD; |
1da91ea8 | 2255 | if (!is_pcm_file(fd_file(f))) |
d90950c6 | 2256 | return -EBADFD; |
ae921398 | 2257 | |
1da91ea8 | 2258 | pcm_file = fd_file(f)->private_data; |
1da177e4 | 2259 | substream1 = pcm_file->substream; |
951e2736 | 2260 | |
d90950c6 TI |
2261 | if (substream == substream1) |
2262 | return -EINVAL; | |
951e2736 | 2263 | |
73365cb1 | 2264 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
d90950c6 TI |
2265 | if (!group) |
2266 | return -ENOMEM; | |
73365cb1 | 2267 | snd_pcm_group_init(group); |
f57f3df0 | 2268 | |
dd0da75b | 2269 | guard(rwsem_write)(&snd_pcm_link_rwsem); |
f0061c18 TI |
2270 | if (substream->runtime->state == SNDRV_PCM_STATE_OPEN || |
2271 | substream->runtime->state != substream1->runtime->state || | |
dd0da75b TI |
2272 | substream->pcm->nonatomic != substream1->pcm->nonatomic) |
2273 | return -EBADFD; | |
2274 | if (snd_pcm_stream_linked(substream1)) | |
2275 | return -EALREADY; | |
f57f3df0 | 2276 | |
650224fe TI |
2277 | scoped_guard(pcm_stream_lock_irq, substream) { |
2278 | if (!snd_pcm_stream_linked(substream)) { | |
2279 | snd_pcm_group_assign(substream, group); | |
2280 | group = NULL; /* assigned, don't free this one below */ | |
2281 | } | |
2282 | target_group = substream->group; | |
1da177e4 | 2283 | } |
f57f3df0 TI |
2284 | |
2285 | snd_pcm_group_lock_irq(target_group, nonatomic); | |
e18035cf | 2286 | snd_pcm_stream_lock_nested(substream1); |
f57f3df0 | 2287 | snd_pcm_group_assign(substream1, target_group); |
0e279dce | 2288 | refcount_inc(&target_group->refs); |
f57f3df0 TI |
2289 | snd_pcm_stream_unlock(substream1); |
2290 | snd_pcm_group_unlock_irq(target_group, nonatomic); | |
dd0da75b | 2291 | return 0; |
1da177e4 LT |
2292 | } |
2293 | ||
877211f5 | 2294 | static void relink_to_local(struct snd_pcm_substream *substream) |
1da177e4 | 2295 | { |
e18035cf | 2296 | snd_pcm_stream_lock_nested(substream); |
a41c4cb9 | 2297 | snd_pcm_group_assign(substream, &substream->self_group); |
f57f3df0 | 2298 | snd_pcm_stream_unlock(substream); |
1da177e4 LT |
2299 | } |
2300 | ||
877211f5 | 2301 | static int snd_pcm_unlink(struct snd_pcm_substream *substream) |
1da177e4 | 2302 | { |
a41c4cb9 | 2303 | struct snd_pcm_group *group; |
f57f3df0 TI |
2304 | bool nonatomic = substream->pcm->nonatomic; |
2305 | bool do_free = false; | |
1da177e4 | 2306 | |
dd0da75b | 2307 | guard(rwsem_write)(&snd_pcm_link_rwsem); |
f57f3df0 | 2308 | |
dd0da75b TI |
2309 | if (!snd_pcm_stream_linked(substream)) |
2310 | return -EALREADY; | |
a41c4cb9 TI |
2311 | |
2312 | group = substream->group; | |
f57f3df0 | 2313 | snd_pcm_group_lock_irq(group, nonatomic); |
a41c4cb9 | 2314 | |
1da177e4 | 2315 | relink_to_local(substream); |
0e279dce | 2316 | refcount_dec(&group->refs); |
a41c4cb9 TI |
2317 | |
2318 | /* detach the last stream, too */ | |
2319 | if (list_is_singular(&group->substreams)) { | |
7df5a5f6 TI |
2320 | relink_to_local(list_first_entry(&group->substreams, |
2321 | struct snd_pcm_substream, | |
2322 | link_list)); | |
0e279dce | 2323 | do_free = refcount_dec_and_test(&group->refs); |
1da177e4 | 2324 | } |
a41c4cb9 | 2325 | |
f57f3df0 TI |
2326 | snd_pcm_group_unlock_irq(group, nonatomic); |
2327 | if (do_free) | |
2328 | kfree(group); | |
dd0da75b | 2329 | return 0; |
1da177e4 LT |
2330 | } |
2331 | ||
2332 | /* | |
2333 | * hw configurator | |
2334 | */ | |
877211f5 TI |
2335 | static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params, |
2336 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2337 | { |
877211f5 | 2338 | struct snd_interval t; |
1da177e4 LT |
2339 | snd_interval_mul(hw_param_interval_c(params, rule->deps[0]), |
2340 | hw_param_interval_c(params, rule->deps[1]), &t); | |
2341 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2342 | } | |
2343 | ||
877211f5 TI |
2344 | static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params, |
2345 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2346 | { |
877211f5 | 2347 | struct snd_interval t; |
1da177e4 LT |
2348 | snd_interval_div(hw_param_interval_c(params, rule->deps[0]), |
2349 | hw_param_interval_c(params, rule->deps[1]), &t); | |
2350 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2351 | } | |
2352 | ||
877211f5 TI |
2353 | static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params, |
2354 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2355 | { |
877211f5 | 2356 | struct snd_interval t; |
1da177e4 LT |
2357 | snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]), |
2358 | hw_param_interval_c(params, rule->deps[1]), | |
2359 | (unsigned long) rule->private, &t); | |
2360 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2361 | } | |
2362 | ||
877211f5 TI |
2363 | static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params, |
2364 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2365 | { |
877211f5 | 2366 | struct snd_interval t; |
1da177e4 LT |
2367 | snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]), |
2368 | (unsigned long) rule->private, | |
2369 | hw_param_interval_c(params, rule->deps[1]), &t); | |
2370 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2371 | } | |
2372 | ||
877211f5 TI |
2373 | static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params, |
2374 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2375 | { |
ba71d227 | 2376 | snd_pcm_format_t k; |
b55f9fdc TS |
2377 | const struct snd_interval *i = |
2378 | hw_param_interval_c(params, rule->deps[0]); | |
877211f5 TI |
2379 | struct snd_mask m; |
2380 | struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
1da177e4 | 2381 | snd_mask_any(&m); |
ba71d227 | 2382 | pcm_for_each_format(k) { |
1da177e4 | 2383 | int bits; |
ba71d227 | 2384 | if (!snd_mask_test_format(mask, k)) |
1da177e4 LT |
2385 | continue; |
2386 | bits = snd_pcm_format_physical_width(k); | |
2387 | if (bits <= 0) | |
2388 | continue; /* ignore invalid formats */ | |
2389 | if ((unsigned)bits < i->min || (unsigned)bits > i->max) | |
ba71d227 | 2390 | snd_mask_reset(&m, (__force unsigned)k); |
1da177e4 LT |
2391 | } |
2392 | return snd_mask_refine(mask, &m); | |
2393 | } | |
2394 | ||
877211f5 TI |
2395 | static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params, |
2396 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2397 | { |
877211f5 | 2398 | struct snd_interval t; |
ba71d227 TI |
2399 | snd_pcm_format_t k; |
2400 | ||
1da177e4 LT |
2401 | t.min = UINT_MAX; |
2402 | t.max = 0; | |
2403 | t.openmin = 0; | |
2404 | t.openmax = 0; | |
ba71d227 | 2405 | pcm_for_each_format(k) { |
1da177e4 | 2406 | int bits; |
ba71d227 | 2407 | if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k)) |
1da177e4 LT |
2408 | continue; |
2409 | bits = snd_pcm_format_physical_width(k); | |
2410 | if (bits <= 0) | |
2411 | continue; /* ignore invalid formats */ | |
2412 | if (t.min > (unsigned)bits) | |
2413 | t.min = bits; | |
2414 | if (t.max < (unsigned)bits) | |
2415 | t.max = bits; | |
2416 | } | |
2417 | t.integer = 1; | |
2418 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2419 | } | |
2420 | ||
9a262344 TI |
2421 | #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12 ||\ |
2422 | SNDRV_PCM_RATE_128000 != 1 << 19 | |
1da177e4 LT |
2423 | #error "Change this table" |
2424 | #endif | |
2425 | ||
9a262344 | 2426 | /* NOTE: the list is unsorted! */ |
8b674308 | 2427 | static const unsigned int rates[] = { |
9a262344 TI |
2428 | 5512, 8000, 11025, 16000, 22050, 32000, 44100, |
2429 | 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000, 705600, 768000, | |
2430 | /* extended */ | |
2431 | 12000, 24000, 128000 | |
8b674308 | 2432 | }; |
1da177e4 | 2433 | |
7653d557 CL |
2434 | const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = { |
2435 | .count = ARRAY_SIZE(rates), | |
2436 | .list = rates, | |
2437 | }; | |
2438 | ||
877211f5 TI |
2439 | static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params, |
2440 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2441 | { |
877211f5 | 2442 | struct snd_pcm_hardware *hw = rule->private; |
1da177e4 | 2443 | return snd_interval_list(hw_param_interval(params, rule->var), |
7653d557 CL |
2444 | snd_pcm_known_rates.count, |
2445 | snd_pcm_known_rates.list, hw->rates); | |
1da177e4 LT |
2446 | } |
2447 | ||
877211f5 TI |
2448 | static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, |
2449 | struct snd_pcm_hw_rule *rule) | |
1da177e4 | 2450 | { |
877211f5 TI |
2451 | struct snd_interval t; |
2452 | struct snd_pcm_substream *substream = rule->private; | |
1da177e4 LT |
2453 | t.min = 0; |
2454 | t.max = substream->buffer_bytes_max; | |
2455 | t.openmin = 0; | |
2456 | t.openmax = 0; | |
2457 | t.integer = 1; | |
2458 | return snd_interval_refine(hw_param_interval(params, rule->var), &t); | |
2459 | } | |
2460 | ||
2112aa03 JK |
2461 | static int snd_pcm_hw_rule_subformats(struct snd_pcm_hw_params *params, |
2462 | struct snd_pcm_hw_rule *rule) | |
2463 | { | |
2464 | struct snd_mask *sfmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT); | |
2465 | struct snd_mask *fmask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
2466 | u32 *subformats = rule->private; | |
2467 | snd_pcm_format_t f; | |
2468 | struct snd_mask m; | |
2469 | ||
2470 | snd_mask_none(&m); | |
2471 | /* All PCMs support at least the default STD subformat. */ | |
2472 | snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_STD); | |
2473 | ||
2474 | pcm_for_each_format(f) { | |
2475 | if (!snd_mask_test(fmask, (__force unsigned)f)) | |
2476 | continue; | |
2477 | ||
2478 | if (f == SNDRV_PCM_FORMAT_S32_LE && *subformats) | |
2479 | m.bits[0] |= *subformats; | |
2480 | else if (snd_pcm_format_linear(f)) | |
2481 | snd_mask_set(&m, (__force unsigned)SNDRV_PCM_SUBFORMAT_MSBITS_MAX); | |
2482 | } | |
2483 | ||
2484 | return snd_mask_refine(sfmask, &m); | |
2485 | } | |
2486 | ||
2487 | static int snd_pcm_hw_constraint_subformats(struct snd_pcm_runtime *runtime, | |
2488 | unsigned int cond, u32 *subformats) | |
2489 | { | |
2490 | return snd_pcm_hw_rule_add(runtime, cond, -1, | |
2491 | snd_pcm_hw_rule_subformats, (void *)subformats, | |
2492 | SNDRV_PCM_HW_PARAM_SUBFORMAT, | |
2493 | SNDRV_PCM_HW_PARAM_FORMAT, -1); | |
2494 | } | |
2495 | ||
d662117c | 2496 | static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream) |
1da177e4 | 2497 | { |
877211f5 TI |
2498 | struct snd_pcm_runtime *runtime = substream->runtime; |
2499 | struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; | |
1da177e4 LT |
2500 | int k, err; |
2501 | ||
2502 | for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) { | |
2503 | snd_mask_any(constrs_mask(constrs, k)); | |
2504 | } | |
2505 | ||
2506 | for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) { | |
2507 | snd_interval_any(constrs_interval(constrs, k)); | |
2508 | } | |
2509 | ||
2510 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS)); | |
2511 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)); | |
2512 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)); | |
2513 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)); | |
2514 | snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS)); | |
2515 | ||
2516 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, | |
2517 | snd_pcm_hw_rule_format, NULL, | |
2518 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
2519 | if (err < 0) | |
2520 | return err; | |
2521 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | |
2522 | snd_pcm_hw_rule_sample_bits, NULL, | |
2523 | SNDRV_PCM_HW_PARAM_FORMAT, | |
2524 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
2525 | if (err < 0) | |
2526 | return err; | |
2527 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, | |
2528 | snd_pcm_hw_rule_div, NULL, | |
2529 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); | |
2530 | if (err < 0) | |
2531 | return err; | |
2532 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, | |
2533 | snd_pcm_hw_rule_mul, NULL, | |
2534 | SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1); | |
2535 | if (err < 0) | |
2536 | return err; | |
2537 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, | |
2538 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2539 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); | |
2540 | if (err < 0) | |
2541 | return err; | |
2542 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, | |
2543 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2544 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1); | |
2545 | if (err < 0) | |
2546 | return err; | |
2547 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, | |
2548 | snd_pcm_hw_rule_div, NULL, | |
2549 | SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); | |
2550 | if (err < 0) | |
2551 | return err; | |
2552 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
2553 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2554 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1); | |
2555 | if (err < 0) | |
2556 | return err; | |
2557 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
2558 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2559 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1); | |
2560 | if (err < 0) | |
2561 | return err; | |
2562 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, | |
2563 | snd_pcm_hw_rule_div, NULL, | |
2564 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1); | |
2565 | if (err < 0) | |
2566 | return err; | |
2567 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
2568 | snd_pcm_hw_rule_div, NULL, | |
2569 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); | |
2570 | if (err < 0) | |
2571 | return err; | |
2572 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
2573 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2574 | SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2575 | if (err < 0) | |
2576 | return err; | |
2577 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, | |
2578 | snd_pcm_hw_rule_muldivk, (void*) 1000000, | |
2579 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2580 | if (err < 0) | |
2581 | return err; | |
2582 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
2583 | snd_pcm_hw_rule_mul, NULL, | |
2584 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1); | |
2585 | if (err < 0) | |
2586 | return err; | |
2587 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
2588 | snd_pcm_hw_rule_mulkdiv, (void*) 8, | |
2589 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2590 | if (err < 0) | |
2591 | return err; | |
2592 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, | |
2593 | snd_pcm_hw_rule_muldivk, (void*) 1000000, | |
2594 | SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2595 | if (err < 0) | |
2596 | return err; | |
2597 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, | |
2598 | snd_pcm_hw_rule_muldivk, (void*) 8, | |
2599 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2600 | if (err < 0) | |
2601 | return err; | |
2602 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
2603 | snd_pcm_hw_rule_muldivk, (void*) 8, | |
2604 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1); | |
2605 | if (err < 0) | |
2606 | return err; | |
2607 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
2608 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2609 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2610 | if (err < 0) | |
2611 | return err; | |
2612 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, | |
2613 | snd_pcm_hw_rule_mulkdiv, (void*) 1000000, | |
2614 | SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1); | |
2615 | if (err < 0) | |
2616 | return err; | |
2617 | return 0; | |
2618 | } | |
2619 | ||
d662117c | 2620 | static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream) |
1da177e4 | 2621 | { |
877211f5 TI |
2622 | struct snd_pcm_runtime *runtime = substream->runtime; |
2623 | struct snd_pcm_hardware *hw = &runtime->hw; | |
1da177e4 LT |
2624 | int err; |
2625 | unsigned int mask = 0; | |
2626 | ||
2627 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) | |
f9b0c053 | 2628 | mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
1da177e4 | 2629 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) |
f9b0c053 | 2630 | mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); |
63825f3a | 2631 | if (hw_support_mmap(substream)) { |
1da177e4 | 2632 | if (hw->info & SNDRV_PCM_INFO_INTERLEAVED) |
f9b0c053 | 2633 | mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); |
1da177e4 | 2634 | if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED) |
f9b0c053 | 2635 | mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED); |
1da177e4 | 2636 | if (hw->info & SNDRV_PCM_INFO_COMPLEX) |
f9b0c053 | 2637 | mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX); |
1da177e4 LT |
2638 | } |
2639 | err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask); | |
7eaa943c TI |
2640 | if (err < 0) |
2641 | return err; | |
1da177e4 LT |
2642 | |
2643 | err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats); | |
7eaa943c TI |
2644 | if (err < 0) |
2645 | return err; | |
1da177e4 | 2646 | |
2112aa03 | 2647 | err = snd_pcm_hw_constraint_subformats(runtime, 0, &hw->subformats); |
7eaa943c TI |
2648 | if (err < 0) |
2649 | return err; | |
1da177e4 LT |
2650 | |
2651 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, | |
2652 | hw->channels_min, hw->channels_max); | |
7eaa943c TI |
2653 | if (err < 0) |
2654 | return err; | |
1da177e4 LT |
2655 | |
2656 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE, | |
2657 | hw->rate_min, hw->rate_max); | |
8b90ca08 GL |
2658 | if (err < 0) |
2659 | return err; | |
1da177e4 LT |
2660 | |
2661 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, | |
2662 | hw->period_bytes_min, hw->period_bytes_max); | |
8b90ca08 GL |
2663 | if (err < 0) |
2664 | return err; | |
1da177e4 LT |
2665 | |
2666 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS, | |
2667 | hw->periods_min, hw->periods_max); | |
7eaa943c TI |
2668 | if (err < 0) |
2669 | return err; | |
1da177e4 LT |
2670 | |
2671 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
2672 | hw->period_bytes_min, hw->buffer_bytes_max); | |
7eaa943c TI |
2673 | if (err < 0) |
2674 | return err; | |
1da177e4 LT |
2675 | |
2676 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
2677 | snd_pcm_hw_rule_buffer_bytes_max, substream, | |
2678 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1); | |
2679 | if (err < 0) | |
2680 | return err; | |
2681 | ||
2682 | /* FIXME: remove */ | |
2683 | if (runtime->dma_bytes) { | |
2684 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes); | |
7eaa943c | 2685 | if (err < 0) |
6cf95152 | 2686 | return err; |
1da177e4 LT |
2687 | } |
2688 | ||
2689 | if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) { | |
2690 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
2691 | snd_pcm_hw_rule_rate, hw, | |
2692 | SNDRV_PCM_HW_PARAM_RATE, -1); | |
2693 | if (err < 0) | |
2694 | return err; | |
2695 | } | |
2696 | ||
2697 | /* FIXME: this belong to lowlevel */ | |
1da177e4 LT |
2698 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); |
2699 | ||
2700 | return 0; | |
2701 | } | |
2702 | ||
3bf75f9b | 2703 | static void pcm_release_private(struct snd_pcm_substream *substream) |
1da177e4 | 2704 | { |
b51abed8 TI |
2705 | if (snd_pcm_stream_linked(substream)) |
2706 | snd_pcm_unlink(substream); | |
3bf75f9b TI |
2707 | } |
2708 | ||
2709 | void snd_pcm_release_substream(struct snd_pcm_substream *substream) | |
2710 | { | |
0df63e44 TI |
2711 | substream->ref_count--; |
2712 | if (substream->ref_count > 0) | |
2713 | return; | |
2714 | ||
3bf75f9b | 2715 | snd_pcm_drop(substream); |
3bf75f9b | 2716 | if (substream->hw_opened) { |
f0061c18 | 2717 | if (substream->runtime->state != SNDRV_PCM_STATE_OPEN) |
0fbb027b | 2718 | do_hw_free(substream); |
1da177e4 | 2719 | substream->ops->close(substream); |
3bf75f9b | 2720 | substream->hw_opened = 0; |
1da177e4 | 2721 | } |
5371a79b RW |
2722 | if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req)) |
2723 | cpu_latency_qos_remove_request(&substream->latency_pm_qos_req); | |
1576274d TI |
2724 | if (substream->pcm_release) { |
2725 | substream->pcm_release(substream); | |
2726 | substream->pcm_release = NULL; | |
2727 | } | |
3bf75f9b TI |
2728 | snd_pcm_detach_substream(substream); |
2729 | } | |
e88e8ae6 TI |
2730 | EXPORT_SYMBOL(snd_pcm_release_substream); |
2731 | ||
3bf75f9b TI |
2732 | int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, |
2733 | struct file *file, | |
2734 | struct snd_pcm_substream **rsubstream) | |
2735 | { | |
2736 | struct snd_pcm_substream *substream; | |
2737 | int err; | |
2738 | ||
2739 | err = snd_pcm_attach_substream(pcm, stream, file, &substream); | |
2740 | if (err < 0) | |
2741 | return err; | |
0df63e44 TI |
2742 | if (substream->ref_count > 1) { |
2743 | *rsubstream = substream; | |
2744 | return 0; | |
2745 | } | |
2746 | ||
3bf75f9b TI |
2747 | err = snd_pcm_hw_constraints_init(substream); |
2748 | if (err < 0) { | |
09e56df8 | 2749 | pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n"); |
3bf75f9b TI |
2750 | goto error; |
2751 | } | |
2752 | ||
137c171c TI |
2753 | err = substream->ops->open(substream); |
2754 | if (err < 0) | |
3bf75f9b TI |
2755 | goto error; |
2756 | ||
2757 | substream->hw_opened = 1; | |
2758 | ||
2759 | err = snd_pcm_hw_constraints_complete(substream); | |
2760 | if (err < 0) { | |
09e56df8 | 2761 | pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n"); |
3bf75f9b TI |
2762 | goto error; |
2763 | } | |
2764 | ||
a25684a9 TI |
2765 | /* automatically set EXPLICIT_SYNC flag in the managed mode whenever |
2766 | * the DMA buffer requires it | |
2767 | */ | |
2768 | if (substream->managed_buffer_alloc && | |
2769 | substream->dma_buffer.dev.need_sync) | |
2770 | substream->runtime->hw.info |= SNDRV_PCM_INFO_EXPLICIT_SYNC; | |
2771 | ||
3bf75f9b | 2772 | *rsubstream = substream; |
1da177e4 | 2773 | return 0; |
3bf75f9b TI |
2774 | |
2775 | error: | |
2776 | snd_pcm_release_substream(substream); | |
2777 | return err; | |
1da177e4 | 2778 | } |
e88e8ae6 TI |
2779 | EXPORT_SYMBOL(snd_pcm_open_substream); |
2780 | ||
1da177e4 | 2781 | static int snd_pcm_open_file(struct file *file, |
877211f5 | 2782 | struct snd_pcm *pcm, |
ffd3d5c6 | 2783 | int stream) |
1da177e4 | 2784 | { |
877211f5 TI |
2785 | struct snd_pcm_file *pcm_file; |
2786 | struct snd_pcm_substream *substream; | |
3bf75f9b | 2787 | int err; |
1da177e4 | 2788 | |
3bf75f9b TI |
2789 | err = snd_pcm_open_substream(pcm, stream, file, &substream); |
2790 | if (err < 0) | |
2791 | return err; | |
2792 | ||
548a648b TI |
2793 | pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL); |
2794 | if (pcm_file == NULL) { | |
2795 | snd_pcm_release_substream(substream); | |
2796 | return -ENOMEM; | |
2797 | } | |
2798 | pcm_file->substream = substream; | |
de89750c | 2799 | if (substream->ref_count == 1) |
0df63e44 | 2800 | substream->pcm_release = pcm_release_private; |
1da177e4 | 2801 | file->private_data = pcm_file; |
ffd3d5c6 | 2802 | |
1da177e4 LT |
2803 | return 0; |
2804 | } | |
2805 | ||
f87135f5 CL |
2806 | static int snd_pcm_playback_open(struct inode *inode, struct file *file) |
2807 | { | |
2808 | struct snd_pcm *pcm; | |
02f4865f TI |
2809 | int err = nonseekable_open(inode, file); |
2810 | if (err < 0) | |
2811 | return err; | |
f87135f5 CL |
2812 | pcm = snd_lookup_minor_data(iminor(inode), |
2813 | SNDRV_DEVICE_TYPE_PCM_PLAYBACK); | |
a0830dbd | 2814 | err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK); |
8bb4d9ce TI |
2815 | if (pcm) |
2816 | snd_card_unref(pcm->card); | |
a0830dbd | 2817 | return err; |
f87135f5 CL |
2818 | } |
2819 | ||
2820 | static int snd_pcm_capture_open(struct inode *inode, struct file *file) | |
1da177e4 | 2821 | { |
877211f5 | 2822 | struct snd_pcm *pcm; |
02f4865f TI |
2823 | int err = nonseekable_open(inode, file); |
2824 | if (err < 0) | |
2825 | return err; | |
f87135f5 CL |
2826 | pcm = snd_lookup_minor_data(iminor(inode), |
2827 | SNDRV_DEVICE_TYPE_PCM_CAPTURE); | |
a0830dbd | 2828 | err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE); |
8bb4d9ce TI |
2829 | if (pcm) |
2830 | snd_card_unref(pcm->card); | |
a0830dbd | 2831 | return err; |
f87135f5 CL |
2832 | } |
2833 | ||
2834 | static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream) | |
2835 | { | |
2836 | int err; | |
ac6424b9 | 2837 | wait_queue_entry_t wait; |
1da177e4 | 2838 | |
1da177e4 LT |
2839 | if (pcm == NULL) { |
2840 | err = -ENODEV; | |
2841 | goto __error1; | |
2842 | } | |
2843 | err = snd_card_file_add(pcm->card, file); | |
2844 | if (err < 0) | |
2845 | goto __error1; | |
2846 | if (!try_module_get(pcm->card->module)) { | |
2847 | err = -EFAULT; | |
2848 | goto __error2; | |
2849 | } | |
2850 | init_waitqueue_entry(&wait, current); | |
2851 | add_wait_queue(&pcm->open_wait, &wait); | |
1a60d4c5 | 2852 | mutex_lock(&pcm->open_mutex); |
1da177e4 | 2853 | while (1) { |
ffd3d5c6 | 2854 | err = snd_pcm_open_file(file, pcm, stream); |
1da177e4 LT |
2855 | if (err >= 0) |
2856 | break; | |
2857 | if (err == -EAGAIN) { | |
2858 | if (file->f_flags & O_NONBLOCK) { | |
2859 | err = -EBUSY; | |
2860 | break; | |
2861 | } | |
2862 | } else | |
2863 | break; | |
2864 | set_current_state(TASK_INTERRUPTIBLE); | |
1a60d4c5 | 2865 | mutex_unlock(&pcm->open_mutex); |
1da177e4 | 2866 | schedule(); |
1a60d4c5 | 2867 | mutex_lock(&pcm->open_mutex); |
0914f796 TI |
2868 | if (pcm->card->shutdown) { |
2869 | err = -ENODEV; | |
2870 | break; | |
2871 | } | |
1da177e4 LT |
2872 | if (signal_pending(current)) { |
2873 | err = -ERESTARTSYS; | |
2874 | break; | |
2875 | } | |
2876 | } | |
2877 | remove_wait_queue(&pcm->open_wait, &wait); | |
1a60d4c5 | 2878 | mutex_unlock(&pcm->open_mutex); |
1da177e4 LT |
2879 | if (err < 0) |
2880 | goto __error; | |
2881 | return err; | |
2882 | ||
2883 | __error: | |
2884 | module_put(pcm->card->module); | |
2885 | __error2: | |
2886 | snd_card_file_remove(pcm->card, file); | |
2887 | __error1: | |
2888 | return err; | |
2889 | } | |
2890 | ||
2891 | static int snd_pcm_release(struct inode *inode, struct file *file) | |
2892 | { | |
877211f5 TI |
2893 | struct snd_pcm *pcm; |
2894 | struct snd_pcm_substream *substream; | |
2895 | struct snd_pcm_file *pcm_file; | |
1da177e4 LT |
2896 | |
2897 | pcm_file = file->private_data; | |
2898 | substream = pcm_file->substream; | |
7eaa943c TI |
2899 | if (snd_BUG_ON(!substream)) |
2900 | return -ENXIO; | |
1da177e4 | 2901 | pcm = substream->pcm; |
534a427b TI |
2902 | |
2903 | /* block until the device gets woken up as it may touch the hardware */ | |
2904 | snd_power_wait(pcm->card); | |
2905 | ||
dd0da75b TI |
2906 | scoped_guard(mutex, &pcm->open_mutex) { |
2907 | snd_pcm_release_substream(substream); | |
2908 | kfree(pcm_file); | |
2909 | } | |
1da177e4 LT |
2910 | wake_up(&pcm->open_wait); |
2911 | module_put(pcm->card->module); | |
2912 | snd_card_file_remove(pcm->card, file); | |
2913 | return 0; | |
2914 | } | |
2915 | ||
f839cc1c TI |
2916 | /* check and update PCM state; return 0 or a negative error |
2917 | * call this inside PCM lock | |
2918 | */ | |
2919 | static int do_pcm_hwsync(struct snd_pcm_substream *substream) | |
1da177e4 | 2920 | { |
f0061c18 | 2921 | switch (substream->runtime->state) { |
1da177e4 | 2922 | case SNDRV_PCM_STATE_DRAINING: |
f839cc1c TI |
2923 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
2924 | return -EBADFD; | |
c0dbbdad | 2925 | fallthrough; |
f839cc1c TI |
2926 | case SNDRV_PCM_STATE_RUNNING: |
2927 | return snd_pcm_update_hw_ptr(substream); | |
2928 | case SNDRV_PCM_STATE_PREPARED: | |
2929 | case SNDRV_PCM_STATE_PAUSED: | |
2930 | return 0; | |
51840409 | 2931 | case SNDRV_PCM_STATE_SUSPENDED: |
f839cc1c TI |
2932 | return -ESTRPIPE; |
2933 | case SNDRV_PCM_STATE_XRUN: | |
2934 | return -EPIPE; | |
1da177e4 | 2935 | default: |
f839cc1c | 2936 | return -EBADFD; |
1da177e4 | 2937 | } |
f839cc1c | 2938 | } |
1da177e4 | 2939 | |
9027c463 | 2940 | /* increase the appl_ptr; returns the processed frames or a negative error */ |
e0327a0f TI |
2941 | static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream, |
2942 | snd_pcm_uframes_t frames, | |
2943 | snd_pcm_sframes_t avail) | |
2944 | { | |
2945 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2946 | snd_pcm_sframes_t appl_ptr; | |
9027c463 | 2947 | int ret; |
e0327a0f TI |
2948 | |
2949 | if (avail <= 0) | |
2950 | return 0; | |
2951 | if (frames > (snd_pcm_uframes_t)avail) | |
2952 | frames = avail; | |
2953 | appl_ptr = runtime->control->appl_ptr + frames; | |
2954 | if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary) | |
2955 | appl_ptr -= runtime->boundary; | |
66e01a5c | 2956 | ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); |
9027c463 | 2957 | return ret < 0 ? ret : frames; |
e0327a0f TI |
2958 | } |
2959 | ||
fb51f1cd | 2960 | /* decrease the appl_ptr; returns the processed frames or zero for error */ |
e0327a0f TI |
2961 | static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream, |
2962 | snd_pcm_uframes_t frames, | |
2963 | snd_pcm_sframes_t avail) | |
2964 | { | |
2965 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2966 | snd_pcm_sframes_t appl_ptr; | |
9027c463 | 2967 | int ret; |
e0327a0f TI |
2968 | |
2969 | if (avail <= 0) | |
2970 | return 0; | |
2971 | if (frames > (snd_pcm_uframes_t)avail) | |
2972 | frames = avail; | |
1da177e4 LT |
2973 | appl_ptr = runtime->control->appl_ptr - frames; |
2974 | if (appl_ptr < 0) | |
2975 | appl_ptr += runtime->boundary; | |
66e01a5c | 2976 | ret = pcm_lib_apply_appl_ptr(substream, appl_ptr); |
fb51f1cd TI |
2977 | /* NOTE: we return zero for errors because PulseAudio gets depressed |
2978 | * upon receiving an error from rewind ioctl and stops processing | |
2979 | * any longer. Returning zero means that no rewind is done, so | |
2980 | * it's not absolutely wrong to answer like that. | |
2981 | */ | |
2982 | return ret < 0 ? 0 : frames; | |
e0327a0f TI |
2983 | } |
2984 | ||
763e5067 TI |
2985 | static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream, |
2986 | snd_pcm_uframes_t frames) | |
1da177e4 | 2987 | { |
1da177e4 | 2988 | snd_pcm_sframes_t ret; |
1da177e4 LT |
2989 | |
2990 | if (frames == 0) | |
2991 | return 0; | |
2992 | ||
650224fe TI |
2993 | scoped_guard(pcm_stream_lock_irq, substream) { |
2994 | ret = do_pcm_hwsync(substream); | |
2995 | if (!ret) | |
2996 | ret = rewind_appl_ptr(substream, frames, | |
2997 | snd_pcm_hw_avail(substream)); | |
2998 | } | |
a25684a9 TI |
2999 | if (ret >= 0) |
3000 | snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); | |
1da177e4 LT |
3001 | return ret; |
3002 | } | |
3003 | ||
763e5067 TI |
3004 | static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream, |
3005 | snd_pcm_uframes_t frames) | |
1da177e4 | 3006 | { |
1da177e4 | 3007 | snd_pcm_sframes_t ret; |
1da177e4 LT |
3008 | |
3009 | if (frames == 0) | |
3010 | return 0; | |
3011 | ||
650224fe TI |
3012 | scoped_guard(pcm_stream_lock_irq, substream) { |
3013 | ret = do_pcm_hwsync(substream); | |
3014 | if (!ret) | |
3015 | ret = forward_appl_ptr(substream, frames, | |
3016 | snd_pcm_avail(substream)); | |
3017 | } | |
a25684a9 TI |
3018 | if (ret >= 0) |
3019 | snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); | |
1da177e4 LT |
3020 | return ret; |
3021 | } | |
3022 | ||
912e4c33 JM |
3023 | static int snd_pcm_delay(struct snd_pcm_substream *substream, |
3024 | snd_pcm_sframes_t *delay) | |
1da177e4 | 3025 | { |
1da177e4 | 3026 | int err; |
1da177e4 | 3027 | |
650224fe TI |
3028 | scoped_guard(pcm_stream_lock_irq, substream) { |
3029 | err = do_pcm_hwsync(substream); | |
3030 | if (delay && !err) | |
3031 | *delay = snd_pcm_calc_delay(substream); | |
3032 | } | |
a25684a9 TI |
3033 | snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU); |
3034 | ||
912e4c33 | 3035 | return err; |
1da177e4 LT |
3036 | } |
3037 | ||
c18c4966 TI |
3038 | static inline int snd_pcm_hwsync(struct snd_pcm_substream *substream) |
3039 | { | |
3040 | return snd_pcm_delay(substream, NULL); | |
3041 | } | |
3042 | ||
877211f5 TI |
3043 | static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream, |
3044 | struct snd_pcm_sync_ptr __user *_sync_ptr) | |
1da177e4 | 3045 | { |
877211f5 TI |
3046 | struct snd_pcm_runtime *runtime = substream->runtime; |
3047 | struct snd_pcm_sync_ptr sync_ptr; | |
3048 | volatile struct snd_pcm_mmap_status *status; | |
3049 | volatile struct snd_pcm_mmap_control *control; | |
1da177e4 LT |
3050 | int err; |
3051 | ||
3052 | memset(&sync_ptr, 0, sizeof(sync_ptr)); | |
3053 | if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags))) | |
3054 | return -EFAULT; | |
877211f5 | 3055 | if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control))) |
1da177e4 LT |
3056 | return -EFAULT; |
3057 | status = runtime->status; | |
3058 | control = runtime->control; | |
3059 | if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) { | |
3060 | err = snd_pcm_hwsync(substream); | |
3061 | if (err < 0) | |
3062 | return err; | |
3063 | } | |
650224fe TI |
3064 | scoped_guard(pcm_stream_lock_irq, substream) { |
3065 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) { | |
3066 | err = pcm_lib_apply_appl_ptr(substream, | |
3067 | sync_ptr.c.control.appl_ptr); | |
3068 | if (err < 0) | |
3069 | return err; | |
3070 | } else { | |
3071 | sync_ptr.c.control.appl_ptr = control->appl_ptr; | |
9027c463 | 3072 | } |
650224fe TI |
3073 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) |
3074 | control->avail_min = sync_ptr.c.control.avail_min; | |
3075 | else | |
3076 | sync_ptr.c.control.avail_min = control->avail_min; | |
3077 | sync_ptr.s.status.state = status->state; | |
3078 | sync_ptr.s.status.hw_ptr = status->hw_ptr; | |
3079 | sync_ptr.s.status.tstamp = status->tstamp; | |
3080 | sync_ptr.s.status.suspended_state = status->suspended_state; | |
3081 | sync_ptr.s.status.audio_tstamp = status->audio_tstamp; | |
9027c463 | 3082 | } |
a25684a9 TI |
3083 | if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) |
3084 | snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); | |
1da177e4 LT |
3085 | if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr))) |
3086 | return -EFAULT; | |
3087 | return 0; | |
3088 | } | |
b751eef1 | 3089 | |
09d94175 | 3090 | struct snd_pcm_mmap_status32 { |
cb639a42 | 3091 | snd_pcm_state_t state; |
09d94175 AB |
3092 | s32 pad1; |
3093 | u32 hw_ptr; | |
80fe7430 AB |
3094 | s32 tstamp_sec; |
3095 | s32 tstamp_nsec; | |
cb639a42 | 3096 | snd_pcm_state_t suspended_state; |
80fe7430 AB |
3097 | s32 audio_tstamp_sec; |
3098 | s32 audio_tstamp_nsec; | |
fdbe0f20 | 3099 | } __packed; |
09d94175 AB |
3100 | |
3101 | struct snd_pcm_mmap_control32 { | |
3102 | u32 appl_ptr; | |
3103 | u32 avail_min; | |
3104 | }; | |
3105 | ||
3106 | struct snd_pcm_sync_ptr32 { | |
3107 | u32 flags; | |
3108 | union { | |
3109 | struct snd_pcm_mmap_status32 status; | |
3110 | unsigned char reserved[64]; | |
3111 | } s; | |
3112 | union { | |
3113 | struct snd_pcm_mmap_control32 control; | |
3114 | unsigned char reserved[64]; | |
3115 | } c; | |
fdbe0f20 | 3116 | } __packed; |
09d94175 | 3117 | |
73c6e9e1 | 3118 | /* recalculate the boundary within 32bit */ |
09d94175 AB |
3119 | static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime) |
3120 | { | |
3121 | snd_pcm_uframes_t boundary; | |
3122 | ||
3123 | if (! runtime->buffer_size) | |
3124 | return 0; | |
3125 | boundary = runtime->buffer_size; | |
3126 | while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size) | |
3127 | boundary *= 2; | |
3128 | return boundary; | |
3129 | } | |
3130 | ||
3131 | static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream, | |
3132 | struct snd_pcm_sync_ptr32 __user *src) | |
3133 | { | |
3134 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3135 | volatile struct snd_pcm_mmap_status *status; | |
3136 | volatile struct snd_pcm_mmap_control *control; | |
3137 | u32 sflags; | |
3138 | struct snd_pcm_mmap_control scontrol; | |
3139 | struct snd_pcm_mmap_status sstatus; | |
3140 | snd_pcm_uframes_t boundary; | |
3141 | int err; | |
3142 | ||
3143 | if (snd_BUG_ON(!runtime)) | |
3144 | return -EINVAL; | |
3145 | ||
3146 | if (get_user(sflags, &src->flags) || | |
3147 | get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || | |
3148 | get_user(scontrol.avail_min, &src->c.control.avail_min)) | |
3149 | return -EFAULT; | |
3150 | if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) { | |
3151 | err = snd_pcm_hwsync(substream); | |
3152 | if (err < 0) | |
3153 | return err; | |
3154 | } | |
3155 | status = runtime->status; | |
3156 | control = runtime->control; | |
3157 | boundary = recalculate_boundary(runtime); | |
3158 | if (! boundary) | |
3159 | boundary = 0x7fffffff; | |
650224fe TI |
3160 | scoped_guard(pcm_stream_lock_irq, substream) { |
3161 | /* FIXME: we should consider the boundary for the sync from app */ | |
3162 | if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) { | |
3163 | err = pcm_lib_apply_appl_ptr(substream, | |
3164 | scontrol.appl_ptr); | |
3165 | if (err < 0) | |
3166 | return err; | |
3167 | } else | |
3168 | scontrol.appl_ptr = control->appl_ptr % boundary; | |
3169 | if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN)) | |
3170 | control->avail_min = scontrol.avail_min; | |
3171 | else | |
3172 | scontrol.avail_min = control->avail_min; | |
3173 | sstatus.state = status->state; | |
3174 | sstatus.hw_ptr = status->hw_ptr % boundary; | |
3175 | sstatus.tstamp = status->tstamp; | |
3176 | sstatus.suspended_state = status->suspended_state; | |
3177 | sstatus.audio_tstamp = status->audio_tstamp; | |
3178 | } | |
a25684a9 TI |
3179 | if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) |
3180 | snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); | |
09d94175 AB |
3181 | if (put_user(sstatus.state, &src->s.status.state) || |
3182 | put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) || | |
80fe7430 AB |
3183 | put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) || |
3184 | put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) || | |
09d94175 | 3185 | put_user(sstatus.suspended_state, &src->s.status.suspended_state) || |
80fe7430 AB |
3186 | put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) || |
3187 | put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) || | |
09d94175 AB |
3188 | put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) || |
3189 | put_user(scontrol.avail_min, &src->c.control.avail_min)) | |
3190 | return -EFAULT; | |
3191 | ||
3192 | return 0; | |
3193 | } | |
80fe7430 | 3194 | #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32) |
09d94175 | 3195 | |
b751eef1 JK |
3196 | static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg) |
3197 | { | |
3198 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3199 | int arg; | |
3200 | ||
3201 | if (get_user(arg, _arg)) | |
3202 | return -EFAULT; | |
3203 | if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST) | |
3204 | return -EINVAL; | |
2408c219 | 3205 | runtime->tstamp_type = arg; |
b751eef1 JK |
3206 | return 0; |
3207 | } | |
67616fed TI |
3208 | |
3209 | static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream, | |
3210 | struct snd_xferi __user *_xferi) | |
3211 | { | |
3212 | struct snd_xferi xferi; | |
3213 | struct snd_pcm_runtime *runtime = substream->runtime; | |
3214 | snd_pcm_sframes_t result; | |
3215 | ||
f0061c18 | 3216 | if (runtime->state == SNDRV_PCM_STATE_OPEN) |
67616fed TI |
3217 | return -EBADFD; |
3218 | if (put_user(0, &_xferi->result)) | |
3219 | return -EFAULT; | |
3220 | if (copy_from_user(&xferi, _xferi, sizeof(xferi))) | |
3221 | return -EFAULT; | |
3222 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
3223 | result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames); | |
3224 | else | |
3225 | result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames); | |
ebe6976d AV |
3226 | if (put_user(result, &_xferi->result)) |
3227 | return -EFAULT; | |
67616fed TI |
3228 | return result < 0 ? result : 0; |
3229 | } | |
3230 | ||
3231 | static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream, | |
3232 | struct snd_xfern __user *_xfern) | |
3233 | { | |
3234 | struct snd_xfern xfern; | |
3235 | struct snd_pcm_runtime *runtime = substream->runtime; | |
ae921398 | 3236 | void *bufs __free(kfree) = NULL; |
67616fed TI |
3237 | snd_pcm_sframes_t result; |
3238 | ||
f0061c18 | 3239 | if (runtime->state == SNDRV_PCM_STATE_OPEN) |
67616fed TI |
3240 | return -EBADFD; |
3241 | if (runtime->channels > 128) | |
3242 | return -EINVAL; | |
3243 | if (put_user(0, &_xfern->result)) | |
3244 | return -EFAULT; | |
3245 | if (copy_from_user(&xfern, _xfern, sizeof(xfern))) | |
3246 | return -EFAULT; | |
3247 | ||
519b2b14 | 3248 | bufs = memdup_array_user(xfern.bufs, runtime->channels, sizeof(void *)); |
67616fed | 3249 | if (IS_ERR(bufs)) |
40a024b8 | 3250 | return PTR_ERR(bufs); |
67616fed TI |
3251 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
3252 | result = snd_pcm_lib_writev(substream, bufs, xfern.frames); | |
3253 | else | |
3254 | result = snd_pcm_lib_readv(substream, bufs, xfern.frames); | |
ebe6976d AV |
3255 | if (put_user(result, &_xfern->result)) |
3256 | return -EFAULT; | |
67616fed TI |
3257 | return result < 0 ? result : 0; |
3258 | } | |
3259 | ||
3260 | static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream, | |
3261 | snd_pcm_uframes_t __user *_frames) | |
3262 | { | |
3263 | snd_pcm_uframes_t frames; | |
3264 | snd_pcm_sframes_t result; | |
3265 | ||
3266 | if (get_user(frames, _frames)) | |
3267 | return -EFAULT; | |
3268 | if (put_user(0, _frames)) | |
3269 | return -EFAULT; | |
763e5067 | 3270 | result = snd_pcm_rewind(substream, frames); |
ebe6976d AV |
3271 | if (put_user(result, _frames)) |
3272 | return -EFAULT; | |
67616fed TI |
3273 | return result < 0 ? result : 0; |
3274 | } | |
3275 | ||
3276 | static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream, | |
3277 | snd_pcm_uframes_t __user *_frames) | |
3278 | { | |
3279 | snd_pcm_uframes_t frames; | |
3280 | snd_pcm_sframes_t result; | |
3281 | ||
3282 | if (get_user(frames, _frames)) | |
3283 | return -EFAULT; | |
3284 | if (put_user(0, _frames)) | |
3285 | return -EFAULT; | |
763e5067 | 3286 | result = snd_pcm_forward(substream, frames); |
ebe6976d AV |
3287 | if (put_user(result, _frames)) |
3288 | return -EFAULT; | |
67616fed TI |
3289 | return result < 0 ? result : 0; |
3290 | } | |
3291 | ||
3292 | static int snd_pcm_common_ioctl(struct file *file, | |
0df63e44 | 3293 | struct snd_pcm_substream *substream, |
1da177e4 LT |
3294 | unsigned int cmd, void __user *arg) |
3295 | { | |
4b671f57 | 3296 | struct snd_pcm_file *pcm_file = file->private_data; |
7d8e8292 TI |
3297 | int res; |
3298 | ||
67616fed TI |
3299 | if (PCM_RUNTIME_CHECK(substream)) |
3300 | return -ENXIO; | |
3301 | ||
f0061c18 | 3302 | if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) |
36df2427 TI |
3303 | return -EBADFD; |
3304 | ||
b6cc78da | 3305 | res = snd_power_wait(substream->pcm->card); |
7d8e8292 TI |
3306 | if (res < 0) |
3307 | return res; | |
4b671f57 | 3308 | |
1da177e4 LT |
3309 | switch (cmd) { |
3310 | case SNDRV_PCM_IOCTL_PVERSION: | |
3311 | return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0; | |
3312 | case SNDRV_PCM_IOCTL_INFO: | |
3313 | return snd_pcm_info_user(substream, arg); | |
28e9e473 JK |
3314 | case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */ |
3315 | return 0; | |
b751eef1 JK |
3316 | case SNDRV_PCM_IOCTL_TTSTAMP: |
3317 | return snd_pcm_tstamp(substream, arg); | |
4b671f57 TI |
3318 | case SNDRV_PCM_IOCTL_USER_PVERSION: |
3319 | if (get_user(pcm_file->user_pversion, | |
3320 | (unsigned int __user *)arg)) | |
3321 | return -EFAULT; | |
3322 | return 0; | |
1da177e4 LT |
3323 | case SNDRV_PCM_IOCTL_HW_REFINE: |
3324 | return snd_pcm_hw_refine_user(substream, arg); | |
3325 | case SNDRV_PCM_IOCTL_HW_PARAMS: | |
3326 | return snd_pcm_hw_params_user(substream, arg); | |
3327 | case SNDRV_PCM_IOCTL_HW_FREE: | |
3328 | return snd_pcm_hw_free(substream); | |
3329 | case SNDRV_PCM_IOCTL_SW_PARAMS: | |
3330 | return snd_pcm_sw_params_user(substream, arg); | |
3ddee7f8 BW |
3331 | case SNDRV_PCM_IOCTL_STATUS32: |
3332 | return snd_pcm_status_user32(substream, arg, false); | |
3333 | case SNDRV_PCM_IOCTL_STATUS_EXT32: | |
3334 | return snd_pcm_status_user32(substream, arg, true); | |
3335 | case SNDRV_PCM_IOCTL_STATUS64: | |
3336 | return snd_pcm_status_user64(substream, arg, false); | |
3337 | case SNDRV_PCM_IOCTL_STATUS_EXT64: | |
3338 | return snd_pcm_status_user64(substream, arg, true); | |
1da177e4 LT |
3339 | case SNDRV_PCM_IOCTL_CHANNEL_INFO: |
3340 | return snd_pcm_channel_info_user(substream, arg); | |
3341 | case SNDRV_PCM_IOCTL_PREPARE: | |
0df63e44 | 3342 | return snd_pcm_prepare(substream, file); |
1da177e4 LT |
3343 | case SNDRV_PCM_IOCTL_RESET: |
3344 | return snd_pcm_reset(substream); | |
3345 | case SNDRV_PCM_IOCTL_START: | |
c2c86a97 | 3346 | return snd_pcm_start_lock_irq(substream); |
1da177e4 LT |
3347 | case SNDRV_PCM_IOCTL_LINK: |
3348 | return snd_pcm_link(substream, (int)(unsigned long) arg); | |
3349 | case SNDRV_PCM_IOCTL_UNLINK: | |
3350 | return snd_pcm_unlink(substream); | |
3351 | case SNDRV_PCM_IOCTL_RESUME: | |
3352 | return snd_pcm_resume(substream); | |
3353 | case SNDRV_PCM_IOCTL_XRUN: | |
3354 | return snd_pcm_xrun(substream); | |
3355 | case SNDRV_PCM_IOCTL_HWSYNC: | |
3356 | return snd_pcm_hwsync(substream); | |
3357 | case SNDRV_PCM_IOCTL_DELAY: | |
c2c86a97 | 3358 | { |
c18c4966 | 3359 | snd_pcm_sframes_t delay = 0; |
c2c86a97 | 3360 | snd_pcm_sframes_t __user *res = arg; |
912e4c33 | 3361 | int err; |
c2c86a97 | 3362 | |
912e4c33 JM |
3363 | err = snd_pcm_delay(substream, &delay); |
3364 | if (err) | |
3365 | return err; | |
c2c86a97 TI |
3366 | if (put_user(delay, res)) |
3367 | return -EFAULT; | |
3368 | return 0; | |
3369 | } | |
80fe7430 AB |
3370 | case __SNDRV_PCM_IOCTL_SYNC_PTR32: |
3371 | return snd_pcm_ioctl_sync_ptr_compat(substream, arg); | |
3372 | case __SNDRV_PCM_IOCTL_SYNC_PTR64: | |
1da177e4 | 3373 | return snd_pcm_sync_ptr(substream, arg); |
59d48582 | 3374 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
1da177e4 LT |
3375 | case SNDRV_PCM_IOCTL_HW_REFINE_OLD: |
3376 | return snd_pcm_hw_refine_old_user(substream, arg); | |
3377 | case SNDRV_PCM_IOCTL_HW_PARAMS_OLD: | |
3378 | return snd_pcm_hw_params_old_user(substream, arg); | |
59d48582 | 3379 | #endif |
1da177e4 | 3380 | case SNDRV_PCM_IOCTL_DRAIN: |
4cdc115f | 3381 | return snd_pcm_drain(substream, file); |
1da177e4 LT |
3382 | case SNDRV_PCM_IOCTL_DROP: |
3383 | return snd_pcm_drop(substream); | |
e661d0dd | 3384 | case SNDRV_PCM_IOCTL_PAUSE: |
cb639a42 | 3385 | return snd_pcm_pause_lock_irq(substream, (unsigned long)arg); |
1da177e4 | 3386 | case SNDRV_PCM_IOCTL_WRITEI_FRAMES: |
1da177e4 | 3387 | case SNDRV_PCM_IOCTL_READI_FRAMES: |
67616fed TI |
3388 | return snd_pcm_xferi_frames_ioctl(substream, arg); |
3389 | case SNDRV_PCM_IOCTL_WRITEN_FRAMES: | |
1da177e4 | 3390 | case SNDRV_PCM_IOCTL_READN_FRAMES: |
67616fed | 3391 | return snd_pcm_xfern_frames_ioctl(substream, arg); |
1da177e4 | 3392 | case SNDRV_PCM_IOCTL_REWIND: |
67616fed | 3393 | return snd_pcm_rewind_ioctl(substream, arg); |
1da177e4 | 3394 | case SNDRV_PCM_IOCTL_FORWARD: |
67616fed | 3395 | return snd_pcm_forward_ioctl(substream, arg); |
1da177e4 | 3396 | } |
67616fed TI |
3397 | pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd); |
3398 | return -ENOTTY; | |
1da177e4 LT |
3399 | } |
3400 | ||
67616fed TI |
3401 | static long snd_pcm_ioctl(struct file *file, unsigned int cmd, |
3402 | unsigned long arg) | |
1da177e4 | 3403 | { |
877211f5 | 3404 | struct snd_pcm_file *pcm_file; |
1da177e4 LT |
3405 | |
3406 | pcm_file = file->private_data; | |
3407 | ||
3408 | if (((cmd >> 8) & 0xff) != 'A') | |
3409 | return -ENOTTY; | |
3410 | ||
67616fed TI |
3411 | return snd_pcm_common_ioctl(file, pcm_file->substream, cmd, |
3412 | (void __user *)arg); | |
1da177e4 LT |
3413 | } |
3414 | ||
c2c86a97 TI |
3415 | /** |
3416 | * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space | |
3417 | * @substream: PCM substream | |
3418 | * @cmd: IOCTL cmd | |
3419 | * @arg: IOCTL argument | |
3420 | * | |
3421 | * The function is provided primarily for OSS layer and USB gadget drivers, | |
3422 | * and it allows only the limited set of ioctls (hw_params, sw_params, | |
3423 | * prepare, start, drain, drop, forward). | |
4e2b7067 TI |
3424 | * |
3425 | * Return: zero if successful, or a negative error code | |
c2c86a97 | 3426 | */ |
bf1bbb5a TI |
3427 | int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, |
3428 | unsigned int cmd, void *arg) | |
1da177e4 | 3429 | { |
c2c86a97 TI |
3430 | snd_pcm_uframes_t *frames = arg; |
3431 | snd_pcm_sframes_t result; | |
1da177e4 | 3432 | |
f0061c18 | 3433 | if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) |
36df2427 TI |
3434 | return -EBADFD; |
3435 | ||
c2c86a97 TI |
3436 | switch (cmd) { |
3437 | case SNDRV_PCM_IOCTL_FORWARD: | |
3438 | { | |
3439 | /* provided only for OSS; capture-only and no value returned */ | |
3440 | if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) | |
3441 | return -EINVAL; | |
763e5067 | 3442 | result = snd_pcm_forward(substream, *frames); |
c2c86a97 TI |
3443 | return result < 0 ? result : 0; |
3444 | } | |
3445 | case SNDRV_PCM_IOCTL_HW_PARAMS: | |
3446 | return snd_pcm_hw_params(substream, arg); | |
3447 | case SNDRV_PCM_IOCTL_SW_PARAMS: | |
3448 | return snd_pcm_sw_params(substream, arg); | |
3449 | case SNDRV_PCM_IOCTL_PREPARE: | |
3450 | return snd_pcm_prepare(substream, NULL); | |
3451 | case SNDRV_PCM_IOCTL_START: | |
3452 | return snd_pcm_start_lock_irq(substream); | |
3453 | case SNDRV_PCM_IOCTL_DRAIN: | |
7d8e8292 | 3454 | return snd_pcm_drain(substream, NULL); |
c2c86a97 TI |
3455 | case SNDRV_PCM_IOCTL_DROP: |
3456 | return snd_pcm_drop(substream); | |
3457 | case SNDRV_PCM_IOCTL_DELAY: | |
912e4c33 | 3458 | return snd_pcm_delay(substream, frames); |
1da177e4 | 3459 | default: |
c2c86a97 | 3460 | return -EINVAL; |
1da177e4 LT |
3461 | } |
3462 | } | |
e88e8ae6 TI |
3463 | EXPORT_SYMBOL(snd_pcm_kernel_ioctl); |
3464 | ||
877211f5 TI |
3465 | static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, |
3466 | loff_t * offset) | |
1da177e4 | 3467 | { |
877211f5 TI |
3468 | struct snd_pcm_file *pcm_file; |
3469 | struct snd_pcm_substream *substream; | |
3470 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3471 | snd_pcm_sframes_t result; |
3472 | ||
3473 | pcm_file = file->private_data; | |
3474 | substream = pcm_file->substream; | |
7eaa943c TI |
3475 | if (PCM_RUNTIME_CHECK(substream)) |
3476 | return -ENXIO; | |
1da177e4 | 3477 | runtime = substream->runtime; |
f0061c18 TI |
3478 | if (runtime->state == SNDRV_PCM_STATE_OPEN || |
3479 | runtime->state == SNDRV_PCM_STATE_DISCONNECTED) | |
1da177e4 LT |
3480 | return -EBADFD; |
3481 | if (!frame_aligned(runtime, count)) | |
3482 | return -EINVAL; | |
3483 | count = bytes_to_frames(runtime, count); | |
3484 | result = snd_pcm_lib_read(substream, buf, count); | |
3485 | if (result > 0) | |
3486 | result = frames_to_bytes(runtime, result); | |
3487 | return result; | |
3488 | } | |
3489 | ||
877211f5 TI |
3490 | static ssize_t snd_pcm_write(struct file *file, const char __user *buf, |
3491 | size_t count, loff_t * offset) | |
1da177e4 | 3492 | { |
877211f5 TI |
3493 | struct snd_pcm_file *pcm_file; |
3494 | struct snd_pcm_substream *substream; | |
3495 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3496 | snd_pcm_sframes_t result; |
3497 | ||
3498 | pcm_file = file->private_data; | |
3499 | substream = pcm_file->substream; | |
7eaa943c TI |
3500 | if (PCM_RUNTIME_CHECK(substream)) |
3501 | return -ENXIO; | |
1da177e4 | 3502 | runtime = substream->runtime; |
f0061c18 TI |
3503 | if (runtime->state == SNDRV_PCM_STATE_OPEN || |
3504 | runtime->state == SNDRV_PCM_STATE_DISCONNECTED) | |
7eaa943c TI |
3505 | return -EBADFD; |
3506 | if (!frame_aligned(runtime, count)) | |
3507 | return -EINVAL; | |
1da177e4 LT |
3508 | count = bytes_to_frames(runtime, count); |
3509 | result = snd_pcm_lib_write(substream, buf, count); | |
3510 | if (result > 0) | |
3511 | result = frames_to_bytes(runtime, result); | |
1da177e4 LT |
3512 | return result; |
3513 | } | |
3514 | ||
1c65d986 | 3515 | static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to) |
1da177e4 | 3516 | { |
877211f5 TI |
3517 | struct snd_pcm_file *pcm_file; |
3518 | struct snd_pcm_substream *substream; | |
3519 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3520 | snd_pcm_sframes_t result; |
3521 | unsigned long i; | |
ae921398 | 3522 | void __user **bufs __free(kfree) = NULL; |
1da177e4 | 3523 | snd_pcm_uframes_t frames; |
de4f5fed | 3524 | const struct iovec *iov = iter_iov(to); |
1da177e4 | 3525 | |
ee0b3e67 | 3526 | pcm_file = iocb->ki_filp->private_data; |
1da177e4 | 3527 | substream = pcm_file->substream; |
7eaa943c TI |
3528 | if (PCM_RUNTIME_CHECK(substream)) |
3529 | return -ENXIO; | |
1da177e4 | 3530 | runtime = substream->runtime; |
f0061c18 TI |
3531 | if (runtime->state == SNDRV_PCM_STATE_OPEN || |
3532 | runtime->state == SNDRV_PCM_STATE_DISCONNECTED) | |
1da177e4 | 3533 | return -EBADFD; |
1fcb7128 | 3534 | if (!user_backed_iter(to)) |
1c65d986 AV |
3535 | return -EINVAL; |
3536 | if (to->nr_segs > 1024 || to->nr_segs != runtime->channels) | |
1da177e4 | 3537 | return -EINVAL; |
de4f5fed | 3538 | if (!frame_aligned(runtime, iov->iov_len)) |
1da177e4 | 3539 | return -EINVAL; |
de4f5fed | 3540 | frames = bytes_to_samples(runtime, iov->iov_len); |
6da2ec56 | 3541 | bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL); |
1da177e4 LT |
3542 | if (bufs == NULL) |
3543 | return -ENOMEM; | |
de4f5fed JA |
3544 | for (i = 0; i < to->nr_segs; ++i) { |
3545 | bufs[i] = iov->iov_base; | |
3546 | iov++; | |
3547 | } | |
1da177e4 LT |
3548 | result = snd_pcm_lib_readv(substream, bufs, frames); |
3549 | if (result > 0) | |
3550 | result = frames_to_bytes(runtime, result); | |
1da177e4 LT |
3551 | return result; |
3552 | } | |
3553 | ||
1c65d986 | 3554 | static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from) |
1da177e4 | 3555 | { |
877211f5 TI |
3556 | struct snd_pcm_file *pcm_file; |
3557 | struct snd_pcm_substream *substream; | |
3558 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3559 | snd_pcm_sframes_t result; |
3560 | unsigned long i; | |
ae921398 | 3561 | void __user **bufs __free(kfree) = NULL; |
1da177e4 | 3562 | snd_pcm_uframes_t frames; |
de4f5fed | 3563 | const struct iovec *iov = iter_iov(from); |
1da177e4 | 3564 | |
ee0b3e67 | 3565 | pcm_file = iocb->ki_filp->private_data; |
1da177e4 | 3566 | substream = pcm_file->substream; |
7eaa943c TI |
3567 | if (PCM_RUNTIME_CHECK(substream)) |
3568 | return -ENXIO; | |
1da177e4 | 3569 | runtime = substream->runtime; |
f0061c18 TI |
3570 | if (runtime->state == SNDRV_PCM_STATE_OPEN || |
3571 | runtime->state == SNDRV_PCM_STATE_DISCONNECTED) | |
7eaa943c | 3572 | return -EBADFD; |
1fcb7128 | 3573 | if (!user_backed_iter(from)) |
1c65d986 AV |
3574 | return -EINVAL; |
3575 | if (from->nr_segs > 128 || from->nr_segs != runtime->channels || | |
de4f5fed | 3576 | !frame_aligned(runtime, iov->iov_len)) |
7eaa943c | 3577 | return -EINVAL; |
de4f5fed | 3578 | frames = bytes_to_samples(runtime, iov->iov_len); |
6da2ec56 | 3579 | bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL); |
1da177e4 LT |
3580 | if (bufs == NULL) |
3581 | return -ENOMEM; | |
de4f5fed JA |
3582 | for (i = 0; i < from->nr_segs; ++i) { |
3583 | bufs[i] = iov->iov_base; | |
3584 | iov++; | |
3585 | } | |
1da177e4 LT |
3586 | result = snd_pcm_lib_writev(substream, bufs, frames); |
3587 | if (result > 0) | |
3588 | result = frames_to_bytes(runtime, result); | |
1da177e4 LT |
3589 | return result; |
3590 | } | |
3591 | ||
6448fcba | 3592 | static __poll_t snd_pcm_poll(struct file *file, poll_table *wait) |
1da177e4 | 3593 | { |
877211f5 TI |
3594 | struct snd_pcm_file *pcm_file; |
3595 | struct snd_pcm_substream *substream; | |
3596 | struct snd_pcm_runtime *runtime; | |
6448fcba | 3597 | __poll_t mask, ok; |
1da177e4 LT |
3598 | snd_pcm_uframes_t avail; |
3599 | ||
3600 | pcm_file = file->private_data; | |
3601 | ||
3602 | substream = pcm_file->substream; | |
6448fcba TI |
3603 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
3604 | ok = EPOLLOUT | EPOLLWRNORM; | |
3605 | else | |
3606 | ok = EPOLLIN | EPOLLRDNORM; | |
7eaa943c | 3607 | if (PCM_RUNTIME_CHECK(substream)) |
6448fcba | 3608 | return ok | EPOLLERR; |
1da177e4 | 3609 | |
1da177e4 | 3610 | runtime = substream->runtime; |
f0061c18 | 3611 | if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) |
36df2427 TI |
3612 | return ok | EPOLLERR; |
3613 | ||
1da177e4 LT |
3614 | poll_wait(file, &runtime->sleep, wait); |
3615 | ||
6448fcba | 3616 | mask = 0; |
650224fe | 3617 | guard(pcm_stream_lock_irq)(substream); |
6448fcba | 3618 | avail = snd_pcm_avail(substream); |
f0061c18 | 3619 | switch (runtime->state) { |
1da177e4 LT |
3620 | case SNDRV_PCM_STATE_RUNNING: |
3621 | case SNDRV_PCM_STATE_PREPARED: | |
3622 | case SNDRV_PCM_STATE_PAUSED: | |
6448fcba TI |
3623 | if (avail >= runtime->control->avail_min) |
3624 | mask = ok; | |
1da177e4 LT |
3625 | break; |
3626 | case SNDRV_PCM_STATE_DRAINING: | |
6448fcba TI |
3627 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
3628 | mask = ok; | |
3629 | if (!avail) | |
3630 | mask |= EPOLLERR; | |
1da177e4 | 3631 | } |
6448fcba | 3632 | break; |
1da177e4 | 3633 | default: |
6448fcba | 3634 | mask = ok | EPOLLERR; |
1da177e4 LT |
3635 | break; |
3636 | } | |
1da177e4 LT |
3637 | return mask; |
3638 | } | |
3639 | ||
3640 | /* | |
3641 | * mmap support | |
3642 | */ | |
3643 | ||
3644 | /* | |
3645 | * Only on coherent architectures, we can mmap the status and the control records | |
3646 | * for effcient data transfer. On others, we have to use HWSYNC ioctl... | |
3647 | */ | |
3648 | #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA) | |
3649 | /* | |
3650 | * mmap status record | |
3651 | */ | |
41412fe9 | 3652 | static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf) |
1da177e4 | 3653 | { |
11bac800 | 3654 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
877211f5 | 3655 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3656 | |
3657 | if (substream == NULL) | |
3ad5afcd | 3658 | return VM_FAULT_SIGBUS; |
1da177e4 | 3659 | runtime = substream->runtime; |
3ad5afcd NP |
3660 | vmf->page = virt_to_page(runtime->status); |
3661 | get_page(vmf->page); | |
3662 | return 0; | |
1da177e4 LT |
3663 | } |
3664 | ||
f0f37e2f | 3665 | static const struct vm_operations_struct snd_pcm_vm_ops_status = |
1da177e4 | 3666 | { |
3ad5afcd | 3667 | .fault = snd_pcm_mmap_status_fault, |
1da177e4 LT |
3668 | }; |
3669 | ||
877211f5 | 3670 | static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3671 | struct vm_area_struct *area) |
3672 | { | |
1da177e4 LT |
3673 | long size; |
3674 | if (!(area->vm_flags & VM_READ)) | |
3675 | return -EINVAL; | |
1da177e4 | 3676 | size = area->vm_end - area->vm_start; |
877211f5 | 3677 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status))) |
1da177e4 LT |
3678 | return -EINVAL; |
3679 | area->vm_ops = &snd_pcm_vm_ops_status; | |
3680 | area->vm_private_data = substream; | |
1c71222e SB |
3681 | vm_flags_mod(area, VM_DONTEXPAND | VM_DONTDUMP, |
3682 | VM_WRITE | VM_MAYWRITE); | |
3683 | ||
1da177e4 LT |
3684 | return 0; |
3685 | } | |
3686 | ||
3687 | /* | |
3688 | * mmap control record | |
3689 | */ | |
41412fe9 | 3690 | static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf) |
1da177e4 | 3691 | { |
11bac800 | 3692 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
877211f5 | 3693 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3694 | |
3695 | if (substream == NULL) | |
3ad5afcd | 3696 | return VM_FAULT_SIGBUS; |
1da177e4 | 3697 | runtime = substream->runtime; |
3ad5afcd NP |
3698 | vmf->page = virt_to_page(runtime->control); |
3699 | get_page(vmf->page); | |
3700 | return 0; | |
1da177e4 LT |
3701 | } |
3702 | ||
f0f37e2f | 3703 | static const struct vm_operations_struct snd_pcm_vm_ops_control = |
1da177e4 | 3704 | { |
3ad5afcd | 3705 | .fault = snd_pcm_mmap_control_fault, |
1da177e4 LT |
3706 | }; |
3707 | ||
877211f5 | 3708 | static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3709 | struct vm_area_struct *area) |
3710 | { | |
1da177e4 LT |
3711 | long size; |
3712 | if (!(area->vm_flags & VM_READ)) | |
3713 | return -EINVAL; | |
1da177e4 | 3714 | size = area->vm_end - area->vm_start; |
877211f5 | 3715 | if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control))) |
1da177e4 LT |
3716 | return -EINVAL; |
3717 | area->vm_ops = &snd_pcm_vm_ops_control; | |
3718 | area->vm_private_data = substream; | |
1c71222e | 3719 | vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP); |
1da177e4 LT |
3720 | return 0; |
3721 | } | |
42f94597 TI |
3722 | |
3723 | static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file) | |
3724 | { | |
81be1093 TI |
3725 | /* If drivers require the explicit sync (typically for non-coherent |
3726 | * pages), we have to disable the mmap of status and control data | |
3727 | * to enforce the control via SYNC_PTR ioctl. | |
3728 | */ | |
3729 | if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC) | |
3730 | return false; | |
b602aa8e TI |
3731 | /* See pcm_control_mmap_allowed() below. |
3732 | * Since older alsa-lib requires both status and control mmaps to be | |
3733 | * coupled, we have to disable the status mmap for old alsa-lib, too. | |
3734 | */ | |
3735 | if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) && | |
3736 | (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)) | |
3737 | return false; | |
3738 | return true; | |
3739 | } | |
3740 | ||
3741 | static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file) | |
3742 | { | |
3743 | if (pcm_file->no_compat_mmap) | |
3744 | return false; | |
81be1093 TI |
3745 | /* see above */ |
3746 | if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC) | |
3747 | return false; | |
b602aa8e | 3748 | /* Disallow the control mmap when SYNC_APPLPTR flag is set; |
42f94597 TI |
3749 | * it enforces the user-space to fall back to snd_pcm_sync_ptr(), |
3750 | * thus it effectively assures the manual update of appl_ptr. | |
42f94597 TI |
3751 | */ |
3752 | if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR) | |
3753 | return false; | |
3754 | return true; | |
3755 | } | |
3756 | ||
1da177e4 LT |
3757 | #else /* ! coherent mmap */ |
3758 | /* | |
3759 | * don't support mmap for status and control records. | |
3760 | */ | |
42f94597 | 3761 | #define pcm_status_mmap_allowed(pcm_file) false |
b602aa8e | 3762 | #define pcm_control_mmap_allowed(pcm_file) false |
42f94597 | 3763 | |
877211f5 | 3764 | static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3765 | struct vm_area_struct *area) |
3766 | { | |
3767 | return -ENXIO; | |
3768 | } | |
877211f5 | 3769 | static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3770 | struct vm_area_struct *area) |
3771 | { | |
3772 | return -ENXIO; | |
3773 | } | |
3774 | #endif /* coherent mmap */ | |
3775 | ||
f69c2861 TI |
3776 | /* |
3777 | * snd_pcm_mmap_data_open - increase the mmap counter | |
3778 | */ | |
3779 | static void snd_pcm_mmap_data_open(struct vm_area_struct *area) | |
3780 | { | |
3781 | struct snd_pcm_substream *substream = area->vm_private_data; | |
3782 | ||
3783 | atomic_inc(&substream->mmap_count); | |
3784 | } | |
3785 | ||
3786 | /* | |
3787 | * snd_pcm_mmap_data_close - decrease the mmap counter | |
3788 | */ | |
3789 | static void snd_pcm_mmap_data_close(struct vm_area_struct *area) | |
3790 | { | |
3791 | struct snd_pcm_substream *substream = area->vm_private_data; | |
3792 | ||
3793 | atomic_dec(&substream->mmap_count); | |
3794 | } | |
3795 | ||
1da177e4 | 3796 | /* |
3ad5afcd | 3797 | * fault callback for mmapping a RAM page |
1da177e4 | 3798 | */ |
41412fe9 | 3799 | static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf) |
1da177e4 | 3800 | { |
11bac800 | 3801 | struct snd_pcm_substream *substream = vmf->vma->vm_private_data; |
877211f5 | 3802 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3803 | unsigned long offset; |
3804 | struct page * page; | |
1da177e4 LT |
3805 | size_t dma_bytes; |
3806 | ||
3807 | if (substream == NULL) | |
3ad5afcd | 3808 | return VM_FAULT_SIGBUS; |
1da177e4 | 3809 | runtime = substream->runtime; |
3ad5afcd | 3810 | offset = vmf->pgoff << PAGE_SHIFT; |
1da177e4 LT |
3811 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); |
3812 | if (offset > dma_bytes - PAGE_SIZE) | |
3ad5afcd | 3813 | return VM_FAULT_SIGBUS; |
9eb4a067 | 3814 | if (substream->ops->page) |
1da177e4 | 3815 | page = substream->ops->page(substream, offset); |
d2913a07 TI |
3816 | else if (!snd_pcm_get_dma_buf(substream)) { |
3817 | if (WARN_ON_ONCE(!runtime->dma_area)) | |
3818 | return VM_FAULT_SIGBUS; | |
d3715889 | 3819 | page = virt_to_page(runtime->dma_area + offset); |
d2913a07 | 3820 | } else |
37af81c5 | 3821 | page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset); |
9eb4a067 TI |
3822 | if (!page) |
3823 | return VM_FAULT_SIGBUS; | |
b5810039 | 3824 | get_page(page); |
3ad5afcd NP |
3825 | vmf->page = page; |
3826 | return 0; | |
1da177e4 LT |
3827 | } |
3828 | ||
657b1989 TI |
3829 | static const struct vm_operations_struct snd_pcm_vm_ops_data = { |
3830 | .open = snd_pcm_mmap_data_open, | |
3831 | .close = snd_pcm_mmap_data_close, | |
3832 | }; | |
3833 | ||
3834 | static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = { | |
1da177e4 LT |
3835 | .open = snd_pcm_mmap_data_open, |
3836 | .close = snd_pcm_mmap_data_close, | |
3ad5afcd | 3837 | .fault = snd_pcm_mmap_data_fault, |
1da177e4 LT |
3838 | }; |
3839 | ||
3840 | /* | |
3841 | * mmap the DMA buffer on RAM | |
3842 | */ | |
30b771cf TI |
3843 | |
3844 | /** | |
3845 | * snd_pcm_lib_default_mmap - Default PCM data mmap function | |
3846 | * @substream: PCM substream | |
3847 | * @area: VMA | |
3848 | * | |
3849 | * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL, | |
3850 | * this function is invoked implicitly. | |
4e2b7067 TI |
3851 | * |
3852 | * Return: zero if successful, or a negative error code | |
30b771cf | 3853 | */ |
18a2b962 TI |
3854 | int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream, |
3855 | struct vm_area_struct *area) | |
1da177e4 | 3856 | { |
1c71222e | 3857 | vm_flags_set(area, VM_DONTEXPAND | VM_DONTDUMP); |
a202bd1a TI |
3858 | if (!substream->ops->page && |
3859 | !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area)) | |
3860 | return 0; | |
657b1989 TI |
3861 | /* mmap with fault handler */ |
3862 | area->vm_ops = &snd_pcm_vm_ops_data_fault; | |
1da177e4 LT |
3863 | return 0; |
3864 | } | |
18a2b962 | 3865 | EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap); |
1da177e4 LT |
3866 | |
3867 | /* | |
3868 | * mmap the DMA buffer on I/O memory area | |
3869 | */ | |
3870 | #if SNDRV_PCM_INFO_MMAP_IOMEM | |
30b771cf TI |
3871 | /** |
3872 | * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem | |
3873 | * @substream: PCM substream | |
3874 | * @area: VMA | |
3875 | * | |
3876 | * When your hardware uses the iomapped pages as the hardware buffer and | |
3877 | * wants to mmap it, pass this function as mmap pcm_ops. Note that this | |
3878 | * is supposed to work only on limited architectures. | |
4e2b7067 TI |
3879 | * |
3880 | * Return: zero if successful, or a negative error code | |
30b771cf | 3881 | */ |
877211f5 TI |
3882 | int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, |
3883 | struct vm_area_struct *area) | |
1da177e4 | 3884 | { |
3c7f6919 | 3885 | struct snd_pcm_runtime *runtime = substream->runtime; |
1da177e4 | 3886 | |
1da177e4 | 3887 | area->vm_page_prot = pgprot_noncached(area->vm_page_prot); |
0fe09a45 | 3888 | return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes); |
1da177e4 | 3889 | } |
e88e8ae6 | 3890 | EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem); |
1da177e4 LT |
3891 | #endif /* SNDRV_PCM_INFO_MMAP */ |
3892 | ||
3893 | /* | |
3894 | * mmap DMA buffer | |
3895 | */ | |
877211f5 | 3896 | int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, |
1da177e4 LT |
3897 | struct vm_area_struct *area) |
3898 | { | |
877211f5 | 3899 | struct snd_pcm_runtime *runtime; |
1da177e4 LT |
3900 | long size; |
3901 | unsigned long offset; | |
3902 | size_t dma_bytes; | |
657b1989 | 3903 | int err; |
1da177e4 LT |
3904 | |
3905 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
3906 | if (!(area->vm_flags & (VM_WRITE|VM_READ))) | |
3907 | return -EINVAL; | |
3908 | } else { | |
3909 | if (!(area->vm_flags & VM_READ)) | |
3910 | return -EINVAL; | |
3911 | } | |
3912 | runtime = substream->runtime; | |
f0061c18 | 3913 | if (runtime->state == SNDRV_PCM_STATE_OPEN) |
1da177e4 LT |
3914 | return -EBADFD; |
3915 | if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) | |
3916 | return -ENXIO; | |
3917 | if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || | |
3918 | runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) | |
3919 | return -EINVAL; | |
3920 | size = area->vm_end - area->vm_start; | |
3921 | offset = area->vm_pgoff << PAGE_SHIFT; | |
3922 | dma_bytes = PAGE_ALIGN(runtime->dma_bytes); | |
3923 | if ((size_t)size > dma_bytes) | |
3924 | return -EINVAL; | |
3925 | if (offset > dma_bytes - size) | |
3926 | return -EINVAL; | |
3927 | ||
657b1989 TI |
3928 | area->vm_ops = &snd_pcm_vm_ops_data; |
3929 | area->vm_private_data = substream; | |
1da177e4 | 3930 | if (substream->ops->mmap) |
657b1989 | 3931 | err = substream->ops->mmap(substream, area); |
1da177e4 | 3932 | else |
18a2b962 | 3933 | err = snd_pcm_lib_default_mmap(substream, area); |
657b1989 TI |
3934 | if (!err) |
3935 | atomic_inc(&substream->mmap_count); | |
3936 | return err; | |
1da177e4 | 3937 | } |
e88e8ae6 TI |
3938 | EXPORT_SYMBOL(snd_pcm_mmap_data); |
3939 | ||
1da177e4 LT |
3940 | static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area) |
3941 | { | |
877211f5 TI |
3942 | struct snd_pcm_file * pcm_file; |
3943 | struct snd_pcm_substream *substream; | |
1da177e4 LT |
3944 | unsigned long offset; |
3945 | ||
3946 | pcm_file = file->private_data; | |
3947 | substream = pcm_file->substream; | |
7eaa943c TI |
3948 | if (PCM_RUNTIME_CHECK(substream)) |
3949 | return -ENXIO; | |
f0061c18 | 3950 | if (substream->runtime->state == SNDRV_PCM_STATE_DISCONNECTED) |
36df2427 | 3951 | return -EBADFD; |
1da177e4 LT |
3952 | |
3953 | offset = area->vm_pgoff << PAGE_SHIFT; | |
3954 | switch (offset) { | |
80fe7430 AB |
3955 | case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD: |
3956 | if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) | |
3957 | return -ENXIO; | |
c0dbbdad | 3958 | fallthrough; |
80fe7430 | 3959 | case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: |
42f94597 | 3960 | if (!pcm_status_mmap_allowed(pcm_file)) |
1da177e4 LT |
3961 | return -ENXIO; |
3962 | return snd_pcm_mmap_status(substream, file, area); | |
80fe7430 AB |
3963 | case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD: |
3964 | if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT)) | |
3965 | return -ENXIO; | |
c0dbbdad | 3966 | fallthrough; |
80fe7430 | 3967 | case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: |
b602aa8e | 3968 | if (!pcm_control_mmap_allowed(pcm_file)) |
1da177e4 LT |
3969 | return -ENXIO; |
3970 | return snd_pcm_mmap_control(substream, file, area); | |
3971 | default: | |
3972 | return snd_pcm_mmap_data(substream, file, area); | |
3973 | } | |
3974 | return 0; | |
3975 | } | |
3976 | ||
3977 | static int snd_pcm_fasync(int fd, struct file * file, int on) | |
3978 | { | |
877211f5 TI |
3979 | struct snd_pcm_file * pcm_file; |
3980 | struct snd_pcm_substream *substream; | |
3981 | struct snd_pcm_runtime *runtime; | |
1da177e4 LT |
3982 | |
3983 | pcm_file = file->private_data; | |
3984 | substream = pcm_file->substream; | |
7eaa943c | 3985 | if (PCM_RUNTIME_CHECK(substream)) |
d05468b7 | 3986 | return -ENXIO; |
1da177e4 | 3987 | runtime = substream->runtime; |
f0061c18 | 3988 | if (runtime->state == SNDRV_PCM_STATE_DISCONNECTED) |
36df2427 | 3989 | return -EBADFD; |
96b09709 | 3990 | return snd_fasync_helper(fd, file, on, &runtime->fasync); |
1da177e4 LT |
3991 | } |
3992 | ||
3993 | /* | |
3994 | * ioctl32 compat | |
3995 | */ | |
3996 | #ifdef CONFIG_COMPAT | |
3997 | #include "pcm_compat.c" | |
3998 | #else | |
3999 | #define snd_pcm_ioctl_compat NULL | |
4000 | #endif | |
4001 | ||
4002 | /* | |
4003 | * To be removed helpers to keep binary compatibility | |
4004 | */ | |
4005 | ||
59d48582 | 4006 | #ifdef CONFIG_SND_SUPPORT_OLD_API |
1da177e4 LT |
4007 | #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5)) |
4008 | #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5)) | |
4009 | ||
877211f5 TI |
4010 | static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params, |
4011 | struct snd_pcm_hw_params_old *oparams) | |
1da177e4 LT |
4012 | { |
4013 | unsigned int i; | |
4014 | ||
4015 | memset(params, 0, sizeof(*params)); | |
4016 | params->flags = oparams->flags; | |
4017 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) | |
4018 | params->masks[i].bits[0] = oparams->masks[i]; | |
4019 | memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals)); | |
4020 | params->rmask = __OLD_TO_NEW_MASK(oparams->rmask); | |
4021 | params->cmask = __OLD_TO_NEW_MASK(oparams->cmask); | |
4022 | params->info = oparams->info; | |
4023 | params->msbits = oparams->msbits; | |
4024 | params->rate_num = oparams->rate_num; | |
4025 | params->rate_den = oparams->rate_den; | |
4026 | params->fifo_size = oparams->fifo_size; | |
4027 | } | |
4028 | ||
877211f5 TI |
4029 | static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams, |
4030 | struct snd_pcm_hw_params *params) | |
1da177e4 LT |
4031 | { |
4032 | unsigned int i; | |
4033 | ||
4034 | memset(oparams, 0, sizeof(*oparams)); | |
4035 | oparams->flags = params->flags; | |
4036 | for (i = 0; i < ARRAY_SIZE(oparams->masks); i++) | |
4037 | oparams->masks[i] = params->masks[i].bits[0]; | |
4038 | memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals)); | |
4039 | oparams->rmask = __NEW_TO_OLD_MASK(params->rmask); | |
4040 | oparams->cmask = __NEW_TO_OLD_MASK(params->cmask); | |
4041 | oparams->info = params->info; | |
4042 | oparams->msbits = params->msbits; | |
4043 | oparams->rate_num = params->rate_num; | |
4044 | oparams->rate_den = params->rate_den; | |
4045 | oparams->fifo_size = params->fifo_size; | |
4046 | } | |
4047 | ||
877211f5 TI |
4048 | static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream, |
4049 | struct snd_pcm_hw_params_old __user * _oparams) | |
1da177e4 | 4050 | { |
ae921398 TI |
4051 | struct snd_pcm_hw_params *params __free(kfree) = NULL; |
4052 | struct snd_pcm_hw_params_old *oparams __free(kfree) = NULL; | |
1da177e4 LT |
4053 | int err; |
4054 | ||
4055 | params = kmalloc(sizeof(*params), GFP_KERNEL); | |
ef44a1ec LZ |
4056 | if (!params) |
4057 | return -ENOMEM; | |
1da177e4 | 4058 | |
ef44a1ec | 4059 | oparams = memdup_user(_oparams, sizeof(*oparams)); |
ae921398 | 4060 | if (IS_ERR(oparams)) |
40a024b8 | 4061 | return PTR_ERR(oparams); |
1da177e4 LT |
4062 | snd_pcm_hw_convert_from_old_params(params, oparams); |
4063 | err = snd_pcm_hw_refine(substream, params); | |
f74ae15f | 4064 | if (err < 0) |
ae921398 | 4065 | return err; |
ef44a1ec | 4066 | |
f74ae15f TS |
4067 | err = fixup_unreferenced_params(substream, params); |
4068 | if (err < 0) | |
ae921398 | 4069 | return err; |
ef44a1ec | 4070 | |
f74ae15f TS |
4071 | snd_pcm_hw_convert_to_old_params(oparams, params); |
4072 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) | |
ae921398 TI |
4073 | return -EFAULT; |
4074 | return 0; | |
1da177e4 LT |
4075 | } |
4076 | ||
877211f5 TI |
4077 | static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream, |
4078 | struct snd_pcm_hw_params_old __user * _oparams) | |
1da177e4 | 4079 | { |
ae921398 TI |
4080 | struct snd_pcm_hw_params *params __free(kfree) = NULL; |
4081 | struct snd_pcm_hw_params_old *oparams __free(kfree) = NULL; | |
1da177e4 LT |
4082 | int err; |
4083 | ||
4084 | params = kmalloc(sizeof(*params), GFP_KERNEL); | |
ef44a1ec LZ |
4085 | if (!params) |
4086 | return -ENOMEM; | |
4087 | ||
4088 | oparams = memdup_user(_oparams, sizeof(*oparams)); | |
ae921398 | 4089 | if (IS_ERR(oparams)) |
40a024b8 | 4090 | return PTR_ERR(oparams); |
f74ae15f | 4091 | |
1da177e4 LT |
4092 | snd_pcm_hw_convert_from_old_params(params, oparams); |
4093 | err = snd_pcm_hw_params(substream, params); | |
f74ae15f | 4094 | if (err < 0) |
ae921398 | 4095 | return err; |
ef44a1ec | 4096 | |
f74ae15f TS |
4097 | snd_pcm_hw_convert_to_old_params(oparams, params); |
4098 | if (copy_to_user(_oparams, oparams, sizeof(*oparams))) | |
ae921398 TI |
4099 | return -EFAULT; |
4100 | return 0; | |
1da177e4 | 4101 | } |
59d48582 | 4102 | #endif /* CONFIG_SND_SUPPORT_OLD_API */ |
1da177e4 | 4103 | |
7003609b | 4104 | #ifndef CONFIG_MMU |
55c63bd2 DG |
4105 | static unsigned long snd_pcm_get_unmapped_area(struct file *file, |
4106 | unsigned long addr, | |
4107 | unsigned long len, | |
4108 | unsigned long pgoff, | |
4109 | unsigned long flags) | |
4110 | { | |
4111 | struct snd_pcm_file *pcm_file = file->private_data; | |
4112 | struct snd_pcm_substream *substream = pcm_file->substream; | |
4113 | struct snd_pcm_runtime *runtime = substream->runtime; | |
4114 | unsigned long offset = pgoff << PAGE_SHIFT; | |
4115 | ||
4116 | switch (offset) { | |
80fe7430 | 4117 | case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW: |
55c63bd2 | 4118 | return (unsigned long)runtime->status; |
80fe7430 | 4119 | case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW: |
55c63bd2 DG |
4120 | return (unsigned long)runtime->control; |
4121 | default: | |
4122 | return (unsigned long)runtime->dma_area + offset; | |
4123 | } | |
7003609b CC |
4124 | } |
4125 | #else | |
55c63bd2 | 4126 | # define snd_pcm_get_unmapped_area NULL |
7003609b CC |
4127 | #endif |
4128 | ||
1da177e4 LT |
4129 | /* |
4130 | * Register section | |
4131 | */ | |
4132 | ||
9c2e08c5 | 4133 | const struct file_operations snd_pcm_f_ops[2] = { |
1da177e4 | 4134 | { |
2af677fc CL |
4135 | .owner = THIS_MODULE, |
4136 | .write = snd_pcm_write, | |
1c65d986 | 4137 | .write_iter = snd_pcm_writev, |
f87135f5 | 4138 | .open = snd_pcm_playback_open, |
2af677fc | 4139 | .release = snd_pcm_release, |
6448fcba | 4140 | .poll = snd_pcm_poll, |
67616fed | 4141 | .unlocked_ioctl = snd_pcm_ioctl, |
2af677fc CL |
4142 | .compat_ioctl = snd_pcm_ioctl_compat, |
4143 | .mmap = snd_pcm_mmap, | |
4144 | .fasync = snd_pcm_fasync, | |
55c63bd2 | 4145 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
1da177e4 LT |
4146 | }, |
4147 | { | |
2af677fc CL |
4148 | .owner = THIS_MODULE, |
4149 | .read = snd_pcm_read, | |
1c65d986 | 4150 | .read_iter = snd_pcm_readv, |
f87135f5 | 4151 | .open = snd_pcm_capture_open, |
2af677fc | 4152 | .release = snd_pcm_release, |
6448fcba | 4153 | .poll = snd_pcm_poll, |
67616fed | 4154 | .unlocked_ioctl = snd_pcm_ioctl, |
2af677fc CL |
4155 | .compat_ioctl = snd_pcm_ioctl_compat, |
4156 | .mmap = snd_pcm_mmap, | |
4157 | .fasync = snd_pcm_fasync, | |
55c63bd2 | 4158 | .get_unmapped_area = snd_pcm_get_unmapped_area, |
1da177e4 LT |
4159 | } |
4160 | }; |