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