1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/compat.h>
9 #include <linux/module.h>
10 #include <linux/file.h>
11 #include <linux/slab.h>
12 #include <linux/sched/signal.h>
13 #include <linux/time.h>
14 #include <linux/pm_qos.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/vmalloc.h>
18 #include <sound/core.h>
19 #include <sound/control.h>
20 #include <sound/info.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/timer.h>
24 #include <sound/minors.h>
25 #include <linux/uio.h>
26 #include <linux/delay.h>
28 #include "pcm_local.h"
30 #ifdef CONFIG_SND_DEBUG
31 #define CREATE_TRACE_POINTS
32 #include "pcm_param_trace.h"
34 #define trace_hw_mask_param_enabled() 0
35 #define trace_hw_interval_param_enabled() 0
36 #define trace_hw_mask_param(substream, type, index, prev, curr)
37 #define trace_hw_interval_param(substream, type, index, prev, curr)
44 struct snd_pcm_hw_params_old {
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
54 unsigned int rate_num;
55 unsigned int rate_den;
56 snd_pcm_uframes_t fifo_size;
57 unsigned char reserved[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
69 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem);
77 void snd_pcm_group_init(struct snd_pcm_group *group)
79 spin_lock_init(&group->lock);
80 mutex_init(&group->mutex);
81 INIT_LIST_HEAD(&group->substreams);
82 refcount_set(&group->refs, 1);
85 /* define group lock helpers */
86 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
87 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
90 mutex_ ## mutex_action(&group->mutex); \
92 spin_ ## action(&group->lock); \
95 DEFINE_PCM_GROUP_LOCK(lock, lock);
96 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
97 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
98 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
101 * snd_pcm_stream_lock - Lock the PCM stream
102 * @substream: PCM substream
104 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
105 * flag of the given substream. This also takes the global link rw lock
106 * (or rw sem), too, for avoiding the race with linked streams.
108 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
110 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
112 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
115 * snd_pcm_stream_unlock - Unlock the PCM stream
116 * @substream: PCM substream
118 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
120 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
122 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
124 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
127 * snd_pcm_stream_lock_irq - Lock the PCM stream
128 * @substream: PCM substream
130 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
131 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
132 * as snd_pcm_stream_lock().
134 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
136 snd_pcm_group_lock_irq(&substream->self_group,
137 substream->pcm->nonatomic);
139 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
141 static void snd_pcm_stream_lock_nested(struct snd_pcm_substream *substream)
143 struct snd_pcm_group *group = &substream->self_group;
145 if (substream->pcm->nonatomic)
146 mutex_lock_nested(&group->mutex, SINGLE_DEPTH_NESTING);
148 spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
152 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
153 * @substream: PCM substream
155 * This is a counter-part of snd_pcm_stream_lock_irq().
157 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
159 snd_pcm_group_unlock_irq(&substream->self_group,
160 substream->pcm->nonatomic);
162 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
164 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
166 unsigned long flags = 0;
167 if (substream->pcm->nonatomic)
168 mutex_lock(&substream->self_group.mutex);
170 spin_lock_irqsave(&substream->self_group.lock, flags);
173 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
176 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
177 * @substream: PCM substream
180 * This is a counter-part of snd_pcm_stream_lock_irqsave().
182 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
185 if (substream->pcm->nonatomic)
186 mutex_unlock(&substream->self_group.mutex);
188 spin_unlock_irqrestore(&substream->self_group.lock, flags);
190 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
192 /* Run PCM ioctl ops */
193 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
194 unsigned cmd, void *arg)
196 if (substream->ops->ioctl)
197 return substream->ops->ioctl(substream, cmd, arg);
199 return snd_pcm_lib_ioctl(substream, cmd, arg);
202 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
204 struct snd_pcm *pcm = substream->pcm;
205 struct snd_pcm_str *pstr = substream->pstr;
207 memset(info, 0, sizeof(*info));
208 info->card = pcm->card->number;
209 info->device = pcm->device;
210 info->stream = substream->stream;
211 info->subdevice = substream->number;
212 strscpy(info->id, pcm->id, sizeof(info->id));
213 strscpy(info->name, pcm->name, sizeof(info->name));
214 info->dev_class = pcm->dev_class;
215 info->dev_subclass = pcm->dev_subclass;
216 info->subdevices_count = pstr->substream_count;
217 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
218 strscpy(info->subname, substream->name, sizeof(info->subname));
223 int snd_pcm_info_user(struct snd_pcm_substream *substream,
224 struct snd_pcm_info __user * _info)
226 struct snd_pcm_info *info;
229 info = kmalloc(sizeof(*info), GFP_KERNEL);
232 err = snd_pcm_info(substream, info);
234 if (copy_to_user(_info, info, sizeof(*info)))
241 /* macro for simplified cast */
242 #define PARAM_MASK_BIT(b) (1U << (__force int)(b))
244 static bool hw_support_mmap(struct snd_pcm_substream *substream)
246 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
249 if (substream->ops->mmap || substream->ops->page)
252 switch (substream->dma_buffer.dev.type) {
253 case SNDRV_DMA_TYPE_UNKNOWN:
254 /* we can't know the device, so just assume that the driver does
258 case SNDRV_DMA_TYPE_CONTINUOUS:
259 case SNDRV_DMA_TYPE_VMALLOC:
262 return dma_can_mmap(substream->dma_buffer.dev.dev);
266 static int constrain_mask_params(struct snd_pcm_substream *substream,
267 struct snd_pcm_hw_params *params)
269 struct snd_pcm_hw_constraints *constrs =
270 &substream->runtime->hw_constraints;
273 struct snd_mask old_mask;
276 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
277 m = hw_param_mask(params, k);
278 if (snd_mask_empty(m))
281 /* This parameter is not requested to change by a caller. */
282 if (!(params->rmask & PARAM_MASK_BIT(k)))
285 if (trace_hw_mask_param_enabled())
288 changed = snd_mask_refine(m, constrs_mask(constrs, k));
294 /* Set corresponding flag so that the caller gets it. */
295 trace_hw_mask_param(substream, k, 0, &old_mask, m);
296 params->cmask |= PARAM_MASK_BIT(k);
302 static int constrain_interval_params(struct snd_pcm_substream *substream,
303 struct snd_pcm_hw_params *params)
305 struct snd_pcm_hw_constraints *constrs =
306 &substream->runtime->hw_constraints;
307 struct snd_interval *i;
309 struct snd_interval old_interval;
312 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
313 i = hw_param_interval(params, k);
314 if (snd_interval_empty(i))
317 /* This parameter is not requested to change by a caller. */
318 if (!(params->rmask & PARAM_MASK_BIT(k)))
321 if (trace_hw_interval_param_enabled())
324 changed = snd_interval_refine(i, constrs_interval(constrs, k));
330 /* Set corresponding flag so that the caller gets it. */
331 trace_hw_interval_param(substream, k, 0, &old_interval, i);
332 params->cmask |= PARAM_MASK_BIT(k);
338 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
339 struct snd_pcm_hw_params *params)
341 struct snd_pcm_hw_constraints *constrs =
342 &substream->runtime->hw_constraints;
344 unsigned int *rstamps;
345 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
347 struct snd_pcm_hw_rule *r;
349 struct snd_mask old_mask;
350 struct snd_interval old_interval;
352 int changed, err = 0;
355 * Each application of rule has own sequence number.
357 * Each member of 'rstamps' array represents the sequence number of
358 * recent application of corresponding rule.
360 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
365 * Each member of 'vstamps' array represents the sequence number of
366 * recent application of rule in which corresponding parameters were
369 * In initial state, elements corresponding to parameters requested by
370 * a caller is 1. For unrequested parameters, corresponding members
371 * have 0 so that the parameters are never changed anymore.
373 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
374 vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
376 /* Due to the above design, actual sequence number starts at 2. */
379 /* Apply all rules in order. */
381 for (k = 0; k < constrs->rules_num; k++) {
382 r = &constrs->rules[k];
385 * Check condition bits of this rule. When the rule has
386 * some condition bits, parameter without the bits is
387 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
388 * is an example of the condition bits.
390 if (r->cond && !(r->cond & params->flags))
394 * The 'deps' array includes maximum four dependencies
395 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth
396 * member of this array is a sentinel and should be
399 * This rule should be processed in this time when dependent
400 * parameters were changed at former applications of the other
403 for (d = 0; r->deps[d] >= 0; d++) {
404 if (vstamps[r->deps[d]] > rstamps[k])
410 if (trace_hw_mask_param_enabled()) {
411 if (hw_is_mask(r->var))
412 old_mask = *hw_param_mask(params, r->var);
414 if (trace_hw_interval_param_enabled()) {
415 if (hw_is_interval(r->var))
416 old_interval = *hw_param_interval(params, r->var);
419 changed = r->func(params, r);
426 * When the parameter is changed, notify it to the caller
427 * by corresponding returned bit, then preparing for next
430 if (changed && r->var >= 0) {
431 if (hw_is_mask(r->var)) {
432 trace_hw_mask_param(substream, r->var,
434 hw_param_mask(params, r->var));
436 if (hw_is_interval(r->var)) {
437 trace_hw_interval_param(substream, r->var,
438 k + 1, &old_interval,
439 hw_param_interval(params, r->var));
442 params->cmask |= PARAM_MASK_BIT(r->var);
443 vstamps[r->var] = stamp;
447 rstamps[k] = stamp++;
450 /* Iterate to evaluate all rules till no parameters are changed. */
459 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
460 struct snd_pcm_hw_params *params)
462 const struct snd_interval *i;
463 const struct snd_mask *m;
466 if (!params->msbits) {
467 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
468 if (snd_interval_single(i))
469 params->msbits = snd_interval_value(i);
472 if (!params->rate_den) {
473 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
474 if (snd_interval_single(i)) {
475 params->rate_num = snd_interval_value(i);
476 params->rate_den = 1;
480 if (!params->fifo_size) {
481 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
482 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
483 if (snd_mask_single(m) && snd_interval_single(i)) {
484 err = snd_pcm_ops_ioctl(substream,
485 SNDRV_PCM_IOCTL1_FIFO_SIZE,
493 params->info = substream->runtime->hw.info;
494 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
495 SNDRV_PCM_INFO_DRAIN_TRIGGER);
496 if (!hw_support_mmap(substream))
497 params->info &= ~(SNDRV_PCM_INFO_MMAP |
498 SNDRV_PCM_INFO_MMAP_VALID);
504 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
505 struct snd_pcm_hw_params *params)
510 params->fifo_size = 0;
511 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
513 if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
514 params->rate_num = 0;
515 params->rate_den = 0;
518 err = constrain_mask_params(substream, params);
522 err = constrain_interval_params(substream, params);
526 err = constrain_params_by_rules(substream, params);
534 EXPORT_SYMBOL(snd_pcm_hw_refine);
536 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
537 struct snd_pcm_hw_params __user * _params)
539 struct snd_pcm_hw_params *params;
542 params = memdup_user(_params, sizeof(*params));
544 return PTR_ERR(params);
546 err = snd_pcm_hw_refine(substream, params);
550 err = fixup_unreferenced_params(substream, params);
554 if (copy_to_user(_params, params, sizeof(*params)))
561 static int period_to_usecs(struct snd_pcm_runtime *runtime)
566 return -1; /* invalid */
568 /* take 75% of period time as the deadline */
569 usecs = (750000 / runtime->rate) * runtime->period_size;
570 usecs += ((750000 % runtime->rate) * runtime->period_size) /
576 static void snd_pcm_set_state(struct snd_pcm_substream *substream,
577 snd_pcm_state_t state)
579 snd_pcm_stream_lock_irq(substream);
580 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
581 substream->runtime->status->state = state;
582 snd_pcm_stream_unlock_irq(substream);
585 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
588 #ifdef CONFIG_SND_PCM_TIMER
589 if (substream->timer)
590 snd_timer_notify(substream->timer, event,
591 &substream->runtime->trigger_tstamp);
595 void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq)
597 if (substream->runtime && substream->runtime->stop_operating) {
598 substream->runtime->stop_operating = false;
599 if (substream->ops && substream->ops->sync_stop)
600 substream->ops->sync_stop(substream);
601 else if (sync_irq && substream->pcm->card->sync_irq > 0)
602 synchronize_irq(substream->pcm->card->sync_irq);
607 * snd_pcm_hw_params_choose - choose a configuration defined by @params
609 * @params: the hw_params instance
611 * Choose one configuration from configuration space defined by @params.
612 * The configuration chosen is that obtained fixing in this order:
613 * first access, first format, first subformat, min channels,
614 * min rate, min period time, max buffer size, min tick time
616 * Return: Zero if successful, or a negative error code on failure.
618 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
619 struct snd_pcm_hw_params *params)
621 static const int vars[] = {
622 SNDRV_PCM_HW_PARAM_ACCESS,
623 SNDRV_PCM_HW_PARAM_FORMAT,
624 SNDRV_PCM_HW_PARAM_SUBFORMAT,
625 SNDRV_PCM_HW_PARAM_CHANNELS,
626 SNDRV_PCM_HW_PARAM_RATE,
627 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
628 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
629 SNDRV_PCM_HW_PARAM_TICK_TIME,
633 struct snd_mask old_mask;
634 struct snd_interval old_interval;
637 for (v = vars; *v != -1; v++) {
638 /* Keep old parameter to trace. */
639 if (trace_hw_mask_param_enabled()) {
641 old_mask = *hw_param_mask(params, *v);
643 if (trace_hw_interval_param_enabled()) {
644 if (hw_is_interval(*v))
645 old_interval = *hw_param_interval(params, *v);
647 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
648 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
650 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
656 /* Trace the changed parameter. */
657 if (hw_is_mask(*v)) {
658 trace_hw_mask_param(pcm, *v, 0, &old_mask,
659 hw_param_mask(params, *v));
661 if (hw_is_interval(*v)) {
662 trace_hw_interval_param(pcm, *v, 0, &old_interval,
663 hw_param_interval(params, *v));
670 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
671 struct snd_pcm_hw_params *params)
673 struct snd_pcm_runtime *runtime;
676 snd_pcm_uframes_t frames;
678 if (PCM_RUNTIME_CHECK(substream))
680 runtime = substream->runtime;
681 snd_pcm_stream_lock_irq(substream);
682 switch (runtime->status->state) {
683 case SNDRV_PCM_STATE_OPEN:
684 case SNDRV_PCM_STATE_SETUP:
685 case SNDRV_PCM_STATE_PREPARED:
688 snd_pcm_stream_unlock_irq(substream);
691 snd_pcm_stream_unlock_irq(substream);
692 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
693 if (!substream->oss.oss)
695 if (atomic_read(&substream->mmap_count))
698 snd_pcm_sync_stop(substream, true);
701 err = snd_pcm_hw_refine(substream, params);
705 err = snd_pcm_hw_params_choose(substream, params);
709 err = fixup_unreferenced_params(substream, params);
713 if (substream->managed_buffer_alloc) {
714 err = snd_pcm_lib_malloc_pages(substream,
715 params_buffer_bytes(params));
718 runtime->buffer_changed = err > 0;
721 if (substream->ops->hw_params != NULL) {
722 err = substream->ops->hw_params(substream, params);
727 runtime->access = params_access(params);
728 runtime->format = params_format(params);
729 runtime->subformat = params_subformat(params);
730 runtime->channels = params_channels(params);
731 runtime->rate = params_rate(params);
732 runtime->period_size = params_period_size(params);
733 runtime->periods = params_periods(params);
734 runtime->buffer_size = params_buffer_size(params);
735 runtime->info = params->info;
736 runtime->rate_num = params->rate_num;
737 runtime->rate_den = params->rate_den;
738 runtime->no_period_wakeup =
739 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
740 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
742 bits = snd_pcm_format_physical_width(runtime->format);
743 runtime->sample_bits = bits;
744 bits *= runtime->channels;
745 runtime->frame_bits = bits;
747 while (bits % 8 != 0) {
751 runtime->byte_align = bits / 8;
752 runtime->min_align = frames;
754 /* Default sw params */
755 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
756 runtime->period_step = 1;
757 runtime->control->avail_min = runtime->period_size;
758 runtime->start_threshold = 1;
759 runtime->stop_threshold = runtime->buffer_size;
760 runtime->silence_threshold = 0;
761 runtime->silence_size = 0;
762 runtime->boundary = runtime->buffer_size;
763 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
764 runtime->boundary *= 2;
766 /* clear the buffer for avoiding possible kernel info leaks */
767 if (runtime->dma_area && !substream->ops->copy_user) {
768 size_t size = runtime->dma_bytes;
770 if (runtime->info & SNDRV_PCM_INFO_MMAP)
771 size = PAGE_ALIGN(size);
772 memset(runtime->dma_area, 0, size);
775 snd_pcm_timer_resolution_change(substream);
776 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
778 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
779 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
780 if ((usecs = period_to_usecs(runtime)) >= 0)
781 cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
785 /* hardware might be unusable from this time,
786 so we force application to retry to set
787 the correct hardware parameter settings */
788 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
789 if (substream->ops->hw_free != NULL)
790 substream->ops->hw_free(substream);
791 if (substream->managed_buffer_alloc)
792 snd_pcm_lib_free_pages(substream);
796 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
797 struct snd_pcm_hw_params __user * _params)
799 struct snd_pcm_hw_params *params;
802 params = memdup_user(_params, sizeof(*params));
804 return PTR_ERR(params);
806 err = snd_pcm_hw_params(substream, params);
810 if (copy_to_user(_params, params, sizeof(*params)))
817 static int do_hw_free(struct snd_pcm_substream *substream)
821 snd_pcm_sync_stop(substream, true);
822 if (substream->ops->hw_free)
823 result = substream->ops->hw_free(substream);
824 if (substream->managed_buffer_alloc)
825 snd_pcm_lib_free_pages(substream);
829 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
831 struct snd_pcm_runtime *runtime;
834 if (PCM_RUNTIME_CHECK(substream))
836 runtime = substream->runtime;
837 snd_pcm_stream_lock_irq(substream);
838 switch (runtime->status->state) {
839 case SNDRV_PCM_STATE_SETUP:
840 case SNDRV_PCM_STATE_PREPARED:
843 snd_pcm_stream_unlock_irq(substream);
846 snd_pcm_stream_unlock_irq(substream);
847 if (atomic_read(&substream->mmap_count))
849 result = do_hw_free(substream);
850 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
851 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
855 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
856 struct snd_pcm_sw_params *params)
858 struct snd_pcm_runtime *runtime;
861 if (PCM_RUNTIME_CHECK(substream))
863 runtime = substream->runtime;
864 snd_pcm_stream_lock_irq(substream);
865 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
866 snd_pcm_stream_unlock_irq(substream);
869 snd_pcm_stream_unlock_irq(substream);
871 if (params->tstamp_mode < 0 ||
872 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
874 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
875 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
877 if (params->avail_min == 0)
879 if (params->silence_size >= runtime->boundary) {
880 if (params->silence_threshold != 0)
883 if (params->silence_size > params->silence_threshold)
885 if (params->silence_threshold > runtime->buffer_size)
889 snd_pcm_stream_lock_irq(substream);
890 runtime->tstamp_mode = params->tstamp_mode;
891 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
892 runtime->tstamp_type = params->tstamp_type;
893 runtime->period_step = params->period_step;
894 runtime->control->avail_min = params->avail_min;
895 runtime->start_threshold = params->start_threshold;
896 runtime->stop_threshold = params->stop_threshold;
897 runtime->silence_threshold = params->silence_threshold;
898 runtime->silence_size = params->silence_size;
899 params->boundary = runtime->boundary;
900 if (snd_pcm_running(substream)) {
901 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
902 runtime->silence_size > 0)
903 snd_pcm_playback_silence(substream, ULONG_MAX);
904 err = snd_pcm_update_state(substream, runtime);
906 snd_pcm_stream_unlock_irq(substream);
910 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
911 struct snd_pcm_sw_params __user * _params)
913 struct snd_pcm_sw_params params;
915 if (copy_from_user(¶ms, _params, sizeof(params)))
917 err = snd_pcm_sw_params(substream, ¶ms);
918 if (copy_to_user(_params, ¶ms, sizeof(params)))
923 static inline snd_pcm_uframes_t
924 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
926 snd_pcm_uframes_t delay;
928 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
929 delay = snd_pcm_playback_hw_avail(substream->runtime);
931 delay = snd_pcm_capture_avail(substream->runtime);
932 return delay + substream->runtime->delay;
935 int snd_pcm_status64(struct snd_pcm_substream *substream,
936 struct snd_pcm_status64 *status)
938 struct snd_pcm_runtime *runtime = substream->runtime;
940 snd_pcm_stream_lock_irq(substream);
942 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
943 &runtime->audio_tstamp_config);
945 /* backwards compatible behavior */
946 if (runtime->audio_tstamp_config.type_requested ==
947 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
948 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
949 runtime->audio_tstamp_config.type_requested =
950 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
952 runtime->audio_tstamp_config.type_requested =
953 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
954 runtime->audio_tstamp_report.valid = 0;
956 runtime->audio_tstamp_report.valid = 1;
958 status->state = runtime->status->state;
959 status->suspended_state = runtime->status->suspended_state;
960 if (status->state == SNDRV_PCM_STATE_OPEN)
962 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
963 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
964 if (snd_pcm_running(substream)) {
965 snd_pcm_update_hw_ptr(substream);
966 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
967 status->tstamp_sec = runtime->status->tstamp.tv_sec;
968 status->tstamp_nsec =
969 runtime->status->tstamp.tv_nsec;
970 status->driver_tstamp_sec =
971 runtime->driver_tstamp.tv_sec;
972 status->driver_tstamp_nsec =
973 runtime->driver_tstamp.tv_nsec;
974 status->audio_tstamp_sec =
975 runtime->status->audio_tstamp.tv_sec;
976 status->audio_tstamp_nsec =
977 runtime->status->audio_tstamp.tv_nsec;
978 if (runtime->audio_tstamp_report.valid == 1)
979 /* backwards compatibility, no report provided in COMPAT mode */
980 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
981 &status->audio_tstamp_accuracy,
982 &runtime->audio_tstamp_report);
987 /* get tstamp only in fallback mode and only if enabled */
988 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
989 struct timespec64 tstamp;
991 snd_pcm_gettime(runtime, &tstamp);
992 status->tstamp_sec = tstamp.tv_sec;
993 status->tstamp_nsec = tstamp.tv_nsec;
997 status->appl_ptr = runtime->control->appl_ptr;
998 status->hw_ptr = runtime->status->hw_ptr;
999 status->avail = snd_pcm_avail(substream);
1000 status->delay = snd_pcm_running(substream) ?
1001 snd_pcm_calc_delay(substream) : 0;
1002 status->avail_max = runtime->avail_max;
1003 status->overrange = runtime->overrange;
1004 runtime->avail_max = 0;
1005 runtime->overrange = 0;
1007 snd_pcm_stream_unlock_irq(substream);
1011 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
1012 struct snd_pcm_status64 __user * _status,
1015 struct snd_pcm_status64 status;
1018 memset(&status, 0, sizeof(status));
1020 * with extension, parameters are read/write,
1021 * get audio_tstamp_data from user,
1022 * ignore rest of status structure
1024 if (ext && get_user(status.audio_tstamp_data,
1025 (u32 __user *)(&_status->audio_tstamp_data)))
1027 res = snd_pcm_status64(substream, &status);
1030 if (copy_to_user(_status, &status, sizeof(status)))
1035 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1036 struct snd_pcm_status32 __user * _status,
1039 struct snd_pcm_status64 status64;
1040 struct snd_pcm_status32 status32;
1043 memset(&status64, 0, sizeof(status64));
1044 memset(&status32, 0, sizeof(status32));
1046 * with extension, parameters are read/write,
1047 * get audio_tstamp_data from user,
1048 * ignore rest of status structure
1050 if (ext && get_user(status64.audio_tstamp_data,
1051 (u32 __user *)(&_status->audio_tstamp_data)))
1053 res = snd_pcm_status64(substream, &status64);
1057 status32 = (struct snd_pcm_status32) {
1058 .state = status64.state,
1059 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1060 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1061 .tstamp_sec = status64.tstamp_sec,
1062 .tstamp_nsec = status64.tstamp_nsec,
1063 .appl_ptr = status64.appl_ptr,
1064 .hw_ptr = status64.hw_ptr,
1065 .delay = status64.delay,
1066 .avail = status64.avail,
1067 .avail_max = status64.avail_max,
1068 .overrange = status64.overrange,
1069 .suspended_state = status64.suspended_state,
1070 .audio_tstamp_data = status64.audio_tstamp_data,
1071 .audio_tstamp_sec = status64.audio_tstamp_sec,
1072 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1073 .driver_tstamp_sec = status64.audio_tstamp_sec,
1074 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1075 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1078 if (copy_to_user(_status, &status32, sizeof(status32)))
1084 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1085 struct snd_pcm_channel_info * info)
1087 struct snd_pcm_runtime *runtime;
1088 unsigned int channel;
1090 channel = info->channel;
1091 runtime = substream->runtime;
1092 snd_pcm_stream_lock_irq(substream);
1093 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1094 snd_pcm_stream_unlock_irq(substream);
1097 snd_pcm_stream_unlock_irq(substream);
1098 if (channel >= runtime->channels)
1100 memset(info, 0, sizeof(*info));
1101 info->channel = channel;
1102 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1105 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1106 struct snd_pcm_channel_info __user * _info)
1108 struct snd_pcm_channel_info info;
1111 if (copy_from_user(&info, _info, sizeof(info)))
1113 res = snd_pcm_channel_info(substream, &info);
1116 if (copy_to_user(_info, &info, sizeof(info)))
1121 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1123 struct snd_pcm_runtime *runtime = substream->runtime;
1124 if (runtime->trigger_master == NULL)
1126 if (runtime->trigger_master == substream) {
1127 if (!runtime->trigger_tstamp_latched)
1128 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1130 snd_pcm_trigger_tstamp(runtime->trigger_master);
1131 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1133 runtime->trigger_master = NULL;
1136 #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1139 int (*pre_action)(struct snd_pcm_substream *substream,
1140 snd_pcm_state_t state);
1141 int (*do_action)(struct snd_pcm_substream *substream,
1142 snd_pcm_state_t state);
1143 void (*undo_action)(struct snd_pcm_substream *substream,
1144 snd_pcm_state_t state);
1145 void (*post_action)(struct snd_pcm_substream *substream,
1146 snd_pcm_state_t state);
1150 * this functions is core for handling of linked stream
1151 * Note: the stream state might be changed also on failure
1152 * Note2: call with calling stream lock + link lock
1154 static int snd_pcm_action_group(const struct action_ops *ops,
1155 struct snd_pcm_substream *substream,
1156 snd_pcm_state_t state,
1159 struct snd_pcm_substream *s = NULL;
1160 struct snd_pcm_substream *s1;
1161 int res = 0, depth = 1;
1163 snd_pcm_group_for_each_entry(s, substream) {
1164 if (do_lock && s != substream) {
1165 if (s->pcm->nonatomic)
1166 mutex_lock_nested(&s->self_group.mutex, depth);
1168 spin_lock_nested(&s->self_group.lock, depth);
1171 res = ops->pre_action(s, state);
1175 snd_pcm_group_for_each_entry(s, substream) {
1176 res = ops->do_action(s, state);
1178 if (ops->undo_action) {
1179 snd_pcm_group_for_each_entry(s1, substream) {
1180 if (s1 == s) /* failed stream */
1182 ops->undo_action(s1, state);
1185 s = NULL; /* unlock all */
1189 snd_pcm_group_for_each_entry(s, substream) {
1190 ops->post_action(s, state);
1194 /* unlock streams */
1195 snd_pcm_group_for_each_entry(s1, substream) {
1196 if (s1 != substream) {
1197 if (s1->pcm->nonatomic)
1198 mutex_unlock(&s1->self_group.mutex);
1200 spin_unlock(&s1->self_group.lock);
1202 if (s1 == s) /* end */
1210 * Note: call with stream lock
1212 static int snd_pcm_action_single(const struct action_ops *ops,
1213 struct snd_pcm_substream *substream,
1214 snd_pcm_state_t state)
1218 res = ops->pre_action(substream, state);
1221 res = ops->do_action(substream, state);
1223 ops->post_action(substream, state);
1224 else if (ops->undo_action)
1225 ops->undo_action(substream, state);
1229 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1230 struct snd_pcm_group *new_group)
1232 substream->group = new_group;
1233 list_move(&substream->link_list, &new_group->substreams);
1237 * Unref and unlock the group, but keep the stream lock;
1238 * when the group becomes empty and no longer referred, destroy itself
1240 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1241 struct snd_pcm_substream *substream)
1247 do_free = refcount_dec_and_test(&group->refs);
1248 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1254 * Lock the group inside a stream lock and reference it;
1255 * return the locked group object, or NULL if not linked
1257 static struct snd_pcm_group *
1258 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1260 bool nonatomic = substream->pcm->nonatomic;
1261 struct snd_pcm_group *group;
1265 if (!snd_pcm_stream_linked(substream))
1267 group = substream->group;
1268 /* block freeing the group object */
1269 refcount_inc(&group->refs);
1271 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1272 spin_trylock(&group->lock);
1276 /* re-lock for avoiding ABBA deadlock */
1277 snd_pcm_stream_unlock(substream);
1278 snd_pcm_group_lock(group, nonatomic);
1279 snd_pcm_stream_lock(substream);
1281 /* check the group again; the above opens a small race window */
1282 if (substream->group == group)
1284 /* group changed, try again */
1285 snd_pcm_group_unref(group, substream);
1291 * Note: call with stream lock
1293 static int snd_pcm_action(const struct action_ops *ops,
1294 struct snd_pcm_substream *substream,
1295 snd_pcm_state_t state)
1297 struct snd_pcm_group *group;
1300 group = snd_pcm_stream_group_ref(substream);
1302 res = snd_pcm_action_group(ops, substream, state, true);
1304 res = snd_pcm_action_single(ops, substream, state);
1305 snd_pcm_group_unref(group, substream);
1310 * Note: don't use any locks before
1312 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1313 struct snd_pcm_substream *substream,
1314 snd_pcm_state_t state)
1318 snd_pcm_stream_lock_irq(substream);
1319 res = snd_pcm_action(ops, substream, state);
1320 snd_pcm_stream_unlock_irq(substream);
1326 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1327 struct snd_pcm_substream *substream,
1328 snd_pcm_state_t state)
1332 /* Guarantee the group members won't change during non-atomic action */
1333 down_read(&snd_pcm_link_rwsem);
1334 if (snd_pcm_stream_linked(substream))
1335 res = snd_pcm_action_group(ops, substream, state, false);
1337 res = snd_pcm_action_single(ops, substream, state);
1338 up_read(&snd_pcm_link_rwsem);
1345 static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1346 snd_pcm_state_t state)
1348 struct snd_pcm_runtime *runtime = substream->runtime;
1349 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1352 !snd_pcm_playback_data(substream))
1354 runtime->trigger_tstamp_latched = false;
1355 runtime->trigger_master = substream;
1359 static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1360 snd_pcm_state_t state)
1362 if (substream->runtime->trigger_master != substream)
1364 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1367 static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1368 snd_pcm_state_t state)
1370 if (substream->runtime->trigger_master == substream)
1371 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1374 static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1375 snd_pcm_state_t state)
1377 struct snd_pcm_runtime *runtime = substream->runtime;
1378 snd_pcm_trigger_tstamp(substream);
1379 runtime->hw_ptr_jiffies = jiffies;
1380 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1382 runtime->status->state = state;
1383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1384 runtime->silence_size > 0)
1385 snd_pcm_playback_silence(substream, ULONG_MAX);
1386 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1389 static const struct action_ops snd_pcm_action_start = {
1390 .pre_action = snd_pcm_pre_start,
1391 .do_action = snd_pcm_do_start,
1392 .undo_action = snd_pcm_undo_start,
1393 .post_action = snd_pcm_post_start
1397 * snd_pcm_start - start all linked streams
1398 * @substream: the PCM substream instance
1400 * Return: Zero if successful, or a negative error code.
1401 * The stream lock must be acquired before calling this function.
1403 int snd_pcm_start(struct snd_pcm_substream *substream)
1405 return snd_pcm_action(&snd_pcm_action_start, substream,
1406 SNDRV_PCM_STATE_RUNNING);
1409 /* take the stream lock and start the streams */
1410 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1412 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1413 SNDRV_PCM_STATE_RUNNING);
1419 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1420 snd_pcm_state_t state)
1422 struct snd_pcm_runtime *runtime = substream->runtime;
1423 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1425 runtime->trigger_master = substream;
1429 static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1430 snd_pcm_state_t state)
1432 if (substream->runtime->trigger_master == substream &&
1433 snd_pcm_running(substream)) {
1434 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1435 substream->runtime->stop_operating = true;
1437 return 0; /* unconditionally stop all substreams */
1440 static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1441 snd_pcm_state_t state)
1443 struct snd_pcm_runtime *runtime = substream->runtime;
1444 if (runtime->status->state != state) {
1445 snd_pcm_trigger_tstamp(substream);
1446 runtime->status->state = state;
1447 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1449 wake_up(&runtime->sleep);
1450 wake_up(&runtime->tsleep);
1453 static const struct action_ops snd_pcm_action_stop = {
1454 .pre_action = snd_pcm_pre_stop,
1455 .do_action = snd_pcm_do_stop,
1456 .post_action = snd_pcm_post_stop
1460 * snd_pcm_stop - try to stop all running streams in the substream group
1461 * @substream: the PCM substream instance
1462 * @state: PCM state after stopping the stream
1464 * The state of each stream is then changed to the given state unconditionally.
1466 * Return: Zero if successful, or a negative error code.
1468 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1470 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1472 EXPORT_SYMBOL(snd_pcm_stop);
1475 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1476 * @substream: the PCM substream
1478 * After stopping, the state is changed to SETUP.
1479 * Unlike snd_pcm_stop(), this affects only the given stream.
1481 * Return: Zero if successful, or a negative error code.
1483 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1485 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1486 SNDRV_PCM_STATE_SETUP);
1490 * snd_pcm_stop_xrun - stop the running streams as XRUN
1491 * @substream: the PCM substream instance
1493 * This stops the given running substream (and all linked substreams) as XRUN.
1494 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1496 * Return: Zero if successful, or a negative error code.
1498 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1500 unsigned long flags;
1502 snd_pcm_stream_lock_irqsave(substream, flags);
1503 if (substream->runtime && snd_pcm_running(substream))
1504 __snd_pcm_xrun(substream);
1505 snd_pcm_stream_unlock_irqrestore(substream, flags);
1508 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1511 * pause callbacks: pass boolean (to start pause or resume) as state argument
1513 #define pause_pushed(state) (__force bool)(state)
1515 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1516 snd_pcm_state_t state)
1518 struct snd_pcm_runtime *runtime = substream->runtime;
1519 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1521 if (pause_pushed(state)) {
1522 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1524 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1526 runtime->trigger_master = substream;
1530 static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1531 snd_pcm_state_t state)
1533 if (substream->runtime->trigger_master != substream)
1535 /* some drivers might use hw_ptr to recover from the pause -
1536 update the hw_ptr now */
1537 if (pause_pushed(state))
1538 snd_pcm_update_hw_ptr(substream);
1539 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1540 * a delta between the current jiffies, this gives a large enough
1541 * delta, effectively to skip the check once.
1543 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1544 return substream->ops->trigger(substream,
1545 pause_pushed(state) ?
1546 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1547 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1550 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1551 snd_pcm_state_t state)
1553 if (substream->runtime->trigger_master == substream)
1554 substream->ops->trigger(substream,
1555 pause_pushed(state) ?
1556 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1557 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1560 static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1561 snd_pcm_state_t state)
1563 struct snd_pcm_runtime *runtime = substream->runtime;
1564 snd_pcm_trigger_tstamp(substream);
1565 if (pause_pushed(state)) {
1566 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1567 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1568 wake_up(&runtime->sleep);
1569 wake_up(&runtime->tsleep);
1571 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1572 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1576 static const struct action_ops snd_pcm_action_pause = {
1577 .pre_action = snd_pcm_pre_pause,
1578 .do_action = snd_pcm_do_pause,
1579 .undo_action = snd_pcm_undo_pause,
1580 .post_action = snd_pcm_post_pause
1584 * Push/release the pause for all linked streams.
1586 static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1588 return snd_pcm_action(&snd_pcm_action_pause, substream,
1589 (__force snd_pcm_state_t)push);
1592 static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1595 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1596 (__force snd_pcm_state_t)push);
1600 /* suspend callback: state argument ignored */
1602 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1603 snd_pcm_state_t state)
1605 struct snd_pcm_runtime *runtime = substream->runtime;
1606 switch (runtime->status->state) {
1607 case SNDRV_PCM_STATE_SUSPENDED:
1609 /* unresumable PCM state; return -EBUSY for skipping suspend */
1610 case SNDRV_PCM_STATE_OPEN:
1611 case SNDRV_PCM_STATE_SETUP:
1612 case SNDRV_PCM_STATE_DISCONNECTED:
1615 runtime->trigger_master = substream;
1619 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1620 snd_pcm_state_t state)
1622 struct snd_pcm_runtime *runtime = substream->runtime;
1623 if (runtime->trigger_master != substream)
1625 if (! snd_pcm_running(substream))
1627 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1628 runtime->stop_operating = true;
1629 return 0; /* suspend unconditionally */
1632 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1633 snd_pcm_state_t state)
1635 struct snd_pcm_runtime *runtime = substream->runtime;
1636 snd_pcm_trigger_tstamp(substream);
1637 runtime->status->suspended_state = runtime->status->state;
1638 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1639 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1640 wake_up(&runtime->sleep);
1641 wake_up(&runtime->tsleep);
1644 static const struct action_ops snd_pcm_action_suspend = {
1645 .pre_action = snd_pcm_pre_suspend,
1646 .do_action = snd_pcm_do_suspend,
1647 .post_action = snd_pcm_post_suspend
1651 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1652 * @substream: the PCM substream
1654 * After this call, all streams are changed to SUSPENDED state.
1656 * Return: Zero if successful, or a negative error code.
1658 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1661 unsigned long flags;
1663 snd_pcm_stream_lock_irqsave(substream, flags);
1664 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1666 snd_pcm_stream_unlock_irqrestore(substream, flags);
1671 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1672 * @pcm: the PCM instance
1674 * After this call, all streams are changed to SUSPENDED state.
1676 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1678 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1680 struct snd_pcm_substream *substream;
1681 int stream, err = 0;
1686 for_each_pcm_substream(pcm, stream, substream) {
1687 /* FIXME: the open/close code should lock this as well */
1688 if (!substream->runtime)
1692 * Skip BE dai link PCM's that are internal and may
1693 * not have their substream ops set.
1695 if (!substream->ops)
1698 err = snd_pcm_suspend(substream);
1699 if (err < 0 && err != -EBUSY)
1703 for_each_pcm_substream(pcm, stream, substream)
1704 snd_pcm_sync_stop(substream, false);
1708 EXPORT_SYMBOL(snd_pcm_suspend_all);
1710 /* resume callbacks: state argument ignored */
1712 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1713 snd_pcm_state_t state)
1715 struct snd_pcm_runtime *runtime = substream->runtime;
1716 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1718 runtime->trigger_master = substream;
1722 static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1723 snd_pcm_state_t state)
1725 struct snd_pcm_runtime *runtime = substream->runtime;
1726 if (runtime->trigger_master != substream)
1728 /* DMA not running previously? */
1729 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1730 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1731 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1733 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1736 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1737 snd_pcm_state_t state)
1739 if (substream->runtime->trigger_master == substream &&
1740 snd_pcm_running(substream))
1741 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1744 static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1745 snd_pcm_state_t state)
1747 struct snd_pcm_runtime *runtime = substream->runtime;
1748 snd_pcm_trigger_tstamp(substream);
1749 runtime->status->state = runtime->status->suspended_state;
1750 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1753 static const struct action_ops snd_pcm_action_resume = {
1754 .pre_action = snd_pcm_pre_resume,
1755 .do_action = snd_pcm_do_resume,
1756 .undo_action = snd_pcm_undo_resume,
1757 .post_action = snd_pcm_post_resume
1760 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1762 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1768 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1773 #endif /* CONFIG_PM */
1778 * Change the RUNNING stream(s) to XRUN state.
1780 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1782 struct snd_pcm_runtime *runtime = substream->runtime;
1785 snd_pcm_stream_lock_irq(substream);
1786 switch (runtime->status->state) {
1787 case SNDRV_PCM_STATE_XRUN:
1788 result = 0; /* already there */
1790 case SNDRV_PCM_STATE_RUNNING:
1791 __snd_pcm_xrun(substream);
1797 snd_pcm_stream_unlock_irq(substream);
1804 /* reset callbacks: state argument ignored */
1805 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1806 snd_pcm_state_t state)
1808 struct snd_pcm_runtime *runtime = substream->runtime;
1809 switch (runtime->status->state) {
1810 case SNDRV_PCM_STATE_RUNNING:
1811 case SNDRV_PCM_STATE_PREPARED:
1812 case SNDRV_PCM_STATE_PAUSED:
1813 case SNDRV_PCM_STATE_SUSPENDED:
1820 static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1821 snd_pcm_state_t state)
1823 struct snd_pcm_runtime *runtime = substream->runtime;
1824 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1827 runtime->hw_ptr_base = 0;
1828 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1829 runtime->status->hw_ptr % runtime->period_size;
1830 runtime->silence_start = runtime->status->hw_ptr;
1831 runtime->silence_filled = 0;
1835 static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1836 snd_pcm_state_t state)
1838 struct snd_pcm_runtime *runtime = substream->runtime;
1839 runtime->control->appl_ptr = runtime->status->hw_ptr;
1840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1841 runtime->silence_size > 0)
1842 snd_pcm_playback_silence(substream, ULONG_MAX);
1845 static const struct action_ops snd_pcm_action_reset = {
1846 .pre_action = snd_pcm_pre_reset,
1847 .do_action = snd_pcm_do_reset,
1848 .post_action = snd_pcm_post_reset
1851 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1853 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1860 /* pass f_flags as state argument */
1861 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1862 snd_pcm_state_t state)
1864 struct snd_pcm_runtime *runtime = substream->runtime;
1865 int f_flags = (__force int)state;
1867 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1868 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1870 if (snd_pcm_running(substream))
1872 substream->f_flags = f_flags;
1876 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1877 snd_pcm_state_t state)
1880 snd_pcm_sync_stop(substream, true);
1881 err = substream->ops->prepare(substream);
1884 return snd_pcm_do_reset(substream, state);
1887 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1888 snd_pcm_state_t state)
1890 struct snd_pcm_runtime *runtime = substream->runtime;
1891 runtime->control->appl_ptr = runtime->status->hw_ptr;
1892 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1895 static const struct action_ops snd_pcm_action_prepare = {
1896 .pre_action = snd_pcm_pre_prepare,
1897 .do_action = snd_pcm_do_prepare,
1898 .post_action = snd_pcm_post_prepare
1902 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1903 * @substream: the PCM substream instance
1904 * @file: file to refer f_flags
1906 * Return: Zero if successful, or a negative error code.
1908 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1914 f_flags = file->f_flags;
1916 f_flags = substream->f_flags;
1918 snd_pcm_stream_lock_irq(substream);
1919 switch (substream->runtime->status->state) {
1920 case SNDRV_PCM_STATE_PAUSED:
1921 snd_pcm_pause(substream, false);
1923 case SNDRV_PCM_STATE_SUSPENDED:
1924 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1927 snd_pcm_stream_unlock_irq(substream);
1929 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1931 (__force snd_pcm_state_t)f_flags);
1938 /* drain init callbacks: state argument ignored */
1939 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1940 snd_pcm_state_t state)
1942 struct snd_pcm_runtime *runtime = substream->runtime;
1943 switch (runtime->status->state) {
1944 case SNDRV_PCM_STATE_OPEN:
1945 case SNDRV_PCM_STATE_DISCONNECTED:
1946 case SNDRV_PCM_STATE_SUSPENDED:
1949 runtime->trigger_master = substream;
1953 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1954 snd_pcm_state_t state)
1956 struct snd_pcm_runtime *runtime = substream->runtime;
1957 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1958 switch (runtime->status->state) {
1959 case SNDRV_PCM_STATE_PREPARED:
1960 /* start playback stream if possible */
1961 if (! snd_pcm_playback_empty(substream)) {
1962 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1963 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1965 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1968 case SNDRV_PCM_STATE_RUNNING:
1969 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1971 case SNDRV_PCM_STATE_XRUN:
1972 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1978 /* stop running stream */
1979 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1980 snd_pcm_state_t new_state;
1982 new_state = snd_pcm_capture_avail(runtime) > 0 ?
1983 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1984 snd_pcm_do_stop(substream, new_state);
1985 snd_pcm_post_stop(substream, new_state);
1989 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1990 runtime->trigger_master == substream &&
1991 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1992 return substream->ops->trigger(substream,
1993 SNDRV_PCM_TRIGGER_DRAIN);
1998 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
1999 snd_pcm_state_t state)
2003 static const struct action_ops snd_pcm_action_drain_init = {
2004 .pre_action = snd_pcm_pre_drain_init,
2005 .do_action = snd_pcm_do_drain_init,
2006 .post_action = snd_pcm_post_drain_init
2010 * Drain the stream(s).
2011 * When the substream is linked, sync until the draining of all playback streams
2013 * After this call, all streams are supposed to be either SETUP or DRAINING
2014 * (capture only) state.
2016 static int snd_pcm_drain(struct snd_pcm_substream *substream,
2019 struct snd_card *card;
2020 struct snd_pcm_runtime *runtime;
2021 struct snd_pcm_substream *s;
2022 struct snd_pcm_group *group;
2023 wait_queue_entry_t wait;
2027 card = substream->pcm->card;
2028 runtime = substream->runtime;
2030 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2034 if (file->f_flags & O_NONBLOCK)
2036 } else if (substream->f_flags & O_NONBLOCK)
2039 snd_pcm_stream_lock_irq(substream);
2041 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2042 snd_pcm_pause(substream, false);
2044 /* pre-start/stop - all running streams are changed to DRAINING state */
2045 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2049 /* in non-blocking, we don't wait in ioctl but let caller poll */
2057 struct snd_pcm_runtime *to_check;
2058 if (signal_pending(current)) {
2059 result = -ERESTARTSYS;
2062 /* find a substream to drain */
2064 group = snd_pcm_stream_group_ref(substream);
2065 snd_pcm_group_for_each_entry(s, substream) {
2066 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2068 runtime = s->runtime;
2069 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2074 snd_pcm_group_unref(group, substream);
2076 break; /* all drained */
2077 init_waitqueue_entry(&wait, current);
2078 set_current_state(TASK_INTERRUPTIBLE);
2079 add_wait_queue(&to_check->sleep, &wait);
2080 snd_pcm_stream_unlock_irq(substream);
2081 if (runtime->no_period_wakeup)
2082 tout = MAX_SCHEDULE_TIMEOUT;
2085 if (runtime->rate) {
2086 long t = runtime->period_size * 2 / runtime->rate;
2087 tout = max(t, tout);
2089 tout = msecs_to_jiffies(tout * 1000);
2091 tout = schedule_timeout(tout);
2093 snd_pcm_stream_lock_irq(substream);
2094 group = snd_pcm_stream_group_ref(substream);
2095 snd_pcm_group_for_each_entry(s, substream) {
2096 if (s->runtime == to_check) {
2097 remove_wait_queue(&to_check->sleep, &wait);
2101 snd_pcm_group_unref(group, substream);
2103 if (card->shutdown) {
2108 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2111 dev_dbg(substream->pcm->card->dev,
2112 "playback drain error (DMA or IRQ trouble?)\n");
2113 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2121 snd_pcm_stream_unlock_irq(substream);
2129 * Immediately put all linked substreams into SETUP state.
2131 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2133 struct snd_pcm_runtime *runtime;
2136 if (PCM_RUNTIME_CHECK(substream))
2138 runtime = substream->runtime;
2140 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2141 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2144 snd_pcm_stream_lock_irq(substream);
2146 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2147 snd_pcm_pause(substream, false);
2149 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2150 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2151 snd_pcm_stream_unlock_irq(substream);
2157 static bool is_pcm_file(struct file *file)
2159 struct inode *inode = file_inode(file);
2160 struct snd_pcm *pcm;
2163 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2165 minor = iminor(inode);
2166 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2168 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2171 snd_card_unref(pcm->card);
2178 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2181 struct snd_pcm_file *pcm_file;
2182 struct snd_pcm_substream *substream1;
2183 struct snd_pcm_group *group, *target_group;
2184 bool nonatomic = substream->pcm->nonatomic;
2185 struct fd f = fdget(fd);
2189 if (!is_pcm_file(f.file)) {
2193 pcm_file = f.file->private_data;
2194 substream1 = pcm_file->substream;
2196 if (substream == substream1) {
2201 group = kzalloc(sizeof(*group), GFP_KERNEL);
2206 snd_pcm_group_init(group);
2208 down_write(&snd_pcm_link_rwsem);
2209 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2210 substream->runtime->status->state != substream1->runtime->status->state ||
2211 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2215 if (snd_pcm_stream_linked(substream1)) {
2220 snd_pcm_stream_lock_irq(substream);
2221 if (!snd_pcm_stream_linked(substream)) {
2222 snd_pcm_group_assign(substream, group);
2223 group = NULL; /* assigned, don't free this one below */
2225 target_group = substream->group;
2226 snd_pcm_stream_unlock_irq(substream);
2228 snd_pcm_group_lock_irq(target_group, nonatomic);
2229 snd_pcm_stream_lock_nested(substream1);
2230 snd_pcm_group_assign(substream1, target_group);
2231 refcount_inc(&target_group->refs);
2232 snd_pcm_stream_unlock(substream1);
2233 snd_pcm_group_unlock_irq(target_group, nonatomic);
2235 up_write(&snd_pcm_link_rwsem);
2243 static void relink_to_local(struct snd_pcm_substream *substream)
2245 snd_pcm_stream_lock_nested(substream);
2246 snd_pcm_group_assign(substream, &substream->self_group);
2247 snd_pcm_stream_unlock(substream);
2250 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2252 struct snd_pcm_group *group;
2253 bool nonatomic = substream->pcm->nonatomic;
2254 bool do_free = false;
2257 down_write(&snd_pcm_link_rwsem);
2259 if (!snd_pcm_stream_linked(substream)) {
2264 group = substream->group;
2265 snd_pcm_group_lock_irq(group, nonatomic);
2267 relink_to_local(substream);
2268 refcount_dec(&group->refs);
2270 /* detach the last stream, too */
2271 if (list_is_singular(&group->substreams)) {
2272 relink_to_local(list_first_entry(&group->substreams,
2273 struct snd_pcm_substream,
2275 do_free = refcount_dec_and_test(&group->refs);
2278 snd_pcm_group_unlock_irq(group, nonatomic);
2283 up_write(&snd_pcm_link_rwsem);
2290 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2291 struct snd_pcm_hw_rule *rule)
2293 struct snd_interval t;
2294 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2295 hw_param_interval_c(params, rule->deps[1]), &t);
2296 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2299 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2300 struct snd_pcm_hw_rule *rule)
2302 struct snd_interval t;
2303 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2304 hw_param_interval_c(params, rule->deps[1]), &t);
2305 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2308 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2309 struct snd_pcm_hw_rule *rule)
2311 struct snd_interval t;
2312 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2313 hw_param_interval_c(params, rule->deps[1]),
2314 (unsigned long) rule->private, &t);
2315 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2318 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2319 struct snd_pcm_hw_rule *rule)
2321 struct snd_interval t;
2322 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2323 (unsigned long) rule->private,
2324 hw_param_interval_c(params, rule->deps[1]), &t);
2325 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2328 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2329 struct snd_pcm_hw_rule *rule)
2332 const struct snd_interval *i =
2333 hw_param_interval_c(params, rule->deps[0]);
2335 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2337 pcm_for_each_format(k) {
2339 if (!snd_mask_test_format(mask, k))
2341 bits = snd_pcm_format_physical_width(k);
2343 continue; /* ignore invalid formats */
2344 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2345 snd_mask_reset(&m, (__force unsigned)k);
2347 return snd_mask_refine(mask, &m);
2350 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2351 struct snd_pcm_hw_rule *rule)
2353 struct snd_interval t;
2360 pcm_for_each_format(k) {
2362 if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2364 bits = snd_pcm_format_physical_width(k);
2366 continue; /* ignore invalid formats */
2367 if (t.min > (unsigned)bits)
2369 if (t.max < (unsigned)bits)
2373 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2376 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2377 #error "Change this table"
2380 static const unsigned int rates[] = {
2381 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2382 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2385 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2386 .count = ARRAY_SIZE(rates),
2390 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2391 struct snd_pcm_hw_rule *rule)
2393 struct snd_pcm_hardware *hw = rule->private;
2394 return snd_interval_list(hw_param_interval(params, rule->var),
2395 snd_pcm_known_rates.count,
2396 snd_pcm_known_rates.list, hw->rates);
2399 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2400 struct snd_pcm_hw_rule *rule)
2402 struct snd_interval t;
2403 struct snd_pcm_substream *substream = rule->private;
2405 t.max = substream->buffer_bytes_max;
2409 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2412 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2414 struct snd_pcm_runtime *runtime = substream->runtime;
2415 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2418 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2419 snd_mask_any(constrs_mask(constrs, k));
2422 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2423 snd_interval_any(constrs_interval(constrs, k));
2426 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2427 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2428 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2429 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2430 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2432 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2433 snd_pcm_hw_rule_format, NULL,
2434 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2437 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2438 snd_pcm_hw_rule_sample_bits, NULL,
2439 SNDRV_PCM_HW_PARAM_FORMAT,
2440 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2443 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2444 snd_pcm_hw_rule_div, NULL,
2445 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2448 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2449 snd_pcm_hw_rule_mul, NULL,
2450 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2453 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2454 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2455 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2458 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2459 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2460 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2463 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2464 snd_pcm_hw_rule_div, NULL,
2465 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2468 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2469 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2470 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2473 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2474 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2475 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2478 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2479 snd_pcm_hw_rule_div, NULL,
2480 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2483 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2484 snd_pcm_hw_rule_div, NULL,
2485 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2488 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2489 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2490 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2493 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2494 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2495 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2498 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2499 snd_pcm_hw_rule_mul, NULL,
2500 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2503 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2504 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2505 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2508 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2509 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2510 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2513 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2514 snd_pcm_hw_rule_muldivk, (void*) 8,
2515 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2518 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2519 snd_pcm_hw_rule_muldivk, (void*) 8,
2520 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2523 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2524 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2525 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2528 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2529 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2530 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2536 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2538 struct snd_pcm_runtime *runtime = substream->runtime;
2539 struct snd_pcm_hardware *hw = &runtime->hw;
2541 unsigned int mask = 0;
2543 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2544 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2545 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2546 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2547 if (hw_support_mmap(substream)) {
2548 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2549 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2550 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2551 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2552 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2553 mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2555 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2559 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2563 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2564 PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
2568 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2569 hw->channels_min, hw->channels_max);
2573 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2574 hw->rate_min, hw->rate_max);
2578 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2579 hw->period_bytes_min, hw->period_bytes_max);
2583 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2584 hw->periods_min, hw->periods_max);
2588 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2589 hw->period_bytes_min, hw->buffer_bytes_max);
2593 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2594 snd_pcm_hw_rule_buffer_bytes_max, substream,
2595 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2600 if (runtime->dma_bytes) {
2601 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2606 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2607 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2608 snd_pcm_hw_rule_rate, hw,
2609 SNDRV_PCM_HW_PARAM_RATE, -1);
2614 /* FIXME: this belong to lowlevel */
2615 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2620 static void pcm_release_private(struct snd_pcm_substream *substream)
2622 if (snd_pcm_stream_linked(substream))
2623 snd_pcm_unlink(substream);
2626 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2628 substream->ref_count--;
2629 if (substream->ref_count > 0)
2632 snd_pcm_drop(substream);
2633 if (substream->hw_opened) {
2634 if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2635 do_hw_free(substream);
2636 substream->ops->close(substream);
2637 substream->hw_opened = 0;
2639 if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2640 cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2641 if (substream->pcm_release) {
2642 substream->pcm_release(substream);
2643 substream->pcm_release = NULL;
2645 snd_pcm_detach_substream(substream);
2647 EXPORT_SYMBOL(snd_pcm_release_substream);
2649 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2651 struct snd_pcm_substream **rsubstream)
2653 struct snd_pcm_substream *substream;
2656 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2659 if (substream->ref_count > 1) {
2660 *rsubstream = substream;
2664 err = snd_pcm_hw_constraints_init(substream);
2666 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2670 if ((err = substream->ops->open(substream)) < 0)
2673 substream->hw_opened = 1;
2675 err = snd_pcm_hw_constraints_complete(substream);
2677 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2681 *rsubstream = substream;
2685 snd_pcm_release_substream(substream);
2688 EXPORT_SYMBOL(snd_pcm_open_substream);
2690 static int snd_pcm_open_file(struct file *file,
2691 struct snd_pcm *pcm,
2694 struct snd_pcm_file *pcm_file;
2695 struct snd_pcm_substream *substream;
2698 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2702 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2703 if (pcm_file == NULL) {
2704 snd_pcm_release_substream(substream);
2707 pcm_file->substream = substream;
2708 if (substream->ref_count == 1)
2709 substream->pcm_release = pcm_release_private;
2710 file->private_data = pcm_file;
2715 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2717 struct snd_pcm *pcm;
2718 int err = nonseekable_open(inode, file);
2721 pcm = snd_lookup_minor_data(iminor(inode),
2722 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2723 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2725 snd_card_unref(pcm->card);
2729 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2731 struct snd_pcm *pcm;
2732 int err = nonseekable_open(inode, file);
2735 pcm = snd_lookup_minor_data(iminor(inode),
2736 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2737 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2739 snd_card_unref(pcm->card);
2743 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2746 wait_queue_entry_t wait;
2752 err = snd_card_file_add(pcm->card, file);
2755 if (!try_module_get(pcm->card->module)) {
2759 init_waitqueue_entry(&wait, current);
2760 add_wait_queue(&pcm->open_wait, &wait);
2761 mutex_lock(&pcm->open_mutex);
2763 err = snd_pcm_open_file(file, pcm, stream);
2766 if (err == -EAGAIN) {
2767 if (file->f_flags & O_NONBLOCK) {
2773 set_current_state(TASK_INTERRUPTIBLE);
2774 mutex_unlock(&pcm->open_mutex);
2776 mutex_lock(&pcm->open_mutex);
2777 if (pcm->card->shutdown) {
2781 if (signal_pending(current)) {
2786 remove_wait_queue(&pcm->open_wait, &wait);
2787 mutex_unlock(&pcm->open_mutex);
2793 module_put(pcm->card->module);
2795 snd_card_file_remove(pcm->card, file);
2800 static int snd_pcm_release(struct inode *inode, struct file *file)
2802 struct snd_pcm *pcm;
2803 struct snd_pcm_substream *substream;
2804 struct snd_pcm_file *pcm_file;
2806 pcm_file = file->private_data;
2807 substream = pcm_file->substream;
2808 if (snd_BUG_ON(!substream))
2810 pcm = substream->pcm;
2811 mutex_lock(&pcm->open_mutex);
2812 snd_pcm_release_substream(substream);
2814 mutex_unlock(&pcm->open_mutex);
2815 wake_up(&pcm->open_wait);
2816 module_put(pcm->card->module);
2817 snd_card_file_remove(pcm->card, file);
2821 /* check and update PCM state; return 0 or a negative error
2822 * call this inside PCM lock
2824 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2826 switch (substream->runtime->status->state) {
2827 case SNDRV_PCM_STATE_DRAINING:
2828 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2831 case SNDRV_PCM_STATE_RUNNING:
2832 return snd_pcm_update_hw_ptr(substream);
2833 case SNDRV_PCM_STATE_PREPARED:
2834 case SNDRV_PCM_STATE_PAUSED:
2836 case SNDRV_PCM_STATE_SUSPENDED:
2838 case SNDRV_PCM_STATE_XRUN:
2845 /* increase the appl_ptr; returns the processed frames or a negative error */
2846 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2847 snd_pcm_uframes_t frames,
2848 snd_pcm_sframes_t avail)
2850 struct snd_pcm_runtime *runtime = substream->runtime;
2851 snd_pcm_sframes_t appl_ptr;
2856 if (frames > (snd_pcm_uframes_t)avail)
2858 appl_ptr = runtime->control->appl_ptr + frames;
2859 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2860 appl_ptr -= runtime->boundary;
2861 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2862 return ret < 0 ? ret : frames;
2865 /* decrease the appl_ptr; returns the processed frames or zero for error */
2866 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2867 snd_pcm_uframes_t frames,
2868 snd_pcm_sframes_t avail)
2870 struct snd_pcm_runtime *runtime = substream->runtime;
2871 snd_pcm_sframes_t appl_ptr;
2876 if (frames > (snd_pcm_uframes_t)avail)
2878 appl_ptr = runtime->control->appl_ptr - frames;
2880 appl_ptr += runtime->boundary;
2881 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2882 /* NOTE: we return zero for errors because PulseAudio gets depressed
2883 * upon receiving an error from rewind ioctl and stops processing
2884 * any longer. Returning zero means that no rewind is done, so
2885 * it's not absolutely wrong to answer like that.
2887 return ret < 0 ? 0 : frames;
2890 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2891 snd_pcm_uframes_t frames)
2893 snd_pcm_sframes_t ret;
2898 snd_pcm_stream_lock_irq(substream);
2899 ret = do_pcm_hwsync(substream);
2901 ret = rewind_appl_ptr(substream, frames,
2902 snd_pcm_hw_avail(substream));
2903 snd_pcm_stream_unlock_irq(substream);
2907 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2908 snd_pcm_uframes_t frames)
2910 snd_pcm_sframes_t ret;
2915 snd_pcm_stream_lock_irq(substream);
2916 ret = do_pcm_hwsync(substream);
2918 ret = forward_appl_ptr(substream, frames,
2919 snd_pcm_avail(substream));
2920 snd_pcm_stream_unlock_irq(substream);
2924 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2928 snd_pcm_stream_lock_irq(substream);
2929 err = do_pcm_hwsync(substream);
2930 snd_pcm_stream_unlock_irq(substream);
2934 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2935 snd_pcm_sframes_t *delay)
2938 snd_pcm_sframes_t n = 0;
2940 snd_pcm_stream_lock_irq(substream);
2941 err = do_pcm_hwsync(substream);
2943 n = snd_pcm_calc_delay(substream);
2944 snd_pcm_stream_unlock_irq(substream);
2950 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2951 struct snd_pcm_sync_ptr __user *_sync_ptr)
2953 struct snd_pcm_runtime *runtime = substream->runtime;
2954 struct snd_pcm_sync_ptr sync_ptr;
2955 volatile struct snd_pcm_mmap_status *status;
2956 volatile struct snd_pcm_mmap_control *control;
2959 memset(&sync_ptr, 0, sizeof(sync_ptr));
2960 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2962 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2964 status = runtime->status;
2965 control = runtime->control;
2966 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2967 err = snd_pcm_hwsync(substream);
2971 snd_pcm_stream_lock_irq(substream);
2972 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2973 err = pcm_lib_apply_appl_ptr(substream,
2974 sync_ptr.c.control.appl_ptr);
2976 snd_pcm_stream_unlock_irq(substream);
2980 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2982 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2983 control->avail_min = sync_ptr.c.control.avail_min;
2985 sync_ptr.c.control.avail_min = control->avail_min;
2986 sync_ptr.s.status.state = status->state;
2987 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2988 sync_ptr.s.status.tstamp = status->tstamp;
2989 sync_ptr.s.status.suspended_state = status->suspended_state;
2990 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2991 snd_pcm_stream_unlock_irq(substream);
2992 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2997 struct snd_pcm_mmap_status32 {
2998 snd_pcm_state_t state;
3003 snd_pcm_state_t suspended_state;
3004 s32 audio_tstamp_sec;
3005 s32 audio_tstamp_nsec;
3006 } __attribute__((packed));
3008 struct snd_pcm_mmap_control32 {
3013 struct snd_pcm_sync_ptr32 {
3016 struct snd_pcm_mmap_status32 status;
3017 unsigned char reserved[64];
3020 struct snd_pcm_mmap_control32 control;
3021 unsigned char reserved[64];
3023 } __attribute__((packed));
3025 /* recalcuate the boundary within 32bit */
3026 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
3028 snd_pcm_uframes_t boundary;
3030 if (! runtime->buffer_size)
3032 boundary = runtime->buffer_size;
3033 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3038 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3039 struct snd_pcm_sync_ptr32 __user *src)
3041 struct snd_pcm_runtime *runtime = substream->runtime;
3042 volatile struct snd_pcm_mmap_status *status;
3043 volatile struct snd_pcm_mmap_control *control;
3045 struct snd_pcm_mmap_control scontrol;
3046 struct snd_pcm_mmap_status sstatus;
3047 snd_pcm_uframes_t boundary;
3050 if (snd_BUG_ON(!runtime))
3053 if (get_user(sflags, &src->flags) ||
3054 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3055 get_user(scontrol.avail_min, &src->c.control.avail_min))
3057 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3058 err = snd_pcm_hwsync(substream);
3062 status = runtime->status;
3063 control = runtime->control;
3064 boundary = recalculate_boundary(runtime);
3066 boundary = 0x7fffffff;
3067 snd_pcm_stream_lock_irq(substream);
3068 /* FIXME: we should consider the boundary for the sync from app */
3069 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3070 err = pcm_lib_apply_appl_ptr(substream,
3073 snd_pcm_stream_unlock_irq(substream);
3077 scontrol.appl_ptr = control->appl_ptr % boundary;
3078 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3079 control->avail_min = scontrol.avail_min;
3081 scontrol.avail_min = control->avail_min;
3082 sstatus.state = status->state;
3083 sstatus.hw_ptr = status->hw_ptr % boundary;
3084 sstatus.tstamp = status->tstamp;
3085 sstatus.suspended_state = status->suspended_state;
3086 sstatus.audio_tstamp = status->audio_tstamp;
3087 snd_pcm_stream_unlock_irq(substream);
3088 if (put_user(sstatus.state, &src->s.status.state) ||
3089 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3090 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3091 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3092 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3093 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3094 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3095 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3096 put_user(scontrol.avail_min, &src->c.control.avail_min))
3101 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3103 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3105 struct snd_pcm_runtime *runtime = substream->runtime;
3108 if (get_user(arg, _arg))
3110 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3112 runtime->tstamp_type = arg;
3116 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3117 struct snd_xferi __user *_xferi)
3119 struct snd_xferi xferi;
3120 struct snd_pcm_runtime *runtime = substream->runtime;
3121 snd_pcm_sframes_t result;
3123 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3125 if (put_user(0, &_xferi->result))
3127 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3129 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3130 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3132 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3133 if (put_user(result, &_xferi->result))
3135 return result < 0 ? result : 0;
3138 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3139 struct snd_xfern __user *_xfern)
3141 struct snd_xfern xfern;
3142 struct snd_pcm_runtime *runtime = substream->runtime;
3144 snd_pcm_sframes_t result;
3146 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3148 if (runtime->channels > 128)
3150 if (put_user(0, &_xfern->result))
3152 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3155 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3157 return PTR_ERR(bufs);
3158 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3159 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3161 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3163 if (put_user(result, &_xfern->result))
3165 return result < 0 ? result : 0;
3168 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3169 snd_pcm_uframes_t __user *_frames)
3171 snd_pcm_uframes_t frames;
3172 snd_pcm_sframes_t result;
3174 if (get_user(frames, _frames))
3176 if (put_user(0, _frames))
3178 result = snd_pcm_rewind(substream, frames);
3179 if (put_user(result, _frames))
3181 return result < 0 ? result : 0;
3184 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3185 snd_pcm_uframes_t __user *_frames)
3187 snd_pcm_uframes_t frames;
3188 snd_pcm_sframes_t result;
3190 if (get_user(frames, _frames))
3192 if (put_user(0, _frames))
3194 result = snd_pcm_forward(substream, frames);
3195 if (put_user(result, _frames))
3197 return result < 0 ? result : 0;
3200 static int snd_pcm_common_ioctl(struct file *file,
3201 struct snd_pcm_substream *substream,
3202 unsigned int cmd, void __user *arg)
3204 struct snd_pcm_file *pcm_file = file->private_data;
3207 if (PCM_RUNTIME_CHECK(substream))
3210 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
3215 case SNDRV_PCM_IOCTL_PVERSION:
3216 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3217 case SNDRV_PCM_IOCTL_INFO:
3218 return snd_pcm_info_user(substream, arg);
3219 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3221 case SNDRV_PCM_IOCTL_TTSTAMP:
3222 return snd_pcm_tstamp(substream, arg);
3223 case SNDRV_PCM_IOCTL_USER_PVERSION:
3224 if (get_user(pcm_file->user_pversion,
3225 (unsigned int __user *)arg))
3228 case SNDRV_PCM_IOCTL_HW_REFINE:
3229 return snd_pcm_hw_refine_user(substream, arg);
3230 case SNDRV_PCM_IOCTL_HW_PARAMS:
3231 return snd_pcm_hw_params_user(substream, arg);
3232 case SNDRV_PCM_IOCTL_HW_FREE:
3233 return snd_pcm_hw_free(substream);
3234 case SNDRV_PCM_IOCTL_SW_PARAMS:
3235 return snd_pcm_sw_params_user(substream, arg);
3236 case SNDRV_PCM_IOCTL_STATUS32:
3237 return snd_pcm_status_user32(substream, arg, false);
3238 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3239 return snd_pcm_status_user32(substream, arg, true);
3240 case SNDRV_PCM_IOCTL_STATUS64:
3241 return snd_pcm_status_user64(substream, arg, false);
3242 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3243 return snd_pcm_status_user64(substream, arg, true);
3244 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3245 return snd_pcm_channel_info_user(substream, arg);
3246 case SNDRV_PCM_IOCTL_PREPARE:
3247 return snd_pcm_prepare(substream, file);
3248 case SNDRV_PCM_IOCTL_RESET:
3249 return snd_pcm_reset(substream);
3250 case SNDRV_PCM_IOCTL_START:
3251 return snd_pcm_start_lock_irq(substream);
3252 case SNDRV_PCM_IOCTL_LINK:
3253 return snd_pcm_link(substream, (int)(unsigned long) arg);
3254 case SNDRV_PCM_IOCTL_UNLINK:
3255 return snd_pcm_unlink(substream);
3256 case SNDRV_PCM_IOCTL_RESUME:
3257 return snd_pcm_resume(substream);
3258 case SNDRV_PCM_IOCTL_XRUN:
3259 return snd_pcm_xrun(substream);
3260 case SNDRV_PCM_IOCTL_HWSYNC:
3261 return snd_pcm_hwsync(substream);
3262 case SNDRV_PCM_IOCTL_DELAY:
3264 snd_pcm_sframes_t delay;
3265 snd_pcm_sframes_t __user *res = arg;
3268 err = snd_pcm_delay(substream, &delay);
3271 if (put_user(delay, res))
3275 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3276 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3277 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3278 return snd_pcm_sync_ptr(substream, arg);
3279 #ifdef CONFIG_SND_SUPPORT_OLD_API
3280 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3281 return snd_pcm_hw_refine_old_user(substream, arg);
3282 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3283 return snd_pcm_hw_params_old_user(substream, arg);
3285 case SNDRV_PCM_IOCTL_DRAIN:
3286 return snd_pcm_drain(substream, file);
3287 case SNDRV_PCM_IOCTL_DROP:
3288 return snd_pcm_drop(substream);
3289 case SNDRV_PCM_IOCTL_PAUSE:
3290 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3291 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3292 case SNDRV_PCM_IOCTL_READI_FRAMES:
3293 return snd_pcm_xferi_frames_ioctl(substream, arg);
3294 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3295 case SNDRV_PCM_IOCTL_READN_FRAMES:
3296 return snd_pcm_xfern_frames_ioctl(substream, arg);
3297 case SNDRV_PCM_IOCTL_REWIND:
3298 return snd_pcm_rewind_ioctl(substream, arg);
3299 case SNDRV_PCM_IOCTL_FORWARD:
3300 return snd_pcm_forward_ioctl(substream, arg);
3302 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3306 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3309 struct snd_pcm_file *pcm_file;
3311 pcm_file = file->private_data;
3313 if (((cmd >> 8) & 0xff) != 'A')
3316 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3317 (void __user *)arg);
3321 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3322 * @substream: PCM substream
3324 * @arg: IOCTL argument
3326 * The function is provided primarily for OSS layer and USB gadget drivers,
3327 * and it allows only the limited set of ioctls (hw_params, sw_params,
3328 * prepare, start, drain, drop, forward).
3330 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3331 unsigned int cmd, void *arg)
3333 snd_pcm_uframes_t *frames = arg;
3334 snd_pcm_sframes_t result;
3337 case SNDRV_PCM_IOCTL_FORWARD:
3339 /* provided only for OSS; capture-only and no value returned */
3340 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3342 result = snd_pcm_forward(substream, *frames);
3343 return result < 0 ? result : 0;
3345 case SNDRV_PCM_IOCTL_HW_PARAMS:
3346 return snd_pcm_hw_params(substream, arg);
3347 case SNDRV_PCM_IOCTL_SW_PARAMS:
3348 return snd_pcm_sw_params(substream, arg);
3349 case SNDRV_PCM_IOCTL_PREPARE:
3350 return snd_pcm_prepare(substream, NULL);
3351 case SNDRV_PCM_IOCTL_START:
3352 return snd_pcm_start_lock_irq(substream);
3353 case SNDRV_PCM_IOCTL_DRAIN:
3354 return snd_pcm_drain(substream, NULL);
3355 case SNDRV_PCM_IOCTL_DROP:
3356 return snd_pcm_drop(substream);
3357 case SNDRV_PCM_IOCTL_DELAY:
3358 return snd_pcm_delay(substream, frames);
3363 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3365 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3368 struct snd_pcm_file *pcm_file;
3369 struct snd_pcm_substream *substream;
3370 struct snd_pcm_runtime *runtime;
3371 snd_pcm_sframes_t result;
3373 pcm_file = file->private_data;
3374 substream = pcm_file->substream;
3375 if (PCM_RUNTIME_CHECK(substream))
3377 runtime = substream->runtime;
3378 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3380 if (!frame_aligned(runtime, count))
3382 count = bytes_to_frames(runtime, count);
3383 result = snd_pcm_lib_read(substream, buf, count);
3385 result = frames_to_bytes(runtime, result);
3389 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3390 size_t count, loff_t * offset)
3392 struct snd_pcm_file *pcm_file;
3393 struct snd_pcm_substream *substream;
3394 struct snd_pcm_runtime *runtime;
3395 snd_pcm_sframes_t result;
3397 pcm_file = file->private_data;
3398 substream = pcm_file->substream;
3399 if (PCM_RUNTIME_CHECK(substream))
3401 runtime = substream->runtime;
3402 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3404 if (!frame_aligned(runtime, count))
3406 count = bytes_to_frames(runtime, count);
3407 result = snd_pcm_lib_write(substream, buf, count);
3409 result = frames_to_bytes(runtime, result);
3413 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3415 struct snd_pcm_file *pcm_file;
3416 struct snd_pcm_substream *substream;
3417 struct snd_pcm_runtime *runtime;
3418 snd_pcm_sframes_t result;
3421 snd_pcm_uframes_t frames;
3423 pcm_file = iocb->ki_filp->private_data;
3424 substream = pcm_file->substream;
3425 if (PCM_RUNTIME_CHECK(substream))
3427 runtime = substream->runtime;
3428 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3430 if (!iter_is_iovec(to))
3432 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3434 if (!frame_aligned(runtime, to->iov->iov_len))
3436 frames = bytes_to_samples(runtime, to->iov->iov_len);
3437 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3440 for (i = 0; i < to->nr_segs; ++i)
3441 bufs[i] = to->iov[i].iov_base;
3442 result = snd_pcm_lib_readv(substream, bufs, frames);
3444 result = frames_to_bytes(runtime, result);
3449 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3451 struct snd_pcm_file *pcm_file;
3452 struct snd_pcm_substream *substream;
3453 struct snd_pcm_runtime *runtime;
3454 snd_pcm_sframes_t result;
3457 snd_pcm_uframes_t frames;
3459 pcm_file = iocb->ki_filp->private_data;
3460 substream = pcm_file->substream;
3461 if (PCM_RUNTIME_CHECK(substream))
3463 runtime = substream->runtime;
3464 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3466 if (!iter_is_iovec(from))
3468 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3469 !frame_aligned(runtime, from->iov->iov_len))
3471 frames = bytes_to_samples(runtime, from->iov->iov_len);
3472 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3475 for (i = 0; i < from->nr_segs; ++i)
3476 bufs[i] = from->iov[i].iov_base;
3477 result = snd_pcm_lib_writev(substream, bufs, frames);
3479 result = frames_to_bytes(runtime, result);
3484 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3486 struct snd_pcm_file *pcm_file;
3487 struct snd_pcm_substream *substream;
3488 struct snd_pcm_runtime *runtime;
3490 snd_pcm_uframes_t avail;
3492 pcm_file = file->private_data;
3494 substream = pcm_file->substream;
3495 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3496 ok = EPOLLOUT | EPOLLWRNORM;
3498 ok = EPOLLIN | EPOLLRDNORM;
3499 if (PCM_RUNTIME_CHECK(substream))
3500 return ok | EPOLLERR;
3502 runtime = substream->runtime;
3503 poll_wait(file, &runtime->sleep, wait);
3506 snd_pcm_stream_lock_irq(substream);
3507 avail = snd_pcm_avail(substream);
3508 switch (runtime->status->state) {
3509 case SNDRV_PCM_STATE_RUNNING:
3510 case SNDRV_PCM_STATE_PREPARED:
3511 case SNDRV_PCM_STATE_PAUSED:
3512 if (avail >= runtime->control->avail_min)
3515 case SNDRV_PCM_STATE_DRAINING:
3516 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3523 mask = ok | EPOLLERR;
3526 snd_pcm_stream_unlock_irq(substream);
3535 * Only on coherent architectures, we can mmap the status and the control records
3536 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3538 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3540 * mmap status record
3542 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3544 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3545 struct snd_pcm_runtime *runtime;
3547 if (substream == NULL)
3548 return VM_FAULT_SIGBUS;
3549 runtime = substream->runtime;
3550 vmf->page = virt_to_page(runtime->status);
3551 get_page(vmf->page);
3555 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3557 .fault = snd_pcm_mmap_status_fault,
3560 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3561 struct vm_area_struct *area)
3564 if (!(area->vm_flags & VM_READ))
3566 size = area->vm_end - area->vm_start;
3567 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3569 area->vm_ops = &snd_pcm_vm_ops_status;
3570 area->vm_private_data = substream;
3571 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3576 * mmap control record
3578 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3580 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3581 struct snd_pcm_runtime *runtime;
3583 if (substream == NULL)
3584 return VM_FAULT_SIGBUS;
3585 runtime = substream->runtime;
3586 vmf->page = virt_to_page(runtime->control);
3587 get_page(vmf->page);
3591 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3593 .fault = snd_pcm_mmap_control_fault,
3596 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3597 struct vm_area_struct *area)
3600 if (!(area->vm_flags & VM_READ))
3602 size = area->vm_end - area->vm_start;
3603 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3605 area->vm_ops = &snd_pcm_vm_ops_control;
3606 area->vm_private_data = substream;
3607 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3611 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3613 /* See pcm_control_mmap_allowed() below.
3614 * Since older alsa-lib requires both status and control mmaps to be
3615 * coupled, we have to disable the status mmap for old alsa-lib, too.
3617 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3618 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3623 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3625 if (pcm_file->no_compat_mmap)
3627 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3628 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3629 * thus it effectively assures the manual update of appl_ptr.
3631 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3636 #else /* ! coherent mmap */
3638 * don't support mmap for status and control records.
3640 #define pcm_status_mmap_allowed(pcm_file) false
3641 #define pcm_control_mmap_allowed(pcm_file) false
3643 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3644 struct vm_area_struct *area)
3648 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3649 struct vm_area_struct *area)
3653 #endif /* coherent mmap */
3655 static inline struct page *
3656 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3658 void *vaddr = substream->runtime->dma_area + ofs;
3660 switch (substream->dma_buffer.dev.type) {
3661 #ifdef CONFIG_SND_DMA_SGBUF
3662 case SNDRV_DMA_TYPE_DEV_SG:
3663 case SNDRV_DMA_TYPE_DEV_UC_SG:
3664 return snd_pcm_sgbuf_ops_page(substream, ofs);
3665 #endif /* CONFIG_SND_DMA_SGBUF */
3666 case SNDRV_DMA_TYPE_VMALLOC:
3667 return vmalloc_to_page(vaddr);
3669 return virt_to_page(vaddr);
3674 * fault callback for mmapping a RAM page
3676 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3678 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3679 struct snd_pcm_runtime *runtime;
3680 unsigned long offset;
3684 if (substream == NULL)
3685 return VM_FAULT_SIGBUS;
3686 runtime = substream->runtime;
3687 offset = vmf->pgoff << PAGE_SHIFT;
3688 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3689 if (offset > dma_bytes - PAGE_SIZE)
3690 return VM_FAULT_SIGBUS;
3691 if (substream->ops->page)
3692 page = substream->ops->page(substream, offset);
3694 page = snd_pcm_default_page_ops(substream, offset);
3696 return VM_FAULT_SIGBUS;
3702 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3703 .open = snd_pcm_mmap_data_open,
3704 .close = snd_pcm_mmap_data_close,
3707 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3708 .open = snd_pcm_mmap_data_open,
3709 .close = snd_pcm_mmap_data_close,
3710 .fault = snd_pcm_mmap_data_fault,
3714 * mmap the DMA buffer on RAM
3718 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3719 * @substream: PCM substream
3722 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3723 * this function is invoked implicitly.
3725 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3726 struct vm_area_struct *area)
3728 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3729 #ifdef CONFIG_GENERIC_ALLOCATOR
3730 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3731 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3732 return remap_pfn_range(area, area->vm_start,
3733 substream->dma_buffer.addr >> PAGE_SHIFT,
3734 area->vm_end - area->vm_start, area->vm_page_prot);
3736 #endif /* CONFIG_GENERIC_ALLOCATOR */
3737 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3738 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3739 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3740 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3742 substream->runtime->dma_area,
3743 substream->runtime->dma_addr,
3744 substream->runtime->dma_bytes);
3745 /* mmap with fault handler */
3746 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3749 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3752 * mmap the DMA buffer on I/O memory area
3754 #if SNDRV_PCM_INFO_MMAP_IOMEM
3756 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3757 * @substream: PCM substream
3760 * When your hardware uses the iomapped pages as the hardware buffer and
3761 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3762 * is supposed to work only on limited architectures.
3764 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3765 struct vm_area_struct *area)
3767 struct snd_pcm_runtime *runtime = substream->runtime;
3769 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3770 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3772 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3773 #endif /* SNDRV_PCM_INFO_MMAP */
3778 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3779 struct vm_area_struct *area)
3781 struct snd_pcm_runtime *runtime;
3783 unsigned long offset;
3787 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3788 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3791 if (!(area->vm_flags & VM_READ))
3794 runtime = substream->runtime;
3795 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3797 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3799 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3800 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3802 size = area->vm_end - area->vm_start;
3803 offset = area->vm_pgoff << PAGE_SHIFT;
3804 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3805 if ((size_t)size > dma_bytes)
3807 if (offset > dma_bytes - size)
3810 area->vm_ops = &snd_pcm_vm_ops_data;
3811 area->vm_private_data = substream;
3812 if (substream->ops->mmap)
3813 err = substream->ops->mmap(substream, area);
3815 err = snd_pcm_lib_default_mmap(substream, area);
3817 atomic_inc(&substream->mmap_count);
3820 EXPORT_SYMBOL(snd_pcm_mmap_data);
3822 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3824 struct snd_pcm_file * pcm_file;
3825 struct snd_pcm_substream *substream;
3826 unsigned long offset;
3828 pcm_file = file->private_data;
3829 substream = pcm_file->substream;
3830 if (PCM_RUNTIME_CHECK(substream))
3833 offset = area->vm_pgoff << PAGE_SHIFT;
3835 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3836 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3839 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3840 if (!pcm_status_mmap_allowed(pcm_file))
3842 return snd_pcm_mmap_status(substream, file, area);
3843 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3844 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3847 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3848 if (!pcm_control_mmap_allowed(pcm_file))
3850 return snd_pcm_mmap_control(substream, file, area);
3852 return snd_pcm_mmap_data(substream, file, area);
3857 static int snd_pcm_fasync(int fd, struct file * file, int on)
3859 struct snd_pcm_file * pcm_file;
3860 struct snd_pcm_substream *substream;
3861 struct snd_pcm_runtime *runtime;
3863 pcm_file = file->private_data;
3864 substream = pcm_file->substream;
3865 if (PCM_RUNTIME_CHECK(substream))
3867 runtime = substream->runtime;
3868 return fasync_helper(fd, file, on, &runtime->fasync);
3874 #ifdef CONFIG_COMPAT
3875 #include "pcm_compat.c"
3877 #define snd_pcm_ioctl_compat NULL
3881 * To be removed helpers to keep binary compatibility
3884 #ifdef CONFIG_SND_SUPPORT_OLD_API
3885 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3886 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3888 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3889 struct snd_pcm_hw_params_old *oparams)
3893 memset(params, 0, sizeof(*params));
3894 params->flags = oparams->flags;
3895 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3896 params->masks[i].bits[0] = oparams->masks[i];
3897 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3898 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3899 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3900 params->info = oparams->info;
3901 params->msbits = oparams->msbits;
3902 params->rate_num = oparams->rate_num;
3903 params->rate_den = oparams->rate_den;
3904 params->fifo_size = oparams->fifo_size;
3907 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3908 struct snd_pcm_hw_params *params)
3912 memset(oparams, 0, sizeof(*oparams));
3913 oparams->flags = params->flags;
3914 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3915 oparams->masks[i] = params->masks[i].bits[0];
3916 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3917 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3918 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3919 oparams->info = params->info;
3920 oparams->msbits = params->msbits;
3921 oparams->rate_num = params->rate_num;
3922 oparams->rate_den = params->rate_den;
3923 oparams->fifo_size = params->fifo_size;
3926 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3927 struct snd_pcm_hw_params_old __user * _oparams)
3929 struct snd_pcm_hw_params *params;
3930 struct snd_pcm_hw_params_old *oparams = NULL;
3933 params = kmalloc(sizeof(*params), GFP_KERNEL);
3937 oparams = memdup_user(_oparams, sizeof(*oparams));
3938 if (IS_ERR(oparams)) {
3939 err = PTR_ERR(oparams);
3942 snd_pcm_hw_convert_from_old_params(params, oparams);
3943 err = snd_pcm_hw_refine(substream, params);
3947 err = fixup_unreferenced_params(substream, params);
3951 snd_pcm_hw_convert_to_old_params(oparams, params);
3952 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3961 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3962 struct snd_pcm_hw_params_old __user * _oparams)
3964 struct snd_pcm_hw_params *params;
3965 struct snd_pcm_hw_params_old *oparams = NULL;
3968 params = kmalloc(sizeof(*params), GFP_KERNEL);
3972 oparams = memdup_user(_oparams, sizeof(*oparams));
3973 if (IS_ERR(oparams)) {
3974 err = PTR_ERR(oparams);
3978 snd_pcm_hw_convert_from_old_params(params, oparams);
3979 err = snd_pcm_hw_params(substream, params);
3983 snd_pcm_hw_convert_to_old_params(oparams, params);
3984 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3992 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3995 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3998 unsigned long pgoff,
3999 unsigned long flags)
4001 struct snd_pcm_file *pcm_file = file->private_data;
4002 struct snd_pcm_substream *substream = pcm_file->substream;
4003 struct snd_pcm_runtime *runtime = substream->runtime;
4004 unsigned long offset = pgoff << PAGE_SHIFT;
4007 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
4008 return (unsigned long)runtime->status;
4009 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
4010 return (unsigned long)runtime->control;
4012 return (unsigned long)runtime->dma_area + offset;
4016 # define snd_pcm_get_unmapped_area NULL
4023 const struct file_operations snd_pcm_f_ops[2] = {
4025 .owner = THIS_MODULE,
4026 .write = snd_pcm_write,
4027 .write_iter = snd_pcm_writev,
4028 .open = snd_pcm_playback_open,
4029 .release = snd_pcm_release,
4030 .llseek = no_llseek,
4031 .poll = snd_pcm_poll,
4032 .unlocked_ioctl = snd_pcm_ioctl,
4033 .compat_ioctl = snd_pcm_ioctl_compat,
4034 .mmap = snd_pcm_mmap,
4035 .fasync = snd_pcm_fasync,
4036 .get_unmapped_area = snd_pcm_get_unmapped_area,
4039 .owner = THIS_MODULE,
4040 .read = snd_pcm_read,
4041 .read_iter = snd_pcm_readv,
4042 .open = snd_pcm_capture_open,
4043 .release = snd_pcm_release,
4044 .llseek = no_llseek,
4045 .poll = snd_pcm_poll,
4046 .unlocked_ioctl = snd_pcm_ioctl,
4047 .compat_ioctl = snd_pcm_ioctl_compat,
4048 .mmap = snd_pcm_mmap,
4049 .fasync = snd_pcm_fasync,
4050 .get_unmapped_area = snd_pcm_get_unmapped_area,