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 struct snd_dma_buffer *dmabuf;
248 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
251 if (substream->ops->mmap || substream->ops->page)
254 dmabuf = snd_pcm_get_dma_buf(substream);
256 dmabuf = &substream->dma_buffer;
257 switch (dmabuf->dev.type) {
258 case SNDRV_DMA_TYPE_UNKNOWN:
259 /* we can't know the device, so just assume that the driver does
263 case SNDRV_DMA_TYPE_CONTINUOUS:
264 case SNDRV_DMA_TYPE_VMALLOC:
267 return dma_can_mmap(dmabuf->dev.dev);
271 static int constrain_mask_params(struct snd_pcm_substream *substream,
272 struct snd_pcm_hw_params *params)
274 struct snd_pcm_hw_constraints *constrs =
275 &substream->runtime->hw_constraints;
278 struct snd_mask old_mask;
281 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
282 m = hw_param_mask(params, k);
283 if (snd_mask_empty(m))
286 /* This parameter is not requested to change by a caller. */
287 if (!(params->rmask & PARAM_MASK_BIT(k)))
290 if (trace_hw_mask_param_enabled())
293 changed = snd_mask_refine(m, constrs_mask(constrs, k));
299 /* Set corresponding flag so that the caller gets it. */
300 trace_hw_mask_param(substream, k, 0, &old_mask, m);
301 params->cmask |= PARAM_MASK_BIT(k);
307 static int constrain_interval_params(struct snd_pcm_substream *substream,
308 struct snd_pcm_hw_params *params)
310 struct snd_pcm_hw_constraints *constrs =
311 &substream->runtime->hw_constraints;
312 struct snd_interval *i;
314 struct snd_interval old_interval;
317 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
318 i = hw_param_interval(params, k);
319 if (snd_interval_empty(i))
322 /* This parameter is not requested to change by a caller. */
323 if (!(params->rmask & PARAM_MASK_BIT(k)))
326 if (trace_hw_interval_param_enabled())
329 changed = snd_interval_refine(i, constrs_interval(constrs, k));
335 /* Set corresponding flag so that the caller gets it. */
336 trace_hw_interval_param(substream, k, 0, &old_interval, i);
337 params->cmask |= PARAM_MASK_BIT(k);
343 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
344 struct snd_pcm_hw_params *params)
346 struct snd_pcm_hw_constraints *constrs =
347 &substream->runtime->hw_constraints;
349 unsigned int *rstamps;
350 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
352 struct snd_pcm_hw_rule *r;
354 struct snd_mask old_mask;
355 struct snd_interval old_interval;
357 int changed, err = 0;
360 * Each application of rule has own sequence number.
362 * Each member of 'rstamps' array represents the sequence number of
363 * recent application of corresponding rule.
365 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
370 * Each member of 'vstamps' array represents the sequence number of
371 * recent application of rule in which corresponding parameters were
374 * In initial state, elements corresponding to parameters requested by
375 * a caller is 1. For unrequested parameters, corresponding members
376 * have 0 so that the parameters are never changed anymore.
378 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
379 vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
381 /* Due to the above design, actual sequence number starts at 2. */
384 /* Apply all rules in order. */
386 for (k = 0; k < constrs->rules_num; k++) {
387 r = &constrs->rules[k];
390 * Check condition bits of this rule. When the rule has
391 * some condition bits, parameter without the bits is
392 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
393 * is an example of the condition bits.
395 if (r->cond && !(r->cond & params->flags))
399 * The 'deps' array includes maximum four dependencies
400 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth
401 * member of this array is a sentinel and should be
404 * This rule should be processed in this time when dependent
405 * parameters were changed at former applications of the other
408 for (d = 0; r->deps[d] >= 0; d++) {
409 if (vstamps[r->deps[d]] > rstamps[k])
415 if (trace_hw_mask_param_enabled()) {
416 if (hw_is_mask(r->var))
417 old_mask = *hw_param_mask(params, r->var);
419 if (trace_hw_interval_param_enabled()) {
420 if (hw_is_interval(r->var))
421 old_interval = *hw_param_interval(params, r->var);
424 changed = r->func(params, r);
431 * When the parameter is changed, notify it to the caller
432 * by corresponding returned bit, then preparing for next
435 if (changed && r->var >= 0) {
436 if (hw_is_mask(r->var)) {
437 trace_hw_mask_param(substream, r->var,
439 hw_param_mask(params, r->var));
441 if (hw_is_interval(r->var)) {
442 trace_hw_interval_param(substream, r->var,
443 k + 1, &old_interval,
444 hw_param_interval(params, r->var));
447 params->cmask |= PARAM_MASK_BIT(r->var);
448 vstamps[r->var] = stamp;
452 rstamps[k] = stamp++;
455 /* Iterate to evaluate all rules till no parameters are changed. */
464 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
465 struct snd_pcm_hw_params *params)
467 const struct snd_interval *i;
468 const struct snd_mask *m;
471 if (!params->msbits) {
472 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
473 if (snd_interval_single(i))
474 params->msbits = snd_interval_value(i);
477 if (!params->rate_den) {
478 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
479 if (snd_interval_single(i)) {
480 params->rate_num = snd_interval_value(i);
481 params->rate_den = 1;
485 if (!params->fifo_size) {
486 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
487 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
488 if (snd_mask_single(m) && snd_interval_single(i)) {
489 err = snd_pcm_ops_ioctl(substream,
490 SNDRV_PCM_IOCTL1_FIFO_SIZE,
498 params->info = substream->runtime->hw.info;
499 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
500 SNDRV_PCM_INFO_DRAIN_TRIGGER);
501 if (!hw_support_mmap(substream))
502 params->info &= ~(SNDRV_PCM_INFO_MMAP |
503 SNDRV_PCM_INFO_MMAP_VALID);
509 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
510 struct snd_pcm_hw_params *params)
515 params->fifo_size = 0;
516 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
518 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
519 params->rate_num = 0;
520 params->rate_den = 0;
523 err = constrain_mask_params(substream, params);
527 err = constrain_interval_params(substream, params);
531 err = constrain_params_by_rules(substream, params);
539 EXPORT_SYMBOL(snd_pcm_hw_refine);
541 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
542 struct snd_pcm_hw_params __user * _params)
544 struct snd_pcm_hw_params *params;
547 params = memdup_user(_params, sizeof(*params));
549 return PTR_ERR(params);
551 err = snd_pcm_hw_refine(substream, params);
555 err = fixup_unreferenced_params(substream, params);
559 if (copy_to_user(_params, params, sizeof(*params)))
566 static int period_to_usecs(struct snd_pcm_runtime *runtime)
571 return -1; /* invalid */
573 /* take 75% of period time as the deadline */
574 usecs = (750000 / runtime->rate) * runtime->period_size;
575 usecs += ((750000 % runtime->rate) * runtime->period_size) /
581 static void snd_pcm_set_state(struct snd_pcm_substream *substream,
582 snd_pcm_state_t state)
584 snd_pcm_stream_lock_irq(substream);
585 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
586 substream->runtime->status->state = state;
587 snd_pcm_stream_unlock_irq(substream);
590 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
593 #ifdef CONFIG_SND_PCM_TIMER
594 if (substream->timer)
595 snd_timer_notify(substream->timer, event,
596 &substream->runtime->trigger_tstamp);
600 void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq)
602 if (substream->runtime && substream->runtime->stop_operating) {
603 substream->runtime->stop_operating = false;
604 if (substream->ops && substream->ops->sync_stop)
605 substream->ops->sync_stop(substream);
606 else if (sync_irq && substream->pcm->card->sync_irq > 0)
607 synchronize_irq(substream->pcm->card->sync_irq);
612 * snd_pcm_hw_params_choose - choose a configuration defined by @params
614 * @params: the hw_params instance
616 * Choose one configuration from configuration space defined by @params.
617 * The configuration chosen is that obtained fixing in this order:
618 * first access, first format, first subformat, min channels,
619 * min rate, min period time, max buffer size, min tick time
621 * Return: Zero if successful, or a negative error code on failure.
623 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
624 struct snd_pcm_hw_params *params)
626 static const int vars[] = {
627 SNDRV_PCM_HW_PARAM_ACCESS,
628 SNDRV_PCM_HW_PARAM_FORMAT,
629 SNDRV_PCM_HW_PARAM_SUBFORMAT,
630 SNDRV_PCM_HW_PARAM_CHANNELS,
631 SNDRV_PCM_HW_PARAM_RATE,
632 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
633 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
634 SNDRV_PCM_HW_PARAM_TICK_TIME,
638 struct snd_mask old_mask;
639 struct snd_interval old_interval;
642 for (v = vars; *v != -1; v++) {
643 /* Keep old parameter to trace. */
644 if (trace_hw_mask_param_enabled()) {
646 old_mask = *hw_param_mask(params, *v);
648 if (trace_hw_interval_param_enabled()) {
649 if (hw_is_interval(*v))
650 old_interval = *hw_param_interval(params, *v);
652 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
653 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
655 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
661 /* Trace the changed parameter. */
662 if (hw_is_mask(*v)) {
663 trace_hw_mask_param(pcm, *v, 0, &old_mask,
664 hw_param_mask(params, *v));
666 if (hw_is_interval(*v)) {
667 trace_hw_interval_param(pcm, *v, 0, &old_interval,
668 hw_param_interval(params, *v));
675 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
676 struct snd_pcm_hw_params *params)
678 struct snd_pcm_runtime *runtime;
681 snd_pcm_uframes_t frames;
683 if (PCM_RUNTIME_CHECK(substream))
685 runtime = substream->runtime;
686 snd_pcm_stream_lock_irq(substream);
687 switch (runtime->status->state) {
688 case SNDRV_PCM_STATE_OPEN:
689 case SNDRV_PCM_STATE_SETUP:
690 case SNDRV_PCM_STATE_PREPARED:
693 snd_pcm_stream_unlock_irq(substream);
696 snd_pcm_stream_unlock_irq(substream);
697 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
698 if (!substream->oss.oss)
700 if (atomic_read(&substream->mmap_count))
703 snd_pcm_sync_stop(substream, true);
706 err = snd_pcm_hw_refine(substream, params);
710 err = snd_pcm_hw_params_choose(substream, params);
714 err = fixup_unreferenced_params(substream, params);
718 if (substream->managed_buffer_alloc) {
719 err = snd_pcm_lib_malloc_pages(substream,
720 params_buffer_bytes(params));
723 runtime->buffer_changed = err > 0;
726 if (substream->ops->hw_params != NULL) {
727 err = substream->ops->hw_params(substream, params);
732 runtime->access = params_access(params);
733 runtime->format = params_format(params);
734 runtime->subformat = params_subformat(params);
735 runtime->channels = params_channels(params);
736 runtime->rate = params_rate(params);
737 runtime->period_size = params_period_size(params);
738 runtime->periods = params_periods(params);
739 runtime->buffer_size = params_buffer_size(params);
740 runtime->info = params->info;
741 runtime->rate_num = params->rate_num;
742 runtime->rate_den = params->rate_den;
743 runtime->no_period_wakeup =
744 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
745 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
747 bits = snd_pcm_format_physical_width(runtime->format);
748 runtime->sample_bits = bits;
749 bits *= runtime->channels;
750 runtime->frame_bits = bits;
752 while (bits % 8 != 0) {
756 runtime->byte_align = bits / 8;
757 runtime->min_align = frames;
759 /* Default sw params */
760 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
761 runtime->period_step = 1;
762 runtime->control->avail_min = runtime->period_size;
763 runtime->start_threshold = 1;
764 runtime->stop_threshold = runtime->buffer_size;
765 runtime->silence_threshold = 0;
766 runtime->silence_size = 0;
767 runtime->boundary = runtime->buffer_size;
768 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
769 runtime->boundary *= 2;
771 /* clear the buffer for avoiding possible kernel info leaks */
772 if (runtime->dma_area && !substream->ops->copy_user) {
773 size_t size = runtime->dma_bytes;
775 if (runtime->info & SNDRV_PCM_INFO_MMAP)
776 size = PAGE_ALIGN(size);
777 memset(runtime->dma_area, 0, size);
780 snd_pcm_timer_resolution_change(substream);
781 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
783 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
784 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
785 usecs = period_to_usecs(runtime);
787 cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
791 /* hardware might be unusable from this time,
792 so we force application to retry to set
793 the correct hardware parameter settings */
794 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
795 if (substream->ops->hw_free != NULL)
796 substream->ops->hw_free(substream);
797 if (substream->managed_buffer_alloc)
798 snd_pcm_lib_free_pages(substream);
802 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
803 struct snd_pcm_hw_params __user * _params)
805 struct snd_pcm_hw_params *params;
808 params = memdup_user(_params, sizeof(*params));
810 return PTR_ERR(params);
812 err = snd_pcm_hw_params(substream, params);
816 if (copy_to_user(_params, params, sizeof(*params)))
823 static int do_hw_free(struct snd_pcm_substream *substream)
827 snd_pcm_sync_stop(substream, true);
828 if (substream->ops->hw_free)
829 result = substream->ops->hw_free(substream);
830 if (substream->managed_buffer_alloc)
831 snd_pcm_lib_free_pages(substream);
835 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
837 struct snd_pcm_runtime *runtime;
840 if (PCM_RUNTIME_CHECK(substream))
842 runtime = substream->runtime;
843 snd_pcm_stream_lock_irq(substream);
844 switch (runtime->status->state) {
845 case SNDRV_PCM_STATE_SETUP:
846 case SNDRV_PCM_STATE_PREPARED:
849 snd_pcm_stream_unlock_irq(substream);
852 snd_pcm_stream_unlock_irq(substream);
853 if (atomic_read(&substream->mmap_count))
855 result = do_hw_free(substream);
856 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
857 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
861 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
862 struct snd_pcm_sw_params *params)
864 struct snd_pcm_runtime *runtime;
867 if (PCM_RUNTIME_CHECK(substream))
869 runtime = substream->runtime;
870 snd_pcm_stream_lock_irq(substream);
871 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
872 snd_pcm_stream_unlock_irq(substream);
875 snd_pcm_stream_unlock_irq(substream);
877 if (params->tstamp_mode < 0 ||
878 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
880 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
881 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
883 if (params->avail_min == 0)
885 if (params->silence_size >= runtime->boundary) {
886 if (params->silence_threshold != 0)
889 if (params->silence_size > params->silence_threshold)
891 if (params->silence_threshold > runtime->buffer_size)
895 snd_pcm_stream_lock_irq(substream);
896 runtime->tstamp_mode = params->tstamp_mode;
897 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
898 runtime->tstamp_type = params->tstamp_type;
899 runtime->period_step = params->period_step;
900 runtime->control->avail_min = params->avail_min;
901 runtime->start_threshold = params->start_threshold;
902 runtime->stop_threshold = params->stop_threshold;
903 runtime->silence_threshold = params->silence_threshold;
904 runtime->silence_size = params->silence_size;
905 params->boundary = runtime->boundary;
906 if (snd_pcm_running(substream)) {
907 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
908 runtime->silence_size > 0)
909 snd_pcm_playback_silence(substream, ULONG_MAX);
910 err = snd_pcm_update_state(substream, runtime);
912 snd_pcm_stream_unlock_irq(substream);
916 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
917 struct snd_pcm_sw_params __user * _params)
919 struct snd_pcm_sw_params params;
921 if (copy_from_user(¶ms, _params, sizeof(params)))
923 err = snd_pcm_sw_params(substream, ¶ms);
924 if (copy_to_user(_params, ¶ms, sizeof(params)))
929 static inline snd_pcm_uframes_t
930 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
932 snd_pcm_uframes_t delay;
934 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
935 delay = snd_pcm_playback_hw_avail(substream->runtime);
937 delay = snd_pcm_capture_avail(substream->runtime);
938 return delay + substream->runtime->delay;
941 int snd_pcm_status64(struct snd_pcm_substream *substream,
942 struct snd_pcm_status64 *status)
944 struct snd_pcm_runtime *runtime = substream->runtime;
946 snd_pcm_stream_lock_irq(substream);
948 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
949 &runtime->audio_tstamp_config);
951 /* backwards compatible behavior */
952 if (runtime->audio_tstamp_config.type_requested ==
953 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
954 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
955 runtime->audio_tstamp_config.type_requested =
956 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
958 runtime->audio_tstamp_config.type_requested =
959 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
960 runtime->audio_tstamp_report.valid = 0;
962 runtime->audio_tstamp_report.valid = 1;
964 status->state = runtime->status->state;
965 status->suspended_state = runtime->status->suspended_state;
966 if (status->state == SNDRV_PCM_STATE_OPEN)
968 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
969 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
970 if (snd_pcm_running(substream)) {
971 snd_pcm_update_hw_ptr(substream);
972 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
973 status->tstamp_sec = runtime->status->tstamp.tv_sec;
974 status->tstamp_nsec =
975 runtime->status->tstamp.tv_nsec;
976 status->driver_tstamp_sec =
977 runtime->driver_tstamp.tv_sec;
978 status->driver_tstamp_nsec =
979 runtime->driver_tstamp.tv_nsec;
980 status->audio_tstamp_sec =
981 runtime->status->audio_tstamp.tv_sec;
982 status->audio_tstamp_nsec =
983 runtime->status->audio_tstamp.tv_nsec;
984 if (runtime->audio_tstamp_report.valid == 1)
985 /* backwards compatibility, no report provided in COMPAT mode */
986 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
987 &status->audio_tstamp_accuracy,
988 &runtime->audio_tstamp_report);
993 /* get tstamp only in fallback mode and only if enabled */
994 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
995 struct timespec64 tstamp;
997 snd_pcm_gettime(runtime, &tstamp);
998 status->tstamp_sec = tstamp.tv_sec;
999 status->tstamp_nsec = tstamp.tv_nsec;
1003 status->appl_ptr = runtime->control->appl_ptr;
1004 status->hw_ptr = runtime->status->hw_ptr;
1005 status->avail = snd_pcm_avail(substream);
1006 status->delay = snd_pcm_running(substream) ?
1007 snd_pcm_calc_delay(substream) : 0;
1008 status->avail_max = runtime->avail_max;
1009 status->overrange = runtime->overrange;
1010 runtime->avail_max = 0;
1011 runtime->overrange = 0;
1013 snd_pcm_stream_unlock_irq(substream);
1017 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
1018 struct snd_pcm_status64 __user * _status,
1021 struct snd_pcm_status64 status;
1024 memset(&status, 0, sizeof(status));
1026 * with extension, parameters are read/write,
1027 * get audio_tstamp_data from user,
1028 * ignore rest of status structure
1030 if (ext && get_user(status.audio_tstamp_data,
1031 (u32 __user *)(&_status->audio_tstamp_data)))
1033 res = snd_pcm_status64(substream, &status);
1036 if (copy_to_user(_status, &status, sizeof(status)))
1041 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1042 struct snd_pcm_status32 __user * _status,
1045 struct snd_pcm_status64 status64;
1046 struct snd_pcm_status32 status32;
1049 memset(&status64, 0, sizeof(status64));
1050 memset(&status32, 0, sizeof(status32));
1052 * with extension, parameters are read/write,
1053 * get audio_tstamp_data from user,
1054 * ignore rest of status structure
1056 if (ext && get_user(status64.audio_tstamp_data,
1057 (u32 __user *)(&_status->audio_tstamp_data)))
1059 res = snd_pcm_status64(substream, &status64);
1063 status32 = (struct snd_pcm_status32) {
1064 .state = status64.state,
1065 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1066 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1067 .tstamp_sec = status64.tstamp_sec,
1068 .tstamp_nsec = status64.tstamp_nsec,
1069 .appl_ptr = status64.appl_ptr,
1070 .hw_ptr = status64.hw_ptr,
1071 .delay = status64.delay,
1072 .avail = status64.avail,
1073 .avail_max = status64.avail_max,
1074 .overrange = status64.overrange,
1075 .suspended_state = status64.suspended_state,
1076 .audio_tstamp_data = status64.audio_tstamp_data,
1077 .audio_tstamp_sec = status64.audio_tstamp_sec,
1078 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1079 .driver_tstamp_sec = status64.audio_tstamp_sec,
1080 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1081 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1084 if (copy_to_user(_status, &status32, sizeof(status32)))
1090 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1091 struct snd_pcm_channel_info * info)
1093 struct snd_pcm_runtime *runtime;
1094 unsigned int channel;
1096 channel = info->channel;
1097 runtime = substream->runtime;
1098 snd_pcm_stream_lock_irq(substream);
1099 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1100 snd_pcm_stream_unlock_irq(substream);
1103 snd_pcm_stream_unlock_irq(substream);
1104 if (channel >= runtime->channels)
1106 memset(info, 0, sizeof(*info));
1107 info->channel = channel;
1108 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1111 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1112 struct snd_pcm_channel_info __user * _info)
1114 struct snd_pcm_channel_info info;
1117 if (copy_from_user(&info, _info, sizeof(info)))
1119 res = snd_pcm_channel_info(substream, &info);
1122 if (copy_to_user(_info, &info, sizeof(info)))
1127 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 if (runtime->trigger_master == NULL)
1132 if (runtime->trigger_master == substream) {
1133 if (!runtime->trigger_tstamp_latched)
1134 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1136 snd_pcm_trigger_tstamp(runtime->trigger_master);
1137 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1139 runtime->trigger_master = NULL;
1142 #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1145 int (*pre_action)(struct snd_pcm_substream *substream,
1146 snd_pcm_state_t state);
1147 int (*do_action)(struct snd_pcm_substream *substream,
1148 snd_pcm_state_t state);
1149 void (*undo_action)(struct snd_pcm_substream *substream,
1150 snd_pcm_state_t state);
1151 void (*post_action)(struct snd_pcm_substream *substream,
1152 snd_pcm_state_t state);
1156 * this functions is core for handling of linked stream
1157 * Note: the stream state might be changed also on failure
1158 * Note2: call with calling stream lock + link lock
1160 static int snd_pcm_action_group(const struct action_ops *ops,
1161 struct snd_pcm_substream *substream,
1162 snd_pcm_state_t state,
1165 struct snd_pcm_substream *s = NULL;
1166 struct snd_pcm_substream *s1;
1167 int res = 0, depth = 1;
1169 snd_pcm_group_for_each_entry(s, substream) {
1170 if (do_lock && s != substream) {
1171 if (s->pcm->nonatomic)
1172 mutex_lock_nested(&s->self_group.mutex, depth);
1174 spin_lock_nested(&s->self_group.lock, depth);
1177 res = ops->pre_action(s, state);
1181 snd_pcm_group_for_each_entry(s, substream) {
1182 res = ops->do_action(s, state);
1184 if (ops->undo_action) {
1185 snd_pcm_group_for_each_entry(s1, substream) {
1186 if (s1 == s) /* failed stream */
1188 ops->undo_action(s1, state);
1191 s = NULL; /* unlock all */
1195 snd_pcm_group_for_each_entry(s, substream) {
1196 ops->post_action(s, state);
1200 /* unlock streams */
1201 snd_pcm_group_for_each_entry(s1, substream) {
1202 if (s1 != substream) {
1203 if (s1->pcm->nonatomic)
1204 mutex_unlock(&s1->self_group.mutex);
1206 spin_unlock(&s1->self_group.lock);
1208 if (s1 == s) /* end */
1216 * Note: call with stream lock
1218 static int snd_pcm_action_single(const struct action_ops *ops,
1219 struct snd_pcm_substream *substream,
1220 snd_pcm_state_t state)
1224 res = ops->pre_action(substream, state);
1227 res = ops->do_action(substream, state);
1229 ops->post_action(substream, state);
1230 else if (ops->undo_action)
1231 ops->undo_action(substream, state);
1235 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1236 struct snd_pcm_group *new_group)
1238 substream->group = new_group;
1239 list_move(&substream->link_list, &new_group->substreams);
1243 * Unref and unlock the group, but keep the stream lock;
1244 * when the group becomes empty and no longer referred, destroy itself
1246 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1247 struct snd_pcm_substream *substream)
1253 do_free = refcount_dec_and_test(&group->refs);
1254 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1260 * Lock the group inside a stream lock and reference it;
1261 * return the locked group object, or NULL if not linked
1263 static struct snd_pcm_group *
1264 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1266 bool nonatomic = substream->pcm->nonatomic;
1267 struct snd_pcm_group *group;
1271 if (!snd_pcm_stream_linked(substream))
1273 group = substream->group;
1274 /* block freeing the group object */
1275 refcount_inc(&group->refs);
1277 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1278 spin_trylock(&group->lock);
1282 /* re-lock for avoiding ABBA deadlock */
1283 snd_pcm_stream_unlock(substream);
1284 snd_pcm_group_lock(group, nonatomic);
1285 snd_pcm_stream_lock(substream);
1287 /* check the group again; the above opens a small race window */
1288 if (substream->group == group)
1290 /* group changed, try again */
1291 snd_pcm_group_unref(group, substream);
1297 * Note: call with stream lock
1299 static int snd_pcm_action(const struct action_ops *ops,
1300 struct snd_pcm_substream *substream,
1301 snd_pcm_state_t state)
1303 struct snd_pcm_group *group;
1306 group = snd_pcm_stream_group_ref(substream);
1308 res = snd_pcm_action_group(ops, substream, state, true);
1310 res = snd_pcm_action_single(ops, substream, state);
1311 snd_pcm_group_unref(group, substream);
1316 * Note: don't use any locks before
1318 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1319 struct snd_pcm_substream *substream,
1320 snd_pcm_state_t state)
1324 snd_pcm_stream_lock_irq(substream);
1325 res = snd_pcm_action(ops, substream, state);
1326 snd_pcm_stream_unlock_irq(substream);
1332 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1333 struct snd_pcm_substream *substream,
1334 snd_pcm_state_t state)
1338 /* Guarantee the group members won't change during non-atomic action */
1339 down_read(&snd_pcm_link_rwsem);
1340 if (snd_pcm_stream_linked(substream))
1341 res = snd_pcm_action_group(ops, substream, state, false);
1343 res = snd_pcm_action_single(ops, substream, state);
1344 up_read(&snd_pcm_link_rwsem);
1351 static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1352 snd_pcm_state_t state)
1354 struct snd_pcm_runtime *runtime = substream->runtime;
1355 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1357 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1358 !snd_pcm_playback_data(substream))
1360 runtime->trigger_tstamp_latched = false;
1361 runtime->trigger_master = substream;
1365 static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1366 snd_pcm_state_t state)
1368 if (substream->runtime->trigger_master != substream)
1370 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1373 static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1374 snd_pcm_state_t state)
1376 if (substream->runtime->trigger_master == substream)
1377 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1380 static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1381 snd_pcm_state_t state)
1383 struct snd_pcm_runtime *runtime = substream->runtime;
1384 snd_pcm_trigger_tstamp(substream);
1385 runtime->hw_ptr_jiffies = jiffies;
1386 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1388 runtime->status->state = state;
1389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1390 runtime->silence_size > 0)
1391 snd_pcm_playback_silence(substream, ULONG_MAX);
1392 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1395 static const struct action_ops snd_pcm_action_start = {
1396 .pre_action = snd_pcm_pre_start,
1397 .do_action = snd_pcm_do_start,
1398 .undo_action = snd_pcm_undo_start,
1399 .post_action = snd_pcm_post_start
1403 * snd_pcm_start - start all linked streams
1404 * @substream: the PCM substream instance
1406 * Return: Zero if successful, or a negative error code.
1407 * The stream lock must be acquired before calling this function.
1409 int snd_pcm_start(struct snd_pcm_substream *substream)
1411 return snd_pcm_action(&snd_pcm_action_start, substream,
1412 SNDRV_PCM_STATE_RUNNING);
1415 /* take the stream lock and start the streams */
1416 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1418 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1419 SNDRV_PCM_STATE_RUNNING);
1425 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1426 snd_pcm_state_t state)
1428 struct snd_pcm_runtime *runtime = substream->runtime;
1429 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1431 runtime->trigger_master = substream;
1435 static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1436 snd_pcm_state_t state)
1438 if (substream->runtime->trigger_master == substream &&
1439 snd_pcm_running(substream)) {
1440 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1441 substream->runtime->stop_operating = true;
1443 return 0; /* unconditionally stop all substreams */
1446 static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1447 snd_pcm_state_t state)
1449 struct snd_pcm_runtime *runtime = substream->runtime;
1450 if (runtime->status->state != state) {
1451 snd_pcm_trigger_tstamp(substream);
1452 runtime->status->state = state;
1453 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1455 wake_up(&runtime->sleep);
1456 wake_up(&runtime->tsleep);
1459 static const struct action_ops snd_pcm_action_stop = {
1460 .pre_action = snd_pcm_pre_stop,
1461 .do_action = snd_pcm_do_stop,
1462 .post_action = snd_pcm_post_stop
1466 * snd_pcm_stop - try to stop all running streams in the substream group
1467 * @substream: the PCM substream instance
1468 * @state: PCM state after stopping the stream
1470 * The state of each stream is then changed to the given state unconditionally.
1472 * Return: Zero if successful, or a negative error code.
1474 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1476 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1478 EXPORT_SYMBOL(snd_pcm_stop);
1481 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1482 * @substream: the PCM substream
1484 * After stopping, the state is changed to SETUP.
1485 * Unlike snd_pcm_stop(), this affects only the given stream.
1487 * Return: Zero if successful, or a negative error code.
1489 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1491 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1492 SNDRV_PCM_STATE_SETUP);
1496 * snd_pcm_stop_xrun - stop the running streams as XRUN
1497 * @substream: the PCM substream instance
1499 * This stops the given running substream (and all linked substreams) as XRUN.
1500 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1502 * Return: Zero if successful, or a negative error code.
1504 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1506 unsigned long flags;
1508 snd_pcm_stream_lock_irqsave(substream, flags);
1509 if (substream->runtime && snd_pcm_running(substream))
1510 __snd_pcm_xrun(substream);
1511 snd_pcm_stream_unlock_irqrestore(substream, flags);
1514 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1517 * pause callbacks: pass boolean (to start pause or resume) as state argument
1519 #define pause_pushed(state) (__force bool)(state)
1521 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1522 snd_pcm_state_t state)
1524 struct snd_pcm_runtime *runtime = substream->runtime;
1525 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1527 if (pause_pushed(state)) {
1528 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1530 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1532 runtime->trigger_master = substream;
1536 static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1537 snd_pcm_state_t state)
1539 if (substream->runtime->trigger_master != substream)
1541 /* some drivers might use hw_ptr to recover from the pause -
1542 update the hw_ptr now */
1543 if (pause_pushed(state))
1544 snd_pcm_update_hw_ptr(substream);
1545 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1546 * a delta between the current jiffies, this gives a large enough
1547 * delta, effectively to skip the check once.
1549 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1550 return substream->ops->trigger(substream,
1551 pause_pushed(state) ?
1552 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1553 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1556 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1557 snd_pcm_state_t state)
1559 if (substream->runtime->trigger_master == substream)
1560 substream->ops->trigger(substream,
1561 pause_pushed(state) ?
1562 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1563 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1566 static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1567 snd_pcm_state_t state)
1569 struct snd_pcm_runtime *runtime = substream->runtime;
1570 snd_pcm_trigger_tstamp(substream);
1571 if (pause_pushed(state)) {
1572 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1573 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1574 wake_up(&runtime->sleep);
1575 wake_up(&runtime->tsleep);
1577 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1578 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1582 static const struct action_ops snd_pcm_action_pause = {
1583 .pre_action = snd_pcm_pre_pause,
1584 .do_action = snd_pcm_do_pause,
1585 .undo_action = snd_pcm_undo_pause,
1586 .post_action = snd_pcm_post_pause
1590 * Push/release the pause for all linked streams.
1592 static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1594 return snd_pcm_action(&snd_pcm_action_pause, substream,
1595 (__force snd_pcm_state_t)push);
1598 static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1601 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1602 (__force snd_pcm_state_t)push);
1606 /* suspend callback: state argument ignored */
1608 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1609 snd_pcm_state_t state)
1611 struct snd_pcm_runtime *runtime = substream->runtime;
1612 switch (runtime->status->state) {
1613 case SNDRV_PCM_STATE_SUSPENDED:
1615 /* unresumable PCM state; return -EBUSY for skipping suspend */
1616 case SNDRV_PCM_STATE_OPEN:
1617 case SNDRV_PCM_STATE_SETUP:
1618 case SNDRV_PCM_STATE_DISCONNECTED:
1621 runtime->trigger_master = substream;
1625 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1626 snd_pcm_state_t state)
1628 struct snd_pcm_runtime *runtime = substream->runtime;
1629 if (runtime->trigger_master != substream)
1631 if (! snd_pcm_running(substream))
1633 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1634 runtime->stop_operating = true;
1635 return 0; /* suspend unconditionally */
1638 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1639 snd_pcm_state_t state)
1641 struct snd_pcm_runtime *runtime = substream->runtime;
1642 snd_pcm_trigger_tstamp(substream);
1643 runtime->status->suspended_state = runtime->status->state;
1644 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1645 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1646 wake_up(&runtime->sleep);
1647 wake_up(&runtime->tsleep);
1650 static const struct action_ops snd_pcm_action_suspend = {
1651 .pre_action = snd_pcm_pre_suspend,
1652 .do_action = snd_pcm_do_suspend,
1653 .post_action = snd_pcm_post_suspend
1657 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1658 * @substream: the PCM substream
1660 * After this call, all streams are changed to SUSPENDED state.
1662 * Return: Zero if successful, or a negative error code.
1664 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1667 unsigned long flags;
1669 snd_pcm_stream_lock_irqsave(substream, flags);
1670 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1672 snd_pcm_stream_unlock_irqrestore(substream, flags);
1677 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1678 * @pcm: the PCM instance
1680 * After this call, all streams are changed to SUSPENDED state.
1682 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1684 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1686 struct snd_pcm_substream *substream;
1687 int stream, err = 0;
1692 for_each_pcm_substream(pcm, stream, substream) {
1693 /* FIXME: the open/close code should lock this as well */
1694 if (!substream->runtime)
1698 * Skip BE dai link PCM's that are internal and may
1699 * not have their substream ops set.
1701 if (!substream->ops)
1704 err = snd_pcm_suspend(substream);
1705 if (err < 0 && err != -EBUSY)
1709 for_each_pcm_substream(pcm, stream, substream)
1710 snd_pcm_sync_stop(substream, false);
1714 EXPORT_SYMBOL(snd_pcm_suspend_all);
1716 /* resume callbacks: state argument ignored */
1718 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1719 snd_pcm_state_t state)
1721 struct snd_pcm_runtime *runtime = substream->runtime;
1722 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1724 runtime->trigger_master = substream;
1728 static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1729 snd_pcm_state_t state)
1731 struct snd_pcm_runtime *runtime = substream->runtime;
1732 if (runtime->trigger_master != substream)
1734 /* DMA not running previously? */
1735 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1736 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1737 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1739 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1742 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1743 snd_pcm_state_t state)
1745 if (substream->runtime->trigger_master == substream &&
1746 snd_pcm_running(substream))
1747 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1750 static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1751 snd_pcm_state_t state)
1753 struct snd_pcm_runtime *runtime = substream->runtime;
1754 snd_pcm_trigger_tstamp(substream);
1755 runtime->status->state = runtime->status->suspended_state;
1756 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1759 static const struct action_ops snd_pcm_action_resume = {
1760 .pre_action = snd_pcm_pre_resume,
1761 .do_action = snd_pcm_do_resume,
1762 .undo_action = snd_pcm_undo_resume,
1763 .post_action = snd_pcm_post_resume
1766 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1768 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1774 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1779 #endif /* CONFIG_PM */
1784 * Change the RUNNING stream(s) to XRUN state.
1786 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1788 struct snd_pcm_runtime *runtime = substream->runtime;
1791 snd_pcm_stream_lock_irq(substream);
1792 switch (runtime->status->state) {
1793 case SNDRV_PCM_STATE_XRUN:
1794 result = 0; /* already there */
1796 case SNDRV_PCM_STATE_RUNNING:
1797 __snd_pcm_xrun(substream);
1803 snd_pcm_stream_unlock_irq(substream);
1810 /* reset callbacks: state argument ignored */
1811 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1812 snd_pcm_state_t state)
1814 struct snd_pcm_runtime *runtime = substream->runtime;
1815 switch (runtime->status->state) {
1816 case SNDRV_PCM_STATE_RUNNING:
1817 case SNDRV_PCM_STATE_PREPARED:
1818 case SNDRV_PCM_STATE_PAUSED:
1819 case SNDRV_PCM_STATE_SUSPENDED:
1826 static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1827 snd_pcm_state_t state)
1829 struct snd_pcm_runtime *runtime = substream->runtime;
1830 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1833 runtime->hw_ptr_base = 0;
1834 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1835 runtime->status->hw_ptr % runtime->period_size;
1836 runtime->silence_start = runtime->status->hw_ptr;
1837 runtime->silence_filled = 0;
1841 static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1842 snd_pcm_state_t state)
1844 struct snd_pcm_runtime *runtime = substream->runtime;
1845 runtime->control->appl_ptr = runtime->status->hw_ptr;
1846 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1847 runtime->silence_size > 0)
1848 snd_pcm_playback_silence(substream, ULONG_MAX);
1851 static const struct action_ops snd_pcm_action_reset = {
1852 .pre_action = snd_pcm_pre_reset,
1853 .do_action = snd_pcm_do_reset,
1854 .post_action = snd_pcm_post_reset
1857 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1859 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1866 /* pass f_flags as state argument */
1867 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1868 snd_pcm_state_t state)
1870 struct snd_pcm_runtime *runtime = substream->runtime;
1871 int f_flags = (__force int)state;
1873 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1874 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1876 if (snd_pcm_running(substream))
1878 substream->f_flags = f_flags;
1882 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1883 snd_pcm_state_t state)
1886 snd_pcm_sync_stop(substream, true);
1887 err = substream->ops->prepare(substream);
1890 return snd_pcm_do_reset(substream, state);
1893 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1894 snd_pcm_state_t state)
1896 struct snd_pcm_runtime *runtime = substream->runtime;
1897 runtime->control->appl_ptr = runtime->status->hw_ptr;
1898 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1901 static const struct action_ops snd_pcm_action_prepare = {
1902 .pre_action = snd_pcm_pre_prepare,
1903 .do_action = snd_pcm_do_prepare,
1904 .post_action = snd_pcm_post_prepare
1908 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1909 * @substream: the PCM substream instance
1910 * @file: file to refer f_flags
1912 * Return: Zero if successful, or a negative error code.
1914 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1920 f_flags = file->f_flags;
1922 f_flags = substream->f_flags;
1924 snd_pcm_stream_lock_irq(substream);
1925 switch (substream->runtime->status->state) {
1926 case SNDRV_PCM_STATE_PAUSED:
1927 snd_pcm_pause(substream, false);
1929 case SNDRV_PCM_STATE_SUSPENDED:
1930 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1933 snd_pcm_stream_unlock_irq(substream);
1935 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1937 (__force snd_pcm_state_t)f_flags);
1944 /* drain init callbacks: state argument ignored */
1945 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1946 snd_pcm_state_t state)
1948 struct snd_pcm_runtime *runtime = substream->runtime;
1949 switch (runtime->status->state) {
1950 case SNDRV_PCM_STATE_OPEN:
1951 case SNDRV_PCM_STATE_DISCONNECTED:
1952 case SNDRV_PCM_STATE_SUSPENDED:
1955 runtime->trigger_master = substream;
1959 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1960 snd_pcm_state_t state)
1962 struct snd_pcm_runtime *runtime = substream->runtime;
1963 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1964 switch (runtime->status->state) {
1965 case SNDRV_PCM_STATE_PREPARED:
1966 /* start playback stream if possible */
1967 if (! snd_pcm_playback_empty(substream)) {
1968 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1969 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1971 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1974 case SNDRV_PCM_STATE_RUNNING:
1975 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1977 case SNDRV_PCM_STATE_XRUN:
1978 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1984 /* stop running stream */
1985 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1986 snd_pcm_state_t new_state;
1988 new_state = snd_pcm_capture_avail(runtime) > 0 ?
1989 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1990 snd_pcm_do_stop(substream, new_state);
1991 snd_pcm_post_stop(substream, new_state);
1995 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1996 runtime->trigger_master == substream &&
1997 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1998 return substream->ops->trigger(substream,
1999 SNDRV_PCM_TRIGGER_DRAIN);
2004 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
2005 snd_pcm_state_t state)
2009 static const struct action_ops snd_pcm_action_drain_init = {
2010 .pre_action = snd_pcm_pre_drain_init,
2011 .do_action = snd_pcm_do_drain_init,
2012 .post_action = snd_pcm_post_drain_init
2016 * Drain the stream(s).
2017 * When the substream is linked, sync until the draining of all playback streams
2019 * After this call, all streams are supposed to be either SETUP or DRAINING
2020 * (capture only) state.
2022 static int snd_pcm_drain(struct snd_pcm_substream *substream,
2025 struct snd_card *card;
2026 struct snd_pcm_runtime *runtime;
2027 struct snd_pcm_substream *s;
2028 struct snd_pcm_group *group;
2029 wait_queue_entry_t wait;
2033 card = substream->pcm->card;
2034 runtime = substream->runtime;
2036 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2040 if (file->f_flags & O_NONBLOCK)
2042 } else if (substream->f_flags & O_NONBLOCK)
2045 snd_pcm_stream_lock_irq(substream);
2047 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2048 snd_pcm_pause(substream, false);
2050 /* pre-start/stop - all running streams are changed to DRAINING state */
2051 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2055 /* in non-blocking, we don't wait in ioctl but let caller poll */
2063 struct snd_pcm_runtime *to_check;
2064 if (signal_pending(current)) {
2065 result = -ERESTARTSYS;
2068 /* find a substream to drain */
2070 group = snd_pcm_stream_group_ref(substream);
2071 snd_pcm_group_for_each_entry(s, substream) {
2072 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2074 runtime = s->runtime;
2075 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2080 snd_pcm_group_unref(group, substream);
2082 break; /* all drained */
2083 init_waitqueue_entry(&wait, current);
2084 set_current_state(TASK_INTERRUPTIBLE);
2085 add_wait_queue(&to_check->sleep, &wait);
2086 snd_pcm_stream_unlock_irq(substream);
2087 if (runtime->no_period_wakeup)
2088 tout = MAX_SCHEDULE_TIMEOUT;
2091 if (runtime->rate) {
2092 long t = runtime->period_size * 2 / runtime->rate;
2093 tout = max(t, tout);
2095 tout = msecs_to_jiffies(tout * 1000);
2097 tout = schedule_timeout(tout);
2099 snd_pcm_stream_lock_irq(substream);
2100 group = snd_pcm_stream_group_ref(substream);
2101 snd_pcm_group_for_each_entry(s, substream) {
2102 if (s->runtime == to_check) {
2103 remove_wait_queue(&to_check->sleep, &wait);
2107 snd_pcm_group_unref(group, substream);
2109 if (card->shutdown) {
2114 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2117 dev_dbg(substream->pcm->card->dev,
2118 "playback drain error (DMA or IRQ trouble?)\n");
2119 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2127 snd_pcm_stream_unlock_irq(substream);
2135 * Immediately put all linked substreams into SETUP state.
2137 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2139 struct snd_pcm_runtime *runtime;
2142 if (PCM_RUNTIME_CHECK(substream))
2144 runtime = substream->runtime;
2146 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2147 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2150 snd_pcm_stream_lock_irq(substream);
2152 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2153 snd_pcm_pause(substream, false);
2155 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2156 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2157 snd_pcm_stream_unlock_irq(substream);
2163 static bool is_pcm_file(struct file *file)
2165 struct inode *inode = file_inode(file);
2166 struct snd_pcm *pcm;
2169 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2171 minor = iminor(inode);
2172 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2174 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2177 snd_card_unref(pcm->card);
2184 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2187 struct snd_pcm_file *pcm_file;
2188 struct snd_pcm_substream *substream1;
2189 struct snd_pcm_group *group, *target_group;
2190 bool nonatomic = substream->pcm->nonatomic;
2191 struct fd f = fdget(fd);
2195 if (!is_pcm_file(f.file)) {
2199 pcm_file = f.file->private_data;
2200 substream1 = pcm_file->substream;
2202 if (substream == substream1) {
2207 group = kzalloc(sizeof(*group), GFP_KERNEL);
2212 snd_pcm_group_init(group);
2214 down_write(&snd_pcm_link_rwsem);
2215 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2216 substream->runtime->status->state != substream1->runtime->status->state ||
2217 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2221 if (snd_pcm_stream_linked(substream1)) {
2226 snd_pcm_stream_lock_irq(substream);
2227 if (!snd_pcm_stream_linked(substream)) {
2228 snd_pcm_group_assign(substream, group);
2229 group = NULL; /* assigned, don't free this one below */
2231 target_group = substream->group;
2232 snd_pcm_stream_unlock_irq(substream);
2234 snd_pcm_group_lock_irq(target_group, nonatomic);
2235 snd_pcm_stream_lock_nested(substream1);
2236 snd_pcm_group_assign(substream1, target_group);
2237 refcount_inc(&target_group->refs);
2238 snd_pcm_stream_unlock(substream1);
2239 snd_pcm_group_unlock_irq(target_group, nonatomic);
2241 up_write(&snd_pcm_link_rwsem);
2249 static void relink_to_local(struct snd_pcm_substream *substream)
2251 snd_pcm_stream_lock_nested(substream);
2252 snd_pcm_group_assign(substream, &substream->self_group);
2253 snd_pcm_stream_unlock(substream);
2256 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2258 struct snd_pcm_group *group;
2259 bool nonatomic = substream->pcm->nonatomic;
2260 bool do_free = false;
2263 down_write(&snd_pcm_link_rwsem);
2265 if (!snd_pcm_stream_linked(substream)) {
2270 group = substream->group;
2271 snd_pcm_group_lock_irq(group, nonatomic);
2273 relink_to_local(substream);
2274 refcount_dec(&group->refs);
2276 /* detach the last stream, too */
2277 if (list_is_singular(&group->substreams)) {
2278 relink_to_local(list_first_entry(&group->substreams,
2279 struct snd_pcm_substream,
2281 do_free = refcount_dec_and_test(&group->refs);
2284 snd_pcm_group_unlock_irq(group, nonatomic);
2289 up_write(&snd_pcm_link_rwsem);
2296 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2297 struct snd_pcm_hw_rule *rule)
2299 struct snd_interval t;
2300 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2301 hw_param_interval_c(params, rule->deps[1]), &t);
2302 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2305 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2306 struct snd_pcm_hw_rule *rule)
2308 struct snd_interval t;
2309 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2310 hw_param_interval_c(params, rule->deps[1]), &t);
2311 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2314 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2315 struct snd_pcm_hw_rule *rule)
2317 struct snd_interval t;
2318 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2319 hw_param_interval_c(params, rule->deps[1]),
2320 (unsigned long) rule->private, &t);
2321 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2324 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2325 struct snd_pcm_hw_rule *rule)
2327 struct snd_interval t;
2328 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2329 (unsigned long) rule->private,
2330 hw_param_interval_c(params, rule->deps[1]), &t);
2331 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2334 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2335 struct snd_pcm_hw_rule *rule)
2338 const struct snd_interval *i =
2339 hw_param_interval_c(params, rule->deps[0]);
2341 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2343 pcm_for_each_format(k) {
2345 if (!snd_mask_test_format(mask, k))
2347 bits = snd_pcm_format_physical_width(k);
2349 continue; /* ignore invalid formats */
2350 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2351 snd_mask_reset(&m, (__force unsigned)k);
2353 return snd_mask_refine(mask, &m);
2356 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2357 struct snd_pcm_hw_rule *rule)
2359 struct snd_interval t;
2366 pcm_for_each_format(k) {
2368 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2370 bits = snd_pcm_format_physical_width(k);
2372 continue; /* ignore invalid formats */
2373 if (t.min > (unsigned)bits)
2375 if (t.max < (unsigned)bits)
2379 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2382 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2383 #error "Change this table"
2386 static const unsigned int rates[] = {
2387 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2388 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2391 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2392 .count = ARRAY_SIZE(rates),
2396 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2397 struct snd_pcm_hw_rule *rule)
2399 struct snd_pcm_hardware *hw = rule->private;
2400 return snd_interval_list(hw_param_interval(params, rule->var),
2401 snd_pcm_known_rates.count,
2402 snd_pcm_known_rates.list, hw->rates);
2405 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2406 struct snd_pcm_hw_rule *rule)
2408 struct snd_interval t;
2409 struct snd_pcm_substream *substream = rule->private;
2411 t.max = substream->buffer_bytes_max;
2415 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2418 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2420 struct snd_pcm_runtime *runtime = substream->runtime;
2421 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2424 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2425 snd_mask_any(constrs_mask(constrs, k));
2428 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2429 snd_interval_any(constrs_interval(constrs, k));
2432 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2433 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2434 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2435 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2436 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2438 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2439 snd_pcm_hw_rule_format, NULL,
2440 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2443 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2444 snd_pcm_hw_rule_sample_bits, NULL,
2445 SNDRV_PCM_HW_PARAM_FORMAT,
2446 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2449 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2450 snd_pcm_hw_rule_div, NULL,
2451 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2454 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2455 snd_pcm_hw_rule_mul, NULL,
2456 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2459 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2460 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2461 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2464 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2465 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2466 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2469 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2470 snd_pcm_hw_rule_div, NULL,
2471 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2474 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2475 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2476 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2479 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2480 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2481 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2484 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2485 snd_pcm_hw_rule_div, NULL,
2486 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2489 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2490 snd_pcm_hw_rule_div, NULL,
2491 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2494 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2495 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2496 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2499 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2500 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2501 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2504 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2505 snd_pcm_hw_rule_mul, NULL,
2506 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2509 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2510 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2511 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2514 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2515 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2516 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2519 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2520 snd_pcm_hw_rule_muldivk, (void*) 8,
2521 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2524 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2525 snd_pcm_hw_rule_muldivk, (void*) 8,
2526 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2529 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2530 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2531 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2534 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2535 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2536 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2542 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2544 struct snd_pcm_runtime *runtime = substream->runtime;
2545 struct snd_pcm_hardware *hw = &runtime->hw;
2547 unsigned int mask = 0;
2549 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2550 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2551 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2552 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2553 if (hw_support_mmap(substream)) {
2554 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2555 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2556 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2557 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2558 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2559 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2561 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2565 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2569 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2570 PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
2574 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2575 hw->channels_min, hw->channels_max);
2579 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2580 hw->rate_min, hw->rate_max);
2584 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2585 hw->period_bytes_min, hw->period_bytes_max);
2589 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2590 hw->periods_min, hw->periods_max);
2594 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2595 hw->period_bytes_min, hw->buffer_bytes_max);
2599 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2600 snd_pcm_hw_rule_buffer_bytes_max, substream,
2601 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2606 if (runtime->dma_bytes) {
2607 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2612 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2613 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2614 snd_pcm_hw_rule_rate, hw,
2615 SNDRV_PCM_HW_PARAM_RATE, -1);
2620 /* FIXME: this belong to lowlevel */
2621 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2626 static void pcm_release_private(struct snd_pcm_substream *substream)
2628 if (snd_pcm_stream_linked(substream))
2629 snd_pcm_unlink(substream);
2632 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2634 substream->ref_count--;
2635 if (substream->ref_count > 0)
2638 snd_pcm_drop(substream);
2639 if (substream->hw_opened) {
2640 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2641 do_hw_free(substream);
2642 substream->ops->close(substream);
2643 substream->hw_opened = 0;
2645 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2646 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2647 if (substream->pcm_release) {
2648 substream->pcm_release(substream);
2649 substream->pcm_release = NULL;
2651 snd_pcm_detach_substream(substream);
2653 EXPORT_SYMBOL(snd_pcm_release_substream);
2655 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2657 struct snd_pcm_substream **rsubstream)
2659 struct snd_pcm_substream *substream;
2662 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2665 if (substream->ref_count > 1) {
2666 *rsubstream = substream;
2670 err = snd_pcm_hw_constraints_init(substream);
2672 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2676 err = substream->ops->open(substream);
2680 substream->hw_opened = 1;
2682 err = snd_pcm_hw_constraints_complete(substream);
2684 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2688 /* automatically set EXPLICIT_SYNC flag in the managed mode whenever
2689 * the DMA buffer requires it
2691 if (substream->managed_buffer_alloc &&
2692 substream->dma_buffer.dev.need_sync)
2693 substream->runtime->hw.info |= SNDRV_PCM_INFO_EXPLICIT_SYNC;
2695 *rsubstream = substream;
2699 snd_pcm_release_substream(substream);
2702 EXPORT_SYMBOL(snd_pcm_open_substream);
2704 static int snd_pcm_open_file(struct file *file,
2705 struct snd_pcm *pcm,
2708 struct snd_pcm_file *pcm_file;
2709 struct snd_pcm_substream *substream;
2712 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2716 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2717 if (pcm_file == NULL) {
2718 snd_pcm_release_substream(substream);
2721 pcm_file->substream = substream;
2722 if (substream->ref_count == 1)
2723 substream->pcm_release = pcm_release_private;
2724 file->private_data = pcm_file;
2729 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2731 struct snd_pcm *pcm;
2732 int err = nonseekable_open(inode, file);
2735 pcm = snd_lookup_minor_data(iminor(inode),
2736 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2737 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2739 snd_card_unref(pcm->card);
2743 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2745 struct snd_pcm *pcm;
2746 int err = nonseekable_open(inode, file);
2749 pcm = snd_lookup_minor_data(iminor(inode),
2750 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2751 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2753 snd_card_unref(pcm->card);
2757 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2760 wait_queue_entry_t wait;
2766 err = snd_card_file_add(pcm->card, file);
2769 if (!try_module_get(pcm->card->module)) {
2773 init_waitqueue_entry(&wait, current);
2774 add_wait_queue(&pcm->open_wait, &wait);
2775 mutex_lock(&pcm->open_mutex);
2777 err = snd_pcm_open_file(file, pcm, stream);
2780 if (err == -EAGAIN) {
2781 if (file->f_flags & O_NONBLOCK) {
2787 set_current_state(TASK_INTERRUPTIBLE);
2788 mutex_unlock(&pcm->open_mutex);
2790 mutex_lock(&pcm->open_mutex);
2791 if (pcm->card->shutdown) {
2795 if (signal_pending(current)) {
2800 remove_wait_queue(&pcm->open_wait, &wait);
2801 mutex_unlock(&pcm->open_mutex);
2807 module_put(pcm->card->module);
2809 snd_card_file_remove(pcm->card, file);
2814 static int snd_pcm_release(struct inode *inode, struct file *file)
2816 struct snd_pcm *pcm;
2817 struct snd_pcm_substream *substream;
2818 struct snd_pcm_file *pcm_file;
2820 pcm_file = file->private_data;
2821 substream = pcm_file->substream;
2822 if (snd_BUG_ON(!substream))
2824 pcm = substream->pcm;
2826 /* block until the device gets woken up as it may touch the hardware */
2827 snd_power_wait(pcm->card);
2829 mutex_lock(&pcm->open_mutex);
2830 snd_pcm_release_substream(substream);
2832 mutex_unlock(&pcm->open_mutex);
2833 wake_up(&pcm->open_wait);
2834 module_put(pcm->card->module);
2835 snd_card_file_remove(pcm->card, file);
2839 /* check and update PCM state; return 0 or a negative error
2840 * call this inside PCM lock
2842 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2844 switch (substream->runtime->status->state) {
2845 case SNDRV_PCM_STATE_DRAINING:
2846 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2849 case SNDRV_PCM_STATE_RUNNING:
2850 return snd_pcm_update_hw_ptr(substream);
2851 case SNDRV_PCM_STATE_PREPARED:
2852 case SNDRV_PCM_STATE_PAUSED:
2854 case SNDRV_PCM_STATE_SUSPENDED:
2856 case SNDRV_PCM_STATE_XRUN:
2863 /* increase the appl_ptr; returns the processed frames or a negative error */
2864 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2865 snd_pcm_uframes_t frames,
2866 snd_pcm_sframes_t avail)
2868 struct snd_pcm_runtime *runtime = substream->runtime;
2869 snd_pcm_sframes_t appl_ptr;
2874 if (frames > (snd_pcm_uframes_t)avail)
2876 appl_ptr = runtime->control->appl_ptr + frames;
2877 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2878 appl_ptr -= runtime->boundary;
2879 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2880 return ret < 0 ? ret : frames;
2883 /* decrease the appl_ptr; returns the processed frames or zero for error */
2884 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2885 snd_pcm_uframes_t frames,
2886 snd_pcm_sframes_t avail)
2888 struct snd_pcm_runtime *runtime = substream->runtime;
2889 snd_pcm_sframes_t appl_ptr;
2894 if (frames > (snd_pcm_uframes_t)avail)
2896 appl_ptr = runtime->control->appl_ptr - frames;
2898 appl_ptr += runtime->boundary;
2899 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2900 /* NOTE: we return zero for errors because PulseAudio gets depressed
2901 * upon receiving an error from rewind ioctl and stops processing
2902 * any longer. Returning zero means that no rewind is done, so
2903 * it's not absolutely wrong to answer like that.
2905 return ret < 0 ? 0 : frames;
2908 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2909 snd_pcm_uframes_t frames)
2911 snd_pcm_sframes_t ret;
2916 snd_pcm_stream_lock_irq(substream);
2917 ret = do_pcm_hwsync(substream);
2919 ret = rewind_appl_ptr(substream, frames,
2920 snd_pcm_hw_avail(substream));
2921 snd_pcm_stream_unlock_irq(substream);
2923 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
2927 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2928 snd_pcm_uframes_t frames)
2930 snd_pcm_sframes_t ret;
2935 snd_pcm_stream_lock_irq(substream);
2936 ret = do_pcm_hwsync(substream);
2938 ret = forward_appl_ptr(substream, frames,
2939 snd_pcm_avail(substream));
2940 snd_pcm_stream_unlock_irq(substream);
2942 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
2946 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2947 snd_pcm_sframes_t *delay)
2951 snd_pcm_stream_lock_irq(substream);
2952 err = do_pcm_hwsync(substream);
2954 *delay = snd_pcm_calc_delay(substream);
2955 snd_pcm_stream_unlock_irq(substream);
2956 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
2961 static inline int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2963 return snd_pcm_delay(substream, NULL);
2966 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2967 struct snd_pcm_sync_ptr __user *_sync_ptr)
2969 struct snd_pcm_runtime *runtime = substream->runtime;
2970 struct snd_pcm_sync_ptr sync_ptr;
2971 volatile struct snd_pcm_mmap_status *status;
2972 volatile struct snd_pcm_mmap_control *control;
2975 memset(&sync_ptr, 0, sizeof(sync_ptr));
2976 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2978 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2980 status = runtime->status;
2981 control = runtime->control;
2982 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2983 err = snd_pcm_hwsync(substream);
2987 snd_pcm_stream_lock_irq(substream);
2988 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2989 err = pcm_lib_apply_appl_ptr(substream,
2990 sync_ptr.c.control.appl_ptr);
2992 snd_pcm_stream_unlock_irq(substream);
2996 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2998 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2999 control->avail_min = sync_ptr.c.control.avail_min;
3001 sync_ptr.c.control.avail_min = control->avail_min;
3002 sync_ptr.s.status.state = status->state;
3003 sync_ptr.s.status.hw_ptr = status->hw_ptr;
3004 sync_ptr.s.status.tstamp = status->tstamp;
3005 sync_ptr.s.status.suspended_state = status->suspended_state;
3006 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
3007 snd_pcm_stream_unlock_irq(substream);
3008 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
3009 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3010 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
3015 struct snd_pcm_mmap_status32 {
3016 snd_pcm_state_t state;
3021 snd_pcm_state_t suspended_state;
3022 s32 audio_tstamp_sec;
3023 s32 audio_tstamp_nsec;
3024 } __attribute__((packed));
3026 struct snd_pcm_mmap_control32 {
3031 struct snd_pcm_sync_ptr32 {
3034 struct snd_pcm_mmap_status32 status;
3035 unsigned char reserved[64];
3038 struct snd_pcm_mmap_control32 control;
3039 unsigned char reserved[64];
3041 } __attribute__((packed));
3043 /* recalcuate the boundary within 32bit */
3044 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
3046 snd_pcm_uframes_t boundary;
3048 if (! runtime->buffer_size)
3050 boundary = runtime->buffer_size;
3051 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3056 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3057 struct snd_pcm_sync_ptr32 __user *src)
3059 struct snd_pcm_runtime *runtime = substream->runtime;
3060 volatile struct snd_pcm_mmap_status *status;
3061 volatile struct snd_pcm_mmap_control *control;
3063 struct snd_pcm_mmap_control scontrol;
3064 struct snd_pcm_mmap_status sstatus;
3065 snd_pcm_uframes_t boundary;
3068 if (snd_BUG_ON(!runtime))
3071 if (get_user(sflags, &src->flags) ||
3072 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3073 get_user(scontrol.avail_min, &src->c.control.avail_min))
3075 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3076 err = snd_pcm_hwsync(substream);
3080 status = runtime->status;
3081 control = runtime->control;
3082 boundary = recalculate_boundary(runtime);
3084 boundary = 0x7fffffff;
3085 snd_pcm_stream_lock_irq(substream);
3086 /* FIXME: we should consider the boundary for the sync from app */
3087 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3088 err = pcm_lib_apply_appl_ptr(substream,
3091 snd_pcm_stream_unlock_irq(substream);
3095 scontrol.appl_ptr = control->appl_ptr % boundary;
3096 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3097 control->avail_min = scontrol.avail_min;
3099 scontrol.avail_min = control->avail_min;
3100 sstatus.state = status->state;
3101 sstatus.hw_ptr = status->hw_ptr % boundary;
3102 sstatus.tstamp = status->tstamp;
3103 sstatus.suspended_state = status->suspended_state;
3104 sstatus.audio_tstamp = status->audio_tstamp;
3105 snd_pcm_stream_unlock_irq(substream);
3106 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3107 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3108 if (put_user(sstatus.state, &src->s.status.state) ||
3109 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3110 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3111 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3112 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3113 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3114 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3115 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3116 put_user(scontrol.avail_min, &src->c.control.avail_min))
3121 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3123 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3125 struct snd_pcm_runtime *runtime = substream->runtime;
3128 if (get_user(arg, _arg))
3130 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3132 runtime->tstamp_type = arg;
3136 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3137 struct snd_xferi __user *_xferi)
3139 struct snd_xferi xferi;
3140 struct snd_pcm_runtime *runtime = substream->runtime;
3141 snd_pcm_sframes_t result;
3143 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3145 if (put_user(0, &_xferi->result))
3147 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3149 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3150 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3152 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3153 if (put_user(result, &_xferi->result))
3155 return result < 0 ? result : 0;
3158 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3159 struct snd_xfern __user *_xfern)
3161 struct snd_xfern xfern;
3162 struct snd_pcm_runtime *runtime = substream->runtime;
3164 snd_pcm_sframes_t result;
3166 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3168 if (runtime->channels > 128)
3170 if (put_user(0, &_xfern->result))
3172 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3175 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3177 return PTR_ERR(bufs);
3178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3179 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3181 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3183 if (put_user(result, &_xfern->result))
3185 return result < 0 ? result : 0;
3188 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3189 snd_pcm_uframes_t __user *_frames)
3191 snd_pcm_uframes_t frames;
3192 snd_pcm_sframes_t result;
3194 if (get_user(frames, _frames))
3196 if (put_user(0, _frames))
3198 result = snd_pcm_rewind(substream, frames);
3199 if (put_user(result, _frames))
3201 return result < 0 ? result : 0;
3204 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3205 snd_pcm_uframes_t __user *_frames)
3207 snd_pcm_uframes_t frames;
3208 snd_pcm_sframes_t result;
3210 if (get_user(frames, _frames))
3212 if (put_user(0, _frames))
3214 result = snd_pcm_forward(substream, frames);
3215 if (put_user(result, _frames))
3217 return result < 0 ? result : 0;
3220 static int snd_pcm_common_ioctl(struct file *file,
3221 struct snd_pcm_substream *substream,
3222 unsigned int cmd, void __user *arg)
3224 struct snd_pcm_file *pcm_file = file->private_data;
3227 if (PCM_RUNTIME_CHECK(substream))
3230 if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3233 res = snd_power_wait(substream->pcm->card);
3238 case SNDRV_PCM_IOCTL_PVERSION:
3239 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3240 case SNDRV_PCM_IOCTL_INFO:
3241 return snd_pcm_info_user(substream, arg);
3242 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3244 case SNDRV_PCM_IOCTL_TTSTAMP:
3245 return snd_pcm_tstamp(substream, arg);
3246 case SNDRV_PCM_IOCTL_USER_PVERSION:
3247 if (get_user(pcm_file->user_pversion,
3248 (unsigned int __user *)arg))
3251 case SNDRV_PCM_IOCTL_HW_REFINE:
3252 return snd_pcm_hw_refine_user(substream, arg);
3253 case SNDRV_PCM_IOCTL_HW_PARAMS:
3254 return snd_pcm_hw_params_user(substream, arg);
3255 case SNDRV_PCM_IOCTL_HW_FREE:
3256 return snd_pcm_hw_free(substream);
3257 case SNDRV_PCM_IOCTL_SW_PARAMS:
3258 return snd_pcm_sw_params_user(substream, arg);
3259 case SNDRV_PCM_IOCTL_STATUS32:
3260 return snd_pcm_status_user32(substream, arg, false);
3261 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3262 return snd_pcm_status_user32(substream, arg, true);
3263 case SNDRV_PCM_IOCTL_STATUS64:
3264 return snd_pcm_status_user64(substream, arg, false);
3265 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3266 return snd_pcm_status_user64(substream, arg, true);
3267 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3268 return snd_pcm_channel_info_user(substream, arg);
3269 case SNDRV_PCM_IOCTL_PREPARE:
3270 return snd_pcm_prepare(substream, file);
3271 case SNDRV_PCM_IOCTL_RESET:
3272 return snd_pcm_reset(substream);
3273 case SNDRV_PCM_IOCTL_START:
3274 return snd_pcm_start_lock_irq(substream);
3275 case SNDRV_PCM_IOCTL_LINK:
3276 return snd_pcm_link(substream, (int)(unsigned long) arg);
3277 case SNDRV_PCM_IOCTL_UNLINK:
3278 return snd_pcm_unlink(substream);
3279 case SNDRV_PCM_IOCTL_RESUME:
3280 return snd_pcm_resume(substream);
3281 case SNDRV_PCM_IOCTL_XRUN:
3282 return snd_pcm_xrun(substream);
3283 case SNDRV_PCM_IOCTL_HWSYNC:
3284 return snd_pcm_hwsync(substream);
3285 case SNDRV_PCM_IOCTL_DELAY:
3287 snd_pcm_sframes_t delay = 0;
3288 snd_pcm_sframes_t __user *res = arg;
3291 err = snd_pcm_delay(substream, &delay);
3294 if (put_user(delay, res))
3298 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3299 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3300 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3301 return snd_pcm_sync_ptr(substream, arg);
3302 #ifdef CONFIG_SND_SUPPORT_OLD_API
3303 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3304 return snd_pcm_hw_refine_old_user(substream, arg);
3305 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3306 return snd_pcm_hw_params_old_user(substream, arg);
3308 case SNDRV_PCM_IOCTL_DRAIN:
3309 return snd_pcm_drain(substream, file);
3310 case SNDRV_PCM_IOCTL_DROP:
3311 return snd_pcm_drop(substream);
3312 case SNDRV_PCM_IOCTL_PAUSE:
3313 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3314 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3315 case SNDRV_PCM_IOCTL_READI_FRAMES:
3316 return snd_pcm_xferi_frames_ioctl(substream, arg);
3317 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3318 case SNDRV_PCM_IOCTL_READN_FRAMES:
3319 return snd_pcm_xfern_frames_ioctl(substream, arg);
3320 case SNDRV_PCM_IOCTL_REWIND:
3321 return snd_pcm_rewind_ioctl(substream, arg);
3322 case SNDRV_PCM_IOCTL_FORWARD:
3323 return snd_pcm_forward_ioctl(substream, arg);
3325 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3329 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3332 struct snd_pcm_file *pcm_file;
3334 pcm_file = file->private_data;
3336 if (((cmd >> 8) & 0xff) != 'A')
3339 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3340 (void __user *)arg);
3344 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3345 * @substream: PCM substream
3347 * @arg: IOCTL argument
3349 * The function is provided primarily for OSS layer and USB gadget drivers,
3350 * and it allows only the limited set of ioctls (hw_params, sw_params,
3351 * prepare, start, drain, drop, forward).
3353 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3354 unsigned int cmd, void *arg)
3356 snd_pcm_uframes_t *frames = arg;
3357 snd_pcm_sframes_t result;
3359 if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3363 case SNDRV_PCM_IOCTL_FORWARD:
3365 /* provided only for OSS; capture-only and no value returned */
3366 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3368 result = snd_pcm_forward(substream, *frames);
3369 return result < 0 ? result : 0;
3371 case SNDRV_PCM_IOCTL_HW_PARAMS:
3372 return snd_pcm_hw_params(substream, arg);
3373 case SNDRV_PCM_IOCTL_SW_PARAMS:
3374 return snd_pcm_sw_params(substream, arg);
3375 case SNDRV_PCM_IOCTL_PREPARE:
3376 return snd_pcm_prepare(substream, NULL);
3377 case SNDRV_PCM_IOCTL_START:
3378 return snd_pcm_start_lock_irq(substream);
3379 case SNDRV_PCM_IOCTL_DRAIN:
3380 return snd_pcm_drain(substream, NULL);
3381 case SNDRV_PCM_IOCTL_DROP:
3382 return snd_pcm_drop(substream);
3383 case SNDRV_PCM_IOCTL_DELAY:
3384 return snd_pcm_delay(substream, frames);
3389 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3391 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3394 struct snd_pcm_file *pcm_file;
3395 struct snd_pcm_substream *substream;
3396 struct snd_pcm_runtime *runtime;
3397 snd_pcm_sframes_t result;
3399 pcm_file = file->private_data;
3400 substream = pcm_file->substream;
3401 if (PCM_RUNTIME_CHECK(substream))
3403 runtime = substream->runtime;
3404 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3405 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3407 if (!frame_aligned(runtime, count))
3409 count = bytes_to_frames(runtime, count);
3410 result = snd_pcm_lib_read(substream, buf, count);
3412 result = frames_to_bytes(runtime, result);
3416 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3417 size_t count, loff_t * offset)
3419 struct snd_pcm_file *pcm_file;
3420 struct snd_pcm_substream *substream;
3421 struct snd_pcm_runtime *runtime;
3422 snd_pcm_sframes_t result;
3424 pcm_file = file->private_data;
3425 substream = pcm_file->substream;
3426 if (PCM_RUNTIME_CHECK(substream))
3428 runtime = substream->runtime;
3429 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3430 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3432 if (!frame_aligned(runtime, count))
3434 count = bytes_to_frames(runtime, count);
3435 result = snd_pcm_lib_write(substream, buf, count);
3437 result = frames_to_bytes(runtime, result);
3441 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3443 struct snd_pcm_file *pcm_file;
3444 struct snd_pcm_substream *substream;
3445 struct snd_pcm_runtime *runtime;
3446 snd_pcm_sframes_t result;
3449 snd_pcm_uframes_t frames;
3451 pcm_file = iocb->ki_filp->private_data;
3452 substream = pcm_file->substream;
3453 if (PCM_RUNTIME_CHECK(substream))
3455 runtime = substream->runtime;
3456 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3457 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3459 if (!iter_is_iovec(to))
3461 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3463 if (!frame_aligned(runtime, to->iov->iov_len))
3465 frames = bytes_to_samples(runtime, to->iov->iov_len);
3466 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3469 for (i = 0; i < to->nr_segs; ++i)
3470 bufs[i] = to->iov[i].iov_base;
3471 result = snd_pcm_lib_readv(substream, bufs, frames);
3473 result = frames_to_bytes(runtime, result);
3478 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3480 struct snd_pcm_file *pcm_file;
3481 struct snd_pcm_substream *substream;
3482 struct snd_pcm_runtime *runtime;
3483 snd_pcm_sframes_t result;
3486 snd_pcm_uframes_t frames;
3488 pcm_file = iocb->ki_filp->private_data;
3489 substream = pcm_file->substream;
3490 if (PCM_RUNTIME_CHECK(substream))
3492 runtime = substream->runtime;
3493 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3494 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3496 if (!iter_is_iovec(from))
3498 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3499 !frame_aligned(runtime, from->iov->iov_len))
3501 frames = bytes_to_samples(runtime, from->iov->iov_len);
3502 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3505 for (i = 0; i < from->nr_segs; ++i)
3506 bufs[i] = from->iov[i].iov_base;
3507 result = snd_pcm_lib_writev(substream, bufs, frames);
3509 result = frames_to_bytes(runtime, result);
3514 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3516 struct snd_pcm_file *pcm_file;
3517 struct snd_pcm_substream *substream;
3518 struct snd_pcm_runtime *runtime;
3520 snd_pcm_uframes_t avail;
3522 pcm_file = file->private_data;
3524 substream = pcm_file->substream;
3525 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3526 ok = EPOLLOUT | EPOLLWRNORM;
3528 ok = EPOLLIN | EPOLLRDNORM;
3529 if (PCM_RUNTIME_CHECK(substream))
3530 return ok | EPOLLERR;
3532 runtime = substream->runtime;
3533 if (runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3534 return ok | EPOLLERR;
3536 poll_wait(file, &runtime->sleep, wait);
3539 snd_pcm_stream_lock_irq(substream);
3540 avail = snd_pcm_avail(substream);
3541 switch (runtime->status->state) {
3542 case SNDRV_PCM_STATE_RUNNING:
3543 case SNDRV_PCM_STATE_PREPARED:
3544 case SNDRV_PCM_STATE_PAUSED:
3545 if (avail >= runtime->control->avail_min)
3548 case SNDRV_PCM_STATE_DRAINING:
3549 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3556 mask = ok | EPOLLERR;
3559 snd_pcm_stream_unlock_irq(substream);
3568 * Only on coherent architectures, we can mmap the status and the control records
3569 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3571 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3573 * mmap status record
3575 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3577 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3578 struct snd_pcm_runtime *runtime;
3580 if (substream == NULL)
3581 return VM_FAULT_SIGBUS;
3582 runtime = substream->runtime;
3583 vmf->page = virt_to_page(runtime->status);
3584 get_page(vmf->page);
3588 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3590 .fault = snd_pcm_mmap_status_fault,
3593 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3594 struct vm_area_struct *area)
3597 if (!(area->vm_flags & VM_READ))
3599 size = area->vm_end - area->vm_start;
3600 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3602 area->vm_ops = &snd_pcm_vm_ops_status;
3603 area->vm_private_data = substream;
3604 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3609 * mmap control record
3611 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3613 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3614 struct snd_pcm_runtime *runtime;
3616 if (substream == NULL)
3617 return VM_FAULT_SIGBUS;
3618 runtime = substream->runtime;
3619 vmf->page = virt_to_page(runtime->control);
3620 get_page(vmf->page);
3624 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3626 .fault = snd_pcm_mmap_control_fault,
3629 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3630 struct vm_area_struct *area)
3633 if (!(area->vm_flags & VM_READ))
3635 size = area->vm_end - area->vm_start;
3636 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3638 area->vm_ops = &snd_pcm_vm_ops_control;
3639 area->vm_private_data = substream;
3640 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3644 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3646 /* If drivers require the explicit sync (typically for non-coherent
3647 * pages), we have to disable the mmap of status and control data
3648 * to enforce the control via SYNC_PTR ioctl.
3650 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
3652 /* See pcm_control_mmap_allowed() below.
3653 * Since older alsa-lib requires both status and control mmaps to be
3654 * coupled, we have to disable the status mmap for old alsa-lib, too.
3656 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3657 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3662 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3664 if (pcm_file->no_compat_mmap)
3667 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
3669 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3670 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3671 * thus it effectively assures the manual update of appl_ptr.
3673 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3678 #else /* ! coherent mmap */
3680 * don't support mmap for status and control records.
3682 #define pcm_status_mmap_allowed(pcm_file) false
3683 #define pcm_control_mmap_allowed(pcm_file) false
3685 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3686 struct vm_area_struct *area)
3690 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3691 struct vm_area_struct *area)
3695 #endif /* coherent mmap */
3698 * fault callback for mmapping a RAM page
3700 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3702 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3703 struct snd_pcm_runtime *runtime;
3704 unsigned long offset;
3708 if (substream == NULL)
3709 return VM_FAULT_SIGBUS;
3710 runtime = substream->runtime;
3711 offset = vmf->pgoff << PAGE_SHIFT;
3712 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3713 if (offset > dma_bytes - PAGE_SIZE)
3714 return VM_FAULT_SIGBUS;
3715 if (substream->ops->page)
3716 page = substream->ops->page(substream, offset);
3717 else if (!snd_pcm_get_dma_buf(substream))
3718 page = virt_to_page(runtime->dma_area + offset);
3720 page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset);
3722 return VM_FAULT_SIGBUS;
3728 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3729 .open = snd_pcm_mmap_data_open,
3730 .close = snd_pcm_mmap_data_close,
3733 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3734 .open = snd_pcm_mmap_data_open,
3735 .close = snd_pcm_mmap_data_close,
3736 .fault = snd_pcm_mmap_data_fault,
3740 * mmap the DMA buffer on RAM
3744 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3745 * @substream: PCM substream
3748 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3749 * this function is invoked implicitly.
3751 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3752 struct vm_area_struct *area)
3754 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3755 if (!substream->ops->page &&
3756 !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area))
3758 /* mmap with fault handler */
3759 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3762 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3765 * mmap the DMA buffer on I/O memory area
3767 #if SNDRV_PCM_INFO_MMAP_IOMEM
3769 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3770 * @substream: PCM substream
3773 * When your hardware uses the iomapped pages as the hardware buffer and
3774 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3775 * is supposed to work only on limited architectures.
3777 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3778 struct vm_area_struct *area)
3780 struct snd_pcm_runtime *runtime = substream->runtime;
3782 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3783 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3785 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3786 #endif /* SNDRV_PCM_INFO_MMAP */
3791 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3792 struct vm_area_struct *area)
3794 struct snd_pcm_runtime *runtime;
3796 unsigned long offset;
3800 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3801 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3804 if (!(area->vm_flags & VM_READ))
3807 runtime = substream->runtime;
3808 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3810 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3812 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3813 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3815 size = area->vm_end - area->vm_start;
3816 offset = area->vm_pgoff << PAGE_SHIFT;
3817 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3818 if ((size_t)size > dma_bytes)
3820 if (offset > dma_bytes - size)
3823 area->vm_ops = &snd_pcm_vm_ops_data;
3824 area->vm_private_data = substream;
3825 if (substream->ops->mmap)
3826 err = substream->ops->mmap(substream, area);
3828 err = snd_pcm_lib_default_mmap(substream, area);
3830 atomic_inc(&substream->mmap_count);
3833 EXPORT_SYMBOL(snd_pcm_mmap_data);
3835 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3837 struct snd_pcm_file * pcm_file;
3838 struct snd_pcm_substream *substream;
3839 unsigned long offset;
3841 pcm_file = file->private_data;
3842 substream = pcm_file->substream;
3843 if (PCM_RUNTIME_CHECK(substream))
3845 if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3848 offset = area->vm_pgoff << PAGE_SHIFT;
3850 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3851 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3854 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3855 if (!pcm_status_mmap_allowed(pcm_file))
3857 return snd_pcm_mmap_status(substream, file, area);
3858 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3859 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3862 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3863 if (!pcm_control_mmap_allowed(pcm_file))
3865 return snd_pcm_mmap_control(substream, file, area);
3867 return snd_pcm_mmap_data(substream, file, area);
3872 static int snd_pcm_fasync(int fd, struct file * file, int on)
3874 struct snd_pcm_file * pcm_file;
3875 struct snd_pcm_substream *substream;
3876 struct snd_pcm_runtime *runtime;
3878 pcm_file = file->private_data;
3879 substream = pcm_file->substream;
3880 if (PCM_RUNTIME_CHECK(substream))
3882 runtime = substream->runtime;
3883 if (runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3885 return fasync_helper(fd, file, on, &runtime->fasync);
3891 #ifdef CONFIG_COMPAT
3892 #include "pcm_compat.c"
3894 #define snd_pcm_ioctl_compat NULL
3898 * To be removed helpers to keep binary compatibility
3901 #ifdef CONFIG_SND_SUPPORT_OLD_API
3902 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3903 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3905 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3906 struct snd_pcm_hw_params_old *oparams)
3910 memset(params, 0, sizeof(*params));
3911 params->flags = oparams->flags;
3912 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3913 params->masks[i].bits[0] = oparams->masks[i];
3914 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3915 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3916 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3917 params->info = oparams->info;
3918 params->msbits = oparams->msbits;
3919 params->rate_num = oparams->rate_num;
3920 params->rate_den = oparams->rate_den;
3921 params->fifo_size = oparams->fifo_size;
3924 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3925 struct snd_pcm_hw_params *params)
3929 memset(oparams, 0, sizeof(*oparams));
3930 oparams->flags = params->flags;
3931 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3932 oparams->masks[i] = params->masks[i].bits[0];
3933 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3934 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3935 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3936 oparams->info = params->info;
3937 oparams->msbits = params->msbits;
3938 oparams->rate_num = params->rate_num;
3939 oparams->rate_den = params->rate_den;
3940 oparams->fifo_size = params->fifo_size;
3943 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3944 struct snd_pcm_hw_params_old __user * _oparams)
3946 struct snd_pcm_hw_params *params;
3947 struct snd_pcm_hw_params_old *oparams = NULL;
3950 params = kmalloc(sizeof(*params), GFP_KERNEL);
3954 oparams = memdup_user(_oparams, sizeof(*oparams));
3955 if (IS_ERR(oparams)) {
3956 err = PTR_ERR(oparams);
3959 snd_pcm_hw_convert_from_old_params(params, oparams);
3960 err = snd_pcm_hw_refine(substream, params);
3964 err = fixup_unreferenced_params(substream, params);
3968 snd_pcm_hw_convert_to_old_params(oparams, params);
3969 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3978 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3979 struct snd_pcm_hw_params_old __user * _oparams)
3981 struct snd_pcm_hw_params *params;
3982 struct snd_pcm_hw_params_old *oparams = NULL;
3985 params = kmalloc(sizeof(*params), GFP_KERNEL);
3989 oparams = memdup_user(_oparams, sizeof(*oparams));
3990 if (IS_ERR(oparams)) {
3991 err = PTR_ERR(oparams);
3995 snd_pcm_hw_convert_from_old_params(params, oparams);
3996 err = snd_pcm_hw_params(substream, params);
4000 snd_pcm_hw_convert_to_old_params(oparams, params);
4001 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
4009 #endif /* CONFIG_SND_SUPPORT_OLD_API */
4012 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
4015 unsigned long pgoff,
4016 unsigned long flags)
4018 struct snd_pcm_file *pcm_file = file->private_data;
4019 struct snd_pcm_substream *substream = pcm_file->substream;
4020 struct snd_pcm_runtime *runtime = substream->runtime;
4021 unsigned long offset = pgoff << PAGE_SHIFT;
4024 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
4025 return (unsigned long)runtime->status;
4026 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
4027 return (unsigned long)runtime->control;
4029 return (unsigned long)runtime->dma_area + offset;
4033 # define snd_pcm_get_unmapped_area NULL
4040 const struct file_operations snd_pcm_f_ops[2] = {
4042 .owner = THIS_MODULE,
4043 .write = snd_pcm_write,
4044 .write_iter = snd_pcm_writev,
4045 .open = snd_pcm_playback_open,
4046 .release = snd_pcm_release,
4047 .llseek = no_llseek,
4048 .poll = snd_pcm_poll,
4049 .unlocked_ioctl = snd_pcm_ioctl,
4050 .compat_ioctl = snd_pcm_ioctl_compat,
4051 .mmap = snd_pcm_mmap,
4052 .fasync = snd_pcm_fasync,
4053 .get_unmapped_area = snd_pcm_get_unmapped_area,
4056 .owner = THIS_MODULE,
4057 .read = snd_pcm_read,
4058 .read_iter = snd_pcm_readv,
4059 .open = snd_pcm_capture_open,
4060 .release = snd_pcm_release,
4061 .llseek = no_llseek,
4062 .poll = snd_pcm_poll,
4063 .unlocked_ioctl = snd_pcm_ioctl,
4064 .compat_ioctl = snd_pcm_ioctl_compat,
4065 .mmap = snd_pcm_mmap,
4066 .fasync = snd_pcm_fasync,
4067 .get_unmapped_area = snd_pcm_get_unmapped_area,