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