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