1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/compat.h>
9 #include <linux/module.h>
10 #include <linux/file.h>
11 #include <linux/slab.h>
12 #include <linux/sched/signal.h>
13 #include <linux/time.h>
14 #include <linux/pm_qos.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/vmalloc.h>
18 #include <sound/core.h>
19 #include <sound/control.h>
20 #include <sound/info.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/timer.h>
24 #include <sound/minors.h>
25 #include <linux/uio.h>
26 #include <linux/delay.h>
28 #include "pcm_local.h"
30 #ifdef CONFIG_SND_DEBUG
31 #define CREATE_TRACE_POINTS
32 #include "pcm_param_trace.h"
34 #define trace_hw_mask_param_enabled() 0
35 #define trace_hw_interval_param_enabled() 0
36 #define trace_hw_mask_param(substream, type, index, prev, curr)
37 #define trace_hw_interval_param(substream, type, index, prev, curr)
44 struct snd_pcm_hw_params_old {
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
54 unsigned int rate_num;
55 unsigned int rate_den;
56 snd_pcm_uframes_t fifo_size;
57 unsigned char reserved[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
69 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem);
77 void snd_pcm_group_init(struct snd_pcm_group *group)
79 spin_lock_init(&group->lock);
80 mutex_init(&group->mutex);
81 INIT_LIST_HEAD(&group->substreams);
82 refcount_set(&group->refs, 1);
85 /* define group lock helpers */
86 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
87 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
90 mutex_ ## mutex_action(&group->mutex); \
92 spin_ ## action(&group->lock); \
95 DEFINE_PCM_GROUP_LOCK(lock, lock);
96 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
97 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
98 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
101 * snd_pcm_stream_lock - Lock the PCM stream
102 * @substream: PCM substream
104 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
105 * flag of the given substream. This also takes the global link rw lock
106 * (or rw sem), too, for avoiding the race with linked streams.
108 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
110 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
112 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
115 * snd_pcm_stream_lock - Unlock the PCM stream
116 * @substream: PCM substream
118 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
120 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
122 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
124 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
127 * snd_pcm_stream_lock_irq - Lock the PCM stream
128 * @substream: PCM substream
130 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
131 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
132 * as snd_pcm_stream_lock().
134 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
136 snd_pcm_group_lock_irq(&substream->self_group,
137 substream->pcm->nonatomic);
139 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
142 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
143 * @substream: PCM substream
145 * This is a counter-part of snd_pcm_stream_lock_irq().
147 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
149 snd_pcm_group_unlock_irq(&substream->self_group,
150 substream->pcm->nonatomic);
152 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
154 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
156 unsigned long flags = 0;
157 if (substream->pcm->nonatomic)
158 mutex_lock(&substream->self_group.mutex);
160 spin_lock_irqsave(&substream->self_group.lock, flags);
163 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
166 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
167 * @substream: PCM substream
170 * This is a counter-part of snd_pcm_stream_lock_irqsave().
172 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
175 if (substream->pcm->nonatomic)
176 mutex_unlock(&substream->self_group.mutex);
178 spin_unlock_irqrestore(&substream->self_group.lock, flags);
180 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
182 /* Run PCM ioctl ops */
183 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
184 unsigned cmd, void *arg)
186 if (substream->ops->ioctl)
187 return substream->ops->ioctl(substream, cmd, arg);
189 return snd_pcm_lib_ioctl(substream, cmd, arg);
192 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
194 struct snd_pcm *pcm = substream->pcm;
195 struct snd_pcm_str *pstr = substream->pstr;
197 memset(info, 0, sizeof(*info));
198 info->card = pcm->card->number;
199 info->device = pcm->device;
200 info->stream = substream->stream;
201 info->subdevice = substream->number;
202 strlcpy(info->id, pcm->id, sizeof(info->id));
203 strlcpy(info->name, pcm->name, sizeof(info->name));
204 info->dev_class = pcm->dev_class;
205 info->dev_subclass = pcm->dev_subclass;
206 info->subdevices_count = pstr->substream_count;
207 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
208 strlcpy(info->subname, substream->name, sizeof(info->subname));
213 int snd_pcm_info_user(struct snd_pcm_substream *substream,
214 struct snd_pcm_info __user * _info)
216 struct snd_pcm_info *info;
219 info = kmalloc(sizeof(*info), GFP_KERNEL);
222 err = snd_pcm_info(substream, info);
224 if (copy_to_user(_info, info, sizeof(*info)))
231 static bool hw_support_mmap(struct snd_pcm_substream *substream)
233 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
236 if (substream->ops->mmap ||
237 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
238 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
241 return dma_can_mmap(substream->dma_buffer.dev.dev);
244 static int constrain_mask_params(struct snd_pcm_substream *substream,
245 struct snd_pcm_hw_params *params)
247 struct snd_pcm_hw_constraints *constrs =
248 &substream->runtime->hw_constraints;
251 struct snd_mask old_mask;
254 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
255 m = hw_param_mask(params, k);
256 if (snd_mask_empty(m))
259 /* This parameter is not requested to change by a caller. */
260 if (!(params->rmask & (1 << k)))
263 if (trace_hw_mask_param_enabled())
266 changed = snd_mask_refine(m, constrs_mask(constrs, k));
272 /* Set corresponding flag so that the caller gets it. */
273 trace_hw_mask_param(substream, k, 0, &old_mask, m);
274 params->cmask |= 1 << k;
280 static int constrain_interval_params(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params)
283 struct snd_pcm_hw_constraints *constrs =
284 &substream->runtime->hw_constraints;
285 struct snd_interval *i;
287 struct snd_interval old_interval;
290 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
291 i = hw_param_interval(params, k);
292 if (snd_interval_empty(i))
295 /* This parameter is not requested to change by a caller. */
296 if (!(params->rmask & (1 << k)))
299 if (trace_hw_interval_param_enabled())
302 changed = snd_interval_refine(i, constrs_interval(constrs, k));
308 /* Set corresponding flag so that the caller gets it. */
309 trace_hw_interval_param(substream, k, 0, &old_interval, i);
310 params->cmask |= 1 << k;
316 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
317 struct snd_pcm_hw_params *params)
319 struct snd_pcm_hw_constraints *constrs =
320 &substream->runtime->hw_constraints;
322 unsigned int *rstamps;
323 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
325 struct snd_pcm_hw_rule *r;
327 struct snd_mask old_mask;
328 struct snd_interval old_interval;
330 int changed, err = 0;
333 * Each application of rule has own sequence number.
335 * Each member of 'rstamps' array represents the sequence number of
336 * recent application of corresponding rule.
338 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
343 * Each member of 'vstamps' array represents the sequence number of
344 * recent application of rule in which corresponding parameters were
347 * In initial state, elements corresponding to parameters requested by
348 * a caller is 1. For unrequested parameters, corresponding members
349 * have 0 so that the parameters are never changed anymore.
351 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
352 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
354 /* Due to the above design, actual sequence number starts at 2. */
357 /* Apply all rules in order. */
359 for (k = 0; k < constrs->rules_num; k++) {
360 r = &constrs->rules[k];
363 * Check condition bits of this rule. When the rule has
364 * some condition bits, parameter without the bits is
365 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
366 * is an example of the condition bits.
368 if (r->cond && !(r->cond & params->flags))
372 * The 'deps' array includes maximum three dependencies
373 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
374 * member of this array is a sentinel and should be
377 * This rule should be processed in this time when dependent
378 * parameters were changed at former applications of the other
381 for (d = 0; r->deps[d] >= 0; d++) {
382 if (vstamps[r->deps[d]] > rstamps[k])
388 if (trace_hw_mask_param_enabled()) {
389 if (hw_is_mask(r->var))
390 old_mask = *hw_param_mask(params, r->var);
392 if (trace_hw_interval_param_enabled()) {
393 if (hw_is_interval(r->var))
394 old_interval = *hw_param_interval(params, r->var);
397 changed = r->func(params, r);
404 * When the parameter is changed, notify it to the caller
405 * by corresponding returned bit, then preparing for next
408 if (changed && r->var >= 0) {
409 if (hw_is_mask(r->var)) {
410 trace_hw_mask_param(substream, r->var,
412 hw_param_mask(params, r->var));
414 if (hw_is_interval(r->var)) {
415 trace_hw_interval_param(substream, r->var,
416 k + 1, &old_interval,
417 hw_param_interval(params, r->var));
420 params->cmask |= (1 << r->var);
421 vstamps[r->var] = stamp;
425 rstamps[k] = stamp++;
428 /* Iterate to evaluate all rules till no parameters are changed. */
437 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
438 struct snd_pcm_hw_params *params)
440 const struct snd_interval *i;
441 const struct snd_mask *m;
444 if (!params->msbits) {
445 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
446 if (snd_interval_single(i))
447 params->msbits = snd_interval_value(i);
450 if (!params->rate_den) {
451 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
452 if (snd_interval_single(i)) {
453 params->rate_num = snd_interval_value(i);
454 params->rate_den = 1;
458 if (!params->fifo_size) {
459 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
460 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
461 if (snd_mask_single(m) && snd_interval_single(i)) {
462 err = snd_pcm_ops_ioctl(substream,
463 SNDRV_PCM_IOCTL1_FIFO_SIZE,
471 params->info = substream->runtime->hw.info;
472 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
473 SNDRV_PCM_INFO_DRAIN_TRIGGER);
474 if (!hw_support_mmap(substream))
475 params->info &= ~(SNDRV_PCM_INFO_MMAP |
476 SNDRV_PCM_INFO_MMAP_VALID);
482 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
483 struct snd_pcm_hw_params *params)
488 params->fifo_size = 0;
489 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
491 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
492 params->rate_num = 0;
493 params->rate_den = 0;
496 err = constrain_mask_params(substream, params);
500 err = constrain_interval_params(substream, params);
504 err = constrain_params_by_rules(substream, params);
512 EXPORT_SYMBOL(snd_pcm_hw_refine);
514 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
515 struct snd_pcm_hw_params __user * _params)
517 struct snd_pcm_hw_params *params;
520 params = memdup_user(_params, sizeof(*params));
522 return PTR_ERR(params);
524 err = snd_pcm_hw_refine(substream, params);
528 err = fixup_unreferenced_params(substream, params);
532 if (copy_to_user(_params, params, sizeof(*params)))
539 static int period_to_usecs(struct snd_pcm_runtime *runtime)
544 return -1; /* invalid */
546 /* take 75% of period time as the deadline */
547 usecs = (750000 / runtime->rate) * runtime->period_size;
548 usecs += ((750000 % runtime->rate) * runtime->period_size) /
554 static void snd_pcm_set_state(struct snd_pcm_substream *substream,
555 snd_pcm_state_t state)
557 snd_pcm_stream_lock_irq(substream);
558 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
559 substream->runtime->status->state = state;
560 snd_pcm_stream_unlock_irq(substream);
563 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
566 #ifdef CONFIG_SND_PCM_TIMER
567 if (substream->timer)
568 snd_timer_notify(substream->timer, event,
569 &substream->runtime->trigger_tstamp);
573 static void snd_pcm_sync_stop(struct snd_pcm_substream *substream)
575 if (substream->runtime->stop_operating) {
576 substream->runtime->stop_operating = false;
577 if (substream->ops->sync_stop)
578 substream->ops->sync_stop(substream);
579 else if (substream->pcm->card->sync_irq > 0)
580 synchronize_irq(substream->pcm->card->sync_irq);
585 * snd_pcm_hw_param_choose - choose a configuration defined by @params
587 * @params: the hw_params instance
589 * Choose one configuration from configuration space defined by @params.
590 * The configuration chosen is that obtained fixing in this order:
591 * first access, first format, first subformat, min channels,
592 * min rate, min period time, max buffer size, min tick time
594 * Return: Zero if successful, or a negative error code on failure.
596 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
597 struct snd_pcm_hw_params *params)
599 static const int vars[] = {
600 SNDRV_PCM_HW_PARAM_ACCESS,
601 SNDRV_PCM_HW_PARAM_FORMAT,
602 SNDRV_PCM_HW_PARAM_SUBFORMAT,
603 SNDRV_PCM_HW_PARAM_CHANNELS,
604 SNDRV_PCM_HW_PARAM_RATE,
605 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
606 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
607 SNDRV_PCM_HW_PARAM_TICK_TIME,
611 struct snd_mask old_mask;
612 struct snd_interval old_interval;
615 for (v = vars; *v != -1; v++) {
616 /* Keep old parameter to trace. */
617 if (trace_hw_mask_param_enabled()) {
619 old_mask = *hw_param_mask(params, *v);
621 if (trace_hw_interval_param_enabled()) {
622 if (hw_is_interval(*v))
623 old_interval = *hw_param_interval(params, *v);
625 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
626 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
628 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
634 /* Trace the changed parameter. */
635 if (hw_is_mask(*v)) {
636 trace_hw_mask_param(pcm, *v, 0, &old_mask,
637 hw_param_mask(params, *v));
639 if (hw_is_interval(*v)) {
640 trace_hw_interval_param(pcm, *v, 0, &old_interval,
641 hw_param_interval(params, *v));
648 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
649 struct snd_pcm_hw_params *params)
651 struct snd_pcm_runtime *runtime;
654 snd_pcm_uframes_t frames;
656 if (PCM_RUNTIME_CHECK(substream))
658 runtime = substream->runtime;
659 snd_pcm_stream_lock_irq(substream);
660 switch (runtime->status->state) {
661 case SNDRV_PCM_STATE_OPEN:
662 case SNDRV_PCM_STATE_SETUP:
663 case SNDRV_PCM_STATE_PREPARED:
666 snd_pcm_stream_unlock_irq(substream);
669 snd_pcm_stream_unlock_irq(substream);
670 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
671 if (!substream->oss.oss)
673 if (atomic_read(&substream->mmap_count))
676 snd_pcm_sync_stop(substream);
679 err = snd_pcm_hw_refine(substream, params);
683 err = snd_pcm_hw_params_choose(substream, params);
687 err = fixup_unreferenced_params(substream, params);
691 if (substream->managed_buffer_alloc) {
692 err = snd_pcm_lib_malloc_pages(substream,
693 params_buffer_bytes(params));
696 runtime->buffer_changed = err > 0;
699 if (substream->ops->hw_params != NULL) {
700 err = substream->ops->hw_params(substream, params);
705 runtime->access = params_access(params);
706 runtime->format = params_format(params);
707 runtime->subformat = params_subformat(params);
708 runtime->channels = params_channels(params);
709 runtime->rate = params_rate(params);
710 runtime->period_size = params_period_size(params);
711 runtime->periods = params_periods(params);
712 runtime->buffer_size = params_buffer_size(params);
713 runtime->info = params->info;
714 runtime->rate_num = params->rate_num;
715 runtime->rate_den = params->rate_den;
716 runtime->no_period_wakeup =
717 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
718 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
720 bits = snd_pcm_format_physical_width(runtime->format);
721 runtime->sample_bits = bits;
722 bits *= runtime->channels;
723 runtime->frame_bits = bits;
725 while (bits % 8 != 0) {
729 runtime->byte_align = bits / 8;
730 runtime->min_align = frames;
732 /* Default sw params */
733 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
734 runtime->period_step = 1;
735 runtime->control->avail_min = runtime->period_size;
736 runtime->start_threshold = 1;
737 runtime->stop_threshold = runtime->buffer_size;
738 runtime->silence_threshold = 0;
739 runtime->silence_size = 0;
740 runtime->boundary = runtime->buffer_size;
741 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
742 runtime->boundary *= 2;
744 /* clear the buffer for avoiding possible kernel info leaks */
745 if (runtime->dma_area && !substream->ops->copy_user)
746 memset(runtime->dma_area, 0, runtime->dma_bytes);
748 snd_pcm_timer_resolution_change(substream);
749 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
751 if (pm_qos_request_active(&substream->latency_pm_qos_req))
752 pm_qos_remove_request(&substream->latency_pm_qos_req);
753 if ((usecs = period_to_usecs(runtime)) >= 0)
754 pm_qos_add_request(&substream->latency_pm_qos_req,
755 PM_QOS_CPU_DMA_LATENCY, usecs);
758 /* hardware might be unusable from this time,
759 so we force application to retry to set
760 the correct hardware parameter settings */
761 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
762 if (substream->ops->hw_free != NULL)
763 substream->ops->hw_free(substream);
764 if (substream->managed_buffer_alloc)
765 snd_pcm_lib_free_pages(substream);
769 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
770 struct snd_pcm_hw_params __user * _params)
772 struct snd_pcm_hw_params *params;
775 params = memdup_user(_params, sizeof(*params));
777 return PTR_ERR(params);
779 err = snd_pcm_hw_params(substream, params);
783 if (copy_to_user(_params, params, sizeof(*params)))
790 static int do_hw_free(struct snd_pcm_substream *substream)
794 snd_pcm_sync_stop(substream);
795 if (substream->ops->hw_free)
796 result = substream->ops->hw_free(substream);
797 if (substream->managed_buffer_alloc)
798 snd_pcm_lib_free_pages(substream);
802 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
804 struct snd_pcm_runtime *runtime;
807 if (PCM_RUNTIME_CHECK(substream))
809 runtime = substream->runtime;
810 snd_pcm_stream_lock_irq(substream);
811 switch (runtime->status->state) {
812 case SNDRV_PCM_STATE_SETUP:
813 case SNDRV_PCM_STATE_PREPARED:
816 snd_pcm_stream_unlock_irq(substream);
819 snd_pcm_stream_unlock_irq(substream);
820 if (atomic_read(&substream->mmap_count))
822 result = do_hw_free(substream);
823 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
824 pm_qos_remove_request(&substream->latency_pm_qos_req);
828 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
829 struct snd_pcm_sw_params *params)
831 struct snd_pcm_runtime *runtime;
834 if (PCM_RUNTIME_CHECK(substream))
836 runtime = substream->runtime;
837 snd_pcm_stream_lock_irq(substream);
838 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
839 snd_pcm_stream_unlock_irq(substream);
842 snd_pcm_stream_unlock_irq(substream);
844 if (params->tstamp_mode < 0 ||
845 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
847 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
848 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
850 if (params->avail_min == 0)
852 if (params->silence_size >= runtime->boundary) {
853 if (params->silence_threshold != 0)
856 if (params->silence_size > params->silence_threshold)
858 if (params->silence_threshold > runtime->buffer_size)
862 snd_pcm_stream_lock_irq(substream);
863 runtime->tstamp_mode = params->tstamp_mode;
864 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
865 runtime->tstamp_type = params->tstamp_type;
866 runtime->period_step = params->period_step;
867 runtime->control->avail_min = params->avail_min;
868 runtime->start_threshold = params->start_threshold;
869 runtime->stop_threshold = params->stop_threshold;
870 runtime->silence_threshold = params->silence_threshold;
871 runtime->silence_size = params->silence_size;
872 params->boundary = runtime->boundary;
873 if (snd_pcm_running(substream)) {
874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
875 runtime->silence_size > 0)
876 snd_pcm_playback_silence(substream, ULONG_MAX);
877 err = snd_pcm_update_state(substream, runtime);
879 snd_pcm_stream_unlock_irq(substream);
883 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
884 struct snd_pcm_sw_params __user * _params)
886 struct snd_pcm_sw_params params;
888 if (copy_from_user(¶ms, _params, sizeof(params)))
890 err = snd_pcm_sw_params(substream, ¶ms);
891 if (copy_to_user(_params, ¶ms, sizeof(params)))
896 static inline snd_pcm_uframes_t
897 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
899 snd_pcm_uframes_t delay;
901 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
902 delay = snd_pcm_playback_hw_avail(substream->runtime);
904 delay = snd_pcm_capture_avail(substream->runtime);
905 return delay + substream->runtime->delay;
908 int snd_pcm_status64(struct snd_pcm_substream *substream,
909 struct snd_pcm_status64 *status)
911 struct snd_pcm_runtime *runtime = substream->runtime;
913 snd_pcm_stream_lock_irq(substream);
915 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
916 &runtime->audio_tstamp_config);
918 /* backwards compatible behavior */
919 if (runtime->audio_tstamp_config.type_requested ==
920 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
921 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
922 runtime->audio_tstamp_config.type_requested =
923 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
925 runtime->audio_tstamp_config.type_requested =
926 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
927 runtime->audio_tstamp_report.valid = 0;
929 runtime->audio_tstamp_report.valid = 1;
931 status->state = runtime->status->state;
932 status->suspended_state = runtime->status->suspended_state;
933 if (status->state == SNDRV_PCM_STATE_OPEN)
935 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
936 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
937 if (snd_pcm_running(substream)) {
938 snd_pcm_update_hw_ptr(substream);
939 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
940 status->tstamp_sec = runtime->status->tstamp.tv_sec;
941 status->tstamp_nsec =
942 runtime->status->tstamp.tv_nsec;
943 status->driver_tstamp_sec =
944 runtime->driver_tstamp.tv_sec;
945 status->driver_tstamp_nsec =
946 runtime->driver_tstamp.tv_nsec;
947 status->audio_tstamp_sec =
948 runtime->status->audio_tstamp.tv_sec;
949 status->audio_tstamp_nsec =
950 runtime->status->audio_tstamp.tv_nsec;
951 if (runtime->audio_tstamp_report.valid == 1)
952 /* backwards compatibility, no report provided in COMPAT mode */
953 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
954 &status->audio_tstamp_accuracy,
955 &runtime->audio_tstamp_report);
960 /* get tstamp only in fallback mode and only if enabled */
961 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
962 struct timespec64 tstamp;
964 snd_pcm_gettime(runtime, &tstamp);
965 status->tstamp_sec = tstamp.tv_sec;
966 status->tstamp_nsec = tstamp.tv_nsec;
970 status->appl_ptr = runtime->control->appl_ptr;
971 status->hw_ptr = runtime->status->hw_ptr;
972 status->avail = snd_pcm_avail(substream);
973 status->delay = snd_pcm_running(substream) ?
974 snd_pcm_calc_delay(substream) : 0;
975 status->avail_max = runtime->avail_max;
976 status->overrange = runtime->overrange;
977 runtime->avail_max = 0;
978 runtime->overrange = 0;
980 snd_pcm_stream_unlock_irq(substream);
984 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
985 struct snd_pcm_status64 __user * _status,
988 struct snd_pcm_status64 status;
991 memset(&status, 0, sizeof(status));
993 * with extension, parameters are read/write,
994 * get audio_tstamp_data from user,
995 * ignore rest of status structure
997 if (ext && get_user(status.audio_tstamp_data,
998 (u32 __user *)(&_status->audio_tstamp_data)))
1000 res = snd_pcm_status64(substream, &status);
1003 if (copy_to_user(_status, &status, sizeof(status)))
1008 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1009 struct snd_pcm_status32 __user * _status,
1012 struct snd_pcm_status64 status64;
1013 struct snd_pcm_status32 status32;
1016 memset(&status64, 0, sizeof(status64));
1017 memset(&status32, 0, sizeof(status32));
1019 * with extension, parameters are read/write,
1020 * get audio_tstamp_data from user,
1021 * ignore rest of status structure
1023 if (ext && get_user(status64.audio_tstamp_data,
1024 (u32 __user *)(&_status->audio_tstamp_data)))
1026 res = snd_pcm_status64(substream, &status64);
1030 status32 = (struct snd_pcm_status32) {
1031 .state = status64.state,
1032 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1033 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1034 .tstamp_sec = status64.tstamp_sec,
1035 .tstamp_nsec = status64.tstamp_nsec,
1036 .appl_ptr = status64.appl_ptr,
1037 .hw_ptr = status64.hw_ptr,
1038 .delay = status64.delay,
1039 .avail = status64.avail,
1040 .avail_max = status64.avail_max,
1041 .overrange = status64.overrange,
1042 .suspended_state = status64.suspended_state,
1043 .audio_tstamp_data = status64.audio_tstamp_data,
1044 .audio_tstamp_sec = status64.audio_tstamp_sec,
1045 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1046 .driver_tstamp_sec = status64.audio_tstamp_sec,
1047 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1048 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1051 if (copy_to_user(_status, &status32, sizeof(status32)))
1057 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1058 struct snd_pcm_channel_info * info)
1060 struct snd_pcm_runtime *runtime;
1061 unsigned int channel;
1063 channel = info->channel;
1064 runtime = substream->runtime;
1065 snd_pcm_stream_lock_irq(substream);
1066 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1067 snd_pcm_stream_unlock_irq(substream);
1070 snd_pcm_stream_unlock_irq(substream);
1071 if (channel >= runtime->channels)
1073 memset(info, 0, sizeof(*info));
1074 info->channel = channel;
1075 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1078 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1079 struct snd_pcm_channel_info __user * _info)
1081 struct snd_pcm_channel_info info;
1084 if (copy_from_user(&info, _info, sizeof(info)))
1086 res = snd_pcm_channel_info(substream, &info);
1089 if (copy_to_user(_info, &info, sizeof(info)))
1094 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1096 struct snd_pcm_runtime *runtime = substream->runtime;
1097 if (runtime->trigger_master == NULL)
1099 if (runtime->trigger_master == substream) {
1100 if (!runtime->trigger_tstamp_latched)
1101 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1103 snd_pcm_trigger_tstamp(runtime->trigger_master);
1104 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1106 runtime->trigger_master = NULL;
1109 #define ACTION_ARG_IGNORE (__force snd_pcm_state_t)0
1112 int (*pre_action)(struct snd_pcm_substream *substream,
1113 snd_pcm_state_t state);
1114 int (*do_action)(struct snd_pcm_substream *substream,
1115 snd_pcm_state_t state);
1116 void (*undo_action)(struct snd_pcm_substream *substream,
1117 snd_pcm_state_t state);
1118 void (*post_action)(struct snd_pcm_substream *substream,
1119 snd_pcm_state_t state);
1123 * this functions is core for handling of linked stream
1124 * Note: the stream state might be changed also on failure
1125 * Note2: call with calling stream lock + link lock
1127 static int snd_pcm_action_group(const struct action_ops *ops,
1128 struct snd_pcm_substream *substream,
1129 snd_pcm_state_t state,
1132 struct snd_pcm_substream *s = NULL;
1133 struct snd_pcm_substream *s1;
1134 int res = 0, depth = 1;
1136 snd_pcm_group_for_each_entry(s, substream) {
1137 if (do_lock && s != substream) {
1138 if (s->pcm->nonatomic)
1139 mutex_lock_nested(&s->self_group.mutex, depth);
1141 spin_lock_nested(&s->self_group.lock, depth);
1144 res = ops->pre_action(s, state);
1148 snd_pcm_group_for_each_entry(s, substream) {
1149 res = ops->do_action(s, state);
1151 if (ops->undo_action) {
1152 snd_pcm_group_for_each_entry(s1, substream) {
1153 if (s1 == s) /* failed stream */
1155 ops->undo_action(s1, state);
1158 s = NULL; /* unlock all */
1162 snd_pcm_group_for_each_entry(s, substream) {
1163 ops->post_action(s, state);
1167 /* unlock streams */
1168 snd_pcm_group_for_each_entry(s1, substream) {
1169 if (s1 != substream) {
1170 if (s1->pcm->nonatomic)
1171 mutex_unlock(&s1->self_group.mutex);
1173 spin_unlock(&s1->self_group.lock);
1175 if (s1 == s) /* end */
1183 * Note: call with stream lock
1185 static int snd_pcm_action_single(const struct action_ops *ops,
1186 struct snd_pcm_substream *substream,
1187 snd_pcm_state_t state)
1191 res = ops->pre_action(substream, state);
1194 res = ops->do_action(substream, state);
1196 ops->post_action(substream, state);
1197 else if (ops->undo_action)
1198 ops->undo_action(substream, state);
1202 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1203 struct snd_pcm_group *new_group)
1205 substream->group = new_group;
1206 list_move(&substream->link_list, &new_group->substreams);
1210 * Unref and unlock the group, but keep the stream lock;
1211 * when the group becomes empty and no longer referred, destroy itself
1213 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1214 struct snd_pcm_substream *substream)
1220 do_free = refcount_dec_and_test(&group->refs);
1221 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1227 * Lock the group inside a stream lock and reference it;
1228 * return the locked group object, or NULL if not linked
1230 static struct snd_pcm_group *
1231 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1233 bool nonatomic = substream->pcm->nonatomic;
1234 struct snd_pcm_group *group;
1238 if (!snd_pcm_stream_linked(substream))
1240 group = substream->group;
1241 /* block freeing the group object */
1242 refcount_inc(&group->refs);
1244 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1245 spin_trylock(&group->lock);
1249 /* re-lock for avoiding ABBA deadlock */
1250 snd_pcm_stream_unlock(substream);
1251 snd_pcm_group_lock(group, nonatomic);
1252 snd_pcm_stream_lock(substream);
1254 /* check the group again; the above opens a small race window */
1255 if (substream->group == group)
1257 /* group changed, try again */
1258 snd_pcm_group_unref(group, substream);
1264 * Note: call with stream lock
1266 static int snd_pcm_action(const struct action_ops *ops,
1267 struct snd_pcm_substream *substream,
1268 snd_pcm_state_t state)
1270 struct snd_pcm_group *group;
1273 group = snd_pcm_stream_group_ref(substream);
1275 res = snd_pcm_action_group(ops, substream, state, true);
1277 res = snd_pcm_action_single(ops, substream, state);
1278 snd_pcm_group_unref(group, substream);
1283 * Note: don't use any locks before
1285 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1286 struct snd_pcm_substream *substream,
1287 snd_pcm_state_t state)
1291 snd_pcm_stream_lock_irq(substream);
1292 res = snd_pcm_action(ops, substream, state);
1293 snd_pcm_stream_unlock_irq(substream);
1299 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1300 struct snd_pcm_substream *substream,
1301 snd_pcm_state_t state)
1305 /* Guarantee the group members won't change during non-atomic action */
1306 down_read(&snd_pcm_link_rwsem);
1307 if (snd_pcm_stream_linked(substream))
1308 res = snd_pcm_action_group(ops, substream, state, false);
1310 res = snd_pcm_action_single(ops, substream, state);
1311 up_read(&snd_pcm_link_rwsem);
1318 static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1319 snd_pcm_state_t state)
1321 struct snd_pcm_runtime *runtime = substream->runtime;
1322 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1325 !snd_pcm_playback_data(substream))
1327 runtime->trigger_tstamp_latched = false;
1328 runtime->trigger_master = substream;
1332 static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1333 snd_pcm_state_t state)
1335 if (substream->runtime->trigger_master != substream)
1337 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1340 static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1341 snd_pcm_state_t state)
1343 if (substream->runtime->trigger_master == substream)
1344 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1347 static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1348 snd_pcm_state_t state)
1350 struct snd_pcm_runtime *runtime = substream->runtime;
1351 snd_pcm_trigger_tstamp(substream);
1352 runtime->hw_ptr_jiffies = jiffies;
1353 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1355 runtime->status->state = state;
1356 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1357 runtime->silence_size > 0)
1358 snd_pcm_playback_silence(substream, ULONG_MAX);
1359 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1362 static const struct action_ops snd_pcm_action_start = {
1363 .pre_action = snd_pcm_pre_start,
1364 .do_action = snd_pcm_do_start,
1365 .undo_action = snd_pcm_undo_start,
1366 .post_action = snd_pcm_post_start
1370 * snd_pcm_start - start all linked streams
1371 * @substream: the PCM substream instance
1373 * Return: Zero if successful, or a negative error code.
1374 * The stream lock must be acquired before calling this function.
1376 int snd_pcm_start(struct snd_pcm_substream *substream)
1378 return snd_pcm_action(&snd_pcm_action_start, substream,
1379 SNDRV_PCM_STATE_RUNNING);
1382 /* take the stream lock and start the streams */
1383 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1385 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1386 SNDRV_PCM_STATE_RUNNING);
1392 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1393 snd_pcm_state_t state)
1395 struct snd_pcm_runtime *runtime = substream->runtime;
1396 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1398 runtime->trigger_master = substream;
1402 static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1403 snd_pcm_state_t state)
1405 if (substream->runtime->trigger_master == substream &&
1406 snd_pcm_running(substream))
1407 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1408 return 0; /* unconditonally stop all substreams */
1411 static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1412 snd_pcm_state_t state)
1414 struct snd_pcm_runtime *runtime = substream->runtime;
1415 if (runtime->status->state != state) {
1416 snd_pcm_trigger_tstamp(substream);
1417 runtime->status->state = state;
1418 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1420 runtime->stop_operating = true;
1421 wake_up(&runtime->sleep);
1422 wake_up(&runtime->tsleep);
1425 static const struct action_ops snd_pcm_action_stop = {
1426 .pre_action = snd_pcm_pre_stop,
1427 .do_action = snd_pcm_do_stop,
1428 .post_action = snd_pcm_post_stop
1432 * snd_pcm_stop - try to stop all running streams in the substream group
1433 * @substream: the PCM substream instance
1434 * @state: PCM state after stopping the stream
1436 * The state of each stream is then changed to the given state unconditionally.
1438 * Return: Zero if successful, or a negative error code.
1440 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1442 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1444 EXPORT_SYMBOL(snd_pcm_stop);
1447 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1448 * @substream: the PCM substream
1450 * After stopping, the state is changed to SETUP.
1451 * Unlike snd_pcm_stop(), this affects only the given stream.
1453 * Return: Zero if succesful, or a negative error code.
1455 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1457 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1458 SNDRV_PCM_STATE_SETUP);
1462 * snd_pcm_stop_xrun - stop the running streams as XRUN
1463 * @substream: the PCM substream instance
1465 * This stops the given running substream (and all linked substreams) as XRUN.
1466 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1468 * Return: Zero if successful, or a negative error code.
1470 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1472 unsigned long flags;
1474 snd_pcm_stream_lock_irqsave(substream, flags);
1475 if (substream->runtime && snd_pcm_running(substream))
1476 __snd_pcm_xrun(substream);
1477 snd_pcm_stream_unlock_irqrestore(substream, flags);
1480 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1483 * pause callbacks: pass boolean (to start pause or resume) as state argument
1485 #define pause_pushed(state) (__force bool)(state)
1487 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1488 snd_pcm_state_t state)
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1491 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1493 if (pause_pushed(state)) {
1494 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1496 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1498 runtime->trigger_master = substream;
1502 static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1503 snd_pcm_state_t state)
1505 if (substream->runtime->trigger_master != substream)
1507 /* some drivers might use hw_ptr to recover from the pause -
1508 update the hw_ptr now */
1509 if (pause_pushed(state))
1510 snd_pcm_update_hw_ptr(substream);
1511 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1512 * a delta between the current jiffies, this gives a large enough
1513 * delta, effectively to skip the check once.
1515 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1516 return substream->ops->trigger(substream,
1517 pause_pushed(state) ?
1518 SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1519 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1522 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1523 snd_pcm_state_t state)
1525 if (substream->runtime->trigger_master == substream)
1526 substream->ops->trigger(substream,
1527 pause_pushed(state) ?
1528 SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1529 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1532 static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1533 snd_pcm_state_t state)
1535 struct snd_pcm_runtime *runtime = substream->runtime;
1536 snd_pcm_trigger_tstamp(substream);
1537 if (pause_pushed(state)) {
1538 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1539 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1540 wake_up(&runtime->sleep);
1541 wake_up(&runtime->tsleep);
1543 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1544 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1548 static const struct action_ops snd_pcm_action_pause = {
1549 .pre_action = snd_pcm_pre_pause,
1550 .do_action = snd_pcm_do_pause,
1551 .undo_action = snd_pcm_undo_pause,
1552 .post_action = snd_pcm_post_pause
1556 * Push/release the pause for all linked streams.
1558 static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1560 return snd_pcm_action(&snd_pcm_action_pause, substream,
1561 (__force snd_pcm_state_t)push);
1564 static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1567 return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1568 (__force snd_pcm_state_t)push);
1572 /* suspend callback: state argument ignored */
1574 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1575 snd_pcm_state_t state)
1577 struct snd_pcm_runtime *runtime = substream->runtime;
1578 switch (runtime->status->state) {
1579 case SNDRV_PCM_STATE_SUSPENDED:
1581 /* unresumable PCM state; return -EBUSY for skipping suspend */
1582 case SNDRV_PCM_STATE_OPEN:
1583 case SNDRV_PCM_STATE_SETUP:
1584 case SNDRV_PCM_STATE_DISCONNECTED:
1587 runtime->trigger_master = substream;
1591 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1592 snd_pcm_state_t state)
1594 struct snd_pcm_runtime *runtime = substream->runtime;
1595 if (runtime->trigger_master != substream)
1597 if (! snd_pcm_running(substream))
1599 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1600 return 0; /* suspend unconditionally */
1603 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1604 snd_pcm_state_t state)
1606 struct snd_pcm_runtime *runtime = substream->runtime;
1607 snd_pcm_trigger_tstamp(substream);
1608 runtime->status->suspended_state = runtime->status->state;
1609 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1610 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1611 wake_up(&runtime->sleep);
1612 wake_up(&runtime->tsleep);
1615 static const struct action_ops snd_pcm_action_suspend = {
1616 .pre_action = snd_pcm_pre_suspend,
1617 .do_action = snd_pcm_do_suspend,
1618 .post_action = snd_pcm_post_suspend
1622 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1623 * @substream: the PCM substream
1625 * After this call, all streams are changed to SUSPENDED state.
1627 * Return: Zero if successful, or a negative error code.
1629 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1632 unsigned long flags;
1634 snd_pcm_stream_lock_irqsave(substream, flags);
1635 err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1637 snd_pcm_stream_unlock_irqrestore(substream, flags);
1642 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1643 * @pcm: the PCM instance
1645 * After this call, all streams are changed to SUSPENDED state.
1647 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1649 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1651 struct snd_pcm_substream *substream;
1652 int stream, err = 0;
1657 for (stream = 0; stream < 2; stream++) {
1658 for (substream = pcm->streams[stream].substream;
1659 substream; substream = substream->next) {
1660 /* FIXME: the open/close code should lock this as well */
1661 if (substream->runtime == NULL)
1665 * Skip BE dai link PCM's that are internal and may
1666 * not have their substream ops set.
1668 if (!substream->ops)
1671 err = snd_pcm_suspend(substream);
1672 if (err < 0 && err != -EBUSY)
1678 EXPORT_SYMBOL(snd_pcm_suspend_all);
1680 /* resume callbacks: state argument ignored */
1682 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1683 snd_pcm_state_t state)
1685 struct snd_pcm_runtime *runtime = substream->runtime;
1686 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1688 runtime->trigger_master = substream;
1692 static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1693 snd_pcm_state_t state)
1695 struct snd_pcm_runtime *runtime = substream->runtime;
1696 if (runtime->trigger_master != substream)
1698 /* DMA not running previously? */
1699 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1700 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1701 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1703 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1706 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1707 snd_pcm_state_t state)
1709 if (substream->runtime->trigger_master == substream &&
1710 snd_pcm_running(substream))
1711 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1714 static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1715 snd_pcm_state_t state)
1717 struct snd_pcm_runtime *runtime = substream->runtime;
1718 snd_pcm_trigger_tstamp(substream);
1719 runtime->status->state = runtime->status->suspended_state;
1720 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1721 snd_pcm_sync_stop(substream);
1724 static const struct action_ops snd_pcm_action_resume = {
1725 .pre_action = snd_pcm_pre_resume,
1726 .do_action = snd_pcm_do_resume,
1727 .undo_action = snd_pcm_undo_resume,
1728 .post_action = snd_pcm_post_resume
1731 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1733 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1739 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1744 #endif /* CONFIG_PM */
1749 * Change the RUNNING stream(s) to XRUN state.
1751 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1753 struct snd_pcm_runtime *runtime = substream->runtime;
1756 snd_pcm_stream_lock_irq(substream);
1757 switch (runtime->status->state) {
1758 case SNDRV_PCM_STATE_XRUN:
1759 result = 0; /* already there */
1761 case SNDRV_PCM_STATE_RUNNING:
1762 __snd_pcm_xrun(substream);
1768 snd_pcm_stream_unlock_irq(substream);
1775 /* reset callbacks: state argument ignored */
1776 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1777 snd_pcm_state_t state)
1779 struct snd_pcm_runtime *runtime = substream->runtime;
1780 switch (runtime->status->state) {
1781 case SNDRV_PCM_STATE_RUNNING:
1782 case SNDRV_PCM_STATE_PREPARED:
1783 case SNDRV_PCM_STATE_PAUSED:
1784 case SNDRV_PCM_STATE_SUSPENDED:
1791 static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1792 snd_pcm_state_t state)
1794 struct snd_pcm_runtime *runtime = substream->runtime;
1795 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1798 runtime->hw_ptr_base = 0;
1799 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1800 runtime->status->hw_ptr % runtime->period_size;
1801 runtime->silence_start = runtime->status->hw_ptr;
1802 runtime->silence_filled = 0;
1806 static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1807 snd_pcm_state_t state)
1809 struct snd_pcm_runtime *runtime = substream->runtime;
1810 runtime->control->appl_ptr = runtime->status->hw_ptr;
1811 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1812 runtime->silence_size > 0)
1813 snd_pcm_playback_silence(substream, ULONG_MAX);
1816 static const struct action_ops snd_pcm_action_reset = {
1817 .pre_action = snd_pcm_pre_reset,
1818 .do_action = snd_pcm_do_reset,
1819 .post_action = snd_pcm_post_reset
1822 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1824 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1831 /* pass f_flags as state argument */
1832 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1833 snd_pcm_state_t state)
1835 struct snd_pcm_runtime *runtime = substream->runtime;
1836 int f_flags = (__force int)state;
1838 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1839 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1841 if (snd_pcm_running(substream))
1843 substream->f_flags = f_flags;
1847 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1848 snd_pcm_state_t state)
1851 snd_pcm_sync_stop(substream);
1852 err = substream->ops->prepare(substream);
1855 return snd_pcm_do_reset(substream, state);
1858 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1859 snd_pcm_state_t state)
1861 struct snd_pcm_runtime *runtime = substream->runtime;
1862 runtime->control->appl_ptr = runtime->status->hw_ptr;
1863 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1866 static const struct action_ops snd_pcm_action_prepare = {
1867 .pre_action = snd_pcm_pre_prepare,
1868 .do_action = snd_pcm_do_prepare,
1869 .post_action = snd_pcm_post_prepare
1873 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1874 * @substream: the PCM substream instance
1875 * @file: file to refer f_flags
1877 * Return: Zero if successful, or a negative error code.
1879 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1885 f_flags = file->f_flags;
1887 f_flags = substream->f_flags;
1889 snd_pcm_stream_lock_irq(substream);
1890 switch (substream->runtime->status->state) {
1891 case SNDRV_PCM_STATE_PAUSED:
1892 snd_pcm_pause(substream, false);
1894 case SNDRV_PCM_STATE_SUSPENDED:
1895 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1898 snd_pcm_stream_unlock_irq(substream);
1900 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1902 (__force snd_pcm_state_t)f_flags);
1909 /* drain init callbacks: state argument ignored */
1910 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1911 snd_pcm_state_t state)
1913 struct snd_pcm_runtime *runtime = substream->runtime;
1914 switch (runtime->status->state) {
1915 case SNDRV_PCM_STATE_OPEN:
1916 case SNDRV_PCM_STATE_DISCONNECTED:
1917 case SNDRV_PCM_STATE_SUSPENDED:
1920 runtime->trigger_master = substream;
1924 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1925 snd_pcm_state_t state)
1927 struct snd_pcm_runtime *runtime = substream->runtime;
1928 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1929 switch (runtime->status->state) {
1930 case SNDRV_PCM_STATE_PREPARED:
1931 /* start playback stream if possible */
1932 if (! snd_pcm_playback_empty(substream)) {
1933 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1934 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1936 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1939 case SNDRV_PCM_STATE_RUNNING:
1940 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1942 case SNDRV_PCM_STATE_XRUN:
1943 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1949 /* stop running stream */
1950 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1951 snd_pcm_state_t new_state;
1953 new_state = snd_pcm_capture_avail(runtime) > 0 ?
1954 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1955 snd_pcm_do_stop(substream, new_state);
1956 snd_pcm_post_stop(substream, new_state);
1960 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1961 runtime->trigger_master == substream &&
1962 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1963 return substream->ops->trigger(substream,
1964 SNDRV_PCM_TRIGGER_DRAIN);
1969 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
1970 snd_pcm_state_t state)
1974 static const struct action_ops snd_pcm_action_drain_init = {
1975 .pre_action = snd_pcm_pre_drain_init,
1976 .do_action = snd_pcm_do_drain_init,
1977 .post_action = snd_pcm_post_drain_init
1981 * Drain the stream(s).
1982 * When the substream is linked, sync until the draining of all playback streams
1984 * After this call, all streams are supposed to be either SETUP or DRAINING
1985 * (capture only) state.
1987 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1990 struct snd_card *card;
1991 struct snd_pcm_runtime *runtime;
1992 struct snd_pcm_substream *s;
1993 struct snd_pcm_group *group;
1994 wait_queue_entry_t wait;
1998 card = substream->pcm->card;
1999 runtime = substream->runtime;
2001 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2005 if (file->f_flags & O_NONBLOCK)
2007 } else if (substream->f_flags & O_NONBLOCK)
2010 snd_pcm_stream_lock_irq(substream);
2012 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2013 snd_pcm_pause(substream, false);
2015 /* pre-start/stop - all running streams are changed to DRAINING state */
2016 result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2020 /* in non-blocking, we don't wait in ioctl but let caller poll */
2028 struct snd_pcm_runtime *to_check;
2029 if (signal_pending(current)) {
2030 result = -ERESTARTSYS;
2033 /* find a substream to drain */
2035 group = snd_pcm_stream_group_ref(substream);
2036 snd_pcm_group_for_each_entry(s, substream) {
2037 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2039 runtime = s->runtime;
2040 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2045 snd_pcm_group_unref(group, substream);
2047 break; /* all drained */
2048 init_waitqueue_entry(&wait, current);
2049 set_current_state(TASK_INTERRUPTIBLE);
2050 add_wait_queue(&to_check->sleep, &wait);
2051 snd_pcm_stream_unlock_irq(substream);
2052 if (runtime->no_period_wakeup)
2053 tout = MAX_SCHEDULE_TIMEOUT;
2056 if (runtime->rate) {
2057 long t = runtime->period_size * 2 / runtime->rate;
2058 tout = max(t, tout);
2060 tout = msecs_to_jiffies(tout * 1000);
2062 tout = schedule_timeout(tout);
2064 snd_pcm_stream_lock_irq(substream);
2065 group = snd_pcm_stream_group_ref(substream);
2066 snd_pcm_group_for_each_entry(s, substream) {
2067 if (s->runtime == to_check) {
2068 remove_wait_queue(&to_check->sleep, &wait);
2072 snd_pcm_group_unref(group, substream);
2074 if (card->shutdown) {
2079 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2082 dev_dbg(substream->pcm->card->dev,
2083 "playback drain error (DMA or IRQ trouble?)\n");
2084 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2092 snd_pcm_stream_unlock_irq(substream);
2100 * Immediately put all linked substreams into SETUP state.
2102 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2104 struct snd_pcm_runtime *runtime;
2107 if (PCM_RUNTIME_CHECK(substream))
2109 runtime = substream->runtime;
2111 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2112 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2115 snd_pcm_stream_lock_irq(substream);
2117 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2118 snd_pcm_pause(substream, false);
2120 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2121 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2122 snd_pcm_stream_unlock_irq(substream);
2128 static bool is_pcm_file(struct file *file)
2130 struct inode *inode = file_inode(file);
2131 struct snd_pcm *pcm;
2134 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2136 minor = iminor(inode);
2137 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2139 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2142 snd_card_unref(pcm->card);
2149 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2152 struct snd_pcm_file *pcm_file;
2153 struct snd_pcm_substream *substream1;
2154 struct snd_pcm_group *group, *target_group;
2155 bool nonatomic = substream->pcm->nonatomic;
2156 struct fd f = fdget(fd);
2160 if (!is_pcm_file(f.file)) {
2164 pcm_file = f.file->private_data;
2165 substream1 = pcm_file->substream;
2166 group = kzalloc(sizeof(*group), GFP_KERNEL);
2171 snd_pcm_group_init(group);
2173 down_write(&snd_pcm_link_rwsem);
2174 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2175 substream->runtime->status->state != substream1->runtime->status->state ||
2176 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2180 if (snd_pcm_stream_linked(substream1)) {
2185 snd_pcm_stream_lock_irq(substream);
2186 if (!snd_pcm_stream_linked(substream)) {
2187 snd_pcm_group_assign(substream, group);
2188 group = NULL; /* assigned, don't free this one below */
2190 target_group = substream->group;
2191 snd_pcm_stream_unlock_irq(substream);
2193 snd_pcm_group_lock_irq(target_group, nonatomic);
2194 snd_pcm_stream_lock(substream1);
2195 snd_pcm_group_assign(substream1, target_group);
2196 refcount_inc(&target_group->refs);
2197 snd_pcm_stream_unlock(substream1);
2198 snd_pcm_group_unlock_irq(target_group, nonatomic);
2200 up_write(&snd_pcm_link_rwsem);
2208 static void relink_to_local(struct snd_pcm_substream *substream)
2210 snd_pcm_stream_lock(substream);
2211 snd_pcm_group_assign(substream, &substream->self_group);
2212 snd_pcm_stream_unlock(substream);
2215 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2217 struct snd_pcm_group *group;
2218 bool nonatomic = substream->pcm->nonatomic;
2219 bool do_free = false;
2222 down_write(&snd_pcm_link_rwsem);
2224 if (!snd_pcm_stream_linked(substream)) {
2229 group = substream->group;
2230 snd_pcm_group_lock_irq(group, nonatomic);
2232 relink_to_local(substream);
2233 refcount_dec(&group->refs);
2235 /* detach the last stream, too */
2236 if (list_is_singular(&group->substreams)) {
2237 relink_to_local(list_first_entry(&group->substreams,
2238 struct snd_pcm_substream,
2240 do_free = refcount_dec_and_test(&group->refs);
2243 snd_pcm_group_unlock_irq(group, nonatomic);
2248 up_write(&snd_pcm_link_rwsem);
2255 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2256 struct snd_pcm_hw_rule *rule)
2258 struct snd_interval t;
2259 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2260 hw_param_interval_c(params, rule->deps[1]), &t);
2261 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2264 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2265 struct snd_pcm_hw_rule *rule)
2267 struct snd_interval t;
2268 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2269 hw_param_interval_c(params, rule->deps[1]), &t);
2270 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2273 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2274 struct snd_pcm_hw_rule *rule)
2276 struct snd_interval t;
2277 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2278 hw_param_interval_c(params, rule->deps[1]),
2279 (unsigned long) rule->private, &t);
2280 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2283 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2284 struct snd_pcm_hw_rule *rule)
2286 struct snd_interval t;
2287 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2288 (unsigned long) rule->private,
2289 hw_param_interval_c(params, rule->deps[1]), &t);
2290 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2293 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2294 struct snd_pcm_hw_rule *rule)
2297 const struct snd_interval *i =
2298 hw_param_interval_c(params, rule->deps[0]);
2300 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2302 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2304 if (! snd_mask_test(mask, k))
2306 bits = snd_pcm_format_physical_width(k);
2308 continue; /* ignore invalid formats */
2309 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2310 snd_mask_reset(&m, k);
2312 return snd_mask_refine(mask, &m);
2315 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2316 struct snd_pcm_hw_rule *rule)
2318 struct snd_interval t;
2324 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2326 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2328 bits = snd_pcm_format_physical_width(k);
2330 continue; /* ignore invalid formats */
2331 if (t.min > (unsigned)bits)
2333 if (t.max < (unsigned)bits)
2337 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2340 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2341 #error "Change this table"
2344 static const unsigned int rates[] = {
2345 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2346 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2349 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2350 .count = ARRAY_SIZE(rates),
2354 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2355 struct snd_pcm_hw_rule *rule)
2357 struct snd_pcm_hardware *hw = rule->private;
2358 return snd_interval_list(hw_param_interval(params, rule->var),
2359 snd_pcm_known_rates.count,
2360 snd_pcm_known_rates.list, hw->rates);
2363 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2364 struct snd_pcm_hw_rule *rule)
2366 struct snd_interval t;
2367 struct snd_pcm_substream *substream = rule->private;
2369 t.max = substream->buffer_bytes_max;
2373 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2376 static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2378 struct snd_pcm_runtime *runtime = substream->runtime;
2379 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2382 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2383 snd_mask_any(constrs_mask(constrs, k));
2386 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2387 snd_interval_any(constrs_interval(constrs, k));
2390 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2391 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2392 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2393 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2394 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2396 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2397 snd_pcm_hw_rule_format, NULL,
2398 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2401 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2402 snd_pcm_hw_rule_sample_bits, NULL,
2403 SNDRV_PCM_HW_PARAM_FORMAT,
2404 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2407 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2408 snd_pcm_hw_rule_div, NULL,
2409 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2412 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2413 snd_pcm_hw_rule_mul, NULL,
2414 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2417 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2418 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2419 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2422 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2423 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2424 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2427 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2428 snd_pcm_hw_rule_div, NULL,
2429 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2432 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2433 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2434 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2437 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2438 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2439 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2442 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2443 snd_pcm_hw_rule_div, NULL,
2444 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2447 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2448 snd_pcm_hw_rule_div, NULL,
2449 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2452 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2453 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2454 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2457 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2458 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2459 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2462 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2463 snd_pcm_hw_rule_mul, NULL,
2464 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2467 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2468 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2469 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2472 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2473 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2474 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2477 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2478 snd_pcm_hw_rule_muldivk, (void*) 8,
2479 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2482 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2483 snd_pcm_hw_rule_muldivk, (void*) 8,
2484 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2487 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2488 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2489 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2492 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2493 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2494 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2500 static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2502 struct snd_pcm_runtime *runtime = substream->runtime;
2503 struct snd_pcm_hardware *hw = &runtime->hw;
2505 unsigned int mask = 0;
2507 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2508 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2509 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2510 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2511 if (hw_support_mmap(substream)) {
2512 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2513 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2514 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2515 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2516 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2517 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2519 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2523 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2527 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2531 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2532 hw->channels_min, hw->channels_max);
2536 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2537 hw->rate_min, hw->rate_max);
2541 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2542 hw->period_bytes_min, hw->period_bytes_max);
2546 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2547 hw->periods_min, hw->periods_max);
2551 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2552 hw->period_bytes_min, hw->buffer_bytes_max);
2556 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2557 snd_pcm_hw_rule_buffer_bytes_max, substream,
2558 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2563 if (runtime->dma_bytes) {
2564 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2569 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2570 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2571 snd_pcm_hw_rule_rate, hw,
2572 SNDRV_PCM_HW_PARAM_RATE, -1);
2577 /* FIXME: this belong to lowlevel */
2578 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2583 static void pcm_release_private(struct snd_pcm_substream *substream)
2585 if (snd_pcm_stream_linked(substream))
2586 snd_pcm_unlink(substream);
2589 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2591 substream->ref_count--;
2592 if (substream->ref_count > 0)
2595 snd_pcm_drop(substream);
2596 if (substream->hw_opened) {
2597 do_hw_free(substream);
2598 substream->ops->close(substream);
2599 substream->hw_opened = 0;
2601 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2602 pm_qos_remove_request(&substream->latency_pm_qos_req);
2603 if (substream->pcm_release) {
2604 substream->pcm_release(substream);
2605 substream->pcm_release = NULL;
2607 snd_pcm_detach_substream(substream);
2609 EXPORT_SYMBOL(snd_pcm_release_substream);
2611 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2613 struct snd_pcm_substream **rsubstream)
2615 struct snd_pcm_substream *substream;
2618 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2621 if (substream->ref_count > 1) {
2622 *rsubstream = substream;
2626 err = snd_pcm_hw_constraints_init(substream);
2628 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2632 if ((err = substream->ops->open(substream)) < 0)
2635 substream->hw_opened = 1;
2637 err = snd_pcm_hw_constraints_complete(substream);
2639 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2643 *rsubstream = substream;
2647 snd_pcm_release_substream(substream);
2650 EXPORT_SYMBOL(snd_pcm_open_substream);
2652 static int snd_pcm_open_file(struct file *file,
2653 struct snd_pcm *pcm,
2656 struct snd_pcm_file *pcm_file;
2657 struct snd_pcm_substream *substream;
2660 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2664 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2665 if (pcm_file == NULL) {
2666 snd_pcm_release_substream(substream);
2669 pcm_file->substream = substream;
2670 if (substream->ref_count == 1)
2671 substream->pcm_release = pcm_release_private;
2672 file->private_data = pcm_file;
2677 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2679 struct snd_pcm *pcm;
2680 int err = nonseekable_open(inode, file);
2683 pcm = snd_lookup_minor_data(iminor(inode),
2684 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2685 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2687 snd_card_unref(pcm->card);
2691 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2693 struct snd_pcm *pcm;
2694 int err = nonseekable_open(inode, file);
2697 pcm = snd_lookup_minor_data(iminor(inode),
2698 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2699 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2701 snd_card_unref(pcm->card);
2705 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2708 wait_queue_entry_t wait;
2714 err = snd_card_file_add(pcm->card, file);
2717 if (!try_module_get(pcm->card->module)) {
2721 init_waitqueue_entry(&wait, current);
2722 add_wait_queue(&pcm->open_wait, &wait);
2723 mutex_lock(&pcm->open_mutex);
2725 err = snd_pcm_open_file(file, pcm, stream);
2728 if (err == -EAGAIN) {
2729 if (file->f_flags & O_NONBLOCK) {
2735 set_current_state(TASK_INTERRUPTIBLE);
2736 mutex_unlock(&pcm->open_mutex);
2738 mutex_lock(&pcm->open_mutex);
2739 if (pcm->card->shutdown) {
2743 if (signal_pending(current)) {
2748 remove_wait_queue(&pcm->open_wait, &wait);
2749 mutex_unlock(&pcm->open_mutex);
2755 module_put(pcm->card->module);
2757 snd_card_file_remove(pcm->card, file);
2762 static int snd_pcm_release(struct inode *inode, struct file *file)
2764 struct snd_pcm *pcm;
2765 struct snd_pcm_substream *substream;
2766 struct snd_pcm_file *pcm_file;
2768 pcm_file = file->private_data;
2769 substream = pcm_file->substream;
2770 if (snd_BUG_ON(!substream))
2772 pcm = substream->pcm;
2773 mutex_lock(&pcm->open_mutex);
2774 snd_pcm_release_substream(substream);
2776 mutex_unlock(&pcm->open_mutex);
2777 wake_up(&pcm->open_wait);
2778 module_put(pcm->card->module);
2779 snd_card_file_remove(pcm->card, file);
2783 /* check and update PCM state; return 0 or a negative error
2784 * call this inside PCM lock
2786 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2788 switch (substream->runtime->status->state) {
2789 case SNDRV_PCM_STATE_DRAINING:
2790 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2793 case SNDRV_PCM_STATE_RUNNING:
2794 return snd_pcm_update_hw_ptr(substream);
2795 case SNDRV_PCM_STATE_PREPARED:
2796 case SNDRV_PCM_STATE_PAUSED:
2798 case SNDRV_PCM_STATE_SUSPENDED:
2800 case SNDRV_PCM_STATE_XRUN:
2807 /* increase the appl_ptr; returns the processed frames or a negative error */
2808 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2809 snd_pcm_uframes_t frames,
2810 snd_pcm_sframes_t avail)
2812 struct snd_pcm_runtime *runtime = substream->runtime;
2813 snd_pcm_sframes_t appl_ptr;
2818 if (frames > (snd_pcm_uframes_t)avail)
2820 appl_ptr = runtime->control->appl_ptr + frames;
2821 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2822 appl_ptr -= runtime->boundary;
2823 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2824 return ret < 0 ? ret : frames;
2827 /* decrease the appl_ptr; returns the processed frames or zero for error */
2828 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2829 snd_pcm_uframes_t frames,
2830 snd_pcm_sframes_t avail)
2832 struct snd_pcm_runtime *runtime = substream->runtime;
2833 snd_pcm_sframes_t appl_ptr;
2838 if (frames > (snd_pcm_uframes_t)avail)
2840 appl_ptr = runtime->control->appl_ptr - frames;
2842 appl_ptr += runtime->boundary;
2843 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2844 /* NOTE: we return zero for errors because PulseAudio gets depressed
2845 * upon receiving an error from rewind ioctl and stops processing
2846 * any longer. Returning zero means that no rewind is done, so
2847 * it's not absolutely wrong to answer like that.
2849 return ret < 0 ? 0 : frames;
2852 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2853 snd_pcm_uframes_t frames)
2855 snd_pcm_sframes_t ret;
2860 snd_pcm_stream_lock_irq(substream);
2861 ret = do_pcm_hwsync(substream);
2863 ret = rewind_appl_ptr(substream, frames,
2864 snd_pcm_hw_avail(substream));
2865 snd_pcm_stream_unlock_irq(substream);
2869 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2870 snd_pcm_uframes_t frames)
2872 snd_pcm_sframes_t ret;
2877 snd_pcm_stream_lock_irq(substream);
2878 ret = do_pcm_hwsync(substream);
2880 ret = forward_appl_ptr(substream, frames,
2881 snd_pcm_avail(substream));
2882 snd_pcm_stream_unlock_irq(substream);
2886 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2890 snd_pcm_stream_lock_irq(substream);
2891 err = do_pcm_hwsync(substream);
2892 snd_pcm_stream_unlock_irq(substream);
2896 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2897 snd_pcm_sframes_t *delay)
2900 snd_pcm_sframes_t n = 0;
2902 snd_pcm_stream_lock_irq(substream);
2903 err = do_pcm_hwsync(substream);
2905 n = snd_pcm_calc_delay(substream);
2906 snd_pcm_stream_unlock_irq(substream);
2912 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2913 struct snd_pcm_sync_ptr __user *_sync_ptr)
2915 struct snd_pcm_runtime *runtime = substream->runtime;
2916 struct snd_pcm_sync_ptr sync_ptr;
2917 volatile struct snd_pcm_mmap_status *status;
2918 volatile struct snd_pcm_mmap_control *control;
2921 memset(&sync_ptr, 0, sizeof(sync_ptr));
2922 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2924 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2926 status = runtime->status;
2927 control = runtime->control;
2928 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2929 err = snd_pcm_hwsync(substream);
2933 snd_pcm_stream_lock_irq(substream);
2934 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2935 err = pcm_lib_apply_appl_ptr(substream,
2936 sync_ptr.c.control.appl_ptr);
2938 snd_pcm_stream_unlock_irq(substream);
2942 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2944 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2945 control->avail_min = sync_ptr.c.control.avail_min;
2947 sync_ptr.c.control.avail_min = control->avail_min;
2948 sync_ptr.s.status.state = status->state;
2949 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2950 sync_ptr.s.status.tstamp = status->tstamp;
2951 sync_ptr.s.status.suspended_state = status->suspended_state;
2952 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2953 snd_pcm_stream_unlock_irq(substream);
2954 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2959 struct snd_pcm_mmap_status32 {
2960 snd_pcm_state_t state;
2965 snd_pcm_state_t suspended_state;
2966 s32 audio_tstamp_sec;
2967 s32 audio_tstamp_nsec;
2968 } __attribute__((packed));
2970 struct snd_pcm_mmap_control32 {
2975 struct snd_pcm_sync_ptr32 {
2978 struct snd_pcm_mmap_status32 status;
2979 unsigned char reserved[64];
2982 struct snd_pcm_mmap_control32 control;
2983 unsigned char reserved[64];
2985 } __attribute__((packed));
2987 /* recalcuate the boundary within 32bit */
2988 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
2990 snd_pcm_uframes_t boundary;
2992 if (! runtime->buffer_size)
2994 boundary = runtime->buffer_size;
2995 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3000 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3001 struct snd_pcm_sync_ptr32 __user *src)
3003 struct snd_pcm_runtime *runtime = substream->runtime;
3004 volatile struct snd_pcm_mmap_status *status;
3005 volatile struct snd_pcm_mmap_control *control;
3007 struct snd_pcm_mmap_control scontrol;
3008 struct snd_pcm_mmap_status sstatus;
3009 snd_pcm_uframes_t boundary;
3012 if (snd_BUG_ON(!runtime))
3015 if (get_user(sflags, &src->flags) ||
3016 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3017 get_user(scontrol.avail_min, &src->c.control.avail_min))
3019 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3020 err = snd_pcm_hwsync(substream);
3024 status = runtime->status;
3025 control = runtime->control;
3026 boundary = recalculate_boundary(runtime);
3028 boundary = 0x7fffffff;
3029 snd_pcm_stream_lock_irq(substream);
3030 /* FIXME: we should consider the boundary for the sync from app */
3031 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
3032 control->appl_ptr = scontrol.appl_ptr;
3034 scontrol.appl_ptr = control->appl_ptr % boundary;
3035 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3036 control->avail_min = scontrol.avail_min;
3038 scontrol.avail_min = control->avail_min;
3039 sstatus.state = status->state;
3040 sstatus.hw_ptr = status->hw_ptr % boundary;
3041 sstatus.tstamp = status->tstamp;
3042 sstatus.suspended_state = status->suspended_state;
3043 sstatus.audio_tstamp = status->audio_tstamp;
3044 snd_pcm_stream_unlock_irq(substream);
3045 if (put_user(sstatus.state, &src->s.status.state) ||
3046 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3047 put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3048 put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3049 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3050 put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3051 put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3052 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3053 put_user(scontrol.avail_min, &src->c.control.avail_min))
3058 #define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3060 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3062 struct snd_pcm_runtime *runtime = substream->runtime;
3065 if (get_user(arg, _arg))
3067 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3069 runtime->tstamp_type = arg;
3073 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3074 struct snd_xferi __user *_xferi)
3076 struct snd_xferi xferi;
3077 struct snd_pcm_runtime *runtime = substream->runtime;
3078 snd_pcm_sframes_t result;
3080 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3082 if (put_user(0, &_xferi->result))
3084 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3086 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3087 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3089 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3090 __put_user(result, &_xferi->result);
3091 return result < 0 ? result : 0;
3094 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3095 struct snd_xfern __user *_xfern)
3097 struct snd_xfern xfern;
3098 struct snd_pcm_runtime *runtime = substream->runtime;
3100 snd_pcm_sframes_t result;
3102 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3104 if (runtime->channels > 128)
3106 if (put_user(0, &_xfern->result))
3108 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3111 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3113 return PTR_ERR(bufs);
3114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3115 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3117 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3119 __put_user(result, &_xfern->result);
3120 return result < 0 ? result : 0;
3123 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3124 snd_pcm_uframes_t __user *_frames)
3126 snd_pcm_uframes_t frames;
3127 snd_pcm_sframes_t result;
3129 if (get_user(frames, _frames))
3131 if (put_user(0, _frames))
3133 result = snd_pcm_rewind(substream, frames);
3134 __put_user(result, _frames);
3135 return result < 0 ? result : 0;
3138 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3139 snd_pcm_uframes_t __user *_frames)
3141 snd_pcm_uframes_t frames;
3142 snd_pcm_sframes_t result;
3144 if (get_user(frames, _frames))
3146 if (put_user(0, _frames))
3148 result = snd_pcm_forward(substream, frames);
3149 __put_user(result, _frames);
3150 return result < 0 ? result : 0;
3153 static int snd_pcm_common_ioctl(struct file *file,
3154 struct snd_pcm_substream *substream,
3155 unsigned int cmd, void __user *arg)
3157 struct snd_pcm_file *pcm_file = file->private_data;
3160 if (PCM_RUNTIME_CHECK(substream))
3163 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
3168 case SNDRV_PCM_IOCTL_PVERSION:
3169 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3170 case SNDRV_PCM_IOCTL_INFO:
3171 return snd_pcm_info_user(substream, arg);
3172 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3174 case SNDRV_PCM_IOCTL_TTSTAMP:
3175 return snd_pcm_tstamp(substream, arg);
3176 case SNDRV_PCM_IOCTL_USER_PVERSION:
3177 if (get_user(pcm_file->user_pversion,
3178 (unsigned int __user *)arg))
3181 case SNDRV_PCM_IOCTL_HW_REFINE:
3182 return snd_pcm_hw_refine_user(substream, arg);
3183 case SNDRV_PCM_IOCTL_HW_PARAMS:
3184 return snd_pcm_hw_params_user(substream, arg);
3185 case SNDRV_PCM_IOCTL_HW_FREE:
3186 return snd_pcm_hw_free(substream);
3187 case SNDRV_PCM_IOCTL_SW_PARAMS:
3188 return snd_pcm_sw_params_user(substream, arg);
3189 case SNDRV_PCM_IOCTL_STATUS32:
3190 return snd_pcm_status_user32(substream, arg, false);
3191 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3192 return snd_pcm_status_user32(substream, arg, true);
3193 case SNDRV_PCM_IOCTL_STATUS64:
3194 return snd_pcm_status_user64(substream, arg, false);
3195 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3196 return snd_pcm_status_user64(substream, arg, true);
3197 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3198 return snd_pcm_channel_info_user(substream, arg);
3199 case SNDRV_PCM_IOCTL_PREPARE:
3200 return snd_pcm_prepare(substream, file);
3201 case SNDRV_PCM_IOCTL_RESET:
3202 return snd_pcm_reset(substream);
3203 case SNDRV_PCM_IOCTL_START:
3204 return snd_pcm_start_lock_irq(substream);
3205 case SNDRV_PCM_IOCTL_LINK:
3206 return snd_pcm_link(substream, (int)(unsigned long) arg);
3207 case SNDRV_PCM_IOCTL_UNLINK:
3208 return snd_pcm_unlink(substream);
3209 case SNDRV_PCM_IOCTL_RESUME:
3210 return snd_pcm_resume(substream);
3211 case SNDRV_PCM_IOCTL_XRUN:
3212 return snd_pcm_xrun(substream);
3213 case SNDRV_PCM_IOCTL_HWSYNC:
3214 return snd_pcm_hwsync(substream);
3215 case SNDRV_PCM_IOCTL_DELAY:
3217 snd_pcm_sframes_t delay;
3218 snd_pcm_sframes_t __user *res = arg;
3221 err = snd_pcm_delay(substream, &delay);
3224 if (put_user(delay, res))
3228 case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3229 return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3230 case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3231 return snd_pcm_sync_ptr(substream, arg);
3232 #ifdef CONFIG_SND_SUPPORT_OLD_API
3233 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3234 return snd_pcm_hw_refine_old_user(substream, arg);
3235 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3236 return snd_pcm_hw_params_old_user(substream, arg);
3238 case SNDRV_PCM_IOCTL_DRAIN:
3239 return snd_pcm_drain(substream, file);
3240 case SNDRV_PCM_IOCTL_DROP:
3241 return snd_pcm_drop(substream);
3242 case SNDRV_PCM_IOCTL_PAUSE:
3243 return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3244 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3245 case SNDRV_PCM_IOCTL_READI_FRAMES:
3246 return snd_pcm_xferi_frames_ioctl(substream, arg);
3247 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3248 case SNDRV_PCM_IOCTL_READN_FRAMES:
3249 return snd_pcm_xfern_frames_ioctl(substream, arg);
3250 case SNDRV_PCM_IOCTL_REWIND:
3251 return snd_pcm_rewind_ioctl(substream, arg);
3252 case SNDRV_PCM_IOCTL_FORWARD:
3253 return snd_pcm_forward_ioctl(substream, arg);
3255 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3259 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3262 struct snd_pcm_file *pcm_file;
3264 pcm_file = file->private_data;
3266 if (((cmd >> 8) & 0xff) != 'A')
3269 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3270 (void __user *)arg);
3274 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3275 * @substream: PCM substream
3277 * @arg: IOCTL argument
3279 * The function is provided primarily for OSS layer and USB gadget drivers,
3280 * and it allows only the limited set of ioctls (hw_params, sw_params,
3281 * prepare, start, drain, drop, forward).
3283 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3284 unsigned int cmd, void *arg)
3286 snd_pcm_uframes_t *frames = arg;
3287 snd_pcm_sframes_t result;
3290 case SNDRV_PCM_IOCTL_FORWARD:
3292 /* provided only for OSS; capture-only and no value returned */
3293 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3295 result = snd_pcm_forward(substream, *frames);
3296 return result < 0 ? result : 0;
3298 case SNDRV_PCM_IOCTL_HW_PARAMS:
3299 return snd_pcm_hw_params(substream, arg);
3300 case SNDRV_PCM_IOCTL_SW_PARAMS:
3301 return snd_pcm_sw_params(substream, arg);
3302 case SNDRV_PCM_IOCTL_PREPARE:
3303 return snd_pcm_prepare(substream, NULL);
3304 case SNDRV_PCM_IOCTL_START:
3305 return snd_pcm_start_lock_irq(substream);
3306 case SNDRV_PCM_IOCTL_DRAIN:
3307 return snd_pcm_drain(substream, NULL);
3308 case SNDRV_PCM_IOCTL_DROP:
3309 return snd_pcm_drop(substream);
3310 case SNDRV_PCM_IOCTL_DELAY:
3311 return snd_pcm_delay(substream, frames);
3316 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3318 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3321 struct snd_pcm_file *pcm_file;
3322 struct snd_pcm_substream *substream;
3323 struct snd_pcm_runtime *runtime;
3324 snd_pcm_sframes_t result;
3326 pcm_file = file->private_data;
3327 substream = pcm_file->substream;
3328 if (PCM_RUNTIME_CHECK(substream))
3330 runtime = substream->runtime;
3331 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3333 if (!frame_aligned(runtime, count))
3335 count = bytes_to_frames(runtime, count);
3336 result = snd_pcm_lib_read(substream, buf, count);
3338 result = frames_to_bytes(runtime, result);
3342 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3343 size_t count, loff_t * offset)
3345 struct snd_pcm_file *pcm_file;
3346 struct snd_pcm_substream *substream;
3347 struct snd_pcm_runtime *runtime;
3348 snd_pcm_sframes_t result;
3350 pcm_file = file->private_data;
3351 substream = pcm_file->substream;
3352 if (PCM_RUNTIME_CHECK(substream))
3354 runtime = substream->runtime;
3355 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3357 if (!frame_aligned(runtime, count))
3359 count = bytes_to_frames(runtime, count);
3360 result = snd_pcm_lib_write(substream, buf, count);
3362 result = frames_to_bytes(runtime, result);
3366 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
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;
3374 snd_pcm_uframes_t frames;
3376 pcm_file = iocb->ki_filp->private_data;
3377 substream = pcm_file->substream;
3378 if (PCM_RUNTIME_CHECK(substream))
3380 runtime = substream->runtime;
3381 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3383 if (!iter_is_iovec(to))
3385 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3387 if (!frame_aligned(runtime, to->iov->iov_len))
3389 frames = bytes_to_samples(runtime, to->iov->iov_len);
3390 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3393 for (i = 0; i < to->nr_segs; ++i)
3394 bufs[i] = to->iov[i].iov_base;
3395 result = snd_pcm_lib_readv(substream, bufs, frames);
3397 result = frames_to_bytes(runtime, result);
3402 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3404 struct snd_pcm_file *pcm_file;
3405 struct snd_pcm_substream *substream;
3406 struct snd_pcm_runtime *runtime;
3407 snd_pcm_sframes_t result;
3410 snd_pcm_uframes_t frames;
3412 pcm_file = iocb->ki_filp->private_data;
3413 substream = pcm_file->substream;
3414 if (PCM_RUNTIME_CHECK(substream))
3416 runtime = substream->runtime;
3417 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3419 if (!iter_is_iovec(from))
3421 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3422 !frame_aligned(runtime, from->iov->iov_len))
3424 frames = bytes_to_samples(runtime, from->iov->iov_len);
3425 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3428 for (i = 0; i < from->nr_segs; ++i)
3429 bufs[i] = from->iov[i].iov_base;
3430 result = snd_pcm_lib_writev(substream, bufs, frames);
3432 result = frames_to_bytes(runtime, result);
3437 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3439 struct snd_pcm_file *pcm_file;
3440 struct snd_pcm_substream *substream;
3441 struct snd_pcm_runtime *runtime;
3443 snd_pcm_uframes_t avail;
3445 pcm_file = file->private_data;
3447 substream = pcm_file->substream;
3448 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3449 ok = EPOLLOUT | EPOLLWRNORM;
3451 ok = EPOLLIN | EPOLLRDNORM;
3452 if (PCM_RUNTIME_CHECK(substream))
3453 return ok | EPOLLERR;
3455 runtime = substream->runtime;
3456 poll_wait(file, &runtime->sleep, wait);
3459 snd_pcm_stream_lock_irq(substream);
3460 avail = snd_pcm_avail(substream);
3461 switch (runtime->status->state) {
3462 case SNDRV_PCM_STATE_RUNNING:
3463 case SNDRV_PCM_STATE_PREPARED:
3464 case SNDRV_PCM_STATE_PAUSED:
3465 if (avail >= runtime->control->avail_min)
3468 case SNDRV_PCM_STATE_DRAINING:
3469 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3476 mask = ok | EPOLLERR;
3479 snd_pcm_stream_unlock_irq(substream);
3488 * Only on coherent architectures, we can mmap the status and the control records
3489 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3491 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3493 * mmap status record
3495 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3497 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3498 struct snd_pcm_runtime *runtime;
3500 if (substream == NULL)
3501 return VM_FAULT_SIGBUS;
3502 runtime = substream->runtime;
3503 vmf->page = virt_to_page(runtime->status);
3504 get_page(vmf->page);
3508 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3510 .fault = snd_pcm_mmap_status_fault,
3513 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3514 struct vm_area_struct *area)
3517 if (!(area->vm_flags & VM_READ))
3519 size = area->vm_end - area->vm_start;
3520 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3522 area->vm_ops = &snd_pcm_vm_ops_status;
3523 area->vm_private_data = substream;
3524 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3529 * mmap control record
3531 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3533 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3534 struct snd_pcm_runtime *runtime;
3536 if (substream == NULL)
3537 return VM_FAULT_SIGBUS;
3538 runtime = substream->runtime;
3539 vmf->page = virt_to_page(runtime->control);
3540 get_page(vmf->page);
3544 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3546 .fault = snd_pcm_mmap_control_fault,
3549 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3550 struct vm_area_struct *area)
3553 if (!(area->vm_flags & VM_READ))
3555 size = area->vm_end - area->vm_start;
3556 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3558 area->vm_ops = &snd_pcm_vm_ops_control;
3559 area->vm_private_data = substream;
3560 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3564 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3566 /* See pcm_control_mmap_allowed() below.
3567 * Since older alsa-lib requires both status and control mmaps to be
3568 * coupled, we have to disable the status mmap for old alsa-lib, too.
3570 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3571 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3576 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3578 if (pcm_file->no_compat_mmap)
3580 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3581 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3582 * thus it effectively assures the manual update of appl_ptr.
3584 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3589 #else /* ! coherent mmap */
3591 * don't support mmap for status and control records.
3593 #define pcm_status_mmap_allowed(pcm_file) false
3594 #define pcm_control_mmap_allowed(pcm_file) false
3596 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3597 struct vm_area_struct *area)
3601 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3602 struct vm_area_struct *area)
3606 #endif /* coherent mmap */
3608 static inline struct page *
3609 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3611 void *vaddr = substream->runtime->dma_area + ofs;
3613 switch (substream->dma_buffer.dev.type) {
3614 #ifdef CONFIG_SND_DMA_SGBUF
3615 case SNDRV_DMA_TYPE_DEV_SG:
3616 case SNDRV_DMA_TYPE_DEV_UC_SG:
3617 return snd_pcm_sgbuf_ops_page(substream, ofs);
3618 #endif /* CONFIG_SND_DMA_SGBUF */
3619 case SNDRV_DMA_TYPE_VMALLOC:
3620 return vmalloc_to_page(vaddr);
3622 return virt_to_page(vaddr);
3627 * fault callback for mmapping a RAM page
3629 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3631 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3632 struct snd_pcm_runtime *runtime;
3633 unsigned long offset;
3637 if (substream == NULL)
3638 return VM_FAULT_SIGBUS;
3639 runtime = substream->runtime;
3640 offset = vmf->pgoff << PAGE_SHIFT;
3641 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3642 if (offset > dma_bytes - PAGE_SIZE)
3643 return VM_FAULT_SIGBUS;
3644 if (substream->ops->page)
3645 page = substream->ops->page(substream, offset);
3647 page = snd_pcm_default_page_ops(substream, offset);
3649 return VM_FAULT_SIGBUS;
3655 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3656 .open = snd_pcm_mmap_data_open,
3657 .close = snd_pcm_mmap_data_close,
3660 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3661 .open = snd_pcm_mmap_data_open,
3662 .close = snd_pcm_mmap_data_close,
3663 .fault = snd_pcm_mmap_data_fault,
3667 * mmap the DMA buffer on RAM
3671 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3672 * @substream: PCM substream
3675 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3676 * this function is invoked implicitly.
3678 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3679 struct vm_area_struct *area)
3681 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3682 #ifdef CONFIG_GENERIC_ALLOCATOR
3683 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3684 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3685 return remap_pfn_range(area, area->vm_start,
3686 substream->dma_buffer.addr >> PAGE_SHIFT,
3687 area->vm_end - area->vm_start, area->vm_page_prot);
3689 #endif /* CONFIG_GENERIC_ALLOCATOR */
3690 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3691 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3692 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3693 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3694 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3696 substream->runtime->dma_area,
3697 substream->runtime->dma_addr,
3698 substream->runtime->dma_bytes);
3699 #endif /* CONFIG_X86 */
3700 /* mmap with fault handler */
3701 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3704 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3707 * mmap the DMA buffer on I/O memory area
3709 #if SNDRV_PCM_INFO_MMAP_IOMEM
3711 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3712 * @substream: PCM substream
3715 * When your hardware uses the iomapped pages as the hardware buffer and
3716 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3717 * is supposed to work only on limited architectures.
3719 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3720 struct vm_area_struct *area)
3722 struct snd_pcm_runtime *runtime = substream->runtime;
3724 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3725 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3727 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3728 #endif /* SNDRV_PCM_INFO_MMAP */
3733 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3734 struct vm_area_struct *area)
3736 struct snd_pcm_runtime *runtime;
3738 unsigned long offset;
3742 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3743 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3746 if (!(area->vm_flags & VM_READ))
3749 runtime = substream->runtime;
3750 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3752 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3754 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3755 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3757 size = area->vm_end - area->vm_start;
3758 offset = area->vm_pgoff << PAGE_SHIFT;
3759 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3760 if ((size_t)size > dma_bytes)
3762 if (offset > dma_bytes - size)
3765 area->vm_ops = &snd_pcm_vm_ops_data;
3766 area->vm_private_data = substream;
3767 if (substream->ops->mmap)
3768 err = substream->ops->mmap(substream, area);
3770 err = snd_pcm_lib_default_mmap(substream, area);
3772 atomic_inc(&substream->mmap_count);
3775 EXPORT_SYMBOL(snd_pcm_mmap_data);
3777 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3779 struct snd_pcm_file * pcm_file;
3780 struct snd_pcm_substream *substream;
3781 unsigned long offset;
3783 pcm_file = file->private_data;
3784 substream = pcm_file->substream;
3785 if (PCM_RUNTIME_CHECK(substream))
3788 offset = area->vm_pgoff << PAGE_SHIFT;
3790 case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3791 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3794 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3795 if (!pcm_status_mmap_allowed(pcm_file))
3797 return snd_pcm_mmap_status(substream, file, area);
3798 case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3799 if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3802 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3803 if (!pcm_control_mmap_allowed(pcm_file))
3805 return snd_pcm_mmap_control(substream, file, area);
3807 return snd_pcm_mmap_data(substream, file, area);
3812 static int snd_pcm_fasync(int fd, struct file * file, int on)
3814 struct snd_pcm_file * pcm_file;
3815 struct snd_pcm_substream *substream;
3816 struct snd_pcm_runtime *runtime;
3818 pcm_file = file->private_data;
3819 substream = pcm_file->substream;
3820 if (PCM_RUNTIME_CHECK(substream))
3822 runtime = substream->runtime;
3823 return fasync_helper(fd, file, on, &runtime->fasync);
3829 #ifdef CONFIG_COMPAT
3830 #include "pcm_compat.c"
3832 #define snd_pcm_ioctl_compat NULL
3836 * To be removed helpers to keep binary compatibility
3839 #ifdef CONFIG_SND_SUPPORT_OLD_API
3840 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3841 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3843 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3844 struct snd_pcm_hw_params_old *oparams)
3848 memset(params, 0, sizeof(*params));
3849 params->flags = oparams->flags;
3850 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3851 params->masks[i].bits[0] = oparams->masks[i];
3852 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3853 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3854 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3855 params->info = oparams->info;
3856 params->msbits = oparams->msbits;
3857 params->rate_num = oparams->rate_num;
3858 params->rate_den = oparams->rate_den;
3859 params->fifo_size = oparams->fifo_size;
3862 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3863 struct snd_pcm_hw_params *params)
3867 memset(oparams, 0, sizeof(*oparams));
3868 oparams->flags = params->flags;
3869 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3870 oparams->masks[i] = params->masks[i].bits[0];
3871 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3872 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3873 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3874 oparams->info = params->info;
3875 oparams->msbits = params->msbits;
3876 oparams->rate_num = params->rate_num;
3877 oparams->rate_den = params->rate_den;
3878 oparams->fifo_size = params->fifo_size;
3881 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3882 struct snd_pcm_hw_params_old __user * _oparams)
3884 struct snd_pcm_hw_params *params;
3885 struct snd_pcm_hw_params_old *oparams = NULL;
3888 params = kmalloc(sizeof(*params), GFP_KERNEL);
3892 oparams = memdup_user(_oparams, sizeof(*oparams));
3893 if (IS_ERR(oparams)) {
3894 err = PTR_ERR(oparams);
3897 snd_pcm_hw_convert_from_old_params(params, oparams);
3898 err = snd_pcm_hw_refine(substream, params);
3902 err = fixup_unreferenced_params(substream, params);
3906 snd_pcm_hw_convert_to_old_params(oparams, params);
3907 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3916 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3917 struct snd_pcm_hw_params_old __user * _oparams)
3919 struct snd_pcm_hw_params *params;
3920 struct snd_pcm_hw_params_old *oparams = NULL;
3923 params = kmalloc(sizeof(*params), GFP_KERNEL);
3927 oparams = memdup_user(_oparams, sizeof(*oparams));
3928 if (IS_ERR(oparams)) {
3929 err = PTR_ERR(oparams);
3933 snd_pcm_hw_convert_from_old_params(params, oparams);
3934 err = snd_pcm_hw_params(substream, params);
3938 snd_pcm_hw_convert_to_old_params(oparams, params);
3939 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3947 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3950 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3953 unsigned long pgoff,
3954 unsigned long flags)
3956 struct snd_pcm_file *pcm_file = file->private_data;
3957 struct snd_pcm_substream *substream = pcm_file->substream;
3958 struct snd_pcm_runtime *runtime = substream->runtime;
3959 unsigned long offset = pgoff << PAGE_SHIFT;
3962 case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3963 return (unsigned long)runtime->status;
3964 case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3965 return (unsigned long)runtime->control;
3967 return (unsigned long)runtime->dma_area + offset;
3971 # define snd_pcm_get_unmapped_area NULL
3978 const struct file_operations snd_pcm_f_ops[2] = {
3980 .owner = THIS_MODULE,
3981 .write = snd_pcm_write,
3982 .write_iter = snd_pcm_writev,
3983 .open = snd_pcm_playback_open,
3984 .release = snd_pcm_release,
3985 .llseek = no_llseek,
3986 .poll = snd_pcm_poll,
3987 .unlocked_ioctl = snd_pcm_ioctl,
3988 .compat_ioctl = snd_pcm_ioctl_compat,
3989 .mmap = snd_pcm_mmap,
3990 .fasync = snd_pcm_fasync,
3991 .get_unmapped_area = snd_pcm_get_unmapped_area,
3994 .owner = THIS_MODULE,
3995 .read = snd_pcm_read,
3996 .read_iter = snd_pcm_readv,
3997 .open = snd_pcm_capture_open,
3998 .release = snd_pcm_release,
3999 .llseek = no_llseek,
4000 .poll = snd_pcm_poll,
4001 .unlocked_ioctl = snd_pcm_ioctl,
4002 .compat_ioctl = snd_pcm_ioctl_compat,
4003 .mmap = snd_pcm_mmap,
4004 .fasync = snd_pcm_fasync,
4005 .get_unmapped_area = snd_pcm_get_unmapped_area,