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);
175 unsigned long _snd_pcm_stream_lock_irqsave_nested(struct snd_pcm_substream *substream)
177 unsigned long flags = 0;
178 if (substream->pcm->nonatomic)
179 mutex_lock_nested(&substream->self_group.mutex,
180 SINGLE_DEPTH_NESTING);
182 spin_lock_irqsave_nested(&substream->self_group.lock, flags,
183 SINGLE_DEPTH_NESTING);
186 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave_nested);
189 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
190 * @substream: PCM substream
193 * This is a counter-part of snd_pcm_stream_lock_irqsave().
195 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
198 if (substream->pcm->nonatomic)
199 mutex_unlock(&substream->self_group.mutex);
201 spin_unlock_irqrestore(&substream->self_group.lock, flags);
203 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
205 /* Run PCM ioctl ops */
206 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
207 unsigned cmd, void *arg)
209 if (substream->ops->ioctl)
210 return substream->ops->ioctl(substream, cmd, arg);
212 return snd_pcm_lib_ioctl(substream, cmd, arg);
215 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
217 struct snd_pcm *pcm = substream->pcm;
218 struct snd_pcm_str *pstr = substream->pstr;
220 memset(info, 0, sizeof(*info));
221 info->card = pcm->card->number;
222 info->device = pcm->device;
223 info->stream = substream->stream;
224 info->subdevice = substream->number;
225 strscpy(info->id, pcm->id, sizeof(info->id));
226 strscpy(info->name, pcm->name, sizeof(info->name));
227 info->dev_class = pcm->dev_class;
228 info->dev_subclass = pcm->dev_subclass;
229 info->subdevices_count = pstr->substream_count;
230 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
231 strscpy(info->subname, substream->name, sizeof(info->subname));
236 int snd_pcm_info_user(struct snd_pcm_substream *substream,
237 struct snd_pcm_info __user * _info)
239 struct snd_pcm_info *info;
242 info = kmalloc(sizeof(*info), GFP_KERNEL);
245 err = snd_pcm_info(substream, info);
247 if (copy_to_user(_info, info, sizeof(*info)))
254 /* macro for simplified cast */
255 #define PARAM_MASK_BIT(b) (1U << (__force int)(b))
257 static bool hw_support_mmap(struct snd_pcm_substream *substream)
259 struct snd_dma_buffer *dmabuf;
261 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
264 if (substream->ops->mmap || substream->ops->page)
267 dmabuf = snd_pcm_get_dma_buf(substream);
269 dmabuf = &substream->dma_buffer;
270 switch (dmabuf->dev.type) {
271 case SNDRV_DMA_TYPE_UNKNOWN:
272 /* we can't know the device, so just assume that the driver does
276 case SNDRV_DMA_TYPE_CONTINUOUS:
277 case SNDRV_DMA_TYPE_VMALLOC:
280 return dma_can_mmap(dmabuf->dev.dev);
284 static int constrain_mask_params(struct snd_pcm_substream *substream,
285 struct snd_pcm_hw_params *params)
287 struct snd_pcm_hw_constraints *constrs =
288 &substream->runtime->hw_constraints;
291 struct snd_mask old_mask;
294 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
295 m = hw_param_mask(params, k);
296 if (snd_mask_empty(m))
299 /* This parameter is not requested to change by a caller. */
300 if (!(params->rmask & PARAM_MASK_BIT(k)))
303 if (trace_hw_mask_param_enabled())
306 changed = snd_mask_refine(m, constrs_mask(constrs, k));
312 /* Set corresponding flag so that the caller gets it. */
313 trace_hw_mask_param(substream, k, 0, &old_mask, m);
314 params->cmask |= PARAM_MASK_BIT(k);
320 static int constrain_interval_params(struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params *params)
323 struct snd_pcm_hw_constraints *constrs =
324 &substream->runtime->hw_constraints;
325 struct snd_interval *i;
327 struct snd_interval old_interval;
330 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
331 i = hw_param_interval(params, k);
332 if (snd_interval_empty(i))
335 /* This parameter is not requested to change by a caller. */
336 if (!(params->rmask & PARAM_MASK_BIT(k)))
339 if (trace_hw_interval_param_enabled())
342 changed = snd_interval_refine(i, constrs_interval(constrs, k));
348 /* Set corresponding flag so that the caller gets it. */
349 trace_hw_interval_param(substream, k, 0, &old_interval, i);
350 params->cmask |= PARAM_MASK_BIT(k);
356 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
357 struct snd_pcm_hw_params *params)
359 struct snd_pcm_hw_constraints *constrs =
360 &substream->runtime->hw_constraints;
362 unsigned int *rstamps;
363 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
365 struct snd_pcm_hw_rule *r;
367 struct snd_mask old_mask;
368 struct snd_interval old_interval;
370 int changed, err = 0;
373 * Each application of rule has own sequence number.
375 * Each member of 'rstamps' array represents the sequence number of
376 * recent application of corresponding rule.
378 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
383 * Each member of 'vstamps' array represents the sequence number of
384 * recent application of rule in which corresponding parameters were
387 * In initial state, elements corresponding to parameters requested by
388 * a caller is 1. For unrequested parameters, corresponding members
389 * have 0 so that the parameters are never changed anymore.
391 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
392 vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
394 /* Due to the above design, actual sequence number starts at 2. */
397 /* Apply all rules in order. */
399 for (k = 0; k < constrs->rules_num; k++) {
400 r = &constrs->rules[k];
403 * Check condition bits of this rule. When the rule has
404 * some condition bits, parameter without the bits is
405 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
406 * is an example of the condition bits.
408 if (r->cond && !(r->cond & params->flags))
412 * The 'deps' array includes maximum four dependencies
413 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth
414 * member of this array is a sentinel and should be
417 * This rule should be processed in this time when dependent
418 * parameters were changed at former applications of the other
421 for (d = 0; r->deps[d] >= 0; d++) {
422 if (vstamps[r->deps[d]] > rstamps[k])
428 if (trace_hw_mask_param_enabled()) {
429 if (hw_is_mask(r->var))
430 old_mask = *hw_param_mask(params, r->var);
432 if (trace_hw_interval_param_enabled()) {
433 if (hw_is_interval(r->var))
434 old_interval = *hw_param_interval(params, r->var);
437 changed = r->func(params, r);
444 * When the parameter is changed, notify it to the caller
445 * by corresponding returned bit, then preparing for next
448 if (changed && r->var >= 0) {
449 if (hw_is_mask(r->var)) {
450 trace_hw_mask_param(substream, r->var,
452 hw_param_mask(params, r->var));
454 if (hw_is_interval(r->var)) {
455 trace_hw_interval_param(substream, r->var,
456 k + 1, &old_interval,
457 hw_param_interval(params, r->var));
460 params->cmask |= PARAM_MASK_BIT(r->var);
461 vstamps[r->var] = stamp;
465 rstamps[k] = stamp++;
468 /* Iterate to evaluate all rules till no parameters are changed. */
477 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
478 struct snd_pcm_hw_params *params)
480 const struct snd_interval *i;
481 const struct snd_mask *m;
484 if (!params->msbits) {
485 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
486 if (snd_interval_single(i))
487 params->msbits = snd_interval_value(i);
490 if (!params->rate_den) {
491 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
492 if (snd_interval_single(i)) {
493 params->rate_num = snd_interval_value(i);
494 params->rate_den = 1;
498 if (!params->fifo_size) {
499 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
500 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
501 if (snd_mask_single(m) && snd_interval_single(i)) {
502 err = snd_pcm_ops_ioctl(substream,
503 SNDRV_PCM_IOCTL1_FIFO_SIZE,
511 params->info = substream->runtime->hw.info;
512 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
513 SNDRV_PCM_INFO_DRAIN_TRIGGER);
514 if (!hw_support_mmap(substream))
515 params->info &= ~(SNDRV_PCM_INFO_MMAP |
516 SNDRV_PCM_INFO_MMAP_VALID);
522 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
523 struct snd_pcm_hw_params *params)
528 params->fifo_size = 0;
529 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
531 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
532 params->rate_num = 0;
533 params->rate_den = 0;
536 err = constrain_mask_params(substream, params);
540 err = constrain_interval_params(substream, params);
544 err = constrain_params_by_rules(substream, params);
552 EXPORT_SYMBOL(snd_pcm_hw_refine);
554 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
555 struct snd_pcm_hw_params __user * _params)
557 struct snd_pcm_hw_params *params;
560 params = memdup_user(_params, sizeof(*params));
562 return PTR_ERR(params);
564 err = snd_pcm_hw_refine(substream, params);
568 err = fixup_unreferenced_params(substream, params);
572 if (copy_to_user(_params, params, sizeof(*params)))
579 static int period_to_usecs(struct snd_pcm_runtime *runtime)
584 return -1; /* invalid */
586 /* take 75% of period time as the deadline */
587 usecs = (750000 / runtime->rate) * runtime->period_size;
588 usecs += ((750000 % runtime->rate) * runtime->period_size) /
594 static void snd_pcm_set_state(struct snd_pcm_substream *substream,
595 snd_pcm_state_t state)
597 snd_pcm_stream_lock_irq(substream);
598 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
599 substream->runtime->status->state = state;
600 snd_pcm_stream_unlock_irq(substream);
603 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
606 #ifdef CONFIG_SND_PCM_TIMER
607 if (substream->timer)
608 snd_timer_notify(substream->timer, event,
609 &substream->runtime->trigger_tstamp);
613 void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq)
615 if (substream->runtime && substream->runtime->stop_operating) {
616 substream->runtime->stop_operating = false;
617 if (substream->ops && substream->ops->sync_stop)
618 substream->ops->sync_stop(substream);
619 else if (sync_irq && substream->pcm->card->sync_irq > 0)
620 synchronize_irq(substream->pcm->card->sync_irq);
625 * snd_pcm_hw_params_choose - choose a configuration defined by @params
627 * @params: the hw_params instance
629 * Choose one configuration from configuration space defined by @params.
630 * The configuration chosen is that obtained fixing in this order:
631 * first access, first format, first subformat, min channels,
632 * min rate, min period time, max buffer size, min tick time
634 * Return: Zero if successful, or a negative error code on failure.
636 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
637 struct snd_pcm_hw_params *params)
639 static const int vars[] = {
640 SNDRV_PCM_HW_PARAM_ACCESS,
641 SNDRV_PCM_HW_PARAM_FORMAT,
642 SNDRV_PCM_HW_PARAM_SUBFORMAT,
643 SNDRV_PCM_HW_PARAM_CHANNELS,
644 SNDRV_PCM_HW_PARAM_RATE,
645 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
646 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
647 SNDRV_PCM_HW_PARAM_TICK_TIME,
651 struct snd_mask old_mask;
652 struct snd_interval old_interval;
655 for (v = vars; *v != -1; v++) {
656 /* Keep old parameter to trace. */
657 if (trace_hw_mask_param_enabled()) {
659 old_mask = *hw_param_mask(params, *v);
661 if (trace_hw_interval_param_enabled()) {
662 if (hw_is_interval(*v))
663 old_interval = *hw_param_interval(params, *v);
665 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
666 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
668 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
674 /* Trace the changed parameter. */
675 if (hw_is_mask(*v)) {
676 trace_hw_mask_param(pcm, *v, 0, &old_mask,
677 hw_param_mask(params, *v));
679 if (hw_is_interval(*v)) {
680 trace_hw_interval_param(pcm, *v, 0, &old_interval,
681 hw_param_interval(params, *v));
688 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
689 struct snd_pcm_hw_params *params)
691 struct snd_pcm_runtime *runtime;
694 snd_pcm_uframes_t frames;
696 if (PCM_RUNTIME_CHECK(substream))
698 runtime = substream->runtime;
699 snd_pcm_stream_lock_irq(substream);
700 switch (runtime->status->state) {
701 case SNDRV_PCM_STATE_OPEN:
702 case SNDRV_PCM_STATE_SETUP:
703 case SNDRV_PCM_STATE_PREPARED:
706 snd_pcm_stream_unlock_irq(substream);
709 snd_pcm_stream_unlock_irq(substream);
710 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
711 if (!substream->oss.oss)
713 if (atomic_read(&substream->mmap_count))
716 snd_pcm_sync_stop(substream, true);
719 err = snd_pcm_hw_refine(substream, params);
723 err = snd_pcm_hw_params_choose(substream, params);
727 err = fixup_unreferenced_params(substream, params);
731 if (substream->managed_buffer_alloc) {
732 err = snd_pcm_lib_malloc_pages(substream,
733 params_buffer_bytes(params));
736 runtime->buffer_changed = err > 0;
739 if (substream->ops->hw_params != NULL) {
740 err = substream->ops->hw_params(substream, params);
745 runtime->access = params_access(params);
746 runtime->format = params_format(params);
747 runtime->subformat = params_subformat(params);
748 runtime->channels = params_channels(params);
749 runtime->rate = params_rate(params);
750 runtime->period_size = params_period_size(params);
751 runtime->periods = params_periods(params);
752 runtime->buffer_size = params_buffer_size(params);
753 runtime->info = params->info;
754 runtime->rate_num = params->rate_num;
755 runtime->rate_den = params->rate_den;
756 runtime->no_period_wakeup =
757 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
758 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
760 bits = snd_pcm_format_physical_width(runtime->format);
761 runtime->sample_bits = bits;
762 bits *= runtime->channels;
763 runtime->frame_bits = bits;
765 while (bits % 8 != 0) {
769 runtime->byte_align = bits / 8;
770 runtime->min_align = frames;
772 /* Default sw params */
773 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
774 runtime->period_step = 1;
775 runtime->control->avail_min = runtime->period_size;
776 runtime->start_threshold = 1;
777 runtime->stop_threshold = runtime->buffer_size;
778 runtime->silence_threshold = 0;
779 runtime->silence_size = 0;
780 runtime->boundary = runtime->buffer_size;
781 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
782 runtime->boundary *= 2;
784 /* clear the buffer for avoiding possible kernel info leaks */
785 if (runtime->dma_area && !substream->ops->copy_user) {
786 size_t size = runtime->dma_bytes;
788 if (runtime->info & SNDRV_PCM_INFO_MMAP)
789 size = PAGE_ALIGN(size);
790 memset(runtime->dma_area, 0, size);
793 snd_pcm_timer_resolution_change(substream);
794 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
796 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
797 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
798 usecs = period_to_usecs(runtime);
800 cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
804 /* hardware might be unusable from this time,
805 so we force application to retry to set
806 the correct hardware parameter settings */
807 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
808 if (substream->ops->hw_free != NULL)
809 substream->ops->hw_free(substream);
810 if (substream->managed_buffer_alloc)
811 snd_pcm_lib_free_pages(substream);
815 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
816 struct snd_pcm_hw_params __user * _params)
818 struct snd_pcm_hw_params *params;
821 params = memdup_user(_params, sizeof(*params));
823 return PTR_ERR(params);
825 err = snd_pcm_hw_params(substream, params);
829 if (copy_to_user(_params, params, sizeof(*params)))
836 static int do_hw_free(struct snd_pcm_substream *substream)
840 snd_pcm_sync_stop(substream, true);
841 if (substream->ops->hw_free)
842 result = substream->ops->hw_free(substream);
843 if (substream->managed_buffer_alloc)
844 snd_pcm_lib_free_pages(substream);
848 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
850 struct snd_pcm_runtime *runtime;
853 if (PCM_RUNTIME_CHECK(substream))
855 runtime = substream->runtime;
856 snd_pcm_stream_lock_irq(substream);
857 switch (runtime->status->state) {
858 case SNDRV_PCM_STATE_SETUP:
859 case SNDRV_PCM_STATE_PREPARED:
862 snd_pcm_stream_unlock_irq(substream);
865 snd_pcm_stream_unlock_irq(substream);
866 if (atomic_read(&substream->mmap_count))
868 result = do_hw_free(substream);
869 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
870 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
874 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
875 struct snd_pcm_sw_params *params)
877 struct snd_pcm_runtime *runtime;
880 if (PCM_RUNTIME_CHECK(substream))
882 runtime = substream->runtime;
883 snd_pcm_stream_lock_irq(substream);
884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
885 snd_pcm_stream_unlock_irq(substream);
888 snd_pcm_stream_unlock_irq(substream);
890 if (params->tstamp_mode < 0 ||
891 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
893 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
894 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
896 if (params->avail_min == 0)
898 if (params->silence_size >= runtime->boundary) {
899 if (params->silence_threshold != 0)
902 if (params->silence_size > params->silence_threshold)
904 if (params->silence_threshold > runtime->buffer_size)
908 snd_pcm_stream_lock_irq(substream);
909 runtime->tstamp_mode = params->tstamp_mode;
910 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
911 runtime->tstamp_type = params->tstamp_type;
912 runtime->period_step = params->period_step;
913 runtime->control->avail_min = params->avail_min;
914 runtime->start_threshold = params->start_threshold;
915 runtime->stop_threshold = params->stop_threshold;
916 runtime->silence_threshold = params->silence_threshold;
917 runtime->silence_size = params->silence_size;
918 params->boundary = runtime->boundary;
919 if (snd_pcm_running(substream)) {
920 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
921 runtime->silence_size > 0)
922 snd_pcm_playback_silence(substream, ULONG_MAX);
923 err = snd_pcm_update_state(substream, runtime);
925 snd_pcm_stream_unlock_irq(substream);
929 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
930 struct snd_pcm_sw_params __user * _params)
932 struct snd_pcm_sw_params params;
934 if (copy_from_user(¶ms, _params, sizeof(params)))
936 err = snd_pcm_sw_params(substream, ¶ms);
937 if (copy_to_user(_params, ¶ms, sizeof(params)))
942 static inline snd_pcm_uframes_t
943 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
945 snd_pcm_uframes_t delay;
947 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
948 delay = snd_pcm_playback_hw_avail(substream->runtime);
950 delay = snd_pcm_capture_avail(substream->runtime);
951 return delay + substream->runtime->delay;
954 int snd_pcm_status64(struct snd_pcm_substream *substream,
955 struct snd_pcm_status64 *status)
957 struct snd_pcm_runtime *runtime = substream->runtime;
959 snd_pcm_stream_lock_irq(substream);
961 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
962 &runtime->audio_tstamp_config);
964 /* backwards compatible behavior */
965 if (runtime->audio_tstamp_config.type_requested ==
966 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
967 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
968 runtime->audio_tstamp_config.type_requested =
969 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
971 runtime->audio_tstamp_config.type_requested =
972 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
973 runtime->audio_tstamp_report.valid = 0;
975 runtime->audio_tstamp_report.valid = 1;
977 status->state = runtime->status->state;
978 status->suspended_state = runtime->status->suspended_state;
979 if (status->state == SNDRV_PCM_STATE_OPEN)
981 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
982 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
983 if (snd_pcm_running(substream)) {
984 snd_pcm_update_hw_ptr(substream);
985 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
986 status->tstamp_sec = runtime->status->tstamp.tv_sec;
987 status->tstamp_nsec =
988 runtime->status->tstamp.tv_nsec;
989 status->driver_tstamp_sec =
990 runtime->driver_tstamp.tv_sec;
991 status->driver_tstamp_nsec =
992 runtime->driver_tstamp.tv_nsec;
993 status->audio_tstamp_sec =
994 runtime->status->audio_tstamp.tv_sec;
995 status->audio_tstamp_nsec =
996 runtime->status->audio_tstamp.tv_nsec;
997 if (runtime->audio_tstamp_report.valid == 1)
998 /* backwards compatibility, no report provided in COMPAT mode */
999 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
1000 &status->audio_tstamp_accuracy,
1001 &runtime->audio_tstamp_report);
1006 /* get tstamp only in fallback mode and only if enabled */
1007 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
1008 struct timespec64 tstamp;
1010 snd_pcm_gettime(runtime, &tstamp);
1011 status->tstamp_sec = tstamp.tv_sec;
1012 status->tstamp_nsec = tstamp.tv_nsec;
1016 status->appl_ptr = runtime->control->appl_ptr;
1017 status->hw_ptr = runtime->status->hw_ptr;
1018 status->avail = snd_pcm_avail(substream);
1019 status->delay = snd_pcm_running(substream) ?
1020 snd_pcm_calc_delay(substream) : 0;
1021 status->avail_max = runtime->avail_max;
1022 status->overrange = runtime->overrange;
1023 runtime->avail_max = 0;
1024 runtime->overrange = 0;
1026 snd_pcm_stream_unlock_irq(substream);
1030 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
1031 struct snd_pcm_status64 __user * _status,
1034 struct snd_pcm_status64 status;
1037 memset(&status, 0, sizeof(status));
1039 * with extension, parameters are read/write,
1040 * get audio_tstamp_data from user,
1041 * ignore rest of status structure
1043 if (ext && get_user(status.audio_tstamp_data,
1044 (u32 __user *)(&_status->audio_tstamp_data)))
1046 res = snd_pcm_status64(substream, &status);
1049 if (copy_to_user(_status, &status, sizeof(status)))
1054 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1055 struct snd_pcm_status32 __user * _status,
1058 struct snd_pcm_status64 status64;
1059 struct snd_pcm_status32 status32;
1062 memset(&status64, 0, sizeof(status64));
1063 memset(&status32, 0, sizeof(status32));
1065 * with extension, parameters are read/write,
1066 * get audio_tstamp_data from user,
1067 * ignore rest of status structure
1069 if (ext && get_user(status64.audio_tstamp_data,
1070 (u32 __user *)(&_status->audio_tstamp_data)))
1072 res = snd_pcm_status64(substream, &status64);
1076 status32 = (struct snd_pcm_status32) {
1077 .state = status64.state,
1078 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1079 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1080 .tstamp_sec = status64.tstamp_sec,
1081 .tstamp_nsec = status64.tstamp_nsec,
1082 .appl_ptr = status64.appl_ptr,
1083 .hw_ptr = status64.hw_ptr,
1084 .delay = status64.delay,
1085 .avail = status64.avail,
1086 .avail_max = status64.avail_max,
1087 .overrange = status64.overrange,
1088 .suspended_state = status64.suspended_state,
1089 .audio_tstamp_data = status64.audio_tstamp_data,
1090 .audio_tstamp_sec = status64.audio_tstamp_sec,
1091 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1092 .driver_tstamp_sec = status64.audio_tstamp_sec,
1093 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1094 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1097 if (copy_to_user(_status, &status32, sizeof(status32)))
1103 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1104 struct snd_pcm_channel_info * info)
1106 struct snd_pcm_runtime *runtime;
1107 unsigned int channel;
1109 channel = info->channel;
1110 runtime = substream->runtime;
1111 snd_pcm_stream_lock_irq(substream);
1112 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1113 snd_pcm_stream_unlock_irq(substream);
1116 snd_pcm_stream_unlock_irq(substream);
1117 if (channel >= runtime->channels)
1119 memset(info, 0, sizeof(*info));
1120 info->channel = channel;
1121 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1124 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1125 struct snd_pcm_channel_info __user * _info)
1127 struct snd_pcm_channel_info info;
1130 if (copy_from_user(&info, _info, sizeof(info)))
1132 res = snd_pcm_channel_info(substream, &info);
1135 if (copy_to_user(_info, &info, sizeof(info)))
1140 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1142 struct snd_pcm_runtime *runtime = substream->runtime;
1143 if (runtime->trigger_master == NULL)
1145 if (runtime->trigger_master == substream) {
1146 if (!runtime->trigger_tstamp_latched)
1147 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1149 snd_pcm_trigger_tstamp(runtime->trigger_master);
1150 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1152 runtime->trigger_master = NULL;
1155 #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1158 int (*pre_action)(struct snd_pcm_substream *substream,
1159 snd_pcm_state_t state);
1160 int (*do_action)(struct snd_pcm_substream *substream,
1161 snd_pcm_state_t state);
1162 void (*undo_action)(struct snd_pcm_substream *substream,
1163 snd_pcm_state_t state);
1164 void (*post_action)(struct snd_pcm_substream *substream,
1165 snd_pcm_state_t state);
1169 * this functions is core for handling of linked stream
1170 * Note: the stream state might be changed also on failure
1171 * Note2: call with calling stream lock + link lock
1173 static int snd_pcm_action_group(const struct action_ops *ops,
1174 struct snd_pcm_substream *substream,
1175 snd_pcm_state_t state,
1178 struct snd_pcm_substream *s = NULL;
1179 struct snd_pcm_substream *s1;
1180 int res = 0, depth = 1;
1182 snd_pcm_group_for_each_entry(s, substream) {
1183 if (do_lock && s != substream) {
1184 if (s->pcm->nonatomic)
1185 mutex_lock_nested(&s->self_group.mutex, depth);
1187 spin_lock_nested(&s->self_group.lock, depth);
1190 res = ops->pre_action(s, state);
1194 snd_pcm_group_for_each_entry(s, substream) {
1195 res = ops->do_action(s, state);
1197 if (ops->undo_action) {
1198 snd_pcm_group_for_each_entry(s1, substream) {
1199 if (s1 == s) /* failed stream */
1201 ops->undo_action(s1, state);
1204 s = NULL; /* unlock all */
1208 snd_pcm_group_for_each_entry(s, substream) {
1209 ops->post_action(s, state);
1213 /* unlock streams */
1214 snd_pcm_group_for_each_entry(s1, substream) {
1215 if (s1 != substream) {
1216 if (s1->pcm->nonatomic)
1217 mutex_unlock(&s1->self_group.mutex);
1219 spin_unlock(&s1->self_group.lock);
1221 if (s1 == s) /* end */
1229 * Note: call with stream lock
1231 static int snd_pcm_action_single(const struct action_ops *ops,
1232 struct snd_pcm_substream *substream,
1233 snd_pcm_state_t state)
1237 res = ops->pre_action(substream, state);
1240 res = ops->do_action(substream, state);
1242 ops->post_action(substream, state);
1243 else if (ops->undo_action)
1244 ops->undo_action(substream, state);
1248 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1249 struct snd_pcm_group *new_group)
1251 substream->group = new_group;
1252 list_move(&substream->link_list, &new_group->substreams);
1256 * Unref and unlock the group, but keep the stream lock;
1257 * when the group becomes empty and no longer referred, destroy itself
1259 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1260 struct snd_pcm_substream *substream)
1266 do_free = refcount_dec_and_test(&group->refs);
1267 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1273 * Lock the group inside a stream lock and reference it;
1274 * return the locked group object, or NULL if not linked
1276 static struct snd_pcm_group *
1277 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1279 bool nonatomic = substream->pcm->nonatomic;
1280 struct snd_pcm_group *group;
1284 if (!snd_pcm_stream_linked(substream))
1286 group = substream->group;
1287 /* block freeing the group object */
1288 refcount_inc(&group->refs);
1290 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1291 spin_trylock(&group->lock);
1295 /* re-lock for avoiding ABBA deadlock */
1296 snd_pcm_stream_unlock(substream);
1297 snd_pcm_group_lock(group, nonatomic);
1298 snd_pcm_stream_lock(substream);
1300 /* check the group again; the above opens a small race window */
1301 if (substream->group == group)
1303 /* group changed, try again */
1304 snd_pcm_group_unref(group, substream);
1310 * Note: call with stream lock
1312 static int snd_pcm_action(const struct action_ops *ops,
1313 struct snd_pcm_substream *substream,
1314 snd_pcm_state_t state)
1316 struct snd_pcm_group *group;
1319 group = snd_pcm_stream_group_ref(substream);
1321 res = snd_pcm_action_group(ops, substream, state, true);
1323 res = snd_pcm_action_single(ops, substream, state);
1324 snd_pcm_group_unref(group, substream);
1329 * Note: don't use any locks before
1331 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1332 struct snd_pcm_substream *substream,
1333 snd_pcm_state_t state)
1337 snd_pcm_stream_lock_irq(substream);
1338 res = snd_pcm_action(ops, substream, state);
1339 snd_pcm_stream_unlock_irq(substream);
1345 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1346 struct snd_pcm_substream *substream,
1347 snd_pcm_state_t state)
1351 /* Guarantee the group members won't change during non-atomic action */
1352 down_read(&snd_pcm_link_rwsem);
1353 if (snd_pcm_stream_linked(substream))
1354 res = snd_pcm_action_group(ops, substream, state, false);
1356 res = snd_pcm_action_single(ops, substream, state);
1357 up_read(&snd_pcm_link_rwsem);
1364 static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1365 snd_pcm_state_t state)
1367 struct snd_pcm_runtime *runtime = substream->runtime;
1368 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1371 !snd_pcm_playback_data(substream))
1373 runtime->trigger_tstamp_latched = false;
1374 runtime->trigger_master = substream;
1378 static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1379 snd_pcm_state_t state)
1381 if (substream->runtime->trigger_master != substream)
1383 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1386 static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1387 snd_pcm_state_t state)
1389 if (substream->runtime->trigger_master == substream)
1390 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1393 static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1394 snd_pcm_state_t state)
1396 struct snd_pcm_runtime *runtime = substream->runtime;
1397 snd_pcm_trigger_tstamp(substream);
1398 runtime->hw_ptr_jiffies = jiffies;
1399 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1401 runtime->status->state = state;
1402 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1403 runtime->silence_size > 0)
1404 snd_pcm_playback_silence(substream, ULONG_MAX);
1405 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1408 static const struct action_ops snd_pcm_action_start = {
1409 .pre_action = snd_pcm_pre_start,
1410 .do_action = snd_pcm_do_start,
1411 .undo_action = snd_pcm_undo_start,
1412 .post_action = snd_pcm_post_start
1416 * snd_pcm_start - start all linked streams
1417 * @substream: the PCM substream instance
1419 * Return: Zero if successful, or a negative error code.
1420 * The stream lock must be acquired before calling this function.
1422 int snd_pcm_start(struct snd_pcm_substream *substream)
1424 return snd_pcm_action(&snd_pcm_action_start, substream,
1425 SNDRV_PCM_STATE_RUNNING);
1428 /* take the stream lock and start the streams */
1429 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1431 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1432 SNDRV_PCM_STATE_RUNNING);
1438 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1439 snd_pcm_state_t state)
1441 struct snd_pcm_runtime *runtime = substream->runtime;
1442 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1444 runtime->trigger_master = substream;
1448 static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1449 snd_pcm_state_t state)
1451 if (substream->runtime->trigger_master == substream &&
1452 snd_pcm_running(substream)) {
1453 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1454 substream->runtime->stop_operating = true;
1456 return 0; /* unconditionally stop all substreams */
1459 static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1460 snd_pcm_state_t state)
1462 struct snd_pcm_runtime *runtime = substream->runtime;
1463 if (runtime->status->state != state) {
1464 snd_pcm_trigger_tstamp(substream);
1465 runtime->status->state = state;
1466 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1468 wake_up(&runtime->sleep);
1469 wake_up(&runtime->tsleep);
1472 static const struct action_ops snd_pcm_action_stop = {
1473 .pre_action = snd_pcm_pre_stop,
1474 .do_action = snd_pcm_do_stop,
1475 .post_action = snd_pcm_post_stop
1479 * snd_pcm_stop - try to stop all running streams in the substream group
1480 * @substream: the PCM substream instance
1481 * @state: PCM state after stopping the stream
1483 * The state of each stream is then changed to the given state unconditionally.
1485 * Return: Zero if successful, or a negative error code.
1487 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1489 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1491 EXPORT_SYMBOL(snd_pcm_stop);
1494 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1495 * @substream: the PCM substream
1497 * After stopping, the state is changed to SETUP.
1498 * Unlike snd_pcm_stop(), this affects only the given stream.
1500 * Return: Zero if successful, or a negative error code.
1502 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1504 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1505 SNDRV_PCM_STATE_SETUP);
1509 * snd_pcm_stop_xrun - stop the running streams as XRUN
1510 * @substream: the PCM substream instance
1512 * This stops the given running substream (and all linked substreams) as XRUN.
1513 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1515 * Return: Zero if successful, or a negative error code.
1517 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1519 unsigned long flags;
1521 snd_pcm_stream_lock_irqsave(substream, flags);
1522 if (substream->runtime && snd_pcm_running(substream))
1523 __snd_pcm_xrun(substream);
1524 snd_pcm_stream_unlock_irqrestore(substream, flags);
1527 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1530 * pause callbacks: pass boolean (to start pause or resume) as state argument
1532 #define pause_pushed(state) (__force bool)(state)
1534 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1535 snd_pcm_state_t state)
1537 struct snd_pcm_runtime *runtime = substream->runtime;
1538 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1540 if (pause_pushed(state)) {
1541 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1543 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1545 runtime->trigger_master = substream;
1549 static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1550 snd_pcm_state_t state)
1552 if (substream->runtime->trigger_master != substream)
1554 /* some drivers might use hw_ptr to recover from the pause -
1555 update the hw_ptr now */
1556 if (pause_pushed(state))
1557 snd_pcm_update_hw_ptr(substream);
1558 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1559 * a delta between the current jiffies, this gives a large enough
1560 * delta, effectively to skip the check once.
1562 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1563 return substream->ops->trigger(substream,
1564 pause_pushed(state) ?
1565 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1566 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1569 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1570 snd_pcm_state_t state)
1572 if (substream->runtime->trigger_master == substream)
1573 substream->ops->trigger(substream,
1574 pause_pushed(state) ?
1575 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1576 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1579 static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1580 snd_pcm_state_t state)
1582 struct snd_pcm_runtime *runtime = substream->runtime;
1583 snd_pcm_trigger_tstamp(substream);
1584 if (pause_pushed(state)) {
1585 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1586 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1587 wake_up(&runtime->sleep);
1588 wake_up(&runtime->tsleep);
1590 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1591 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1595 static const struct action_ops snd_pcm_action_pause = {
1596 .pre_action = snd_pcm_pre_pause,
1597 .do_action = snd_pcm_do_pause,
1598 .undo_action = snd_pcm_undo_pause,
1599 .post_action = snd_pcm_post_pause
1603 * Push/release the pause for all linked streams.
1605 static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1607 return snd_pcm_action(&snd_pcm_action_pause, substream,
1608 (__force snd_pcm_state_t)push);
1611 static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1614 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1615 (__force snd_pcm_state_t)push);
1619 /* suspend callback: state argument ignored */
1621 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1622 snd_pcm_state_t state)
1624 struct snd_pcm_runtime *runtime = substream->runtime;
1625 switch (runtime->status->state) {
1626 case SNDRV_PCM_STATE_SUSPENDED:
1628 /* unresumable PCM state; return -EBUSY for skipping suspend */
1629 case SNDRV_PCM_STATE_OPEN:
1630 case SNDRV_PCM_STATE_SETUP:
1631 case SNDRV_PCM_STATE_DISCONNECTED:
1634 runtime->trigger_master = substream;
1638 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1639 snd_pcm_state_t state)
1641 struct snd_pcm_runtime *runtime = substream->runtime;
1642 if (runtime->trigger_master != substream)
1644 if (! snd_pcm_running(substream))
1646 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1647 runtime->stop_operating = true;
1648 return 0; /* suspend unconditionally */
1651 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1652 snd_pcm_state_t state)
1654 struct snd_pcm_runtime *runtime = substream->runtime;
1655 snd_pcm_trigger_tstamp(substream);
1656 runtime->status->suspended_state = runtime->status->state;
1657 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1658 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1659 wake_up(&runtime->sleep);
1660 wake_up(&runtime->tsleep);
1663 static const struct action_ops snd_pcm_action_suspend = {
1664 .pre_action = snd_pcm_pre_suspend,
1665 .do_action = snd_pcm_do_suspend,
1666 .post_action = snd_pcm_post_suspend
1670 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1671 * @substream: the PCM substream
1673 * After this call, all streams are changed to SUSPENDED state.
1675 * Return: Zero if successful, or a negative error code.
1677 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1680 unsigned long flags;
1682 snd_pcm_stream_lock_irqsave(substream, flags);
1683 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1685 snd_pcm_stream_unlock_irqrestore(substream, flags);
1690 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1691 * @pcm: the PCM instance
1693 * After this call, all streams are changed to SUSPENDED state.
1695 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1697 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1699 struct snd_pcm_substream *substream;
1700 int stream, err = 0;
1705 for_each_pcm_substream(pcm, stream, substream) {
1706 /* FIXME: the open/close code should lock this as well */
1707 if (!substream->runtime)
1711 * Skip BE dai link PCM's that are internal and may
1712 * not have their substream ops set.
1714 if (!substream->ops)
1717 err = snd_pcm_suspend(substream);
1718 if (err < 0 && err != -EBUSY)
1722 for_each_pcm_substream(pcm, stream, substream)
1723 snd_pcm_sync_stop(substream, false);
1727 EXPORT_SYMBOL(snd_pcm_suspend_all);
1729 /* resume callbacks: state argument ignored */
1731 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1732 snd_pcm_state_t state)
1734 struct snd_pcm_runtime *runtime = substream->runtime;
1735 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1737 runtime->trigger_master = substream;
1741 static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1742 snd_pcm_state_t state)
1744 struct snd_pcm_runtime *runtime = substream->runtime;
1745 if (runtime->trigger_master != substream)
1747 /* DMA not running previously? */
1748 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1749 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1750 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1752 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1755 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1756 snd_pcm_state_t state)
1758 if (substream->runtime->trigger_master == substream &&
1759 snd_pcm_running(substream))
1760 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1763 static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1764 snd_pcm_state_t state)
1766 struct snd_pcm_runtime *runtime = substream->runtime;
1767 snd_pcm_trigger_tstamp(substream);
1768 runtime->status->state = runtime->status->suspended_state;
1769 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1772 static const struct action_ops snd_pcm_action_resume = {
1773 .pre_action = snd_pcm_pre_resume,
1774 .do_action = snd_pcm_do_resume,
1775 .undo_action = snd_pcm_undo_resume,
1776 .post_action = snd_pcm_post_resume
1779 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1781 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1787 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1792 #endif /* CONFIG_PM */
1797 * Change the RUNNING stream(s) to XRUN state.
1799 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1801 struct snd_pcm_runtime *runtime = substream->runtime;
1804 snd_pcm_stream_lock_irq(substream);
1805 switch (runtime->status->state) {
1806 case SNDRV_PCM_STATE_XRUN:
1807 result = 0; /* already there */
1809 case SNDRV_PCM_STATE_RUNNING:
1810 __snd_pcm_xrun(substream);
1816 snd_pcm_stream_unlock_irq(substream);
1823 /* reset callbacks: state argument ignored */
1824 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1825 snd_pcm_state_t state)
1827 struct snd_pcm_runtime *runtime = substream->runtime;
1828 switch (runtime->status->state) {
1829 case SNDRV_PCM_STATE_RUNNING:
1830 case SNDRV_PCM_STATE_PREPARED:
1831 case SNDRV_PCM_STATE_PAUSED:
1832 case SNDRV_PCM_STATE_SUSPENDED:
1839 static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1840 snd_pcm_state_t state)
1842 struct snd_pcm_runtime *runtime = substream->runtime;
1843 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1846 runtime->hw_ptr_base = 0;
1847 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1848 runtime->status->hw_ptr % runtime->period_size;
1849 runtime->silence_start = runtime->status->hw_ptr;
1850 runtime->silence_filled = 0;
1854 static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1855 snd_pcm_state_t state)
1857 struct snd_pcm_runtime *runtime = substream->runtime;
1858 runtime->control->appl_ptr = runtime->status->hw_ptr;
1859 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1860 runtime->silence_size > 0)
1861 snd_pcm_playback_silence(substream, ULONG_MAX);
1864 static const struct action_ops snd_pcm_action_reset = {
1865 .pre_action = snd_pcm_pre_reset,
1866 .do_action = snd_pcm_do_reset,
1867 .post_action = snd_pcm_post_reset
1870 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1872 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1879 /* pass f_flags as state argument */
1880 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1881 snd_pcm_state_t state)
1883 struct snd_pcm_runtime *runtime = substream->runtime;
1884 int f_flags = (__force int)state;
1886 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1887 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1889 if (snd_pcm_running(substream))
1891 substream->f_flags = f_flags;
1895 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1896 snd_pcm_state_t state)
1899 snd_pcm_sync_stop(substream, true);
1900 err = substream->ops->prepare(substream);
1903 return snd_pcm_do_reset(substream, state);
1906 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1907 snd_pcm_state_t state)
1909 struct snd_pcm_runtime *runtime = substream->runtime;
1910 runtime->control->appl_ptr = runtime->status->hw_ptr;
1911 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1914 static const struct action_ops snd_pcm_action_prepare = {
1915 .pre_action = snd_pcm_pre_prepare,
1916 .do_action = snd_pcm_do_prepare,
1917 .post_action = snd_pcm_post_prepare
1921 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1922 * @substream: the PCM substream instance
1923 * @file: file to refer f_flags
1925 * Return: Zero if successful, or a negative error code.
1927 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1933 f_flags = file->f_flags;
1935 f_flags = substream->f_flags;
1937 snd_pcm_stream_lock_irq(substream);
1938 switch (substream->runtime->status->state) {
1939 case SNDRV_PCM_STATE_PAUSED:
1940 snd_pcm_pause(substream, false);
1942 case SNDRV_PCM_STATE_SUSPENDED:
1943 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1946 snd_pcm_stream_unlock_irq(substream);
1948 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1950 (__force snd_pcm_state_t)f_flags);
1957 /* drain init callbacks: state argument ignored */
1958 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1959 snd_pcm_state_t state)
1961 struct snd_pcm_runtime *runtime = substream->runtime;
1962 switch (runtime->status->state) {
1963 case SNDRV_PCM_STATE_OPEN:
1964 case SNDRV_PCM_STATE_DISCONNECTED:
1965 case SNDRV_PCM_STATE_SUSPENDED:
1968 runtime->trigger_master = substream;
1972 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1973 snd_pcm_state_t state)
1975 struct snd_pcm_runtime *runtime = substream->runtime;
1976 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1977 switch (runtime->status->state) {
1978 case SNDRV_PCM_STATE_PREPARED:
1979 /* start playback stream if possible */
1980 if (! snd_pcm_playback_empty(substream)) {
1981 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1982 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1984 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1987 case SNDRV_PCM_STATE_RUNNING:
1988 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1990 case SNDRV_PCM_STATE_XRUN:
1991 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1997 /* stop running stream */
1998 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1999 snd_pcm_state_t new_state;
2001 new_state = snd_pcm_capture_avail(runtime) > 0 ?
2002 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
2003 snd_pcm_do_stop(substream, new_state);
2004 snd_pcm_post_stop(substream, new_state);
2008 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
2009 runtime->trigger_master == substream &&
2010 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
2011 return substream->ops->trigger(substream,
2012 SNDRV_PCM_TRIGGER_DRAIN);
2017 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
2018 snd_pcm_state_t state)
2022 static const struct action_ops snd_pcm_action_drain_init = {
2023 .pre_action = snd_pcm_pre_drain_init,
2024 .do_action = snd_pcm_do_drain_init,
2025 .post_action = snd_pcm_post_drain_init
2029 * Drain the stream(s).
2030 * When the substream is linked, sync until the draining of all playback streams
2032 * After this call, all streams are supposed to be either SETUP or DRAINING
2033 * (capture only) state.
2035 static int snd_pcm_drain(struct snd_pcm_substream *substream,
2038 struct snd_card *card;
2039 struct snd_pcm_runtime *runtime;
2040 struct snd_pcm_substream *s;
2041 struct snd_pcm_group *group;
2042 wait_queue_entry_t wait;
2046 card = substream->pcm->card;
2047 runtime = substream->runtime;
2049 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2053 if (file->f_flags & O_NONBLOCK)
2055 } else if (substream->f_flags & O_NONBLOCK)
2058 snd_pcm_stream_lock_irq(substream);
2060 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2061 snd_pcm_pause(substream, false);
2063 /* pre-start/stop - all running streams are changed to DRAINING state */
2064 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2068 /* in non-blocking, we don't wait in ioctl but let caller poll */
2076 struct snd_pcm_runtime *to_check;
2077 if (signal_pending(current)) {
2078 result = -ERESTARTSYS;
2081 /* find a substream to drain */
2083 group = snd_pcm_stream_group_ref(substream);
2084 snd_pcm_group_for_each_entry(s, substream) {
2085 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2087 runtime = s->runtime;
2088 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2093 snd_pcm_group_unref(group, substream);
2095 break; /* all drained */
2096 init_waitqueue_entry(&wait, current);
2097 set_current_state(TASK_INTERRUPTIBLE);
2098 add_wait_queue(&to_check->sleep, &wait);
2099 snd_pcm_stream_unlock_irq(substream);
2100 if (runtime->no_period_wakeup)
2101 tout = MAX_SCHEDULE_TIMEOUT;
2104 if (runtime->rate) {
2105 long t = runtime->period_size * 2 / runtime->rate;
2106 tout = max(t, tout);
2108 tout = msecs_to_jiffies(tout * 1000);
2110 tout = schedule_timeout(tout);
2112 snd_pcm_stream_lock_irq(substream);
2113 group = snd_pcm_stream_group_ref(substream);
2114 snd_pcm_group_for_each_entry(s, substream) {
2115 if (s->runtime == to_check) {
2116 remove_wait_queue(&to_check->sleep, &wait);
2120 snd_pcm_group_unref(group, substream);
2122 if (card->shutdown) {
2127 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2130 dev_dbg(substream->pcm->card->dev,
2131 "playback drain error (DMA or IRQ trouble?)\n");
2132 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2140 snd_pcm_stream_unlock_irq(substream);
2148 * Immediately put all linked substreams into SETUP state.
2150 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2152 struct snd_pcm_runtime *runtime;
2155 if (PCM_RUNTIME_CHECK(substream))
2157 runtime = substream->runtime;
2159 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2160 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2163 snd_pcm_stream_lock_irq(substream);
2165 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2166 snd_pcm_pause(substream, false);
2168 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2169 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2170 snd_pcm_stream_unlock_irq(substream);
2176 static bool is_pcm_file(struct file *file)
2178 struct inode *inode = file_inode(file);
2179 struct snd_pcm *pcm;
2182 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2184 minor = iminor(inode);
2185 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2187 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2190 snd_card_unref(pcm->card);
2197 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2200 struct snd_pcm_file *pcm_file;
2201 struct snd_pcm_substream *substream1;
2202 struct snd_pcm_group *group, *target_group;
2203 bool nonatomic = substream->pcm->nonatomic;
2204 struct fd f = fdget(fd);
2208 if (!is_pcm_file(f.file)) {
2212 pcm_file = f.file->private_data;
2213 substream1 = pcm_file->substream;
2215 if (substream == substream1) {
2220 group = kzalloc(sizeof(*group), GFP_KERNEL);
2225 snd_pcm_group_init(group);
2227 down_write(&snd_pcm_link_rwsem);
2228 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2229 substream->runtime->status->state != substream1->runtime->status->state ||
2230 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2234 if (snd_pcm_stream_linked(substream1)) {
2239 snd_pcm_stream_lock_irq(substream);
2240 if (!snd_pcm_stream_linked(substream)) {
2241 snd_pcm_group_assign(substream, group);
2242 group = NULL; /* assigned, don't free this one below */
2244 target_group = substream->group;
2245 snd_pcm_stream_unlock_irq(substream);
2247 snd_pcm_group_lock_irq(target_group, nonatomic);
2248 snd_pcm_stream_lock_nested(substream1);
2249 snd_pcm_group_assign(substream1, target_group);
2250 refcount_inc(&target_group->refs);
2251 snd_pcm_stream_unlock(substream1);
2252 snd_pcm_group_unlock_irq(target_group, nonatomic);
2254 up_write(&snd_pcm_link_rwsem);
2262 static void relink_to_local(struct snd_pcm_substream *substream)
2264 snd_pcm_stream_lock_nested(substream);
2265 snd_pcm_group_assign(substream, &substream->self_group);
2266 snd_pcm_stream_unlock(substream);
2269 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2271 struct snd_pcm_group *group;
2272 bool nonatomic = substream->pcm->nonatomic;
2273 bool do_free = false;
2276 down_write(&snd_pcm_link_rwsem);
2278 if (!snd_pcm_stream_linked(substream)) {
2283 group = substream->group;
2284 snd_pcm_group_lock_irq(group, nonatomic);
2286 relink_to_local(substream);
2287 refcount_dec(&group->refs);
2289 /* detach the last stream, too */
2290 if (list_is_singular(&group->substreams)) {
2291 relink_to_local(list_first_entry(&group->substreams,
2292 struct snd_pcm_substream,
2294 do_free = refcount_dec_and_test(&group->refs);
2297 snd_pcm_group_unlock_irq(group, nonatomic);
2302 up_write(&snd_pcm_link_rwsem);
2309 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2310 struct snd_pcm_hw_rule *rule)
2312 struct snd_interval t;
2313 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2314 hw_param_interval_c(params, rule->deps[1]), &t);
2315 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2318 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2319 struct snd_pcm_hw_rule *rule)
2321 struct snd_interval t;
2322 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2323 hw_param_interval_c(params, rule->deps[1]), &t);
2324 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2327 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2328 struct snd_pcm_hw_rule *rule)
2330 struct snd_interval t;
2331 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2332 hw_param_interval_c(params, rule->deps[1]),
2333 (unsigned long) rule->private, &t);
2334 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2337 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2338 struct snd_pcm_hw_rule *rule)
2340 struct snd_interval t;
2341 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2342 (unsigned long) rule->private,
2343 hw_param_interval_c(params, rule->deps[1]), &t);
2344 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2347 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2348 struct snd_pcm_hw_rule *rule)
2351 const struct snd_interval *i =
2352 hw_param_interval_c(params, rule->deps[0]);
2354 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2356 pcm_for_each_format(k) {
2358 if (!snd_mask_test_format(mask, k))
2360 bits = snd_pcm_format_physical_width(k);
2362 continue; /* ignore invalid formats */
2363 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2364 snd_mask_reset(&m, (__force unsigned)k);
2366 return snd_mask_refine(mask, &m);
2369 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2370 struct snd_pcm_hw_rule *rule)
2372 struct snd_interval t;
2379 pcm_for_each_format(k) {
2381 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2383 bits = snd_pcm_format_physical_width(k);
2385 continue; /* ignore invalid formats */
2386 if (t.min > (unsigned)bits)
2388 if (t.max < (unsigned)bits)
2392 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2395 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2396 #error "Change this table"
2399 static const unsigned int rates[] = {
2400 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2401 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2404 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2405 .count = ARRAY_SIZE(rates),
2409 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2410 struct snd_pcm_hw_rule *rule)
2412 struct snd_pcm_hardware *hw = rule->private;
2413 return snd_interval_list(hw_param_interval(params, rule->var),
2414 snd_pcm_known_rates.count,
2415 snd_pcm_known_rates.list, hw->rates);
2418 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2419 struct snd_pcm_hw_rule *rule)
2421 struct snd_interval t;
2422 struct snd_pcm_substream *substream = rule->private;
2424 t.max = substream->buffer_bytes_max;
2428 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2431 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2433 struct snd_pcm_runtime *runtime = substream->runtime;
2434 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2437 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2438 snd_mask_any(constrs_mask(constrs, k));
2441 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2442 snd_interval_any(constrs_interval(constrs, k));
2445 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2446 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2447 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2448 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2449 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2451 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2452 snd_pcm_hw_rule_format, NULL,
2453 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2456 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2457 snd_pcm_hw_rule_sample_bits, NULL,
2458 SNDRV_PCM_HW_PARAM_FORMAT,
2459 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2462 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2463 snd_pcm_hw_rule_div, NULL,
2464 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2467 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2468 snd_pcm_hw_rule_mul, NULL,
2469 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2472 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2473 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2474 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2477 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2478 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2479 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2482 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2483 snd_pcm_hw_rule_div, NULL,
2484 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2487 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2488 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2489 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2492 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2493 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2494 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2497 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2498 snd_pcm_hw_rule_div, NULL,
2499 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2502 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2503 snd_pcm_hw_rule_div, NULL,
2504 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2507 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2508 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2509 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2512 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2513 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2514 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2517 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2518 snd_pcm_hw_rule_mul, NULL,
2519 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2522 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2523 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2524 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2527 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2528 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2529 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2532 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2533 snd_pcm_hw_rule_muldivk, (void*) 8,
2534 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2537 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2538 snd_pcm_hw_rule_muldivk, (void*) 8,
2539 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2542 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2543 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2544 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2547 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2548 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2549 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2555 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2557 struct snd_pcm_runtime *runtime = substream->runtime;
2558 struct snd_pcm_hardware *hw = &runtime->hw;
2560 unsigned int mask = 0;
2562 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2563 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2564 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2565 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2566 if (hw_support_mmap(substream)) {
2567 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2568 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2569 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2570 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2571 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2572 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2574 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2578 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2582 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2583 PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
2587 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2588 hw->channels_min, hw->channels_max);
2592 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2593 hw->rate_min, hw->rate_max);
2597 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2598 hw->period_bytes_min, hw->period_bytes_max);
2602 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2603 hw->periods_min, hw->periods_max);
2607 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2608 hw->period_bytes_min, hw->buffer_bytes_max);
2612 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2613 snd_pcm_hw_rule_buffer_bytes_max, substream,
2614 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2619 if (runtime->dma_bytes) {
2620 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2625 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2626 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2627 snd_pcm_hw_rule_rate, hw,
2628 SNDRV_PCM_HW_PARAM_RATE, -1);
2633 /* FIXME: this belong to lowlevel */
2634 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2639 static void pcm_release_private(struct snd_pcm_substream *substream)
2641 if (snd_pcm_stream_linked(substream))
2642 snd_pcm_unlink(substream);
2645 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2647 substream->ref_count--;
2648 if (substream->ref_count > 0)
2651 snd_pcm_drop(substream);
2652 if (substream->hw_opened) {
2653 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2654 do_hw_free(substream);
2655 substream->ops->close(substream);
2656 substream->hw_opened = 0;
2658 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2659 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2660 if (substream->pcm_release) {
2661 substream->pcm_release(substream);
2662 substream->pcm_release = NULL;
2664 snd_pcm_detach_substream(substream);
2666 EXPORT_SYMBOL(snd_pcm_release_substream);
2668 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2670 struct snd_pcm_substream **rsubstream)
2672 struct snd_pcm_substream *substream;
2675 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2678 if (substream->ref_count > 1) {
2679 *rsubstream = substream;
2683 err = snd_pcm_hw_constraints_init(substream);
2685 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2689 err = substream->ops->open(substream);
2693 substream->hw_opened = 1;
2695 err = snd_pcm_hw_constraints_complete(substream);
2697 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2701 /* automatically set EXPLICIT_SYNC flag in the managed mode whenever
2702 * the DMA buffer requires it
2704 if (substream->managed_buffer_alloc &&
2705 substream->dma_buffer.dev.need_sync)
2706 substream->runtime->hw.info |= SNDRV_PCM_INFO_EXPLICIT_SYNC;
2708 *rsubstream = substream;
2712 snd_pcm_release_substream(substream);
2715 EXPORT_SYMBOL(snd_pcm_open_substream);
2717 static int snd_pcm_open_file(struct file *file,
2718 struct snd_pcm *pcm,
2721 struct snd_pcm_file *pcm_file;
2722 struct snd_pcm_substream *substream;
2725 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2729 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2730 if (pcm_file == NULL) {
2731 snd_pcm_release_substream(substream);
2734 pcm_file->substream = substream;
2735 if (substream->ref_count == 1)
2736 substream->pcm_release = pcm_release_private;
2737 file->private_data = pcm_file;
2742 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2744 struct snd_pcm *pcm;
2745 int err = nonseekable_open(inode, file);
2748 pcm = snd_lookup_minor_data(iminor(inode),
2749 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2750 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2752 snd_card_unref(pcm->card);
2756 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2758 struct snd_pcm *pcm;
2759 int err = nonseekable_open(inode, file);
2762 pcm = snd_lookup_minor_data(iminor(inode),
2763 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2764 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2766 snd_card_unref(pcm->card);
2770 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2773 wait_queue_entry_t wait;
2779 err = snd_card_file_add(pcm->card, file);
2782 if (!try_module_get(pcm->card->module)) {
2786 init_waitqueue_entry(&wait, current);
2787 add_wait_queue(&pcm->open_wait, &wait);
2788 mutex_lock(&pcm->open_mutex);
2790 err = snd_pcm_open_file(file, pcm, stream);
2793 if (err == -EAGAIN) {
2794 if (file->f_flags & O_NONBLOCK) {
2800 set_current_state(TASK_INTERRUPTIBLE);
2801 mutex_unlock(&pcm->open_mutex);
2803 mutex_lock(&pcm->open_mutex);
2804 if (pcm->card->shutdown) {
2808 if (signal_pending(current)) {
2813 remove_wait_queue(&pcm->open_wait, &wait);
2814 mutex_unlock(&pcm->open_mutex);
2820 module_put(pcm->card->module);
2822 snd_card_file_remove(pcm->card, file);
2827 static int snd_pcm_release(struct inode *inode, struct file *file)
2829 struct snd_pcm *pcm;
2830 struct snd_pcm_substream *substream;
2831 struct snd_pcm_file *pcm_file;
2833 pcm_file = file->private_data;
2834 substream = pcm_file->substream;
2835 if (snd_BUG_ON(!substream))
2837 pcm = substream->pcm;
2839 /* block until the device gets woken up as it may touch the hardware */
2840 snd_power_wait(pcm->card);
2842 mutex_lock(&pcm->open_mutex);
2843 snd_pcm_release_substream(substream);
2845 mutex_unlock(&pcm->open_mutex);
2846 wake_up(&pcm->open_wait);
2847 module_put(pcm->card->module);
2848 snd_card_file_remove(pcm->card, file);
2852 /* check and update PCM state; return 0 or a negative error
2853 * call this inside PCM lock
2855 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2857 switch (substream->runtime->status->state) {
2858 case SNDRV_PCM_STATE_DRAINING:
2859 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2862 case SNDRV_PCM_STATE_RUNNING:
2863 return snd_pcm_update_hw_ptr(substream);
2864 case SNDRV_PCM_STATE_PREPARED:
2865 case SNDRV_PCM_STATE_PAUSED:
2867 case SNDRV_PCM_STATE_SUSPENDED:
2869 case SNDRV_PCM_STATE_XRUN:
2876 /* increase the appl_ptr; returns the processed frames or a negative error */
2877 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2878 snd_pcm_uframes_t frames,
2879 snd_pcm_sframes_t avail)
2881 struct snd_pcm_runtime *runtime = substream->runtime;
2882 snd_pcm_sframes_t appl_ptr;
2887 if (frames > (snd_pcm_uframes_t)avail)
2889 appl_ptr = runtime->control->appl_ptr + frames;
2890 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2891 appl_ptr -= runtime->boundary;
2892 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2893 return ret < 0 ? ret : frames;
2896 /* decrease the appl_ptr; returns the processed frames or zero for error */
2897 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2898 snd_pcm_uframes_t frames,
2899 snd_pcm_sframes_t avail)
2901 struct snd_pcm_runtime *runtime = substream->runtime;
2902 snd_pcm_sframes_t appl_ptr;
2907 if (frames > (snd_pcm_uframes_t)avail)
2909 appl_ptr = runtime->control->appl_ptr - frames;
2911 appl_ptr += runtime->boundary;
2912 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2913 /* NOTE: we return zero for errors because PulseAudio gets depressed
2914 * upon receiving an error from rewind ioctl and stops processing
2915 * any longer. Returning zero means that no rewind is done, so
2916 * it's not absolutely wrong to answer like that.
2918 return ret < 0 ? 0 : frames;
2921 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2922 snd_pcm_uframes_t frames)
2924 snd_pcm_sframes_t ret;
2929 snd_pcm_stream_lock_irq(substream);
2930 ret = do_pcm_hwsync(substream);
2932 ret = rewind_appl_ptr(substream, frames,
2933 snd_pcm_hw_avail(substream));
2934 snd_pcm_stream_unlock_irq(substream);
2936 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
2940 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2941 snd_pcm_uframes_t frames)
2943 snd_pcm_sframes_t ret;
2948 snd_pcm_stream_lock_irq(substream);
2949 ret = do_pcm_hwsync(substream);
2951 ret = forward_appl_ptr(substream, frames,
2952 snd_pcm_avail(substream));
2953 snd_pcm_stream_unlock_irq(substream);
2955 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
2959 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2960 snd_pcm_sframes_t *delay)
2964 snd_pcm_stream_lock_irq(substream);
2965 err = do_pcm_hwsync(substream);
2967 *delay = snd_pcm_calc_delay(substream);
2968 snd_pcm_stream_unlock_irq(substream);
2969 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU);
2974 static inline int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2976 return snd_pcm_delay(substream, NULL);
2979 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2980 struct snd_pcm_sync_ptr __user *_sync_ptr)
2982 struct snd_pcm_runtime *runtime = substream->runtime;
2983 struct snd_pcm_sync_ptr sync_ptr;
2984 volatile struct snd_pcm_mmap_status *status;
2985 volatile struct snd_pcm_mmap_control *control;
2988 memset(&sync_ptr, 0, sizeof(sync_ptr));
2989 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2991 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2993 status = runtime->status;
2994 control = runtime->control;
2995 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2996 err = snd_pcm_hwsync(substream);
3000 snd_pcm_stream_lock_irq(substream);
3001 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
3002 err = pcm_lib_apply_appl_ptr(substream,
3003 sync_ptr.c.control.appl_ptr);
3005 snd_pcm_stream_unlock_irq(substream);
3009 sync_ptr.c.control.appl_ptr = control->appl_ptr;
3011 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3012 control->avail_min = sync_ptr.c.control.avail_min;
3014 sync_ptr.c.control.avail_min = control->avail_min;
3015 sync_ptr.s.status.state = status->state;
3016 sync_ptr.s.status.hw_ptr = status->hw_ptr;
3017 sync_ptr.s.status.tstamp = status->tstamp;
3018 sync_ptr.s.status.suspended_state = status->suspended_state;
3019 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
3020 snd_pcm_stream_unlock_irq(substream);
3021 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
3022 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3023 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
3028 struct snd_pcm_mmap_status32 {
3029 snd_pcm_state_t state;
3034 snd_pcm_state_t suspended_state;
3035 s32 audio_tstamp_sec;
3036 s32 audio_tstamp_nsec;
3037 } __attribute__((packed));
3039 struct snd_pcm_mmap_control32 {
3044 struct snd_pcm_sync_ptr32 {
3047 struct snd_pcm_mmap_status32 status;
3048 unsigned char reserved[64];
3051 struct snd_pcm_mmap_control32 control;
3052 unsigned char reserved[64];
3054 } __attribute__((packed));
3056 /* recalcuate the boundary within 32bit */
3057 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
3059 snd_pcm_uframes_t boundary;
3061 if (! runtime->buffer_size)
3063 boundary = runtime->buffer_size;
3064 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3069 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3070 struct snd_pcm_sync_ptr32 __user *src)
3072 struct snd_pcm_runtime *runtime = substream->runtime;
3073 volatile struct snd_pcm_mmap_status *status;
3074 volatile struct snd_pcm_mmap_control *control;
3076 struct snd_pcm_mmap_control scontrol;
3077 struct snd_pcm_mmap_status sstatus;
3078 snd_pcm_uframes_t boundary;
3081 if (snd_BUG_ON(!runtime))
3084 if (get_user(sflags, &src->flags) ||
3085 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3086 get_user(scontrol.avail_min, &src->c.control.avail_min))
3088 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3089 err = snd_pcm_hwsync(substream);
3093 status = runtime->status;
3094 control = runtime->control;
3095 boundary = recalculate_boundary(runtime);
3097 boundary = 0x7fffffff;
3098 snd_pcm_stream_lock_irq(substream);
3099 /* FIXME: we should consider the boundary for the sync from app */
3100 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3101 err = pcm_lib_apply_appl_ptr(substream,
3104 snd_pcm_stream_unlock_irq(substream);
3108 scontrol.appl_ptr = control->appl_ptr % boundary;
3109 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3110 control->avail_min = scontrol.avail_min;
3112 scontrol.avail_min = control->avail_min;
3113 sstatus.state = status->state;
3114 sstatus.hw_ptr = status->hw_ptr % boundary;
3115 sstatus.tstamp = status->tstamp;
3116 sstatus.suspended_state = status->suspended_state;
3117 sstatus.audio_tstamp = status->audio_tstamp;
3118 snd_pcm_stream_unlock_irq(substream);
3119 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3120 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE);
3121 if (put_user(sstatus.state, &src->s.status.state) ||
3122 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3123 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3124 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3125 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3126 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3127 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3128 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3129 put_user(scontrol.avail_min, &src->c.control.avail_min))
3134 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3136 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3138 struct snd_pcm_runtime *runtime = substream->runtime;
3141 if (get_user(arg, _arg))
3143 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3145 runtime->tstamp_type = arg;
3149 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3150 struct snd_xferi __user *_xferi)
3152 struct snd_xferi xferi;
3153 struct snd_pcm_runtime *runtime = substream->runtime;
3154 snd_pcm_sframes_t result;
3156 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3158 if (put_user(0, &_xferi->result))
3160 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3162 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3163 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3165 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3166 if (put_user(result, &_xferi->result))
3168 return result < 0 ? result : 0;
3171 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3172 struct snd_xfern __user *_xfern)
3174 struct snd_xfern xfern;
3175 struct snd_pcm_runtime *runtime = substream->runtime;
3177 snd_pcm_sframes_t result;
3179 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3181 if (runtime->channels > 128)
3183 if (put_user(0, &_xfern->result))
3185 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3188 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3190 return PTR_ERR(bufs);
3191 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3192 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3194 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3196 if (put_user(result, &_xfern->result))
3198 return result < 0 ? result : 0;
3201 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3202 snd_pcm_uframes_t __user *_frames)
3204 snd_pcm_uframes_t frames;
3205 snd_pcm_sframes_t result;
3207 if (get_user(frames, _frames))
3209 if (put_user(0, _frames))
3211 result = snd_pcm_rewind(substream, frames);
3212 if (put_user(result, _frames))
3214 return result < 0 ? result : 0;
3217 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3218 snd_pcm_uframes_t __user *_frames)
3220 snd_pcm_uframes_t frames;
3221 snd_pcm_sframes_t result;
3223 if (get_user(frames, _frames))
3225 if (put_user(0, _frames))
3227 result = snd_pcm_forward(substream, frames);
3228 if (put_user(result, _frames))
3230 return result < 0 ? result : 0;
3233 static int snd_pcm_common_ioctl(struct file *file,
3234 struct snd_pcm_substream *substream,
3235 unsigned int cmd, void __user *arg)
3237 struct snd_pcm_file *pcm_file = file->private_data;
3240 if (PCM_RUNTIME_CHECK(substream))
3243 if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3246 res = snd_power_wait(substream->pcm->card);
3251 case SNDRV_PCM_IOCTL_PVERSION:
3252 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3253 case SNDRV_PCM_IOCTL_INFO:
3254 return snd_pcm_info_user(substream, arg);
3255 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3257 case SNDRV_PCM_IOCTL_TTSTAMP:
3258 return snd_pcm_tstamp(substream, arg);
3259 case SNDRV_PCM_IOCTL_USER_PVERSION:
3260 if (get_user(pcm_file->user_pversion,
3261 (unsigned int __user *)arg))
3264 case SNDRV_PCM_IOCTL_HW_REFINE:
3265 return snd_pcm_hw_refine_user(substream, arg);
3266 case SNDRV_PCM_IOCTL_HW_PARAMS:
3267 return snd_pcm_hw_params_user(substream, arg);
3268 case SNDRV_PCM_IOCTL_HW_FREE:
3269 return snd_pcm_hw_free(substream);
3270 case SNDRV_PCM_IOCTL_SW_PARAMS:
3271 return snd_pcm_sw_params_user(substream, arg);
3272 case SNDRV_PCM_IOCTL_STATUS32:
3273 return snd_pcm_status_user32(substream, arg, false);
3274 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3275 return snd_pcm_status_user32(substream, arg, true);
3276 case SNDRV_PCM_IOCTL_STATUS64:
3277 return snd_pcm_status_user64(substream, arg, false);
3278 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3279 return snd_pcm_status_user64(substream, arg, true);
3280 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3281 return snd_pcm_channel_info_user(substream, arg);
3282 case SNDRV_PCM_IOCTL_PREPARE:
3283 return snd_pcm_prepare(substream, file);
3284 case SNDRV_PCM_IOCTL_RESET:
3285 return snd_pcm_reset(substream);
3286 case SNDRV_PCM_IOCTL_START:
3287 return snd_pcm_start_lock_irq(substream);
3288 case SNDRV_PCM_IOCTL_LINK:
3289 return snd_pcm_link(substream, (int)(unsigned long) arg);
3290 case SNDRV_PCM_IOCTL_UNLINK:
3291 return snd_pcm_unlink(substream);
3292 case SNDRV_PCM_IOCTL_RESUME:
3293 return snd_pcm_resume(substream);
3294 case SNDRV_PCM_IOCTL_XRUN:
3295 return snd_pcm_xrun(substream);
3296 case SNDRV_PCM_IOCTL_HWSYNC:
3297 return snd_pcm_hwsync(substream);
3298 case SNDRV_PCM_IOCTL_DELAY:
3300 snd_pcm_sframes_t delay = 0;
3301 snd_pcm_sframes_t __user *res = arg;
3304 err = snd_pcm_delay(substream, &delay);
3307 if (put_user(delay, res))
3311 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3312 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3313 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3314 return snd_pcm_sync_ptr(substream, arg);
3315 #ifdef CONFIG_SND_SUPPORT_OLD_API
3316 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3317 return snd_pcm_hw_refine_old_user(substream, arg);
3318 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3319 return snd_pcm_hw_params_old_user(substream, arg);
3321 case SNDRV_PCM_IOCTL_DRAIN:
3322 return snd_pcm_drain(substream, file);
3323 case SNDRV_PCM_IOCTL_DROP:
3324 return snd_pcm_drop(substream);
3325 case SNDRV_PCM_IOCTL_PAUSE:
3326 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3327 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3328 case SNDRV_PCM_IOCTL_READI_FRAMES:
3329 return snd_pcm_xferi_frames_ioctl(substream, arg);
3330 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3331 case SNDRV_PCM_IOCTL_READN_FRAMES:
3332 return snd_pcm_xfern_frames_ioctl(substream, arg);
3333 case SNDRV_PCM_IOCTL_REWIND:
3334 return snd_pcm_rewind_ioctl(substream, arg);
3335 case SNDRV_PCM_IOCTL_FORWARD:
3336 return snd_pcm_forward_ioctl(substream, arg);
3338 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3342 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3345 struct snd_pcm_file *pcm_file;
3347 pcm_file = file->private_data;
3349 if (((cmd >> 8) & 0xff) != 'A')
3352 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3353 (void __user *)arg);
3357 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3358 * @substream: PCM substream
3360 * @arg: IOCTL argument
3362 * The function is provided primarily for OSS layer and USB gadget drivers,
3363 * and it allows only the limited set of ioctls (hw_params, sw_params,
3364 * prepare, start, drain, drop, forward).
3366 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3367 unsigned int cmd, void *arg)
3369 snd_pcm_uframes_t *frames = arg;
3370 snd_pcm_sframes_t result;
3372 if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3376 case SNDRV_PCM_IOCTL_FORWARD:
3378 /* provided only for OSS; capture-only and no value returned */
3379 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3381 result = snd_pcm_forward(substream, *frames);
3382 return result < 0 ? result : 0;
3384 case SNDRV_PCM_IOCTL_HW_PARAMS:
3385 return snd_pcm_hw_params(substream, arg);
3386 case SNDRV_PCM_IOCTL_SW_PARAMS:
3387 return snd_pcm_sw_params(substream, arg);
3388 case SNDRV_PCM_IOCTL_PREPARE:
3389 return snd_pcm_prepare(substream, NULL);
3390 case SNDRV_PCM_IOCTL_START:
3391 return snd_pcm_start_lock_irq(substream);
3392 case SNDRV_PCM_IOCTL_DRAIN:
3393 return snd_pcm_drain(substream, NULL);
3394 case SNDRV_PCM_IOCTL_DROP:
3395 return snd_pcm_drop(substream);
3396 case SNDRV_PCM_IOCTL_DELAY:
3397 return snd_pcm_delay(substream, frames);
3402 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3404 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3407 struct snd_pcm_file *pcm_file;
3408 struct snd_pcm_substream *substream;
3409 struct snd_pcm_runtime *runtime;
3410 snd_pcm_sframes_t result;
3412 pcm_file = file->private_data;
3413 substream = pcm_file->substream;
3414 if (PCM_RUNTIME_CHECK(substream))
3416 runtime = substream->runtime;
3417 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3418 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3420 if (!frame_aligned(runtime, count))
3422 count = bytes_to_frames(runtime, count);
3423 result = snd_pcm_lib_read(substream, buf, count);
3425 result = frames_to_bytes(runtime, result);
3429 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3430 size_t count, loff_t * offset)
3432 struct snd_pcm_file *pcm_file;
3433 struct snd_pcm_substream *substream;
3434 struct snd_pcm_runtime *runtime;
3435 snd_pcm_sframes_t result;
3437 pcm_file = file->private_data;
3438 substream = pcm_file->substream;
3439 if (PCM_RUNTIME_CHECK(substream))
3441 runtime = substream->runtime;
3442 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3443 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3445 if (!frame_aligned(runtime, count))
3447 count = bytes_to_frames(runtime, count);
3448 result = snd_pcm_lib_write(substream, buf, count);
3450 result = frames_to_bytes(runtime, result);
3454 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3456 struct snd_pcm_file *pcm_file;
3457 struct snd_pcm_substream *substream;
3458 struct snd_pcm_runtime *runtime;
3459 snd_pcm_sframes_t result;
3462 snd_pcm_uframes_t frames;
3464 pcm_file = iocb->ki_filp->private_data;
3465 substream = pcm_file->substream;
3466 if (PCM_RUNTIME_CHECK(substream))
3468 runtime = substream->runtime;
3469 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3470 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3472 if (!iter_is_iovec(to))
3474 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3476 if (!frame_aligned(runtime, to->iov->iov_len))
3478 frames = bytes_to_samples(runtime, to->iov->iov_len);
3479 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3482 for (i = 0; i < to->nr_segs; ++i)
3483 bufs[i] = to->iov[i].iov_base;
3484 result = snd_pcm_lib_readv(substream, bufs, frames);
3486 result = frames_to_bytes(runtime, result);
3491 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3493 struct snd_pcm_file *pcm_file;
3494 struct snd_pcm_substream *substream;
3495 struct snd_pcm_runtime *runtime;
3496 snd_pcm_sframes_t result;
3499 snd_pcm_uframes_t frames;
3501 pcm_file = iocb->ki_filp->private_data;
3502 substream = pcm_file->substream;
3503 if (PCM_RUNTIME_CHECK(substream))
3505 runtime = substream->runtime;
3506 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
3507 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3509 if (!iter_is_iovec(from))
3511 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3512 !frame_aligned(runtime, from->iov->iov_len))
3514 frames = bytes_to_samples(runtime, from->iov->iov_len);
3515 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3518 for (i = 0; i < from->nr_segs; ++i)
3519 bufs[i] = from->iov[i].iov_base;
3520 result = snd_pcm_lib_writev(substream, bufs, frames);
3522 result = frames_to_bytes(runtime, result);
3527 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3529 struct snd_pcm_file *pcm_file;
3530 struct snd_pcm_substream *substream;
3531 struct snd_pcm_runtime *runtime;
3533 snd_pcm_uframes_t avail;
3535 pcm_file = file->private_data;
3537 substream = pcm_file->substream;
3538 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3539 ok = EPOLLOUT | EPOLLWRNORM;
3541 ok = EPOLLIN | EPOLLRDNORM;
3542 if (PCM_RUNTIME_CHECK(substream))
3543 return ok | EPOLLERR;
3545 runtime = substream->runtime;
3546 if (runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3547 return ok | EPOLLERR;
3549 poll_wait(file, &runtime->sleep, wait);
3552 snd_pcm_stream_lock_irq(substream);
3553 avail = snd_pcm_avail(substream);
3554 switch (runtime->status->state) {
3555 case SNDRV_PCM_STATE_RUNNING:
3556 case SNDRV_PCM_STATE_PREPARED:
3557 case SNDRV_PCM_STATE_PAUSED:
3558 if (avail >= runtime->control->avail_min)
3561 case SNDRV_PCM_STATE_DRAINING:
3562 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3569 mask = ok | EPOLLERR;
3572 snd_pcm_stream_unlock_irq(substream);
3581 * Only on coherent architectures, we can mmap the status and the control records
3582 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3584 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3586 * mmap status record
3588 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3590 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3591 struct snd_pcm_runtime *runtime;
3593 if (substream == NULL)
3594 return VM_FAULT_SIGBUS;
3595 runtime = substream->runtime;
3596 vmf->page = virt_to_page(runtime->status);
3597 get_page(vmf->page);
3601 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3603 .fault = snd_pcm_mmap_status_fault,
3606 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3607 struct vm_area_struct *area)
3610 if (!(area->vm_flags & VM_READ))
3612 size = area->vm_end - area->vm_start;
3613 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3615 area->vm_ops = &snd_pcm_vm_ops_status;
3616 area->vm_private_data = substream;
3617 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3622 * mmap control record
3624 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3626 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3627 struct snd_pcm_runtime *runtime;
3629 if (substream == NULL)
3630 return VM_FAULT_SIGBUS;
3631 runtime = substream->runtime;
3632 vmf->page = virt_to_page(runtime->control);
3633 get_page(vmf->page);
3637 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3639 .fault = snd_pcm_mmap_control_fault,
3642 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3643 struct vm_area_struct *area)
3646 if (!(area->vm_flags & VM_READ))
3648 size = area->vm_end - area->vm_start;
3649 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3651 area->vm_ops = &snd_pcm_vm_ops_control;
3652 area->vm_private_data = substream;
3653 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3657 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3659 /* If drivers require the explicit sync (typically for non-coherent
3660 * pages), we have to disable the mmap of status and control data
3661 * to enforce the control via SYNC_PTR ioctl.
3663 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
3665 /* See pcm_control_mmap_allowed() below.
3666 * Since older alsa-lib requires both status and control mmaps to be
3667 * coupled, we have to disable the status mmap for old alsa-lib, too.
3669 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3670 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3675 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3677 if (pcm_file->no_compat_mmap)
3680 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_EXPLICIT_SYNC)
3682 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3683 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3684 * thus it effectively assures the manual update of appl_ptr.
3686 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3691 #else /* ! coherent mmap */
3693 * don't support mmap for status and control records.
3695 #define pcm_status_mmap_allowed(pcm_file) false
3696 #define pcm_control_mmap_allowed(pcm_file) false
3698 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3699 struct vm_area_struct *area)
3703 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3704 struct vm_area_struct *area)
3708 #endif /* coherent mmap */
3711 * fault callback for mmapping a RAM page
3713 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3715 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3716 struct snd_pcm_runtime *runtime;
3717 unsigned long offset;
3721 if (substream == NULL)
3722 return VM_FAULT_SIGBUS;
3723 runtime = substream->runtime;
3724 offset = vmf->pgoff << PAGE_SHIFT;
3725 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3726 if (offset > dma_bytes - PAGE_SIZE)
3727 return VM_FAULT_SIGBUS;
3728 if (substream->ops->page)
3729 page = substream->ops->page(substream, offset);
3730 else if (!snd_pcm_get_dma_buf(substream))
3731 page = virt_to_page(runtime->dma_area + offset);
3733 page = snd_sgbuf_get_page(snd_pcm_get_dma_buf(substream), offset);
3735 return VM_FAULT_SIGBUS;
3741 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3742 .open = snd_pcm_mmap_data_open,
3743 .close = snd_pcm_mmap_data_close,
3746 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3747 .open = snd_pcm_mmap_data_open,
3748 .close = snd_pcm_mmap_data_close,
3749 .fault = snd_pcm_mmap_data_fault,
3753 * mmap the DMA buffer on RAM
3757 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3758 * @substream: PCM substream
3761 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3762 * this function is invoked implicitly.
3764 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3765 struct vm_area_struct *area)
3767 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3768 if (!substream->ops->page &&
3769 !snd_dma_buffer_mmap(snd_pcm_get_dma_buf(substream), area))
3771 /* mmap with fault handler */
3772 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3775 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3778 * mmap the DMA buffer on I/O memory area
3780 #if SNDRV_PCM_INFO_MMAP_IOMEM
3782 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3783 * @substream: PCM substream
3786 * When your hardware uses the iomapped pages as the hardware buffer and
3787 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3788 * is supposed to work only on limited architectures.
3790 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3791 struct vm_area_struct *area)
3793 struct snd_pcm_runtime *runtime = substream->runtime;
3795 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3796 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3798 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3799 #endif /* SNDRV_PCM_INFO_MMAP */
3804 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3805 struct vm_area_struct *area)
3807 struct snd_pcm_runtime *runtime;
3809 unsigned long offset;
3813 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3814 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3817 if (!(area->vm_flags & VM_READ))
3820 runtime = substream->runtime;
3821 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3823 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3825 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3826 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3828 size = area->vm_end - area->vm_start;
3829 offset = area->vm_pgoff << PAGE_SHIFT;
3830 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3831 if ((size_t)size > dma_bytes)
3833 if (offset > dma_bytes - size)
3836 area->vm_ops = &snd_pcm_vm_ops_data;
3837 area->vm_private_data = substream;
3838 if (substream->ops->mmap)
3839 err = substream->ops->mmap(substream, area);
3841 err = snd_pcm_lib_default_mmap(substream, area);
3843 atomic_inc(&substream->mmap_count);
3846 EXPORT_SYMBOL(snd_pcm_mmap_data);
3848 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3850 struct snd_pcm_file * pcm_file;
3851 struct snd_pcm_substream *substream;
3852 unsigned long offset;
3854 pcm_file = file->private_data;
3855 substream = pcm_file->substream;
3856 if (PCM_RUNTIME_CHECK(substream))
3858 if (substream->runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3861 offset = area->vm_pgoff << PAGE_SHIFT;
3863 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3864 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3867 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3868 if (!pcm_status_mmap_allowed(pcm_file))
3870 return snd_pcm_mmap_status(substream, file, area);
3871 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3872 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3875 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3876 if (!pcm_control_mmap_allowed(pcm_file))
3878 return snd_pcm_mmap_control(substream, file, area);
3880 return snd_pcm_mmap_data(substream, file, area);
3885 static int snd_pcm_fasync(int fd, struct file * file, int on)
3887 struct snd_pcm_file * pcm_file;
3888 struct snd_pcm_substream *substream;
3889 struct snd_pcm_runtime *runtime;
3891 pcm_file = file->private_data;
3892 substream = pcm_file->substream;
3893 if (PCM_RUNTIME_CHECK(substream))
3895 runtime = substream->runtime;
3896 if (runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
3898 return fasync_helper(fd, file, on, &runtime->fasync);
3904 #ifdef CONFIG_COMPAT
3905 #include "pcm_compat.c"
3907 #define snd_pcm_ioctl_compat NULL
3911 * To be removed helpers to keep binary compatibility
3914 #ifdef CONFIG_SND_SUPPORT_OLD_API
3915 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3916 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3918 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3919 struct snd_pcm_hw_params_old *oparams)
3923 memset(params, 0, sizeof(*params));
3924 params->flags = oparams->flags;
3925 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3926 params->masks[i].bits[0] = oparams->masks[i];
3927 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3928 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3929 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3930 params->info = oparams->info;
3931 params->msbits = oparams->msbits;
3932 params->rate_num = oparams->rate_num;
3933 params->rate_den = oparams->rate_den;
3934 params->fifo_size = oparams->fifo_size;
3937 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3938 struct snd_pcm_hw_params *params)
3942 memset(oparams, 0, sizeof(*oparams));
3943 oparams->flags = params->flags;
3944 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3945 oparams->masks[i] = params->masks[i].bits[0];
3946 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3947 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3948 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3949 oparams->info = params->info;
3950 oparams->msbits = params->msbits;
3951 oparams->rate_num = params->rate_num;
3952 oparams->rate_den = params->rate_den;
3953 oparams->fifo_size = params->fifo_size;
3956 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3957 struct snd_pcm_hw_params_old __user * _oparams)
3959 struct snd_pcm_hw_params *params;
3960 struct snd_pcm_hw_params_old *oparams = NULL;
3963 params = kmalloc(sizeof(*params), GFP_KERNEL);
3967 oparams = memdup_user(_oparams, sizeof(*oparams));
3968 if (IS_ERR(oparams)) {
3969 err = PTR_ERR(oparams);
3972 snd_pcm_hw_convert_from_old_params(params, oparams);
3973 err = snd_pcm_hw_refine(substream, params);
3977 err = fixup_unreferenced_params(substream, params);
3981 snd_pcm_hw_convert_to_old_params(oparams, params);
3982 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3991 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3992 struct snd_pcm_hw_params_old __user * _oparams)
3994 struct snd_pcm_hw_params *params;
3995 struct snd_pcm_hw_params_old *oparams = NULL;
3998 params = kmalloc(sizeof(*params), GFP_KERNEL);
4002 oparams = memdup_user(_oparams, sizeof(*oparams));
4003 if (IS_ERR(oparams)) {
4004 err = PTR_ERR(oparams);
4008 snd_pcm_hw_convert_from_old_params(params, oparams);
4009 err = snd_pcm_hw_params(substream, params);
4013 snd_pcm_hw_convert_to_old_params(oparams, params);
4014 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
4022 #endif /* CONFIG_SND_SUPPORT_OLD_API */
4025 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
4028 unsigned long pgoff,
4029 unsigned long flags)
4031 struct snd_pcm_file *pcm_file = file->private_data;
4032 struct snd_pcm_substream *substream = pcm_file->substream;
4033 struct snd_pcm_runtime *runtime = substream->runtime;
4034 unsigned long offset = pgoff << PAGE_SHIFT;
4037 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
4038 return (unsigned long)runtime->status;
4039 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
4040 return (unsigned long)runtime->control;
4042 return (unsigned long)runtime->dma_area + offset;
4046 # define snd_pcm_get_unmapped_area NULL
4053 const struct file_operations snd_pcm_f_ops[2] = {
4055 .owner = THIS_MODULE,
4056 .write = snd_pcm_write,
4057 .write_iter = snd_pcm_writev,
4058 .open = snd_pcm_playback_open,
4059 .release = snd_pcm_release,
4060 .llseek = no_llseek,
4061 .poll = snd_pcm_poll,
4062 .unlocked_ioctl = snd_pcm_ioctl,
4063 .compat_ioctl = snd_pcm_ioctl_compat,
4064 .mmap = snd_pcm_mmap,
4065 .fasync = snd_pcm_fasync,
4066 .get_unmapped_area = snd_pcm_get_unmapped_area,
4069 .owner = THIS_MODULE,
4070 .read = snd_pcm_read,
4071 .read_iter = snd_pcm_readv,
4072 .open = snd_pcm_capture_open,
4073 .release = snd_pcm_release,
4074 .llseek = no_llseek,
4075 .poll = snd_pcm_poll,
4076 .unlocked_ioctl = snd_pcm_ioctl,
4077 .compat_ioctl = snd_pcm_ioctl_compat,
4078 .mmap = snd_pcm_mmap,
4079 .fasync = snd_pcm_fasync,
4080 .get_unmapped_area = snd_pcm_get_unmapped_area,