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