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