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_unlock - 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);
141 static void snd_pcm_stream_lock_nested(struct snd_pcm_substream *substream)
143 struct snd_pcm_group *group = &substream->self_group;
145 if (substream->pcm->nonatomic)
146 mutex_lock_nested(&group->mutex, SINGLE_DEPTH_NESTING);
148 spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
152 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
153 * @substream: PCM substream
155 * This is a counter-part of snd_pcm_stream_lock_irq().
157 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
159 snd_pcm_group_unlock_irq(&substream->self_group,
160 substream->pcm->nonatomic);
162 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
164 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
166 unsigned long flags = 0;
167 if (substream->pcm->nonatomic)
168 mutex_lock(&substream->self_group.mutex);
170 spin_lock_irqsave(&substream->self_group.lock, flags);
173 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
176 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
177 * @substream: PCM substream
180 * This is a counter-part of snd_pcm_stream_lock_irqsave().
182 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
185 if (substream->pcm->nonatomic)
186 mutex_unlock(&substream->self_group.mutex);
188 spin_unlock_irqrestore(&substream->self_group.lock, flags);
190 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
192 /* Run PCM ioctl ops */
193 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
194 unsigned cmd, void *arg)
196 if (substream->ops->ioctl)
197 return substream->ops->ioctl(substream, cmd, arg);
199 return snd_pcm_lib_ioctl(substream, cmd, arg);
202 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
204 struct snd_pcm *pcm = substream->pcm;
205 struct snd_pcm_str *pstr = substream->pstr;
207 memset(info, 0, sizeof(*info));
208 info->card = pcm->card->number;
209 info->device = pcm->device;
210 info->stream = substream->stream;
211 info->subdevice = substream->number;
212 strscpy(info->id, pcm->id, sizeof(info->id));
213 strscpy(info->name, pcm->name, sizeof(info->name));
214 info->dev_class = pcm->dev_class;
215 info->dev_subclass = pcm->dev_subclass;
216 info->subdevices_count = pstr->substream_count;
217 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
218 strscpy(info->subname, substream->name, sizeof(info->subname));
223 int snd_pcm_info_user(struct snd_pcm_substream *substream,
224 struct snd_pcm_info __user * _info)
226 struct snd_pcm_info *info;
229 info = kmalloc(sizeof(*info), GFP_KERNEL);
232 err = snd_pcm_info(substream, info);
234 if (copy_to_user(_info, info, sizeof(*info)))
241 /* macro for simplified cast */
242 #define PARAM_MASK_BIT(b) (1U << (__force int)(b))
244 static bool hw_support_mmap(struct snd_pcm_substream *substream)
246 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
249 if (substream->ops->mmap)
252 switch (substream->dma_buffer.dev.type) {
253 case SNDRV_DMA_TYPE_UNKNOWN:
255 case SNDRV_DMA_TYPE_CONTINUOUS:
256 case SNDRV_DMA_TYPE_VMALLOC:
259 return dma_can_mmap(substream->dma_buffer.dev.dev);
263 static int constrain_mask_params(struct snd_pcm_substream *substream,
264 struct snd_pcm_hw_params *params)
266 struct snd_pcm_hw_constraints *constrs =
267 &substream->runtime->hw_constraints;
270 struct snd_mask old_mask;
273 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
274 m = hw_param_mask(params, k);
275 if (snd_mask_empty(m))
278 /* This parameter is not requested to change by a caller. */
279 if (!(params->rmask & PARAM_MASK_BIT(k)))
282 if (trace_hw_mask_param_enabled())
285 changed = snd_mask_refine(m, constrs_mask(constrs, k));
291 /* Set corresponding flag so that the caller gets it. */
292 trace_hw_mask_param(substream, k, 0, &old_mask, m);
293 params->cmask |= PARAM_MASK_BIT(k);
299 static int constrain_interval_params(struct snd_pcm_substream *substream,
300 struct snd_pcm_hw_params *params)
302 struct snd_pcm_hw_constraints *constrs =
303 &substream->runtime->hw_constraints;
304 struct snd_interval *i;
306 struct snd_interval old_interval;
309 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
310 i = hw_param_interval(params, k);
311 if (snd_interval_empty(i))
314 /* This parameter is not requested to change by a caller. */
315 if (!(params->rmask & PARAM_MASK_BIT(k)))
318 if (trace_hw_interval_param_enabled())
321 changed = snd_interval_refine(i, constrs_interval(constrs, k));
327 /* Set corresponding flag so that the caller gets it. */
328 trace_hw_interval_param(substream, k, 0, &old_interval, i);
329 params->cmask |= PARAM_MASK_BIT(k);
335 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params *params)
338 struct snd_pcm_hw_constraints *constrs =
339 &substream->runtime->hw_constraints;
341 unsigned int *rstamps;
342 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
344 struct snd_pcm_hw_rule *r;
346 struct snd_mask old_mask;
347 struct snd_interval old_interval;
349 int changed, err = 0;
352 * Each application of rule has own sequence number.
354 * Each member of 'rstamps' array represents the sequence number of
355 * recent application of corresponding rule.
357 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
362 * Each member of 'vstamps' array represents the sequence number of
363 * recent application of rule in which corresponding parameters were
366 * In initial state, elements corresponding to parameters requested by
367 * a caller is 1. For unrequested parameters, corresponding members
368 * have 0 so that the parameters are never changed anymore.
370 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
371 vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
373 /* Due to the above design, actual sequence number starts at 2. */
376 /* Apply all rules in order. */
378 for (k = 0; k < constrs->rules_num; k++) {
379 r = &constrs->rules[k];
382 * Check condition bits of this rule. When the rule has
383 * some condition bits, parameter without the bits is
384 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
385 * is an example of the condition bits.
387 if (r->cond && !(r->cond & params->flags))
391 * The 'deps' array includes maximum four dependencies
392 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth
393 * member of this array is a sentinel and should be
396 * This rule should be processed in this time when dependent
397 * parameters were changed at former applications of the other
400 for (d = 0; r->deps[d] >= 0; d++) {
401 if (vstamps[r->deps[d]] > rstamps[k])
407 if (trace_hw_mask_param_enabled()) {
408 if (hw_is_mask(r->var))
409 old_mask = *hw_param_mask(params, r->var);
411 if (trace_hw_interval_param_enabled()) {
412 if (hw_is_interval(r->var))
413 old_interval = *hw_param_interval(params, r->var);
416 changed = r->func(params, r);
423 * When the parameter is changed, notify it to the caller
424 * by corresponding returned bit, then preparing for next
427 if (changed && r->var >= 0) {
428 if (hw_is_mask(r->var)) {
429 trace_hw_mask_param(substream, r->var,
431 hw_param_mask(params, r->var));
433 if (hw_is_interval(r->var)) {
434 trace_hw_interval_param(substream, r->var,
435 k + 1, &old_interval,
436 hw_param_interval(params, r->var));
439 params->cmask |= PARAM_MASK_BIT(r->var);
440 vstamps[r->var] = stamp;
444 rstamps[k] = stamp++;
447 /* Iterate to evaluate all rules till no parameters are changed. */
456 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
457 struct snd_pcm_hw_params *params)
459 const struct snd_interval *i;
460 const struct snd_mask *m;
463 if (!params->msbits) {
464 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
465 if (snd_interval_single(i))
466 params->msbits = snd_interval_value(i);
469 if (!params->rate_den) {
470 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
471 if (snd_interval_single(i)) {
472 params->rate_num = snd_interval_value(i);
473 params->rate_den = 1;
477 if (!params->fifo_size) {
478 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
479 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
480 if (snd_mask_single(m) && snd_interval_single(i)) {
481 err = snd_pcm_ops_ioctl(substream,
482 SNDRV_PCM_IOCTL1_FIFO_SIZE,
490 params->info = substream->runtime->hw.info;
491 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
492 SNDRV_PCM_INFO_DRAIN_TRIGGER);
493 if (!hw_support_mmap(substream))
494 params->info &= ~(SNDRV_PCM_INFO_MMAP |
495 SNDRV_PCM_INFO_MMAP_VALID);
501 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
502 struct snd_pcm_hw_params *params)
507 params->fifo_size = 0;
508 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
510 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
511 params->rate_num = 0;
512 params->rate_den = 0;
515 err = constrain_mask_params(substream, params);
519 err = constrain_interval_params(substream, params);
523 err = constrain_params_by_rules(substream, params);
531 EXPORT_SYMBOL(snd_pcm_hw_refine);
533 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
534 struct snd_pcm_hw_params __user * _params)
536 struct snd_pcm_hw_params *params;
539 params = memdup_user(_params, sizeof(*params));
541 return PTR_ERR(params);
543 err = snd_pcm_hw_refine(substream, params);
547 err = fixup_unreferenced_params(substream, params);
551 if (copy_to_user(_params, params, sizeof(*params)))
558 static int period_to_usecs(struct snd_pcm_runtime *runtime)
563 return -1; /* invalid */
565 /* take 75% of period time as the deadline */
566 usecs = (750000 / runtime->rate) * runtime->period_size;
567 usecs += ((750000 % runtime->rate) * runtime->period_size) /
573 static void snd_pcm_set_state(struct snd_pcm_substream *substream,
574 snd_pcm_state_t state)
576 snd_pcm_stream_lock_irq(substream);
577 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
578 substream->runtime->status->state = state;
579 snd_pcm_stream_unlock_irq(substream);
582 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
585 #ifdef CONFIG_SND_PCM_TIMER
586 if (substream->timer)
587 snd_timer_notify(substream->timer, event,
588 &substream->runtime->trigger_tstamp);
592 void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq)
594 if (substream->runtime && substream->runtime->stop_operating) {
595 substream->runtime->stop_operating = false;
596 if (substream->ops && substream->ops->sync_stop)
597 substream->ops->sync_stop(substream);
598 else if (sync_irq && substream->pcm->card->sync_irq > 0)
599 synchronize_irq(substream->pcm->card->sync_irq);
604 * snd_pcm_hw_params_choose - choose a configuration defined by @params
606 * @params: the hw_params instance
608 * Choose one configuration from configuration space defined by @params.
609 * The configuration chosen is that obtained fixing in this order:
610 * first access, first format, first subformat, min channels,
611 * min rate, min period time, max buffer size, min tick time
613 * Return: Zero if successful, or a negative error code on failure.
615 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
616 struct snd_pcm_hw_params *params)
618 static const int vars[] = {
619 SNDRV_PCM_HW_PARAM_ACCESS,
620 SNDRV_PCM_HW_PARAM_FORMAT,
621 SNDRV_PCM_HW_PARAM_SUBFORMAT,
622 SNDRV_PCM_HW_PARAM_CHANNELS,
623 SNDRV_PCM_HW_PARAM_RATE,
624 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
625 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
626 SNDRV_PCM_HW_PARAM_TICK_TIME,
630 struct snd_mask old_mask;
631 struct snd_interval old_interval;
634 for (v = vars; *v != -1; v++) {
635 /* Keep old parameter to trace. */
636 if (trace_hw_mask_param_enabled()) {
638 old_mask = *hw_param_mask(params, *v);
640 if (trace_hw_interval_param_enabled()) {
641 if (hw_is_interval(*v))
642 old_interval = *hw_param_interval(params, *v);
644 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
645 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
647 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
653 /* Trace the changed parameter. */
654 if (hw_is_mask(*v)) {
655 trace_hw_mask_param(pcm, *v, 0, &old_mask,
656 hw_param_mask(params, *v));
658 if (hw_is_interval(*v)) {
659 trace_hw_interval_param(pcm, *v, 0, &old_interval,
660 hw_param_interval(params, *v));
667 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
668 struct snd_pcm_hw_params *params)
670 struct snd_pcm_runtime *runtime;
673 snd_pcm_uframes_t frames;
675 if (PCM_RUNTIME_CHECK(substream))
677 runtime = substream->runtime;
678 snd_pcm_stream_lock_irq(substream);
679 switch (runtime->status->state) {
680 case SNDRV_PCM_STATE_OPEN:
681 case SNDRV_PCM_STATE_SETUP:
682 case SNDRV_PCM_STATE_PREPARED:
685 snd_pcm_stream_unlock_irq(substream);
688 snd_pcm_stream_unlock_irq(substream);
689 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
690 if (!substream->oss.oss)
692 if (atomic_read(&substream->mmap_count))
695 snd_pcm_sync_stop(substream, true);
698 err = snd_pcm_hw_refine(substream, params);
702 err = snd_pcm_hw_params_choose(substream, params);
706 err = fixup_unreferenced_params(substream, params);
710 if (substream->managed_buffer_alloc) {
711 err = snd_pcm_lib_malloc_pages(substream,
712 params_buffer_bytes(params));
715 runtime->buffer_changed = err > 0;
718 if (substream->ops->hw_params != NULL) {
719 err = substream->ops->hw_params(substream, params);
724 runtime->access = params_access(params);
725 runtime->format = params_format(params);
726 runtime->subformat = params_subformat(params);
727 runtime->channels = params_channels(params);
728 runtime->rate = params_rate(params);
729 runtime->period_size = params_period_size(params);
730 runtime->periods = params_periods(params);
731 runtime->buffer_size = params_buffer_size(params);
732 runtime->info = params->info;
733 runtime->rate_num = params->rate_num;
734 runtime->rate_den = params->rate_den;
735 runtime->no_period_wakeup =
736 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
737 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
739 bits = snd_pcm_format_physical_width(runtime->format);
740 runtime->sample_bits = bits;
741 bits *= runtime->channels;
742 runtime->frame_bits = bits;
744 while (bits % 8 != 0) {
748 runtime->byte_align = bits / 8;
749 runtime->min_align = frames;
751 /* Default sw params */
752 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
753 runtime->period_step = 1;
754 runtime->control->avail_min = runtime->period_size;
755 runtime->start_threshold = 1;
756 runtime->stop_threshold = runtime->buffer_size;
757 runtime->silence_threshold = 0;
758 runtime->silence_size = 0;
759 runtime->boundary = runtime->buffer_size;
760 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
761 runtime->boundary *= 2;
763 /* clear the buffer for avoiding possible kernel info leaks */
764 if (runtime->dma_area && !substream->ops->copy_user) {
765 size_t size = runtime->dma_bytes;
767 if (runtime->info & SNDRV_PCM_INFO_MMAP)
768 size = PAGE_ALIGN(size);
769 memset(runtime->dma_area, 0, size);
772 snd_pcm_timer_resolution_change(substream);
773 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
775 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
776 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
777 usecs = period_to_usecs(runtime);
779 cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
783 /* hardware might be unusable from this time,
784 so we force application to retry to set
785 the correct hardware parameter settings */
786 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
787 if (substream->ops->hw_free != NULL)
788 substream->ops->hw_free(substream);
789 if (substream->managed_buffer_alloc)
790 snd_pcm_lib_free_pages(substream);
794 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
795 struct snd_pcm_hw_params __user * _params)
797 struct snd_pcm_hw_params *params;
800 params = memdup_user(_params, sizeof(*params));
802 return PTR_ERR(params);
804 err = snd_pcm_hw_params(substream, params);
808 if (copy_to_user(_params, params, sizeof(*params)))
815 static int do_hw_free(struct snd_pcm_substream *substream)
819 snd_pcm_sync_stop(substream, true);
820 if (substream->ops->hw_free)
821 result = substream->ops->hw_free(substream);
822 if (substream->managed_buffer_alloc)
823 snd_pcm_lib_free_pages(substream);
827 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
829 struct snd_pcm_runtime *runtime;
832 if (PCM_RUNTIME_CHECK(substream))
834 runtime = substream->runtime;
835 snd_pcm_stream_lock_irq(substream);
836 switch (runtime->status->state) {
837 case SNDRV_PCM_STATE_SETUP:
838 case SNDRV_PCM_STATE_PREPARED:
841 snd_pcm_stream_unlock_irq(substream);
844 snd_pcm_stream_unlock_irq(substream);
845 if (atomic_read(&substream->mmap_count))
847 result = do_hw_free(substream);
848 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
849 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
853 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
854 struct snd_pcm_sw_params *params)
856 struct snd_pcm_runtime *runtime;
859 if (PCM_RUNTIME_CHECK(substream))
861 runtime = substream->runtime;
862 snd_pcm_stream_lock_irq(substream);
863 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
864 snd_pcm_stream_unlock_irq(substream);
867 snd_pcm_stream_unlock_irq(substream);
869 if (params->tstamp_mode < 0 ||
870 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
872 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
873 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
875 if (params->avail_min == 0)
877 if (params->silence_size >= runtime->boundary) {
878 if (params->silence_threshold != 0)
881 if (params->silence_size > params->silence_threshold)
883 if (params->silence_threshold > runtime->buffer_size)
887 snd_pcm_stream_lock_irq(substream);
888 runtime->tstamp_mode = params->tstamp_mode;
889 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
890 runtime->tstamp_type = params->tstamp_type;
891 runtime->period_step = params->period_step;
892 runtime->control->avail_min = params->avail_min;
893 runtime->start_threshold = params->start_threshold;
894 runtime->stop_threshold = params->stop_threshold;
895 runtime->silence_threshold = params->silence_threshold;
896 runtime->silence_size = params->silence_size;
897 params->boundary = runtime->boundary;
898 if (snd_pcm_running(substream)) {
899 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
900 runtime->silence_size > 0)
901 snd_pcm_playback_silence(substream, ULONG_MAX);
902 err = snd_pcm_update_state(substream, runtime);
904 snd_pcm_stream_unlock_irq(substream);
908 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
909 struct snd_pcm_sw_params __user * _params)
911 struct snd_pcm_sw_params params;
913 if (copy_from_user(¶ms, _params, sizeof(params)))
915 err = snd_pcm_sw_params(substream, ¶ms);
916 if (copy_to_user(_params, ¶ms, sizeof(params)))
921 static inline snd_pcm_uframes_t
922 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
924 snd_pcm_uframes_t delay;
926 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
927 delay = snd_pcm_playback_hw_avail(substream->runtime);
929 delay = snd_pcm_capture_avail(substream->runtime);
930 return delay + substream->runtime->delay;
933 int snd_pcm_status64(struct snd_pcm_substream *substream,
934 struct snd_pcm_status64 *status)
936 struct snd_pcm_runtime *runtime = substream->runtime;
938 snd_pcm_stream_lock_irq(substream);
940 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
941 &runtime->audio_tstamp_config);
943 /* backwards compatible behavior */
944 if (runtime->audio_tstamp_config.type_requested ==
945 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
946 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
947 runtime->audio_tstamp_config.type_requested =
948 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
950 runtime->audio_tstamp_config.type_requested =
951 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
952 runtime->audio_tstamp_report.valid = 0;
954 runtime->audio_tstamp_report.valid = 1;
956 status->state = runtime->status->state;
957 status->suspended_state = runtime->status->suspended_state;
958 if (status->state == SNDRV_PCM_STATE_OPEN)
960 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
961 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
962 if (snd_pcm_running(substream)) {
963 snd_pcm_update_hw_ptr(substream);
964 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
965 status->tstamp_sec = runtime->status->tstamp.tv_sec;
966 status->tstamp_nsec =
967 runtime->status->tstamp.tv_nsec;
968 status->driver_tstamp_sec =
969 runtime->driver_tstamp.tv_sec;
970 status->driver_tstamp_nsec =
971 runtime->driver_tstamp.tv_nsec;
972 status->audio_tstamp_sec =
973 runtime->status->audio_tstamp.tv_sec;
974 status->audio_tstamp_nsec =
975 runtime->status->audio_tstamp.tv_nsec;
976 if (runtime->audio_tstamp_report.valid == 1)
977 /* backwards compatibility, no report provided in COMPAT mode */
978 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
979 &status->audio_tstamp_accuracy,
980 &runtime->audio_tstamp_report);
985 /* get tstamp only in fallback mode and only if enabled */
986 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
987 struct timespec64 tstamp;
989 snd_pcm_gettime(runtime, &tstamp);
990 status->tstamp_sec = tstamp.tv_sec;
991 status->tstamp_nsec = tstamp.tv_nsec;
995 status->appl_ptr = runtime->control->appl_ptr;
996 status->hw_ptr = runtime->status->hw_ptr;
997 status->avail = snd_pcm_avail(substream);
998 status->delay = snd_pcm_running(substream) ?
999 snd_pcm_calc_delay(substream) : 0;
1000 status->avail_max = runtime->avail_max;
1001 status->overrange = runtime->overrange;
1002 runtime->avail_max = 0;
1003 runtime->overrange = 0;
1005 snd_pcm_stream_unlock_irq(substream);
1009 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
1010 struct snd_pcm_status64 __user * _status,
1013 struct snd_pcm_status64 status;
1016 memset(&status, 0, sizeof(status));
1018 * with extension, parameters are read/write,
1019 * get audio_tstamp_data from user,
1020 * ignore rest of status structure
1022 if (ext && get_user(status.audio_tstamp_data,
1023 (u32 __user *)(&_status->audio_tstamp_data)))
1025 res = snd_pcm_status64(substream, &status);
1028 if (copy_to_user(_status, &status, sizeof(status)))
1033 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1034 struct snd_pcm_status32 __user * _status,
1037 struct snd_pcm_status64 status64;
1038 struct snd_pcm_status32 status32;
1041 memset(&status64, 0, sizeof(status64));
1042 memset(&status32, 0, sizeof(status32));
1044 * with extension, parameters are read/write,
1045 * get audio_tstamp_data from user,
1046 * ignore rest of status structure
1048 if (ext && get_user(status64.audio_tstamp_data,
1049 (u32 __user *)(&_status->audio_tstamp_data)))
1051 res = snd_pcm_status64(substream, &status64);
1055 status32 = (struct snd_pcm_status32) {
1056 .state = status64.state,
1057 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1058 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1059 .tstamp_sec = status64.tstamp_sec,
1060 .tstamp_nsec = status64.tstamp_nsec,
1061 .appl_ptr = status64.appl_ptr,
1062 .hw_ptr = status64.hw_ptr,
1063 .delay = status64.delay,
1064 .avail = status64.avail,
1065 .avail_max = status64.avail_max,
1066 .overrange = status64.overrange,
1067 .suspended_state = status64.suspended_state,
1068 .audio_tstamp_data = status64.audio_tstamp_data,
1069 .audio_tstamp_sec = status64.audio_tstamp_sec,
1070 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1071 .driver_tstamp_sec = status64.audio_tstamp_sec,
1072 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1073 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1076 if (copy_to_user(_status, &status32, sizeof(status32)))
1082 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1083 struct snd_pcm_channel_info * info)
1085 struct snd_pcm_runtime *runtime;
1086 unsigned int channel;
1088 channel = info->channel;
1089 runtime = substream->runtime;
1090 snd_pcm_stream_lock_irq(substream);
1091 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1092 snd_pcm_stream_unlock_irq(substream);
1095 snd_pcm_stream_unlock_irq(substream);
1096 if (channel >= runtime->channels)
1098 memset(info, 0, sizeof(*info));
1099 info->channel = channel;
1100 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1103 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1104 struct snd_pcm_channel_info __user * _info)
1106 struct snd_pcm_channel_info info;
1109 if (copy_from_user(&info, _info, sizeof(info)))
1111 res = snd_pcm_channel_info(substream, &info);
1114 if (copy_to_user(_info, &info, sizeof(info)))
1119 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1121 struct snd_pcm_runtime *runtime = substream->runtime;
1122 if (runtime->trigger_master == NULL)
1124 if (runtime->trigger_master == substream) {
1125 if (!runtime->trigger_tstamp_latched)
1126 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1128 snd_pcm_trigger_tstamp(runtime->trigger_master);
1129 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1131 runtime->trigger_master = NULL;
1134 #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1137 int (*pre_action)(struct snd_pcm_substream *substream,
1138 snd_pcm_state_t state);
1139 int (*do_action)(struct snd_pcm_substream *substream,
1140 snd_pcm_state_t state);
1141 void (*undo_action)(struct snd_pcm_substream *substream,
1142 snd_pcm_state_t state);
1143 void (*post_action)(struct snd_pcm_substream *substream,
1144 snd_pcm_state_t state);
1148 * this functions is core for handling of linked stream
1149 * Note: the stream state might be changed also on failure
1150 * Note2: call with calling stream lock + link lock
1152 static int snd_pcm_action_group(const struct action_ops *ops,
1153 struct snd_pcm_substream *substream,
1154 snd_pcm_state_t state,
1157 struct snd_pcm_substream *s = NULL;
1158 struct snd_pcm_substream *s1;
1159 int res = 0, depth = 1;
1161 snd_pcm_group_for_each_entry(s, substream) {
1162 if (do_lock && s != substream) {
1163 if (s->pcm->nonatomic)
1164 mutex_lock_nested(&s->self_group.mutex, depth);
1166 spin_lock_nested(&s->self_group.lock, depth);
1169 res = ops->pre_action(s, state);
1173 snd_pcm_group_for_each_entry(s, substream) {
1174 res = ops->do_action(s, state);
1176 if (ops->undo_action) {
1177 snd_pcm_group_for_each_entry(s1, substream) {
1178 if (s1 == s) /* failed stream */
1180 ops->undo_action(s1, state);
1183 s = NULL; /* unlock all */
1187 snd_pcm_group_for_each_entry(s, substream) {
1188 ops->post_action(s, state);
1192 /* unlock streams */
1193 snd_pcm_group_for_each_entry(s1, substream) {
1194 if (s1 != substream) {
1195 if (s1->pcm->nonatomic)
1196 mutex_unlock(&s1->self_group.mutex);
1198 spin_unlock(&s1->self_group.lock);
1200 if (s1 == s) /* end */
1208 * Note: call with stream lock
1210 static int snd_pcm_action_single(const struct action_ops *ops,
1211 struct snd_pcm_substream *substream,
1212 snd_pcm_state_t state)
1216 res = ops->pre_action(substream, state);
1219 res = ops->do_action(substream, state);
1221 ops->post_action(substream, state);
1222 else if (ops->undo_action)
1223 ops->undo_action(substream, state);
1227 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1228 struct snd_pcm_group *new_group)
1230 substream->group = new_group;
1231 list_move(&substream->link_list, &new_group->substreams);
1235 * Unref and unlock the group, but keep the stream lock;
1236 * when the group becomes empty and no longer referred, destroy itself
1238 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1239 struct snd_pcm_substream *substream)
1245 do_free = refcount_dec_and_test(&group->refs);
1246 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1252 * Lock the group inside a stream lock and reference it;
1253 * return the locked group object, or NULL if not linked
1255 static struct snd_pcm_group *
1256 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1258 bool nonatomic = substream->pcm->nonatomic;
1259 struct snd_pcm_group *group;
1263 if (!snd_pcm_stream_linked(substream))
1265 group = substream->group;
1266 /* block freeing the group object */
1267 refcount_inc(&group->refs);
1269 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1270 spin_trylock(&group->lock);
1274 /* re-lock for avoiding ABBA deadlock */
1275 snd_pcm_stream_unlock(substream);
1276 snd_pcm_group_lock(group, nonatomic);
1277 snd_pcm_stream_lock(substream);
1279 /* check the group again; the above opens a small race window */
1280 if (substream->group == group)
1282 /* group changed, try again */
1283 snd_pcm_group_unref(group, substream);
1289 * Note: call with stream lock
1291 static int snd_pcm_action(const struct action_ops *ops,
1292 struct snd_pcm_substream *substream,
1293 snd_pcm_state_t state)
1295 struct snd_pcm_group *group;
1298 group = snd_pcm_stream_group_ref(substream);
1300 res = snd_pcm_action_group(ops, substream, state, true);
1302 res = snd_pcm_action_single(ops, substream, state);
1303 snd_pcm_group_unref(group, substream);
1308 * Note: don't use any locks before
1310 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1311 struct snd_pcm_substream *substream,
1312 snd_pcm_state_t state)
1316 snd_pcm_stream_lock_irq(substream);
1317 res = snd_pcm_action(ops, substream, state);
1318 snd_pcm_stream_unlock_irq(substream);
1324 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1325 struct snd_pcm_substream *substream,
1326 snd_pcm_state_t state)
1330 /* Guarantee the group members won't change during non-atomic action */
1331 down_read(&snd_pcm_link_rwsem);
1332 if (snd_pcm_stream_linked(substream))
1333 res = snd_pcm_action_group(ops, substream, state, false);
1335 res = snd_pcm_action_single(ops, substream, state);
1336 up_read(&snd_pcm_link_rwsem);
1343 static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1344 snd_pcm_state_t state)
1346 struct snd_pcm_runtime *runtime = substream->runtime;
1347 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1350 !snd_pcm_playback_data(substream))
1352 runtime->trigger_tstamp_latched = false;
1353 runtime->trigger_master = substream;
1357 static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1358 snd_pcm_state_t state)
1360 if (substream->runtime->trigger_master != substream)
1362 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1365 static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1366 snd_pcm_state_t state)
1368 if (substream->runtime->trigger_master == substream)
1369 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1372 static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1373 snd_pcm_state_t state)
1375 struct snd_pcm_runtime *runtime = substream->runtime;
1376 snd_pcm_trigger_tstamp(substream);
1377 runtime->hw_ptr_jiffies = jiffies;
1378 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1380 runtime->status->state = state;
1381 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1382 runtime->silence_size > 0)
1383 snd_pcm_playback_silence(substream, ULONG_MAX);
1384 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1387 static const struct action_ops snd_pcm_action_start = {
1388 .pre_action = snd_pcm_pre_start,
1389 .do_action = snd_pcm_do_start,
1390 .undo_action = snd_pcm_undo_start,
1391 .post_action = snd_pcm_post_start
1395 * snd_pcm_start - start all linked streams
1396 * @substream: the PCM substream instance
1398 * Return: Zero if successful, or a negative error code.
1399 * The stream lock must be acquired before calling this function.
1401 int snd_pcm_start(struct snd_pcm_substream *substream)
1403 return snd_pcm_action(&snd_pcm_action_start, substream,
1404 SNDRV_PCM_STATE_RUNNING);
1407 /* take the stream lock and start the streams */
1408 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1410 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1411 SNDRV_PCM_STATE_RUNNING);
1417 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1418 snd_pcm_state_t state)
1420 struct snd_pcm_runtime *runtime = substream->runtime;
1421 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1423 runtime->trigger_master = substream;
1427 static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1428 snd_pcm_state_t state)
1430 if (substream->runtime->trigger_master == substream &&
1431 snd_pcm_running(substream)) {
1432 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1433 substream->runtime->stop_operating = true;
1435 return 0; /* unconditionally stop all substreams */
1438 static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1439 snd_pcm_state_t state)
1441 struct snd_pcm_runtime *runtime = substream->runtime;
1442 if (runtime->status->state != state) {
1443 snd_pcm_trigger_tstamp(substream);
1444 runtime->status->state = state;
1445 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1447 wake_up(&runtime->sleep);
1448 wake_up(&runtime->tsleep);
1451 static const struct action_ops snd_pcm_action_stop = {
1452 .pre_action = snd_pcm_pre_stop,
1453 .do_action = snd_pcm_do_stop,
1454 .post_action = snd_pcm_post_stop
1458 * snd_pcm_stop - try to stop all running streams in the substream group
1459 * @substream: the PCM substream instance
1460 * @state: PCM state after stopping the stream
1462 * The state of each stream is then changed to the given state unconditionally.
1464 * Return: Zero if successful, or a negative error code.
1466 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1468 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1470 EXPORT_SYMBOL(snd_pcm_stop);
1473 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1474 * @substream: the PCM substream
1476 * After stopping, the state is changed to SETUP.
1477 * Unlike snd_pcm_stop(), this affects only the given stream.
1479 * Return: Zero if successful, or a negative error code.
1481 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1483 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1484 SNDRV_PCM_STATE_SETUP);
1488 * snd_pcm_stop_xrun - stop the running streams as XRUN
1489 * @substream: the PCM substream instance
1491 * This stops the given running substream (and all linked substreams) as XRUN.
1492 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1494 * Return: Zero if successful, or a negative error code.
1496 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1498 unsigned long flags;
1500 snd_pcm_stream_lock_irqsave(substream, flags);
1501 if (substream->runtime && snd_pcm_running(substream))
1502 __snd_pcm_xrun(substream);
1503 snd_pcm_stream_unlock_irqrestore(substream, flags);
1506 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1509 * pause callbacks: pass boolean (to start pause or resume) as state argument
1511 #define pause_pushed(state) (__force bool)(state)
1513 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1514 snd_pcm_state_t state)
1516 struct snd_pcm_runtime *runtime = substream->runtime;
1517 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1519 if (pause_pushed(state)) {
1520 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1522 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1524 runtime->trigger_master = substream;
1528 static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1529 snd_pcm_state_t state)
1531 if (substream->runtime->trigger_master != substream)
1533 /* some drivers might use hw_ptr to recover from the pause -
1534 update the hw_ptr now */
1535 if (pause_pushed(state))
1536 snd_pcm_update_hw_ptr(substream);
1537 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1538 * a delta between the current jiffies, this gives a large enough
1539 * delta, effectively to skip the check once.
1541 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1542 return substream->ops->trigger(substream,
1543 pause_pushed(state) ?
1544 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1545 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1548 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1549 snd_pcm_state_t state)
1551 if (substream->runtime->trigger_master == substream)
1552 substream->ops->trigger(substream,
1553 pause_pushed(state) ?
1554 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1555 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1558 static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1559 snd_pcm_state_t state)
1561 struct snd_pcm_runtime *runtime = substream->runtime;
1562 snd_pcm_trigger_tstamp(substream);
1563 if (pause_pushed(state)) {
1564 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1565 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1566 wake_up(&runtime->sleep);
1567 wake_up(&runtime->tsleep);
1569 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1570 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1574 static const struct action_ops snd_pcm_action_pause = {
1575 .pre_action = snd_pcm_pre_pause,
1576 .do_action = snd_pcm_do_pause,
1577 .undo_action = snd_pcm_undo_pause,
1578 .post_action = snd_pcm_post_pause
1582 * Push/release the pause for all linked streams.
1584 static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1586 return snd_pcm_action(&snd_pcm_action_pause, substream,
1587 (__force snd_pcm_state_t)push);
1590 static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1593 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1594 (__force snd_pcm_state_t)push);
1598 /* suspend callback: state argument ignored */
1600 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1601 snd_pcm_state_t state)
1603 struct snd_pcm_runtime *runtime = substream->runtime;
1604 switch (runtime->status->state) {
1605 case SNDRV_PCM_STATE_SUSPENDED:
1607 /* unresumable PCM state; return -EBUSY for skipping suspend */
1608 case SNDRV_PCM_STATE_OPEN:
1609 case SNDRV_PCM_STATE_SETUP:
1610 case SNDRV_PCM_STATE_DISCONNECTED:
1613 runtime->trigger_master = substream;
1617 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1618 snd_pcm_state_t state)
1620 struct snd_pcm_runtime *runtime = substream->runtime;
1621 if (runtime->trigger_master != substream)
1623 if (! snd_pcm_running(substream))
1625 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1626 runtime->stop_operating = true;
1627 return 0; /* suspend unconditionally */
1630 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1631 snd_pcm_state_t state)
1633 struct snd_pcm_runtime *runtime = substream->runtime;
1634 snd_pcm_trigger_tstamp(substream);
1635 runtime->status->suspended_state = runtime->status->state;
1636 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1637 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1638 wake_up(&runtime->sleep);
1639 wake_up(&runtime->tsleep);
1642 static const struct action_ops snd_pcm_action_suspend = {
1643 .pre_action = snd_pcm_pre_suspend,
1644 .do_action = snd_pcm_do_suspend,
1645 .post_action = snd_pcm_post_suspend
1649 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1650 * @substream: the PCM substream
1652 * After this call, all streams are changed to SUSPENDED state.
1654 * Return: Zero if successful, or a negative error code.
1656 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1659 unsigned long flags;
1661 snd_pcm_stream_lock_irqsave(substream, flags);
1662 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1664 snd_pcm_stream_unlock_irqrestore(substream, flags);
1669 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1670 * @pcm: the PCM instance
1672 * After this call, all streams are changed to SUSPENDED state.
1674 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1676 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1678 struct snd_pcm_substream *substream;
1679 int stream, err = 0;
1684 for_each_pcm_substream(pcm, stream, substream) {
1685 /* FIXME: the open/close code should lock this as well */
1686 if (!substream->runtime)
1690 * Skip BE dai link PCM's that are internal and may
1691 * not have their substream ops set.
1693 if (!substream->ops)
1696 err = snd_pcm_suspend(substream);
1697 if (err < 0 && err != -EBUSY)
1701 for_each_pcm_substream(pcm, stream, substream)
1702 snd_pcm_sync_stop(substream, false);
1706 EXPORT_SYMBOL(snd_pcm_suspend_all);
1708 /* resume callbacks: state argument ignored */
1710 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1711 snd_pcm_state_t state)
1713 struct snd_pcm_runtime *runtime = substream->runtime;
1714 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1716 runtime->trigger_master = substream;
1720 static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1721 snd_pcm_state_t state)
1723 struct snd_pcm_runtime *runtime = substream->runtime;
1724 if (runtime->trigger_master != substream)
1726 /* DMA not running previously? */
1727 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1728 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1729 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1731 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1734 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1735 snd_pcm_state_t state)
1737 if (substream->runtime->trigger_master == substream &&
1738 snd_pcm_running(substream))
1739 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1742 static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1743 snd_pcm_state_t state)
1745 struct snd_pcm_runtime *runtime = substream->runtime;
1746 snd_pcm_trigger_tstamp(substream);
1747 runtime->status->state = runtime->status->suspended_state;
1748 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1751 static const struct action_ops snd_pcm_action_resume = {
1752 .pre_action = snd_pcm_pre_resume,
1753 .do_action = snd_pcm_do_resume,
1754 .undo_action = snd_pcm_undo_resume,
1755 .post_action = snd_pcm_post_resume
1758 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1760 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1766 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1771 #endif /* CONFIG_PM */
1776 * Change the RUNNING stream(s) to XRUN state.
1778 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1780 struct snd_pcm_runtime *runtime = substream->runtime;
1783 snd_pcm_stream_lock_irq(substream);
1784 switch (runtime->status->state) {
1785 case SNDRV_PCM_STATE_XRUN:
1786 result = 0; /* already there */
1788 case SNDRV_PCM_STATE_RUNNING:
1789 __snd_pcm_xrun(substream);
1795 snd_pcm_stream_unlock_irq(substream);
1802 /* reset callbacks: state argument ignored */
1803 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1804 snd_pcm_state_t state)
1806 struct snd_pcm_runtime *runtime = substream->runtime;
1807 switch (runtime->status->state) {
1808 case SNDRV_PCM_STATE_RUNNING:
1809 case SNDRV_PCM_STATE_PREPARED:
1810 case SNDRV_PCM_STATE_PAUSED:
1811 case SNDRV_PCM_STATE_SUSPENDED:
1818 static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1819 snd_pcm_state_t state)
1821 struct snd_pcm_runtime *runtime = substream->runtime;
1822 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1825 runtime->hw_ptr_base = 0;
1826 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1827 runtime->status->hw_ptr % runtime->period_size;
1828 runtime->silence_start = runtime->status->hw_ptr;
1829 runtime->silence_filled = 0;
1833 static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1834 snd_pcm_state_t state)
1836 struct snd_pcm_runtime *runtime = substream->runtime;
1837 runtime->control->appl_ptr = runtime->status->hw_ptr;
1838 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1839 runtime->silence_size > 0)
1840 snd_pcm_playback_silence(substream, ULONG_MAX);
1843 static const struct action_ops snd_pcm_action_reset = {
1844 .pre_action = snd_pcm_pre_reset,
1845 .do_action = snd_pcm_do_reset,
1846 .post_action = snd_pcm_post_reset
1849 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1851 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1858 /* pass f_flags as state argument */
1859 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1860 snd_pcm_state_t state)
1862 struct snd_pcm_runtime *runtime = substream->runtime;
1863 int f_flags = (__force int)state;
1865 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1866 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1868 if (snd_pcm_running(substream))
1870 substream->f_flags = f_flags;
1874 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1875 snd_pcm_state_t state)
1878 snd_pcm_sync_stop(substream, true);
1879 err = substream->ops->prepare(substream);
1882 return snd_pcm_do_reset(substream, state);
1885 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1886 snd_pcm_state_t state)
1888 struct snd_pcm_runtime *runtime = substream->runtime;
1889 runtime->control->appl_ptr = runtime->status->hw_ptr;
1890 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1893 static const struct action_ops snd_pcm_action_prepare = {
1894 .pre_action = snd_pcm_pre_prepare,
1895 .do_action = snd_pcm_do_prepare,
1896 .post_action = snd_pcm_post_prepare
1900 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1901 * @substream: the PCM substream instance
1902 * @file: file to refer f_flags
1904 * Return: Zero if successful, or a negative error code.
1906 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1912 f_flags = file->f_flags;
1914 f_flags = substream->f_flags;
1916 snd_pcm_stream_lock_irq(substream);
1917 switch (substream->runtime->status->state) {
1918 case SNDRV_PCM_STATE_PAUSED:
1919 snd_pcm_pause(substream, false);
1921 case SNDRV_PCM_STATE_SUSPENDED:
1922 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1925 snd_pcm_stream_unlock_irq(substream);
1927 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1929 (__force snd_pcm_state_t)f_flags);
1936 /* drain init callbacks: state argument ignored */
1937 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1938 snd_pcm_state_t state)
1940 struct snd_pcm_runtime *runtime = substream->runtime;
1941 switch (runtime->status->state) {
1942 case SNDRV_PCM_STATE_OPEN:
1943 case SNDRV_PCM_STATE_DISCONNECTED:
1944 case SNDRV_PCM_STATE_SUSPENDED:
1947 runtime->trigger_master = substream;
1951 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1952 snd_pcm_state_t state)
1954 struct snd_pcm_runtime *runtime = substream->runtime;
1955 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1956 switch (runtime->status->state) {
1957 case SNDRV_PCM_STATE_PREPARED:
1958 /* start playback stream if possible */
1959 if (! snd_pcm_playback_empty(substream)) {
1960 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1961 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1963 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1966 case SNDRV_PCM_STATE_RUNNING:
1967 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1969 case SNDRV_PCM_STATE_XRUN:
1970 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1976 /* stop running stream */
1977 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1978 snd_pcm_state_t new_state;
1980 new_state = snd_pcm_capture_avail(runtime) > 0 ?
1981 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1982 snd_pcm_do_stop(substream, new_state);
1983 snd_pcm_post_stop(substream, new_state);
1987 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1988 runtime->trigger_master == substream &&
1989 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1990 return substream->ops->trigger(substream,
1991 SNDRV_PCM_TRIGGER_DRAIN);
1996 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
1997 snd_pcm_state_t state)
2001 static const struct action_ops snd_pcm_action_drain_init = {
2002 .pre_action = snd_pcm_pre_drain_init,
2003 .do_action = snd_pcm_do_drain_init,
2004 .post_action = snd_pcm_post_drain_init
2008 * Drain the stream(s).
2009 * When the substream is linked, sync until the draining of all playback streams
2011 * After this call, all streams are supposed to be either SETUP or DRAINING
2012 * (capture only) state.
2014 static int snd_pcm_drain(struct snd_pcm_substream *substream,
2017 struct snd_card *card;
2018 struct snd_pcm_runtime *runtime;
2019 struct snd_pcm_substream *s;
2020 struct snd_pcm_group *group;
2021 wait_queue_entry_t wait;
2025 card = substream->pcm->card;
2026 runtime = substream->runtime;
2028 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2032 if (file->f_flags & O_NONBLOCK)
2034 } else if (substream->f_flags & O_NONBLOCK)
2037 snd_pcm_stream_lock_irq(substream);
2039 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2040 snd_pcm_pause(substream, false);
2042 /* pre-start/stop - all running streams are changed to DRAINING state */
2043 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2047 /* in non-blocking, we don't wait in ioctl but let caller poll */
2055 struct snd_pcm_runtime *to_check;
2056 if (signal_pending(current)) {
2057 result = -ERESTARTSYS;
2060 /* find a substream to drain */
2062 group = snd_pcm_stream_group_ref(substream);
2063 snd_pcm_group_for_each_entry(s, substream) {
2064 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2066 runtime = s->runtime;
2067 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2072 snd_pcm_group_unref(group, substream);
2074 break; /* all drained */
2075 init_waitqueue_entry(&wait, current);
2076 set_current_state(TASK_INTERRUPTIBLE);
2077 add_wait_queue(&to_check->sleep, &wait);
2078 snd_pcm_stream_unlock_irq(substream);
2079 if (runtime->no_period_wakeup)
2080 tout = MAX_SCHEDULE_TIMEOUT;
2083 if (runtime->rate) {
2084 long t = runtime->period_size * 2 / runtime->rate;
2085 tout = max(t, tout);
2087 tout = msecs_to_jiffies(tout * 1000);
2089 tout = schedule_timeout(tout);
2091 snd_pcm_stream_lock_irq(substream);
2092 group = snd_pcm_stream_group_ref(substream);
2093 snd_pcm_group_for_each_entry(s, substream) {
2094 if (s->runtime == to_check) {
2095 remove_wait_queue(&to_check->sleep, &wait);
2099 snd_pcm_group_unref(group, substream);
2101 if (card->shutdown) {
2106 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2109 dev_dbg(substream->pcm->card->dev,
2110 "playback drain error (DMA or IRQ trouble?)\n");
2111 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2119 snd_pcm_stream_unlock_irq(substream);
2127 * Immediately put all linked substreams into SETUP state.
2129 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2131 struct snd_pcm_runtime *runtime;
2134 if (PCM_RUNTIME_CHECK(substream))
2136 runtime = substream->runtime;
2138 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2139 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2142 snd_pcm_stream_lock_irq(substream);
2144 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2145 snd_pcm_pause(substream, false);
2147 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2148 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2149 snd_pcm_stream_unlock_irq(substream);
2155 static bool is_pcm_file(struct file *file)
2157 struct inode *inode = file_inode(file);
2158 struct snd_pcm *pcm;
2161 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2163 minor = iminor(inode);
2164 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2166 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2169 snd_card_unref(pcm->card);
2176 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2179 struct snd_pcm_file *pcm_file;
2180 struct snd_pcm_substream *substream1;
2181 struct snd_pcm_group *group, *target_group;
2182 bool nonatomic = substream->pcm->nonatomic;
2183 struct fd f = fdget(fd);
2187 if (!is_pcm_file(f.file)) {
2191 pcm_file = f.file->private_data;
2192 substream1 = pcm_file->substream;
2194 if (substream == substream1) {
2199 group = kzalloc(sizeof(*group), GFP_KERNEL);
2204 snd_pcm_group_init(group);
2206 down_write(&snd_pcm_link_rwsem);
2207 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2208 substream->runtime->status->state != substream1->runtime->status->state ||
2209 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2213 if (snd_pcm_stream_linked(substream1)) {
2218 snd_pcm_stream_lock_irq(substream);
2219 if (!snd_pcm_stream_linked(substream)) {
2220 snd_pcm_group_assign(substream, group);
2221 group = NULL; /* assigned, don't free this one below */
2223 target_group = substream->group;
2224 snd_pcm_stream_unlock_irq(substream);
2226 snd_pcm_group_lock_irq(target_group, nonatomic);
2227 snd_pcm_stream_lock_nested(substream1);
2228 snd_pcm_group_assign(substream1, target_group);
2229 refcount_inc(&target_group->refs);
2230 snd_pcm_stream_unlock(substream1);
2231 snd_pcm_group_unlock_irq(target_group, nonatomic);
2233 up_write(&snd_pcm_link_rwsem);
2241 static void relink_to_local(struct snd_pcm_substream *substream)
2243 snd_pcm_stream_lock_nested(substream);
2244 snd_pcm_group_assign(substream, &substream->self_group);
2245 snd_pcm_stream_unlock(substream);
2248 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2250 struct snd_pcm_group *group;
2251 bool nonatomic = substream->pcm->nonatomic;
2252 bool do_free = false;
2255 down_write(&snd_pcm_link_rwsem);
2257 if (!snd_pcm_stream_linked(substream)) {
2262 group = substream->group;
2263 snd_pcm_group_lock_irq(group, nonatomic);
2265 relink_to_local(substream);
2266 refcount_dec(&group->refs);
2268 /* detach the last stream, too */
2269 if (list_is_singular(&group->substreams)) {
2270 relink_to_local(list_first_entry(&group->substreams,
2271 struct snd_pcm_substream,
2273 do_free = refcount_dec_and_test(&group->refs);
2276 snd_pcm_group_unlock_irq(group, nonatomic);
2281 up_write(&snd_pcm_link_rwsem);
2288 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2289 struct snd_pcm_hw_rule *rule)
2291 struct snd_interval t;
2292 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2293 hw_param_interval_c(params, rule->deps[1]), &t);
2294 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2297 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2298 struct snd_pcm_hw_rule *rule)
2300 struct snd_interval t;
2301 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2302 hw_param_interval_c(params, rule->deps[1]), &t);
2303 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2306 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2307 struct snd_pcm_hw_rule *rule)
2309 struct snd_interval t;
2310 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2311 hw_param_interval_c(params, rule->deps[1]),
2312 (unsigned long) rule->private, &t);
2313 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2316 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2317 struct snd_pcm_hw_rule *rule)
2319 struct snd_interval t;
2320 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2321 (unsigned long) rule->private,
2322 hw_param_interval_c(params, rule->deps[1]), &t);
2323 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2326 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2327 struct snd_pcm_hw_rule *rule)
2330 const struct snd_interval *i =
2331 hw_param_interval_c(params, rule->deps[0]);
2333 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2335 pcm_for_each_format(k) {
2337 if (!snd_mask_test_format(mask, k))
2339 bits = snd_pcm_format_physical_width(k);
2341 continue; /* ignore invalid formats */
2342 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2343 snd_mask_reset(&m, (__force unsigned)k);
2345 return snd_mask_refine(mask, &m);
2348 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2349 struct snd_pcm_hw_rule *rule)
2351 struct snd_interval t;
2358 pcm_for_each_format(k) {
2360 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2362 bits = snd_pcm_format_physical_width(k);
2364 continue; /* ignore invalid formats */
2365 if (t.min > (unsigned)bits)
2367 if (t.max < (unsigned)bits)
2371 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2374 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2375 #error "Change this table"
2378 static const unsigned int rates[] = {
2379 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2380 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2383 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2384 .count = ARRAY_SIZE(rates),
2388 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2389 struct snd_pcm_hw_rule *rule)
2391 struct snd_pcm_hardware *hw = rule->private;
2392 return snd_interval_list(hw_param_interval(params, rule->var),
2393 snd_pcm_known_rates.count,
2394 snd_pcm_known_rates.list, hw->rates);
2397 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2398 struct snd_pcm_hw_rule *rule)
2400 struct snd_interval t;
2401 struct snd_pcm_substream *substream = rule->private;
2403 t.max = substream->buffer_bytes_max;
2407 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2410 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2412 struct snd_pcm_runtime *runtime = substream->runtime;
2413 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2416 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2417 snd_mask_any(constrs_mask(constrs, k));
2420 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2421 snd_interval_any(constrs_interval(constrs, k));
2424 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2425 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2426 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2427 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2428 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2430 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2431 snd_pcm_hw_rule_format, NULL,
2432 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2435 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2436 snd_pcm_hw_rule_sample_bits, NULL,
2437 SNDRV_PCM_HW_PARAM_FORMAT,
2438 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2441 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2442 snd_pcm_hw_rule_div, NULL,
2443 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2446 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2447 snd_pcm_hw_rule_mul, NULL,
2448 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2451 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2452 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2453 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2456 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2457 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2458 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2461 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2462 snd_pcm_hw_rule_div, NULL,
2463 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2466 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2467 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2468 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2471 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2472 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2473 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2476 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2477 snd_pcm_hw_rule_div, NULL,
2478 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2481 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2482 snd_pcm_hw_rule_div, NULL,
2483 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2486 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2487 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2488 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2491 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2492 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2493 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2496 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2497 snd_pcm_hw_rule_mul, NULL,
2498 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2501 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2502 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2503 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2506 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2507 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2508 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2511 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2512 snd_pcm_hw_rule_muldivk, (void*) 8,
2513 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2516 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2517 snd_pcm_hw_rule_muldivk, (void*) 8,
2518 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2521 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2522 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2523 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2526 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2527 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2528 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2534 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2536 struct snd_pcm_runtime *runtime = substream->runtime;
2537 struct snd_pcm_hardware *hw = &runtime->hw;
2539 unsigned int mask = 0;
2541 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2542 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2543 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2544 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2545 if (hw_support_mmap(substream)) {
2546 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2547 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2548 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2549 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2550 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2551 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2553 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2557 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2561 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2562 PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
2566 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2567 hw->channels_min, hw->channels_max);
2571 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2572 hw->rate_min, hw->rate_max);
2576 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2577 hw->period_bytes_min, hw->period_bytes_max);
2581 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2582 hw->periods_min, hw->periods_max);
2586 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2587 hw->period_bytes_min, hw->buffer_bytes_max);
2591 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2592 snd_pcm_hw_rule_buffer_bytes_max, substream,
2593 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2598 if (runtime->dma_bytes) {
2599 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2604 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2605 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2606 snd_pcm_hw_rule_rate, hw,
2607 SNDRV_PCM_HW_PARAM_RATE, -1);
2612 /* FIXME: this belong to lowlevel */
2613 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2618 static void pcm_release_private(struct snd_pcm_substream *substream)
2620 if (snd_pcm_stream_linked(substream))
2621 snd_pcm_unlink(substream);
2624 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2626 substream->ref_count--;
2627 if (substream->ref_count > 0)
2630 snd_pcm_drop(substream);
2631 if (substream->hw_opened) {
2632 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2633 do_hw_free(substream);
2634 substream->ops->close(substream);
2635 substream->hw_opened = 0;
2637 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2638 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2639 if (substream->pcm_release) {
2640 substream->pcm_release(substream);
2641 substream->pcm_release = NULL;
2643 snd_pcm_detach_substream(substream);
2645 EXPORT_SYMBOL(snd_pcm_release_substream);
2647 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2649 struct snd_pcm_substream **rsubstream)
2651 struct snd_pcm_substream *substream;
2654 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2657 if (substream->ref_count > 1) {
2658 *rsubstream = substream;
2662 err = snd_pcm_hw_constraints_init(substream);
2664 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2668 err = substream->ops->open(substream);
2672 substream->hw_opened = 1;
2674 err = snd_pcm_hw_constraints_complete(substream);
2676 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2680 *rsubstream = substream;
2684 snd_pcm_release_substream(substream);
2687 EXPORT_SYMBOL(snd_pcm_open_substream);
2689 static int snd_pcm_open_file(struct file *file,
2690 struct snd_pcm *pcm,
2693 struct snd_pcm_file *pcm_file;
2694 struct snd_pcm_substream *substream;
2697 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2701 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2702 if (pcm_file == NULL) {
2703 snd_pcm_release_substream(substream);
2706 pcm_file->substream = substream;
2707 if (substream->ref_count == 1)
2708 substream->pcm_release = pcm_release_private;
2709 file->private_data = pcm_file;
2714 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2716 struct snd_pcm *pcm;
2717 int err = nonseekable_open(inode, file);
2720 pcm = snd_lookup_minor_data(iminor(inode),
2721 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2722 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2724 snd_card_unref(pcm->card);
2728 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2730 struct snd_pcm *pcm;
2731 int err = nonseekable_open(inode, file);
2734 pcm = snd_lookup_minor_data(iminor(inode),
2735 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2736 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2738 snd_card_unref(pcm->card);
2742 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2745 wait_queue_entry_t wait;
2751 err = snd_card_file_add(pcm->card, file);
2754 if (!try_module_get(pcm->card->module)) {
2758 init_waitqueue_entry(&wait, current);
2759 add_wait_queue(&pcm->open_wait, &wait);
2760 mutex_lock(&pcm->open_mutex);
2762 err = snd_pcm_open_file(file, pcm, stream);
2765 if (err == -EAGAIN) {
2766 if (file->f_flags & O_NONBLOCK) {
2772 set_current_state(TASK_INTERRUPTIBLE);
2773 mutex_unlock(&pcm->open_mutex);
2775 mutex_lock(&pcm->open_mutex);
2776 if (pcm->card->shutdown) {
2780 if (signal_pending(current)) {
2785 remove_wait_queue(&pcm->open_wait, &wait);
2786 mutex_unlock(&pcm->open_mutex);
2792 module_put(pcm->card->module);
2794 snd_card_file_remove(pcm->card, file);
2799 static int snd_pcm_release(struct inode *inode, struct file *file)
2801 struct snd_pcm *pcm;
2802 struct snd_pcm_substream *substream;
2803 struct snd_pcm_file *pcm_file;
2805 pcm_file = file->private_data;
2806 substream = pcm_file->substream;
2807 if (snd_BUG_ON(!substream))
2809 pcm = substream->pcm;
2811 /* block until the device gets woken up as it may touch the hardware */
2812 snd_power_wait(pcm->card);
2814 mutex_lock(&pcm->open_mutex);
2815 snd_pcm_release_substream(substream);
2817 mutex_unlock(&pcm->open_mutex);
2818 wake_up(&pcm->open_wait);
2819 module_put(pcm->card->module);
2820 snd_card_file_remove(pcm->card, file);
2824 /* check and update PCM state; return 0 or a negative error
2825 * call this inside PCM lock
2827 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2829 switch (substream->runtime->status->state) {
2830 case SNDRV_PCM_STATE_DRAINING:
2831 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2834 case SNDRV_PCM_STATE_RUNNING:
2835 return snd_pcm_update_hw_ptr(substream);
2836 case SNDRV_PCM_STATE_PREPARED:
2837 case SNDRV_PCM_STATE_PAUSED:
2839 case SNDRV_PCM_STATE_SUSPENDED:
2841 case SNDRV_PCM_STATE_XRUN:
2848 /* increase the appl_ptr; returns the processed frames or a negative error */
2849 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2850 snd_pcm_uframes_t frames,
2851 snd_pcm_sframes_t avail)
2853 struct snd_pcm_runtime *runtime = substream->runtime;
2854 snd_pcm_sframes_t appl_ptr;
2859 if (frames > (snd_pcm_uframes_t)avail)
2861 appl_ptr = runtime->control->appl_ptr + frames;
2862 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2863 appl_ptr -= runtime->boundary;
2864 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2865 return ret < 0 ? ret : frames;
2868 /* decrease the appl_ptr; returns the processed frames or zero for error */
2869 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2870 snd_pcm_uframes_t frames,
2871 snd_pcm_sframes_t avail)
2873 struct snd_pcm_runtime *runtime = substream->runtime;
2874 snd_pcm_sframes_t appl_ptr;
2879 if (frames > (snd_pcm_uframes_t)avail)
2881 appl_ptr = runtime->control->appl_ptr - frames;
2883 appl_ptr += runtime->boundary;
2884 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2885 /* NOTE: we return zero for errors because PulseAudio gets depressed
2886 * upon receiving an error from rewind ioctl and stops processing
2887 * any longer. Returning zero means that no rewind is done, so
2888 * it's not absolutely wrong to answer like that.
2890 return ret < 0 ? 0 : frames;
2893 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2894 snd_pcm_uframes_t frames)
2896 snd_pcm_sframes_t ret;
2901 snd_pcm_stream_lock_irq(substream);
2902 ret = do_pcm_hwsync(substream);
2904 ret = rewind_appl_ptr(substream, frames,
2905 snd_pcm_hw_avail(substream));
2906 snd_pcm_stream_unlock_irq(substream);
2910 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2911 snd_pcm_uframes_t frames)
2913 snd_pcm_sframes_t ret;
2918 snd_pcm_stream_lock_irq(substream);
2919 ret = do_pcm_hwsync(substream);
2921 ret = forward_appl_ptr(substream, frames,
2922 snd_pcm_avail(substream));
2923 snd_pcm_stream_unlock_irq(substream);
2927 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2931 snd_pcm_stream_lock_irq(substream);
2932 err = do_pcm_hwsync(substream);
2933 snd_pcm_stream_unlock_irq(substream);
2937 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2938 snd_pcm_sframes_t *delay)
2941 snd_pcm_sframes_t n = 0;
2943 snd_pcm_stream_lock_irq(substream);
2944 err = do_pcm_hwsync(substream);
2946 n = snd_pcm_calc_delay(substream);
2947 snd_pcm_stream_unlock_irq(substream);
2953 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2954 struct snd_pcm_sync_ptr __user *_sync_ptr)
2956 struct snd_pcm_runtime *runtime = substream->runtime;
2957 struct snd_pcm_sync_ptr sync_ptr;
2958 volatile struct snd_pcm_mmap_status *status;
2959 volatile struct snd_pcm_mmap_control *control;
2962 memset(&sync_ptr, 0, sizeof(sync_ptr));
2963 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2965 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2967 status = runtime->status;
2968 control = runtime->control;
2969 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2970 err = snd_pcm_hwsync(substream);
2974 snd_pcm_stream_lock_irq(substream);
2975 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2976 err = pcm_lib_apply_appl_ptr(substream,
2977 sync_ptr.c.control.appl_ptr);
2979 snd_pcm_stream_unlock_irq(substream);
2983 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2985 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2986 control->avail_min = sync_ptr.c.control.avail_min;
2988 sync_ptr.c.control.avail_min = control->avail_min;
2989 sync_ptr.s.status.state = status->state;
2990 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2991 sync_ptr.s.status.tstamp = status->tstamp;
2992 sync_ptr.s.status.suspended_state = status->suspended_state;
2993 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2994 snd_pcm_stream_unlock_irq(substream);
2995 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
3000 struct snd_pcm_mmap_status32 {
3001 snd_pcm_state_t state;
3006 snd_pcm_state_t suspended_state;
3007 s32 audio_tstamp_sec;
3008 s32 audio_tstamp_nsec;
3009 } __attribute__((packed));
3011 struct snd_pcm_mmap_control32 {
3016 struct snd_pcm_sync_ptr32 {
3019 struct snd_pcm_mmap_status32 status;
3020 unsigned char reserved[64];
3023 struct snd_pcm_mmap_control32 control;
3024 unsigned char reserved[64];
3026 } __attribute__((packed));
3028 /* recalcuate the boundary within 32bit */
3029 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
3031 snd_pcm_uframes_t boundary;
3033 if (! runtime->buffer_size)
3035 boundary = runtime->buffer_size;
3036 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3041 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3042 struct snd_pcm_sync_ptr32 __user *src)
3044 struct snd_pcm_runtime *runtime = substream->runtime;
3045 volatile struct snd_pcm_mmap_status *status;
3046 volatile struct snd_pcm_mmap_control *control;
3048 struct snd_pcm_mmap_control scontrol;
3049 struct snd_pcm_mmap_status sstatus;
3050 snd_pcm_uframes_t boundary;
3053 if (snd_BUG_ON(!runtime))
3056 if (get_user(sflags, &src->flags) ||
3057 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3058 get_user(scontrol.avail_min, &src->c.control.avail_min))
3060 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3061 err = snd_pcm_hwsync(substream);
3065 status = runtime->status;
3066 control = runtime->control;
3067 boundary = recalculate_boundary(runtime);
3069 boundary = 0x7fffffff;
3070 snd_pcm_stream_lock_irq(substream);
3071 /* FIXME: we should consider the boundary for the sync from app */
3072 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3073 err = pcm_lib_apply_appl_ptr(substream,
3076 snd_pcm_stream_unlock_irq(substream);
3080 scontrol.appl_ptr = control->appl_ptr % boundary;
3081 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3082 control->avail_min = scontrol.avail_min;
3084 scontrol.avail_min = control->avail_min;
3085 sstatus.state = status->state;
3086 sstatus.hw_ptr = status->hw_ptr % boundary;
3087 sstatus.tstamp = status->tstamp;
3088 sstatus.suspended_state = status->suspended_state;
3089 sstatus.audio_tstamp = status->audio_tstamp;
3090 snd_pcm_stream_unlock_irq(substream);
3091 if (put_user(sstatus.state, &src->s.status.state) ||
3092 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3093 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3094 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3095 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3096 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3097 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3098 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3099 put_user(scontrol.avail_min, &src->c.control.avail_min))
3104 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3106 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3108 struct snd_pcm_runtime *runtime = substream->runtime;
3111 if (get_user(arg, _arg))
3113 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3115 runtime->tstamp_type = arg;
3119 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3120 struct snd_xferi __user *_xferi)
3122 struct snd_xferi xferi;
3123 struct snd_pcm_runtime *runtime = substream->runtime;
3124 snd_pcm_sframes_t result;
3126 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3128 if (put_user(0, &_xferi->result))
3130 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3132 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3133 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3135 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3136 if (put_user(result, &_xferi->result))
3138 return result < 0 ? result : 0;
3141 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3142 struct snd_xfern __user *_xfern)
3144 struct snd_xfern xfern;
3145 struct snd_pcm_runtime *runtime = substream->runtime;
3147 snd_pcm_sframes_t result;
3149 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3151 if (runtime->channels > 128)
3153 if (put_user(0, &_xfern->result))
3155 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3158 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3160 return PTR_ERR(bufs);
3161 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3162 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3164 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3166 if (put_user(result, &_xfern->result))
3168 return result < 0 ? result : 0;
3171 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3172 snd_pcm_uframes_t __user *_frames)
3174 snd_pcm_uframes_t frames;
3175 snd_pcm_sframes_t result;
3177 if (get_user(frames, _frames))
3179 if (put_user(0, _frames))
3181 result = snd_pcm_rewind(substream, frames);
3182 if (put_user(result, _frames))
3184 return result < 0 ? result : 0;
3187 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3188 snd_pcm_uframes_t __user *_frames)
3190 snd_pcm_uframes_t frames;
3191 snd_pcm_sframes_t result;
3193 if (get_user(frames, _frames))
3195 if (put_user(0, _frames))
3197 result = snd_pcm_forward(substream, frames);
3198 if (put_user(result, _frames))
3200 return result < 0 ? result : 0;
3203 static int snd_pcm_common_ioctl(struct file *file,
3204 struct snd_pcm_substream *substream,
3205 unsigned int cmd, void __user *arg)
3207 struct snd_pcm_file *pcm_file = file->private_data;
3210 if (PCM_RUNTIME_CHECK(substream))
3213 res = snd_power_wait(substream->pcm->card);
3218 case SNDRV_PCM_IOCTL_PVERSION:
3219 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3220 case SNDRV_PCM_IOCTL_INFO:
3221 return snd_pcm_info_user(substream, arg);
3222 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3224 case SNDRV_PCM_IOCTL_TTSTAMP:
3225 return snd_pcm_tstamp(substream, arg);
3226 case SNDRV_PCM_IOCTL_USER_PVERSION:
3227 if (get_user(pcm_file->user_pversion,
3228 (unsigned int __user *)arg))
3231 case SNDRV_PCM_IOCTL_HW_REFINE:
3232 return snd_pcm_hw_refine_user(substream, arg);
3233 case SNDRV_PCM_IOCTL_HW_PARAMS:
3234 return snd_pcm_hw_params_user(substream, arg);
3235 case SNDRV_PCM_IOCTL_HW_FREE:
3236 return snd_pcm_hw_free(substream);
3237 case SNDRV_PCM_IOCTL_SW_PARAMS:
3238 return snd_pcm_sw_params_user(substream, arg);
3239 case SNDRV_PCM_IOCTL_STATUS32:
3240 return snd_pcm_status_user32(substream, arg, false);
3241 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3242 return snd_pcm_status_user32(substream, arg, true);
3243 case SNDRV_PCM_IOCTL_STATUS64:
3244 return snd_pcm_status_user64(substream, arg, false);
3245 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3246 return snd_pcm_status_user64(substream, arg, true);
3247 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3248 return snd_pcm_channel_info_user(substream, arg);
3249 case SNDRV_PCM_IOCTL_PREPARE:
3250 return snd_pcm_prepare(substream, file);
3251 case SNDRV_PCM_IOCTL_RESET:
3252 return snd_pcm_reset(substream);
3253 case SNDRV_PCM_IOCTL_START:
3254 return snd_pcm_start_lock_irq(substream);
3255 case SNDRV_PCM_IOCTL_LINK:
3256 return snd_pcm_link(substream, (int)(unsigned long) arg);
3257 case SNDRV_PCM_IOCTL_UNLINK:
3258 return snd_pcm_unlink(substream);
3259 case SNDRV_PCM_IOCTL_RESUME:
3260 return snd_pcm_resume(substream);
3261 case SNDRV_PCM_IOCTL_XRUN:
3262 return snd_pcm_xrun(substream);
3263 case SNDRV_PCM_IOCTL_HWSYNC:
3264 return snd_pcm_hwsync(substream);
3265 case SNDRV_PCM_IOCTL_DELAY:
3267 snd_pcm_sframes_t delay;
3268 snd_pcm_sframes_t __user *res = arg;
3271 err = snd_pcm_delay(substream, &delay);
3274 if (put_user(delay, res))
3278 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3279 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3280 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3281 return snd_pcm_sync_ptr(substream, arg);
3282 #ifdef CONFIG_SND_SUPPORT_OLD_API
3283 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3284 return snd_pcm_hw_refine_old_user(substream, arg);
3285 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3286 return snd_pcm_hw_params_old_user(substream, arg);
3288 case SNDRV_PCM_IOCTL_DRAIN:
3289 return snd_pcm_drain(substream, file);
3290 case SNDRV_PCM_IOCTL_DROP:
3291 return snd_pcm_drop(substream);
3292 case SNDRV_PCM_IOCTL_PAUSE:
3293 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3294 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3295 case SNDRV_PCM_IOCTL_READI_FRAMES:
3296 return snd_pcm_xferi_frames_ioctl(substream, arg);
3297 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3298 case SNDRV_PCM_IOCTL_READN_FRAMES:
3299 return snd_pcm_xfern_frames_ioctl(substream, arg);
3300 case SNDRV_PCM_IOCTL_REWIND:
3301 return snd_pcm_rewind_ioctl(substream, arg);
3302 case SNDRV_PCM_IOCTL_FORWARD:
3303 return snd_pcm_forward_ioctl(substream, arg);
3305 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3309 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3312 struct snd_pcm_file *pcm_file;
3314 pcm_file = file->private_data;
3316 if (((cmd >> 8) & 0xff) != 'A')
3319 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3320 (void __user *)arg);
3324 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3325 * @substream: PCM substream
3327 * @arg: IOCTL argument
3329 * The function is provided primarily for OSS layer and USB gadget drivers,
3330 * and it allows only the limited set of ioctls (hw_params, sw_params,
3331 * prepare, start, drain, drop, forward).
3333 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3334 unsigned int cmd, void *arg)
3336 snd_pcm_uframes_t *frames = arg;
3337 snd_pcm_sframes_t result;
3340 case SNDRV_PCM_IOCTL_FORWARD:
3342 /* provided only for OSS; capture-only and no value returned */
3343 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3345 result = snd_pcm_forward(substream, *frames);
3346 return result < 0 ? result : 0;
3348 case SNDRV_PCM_IOCTL_HW_PARAMS:
3349 return snd_pcm_hw_params(substream, arg);
3350 case SNDRV_PCM_IOCTL_SW_PARAMS:
3351 return snd_pcm_sw_params(substream, arg);
3352 case SNDRV_PCM_IOCTL_PREPARE:
3353 return snd_pcm_prepare(substream, NULL);
3354 case SNDRV_PCM_IOCTL_START:
3355 return snd_pcm_start_lock_irq(substream);
3356 case SNDRV_PCM_IOCTL_DRAIN:
3357 return snd_pcm_drain(substream, NULL);
3358 case SNDRV_PCM_IOCTL_DROP:
3359 return snd_pcm_drop(substream);
3360 case SNDRV_PCM_IOCTL_DELAY:
3361 return snd_pcm_delay(substream, frames);
3366 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3368 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3371 struct snd_pcm_file *pcm_file;
3372 struct snd_pcm_substream *substream;
3373 struct snd_pcm_runtime *runtime;
3374 snd_pcm_sframes_t result;
3376 pcm_file = file->private_data;
3377 substream = pcm_file->substream;
3378 if (PCM_RUNTIME_CHECK(substream))
3380 runtime = substream->runtime;
3381 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3383 if (!frame_aligned(runtime, count))
3385 count = bytes_to_frames(runtime, count);
3386 result = snd_pcm_lib_read(substream, buf, count);
3388 result = frames_to_bytes(runtime, result);
3392 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3393 size_t count, loff_t * offset)
3395 struct snd_pcm_file *pcm_file;
3396 struct snd_pcm_substream *substream;
3397 struct snd_pcm_runtime *runtime;
3398 snd_pcm_sframes_t result;
3400 pcm_file = file->private_data;
3401 substream = pcm_file->substream;
3402 if (PCM_RUNTIME_CHECK(substream))
3404 runtime = substream->runtime;
3405 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3407 if (!frame_aligned(runtime, count))
3409 count = bytes_to_frames(runtime, count);
3410 result = snd_pcm_lib_write(substream, buf, count);
3412 result = frames_to_bytes(runtime, result);
3416 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3418 struct snd_pcm_file *pcm_file;
3419 struct snd_pcm_substream *substream;
3420 struct snd_pcm_runtime *runtime;
3421 snd_pcm_sframes_t result;
3424 snd_pcm_uframes_t frames;
3426 pcm_file = iocb->ki_filp->private_data;
3427 substream = pcm_file->substream;
3428 if (PCM_RUNTIME_CHECK(substream))
3430 runtime = substream->runtime;
3431 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3433 if (!iter_is_iovec(to))
3435 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3437 if (!frame_aligned(runtime, to->iov->iov_len))
3439 frames = bytes_to_samples(runtime, to->iov->iov_len);
3440 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3443 for (i = 0; i < to->nr_segs; ++i)
3444 bufs[i] = to->iov[i].iov_base;
3445 result = snd_pcm_lib_readv(substream, bufs, frames);
3447 result = frames_to_bytes(runtime, result);
3452 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3454 struct snd_pcm_file *pcm_file;
3455 struct snd_pcm_substream *substream;
3456 struct snd_pcm_runtime *runtime;
3457 snd_pcm_sframes_t result;
3460 snd_pcm_uframes_t frames;
3462 pcm_file = iocb->ki_filp->private_data;
3463 substream = pcm_file->substream;
3464 if (PCM_RUNTIME_CHECK(substream))
3466 runtime = substream->runtime;
3467 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3469 if (!iter_is_iovec(from))
3471 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3472 !frame_aligned(runtime, from->iov->iov_len))
3474 frames = bytes_to_samples(runtime, from->iov->iov_len);
3475 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3478 for (i = 0; i < from->nr_segs; ++i)
3479 bufs[i] = from->iov[i].iov_base;
3480 result = snd_pcm_lib_writev(substream, bufs, frames);
3482 result = frames_to_bytes(runtime, result);
3487 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3489 struct snd_pcm_file *pcm_file;
3490 struct snd_pcm_substream *substream;
3491 struct snd_pcm_runtime *runtime;
3493 snd_pcm_uframes_t avail;
3495 pcm_file = file->private_data;
3497 substream = pcm_file->substream;
3498 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3499 ok = EPOLLOUT | EPOLLWRNORM;
3501 ok = EPOLLIN | EPOLLRDNORM;
3502 if (PCM_RUNTIME_CHECK(substream))
3503 return ok | EPOLLERR;
3505 runtime = substream->runtime;
3506 poll_wait(file, &runtime->sleep, wait);
3509 snd_pcm_stream_lock_irq(substream);
3510 avail = snd_pcm_avail(substream);
3511 switch (runtime->status->state) {
3512 case SNDRV_PCM_STATE_RUNNING:
3513 case SNDRV_PCM_STATE_PREPARED:
3514 case SNDRV_PCM_STATE_PAUSED:
3515 if (avail >= runtime->control->avail_min)
3518 case SNDRV_PCM_STATE_DRAINING:
3519 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3526 mask = ok | EPOLLERR;
3529 snd_pcm_stream_unlock_irq(substream);
3538 * Only on coherent architectures, we can mmap the status and the control records
3539 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3541 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3543 * mmap status record
3545 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3547 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3548 struct snd_pcm_runtime *runtime;
3550 if (substream == NULL)
3551 return VM_FAULT_SIGBUS;
3552 runtime = substream->runtime;
3553 vmf->page = virt_to_page(runtime->status);
3554 get_page(vmf->page);
3558 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3560 .fault = snd_pcm_mmap_status_fault,
3563 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3564 struct vm_area_struct *area)
3567 if (!(area->vm_flags & VM_READ))
3569 size = area->vm_end - area->vm_start;
3570 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3572 area->vm_ops = &snd_pcm_vm_ops_status;
3573 area->vm_private_data = substream;
3574 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3579 * mmap control record
3581 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3583 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3584 struct snd_pcm_runtime *runtime;
3586 if (substream == NULL)
3587 return VM_FAULT_SIGBUS;
3588 runtime = substream->runtime;
3589 vmf->page = virt_to_page(runtime->control);
3590 get_page(vmf->page);
3594 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3596 .fault = snd_pcm_mmap_control_fault,
3599 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3600 struct vm_area_struct *area)
3603 if (!(area->vm_flags & VM_READ))
3605 size = area->vm_end - area->vm_start;
3606 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3608 area->vm_ops = &snd_pcm_vm_ops_control;
3609 area->vm_private_data = substream;
3610 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3614 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3616 /* See pcm_control_mmap_allowed() below.
3617 * Since older alsa-lib requires both status and control mmaps to be
3618 * coupled, we have to disable the status mmap for old alsa-lib, too.
3620 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3621 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3626 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3628 if (pcm_file->no_compat_mmap)
3630 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3631 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3632 * thus it effectively assures the manual update of appl_ptr.
3634 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3639 #else /* ! coherent mmap */
3641 * don't support mmap for status and control records.
3643 #define pcm_status_mmap_allowed(pcm_file) false
3644 #define pcm_control_mmap_allowed(pcm_file) false
3646 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3647 struct vm_area_struct *area)
3651 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3652 struct vm_area_struct *area)
3656 #endif /* coherent mmap */
3659 * fault callback for mmapping a RAM page
3661 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3663 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3664 struct snd_pcm_runtime *runtime;
3665 unsigned long offset;
3669 if (substream == NULL)
3670 return VM_FAULT_SIGBUS;
3671 runtime = substream->runtime;
3672 offset = vmf->pgoff << PAGE_SHIFT;
3673 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3674 if (offset > dma_bytes - PAGE_SIZE)
3675 return VM_FAULT_SIGBUS;
3676 if (substream->ops->page)
3677 page = substream->ops->page(substream, offset);
3678 else if (!snd_pcm_get_dma_buf(substream))
3679 page = virt_to_page(runtime->dma_area + offset);
3681 page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset);
3683 return VM_FAULT_SIGBUS;
3689 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3690 .open = snd_pcm_mmap_data_open,
3691 .close = snd_pcm_mmap_data_close,
3694 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3695 .open = snd_pcm_mmap_data_open,
3696 .close = snd_pcm_mmap_data_close,
3697 .fault = snd_pcm_mmap_data_fault,
3701 * mmap the DMA buffer on RAM
3705 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3706 * @substream: PCM substream
3709 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3710 * this function is invoked implicitly.
3712 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3713 struct vm_area_struct *area)
3715 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3716 if (!substream->ops->page &&
3717 !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area))
3719 /* mmap with fault handler */
3720 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3723 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3726 * mmap the DMA buffer on I/O memory area
3728 #if SNDRV_PCM_INFO_MMAP_IOMEM
3730 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3731 * @substream: PCM substream
3734 * When your hardware uses the iomapped pages as the hardware buffer and
3735 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3736 * is supposed to work only on limited architectures.
3738 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3739 struct vm_area_struct *area)
3741 struct snd_pcm_runtime *runtime = substream->runtime;
3743 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3744 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3746 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3747 #endif /* SNDRV_PCM_INFO_MMAP */
3752 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3753 struct vm_area_struct *area)
3755 struct snd_pcm_runtime *runtime;
3757 unsigned long offset;
3761 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3762 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3765 if (!(area->vm_flags & VM_READ))
3768 runtime = substream->runtime;
3769 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3771 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3773 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3774 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3776 size = area->vm_end - area->vm_start;
3777 offset = area->vm_pgoff << PAGE_SHIFT;
3778 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3779 if ((size_t)size > dma_bytes)
3781 if (offset > dma_bytes - size)
3784 area->vm_ops = &snd_pcm_vm_ops_data;
3785 area->vm_private_data = substream;
3786 if (substream->ops->mmap)
3787 err = substream->ops->mmap(substream, area);
3789 err = snd_pcm_lib_default_mmap(substream, area);
3791 atomic_inc(&substream->mmap_count);
3794 EXPORT_SYMBOL(snd_pcm_mmap_data);
3796 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3798 struct snd_pcm_file * pcm_file;
3799 struct snd_pcm_substream *substream;
3800 unsigned long offset;
3802 pcm_file = file->private_data;
3803 substream = pcm_file->substream;
3804 if (PCM_RUNTIME_CHECK(substream))
3807 offset = area->vm_pgoff << PAGE_SHIFT;
3809 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3810 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3813 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3814 if (!pcm_status_mmap_allowed(pcm_file))
3816 return snd_pcm_mmap_status(substream, file, area);
3817 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3818 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3821 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3822 if (!pcm_control_mmap_allowed(pcm_file))
3824 return snd_pcm_mmap_control(substream, file, area);
3826 return snd_pcm_mmap_data(substream, file, area);
3831 static int snd_pcm_fasync(int fd, struct file * file, int on)
3833 struct snd_pcm_file * pcm_file;
3834 struct snd_pcm_substream *substream;
3835 struct snd_pcm_runtime *runtime;
3837 pcm_file = file->private_data;
3838 substream = pcm_file->substream;
3839 if (PCM_RUNTIME_CHECK(substream))
3841 runtime = substream->runtime;
3842 return fasync_helper(fd, file, on, &runtime->fasync);
3848 #ifdef CONFIG_COMPAT
3849 #include "pcm_compat.c"
3851 #define snd_pcm_ioctl_compat NULL
3855 * To be removed helpers to keep binary compatibility
3858 #ifdef CONFIG_SND_SUPPORT_OLD_API
3859 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3860 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3862 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3863 struct snd_pcm_hw_params_old *oparams)
3867 memset(params, 0, sizeof(*params));
3868 params->flags = oparams->flags;
3869 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3870 params->masks[i].bits[0] = oparams->masks[i];
3871 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3872 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3873 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3874 params->info = oparams->info;
3875 params->msbits = oparams->msbits;
3876 params->rate_num = oparams->rate_num;
3877 params->rate_den = oparams->rate_den;
3878 params->fifo_size = oparams->fifo_size;
3881 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3882 struct snd_pcm_hw_params *params)
3886 memset(oparams, 0, sizeof(*oparams));
3887 oparams->flags = params->flags;
3888 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3889 oparams->masks[i] = params->masks[i].bits[0];
3890 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3891 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3892 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3893 oparams->info = params->info;
3894 oparams->msbits = params->msbits;
3895 oparams->rate_num = params->rate_num;
3896 oparams->rate_den = params->rate_den;
3897 oparams->fifo_size = params->fifo_size;
3900 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3901 struct snd_pcm_hw_params_old __user * _oparams)
3903 struct snd_pcm_hw_params *params;
3904 struct snd_pcm_hw_params_old *oparams = NULL;
3907 params = kmalloc(sizeof(*params), GFP_KERNEL);
3911 oparams = memdup_user(_oparams, sizeof(*oparams));
3912 if (IS_ERR(oparams)) {
3913 err = PTR_ERR(oparams);
3916 snd_pcm_hw_convert_from_old_params(params, oparams);
3917 err = snd_pcm_hw_refine(substream, params);
3921 err = fixup_unreferenced_params(substream, params);
3925 snd_pcm_hw_convert_to_old_params(oparams, params);
3926 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3935 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3936 struct snd_pcm_hw_params_old __user * _oparams)
3938 struct snd_pcm_hw_params *params;
3939 struct snd_pcm_hw_params_old *oparams = NULL;
3942 params = kmalloc(sizeof(*params), GFP_KERNEL);
3946 oparams = memdup_user(_oparams, sizeof(*oparams));
3947 if (IS_ERR(oparams)) {
3948 err = PTR_ERR(oparams);
3952 snd_pcm_hw_convert_from_old_params(params, oparams);
3953 err = snd_pcm_hw_params(substream, params);
3957 snd_pcm_hw_convert_to_old_params(oparams, params);
3958 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3966 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3969 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3972 unsigned long pgoff,
3973 unsigned long flags)
3975 struct snd_pcm_file *pcm_file = file->private_data;
3976 struct snd_pcm_substream *substream = pcm_file->substream;
3977 struct snd_pcm_runtime *runtime = substream->runtime;
3978 unsigned long offset = pgoff << PAGE_SHIFT;
3981 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3982 return (unsigned long)runtime->status;
3983 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3984 return (unsigned long)runtime->control;
3986 return (unsigned long)runtime->dma_area + offset;
3990 # define snd_pcm_get_unmapped_area NULL
3997 const struct file_operations snd_pcm_f_ops[2] = {
3999 .owner = THIS_MODULE,
4000 .write = snd_pcm_write,
4001 .write_iter = snd_pcm_writev,
4002 .open = snd_pcm_playback_open,
4003 .release = snd_pcm_release,
4004 .llseek = no_llseek,
4005 .poll = snd_pcm_poll,
4006 .unlocked_ioctl = snd_pcm_ioctl,
4007 .compat_ioctl = snd_pcm_ioctl_compat,
4008 .mmap = snd_pcm_mmap,
4009 .fasync = snd_pcm_fasync,
4010 .get_unmapped_area = snd_pcm_get_unmapped_area,
4013 .owner = THIS_MODULE,
4014 .read = snd_pcm_read,
4015 .read_iter = snd_pcm_readv,
4016 .open = snd_pcm_capture_open,
4017 .release = snd_pcm_release,
4018 .llseek = no_llseek,
4019 .poll = snd_pcm_poll,
4020 .unlocked_ioctl = snd_pcm_ioctl,
4021 .compat_ioctl = snd_pcm_ioctl_compat,
4022 .mmap = snd_pcm_mmap,
4023 .fasync = snd_pcm_fasync,
4024 .get_unmapped_area = snd_pcm_get_unmapped_area,