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