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