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