ARM: dts: omap5: Add bus_dma_limit for L3 bus
[linux/fpc-iii.git] / sound / core / pcm_native.c
blobdf40d38f6e290f97a28d1f8db76b9f2e0011c173
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5 */
7 #include <linux/mm.h>
8 #include <linux/module.h>
9 #include <linux/file.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/time.h>
13 #include <linux/pm_qos.h>
14 #include <linux/io.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/vmalloc.h>
17 #include <sound/core.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/timer.h>
23 #include <sound/minors.h>
24 #include <linux/uio.h>
25 #include <linux/delay.h>
27 #include "pcm_local.h"
29 #ifdef CONFIG_SND_DEBUG
30 #define CREATE_TRACE_POINTS
31 #include "pcm_param_trace.h"
32 #else
33 #define trace_hw_mask_param_enabled() 0
34 #define trace_hw_interval_param_enabled() 0
35 #define trace_hw_mask_param(substream, type, index, prev, curr)
36 #define trace_hw_interval_param(substream, type, index, prev, curr)
37 #endif
40 * Compatibility
43 struct snd_pcm_hw_params_old {
44 unsigned int flags;
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
49 unsigned int rmask;
50 unsigned int cmask;
51 unsigned int info;
52 unsigned int msbits;
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
67 #endif
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 static DECLARE_RWSEM(snd_pcm_link_rwsem);
76 void snd_pcm_group_init(struct snd_pcm_group *group)
78 spin_lock_init(&group->lock);
79 mutex_init(&group->mutex);
80 INIT_LIST_HEAD(&group->substreams);
81 refcount_set(&group->refs, 1);
84 /* define group lock helpers */
85 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
86 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
87 { \
88 if (nonatomic) \
89 mutex_ ## mutex_action(&group->mutex); \
90 else \
91 spin_ ## action(&group->lock); \
94 DEFINE_PCM_GROUP_LOCK(lock, lock);
95 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
96 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
97 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
99 /**
100 * snd_pcm_stream_lock - Lock the PCM stream
101 * @substream: PCM substream
103 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
104 * flag of the given substream. This also takes the global link rw lock
105 * (or rw sem), too, for avoiding the race with linked streams.
107 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
109 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
111 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
114 * snd_pcm_stream_lock - Unlock the PCM stream
115 * @substream: PCM substream
117 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
119 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
121 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
123 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
126 * snd_pcm_stream_lock_irq - Lock the PCM stream
127 * @substream: PCM substream
129 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
130 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
131 * as snd_pcm_stream_lock().
133 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
135 snd_pcm_group_lock_irq(&substream->self_group,
136 substream->pcm->nonatomic);
138 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
141 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
142 * @substream: PCM substream
144 * This is a counter-part of snd_pcm_stream_lock_irq().
146 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
148 snd_pcm_group_unlock_irq(&substream->self_group,
149 substream->pcm->nonatomic);
151 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
153 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
155 unsigned long flags = 0;
156 if (substream->pcm->nonatomic)
157 mutex_lock(&substream->self_group.mutex);
158 else
159 spin_lock_irqsave(&substream->self_group.lock, flags);
160 return flags;
162 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
165 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
166 * @substream: PCM substream
167 * @flags: irq flags
169 * This is a counter-part of snd_pcm_stream_lock_irqsave().
171 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
172 unsigned long flags)
174 if (substream->pcm->nonatomic)
175 mutex_unlock(&substream->self_group.mutex);
176 else
177 spin_unlock_irqrestore(&substream->self_group.lock, flags);
179 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
181 /* Run PCM ioctl ops */
182 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
183 unsigned cmd, void *arg)
185 if (substream->ops->ioctl)
186 return substream->ops->ioctl(substream, cmd, arg);
187 else
188 return snd_pcm_lib_ioctl(substream, cmd, arg);
191 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
193 struct snd_pcm *pcm = substream->pcm;
194 struct snd_pcm_str *pstr = substream->pstr;
196 memset(info, 0, sizeof(*info));
197 info->card = pcm->card->number;
198 info->device = pcm->device;
199 info->stream = substream->stream;
200 info->subdevice = substream->number;
201 strlcpy(info->id, pcm->id, sizeof(info->id));
202 strlcpy(info->name, pcm->name, sizeof(info->name));
203 info->dev_class = pcm->dev_class;
204 info->dev_subclass = pcm->dev_subclass;
205 info->subdevices_count = pstr->substream_count;
206 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
207 strlcpy(info->subname, substream->name, sizeof(info->subname));
209 return 0;
212 int snd_pcm_info_user(struct snd_pcm_substream *substream,
213 struct snd_pcm_info __user * _info)
215 struct snd_pcm_info *info;
216 int err;
218 info = kmalloc(sizeof(*info), GFP_KERNEL);
219 if (! info)
220 return -ENOMEM;
221 err = snd_pcm_info(substream, info);
222 if (err >= 0) {
223 if (copy_to_user(_info, info, sizeof(*info)))
224 err = -EFAULT;
226 kfree(info);
227 return err;
230 static bool hw_support_mmap(struct snd_pcm_substream *substream)
232 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
233 return false;
235 if (substream->ops->mmap ||
236 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
237 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
238 return true;
240 return dma_can_mmap(substream->dma_buffer.dev.dev);
243 static int constrain_mask_params(struct snd_pcm_substream *substream,
244 struct snd_pcm_hw_params *params)
246 struct snd_pcm_hw_constraints *constrs =
247 &substream->runtime->hw_constraints;
248 struct snd_mask *m;
249 unsigned int k;
250 struct snd_mask old_mask;
251 int changed;
253 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
254 m = hw_param_mask(params, k);
255 if (snd_mask_empty(m))
256 return -EINVAL;
258 /* This parameter is not requested to change by a caller. */
259 if (!(params->rmask & (1 << k)))
260 continue;
262 if (trace_hw_mask_param_enabled())
263 old_mask = *m;
265 changed = snd_mask_refine(m, constrs_mask(constrs, k));
266 if (changed < 0)
267 return changed;
268 if (changed == 0)
269 continue;
271 /* Set corresponding flag so that the caller gets it. */
272 trace_hw_mask_param(substream, k, 0, &old_mask, m);
273 params->cmask |= 1 << k;
276 return 0;
279 static int constrain_interval_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params)
282 struct snd_pcm_hw_constraints *constrs =
283 &substream->runtime->hw_constraints;
284 struct snd_interval *i;
285 unsigned int k;
286 struct snd_interval old_interval;
287 int changed;
289 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
290 i = hw_param_interval(params, k);
291 if (snd_interval_empty(i))
292 return -EINVAL;
294 /* This parameter is not requested to change by a caller. */
295 if (!(params->rmask & (1 << k)))
296 continue;
298 if (trace_hw_interval_param_enabled())
299 old_interval = *i;
301 changed = snd_interval_refine(i, constrs_interval(constrs, k));
302 if (changed < 0)
303 return changed;
304 if (changed == 0)
305 continue;
307 /* Set corresponding flag so that the caller gets it. */
308 trace_hw_interval_param(substream, k, 0, &old_interval, i);
309 params->cmask |= 1 << k;
312 return 0;
315 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
316 struct snd_pcm_hw_params *params)
318 struct snd_pcm_hw_constraints *constrs =
319 &substream->runtime->hw_constraints;
320 unsigned int k;
321 unsigned int *rstamps;
322 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
323 unsigned int stamp;
324 struct snd_pcm_hw_rule *r;
325 unsigned int d;
326 struct snd_mask old_mask;
327 struct snd_interval old_interval;
328 bool again;
329 int changed, err = 0;
332 * Each application of rule has own sequence number.
334 * Each member of 'rstamps' array represents the sequence number of
335 * recent application of corresponding rule.
337 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
338 if (!rstamps)
339 return -ENOMEM;
342 * Each member of 'vstamps' array represents the sequence number of
343 * recent application of rule in which corresponding parameters were
344 * changed.
346 * In initial state, elements corresponding to parameters requested by
347 * a caller is 1. For unrequested parameters, corresponding members
348 * have 0 so that the parameters are never changed anymore.
350 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
351 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
353 /* Due to the above design, actual sequence number starts at 2. */
354 stamp = 2;
355 retry:
356 /* Apply all rules in order. */
357 again = false;
358 for (k = 0; k < constrs->rules_num; k++) {
359 r = &constrs->rules[k];
362 * Check condition bits of this rule. When the rule has
363 * some condition bits, parameter without the bits is
364 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
365 * is an example of the condition bits.
367 if (r->cond && !(r->cond & params->flags))
368 continue;
371 * The 'deps' array includes maximum three dependencies
372 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
373 * member of this array is a sentinel and should be
374 * negative value.
376 * This rule should be processed in this time when dependent
377 * parameters were changed at former applications of the other
378 * rules.
380 for (d = 0; r->deps[d] >= 0; d++) {
381 if (vstamps[r->deps[d]] > rstamps[k])
382 break;
384 if (r->deps[d] < 0)
385 continue;
387 if (trace_hw_mask_param_enabled()) {
388 if (hw_is_mask(r->var))
389 old_mask = *hw_param_mask(params, r->var);
391 if (trace_hw_interval_param_enabled()) {
392 if (hw_is_interval(r->var))
393 old_interval = *hw_param_interval(params, r->var);
396 changed = r->func(params, r);
397 if (changed < 0) {
398 err = changed;
399 goto out;
403 * When the parameter is changed, notify it to the caller
404 * by corresponding returned bit, then preparing for next
405 * iteration.
407 if (changed && r->var >= 0) {
408 if (hw_is_mask(r->var)) {
409 trace_hw_mask_param(substream, r->var,
410 k + 1, &old_mask,
411 hw_param_mask(params, r->var));
413 if (hw_is_interval(r->var)) {
414 trace_hw_interval_param(substream, r->var,
415 k + 1, &old_interval,
416 hw_param_interval(params, r->var));
419 params->cmask |= (1 << r->var);
420 vstamps[r->var] = stamp;
421 again = true;
424 rstamps[k] = stamp++;
427 /* Iterate to evaluate all rules till no parameters are changed. */
428 if (again)
429 goto retry;
431 out:
432 kfree(rstamps);
433 return err;
436 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
437 struct snd_pcm_hw_params *params)
439 const struct snd_interval *i;
440 const struct snd_mask *m;
441 int err;
443 if (!params->msbits) {
444 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
445 if (snd_interval_single(i))
446 params->msbits = snd_interval_value(i);
449 if (!params->rate_den) {
450 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
451 if (snd_interval_single(i)) {
452 params->rate_num = snd_interval_value(i);
453 params->rate_den = 1;
457 if (!params->fifo_size) {
458 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
459 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
460 if (snd_mask_single(m) && snd_interval_single(i)) {
461 err = snd_pcm_ops_ioctl(substream,
462 SNDRV_PCM_IOCTL1_FIFO_SIZE,
463 params);
464 if (err < 0)
465 return err;
469 if (!params->info) {
470 params->info = substream->runtime->hw.info;
471 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
472 SNDRV_PCM_INFO_DRAIN_TRIGGER);
473 if (!hw_support_mmap(substream))
474 params->info &= ~(SNDRV_PCM_INFO_MMAP |
475 SNDRV_PCM_INFO_MMAP_VALID);
478 return 0;
481 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params)
484 int err;
486 params->info = 0;
487 params->fifo_size = 0;
488 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
489 params->msbits = 0;
490 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
491 params->rate_num = 0;
492 params->rate_den = 0;
495 err = constrain_mask_params(substream, params);
496 if (err < 0)
497 return err;
499 err = constrain_interval_params(substream, params);
500 if (err < 0)
501 return err;
503 err = constrain_params_by_rules(substream, params);
504 if (err < 0)
505 return err;
507 params->rmask = 0;
509 return 0;
511 EXPORT_SYMBOL(snd_pcm_hw_refine);
513 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
514 struct snd_pcm_hw_params __user * _params)
516 struct snd_pcm_hw_params *params;
517 int err;
519 params = memdup_user(_params, sizeof(*params));
520 if (IS_ERR(params))
521 return PTR_ERR(params);
523 err = snd_pcm_hw_refine(substream, params);
524 if (err < 0)
525 goto end;
527 err = fixup_unreferenced_params(substream, params);
528 if (err < 0)
529 goto end;
531 if (copy_to_user(_params, params, sizeof(*params)))
532 err = -EFAULT;
533 end:
534 kfree(params);
535 return err;
538 static int period_to_usecs(struct snd_pcm_runtime *runtime)
540 int usecs;
542 if (! runtime->rate)
543 return -1; /* invalid */
545 /* take 75% of period time as the deadline */
546 usecs = (750000 / runtime->rate) * runtime->period_size;
547 usecs += ((750000 % runtime->rate) * runtime->period_size) /
548 runtime->rate;
550 return usecs;
553 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
555 snd_pcm_stream_lock_irq(substream);
556 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
557 substream->runtime->status->state = state;
558 snd_pcm_stream_unlock_irq(substream);
561 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
562 int event)
564 #ifdef CONFIG_SND_PCM_TIMER
565 if (substream->timer)
566 snd_timer_notify(substream->timer, event,
567 &substream->runtime->trigger_tstamp);
568 #endif
571 static void snd_pcm_sync_stop(struct snd_pcm_substream *substream)
573 if (substream->runtime->stop_operating) {
574 substream->runtime->stop_operating = false;
575 if (substream->ops->sync_stop)
576 substream->ops->sync_stop(substream);
577 else if (substream->pcm->card->sync_irq > 0)
578 synchronize_irq(substream->pcm->card->sync_irq);
583 * snd_pcm_hw_param_choose - choose a configuration defined by @params
584 * @pcm: PCM instance
585 * @params: the hw_params instance
587 * Choose one configuration from configuration space defined by @params.
588 * The configuration chosen is that obtained fixing in this order:
589 * first access, first format, first subformat, min channels,
590 * min rate, min period time, max buffer size, min tick time
592 * Return: Zero if successful, or a negative error code on failure.
594 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
595 struct snd_pcm_hw_params *params)
597 static const int vars[] = {
598 SNDRV_PCM_HW_PARAM_ACCESS,
599 SNDRV_PCM_HW_PARAM_FORMAT,
600 SNDRV_PCM_HW_PARAM_SUBFORMAT,
601 SNDRV_PCM_HW_PARAM_CHANNELS,
602 SNDRV_PCM_HW_PARAM_RATE,
603 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
604 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
605 SNDRV_PCM_HW_PARAM_TICK_TIME,
608 const int *v;
609 struct snd_mask old_mask;
610 struct snd_interval old_interval;
611 int changed;
613 for (v = vars; *v != -1; v++) {
614 /* Keep old parameter to trace. */
615 if (trace_hw_mask_param_enabled()) {
616 if (hw_is_mask(*v))
617 old_mask = *hw_param_mask(params, *v);
619 if (trace_hw_interval_param_enabled()) {
620 if (hw_is_interval(*v))
621 old_interval = *hw_param_interval(params, *v);
623 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
624 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
625 else
626 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
627 if (changed < 0)
628 return changed;
629 if (changed == 0)
630 continue;
632 /* Trace the changed parameter. */
633 if (hw_is_mask(*v)) {
634 trace_hw_mask_param(pcm, *v, 0, &old_mask,
635 hw_param_mask(params, *v));
637 if (hw_is_interval(*v)) {
638 trace_hw_interval_param(pcm, *v, 0, &old_interval,
639 hw_param_interval(params, *v));
643 return 0;
646 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
647 struct snd_pcm_hw_params *params)
649 struct snd_pcm_runtime *runtime;
650 int err, usecs;
651 unsigned int bits;
652 snd_pcm_uframes_t frames;
654 if (PCM_RUNTIME_CHECK(substream))
655 return -ENXIO;
656 runtime = substream->runtime;
657 snd_pcm_stream_lock_irq(substream);
658 switch (runtime->status->state) {
659 case SNDRV_PCM_STATE_OPEN:
660 case SNDRV_PCM_STATE_SETUP:
661 case SNDRV_PCM_STATE_PREPARED:
662 break;
663 default:
664 snd_pcm_stream_unlock_irq(substream);
665 return -EBADFD;
667 snd_pcm_stream_unlock_irq(substream);
668 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
669 if (!substream->oss.oss)
670 #endif
671 if (atomic_read(&substream->mmap_count))
672 return -EBADFD;
674 snd_pcm_sync_stop(substream);
676 params->rmask = ~0U;
677 err = snd_pcm_hw_refine(substream, params);
678 if (err < 0)
679 goto _error;
681 err = snd_pcm_hw_params_choose(substream, params);
682 if (err < 0)
683 goto _error;
685 err = fixup_unreferenced_params(substream, params);
686 if (err < 0)
687 goto _error;
689 if (substream->managed_buffer_alloc) {
690 err = snd_pcm_lib_malloc_pages(substream,
691 params_buffer_bytes(params));
692 if (err < 0)
693 goto _error;
694 runtime->buffer_changed = err > 0;
697 if (substream->ops->hw_params != NULL) {
698 err = substream->ops->hw_params(substream, params);
699 if (err < 0)
700 goto _error;
703 runtime->access = params_access(params);
704 runtime->format = params_format(params);
705 runtime->subformat = params_subformat(params);
706 runtime->channels = params_channels(params);
707 runtime->rate = params_rate(params);
708 runtime->period_size = params_period_size(params);
709 runtime->periods = params_periods(params);
710 runtime->buffer_size = params_buffer_size(params);
711 runtime->info = params->info;
712 runtime->rate_num = params->rate_num;
713 runtime->rate_den = params->rate_den;
714 runtime->no_period_wakeup =
715 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
716 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
718 bits = snd_pcm_format_physical_width(runtime->format);
719 runtime->sample_bits = bits;
720 bits *= runtime->channels;
721 runtime->frame_bits = bits;
722 frames = 1;
723 while (bits % 8 != 0) {
724 bits *= 2;
725 frames *= 2;
727 runtime->byte_align = bits / 8;
728 runtime->min_align = frames;
730 /* Default sw params */
731 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
732 runtime->period_step = 1;
733 runtime->control->avail_min = runtime->period_size;
734 runtime->start_threshold = 1;
735 runtime->stop_threshold = runtime->buffer_size;
736 runtime->silence_threshold = 0;
737 runtime->silence_size = 0;
738 runtime->boundary = runtime->buffer_size;
739 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
740 runtime->boundary *= 2;
742 /* clear the buffer for avoiding possible kernel info leaks */
743 if (runtime->dma_area && !substream->ops->copy_user)
744 memset(runtime->dma_area, 0, runtime->dma_bytes);
746 snd_pcm_timer_resolution_change(substream);
747 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
749 if (pm_qos_request_active(&substream->latency_pm_qos_req))
750 pm_qos_remove_request(&substream->latency_pm_qos_req);
751 if ((usecs = period_to_usecs(runtime)) >= 0)
752 pm_qos_add_request(&substream->latency_pm_qos_req,
753 PM_QOS_CPU_DMA_LATENCY, usecs);
754 return 0;
755 _error:
756 /* hardware might be unusable from this time,
757 so we force application to retry to set
758 the correct hardware parameter settings */
759 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
760 if (substream->ops->hw_free != NULL)
761 substream->ops->hw_free(substream);
762 if (substream->managed_buffer_alloc)
763 snd_pcm_lib_free_pages(substream);
764 return err;
767 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
768 struct snd_pcm_hw_params __user * _params)
770 struct snd_pcm_hw_params *params;
771 int err;
773 params = memdup_user(_params, sizeof(*params));
774 if (IS_ERR(params))
775 return PTR_ERR(params);
777 err = snd_pcm_hw_params(substream, params);
778 if (err < 0)
779 goto end;
781 if (copy_to_user(_params, params, sizeof(*params)))
782 err = -EFAULT;
783 end:
784 kfree(params);
785 return err;
788 static int do_hw_free(struct snd_pcm_substream *substream)
790 int result = 0;
792 snd_pcm_sync_stop(substream);
793 if (substream->ops->hw_free)
794 result = substream->ops->hw_free(substream);
795 if (substream->managed_buffer_alloc)
796 snd_pcm_lib_free_pages(substream);
797 return result;
800 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
802 struct snd_pcm_runtime *runtime;
803 int result;
805 if (PCM_RUNTIME_CHECK(substream))
806 return -ENXIO;
807 runtime = substream->runtime;
808 snd_pcm_stream_lock_irq(substream);
809 switch (runtime->status->state) {
810 case SNDRV_PCM_STATE_SETUP:
811 case SNDRV_PCM_STATE_PREPARED:
812 break;
813 default:
814 snd_pcm_stream_unlock_irq(substream);
815 return -EBADFD;
817 snd_pcm_stream_unlock_irq(substream);
818 if (atomic_read(&substream->mmap_count))
819 return -EBADFD;
820 result = do_hw_free(substream);
821 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
822 pm_qos_remove_request(&substream->latency_pm_qos_req);
823 return result;
826 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
827 struct snd_pcm_sw_params *params)
829 struct snd_pcm_runtime *runtime;
830 int err;
832 if (PCM_RUNTIME_CHECK(substream))
833 return -ENXIO;
834 runtime = substream->runtime;
835 snd_pcm_stream_lock_irq(substream);
836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
837 snd_pcm_stream_unlock_irq(substream);
838 return -EBADFD;
840 snd_pcm_stream_unlock_irq(substream);
842 if (params->tstamp_mode < 0 ||
843 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
844 return -EINVAL;
845 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
846 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
847 return -EINVAL;
848 if (params->avail_min == 0)
849 return -EINVAL;
850 if (params->silence_size >= runtime->boundary) {
851 if (params->silence_threshold != 0)
852 return -EINVAL;
853 } else {
854 if (params->silence_size > params->silence_threshold)
855 return -EINVAL;
856 if (params->silence_threshold > runtime->buffer_size)
857 return -EINVAL;
859 err = 0;
860 snd_pcm_stream_lock_irq(substream);
861 runtime->tstamp_mode = params->tstamp_mode;
862 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
863 runtime->tstamp_type = params->tstamp_type;
864 runtime->period_step = params->period_step;
865 runtime->control->avail_min = params->avail_min;
866 runtime->start_threshold = params->start_threshold;
867 runtime->stop_threshold = params->stop_threshold;
868 runtime->silence_threshold = params->silence_threshold;
869 runtime->silence_size = params->silence_size;
870 params->boundary = runtime->boundary;
871 if (snd_pcm_running(substream)) {
872 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
873 runtime->silence_size > 0)
874 snd_pcm_playback_silence(substream, ULONG_MAX);
875 err = snd_pcm_update_state(substream, runtime);
877 snd_pcm_stream_unlock_irq(substream);
878 return err;
881 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
882 struct snd_pcm_sw_params __user * _params)
884 struct snd_pcm_sw_params params;
885 int err;
886 if (copy_from_user(&params, _params, sizeof(params)))
887 return -EFAULT;
888 err = snd_pcm_sw_params(substream, &params);
889 if (copy_to_user(_params, &params, sizeof(params)))
890 return -EFAULT;
891 return err;
894 static inline snd_pcm_uframes_t
895 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
897 snd_pcm_uframes_t delay;
899 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
900 delay = snd_pcm_playback_hw_avail(substream->runtime);
901 else
902 delay = snd_pcm_capture_avail(substream->runtime);
903 return delay + substream->runtime->delay;
906 int snd_pcm_status(struct snd_pcm_substream *substream,
907 struct snd_pcm_status *status)
909 struct snd_pcm_runtime *runtime = substream->runtime;
911 snd_pcm_stream_lock_irq(substream);
913 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
914 &runtime->audio_tstamp_config);
916 /* backwards compatible behavior */
917 if (runtime->audio_tstamp_config.type_requested ==
918 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
919 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
920 runtime->audio_tstamp_config.type_requested =
921 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
922 else
923 runtime->audio_tstamp_config.type_requested =
924 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
925 runtime->audio_tstamp_report.valid = 0;
926 } else
927 runtime->audio_tstamp_report.valid = 1;
929 status->state = runtime->status->state;
930 status->suspended_state = runtime->status->suspended_state;
931 if (status->state == SNDRV_PCM_STATE_OPEN)
932 goto _end;
933 status->trigger_tstamp = runtime->trigger_tstamp;
934 if (snd_pcm_running(substream)) {
935 snd_pcm_update_hw_ptr(substream);
936 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
937 status->tstamp = runtime->status->tstamp;
938 status->driver_tstamp = runtime->driver_tstamp;
939 status->audio_tstamp =
940 runtime->status->audio_tstamp;
941 if (runtime->audio_tstamp_report.valid == 1)
942 /* backwards compatibility, no report provided in COMPAT mode */
943 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
944 &status->audio_tstamp_accuracy,
945 &runtime->audio_tstamp_report);
947 goto _tstamp_end;
949 } else {
950 /* get tstamp only in fallback mode and only if enabled */
951 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
952 snd_pcm_gettime(runtime, &status->tstamp);
954 _tstamp_end:
955 status->appl_ptr = runtime->control->appl_ptr;
956 status->hw_ptr = runtime->status->hw_ptr;
957 status->avail = snd_pcm_avail(substream);
958 status->delay = snd_pcm_running(substream) ?
959 snd_pcm_calc_delay(substream) : 0;
960 status->avail_max = runtime->avail_max;
961 status->overrange = runtime->overrange;
962 runtime->avail_max = 0;
963 runtime->overrange = 0;
964 _end:
965 snd_pcm_stream_unlock_irq(substream);
966 return 0;
969 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
970 struct snd_pcm_status __user * _status,
971 bool ext)
973 struct snd_pcm_status status;
974 int res;
976 memset(&status, 0, sizeof(status));
978 * with extension, parameters are read/write,
979 * get audio_tstamp_data from user,
980 * ignore rest of status structure
982 if (ext && get_user(status.audio_tstamp_data,
983 (u32 __user *)(&_status->audio_tstamp_data)))
984 return -EFAULT;
985 res = snd_pcm_status(substream, &status);
986 if (res < 0)
987 return res;
988 if (copy_to_user(_status, &status, sizeof(status)))
989 return -EFAULT;
990 return 0;
993 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
994 struct snd_pcm_channel_info * info)
996 struct snd_pcm_runtime *runtime;
997 unsigned int channel;
999 channel = info->channel;
1000 runtime = substream->runtime;
1001 snd_pcm_stream_lock_irq(substream);
1002 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1003 snd_pcm_stream_unlock_irq(substream);
1004 return -EBADFD;
1006 snd_pcm_stream_unlock_irq(substream);
1007 if (channel >= runtime->channels)
1008 return -EINVAL;
1009 memset(info, 0, sizeof(*info));
1010 info->channel = channel;
1011 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1014 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1015 struct snd_pcm_channel_info __user * _info)
1017 struct snd_pcm_channel_info info;
1018 int res;
1020 if (copy_from_user(&info, _info, sizeof(info)))
1021 return -EFAULT;
1022 res = snd_pcm_channel_info(substream, &info);
1023 if (res < 0)
1024 return res;
1025 if (copy_to_user(_info, &info, sizeof(info)))
1026 return -EFAULT;
1027 return 0;
1030 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1032 struct snd_pcm_runtime *runtime = substream->runtime;
1033 if (runtime->trigger_master == NULL)
1034 return;
1035 if (runtime->trigger_master == substream) {
1036 if (!runtime->trigger_tstamp_latched)
1037 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1038 } else {
1039 snd_pcm_trigger_tstamp(runtime->trigger_master);
1040 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1042 runtime->trigger_master = NULL;
1045 struct action_ops {
1046 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1047 int (*do_action)(struct snd_pcm_substream *substream, int state);
1048 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1049 void (*post_action)(struct snd_pcm_substream *substream, int state);
1053 * this functions is core for handling of linked stream
1054 * Note: the stream state might be changed also on failure
1055 * Note2: call with calling stream lock + link lock
1057 static int snd_pcm_action_group(const struct action_ops *ops,
1058 struct snd_pcm_substream *substream,
1059 int state, int do_lock)
1061 struct snd_pcm_substream *s = NULL;
1062 struct snd_pcm_substream *s1;
1063 int res = 0, depth = 1;
1065 snd_pcm_group_for_each_entry(s, substream) {
1066 if (do_lock && s != substream) {
1067 if (s->pcm->nonatomic)
1068 mutex_lock_nested(&s->self_group.mutex, depth);
1069 else
1070 spin_lock_nested(&s->self_group.lock, depth);
1071 depth++;
1073 res = ops->pre_action(s, state);
1074 if (res < 0)
1075 goto _unlock;
1077 snd_pcm_group_for_each_entry(s, substream) {
1078 res = ops->do_action(s, state);
1079 if (res < 0) {
1080 if (ops->undo_action) {
1081 snd_pcm_group_for_each_entry(s1, substream) {
1082 if (s1 == s) /* failed stream */
1083 break;
1084 ops->undo_action(s1, state);
1087 s = NULL; /* unlock all */
1088 goto _unlock;
1091 snd_pcm_group_for_each_entry(s, substream) {
1092 ops->post_action(s, state);
1094 _unlock:
1095 if (do_lock) {
1096 /* unlock streams */
1097 snd_pcm_group_for_each_entry(s1, substream) {
1098 if (s1 != substream) {
1099 if (s1->pcm->nonatomic)
1100 mutex_unlock(&s1->self_group.mutex);
1101 else
1102 spin_unlock(&s1->self_group.lock);
1104 if (s1 == s) /* end */
1105 break;
1108 return res;
1112 * Note: call with stream lock
1114 static int snd_pcm_action_single(const struct action_ops *ops,
1115 struct snd_pcm_substream *substream,
1116 int state)
1118 int res;
1120 res = ops->pre_action(substream, state);
1121 if (res < 0)
1122 return res;
1123 res = ops->do_action(substream, state);
1124 if (res == 0)
1125 ops->post_action(substream, state);
1126 else if (ops->undo_action)
1127 ops->undo_action(substream, state);
1128 return res;
1131 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1132 struct snd_pcm_group *new_group)
1134 substream->group = new_group;
1135 list_move(&substream->link_list, &new_group->substreams);
1139 * Unref and unlock the group, but keep the stream lock;
1140 * when the group becomes empty and no longer referred, destroy itself
1142 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1143 struct snd_pcm_substream *substream)
1145 bool do_free;
1147 if (!group)
1148 return;
1149 do_free = refcount_dec_and_test(&group->refs);
1150 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1151 if (do_free)
1152 kfree(group);
1156 * Lock the group inside a stream lock and reference it;
1157 * return the locked group object, or NULL if not linked
1159 static struct snd_pcm_group *
1160 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1162 bool nonatomic = substream->pcm->nonatomic;
1163 struct snd_pcm_group *group;
1164 bool trylock;
1166 for (;;) {
1167 if (!snd_pcm_stream_linked(substream))
1168 return NULL;
1169 group = substream->group;
1170 /* block freeing the group object */
1171 refcount_inc(&group->refs);
1173 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1174 spin_trylock(&group->lock);
1175 if (trylock)
1176 break; /* OK */
1178 /* re-lock for avoiding ABBA deadlock */
1179 snd_pcm_stream_unlock(substream);
1180 snd_pcm_group_lock(group, nonatomic);
1181 snd_pcm_stream_lock(substream);
1183 /* check the group again; the above opens a small race window */
1184 if (substream->group == group)
1185 break; /* OK */
1186 /* group changed, try again */
1187 snd_pcm_group_unref(group, substream);
1189 return group;
1193 * Note: call with stream lock
1195 static int snd_pcm_action(const struct action_ops *ops,
1196 struct snd_pcm_substream *substream,
1197 int state)
1199 struct snd_pcm_group *group;
1200 int res;
1202 group = snd_pcm_stream_group_ref(substream);
1203 if (group)
1204 res = snd_pcm_action_group(ops, substream, state, 1);
1205 else
1206 res = snd_pcm_action_single(ops, substream, state);
1207 snd_pcm_group_unref(group, substream);
1208 return res;
1212 * Note: don't use any locks before
1214 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1215 struct snd_pcm_substream *substream,
1216 int state)
1218 int res;
1220 snd_pcm_stream_lock_irq(substream);
1221 res = snd_pcm_action(ops, substream, state);
1222 snd_pcm_stream_unlock_irq(substream);
1223 return res;
1228 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1229 struct snd_pcm_substream *substream,
1230 int state)
1232 int res;
1234 /* Guarantee the group members won't change during non-atomic action */
1235 down_read(&snd_pcm_link_rwsem);
1236 if (snd_pcm_stream_linked(substream))
1237 res = snd_pcm_action_group(ops, substream, state, 0);
1238 else
1239 res = snd_pcm_action_single(ops, substream, state);
1240 up_read(&snd_pcm_link_rwsem);
1241 return res;
1245 * start callbacks
1247 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1249 struct snd_pcm_runtime *runtime = substream->runtime;
1250 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1251 return -EBADFD;
1252 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1253 !snd_pcm_playback_data(substream))
1254 return -EPIPE;
1255 runtime->trigger_tstamp_latched = false;
1256 runtime->trigger_master = substream;
1257 return 0;
1260 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1262 if (substream->runtime->trigger_master != substream)
1263 return 0;
1264 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1267 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1269 if (substream->runtime->trigger_master == substream)
1270 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1273 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1275 struct snd_pcm_runtime *runtime = substream->runtime;
1276 snd_pcm_trigger_tstamp(substream);
1277 runtime->hw_ptr_jiffies = jiffies;
1278 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1279 runtime->rate;
1280 runtime->status->state = state;
1281 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1282 runtime->silence_size > 0)
1283 snd_pcm_playback_silence(substream, ULONG_MAX);
1284 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1287 static const struct action_ops snd_pcm_action_start = {
1288 .pre_action = snd_pcm_pre_start,
1289 .do_action = snd_pcm_do_start,
1290 .undo_action = snd_pcm_undo_start,
1291 .post_action = snd_pcm_post_start
1295 * snd_pcm_start - start all linked streams
1296 * @substream: the PCM substream instance
1298 * Return: Zero if successful, or a negative error code.
1299 * The stream lock must be acquired before calling this function.
1301 int snd_pcm_start(struct snd_pcm_substream *substream)
1303 return snd_pcm_action(&snd_pcm_action_start, substream,
1304 SNDRV_PCM_STATE_RUNNING);
1307 /* take the stream lock and start the streams */
1308 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1310 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1311 SNDRV_PCM_STATE_RUNNING);
1315 * stop callbacks
1317 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1319 struct snd_pcm_runtime *runtime = substream->runtime;
1320 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1321 return -EBADFD;
1322 runtime->trigger_master = substream;
1323 return 0;
1326 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1328 if (substream->runtime->trigger_master == substream &&
1329 snd_pcm_running(substream))
1330 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1331 return 0; /* unconditonally stop all substreams */
1334 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1336 struct snd_pcm_runtime *runtime = substream->runtime;
1337 if (runtime->status->state != state) {
1338 snd_pcm_trigger_tstamp(substream);
1339 runtime->status->state = state;
1340 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1342 runtime->stop_operating = true;
1343 wake_up(&runtime->sleep);
1344 wake_up(&runtime->tsleep);
1347 static const struct action_ops snd_pcm_action_stop = {
1348 .pre_action = snd_pcm_pre_stop,
1349 .do_action = snd_pcm_do_stop,
1350 .post_action = snd_pcm_post_stop
1354 * snd_pcm_stop - try to stop all running streams in the substream group
1355 * @substream: the PCM substream instance
1356 * @state: PCM state after stopping the stream
1358 * The state of each stream is then changed to the given state unconditionally.
1360 * Return: Zero if successful, or a negative error code.
1362 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1364 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1366 EXPORT_SYMBOL(snd_pcm_stop);
1369 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1370 * @substream: the PCM substream
1372 * After stopping, the state is changed to SETUP.
1373 * Unlike snd_pcm_stop(), this affects only the given stream.
1375 * Return: Zero if succesful, or a negative error code.
1377 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1379 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1380 SNDRV_PCM_STATE_SETUP);
1384 * snd_pcm_stop_xrun - stop the running streams as XRUN
1385 * @substream: the PCM substream instance
1387 * This stops the given running substream (and all linked substreams) as XRUN.
1388 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1390 * Return: Zero if successful, or a negative error code.
1392 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1394 unsigned long flags;
1396 snd_pcm_stream_lock_irqsave(substream, flags);
1397 if (substream->runtime && snd_pcm_running(substream))
1398 __snd_pcm_xrun(substream);
1399 snd_pcm_stream_unlock_irqrestore(substream, flags);
1400 return 0;
1402 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1405 * pause callbacks
1407 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1409 struct snd_pcm_runtime *runtime = substream->runtime;
1410 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1411 return -ENOSYS;
1412 if (push) {
1413 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1414 return -EBADFD;
1415 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1416 return -EBADFD;
1417 runtime->trigger_master = substream;
1418 return 0;
1421 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1423 if (substream->runtime->trigger_master != substream)
1424 return 0;
1425 /* some drivers might use hw_ptr to recover from the pause -
1426 update the hw_ptr now */
1427 if (push)
1428 snd_pcm_update_hw_ptr(substream);
1429 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1430 * a delta between the current jiffies, this gives a large enough
1431 * delta, effectively to skip the check once.
1433 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1434 return substream->ops->trigger(substream,
1435 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1436 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1439 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1441 if (substream->runtime->trigger_master == substream)
1442 substream->ops->trigger(substream,
1443 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1444 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1447 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1449 struct snd_pcm_runtime *runtime = substream->runtime;
1450 snd_pcm_trigger_tstamp(substream);
1451 if (push) {
1452 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1453 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1454 wake_up(&runtime->sleep);
1455 wake_up(&runtime->tsleep);
1456 } else {
1457 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1458 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1462 static const struct action_ops snd_pcm_action_pause = {
1463 .pre_action = snd_pcm_pre_pause,
1464 .do_action = snd_pcm_do_pause,
1465 .undo_action = snd_pcm_undo_pause,
1466 .post_action = snd_pcm_post_pause
1470 * Push/release the pause for all linked streams.
1472 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1474 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1477 #ifdef CONFIG_PM
1478 /* suspend */
1480 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1482 struct snd_pcm_runtime *runtime = substream->runtime;
1483 switch (runtime->status->state) {
1484 case SNDRV_PCM_STATE_SUSPENDED:
1485 return -EBUSY;
1486 /* unresumable PCM state; return -EBUSY for skipping suspend */
1487 case SNDRV_PCM_STATE_OPEN:
1488 case SNDRV_PCM_STATE_SETUP:
1489 case SNDRV_PCM_STATE_DISCONNECTED:
1490 return -EBUSY;
1492 runtime->trigger_master = substream;
1493 return 0;
1496 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1498 struct snd_pcm_runtime *runtime = substream->runtime;
1499 if (runtime->trigger_master != substream)
1500 return 0;
1501 if (! snd_pcm_running(substream))
1502 return 0;
1503 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1504 return 0; /* suspend unconditionally */
1507 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1509 struct snd_pcm_runtime *runtime = substream->runtime;
1510 snd_pcm_trigger_tstamp(substream);
1511 runtime->status->suspended_state = runtime->status->state;
1512 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1513 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1514 wake_up(&runtime->sleep);
1515 wake_up(&runtime->tsleep);
1518 static const struct action_ops snd_pcm_action_suspend = {
1519 .pre_action = snd_pcm_pre_suspend,
1520 .do_action = snd_pcm_do_suspend,
1521 .post_action = snd_pcm_post_suspend
1525 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1526 * @substream: the PCM substream
1528 * After this call, all streams are changed to SUSPENDED state.
1530 * Return: Zero if successful, or a negative error code.
1532 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1534 int err;
1535 unsigned long flags;
1537 snd_pcm_stream_lock_irqsave(substream, flags);
1538 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1539 snd_pcm_stream_unlock_irqrestore(substream, flags);
1540 return err;
1544 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1545 * @pcm: the PCM instance
1547 * After this call, all streams are changed to SUSPENDED state.
1549 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1551 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1553 struct snd_pcm_substream *substream;
1554 int stream, err = 0;
1556 if (! pcm)
1557 return 0;
1559 for (stream = 0; stream < 2; stream++) {
1560 for (substream = pcm->streams[stream].substream;
1561 substream; substream = substream->next) {
1562 /* FIXME: the open/close code should lock this as well */
1563 if (substream->runtime == NULL)
1564 continue;
1567 * Skip BE dai link PCM's that are internal and may
1568 * not have their substream ops set.
1570 if (!substream->ops)
1571 continue;
1573 err = snd_pcm_suspend(substream);
1574 if (err < 0 && err != -EBUSY)
1575 return err;
1578 return 0;
1580 EXPORT_SYMBOL(snd_pcm_suspend_all);
1582 /* resume */
1584 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1586 struct snd_pcm_runtime *runtime = substream->runtime;
1587 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1588 return -ENOSYS;
1589 runtime->trigger_master = substream;
1590 return 0;
1593 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1595 struct snd_pcm_runtime *runtime = substream->runtime;
1596 if (runtime->trigger_master != substream)
1597 return 0;
1598 /* DMA not running previously? */
1599 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1600 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1601 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1602 return 0;
1603 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1606 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1608 if (substream->runtime->trigger_master == substream &&
1609 snd_pcm_running(substream))
1610 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1613 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1615 struct snd_pcm_runtime *runtime = substream->runtime;
1616 snd_pcm_trigger_tstamp(substream);
1617 runtime->status->state = runtime->status->suspended_state;
1618 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1619 snd_pcm_sync_stop(substream);
1622 static const struct action_ops snd_pcm_action_resume = {
1623 .pre_action = snd_pcm_pre_resume,
1624 .do_action = snd_pcm_do_resume,
1625 .undo_action = snd_pcm_undo_resume,
1626 .post_action = snd_pcm_post_resume
1629 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1631 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1634 #else
1636 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1638 return -ENOSYS;
1641 #endif /* CONFIG_PM */
1644 * xrun ioctl
1646 * Change the RUNNING stream(s) to XRUN state.
1648 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1650 struct snd_pcm_runtime *runtime = substream->runtime;
1651 int result;
1653 snd_pcm_stream_lock_irq(substream);
1654 switch (runtime->status->state) {
1655 case SNDRV_PCM_STATE_XRUN:
1656 result = 0; /* already there */
1657 break;
1658 case SNDRV_PCM_STATE_RUNNING:
1659 __snd_pcm_xrun(substream);
1660 result = 0;
1661 break;
1662 default:
1663 result = -EBADFD;
1665 snd_pcm_stream_unlock_irq(substream);
1666 return result;
1670 * reset ioctl
1672 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1674 struct snd_pcm_runtime *runtime = substream->runtime;
1675 switch (runtime->status->state) {
1676 case SNDRV_PCM_STATE_RUNNING:
1677 case SNDRV_PCM_STATE_PREPARED:
1678 case SNDRV_PCM_STATE_PAUSED:
1679 case SNDRV_PCM_STATE_SUSPENDED:
1680 return 0;
1681 default:
1682 return -EBADFD;
1686 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1688 struct snd_pcm_runtime *runtime = substream->runtime;
1689 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1690 if (err < 0)
1691 return err;
1692 runtime->hw_ptr_base = 0;
1693 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1694 runtime->status->hw_ptr % runtime->period_size;
1695 runtime->silence_start = runtime->status->hw_ptr;
1696 runtime->silence_filled = 0;
1697 return 0;
1700 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1702 struct snd_pcm_runtime *runtime = substream->runtime;
1703 runtime->control->appl_ptr = runtime->status->hw_ptr;
1704 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1705 runtime->silence_size > 0)
1706 snd_pcm_playback_silence(substream, ULONG_MAX);
1709 static const struct action_ops snd_pcm_action_reset = {
1710 .pre_action = snd_pcm_pre_reset,
1711 .do_action = snd_pcm_do_reset,
1712 .post_action = snd_pcm_post_reset
1715 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1717 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1721 * prepare ioctl
1723 /* we use the second argument for updating f_flags */
1724 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1725 int f_flags)
1727 struct snd_pcm_runtime *runtime = substream->runtime;
1728 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1729 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1730 return -EBADFD;
1731 if (snd_pcm_running(substream))
1732 return -EBUSY;
1733 substream->f_flags = f_flags;
1734 return 0;
1737 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1739 int err;
1740 snd_pcm_sync_stop(substream);
1741 err = substream->ops->prepare(substream);
1742 if (err < 0)
1743 return err;
1744 return snd_pcm_do_reset(substream, 0);
1747 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1749 struct snd_pcm_runtime *runtime = substream->runtime;
1750 runtime->control->appl_ptr = runtime->status->hw_ptr;
1751 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1754 static const struct action_ops snd_pcm_action_prepare = {
1755 .pre_action = snd_pcm_pre_prepare,
1756 .do_action = snd_pcm_do_prepare,
1757 .post_action = snd_pcm_post_prepare
1761 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1762 * @substream: the PCM substream instance
1763 * @file: file to refer f_flags
1765 * Return: Zero if successful, or a negative error code.
1767 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1768 struct file *file)
1770 int f_flags;
1772 if (file)
1773 f_flags = file->f_flags;
1774 else
1775 f_flags = substream->f_flags;
1777 snd_pcm_stream_lock_irq(substream);
1778 switch (substream->runtime->status->state) {
1779 case SNDRV_PCM_STATE_PAUSED:
1780 snd_pcm_pause(substream, 0);
1781 /* fallthru */
1782 case SNDRV_PCM_STATE_SUSPENDED:
1783 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1784 break;
1786 snd_pcm_stream_unlock_irq(substream);
1788 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1789 substream, f_flags);
1793 * drain ioctl
1796 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1798 struct snd_pcm_runtime *runtime = substream->runtime;
1799 switch (runtime->status->state) {
1800 case SNDRV_PCM_STATE_OPEN:
1801 case SNDRV_PCM_STATE_DISCONNECTED:
1802 case SNDRV_PCM_STATE_SUSPENDED:
1803 return -EBADFD;
1805 runtime->trigger_master = substream;
1806 return 0;
1809 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1811 struct snd_pcm_runtime *runtime = substream->runtime;
1812 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1813 switch (runtime->status->state) {
1814 case SNDRV_PCM_STATE_PREPARED:
1815 /* start playback stream if possible */
1816 if (! snd_pcm_playback_empty(substream)) {
1817 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1818 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1819 } else {
1820 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1822 break;
1823 case SNDRV_PCM_STATE_RUNNING:
1824 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1825 break;
1826 case SNDRV_PCM_STATE_XRUN:
1827 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1828 break;
1829 default:
1830 break;
1832 } else {
1833 /* stop running stream */
1834 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1835 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1836 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1837 snd_pcm_do_stop(substream, new_state);
1838 snd_pcm_post_stop(substream, new_state);
1842 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1843 runtime->trigger_master == substream &&
1844 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1845 return substream->ops->trigger(substream,
1846 SNDRV_PCM_TRIGGER_DRAIN);
1848 return 0;
1851 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1855 static const struct action_ops snd_pcm_action_drain_init = {
1856 .pre_action = snd_pcm_pre_drain_init,
1857 .do_action = snd_pcm_do_drain_init,
1858 .post_action = snd_pcm_post_drain_init
1862 * Drain the stream(s).
1863 * When the substream is linked, sync until the draining of all playback streams
1864 * is finished.
1865 * After this call, all streams are supposed to be either SETUP or DRAINING
1866 * (capture only) state.
1868 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1869 struct file *file)
1871 struct snd_card *card;
1872 struct snd_pcm_runtime *runtime;
1873 struct snd_pcm_substream *s;
1874 struct snd_pcm_group *group;
1875 wait_queue_entry_t wait;
1876 int result = 0;
1877 int nonblock = 0;
1879 card = substream->pcm->card;
1880 runtime = substream->runtime;
1882 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1883 return -EBADFD;
1885 if (file) {
1886 if (file->f_flags & O_NONBLOCK)
1887 nonblock = 1;
1888 } else if (substream->f_flags & O_NONBLOCK)
1889 nonblock = 1;
1891 snd_pcm_stream_lock_irq(substream);
1892 /* resume pause */
1893 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1894 snd_pcm_pause(substream, 0);
1896 /* pre-start/stop - all running streams are changed to DRAINING state */
1897 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1898 if (result < 0)
1899 goto unlock;
1900 /* in non-blocking, we don't wait in ioctl but let caller poll */
1901 if (nonblock) {
1902 result = -EAGAIN;
1903 goto unlock;
1906 for (;;) {
1907 long tout;
1908 struct snd_pcm_runtime *to_check;
1909 if (signal_pending(current)) {
1910 result = -ERESTARTSYS;
1911 break;
1913 /* find a substream to drain */
1914 to_check = NULL;
1915 group = snd_pcm_stream_group_ref(substream);
1916 snd_pcm_group_for_each_entry(s, substream) {
1917 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1918 continue;
1919 runtime = s->runtime;
1920 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1921 to_check = runtime;
1922 break;
1925 snd_pcm_group_unref(group, substream);
1926 if (!to_check)
1927 break; /* all drained */
1928 init_waitqueue_entry(&wait, current);
1929 set_current_state(TASK_INTERRUPTIBLE);
1930 add_wait_queue(&to_check->sleep, &wait);
1931 snd_pcm_stream_unlock_irq(substream);
1932 if (runtime->no_period_wakeup)
1933 tout = MAX_SCHEDULE_TIMEOUT;
1934 else {
1935 tout = 10;
1936 if (runtime->rate) {
1937 long t = runtime->period_size * 2 / runtime->rate;
1938 tout = max(t, tout);
1940 tout = msecs_to_jiffies(tout * 1000);
1942 tout = schedule_timeout(tout);
1944 snd_pcm_stream_lock_irq(substream);
1945 group = snd_pcm_stream_group_ref(substream);
1946 snd_pcm_group_for_each_entry(s, substream) {
1947 if (s->runtime == to_check) {
1948 remove_wait_queue(&to_check->sleep, &wait);
1949 break;
1952 snd_pcm_group_unref(group, substream);
1954 if (card->shutdown) {
1955 result = -ENODEV;
1956 break;
1958 if (tout == 0) {
1959 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1960 result = -ESTRPIPE;
1961 else {
1962 dev_dbg(substream->pcm->card->dev,
1963 "playback drain error (DMA or IRQ trouble?)\n");
1964 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1965 result = -EIO;
1967 break;
1971 unlock:
1972 snd_pcm_stream_unlock_irq(substream);
1974 return result;
1978 * drop ioctl
1980 * Immediately put all linked substreams into SETUP state.
1982 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1984 struct snd_pcm_runtime *runtime;
1985 int result = 0;
1987 if (PCM_RUNTIME_CHECK(substream))
1988 return -ENXIO;
1989 runtime = substream->runtime;
1991 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1992 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1993 return -EBADFD;
1995 snd_pcm_stream_lock_irq(substream);
1996 /* resume pause */
1997 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1998 snd_pcm_pause(substream, 0);
2000 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2001 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2002 snd_pcm_stream_unlock_irq(substream);
2004 return result;
2008 static bool is_pcm_file(struct file *file)
2010 struct inode *inode = file_inode(file);
2011 struct snd_pcm *pcm;
2012 unsigned int minor;
2014 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2015 return false;
2016 minor = iminor(inode);
2017 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2018 if (!pcm)
2019 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2020 if (!pcm)
2021 return false;
2022 snd_card_unref(pcm->card);
2023 return true;
2027 * PCM link handling
2029 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2031 int res = 0;
2032 struct snd_pcm_file *pcm_file;
2033 struct snd_pcm_substream *substream1;
2034 struct snd_pcm_group *group, *target_group;
2035 bool nonatomic = substream->pcm->nonatomic;
2036 struct fd f = fdget(fd);
2038 if (!f.file)
2039 return -EBADFD;
2040 if (!is_pcm_file(f.file)) {
2041 res = -EBADFD;
2042 goto _badf;
2044 pcm_file = f.file->private_data;
2045 substream1 = pcm_file->substream;
2046 group = kzalloc(sizeof(*group), GFP_KERNEL);
2047 if (!group) {
2048 res = -ENOMEM;
2049 goto _nolock;
2051 snd_pcm_group_init(group);
2053 down_write(&snd_pcm_link_rwsem);
2054 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2055 substream->runtime->status->state != substream1->runtime->status->state ||
2056 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2057 res = -EBADFD;
2058 goto _end;
2060 if (snd_pcm_stream_linked(substream1)) {
2061 res = -EALREADY;
2062 goto _end;
2065 snd_pcm_stream_lock_irq(substream);
2066 if (!snd_pcm_stream_linked(substream)) {
2067 snd_pcm_group_assign(substream, group);
2068 group = NULL; /* assigned, don't free this one below */
2070 target_group = substream->group;
2071 snd_pcm_stream_unlock_irq(substream);
2073 snd_pcm_group_lock_irq(target_group, nonatomic);
2074 snd_pcm_stream_lock(substream1);
2075 snd_pcm_group_assign(substream1, target_group);
2076 refcount_inc(&target_group->refs);
2077 snd_pcm_stream_unlock(substream1);
2078 snd_pcm_group_unlock_irq(target_group, nonatomic);
2079 _end:
2080 up_write(&snd_pcm_link_rwsem);
2081 _nolock:
2082 kfree(group);
2083 _badf:
2084 fdput(f);
2085 return res;
2088 static void relink_to_local(struct snd_pcm_substream *substream)
2090 snd_pcm_stream_lock(substream);
2091 snd_pcm_group_assign(substream, &substream->self_group);
2092 snd_pcm_stream_unlock(substream);
2095 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2097 struct snd_pcm_group *group;
2098 bool nonatomic = substream->pcm->nonatomic;
2099 bool do_free = false;
2100 int res = 0;
2102 down_write(&snd_pcm_link_rwsem);
2104 if (!snd_pcm_stream_linked(substream)) {
2105 res = -EALREADY;
2106 goto _end;
2109 group = substream->group;
2110 snd_pcm_group_lock_irq(group, nonatomic);
2112 relink_to_local(substream);
2113 refcount_dec(&group->refs);
2115 /* detach the last stream, too */
2116 if (list_is_singular(&group->substreams)) {
2117 relink_to_local(list_first_entry(&group->substreams,
2118 struct snd_pcm_substream,
2119 link_list));
2120 do_free = refcount_dec_and_test(&group->refs);
2123 snd_pcm_group_unlock_irq(group, nonatomic);
2124 if (do_free)
2125 kfree(group);
2127 _end:
2128 up_write(&snd_pcm_link_rwsem);
2129 return res;
2133 * hw configurator
2135 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2136 struct snd_pcm_hw_rule *rule)
2138 struct snd_interval t;
2139 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2140 hw_param_interval_c(params, rule->deps[1]), &t);
2141 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2144 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2145 struct snd_pcm_hw_rule *rule)
2147 struct snd_interval t;
2148 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2149 hw_param_interval_c(params, rule->deps[1]), &t);
2150 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2153 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2154 struct snd_pcm_hw_rule *rule)
2156 struct snd_interval t;
2157 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2158 hw_param_interval_c(params, rule->deps[1]),
2159 (unsigned long) rule->private, &t);
2160 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2163 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2164 struct snd_pcm_hw_rule *rule)
2166 struct snd_interval t;
2167 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2168 (unsigned long) rule->private,
2169 hw_param_interval_c(params, rule->deps[1]), &t);
2170 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2173 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2174 struct snd_pcm_hw_rule *rule)
2176 unsigned int k;
2177 const struct snd_interval *i =
2178 hw_param_interval_c(params, rule->deps[0]);
2179 struct snd_mask m;
2180 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2181 snd_mask_any(&m);
2182 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2183 int bits;
2184 if (! snd_mask_test(mask, k))
2185 continue;
2186 bits = snd_pcm_format_physical_width(k);
2187 if (bits <= 0)
2188 continue; /* ignore invalid formats */
2189 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2190 snd_mask_reset(&m, k);
2192 return snd_mask_refine(mask, &m);
2195 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2196 struct snd_pcm_hw_rule *rule)
2198 struct snd_interval t;
2199 unsigned int k;
2200 t.min = UINT_MAX;
2201 t.max = 0;
2202 t.openmin = 0;
2203 t.openmax = 0;
2204 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2205 int bits;
2206 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2207 continue;
2208 bits = snd_pcm_format_physical_width(k);
2209 if (bits <= 0)
2210 continue; /* ignore invalid formats */
2211 if (t.min > (unsigned)bits)
2212 t.min = bits;
2213 if (t.max < (unsigned)bits)
2214 t.max = bits;
2216 t.integer = 1;
2217 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2220 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2221 #error "Change this table"
2222 #endif
2224 static const unsigned int rates[] = {
2225 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2226 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2229 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2230 .count = ARRAY_SIZE(rates),
2231 .list = rates,
2234 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2235 struct snd_pcm_hw_rule *rule)
2237 struct snd_pcm_hardware *hw = rule->private;
2238 return snd_interval_list(hw_param_interval(params, rule->var),
2239 snd_pcm_known_rates.count,
2240 snd_pcm_known_rates.list, hw->rates);
2243 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2244 struct snd_pcm_hw_rule *rule)
2246 struct snd_interval t;
2247 struct snd_pcm_substream *substream = rule->private;
2248 t.min = 0;
2249 t.max = substream->buffer_bytes_max;
2250 t.openmin = 0;
2251 t.openmax = 0;
2252 t.integer = 1;
2253 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2256 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2258 struct snd_pcm_runtime *runtime = substream->runtime;
2259 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2260 int k, err;
2262 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2263 snd_mask_any(constrs_mask(constrs, k));
2266 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2267 snd_interval_any(constrs_interval(constrs, k));
2270 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2271 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2272 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2273 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2274 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2276 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2277 snd_pcm_hw_rule_format, NULL,
2278 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2279 if (err < 0)
2280 return err;
2281 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2282 snd_pcm_hw_rule_sample_bits, NULL,
2283 SNDRV_PCM_HW_PARAM_FORMAT,
2284 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2285 if (err < 0)
2286 return err;
2287 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2288 snd_pcm_hw_rule_div, NULL,
2289 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2290 if (err < 0)
2291 return err;
2292 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2293 snd_pcm_hw_rule_mul, NULL,
2294 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2295 if (err < 0)
2296 return err;
2297 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2298 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2299 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2300 if (err < 0)
2301 return err;
2302 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2303 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2304 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2305 if (err < 0)
2306 return err;
2307 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2308 snd_pcm_hw_rule_div, NULL,
2309 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2310 if (err < 0)
2311 return err;
2312 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2313 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2314 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2315 if (err < 0)
2316 return err;
2317 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2318 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2319 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2320 if (err < 0)
2321 return err;
2322 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2323 snd_pcm_hw_rule_div, NULL,
2324 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2325 if (err < 0)
2326 return err;
2327 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2328 snd_pcm_hw_rule_div, NULL,
2329 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2330 if (err < 0)
2331 return err;
2332 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2333 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2334 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2335 if (err < 0)
2336 return err;
2337 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2338 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2339 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2340 if (err < 0)
2341 return err;
2342 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2343 snd_pcm_hw_rule_mul, NULL,
2344 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2345 if (err < 0)
2346 return err;
2347 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2348 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2349 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2350 if (err < 0)
2351 return err;
2352 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2353 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2354 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2355 if (err < 0)
2356 return err;
2357 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2358 snd_pcm_hw_rule_muldivk, (void*) 8,
2359 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2360 if (err < 0)
2361 return err;
2362 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2363 snd_pcm_hw_rule_muldivk, (void*) 8,
2364 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2365 if (err < 0)
2366 return err;
2367 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2368 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2369 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2370 if (err < 0)
2371 return err;
2372 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2373 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2374 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2375 if (err < 0)
2376 return err;
2377 return 0;
2380 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2382 struct snd_pcm_runtime *runtime = substream->runtime;
2383 struct snd_pcm_hardware *hw = &runtime->hw;
2384 int err;
2385 unsigned int mask = 0;
2387 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2388 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2389 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2390 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2391 if (hw_support_mmap(substream)) {
2392 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2393 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2394 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2395 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2396 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2397 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2399 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2400 if (err < 0)
2401 return err;
2403 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2404 if (err < 0)
2405 return err;
2407 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2408 if (err < 0)
2409 return err;
2411 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2412 hw->channels_min, hw->channels_max);
2413 if (err < 0)
2414 return err;
2416 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2417 hw->rate_min, hw->rate_max);
2418 if (err < 0)
2419 return err;
2421 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2422 hw->period_bytes_min, hw->period_bytes_max);
2423 if (err < 0)
2424 return err;
2426 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2427 hw->periods_min, hw->periods_max);
2428 if (err < 0)
2429 return err;
2431 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2432 hw->period_bytes_min, hw->buffer_bytes_max);
2433 if (err < 0)
2434 return err;
2436 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2437 snd_pcm_hw_rule_buffer_bytes_max, substream,
2438 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2439 if (err < 0)
2440 return err;
2442 /* FIXME: remove */
2443 if (runtime->dma_bytes) {
2444 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2445 if (err < 0)
2446 return err;
2449 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2450 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2451 snd_pcm_hw_rule_rate, hw,
2452 SNDRV_PCM_HW_PARAM_RATE, -1);
2453 if (err < 0)
2454 return err;
2457 /* FIXME: this belong to lowlevel */
2458 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2460 return 0;
2463 static void pcm_release_private(struct snd_pcm_substream *substream)
2465 if (snd_pcm_stream_linked(substream))
2466 snd_pcm_unlink(substream);
2469 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2471 substream->ref_count--;
2472 if (substream->ref_count > 0)
2473 return;
2475 snd_pcm_drop(substream);
2476 if (substream->hw_opened) {
2477 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2478 do_hw_free(substream);
2479 substream->ops->close(substream);
2480 substream->hw_opened = 0;
2482 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2483 pm_qos_remove_request(&substream->latency_pm_qos_req);
2484 if (substream->pcm_release) {
2485 substream->pcm_release(substream);
2486 substream->pcm_release = NULL;
2488 snd_pcm_detach_substream(substream);
2490 EXPORT_SYMBOL(snd_pcm_release_substream);
2492 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2493 struct file *file,
2494 struct snd_pcm_substream **rsubstream)
2496 struct snd_pcm_substream *substream;
2497 int err;
2499 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2500 if (err < 0)
2501 return err;
2502 if (substream->ref_count > 1) {
2503 *rsubstream = substream;
2504 return 0;
2507 err = snd_pcm_hw_constraints_init(substream);
2508 if (err < 0) {
2509 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2510 goto error;
2513 if ((err = substream->ops->open(substream)) < 0)
2514 goto error;
2516 substream->hw_opened = 1;
2518 err = snd_pcm_hw_constraints_complete(substream);
2519 if (err < 0) {
2520 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2521 goto error;
2524 *rsubstream = substream;
2525 return 0;
2527 error:
2528 snd_pcm_release_substream(substream);
2529 return err;
2531 EXPORT_SYMBOL(snd_pcm_open_substream);
2533 static int snd_pcm_open_file(struct file *file,
2534 struct snd_pcm *pcm,
2535 int stream)
2537 struct snd_pcm_file *pcm_file;
2538 struct snd_pcm_substream *substream;
2539 int err;
2541 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2542 if (err < 0)
2543 return err;
2545 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2546 if (pcm_file == NULL) {
2547 snd_pcm_release_substream(substream);
2548 return -ENOMEM;
2550 pcm_file->substream = substream;
2551 if (substream->ref_count == 1)
2552 substream->pcm_release = pcm_release_private;
2553 file->private_data = pcm_file;
2555 return 0;
2558 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2560 struct snd_pcm *pcm;
2561 int err = nonseekable_open(inode, file);
2562 if (err < 0)
2563 return err;
2564 pcm = snd_lookup_minor_data(iminor(inode),
2565 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2566 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2567 if (pcm)
2568 snd_card_unref(pcm->card);
2569 return err;
2572 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2574 struct snd_pcm *pcm;
2575 int err = nonseekable_open(inode, file);
2576 if (err < 0)
2577 return err;
2578 pcm = snd_lookup_minor_data(iminor(inode),
2579 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2580 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2581 if (pcm)
2582 snd_card_unref(pcm->card);
2583 return err;
2586 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2588 int err;
2589 wait_queue_entry_t wait;
2591 if (pcm == NULL) {
2592 err = -ENODEV;
2593 goto __error1;
2595 err = snd_card_file_add(pcm->card, file);
2596 if (err < 0)
2597 goto __error1;
2598 if (!try_module_get(pcm->card->module)) {
2599 err = -EFAULT;
2600 goto __error2;
2602 init_waitqueue_entry(&wait, current);
2603 add_wait_queue(&pcm->open_wait, &wait);
2604 mutex_lock(&pcm->open_mutex);
2605 while (1) {
2606 err = snd_pcm_open_file(file, pcm, stream);
2607 if (err >= 0)
2608 break;
2609 if (err == -EAGAIN) {
2610 if (file->f_flags & O_NONBLOCK) {
2611 err = -EBUSY;
2612 break;
2614 } else
2615 break;
2616 set_current_state(TASK_INTERRUPTIBLE);
2617 mutex_unlock(&pcm->open_mutex);
2618 schedule();
2619 mutex_lock(&pcm->open_mutex);
2620 if (pcm->card->shutdown) {
2621 err = -ENODEV;
2622 break;
2624 if (signal_pending(current)) {
2625 err = -ERESTARTSYS;
2626 break;
2629 remove_wait_queue(&pcm->open_wait, &wait);
2630 mutex_unlock(&pcm->open_mutex);
2631 if (err < 0)
2632 goto __error;
2633 return err;
2635 __error:
2636 module_put(pcm->card->module);
2637 __error2:
2638 snd_card_file_remove(pcm->card, file);
2639 __error1:
2640 return err;
2643 static int snd_pcm_release(struct inode *inode, struct file *file)
2645 struct snd_pcm *pcm;
2646 struct snd_pcm_substream *substream;
2647 struct snd_pcm_file *pcm_file;
2649 pcm_file = file->private_data;
2650 substream = pcm_file->substream;
2651 if (snd_BUG_ON(!substream))
2652 return -ENXIO;
2653 pcm = substream->pcm;
2654 mutex_lock(&pcm->open_mutex);
2655 snd_pcm_release_substream(substream);
2656 kfree(pcm_file);
2657 mutex_unlock(&pcm->open_mutex);
2658 wake_up(&pcm->open_wait);
2659 module_put(pcm->card->module);
2660 snd_card_file_remove(pcm->card, file);
2661 return 0;
2664 /* check and update PCM state; return 0 or a negative error
2665 * call this inside PCM lock
2667 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2669 switch (substream->runtime->status->state) {
2670 case SNDRV_PCM_STATE_DRAINING:
2671 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2672 return -EBADFD;
2673 /* Fall through */
2674 case SNDRV_PCM_STATE_RUNNING:
2675 return snd_pcm_update_hw_ptr(substream);
2676 case SNDRV_PCM_STATE_PREPARED:
2677 case SNDRV_PCM_STATE_PAUSED:
2678 return 0;
2679 case SNDRV_PCM_STATE_SUSPENDED:
2680 return -ESTRPIPE;
2681 case SNDRV_PCM_STATE_XRUN:
2682 return -EPIPE;
2683 default:
2684 return -EBADFD;
2688 /* increase the appl_ptr; returns the processed frames or a negative error */
2689 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2690 snd_pcm_uframes_t frames,
2691 snd_pcm_sframes_t avail)
2693 struct snd_pcm_runtime *runtime = substream->runtime;
2694 snd_pcm_sframes_t appl_ptr;
2695 int ret;
2697 if (avail <= 0)
2698 return 0;
2699 if (frames > (snd_pcm_uframes_t)avail)
2700 frames = avail;
2701 appl_ptr = runtime->control->appl_ptr + frames;
2702 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2703 appl_ptr -= runtime->boundary;
2704 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2705 return ret < 0 ? ret : frames;
2708 /* decrease the appl_ptr; returns the processed frames or zero for error */
2709 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2710 snd_pcm_uframes_t frames,
2711 snd_pcm_sframes_t avail)
2713 struct snd_pcm_runtime *runtime = substream->runtime;
2714 snd_pcm_sframes_t appl_ptr;
2715 int ret;
2717 if (avail <= 0)
2718 return 0;
2719 if (frames > (snd_pcm_uframes_t)avail)
2720 frames = avail;
2721 appl_ptr = runtime->control->appl_ptr - frames;
2722 if (appl_ptr < 0)
2723 appl_ptr += runtime->boundary;
2724 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2725 /* NOTE: we return zero for errors because PulseAudio gets depressed
2726 * upon receiving an error from rewind ioctl and stops processing
2727 * any longer. Returning zero means that no rewind is done, so
2728 * it's not absolutely wrong to answer like that.
2730 return ret < 0 ? 0 : frames;
2733 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2734 snd_pcm_uframes_t frames)
2736 snd_pcm_sframes_t ret;
2738 if (frames == 0)
2739 return 0;
2741 snd_pcm_stream_lock_irq(substream);
2742 ret = do_pcm_hwsync(substream);
2743 if (!ret)
2744 ret = rewind_appl_ptr(substream, frames,
2745 snd_pcm_hw_avail(substream));
2746 snd_pcm_stream_unlock_irq(substream);
2747 return ret;
2750 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2751 snd_pcm_uframes_t frames)
2753 snd_pcm_sframes_t ret;
2755 if (frames == 0)
2756 return 0;
2758 snd_pcm_stream_lock_irq(substream);
2759 ret = do_pcm_hwsync(substream);
2760 if (!ret)
2761 ret = forward_appl_ptr(substream, frames,
2762 snd_pcm_avail(substream));
2763 snd_pcm_stream_unlock_irq(substream);
2764 return ret;
2767 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2769 int err;
2771 snd_pcm_stream_lock_irq(substream);
2772 err = do_pcm_hwsync(substream);
2773 snd_pcm_stream_unlock_irq(substream);
2774 return err;
2777 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2778 snd_pcm_sframes_t *delay)
2780 int err;
2781 snd_pcm_sframes_t n = 0;
2783 snd_pcm_stream_lock_irq(substream);
2784 err = do_pcm_hwsync(substream);
2785 if (!err)
2786 n = snd_pcm_calc_delay(substream);
2787 snd_pcm_stream_unlock_irq(substream);
2788 if (!err)
2789 *delay = n;
2790 return err;
2793 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2794 struct snd_pcm_sync_ptr __user *_sync_ptr)
2796 struct snd_pcm_runtime *runtime = substream->runtime;
2797 struct snd_pcm_sync_ptr sync_ptr;
2798 volatile struct snd_pcm_mmap_status *status;
2799 volatile struct snd_pcm_mmap_control *control;
2800 int err;
2802 memset(&sync_ptr, 0, sizeof(sync_ptr));
2803 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2804 return -EFAULT;
2805 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2806 return -EFAULT;
2807 status = runtime->status;
2808 control = runtime->control;
2809 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2810 err = snd_pcm_hwsync(substream);
2811 if (err < 0)
2812 return err;
2814 snd_pcm_stream_lock_irq(substream);
2815 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2816 err = pcm_lib_apply_appl_ptr(substream,
2817 sync_ptr.c.control.appl_ptr);
2818 if (err < 0) {
2819 snd_pcm_stream_unlock_irq(substream);
2820 return err;
2822 } else {
2823 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2825 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2826 control->avail_min = sync_ptr.c.control.avail_min;
2827 else
2828 sync_ptr.c.control.avail_min = control->avail_min;
2829 sync_ptr.s.status.state = status->state;
2830 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2831 sync_ptr.s.status.tstamp = status->tstamp;
2832 sync_ptr.s.status.suspended_state = status->suspended_state;
2833 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2834 snd_pcm_stream_unlock_irq(substream);
2835 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2836 return -EFAULT;
2837 return 0;
2840 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2842 struct snd_pcm_runtime *runtime = substream->runtime;
2843 int arg;
2845 if (get_user(arg, _arg))
2846 return -EFAULT;
2847 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2848 return -EINVAL;
2849 runtime->tstamp_type = arg;
2850 return 0;
2853 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2854 struct snd_xferi __user *_xferi)
2856 struct snd_xferi xferi;
2857 struct snd_pcm_runtime *runtime = substream->runtime;
2858 snd_pcm_sframes_t result;
2860 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2861 return -EBADFD;
2862 if (put_user(0, &_xferi->result))
2863 return -EFAULT;
2864 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2865 return -EFAULT;
2866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2867 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2868 else
2869 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2870 __put_user(result, &_xferi->result);
2871 return result < 0 ? result : 0;
2874 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2875 struct snd_xfern __user *_xfern)
2877 struct snd_xfern xfern;
2878 struct snd_pcm_runtime *runtime = substream->runtime;
2879 void *bufs;
2880 snd_pcm_sframes_t result;
2882 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2883 return -EBADFD;
2884 if (runtime->channels > 128)
2885 return -EINVAL;
2886 if (put_user(0, &_xfern->result))
2887 return -EFAULT;
2888 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2889 return -EFAULT;
2891 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2892 if (IS_ERR(bufs))
2893 return PTR_ERR(bufs);
2894 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2895 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2896 else
2897 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2898 kfree(bufs);
2899 __put_user(result, &_xfern->result);
2900 return result < 0 ? result : 0;
2903 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2904 snd_pcm_uframes_t __user *_frames)
2906 snd_pcm_uframes_t frames;
2907 snd_pcm_sframes_t result;
2909 if (get_user(frames, _frames))
2910 return -EFAULT;
2911 if (put_user(0, _frames))
2912 return -EFAULT;
2913 result = snd_pcm_rewind(substream, frames);
2914 __put_user(result, _frames);
2915 return result < 0 ? result : 0;
2918 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2919 snd_pcm_uframes_t __user *_frames)
2921 snd_pcm_uframes_t frames;
2922 snd_pcm_sframes_t result;
2924 if (get_user(frames, _frames))
2925 return -EFAULT;
2926 if (put_user(0, _frames))
2927 return -EFAULT;
2928 result = snd_pcm_forward(substream, frames);
2929 __put_user(result, _frames);
2930 return result < 0 ? result : 0;
2933 static int snd_pcm_common_ioctl(struct file *file,
2934 struct snd_pcm_substream *substream,
2935 unsigned int cmd, void __user *arg)
2937 struct snd_pcm_file *pcm_file = file->private_data;
2938 int res;
2940 if (PCM_RUNTIME_CHECK(substream))
2941 return -ENXIO;
2943 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2944 if (res < 0)
2945 return res;
2947 switch (cmd) {
2948 case SNDRV_PCM_IOCTL_PVERSION:
2949 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2950 case SNDRV_PCM_IOCTL_INFO:
2951 return snd_pcm_info_user(substream, arg);
2952 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2953 return 0;
2954 case SNDRV_PCM_IOCTL_TTSTAMP:
2955 return snd_pcm_tstamp(substream, arg);
2956 case SNDRV_PCM_IOCTL_USER_PVERSION:
2957 if (get_user(pcm_file->user_pversion,
2958 (unsigned int __user *)arg))
2959 return -EFAULT;
2960 return 0;
2961 case SNDRV_PCM_IOCTL_HW_REFINE:
2962 return snd_pcm_hw_refine_user(substream, arg);
2963 case SNDRV_PCM_IOCTL_HW_PARAMS:
2964 return snd_pcm_hw_params_user(substream, arg);
2965 case SNDRV_PCM_IOCTL_HW_FREE:
2966 return snd_pcm_hw_free(substream);
2967 case SNDRV_PCM_IOCTL_SW_PARAMS:
2968 return snd_pcm_sw_params_user(substream, arg);
2969 case SNDRV_PCM_IOCTL_STATUS:
2970 return snd_pcm_status_user(substream, arg, false);
2971 case SNDRV_PCM_IOCTL_STATUS_EXT:
2972 return snd_pcm_status_user(substream, arg, true);
2973 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2974 return snd_pcm_channel_info_user(substream, arg);
2975 case SNDRV_PCM_IOCTL_PREPARE:
2976 return snd_pcm_prepare(substream, file);
2977 case SNDRV_PCM_IOCTL_RESET:
2978 return snd_pcm_reset(substream);
2979 case SNDRV_PCM_IOCTL_START:
2980 return snd_pcm_start_lock_irq(substream);
2981 case SNDRV_PCM_IOCTL_LINK:
2982 return snd_pcm_link(substream, (int)(unsigned long) arg);
2983 case SNDRV_PCM_IOCTL_UNLINK:
2984 return snd_pcm_unlink(substream);
2985 case SNDRV_PCM_IOCTL_RESUME:
2986 return snd_pcm_resume(substream);
2987 case SNDRV_PCM_IOCTL_XRUN:
2988 return snd_pcm_xrun(substream);
2989 case SNDRV_PCM_IOCTL_HWSYNC:
2990 return snd_pcm_hwsync(substream);
2991 case SNDRV_PCM_IOCTL_DELAY:
2993 snd_pcm_sframes_t delay;
2994 snd_pcm_sframes_t __user *res = arg;
2995 int err;
2997 err = snd_pcm_delay(substream, &delay);
2998 if (err)
2999 return err;
3000 if (put_user(delay, res))
3001 return -EFAULT;
3002 return 0;
3004 case SNDRV_PCM_IOCTL_SYNC_PTR:
3005 return snd_pcm_sync_ptr(substream, arg);
3006 #ifdef CONFIG_SND_SUPPORT_OLD_API
3007 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3008 return snd_pcm_hw_refine_old_user(substream, arg);
3009 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3010 return snd_pcm_hw_params_old_user(substream, arg);
3011 #endif
3012 case SNDRV_PCM_IOCTL_DRAIN:
3013 return snd_pcm_drain(substream, file);
3014 case SNDRV_PCM_IOCTL_DROP:
3015 return snd_pcm_drop(substream);
3016 case SNDRV_PCM_IOCTL_PAUSE:
3017 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3018 substream,
3019 (int)(unsigned long)arg);
3020 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3021 case SNDRV_PCM_IOCTL_READI_FRAMES:
3022 return snd_pcm_xferi_frames_ioctl(substream, arg);
3023 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3024 case SNDRV_PCM_IOCTL_READN_FRAMES:
3025 return snd_pcm_xfern_frames_ioctl(substream, arg);
3026 case SNDRV_PCM_IOCTL_REWIND:
3027 return snd_pcm_rewind_ioctl(substream, arg);
3028 case SNDRV_PCM_IOCTL_FORWARD:
3029 return snd_pcm_forward_ioctl(substream, arg);
3031 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3032 return -ENOTTY;
3035 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3036 unsigned long arg)
3038 struct snd_pcm_file *pcm_file;
3040 pcm_file = file->private_data;
3042 if (((cmd >> 8) & 0xff) != 'A')
3043 return -ENOTTY;
3045 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3046 (void __user *)arg);
3050 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3051 * @substream: PCM substream
3052 * @cmd: IOCTL cmd
3053 * @arg: IOCTL argument
3055 * The function is provided primarily for OSS layer and USB gadget drivers,
3056 * and it allows only the limited set of ioctls (hw_params, sw_params,
3057 * prepare, start, drain, drop, forward).
3059 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3060 unsigned int cmd, void *arg)
3062 snd_pcm_uframes_t *frames = arg;
3063 snd_pcm_sframes_t result;
3065 switch (cmd) {
3066 case SNDRV_PCM_IOCTL_FORWARD:
3068 /* provided only for OSS; capture-only and no value returned */
3069 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3070 return -EINVAL;
3071 result = snd_pcm_forward(substream, *frames);
3072 return result < 0 ? result : 0;
3074 case SNDRV_PCM_IOCTL_HW_PARAMS:
3075 return snd_pcm_hw_params(substream, arg);
3076 case SNDRV_PCM_IOCTL_SW_PARAMS:
3077 return snd_pcm_sw_params(substream, arg);
3078 case SNDRV_PCM_IOCTL_PREPARE:
3079 return snd_pcm_prepare(substream, NULL);
3080 case SNDRV_PCM_IOCTL_START:
3081 return snd_pcm_start_lock_irq(substream);
3082 case SNDRV_PCM_IOCTL_DRAIN:
3083 return snd_pcm_drain(substream, NULL);
3084 case SNDRV_PCM_IOCTL_DROP:
3085 return snd_pcm_drop(substream);
3086 case SNDRV_PCM_IOCTL_DELAY:
3087 return snd_pcm_delay(substream, frames);
3088 default:
3089 return -EINVAL;
3092 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3094 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3095 loff_t * offset)
3097 struct snd_pcm_file *pcm_file;
3098 struct snd_pcm_substream *substream;
3099 struct snd_pcm_runtime *runtime;
3100 snd_pcm_sframes_t result;
3102 pcm_file = file->private_data;
3103 substream = pcm_file->substream;
3104 if (PCM_RUNTIME_CHECK(substream))
3105 return -ENXIO;
3106 runtime = substream->runtime;
3107 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3108 return -EBADFD;
3109 if (!frame_aligned(runtime, count))
3110 return -EINVAL;
3111 count = bytes_to_frames(runtime, count);
3112 result = snd_pcm_lib_read(substream, buf, count);
3113 if (result > 0)
3114 result = frames_to_bytes(runtime, result);
3115 return result;
3118 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3119 size_t count, loff_t * offset)
3121 struct snd_pcm_file *pcm_file;
3122 struct snd_pcm_substream *substream;
3123 struct snd_pcm_runtime *runtime;
3124 snd_pcm_sframes_t result;
3126 pcm_file = file->private_data;
3127 substream = pcm_file->substream;
3128 if (PCM_RUNTIME_CHECK(substream))
3129 return -ENXIO;
3130 runtime = substream->runtime;
3131 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3132 return -EBADFD;
3133 if (!frame_aligned(runtime, count))
3134 return -EINVAL;
3135 count = bytes_to_frames(runtime, count);
3136 result = snd_pcm_lib_write(substream, buf, count);
3137 if (result > 0)
3138 result = frames_to_bytes(runtime, result);
3139 return result;
3142 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3144 struct snd_pcm_file *pcm_file;
3145 struct snd_pcm_substream *substream;
3146 struct snd_pcm_runtime *runtime;
3147 snd_pcm_sframes_t result;
3148 unsigned long i;
3149 void __user **bufs;
3150 snd_pcm_uframes_t frames;
3152 pcm_file = iocb->ki_filp->private_data;
3153 substream = pcm_file->substream;
3154 if (PCM_RUNTIME_CHECK(substream))
3155 return -ENXIO;
3156 runtime = substream->runtime;
3157 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3158 return -EBADFD;
3159 if (!iter_is_iovec(to))
3160 return -EINVAL;
3161 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3162 return -EINVAL;
3163 if (!frame_aligned(runtime, to->iov->iov_len))
3164 return -EINVAL;
3165 frames = bytes_to_samples(runtime, to->iov->iov_len);
3166 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3167 if (bufs == NULL)
3168 return -ENOMEM;
3169 for (i = 0; i < to->nr_segs; ++i)
3170 bufs[i] = to->iov[i].iov_base;
3171 result = snd_pcm_lib_readv(substream, bufs, frames);
3172 if (result > 0)
3173 result = frames_to_bytes(runtime, result);
3174 kfree(bufs);
3175 return result;
3178 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3180 struct snd_pcm_file *pcm_file;
3181 struct snd_pcm_substream *substream;
3182 struct snd_pcm_runtime *runtime;
3183 snd_pcm_sframes_t result;
3184 unsigned long i;
3185 void __user **bufs;
3186 snd_pcm_uframes_t frames;
3188 pcm_file = iocb->ki_filp->private_data;
3189 substream = pcm_file->substream;
3190 if (PCM_RUNTIME_CHECK(substream))
3191 return -ENXIO;
3192 runtime = substream->runtime;
3193 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3194 return -EBADFD;
3195 if (!iter_is_iovec(from))
3196 return -EINVAL;
3197 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3198 !frame_aligned(runtime, from->iov->iov_len))
3199 return -EINVAL;
3200 frames = bytes_to_samples(runtime, from->iov->iov_len);
3201 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3202 if (bufs == NULL)
3203 return -ENOMEM;
3204 for (i = 0; i < from->nr_segs; ++i)
3205 bufs[i] = from->iov[i].iov_base;
3206 result = snd_pcm_lib_writev(substream, bufs, frames);
3207 if (result > 0)
3208 result = frames_to_bytes(runtime, result);
3209 kfree(bufs);
3210 return result;
3213 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3215 struct snd_pcm_file *pcm_file;
3216 struct snd_pcm_substream *substream;
3217 struct snd_pcm_runtime *runtime;
3218 __poll_t mask, ok;
3219 snd_pcm_uframes_t avail;
3221 pcm_file = file->private_data;
3223 substream = pcm_file->substream;
3224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3225 ok = EPOLLOUT | EPOLLWRNORM;
3226 else
3227 ok = EPOLLIN | EPOLLRDNORM;
3228 if (PCM_RUNTIME_CHECK(substream))
3229 return ok | EPOLLERR;
3231 runtime = substream->runtime;
3232 poll_wait(file, &runtime->sleep, wait);
3234 mask = 0;
3235 snd_pcm_stream_lock_irq(substream);
3236 avail = snd_pcm_avail(substream);
3237 switch (runtime->status->state) {
3238 case SNDRV_PCM_STATE_RUNNING:
3239 case SNDRV_PCM_STATE_PREPARED:
3240 case SNDRV_PCM_STATE_PAUSED:
3241 if (avail >= runtime->control->avail_min)
3242 mask = ok;
3243 break;
3244 case SNDRV_PCM_STATE_DRAINING:
3245 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3246 mask = ok;
3247 if (!avail)
3248 mask |= EPOLLERR;
3250 break;
3251 default:
3252 mask = ok | EPOLLERR;
3253 break;
3255 snd_pcm_stream_unlock_irq(substream);
3256 return mask;
3260 * mmap support
3264 * Only on coherent architectures, we can mmap the status and the control records
3265 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3267 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3269 * mmap status record
3271 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3273 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3274 struct snd_pcm_runtime *runtime;
3276 if (substream == NULL)
3277 return VM_FAULT_SIGBUS;
3278 runtime = substream->runtime;
3279 vmf->page = virt_to_page(runtime->status);
3280 get_page(vmf->page);
3281 return 0;
3284 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3286 .fault = snd_pcm_mmap_status_fault,
3289 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3290 struct vm_area_struct *area)
3292 long size;
3293 if (!(area->vm_flags & VM_READ))
3294 return -EINVAL;
3295 size = area->vm_end - area->vm_start;
3296 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3297 return -EINVAL;
3298 area->vm_ops = &snd_pcm_vm_ops_status;
3299 area->vm_private_data = substream;
3300 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3301 return 0;
3305 * mmap control record
3307 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3309 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3310 struct snd_pcm_runtime *runtime;
3312 if (substream == NULL)
3313 return VM_FAULT_SIGBUS;
3314 runtime = substream->runtime;
3315 vmf->page = virt_to_page(runtime->control);
3316 get_page(vmf->page);
3317 return 0;
3320 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3322 .fault = snd_pcm_mmap_control_fault,
3325 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3326 struct vm_area_struct *area)
3328 long size;
3329 if (!(area->vm_flags & VM_READ))
3330 return -EINVAL;
3331 size = area->vm_end - area->vm_start;
3332 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3333 return -EINVAL;
3334 area->vm_ops = &snd_pcm_vm_ops_control;
3335 area->vm_private_data = substream;
3336 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3337 return 0;
3340 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3342 if (pcm_file->no_compat_mmap)
3343 return false;
3344 /* See pcm_control_mmap_allowed() below.
3345 * Since older alsa-lib requires both status and control mmaps to be
3346 * coupled, we have to disable the status mmap for old alsa-lib, too.
3348 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3349 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3350 return false;
3351 return true;
3354 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3356 if (pcm_file->no_compat_mmap)
3357 return false;
3358 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3359 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3360 * thus it effectively assures the manual update of appl_ptr.
3362 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3363 return false;
3364 return true;
3367 #else /* ! coherent mmap */
3369 * don't support mmap for status and control records.
3371 #define pcm_status_mmap_allowed(pcm_file) false
3372 #define pcm_control_mmap_allowed(pcm_file) false
3374 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3375 struct vm_area_struct *area)
3377 return -ENXIO;
3379 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3380 struct vm_area_struct *area)
3382 return -ENXIO;
3384 #endif /* coherent mmap */
3386 static inline struct page *
3387 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3389 void *vaddr = substream->runtime->dma_area + ofs;
3391 switch (substream->dma_buffer.dev.type) {
3392 #ifdef CONFIG_SND_DMA_SGBUF
3393 case SNDRV_DMA_TYPE_DEV_SG:
3394 case SNDRV_DMA_TYPE_DEV_UC_SG:
3395 return snd_pcm_sgbuf_ops_page(substream, ofs);
3396 #endif /* CONFIG_SND_DMA_SGBUF */
3397 case SNDRV_DMA_TYPE_VMALLOC:
3398 return vmalloc_to_page(vaddr);
3399 default:
3400 return virt_to_page(vaddr);
3405 * fault callback for mmapping a RAM page
3407 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3409 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3410 struct snd_pcm_runtime *runtime;
3411 unsigned long offset;
3412 struct page * page;
3413 size_t dma_bytes;
3415 if (substream == NULL)
3416 return VM_FAULT_SIGBUS;
3417 runtime = substream->runtime;
3418 offset = vmf->pgoff << PAGE_SHIFT;
3419 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3420 if (offset > dma_bytes - PAGE_SIZE)
3421 return VM_FAULT_SIGBUS;
3422 if (substream->ops->page)
3423 page = substream->ops->page(substream, offset);
3424 else
3425 page = snd_pcm_default_page_ops(substream, offset);
3426 if (!page)
3427 return VM_FAULT_SIGBUS;
3428 get_page(page);
3429 vmf->page = page;
3430 return 0;
3433 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3434 .open = snd_pcm_mmap_data_open,
3435 .close = snd_pcm_mmap_data_close,
3438 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3439 .open = snd_pcm_mmap_data_open,
3440 .close = snd_pcm_mmap_data_close,
3441 .fault = snd_pcm_mmap_data_fault,
3445 * mmap the DMA buffer on RAM
3449 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3450 * @substream: PCM substream
3451 * @area: VMA
3453 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3454 * this function is invoked implicitly.
3456 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3457 struct vm_area_struct *area)
3459 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3460 #ifdef CONFIG_GENERIC_ALLOCATOR
3461 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3462 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3463 return remap_pfn_range(area, area->vm_start,
3464 substream->dma_buffer.addr >> PAGE_SHIFT,
3465 area->vm_end - area->vm_start, area->vm_page_prot);
3467 #endif /* CONFIG_GENERIC_ALLOCATOR */
3468 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3469 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3470 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3471 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3472 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3473 area,
3474 substream->runtime->dma_area,
3475 substream->runtime->dma_addr,
3476 substream->runtime->dma_bytes);
3477 #endif /* CONFIG_X86 */
3478 /* mmap with fault handler */
3479 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3480 return 0;
3482 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3485 * mmap the DMA buffer on I/O memory area
3487 #if SNDRV_PCM_INFO_MMAP_IOMEM
3489 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3490 * @substream: PCM substream
3491 * @area: VMA
3493 * When your hardware uses the iomapped pages as the hardware buffer and
3494 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3495 * is supposed to work only on limited architectures.
3497 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3498 struct vm_area_struct *area)
3500 struct snd_pcm_runtime *runtime = substream->runtime;
3502 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3503 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3505 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3506 #endif /* SNDRV_PCM_INFO_MMAP */
3509 * mmap DMA buffer
3511 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3512 struct vm_area_struct *area)
3514 struct snd_pcm_runtime *runtime;
3515 long size;
3516 unsigned long offset;
3517 size_t dma_bytes;
3518 int err;
3520 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3521 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3522 return -EINVAL;
3523 } else {
3524 if (!(area->vm_flags & VM_READ))
3525 return -EINVAL;
3527 runtime = substream->runtime;
3528 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3529 return -EBADFD;
3530 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3531 return -ENXIO;
3532 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3533 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3534 return -EINVAL;
3535 size = area->vm_end - area->vm_start;
3536 offset = area->vm_pgoff << PAGE_SHIFT;
3537 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3538 if ((size_t)size > dma_bytes)
3539 return -EINVAL;
3540 if (offset > dma_bytes - size)
3541 return -EINVAL;
3543 area->vm_ops = &snd_pcm_vm_ops_data;
3544 area->vm_private_data = substream;
3545 if (substream->ops->mmap)
3546 err = substream->ops->mmap(substream, area);
3547 else
3548 err = snd_pcm_lib_default_mmap(substream, area);
3549 if (!err)
3550 atomic_inc(&substream->mmap_count);
3551 return err;
3553 EXPORT_SYMBOL(snd_pcm_mmap_data);
3555 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3557 struct snd_pcm_file * pcm_file;
3558 struct snd_pcm_substream *substream;
3559 unsigned long offset;
3561 pcm_file = file->private_data;
3562 substream = pcm_file->substream;
3563 if (PCM_RUNTIME_CHECK(substream))
3564 return -ENXIO;
3566 offset = area->vm_pgoff << PAGE_SHIFT;
3567 switch (offset) {
3568 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3569 if (!pcm_status_mmap_allowed(pcm_file))
3570 return -ENXIO;
3571 return snd_pcm_mmap_status(substream, file, area);
3572 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3573 if (!pcm_control_mmap_allowed(pcm_file))
3574 return -ENXIO;
3575 return snd_pcm_mmap_control(substream, file, area);
3576 default:
3577 return snd_pcm_mmap_data(substream, file, area);
3579 return 0;
3582 static int snd_pcm_fasync(int fd, struct file * file, int on)
3584 struct snd_pcm_file * pcm_file;
3585 struct snd_pcm_substream *substream;
3586 struct snd_pcm_runtime *runtime;
3588 pcm_file = file->private_data;
3589 substream = pcm_file->substream;
3590 if (PCM_RUNTIME_CHECK(substream))
3591 return -ENXIO;
3592 runtime = substream->runtime;
3593 return fasync_helper(fd, file, on, &runtime->fasync);
3597 * ioctl32 compat
3599 #ifdef CONFIG_COMPAT
3600 #include "pcm_compat.c"
3601 #else
3602 #define snd_pcm_ioctl_compat NULL
3603 #endif
3606 * To be removed helpers to keep binary compatibility
3609 #ifdef CONFIG_SND_SUPPORT_OLD_API
3610 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3611 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3613 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3614 struct snd_pcm_hw_params_old *oparams)
3616 unsigned int i;
3618 memset(params, 0, sizeof(*params));
3619 params->flags = oparams->flags;
3620 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3621 params->masks[i].bits[0] = oparams->masks[i];
3622 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3623 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3624 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3625 params->info = oparams->info;
3626 params->msbits = oparams->msbits;
3627 params->rate_num = oparams->rate_num;
3628 params->rate_den = oparams->rate_den;
3629 params->fifo_size = oparams->fifo_size;
3632 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3633 struct snd_pcm_hw_params *params)
3635 unsigned int i;
3637 memset(oparams, 0, sizeof(*oparams));
3638 oparams->flags = params->flags;
3639 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3640 oparams->masks[i] = params->masks[i].bits[0];
3641 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3642 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3643 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3644 oparams->info = params->info;
3645 oparams->msbits = params->msbits;
3646 oparams->rate_num = params->rate_num;
3647 oparams->rate_den = params->rate_den;
3648 oparams->fifo_size = params->fifo_size;
3651 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3652 struct snd_pcm_hw_params_old __user * _oparams)
3654 struct snd_pcm_hw_params *params;
3655 struct snd_pcm_hw_params_old *oparams = NULL;
3656 int err;
3658 params = kmalloc(sizeof(*params), GFP_KERNEL);
3659 if (!params)
3660 return -ENOMEM;
3662 oparams = memdup_user(_oparams, sizeof(*oparams));
3663 if (IS_ERR(oparams)) {
3664 err = PTR_ERR(oparams);
3665 goto out;
3667 snd_pcm_hw_convert_from_old_params(params, oparams);
3668 err = snd_pcm_hw_refine(substream, params);
3669 if (err < 0)
3670 goto out_old;
3672 err = fixup_unreferenced_params(substream, params);
3673 if (err < 0)
3674 goto out_old;
3676 snd_pcm_hw_convert_to_old_params(oparams, params);
3677 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3678 err = -EFAULT;
3679 out_old:
3680 kfree(oparams);
3681 out:
3682 kfree(params);
3683 return err;
3686 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3687 struct snd_pcm_hw_params_old __user * _oparams)
3689 struct snd_pcm_hw_params *params;
3690 struct snd_pcm_hw_params_old *oparams = NULL;
3691 int err;
3693 params = kmalloc(sizeof(*params), GFP_KERNEL);
3694 if (!params)
3695 return -ENOMEM;
3697 oparams = memdup_user(_oparams, sizeof(*oparams));
3698 if (IS_ERR(oparams)) {
3699 err = PTR_ERR(oparams);
3700 goto out;
3703 snd_pcm_hw_convert_from_old_params(params, oparams);
3704 err = snd_pcm_hw_params(substream, params);
3705 if (err < 0)
3706 goto out_old;
3708 snd_pcm_hw_convert_to_old_params(oparams, params);
3709 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3710 err = -EFAULT;
3711 out_old:
3712 kfree(oparams);
3713 out:
3714 kfree(params);
3715 return err;
3717 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3719 #ifndef CONFIG_MMU
3720 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3721 unsigned long addr,
3722 unsigned long len,
3723 unsigned long pgoff,
3724 unsigned long flags)
3726 struct snd_pcm_file *pcm_file = file->private_data;
3727 struct snd_pcm_substream *substream = pcm_file->substream;
3728 struct snd_pcm_runtime *runtime = substream->runtime;
3729 unsigned long offset = pgoff << PAGE_SHIFT;
3731 switch (offset) {
3732 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3733 return (unsigned long)runtime->status;
3734 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3735 return (unsigned long)runtime->control;
3736 default:
3737 return (unsigned long)runtime->dma_area + offset;
3740 #else
3741 # define snd_pcm_get_unmapped_area NULL
3742 #endif
3745 * Register section
3748 const struct file_operations snd_pcm_f_ops[2] = {
3750 .owner = THIS_MODULE,
3751 .write = snd_pcm_write,
3752 .write_iter = snd_pcm_writev,
3753 .open = snd_pcm_playback_open,
3754 .release = snd_pcm_release,
3755 .llseek = no_llseek,
3756 .poll = snd_pcm_poll,
3757 .unlocked_ioctl = snd_pcm_ioctl,
3758 .compat_ioctl = snd_pcm_ioctl_compat,
3759 .mmap = snd_pcm_mmap,
3760 .fasync = snd_pcm_fasync,
3761 .get_unmapped_area = snd_pcm_get_unmapped_area,
3764 .owner = THIS_MODULE,
3765 .read = snd_pcm_read,
3766 .read_iter = snd_pcm_readv,
3767 .open = snd_pcm_capture_open,
3768 .release = snd_pcm_release,
3769 .llseek = no_llseek,
3770 .poll = snd_pcm_poll,
3771 .unlocked_ioctl = snd_pcm_ioctl,
3772 .compat_ioctl = snd_pcm_ioctl_compat,
3773 .mmap = snd_pcm_mmap,
3774 .fasync = snd_pcm_fasync,
3775 .get_unmapped_area = snd_pcm_get_unmapped_area,