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, int state)
556 snd_pcm_stream_lock_irq(substream);
557 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
558 substream->runtime->status->state = state;
559 snd_pcm_stream_unlock_irq(substream);
562 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
565 #ifdef CONFIG_SND_PCM_TIMER
566 if (substream->timer)
567 snd_timer_notify(substream->timer, event,
568 &substream->runtime->trigger_tstamp);
572 static void snd_pcm_sync_stop(struct snd_pcm_substream *substream)
574 if (substream->runtime->stop_operating) {
575 substream->runtime->stop_operating = false;
576 if (substream->ops->sync_stop)
577 substream->ops->sync_stop(substream);
578 else if (substream->pcm->card->sync_irq > 0)
579 synchronize_irq(substream->pcm->card->sync_irq);
584 * snd_pcm_hw_param_choose - choose a configuration defined by @params
586 * @params: the hw_params instance
588 * Choose one configuration from configuration space defined by @params.
589 * The configuration chosen is that obtained fixing in this order:
590 * first access, first format, first subformat, min channels,
591 * min rate, min period time, max buffer size, min tick time
593 * Return: Zero if successful, or a negative error code on failure.
595 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
596 struct snd_pcm_hw_params *params)
598 static const int vars[] = {
599 SNDRV_PCM_HW_PARAM_ACCESS,
600 SNDRV_PCM_HW_PARAM_FORMAT,
601 SNDRV_PCM_HW_PARAM_SUBFORMAT,
602 SNDRV_PCM_HW_PARAM_CHANNELS,
603 SNDRV_PCM_HW_PARAM_RATE,
604 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
605 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
606 SNDRV_PCM_HW_PARAM_TICK_TIME,
610 struct snd_mask old_mask;
611 struct snd_interval old_interval;
614 for (v = vars; *v != -1; v++) {
615 /* Keep old parameter to trace. */
616 if (trace_hw_mask_param_enabled()) {
618 old_mask = *hw_param_mask(params, *v);
620 if (trace_hw_interval_param_enabled()) {
621 if (hw_is_interval(*v))
622 old_interval = *hw_param_interval(params, *v);
624 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
625 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
627 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
633 /* Trace the changed parameter. */
634 if (hw_is_mask(*v)) {
635 trace_hw_mask_param(pcm, *v, 0, &old_mask,
636 hw_param_mask(params, *v));
638 if (hw_is_interval(*v)) {
639 trace_hw_interval_param(pcm, *v, 0, &old_interval,
640 hw_param_interval(params, *v));
647 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
648 struct snd_pcm_hw_params *params)
650 struct snd_pcm_runtime *runtime;
653 snd_pcm_uframes_t frames;
655 if (PCM_RUNTIME_CHECK(substream))
657 runtime = substream->runtime;
658 snd_pcm_stream_lock_irq(substream);
659 switch (runtime->status->state) {
660 case SNDRV_PCM_STATE_OPEN:
661 case SNDRV_PCM_STATE_SETUP:
662 case SNDRV_PCM_STATE_PREPARED:
665 snd_pcm_stream_unlock_irq(substream);
668 snd_pcm_stream_unlock_irq(substream);
669 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
670 if (!substream->oss.oss)
672 if (atomic_read(&substream->mmap_count))
675 snd_pcm_sync_stop(substream);
678 err = snd_pcm_hw_refine(substream, params);
682 err = snd_pcm_hw_params_choose(substream, params);
686 err = fixup_unreferenced_params(substream, params);
690 if (substream->managed_buffer_alloc) {
691 err = snd_pcm_lib_malloc_pages(substream,
692 params_buffer_bytes(params));
695 runtime->buffer_changed = err > 0;
698 if (substream->ops->hw_params != NULL) {
699 err = substream->ops->hw_params(substream, params);
704 runtime->access = params_access(params);
705 runtime->format = params_format(params);
706 runtime->subformat = params_subformat(params);
707 runtime->channels = params_channels(params);
708 runtime->rate = params_rate(params);
709 runtime->period_size = params_period_size(params);
710 runtime->periods = params_periods(params);
711 runtime->buffer_size = params_buffer_size(params);
712 runtime->info = params->info;
713 runtime->rate_num = params->rate_num;
714 runtime->rate_den = params->rate_den;
715 runtime->no_period_wakeup =
716 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
717 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
719 bits = snd_pcm_format_physical_width(runtime->format);
720 runtime->sample_bits = bits;
721 bits *= runtime->channels;
722 runtime->frame_bits = bits;
724 while (bits % 8 != 0) {
728 runtime->byte_align = bits / 8;
729 runtime->min_align = frames;
731 /* Default sw params */
732 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
733 runtime->period_step = 1;
734 runtime->control->avail_min = runtime->period_size;
735 runtime->start_threshold = 1;
736 runtime->stop_threshold = runtime->buffer_size;
737 runtime->silence_threshold = 0;
738 runtime->silence_size = 0;
739 runtime->boundary = runtime->buffer_size;
740 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
741 runtime->boundary *= 2;
743 snd_pcm_timer_resolution_change(substream);
744 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
746 if (pm_qos_request_active(&substream->latency_pm_qos_req))
747 pm_qos_remove_request(&substream->latency_pm_qos_req);
748 if ((usecs = period_to_usecs(runtime)) >= 0)
749 pm_qos_add_request(&substream->latency_pm_qos_req,
750 PM_QOS_CPU_DMA_LATENCY, usecs);
753 /* hardware might be unusable from this time,
754 so we force application to retry to set
755 the correct hardware parameter settings */
756 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
757 if (substream->ops->hw_free != NULL)
758 substream->ops->hw_free(substream);
759 if (substream->managed_buffer_alloc)
760 snd_pcm_lib_free_pages(substream);
764 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
765 struct snd_pcm_hw_params __user * _params)
767 struct snd_pcm_hw_params *params;
770 params = memdup_user(_params, sizeof(*params));
772 return PTR_ERR(params);
774 err = snd_pcm_hw_params(substream, params);
778 if (copy_to_user(_params, params, sizeof(*params)))
785 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
787 struct snd_pcm_runtime *runtime;
790 if (PCM_RUNTIME_CHECK(substream))
792 runtime = substream->runtime;
793 snd_pcm_stream_lock_irq(substream);
794 switch (runtime->status->state) {
795 case SNDRV_PCM_STATE_SETUP:
796 case SNDRV_PCM_STATE_PREPARED:
799 snd_pcm_stream_unlock_irq(substream);
802 snd_pcm_stream_unlock_irq(substream);
803 if (atomic_read(&substream->mmap_count))
805 snd_pcm_sync_stop(substream);
806 if (substream->ops->hw_free)
807 result = substream->ops->hw_free(substream);
808 if (substream->managed_buffer_alloc)
809 snd_pcm_lib_free_pages(substream);
810 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
811 pm_qos_remove_request(&substream->latency_pm_qos_req);
815 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
816 struct snd_pcm_sw_params *params)
818 struct snd_pcm_runtime *runtime;
821 if (PCM_RUNTIME_CHECK(substream))
823 runtime = substream->runtime;
824 snd_pcm_stream_lock_irq(substream);
825 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
826 snd_pcm_stream_unlock_irq(substream);
829 snd_pcm_stream_unlock_irq(substream);
831 if (params->tstamp_mode < 0 ||
832 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
834 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
835 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
837 if (params->avail_min == 0)
839 if (params->silence_size >= runtime->boundary) {
840 if (params->silence_threshold != 0)
843 if (params->silence_size > params->silence_threshold)
845 if (params->silence_threshold > runtime->buffer_size)
849 snd_pcm_stream_lock_irq(substream);
850 runtime->tstamp_mode = params->tstamp_mode;
851 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
852 runtime->tstamp_type = params->tstamp_type;
853 runtime->period_step = params->period_step;
854 runtime->control->avail_min = params->avail_min;
855 runtime->start_threshold = params->start_threshold;
856 runtime->stop_threshold = params->stop_threshold;
857 runtime->silence_threshold = params->silence_threshold;
858 runtime->silence_size = params->silence_size;
859 params->boundary = runtime->boundary;
860 if (snd_pcm_running(substream)) {
861 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
862 runtime->silence_size > 0)
863 snd_pcm_playback_silence(substream, ULONG_MAX);
864 err = snd_pcm_update_state(substream, runtime);
866 snd_pcm_stream_unlock_irq(substream);
870 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
871 struct snd_pcm_sw_params __user * _params)
873 struct snd_pcm_sw_params params;
875 if (copy_from_user(¶ms, _params, sizeof(params)))
877 err = snd_pcm_sw_params(substream, ¶ms);
878 if (copy_to_user(_params, ¶ms, sizeof(params)))
883 static inline snd_pcm_uframes_t
884 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
886 snd_pcm_uframes_t delay;
888 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
889 delay = snd_pcm_playback_hw_avail(substream->runtime);
891 delay = snd_pcm_capture_avail(substream->runtime);
892 return delay + substream->runtime->delay;
895 int snd_pcm_status64(struct snd_pcm_substream *substream,
896 struct snd_pcm_status64 *status)
898 struct snd_pcm_runtime *runtime = substream->runtime;
900 snd_pcm_stream_lock_irq(substream);
902 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
903 &runtime->audio_tstamp_config);
905 /* backwards compatible behavior */
906 if (runtime->audio_tstamp_config.type_requested ==
907 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
908 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
909 runtime->audio_tstamp_config.type_requested =
910 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
912 runtime->audio_tstamp_config.type_requested =
913 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
914 runtime->audio_tstamp_report.valid = 0;
916 runtime->audio_tstamp_report.valid = 1;
918 status->state = runtime->status->state;
919 status->suspended_state = runtime->status->suspended_state;
920 if (status->state == SNDRV_PCM_STATE_OPEN)
922 status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
923 status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
924 if (snd_pcm_running(substream)) {
925 snd_pcm_update_hw_ptr(substream);
926 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
927 status->tstamp_sec = runtime->status->tstamp.tv_sec;
928 status->tstamp_nsec =
929 runtime->status->tstamp.tv_nsec;
930 status->driver_tstamp_sec =
931 runtime->driver_tstamp.tv_sec;
932 status->driver_tstamp_nsec =
933 runtime->driver_tstamp.tv_nsec;
934 status->audio_tstamp_sec =
935 runtime->status->audio_tstamp.tv_sec;
936 status->audio_tstamp_nsec =
937 runtime->status->audio_tstamp.tv_nsec;
938 if (runtime->audio_tstamp_report.valid == 1)
939 /* backwards compatibility, no report provided in COMPAT mode */
940 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
941 &status->audio_tstamp_accuracy,
942 &runtime->audio_tstamp_report);
947 /* get tstamp only in fallback mode and only if enabled */
948 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
949 struct timespec64 tstamp;
951 snd_pcm_gettime(runtime, &tstamp);
952 status->tstamp_sec = tstamp.tv_sec;
953 status->tstamp_nsec = tstamp.tv_nsec;
957 status->appl_ptr = runtime->control->appl_ptr;
958 status->hw_ptr = runtime->status->hw_ptr;
959 status->avail = snd_pcm_avail(substream);
960 status->delay = snd_pcm_running(substream) ?
961 snd_pcm_calc_delay(substream) : 0;
962 status->avail_max = runtime->avail_max;
963 status->overrange = runtime->overrange;
964 runtime->avail_max = 0;
965 runtime->overrange = 0;
967 snd_pcm_stream_unlock_irq(substream);
971 static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
972 struct snd_pcm_status64 __user * _status,
975 struct snd_pcm_status64 status;
978 memset(&status, 0, sizeof(status));
980 * with extension, parameters are read/write,
981 * get audio_tstamp_data from user,
982 * ignore rest of status structure
984 if (ext && get_user(status.audio_tstamp_data,
985 (u32 __user *)(&_status->audio_tstamp_data)))
987 res = snd_pcm_status64(substream, &status);
990 if (copy_to_user(_status, &status, sizeof(status)))
995 static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
996 struct snd_pcm_status32 __user * _status,
999 struct snd_pcm_status64 status64;
1000 struct snd_pcm_status32 status32;
1003 memset(&status64, 0, sizeof(status64));
1004 memset(&status32, 0, sizeof(status32));
1006 * with extension, parameters are read/write,
1007 * get audio_tstamp_data from user,
1008 * ignore rest of status structure
1010 if (ext && get_user(status64.audio_tstamp_data,
1011 (u32 __user *)(&_status->audio_tstamp_data)))
1013 res = snd_pcm_status64(substream, &status64);
1017 status32 = (struct snd_pcm_status32) {
1018 .state = status64.state,
1019 .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1020 .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1021 .tstamp_sec = status64.tstamp_sec,
1022 .tstamp_nsec = status64.tstamp_nsec,
1023 .appl_ptr = status64.appl_ptr,
1024 .hw_ptr = status64.hw_ptr,
1025 .delay = status64.delay,
1026 .avail = status64.avail,
1027 .avail_max = status64.avail_max,
1028 .overrange = status64.overrange,
1029 .suspended_state = status64.suspended_state,
1030 .audio_tstamp_data = status64.audio_tstamp_data,
1031 .audio_tstamp_sec = status64.audio_tstamp_sec,
1032 .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1033 .driver_tstamp_sec = status64.audio_tstamp_sec,
1034 .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1035 .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1038 if (copy_to_user(_status, &status32, sizeof(status32)))
1044 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1045 struct snd_pcm_channel_info * info)
1047 struct snd_pcm_runtime *runtime;
1048 unsigned int channel;
1050 channel = info->channel;
1051 runtime = substream->runtime;
1052 snd_pcm_stream_lock_irq(substream);
1053 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1054 snd_pcm_stream_unlock_irq(substream);
1057 snd_pcm_stream_unlock_irq(substream);
1058 if (channel >= runtime->channels)
1060 memset(info, 0, sizeof(*info));
1061 info->channel = channel;
1062 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1065 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1066 struct snd_pcm_channel_info __user * _info)
1068 struct snd_pcm_channel_info info;
1071 if (copy_from_user(&info, _info, sizeof(info)))
1073 res = snd_pcm_channel_info(substream, &info);
1076 if (copy_to_user(_info, &info, sizeof(info)))
1081 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1083 struct snd_pcm_runtime *runtime = substream->runtime;
1084 if (runtime->trigger_master == NULL)
1086 if (runtime->trigger_master == substream) {
1087 if (!runtime->trigger_tstamp_latched)
1088 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1090 snd_pcm_trigger_tstamp(runtime->trigger_master);
1091 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1093 runtime->trigger_master = NULL;
1097 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1098 int (*do_action)(struct snd_pcm_substream *substream, int state);
1099 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1100 void (*post_action)(struct snd_pcm_substream *substream, int state);
1104 * this functions is core for handling of linked stream
1105 * Note: the stream state might be changed also on failure
1106 * Note2: call with calling stream lock + link lock
1108 static int snd_pcm_action_group(const struct action_ops *ops,
1109 struct snd_pcm_substream *substream,
1110 int state, int do_lock)
1112 struct snd_pcm_substream *s = NULL;
1113 struct snd_pcm_substream *s1;
1114 int res = 0, depth = 1;
1116 snd_pcm_group_for_each_entry(s, substream) {
1117 if (do_lock && s != substream) {
1118 if (s->pcm->nonatomic)
1119 mutex_lock_nested(&s->self_group.mutex, depth);
1121 spin_lock_nested(&s->self_group.lock, depth);
1124 res = ops->pre_action(s, state);
1128 snd_pcm_group_for_each_entry(s, substream) {
1129 res = ops->do_action(s, state);
1131 if (ops->undo_action) {
1132 snd_pcm_group_for_each_entry(s1, substream) {
1133 if (s1 == s) /* failed stream */
1135 ops->undo_action(s1, state);
1138 s = NULL; /* unlock all */
1142 snd_pcm_group_for_each_entry(s, substream) {
1143 ops->post_action(s, state);
1147 /* unlock streams */
1148 snd_pcm_group_for_each_entry(s1, substream) {
1149 if (s1 != substream) {
1150 if (s1->pcm->nonatomic)
1151 mutex_unlock(&s1->self_group.mutex);
1153 spin_unlock(&s1->self_group.lock);
1155 if (s1 == s) /* end */
1163 * Note: call with stream lock
1165 static int snd_pcm_action_single(const struct action_ops *ops,
1166 struct snd_pcm_substream *substream,
1171 res = ops->pre_action(substream, state);
1174 res = ops->do_action(substream, state);
1176 ops->post_action(substream, state);
1177 else if (ops->undo_action)
1178 ops->undo_action(substream, state);
1182 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1183 struct snd_pcm_group *new_group)
1185 substream->group = new_group;
1186 list_move(&substream->link_list, &new_group->substreams);
1190 * Unref and unlock the group, but keep the stream lock;
1191 * when the group becomes empty and no longer referred, destroy itself
1193 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1194 struct snd_pcm_substream *substream)
1200 do_free = refcount_dec_and_test(&group->refs);
1201 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1207 * Lock the group inside a stream lock and reference it;
1208 * return the locked group object, or NULL if not linked
1210 static struct snd_pcm_group *
1211 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1213 bool nonatomic = substream->pcm->nonatomic;
1214 struct snd_pcm_group *group;
1218 if (!snd_pcm_stream_linked(substream))
1220 group = substream->group;
1221 /* block freeing the group object */
1222 refcount_inc(&group->refs);
1224 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1225 spin_trylock(&group->lock);
1229 /* re-lock for avoiding ABBA deadlock */
1230 snd_pcm_stream_unlock(substream);
1231 snd_pcm_group_lock(group, nonatomic);
1232 snd_pcm_stream_lock(substream);
1234 /* check the group again; the above opens a small race window */
1235 if (substream->group == group)
1237 /* group changed, try again */
1238 snd_pcm_group_unref(group, substream);
1244 * Note: call with stream lock
1246 static int snd_pcm_action(const struct action_ops *ops,
1247 struct snd_pcm_substream *substream,
1250 struct snd_pcm_group *group;
1253 group = snd_pcm_stream_group_ref(substream);
1255 res = snd_pcm_action_group(ops, substream, state, 1);
1257 res = snd_pcm_action_single(ops, substream, state);
1258 snd_pcm_group_unref(group, substream);
1263 * Note: don't use any locks before
1265 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1266 struct snd_pcm_substream *substream,
1271 snd_pcm_stream_lock_irq(substream);
1272 res = snd_pcm_action(ops, substream, state);
1273 snd_pcm_stream_unlock_irq(substream);
1279 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1280 struct snd_pcm_substream *substream,
1285 /* Guarantee the group members won't change during non-atomic action */
1286 down_read(&snd_pcm_link_rwsem);
1287 if (snd_pcm_stream_linked(substream))
1288 res = snd_pcm_action_group(ops, substream, state, 0);
1290 res = snd_pcm_action_single(ops, substream, state);
1291 up_read(&snd_pcm_link_rwsem);
1298 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1300 struct snd_pcm_runtime *runtime = substream->runtime;
1301 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1303 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1304 !snd_pcm_playback_data(substream))
1306 runtime->trigger_tstamp_latched = false;
1307 runtime->trigger_master = substream;
1311 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1313 if (substream->runtime->trigger_master != substream)
1315 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1318 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1320 if (substream->runtime->trigger_master == substream)
1321 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1324 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1326 struct snd_pcm_runtime *runtime = substream->runtime;
1327 snd_pcm_trigger_tstamp(substream);
1328 runtime->hw_ptr_jiffies = jiffies;
1329 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1331 runtime->status->state = state;
1332 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1333 runtime->silence_size > 0)
1334 snd_pcm_playback_silence(substream, ULONG_MAX);
1335 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1338 static const struct action_ops snd_pcm_action_start = {
1339 .pre_action = snd_pcm_pre_start,
1340 .do_action = snd_pcm_do_start,
1341 .undo_action = snd_pcm_undo_start,
1342 .post_action = snd_pcm_post_start
1346 * snd_pcm_start - start all linked streams
1347 * @substream: the PCM substream instance
1349 * Return: Zero if successful, or a negative error code.
1350 * The stream lock must be acquired before calling this function.
1352 int snd_pcm_start(struct snd_pcm_substream *substream)
1354 return snd_pcm_action(&snd_pcm_action_start, substream,
1355 SNDRV_PCM_STATE_RUNNING);
1358 /* take the stream lock and start the streams */
1359 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1361 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1362 SNDRV_PCM_STATE_RUNNING);
1368 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1370 struct snd_pcm_runtime *runtime = substream->runtime;
1371 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1373 runtime->trigger_master = substream;
1377 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1379 if (substream->runtime->trigger_master == substream &&
1380 snd_pcm_running(substream))
1381 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1382 return 0; /* unconditonally stop all substreams */
1385 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1387 struct snd_pcm_runtime *runtime = substream->runtime;
1388 if (runtime->status->state != state) {
1389 snd_pcm_trigger_tstamp(substream);
1390 runtime->status->state = state;
1391 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1393 runtime->stop_operating = true;
1394 wake_up(&runtime->sleep);
1395 wake_up(&runtime->tsleep);
1398 static const struct action_ops snd_pcm_action_stop = {
1399 .pre_action = snd_pcm_pre_stop,
1400 .do_action = snd_pcm_do_stop,
1401 .post_action = snd_pcm_post_stop
1405 * snd_pcm_stop - try to stop all running streams in the substream group
1406 * @substream: the PCM substream instance
1407 * @state: PCM state after stopping the stream
1409 * The state of each stream is then changed to the given state unconditionally.
1411 * Return: Zero if successful, or a negative error code.
1413 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1415 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1417 EXPORT_SYMBOL(snd_pcm_stop);
1420 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1421 * @substream: the PCM substream
1423 * After stopping, the state is changed to SETUP.
1424 * Unlike snd_pcm_stop(), this affects only the given stream.
1426 * Return: Zero if succesful, or a negative error code.
1428 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1430 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1431 SNDRV_PCM_STATE_SETUP);
1435 * snd_pcm_stop_xrun - stop the running streams as XRUN
1436 * @substream: the PCM substream instance
1438 * This stops the given running substream (and all linked substreams) as XRUN.
1439 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1441 * Return: Zero if successful, or a negative error code.
1443 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1445 unsigned long flags;
1447 snd_pcm_stream_lock_irqsave(substream, flags);
1448 if (substream->runtime && snd_pcm_running(substream))
1449 __snd_pcm_xrun(substream);
1450 snd_pcm_stream_unlock_irqrestore(substream, flags);
1453 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1458 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1460 struct snd_pcm_runtime *runtime = substream->runtime;
1461 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1464 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1466 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1468 runtime->trigger_master = substream;
1472 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1474 if (substream->runtime->trigger_master != substream)
1476 /* some drivers might use hw_ptr to recover from the pause -
1477 update the hw_ptr now */
1479 snd_pcm_update_hw_ptr(substream);
1480 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1481 * a delta between the current jiffies, this gives a large enough
1482 * delta, effectively to skip the check once.
1484 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1485 return substream->ops->trigger(substream,
1486 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1487 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1490 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1492 if (substream->runtime->trigger_master == substream)
1493 substream->ops->trigger(substream,
1494 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1495 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1498 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1500 struct snd_pcm_runtime *runtime = substream->runtime;
1501 snd_pcm_trigger_tstamp(substream);
1503 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1504 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1505 wake_up(&runtime->sleep);
1506 wake_up(&runtime->tsleep);
1508 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1509 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1513 static const struct action_ops snd_pcm_action_pause = {
1514 .pre_action = snd_pcm_pre_pause,
1515 .do_action = snd_pcm_do_pause,
1516 .undo_action = snd_pcm_undo_pause,
1517 .post_action = snd_pcm_post_pause
1521 * Push/release the pause for all linked streams.
1523 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1525 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1531 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1533 struct snd_pcm_runtime *runtime = substream->runtime;
1534 switch (runtime->status->state) {
1535 case SNDRV_PCM_STATE_SUSPENDED:
1537 /* unresumable PCM state; return -EBUSY for skipping suspend */
1538 case SNDRV_PCM_STATE_OPEN:
1539 case SNDRV_PCM_STATE_SETUP:
1540 case SNDRV_PCM_STATE_DISCONNECTED:
1543 runtime->trigger_master = substream;
1547 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1549 struct snd_pcm_runtime *runtime = substream->runtime;
1550 if (runtime->trigger_master != substream)
1552 if (! snd_pcm_running(substream))
1554 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1555 return 0; /* suspend unconditionally */
1558 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1560 struct snd_pcm_runtime *runtime = substream->runtime;
1561 snd_pcm_trigger_tstamp(substream);
1562 runtime->status->suspended_state = runtime->status->state;
1563 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1564 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1565 wake_up(&runtime->sleep);
1566 wake_up(&runtime->tsleep);
1569 static const struct action_ops snd_pcm_action_suspend = {
1570 .pre_action = snd_pcm_pre_suspend,
1571 .do_action = snd_pcm_do_suspend,
1572 .post_action = snd_pcm_post_suspend
1576 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1577 * @substream: the PCM substream
1579 * After this call, all streams are changed to SUSPENDED state.
1581 * Return: Zero if successful, or a negative error code.
1583 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1586 unsigned long flags;
1588 snd_pcm_stream_lock_irqsave(substream, flags);
1589 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1590 snd_pcm_stream_unlock_irqrestore(substream, flags);
1595 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1596 * @pcm: the PCM instance
1598 * After this call, all streams are changed to SUSPENDED state.
1600 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1602 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1604 struct snd_pcm_substream *substream;
1605 int stream, err = 0;
1610 for (stream = 0; stream < 2; stream++) {
1611 for (substream = pcm->streams[stream].substream;
1612 substream; substream = substream->next) {
1613 /* FIXME: the open/close code should lock this as well */
1614 if (substream->runtime == NULL)
1618 * Skip BE dai link PCM's that are internal and may
1619 * not have their substream ops set.
1621 if (!substream->ops)
1624 err = snd_pcm_suspend(substream);
1625 if (err < 0 && err != -EBUSY)
1631 EXPORT_SYMBOL(snd_pcm_suspend_all);
1635 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1637 struct snd_pcm_runtime *runtime = substream->runtime;
1638 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1640 runtime->trigger_master = substream;
1644 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1646 struct snd_pcm_runtime *runtime = substream->runtime;
1647 if (runtime->trigger_master != substream)
1649 /* DMA not running previously? */
1650 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1651 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1652 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1654 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1657 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1659 if (substream->runtime->trigger_master == substream &&
1660 snd_pcm_running(substream))
1661 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1664 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1666 struct snd_pcm_runtime *runtime = substream->runtime;
1667 snd_pcm_trigger_tstamp(substream);
1668 runtime->status->state = runtime->status->suspended_state;
1669 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1670 snd_pcm_sync_stop(substream);
1673 static const struct action_ops snd_pcm_action_resume = {
1674 .pre_action = snd_pcm_pre_resume,
1675 .do_action = snd_pcm_do_resume,
1676 .undo_action = snd_pcm_undo_resume,
1677 .post_action = snd_pcm_post_resume
1680 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1682 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1687 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1692 #endif /* CONFIG_PM */
1697 * Change the RUNNING stream(s) to XRUN state.
1699 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1701 struct snd_pcm_runtime *runtime = substream->runtime;
1704 snd_pcm_stream_lock_irq(substream);
1705 switch (runtime->status->state) {
1706 case SNDRV_PCM_STATE_XRUN:
1707 result = 0; /* already there */
1709 case SNDRV_PCM_STATE_RUNNING:
1710 __snd_pcm_xrun(substream);
1716 snd_pcm_stream_unlock_irq(substream);
1723 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1725 struct snd_pcm_runtime *runtime = substream->runtime;
1726 switch (runtime->status->state) {
1727 case SNDRV_PCM_STATE_RUNNING:
1728 case SNDRV_PCM_STATE_PREPARED:
1729 case SNDRV_PCM_STATE_PAUSED:
1730 case SNDRV_PCM_STATE_SUSPENDED:
1737 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1739 struct snd_pcm_runtime *runtime = substream->runtime;
1740 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1743 runtime->hw_ptr_base = 0;
1744 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1745 runtime->status->hw_ptr % runtime->period_size;
1746 runtime->silence_start = runtime->status->hw_ptr;
1747 runtime->silence_filled = 0;
1751 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1753 struct snd_pcm_runtime *runtime = substream->runtime;
1754 runtime->control->appl_ptr = runtime->status->hw_ptr;
1755 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1756 runtime->silence_size > 0)
1757 snd_pcm_playback_silence(substream, ULONG_MAX);
1760 static const struct action_ops snd_pcm_action_reset = {
1761 .pre_action = snd_pcm_pre_reset,
1762 .do_action = snd_pcm_do_reset,
1763 .post_action = snd_pcm_post_reset
1766 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1768 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1774 /* we use the second argument for updating f_flags */
1775 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1778 struct snd_pcm_runtime *runtime = substream->runtime;
1779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1780 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1782 if (snd_pcm_running(substream))
1784 substream->f_flags = f_flags;
1788 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1791 snd_pcm_sync_stop(substream);
1792 err = substream->ops->prepare(substream);
1795 return snd_pcm_do_reset(substream, 0);
1798 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1800 struct snd_pcm_runtime *runtime = substream->runtime;
1801 runtime->control->appl_ptr = runtime->status->hw_ptr;
1802 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1805 static const struct action_ops snd_pcm_action_prepare = {
1806 .pre_action = snd_pcm_pre_prepare,
1807 .do_action = snd_pcm_do_prepare,
1808 .post_action = snd_pcm_post_prepare
1812 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1813 * @substream: the PCM substream instance
1814 * @file: file to refer f_flags
1816 * Return: Zero if successful, or a negative error code.
1818 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1824 f_flags = file->f_flags;
1826 f_flags = substream->f_flags;
1828 snd_pcm_stream_lock_irq(substream);
1829 switch (substream->runtime->status->state) {
1830 case SNDRV_PCM_STATE_PAUSED:
1831 snd_pcm_pause(substream, 0);
1833 case SNDRV_PCM_STATE_SUSPENDED:
1834 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1837 snd_pcm_stream_unlock_irq(substream);
1839 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1840 substream, f_flags);
1847 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1849 struct snd_pcm_runtime *runtime = substream->runtime;
1850 switch (runtime->status->state) {
1851 case SNDRV_PCM_STATE_OPEN:
1852 case SNDRV_PCM_STATE_DISCONNECTED:
1853 case SNDRV_PCM_STATE_SUSPENDED:
1856 runtime->trigger_master = substream;
1860 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1862 struct snd_pcm_runtime *runtime = substream->runtime;
1863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1864 switch (runtime->status->state) {
1865 case SNDRV_PCM_STATE_PREPARED:
1866 /* start playback stream if possible */
1867 if (! snd_pcm_playback_empty(substream)) {
1868 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1869 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1871 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1874 case SNDRV_PCM_STATE_RUNNING:
1875 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1877 case SNDRV_PCM_STATE_XRUN:
1878 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1884 /* stop running stream */
1885 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1886 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1887 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1888 snd_pcm_do_stop(substream, new_state);
1889 snd_pcm_post_stop(substream, new_state);
1893 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1894 runtime->trigger_master == substream &&
1895 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1896 return substream->ops->trigger(substream,
1897 SNDRV_PCM_TRIGGER_DRAIN);
1902 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1906 static const struct action_ops snd_pcm_action_drain_init = {
1907 .pre_action = snd_pcm_pre_drain_init,
1908 .do_action = snd_pcm_do_drain_init,
1909 .post_action = snd_pcm_post_drain_init
1913 * Drain the stream(s).
1914 * When the substream is linked, sync until the draining of all playback streams
1916 * After this call, all streams are supposed to be either SETUP or DRAINING
1917 * (capture only) state.
1919 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1922 struct snd_card *card;
1923 struct snd_pcm_runtime *runtime;
1924 struct snd_pcm_substream *s;
1925 struct snd_pcm_group *group;
1926 wait_queue_entry_t wait;
1930 card = substream->pcm->card;
1931 runtime = substream->runtime;
1933 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1937 if (file->f_flags & O_NONBLOCK)
1939 } else if (substream->f_flags & O_NONBLOCK)
1942 snd_pcm_stream_lock_irq(substream);
1944 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1945 snd_pcm_pause(substream, 0);
1947 /* pre-start/stop - all running streams are changed to DRAINING state */
1948 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1951 /* in non-blocking, we don't wait in ioctl but let caller poll */
1959 struct snd_pcm_runtime *to_check;
1960 if (signal_pending(current)) {
1961 result = -ERESTARTSYS;
1964 /* find a substream to drain */
1966 group = snd_pcm_stream_group_ref(substream);
1967 snd_pcm_group_for_each_entry(s, substream) {
1968 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1970 runtime = s->runtime;
1971 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1976 snd_pcm_group_unref(group, substream);
1978 break; /* all drained */
1979 init_waitqueue_entry(&wait, current);
1980 set_current_state(TASK_INTERRUPTIBLE);
1981 add_wait_queue(&to_check->sleep, &wait);
1982 snd_pcm_stream_unlock_irq(substream);
1983 if (runtime->no_period_wakeup)
1984 tout = MAX_SCHEDULE_TIMEOUT;
1987 if (runtime->rate) {
1988 long t = runtime->period_size * 2 / runtime->rate;
1989 tout = max(t, tout);
1991 tout = msecs_to_jiffies(tout * 1000);
1993 tout = schedule_timeout(tout);
1995 snd_pcm_stream_lock_irq(substream);
1996 group = snd_pcm_stream_group_ref(substream);
1997 snd_pcm_group_for_each_entry(s, substream) {
1998 if (s->runtime == to_check) {
1999 remove_wait_queue(&to_check->sleep, &wait);
2003 snd_pcm_group_unref(group, substream);
2005 if (card->shutdown) {
2010 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2013 dev_dbg(substream->pcm->card->dev,
2014 "playback drain error (DMA or IRQ trouble?)\n");
2015 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2023 snd_pcm_stream_unlock_irq(substream);
2031 * Immediately put all linked substreams into SETUP state.
2033 static int snd_pcm_drop(struct snd_pcm_substream *substream)
2035 struct snd_pcm_runtime *runtime;
2038 if (PCM_RUNTIME_CHECK(substream))
2040 runtime = substream->runtime;
2042 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2043 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2046 snd_pcm_stream_lock_irq(substream);
2048 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2049 snd_pcm_pause(substream, 0);
2051 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2052 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2053 snd_pcm_stream_unlock_irq(substream);
2059 static bool is_pcm_file(struct file *file)
2061 struct inode *inode = file_inode(file);
2062 struct snd_pcm *pcm;
2065 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2067 minor = iminor(inode);
2068 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2070 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2073 snd_card_unref(pcm->card);
2080 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2083 struct snd_pcm_file *pcm_file;
2084 struct snd_pcm_substream *substream1;
2085 struct snd_pcm_group *group, *target_group;
2086 bool nonatomic = substream->pcm->nonatomic;
2087 struct fd f = fdget(fd);
2091 if (!is_pcm_file(f.file)) {
2095 pcm_file = f.file->private_data;
2096 substream1 = pcm_file->substream;
2097 group = kzalloc(sizeof(*group), GFP_KERNEL);
2102 snd_pcm_group_init(group);
2104 down_write(&snd_pcm_link_rwsem);
2105 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2106 substream->runtime->status->state != substream1->runtime->status->state ||
2107 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2111 if (snd_pcm_stream_linked(substream1)) {
2116 snd_pcm_stream_lock_irq(substream);
2117 if (!snd_pcm_stream_linked(substream)) {
2118 snd_pcm_group_assign(substream, group);
2119 group = NULL; /* assigned, don't free this one below */
2121 target_group = substream->group;
2122 snd_pcm_stream_unlock_irq(substream);
2124 snd_pcm_group_lock_irq(target_group, nonatomic);
2125 snd_pcm_stream_lock(substream1);
2126 snd_pcm_group_assign(substream1, target_group);
2127 refcount_inc(&target_group->refs);
2128 snd_pcm_stream_unlock(substream1);
2129 snd_pcm_group_unlock_irq(target_group, nonatomic);
2131 up_write(&snd_pcm_link_rwsem);
2139 static void relink_to_local(struct snd_pcm_substream *substream)
2141 snd_pcm_stream_lock(substream);
2142 snd_pcm_group_assign(substream, &substream->self_group);
2143 snd_pcm_stream_unlock(substream);
2146 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2148 struct snd_pcm_group *group;
2149 bool nonatomic = substream->pcm->nonatomic;
2150 bool do_free = false;
2153 down_write(&snd_pcm_link_rwsem);
2155 if (!snd_pcm_stream_linked(substream)) {
2160 group = substream->group;
2161 snd_pcm_group_lock_irq(group, nonatomic);
2163 relink_to_local(substream);
2164 refcount_dec(&group->refs);
2166 /* detach the last stream, too */
2167 if (list_is_singular(&group->substreams)) {
2168 relink_to_local(list_first_entry(&group->substreams,
2169 struct snd_pcm_substream,
2171 do_free = refcount_dec_and_test(&group->refs);
2174 snd_pcm_group_unlock_irq(group, nonatomic);
2179 up_write(&snd_pcm_link_rwsem);
2186 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2187 struct snd_pcm_hw_rule *rule)
2189 struct snd_interval t;
2190 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2191 hw_param_interval_c(params, rule->deps[1]), &t);
2192 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2195 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2196 struct snd_pcm_hw_rule *rule)
2198 struct snd_interval t;
2199 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2200 hw_param_interval_c(params, rule->deps[1]), &t);
2201 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2204 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2205 struct snd_pcm_hw_rule *rule)
2207 struct snd_interval t;
2208 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2209 hw_param_interval_c(params, rule->deps[1]),
2210 (unsigned long) rule->private, &t);
2211 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2214 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2215 struct snd_pcm_hw_rule *rule)
2217 struct snd_interval t;
2218 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2219 (unsigned long) rule->private,
2220 hw_param_interval_c(params, rule->deps[1]), &t);
2221 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2224 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2225 struct snd_pcm_hw_rule *rule)
2228 const struct snd_interval *i =
2229 hw_param_interval_c(params, rule->deps[0]);
2231 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2233 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2235 if (! snd_mask_test(mask, k))
2237 bits = snd_pcm_format_physical_width(k);
2239 continue; /* ignore invalid formats */
2240 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2241 snd_mask_reset(&m, k);
2243 return snd_mask_refine(mask, &m);
2246 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2247 struct snd_pcm_hw_rule *rule)
2249 struct snd_interval t;
2255 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2257 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2259 bits = snd_pcm_format_physical_width(k);
2261 continue; /* ignore invalid formats */
2262 if (t.min > (unsigned)bits)
2264 if (t.max < (unsigned)bits)
2268 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2271 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2272 #error "Change this table"
2275 static const unsigned int rates[] = {
2276 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2277 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2280 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2281 .count = ARRAY_SIZE(rates),
2285 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2286 struct snd_pcm_hw_rule *rule)
2288 struct snd_pcm_hardware *hw = rule->private;
2289 return snd_interval_list(hw_param_interval(params, rule->var),
2290 snd_pcm_known_rates.count,
2291 snd_pcm_known_rates.list, hw->rates);
2294 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2295 struct snd_pcm_hw_rule *rule)
2297 struct snd_interval t;
2298 struct snd_pcm_substream *substream = rule->private;
2300 t.max = substream->buffer_bytes_max;
2304 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2307 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2309 struct snd_pcm_runtime *runtime = substream->runtime;
2310 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2313 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2314 snd_mask_any(constrs_mask(constrs, k));
2317 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2318 snd_interval_any(constrs_interval(constrs, k));
2321 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2322 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2323 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2324 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2325 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2327 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2328 snd_pcm_hw_rule_format, NULL,
2329 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2332 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2333 snd_pcm_hw_rule_sample_bits, NULL,
2334 SNDRV_PCM_HW_PARAM_FORMAT,
2335 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2338 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2339 snd_pcm_hw_rule_div, NULL,
2340 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2343 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2344 snd_pcm_hw_rule_mul, NULL,
2345 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2348 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2349 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2350 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2353 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2354 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2355 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2358 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2359 snd_pcm_hw_rule_div, NULL,
2360 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2363 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2364 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2365 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2368 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2369 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2370 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2373 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2374 snd_pcm_hw_rule_div, NULL,
2375 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2378 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2379 snd_pcm_hw_rule_div, NULL,
2380 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2383 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2384 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2385 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2388 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2389 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2390 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2393 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2394 snd_pcm_hw_rule_mul, NULL,
2395 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2398 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2399 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2400 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2403 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2404 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2405 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2408 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2409 snd_pcm_hw_rule_muldivk, (void*) 8,
2410 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2413 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2414 snd_pcm_hw_rule_muldivk, (void*) 8,
2415 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2418 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2419 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2420 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2423 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2424 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2425 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2431 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2433 struct snd_pcm_runtime *runtime = substream->runtime;
2434 struct snd_pcm_hardware *hw = &runtime->hw;
2436 unsigned int mask = 0;
2438 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2439 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2440 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2441 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2442 if (hw_support_mmap(substream)) {
2443 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2444 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2445 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2446 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2447 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2448 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2450 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2454 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2458 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2462 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2463 hw->channels_min, hw->channels_max);
2467 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2468 hw->rate_min, hw->rate_max);
2472 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2473 hw->period_bytes_min, hw->period_bytes_max);
2477 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2478 hw->periods_min, hw->periods_max);
2482 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2483 hw->period_bytes_min, hw->buffer_bytes_max);
2487 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2488 snd_pcm_hw_rule_buffer_bytes_max, substream,
2489 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2494 if (runtime->dma_bytes) {
2495 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2500 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2501 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2502 snd_pcm_hw_rule_rate, hw,
2503 SNDRV_PCM_HW_PARAM_RATE, -1);
2508 /* FIXME: this belong to lowlevel */
2509 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2514 static void pcm_release_private(struct snd_pcm_substream *substream)
2516 if (snd_pcm_stream_linked(substream))
2517 snd_pcm_unlink(substream);
2520 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2522 substream->ref_count--;
2523 if (substream->ref_count > 0)
2526 snd_pcm_drop(substream);
2527 if (substream->hw_opened) {
2528 if (substream->ops->hw_free &&
2529 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2530 substream->ops->hw_free(substream);
2531 substream->ops->close(substream);
2532 substream->hw_opened = 0;
2534 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2535 pm_qos_remove_request(&substream->latency_pm_qos_req);
2536 if (substream->pcm_release) {
2537 substream->pcm_release(substream);
2538 substream->pcm_release = NULL;
2540 snd_pcm_detach_substream(substream);
2542 EXPORT_SYMBOL(snd_pcm_release_substream);
2544 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2546 struct snd_pcm_substream **rsubstream)
2548 struct snd_pcm_substream *substream;
2551 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2554 if (substream->ref_count > 1) {
2555 *rsubstream = substream;
2559 err = snd_pcm_hw_constraints_init(substream);
2561 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2565 if ((err = substream->ops->open(substream)) < 0)
2568 substream->hw_opened = 1;
2570 err = snd_pcm_hw_constraints_complete(substream);
2572 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2576 *rsubstream = substream;
2580 snd_pcm_release_substream(substream);
2583 EXPORT_SYMBOL(snd_pcm_open_substream);
2585 static int snd_pcm_open_file(struct file *file,
2586 struct snd_pcm *pcm,
2589 struct snd_pcm_file *pcm_file;
2590 struct snd_pcm_substream *substream;
2593 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2597 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2598 if (pcm_file == NULL) {
2599 snd_pcm_release_substream(substream);
2602 pcm_file->substream = substream;
2603 if (substream->ref_count == 1)
2604 substream->pcm_release = pcm_release_private;
2605 file->private_data = pcm_file;
2610 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2612 struct snd_pcm *pcm;
2613 int err = nonseekable_open(inode, file);
2616 pcm = snd_lookup_minor_data(iminor(inode),
2617 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2618 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2620 snd_card_unref(pcm->card);
2624 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2626 struct snd_pcm *pcm;
2627 int err = nonseekable_open(inode, file);
2630 pcm = snd_lookup_minor_data(iminor(inode),
2631 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2632 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2634 snd_card_unref(pcm->card);
2638 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2641 wait_queue_entry_t wait;
2647 err = snd_card_file_add(pcm->card, file);
2650 if (!try_module_get(pcm->card->module)) {
2654 init_waitqueue_entry(&wait, current);
2655 add_wait_queue(&pcm->open_wait, &wait);
2656 mutex_lock(&pcm->open_mutex);
2658 err = snd_pcm_open_file(file, pcm, stream);
2661 if (err == -EAGAIN) {
2662 if (file->f_flags & O_NONBLOCK) {
2668 set_current_state(TASK_INTERRUPTIBLE);
2669 mutex_unlock(&pcm->open_mutex);
2671 mutex_lock(&pcm->open_mutex);
2672 if (pcm->card->shutdown) {
2676 if (signal_pending(current)) {
2681 remove_wait_queue(&pcm->open_wait, &wait);
2682 mutex_unlock(&pcm->open_mutex);
2688 module_put(pcm->card->module);
2690 snd_card_file_remove(pcm->card, file);
2695 static int snd_pcm_release(struct inode *inode, struct file *file)
2697 struct snd_pcm *pcm;
2698 struct snd_pcm_substream *substream;
2699 struct snd_pcm_file *pcm_file;
2701 pcm_file = file->private_data;
2702 substream = pcm_file->substream;
2703 if (snd_BUG_ON(!substream))
2705 pcm = substream->pcm;
2706 mutex_lock(&pcm->open_mutex);
2707 snd_pcm_release_substream(substream);
2709 mutex_unlock(&pcm->open_mutex);
2710 wake_up(&pcm->open_wait);
2711 module_put(pcm->card->module);
2712 snd_card_file_remove(pcm->card, file);
2716 /* check and update PCM state; return 0 or a negative error
2717 * call this inside PCM lock
2719 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2721 switch (substream->runtime->status->state) {
2722 case SNDRV_PCM_STATE_DRAINING:
2723 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2726 case SNDRV_PCM_STATE_RUNNING:
2727 return snd_pcm_update_hw_ptr(substream);
2728 case SNDRV_PCM_STATE_PREPARED:
2729 case SNDRV_PCM_STATE_PAUSED:
2731 case SNDRV_PCM_STATE_SUSPENDED:
2733 case SNDRV_PCM_STATE_XRUN:
2740 /* increase the appl_ptr; returns the processed frames or a negative error */
2741 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2742 snd_pcm_uframes_t frames,
2743 snd_pcm_sframes_t avail)
2745 struct snd_pcm_runtime *runtime = substream->runtime;
2746 snd_pcm_sframes_t appl_ptr;
2751 if (frames > (snd_pcm_uframes_t)avail)
2753 appl_ptr = runtime->control->appl_ptr + frames;
2754 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2755 appl_ptr -= runtime->boundary;
2756 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2757 return ret < 0 ? ret : frames;
2760 /* decrease the appl_ptr; returns the processed frames or zero for error */
2761 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2762 snd_pcm_uframes_t frames,
2763 snd_pcm_sframes_t avail)
2765 struct snd_pcm_runtime *runtime = substream->runtime;
2766 snd_pcm_sframes_t appl_ptr;
2771 if (frames > (snd_pcm_uframes_t)avail)
2773 appl_ptr = runtime->control->appl_ptr - frames;
2775 appl_ptr += runtime->boundary;
2776 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2777 /* NOTE: we return zero for errors because PulseAudio gets depressed
2778 * upon receiving an error from rewind ioctl and stops processing
2779 * any longer. Returning zero means that no rewind is done, so
2780 * it's not absolutely wrong to answer like that.
2782 return ret < 0 ? 0 : frames;
2785 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2786 snd_pcm_uframes_t frames)
2788 snd_pcm_sframes_t ret;
2793 snd_pcm_stream_lock_irq(substream);
2794 ret = do_pcm_hwsync(substream);
2796 ret = rewind_appl_ptr(substream, frames,
2797 snd_pcm_hw_avail(substream));
2798 snd_pcm_stream_unlock_irq(substream);
2802 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2803 snd_pcm_uframes_t frames)
2805 snd_pcm_sframes_t ret;
2810 snd_pcm_stream_lock_irq(substream);
2811 ret = do_pcm_hwsync(substream);
2813 ret = forward_appl_ptr(substream, frames,
2814 snd_pcm_avail(substream));
2815 snd_pcm_stream_unlock_irq(substream);
2819 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2823 snd_pcm_stream_lock_irq(substream);
2824 err = do_pcm_hwsync(substream);
2825 snd_pcm_stream_unlock_irq(substream);
2829 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2830 snd_pcm_sframes_t *delay)
2833 snd_pcm_sframes_t n = 0;
2835 snd_pcm_stream_lock_irq(substream);
2836 err = do_pcm_hwsync(substream);
2838 n = snd_pcm_calc_delay(substream);
2839 snd_pcm_stream_unlock_irq(substream);
2845 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2846 struct snd_pcm_sync_ptr __user *_sync_ptr)
2848 struct snd_pcm_runtime *runtime = substream->runtime;
2849 struct snd_pcm_sync_ptr sync_ptr;
2850 volatile struct snd_pcm_mmap_status *status;
2851 volatile struct snd_pcm_mmap_control *control;
2854 memset(&sync_ptr, 0, sizeof(sync_ptr));
2855 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2857 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2859 status = runtime->status;
2860 control = runtime->control;
2861 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2862 err = snd_pcm_hwsync(substream);
2866 snd_pcm_stream_lock_irq(substream);
2867 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2868 err = pcm_lib_apply_appl_ptr(substream,
2869 sync_ptr.c.control.appl_ptr);
2871 snd_pcm_stream_unlock_irq(substream);
2875 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2877 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2878 control->avail_min = sync_ptr.c.control.avail_min;
2880 sync_ptr.c.control.avail_min = control->avail_min;
2881 sync_ptr.s.status.state = status->state;
2882 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2883 sync_ptr.s.status.tstamp = status->tstamp;
2884 sync_ptr.s.status.suspended_state = status->suspended_state;
2885 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2886 snd_pcm_stream_unlock_irq(substream);
2887 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2892 #ifdef CONFIG_COMPAT
2893 struct snd_pcm_mmap_status32 {
2897 struct compat_timespec tstamp;
2898 s32 suspended_state;
2899 struct compat_timespec audio_tstamp;
2900 } __attribute__((packed));
2902 struct snd_pcm_mmap_control32 {
2907 struct snd_pcm_sync_ptr32 {
2910 struct snd_pcm_mmap_status32 status;
2911 unsigned char reserved[64];
2914 struct snd_pcm_mmap_control32 control;
2915 unsigned char reserved[64];
2917 } __attribute__((packed));
2919 /* recalcuate the boundary within 32bit */
2920 static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
2922 snd_pcm_uframes_t boundary;
2924 if (! runtime->buffer_size)
2926 boundary = runtime->buffer_size;
2927 while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
2932 static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
2933 struct snd_pcm_sync_ptr32 __user *src)
2935 struct snd_pcm_runtime *runtime = substream->runtime;
2936 volatile struct snd_pcm_mmap_status *status;
2937 volatile struct snd_pcm_mmap_control *control;
2939 struct snd_pcm_mmap_control scontrol;
2940 struct snd_pcm_mmap_status sstatus;
2941 snd_pcm_uframes_t boundary;
2944 if (snd_BUG_ON(!runtime))
2947 if (get_user(sflags, &src->flags) ||
2948 get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
2949 get_user(scontrol.avail_min, &src->c.control.avail_min))
2951 if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2952 err = snd_pcm_hwsync(substream);
2956 status = runtime->status;
2957 control = runtime->control;
2958 boundary = recalculate_boundary(runtime);
2960 boundary = 0x7fffffff;
2961 snd_pcm_stream_lock_irq(substream);
2962 /* FIXME: we should consider the boundary for the sync from app */
2963 if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL))
2964 control->appl_ptr = scontrol.appl_ptr;
2966 scontrol.appl_ptr = control->appl_ptr % boundary;
2967 if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2968 control->avail_min = scontrol.avail_min;
2970 scontrol.avail_min = control->avail_min;
2971 sstatus.state = status->state;
2972 sstatus.hw_ptr = status->hw_ptr % boundary;
2973 sstatus.tstamp = status->tstamp;
2974 sstatus.suspended_state = status->suspended_state;
2975 sstatus.audio_tstamp = status->audio_tstamp;
2976 snd_pcm_stream_unlock_irq(substream);
2977 if (put_user(sstatus.state, &src->s.status.state) ||
2978 put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
2979 compat_put_timespec(&sstatus.tstamp, &src->s.status.tstamp) ||
2980 put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
2981 compat_put_timespec(&sstatus.audio_tstamp,
2982 &src->s.status.audio_tstamp) ||
2983 put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
2984 put_user(scontrol.avail_min, &src->c.control.avail_min))
2991 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2993 struct snd_pcm_runtime *runtime = substream->runtime;
2996 if (get_user(arg, _arg))
2998 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3000 runtime->tstamp_type = arg;
3004 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3005 struct snd_xferi __user *_xferi)
3007 struct snd_xferi xferi;
3008 struct snd_pcm_runtime *runtime = substream->runtime;
3009 snd_pcm_sframes_t result;
3011 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3013 if (put_user(0, &_xferi->result))
3015 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3017 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3018 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3020 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3021 __put_user(result, &_xferi->result);
3022 return result < 0 ? result : 0;
3025 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3026 struct snd_xfern __user *_xfern)
3028 struct snd_xfern xfern;
3029 struct snd_pcm_runtime *runtime = substream->runtime;
3031 snd_pcm_sframes_t result;
3033 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3035 if (runtime->channels > 128)
3037 if (put_user(0, &_xfern->result))
3039 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3042 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3044 return PTR_ERR(bufs);
3045 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3046 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3048 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3050 __put_user(result, &_xfern->result);
3051 return result < 0 ? result : 0;
3054 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3055 snd_pcm_uframes_t __user *_frames)
3057 snd_pcm_uframes_t frames;
3058 snd_pcm_sframes_t result;
3060 if (get_user(frames, _frames))
3062 if (put_user(0, _frames))
3064 result = snd_pcm_rewind(substream, frames);
3065 __put_user(result, _frames);
3066 return result < 0 ? result : 0;
3069 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3070 snd_pcm_uframes_t __user *_frames)
3072 snd_pcm_uframes_t frames;
3073 snd_pcm_sframes_t result;
3075 if (get_user(frames, _frames))
3077 if (put_user(0, _frames))
3079 result = snd_pcm_forward(substream, frames);
3080 __put_user(result, _frames);
3081 return result < 0 ? result : 0;
3084 static int snd_pcm_common_ioctl(struct file *file,
3085 struct snd_pcm_substream *substream,
3086 unsigned int cmd, void __user *arg)
3088 struct snd_pcm_file *pcm_file = file->private_data;
3091 if (PCM_RUNTIME_CHECK(substream))
3094 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
3099 case SNDRV_PCM_IOCTL_PVERSION:
3100 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3101 case SNDRV_PCM_IOCTL_INFO:
3102 return snd_pcm_info_user(substream, arg);
3103 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
3105 case SNDRV_PCM_IOCTL_TTSTAMP:
3106 return snd_pcm_tstamp(substream, arg);
3107 case SNDRV_PCM_IOCTL_USER_PVERSION:
3108 if (get_user(pcm_file->user_pversion,
3109 (unsigned int __user *)arg))
3112 case SNDRV_PCM_IOCTL_HW_REFINE:
3113 return snd_pcm_hw_refine_user(substream, arg);
3114 case SNDRV_PCM_IOCTL_HW_PARAMS:
3115 return snd_pcm_hw_params_user(substream, arg);
3116 case SNDRV_PCM_IOCTL_HW_FREE:
3117 return snd_pcm_hw_free(substream);
3118 case SNDRV_PCM_IOCTL_SW_PARAMS:
3119 return snd_pcm_sw_params_user(substream, arg);
3120 case SNDRV_PCM_IOCTL_STATUS32:
3121 return snd_pcm_status_user32(substream, arg, false);
3122 case SNDRV_PCM_IOCTL_STATUS_EXT32:
3123 return snd_pcm_status_user32(substream, arg, true);
3124 case SNDRV_PCM_IOCTL_STATUS64:
3125 return snd_pcm_status_user64(substream, arg, false);
3126 case SNDRV_PCM_IOCTL_STATUS_EXT64:
3127 return snd_pcm_status_user64(substream, arg, true);
3128 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3129 return snd_pcm_channel_info_user(substream, arg);
3130 case SNDRV_PCM_IOCTL_PREPARE:
3131 return snd_pcm_prepare(substream, file);
3132 case SNDRV_PCM_IOCTL_RESET:
3133 return snd_pcm_reset(substream);
3134 case SNDRV_PCM_IOCTL_START:
3135 return snd_pcm_start_lock_irq(substream);
3136 case SNDRV_PCM_IOCTL_LINK:
3137 return snd_pcm_link(substream, (int)(unsigned long) arg);
3138 case SNDRV_PCM_IOCTL_UNLINK:
3139 return snd_pcm_unlink(substream);
3140 case SNDRV_PCM_IOCTL_RESUME:
3141 return snd_pcm_resume(substream);
3142 case SNDRV_PCM_IOCTL_XRUN:
3143 return snd_pcm_xrun(substream);
3144 case SNDRV_PCM_IOCTL_HWSYNC:
3145 return snd_pcm_hwsync(substream);
3146 case SNDRV_PCM_IOCTL_DELAY:
3148 snd_pcm_sframes_t delay;
3149 snd_pcm_sframes_t __user *res = arg;
3152 err = snd_pcm_delay(substream, &delay);
3155 if (put_user(delay, res))
3159 case SNDRV_PCM_IOCTL_SYNC_PTR:
3160 return snd_pcm_sync_ptr(substream, arg);
3161 #ifdef CONFIG_SND_SUPPORT_OLD_API
3162 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3163 return snd_pcm_hw_refine_old_user(substream, arg);
3164 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3165 return snd_pcm_hw_params_old_user(substream, arg);
3167 case SNDRV_PCM_IOCTL_DRAIN:
3168 return snd_pcm_drain(substream, file);
3169 case SNDRV_PCM_IOCTL_DROP:
3170 return snd_pcm_drop(substream);
3171 case SNDRV_PCM_IOCTL_PAUSE:
3172 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3174 (int)(unsigned long)arg);
3175 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3176 case SNDRV_PCM_IOCTL_READI_FRAMES:
3177 return snd_pcm_xferi_frames_ioctl(substream, arg);
3178 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3179 case SNDRV_PCM_IOCTL_READN_FRAMES:
3180 return snd_pcm_xfern_frames_ioctl(substream, arg);
3181 case SNDRV_PCM_IOCTL_REWIND:
3182 return snd_pcm_rewind_ioctl(substream, arg);
3183 case SNDRV_PCM_IOCTL_FORWARD:
3184 return snd_pcm_forward_ioctl(substream, arg);
3186 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3190 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3193 struct snd_pcm_file *pcm_file;
3195 pcm_file = file->private_data;
3197 if (((cmd >> 8) & 0xff) != 'A')
3200 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3201 (void __user *)arg);
3205 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3206 * @substream: PCM substream
3208 * @arg: IOCTL argument
3210 * The function is provided primarily for OSS layer and USB gadget drivers,
3211 * and it allows only the limited set of ioctls (hw_params, sw_params,
3212 * prepare, start, drain, drop, forward).
3214 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3215 unsigned int cmd, void *arg)
3217 snd_pcm_uframes_t *frames = arg;
3218 snd_pcm_sframes_t result;
3221 case SNDRV_PCM_IOCTL_FORWARD:
3223 /* provided only for OSS; capture-only and no value returned */
3224 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3226 result = snd_pcm_forward(substream, *frames);
3227 return result < 0 ? result : 0;
3229 case SNDRV_PCM_IOCTL_HW_PARAMS:
3230 return snd_pcm_hw_params(substream, arg);
3231 case SNDRV_PCM_IOCTL_SW_PARAMS:
3232 return snd_pcm_sw_params(substream, arg);
3233 case SNDRV_PCM_IOCTL_PREPARE:
3234 return snd_pcm_prepare(substream, NULL);
3235 case SNDRV_PCM_IOCTL_START:
3236 return snd_pcm_start_lock_irq(substream);
3237 case SNDRV_PCM_IOCTL_DRAIN:
3238 return snd_pcm_drain(substream, NULL);
3239 case SNDRV_PCM_IOCTL_DROP:
3240 return snd_pcm_drop(substream);
3241 case SNDRV_PCM_IOCTL_DELAY:
3242 return snd_pcm_delay(substream, frames);
3247 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3249 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3252 struct snd_pcm_file *pcm_file;
3253 struct snd_pcm_substream *substream;
3254 struct snd_pcm_runtime *runtime;
3255 snd_pcm_sframes_t result;
3257 pcm_file = file->private_data;
3258 substream = pcm_file->substream;
3259 if (PCM_RUNTIME_CHECK(substream))
3261 runtime = substream->runtime;
3262 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3264 if (!frame_aligned(runtime, count))
3266 count = bytes_to_frames(runtime, count);
3267 result = snd_pcm_lib_read(substream, buf, count);
3269 result = frames_to_bytes(runtime, result);
3273 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3274 size_t count, loff_t * offset)
3276 struct snd_pcm_file *pcm_file;
3277 struct snd_pcm_substream *substream;
3278 struct snd_pcm_runtime *runtime;
3279 snd_pcm_sframes_t result;
3281 pcm_file = file->private_data;
3282 substream = pcm_file->substream;
3283 if (PCM_RUNTIME_CHECK(substream))
3285 runtime = substream->runtime;
3286 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3288 if (!frame_aligned(runtime, count))
3290 count = bytes_to_frames(runtime, count);
3291 result = snd_pcm_lib_write(substream, buf, count);
3293 result = frames_to_bytes(runtime, result);
3297 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3299 struct snd_pcm_file *pcm_file;
3300 struct snd_pcm_substream *substream;
3301 struct snd_pcm_runtime *runtime;
3302 snd_pcm_sframes_t result;
3305 snd_pcm_uframes_t frames;
3307 pcm_file = iocb->ki_filp->private_data;
3308 substream = pcm_file->substream;
3309 if (PCM_RUNTIME_CHECK(substream))
3311 runtime = substream->runtime;
3312 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3314 if (!iter_is_iovec(to))
3316 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3318 if (!frame_aligned(runtime, to->iov->iov_len))
3320 frames = bytes_to_samples(runtime, to->iov->iov_len);
3321 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3324 for (i = 0; i < to->nr_segs; ++i)
3325 bufs[i] = to->iov[i].iov_base;
3326 result = snd_pcm_lib_readv(substream, bufs, frames);
3328 result = frames_to_bytes(runtime, result);
3333 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3335 struct snd_pcm_file *pcm_file;
3336 struct snd_pcm_substream *substream;
3337 struct snd_pcm_runtime *runtime;
3338 snd_pcm_sframes_t result;
3341 snd_pcm_uframes_t frames;
3343 pcm_file = iocb->ki_filp->private_data;
3344 substream = pcm_file->substream;
3345 if (PCM_RUNTIME_CHECK(substream))
3347 runtime = substream->runtime;
3348 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3350 if (!iter_is_iovec(from))
3352 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3353 !frame_aligned(runtime, from->iov->iov_len))
3355 frames = bytes_to_samples(runtime, from->iov->iov_len);
3356 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3359 for (i = 0; i < from->nr_segs; ++i)
3360 bufs[i] = from->iov[i].iov_base;
3361 result = snd_pcm_lib_writev(substream, bufs, frames);
3363 result = frames_to_bytes(runtime, result);
3368 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3370 struct snd_pcm_file *pcm_file;
3371 struct snd_pcm_substream *substream;
3372 struct snd_pcm_runtime *runtime;
3374 snd_pcm_uframes_t avail;
3376 pcm_file = file->private_data;
3378 substream = pcm_file->substream;
3379 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3380 ok = EPOLLOUT | EPOLLWRNORM;
3382 ok = EPOLLIN | EPOLLRDNORM;
3383 if (PCM_RUNTIME_CHECK(substream))
3384 return ok | EPOLLERR;
3386 runtime = substream->runtime;
3387 poll_wait(file, &runtime->sleep, wait);
3390 snd_pcm_stream_lock_irq(substream);
3391 avail = snd_pcm_avail(substream);
3392 switch (runtime->status->state) {
3393 case SNDRV_PCM_STATE_RUNNING:
3394 case SNDRV_PCM_STATE_PREPARED:
3395 case SNDRV_PCM_STATE_PAUSED:
3396 if (avail >= runtime->control->avail_min)
3399 case SNDRV_PCM_STATE_DRAINING: