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