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