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