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