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