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