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