1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/compat.h>
9 #include <linux/module.h>
10 #include <linux/file.h>
11 #include <linux/slab.h>
12 #include <linux/sched/signal.h>
13 #include <linux/time.h>
14 #include <linux/pm_qos.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/vmalloc.h>
18 #include <sound/core.h>
19 #include <sound/control.h>
20 #include <sound/info.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/timer.h>
24 #include <sound/minors.h>
25 #include <linux/uio.h>
26 #include <linux/delay.h>
28 #include "pcm_local.h"
30 #ifdef CONFIG_SND_DEBUG
31 #define CREATE_TRACE_POINTS
32 #include "pcm_param_trace.h"
34 #define trace_hw_mask_param_enabled() 0
35 #define trace_hw_interval_param_enabled() 0
36 #define trace_hw_mask_param(substream, type, index, prev, curr)
37 #define trace_hw_interval_param(substream, type, index, prev, curr)
44 struct snd_pcm_hw_params_old {
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
54 unsigned int rate_num;
55 unsigned int rate_den;
56 snd_pcm_uframes_t fifo_size;
57 unsigned char reserved[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
69 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem);
77 void snd_pcm_group_init(struct snd_pcm_group *group)
79 spin_lock_init(&group->lock);
80 mutex_init(&group->mutex);
81 INIT_LIST_HEAD(&group->substreams);
82 refcount_set(&group->refs, 1);
85 /* define group lock helpers */
86 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
87 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
90 mutex_ ## mutex_action(&group->mutex); \
92 spin_ ## action(&group->lock); \
95 DEFINE_PCM_GROUP_LOCK(lock, lock);
96 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
97 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
98 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
101 * snd_pcm_stream_lock - Lock the PCM stream
102 * @substream: PCM substream
104 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
105 * flag of the given substream. This also takes the global link rw lock
106 * (or rw sem), too, for avoiding the race with linked streams.
108 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
110 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
112 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
115 * snd_pcm_stream_lock - Unlock the PCM stream
116 * @substream: PCM substream
118 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
120 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
122 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
124 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
127 * snd_pcm_stream_lock_irq - Lock the PCM stream
128 * @substream: PCM substream
130 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
131 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
132 * as snd_pcm_stream_lock().
134 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
136 snd_pcm_group_lock_irq(&substream->self_group,
137 substream->pcm->nonatomic);
139 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
142 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
143 * @substream: PCM substream
145 * This is a counter-part of snd_pcm_stream_lock_irq().
147 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
149 snd_pcm_group_unlock_irq(&substream->self_group,
150 substream->pcm->nonatomic);
152 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
154 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
156 unsigned long flags = 0;
157 if (substream->pcm->nonatomic)
158 mutex_lock(&substream->self_group.mutex);
160 spin_lock_irqsave(&substream->self_group.lock, flags);
163 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
166 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
167 * @substream: PCM substream
170 * This is a counter-part of snd_pcm_stream_lock_irqsave().
172 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
175 if (substream->pcm->nonatomic)
176 mutex_unlock(&substream->self_group.mutex);
178 spin_unlock_irqrestore(&substream->self_group.lock, flags);
180 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
182 /* Run PCM ioctl ops */
183 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
184 unsigned cmd, void *arg)
186 if (substream->ops->ioctl)
187 return substream->ops->ioctl(substream, cmd, arg);
189 return snd_pcm_lib_ioctl(substream, cmd, arg);
192 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
194 struct snd_pcm *pcm = substream->pcm;
195 struct snd_pcm_str *pstr = substream->pstr;
197 memset(info, 0, sizeof(*info));
198 info->card = pcm->card->number;
199 info->device = pcm->device;
200 info->stream = substream->stream;
201 info->subdevice = substream->number;
202 strlcpy(info->id, pcm->id, sizeof(info->id));
203 strlcpy(info->name, pcm->name, sizeof(info->name));
204 info->dev_class = pcm->dev_class;
205 info->dev_subclass = pcm->dev_subclass;
206 info->subdevices_count = pstr->substream_count;
207 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
208 strlcpy(info->subname, substream->name, sizeof(info->subname));
213 int snd_pcm_info_user(struct snd_pcm_substream *substream,
214 struct snd_pcm_info __user * _info)
216 struct snd_pcm_info *info;
219 info = kmalloc(sizeof(*info), GFP_KERNEL);
222 err = snd_pcm_info(substream, info);
224 if (copy_to_user(_info, info, sizeof(*info)))
231 /* macro for simplified cast */
232 #define PARAM_MASK_BIT(b) (1U << (__force int)(b))
234 static bool hw_support_mmap(struct snd_pcm_substream *substream)
236 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
239 if (substream->ops->mmap ||
240 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
241 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
244 return dma_can_mmap(substream->dma_buffer.dev.dev);
247 static int constrain_mask_params(struct snd_pcm_substream *substream,
248 struct snd_pcm_hw_params *params)
250 struct snd_pcm_hw_constraints *constrs =
251 &substream->runtime->hw_constraints;
254 struct snd_mask old_mask;
257 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
258 m = hw_param_mask(params, k);
259 if (snd_mask_empty(m))
262 /* This parameter is not requested to change by a caller. */
263 if (!(params->rmask & PARAM_MASK_BIT(k)))
266 if (trace_hw_mask_param_enabled())
269 changed = snd_mask_refine(m, constrs_mask(constrs, k));
275 /* Set corresponding flag so that the caller gets it. */
276 trace_hw_mask_param(substream, k, 0, &old_mask, m);
277 params->cmask |= PARAM_MASK_BIT(k);
283 static int constrain_interval_params(struct snd_pcm_substream *substream,
284 struct snd_pcm_hw_params *params)
286 struct snd_pcm_hw_constraints *constrs =
287 &substream->runtime->hw_constraints;
288 struct snd_interval *i;
290 struct snd_interval old_interval;
293 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
294 i = hw_param_interval(params, k);
295 if (snd_interval_empty(i))
298 /* This parameter is not requested to change by a caller. */
299 if (!(params->rmask & PARAM_MASK_BIT(k)))
302 if (trace_hw_interval_param_enabled())
305 changed = snd_interval_refine(i, constrs_interval(constrs, k));
311 /* Set corresponding flag so that the caller gets it. */
312 trace_hw_interval_param(substream, k, 0, &old_interval, i);
313 params->cmask |= PARAM_MASK_BIT(k);
319 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
320 struct snd_pcm_hw_params *params)
322 struct snd_pcm_hw_constraints *constrs =
323 &substream->runtime->hw_constraints;
325 unsigned int *rstamps;
326 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
328 struct snd_pcm_hw_rule *r;
330 struct snd_mask old_mask;
331 struct snd_interval old_interval;
333 int changed, err = 0;
336 * Each application of rule has own sequence number.
338 * Each member of 'rstamps' array represents the sequence number of
339 * recent application of corresponding rule.
341 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
346 * Each member of 'vstamps' array represents the sequence number of
347 * recent application of rule in which corresponding parameters were
350 * In initial state, elements corresponding to parameters requested by
351 * a caller is 1. For unrequested parameters, corresponding members
352 * have 0 so that the parameters are never changed anymore.
354 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
355 vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
357 /* Due to the above design, actual sequence number starts at 2. */
360 /* Apply all rules in order. */
362 for (k = 0; k < constrs->rules_num; k++) {
363 r = &constrs->rules[k];
366 * Check condition bits of this rule. When the rule has
367 * some condition bits, parameter without the bits is
368 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
369 * is an example of the condition bits.
371 if (r->cond && !(r->cond & params->flags))
375 * The 'deps' array includes maximum three dependencies
376 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
377 * member of this array is a sentinel and should be
380 * This rule should be processed in this time when dependent
381 * parameters were changed at former applications of the other
384 for (d = 0; r->deps[d] >= 0; d++) {
385 if (vstamps[r->deps[d]] > rstamps[k])
391 if (trace_hw_mask_param_enabled()) {
392 if (hw_is_mask(r->var))
393 old_mask = *hw_param_mask(params, r->var);
395 if (trace_hw_interval_param_enabled()) {
396 if (hw_is_interval(r->var))
397 old_interval = *hw_param_interval(params, r->var);
400 changed = r->func(params, r);
407 * When the parameter is changed, notify it to the caller
408 * by corresponding returned bit, then preparing for next
411 if (changed && r->var >= 0) {
412 if (hw_is_mask(r->var)) {
413 trace_hw_mask_param(substream, r->var,
415 hw_param_mask(params, r->var));
417 if (hw_is_interval(r->var)) {
418 trace_hw_interval_param(substream, r->var,
419 k + 1, &old_interval,
420 hw_param_interval(params, r->var));
423 params->cmask |= PARAM_MASK_BIT(r->var);
424 vstamps[r->var] = stamp;
428 rstamps[k] = stamp++;
431 /* Iterate to evaluate all rules till no parameters are changed. */
440 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params *params)
443 const struct snd_interval *i;
444 const struct snd_mask *m;
447 if (!params->msbits) {
448 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
449 if (snd_interval_single(i))
450 params->msbits = snd_interval_value(i);
453 if (!params->rate_den) {
454 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
455 if (snd_interval_single(i)) {
456 params->rate_num = snd_interval_value(i);
457 params->rate_den = 1;
461 if (!params->fifo_size) {
462 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
463 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
464 if (snd_mask_single(m) && snd_interval_single(i)) {
465 err = snd_pcm_ops_ioctl(substream,
466 SNDRV_PCM_IOCTL1_FIFO_SIZE,
474 params->info = substream->runtime->hw.info;
475 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
476 SNDRV_PCM_INFO_DRAIN_TRIGGER);
477 if (!hw_support_mmap(substream))
478 params->info &= ~(SNDRV_PCM_INFO_MMAP |
479 SNDRV_PCM_INFO_MMAP_VALID);
485 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *params)
491 params->fifo_size = 0;
492 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
494 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
495 params->rate_num = 0;
496 params->rate_den = 0;
499 err = constrain_mask_params(substream, params);
503 err = constrain_interval_params(substream, params);
507 err = constrain_params_by_rules(substream, params);
515 EXPORT_SYMBOL(snd_pcm_hw_refine);
517 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
518 struct snd_pcm_hw_params __user * _params)
520 struct snd_pcm_hw_params *params;
523 params = memdup_user(_params, sizeof(*params));
525 return PTR_ERR(params);
527 err = snd_pcm_hw_refine(substream, params);
531 err = fixup_unreferenced_params(substream, params);
535 if (copy_to_user(_params, params, sizeof(*params)))
542 static int period_to_usecs(struct snd_pcm_runtime *runtime)
547 return -1; /* invalid */
549 /* take 75% of period time as the deadline */
550 usecs = (750000 / runtime->rate) * runtime->period_size;
551 usecs += ((750000 % runtime->rate) * runtime->period_size) /
557 static void snd_pcm_set_state(struct snd_pcm_substream *substream,
558 snd_pcm_state_t state)
560 snd_pcm_stream_lock_irq(substream);
561 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
562 substream->runtime->status->state = state;
563 snd_pcm_stream_unlock_irq(substream);
566 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
569 #ifdef CONFIG_SND_PCM_TIMER
570 if (substream->timer)
571 snd_timer_notify(substream->timer, event,
572 &substream->runtime->trigger_tstamp);
576 static void snd_pcm_sync_stop(struct snd_pcm_substream *substream)
578 if (substream->runtime->stop_operating) {
579 substream->runtime->stop_operating = false;
580 if (substream->ops->sync_stop)
581 substream->ops->sync_stop(substream);
582 else if (substream->pcm->card->sync_irq > 0)
583 synchronize_irq(substream->pcm->card->sync_irq);
588 * snd_pcm_hw_param_choose - choose a configuration defined by @params
590 * @params: the hw_params instance
592 * Choose one configuration from configuration space defined by @params.
593 * The configuration chosen is that obtained fixing in this order:
594 * first access, first format, first subformat, min channels,
595 * min rate, min period time, max buffer size, min tick time
597 * Return: Zero if successful, or a negative error code on failure.
599 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
600 struct snd_pcm_hw_params *params)
602 static const int vars[] = {
603 SNDRV_PCM_HW_PARAM_ACCESS,
604 SNDRV_PCM_HW_PARAM_FORMAT,
605 SNDRV_PCM_HW_PARAM_SUBFORMAT,
606 SNDRV_PCM_HW_PARAM_CHANNELS,
607 SNDRV_PCM_HW_PARAM_RATE,
608 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
609 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
610 SNDRV_PCM_HW_PARAM_TICK_TIME,
614 struct snd_mask old_mask;
615 struct snd_interval old_interval;
618 for (v = vars; *v != -1; v++) {
619 /* Keep old parameter to trace. */
620 if (trace_hw_mask_param_enabled()) {
622 old_mask = *hw_param_mask(params, *v);
624 if (trace_hw_interval_param_enabled()) {
625 if (hw_is_interval(*v))
626 old_interval = *hw_param_interval(params, *v);
628 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
629 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
631 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
637 /* Trace the changed parameter. */
638 if (hw_is_mask(*v)) {
639 trace_hw_mask_param(pcm, *v, 0, &old_mask,
640 hw_param_mask(params, *v));
642 if (hw_is_interval(*v)) {
643 trace_hw_interval_param(pcm, *v, 0, &old_interval,
644 hw_param_interval(params, *v));
651 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
652 struct snd_pcm_hw_params *params)
654 struct snd_pcm_runtime *runtime;
657 snd_pcm_uframes_t frames;
659 if (PCM_RUNTIME_CHECK(substream))
661 runtime = substream->runtime;
662 snd_pcm_stream_lock_irq(substream);
663 switch (runtime->status->state) {
664 case SNDRV_PCM_STATE_OPEN:
665 case SNDRV_PCM_STATE_SETUP:
666 case SNDRV_PCM_STATE_PREPARED:
669 snd_pcm_stream_unlock_irq(substream);
672 snd_pcm_stream_unlock_irq(substream);
673 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
674 if (!substream->oss.oss)
676 if (atomic_read(&substream->mmap_count))
679 snd_pcm_sync_stop(substream);
682 err = snd_pcm_hw_refine(substream, params);
686 err = snd_pcm_hw_params_choose(substream, params);
690 err = fixup_unreferenced_params(substream, params);
694 if (substream->managed_buffer_alloc) {
695 err = snd_pcm_lib_malloc_pages(substream,
696 params_buffer_bytes(params));
699 runtime->buffer_changed = err > 0;
702 if (substream->ops->hw_params != NULL) {
703 err = substream->ops->hw_params(substream, params);
708 runtime->access = params_access(params);
709 runtime->format = params_format(params);
710 runtime->subformat = params_subformat(params);
711 runtime->channels = params_channels(params);
712 runtime->rate = params_rate(params);
713 runtime->period_size = params_period_size(params);
714 runtime->periods = params_periods(params);
715 runtime->buffer_size = params_buffer_size(params);
716 runtime->info = params->info;
717 runtime->rate_num = params->rate_num;
718 runtime->rate_den = params->rate_den;
719 runtime->no_period_wakeup =
720 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
721 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
723 bits = snd_pcm_format_physical_width(runtime->format);
724 runtime->sample_bits = bits;
725 bits *= runtime->channels;
726 runtime->frame_bits = bits;
728 while (bits % 8 != 0) {
732 runtime->byte_align = bits / 8;
733 runtime->min_align = frames;
735 /* Default sw params */
736 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
737 runtime->period_step = 1;
738 runtime->control->avail_min = runtime->period_size;
739 runtime->start_threshold = 1;
740 runtime->stop_threshold = runtime->buffer_size;
741 runtime->silence_threshold = 0;
742 runtime->silence_size = 0;
743 runtime->boundary = runtime->buffer_size;
744 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
745 runtime->boundary *= 2;
747 /* clear the buffer for avoiding possible kernel info leaks */
748 if (runtime->dma_area && !substream->ops->copy_user)
749 memset(runtime->dma_area, 0, runtime->dma_bytes);
751 snd_pcm_timer_resolution_change(substream);
752 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
754 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
755 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
756 if ((usecs = period_to_usecs(runtime)) >= 0)
757 cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
761 /* hardware might be unusable from this time,
762 so we force application to retry to set
763 the correct hardware parameter settings */
764 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
765 if (substream->ops->hw_free != NULL)
766 substream->ops->hw_free(substream);
767 if (substream->managed_buffer_alloc)
768 snd_pcm_lib_free_pages(substream);
772 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
773 struct snd_pcm_hw_params __user * _params)
775 struct snd_pcm_hw_params *params;
778 params = memdup_user(_params, sizeof(*params));
780 return PTR_ERR(params);
782 err = snd_pcm_hw_params(substream, params);
786 if (copy_to_user(_params, params, sizeof(*params)))
793 static int do_hw_free(struct snd_pcm_substream *substream)
797 snd_pcm_sync_stop(substream);
798 if (substream->ops->hw_free)
799 result = substream->ops->hw_free(substream);
800 if (substream->managed_buffer_alloc)
801 snd_pcm_lib_free_pages(substream);
805 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
807 struct snd_pcm_runtime *runtime;
810 if (PCM_RUNTIME_CHECK(substream))
812 runtime = substream->runtime;
813 snd_pcm_stream_lock_irq(substream);
814 switch (runtime->status->state) {
815 case SNDRV_PCM_STATE_SETUP:
816 case SNDRV_PCM_STATE_PREPARED:
819 snd_pcm_stream_unlock_irq(substream);
822 snd_pcm_stream_unlock_irq(substream);
823 if (atomic_read(&substream->mmap_count))
825 result = do_hw_free(substream);
826 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
827 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
831 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
832 struct snd_pcm_sw_params *params)
834 struct snd_pcm_runtime *runtime;
837 if (PCM_RUNTIME_CHECK(substream))
839 runtime = substream->runtime;
840 snd_pcm_stream_lock_irq(substream);
841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
842 snd_pcm_stream_unlock_irq(substream);
845 snd_pcm_stream_unlock_irq(substream);
847 if (params->tstamp_mode < 0 ||
848 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
850 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
851 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
853 if (params->avail_min == 0)
855 if (params->silence_size >= runtime->boundary) {
856 if (params->silence_threshold != 0)
859 if (params->silence_size > params->silence_threshold)
861 if (params->silence_threshold > runtime->buffer_size)
865 snd_pcm_stream_lock_irq(substream);
866 runtime->tstamp_mode = params->tstamp_mode;
867 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
868 runtime->tstamp_type = params->tstamp_type;
869 runtime->period_step = params->period_step;
870 runtime->control->avail_min = params->avail_min;
871 runtime->start_threshold = params->start_threshold;
872 runtime->stop_threshold = params->stop_threshold;
873 runtime->silence_threshold = params->silence_threshold;
874 runtime->silence_size = params->silence_size;
875 params->boundary = runtime->boundary;
876 if (snd_pcm_running(substream)) {
877 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
878 runtime->silence_size > 0)
879 snd_pcm_playback_silence(substream, ULONG_MAX);
880 err = snd_pcm_update_state(substream, runtime);
882 snd_pcm_stream_unlock_irq(substream);
886 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
887 struct snd_pcm_sw_params __user * _params)
889 struct snd_pcm_sw_params params;
891 if (copy_from_user(¶ms, _params, sizeof(params)))
893 err = snd_pcm_sw_params(substream, ¶ms);
894 if (copy_to_user(_params, ¶ms, sizeof(params)))
899 static inline snd_pcm_uframes_t
900 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
902 snd_pcm_uframes_t delay;
904 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
905 delay = snd_pcm_playback_hw_avail(substream->runtime);
907 delay = snd_pcm_capture_avail(substream->runtime);
908 return delay + substream->runtime->delay;
911 int snd_pcm_status64(struct snd_pcm_substream *substream,
912 struct snd_pcm_status64 *status)
914 struct snd_pcm_runtime *runtime = substream->runtime;
916 snd_pcm_stream_lock_irq(substream);
918 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
919 &runtime->audio_tstamp_config);
921 /* backwards compatible behavior */
922 if (runtime->audio_tstamp_config.type_requested ==
923 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
924 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
925 runtime->audio_tstamp_config.type_requested =
926 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
928 runtime->audio_tstamp_config.type_requested =
929 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
930 runtime->audio_tstamp_report.valid = 0;
932 runtime->audio_tstamp_report.valid = 1;
934 status->state = runtime->status->state;
935 status->suspended_state = runtime->status->suspended_state;
936 if (status->state == SNDRV_PCM_STATE_OPEN)
938 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
939 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
940 if (snd_pcm_running(substream)) {
941 snd_pcm_update_hw_ptr(substream);
942 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
943 status->tstamp_sec = runtime->status->tstamp.tv_sec;
944 status->tstamp_nsec =
945 runtime->status->tstamp.tv_nsec;
946 status->driver_tstamp_sec =
947 runtime->driver_tstamp.tv_sec;
948 status->driver_tstamp_nsec =
949 runtime->driver_tstamp.tv_nsec;
950 status->audio_tstamp_sec =
951 runtime->status->audio_tstamp.tv_sec;
952 status->audio_tstamp_nsec =
953 runtime->status->audio_tstamp.tv_nsec;
954 if (runtime->audio_tstamp_report.valid == 1)
955 /* backwards compatibility, no report provided in COMPAT mode */
956 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
957 &status->audio_tstamp_accuracy,
958 &runtime->audio_tstamp_report);
963 /* get tstamp only in fallback mode and only if enabled */
964 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
965 struct timespec64 tstamp;
967 snd_pcm_gettime(runtime, &tstamp);
968 status->tstamp_sec = tstamp.tv_sec;
969 status->tstamp_nsec = tstamp.tv_nsec;
973 status->appl_ptr = runtime->control->appl_ptr;
974 status->hw_ptr = runtime->status->hw_ptr;
975 status->avail = snd_pcm_avail(substream);
976 status->delay = snd_pcm_running(substream) ?
977 snd_pcm_calc_delay(substream) : 0;
978 status->avail_max = runtime->avail_max;
979 status->overrange = runtime->overrange;
980 runtime->avail_max = 0;
981 runtime->overrange = 0;
983 snd_pcm_stream_unlock_irq(substream);
987 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
988 struct snd_pcm_status64 __user * _status,
991 struct snd_pcm_status64 status;
994 memset(&status, 0, sizeof(status));
996 * with extension, parameters are read/write,
997 * get audio_tstamp_data from user,
998 * ignore rest of status structure
1000 if (ext && get_user(status.audio_tstamp_data,
1001 (u32 __user *)(&_status->audio_tstamp_data)))
1003 res = snd_pcm_status64(substream, &status);
1006 if (copy_to_user(_status, &status, sizeof(status)))
1011 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1012 struct snd_pcm_status32 __user * _status,
1015 struct snd_pcm_status64 status64;
1016 struct snd_pcm_status32 status32;
1019 memset(&status64, 0, sizeof(status64));
1020 memset(&status32, 0, sizeof(status32));
1022 * with extension, parameters are read/write,
1023 * get audio_tstamp_data from user,
1024 * ignore rest of status structure
1026 if (ext && get_user(status64.audio_tstamp_data,
1027 (u32 __user *)(&_status->audio_tstamp_data)))
1029 res = snd_pcm_status64(substream, &status64);
1033 status32 = (struct snd_pcm_status32) {
1034 .state = status64.state,
1035 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1036 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1037 .tstamp_sec = status64.tstamp_sec,
1038 .tstamp_nsec = status64.tstamp_nsec,
1039 .appl_ptr = status64.appl_ptr,
1040 .hw_ptr = status64.hw_ptr,
1041 .delay = status64.delay,
1042 .avail = status64.avail,
1043 .avail_max = status64.avail_max,
1044 .overrange = status64.overrange,
1045 .suspended_state = status64.suspended_state,
1046 .audio_tstamp_data = status64.audio_tstamp_data,
1047 .audio_tstamp_sec = status64.audio_tstamp_sec,
1048 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1049 .driver_tstamp_sec = status64.audio_tstamp_sec,
1050 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1051 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1054 if (copy_to_user(_status, &status32, sizeof(status32)))
1060 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1061 struct snd_pcm_channel_info * info)
1063 struct snd_pcm_runtime *runtime;
1064 unsigned int channel;
1066 channel = info->channel;
1067 runtime = substream->runtime;
1068 snd_pcm_stream_lock_irq(substream);
1069 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1070 snd_pcm_stream_unlock_irq(substream);
1073 snd_pcm_stream_unlock_irq(substream);
1074 if (channel >= runtime->channels)
1076 memset(info, 0, sizeof(*info));
1077 info->channel = channel;
1078 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1081 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1082 struct snd_pcm_channel_info __user * _info)
1084 struct snd_pcm_channel_info info;
1087 if (copy_from_user(&info, _info, sizeof(info)))
1089 res = snd_pcm_channel_info(substream, &info);
1092 if (copy_to_user(_info, &info, sizeof(info)))
1097 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1099 struct snd_pcm_runtime *runtime = substream->runtime;
1100 if (runtime->trigger_master == NULL)
1102 if (runtime->trigger_master == substream) {
1103 if (!runtime->trigger_tstamp_latched)
1104 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1106 snd_pcm_trigger_tstamp(runtime->trigger_master);
1107 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1109 runtime->trigger_master = NULL;
1112 #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1115 int (*pre_action)(struct snd_pcm_substream *substream,
1116 snd_pcm_state_t state);
1117 int (*do_action)(struct snd_pcm_substream *substream,
1118 snd_pcm_state_t state);
1119 void (*undo_action)(struct snd_pcm_substream *substream,
1120 snd_pcm_state_t state);
1121 void (*post_action)(struct snd_pcm_substream *substream,
1122 snd_pcm_state_t state);
1126 * this functions is core for handling of linked stream
1127 * Note: the stream state might be changed also on failure
1128 * Note2: call with calling stream lock + link lock
1130 static int snd_pcm_action_group(const struct action_ops *ops,
1131 struct snd_pcm_substream *substream,
1132 snd_pcm_state_t state,
1135 struct snd_pcm_substream *s = NULL;
1136 struct snd_pcm_substream *s1;
1137 int res = 0, depth = 1;
1139 snd_pcm_group_for_each_entry(s, substream) {
1140 if (do_lock && s != substream) {
1141 if (s->pcm->nonatomic)
1142 mutex_lock_nested(&s->self_group.mutex, depth);
1144 spin_lock_nested(&s->self_group.lock, depth);
1147 res = ops->pre_action(s, state);
1151 snd_pcm_group_for_each_entry(s, substream) {
1152 res = ops->do_action(s, state);
1154 if (ops->undo_action) {
1155 snd_pcm_group_for_each_entry(s1, substream) {
1156 if (s1 == s) /* failed stream */
1158 ops->undo_action(s1, state);
1161 s = NULL; /* unlock all */
1165 snd_pcm_group_for_each_entry(s, substream) {
1166 ops->post_action(s, state);
1170 /* unlock streams */
1171 snd_pcm_group_for_each_entry(s1, substream) {
1172 if (s1 != substream) {
1173 if (s1->pcm->nonatomic)
1174 mutex_unlock(&s1->self_group.mutex);
1176 spin_unlock(&s1->self_group.lock);
1178 if (s1 == s) /* end */
1186 * Note: call with stream lock
1188 static int snd_pcm_action_single(const struct action_ops *ops,
1189 struct snd_pcm_substream *substream,
1190 snd_pcm_state_t state)
1194 res = ops->pre_action(substream, state);
1197 res = ops->do_action(substream, state);
1199 ops->post_action(substream, state);
1200 else if (ops->undo_action)
1201 ops->undo_action(substream, state);
1205 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1206 struct snd_pcm_group *new_group)
1208 substream->group = new_group;
1209 list_move(&substream->link_list, &new_group->substreams);
1213 * Unref and unlock the group, but keep the stream lock;
1214 * when the group becomes empty and no longer referred, destroy itself
1216 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1217 struct snd_pcm_substream *substream)
1223 do_free = refcount_dec_and_test(&group->refs);
1224 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1230 * Lock the group inside a stream lock and reference it;
1231 * return the locked group object, or NULL if not linked
1233 static struct snd_pcm_group *
1234 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1236 bool nonatomic = substream->pcm->nonatomic;
1237 struct snd_pcm_group *group;
1241 if (!snd_pcm_stream_linked(substream))
1243 group = substream->group;
1244 /* block freeing the group object */
1245 refcount_inc(&group->refs);
1247 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1248 spin_trylock(&group->lock);
1252 /* re-lock for avoiding ABBA deadlock */
1253 snd_pcm_stream_unlock(substream);
1254 snd_pcm_group_lock(group, nonatomic);
1255 snd_pcm_stream_lock(substream);
1257 /* check the group again; the above opens a small race window */
1258 if (substream->group == group)
1260 /* group changed, try again */
1261 snd_pcm_group_unref(group, substream);
1267 * Note: call with stream lock
1269 static int snd_pcm_action(const struct action_ops *ops,
1270 struct snd_pcm_substream *substream,
1271 snd_pcm_state_t state)
1273 struct snd_pcm_group *group;
1276 group = snd_pcm_stream_group_ref(substream);
1278 res = snd_pcm_action_group(ops, substream, state, true);
1280 res = snd_pcm_action_single(ops, substream, state);
1281 snd_pcm_group_unref(group, substream);
1286 * Note: don't use any locks before
1288 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1289 struct snd_pcm_substream *substream,
1290 snd_pcm_state_t state)
1294 snd_pcm_stream_lock_irq(substream);
1295 res = snd_pcm_action(ops, substream, state);
1296 snd_pcm_stream_unlock_irq(substream);
1302 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1303 struct snd_pcm_substream *substream,
1304 snd_pcm_state_t state)
1308 /* Guarantee the group members won't change during non-atomic action */
1309 down_read(&snd_pcm_link_rwsem);
1310 if (snd_pcm_stream_linked(substream))
1311 res = snd_pcm_action_group(ops, substream, state, false);
1313 res = snd_pcm_action_single(ops, substream, state);
1314 up_read(&snd_pcm_link_rwsem);
1321 static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1322 snd_pcm_state_t state)
1324 struct snd_pcm_runtime *runtime = substream->runtime;
1325 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1328 !snd_pcm_playback_data(substream))
1330 runtime->trigger_tstamp_latched = false;
1331 runtime->trigger_master = substream;
1335 static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1336 snd_pcm_state_t state)
1338 if (substream->runtime->trigger_master != substream)
1340 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1343 static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1344 snd_pcm_state_t state)
1346 if (substream->runtime->trigger_master == substream)
1347 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1350 static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1351 snd_pcm_state_t state)
1353 struct snd_pcm_runtime *runtime = substream->runtime;
1354 snd_pcm_trigger_tstamp(substream);
1355 runtime->hw_ptr_jiffies = jiffies;
1356 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1358 runtime->status->state = state;
1359 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1360 runtime->silence_size > 0)
1361 snd_pcm_playback_silence(substream, ULONG_MAX);
1362 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1365 static const struct action_ops snd_pcm_action_start = {
1366 .pre_action = snd_pcm_pre_start,
1367 .do_action = snd_pcm_do_start,
1368 .undo_action = snd_pcm_undo_start,
1369 .post_action = snd_pcm_post_start
1373 * snd_pcm_start - start all linked streams
1374 * @substream: the PCM substream instance
1376 * Return: Zero if successful, or a negative error code.
1377 * The stream lock must be acquired before calling this function.
1379 int snd_pcm_start(struct snd_pcm_substream *substream)
1381 return snd_pcm_action(&snd_pcm_action_start, substream,
1382 SNDRV_PCM_STATE_RUNNING);
1385 /* take the stream lock and start the streams */
1386 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1388 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1389 SNDRV_PCM_STATE_RUNNING);
1395 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1396 snd_pcm_state_t state)
1398 struct snd_pcm_runtime *runtime = substream->runtime;
1399 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1401 runtime->trigger_master = substream;
1405 static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1406 snd_pcm_state_t state)
1408 if (substream->runtime->trigger_master == substream &&
1409 snd_pcm_running(substream))
1410 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1411 return 0; /* unconditonally stop all substreams */
1414 static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1415 snd_pcm_state_t state)
1417 struct snd_pcm_runtime *runtime = substream->runtime;
1418 if (runtime->status->state != state) {
1419 snd_pcm_trigger_tstamp(substream);
1420 runtime->status->state = state;
1421 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1423 runtime->stop_operating = true;
1424 wake_up(&runtime->sleep);
1425 wake_up(&runtime->tsleep);
1428 static const struct action_ops snd_pcm_action_stop = {
1429 .pre_action = snd_pcm_pre_stop,
1430 .do_action = snd_pcm_do_stop,
1431 .post_action = snd_pcm_post_stop
1435 * snd_pcm_stop - try to stop all running streams in the substream group
1436 * @substream: the PCM substream instance
1437 * @state: PCM state after stopping the stream
1439 * The state of each stream is then changed to the given state unconditionally.
1441 * Return: Zero if successful, or a negative error code.
1443 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1445 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1447 EXPORT_SYMBOL(snd_pcm_stop);
1450 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1451 * @substream: the PCM substream
1453 * After stopping, the state is changed to SETUP.
1454 * Unlike snd_pcm_stop(), this affects only the given stream.
1456 * Return: Zero if succesful, or a negative error code.
1458 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1460 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1461 SNDRV_PCM_STATE_SETUP);
1465 * snd_pcm_stop_xrun - stop the running streams as XRUN
1466 * @substream: the PCM substream instance
1468 * This stops the given running substream (and all linked substreams) as XRUN.
1469 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1471 * Return: Zero if successful, or a negative error code.
1473 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1475 unsigned long flags;
1477 snd_pcm_stream_lock_irqsave(substream, flags);
1478 if (substream->runtime && snd_pcm_running(substream))
1479 __snd_pcm_xrun(substream);
1480 snd_pcm_stream_unlock_irqrestore(substream, flags);
1483 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1486 * pause callbacks: pass boolean (to start pause or resume) as state argument
1488 #define pause_pushed(state) (__force bool)(state)
1490 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1491 snd_pcm_state_t state)
1493 struct snd_pcm_runtime *runtime = substream->runtime;
1494 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1496 if (pause_pushed(state)) {
1497 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1499 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1501 runtime->trigger_master = substream;
1505 static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1506 snd_pcm_state_t state)
1508 if (substream->runtime->trigger_master != substream)
1510 /* some drivers might use hw_ptr to recover from the pause -
1511 update the hw_ptr now */
1512 if (pause_pushed(state))
1513 snd_pcm_update_hw_ptr(substream);
1514 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1515 * a delta between the current jiffies, this gives a large enough
1516 * delta, effectively to skip the check once.
1518 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1519 return substream->ops->trigger(substream,
1520 pause_pushed(state) ?
1521 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1522 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1525 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1526 snd_pcm_state_t state)
1528 if (substream->runtime->trigger_master == substream)
1529 substream->ops->trigger(substream,
1530 pause_pushed(state) ?
1531 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1532 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1535 static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1536 snd_pcm_state_t state)
1538 struct snd_pcm_runtime *runtime = substream->runtime;
1539 snd_pcm_trigger_tstamp(substream);
1540 if (pause_pushed(state)) {
1541 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1542 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1543 wake_up(&runtime->sleep);
1544 wake_up(&runtime->tsleep);
1546 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1547 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1551 static const struct action_ops snd_pcm_action_pause = {
1552 .pre_action = snd_pcm_pre_pause,
1553 .do_action = snd_pcm_do_pause,
1554 .undo_action = snd_pcm_undo_pause,
1555 .post_action = snd_pcm_post_pause
1559 * Push/release the pause for all linked streams.
1561 static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1563 return snd_pcm_action(&snd_pcm_action_pause, substream,
1564 (__force snd_pcm_state_t)push);
1567 static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1570 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1571 (__force snd_pcm_state_t)push);
1575 /* suspend callback: state argument ignored */
1577 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1578 snd_pcm_state_t state)
1580 struct snd_pcm_runtime *runtime = substream->runtime;
1581 switch (runtime->status->state) {
1582 case SNDRV_PCM_STATE_SUSPENDED:
1584 /* unresumable PCM state; return -EBUSY for skipping suspend */
1585 case SNDRV_PCM_STATE_OPEN:
1586 case SNDRV_PCM_STATE_SETUP:
1587 case SNDRV_PCM_STATE_DISCONNECTED:
1590 runtime->trigger_master = substream;
1594 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1595 snd_pcm_state_t state)
1597 struct snd_pcm_runtime *runtime = substream->runtime;
1598 if (runtime->trigger_master != substream)
1600 if (! snd_pcm_running(substream))
1602 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1603 return 0; /* suspend unconditionally */
1606 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1607 snd_pcm_state_t state)
1609 struct snd_pcm_runtime *runtime = substream->runtime;
1610 snd_pcm_trigger_tstamp(substream);
1611 runtime->status->suspended_state = runtime->status->state;
1612 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1613 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1614 wake_up(&runtime->sleep);
1615 wake_up(&runtime->tsleep);
1618 static const struct action_ops snd_pcm_action_suspend = {
1619 .pre_action = snd_pcm_pre_suspend,
1620 .do_action = snd_pcm_do_suspend,
1621 .post_action = snd_pcm_post_suspend
1625 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1626 * @substream: the PCM substream
1628 * After this call, all streams are changed to SUSPENDED state.
1630 * Return: Zero if successful, or a negative error code.
1632 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1635 unsigned long flags;
1637 snd_pcm_stream_lock_irqsave(substream, flags);
1638 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1640 snd_pcm_stream_unlock_irqrestore(substream, flags);
1645 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1646 * @pcm: the PCM instance
1648 * After this call, all streams are changed to SUSPENDED state.
1650 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1652 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1654 struct snd_pcm_substream *substream;
1655 int stream, err = 0;
1660 for (stream = 0; stream < 2; stream++) {
1661 for (substream = pcm->streams[stream].substream;
1662 substream; substream = substream->next) {
1663 /* FIXME: the open/close code should lock this as well */
1664 if (substream->runtime == NULL)
1668 * Skip BE dai link PCM's that are internal and may
1669 * not have their substream ops set.
1671 if (!substream->ops)
1674 err = snd_pcm_suspend(substream);
1675 if (err < 0 && err != -EBUSY)
1681 EXPORT_SYMBOL(snd_pcm_suspend_all);
1683 /* resume callbacks: state argument ignored */
1685 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1686 snd_pcm_state_t state)
1688 struct snd_pcm_runtime *runtime = substream->runtime;
1689 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1691 runtime->trigger_master = substream;
1695 static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1696 snd_pcm_state_t state)
1698 struct snd_pcm_runtime *runtime = substream->runtime;
1699 if (runtime->trigger_master != substream)
1701 /* DMA not running previously? */
1702 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1703 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1704 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1706 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1709 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1710 snd_pcm_state_t state)
1712 if (substream->runtime->trigger_master == substream &&
1713 snd_pcm_running(substream))
1714 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1717 static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1718 snd_pcm_state_t state)
1720 struct snd_pcm_runtime *runtime = substream->runtime;
1721 snd_pcm_trigger_tstamp(substream);
1722 runtime->status->state = runtime->status->suspended_state;
1723 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1724 snd_pcm_sync_stop(substream);
1727 static const struct action_ops snd_pcm_action_resume = {
1728 .pre_action = snd_pcm_pre_resume,
1729 .do_action = snd_pcm_do_resume,
1730 .undo_action = snd_pcm_undo_resume,
1731 .post_action = snd_pcm_post_resume
1734 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1736 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1742 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1747 #endif /* CONFIG_PM */
1752 * Change the RUNNING stream(s) to XRUN state.
1754 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1756 struct snd_pcm_runtime *runtime = substream->runtime;
1759 snd_pcm_stream_lock_irq(substream);
1760 switch (runtime->status->state) {
1761 case SNDRV_PCM_STATE_XRUN:
1762 result = 0; /* already there */
1764 case SNDRV_PCM_STATE_RUNNING:
1765 __snd_pcm_xrun(substream);
1771 snd_pcm_stream_unlock_irq(substream);
1778 /* reset callbacks: state argument ignored */
1779 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1780 snd_pcm_state_t state)
1782 struct snd_pcm_runtime *runtime = substream->runtime;
1783 switch (runtime->status->state) {
1784 case SNDRV_PCM_STATE_RUNNING:
1785 case SNDRV_PCM_STATE_PREPARED:
1786 case SNDRV_PCM_STATE_PAUSED:
1787 case SNDRV_PCM_STATE_SUSPENDED:
1794 static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1795 snd_pcm_state_t state)
1797 struct snd_pcm_runtime *runtime = substream->runtime;
1798 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1801 runtime->hw_ptr_base = 0;
1802 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1803 runtime->status->hw_ptr % runtime->period_size;
1804 runtime->silence_start = runtime->status->hw_ptr;
1805 runtime->silence_filled = 0;
1809 static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1810 snd_pcm_state_t state)
1812 struct snd_pcm_runtime *runtime = substream->runtime;
1813 runtime->control->appl_ptr = runtime->status->hw_ptr;
1814 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1815 runtime->silence_size > 0)
1816 snd_pcm_playback_silence(substream, ULONG_MAX);
1819 static const struct action_ops snd_pcm_action_reset = {
1820 .pre_action = snd_pcm_pre_reset,
1821 .do_action = snd_pcm_do_reset,
1822 .post_action = snd_pcm_post_reset
1825 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1827 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1834 /* pass f_flags as state argument */
1835 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1836 snd_pcm_state_t state)
1838 struct snd_pcm_runtime *runtime = substream->runtime;
1839 int f_flags = (__force int)state;
1841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1842 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1844 if (snd_pcm_running(substream))
1846 substream->f_flags = f_flags;
1850 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1851 snd_pcm_state_t state)
1854 snd_pcm_sync_stop(substream);
1855 err = substream->ops->prepare(substream);
1858 return snd_pcm_do_reset(substream, state);
1861 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1862 snd_pcm_state_t state)
1864 struct snd_pcm_runtime *runtime = substream->runtime;
1865 runtime->control->appl_ptr = runtime->status->hw_ptr;
1866 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1869 static const struct action_ops snd_pcm_action_prepare = {
1870 .pre_action = snd_pcm_pre_prepare,
1871 .do_action = snd_pcm_do_prepare,
1872 .post_action = snd_pcm_post_prepare
1876 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1877 * @substream: the PCM substream instance
1878 * @file: file to refer f_flags
1880 * Return: Zero if successful, or a negative error code.
1882 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1888 f_flags = file->f_flags;
1890 f_flags = substream->f_flags;
1892 snd_pcm_stream_lock_irq(substream);
1893 switch (substream->runtime->status->state) {
1894 case SNDRV_PCM_STATE_PAUSED:
1895 snd_pcm_pause(substream, false);
1897 case SNDRV_PCM_STATE_SUSPENDED:
1898 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1901 snd_pcm_stream_unlock_irq(substream);
1903 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1905 (__force snd_pcm_state_t)f_flags);
1912 /* drain init callbacks: state argument ignored */
1913 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1914 snd_pcm_state_t state)
1916 struct snd_pcm_runtime *runtime = substream->runtime;
1917 switch (runtime->status->state) {
1918 case SNDRV_PCM_STATE_OPEN:
1919 case SNDRV_PCM_STATE_DISCONNECTED:
1920 case SNDRV_PCM_STATE_SUSPENDED:
1923 runtime->trigger_master = substream;
1927 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1928 snd_pcm_state_t state)
1930 struct snd_pcm_runtime *runtime = substream->runtime;
1931 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1932 switch (runtime->status->state) {
1933 case SNDRV_PCM_STATE_PREPARED:
1934 /* start playback stream if possible */
1935 if (! snd_pcm_playback_empty(substream)) {
1936 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1937 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1939 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1942 case SNDRV_PCM_STATE_RUNNING:
1943 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1945 case SNDRV_PCM_STATE_XRUN:
1946 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1952 /* stop running stream */
1953 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1954 snd_pcm_state_t new_state;
1956 new_state = snd_pcm_capture_avail(runtime) > 0 ?
1957 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1958 snd_pcm_do_stop(substream, new_state);
1959 snd_pcm_post_stop(substream, new_state);
1963 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1964 runtime->trigger_master == substream &&
1965 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1966 return substream->ops->trigger(substream,
1967 SNDRV_PCM_TRIGGER_DRAIN);
1972 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
1973 snd_pcm_state_t state)
1977 static const struct action_ops snd_pcm_action_drain_init = {
1978 .pre_action = snd_pcm_pre_drain_init,
1979 .do_action = snd_pcm_do_drain_init,
1980 .post_action = snd_pcm_post_drain_init
1984 * Drain the stream(s).
1985 * When the substream is linked, sync until the draining of all playback streams
1987 * After this call, all streams are supposed to be either SETUP or DRAINING
1988 * (capture only) state.
1990 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1993 struct snd_card *card;
1994 struct snd_pcm_runtime *runtime;
1995 struct snd_pcm_substream *s;
1996 struct snd_pcm_group *group;
1997 wait_queue_entry_t wait;
2001 card = substream->pcm->card;
2002 runtime = substream->runtime;
2004 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2008 if (file->f_flags & O_NONBLOCK)
2010 } else if (substream->f_flags & O_NONBLOCK)
2013 snd_pcm_stream_lock_irq(substream);
2015 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2016 snd_pcm_pause(substream, false);
2018 /* pre-start/stop - all running streams are changed to DRAINING state */
2019 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2023 /* in non-blocking, we don't wait in ioctl but let caller poll */
2031 struct snd_pcm_runtime *to_check;
2032 if (signal_pending(current)) {
2033 result = -ERESTARTSYS;
2036 /* find a substream to drain */
2038 group = snd_pcm_stream_group_ref(substream);
2039 snd_pcm_group_for_each_entry(s, substream) {
2040 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2042 runtime = s->runtime;
2043 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2048 snd_pcm_group_unref(group, substream);
2050 break; /* all drained */
2051 init_waitqueue_entry(&wait, current);
2052 set_current_state(TASK_INTERRUPTIBLE);
2053 add_wait_queue(&to_check->sleep, &wait);
2054 snd_pcm_stream_unlock_irq(substream);
2055 if (runtime->no_period_wakeup)
2056 tout = MAX_SCHEDULE_TIMEOUT;
2059 if (runtime->rate) {
2060 long t = runtime->period_size * 2 / runtime->rate;
2061 tout = max(t, tout);
2063 tout = msecs_to_jiffies(tout * 1000);
2065 tout = schedule_timeout(tout);
2067 snd_pcm_stream_lock_irq(substream);
2068 group = snd_pcm_stream_group_ref(substream);
2069 snd_pcm_group_for_each_entry(s, substream) {
2070 if (s->runtime == to_check) {
2071 remove_wait_queue(&to_check->sleep, &wait);
2075 snd_pcm_group_unref(group, substream);
2077 if (card->shutdown) {
2082 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2085 dev_dbg(substream->pcm->card->dev,
2086 "playback drain error (DMA or IRQ trouble?)\n");
2087 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2095 snd_pcm_stream_unlock_irq(substream);
2103 * Immediately put all linked substreams into SETUP state.
2105 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2107 struct snd_pcm_runtime *runtime;
2110 if (PCM_RUNTIME_CHECK(substream))
2112 runtime = substream->runtime;
2114 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2115 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2118 snd_pcm_stream_lock_irq(substream);
2120 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2121 snd_pcm_pause(substream, false);
2123 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2124 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2125 snd_pcm_stream_unlock_irq(substream);
2131 static bool is_pcm_file(struct file *file)
2133 struct inode *inode = file_inode(file);
2134 struct snd_pcm *pcm;
2137 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2139 minor = iminor(inode);
2140 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2142 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2145 snd_card_unref(pcm->card);
2152 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2155 struct snd_pcm_file *pcm_file;
2156 struct snd_pcm_substream *substream1;
2157 struct snd_pcm_group *group, *target_group;
2158 bool nonatomic = substream->pcm->nonatomic;
2159 struct fd f = fdget(fd);
2163 if (!is_pcm_file(f.file)) {
2167 pcm_file = f.file->private_data;
2168 substream1 = pcm_file->substream;
2169 group = kzalloc(sizeof(*group), GFP_KERNEL);
2174 snd_pcm_group_init(group);
2176 down_write(&snd_pcm_link_rwsem);
2177 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2178 substream->runtime->status->state != substream1->runtime->status->state ||
2179 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2183 if (snd_pcm_stream_linked(substream1)) {
2188 snd_pcm_stream_lock_irq(substream);
2189 if (!snd_pcm_stream_linked(substream)) {
2190 snd_pcm_group_assign(substream, group);
2191 group = NULL; /* assigned, don't free this one below */
2193 target_group = substream->group;
2194 snd_pcm_stream_unlock_irq(substream);
2196 snd_pcm_group_lock_irq(target_group, nonatomic);
2197 snd_pcm_stream_lock(substream1);
2198 snd_pcm_group_assign(substream1, target_group);
2199 refcount_inc(&target_group->refs);
2200 snd_pcm_stream_unlock(substream1);
2201 snd_pcm_group_unlock_irq(target_group, nonatomic);
2203 up_write(&snd_pcm_link_rwsem);
2211 static void relink_to_local(struct snd_pcm_substream *substream)
2213 snd_pcm_stream_lock(substream);
2214 snd_pcm_group_assign(substream, &substream->self_group);
2215 snd_pcm_stream_unlock(substream);
2218 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2220 struct snd_pcm_group *group;
2221 bool nonatomic = substream->pcm->nonatomic;
2222 bool do_free = false;
2225 down_write(&snd_pcm_link_rwsem);
2227 if (!snd_pcm_stream_linked(substream)) {
2232 group = substream->group;
2233 snd_pcm_group_lock_irq(group, nonatomic);
2235 relink_to_local(substream);
2236 refcount_dec(&group->refs);
2238 /* detach the last stream, too */
2239 if (list_is_singular(&group->substreams)) {
2240 relink_to_local(list_first_entry(&group->substreams,
2241 struct snd_pcm_substream,
2243 do_free = refcount_dec_and_test(&group->refs);
2246 snd_pcm_group_unlock_irq(group, nonatomic);
2251 up_write(&snd_pcm_link_rwsem);
2258 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2259 struct snd_pcm_hw_rule *rule)
2261 struct snd_interval t;
2262 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2263 hw_param_interval_c(params, rule->deps[1]), &t);
2264 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2267 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2268 struct snd_pcm_hw_rule *rule)
2270 struct snd_interval t;
2271 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2272 hw_param_interval_c(params, rule->deps[1]), &t);
2273 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2276 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2277 struct snd_pcm_hw_rule *rule)
2279 struct snd_interval t;
2280 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2281 hw_param_interval_c(params, rule->deps[1]),
2282 (unsigned long) rule->private, &t);
2283 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2286 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2287 struct snd_pcm_hw_rule *rule)
2289 struct snd_interval t;
2290 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2291 (unsigned long) rule->private,
2292 hw_param_interval_c(params, rule->deps[1]), &t);
2293 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2296 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2297 struct snd_pcm_hw_rule *rule)
2300 const struct snd_interval *i =
2301 hw_param_interval_c(params, rule->deps[0]);
2303 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2305 pcm_for_each_format(k) {
2307 if (!snd_mask_test_format(mask, k))
2309 bits = snd_pcm_format_physical_width(k);
2311 continue; /* ignore invalid formats */
2312 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2313 snd_mask_reset(&m, (__force unsigned)k);
2315 return snd_mask_refine(mask, &m);
2318 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2319 struct snd_pcm_hw_rule *rule)
2321 struct snd_interval t;
2328 pcm_for_each_format(k) {
2330 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2332 bits = snd_pcm_format_physical_width(k);
2334 continue; /* ignore invalid formats */
2335 if (t.min > (unsigned)bits)
2337 if (t.max < (unsigned)bits)
2341 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2344 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2345 #error "Change this table"
2348 static const unsigned int rates[] = {
2349 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2350 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2353 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2354 .count = ARRAY_SIZE(rates),
2358 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2359 struct snd_pcm_hw_rule *rule)
2361 struct snd_pcm_hardware *hw = rule->private;
2362 return snd_interval_list(hw_param_interval(params, rule->var),
2363 snd_pcm_known_rates.count,
2364 snd_pcm_known_rates.list, hw->rates);
2367 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2368 struct snd_pcm_hw_rule *rule)
2370 struct snd_interval t;
2371 struct snd_pcm_substream *substream = rule->private;
2373 t.max = substream->buffer_bytes_max;
2377 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2380 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2382 struct snd_pcm_runtime *runtime = substream->runtime;
2383 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2386 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2387 snd_mask_any(constrs_mask(constrs, k));
2390 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2391 snd_interval_any(constrs_interval(constrs, k));
2394 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2395 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2396 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2397 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2398 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2400 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2401 snd_pcm_hw_rule_format, NULL,
2402 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2405 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2406 snd_pcm_hw_rule_sample_bits, NULL,
2407 SNDRV_PCM_HW_PARAM_FORMAT,
2408 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2411 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2412 snd_pcm_hw_rule_div, NULL,
2413 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2416 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2417 snd_pcm_hw_rule_mul, NULL,
2418 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2421 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2422 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2423 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2426 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2427 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2428 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2431 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2432 snd_pcm_hw_rule_div, NULL,
2433 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2436 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2437 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2438 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2441 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2442 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2443 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2446 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2447 snd_pcm_hw_rule_div, NULL,
2448 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2451 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2452 snd_pcm_hw_rule_div, NULL,
2453 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2456 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2457 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2458 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2461 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2462 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2463 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2466 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2467 snd_pcm_hw_rule_mul, NULL,
2468 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2471 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2472 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2473 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2476 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2477 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2478 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2481 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2482 snd_pcm_hw_rule_muldivk, (void*) 8,
2483 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2486 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2487 snd_pcm_hw_rule_muldivk, (void*) 8,
2488 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2491 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2492 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2493 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2496 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2497 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2498 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2504 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2506 struct snd_pcm_runtime *runtime = substream->runtime;
2507 struct snd_pcm_hardware *hw = &runtime->hw;
2509 unsigned int mask = 0;
2511 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2512 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2513 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2514 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2515 if (hw_support_mmap(substream)) {
2516 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2517 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2518 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2519 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2520 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2521 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2523 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2527 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2531 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2532 PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
2536 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2537 hw->channels_min, hw->channels_max);
2541 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2542 hw->rate_min, hw->rate_max);
2546 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2547 hw->period_bytes_min, hw->period_bytes_max);
2551 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2552 hw->periods_min, hw->periods_max);
2556 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2557 hw->period_bytes_min, hw->buffer_bytes_max);
2561 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2562 snd_pcm_hw_rule_buffer_bytes_max, substream,
2563 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2568 if (runtime->dma_bytes) {
2569 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2574 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2575 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2576 snd_pcm_hw_rule_rate, hw,
2577 SNDRV_PCM_HW_PARAM_RATE, -1);
2582 /* FIXME: this belong to lowlevel */
2583 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2588 static void pcm_release_private(struct snd_pcm_substream *substream)
2590 if (snd_pcm_stream_linked(substream))
2591 snd_pcm_unlink(substream);
2594 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2596 substream->ref_count--;
2597 if (substream->ref_count > 0)
2600 snd_pcm_drop(substream);
2601 if (substream->hw_opened) {
2602 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2603 do_hw_free(substream);
2604 substream->ops->close(substream);
2605 substream->hw_opened = 0;
2607 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2608 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2609 if (substream->pcm_release) {
2610 substream->pcm_release(substream);
2611 substream->pcm_release = NULL;
2613 snd_pcm_detach_substream(substream);
2615 EXPORT_SYMBOL(snd_pcm_release_substream);
2617 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2619 struct snd_pcm_substream **rsubstream)
2621 struct snd_pcm_substream *substream;
2624 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2627 if (substream->ref_count > 1) {
2628 *rsubstream = substream;
2632 err = snd_pcm_hw_constraints_init(substream);
2634 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2638 if ((err = substream->ops->open(substream)) < 0)
2641 substream->hw_opened = 1;
2643 err = snd_pcm_hw_constraints_complete(substream);
2645 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2649 *rsubstream = substream;
2653 snd_pcm_release_substream(substream);
2656 EXPORT_SYMBOL(snd_pcm_open_substream);
2658 static int snd_pcm_open_file(struct file *file,
2659 struct snd_pcm *pcm,
2662 struct snd_pcm_file *pcm_file;
2663 struct snd_pcm_substream *substream;
2666 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2670 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2671 if (pcm_file == NULL) {
2672 snd_pcm_release_substream(substream);
2675 pcm_file->substream = substream;
2676 if (substream->ref_count == 1)
2677 substream->pcm_release = pcm_release_private;
2678 file->private_data = pcm_file;
2683 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2685 struct snd_pcm *pcm;
2686 int err = nonseekable_open(inode, file);
2689 pcm = snd_lookup_minor_data(iminor(inode),
2690 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2691 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2693 snd_card_unref(pcm->card);
2697 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2699 struct snd_pcm *pcm;
2700 int err = nonseekable_open(inode, file);
2703 pcm = snd_lookup_minor_data(iminor(inode),
2704 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2705 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2707 snd_card_unref(pcm->card);
2711 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2714 wait_queue_entry_t wait;
2720 err = snd_card_file_add(pcm->card, file);
2723 if (!try_module_get(pcm->card->module)) {
2727 init_waitqueue_entry(&wait, current);
2728 add_wait_queue(&pcm->open_wait, &wait);
2729 mutex_lock(&pcm->open_mutex);
2731 err = snd_pcm_open_file(file, pcm, stream);
2734 if (err == -EAGAIN) {
2735 if (file->f_flags & O_NONBLOCK) {
2741 set_current_state(TASK_INTERRUPTIBLE);
2742 mutex_unlock(&pcm->open_mutex);
2744 mutex_lock(&pcm->open_mutex);
2745 if (pcm->card->shutdown) {
2749 if (signal_pending(current)) {
2754 remove_wait_queue(&pcm->open_wait, &wait);
2755 mutex_unlock(&pcm->open_mutex);
2761 module_put(pcm->card->module);
2763 snd_card_file_remove(pcm->card, file);
2768 static int snd_pcm_release(struct inode *inode, struct file *file)
2770 struct snd_pcm *pcm;
2771 struct snd_pcm_substream *substream;
2772 struct snd_pcm_file *pcm_file;
2774 pcm_file = file->private_data;
2775 substream = pcm_file->substream;
2776 if (snd_BUG_ON(!substream))
2778 pcm = substream->pcm;
2779 mutex_lock(&pcm->open_mutex);
2780 snd_pcm_release_substream(substream);
2782 mutex_unlock(&pcm->open_mutex);
2783 wake_up(&pcm->open_wait);
2784 module_put(pcm->card->module);
2785 snd_card_file_remove(pcm->card, file);
2789 /* check and update PCM state; return 0 or a negative error
2790 * call this inside PCM lock
2792 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2794 switch (substream->runtime->status->state) {
2795 case SNDRV_PCM_STATE_DRAINING:
2796 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2799 case SNDRV_PCM_STATE_RUNNING:
2800 return snd_pcm_update_hw_ptr(substream);
2801 case SNDRV_PCM_STATE_PREPARED:
2802 case SNDRV_PCM_STATE_PAUSED:
2804 case SNDRV_PCM_STATE_SUSPENDED:
2806 case SNDRV_PCM_STATE_XRUN:
2813 /* increase the appl_ptr; returns the processed frames or a negative error */
2814 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2815 snd_pcm_uframes_t frames,
2816 snd_pcm_sframes_t avail)
2818 struct snd_pcm_runtime *runtime = substream->runtime;
2819 snd_pcm_sframes_t appl_ptr;
2824 if (frames > (snd_pcm_uframes_t)avail)
2826 appl_ptr = runtime->control->appl_ptr + frames;
2827 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2828 appl_ptr -= runtime->boundary;
2829 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2830 return ret < 0 ? ret : frames;
2833 /* decrease the appl_ptr; returns the processed frames or zero for error */
2834 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2835 snd_pcm_uframes_t frames,
2836 snd_pcm_sframes_t avail)
2838 struct snd_pcm_runtime *runtime = substream->runtime;
2839 snd_pcm_sframes_t appl_ptr;
2844 if (frames > (snd_pcm_uframes_t)avail)
2846 appl_ptr = runtime->control->appl_ptr - frames;
2848 appl_ptr += runtime->boundary;
2849 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2850 /* NOTE: we return zero for errors because PulseAudio gets depressed
2851 * upon receiving an error from rewind ioctl and stops processing
2852 * any longer. Returning zero means that no rewind is done, so
2853 * it's not absolutely wrong to answer like that.
2855 return ret < 0 ? 0 : frames;
2858 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2859 snd_pcm_uframes_t frames)
2861 snd_pcm_sframes_t ret;
2866 snd_pcm_stream_lock_irq(substream);
2867 ret = do_pcm_hwsync(substream);
2869 ret = rewind_appl_ptr(substream, frames,
2870 snd_pcm_hw_avail(substream));
2871 snd_pcm_stream_unlock_irq(substream);
2875 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2876 snd_pcm_uframes_t frames)
2878 snd_pcm_sframes_t ret;
2883 snd_pcm_stream_lock_irq(substream);
2884 ret = do_pcm_hwsync(substream);
2886 ret = forward_appl_ptr(substream, frames,
2887 snd_pcm_avail(substream));
2888 snd_pcm_stream_unlock_irq(substream);
2892 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2896 snd_pcm_stream_lock_irq(substream);
2897 err = do_pcm_hwsync(substream);
2898 snd_pcm_stream_unlock_irq(substream);
2902 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2903 snd_pcm_sframes_t *delay)
2906 snd_pcm_sframes_t n = 0;
2908 snd_pcm_stream_lock_irq(substream);
2909 err = do_pcm_hwsync(substream);
2911 n = snd_pcm_calc_delay(substream);
2912 snd_pcm_stream_unlock_irq(substream);
2918 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2919 struct snd_pcm_sync_ptr __user *_sync_ptr)
2921 struct snd_pcm_runtime *runtime = substream->runtime;
2922 struct snd_pcm_sync_ptr sync_ptr;
2923 volatile struct snd_pcm_mmap_status *status;
2924 volatile struct snd_pcm_mmap_control *control;
2927 memset(&sync_ptr, 0, sizeof(sync_ptr));
2928 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2930 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2932 status = runtime->status;
2933 control = runtime->control;
2934 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2935 err = snd_pcm_hwsync(substream);
2939 snd_pcm_stream_lock_irq(substream);
2940 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2941 err = pcm_lib_apply_appl_ptr(substream,
2942 sync_ptr.c.control.appl_ptr);
2944 snd_pcm_stream_unlock_irq(substream);
2948 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2950 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2951 control->avail_min = sync_ptr.c.control.avail_min;
2953 sync_ptr.c.control.avail_min = control->avail_min;
2954 sync_ptr.s.status.state = status->state;
2955 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2956 sync_ptr.s.status.tstamp = status->tstamp;
2957 sync_ptr.s.status.suspended_state = status->suspended_state;
2958 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2959 snd_pcm_stream_unlock_irq(substream);
2960 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2965 struct snd_pcm_mmap_status32 {
2966 snd_pcm_state_t state;
2971 snd_pcm_state_t suspended_state;
2972 s32 audio_tstamp_sec;
2973 s32 audio_tstamp_nsec;
2974 } __attribute__((packed));
2976 struct snd_pcm_mmap_control32 {
2981 struct snd_pcm_sync_ptr32 {
2984 struct snd_pcm_mmap_status32 status;
2985 unsigned char reserved[64];
2988 struct snd_pcm_mmap_control32 control;
2989 unsigned char reserved[64];
2991 } __attribute__((packed));
2993 /* recalcuate the boundary within 32bit */
2994 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
2996 snd_pcm_uframes_t boundary;
2998 if (! runtime->buffer_size)
3000 boundary = runtime->buffer_size;
3001 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3006 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3007 struct snd_pcm_sync_ptr32 __user *src)
3009 struct snd_pcm_runtime *runtime = substream->runtime;
3010 volatile struct snd_pcm_mmap_status *status;
3011 volatile struct snd_pcm_mmap_control *control;
3013 struct snd_pcm_mmap_control scontrol;
3014 struct snd_pcm_mmap_status sstatus;
3015 snd_pcm_uframes_t boundary;
3018 if (snd_BUG_ON(!runtime))
3021 if (get_user(sflags, &src->flags) ||
3022 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3023 get_user(scontrol.avail_min, &src->c.control.avail_min))
3025 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3026 err = snd_pcm_hwsync(substream);
3030 status = runtime->status;
3031 control = runtime->control;
3032 boundary = recalculate_boundary(runtime);
3034 boundary = 0x7fffffff;
3035 snd_pcm_stream_lock_irq(substream);
3036 /* FIXME: we should consider the boundary for the sync from app */
3037 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3038 control->appl_ptr = scontrol.appl_ptr;
3040 scontrol.appl_ptr = control->appl_ptr % boundary;
3041 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3042 control->avail_min = scontrol.avail_min;
3044 scontrol.avail_min = control->avail_min;
3045 sstatus.state = status->state;
3046 sstatus.hw_ptr = status->hw_ptr % boundary;
3047 sstatus.tstamp = status->tstamp;
3048 sstatus.suspended_state = status->suspended_state;
3049 sstatus.audio_tstamp = status->audio_tstamp;
3050 snd_pcm_stream_unlock_irq(substream);
3051 if (put_user(sstatus.state, &src->s.status.state) ||
3052 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3053 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3054 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3055 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3056 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3057 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3058 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3059 put_user(scontrol.avail_min, &src->c.control.avail_min))
3064 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3066 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3068 struct snd_pcm_runtime *runtime = substream->runtime;
3071 if (get_user(arg, _arg))
3073 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3075 runtime->tstamp_type = arg;
3079 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3080 struct snd_xferi __user *_xferi)
3082 struct snd_xferi xferi;
3083 struct snd_pcm_runtime *runtime = substream->runtime;
3084 snd_pcm_sframes_t result;
3086 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3088 if (put_user(0, &_xferi->result))
3090 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3092 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3093 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3095 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3096 if (put_user(result, &_xferi->result))
3098 return result < 0 ? result : 0;
3101 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3102 struct snd_xfern __user *_xfern)
3104 struct snd_xfern xfern;
3105 struct snd_pcm_runtime *runtime = substream->runtime;
3107 snd_pcm_sframes_t result;
3109 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3111 if (runtime->channels > 128)
3113 if (put_user(0, &_xfern->result))
3115 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3118 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3120 return PTR_ERR(bufs);
3121 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3122 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3124 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3126 if (put_user(result, &_xfern->result))
3128 return result < 0 ? result : 0;
3131 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3132 snd_pcm_uframes_t __user *_frames)
3134 snd_pcm_uframes_t frames;
3135 snd_pcm_sframes_t result;
3137 if (get_user(frames, _frames))
3139 if (put_user(0, _frames))
3141 result = snd_pcm_rewind(substream, frames);
3142 if (put_user(result, _frames))
3144 return result < 0 ? result : 0;
3147 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3148 snd_pcm_uframes_t __user *_frames)
3150 snd_pcm_uframes_t frames;
3151 snd_pcm_sframes_t result;
3153 if (get_user(frames, _frames))
3155 if (put_user(0, _frames))
3157 result = snd_pcm_forward(substream, frames);
3158 if (put_user(result, _frames))
3160 return result < 0 ? result : 0;
3163 static int snd_pcm_common_ioctl(struct file *file,
3164 struct snd_pcm_substream *substream,
3165 unsigned int cmd, void __user *arg)
3167 struct snd_pcm_file *pcm_file = file->private_data;
3170 if (PCM_RUNTIME_CHECK(substream))
3173 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
3178 case SNDRV_PCM_IOCTL_PVERSION:
3179 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3180 case SNDRV_PCM_IOCTL_INFO:
3181 return snd_pcm_info_user(substream, arg);
3182 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3184 case SNDRV_PCM_IOCTL_TTSTAMP:
3185 return snd_pcm_tstamp(substream, arg);
3186 case SNDRV_PCM_IOCTL_USER_PVERSION:
3187 if (get_user(pcm_file->user_pversion,
3188 (unsigned int __user *)arg))
3191 case SNDRV_PCM_IOCTL_HW_REFINE:
3192 return snd_pcm_hw_refine_user(substream, arg);
3193 case SNDRV_PCM_IOCTL_HW_PARAMS:
3194 return snd_pcm_hw_params_user(substream, arg);
3195 case SNDRV_PCM_IOCTL_HW_FREE:
3196 return snd_pcm_hw_free(substream);
3197 case SNDRV_PCM_IOCTL_SW_PARAMS:
3198 return snd_pcm_sw_params_user(substream, arg);
3199 case SNDRV_PCM_IOCTL_STATUS32:
3200 return snd_pcm_status_user32(substream, arg, false);
3201 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3202 return snd_pcm_status_user32(substream, arg, true);
3203 case SNDRV_PCM_IOCTL_STATUS64:
3204 return snd_pcm_status_user64(substream, arg, false);
3205 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3206 return snd_pcm_status_user64(substream, arg, true);
3207 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3208 return snd_pcm_channel_info_user(substream, arg);
3209 case SNDRV_PCM_IOCTL_PREPARE:
3210 return snd_pcm_prepare(substream, file);
3211 case SNDRV_PCM_IOCTL_RESET:
3212 return snd_pcm_reset(substream);
3213 case SNDRV_PCM_IOCTL_START:
3214 return snd_pcm_start_lock_irq(substream);
3215 case SNDRV_PCM_IOCTL_LINK:
3216 return snd_pcm_link(substream, (int)(unsigned long) arg);
3217 case SNDRV_PCM_IOCTL_UNLINK:
3218 return snd_pcm_unlink(substream);
3219 case SNDRV_PCM_IOCTL_RESUME:
3220 return snd_pcm_resume(substream);
3221 case SNDRV_PCM_IOCTL_XRUN:
3222 return snd_pcm_xrun(substream);
3223 case SNDRV_PCM_IOCTL_HWSYNC:
3224 return snd_pcm_hwsync(substream);
3225 case SNDRV_PCM_IOCTL_DELAY:
3227 snd_pcm_sframes_t delay;
3228 snd_pcm_sframes_t __user *res = arg;
3231 err = snd_pcm_delay(substream, &delay);
3234 if (put_user(delay, res))
3238 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3239 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3240 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3241 return snd_pcm_sync_ptr(substream, arg);
3242 #ifdef CONFIG_SND_SUPPORT_OLD_API
3243 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3244 return snd_pcm_hw_refine_old_user(substream, arg);
3245 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3246 return snd_pcm_hw_params_old_user(substream, arg);
3248 case SNDRV_PCM_IOCTL_DRAIN:
3249 return snd_pcm_drain(substream, file);
3250 case SNDRV_PCM_IOCTL_DROP:
3251 return snd_pcm_drop(substream);
3252 case SNDRV_PCM_IOCTL_PAUSE:
3253 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3254 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3255 case SNDRV_PCM_IOCTL_READI_FRAMES:
3256 return snd_pcm_xferi_frames_ioctl(substream, arg);
3257 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3258 case SNDRV_PCM_IOCTL_READN_FRAMES:
3259 return snd_pcm_xfern_frames_ioctl(substream, arg);
3260 case SNDRV_PCM_IOCTL_REWIND:
3261 return snd_pcm_rewind_ioctl(substream, arg);
3262 case SNDRV_PCM_IOCTL_FORWARD:
3263 return snd_pcm_forward_ioctl(substream, arg);
3265 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3269 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3272 struct snd_pcm_file *pcm_file;
3274 pcm_file = file->private_data;
3276 if (((cmd >> 8) & 0xff) != 'A')
3279 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3280 (void __user *)arg);
3284 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3285 * @substream: PCM substream
3287 * @arg: IOCTL argument
3289 * The function is provided primarily for OSS layer and USB gadget drivers,
3290 * and it allows only the limited set of ioctls (hw_params, sw_params,
3291 * prepare, start, drain, drop, forward).
3293 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3294 unsigned int cmd, void *arg)
3296 snd_pcm_uframes_t *frames = arg;
3297 snd_pcm_sframes_t result;
3300 case SNDRV_PCM_IOCTL_FORWARD:
3302 /* provided only for OSS; capture-only and no value returned */
3303 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3305 result = snd_pcm_forward(substream, *frames);
3306 return result < 0 ? result : 0;
3308 case SNDRV_PCM_IOCTL_HW_PARAMS:
3309 return snd_pcm_hw_params(substream, arg);
3310 case SNDRV_PCM_IOCTL_SW_PARAMS:
3311 return snd_pcm_sw_params(substream, arg);
3312 case SNDRV_PCM_IOCTL_PREPARE:
3313 return snd_pcm_prepare(substream, NULL);
3314 case SNDRV_PCM_IOCTL_START:
3315 return snd_pcm_start_lock_irq(substream);
3316 case SNDRV_PCM_IOCTL_DRAIN:
3317 return snd_pcm_drain(substream, NULL);
3318 case SNDRV_PCM_IOCTL_DROP:
3319 return snd_pcm_drop(substream);
3320 case SNDRV_PCM_IOCTL_DELAY:
3321 return snd_pcm_delay(substream, frames);
3326 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3328 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3331 struct snd_pcm_file *pcm_file;
3332 struct snd_pcm_substream *substream;
3333 struct snd_pcm_runtime *runtime;
3334 snd_pcm_sframes_t result;
3336 pcm_file = file->private_data;
3337 substream = pcm_file->substream;
3338 if (PCM_RUNTIME_CHECK(substream))
3340 runtime = substream->runtime;
3341 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3343 if (!frame_aligned(runtime, count))
3345 count = bytes_to_frames(runtime, count);
3346 result = snd_pcm_lib_read(substream, buf, count);
3348 result = frames_to_bytes(runtime, result);
3352 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3353 size_t count, loff_t * offset)
3355 struct snd_pcm_file *pcm_file;
3356 struct snd_pcm_substream *substream;
3357 struct snd_pcm_runtime *runtime;
3358 snd_pcm_sframes_t result;
3360 pcm_file = file->private_data;
3361 substream = pcm_file->substream;
3362 if (PCM_RUNTIME_CHECK(substream))
3364 runtime = substream->runtime;
3365 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3367 if (!frame_aligned(runtime, count))
3369 count = bytes_to_frames(runtime, count);
3370 result = snd_pcm_lib_write(substream, buf, count);
3372 result = frames_to_bytes(runtime, result);
3376 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3378 struct snd_pcm_file *pcm_file;
3379 struct snd_pcm_substream *substream;
3380 struct snd_pcm_runtime *runtime;
3381 snd_pcm_sframes_t result;
3384 snd_pcm_uframes_t frames;
3386 pcm_file = iocb->ki_filp->private_data;
3387 substream = pcm_file->substream;
3388 if (PCM_RUNTIME_CHECK(substream))
3390 runtime = substream->runtime;
3391 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3393 if (!iter_is_iovec(to))
3395 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3397 if (!frame_aligned(runtime, to->iov->iov_len))
3399 frames = bytes_to_samples(runtime, to->iov->iov_len);
3400 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3403 for (i = 0; i < to->nr_segs; ++i)
3404 bufs[i] = to->iov[i].iov_base;
3405 result = snd_pcm_lib_readv(substream, bufs, frames);
3407 result = frames_to_bytes(runtime, result);
3412 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3414 struct snd_pcm_file *pcm_file;
3415 struct snd_pcm_substream *substream;
3416 struct snd_pcm_runtime *runtime;
3417 snd_pcm_sframes_t result;
3420 snd_pcm_uframes_t frames;
3422 pcm_file = iocb->ki_filp->private_data;
3423 substream = pcm_file->substream;
3424 if (PCM_RUNTIME_CHECK(substream))
3426 runtime = substream->runtime;
3427 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3429 if (!iter_is_iovec(from))
3431 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3432 !frame_aligned(runtime, from->iov->iov_len))
3434 frames = bytes_to_samples(runtime, from->iov->iov_len);
3435 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3438 for (i = 0; i < from->nr_segs; ++i)
3439 bufs[i] = from->iov[i].iov_base;
3440 result = snd_pcm_lib_writev(substream, bufs, frames);
3442 result = frames_to_bytes(runtime, result);
3447 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3449 struct snd_pcm_file *pcm_file;
3450 struct snd_pcm_substream *substream;
3451 struct snd_pcm_runtime *runtime;
3453 snd_pcm_uframes_t avail;
3455 pcm_file = file->private_data;
3457 substream = pcm_file->substream;
3458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3459 ok = EPOLLOUT | EPOLLWRNORM;
3461 ok = EPOLLIN | EPOLLRDNORM;
3462 if (PCM_RUNTIME_CHECK(substream))
3463 return ok | EPOLLERR;
3465 runtime = substream->runtime;
3466 poll_wait(file, &runtime->sleep, wait);
3469 snd_pcm_stream_lock_irq(substream);
3470 avail = snd_pcm_avail(substream);
3471 switch (runtime->status->state) {
3472 case SNDRV_PCM_STATE_RUNNING:
3473 case SNDRV_PCM_STATE_PREPARED:
3474 case SNDRV_PCM_STATE_PAUSED:
3475 if (avail >= runtime->control->avail_min)
3478 case SNDRV_PCM_STATE_DRAINING:
3479 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3486 mask = ok | EPOLLERR;
3489 snd_pcm_stream_unlock_irq(substream);
3498 * Only on coherent architectures, we can mmap the status and the control records
3499 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3501 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3503 * mmap status record
3505 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3507 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3508 struct snd_pcm_runtime *runtime;
3510 if (substream == NULL)
3511 return VM_FAULT_SIGBUS;
3512 runtime = substream->runtime;
3513 vmf->page = virt_to_page(runtime->status);
3514 get_page(vmf->page);
3518 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3520 .fault = snd_pcm_mmap_status_fault,
3523 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3524 struct vm_area_struct *area)
3527 if (!(area->vm_flags & VM_READ))
3529 size = area->vm_end - area->vm_start;
3530 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3532 area->vm_ops = &snd_pcm_vm_ops_status;
3533 area->vm_private_data = substream;
3534 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3539 * mmap control record
3541 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3543 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3544 struct snd_pcm_runtime *runtime;
3546 if (substream == NULL)
3547 return VM_FAULT_SIGBUS;
3548 runtime = substream->runtime;
3549 vmf->page = virt_to_page(runtime->control);
3550 get_page(vmf->page);
3554 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3556 .fault = snd_pcm_mmap_control_fault,
3559 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3560 struct vm_area_struct *area)
3563 if (!(area->vm_flags & VM_READ))
3565 size = area->vm_end - area->vm_start;
3566 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3568 area->vm_ops = &snd_pcm_vm_ops_control;
3569 area->vm_private_data = substream;
3570 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3574 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3576 /* See pcm_control_mmap_allowed() below.
3577 * Since older alsa-lib requires both status and control mmaps to be
3578 * coupled, we have to disable the status mmap for old alsa-lib, too.
3580 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3581 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3586 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3588 if (pcm_file->no_compat_mmap)
3590 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3591 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3592 * thus it effectively assures the manual update of appl_ptr.
3594 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3599 #else /* ! coherent mmap */
3601 * don't support mmap for status and control records.
3603 #define pcm_status_mmap_allowed(pcm_file) false
3604 #define pcm_control_mmap_allowed(pcm_file) false
3606 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3607 struct vm_area_struct *area)
3611 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3612 struct vm_area_struct *area)
3616 #endif /* coherent mmap */
3618 static inline struct page *
3619 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3621 void *vaddr = substream->runtime->dma_area + ofs;
3623 switch (substream->dma_buffer.dev.type) {
3624 #ifdef CONFIG_SND_DMA_SGBUF
3625 case SNDRV_DMA_TYPE_DEV_SG:
3626 case SNDRV_DMA_TYPE_DEV_UC_SG:
3627 return snd_pcm_sgbuf_ops_page(substream, ofs);
3628 #endif /* CONFIG_SND_DMA_SGBUF */
3629 case SNDRV_DMA_TYPE_VMALLOC:
3630 return vmalloc_to_page(vaddr);
3632 return virt_to_page(vaddr);
3637 * fault callback for mmapping a RAM page
3639 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3641 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3642 struct snd_pcm_runtime *runtime;
3643 unsigned long offset;
3647 if (substream == NULL)
3648 return VM_FAULT_SIGBUS;
3649 runtime = substream->runtime;
3650 offset = vmf->pgoff << PAGE_SHIFT;
3651 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3652 if (offset > dma_bytes - PAGE_SIZE)
3653 return VM_FAULT_SIGBUS;
3654 if (substream->ops->page)
3655 page = substream->ops->page(substream, offset);
3657 page = snd_pcm_default_page_ops(substream, offset);
3659 return VM_FAULT_SIGBUS;
3665 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3666 .open = snd_pcm_mmap_data_open,
3667 .close = snd_pcm_mmap_data_close,
3670 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3671 .open = snd_pcm_mmap_data_open,
3672 .close = snd_pcm_mmap_data_close,
3673 .fault = snd_pcm_mmap_data_fault,
3677 * mmap the DMA buffer on RAM
3681 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3682 * @substream: PCM substream
3685 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3686 * this function is invoked implicitly.
3688 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3689 struct vm_area_struct *area)
3691 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3692 #ifdef CONFIG_GENERIC_ALLOCATOR
3693 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3694 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3695 return remap_pfn_range(area, area->vm_start,
3696 substream->dma_buffer.addr >> PAGE_SHIFT,
3697 area->vm_end - area->vm_start, area->vm_page_prot);
3699 #endif /* CONFIG_GENERIC_ALLOCATOR */
3700 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3701 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3702 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3703 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3704 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3706 substream->runtime->dma_area,
3707 substream->runtime->dma_addr,
3708 substream->runtime->dma_bytes);
3709 #endif /* CONFIG_X86 */
3710 /* mmap with fault handler */
3711 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3714 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3717 * mmap the DMA buffer on I/O memory area
3719 #if SNDRV_PCM_INFO_MMAP_IOMEM
3721 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3722 * @substream: PCM substream
3725 * When your hardware uses the iomapped pages as the hardware buffer and
3726 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3727 * is supposed to work only on limited architectures.
3729 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3730 struct vm_area_struct *area)
3732 struct snd_pcm_runtime *runtime = substream->runtime;
3734 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3735 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3737 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3738 #endif /* SNDRV_PCM_INFO_MMAP */
3743 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3744 struct vm_area_struct *area)
3746 struct snd_pcm_runtime *runtime;
3748 unsigned long offset;
3752 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3753 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3756 if (!(area->vm_flags & VM_READ))
3759 runtime = substream->runtime;
3760 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3762 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3764 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3765 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3767 size = area->vm_end - area->vm_start;
3768 offset = area->vm_pgoff << PAGE_SHIFT;
3769 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3770 if ((size_t)size > dma_bytes)
3772 if (offset > dma_bytes - size)
3775 area->vm_ops = &snd_pcm_vm_ops_data;
3776 area->vm_private_data = substream;
3777 if (substream->ops->mmap)
3778 err = substream->ops->mmap(substream, area);
3780 err = snd_pcm_lib_default_mmap(substream, area);
3782 atomic_inc(&substream->mmap_count);
3785 EXPORT_SYMBOL(snd_pcm_mmap_data);
3787 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3789 struct snd_pcm_file * pcm_file;
3790 struct snd_pcm_substream *substream;
3791 unsigned long offset;
3793 pcm_file = file->private_data;
3794 substream = pcm_file->substream;
3795 if (PCM_RUNTIME_CHECK(substream))
3798 offset = area->vm_pgoff << PAGE_SHIFT;
3800 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3801 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3804 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3805 if (!pcm_status_mmap_allowed(pcm_file))
3807 return snd_pcm_mmap_status(substream, file, area);
3808 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3809 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3812 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3813 if (!pcm_control_mmap_allowed(pcm_file))
3815 return snd_pcm_mmap_control(substream, file, area);
3817 return snd_pcm_mmap_data(substream, file, area);
3822 static int snd_pcm_fasync(int fd, struct file * file, int on)
3824 struct snd_pcm_file * pcm_file;
3825 struct snd_pcm_substream *substream;
3826 struct snd_pcm_runtime *runtime;
3828 pcm_file = file->private_data;
3829 substream = pcm_file->substream;
3830 if (PCM_RUNTIME_CHECK(substream))
3832 runtime = substream->runtime;
3833 return fasync_helper(fd, file, on, &runtime->fasync);
3839 #ifdef CONFIG_COMPAT
3840 #include "pcm_compat.c"
3842 #define snd_pcm_ioctl_compat NULL
3846 * To be removed helpers to keep binary compatibility
3849 #ifdef CONFIG_SND_SUPPORT_OLD_API
3850 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3851 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3853 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3854 struct snd_pcm_hw_params_old *oparams)
3858 memset(params, 0, sizeof(*params));
3859 params->flags = oparams->flags;
3860 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3861 params->masks[i].bits[0] = oparams->masks[i];
3862 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3863 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3864 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3865 params->info = oparams->info;
3866 params->msbits = oparams->msbits;
3867 params->rate_num = oparams->rate_num;
3868 params->rate_den = oparams->rate_den;
3869 params->fifo_size = oparams->fifo_size;
3872 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3873 struct snd_pcm_hw_params *params)
3877 memset(oparams, 0, sizeof(*oparams));
3878 oparams->flags = params->flags;
3879 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3880 oparams->masks[i] = params->masks[i].bits[0];
3881 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3882 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3883 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3884 oparams->info = params->info;
3885 oparams->msbits = params->msbits;
3886 oparams->rate_num = params->rate_num;
3887 oparams->rate_den = params->rate_den;
3888 oparams->fifo_size = params->fifo_size;
3891 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3892 struct snd_pcm_hw_params_old __user * _oparams)
3894 struct snd_pcm_hw_params *params;
3895 struct snd_pcm_hw_params_old *oparams = NULL;
3898 params = kmalloc(sizeof(*params), GFP_KERNEL);
3902 oparams = memdup_user(_oparams, sizeof(*oparams));
3903 if (IS_ERR(oparams)) {
3904 err = PTR_ERR(oparams);
3907 snd_pcm_hw_convert_from_old_params(params, oparams);
3908 err = snd_pcm_hw_refine(substream, params);
3912 err = fixup_unreferenced_params(substream, params);
3916 snd_pcm_hw_convert_to_old_params(oparams, params);
3917 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3926 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3927 struct snd_pcm_hw_params_old __user * _oparams)
3929 struct snd_pcm_hw_params *params;
3930 struct snd_pcm_hw_params_old *oparams = NULL;
3933 params = kmalloc(sizeof(*params), GFP_KERNEL);
3937 oparams = memdup_user(_oparams, sizeof(*oparams));
3938 if (IS_ERR(oparams)) {
3939 err = PTR_ERR(oparams);
3943 snd_pcm_hw_convert_from_old_params(params, oparams);
3944 err = snd_pcm_hw_params(substream, params);
3948 snd_pcm_hw_convert_to_old_params(oparams, params);
3949 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3957 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3960 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3963 unsigned long pgoff,
3964 unsigned long flags)
3966 struct snd_pcm_file *pcm_file = file->private_data;
3967 struct snd_pcm_substream *substream = pcm_file->substream;
3968 struct snd_pcm_runtime *runtime = substream->runtime;
3969 unsigned long offset = pgoff << PAGE_SHIFT;
3972 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3973 return (unsigned long)runtime->status;
3974 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3975 return (unsigned long)runtime->control;
3977 return (unsigned long)runtime->dma_area + offset;
3981 # define snd_pcm_get_unmapped_area NULL
3988 const struct file_operations snd_pcm_f_ops[2] = {
3990 .owner = THIS_MODULE,
3991 .write = snd_pcm_write,
3992 .write_iter = snd_pcm_writev,
3993 .open = snd_pcm_playback_open,
3994 .release = snd_pcm_release,
3995 .llseek = no_llseek,
3996 .poll = snd_pcm_poll,
3997 .unlocked_ioctl = snd_pcm_ioctl,
3998 .compat_ioctl = snd_pcm_ioctl_compat,
3999 .mmap = snd_pcm_mmap,
4000 .fasync = snd_pcm_fasync,
4001 .get_unmapped_area = snd_pcm_get_unmapped_area,
4004 .owner = THIS_MODULE,
4005 .read = snd_pcm_read,
4006 .read_iter = snd_pcm_readv,
4007 .open = snd_pcm_capture_open,
4008 .release = snd_pcm_release,
4009 .llseek = no_llseek,
4010 .poll = snd_pcm_poll,
4011 .unlocked_ioctl = snd_pcm_ioctl,
4012 .compat_ioctl = snd_pcm_ioctl_compat,
4013 .mmap = snd_pcm_mmap,
4014 .fasync = snd_pcm_fasync,
4015 .get_unmapped_area = snd_pcm_get_unmapped_area,