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