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