1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8 #include <linux/module.h>
9 #include <linux/file.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/time.h>
13 #include <linux/pm_qos.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/vmalloc.h>
17 #include <sound/core.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/timer.h>
23 #include <sound/minors.h>
24 #include <linux/uio.h>
25 #include <linux/delay.h>
27 #include "pcm_local.h"
29 #ifdef CONFIG_SND_DEBUG
30 #define CREATE_TRACE_POINTS
31 #include "pcm_param_trace.h"
33 #define trace_hw_mask_param_enabled() 0
34 #define trace_hw_interval_param_enabled() 0
35 #define trace_hw_mask_param(substream, type, index, prev, curr)
36 #define trace_hw_interval_param(substream, type, index, prev, curr)
43 struct snd_pcm_hw_params_old {
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 static DECLARE_RWSEM(snd_pcm_link_rwsem);
76 void snd_pcm_group_init(struct snd_pcm_group *group)
78 spin_lock_init(&group->lock);
79 mutex_init(&group->mutex);
80 INIT_LIST_HEAD(&group->substreams);
81 refcount_set(&group->refs, 1);
84 /* define group lock helpers */
85 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
86 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
89 mutex_ ## mutex_action(&group->mutex); \
91 spin_ ## action(&group->lock); \
94 DEFINE_PCM_GROUP_LOCK(lock, lock);
95 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
96 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
97 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
100 * snd_pcm_stream_lock - Lock the PCM stream
101 * @substream: PCM substream
103 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
104 * flag of the given substream. This also takes the global link rw lock
105 * (or rw sem), too, for avoiding the race with linked streams.
107 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
109 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
111 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
114 * snd_pcm_stream_lock - Unlock the PCM stream
115 * @substream: PCM substream
117 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
119 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
121 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
123 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
126 * snd_pcm_stream_lock_irq - Lock the PCM stream
127 * @substream: PCM substream
129 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
130 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
131 * as snd_pcm_stream_lock().
133 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
135 snd_pcm_group_lock_irq(&substream->self_group,
136 substream->pcm->nonatomic);
138 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
141 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
142 * @substream: PCM substream
144 * This is a counter-part of snd_pcm_stream_lock_irq().
146 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
148 snd_pcm_group_unlock_irq(&substream->self_group,
149 substream->pcm->nonatomic);
151 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
153 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
155 unsigned long flags = 0;
156 if (substream->pcm->nonatomic)
157 mutex_lock(&substream->self_group.mutex);
159 spin_lock_irqsave(&substream->self_group.lock, flags);
162 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
165 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
166 * @substream: PCM substream
169 * This is a counter-part of snd_pcm_stream_lock_irqsave().
171 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
174 if (substream->pcm->nonatomic)
175 mutex_unlock(&substream->self_group.mutex);
177 spin_unlock_irqrestore(&substream->self_group.lock, flags);
179 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
181 /* Run PCM ioctl ops */
182 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
183 unsigned cmd, void *arg)
185 if (substream->ops->ioctl)
186 return substream->ops->ioctl(substream, cmd, arg);
188 return snd_pcm_lib_ioctl(substream, cmd, arg);
191 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
193 struct snd_pcm *pcm = substream->pcm;
194 struct snd_pcm_str *pstr = substream->pstr;
196 memset(info, 0, sizeof(*info));
197 info->card = pcm->card->number;
198 info->device = pcm->device;
199 info->stream = substream->stream;
200 info->subdevice = substream->number;
201 strlcpy(info->id, pcm->id, sizeof(info->id));
202 strlcpy(info->name, pcm->name, sizeof(info->name));
203 info->dev_class = pcm->dev_class;
204 info->dev_subclass = pcm->dev_subclass;
205 info->subdevices_count = pstr->substream_count;
206 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
207 strlcpy(info->subname, substream->name, sizeof(info->subname));
212 int snd_pcm_info_user(struct snd_pcm_substream *substream,
213 struct snd_pcm_info __user * _info)
215 struct snd_pcm_info *info;
218 info = kmalloc(sizeof(*info), GFP_KERNEL);
221 err = snd_pcm_info(substream, info);
223 if (copy_to_user(_info, info, sizeof(*info)))
230 static bool hw_support_mmap(struct snd_pcm_substream *substream)
232 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
235 if (substream->ops->mmap ||
236 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
237 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
240 return dma_can_mmap(substream->dma_buffer.dev.dev);
243 static int constrain_mask_params(struct snd_pcm_substream *substream,
244 struct snd_pcm_hw_params *params)
246 struct snd_pcm_hw_constraints *constrs =
247 &substream->runtime->hw_constraints;
250 struct snd_mask old_mask;
253 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
254 m = hw_param_mask(params, k);
255 if (snd_mask_empty(m))
258 /* This parameter is not requested to change by a caller. */
259 if (!(params->rmask & (1 << k)))
262 if (trace_hw_mask_param_enabled())
265 changed = snd_mask_refine(m, constrs_mask(constrs, k));
271 /* Set corresponding flag so that the caller gets it. */
272 trace_hw_mask_param(substream, k, 0, &old_mask, m);
273 params->cmask |= 1 << k;
279 static int constrain_interval_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params)
282 struct snd_pcm_hw_constraints *constrs =
283 &substream->runtime->hw_constraints;
284 struct snd_interval *i;
286 struct snd_interval old_interval;
289 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
290 i = hw_param_interval(params, k);
291 if (snd_interval_empty(i))
294 /* This parameter is not requested to change by a caller. */
295 if (!(params->rmask & (1 << k)))
298 if (trace_hw_interval_param_enabled())
301 changed = snd_interval_refine(i, constrs_interval(constrs, k));
307 /* Set corresponding flag so that the caller gets it. */
308 trace_hw_interval_param(substream, k, 0, &old_interval, i);
309 params->cmask |= 1 << k;
315 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
316 struct snd_pcm_hw_params *params)
318 struct snd_pcm_hw_constraints *constrs =
319 &substream->runtime->hw_constraints;
321 unsigned int *rstamps;
322 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
324 struct snd_pcm_hw_rule *r;
326 struct snd_mask old_mask;
327 struct snd_interval old_interval;
329 int changed, err = 0;
332 * Each application of rule has own sequence number.
334 * Each member of 'rstamps' array represents the sequence number of
335 * recent application of corresponding rule.
337 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
342 * Each member of 'vstamps' array represents the sequence number of
343 * recent application of rule in which corresponding parameters were
346 * In initial state, elements corresponding to parameters requested by
347 * a caller is 1. For unrequested parameters, corresponding members
348 * have 0 so that the parameters are never changed anymore.
350 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
351 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
353 /* Due to the above design, actual sequence number starts at 2. */
356 /* Apply all rules in order. */
358 for (k = 0; k < constrs->rules_num; k++) {
359 r = &constrs->rules[k];
362 * Check condition bits of this rule. When the rule has
363 * some condition bits, parameter without the bits is
364 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
365 * is an example of the condition bits.
367 if (r->cond && !(r->cond & params->flags))
371 * The 'deps' array includes maximum three dependencies
372 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
373 * member of this array is a sentinel and should be
376 * This rule should be processed in this time when dependent
377 * parameters were changed at former applications of the other
380 for (d = 0; r->deps[d] >= 0; d++) {
381 if (vstamps[r->deps[d]] > rstamps[k])
387 if (trace_hw_mask_param_enabled()) {
388 if (hw_is_mask(r->var))
389 old_mask = *hw_param_mask(params, r->var);
391 if (trace_hw_interval_param_enabled()) {
392 if (hw_is_interval(r->var))
393 old_interval = *hw_param_interval(params, r->var);
396 changed = r->func(params, r);
403 * When the parameter is changed, notify it to the caller
404 * by corresponding returned bit, then preparing for next
407 if (changed && r->var >= 0) {
408 if (hw_is_mask(r->var)) {
409 trace_hw_mask_param(substream, r->var,
411 hw_param_mask(params, r->var));
413 if (hw_is_interval(r->var)) {
414 trace_hw_interval_param(substream, r->var,
415 k + 1, &old_interval,
416 hw_param_interval(params, r->var));
419 params->cmask |= (1 << r->var);
420 vstamps[r->var] = stamp;
424 rstamps[k] = stamp++;
427 /* Iterate to evaluate all rules till no parameters are changed. */
436 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
437 struct snd_pcm_hw_params *params)
439 const struct snd_interval *i;
440 const struct snd_mask *m;
443 if (!params->msbits) {
444 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
445 if (snd_interval_single(i))
446 params->msbits = snd_interval_value(i);
449 if (!params->rate_den) {
450 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
451 if (snd_interval_single(i)) {
452 params->rate_num = snd_interval_value(i);
453 params->rate_den = 1;
457 if (!params->fifo_size) {
458 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
459 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
460 if (snd_mask_single(m) && snd_interval_single(i)) {
461 err = snd_pcm_ops_ioctl(substream,
462 SNDRV_PCM_IOCTL1_FIFO_SIZE,
470 params->info = substream->runtime->hw.info;
471 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
472 SNDRV_PCM_INFO_DRAIN_TRIGGER);
473 if (!hw_support_mmap(substream))
474 params->info &= ~(SNDRV_PCM_INFO_MMAP |
475 SNDRV_PCM_INFO_MMAP_VALID);
481 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params)
487 params->fifo_size = 0;
488 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
490 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
491 params->rate_num = 0;
492 params->rate_den = 0;
495 err = constrain_mask_params(substream, params);
499 err = constrain_interval_params(substream, params);
503 err = constrain_params_by_rules(substream, params);
511 EXPORT_SYMBOL(snd_pcm_hw_refine);
513 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
514 struct snd_pcm_hw_params __user * _params)
516 struct snd_pcm_hw_params *params;
519 params = memdup_user(_params, sizeof(*params));
521 return PTR_ERR(params);
523 err = snd_pcm_hw_refine(substream, params);
527 err = fixup_unreferenced_params(substream, params);
531 if (copy_to_user(_params, params, sizeof(*params)))
538 static int period_to_usecs(struct snd_pcm_runtime *runtime)
543 return -1; /* invalid */
545 /* take 75% of period time as the deadline */
546 usecs = (750000 / runtime->rate) * runtime->period_size;
547 usecs += ((750000 % runtime->rate) * runtime->period_size) /
553 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
555 snd_pcm_stream_lock_irq(substream);
556 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
557 substream->runtime->status->state = state;
558 snd_pcm_stream_unlock_irq(substream);
561 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
564 #ifdef CONFIG_SND_PCM_TIMER
565 if (substream->timer)
566 snd_timer_notify(substream->timer, event,
567 &substream->runtime->trigger_tstamp);
571 static void snd_pcm_sync_stop(struct snd_pcm_substream *substream)
573 if (substream->runtime->stop_operating) {
574 substream->runtime->stop_operating = false;
575 if (substream->ops->sync_stop)
576 substream->ops->sync_stop(substream);
577 else if (substream->pcm->card->sync_irq > 0)
578 synchronize_irq(substream->pcm->card->sync_irq);
583 * snd_pcm_hw_param_choose - choose a configuration defined by @params
585 * @params: the hw_params instance
587 * Choose one configuration from configuration space defined by @params.
588 * The configuration chosen is that obtained fixing in this order:
589 * first access, first format, first subformat, min channels,
590 * min rate, min period time, max buffer size, min tick time
592 * Return: Zero if successful, or a negative error code on failure.
594 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
595 struct snd_pcm_hw_params *params)
597 static const int vars[] = {
598 SNDRV_PCM_HW_PARAM_ACCESS,
599 SNDRV_PCM_HW_PARAM_FORMAT,
600 SNDRV_PCM_HW_PARAM_SUBFORMAT,
601 SNDRV_PCM_HW_PARAM_CHANNELS,
602 SNDRV_PCM_HW_PARAM_RATE,
603 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
604 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
605 SNDRV_PCM_HW_PARAM_TICK_TIME,
609 struct snd_mask old_mask;
610 struct snd_interval old_interval;
613 for (v = vars; *v != -1; v++) {
614 /* Keep old parameter to trace. */
615 if (trace_hw_mask_param_enabled()) {
617 old_mask = *hw_param_mask(params, *v);
619 if (trace_hw_interval_param_enabled()) {
620 if (hw_is_interval(*v))
621 old_interval = *hw_param_interval(params, *v);
623 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
624 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
626 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
632 /* Trace the changed parameter. */
633 if (hw_is_mask(*v)) {
634 trace_hw_mask_param(pcm, *v, 0, &old_mask,
635 hw_param_mask(params, *v));
637 if (hw_is_interval(*v)) {
638 trace_hw_interval_param(pcm, *v, 0, &old_interval,
639 hw_param_interval(params, *v));
646 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
647 struct snd_pcm_hw_params *params)
649 struct snd_pcm_runtime *runtime;
652 snd_pcm_uframes_t frames;
654 if (PCM_RUNTIME_CHECK(substream))
656 runtime = substream->runtime;
657 snd_pcm_stream_lock_irq(substream);
658 switch (runtime->status->state) {
659 case SNDRV_PCM_STATE_OPEN:
660 case SNDRV_PCM_STATE_SETUP:
661 case SNDRV_PCM_STATE_PREPARED:
664 snd_pcm_stream_unlock_irq(substream);
667 snd_pcm_stream_unlock_irq(substream);
668 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
669 if (!substream->oss.oss)
671 if (atomic_read(&substream->mmap_count))
674 snd_pcm_sync_stop(substream);
677 err = snd_pcm_hw_refine(substream, params);
681 err = snd_pcm_hw_params_choose(substream, params);
685 err = fixup_unreferenced_params(substream, params);
689 if (substream->managed_buffer_alloc) {
690 err = snd_pcm_lib_malloc_pages(substream,
691 params_buffer_bytes(params));
694 runtime->buffer_changed = err > 0;
697 if (substream->ops->hw_params != NULL) {
698 err = substream->ops->hw_params(substream, params);
703 runtime->access = params_access(params);
704 runtime->format = params_format(params);
705 runtime->subformat = params_subformat(params);
706 runtime->channels = params_channels(params);
707 runtime->rate = params_rate(params);
708 runtime->period_size = params_period_size(params);
709 runtime->periods = params_periods(params);
710 runtime->buffer_size = params_buffer_size(params);
711 runtime->info = params->info;
712 runtime->rate_num = params->rate_num;
713 runtime->rate_den = params->rate_den;
714 runtime->no_period_wakeup =
715 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
716 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
718 bits = snd_pcm_format_physical_width(runtime->format);
719 runtime->sample_bits = bits;
720 bits *= runtime->channels;
721 runtime->frame_bits = bits;
723 while (bits % 8 != 0) {
727 runtime->byte_align = bits / 8;
728 runtime->min_align = frames;
730 /* Default sw params */
731 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
732 runtime->period_step = 1;
733 runtime->control->avail_min = runtime->period_size;
734 runtime->start_threshold = 1;
735 runtime->stop_threshold = runtime->buffer_size;
736 runtime->silence_threshold = 0;
737 runtime->silence_size = 0;
738 runtime->boundary = runtime->buffer_size;
739 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
740 runtime->boundary *= 2;
742 snd_pcm_timer_resolution_change(substream);
743 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
745 if (pm_qos_request_active(&substream->latency_pm_qos_req))
746 pm_qos_remove_request(&substream->latency_pm_qos_req);
747 if ((usecs = period_to_usecs(runtime)) >= 0)
748 pm_qos_add_request(&substream->latency_pm_qos_req,
749 PM_QOS_CPU_DMA_LATENCY, usecs);
752 /* hardware might be unusable from this time,
753 so we force application to retry to set
754 the correct hardware parameter settings */
755 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
756 if (substream->ops->hw_free != NULL)
757 substream->ops->hw_free(substream);
758 if (substream->managed_buffer_alloc)
759 snd_pcm_lib_free_pages(substream);
763 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
764 struct snd_pcm_hw_params __user * _params)
766 struct snd_pcm_hw_params *params;
769 params = memdup_user(_params, sizeof(*params));
771 return PTR_ERR(params);
773 err = snd_pcm_hw_params(substream, params);
777 if (copy_to_user(_params, params, sizeof(*params)))
784 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
786 struct snd_pcm_runtime *runtime;
789 if (PCM_RUNTIME_CHECK(substream))
791 runtime = substream->runtime;
792 snd_pcm_stream_lock_irq(substream);
793 switch (runtime->status->state) {
794 case SNDRV_PCM_STATE_SETUP:
795 case SNDRV_PCM_STATE_PREPARED:
798 snd_pcm_stream_unlock_irq(substream);
801 snd_pcm_stream_unlock_irq(substream);
802 if (atomic_read(&substream->mmap_count))
804 snd_pcm_sync_stop(substream);
805 if (substream->ops->hw_free)
806 result = substream->ops->hw_free(substream);
807 if (substream->managed_buffer_alloc)
808 snd_pcm_lib_free_pages(substream);
809 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
810 pm_qos_remove_request(&substream->latency_pm_qos_req);
814 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
815 struct snd_pcm_sw_params *params)
817 struct snd_pcm_runtime *runtime;
820 if (PCM_RUNTIME_CHECK(substream))
822 runtime = substream->runtime;
823 snd_pcm_stream_lock_irq(substream);
824 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
825 snd_pcm_stream_unlock_irq(substream);
828 snd_pcm_stream_unlock_irq(substream);
830 if (params->tstamp_mode < 0 ||
831 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
833 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
834 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
836 if (params->avail_min == 0)
838 if (params->silence_size >= runtime->boundary) {
839 if (params->silence_threshold != 0)
842 if (params->silence_size > params->silence_threshold)
844 if (params->silence_threshold > runtime->buffer_size)
848 snd_pcm_stream_lock_irq(substream);
849 runtime->tstamp_mode = params->tstamp_mode;
850 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
851 runtime->tstamp_type = params->tstamp_type;
852 runtime->period_step = params->period_step;
853 runtime->control->avail_min = params->avail_min;
854 runtime->start_threshold = params->start_threshold;
855 runtime->stop_threshold = params->stop_threshold;
856 runtime->silence_threshold = params->silence_threshold;
857 runtime->silence_size = params->silence_size;
858 params->boundary = runtime->boundary;
859 if (snd_pcm_running(substream)) {
860 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
861 runtime->silence_size > 0)
862 snd_pcm_playback_silence(substream, ULONG_MAX);
863 err = snd_pcm_update_state(substream, runtime);
865 snd_pcm_stream_unlock_irq(substream);
869 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
870 struct snd_pcm_sw_params __user * _params)
872 struct snd_pcm_sw_params params;
874 if (copy_from_user(¶ms, _params, sizeof(params)))
876 err = snd_pcm_sw_params(substream, ¶ms);
877 if (copy_to_user(_params, ¶ms, sizeof(params)))
882 static inline snd_pcm_uframes_t
883 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
885 snd_pcm_uframes_t delay;
887 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
888 delay = snd_pcm_playback_hw_avail(substream->runtime);
890 delay = snd_pcm_capture_avail(substream->runtime);
891 return delay + substream->runtime->delay;
894 int snd_pcm_status(struct snd_pcm_substream *substream,
895 struct snd_pcm_status *status)
897 struct snd_pcm_runtime *runtime = substream->runtime;
899 snd_pcm_stream_lock_irq(substream);
901 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
902 &runtime->audio_tstamp_config);
904 /* backwards compatible behavior */
905 if (runtime->audio_tstamp_config.type_requested ==
906 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
907 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
908 runtime->audio_tstamp_config.type_requested =
909 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
911 runtime->audio_tstamp_config.type_requested =
912 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
913 runtime->audio_tstamp_report.valid = 0;
915 runtime->audio_tstamp_report.valid = 1;
917 status->state = runtime->status->state;
918 status->suspended_state = runtime->status->suspended_state;
919 if (status->state == SNDRV_PCM_STATE_OPEN)
921 status->trigger_tstamp = runtime->trigger_tstamp;
922 if (snd_pcm_running(substream)) {
923 snd_pcm_update_hw_ptr(substream);
924 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
925 status->tstamp = runtime->status->tstamp;
926 status->driver_tstamp = runtime->driver_tstamp;
927 status->audio_tstamp =
928 runtime->status->audio_tstamp;
929 if (runtime->audio_tstamp_report.valid == 1)
930 /* backwards compatibility, no report provided in COMPAT mode */
931 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
932 &status->audio_tstamp_accuracy,
933 &runtime->audio_tstamp_report);
938 /* get tstamp only in fallback mode and only if enabled */
939 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
940 snd_pcm_gettime(runtime, &status->tstamp);
943 status->appl_ptr = runtime->control->appl_ptr;
944 status->hw_ptr = runtime->status->hw_ptr;
945 status->avail = snd_pcm_avail(substream);
946 status->delay = snd_pcm_running(substream) ?
947 snd_pcm_calc_delay(substream) : 0;
948 status->avail_max = runtime->avail_max;
949 status->overrange = runtime->overrange;
950 runtime->avail_max = 0;
951 runtime->overrange = 0;
953 snd_pcm_stream_unlock_irq(substream);
957 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
958 struct snd_pcm_status __user * _status,
961 struct snd_pcm_status status;
964 memset(&status, 0, sizeof(status));
966 * with extension, parameters are read/write,
967 * get audio_tstamp_data from user,
968 * ignore rest of status structure
970 if (ext && get_user(status.audio_tstamp_data,
971 (u32 __user *)(&_status->audio_tstamp_data)))
973 res = snd_pcm_status(substream, &status);
976 if (copy_to_user(_status, &status, sizeof(status)))
981 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
982 struct snd_pcm_channel_info * info)
984 struct snd_pcm_runtime *runtime;
985 unsigned int channel;
987 channel = info->channel;
988 runtime = substream->runtime;
989 snd_pcm_stream_lock_irq(substream);
990 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
991 snd_pcm_stream_unlock_irq(substream);
994 snd_pcm_stream_unlock_irq(substream);
995 if (channel >= runtime->channels)
997 memset(info, 0, sizeof(*info));
998 info->channel = channel;
999 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1002 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1003 struct snd_pcm_channel_info __user * _info)
1005 struct snd_pcm_channel_info info;
1008 if (copy_from_user(&info, _info, sizeof(info)))
1010 res = snd_pcm_channel_info(substream, &info);
1013 if (copy_to_user(_info, &info, sizeof(info)))
1018 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1020 struct snd_pcm_runtime *runtime = substream->runtime;
1021 if (runtime->trigger_master == NULL)
1023 if (runtime->trigger_master == substream) {
1024 if (!runtime->trigger_tstamp_latched)
1025 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1027 snd_pcm_trigger_tstamp(runtime->trigger_master);
1028 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1030 runtime->trigger_master = NULL;
1034 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1035 int (*do_action)(struct snd_pcm_substream *substream, int state);
1036 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1037 void (*post_action)(struct snd_pcm_substream *substream, int state);
1041 * this functions is core for handling of linked stream
1042 * Note: the stream state might be changed also on failure
1043 * Note2: call with calling stream lock + link lock
1045 static int snd_pcm_action_group(const struct action_ops *ops,
1046 struct snd_pcm_substream *substream,
1047 int state, int do_lock)
1049 struct snd_pcm_substream *s = NULL;
1050 struct snd_pcm_substream *s1;
1051 int res = 0, depth = 1;
1053 snd_pcm_group_for_each_entry(s, substream) {
1054 if (do_lock && s != substream) {
1055 if (s->pcm->nonatomic)
1056 mutex_lock_nested(&s->self_group.mutex, depth);
1058 spin_lock_nested(&s->self_group.lock, depth);
1061 res = ops->pre_action(s, state);
1065 snd_pcm_group_for_each_entry(s, substream) {
1066 res = ops->do_action(s, state);
1068 if (ops->undo_action) {
1069 snd_pcm_group_for_each_entry(s1, substream) {
1070 if (s1 == s) /* failed stream */
1072 ops->undo_action(s1, state);
1075 s = NULL; /* unlock all */
1079 snd_pcm_group_for_each_entry(s, substream) {
1080 ops->post_action(s, state);
1084 /* unlock streams */
1085 snd_pcm_group_for_each_entry(s1, substream) {
1086 if (s1 != substream) {
1087 if (s1->pcm->nonatomic)
1088 mutex_unlock(&s1->self_group.mutex);
1090 spin_unlock(&s1->self_group.lock);
1092 if (s1 == s) /* end */
1100 * Note: call with stream lock
1102 static int snd_pcm_action_single(const struct action_ops *ops,
1103 struct snd_pcm_substream *substream,
1108 res = ops->pre_action(substream, state);
1111 res = ops->do_action(substream, state);
1113 ops->post_action(substream, state);
1114 else if (ops->undo_action)
1115 ops->undo_action(substream, state);
1119 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1120 struct snd_pcm_group *new_group)
1122 substream->group = new_group;
1123 list_move(&substream->link_list, &new_group->substreams);
1127 * Unref and unlock the group, but keep the stream lock;
1128 * when the group becomes empty and no longer referred, destroy itself
1130 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1131 struct snd_pcm_substream *substream)
1137 do_free = refcount_dec_and_test(&group->refs);
1138 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1144 * Lock the group inside a stream lock and reference it;
1145 * return the locked group object, or NULL if not linked
1147 static struct snd_pcm_group *
1148 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1150 bool nonatomic = substream->pcm->nonatomic;
1151 struct snd_pcm_group *group;
1155 if (!snd_pcm_stream_linked(substream))
1157 group = substream->group;
1158 /* block freeing the group object */
1159 refcount_inc(&group->refs);
1161 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1162 spin_trylock(&group->lock);
1166 /* re-lock for avoiding ABBA deadlock */
1167 snd_pcm_stream_unlock(substream);
1168 snd_pcm_group_lock(group, nonatomic);
1169 snd_pcm_stream_lock(substream);
1171 /* check the group again; the above opens a small race window */
1172 if (substream->group == group)
1174 /* group changed, try again */
1175 snd_pcm_group_unref(group, substream);
1181 * Note: call with stream lock
1183 static int snd_pcm_action(const struct action_ops *ops,
1184 struct snd_pcm_substream *substream,
1187 struct snd_pcm_group *group;
1190 group = snd_pcm_stream_group_ref(substream);
1192 res = snd_pcm_action_group(ops, substream, state, 1);
1194 res = snd_pcm_action_single(ops, substream, state);
1195 snd_pcm_group_unref(group, substream);
1200 * Note: don't use any locks before
1202 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1203 struct snd_pcm_substream *substream,
1208 snd_pcm_stream_lock_irq(substream);
1209 res = snd_pcm_action(ops, substream, state);
1210 snd_pcm_stream_unlock_irq(substream);
1216 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1217 struct snd_pcm_substream *substream,
1222 /* Guarantee the group members won't change during non-atomic action */
1223 down_read(&snd_pcm_link_rwsem);
1224 if (snd_pcm_stream_linked(substream))
1225 res = snd_pcm_action_group(ops, substream, state, 0);
1227 res = snd_pcm_action_single(ops, substream, state);
1228 up_read(&snd_pcm_link_rwsem);
1235 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1237 struct snd_pcm_runtime *runtime = substream->runtime;
1238 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1240 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1241 !snd_pcm_playback_data(substream))
1243 runtime->trigger_tstamp_latched = false;
1244 runtime->trigger_master = substream;
1248 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1250 if (substream->runtime->trigger_master != substream)
1252 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1255 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1257 if (substream->runtime->trigger_master == substream)
1258 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1261 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 snd_pcm_trigger_tstamp(substream);
1265 runtime->hw_ptr_jiffies = jiffies;
1266 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1268 runtime->status->state = state;
1269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1270 runtime->silence_size > 0)
1271 snd_pcm_playback_silence(substream, ULONG_MAX);
1272 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1275 static const struct action_ops snd_pcm_action_start = {
1276 .pre_action = snd_pcm_pre_start,
1277 .do_action = snd_pcm_do_start,
1278 .undo_action = snd_pcm_undo_start,
1279 .post_action = snd_pcm_post_start
1283 * snd_pcm_start - start all linked streams
1284 * @substream: the PCM substream instance
1286 * Return: Zero if successful, or a negative error code.
1287 * The stream lock must be acquired before calling this function.
1289 int snd_pcm_start(struct snd_pcm_substream *substream)
1291 return snd_pcm_action(&snd_pcm_action_start, substream,
1292 SNDRV_PCM_STATE_RUNNING);
1295 /* take the stream lock and start the streams */
1296 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1298 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1299 SNDRV_PCM_STATE_RUNNING);
1305 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1307 struct snd_pcm_runtime *runtime = substream->runtime;
1308 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1310 runtime->trigger_master = substream;
1314 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1316 if (substream->runtime->trigger_master == substream &&
1317 snd_pcm_running(substream))
1318 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1319 return 0; /* unconditonally stop all substreams */
1322 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1324 struct snd_pcm_runtime *runtime = substream->runtime;
1325 if (runtime->status->state != state) {
1326 snd_pcm_trigger_tstamp(substream);
1327 runtime->status->state = state;
1328 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1330 runtime->stop_operating = true;
1331 wake_up(&runtime->sleep);
1332 wake_up(&runtime->tsleep);
1335 static const struct action_ops snd_pcm_action_stop = {
1336 .pre_action = snd_pcm_pre_stop,
1337 .do_action = snd_pcm_do_stop,
1338 .post_action = snd_pcm_post_stop
1342 * snd_pcm_stop - try to stop all running streams in the substream group
1343 * @substream: the PCM substream instance
1344 * @state: PCM state after stopping the stream
1346 * The state of each stream is then changed to the given state unconditionally.
1348 * Return: Zero if successful, or a negative error code.
1350 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1352 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1354 EXPORT_SYMBOL(snd_pcm_stop);
1357 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1358 * @substream: the PCM substream
1360 * After stopping, the state is changed to SETUP.
1361 * Unlike snd_pcm_stop(), this affects only the given stream.
1363 * Return: Zero if succesful, or a negative error code.
1365 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1367 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1368 SNDRV_PCM_STATE_SETUP);
1372 * snd_pcm_stop_xrun - stop the running streams as XRUN
1373 * @substream: the PCM substream instance
1375 * This stops the given running substream (and all linked substreams) as XRUN.
1376 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1378 * Return: Zero if successful, or a negative error code.
1380 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1382 unsigned long flags;
1384 snd_pcm_stream_lock_irqsave(substream, flags);
1385 if (substream->runtime && snd_pcm_running(substream))
1386 __snd_pcm_xrun(substream);
1387 snd_pcm_stream_unlock_irqrestore(substream, flags);
1390 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1395 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1397 struct snd_pcm_runtime *runtime = substream->runtime;
1398 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1401 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1403 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1405 runtime->trigger_master = substream;
1409 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1411 if (substream->runtime->trigger_master != substream)
1413 /* some drivers might use hw_ptr to recover from the pause -
1414 update the hw_ptr now */
1416 snd_pcm_update_hw_ptr(substream);
1417 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1418 * a delta between the current jiffies, this gives a large enough
1419 * delta, effectively to skip the check once.
1421 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1422 return substream->ops->trigger(substream,
1423 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1424 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1427 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1429 if (substream->runtime->trigger_master == substream)
1430 substream->ops->trigger(substream,
1431 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1432 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1435 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1437 struct snd_pcm_runtime *runtime = substream->runtime;
1438 snd_pcm_trigger_tstamp(substream);
1440 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1441 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1442 wake_up(&runtime->sleep);
1443 wake_up(&runtime->tsleep);
1445 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1446 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1450 static const struct action_ops snd_pcm_action_pause = {
1451 .pre_action = snd_pcm_pre_pause,
1452 .do_action = snd_pcm_do_pause,
1453 .undo_action = snd_pcm_undo_pause,
1454 .post_action = snd_pcm_post_pause
1458 * Push/release the pause for all linked streams.
1460 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1462 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1468 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1470 struct snd_pcm_runtime *runtime = substream->runtime;
1471 switch (runtime->status->state) {
1472 case SNDRV_PCM_STATE_SUSPENDED:
1474 /* unresumable PCM state; return -EBUSY for skipping suspend */
1475 case SNDRV_PCM_STATE_OPEN:
1476 case SNDRV_PCM_STATE_SETUP:
1477 case SNDRV_PCM_STATE_DISCONNECTED:
1480 runtime->trigger_master = substream;
1484 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1486 struct snd_pcm_runtime *runtime = substream->runtime;
1487 if (runtime->trigger_master != substream)
1489 if (! snd_pcm_running(substream))
1491 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1492 return 0; /* suspend unconditionally */
1495 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1497 struct snd_pcm_runtime *runtime = substream->runtime;
1498 snd_pcm_trigger_tstamp(substream);
1499 runtime->status->suspended_state = runtime->status->state;
1500 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1501 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1502 wake_up(&runtime->sleep);
1503 wake_up(&runtime->tsleep);
1506 static const struct action_ops snd_pcm_action_suspend = {
1507 .pre_action = snd_pcm_pre_suspend,
1508 .do_action = snd_pcm_do_suspend,
1509 .post_action = snd_pcm_post_suspend
1513 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1514 * @substream: the PCM substream
1516 * After this call, all streams are changed to SUSPENDED state.
1518 * Return: Zero if successful, or a negative error code.
1520 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1523 unsigned long flags;
1525 snd_pcm_stream_lock_irqsave(substream, flags);
1526 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1527 snd_pcm_stream_unlock_irqrestore(substream, flags);
1532 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1533 * @pcm: the PCM instance
1535 * After this call, all streams are changed to SUSPENDED state.
1537 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1539 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1541 struct snd_pcm_substream *substream;
1542 int stream, err = 0;
1547 for (stream = 0; stream < 2; stream++) {
1548 for (substream = pcm->streams[stream].substream;
1549 substream; substream = substream->next) {
1550 /* FIXME: the open/close code should lock this as well */
1551 if (substream->runtime == NULL)
1555 * Skip BE dai link PCM's that are internal and may
1556 * not have their substream ops set.
1558 if (!substream->ops)
1561 err = snd_pcm_suspend(substream);
1562 if (err < 0 && err != -EBUSY)
1568 EXPORT_SYMBOL(snd_pcm_suspend_all);
1572 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1574 struct snd_pcm_runtime *runtime = substream->runtime;
1575 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1577 runtime->trigger_master = substream;
1581 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1583 struct snd_pcm_runtime *runtime = substream->runtime;
1584 if (runtime->trigger_master != substream)
1586 /* DMA not running previously? */
1587 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1588 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1589 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1591 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1594 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1596 if (substream->runtime->trigger_master == substream &&
1597 snd_pcm_running(substream))
1598 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1601 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1603 struct snd_pcm_runtime *runtime = substream->runtime;
1604 snd_pcm_trigger_tstamp(substream);
1605 runtime->status->state = runtime->status->suspended_state;
1606 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1607 snd_pcm_sync_stop(substream);
1610 static const struct action_ops snd_pcm_action_resume = {
1611 .pre_action = snd_pcm_pre_resume,
1612 .do_action = snd_pcm_do_resume,
1613 .undo_action = snd_pcm_undo_resume,
1614 .post_action = snd_pcm_post_resume
1617 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1619 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1624 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1629 #endif /* CONFIG_PM */
1634 * Change the RUNNING stream(s) to XRUN state.
1636 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1638 struct snd_pcm_runtime *runtime = substream->runtime;
1641 snd_pcm_stream_lock_irq(substream);
1642 switch (runtime->status->state) {
1643 case SNDRV_PCM_STATE_XRUN:
1644 result = 0; /* already there */
1646 case SNDRV_PCM_STATE_RUNNING:
1647 __snd_pcm_xrun(substream);
1653 snd_pcm_stream_unlock_irq(substream);
1660 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1662 struct snd_pcm_runtime *runtime = substream->runtime;
1663 switch (runtime->status->state) {
1664 case SNDRV_PCM_STATE_RUNNING:
1665 case SNDRV_PCM_STATE_PREPARED:
1666 case SNDRV_PCM_STATE_PAUSED:
1667 case SNDRV_PCM_STATE_SUSPENDED:
1674 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1680 runtime->hw_ptr_base = 0;
1681 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1682 runtime->status->hw_ptr % runtime->period_size;
1683 runtime->silence_start = runtime->status->hw_ptr;
1684 runtime->silence_filled = 0;
1688 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1690 struct snd_pcm_runtime *runtime = substream->runtime;
1691 runtime->control->appl_ptr = runtime->status->hw_ptr;
1692 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1693 runtime->silence_size > 0)
1694 snd_pcm_playback_silence(substream, ULONG_MAX);
1697 static const struct action_ops snd_pcm_action_reset = {
1698 .pre_action = snd_pcm_pre_reset,
1699 .do_action = snd_pcm_do_reset,
1700 .post_action = snd_pcm_post_reset
1703 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1705 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1711 /* we use the second argument for updating f_flags */
1712 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1715 struct snd_pcm_runtime *runtime = substream->runtime;
1716 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1717 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1719 if (snd_pcm_running(substream))
1721 substream->f_flags = f_flags;
1725 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1728 snd_pcm_sync_stop(substream);
1729 err = substream->ops->prepare(substream);
1732 return snd_pcm_do_reset(substream, 0);
1735 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1737 struct snd_pcm_runtime *runtime = substream->runtime;
1738 runtime->control->appl_ptr = runtime->status->hw_ptr;
1739 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1742 static const struct action_ops snd_pcm_action_prepare = {
1743 .pre_action = snd_pcm_pre_prepare,
1744 .do_action = snd_pcm_do_prepare,
1745 .post_action = snd_pcm_post_prepare
1749 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1750 * @substream: the PCM substream instance
1751 * @file: file to refer f_flags
1753 * Return: Zero if successful, or a negative error code.
1755 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1761 f_flags = file->f_flags;
1763 f_flags = substream->f_flags;
1765 snd_pcm_stream_lock_irq(substream);
1766 switch (substream->runtime->status->state) {
1767 case SNDRV_PCM_STATE_PAUSED:
1768 snd_pcm_pause(substream, 0);
1770 case SNDRV_PCM_STATE_SUSPENDED:
1771 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1774 snd_pcm_stream_unlock_irq(substream);
1776 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1777 substream, f_flags);
1784 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1786 struct snd_pcm_runtime *runtime = substream->runtime;
1787 switch (runtime->status->state) {
1788 case SNDRV_PCM_STATE_OPEN:
1789 case SNDRV_PCM_STATE_DISCONNECTED:
1790 case SNDRV_PCM_STATE_SUSPENDED:
1793 runtime->trigger_master = substream;
1797 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1799 struct snd_pcm_runtime *runtime = substream->runtime;
1800 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1801 switch (runtime->status->state) {
1802 case SNDRV_PCM_STATE_PREPARED:
1803 /* start playback stream if possible */
1804 if (! snd_pcm_playback_empty(substream)) {
1805 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1806 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1808 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1811 case SNDRV_PCM_STATE_RUNNING:
1812 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1814 case SNDRV_PCM_STATE_XRUN:
1815 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1821 /* stop running stream */
1822 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1823 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1824 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1825 snd_pcm_do_stop(substream, new_state);
1826 snd_pcm_post_stop(substream, new_state);
1830 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1831 runtime->trigger_master == substream &&
1832 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1833 return substream->ops->trigger(substream,
1834 SNDRV_PCM_TRIGGER_DRAIN);
1839 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1843 static const struct action_ops snd_pcm_action_drain_init = {
1844 .pre_action = snd_pcm_pre_drain_init,
1845 .do_action = snd_pcm_do_drain_init,
1846 .post_action = snd_pcm_post_drain_init
1850 * Drain the stream(s).
1851 * When the substream is linked, sync until the draining of all playback streams
1853 * After this call, all streams are supposed to be either SETUP or DRAINING
1854 * (capture only) state.
1856 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1859 struct snd_card *card;
1860 struct snd_pcm_runtime *runtime;
1861 struct snd_pcm_substream *s;
1862 struct snd_pcm_group *group;
1863 wait_queue_entry_t wait;
1867 card = substream->pcm->card;
1868 runtime = substream->runtime;
1870 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1874 if (file->f_flags & O_NONBLOCK)
1876 } else if (substream->f_flags & O_NONBLOCK)
1879 snd_pcm_stream_lock_irq(substream);
1881 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1882 snd_pcm_pause(substream, 0);
1884 /* pre-start/stop - all running streams are changed to DRAINING state */
1885 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1888 /* in non-blocking, we don't wait in ioctl but let caller poll */
1896 struct snd_pcm_runtime *to_check;
1897 if (signal_pending(current)) {
1898 result = -ERESTARTSYS;
1901 /* find a substream to drain */
1903 group = snd_pcm_stream_group_ref(substream);
1904 snd_pcm_group_for_each_entry(s, substream) {
1905 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1907 runtime = s->runtime;
1908 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1913 snd_pcm_group_unref(group, substream);
1915 break; /* all drained */
1916 init_waitqueue_entry(&wait, current);
1917 set_current_state(TASK_INTERRUPTIBLE);
1918 add_wait_queue(&to_check->sleep, &wait);
1919 snd_pcm_stream_unlock_irq(substream);
1920 if (runtime->no_period_wakeup)
1921 tout = MAX_SCHEDULE_TIMEOUT;
1924 if (runtime->rate) {
1925 long t = runtime->period_size * 2 / runtime->rate;
1926 tout = max(t, tout);
1928 tout = msecs_to_jiffies(tout * 1000);
1930 tout = schedule_timeout(tout);
1932 snd_pcm_stream_lock_irq(substream);
1933 group = snd_pcm_stream_group_ref(substream);
1934 snd_pcm_group_for_each_entry(s, substream) {
1935 if (s->runtime == to_check) {
1936 remove_wait_queue(&to_check->sleep, &wait);
1940 snd_pcm_group_unref(group, substream);
1942 if (card->shutdown) {
1947 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1950 dev_dbg(substream->pcm->card->dev,
1951 "playback drain error (DMA or IRQ trouble?)\n");
1952 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1960 snd_pcm_stream_unlock_irq(substream);
1968 * Immediately put all linked substreams into SETUP state.
1970 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1972 struct snd_pcm_runtime *runtime;
1975 if (PCM_RUNTIME_CHECK(substream))
1977 runtime = substream->runtime;
1979 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1980 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1983 snd_pcm_stream_lock_irq(substream);
1985 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1986 snd_pcm_pause(substream, 0);
1988 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1989 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1990 snd_pcm_stream_unlock_irq(substream);
1996 static bool is_pcm_file(struct file *file)
1998 struct inode *inode = file_inode(file);
1999 struct snd_pcm *pcm;
2002 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2004 minor = iminor(inode);
2005 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2007 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2010 snd_card_unref(pcm->card);
2017 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2020 struct snd_pcm_file *pcm_file;
2021 struct snd_pcm_substream *substream1;
2022 struct snd_pcm_group *group, *target_group;
2023 bool nonatomic = substream->pcm->nonatomic;
2024 struct fd f = fdget(fd);
2028 if (!is_pcm_file(f.file)) {
2032 pcm_file = f.file->private_data;
2033 substream1 = pcm_file->substream;
2034 group = kzalloc(sizeof(*group), GFP_KERNEL);
2039 snd_pcm_group_init(group);
2041 down_write(&snd_pcm_link_rwsem);
2042 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2043 substream->runtime->status->state != substream1->runtime->status->state ||
2044 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2048 if (snd_pcm_stream_linked(substream1)) {
2053 snd_pcm_stream_lock_irq(substream);
2054 if (!snd_pcm_stream_linked(substream)) {
2055 snd_pcm_group_assign(substream, group);
2056 group = NULL; /* assigned, don't free this one below */
2058 target_group = substream->group;
2059 snd_pcm_stream_unlock_irq(substream);
2061 snd_pcm_group_lock_irq(target_group, nonatomic);
2062 snd_pcm_stream_lock(substream1);
2063 snd_pcm_group_assign(substream1, target_group);
2064 refcount_inc(&target_group->refs);
2065 snd_pcm_stream_unlock(substream1);
2066 snd_pcm_group_unlock_irq(target_group, nonatomic);
2068 up_write(&snd_pcm_link_rwsem);
2076 static void relink_to_local(struct snd_pcm_substream *substream)
2078 snd_pcm_stream_lock(substream);
2079 snd_pcm_group_assign(substream, &substream->self_group);
2080 snd_pcm_stream_unlock(substream);
2083 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2085 struct snd_pcm_group *group;
2086 bool nonatomic = substream->pcm->nonatomic;
2087 bool do_free = false;
2090 down_write(&snd_pcm_link_rwsem);
2092 if (!snd_pcm_stream_linked(substream)) {
2097 group = substream->group;
2098 snd_pcm_group_lock_irq(group, nonatomic);
2100 relink_to_local(substream);
2101 refcount_dec(&group->refs);
2103 /* detach the last stream, too */
2104 if (list_is_singular(&group->substreams)) {
2105 relink_to_local(list_first_entry(&group->substreams,
2106 struct snd_pcm_substream,
2108 do_free = refcount_dec_and_test(&group->refs);
2111 snd_pcm_group_unlock_irq(group, nonatomic);
2116 up_write(&snd_pcm_link_rwsem);
2123 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2124 struct snd_pcm_hw_rule *rule)
2126 struct snd_interval t;
2127 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2128 hw_param_interval_c(params, rule->deps[1]), &t);
2129 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2132 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2133 struct snd_pcm_hw_rule *rule)
2135 struct snd_interval t;
2136 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2137 hw_param_interval_c(params, rule->deps[1]), &t);
2138 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2141 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2142 struct snd_pcm_hw_rule *rule)
2144 struct snd_interval t;
2145 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2146 hw_param_interval_c(params, rule->deps[1]),
2147 (unsigned long) rule->private, &t);
2148 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2151 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2152 struct snd_pcm_hw_rule *rule)
2154 struct snd_interval t;
2155 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2156 (unsigned long) rule->private,
2157 hw_param_interval_c(params, rule->deps[1]), &t);
2158 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2161 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2162 struct snd_pcm_hw_rule *rule)
2165 const struct snd_interval *i =
2166 hw_param_interval_c(params, rule->deps[0]);
2168 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2170 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2172 if (! snd_mask_test(mask, k))
2174 bits = snd_pcm_format_physical_width(k);
2176 continue; /* ignore invalid formats */
2177 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2178 snd_mask_reset(&m, k);
2180 return snd_mask_refine(mask, &m);
2183 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2184 struct snd_pcm_hw_rule *rule)
2186 struct snd_interval t;
2192 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2194 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2196 bits = snd_pcm_format_physical_width(k);
2198 continue; /* ignore invalid formats */
2199 if (t.min > (unsigned)bits)
2201 if (t.max < (unsigned)bits)
2205 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2208 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2209 #error "Change this table"
2212 static const unsigned int rates[] = {
2213 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2214 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2217 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2218 .count = ARRAY_SIZE(rates),
2222 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2223 struct snd_pcm_hw_rule *rule)
2225 struct snd_pcm_hardware *hw = rule->private;
2226 return snd_interval_list(hw_param_interval(params, rule->var),
2227 snd_pcm_known_rates.count,
2228 snd_pcm_known_rates.list, hw->rates);
2231 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2232 struct snd_pcm_hw_rule *rule)
2234 struct snd_interval t;
2235 struct snd_pcm_substream *substream = rule->private;
2237 t.max = substream->buffer_bytes_max;
2241 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2244 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2246 struct snd_pcm_runtime *runtime = substream->runtime;
2247 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2250 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2251 snd_mask_any(constrs_mask(constrs, k));
2254 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2255 snd_interval_any(constrs_interval(constrs, k));
2258 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2259 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2260 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2261 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2262 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2264 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2265 snd_pcm_hw_rule_format, NULL,
2266 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2269 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2270 snd_pcm_hw_rule_sample_bits, NULL,
2271 SNDRV_PCM_HW_PARAM_FORMAT,
2272 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2275 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2276 snd_pcm_hw_rule_div, NULL,
2277 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2280 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2281 snd_pcm_hw_rule_mul, NULL,
2282 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2285 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2286 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2287 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2290 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2291 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2292 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2295 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2296 snd_pcm_hw_rule_div, NULL,
2297 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2300 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2301 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2302 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2305 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2306 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2307 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2310 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2311 snd_pcm_hw_rule_div, NULL,
2312 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2315 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2316 snd_pcm_hw_rule_div, NULL,
2317 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2320 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2321 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2322 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2325 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2326 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2327 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2330 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2331 snd_pcm_hw_rule_mul, NULL,
2332 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2335 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2336 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2337 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2340 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2341 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2342 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2345 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2346 snd_pcm_hw_rule_muldivk, (void*) 8,
2347 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2350 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2351 snd_pcm_hw_rule_muldivk, (void*) 8,
2352 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2355 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2356 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2357 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2360 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2361 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2362 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2368 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2370 struct snd_pcm_runtime *runtime = substream->runtime;
2371 struct snd_pcm_hardware *hw = &runtime->hw;
2373 unsigned int mask = 0;
2375 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2376 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2377 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2378 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2379 if (hw_support_mmap(substream)) {
2380 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2381 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2382 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2383 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2384 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2385 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2387 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2391 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2395 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2399 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2400 hw->channels_min, hw->channels_max);
2404 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2405 hw->rate_min, hw->rate_max);
2409 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2410 hw->period_bytes_min, hw->period_bytes_max);
2414 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2415 hw->periods_min, hw->periods_max);
2419 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2420 hw->period_bytes_min, hw->buffer_bytes_max);
2424 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2425 snd_pcm_hw_rule_buffer_bytes_max, substream,
2426 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2431 if (runtime->dma_bytes) {
2432 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2437 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2438 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2439 snd_pcm_hw_rule_rate, hw,
2440 SNDRV_PCM_HW_PARAM_RATE, -1);
2445 /* FIXME: this belong to lowlevel */
2446 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2451 static void pcm_release_private(struct snd_pcm_substream *substream)
2453 if (snd_pcm_stream_linked(substream))
2454 snd_pcm_unlink(substream);
2457 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2459 substream->ref_count--;
2460 if (substream->ref_count > 0)
2463 snd_pcm_drop(substream);
2464 if (substream->hw_opened) {
2465 if (substream->ops->hw_free &&
2466 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2467 substream->ops->hw_free(substream);
2468 substream->ops->close(substream);
2469 substream->hw_opened = 0;
2471 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2472 pm_qos_remove_request(&substream->latency_pm_qos_req);
2473 if (substream->pcm_release) {
2474 substream->pcm_release(substream);
2475 substream->pcm_release = NULL;
2477 snd_pcm_detach_substream(substream);
2479 EXPORT_SYMBOL(snd_pcm_release_substream);
2481 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2483 struct snd_pcm_substream **rsubstream)
2485 struct snd_pcm_substream *substream;
2488 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2491 if (substream->ref_count > 1) {
2492 *rsubstream = substream;
2496 err = snd_pcm_hw_constraints_init(substream);
2498 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2502 if ((err = substream->ops->open(substream)) < 0)
2505 substream->hw_opened = 1;
2507 err = snd_pcm_hw_constraints_complete(substream);
2509 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2513 *rsubstream = substream;
2517 snd_pcm_release_substream(substream);
2520 EXPORT_SYMBOL(snd_pcm_open_substream);
2522 static int snd_pcm_open_file(struct file *file,
2523 struct snd_pcm *pcm,
2526 struct snd_pcm_file *pcm_file;
2527 struct snd_pcm_substream *substream;
2530 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2534 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2535 if (pcm_file == NULL) {
2536 snd_pcm_release_substream(substream);
2539 pcm_file->substream = substream;
2540 if (substream->ref_count == 1)
2541 substream->pcm_release = pcm_release_private;
2542 file->private_data = pcm_file;
2547 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2549 struct snd_pcm *pcm;
2550 int err = nonseekable_open(inode, file);
2553 pcm = snd_lookup_minor_data(iminor(inode),
2554 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2555 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2557 snd_card_unref(pcm->card);
2561 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2563 struct snd_pcm *pcm;
2564 int err = nonseekable_open(inode, file);
2567 pcm = snd_lookup_minor_data(iminor(inode),
2568 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2569 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2571 snd_card_unref(pcm->card);
2575 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2578 wait_queue_entry_t wait;
2584 err = snd_card_file_add(pcm->card, file);
2587 if (!try_module_get(pcm->card->module)) {
2591 init_waitqueue_entry(&wait, current);
2592 add_wait_queue(&pcm->open_wait, &wait);
2593 mutex_lock(&pcm->open_mutex);
2595 err = snd_pcm_open_file(file, pcm, stream);
2598 if (err == -EAGAIN) {
2599 if (file->f_flags & O_NONBLOCK) {
2605 set_current_state(TASK_INTERRUPTIBLE);
2606 mutex_unlock(&pcm->open_mutex);
2608 mutex_lock(&pcm->open_mutex);
2609 if (pcm->card->shutdown) {
2613 if (signal_pending(current)) {
2618 remove_wait_queue(&pcm->open_wait, &wait);
2619 mutex_unlock(&pcm->open_mutex);
2625 module_put(pcm->card->module);
2627 snd_card_file_remove(pcm->card, file);
2632 static int snd_pcm_release(struct inode *inode, struct file *file)
2634 struct snd_pcm *pcm;
2635 struct snd_pcm_substream *substream;
2636 struct snd_pcm_file *pcm_file;
2638 pcm_file = file->private_data;
2639 substream = pcm_file->substream;
2640 if (snd_BUG_ON(!substream))
2642 pcm = substream->pcm;
2643 mutex_lock(&pcm->open_mutex);
2644 snd_pcm_release_substream(substream);
2646 mutex_unlock(&pcm->open_mutex);
2647 wake_up(&pcm->open_wait);
2648 module_put(pcm->card->module);
2649 snd_card_file_remove(pcm->card, file);
2653 /* check and update PCM state; return 0 or a negative error
2654 * call this inside PCM lock
2656 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2658 switch (substream->runtime->status->state) {
2659 case SNDRV_PCM_STATE_DRAINING:
2660 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2663 case SNDRV_PCM_STATE_RUNNING:
2664 return snd_pcm_update_hw_ptr(substream);
2665 case SNDRV_PCM_STATE_PREPARED:
2666 case SNDRV_PCM_STATE_PAUSED:
2668 case SNDRV_PCM_STATE_SUSPENDED:
2670 case SNDRV_PCM_STATE_XRUN:
2677 /* increase the appl_ptr; returns the processed frames or a negative error */
2678 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2679 snd_pcm_uframes_t frames,
2680 snd_pcm_sframes_t avail)
2682 struct snd_pcm_runtime *runtime = substream->runtime;
2683 snd_pcm_sframes_t appl_ptr;
2688 if (frames > (snd_pcm_uframes_t)avail)
2690 appl_ptr = runtime->control->appl_ptr + frames;
2691 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2692 appl_ptr -= runtime->boundary;
2693 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2694 return ret < 0 ? ret : frames;
2697 /* decrease the appl_ptr; returns the processed frames or zero for error */
2698 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2699 snd_pcm_uframes_t frames,
2700 snd_pcm_sframes_t avail)
2702 struct snd_pcm_runtime *runtime = substream->runtime;
2703 snd_pcm_sframes_t appl_ptr;
2708 if (frames > (snd_pcm_uframes_t)avail)
2710 appl_ptr = runtime->control->appl_ptr - frames;
2712 appl_ptr += runtime->boundary;
2713 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2714 /* NOTE: we return zero for errors because PulseAudio gets depressed
2715 * upon receiving an error from rewind ioctl and stops processing
2716 * any longer. Returning zero means that no rewind is done, so
2717 * it's not absolutely wrong to answer like that.
2719 return ret < 0 ? 0 : frames;
2722 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2723 snd_pcm_uframes_t frames)
2725 snd_pcm_sframes_t ret;
2730 snd_pcm_stream_lock_irq(substream);
2731 ret = do_pcm_hwsync(substream);
2733 ret = rewind_appl_ptr(substream, frames,
2734 snd_pcm_hw_avail(substream));
2735 snd_pcm_stream_unlock_irq(substream);
2739 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2740 snd_pcm_uframes_t frames)
2742 snd_pcm_sframes_t ret;
2747 snd_pcm_stream_lock_irq(substream);
2748 ret = do_pcm_hwsync(substream);
2750 ret = forward_appl_ptr(substream, frames,
2751 snd_pcm_avail(substream));
2752 snd_pcm_stream_unlock_irq(substream);
2756 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2760 snd_pcm_stream_lock_irq(substream);
2761 err = do_pcm_hwsync(substream);
2762 snd_pcm_stream_unlock_irq(substream);
2766 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2767 snd_pcm_sframes_t *delay)
2770 snd_pcm_sframes_t n = 0;
2772 snd_pcm_stream_lock_irq(substream);
2773 err = do_pcm_hwsync(substream);
2775 n = snd_pcm_calc_delay(substream);
2776 snd_pcm_stream_unlock_irq(substream);
2782 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2783 struct snd_pcm_sync_ptr __user *_sync_ptr)
2785 struct snd_pcm_runtime *runtime = substream->runtime;
2786 struct snd_pcm_sync_ptr sync_ptr;
2787 volatile struct snd_pcm_mmap_status *status;
2788 volatile struct snd_pcm_mmap_control *control;
2791 memset(&sync_ptr, 0, sizeof(sync_ptr));
2792 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2794 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2796 status = runtime->status;
2797 control = runtime->control;
2798 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2799 err = snd_pcm_hwsync(substream);
2803 snd_pcm_stream_lock_irq(substream);
2804 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2805 err = pcm_lib_apply_appl_ptr(substream,
2806 sync_ptr.c.control.appl_ptr);
2808 snd_pcm_stream_unlock_irq(substream);
2812 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2814 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2815 control->avail_min = sync_ptr.c.control.avail_min;
2817 sync_ptr.c.control.avail_min = control->avail_min;
2818 sync_ptr.s.status.state = status->state;
2819 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2820 sync_ptr.s.status.tstamp = status->tstamp;
2821 sync_ptr.s.status.suspended_state = status->suspended_state;
2822 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2823 snd_pcm_stream_unlock_irq(substream);
2824 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2829 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2831 struct snd_pcm_runtime *runtime = substream->runtime;
2834 if (get_user(arg, _arg))
2836 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2838 runtime->tstamp_type = arg;
2842 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2843 struct snd_xferi __user *_xferi)
2845 struct snd_xferi xferi;
2846 struct snd_pcm_runtime *runtime = substream->runtime;
2847 snd_pcm_sframes_t result;
2849 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2851 if (put_user(0, &_xferi->result))
2853 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2855 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2856 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2858 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2859 __put_user(result, &_xferi->result);
2860 return result < 0 ? result : 0;
2863 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2864 struct snd_xfern __user *_xfern)
2866 struct snd_xfern xfern;
2867 struct snd_pcm_runtime *runtime = substream->runtime;
2869 snd_pcm_sframes_t result;
2871 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2873 if (runtime->channels > 128)
2875 if (put_user(0, &_xfern->result))
2877 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2880 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2882 return PTR_ERR(bufs);
2883 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2884 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2886 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2888 __put_user(result, &_xfern->result);
2889 return result < 0 ? result : 0;
2892 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2893 snd_pcm_uframes_t __user *_frames)
2895 snd_pcm_uframes_t frames;
2896 snd_pcm_sframes_t result;
2898 if (get_user(frames, _frames))
2900 if (put_user(0, _frames))
2902 result = snd_pcm_rewind(substream, frames);
2903 __put_user(result, _frames);
2904 return result < 0 ? result : 0;
2907 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2908 snd_pcm_uframes_t __user *_frames)
2910 snd_pcm_uframes_t frames;
2911 snd_pcm_sframes_t result;
2913 if (get_user(frames, _frames))
2915 if (put_user(0, _frames))
2917 result = snd_pcm_forward(substream, frames);
2918 __put_user(result, _frames);
2919 return result < 0 ? result : 0;
2922 static int snd_pcm_common_ioctl(struct file *file,
2923 struct snd_pcm_substream *substream,
2924 unsigned int cmd, void __user *arg)
2926 struct snd_pcm_file *pcm_file = file->private_data;
2929 if (PCM_RUNTIME_CHECK(substream))
2932 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2937 case SNDRV_PCM_IOCTL_PVERSION:
2938 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2939 case SNDRV_PCM_IOCTL_INFO:
2940 return snd_pcm_info_user(substream, arg);
2941 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2943 case SNDRV_PCM_IOCTL_TTSTAMP:
2944 return snd_pcm_tstamp(substream, arg);
2945 case SNDRV_PCM_IOCTL_USER_PVERSION:
2946 if (get_user(pcm_file->user_pversion,
2947 (unsigned int __user *)arg))
2950 case SNDRV_PCM_IOCTL_HW_REFINE:
2951 return snd_pcm_hw_refine_user(substream, arg);
2952 case SNDRV_PCM_IOCTL_HW_PARAMS:
2953 return snd_pcm_hw_params_user(substream, arg);
2954 case SNDRV_PCM_IOCTL_HW_FREE:
2955 return snd_pcm_hw_free(substream);
2956 case SNDRV_PCM_IOCTL_SW_PARAMS:
2957 return snd_pcm_sw_params_user(substream, arg);
2958 case SNDRV_PCM_IOCTL_STATUS:
2959 return snd_pcm_status_user(substream, arg, false);
2960 case SNDRV_PCM_IOCTL_STATUS_EXT:
2961 return snd_pcm_status_user(substream, arg, true);
2962 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2963 return snd_pcm_channel_info_user(substream, arg);
2964 case SNDRV_PCM_IOCTL_PREPARE:
2965 return snd_pcm_prepare(substream, file);
2966 case SNDRV_PCM_IOCTL_RESET:
2967 return snd_pcm_reset(substream);
2968 case SNDRV_PCM_IOCTL_START:
2969 return snd_pcm_start_lock_irq(substream);
2970 case SNDRV_PCM_IOCTL_LINK:
2971 return snd_pcm_link(substream, (int)(unsigned long) arg);
2972 case SNDRV_PCM_IOCTL_UNLINK:
2973 return snd_pcm_unlink(substream);
2974 case SNDRV_PCM_IOCTL_RESUME:
2975 return snd_pcm_resume(substream);
2976 case SNDRV_PCM_IOCTL_XRUN:
2977 return snd_pcm_xrun(substream);
2978 case SNDRV_PCM_IOCTL_HWSYNC:
2979 return snd_pcm_hwsync(substream);
2980 case SNDRV_PCM_IOCTL_DELAY:
2982 snd_pcm_sframes_t delay;
2983 snd_pcm_sframes_t __user *res = arg;
2986 err = snd_pcm_delay(substream, &delay);
2989 if (put_user(delay, res))
2993 case SNDRV_PCM_IOCTL_SYNC_PTR:
2994 return snd_pcm_sync_ptr(substream, arg);
2995 #ifdef CONFIG_SND_SUPPORT_OLD_API
2996 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2997 return snd_pcm_hw_refine_old_user(substream, arg);
2998 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2999 return snd_pcm_hw_params_old_user(substream, arg);
3001 case SNDRV_PCM_IOCTL_DRAIN:
3002 return snd_pcm_drain(substream, file);
3003 case SNDRV_PCM_IOCTL_DROP:
3004 return snd_pcm_drop(substream);
3005 case SNDRV_PCM_IOCTL_PAUSE:
3006 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3008 (int)(unsigned long)arg);
3009 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3010 case SNDRV_PCM_IOCTL_READI_FRAMES:
3011 return snd_pcm_xferi_frames_ioctl(substream, arg);
3012 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3013 case SNDRV_PCM_IOCTL_READN_FRAMES:
3014 return snd_pcm_xfern_frames_ioctl(substream, arg);
3015 case SNDRV_PCM_IOCTL_REWIND:
3016 return snd_pcm_rewind_ioctl(substream, arg);
3017 case SNDRV_PCM_IOCTL_FORWARD:
3018 return snd_pcm_forward_ioctl(substream, arg);
3020 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3024 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3027 struct snd_pcm_file *pcm_file;
3029 pcm_file = file->private_data;
3031 if (((cmd >> 8) & 0xff) != 'A')
3034 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3035 (void __user *)arg);
3039 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3040 * @substream: PCM substream
3042 * @arg: IOCTL argument
3044 * The function is provided primarily for OSS layer and USB gadget drivers,
3045 * and it allows only the limited set of ioctls (hw_params, sw_params,
3046 * prepare, start, drain, drop, forward).
3048 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3049 unsigned int cmd, void *arg)
3051 snd_pcm_uframes_t *frames = arg;
3052 snd_pcm_sframes_t result;
3055 case SNDRV_PCM_IOCTL_FORWARD:
3057 /* provided only for OSS; capture-only and no value returned */
3058 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3060 result = snd_pcm_forward(substream, *frames);
3061 return result < 0 ? result : 0;
3063 case SNDRV_PCM_IOCTL_HW_PARAMS:
3064 return snd_pcm_hw_params(substream, arg);
3065 case SNDRV_PCM_IOCTL_SW_PARAMS:
3066 return snd_pcm_sw_params(substream, arg);
3067 case SNDRV_PCM_IOCTL_PREPARE:
3068 return snd_pcm_prepare(substream, NULL);
3069 case SNDRV_PCM_IOCTL_START:
3070 return snd_pcm_start_lock_irq(substream);
3071 case SNDRV_PCM_IOCTL_DRAIN:
3072 return snd_pcm_drain(substream, NULL);
3073 case SNDRV_PCM_IOCTL_DROP:
3074 return snd_pcm_drop(substream);
3075 case SNDRV_PCM_IOCTL_DELAY:
3076 return snd_pcm_delay(substream, frames);
3081 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3083 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3086 struct snd_pcm_file *pcm_file;
3087 struct snd_pcm_substream *substream;
3088 struct snd_pcm_runtime *runtime;
3089 snd_pcm_sframes_t result;
3091 pcm_file = file->private_data;
3092 substream = pcm_file->substream;
3093 if (PCM_RUNTIME_CHECK(substream))
3095 runtime = substream->runtime;
3096 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3098 if (!frame_aligned(runtime, count))
3100 count = bytes_to_frames(runtime, count);
3101 result = snd_pcm_lib_read(substream, buf, count);
3103 result = frames_to_bytes(runtime, result);
3107 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3108 size_t count, loff_t * offset)
3110 struct snd_pcm_file *pcm_file;
3111 struct snd_pcm_substream *substream;
3112 struct snd_pcm_runtime *runtime;
3113 snd_pcm_sframes_t result;
3115 pcm_file = file->private_data;
3116 substream = pcm_file->substream;
3117 if (PCM_RUNTIME_CHECK(substream))
3119 runtime = substream->runtime;
3120 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3122 if (!frame_aligned(runtime, count))
3124 count = bytes_to_frames(runtime, count);
3125 result = snd_pcm_lib_write(substream, buf, count);
3127 result = frames_to_bytes(runtime, result);
3131 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3133 struct snd_pcm_file *pcm_file;
3134 struct snd_pcm_substream *substream;
3135 struct snd_pcm_runtime *runtime;
3136 snd_pcm_sframes_t result;
3139 snd_pcm_uframes_t frames;
3141 pcm_file = iocb->ki_filp->private_data;
3142 substream = pcm_file->substream;
3143 if (PCM_RUNTIME_CHECK(substream))
3145 runtime = substream->runtime;
3146 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3148 if (!iter_is_iovec(to))
3150 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3152 if (!frame_aligned(runtime, to->iov->iov_len))
3154 frames = bytes_to_samples(runtime, to->iov->iov_len);
3155 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3158 for (i = 0; i < to->nr_segs; ++i)
3159 bufs[i] = to->iov[i].iov_base;
3160 result = snd_pcm_lib_readv(substream, bufs, frames);
3162 result = frames_to_bytes(runtime, result);
3167 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3169 struct snd_pcm_file *pcm_file;
3170 struct snd_pcm_substream *substream;
3171 struct snd_pcm_runtime *runtime;
3172 snd_pcm_sframes_t result;
3175 snd_pcm_uframes_t frames;
3177 pcm_file = iocb->ki_filp->private_data;
3178 substream = pcm_file->substream;
3179 if (PCM_RUNTIME_CHECK(substream))
3181 runtime = substream->runtime;
3182 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3184 if (!iter_is_iovec(from))
3186 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3187 !frame_aligned(runtime, from->iov->iov_len))
3189 frames = bytes_to_samples(runtime, from->iov->iov_len);
3190 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3193 for (i = 0; i < from->nr_segs; ++i)
3194 bufs[i] = from->iov[i].iov_base;
3195 result = snd_pcm_lib_writev(substream, bufs, frames);
3197 result = frames_to_bytes(runtime, result);
3202 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3204 struct snd_pcm_file *pcm_file;
3205 struct snd_pcm_substream *substream;
3206 struct snd_pcm_runtime *runtime;
3208 snd_pcm_uframes_t avail;
3210 pcm_file = file->private_data;
3212 substream = pcm_file->substream;
3213 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3214 ok = EPOLLOUT | EPOLLWRNORM;
3216 ok = EPOLLIN | EPOLLRDNORM;
3217 if (PCM_RUNTIME_CHECK(substream))
3218 return ok | EPOLLERR;
3220 runtime = substream->runtime;
3221 poll_wait(file, &runtime->sleep, wait);
3224 snd_pcm_stream_lock_irq(substream);
3225 avail = snd_pcm_avail(substream);
3226 switch (runtime->status->state) {
3227 case SNDRV_PCM_STATE_RUNNING:
3228 case SNDRV_PCM_STATE_PREPARED:
3229 case SNDRV_PCM_STATE_PAUSED:
3230 if (avail >= runtime->control->avail_min)
3233 case SNDRV_PCM_STATE_DRAINING:
3234 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3241 mask = ok | EPOLLERR;
3244 snd_pcm_stream_unlock_irq(substream);
3253 * Only on coherent architectures, we can mmap the status and the control records
3254 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3256 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3258 * mmap status record
3260 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3262 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3263 struct snd_pcm_runtime *runtime;
3265 if (substream == NULL)
3266 return VM_FAULT_SIGBUS;
3267 runtime = substream->runtime;
3268 vmf->page = virt_to_page(runtime->status);
3269 get_page(vmf->page);
3273 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3275 .fault = snd_pcm_mmap_status_fault,
3278 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3279 struct vm_area_struct *area)
3282 if (!(area->vm_flags & VM_READ))
3284 size = area->vm_end - area->vm_start;
3285 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3287 area->vm_ops = &snd_pcm_vm_ops_status;
3288 area->vm_private_data = substream;
3289 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3294 * mmap control record
3296 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3298 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3299 struct snd_pcm_runtime *runtime;
3301 if (substream == NULL)
3302 return VM_FAULT_SIGBUS;
3303 runtime = substream->runtime;
3304 vmf->page = virt_to_page(runtime->control);
3305 get_page(vmf->page);
3309 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3311 .fault = snd_pcm_mmap_control_fault,
3314 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3315 struct vm_area_struct *area)
3318 if (!(area->vm_flags & VM_READ))
3320 size = area->vm_end - area->vm_start;
3321 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3323 area->vm_ops = &snd_pcm_vm_ops_control;
3324 area->vm_private_data = substream;
3325 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3329 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3331 if (pcm_file->no_compat_mmap)
3333 /* See pcm_control_mmap_allowed() below.
3334 * Since older alsa-lib requires both status and control mmaps to be
3335 * coupled, we have to disable the status mmap for old alsa-lib, too.
3337 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3338 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3343 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3345 if (pcm_file->no_compat_mmap)
3347 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3348 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3349 * thus it effectively assures the manual update of appl_ptr.
3351 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3356 #else /* ! coherent mmap */
3358 * don't support mmap for status and control records.
3360 #define pcm_status_mmap_allowed(pcm_file) false
3361 #define pcm_control_mmap_allowed(pcm_file) false
3363 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3364 struct vm_area_struct *area)
3368 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3369 struct vm_area_struct *area)
3373 #endif /* coherent mmap */
3375 static inline struct page *
3376 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3378 void *vaddr = substream->runtime->dma_area + ofs;
3380 switch (substream->dma_buffer.dev.type) {
3381 #ifdef CONFIG_SND_DMA_SGBUF
3382 case SNDRV_DMA_TYPE_DEV_SG:
3383 case SNDRV_DMA_TYPE_DEV_UC_SG:
3384 return snd_pcm_sgbuf_ops_page(substream, ofs);
3385 #endif /* CONFIG_SND_DMA_SGBUF */
3386 case SNDRV_DMA_TYPE_VMALLOC:
3387 return vmalloc_to_page(vaddr);
3389 return virt_to_page(vaddr);
3394 * fault callback for mmapping a RAM page
3396 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3398 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3399 struct snd_pcm_runtime *runtime;
3400 unsigned long offset;
3404 if (substream == NULL)
3405 return VM_FAULT_SIGBUS;
3406 runtime = substream->runtime;
3407 offset = vmf->pgoff << PAGE_SHIFT;
3408 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3409 if (offset > dma_bytes - PAGE_SIZE)
3410 return VM_FAULT_SIGBUS;
3411 if (substream->ops->page)
3412 page = substream->ops->page(substream, offset);
3414 page = snd_pcm_default_page_ops(substream, offset);
3416 return VM_FAULT_SIGBUS;
3422 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3423 .open = snd_pcm_mmap_data_open,
3424 .close = snd_pcm_mmap_data_close,
3427 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3428 .open = snd_pcm_mmap_data_open,
3429 .close = snd_pcm_mmap_data_close,
3430 .fault = snd_pcm_mmap_data_fault,
3434 * mmap the DMA buffer on RAM
3438 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3439 * @substream: PCM substream
3442 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3443 * this function is invoked implicitly.
3445 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3446 struct vm_area_struct *area)
3448 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3449 #ifdef CONFIG_GENERIC_ALLOCATOR
3450 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3451 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3452 return remap_pfn_range(area, area->vm_start,
3453 substream->dma_buffer.addr >> PAGE_SHIFT,
3454 area->vm_end - area->vm_start, area->vm_page_prot);
3456 #endif /* CONFIG_GENERIC_ALLOCATOR */
3457 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3458 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3459 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3460 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3461 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3463 substream->runtime->dma_area,
3464 substream->runtime->dma_addr,
3465 substream->runtime->dma_bytes);
3466 #endif /* CONFIG_X86 */
3467 /* mmap with fault handler */
3468 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3471 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3474 * mmap the DMA buffer on I/O memory area
3476 #if SNDRV_PCM_INFO_MMAP_IOMEM
3478 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3479 * @substream: PCM substream
3482 * When your hardware uses the iomapped pages as the hardware buffer and
3483 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3484 * is supposed to work only on limited architectures.
3486 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3487 struct vm_area_struct *area)
3489 struct snd_pcm_runtime *runtime = substream->runtime;
3491 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3492 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3494 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3495 #endif /* SNDRV_PCM_INFO_MMAP */
3500 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3501 struct vm_area_struct *area)
3503 struct snd_pcm_runtime *runtime;
3505 unsigned long offset;
3509 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3510 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3513 if (!(area->vm_flags & VM_READ))
3516 runtime = substream->runtime;
3517 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3519 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3521 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3522 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3524 size = area->vm_end - area->vm_start;
3525 offset = area->vm_pgoff << PAGE_SHIFT;
3526 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3527 if ((size_t)size > dma_bytes)
3529 if (offset > dma_bytes - size)
3532 area->vm_ops = &snd_pcm_vm_ops_data;
3533 area->vm_private_data = substream;
3534 if (substream->ops->mmap)
3535 err = substream->ops->mmap(substream, area);
3537 err = snd_pcm_lib_default_mmap(substream, area);
3539 atomic_inc(&substream->mmap_count);
3542 EXPORT_SYMBOL(snd_pcm_mmap_data);
3544 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3546 struct snd_pcm_file * pcm_file;
3547 struct snd_pcm_substream *substream;
3548 unsigned long offset;
3550 pcm_file = file->private_data;
3551 substream = pcm_file->substream;
3552 if (PCM_RUNTIME_CHECK(substream))
3555 offset = area->vm_pgoff << PAGE_SHIFT;
3557 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3558 if (!pcm_status_mmap_allowed(pcm_file))
3560 return snd_pcm_mmap_status(substream, file, area);
3561 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3562 if (!pcm_control_mmap_allowed(pcm_file))
3564 return snd_pcm_mmap_control(substream, file, area);
3566 return snd_pcm_mmap_data(substream, file, area);
3571 static int snd_pcm_fasync(int fd, struct file * file, int on)
3573 struct snd_pcm_file * pcm_file;
3574 struct snd_pcm_substream *substream;
3575 struct snd_pcm_runtime *runtime;
3577 pcm_file = file->private_data;
3578 substream = pcm_file->substream;
3579 if (PCM_RUNTIME_CHECK(substream))
3581 runtime = substream->runtime;
3582 return fasync_helper(fd, file, on, &runtime->fasync);
3588 #ifdef CONFIG_COMPAT
3589 #include "pcm_compat.c"
3591 #define snd_pcm_ioctl_compat NULL
3595 * To be removed helpers to keep binary compatibility
3598 #ifdef CONFIG_SND_SUPPORT_OLD_API
3599 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3600 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3602 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3603 struct snd_pcm_hw_params_old *oparams)
3607 memset(params, 0, sizeof(*params));
3608 params->flags = oparams->flags;
3609 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3610 params->masks[i].bits[0] = oparams->masks[i];
3611 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3612 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3613 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3614 params->info = oparams->info;
3615 params->msbits = oparams->msbits;
3616 params->rate_num = oparams->rate_num;
3617 params->rate_den = oparams->rate_den;
3618 params->fifo_size = oparams->fifo_size;
3621 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3622 struct snd_pcm_hw_params *params)
3626 memset(oparams, 0, sizeof(*oparams));
3627 oparams->flags = params->flags;
3628 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3629 oparams->masks[i] = params->masks[i].bits[0];
3630 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3631 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3632 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3633 oparams->info = params->info;
3634 oparams->msbits = params->msbits;
3635 oparams->rate_num = params->rate_num;
3636 oparams->rate_den = params->rate_den;
3637 oparams->fifo_size = params->fifo_size;
3640 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3641 struct snd_pcm_hw_params_old __user * _oparams)
3643 struct snd_pcm_hw_params *params;
3644 struct snd_pcm_hw_params_old *oparams = NULL;
3647 params = kmalloc(sizeof(*params), GFP_KERNEL);
3651 oparams = memdup_user(_oparams, sizeof(*oparams));
3652 if (IS_ERR(oparams)) {
3653 err = PTR_ERR(oparams);
3656 snd_pcm_hw_convert_from_old_params(params, oparams);
3657 err = snd_pcm_hw_refine(substream, params);
3661 err = fixup_unreferenced_params(substream, params);
3665 snd_pcm_hw_convert_to_old_params(oparams, params);
3666 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3675 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3676 struct snd_pcm_hw_params_old __user * _oparams)
3678 struct snd_pcm_hw_params *params;
3679 struct snd_pcm_hw_params_old *oparams = NULL;
3682 params = kmalloc(sizeof(*params), GFP_KERNEL);
3686 oparams = memdup_user(_oparams, sizeof(*oparams));
3687 if (IS_ERR(oparams)) {
3688 err = PTR_ERR(oparams);
3692 snd_pcm_hw_convert_from_old_params(params, oparams);
3693 err = snd_pcm_hw_params(substream, params);
3697 snd_pcm_hw_convert_to_old_params(oparams, params);
3698 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3706 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3709 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3712 unsigned long pgoff,
3713 unsigned long flags)
3715 struct snd_pcm_file *pcm_file = file->private_data;
3716 struct snd_pcm_substream *substream = pcm_file->substream;
3717 struct snd_pcm_runtime *runtime = substream->runtime;
3718 unsigned long offset = pgoff << PAGE_SHIFT;
3721 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3722 return (unsigned long)runtime->status;
3723 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3724 return (unsigned long)runtime->control;
3726 return (unsigned long)runtime->dma_area + offset;
3730 # define snd_pcm_get_unmapped_area NULL
3737 const struct file_operations snd_pcm_f_ops[2] = {
3739 .owner = THIS_MODULE,
3740 .write = snd_pcm_write,
3741 .write_iter = snd_pcm_writev,
3742 .open = snd_pcm_playback_open,
3743 .release = snd_pcm_release,
3744 .llseek = no_llseek,
3745 .poll = snd_pcm_poll,
3746 .unlocked_ioctl = snd_pcm_ioctl,
3747 .compat_ioctl = snd_pcm_ioctl_compat,
3748 .mmap = snd_pcm_mmap,
3749 .fasync = snd_pcm_fasync,
3750 .get_unmapped_area = snd_pcm_get_unmapped_area,
3753 .owner = THIS_MODULE,
3754 .read = snd_pcm_read,
3755 .read_iter = snd_pcm_readv,
3756 .open = snd_pcm_capture_open,
3757 .release = snd_pcm_release,
3758 .llseek = no_llseek,
3759 .poll = snd_pcm_poll,
3760 .unlocked_ioctl = snd_pcm_ioctl,
3761 .compat_ioctl = snd_pcm_ioctl_compat,
3762 .mmap = snd_pcm_mmap,
3763 .fasync = snd_pcm_fasync,
3764 .get_unmapped_area = snd_pcm_get_unmapped_area,