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 /* clear the buffer for avoiding possible kernel info leaks */
743 if (runtime->dma_area && !substream->ops->copy_user)
744 memset(runtime->dma_area, 0, runtime->dma_bytes);
746 snd_pcm_timer_resolution_change(substream);
747 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
749 if (pm_qos_request_active(&substream->latency_pm_qos_req))
750 pm_qos_remove_request(&substream->latency_pm_qos_req);
751 if ((usecs = period_to_usecs(runtime)) >= 0)
752 pm_qos_add_request(&substream->latency_pm_qos_req,
753 PM_QOS_CPU_DMA_LATENCY, usecs);
756 /* hardware might be unusable from this time,
757 so we force application to retry to set
758 the correct hardware parameter settings */
759 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
760 if (substream->ops->hw_free != NULL)
761 substream->ops->hw_free(substream);
762 if (substream->managed_buffer_alloc)
763 snd_pcm_lib_free_pages(substream);
767 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
768 struct snd_pcm_hw_params __user * _params)
770 struct snd_pcm_hw_params *params;
773 params = memdup_user(_params, sizeof(*params));
775 return PTR_ERR(params);
777 err = snd_pcm_hw_params(substream, params);
781 if (copy_to_user(_params, params, sizeof(*params)))
788 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
790 struct snd_pcm_runtime *runtime;
793 if (PCM_RUNTIME_CHECK(substream))
795 runtime = substream->runtime;
796 snd_pcm_stream_lock_irq(substream);
797 switch (runtime->status->state) {
798 case SNDRV_PCM_STATE_SETUP:
799 case SNDRV_PCM_STATE_PREPARED:
802 snd_pcm_stream_unlock_irq(substream);
805 snd_pcm_stream_unlock_irq(substream);
806 if (atomic_read(&substream->mmap_count))
808 snd_pcm_sync_stop(substream);
809 if (substream->ops->hw_free)
810 result = substream->ops->hw_free(substream);
811 if (substream->managed_buffer_alloc)
812 snd_pcm_lib_free_pages(substream);
813 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
814 pm_qos_remove_request(&substream->latency_pm_qos_req);
818 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
819 struct snd_pcm_sw_params *params)
821 struct snd_pcm_runtime *runtime;
824 if (PCM_RUNTIME_CHECK(substream))
826 runtime = substream->runtime;
827 snd_pcm_stream_lock_irq(substream);
828 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
829 snd_pcm_stream_unlock_irq(substream);
832 snd_pcm_stream_unlock_irq(substream);
834 if (params->tstamp_mode < 0 ||
835 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
837 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
838 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
840 if (params->avail_min == 0)
842 if (params->silence_size >= runtime->boundary) {
843 if (params->silence_threshold != 0)
846 if (params->silence_size > params->silence_threshold)
848 if (params->silence_threshold > runtime->buffer_size)
852 snd_pcm_stream_lock_irq(substream);
853 runtime->tstamp_mode = params->tstamp_mode;
854 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
855 runtime->tstamp_type = params->tstamp_type;
856 runtime->period_step = params->period_step;
857 runtime->control->avail_min = params->avail_min;
858 runtime->start_threshold = params->start_threshold;
859 runtime->stop_threshold = params->stop_threshold;
860 runtime->silence_threshold = params->silence_threshold;
861 runtime->silence_size = params->silence_size;
862 params->boundary = runtime->boundary;
863 if (snd_pcm_running(substream)) {
864 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
865 runtime->silence_size > 0)
866 snd_pcm_playback_silence(substream, ULONG_MAX);
867 err = snd_pcm_update_state(substream, runtime);
869 snd_pcm_stream_unlock_irq(substream);
873 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
874 struct snd_pcm_sw_params __user * _params)
876 struct snd_pcm_sw_params params;
878 if (copy_from_user(¶ms, _params, sizeof(params)))
880 err = snd_pcm_sw_params(substream, ¶ms);
881 if (copy_to_user(_params, ¶ms, sizeof(params)))
886 static inline snd_pcm_uframes_t
887 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
889 snd_pcm_uframes_t delay;
891 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
892 delay = snd_pcm_playback_hw_avail(substream->runtime);
894 delay = snd_pcm_capture_avail(substream->runtime);
895 return delay + substream->runtime->delay;
898 int snd_pcm_status(struct snd_pcm_substream *substream,
899 struct snd_pcm_status *status)
901 struct snd_pcm_runtime *runtime = substream->runtime;
903 snd_pcm_stream_lock_irq(substream);
905 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
906 &runtime->audio_tstamp_config);
908 /* backwards compatible behavior */
909 if (runtime->audio_tstamp_config.type_requested ==
910 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
911 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
912 runtime->audio_tstamp_config.type_requested =
913 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
915 runtime->audio_tstamp_config.type_requested =
916 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
917 runtime->audio_tstamp_report.valid = 0;
919 runtime->audio_tstamp_report.valid = 1;
921 status->state = runtime->status->state;
922 status->suspended_state = runtime->status->suspended_state;
923 if (status->state == SNDRV_PCM_STATE_OPEN)
925 status->trigger_tstamp = runtime->trigger_tstamp;
926 if (snd_pcm_running(substream)) {
927 snd_pcm_update_hw_ptr(substream);
928 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
929 status->tstamp = runtime->status->tstamp;
930 status->driver_tstamp = runtime->driver_tstamp;
931 status->audio_tstamp =
932 runtime->status->audio_tstamp;
933 if (runtime->audio_tstamp_report.valid == 1)
934 /* backwards compatibility, no report provided in COMPAT mode */
935 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
936 &status->audio_tstamp_accuracy,
937 &runtime->audio_tstamp_report);
942 /* get tstamp only in fallback mode and only if enabled */
943 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
944 snd_pcm_gettime(runtime, &status->tstamp);
947 status->appl_ptr = runtime->control->appl_ptr;
948 status->hw_ptr = runtime->status->hw_ptr;
949 status->avail = snd_pcm_avail(substream);
950 status->delay = snd_pcm_running(substream) ?
951 snd_pcm_calc_delay(substream) : 0;
952 status->avail_max = runtime->avail_max;
953 status->overrange = runtime->overrange;
954 runtime->avail_max = 0;
955 runtime->overrange = 0;
957 snd_pcm_stream_unlock_irq(substream);
961 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
962 struct snd_pcm_status __user * _status,
965 struct snd_pcm_status status;
968 memset(&status, 0, sizeof(status));
970 * with extension, parameters are read/write,
971 * get audio_tstamp_data from user,
972 * ignore rest of status structure
974 if (ext && get_user(status.audio_tstamp_data,
975 (u32 __user *)(&_status->audio_tstamp_data)))
977 res = snd_pcm_status(substream, &status);
980 if (copy_to_user(_status, &status, sizeof(status)))
985 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
986 struct snd_pcm_channel_info * info)
988 struct snd_pcm_runtime *runtime;
989 unsigned int channel;
991 channel = info->channel;
992 runtime = substream->runtime;
993 snd_pcm_stream_lock_irq(substream);
994 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
995 snd_pcm_stream_unlock_irq(substream);
998 snd_pcm_stream_unlock_irq(substream);
999 if (channel >= runtime->channels)
1001 memset(info, 0, sizeof(*info));
1002 info->channel = channel;
1003 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1006 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1007 struct snd_pcm_channel_info __user * _info)
1009 struct snd_pcm_channel_info info;
1012 if (copy_from_user(&info, _info, sizeof(info)))
1014 res = snd_pcm_channel_info(substream, &info);
1017 if (copy_to_user(_info, &info, sizeof(info)))
1022 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1024 struct snd_pcm_runtime *runtime = substream->runtime;
1025 if (runtime->trigger_master == NULL)
1027 if (runtime->trigger_master == substream) {
1028 if (!runtime->trigger_tstamp_latched)
1029 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1031 snd_pcm_trigger_tstamp(runtime->trigger_master);
1032 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1034 runtime->trigger_master = NULL;
1038 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1039 int (*do_action)(struct snd_pcm_substream *substream, int state);
1040 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1041 void (*post_action)(struct snd_pcm_substream *substream, int state);
1045 * this functions is core for handling of linked stream
1046 * Note: the stream state might be changed also on failure
1047 * Note2: call with calling stream lock + link lock
1049 static int snd_pcm_action_group(const struct action_ops *ops,
1050 struct snd_pcm_substream *substream,
1051 int state, int do_lock)
1053 struct snd_pcm_substream *s = NULL;
1054 struct snd_pcm_substream *s1;
1055 int res = 0, depth = 1;
1057 snd_pcm_group_for_each_entry(s, substream) {
1058 if (do_lock && s != substream) {
1059 if (s->pcm->nonatomic)
1060 mutex_lock_nested(&s->self_group.mutex, depth);
1062 spin_lock_nested(&s->self_group.lock, depth);
1065 res = ops->pre_action(s, state);
1069 snd_pcm_group_for_each_entry(s, substream) {
1070 res = ops->do_action(s, state);
1072 if (ops->undo_action) {
1073 snd_pcm_group_for_each_entry(s1, substream) {
1074 if (s1 == s) /* failed stream */
1076 ops->undo_action(s1, state);
1079 s = NULL; /* unlock all */
1083 snd_pcm_group_for_each_entry(s, substream) {
1084 ops->post_action(s, state);
1088 /* unlock streams */
1089 snd_pcm_group_for_each_entry(s1, substream) {
1090 if (s1 != substream) {
1091 if (s1->pcm->nonatomic)
1092 mutex_unlock(&s1->self_group.mutex);
1094 spin_unlock(&s1->self_group.lock);
1096 if (s1 == s) /* end */
1104 * Note: call with stream lock
1106 static int snd_pcm_action_single(const struct action_ops *ops,
1107 struct snd_pcm_substream *substream,
1112 res = ops->pre_action(substream, state);
1115 res = ops->do_action(substream, state);
1117 ops->post_action(substream, state);
1118 else if (ops->undo_action)
1119 ops->undo_action(substream, state);
1123 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1124 struct snd_pcm_group *new_group)
1126 substream->group = new_group;
1127 list_move(&substream->link_list, &new_group->substreams);
1131 * Unref and unlock the group, but keep the stream lock;
1132 * when the group becomes empty and no longer referred, destroy itself
1134 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1135 struct snd_pcm_substream *substream)
1141 do_free = refcount_dec_and_test(&group->refs);
1142 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1148 * Lock the group inside a stream lock and reference it;
1149 * return the locked group object, or NULL if not linked
1151 static struct snd_pcm_group *
1152 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1154 bool nonatomic = substream->pcm->nonatomic;
1155 struct snd_pcm_group *group;
1159 if (!snd_pcm_stream_linked(substream))
1161 group = substream->group;
1162 /* block freeing the group object */
1163 refcount_inc(&group->refs);
1165 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1166 spin_trylock(&group->lock);
1170 /* re-lock for avoiding ABBA deadlock */
1171 snd_pcm_stream_unlock(substream);
1172 snd_pcm_group_lock(group, nonatomic);
1173 snd_pcm_stream_lock(substream);
1175 /* check the group again; the above opens a small race window */
1176 if (substream->group == group)
1178 /* group changed, try again */
1179 snd_pcm_group_unref(group, substream);
1185 * Note: call with stream lock
1187 static int snd_pcm_action(const struct action_ops *ops,
1188 struct snd_pcm_substream *substream,
1191 struct snd_pcm_group *group;
1194 group = snd_pcm_stream_group_ref(substream);
1196 res = snd_pcm_action_group(ops, substream, state, 1);
1198 res = snd_pcm_action_single(ops, substream, state);
1199 snd_pcm_group_unref(group, substream);
1204 * Note: don't use any locks before
1206 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1207 struct snd_pcm_substream *substream,
1212 snd_pcm_stream_lock_irq(substream);
1213 res = snd_pcm_action(ops, substream, state);
1214 snd_pcm_stream_unlock_irq(substream);
1220 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1221 struct snd_pcm_substream *substream,
1226 /* Guarantee the group members won't change during non-atomic action */
1227 down_read(&snd_pcm_link_rwsem);
1228 if (snd_pcm_stream_linked(substream))
1229 res = snd_pcm_action_group(ops, substream, state, 0);
1231 res = snd_pcm_action_single(ops, substream, state);
1232 up_read(&snd_pcm_link_rwsem);
1239 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1241 struct snd_pcm_runtime *runtime = substream->runtime;
1242 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1244 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1245 !snd_pcm_playback_data(substream))
1247 runtime->trigger_tstamp_latched = false;
1248 runtime->trigger_master = substream;
1252 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1254 if (substream->runtime->trigger_master != substream)
1256 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1259 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1261 if (substream->runtime->trigger_master == substream)
1262 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1265 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1267 struct snd_pcm_runtime *runtime = substream->runtime;
1268 snd_pcm_trigger_tstamp(substream);
1269 runtime->hw_ptr_jiffies = jiffies;
1270 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1272 runtime->status->state = state;
1273 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1274 runtime->silence_size > 0)
1275 snd_pcm_playback_silence(substream, ULONG_MAX);
1276 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1279 static const struct action_ops snd_pcm_action_start = {
1280 .pre_action = snd_pcm_pre_start,
1281 .do_action = snd_pcm_do_start,
1282 .undo_action = snd_pcm_undo_start,
1283 .post_action = snd_pcm_post_start
1287 * snd_pcm_start - start all linked streams
1288 * @substream: the PCM substream instance
1290 * Return: Zero if successful, or a negative error code.
1291 * The stream lock must be acquired before calling this function.
1293 int snd_pcm_start(struct snd_pcm_substream *substream)
1295 return snd_pcm_action(&snd_pcm_action_start, substream,
1296 SNDRV_PCM_STATE_RUNNING);
1299 /* take the stream lock and start the streams */
1300 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1302 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1303 SNDRV_PCM_STATE_RUNNING);
1309 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1311 struct snd_pcm_runtime *runtime = substream->runtime;
1312 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1314 runtime->trigger_master = substream;
1318 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1320 if (substream->runtime->trigger_master == substream &&
1321 snd_pcm_running(substream))
1322 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1323 return 0; /* unconditonally stop all substreams */
1326 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1328 struct snd_pcm_runtime *runtime = substream->runtime;
1329 if (runtime->status->state != state) {
1330 snd_pcm_trigger_tstamp(substream);
1331 runtime->status->state = state;
1332 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1334 runtime->stop_operating = true;
1335 wake_up(&runtime->sleep);
1336 wake_up(&runtime->tsleep);
1339 static const struct action_ops snd_pcm_action_stop = {
1340 .pre_action = snd_pcm_pre_stop,
1341 .do_action = snd_pcm_do_stop,
1342 .post_action = snd_pcm_post_stop
1346 * snd_pcm_stop - try to stop all running streams in the substream group
1347 * @substream: the PCM substream instance
1348 * @state: PCM state after stopping the stream
1350 * The state of each stream is then changed to the given state unconditionally.
1352 * Return: Zero if successful, or a negative error code.
1354 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1356 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1358 EXPORT_SYMBOL(snd_pcm_stop);
1361 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1362 * @substream: the PCM substream
1364 * After stopping, the state is changed to SETUP.
1365 * Unlike snd_pcm_stop(), this affects only the given stream.
1367 * Return: Zero if succesful, or a negative error code.
1369 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1371 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1372 SNDRV_PCM_STATE_SETUP);
1376 * snd_pcm_stop_xrun - stop the running streams as XRUN
1377 * @substream: the PCM substream instance
1379 * This stops the given running substream (and all linked substreams) as XRUN.
1380 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1382 * Return: Zero if successful, or a negative error code.
1384 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1386 unsigned long flags;
1388 snd_pcm_stream_lock_irqsave(substream, flags);
1389 if (substream->runtime && snd_pcm_running(substream))
1390 __snd_pcm_xrun(substream);
1391 snd_pcm_stream_unlock_irqrestore(substream, flags);
1394 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1399 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1401 struct snd_pcm_runtime *runtime = substream->runtime;
1402 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1405 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1407 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1409 runtime->trigger_master = substream;
1413 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1415 if (substream->runtime->trigger_master != substream)
1417 /* some drivers might use hw_ptr to recover from the pause -
1418 update the hw_ptr now */
1420 snd_pcm_update_hw_ptr(substream);
1421 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1422 * a delta between the current jiffies, this gives a large enough
1423 * delta, effectively to skip the check once.
1425 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1426 return substream->ops->trigger(substream,
1427 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1428 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1431 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1433 if (substream->runtime->trigger_master == substream)
1434 substream->ops->trigger(substream,
1435 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1436 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1439 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1441 struct snd_pcm_runtime *runtime = substream->runtime;
1442 snd_pcm_trigger_tstamp(substream);
1444 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1445 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1446 wake_up(&runtime->sleep);
1447 wake_up(&runtime->tsleep);
1449 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1450 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1454 static const struct action_ops snd_pcm_action_pause = {
1455 .pre_action = snd_pcm_pre_pause,
1456 .do_action = snd_pcm_do_pause,
1457 .undo_action = snd_pcm_undo_pause,
1458 .post_action = snd_pcm_post_pause
1462 * Push/release the pause for all linked streams.
1464 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1466 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1472 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1474 struct snd_pcm_runtime *runtime = substream->runtime;
1475 switch (runtime->status->state) {
1476 case SNDRV_PCM_STATE_SUSPENDED:
1478 /* unresumable PCM state; return -EBUSY for skipping suspend */
1479 case SNDRV_PCM_STATE_OPEN:
1480 case SNDRV_PCM_STATE_SETUP:
1481 case SNDRV_PCM_STATE_DISCONNECTED:
1484 runtime->trigger_master = substream;
1488 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1491 if (runtime->trigger_master != substream)
1493 if (! snd_pcm_running(substream))
1495 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1496 return 0; /* suspend unconditionally */
1499 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1501 struct snd_pcm_runtime *runtime = substream->runtime;
1502 snd_pcm_trigger_tstamp(substream);
1503 runtime->status->suspended_state = runtime->status->state;
1504 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1505 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1506 wake_up(&runtime->sleep);
1507 wake_up(&runtime->tsleep);
1510 static const struct action_ops snd_pcm_action_suspend = {
1511 .pre_action = snd_pcm_pre_suspend,
1512 .do_action = snd_pcm_do_suspend,
1513 .post_action = snd_pcm_post_suspend
1517 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1518 * @substream: the PCM substream
1520 * After this call, all streams are changed to SUSPENDED state.
1522 * Return: Zero if successful, or a negative error code.
1524 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1527 unsigned long flags;
1529 snd_pcm_stream_lock_irqsave(substream, flags);
1530 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1531 snd_pcm_stream_unlock_irqrestore(substream, flags);
1536 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1537 * @pcm: the PCM instance
1539 * After this call, all streams are changed to SUSPENDED state.
1541 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1543 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1545 struct snd_pcm_substream *substream;
1546 int stream, err = 0;
1551 for (stream = 0; stream < 2; stream++) {
1552 for (substream = pcm->streams[stream].substream;
1553 substream; substream = substream->next) {
1554 /* FIXME: the open/close code should lock this as well */
1555 if (substream->runtime == NULL)
1559 * Skip BE dai link PCM's that are internal and may
1560 * not have their substream ops set.
1562 if (!substream->ops)
1565 err = snd_pcm_suspend(substream);
1566 if (err < 0 && err != -EBUSY)
1572 EXPORT_SYMBOL(snd_pcm_suspend_all);
1576 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1578 struct snd_pcm_runtime *runtime = substream->runtime;
1579 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1581 runtime->trigger_master = substream;
1585 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1587 struct snd_pcm_runtime *runtime = substream->runtime;
1588 if (runtime->trigger_master != substream)
1590 /* DMA not running previously? */
1591 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1592 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1593 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1595 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1598 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1600 if (substream->runtime->trigger_master == substream &&
1601 snd_pcm_running(substream))
1602 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1605 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1607 struct snd_pcm_runtime *runtime = substream->runtime;
1608 snd_pcm_trigger_tstamp(substream);
1609 runtime->status->state = runtime->status->suspended_state;
1610 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1611 snd_pcm_sync_stop(substream);
1614 static const struct action_ops snd_pcm_action_resume = {
1615 .pre_action = snd_pcm_pre_resume,
1616 .do_action = snd_pcm_do_resume,
1617 .undo_action = snd_pcm_undo_resume,
1618 .post_action = snd_pcm_post_resume
1621 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1623 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1628 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1633 #endif /* CONFIG_PM */
1638 * Change the RUNNING stream(s) to XRUN state.
1640 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1642 struct snd_pcm_runtime *runtime = substream->runtime;
1645 snd_pcm_stream_lock_irq(substream);
1646 switch (runtime->status->state) {
1647 case SNDRV_PCM_STATE_XRUN:
1648 result = 0; /* already there */
1650 case SNDRV_PCM_STATE_RUNNING:
1651 __snd_pcm_xrun(substream);
1657 snd_pcm_stream_unlock_irq(substream);
1664 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1666 struct snd_pcm_runtime *runtime = substream->runtime;
1667 switch (runtime->status->state) {
1668 case SNDRV_PCM_STATE_RUNNING:
1669 case SNDRV_PCM_STATE_PREPARED:
1670 case SNDRV_PCM_STATE_PAUSED:
1671 case SNDRV_PCM_STATE_SUSPENDED:
1678 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1680 struct snd_pcm_runtime *runtime = substream->runtime;
1681 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1684 runtime->hw_ptr_base = 0;
1685 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1686 runtime->status->hw_ptr % runtime->period_size;
1687 runtime->silence_start = runtime->status->hw_ptr;
1688 runtime->silence_filled = 0;
1692 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1694 struct snd_pcm_runtime *runtime = substream->runtime;
1695 runtime->control->appl_ptr = runtime->status->hw_ptr;
1696 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1697 runtime->silence_size > 0)
1698 snd_pcm_playback_silence(substream, ULONG_MAX);
1701 static const struct action_ops snd_pcm_action_reset = {
1702 .pre_action = snd_pcm_pre_reset,
1703 .do_action = snd_pcm_do_reset,
1704 .post_action = snd_pcm_post_reset
1707 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1709 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1715 /* we use the second argument for updating f_flags */
1716 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1719 struct snd_pcm_runtime *runtime = substream->runtime;
1720 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1721 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1723 if (snd_pcm_running(substream))
1725 substream->f_flags = f_flags;
1729 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1732 snd_pcm_sync_stop(substream);
1733 err = substream->ops->prepare(substream);
1736 return snd_pcm_do_reset(substream, 0);
1739 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1741 struct snd_pcm_runtime *runtime = substream->runtime;
1742 runtime->control->appl_ptr = runtime->status->hw_ptr;
1743 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1746 static const struct action_ops snd_pcm_action_prepare = {
1747 .pre_action = snd_pcm_pre_prepare,
1748 .do_action = snd_pcm_do_prepare,
1749 .post_action = snd_pcm_post_prepare
1753 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1754 * @substream: the PCM substream instance
1755 * @file: file to refer f_flags
1757 * Return: Zero if successful, or a negative error code.
1759 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1765 f_flags = file->f_flags;
1767 f_flags = substream->f_flags;
1769 snd_pcm_stream_lock_irq(substream);
1770 switch (substream->runtime->status->state) {
1771 case SNDRV_PCM_STATE_PAUSED:
1772 snd_pcm_pause(substream, 0);
1774 case SNDRV_PCM_STATE_SUSPENDED:
1775 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1778 snd_pcm_stream_unlock_irq(substream);
1780 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1781 substream, f_flags);
1788 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1790 struct snd_pcm_runtime *runtime = substream->runtime;
1791 switch (runtime->status->state) {
1792 case SNDRV_PCM_STATE_OPEN:
1793 case SNDRV_PCM_STATE_DISCONNECTED:
1794 case SNDRV_PCM_STATE_SUSPENDED:
1797 runtime->trigger_master = substream;
1801 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1803 struct snd_pcm_runtime *runtime = substream->runtime;
1804 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1805 switch (runtime->status->state) {
1806 case SNDRV_PCM_STATE_PREPARED:
1807 /* start playback stream if possible */
1808 if (! snd_pcm_playback_empty(substream)) {
1809 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1810 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1812 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1815 case SNDRV_PCM_STATE_RUNNING:
1816 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1818 case SNDRV_PCM_STATE_XRUN:
1819 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1825 /* stop running stream */
1826 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1827 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1828 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1829 snd_pcm_do_stop(substream, new_state);
1830 snd_pcm_post_stop(substream, new_state);
1834 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1835 runtime->trigger_master == substream &&
1836 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1837 return substream->ops->trigger(substream,
1838 SNDRV_PCM_TRIGGER_DRAIN);
1843 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1847 static const struct action_ops snd_pcm_action_drain_init = {
1848 .pre_action = snd_pcm_pre_drain_init,
1849 .do_action = snd_pcm_do_drain_init,
1850 .post_action = snd_pcm_post_drain_init
1854 * Drain the stream(s).
1855 * When the substream is linked, sync until the draining of all playback streams
1857 * After this call, all streams are supposed to be either SETUP or DRAINING
1858 * (capture only) state.
1860 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1863 struct snd_card *card;
1864 struct snd_pcm_runtime *runtime;
1865 struct snd_pcm_substream *s;
1866 struct snd_pcm_group *group;
1867 wait_queue_entry_t wait;
1871 card = substream->pcm->card;
1872 runtime = substream->runtime;
1874 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1878 if (file->f_flags & O_NONBLOCK)
1880 } else if (substream->f_flags & O_NONBLOCK)
1883 snd_pcm_stream_lock_irq(substream);
1885 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1886 snd_pcm_pause(substream, 0);
1888 /* pre-start/stop - all running streams are changed to DRAINING state */
1889 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1892 /* in non-blocking, we don't wait in ioctl but let caller poll */
1900 struct snd_pcm_runtime *to_check;
1901 if (signal_pending(current)) {
1902 result = -ERESTARTSYS;
1905 /* find a substream to drain */
1907 group = snd_pcm_stream_group_ref(substream);
1908 snd_pcm_group_for_each_entry(s, substream) {
1909 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1911 runtime = s->runtime;
1912 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1917 snd_pcm_group_unref(group, substream);
1919 break; /* all drained */
1920 init_waitqueue_entry(&wait, current);
1921 set_current_state(TASK_INTERRUPTIBLE);
1922 add_wait_queue(&to_check->sleep, &wait);
1923 snd_pcm_stream_unlock_irq(substream);
1924 if (runtime->no_period_wakeup)
1925 tout = MAX_SCHEDULE_TIMEOUT;
1928 if (runtime->rate) {
1929 long t = runtime->period_size * 2 / runtime->rate;
1930 tout = max(t, tout);
1932 tout = msecs_to_jiffies(tout * 1000);
1934 tout = schedule_timeout(tout);
1936 snd_pcm_stream_lock_irq(substream);
1937 group = snd_pcm_stream_group_ref(substream);
1938 snd_pcm_group_for_each_entry(s, substream) {
1939 if (s->runtime == to_check) {
1940 remove_wait_queue(&to_check->sleep, &wait);
1944 snd_pcm_group_unref(group, substream);
1946 if (card->shutdown) {
1951 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1954 dev_dbg(substream->pcm->card->dev,
1955 "playback drain error (DMA or IRQ trouble?)\n");
1956 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1964 snd_pcm_stream_unlock_irq(substream);
1972 * Immediately put all linked substreams into SETUP state.
1974 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1976 struct snd_pcm_runtime *runtime;
1979 if (PCM_RUNTIME_CHECK(substream))
1981 runtime = substream->runtime;
1983 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1984 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1987 snd_pcm_stream_lock_irq(substream);
1989 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1990 snd_pcm_pause(substream, 0);
1992 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1993 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1994 snd_pcm_stream_unlock_irq(substream);
2000 static bool is_pcm_file(struct file *file)
2002 struct inode *inode = file_inode(file);
2003 struct snd_pcm *pcm;
2006 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2008 minor = iminor(inode);
2009 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2011 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2014 snd_card_unref(pcm->card);
2021 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2024 struct snd_pcm_file *pcm_file;
2025 struct snd_pcm_substream *substream1;
2026 struct snd_pcm_group *group, *target_group;
2027 bool nonatomic = substream->pcm->nonatomic;
2028 struct fd f = fdget(fd);
2032 if (!is_pcm_file(f.file)) {
2036 pcm_file = f.file->private_data;
2037 substream1 = pcm_file->substream;
2038 group = kzalloc(sizeof(*group), GFP_KERNEL);
2043 snd_pcm_group_init(group);
2045 down_write(&snd_pcm_link_rwsem);
2046 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2047 substream->runtime->status->state != substream1->runtime->status->state ||
2048 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2052 if (snd_pcm_stream_linked(substream1)) {
2057 snd_pcm_stream_lock_irq(substream);
2058 if (!snd_pcm_stream_linked(substream)) {
2059 snd_pcm_group_assign(substream, group);
2060 group = NULL; /* assigned, don't free this one below */
2062 target_group = substream->group;
2063 snd_pcm_stream_unlock_irq(substream);
2065 snd_pcm_group_lock_irq(target_group, nonatomic);
2066 snd_pcm_stream_lock(substream1);
2067 snd_pcm_group_assign(substream1, target_group);
2068 refcount_inc(&target_group->refs);
2069 snd_pcm_stream_unlock(substream1);
2070 snd_pcm_group_unlock_irq(target_group, nonatomic);
2072 up_write(&snd_pcm_link_rwsem);
2080 static void relink_to_local(struct snd_pcm_substream *substream)
2082 snd_pcm_stream_lock(substream);
2083 snd_pcm_group_assign(substream, &substream->self_group);
2084 snd_pcm_stream_unlock(substream);
2087 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2089 struct snd_pcm_group *group;
2090 bool nonatomic = substream->pcm->nonatomic;
2091 bool do_free = false;
2094 down_write(&snd_pcm_link_rwsem);
2096 if (!snd_pcm_stream_linked(substream)) {
2101 group = substream->group;
2102 snd_pcm_group_lock_irq(group, nonatomic);
2104 relink_to_local(substream);
2105 refcount_dec(&group->refs);
2107 /* detach the last stream, too */
2108 if (list_is_singular(&group->substreams)) {
2109 relink_to_local(list_first_entry(&group->substreams,
2110 struct snd_pcm_substream,
2112 do_free = refcount_dec_and_test(&group->refs);
2115 snd_pcm_group_unlock_irq(group, nonatomic);
2120 up_write(&snd_pcm_link_rwsem);
2127 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2128 struct snd_pcm_hw_rule *rule)
2130 struct snd_interval t;
2131 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2132 hw_param_interval_c(params, rule->deps[1]), &t);
2133 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2136 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2137 struct snd_pcm_hw_rule *rule)
2139 struct snd_interval t;
2140 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2141 hw_param_interval_c(params, rule->deps[1]), &t);
2142 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2145 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2146 struct snd_pcm_hw_rule *rule)
2148 struct snd_interval t;
2149 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2150 hw_param_interval_c(params, rule->deps[1]),
2151 (unsigned long) rule->private, &t);
2152 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2155 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2156 struct snd_pcm_hw_rule *rule)
2158 struct snd_interval t;
2159 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2160 (unsigned long) rule->private,
2161 hw_param_interval_c(params, rule->deps[1]), &t);
2162 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2165 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2166 struct snd_pcm_hw_rule *rule)
2169 const struct snd_interval *i =
2170 hw_param_interval_c(params, rule->deps[0]);
2172 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2174 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2176 if (! snd_mask_test(mask, k))
2178 bits = snd_pcm_format_physical_width(k);
2180 continue; /* ignore invalid formats */
2181 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2182 snd_mask_reset(&m, k);
2184 return snd_mask_refine(mask, &m);
2187 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2188 struct snd_pcm_hw_rule *rule)
2190 struct snd_interval t;
2196 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2198 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2200 bits = snd_pcm_format_physical_width(k);
2202 continue; /* ignore invalid formats */
2203 if (t.min > (unsigned)bits)
2205 if (t.max < (unsigned)bits)
2209 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2212 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2213 #error "Change this table"
2216 static const unsigned int rates[] = {
2217 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2218 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2221 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2222 .count = ARRAY_SIZE(rates),
2226 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2227 struct snd_pcm_hw_rule *rule)
2229 struct snd_pcm_hardware *hw = rule->private;
2230 return snd_interval_list(hw_param_interval(params, rule->var),
2231 snd_pcm_known_rates.count,
2232 snd_pcm_known_rates.list, hw->rates);
2235 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2236 struct snd_pcm_hw_rule *rule)
2238 struct snd_interval t;
2239 struct snd_pcm_substream *substream = rule->private;
2241 t.max = substream->buffer_bytes_max;
2245 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2248 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2250 struct snd_pcm_runtime *runtime = substream->runtime;
2251 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2254 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2255 snd_mask_any(constrs_mask(constrs, k));
2258 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2259 snd_interval_any(constrs_interval(constrs, k));
2262 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2263 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2264 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2265 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2266 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2268 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2269 snd_pcm_hw_rule_format, NULL,
2270 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2273 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2274 snd_pcm_hw_rule_sample_bits, NULL,
2275 SNDRV_PCM_HW_PARAM_FORMAT,
2276 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2279 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2280 snd_pcm_hw_rule_div, NULL,
2281 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2284 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2285 snd_pcm_hw_rule_mul, NULL,
2286 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2289 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2290 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2291 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2294 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2295 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2296 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2299 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2300 snd_pcm_hw_rule_div, NULL,
2301 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2304 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2305 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2306 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2309 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2310 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2311 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2314 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2315 snd_pcm_hw_rule_div, NULL,
2316 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2319 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2320 snd_pcm_hw_rule_div, NULL,
2321 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2324 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2325 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2326 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2329 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2330 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2331 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2334 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2335 snd_pcm_hw_rule_mul, NULL,
2336 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2339 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2340 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2341 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2344 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2345 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2346 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2349 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2350 snd_pcm_hw_rule_muldivk, (void*) 8,
2351 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2354 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2355 snd_pcm_hw_rule_muldivk, (void*) 8,
2356 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2359 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2360 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2361 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2364 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2365 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2366 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2372 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2374 struct snd_pcm_runtime *runtime = substream->runtime;
2375 struct snd_pcm_hardware *hw = &runtime->hw;
2377 unsigned int mask = 0;
2379 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2380 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2381 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2382 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2383 if (hw_support_mmap(substream)) {
2384 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2385 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2386 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2387 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2388 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2389 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2391 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2395 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2399 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2403 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2404 hw->channels_min, hw->channels_max);
2408 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2409 hw->rate_min, hw->rate_max);
2413 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2414 hw->period_bytes_min, hw->period_bytes_max);
2418 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2419 hw->periods_min, hw->periods_max);
2423 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2424 hw->period_bytes_min, hw->buffer_bytes_max);
2428 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2429 snd_pcm_hw_rule_buffer_bytes_max, substream,
2430 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2435 if (runtime->dma_bytes) {
2436 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2441 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2442 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2443 snd_pcm_hw_rule_rate, hw,
2444 SNDRV_PCM_HW_PARAM_RATE, -1);
2449 /* FIXME: this belong to lowlevel */
2450 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2455 static void pcm_release_private(struct snd_pcm_substream *substream)
2457 if (snd_pcm_stream_linked(substream))
2458 snd_pcm_unlink(substream);
2461 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2463 substream->ref_count--;
2464 if (substream->ref_count > 0)
2467 snd_pcm_drop(substream);
2468 if (substream->hw_opened) {
2469 if (substream->ops->hw_free &&
2470 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2471 substream->ops->hw_free(substream);
2472 substream->ops->close(substream);
2473 substream->hw_opened = 0;
2475 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2476 pm_qos_remove_request(&substream->latency_pm_qos_req);
2477 if (substream->pcm_release) {
2478 substream->pcm_release(substream);
2479 substream->pcm_release = NULL;
2481 snd_pcm_detach_substream(substream);
2483 EXPORT_SYMBOL(snd_pcm_release_substream);
2485 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2487 struct snd_pcm_substream **rsubstream)
2489 struct snd_pcm_substream *substream;
2492 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2495 if (substream->ref_count > 1) {
2496 *rsubstream = substream;
2500 err = snd_pcm_hw_constraints_init(substream);
2502 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2506 if ((err = substream->ops->open(substream)) < 0)
2509 substream->hw_opened = 1;
2511 err = snd_pcm_hw_constraints_complete(substream);
2513 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2517 *rsubstream = substream;
2521 snd_pcm_release_substream(substream);
2524 EXPORT_SYMBOL(snd_pcm_open_substream);
2526 static int snd_pcm_open_file(struct file *file,
2527 struct snd_pcm *pcm,
2530 struct snd_pcm_file *pcm_file;
2531 struct snd_pcm_substream *substream;
2534 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2538 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2539 if (pcm_file == NULL) {
2540 snd_pcm_release_substream(substream);
2543 pcm_file->substream = substream;
2544 if (substream->ref_count == 1)
2545 substream->pcm_release = pcm_release_private;
2546 file->private_data = pcm_file;
2551 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2553 struct snd_pcm *pcm;
2554 int err = nonseekable_open(inode, file);
2557 pcm = snd_lookup_minor_data(iminor(inode),
2558 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2559 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2561 snd_card_unref(pcm->card);
2565 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2567 struct snd_pcm *pcm;
2568 int err = nonseekable_open(inode, file);
2571 pcm = snd_lookup_minor_data(iminor(inode),
2572 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2573 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2575 snd_card_unref(pcm->card);
2579 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2582 wait_queue_entry_t wait;
2588 err = snd_card_file_add(pcm->card, file);
2591 if (!try_module_get(pcm->card->module)) {
2595 init_waitqueue_entry(&wait, current);
2596 add_wait_queue(&pcm->open_wait, &wait);
2597 mutex_lock(&pcm->open_mutex);
2599 err = snd_pcm_open_file(file, pcm, stream);
2602 if (err == -EAGAIN) {
2603 if (file->f_flags & O_NONBLOCK) {
2609 set_current_state(TASK_INTERRUPTIBLE);
2610 mutex_unlock(&pcm->open_mutex);
2612 mutex_lock(&pcm->open_mutex);
2613 if (pcm->card->shutdown) {
2617 if (signal_pending(current)) {
2622 remove_wait_queue(&pcm->open_wait, &wait);
2623 mutex_unlock(&pcm->open_mutex);
2629 module_put(pcm->card->module);
2631 snd_card_file_remove(pcm->card, file);
2636 static int snd_pcm_release(struct inode *inode, struct file *file)
2638 struct snd_pcm *pcm;
2639 struct snd_pcm_substream *substream;
2640 struct snd_pcm_file *pcm_file;
2642 pcm_file = file->private_data;
2643 substream = pcm_file->substream;
2644 if (snd_BUG_ON(!substream))
2646 pcm = substream->pcm;
2647 mutex_lock(&pcm->open_mutex);
2648 snd_pcm_release_substream(substream);
2650 mutex_unlock(&pcm->open_mutex);
2651 wake_up(&pcm->open_wait);
2652 module_put(pcm->card->module);
2653 snd_card_file_remove(pcm->card, file);
2657 /* check and update PCM state; return 0 or a negative error
2658 * call this inside PCM lock
2660 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2662 switch (substream->runtime->status->state) {
2663 case SNDRV_PCM_STATE_DRAINING:
2664 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2667 case SNDRV_PCM_STATE_RUNNING:
2668 return snd_pcm_update_hw_ptr(substream);
2669 case SNDRV_PCM_STATE_PREPARED:
2670 case SNDRV_PCM_STATE_PAUSED:
2672 case SNDRV_PCM_STATE_SUSPENDED:
2674 case SNDRV_PCM_STATE_XRUN:
2681 /* increase the appl_ptr; returns the processed frames or a negative error */
2682 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2683 snd_pcm_uframes_t frames,
2684 snd_pcm_sframes_t avail)
2686 struct snd_pcm_runtime *runtime = substream->runtime;
2687 snd_pcm_sframes_t appl_ptr;
2692 if (frames > (snd_pcm_uframes_t)avail)
2694 appl_ptr = runtime->control->appl_ptr + frames;
2695 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2696 appl_ptr -= runtime->boundary;
2697 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2698 return ret < 0 ? ret : frames;
2701 /* decrease the appl_ptr; returns the processed frames or zero for error */
2702 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2703 snd_pcm_uframes_t frames,
2704 snd_pcm_sframes_t avail)
2706 struct snd_pcm_runtime *runtime = substream->runtime;
2707 snd_pcm_sframes_t appl_ptr;
2712 if (frames > (snd_pcm_uframes_t)avail)
2714 appl_ptr = runtime->control->appl_ptr - frames;
2716 appl_ptr += runtime->boundary;
2717 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2718 /* NOTE: we return zero for errors because PulseAudio gets depressed
2719 * upon receiving an error from rewind ioctl and stops processing
2720 * any longer. Returning zero means that no rewind is done, so
2721 * it's not absolutely wrong to answer like that.
2723 return ret < 0 ? 0 : frames;
2726 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2727 snd_pcm_uframes_t frames)
2729 snd_pcm_sframes_t ret;
2734 snd_pcm_stream_lock_irq(substream);
2735 ret = do_pcm_hwsync(substream);
2737 ret = rewind_appl_ptr(substream, frames,
2738 snd_pcm_hw_avail(substream));
2739 snd_pcm_stream_unlock_irq(substream);
2743 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2744 snd_pcm_uframes_t frames)
2746 snd_pcm_sframes_t ret;
2751 snd_pcm_stream_lock_irq(substream);
2752 ret = do_pcm_hwsync(substream);
2754 ret = forward_appl_ptr(substream, frames,
2755 snd_pcm_avail(substream));
2756 snd_pcm_stream_unlock_irq(substream);
2760 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2764 snd_pcm_stream_lock_irq(substream);
2765 err = do_pcm_hwsync(substream);
2766 snd_pcm_stream_unlock_irq(substream);
2770 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2771 snd_pcm_sframes_t *delay)
2774 snd_pcm_sframes_t n = 0;
2776 snd_pcm_stream_lock_irq(substream);
2777 err = do_pcm_hwsync(substream);
2779 n = snd_pcm_calc_delay(substream);
2780 snd_pcm_stream_unlock_irq(substream);
2786 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2787 struct snd_pcm_sync_ptr __user *_sync_ptr)
2789 struct snd_pcm_runtime *runtime = substream->runtime;
2790 struct snd_pcm_sync_ptr sync_ptr;
2791 volatile struct snd_pcm_mmap_status *status;
2792 volatile struct snd_pcm_mmap_control *control;
2795 memset(&sync_ptr, 0, sizeof(sync_ptr));
2796 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2798 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2800 status = runtime->status;
2801 control = runtime->control;
2802 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2803 err = snd_pcm_hwsync(substream);
2807 snd_pcm_stream_lock_irq(substream);
2808 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2809 err = pcm_lib_apply_appl_ptr(substream,
2810 sync_ptr.c.control.appl_ptr);
2812 snd_pcm_stream_unlock_irq(substream);
2816 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2818 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2819 control->avail_min = sync_ptr.c.control.avail_min;
2821 sync_ptr.c.control.avail_min = control->avail_min;
2822 sync_ptr.s.status.state = status->state;
2823 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2824 sync_ptr.s.status.tstamp = status->tstamp;
2825 sync_ptr.s.status.suspended_state = status->suspended_state;
2826 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2827 snd_pcm_stream_unlock_irq(substream);
2828 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2833 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2835 struct snd_pcm_runtime *runtime = substream->runtime;
2838 if (get_user(arg, _arg))
2840 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2842 runtime->tstamp_type = arg;
2846 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2847 struct snd_xferi __user *_xferi)
2849 struct snd_xferi xferi;
2850 struct snd_pcm_runtime *runtime = substream->runtime;
2851 snd_pcm_sframes_t result;
2853 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2855 if (put_user(0, &_xferi->result))
2857 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2859 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2860 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2862 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2863 __put_user(result, &_xferi->result);
2864 return result < 0 ? result : 0;
2867 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2868 struct snd_xfern __user *_xfern)
2870 struct snd_xfern xfern;
2871 struct snd_pcm_runtime *runtime = substream->runtime;
2873 snd_pcm_sframes_t result;
2875 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2877 if (runtime->channels > 128)
2879 if (put_user(0, &_xfern->result))
2881 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2884 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2886 return PTR_ERR(bufs);
2887 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2888 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2890 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2892 __put_user(result, &_xfern->result);
2893 return result < 0 ? result : 0;
2896 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2897 snd_pcm_uframes_t __user *_frames)
2899 snd_pcm_uframes_t frames;
2900 snd_pcm_sframes_t result;
2902 if (get_user(frames, _frames))
2904 if (put_user(0, _frames))
2906 result = snd_pcm_rewind(substream, frames);
2907 __put_user(result, _frames);
2908 return result < 0 ? result : 0;
2911 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2912 snd_pcm_uframes_t __user *_frames)
2914 snd_pcm_uframes_t frames;
2915 snd_pcm_sframes_t result;
2917 if (get_user(frames, _frames))
2919 if (put_user(0, _frames))
2921 result = snd_pcm_forward(substream, frames);
2922 __put_user(result, _frames);
2923 return result < 0 ? result : 0;
2926 static int snd_pcm_common_ioctl(struct file *file,
2927 struct snd_pcm_substream *substream,
2928 unsigned int cmd, void __user *arg)
2930 struct snd_pcm_file *pcm_file = file->private_data;
2933 if (PCM_RUNTIME_CHECK(substream))
2936 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2941 case SNDRV_PCM_IOCTL_PVERSION:
2942 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2943 case SNDRV_PCM_IOCTL_INFO:
2944 return snd_pcm_info_user(substream, arg);
2945 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2947 case SNDRV_PCM_IOCTL_TTSTAMP:
2948 return snd_pcm_tstamp(substream, arg);
2949 case SNDRV_PCM_IOCTL_USER_PVERSION:
2950 if (get_user(pcm_file->user_pversion,
2951 (unsigned int __user *)arg))
2954 case SNDRV_PCM_IOCTL_HW_REFINE:
2955 return snd_pcm_hw_refine_user(substream, arg);
2956 case SNDRV_PCM_IOCTL_HW_PARAMS:
2957 return snd_pcm_hw_params_user(substream, arg);
2958 case SNDRV_PCM_IOCTL_HW_FREE:
2959 return snd_pcm_hw_free(substream);
2960 case SNDRV_PCM_IOCTL_SW_PARAMS:
2961 return snd_pcm_sw_params_user(substream, arg);
2962 case SNDRV_PCM_IOCTL_STATUS:
2963 return snd_pcm_status_user(substream, arg, false);
2964 case SNDRV_PCM_IOCTL_STATUS_EXT:
2965 return snd_pcm_status_user(substream, arg, true);
2966 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2967 return snd_pcm_channel_info_user(substream, arg);
2968 case SNDRV_PCM_IOCTL_PREPARE:
2969 return snd_pcm_prepare(substream, file);
2970 case SNDRV_PCM_IOCTL_RESET:
2971 return snd_pcm_reset(substream);
2972 case SNDRV_PCM_IOCTL_START:
2973 return snd_pcm_start_lock_irq(substream);
2974 case SNDRV_PCM_IOCTL_LINK:
2975 return snd_pcm_link(substream, (int)(unsigned long) arg);
2976 case SNDRV_PCM_IOCTL_UNLINK:
2977 return snd_pcm_unlink(substream);
2978 case SNDRV_PCM_IOCTL_RESUME:
2979 return snd_pcm_resume(substream);
2980 case SNDRV_PCM_IOCTL_XRUN:
2981 return snd_pcm_xrun(substream);
2982 case SNDRV_PCM_IOCTL_HWSYNC:
2983 return snd_pcm_hwsync(substream);
2984 case SNDRV_PCM_IOCTL_DELAY:
2986 snd_pcm_sframes_t delay;
2987 snd_pcm_sframes_t __user *res = arg;
2990 err = snd_pcm_delay(substream, &delay);
2993 if (put_user(delay, res))
2997 case SNDRV_PCM_IOCTL_SYNC_PTR:
2998 return snd_pcm_sync_ptr(substream, arg);
2999 #ifdef CONFIG_SND_SUPPORT_OLD_API
3000 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3001 return snd_pcm_hw_refine_old_user(substream, arg);
3002 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3003 return snd_pcm_hw_params_old_user(substream, arg);
3005 case SNDRV_PCM_IOCTL_DRAIN:
3006 return snd_pcm_drain(substream, file);
3007 case SNDRV_PCM_IOCTL_DROP:
3008 return snd_pcm_drop(substream);
3009 case SNDRV_PCM_IOCTL_PAUSE:
3010 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3012 (int)(unsigned long)arg);
3013 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3014 case SNDRV_PCM_IOCTL_READI_FRAMES:
3015 return snd_pcm_xferi_frames_ioctl(substream, arg);
3016 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3017 case SNDRV_PCM_IOCTL_READN_FRAMES:
3018 return snd_pcm_xfern_frames_ioctl(substream, arg);
3019 case SNDRV_PCM_IOCTL_REWIND:
3020 return snd_pcm_rewind_ioctl(substream, arg);
3021 case SNDRV_PCM_IOCTL_FORWARD:
3022 return snd_pcm_forward_ioctl(substream, arg);
3024 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3028 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3031 struct snd_pcm_file *pcm_file;
3033 pcm_file = file->private_data;
3035 if (((cmd >> 8) & 0xff) != 'A')
3038 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3039 (void __user *)arg);
3043 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3044 * @substream: PCM substream
3046 * @arg: IOCTL argument
3048 * The function is provided primarily for OSS layer and USB gadget drivers,
3049 * and it allows only the limited set of ioctls (hw_params, sw_params,
3050 * prepare, start, drain, drop, forward).
3052 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3053 unsigned int cmd, void *arg)
3055 snd_pcm_uframes_t *frames = arg;
3056 snd_pcm_sframes_t result;
3059 case SNDRV_PCM_IOCTL_FORWARD:
3061 /* provided only for OSS; capture-only and no value returned */
3062 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3064 result = snd_pcm_forward(substream, *frames);
3065 return result < 0 ? result : 0;
3067 case SNDRV_PCM_IOCTL_HW_PARAMS:
3068 return snd_pcm_hw_params(substream, arg);
3069 case SNDRV_PCM_IOCTL_SW_PARAMS:
3070 return snd_pcm_sw_params(substream, arg);
3071 case SNDRV_PCM_IOCTL_PREPARE:
3072 return snd_pcm_prepare(substream, NULL);
3073 case SNDRV_PCM_IOCTL_START:
3074 return snd_pcm_start_lock_irq(substream);
3075 case SNDRV_PCM_IOCTL_DRAIN:
3076 return snd_pcm_drain(substream, NULL);
3077 case SNDRV_PCM_IOCTL_DROP:
3078 return snd_pcm_drop(substream);
3079 case SNDRV_PCM_IOCTL_DELAY:
3080 return snd_pcm_delay(substream, frames);
3085 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3087 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3090 struct snd_pcm_file *pcm_file;
3091 struct snd_pcm_substream *substream;
3092 struct snd_pcm_runtime *runtime;
3093 snd_pcm_sframes_t result;
3095 pcm_file = file->private_data;
3096 substream = pcm_file->substream;
3097 if (PCM_RUNTIME_CHECK(substream))
3099 runtime = substream->runtime;
3100 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3102 if (!frame_aligned(runtime, count))
3104 count = bytes_to_frames(runtime, count);
3105 result = snd_pcm_lib_read(substream, buf, count);
3107 result = frames_to_bytes(runtime, result);
3111 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3112 size_t count, loff_t * offset)
3114 struct snd_pcm_file *pcm_file;
3115 struct snd_pcm_substream *substream;
3116 struct snd_pcm_runtime *runtime;
3117 snd_pcm_sframes_t result;
3119 pcm_file = file->private_data;
3120 substream = pcm_file->substream;
3121 if (PCM_RUNTIME_CHECK(substream))
3123 runtime = substream->runtime;
3124 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3126 if (!frame_aligned(runtime, count))
3128 count = bytes_to_frames(runtime, count);
3129 result = snd_pcm_lib_write(substream, buf, count);
3131 result = frames_to_bytes(runtime, result);
3135 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3137 struct snd_pcm_file *pcm_file;
3138 struct snd_pcm_substream *substream;
3139 struct snd_pcm_runtime *runtime;
3140 snd_pcm_sframes_t result;
3143 snd_pcm_uframes_t frames;
3145 pcm_file = iocb->ki_filp->private_data;
3146 substream = pcm_file->substream;
3147 if (PCM_RUNTIME_CHECK(substream))
3149 runtime = substream->runtime;
3150 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3152 if (!iter_is_iovec(to))
3154 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3156 if (!frame_aligned(runtime, to->iov->iov_len))
3158 frames = bytes_to_samples(runtime, to->iov->iov_len);
3159 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3162 for (i = 0; i < to->nr_segs; ++i)
3163 bufs[i] = to->iov[i].iov_base;
3164 result = snd_pcm_lib_readv(substream, bufs, frames);
3166 result = frames_to_bytes(runtime, result);
3171 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3173 struct snd_pcm_file *pcm_file;
3174 struct snd_pcm_substream *substream;
3175 struct snd_pcm_runtime *runtime;
3176 snd_pcm_sframes_t result;
3179 snd_pcm_uframes_t frames;
3181 pcm_file = iocb->ki_filp->private_data;
3182 substream = pcm_file->substream;
3183 if (PCM_RUNTIME_CHECK(substream))
3185 runtime = substream->runtime;
3186 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3188 if (!iter_is_iovec(from))
3190 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3191 !frame_aligned(runtime, from->iov->iov_len))
3193 frames = bytes_to_samples(runtime, from->iov->iov_len);
3194 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3197 for (i = 0; i < from->nr_segs; ++i)
3198 bufs[i] = from->iov[i].iov_base;
3199 result = snd_pcm_lib_writev(substream, bufs, frames);
3201 result = frames_to_bytes(runtime, result);
3206 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3208 struct snd_pcm_file *pcm_file;
3209 struct snd_pcm_substream *substream;
3210 struct snd_pcm_runtime *runtime;
3212 snd_pcm_uframes_t avail;
3214 pcm_file = file->private_data;
3216 substream = pcm_file->substream;
3217 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3218 ok = EPOLLOUT | EPOLLWRNORM;
3220 ok = EPOLLIN | EPOLLRDNORM;
3221 if (PCM_RUNTIME_CHECK(substream))
3222 return ok | EPOLLERR;
3224 runtime = substream->runtime;
3225 poll_wait(file, &runtime->sleep, wait);
3228 snd_pcm_stream_lock_irq(substream);
3229 avail = snd_pcm_avail(substream);
3230 switch (runtime->status->state) {
3231 case SNDRV_PCM_STATE_RUNNING:
3232 case SNDRV_PCM_STATE_PREPARED:
3233 case SNDRV_PCM_STATE_PAUSED:
3234 if (avail >= runtime->control->avail_min)
3237 case SNDRV_PCM_STATE_DRAINING:
3238 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3245 mask = ok | EPOLLERR;
3248 snd_pcm_stream_unlock_irq(substream);
3257 * Only on coherent architectures, we can mmap the status and the control records
3258 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3260 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3262 * mmap status record
3264 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3266 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3267 struct snd_pcm_runtime *runtime;
3269 if (substream == NULL)
3270 return VM_FAULT_SIGBUS;
3271 runtime = substream->runtime;
3272 vmf->page = virt_to_page(runtime->status);
3273 get_page(vmf->page);
3277 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3279 .fault = snd_pcm_mmap_status_fault,
3282 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3283 struct vm_area_struct *area)
3286 if (!(area->vm_flags & VM_READ))
3288 size = area->vm_end - area->vm_start;
3289 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3291 area->vm_ops = &snd_pcm_vm_ops_status;
3292 area->vm_private_data = substream;
3293 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3298 * mmap control record
3300 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3302 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3303 struct snd_pcm_runtime *runtime;
3305 if (substream == NULL)
3306 return VM_FAULT_SIGBUS;
3307 runtime = substream->runtime;
3308 vmf->page = virt_to_page(runtime->control);
3309 get_page(vmf->page);
3313 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3315 .fault = snd_pcm_mmap_control_fault,
3318 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3319 struct vm_area_struct *area)
3322 if (!(area->vm_flags & VM_READ))
3324 size = area->vm_end - area->vm_start;
3325 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3327 area->vm_ops = &snd_pcm_vm_ops_control;
3328 area->vm_private_data = substream;
3329 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3333 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3335 if (pcm_file->no_compat_mmap)
3337 /* See pcm_control_mmap_allowed() below.
3338 * Since older alsa-lib requires both status and control mmaps to be
3339 * coupled, we have to disable the status mmap for old alsa-lib, too.
3341 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3342 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3347 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3349 if (pcm_file->no_compat_mmap)
3351 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3352 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3353 * thus it effectively assures the manual update of appl_ptr.
3355 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3360 #else /* ! coherent mmap */
3362 * don't support mmap for status and control records.
3364 #define pcm_status_mmap_allowed(pcm_file) false
3365 #define pcm_control_mmap_allowed(pcm_file) false
3367 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3368 struct vm_area_struct *area)
3372 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3373 struct vm_area_struct *area)
3377 #endif /* coherent mmap */
3379 static inline struct page *
3380 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3382 void *vaddr = substream->runtime->dma_area + ofs;
3384 switch (substream->dma_buffer.dev.type) {
3385 #ifdef CONFIG_SND_DMA_SGBUF
3386 case SNDRV_DMA_TYPE_DEV_SG:
3387 case SNDRV_DMA_TYPE_DEV_UC_SG:
3388 return snd_pcm_sgbuf_ops_page(substream, ofs);
3389 #endif /* CONFIG_SND_DMA_SGBUF */
3390 case SNDRV_DMA_TYPE_VMALLOC:
3391 return vmalloc_to_page(vaddr);
3393 return virt_to_page(vaddr);
3398 * fault callback for mmapping a RAM page
3400 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3402 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3403 struct snd_pcm_runtime *runtime;
3404 unsigned long offset;
3408 if (substream == NULL)
3409 return VM_FAULT_SIGBUS;
3410 runtime = substream->runtime;
3411 offset = vmf->pgoff << PAGE_SHIFT;
3412 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3413 if (offset > dma_bytes - PAGE_SIZE)
3414 return VM_FAULT_SIGBUS;
3415 if (substream->ops->page)
3416 page = substream->ops->page(substream, offset);
3418 page = snd_pcm_default_page_ops(substream, offset);
3420 return VM_FAULT_SIGBUS;
3426 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3427 .open = snd_pcm_mmap_data_open,
3428 .close = snd_pcm_mmap_data_close,
3431 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3432 .open = snd_pcm_mmap_data_open,
3433 .close = snd_pcm_mmap_data_close,
3434 .fault = snd_pcm_mmap_data_fault,
3438 * mmap the DMA buffer on RAM
3442 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3443 * @substream: PCM substream
3446 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3447 * this function is invoked implicitly.
3449 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3450 struct vm_area_struct *area)
3452 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3453 #ifdef CONFIG_GENERIC_ALLOCATOR
3454 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3455 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3456 return remap_pfn_range(area, area->vm_start,
3457 substream->dma_buffer.addr >> PAGE_SHIFT,
3458 area->vm_end - area->vm_start, area->vm_page_prot);
3460 #endif /* CONFIG_GENERIC_ALLOCATOR */
3461 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3462 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3463 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3464 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3465 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3467 substream->runtime->dma_area,
3468 substream->runtime->dma_addr,
3469 substream->runtime->dma_bytes);
3470 #endif /* CONFIG_X86 */
3471 /* mmap with fault handler */
3472 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3475 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3478 * mmap the DMA buffer on I/O memory area
3480 #if SNDRV_PCM_INFO_MMAP_IOMEM
3482 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3483 * @substream: PCM substream
3486 * When your hardware uses the iomapped pages as the hardware buffer and
3487 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3488 * is supposed to work only on limited architectures.
3490 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3491 struct vm_area_struct *area)
3493 struct snd_pcm_runtime *runtime = substream->runtime;
3495 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3496 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3498 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3499 #endif /* SNDRV_PCM_INFO_MMAP */
3504 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3505 struct vm_area_struct *area)
3507 struct snd_pcm_runtime *runtime;
3509 unsigned long offset;
3513 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3514 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3517 if (!(area->vm_flags & VM_READ))
3520 runtime = substream->runtime;
3521 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3523 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3525 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3526 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3528 size = area->vm_end - area->vm_start;
3529 offset = area->vm_pgoff << PAGE_SHIFT;
3530 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3531 if ((size_t)size > dma_bytes)
3533 if (offset > dma_bytes - size)
3536 area->vm_ops = &snd_pcm_vm_ops_data;
3537 area->vm_private_data = substream;
3538 if (substream->ops->mmap)
3539 err = substream->ops->mmap(substream, area);
3541 err = snd_pcm_lib_default_mmap(substream, area);
3543 atomic_inc(&substream->mmap_count);
3546 EXPORT_SYMBOL(snd_pcm_mmap_data);
3548 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3550 struct snd_pcm_file * pcm_file;
3551 struct snd_pcm_substream *substream;
3552 unsigned long offset;
3554 pcm_file = file->private_data;
3555 substream = pcm_file->substream;
3556 if (PCM_RUNTIME_CHECK(substream))
3559 offset = area->vm_pgoff << PAGE_SHIFT;
3561 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3562 if (!pcm_status_mmap_allowed(pcm_file))
3564 return snd_pcm_mmap_status(substream, file, area);
3565 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3566 if (!pcm_control_mmap_allowed(pcm_file))
3568 return snd_pcm_mmap_control(substream, file, area);
3570 return snd_pcm_mmap_data(substream, file, area);
3575 static int snd_pcm_fasync(int fd, struct file * file, int on)
3577 struct snd_pcm_file * pcm_file;
3578 struct snd_pcm_substream *substream;
3579 struct snd_pcm_runtime *runtime;
3581 pcm_file = file->private_data;
3582 substream = pcm_file->substream;
3583 if (PCM_RUNTIME_CHECK(substream))
3585 runtime = substream->runtime;
3586 return fasync_helper(fd, file, on, &runtime->fasync);
3592 #ifdef CONFIG_COMPAT
3593 #include "pcm_compat.c"
3595 #define snd_pcm_ioctl_compat NULL
3599 * To be removed helpers to keep binary compatibility
3602 #ifdef CONFIG_SND_SUPPORT_OLD_API
3603 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3604 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3606 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3607 struct snd_pcm_hw_params_old *oparams)
3611 memset(params, 0, sizeof(*params));
3612 params->flags = oparams->flags;
3613 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3614 params->masks[i].bits[0] = oparams->masks[i];
3615 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3616 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3617 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3618 params->info = oparams->info;
3619 params->msbits = oparams->msbits;
3620 params->rate_num = oparams->rate_num;
3621 params->rate_den = oparams->rate_den;
3622 params->fifo_size = oparams->fifo_size;
3625 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3626 struct snd_pcm_hw_params *params)
3630 memset(oparams, 0, sizeof(*oparams));
3631 oparams->flags = params->flags;
3632 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3633 oparams->masks[i] = params->masks[i].bits[0];
3634 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3635 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3636 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3637 oparams->info = params->info;
3638 oparams->msbits = params->msbits;
3639 oparams->rate_num = params->rate_num;
3640 oparams->rate_den = params->rate_den;
3641 oparams->fifo_size = params->fifo_size;
3644 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3645 struct snd_pcm_hw_params_old __user * _oparams)
3647 struct snd_pcm_hw_params *params;
3648 struct snd_pcm_hw_params_old *oparams = NULL;
3651 params = kmalloc(sizeof(*params), GFP_KERNEL);
3655 oparams = memdup_user(_oparams, sizeof(*oparams));
3656 if (IS_ERR(oparams)) {
3657 err = PTR_ERR(oparams);
3660 snd_pcm_hw_convert_from_old_params(params, oparams);
3661 err = snd_pcm_hw_refine(substream, params);
3665 err = fixup_unreferenced_params(substream, params);
3669 snd_pcm_hw_convert_to_old_params(oparams, params);
3670 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3679 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3680 struct snd_pcm_hw_params_old __user * _oparams)
3682 struct snd_pcm_hw_params *params;
3683 struct snd_pcm_hw_params_old *oparams = NULL;
3686 params = kmalloc(sizeof(*params), GFP_KERNEL);
3690 oparams = memdup_user(_oparams, sizeof(*oparams));
3691 if (IS_ERR(oparams)) {
3692 err = PTR_ERR(oparams);
3696 snd_pcm_hw_convert_from_old_params(params, oparams);
3697 err = snd_pcm_hw_params(substream, params);
3701 snd_pcm_hw_convert_to_old_params(oparams, params);
3702 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3710 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3713 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3716 unsigned long pgoff,
3717 unsigned long flags)
3719 struct snd_pcm_file *pcm_file = file->private_data;
3720 struct snd_pcm_substream *substream = pcm_file->substream;
3721 struct snd_pcm_runtime *runtime = substream->runtime;
3722 unsigned long offset = pgoff << PAGE_SHIFT;
3725 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3726 return (unsigned long)runtime->status;
3727 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3728 return (unsigned long)runtime->control;
3730 return (unsigned long)runtime->dma_area + offset;
3734 # define snd_pcm_get_unmapped_area NULL
3741 const struct file_operations snd_pcm_f_ops[2] = {
3743 .owner = THIS_MODULE,
3744 .write = snd_pcm_write,
3745 .write_iter = snd_pcm_writev,
3746 .open = snd_pcm_playback_open,
3747 .release = snd_pcm_release,
3748 .llseek = no_llseek,
3749 .poll = snd_pcm_poll,
3750 .unlocked_ioctl = snd_pcm_ioctl,
3751 .compat_ioctl = snd_pcm_ioctl_compat,
3752 .mmap = snd_pcm_mmap,
3753 .fasync = snd_pcm_fasync,
3754 .get_unmapped_area = snd_pcm_get_unmapped_area,
3757 .owner = THIS_MODULE,
3758 .read = snd_pcm_read,
3759 .read_iter = snd_pcm_readv,
3760 .open = snd_pcm_capture_open,
3761 .release = snd_pcm_release,
3762 .llseek = no_llseek,
3763 .poll = snd_pcm_poll,
3764 .unlocked_ioctl = snd_pcm_ioctl,
3765 .compat_ioctl = snd_pcm_ioctl_compat,
3766 .mmap = snd_pcm_mmap,
3767 .fasync = snd_pcm_fasync,
3768 .get_unmapped_area = snd_pcm_get_unmapped_area,