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