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