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