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