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);
581 * snd_pcm_hw_param_choose - choose a configuration defined by @params
583 * @params: the hw_params instance
585 * Choose one configuration from configuration space defined by @params.
586 * The configuration chosen is that obtained fixing in this order:
587 * first access, first format, first subformat, min channels,
588 * min rate, min period time, max buffer size, min tick time
590 * Return: Zero if successful, or a negative error code on failure.
592 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
593 struct snd_pcm_hw_params *params)
595 static const int vars[] = {
596 SNDRV_PCM_HW_PARAM_ACCESS,
597 SNDRV_PCM_HW_PARAM_FORMAT,
598 SNDRV_PCM_HW_PARAM_SUBFORMAT,
599 SNDRV_PCM_HW_PARAM_CHANNELS,
600 SNDRV_PCM_HW_PARAM_RATE,
601 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
602 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
603 SNDRV_PCM_HW_PARAM_TICK_TIME,
607 struct snd_mask old_mask;
608 struct snd_interval old_interval;
611 for (v = vars; *v != -1; v++) {
612 /* Keep old parameter to trace. */
613 if (trace_hw_mask_param_enabled()) {
615 old_mask = *hw_param_mask(params, *v);
617 if (trace_hw_interval_param_enabled()) {
618 if (hw_is_interval(*v))
619 old_interval = *hw_param_interval(params, *v);
621 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
622 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
624 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
630 /* Trace the changed parameter. */
631 if (hw_is_mask(*v)) {
632 trace_hw_mask_param(pcm, *v, 0, &old_mask,
633 hw_param_mask(params, *v));
635 if (hw_is_interval(*v)) {
636 trace_hw_interval_param(pcm, *v, 0, &old_interval,
637 hw_param_interval(params, *v));
644 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
645 struct snd_pcm_hw_params *params)
647 struct snd_pcm_runtime *runtime;
650 snd_pcm_uframes_t frames;
652 if (PCM_RUNTIME_CHECK(substream))
654 runtime = substream->runtime;
655 snd_pcm_stream_lock_irq(substream);
656 switch (runtime->status->state) {
657 case SNDRV_PCM_STATE_OPEN:
658 case SNDRV_PCM_STATE_SETUP:
659 case SNDRV_PCM_STATE_PREPARED:
662 snd_pcm_stream_unlock_irq(substream);
665 snd_pcm_stream_unlock_irq(substream);
666 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
667 if (!substream->oss.oss)
669 if (atomic_read(&substream->mmap_count))
672 snd_pcm_sync_stop(substream);
675 err = snd_pcm_hw_refine(substream, params);
679 err = snd_pcm_hw_params_choose(substream, params);
683 err = fixup_unreferenced_params(substream, params);
687 if (substream->managed_buffer_alloc) {
688 err = snd_pcm_lib_malloc_pages(substream,
689 params_buffer_bytes(params));
692 runtime->buffer_changed = err > 0;
695 if (substream->ops->hw_params != NULL) {
696 err = substream->ops->hw_params(substream, params);
701 runtime->access = params_access(params);
702 runtime->format = params_format(params);
703 runtime->subformat = params_subformat(params);
704 runtime->channels = params_channels(params);
705 runtime->rate = params_rate(params);
706 runtime->period_size = params_period_size(params);
707 runtime->periods = params_periods(params);
708 runtime->buffer_size = params_buffer_size(params);
709 runtime->info = params->info;
710 runtime->rate_num = params->rate_num;
711 runtime->rate_den = params->rate_den;
712 runtime->no_period_wakeup =
713 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
714 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
716 bits = snd_pcm_format_physical_width(runtime->format);
717 runtime->sample_bits = bits;
718 bits *= runtime->channels;
719 runtime->frame_bits = bits;
721 while (bits % 8 != 0) {
725 runtime->byte_align = bits / 8;
726 runtime->min_align = frames;
728 /* Default sw params */
729 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
730 runtime->period_step = 1;
731 runtime->control->avail_min = runtime->period_size;
732 runtime->start_threshold = 1;
733 runtime->stop_threshold = runtime->buffer_size;
734 runtime->silence_threshold = 0;
735 runtime->silence_size = 0;
736 runtime->boundary = runtime->buffer_size;
737 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
738 runtime->boundary *= 2;
740 snd_pcm_timer_resolution_change(substream);
741 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
743 if (pm_qos_request_active(&substream->latency_pm_qos_req))
744 pm_qos_remove_request(&substream->latency_pm_qos_req);
745 if ((usecs = period_to_usecs(runtime)) >= 0)
746 pm_qos_add_request(&substream->latency_pm_qos_req,
747 PM_QOS_CPU_DMA_LATENCY, usecs);
750 /* hardware might be unusable from this time,
751 so we force application to retry to set
752 the correct hardware parameter settings */
753 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
754 if (substream->ops->hw_free != NULL)
755 substream->ops->hw_free(substream);
756 if (substream->managed_buffer_alloc)
757 snd_pcm_lib_free_pages(substream);
761 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
762 struct snd_pcm_hw_params __user * _params)
764 struct snd_pcm_hw_params *params;
767 params = memdup_user(_params, sizeof(*params));
769 return PTR_ERR(params);
771 err = snd_pcm_hw_params(substream, params);
775 if (copy_to_user(_params, params, sizeof(*params)))
782 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
784 struct snd_pcm_runtime *runtime;
787 if (PCM_RUNTIME_CHECK(substream))
789 runtime = substream->runtime;
790 snd_pcm_stream_lock_irq(substream);
791 switch (runtime->status->state) {
792 case SNDRV_PCM_STATE_SETUP:
793 case SNDRV_PCM_STATE_PREPARED:
796 snd_pcm_stream_unlock_irq(substream);
799 snd_pcm_stream_unlock_irq(substream);
800 if (atomic_read(&substream->mmap_count))
802 snd_pcm_sync_stop(substream);
803 if (substream->ops->hw_free)
804 result = substream->ops->hw_free(substream);
805 if (substream->managed_buffer_alloc)
806 snd_pcm_lib_free_pages(substream);
807 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
808 pm_qos_remove_request(&substream->latency_pm_qos_req);
812 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
813 struct snd_pcm_sw_params *params)
815 struct snd_pcm_runtime *runtime;
818 if (PCM_RUNTIME_CHECK(substream))
820 runtime = substream->runtime;
821 snd_pcm_stream_lock_irq(substream);
822 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
823 snd_pcm_stream_unlock_irq(substream);
826 snd_pcm_stream_unlock_irq(substream);
828 if (params->tstamp_mode < 0 ||
829 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
831 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
832 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
834 if (params->avail_min == 0)
836 if (params->silence_size >= runtime->boundary) {
837 if (params->silence_threshold != 0)
840 if (params->silence_size > params->silence_threshold)
842 if (params->silence_threshold > runtime->buffer_size)
846 snd_pcm_stream_lock_irq(substream);
847 runtime->tstamp_mode = params->tstamp_mode;
848 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
849 runtime->tstamp_type = params->tstamp_type;
850 runtime->period_step = params->period_step;
851 runtime->control->avail_min = params->avail_min;
852 runtime->start_threshold = params->start_threshold;
853 runtime->stop_threshold = params->stop_threshold;
854 runtime->silence_threshold = params->silence_threshold;
855 runtime->silence_size = params->silence_size;
856 params->boundary = runtime->boundary;
857 if (snd_pcm_running(substream)) {
858 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
859 runtime->silence_size > 0)
860 snd_pcm_playback_silence(substream, ULONG_MAX);
861 err = snd_pcm_update_state(substream, runtime);
863 snd_pcm_stream_unlock_irq(substream);
867 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
868 struct snd_pcm_sw_params __user * _params)
870 struct snd_pcm_sw_params params;
872 if (copy_from_user(¶ms, _params, sizeof(params)))
874 err = snd_pcm_sw_params(substream, ¶ms);
875 if (copy_to_user(_params, ¶ms, sizeof(params)))
880 static inline snd_pcm_uframes_t
881 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
883 snd_pcm_uframes_t delay;
885 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
886 delay = snd_pcm_playback_hw_avail(substream->runtime);
888 delay = snd_pcm_capture_avail(substream->runtime);
889 return delay + substream->runtime->delay;
892 int snd_pcm_status(struct snd_pcm_substream *substream,
893 struct snd_pcm_status *status)
895 struct snd_pcm_runtime *runtime = substream->runtime;
897 snd_pcm_stream_lock_irq(substream);
899 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
900 &runtime->audio_tstamp_config);
902 /* backwards compatible behavior */
903 if (runtime->audio_tstamp_config.type_requested ==
904 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
905 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
906 runtime->audio_tstamp_config.type_requested =
907 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
909 runtime->audio_tstamp_config.type_requested =
910 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
911 runtime->audio_tstamp_report.valid = 0;
913 runtime->audio_tstamp_report.valid = 1;
915 status->state = runtime->status->state;
916 status->suspended_state = runtime->status->suspended_state;
917 if (status->state == SNDRV_PCM_STATE_OPEN)
919 status->trigger_tstamp = runtime->trigger_tstamp;
920 if (snd_pcm_running(substream)) {
921 snd_pcm_update_hw_ptr(substream);
922 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
923 status->tstamp = runtime->status->tstamp;
924 status->driver_tstamp = runtime->driver_tstamp;
925 status->audio_tstamp =
926 runtime->status->audio_tstamp;
927 if (runtime->audio_tstamp_report.valid == 1)
928 /* backwards compatibility, no report provided in COMPAT mode */
929 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
930 &status->audio_tstamp_accuracy,
931 &runtime->audio_tstamp_report);
936 /* get tstamp only in fallback mode and only if enabled */
937 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
938 snd_pcm_gettime(runtime, &status->tstamp);
941 status->appl_ptr = runtime->control->appl_ptr;
942 status->hw_ptr = runtime->status->hw_ptr;
943 status->avail = snd_pcm_avail(substream);
944 status->delay = snd_pcm_running(substream) ?
945 snd_pcm_calc_delay(substream) : 0;
946 status->avail_max = runtime->avail_max;
947 status->overrange = runtime->overrange;
948 runtime->avail_max = 0;
949 runtime->overrange = 0;
951 snd_pcm_stream_unlock_irq(substream);
955 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
956 struct snd_pcm_status __user * _status,
959 struct snd_pcm_status status;
962 memset(&status, 0, sizeof(status));
964 * with extension, parameters are read/write,
965 * get audio_tstamp_data from user,
966 * ignore rest of status structure
968 if (ext && get_user(status.audio_tstamp_data,
969 (u32 __user *)(&_status->audio_tstamp_data)))
971 res = snd_pcm_status(substream, &status);
974 if (copy_to_user(_status, &status, sizeof(status)))
979 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
980 struct snd_pcm_channel_info * info)
982 struct snd_pcm_runtime *runtime;
983 unsigned int channel;
985 channel = info->channel;
986 runtime = substream->runtime;
987 snd_pcm_stream_lock_irq(substream);
988 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
989 snd_pcm_stream_unlock_irq(substream);
992 snd_pcm_stream_unlock_irq(substream);
993 if (channel >= runtime->channels)
995 memset(info, 0, sizeof(*info));
996 info->channel = channel;
997 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1000 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1001 struct snd_pcm_channel_info __user * _info)
1003 struct snd_pcm_channel_info info;
1006 if (copy_from_user(&info, _info, sizeof(info)))
1008 res = snd_pcm_channel_info(substream, &info);
1011 if (copy_to_user(_info, &info, sizeof(info)))
1016 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1018 struct snd_pcm_runtime *runtime = substream->runtime;
1019 if (runtime->trigger_master == NULL)
1021 if (runtime->trigger_master == substream) {
1022 if (!runtime->trigger_tstamp_latched)
1023 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1025 snd_pcm_trigger_tstamp(runtime->trigger_master);
1026 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1028 runtime->trigger_master = NULL;
1032 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1033 int (*do_action)(struct snd_pcm_substream *substream, int state);
1034 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1035 void (*post_action)(struct snd_pcm_substream *substream, int state);
1039 * this functions is core for handling of linked stream
1040 * Note: the stream state might be changed also on failure
1041 * Note2: call with calling stream lock + link lock
1043 static int snd_pcm_action_group(const struct action_ops *ops,
1044 struct snd_pcm_substream *substream,
1045 int state, int do_lock)
1047 struct snd_pcm_substream *s = NULL;
1048 struct snd_pcm_substream *s1;
1049 int res = 0, depth = 1;
1051 snd_pcm_group_for_each_entry(s, substream) {
1052 if (do_lock && s != substream) {
1053 if (s->pcm->nonatomic)
1054 mutex_lock_nested(&s->self_group.mutex, depth);
1056 spin_lock_nested(&s->self_group.lock, depth);
1059 res = ops->pre_action(s, state);
1063 snd_pcm_group_for_each_entry(s, substream) {
1064 res = ops->do_action(s, state);
1066 if (ops->undo_action) {
1067 snd_pcm_group_for_each_entry(s1, substream) {
1068 if (s1 == s) /* failed stream */
1070 ops->undo_action(s1, state);
1073 s = NULL; /* unlock all */
1077 snd_pcm_group_for_each_entry(s, substream) {
1078 ops->post_action(s, state);
1082 /* unlock streams */
1083 snd_pcm_group_for_each_entry(s1, substream) {
1084 if (s1 != substream) {
1085 if (s1->pcm->nonatomic)
1086 mutex_unlock(&s1->self_group.mutex);
1088 spin_unlock(&s1->self_group.lock);
1090 if (s1 == s) /* end */
1098 * Note: call with stream lock
1100 static int snd_pcm_action_single(const struct action_ops *ops,
1101 struct snd_pcm_substream *substream,
1106 res = ops->pre_action(substream, state);
1109 res = ops->do_action(substream, state);
1111 ops->post_action(substream, state);
1112 else if (ops->undo_action)
1113 ops->undo_action(substream, state);
1117 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1118 struct snd_pcm_group *new_group)
1120 substream->group = new_group;
1121 list_move(&substream->link_list, &new_group->substreams);
1125 * Unref and unlock the group, but keep the stream lock;
1126 * when the group becomes empty and no longer referred, destroy itself
1128 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1129 struct snd_pcm_substream *substream)
1135 do_free = refcount_dec_and_test(&group->refs);
1136 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1142 * Lock the group inside a stream lock and reference it;
1143 * return the locked group object, or NULL if not linked
1145 static struct snd_pcm_group *
1146 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1148 bool nonatomic = substream->pcm->nonatomic;
1149 struct snd_pcm_group *group;
1153 if (!snd_pcm_stream_linked(substream))
1155 group = substream->group;
1156 /* block freeing the group object */
1157 refcount_inc(&group->refs);
1159 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1160 spin_trylock(&group->lock);
1164 /* re-lock for avoiding ABBA deadlock */
1165 snd_pcm_stream_unlock(substream);
1166 snd_pcm_group_lock(group, nonatomic);
1167 snd_pcm_stream_lock(substream);
1169 /* check the group again; the above opens a small race window */
1170 if (substream->group == group)
1172 /* group changed, try again */
1173 snd_pcm_group_unref(group, substream);
1179 * Note: call with stream lock
1181 static int snd_pcm_action(const struct action_ops *ops,
1182 struct snd_pcm_substream *substream,
1185 struct snd_pcm_group *group;
1188 group = snd_pcm_stream_group_ref(substream);
1190 res = snd_pcm_action_group(ops, substream, state, 1);
1192 res = snd_pcm_action_single(ops, substream, state);
1193 snd_pcm_group_unref(group, substream);
1198 * Note: don't use any locks before
1200 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1201 struct snd_pcm_substream *substream,
1206 snd_pcm_stream_lock_irq(substream);
1207 res = snd_pcm_action(ops, substream, state);
1208 snd_pcm_stream_unlock_irq(substream);
1214 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1215 struct snd_pcm_substream *substream,
1220 /* Guarantee the group members won't change during non-atomic action */
1221 down_read(&snd_pcm_link_rwsem);
1222 if (snd_pcm_stream_linked(substream))
1223 res = snd_pcm_action_group(ops, substream, state, 0);
1225 res = snd_pcm_action_single(ops, substream, state);
1226 up_read(&snd_pcm_link_rwsem);
1233 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1235 struct snd_pcm_runtime *runtime = substream->runtime;
1236 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1238 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1239 !snd_pcm_playback_data(substream))
1241 runtime->trigger_tstamp_latched = false;
1242 runtime->trigger_master = substream;
1246 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1248 if (substream->runtime->trigger_master != substream)
1250 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1253 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1255 if (substream->runtime->trigger_master == substream)
1256 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1259 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1261 struct snd_pcm_runtime *runtime = substream->runtime;
1262 snd_pcm_trigger_tstamp(substream);
1263 runtime->hw_ptr_jiffies = jiffies;
1264 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1266 runtime->status->state = state;
1267 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1268 runtime->silence_size > 0)
1269 snd_pcm_playback_silence(substream, ULONG_MAX);
1270 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1273 static const struct action_ops snd_pcm_action_start = {
1274 .pre_action = snd_pcm_pre_start,
1275 .do_action = snd_pcm_do_start,
1276 .undo_action = snd_pcm_undo_start,
1277 .post_action = snd_pcm_post_start
1281 * snd_pcm_start - start all linked streams
1282 * @substream: the PCM substream instance
1284 * Return: Zero if successful, or a negative error code.
1285 * The stream lock must be acquired before calling this function.
1287 int snd_pcm_start(struct snd_pcm_substream *substream)
1289 return snd_pcm_action(&snd_pcm_action_start, substream,
1290 SNDRV_PCM_STATE_RUNNING);
1293 /* take the stream lock and start the streams */
1294 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1296 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1297 SNDRV_PCM_STATE_RUNNING);
1303 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1305 struct snd_pcm_runtime *runtime = substream->runtime;
1306 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1308 runtime->trigger_master = substream;
1312 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1314 if (substream->runtime->trigger_master == substream &&
1315 snd_pcm_running(substream))
1316 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1317 return 0; /* unconditonally stop all substreams */
1320 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1322 struct snd_pcm_runtime *runtime = substream->runtime;
1323 if (runtime->status->state != state) {
1324 snd_pcm_trigger_tstamp(substream);
1325 runtime->status->state = state;
1326 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1328 runtime->stop_operating = true;
1329 wake_up(&runtime->sleep);
1330 wake_up(&runtime->tsleep);
1333 static const struct action_ops snd_pcm_action_stop = {
1334 .pre_action = snd_pcm_pre_stop,
1335 .do_action = snd_pcm_do_stop,
1336 .post_action = snd_pcm_post_stop
1340 * snd_pcm_stop - try to stop all running streams in the substream group
1341 * @substream: the PCM substream instance
1342 * @state: PCM state after stopping the stream
1344 * The state of each stream is then changed to the given state unconditionally.
1346 * Return: Zero if successful, or a negative error code.
1348 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1350 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1352 EXPORT_SYMBOL(snd_pcm_stop);
1355 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1356 * @substream: the PCM substream
1358 * After stopping, the state is changed to SETUP.
1359 * Unlike snd_pcm_stop(), this affects only the given stream.
1361 * Return: Zero if succesful, or a negative error code.
1363 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1365 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1366 SNDRV_PCM_STATE_SETUP);
1370 * snd_pcm_stop_xrun - stop the running streams as XRUN
1371 * @substream: the PCM substream instance
1373 * This stops the given running substream (and all linked substreams) as XRUN.
1374 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1376 * Return: Zero if successful, or a negative error code.
1378 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1380 unsigned long flags;
1382 snd_pcm_stream_lock_irqsave(substream, flags);
1383 if (substream->runtime && snd_pcm_running(substream))
1384 __snd_pcm_xrun(substream);
1385 snd_pcm_stream_unlock_irqrestore(substream, flags);
1388 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1393 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1395 struct snd_pcm_runtime *runtime = substream->runtime;
1396 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1399 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1401 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1403 runtime->trigger_master = substream;
1407 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1409 if (substream->runtime->trigger_master != substream)
1411 /* some drivers might use hw_ptr to recover from the pause -
1412 update the hw_ptr now */
1414 snd_pcm_update_hw_ptr(substream);
1415 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1416 * a delta between the current jiffies, this gives a large enough
1417 * delta, effectively to skip the check once.
1419 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1420 return substream->ops->trigger(substream,
1421 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1422 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1425 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1427 if (substream->runtime->trigger_master == substream)
1428 substream->ops->trigger(substream,
1429 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1430 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1433 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1435 struct snd_pcm_runtime *runtime = substream->runtime;
1436 snd_pcm_trigger_tstamp(substream);
1438 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1439 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1440 wake_up(&runtime->sleep);
1441 wake_up(&runtime->tsleep);
1443 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1444 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1448 static const struct action_ops snd_pcm_action_pause = {
1449 .pre_action = snd_pcm_pre_pause,
1450 .do_action = snd_pcm_do_pause,
1451 .undo_action = snd_pcm_undo_pause,
1452 .post_action = snd_pcm_post_pause
1456 * Push/release the pause for all linked streams.
1458 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1460 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1466 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1468 struct snd_pcm_runtime *runtime = substream->runtime;
1469 switch (runtime->status->state) {
1470 case SNDRV_PCM_STATE_SUSPENDED:
1472 /* unresumable PCM state; return -EBUSY for skipping suspend */
1473 case SNDRV_PCM_STATE_OPEN:
1474 case SNDRV_PCM_STATE_SETUP:
1475 case SNDRV_PCM_STATE_DISCONNECTED:
1478 runtime->trigger_master = substream;
1482 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1484 struct snd_pcm_runtime *runtime = substream->runtime;
1485 if (runtime->trigger_master != substream)
1487 if (! snd_pcm_running(substream))
1489 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1490 return 0; /* suspend unconditionally */
1493 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1495 struct snd_pcm_runtime *runtime = substream->runtime;
1496 snd_pcm_trigger_tstamp(substream);
1497 runtime->status->suspended_state = runtime->status->state;
1498 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1499 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1500 wake_up(&runtime->sleep);
1501 wake_up(&runtime->tsleep);
1504 static const struct action_ops snd_pcm_action_suspend = {
1505 .pre_action = snd_pcm_pre_suspend,
1506 .do_action = snd_pcm_do_suspend,
1507 .post_action = snd_pcm_post_suspend
1511 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1512 * @substream: the PCM substream
1514 * After this call, all streams are changed to SUSPENDED state.
1516 * Return: Zero if successful, or a negative error code.
1518 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1521 unsigned long flags;
1523 snd_pcm_stream_lock_irqsave(substream, flags);
1524 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1525 snd_pcm_stream_unlock_irqrestore(substream, flags);
1530 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1531 * @pcm: the PCM instance
1533 * After this call, all streams are changed to SUSPENDED state.
1535 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1537 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1539 struct snd_pcm_substream *substream;
1540 int stream, err = 0;
1545 for (stream = 0; stream < 2; stream++) {
1546 for (substream = pcm->streams[stream].substream;
1547 substream; substream = substream->next) {
1548 /* FIXME: the open/close code should lock this as well */
1549 if (substream->runtime == NULL)
1553 * Skip BE dai link PCM's that are internal and may
1554 * not have their substream ops set.
1556 if (!substream->ops)
1559 err = snd_pcm_suspend(substream);
1560 if (err < 0 && err != -EBUSY)
1566 EXPORT_SYMBOL(snd_pcm_suspend_all);
1570 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1572 struct snd_pcm_runtime *runtime = substream->runtime;
1573 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1575 runtime->trigger_master = substream;
1579 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1581 struct snd_pcm_runtime *runtime = substream->runtime;
1582 if (runtime->trigger_master != substream)
1584 /* DMA not running previously? */
1585 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1586 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1587 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1589 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1592 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1594 if (substream->runtime->trigger_master == substream &&
1595 snd_pcm_running(substream))
1596 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1599 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1601 struct snd_pcm_runtime *runtime = substream->runtime;
1602 snd_pcm_trigger_tstamp(substream);
1603 runtime->status->state = runtime->status->suspended_state;
1604 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1605 snd_pcm_sync_stop(substream);
1608 static const struct action_ops snd_pcm_action_resume = {
1609 .pre_action = snd_pcm_pre_resume,
1610 .do_action = snd_pcm_do_resume,
1611 .undo_action = snd_pcm_undo_resume,
1612 .post_action = snd_pcm_post_resume
1615 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1617 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1622 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1627 #endif /* CONFIG_PM */
1632 * Change the RUNNING stream(s) to XRUN state.
1634 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1636 struct snd_pcm_runtime *runtime = substream->runtime;
1639 snd_pcm_stream_lock_irq(substream);
1640 switch (runtime->status->state) {
1641 case SNDRV_PCM_STATE_XRUN:
1642 result = 0; /* already there */
1644 case SNDRV_PCM_STATE_RUNNING:
1645 __snd_pcm_xrun(substream);
1651 snd_pcm_stream_unlock_irq(substream);
1658 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1660 struct snd_pcm_runtime *runtime = substream->runtime;
1661 switch (runtime->status->state) {
1662 case SNDRV_PCM_STATE_RUNNING:
1663 case SNDRV_PCM_STATE_PREPARED:
1664 case SNDRV_PCM_STATE_PAUSED:
1665 case SNDRV_PCM_STATE_SUSPENDED:
1672 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1674 struct snd_pcm_runtime *runtime = substream->runtime;
1675 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1678 runtime->hw_ptr_base = 0;
1679 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1680 runtime->status->hw_ptr % runtime->period_size;
1681 runtime->silence_start = runtime->status->hw_ptr;
1682 runtime->silence_filled = 0;
1686 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1688 struct snd_pcm_runtime *runtime = substream->runtime;
1689 runtime->control->appl_ptr = runtime->status->hw_ptr;
1690 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1691 runtime->silence_size > 0)
1692 snd_pcm_playback_silence(substream, ULONG_MAX);
1695 static const struct action_ops snd_pcm_action_reset = {
1696 .pre_action = snd_pcm_pre_reset,
1697 .do_action = snd_pcm_do_reset,
1698 .post_action = snd_pcm_post_reset
1701 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1703 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1709 /* we use the second argument for updating f_flags */
1710 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1713 struct snd_pcm_runtime *runtime = substream->runtime;
1714 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1715 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1717 if (snd_pcm_running(substream))
1719 substream->f_flags = f_flags;
1723 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1726 snd_pcm_sync_stop(substream);
1727 err = substream->ops->prepare(substream);
1730 return snd_pcm_do_reset(substream, 0);
1733 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1735 struct snd_pcm_runtime *runtime = substream->runtime;
1736 runtime->control->appl_ptr = runtime->status->hw_ptr;
1737 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1740 static const struct action_ops snd_pcm_action_prepare = {
1741 .pre_action = snd_pcm_pre_prepare,
1742 .do_action = snd_pcm_do_prepare,
1743 .post_action = snd_pcm_post_prepare
1747 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1748 * @substream: the PCM substream instance
1749 * @file: file to refer f_flags
1751 * Return: Zero if successful, or a negative error code.
1753 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1759 f_flags = file->f_flags;
1761 f_flags = substream->f_flags;
1763 snd_pcm_stream_lock_irq(substream);
1764 switch (substream->runtime->status->state) {
1765 case SNDRV_PCM_STATE_PAUSED:
1766 snd_pcm_pause(substream, 0);
1768 case SNDRV_PCM_STATE_SUSPENDED:
1769 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1772 snd_pcm_stream_unlock_irq(substream);
1774 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1775 substream, f_flags);
1782 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1784 struct snd_pcm_runtime *runtime = substream->runtime;
1785 switch (runtime->status->state) {
1786 case SNDRV_PCM_STATE_OPEN:
1787 case SNDRV_PCM_STATE_DISCONNECTED:
1788 case SNDRV_PCM_STATE_SUSPENDED:
1791 runtime->trigger_master = substream;
1795 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1797 struct snd_pcm_runtime *runtime = substream->runtime;
1798 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1799 switch (runtime->status->state) {
1800 case SNDRV_PCM_STATE_PREPARED:
1801 /* start playback stream if possible */
1802 if (! snd_pcm_playback_empty(substream)) {
1803 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1804 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1806 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1809 case SNDRV_PCM_STATE_RUNNING:
1810 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1812 case SNDRV_PCM_STATE_XRUN:
1813 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1819 /* stop running stream */
1820 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1821 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1822 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1823 snd_pcm_do_stop(substream, new_state);
1824 snd_pcm_post_stop(substream, new_state);
1828 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1829 runtime->trigger_master == substream &&
1830 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1831 return substream->ops->trigger(substream,
1832 SNDRV_PCM_TRIGGER_DRAIN);
1837 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1841 static const struct action_ops snd_pcm_action_drain_init = {
1842 .pre_action = snd_pcm_pre_drain_init,
1843 .do_action = snd_pcm_do_drain_init,
1844 .post_action = snd_pcm_post_drain_init
1848 * Drain the stream(s).
1849 * When the substream is linked, sync until the draining of all playback streams
1851 * After this call, all streams are supposed to be either SETUP or DRAINING
1852 * (capture only) state.
1854 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1857 struct snd_card *card;
1858 struct snd_pcm_runtime *runtime;
1859 struct snd_pcm_substream *s;
1860 struct snd_pcm_group *group;
1861 wait_queue_entry_t wait;
1865 card = substream->pcm->card;
1866 runtime = substream->runtime;
1868 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1872 if (file->f_flags & O_NONBLOCK)
1874 } else if (substream->f_flags & O_NONBLOCK)
1877 snd_pcm_stream_lock_irq(substream);
1879 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1880 snd_pcm_pause(substream, 0);
1882 /* pre-start/stop - all running streams are changed to DRAINING state */
1883 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1886 /* in non-blocking, we don't wait in ioctl but let caller poll */
1894 struct snd_pcm_runtime *to_check;
1895 if (signal_pending(current)) {
1896 result = -ERESTARTSYS;
1899 /* find a substream to drain */
1901 group = snd_pcm_stream_group_ref(substream);
1902 snd_pcm_group_for_each_entry(s, substream) {
1903 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1905 runtime = s->runtime;
1906 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1911 snd_pcm_group_unref(group, substream);
1913 break; /* all drained */
1914 init_waitqueue_entry(&wait, current);
1915 set_current_state(TASK_INTERRUPTIBLE);
1916 add_wait_queue(&to_check->sleep, &wait);
1917 snd_pcm_stream_unlock_irq(substream);
1918 if (runtime->no_period_wakeup)
1919 tout = MAX_SCHEDULE_TIMEOUT;
1922 if (runtime->rate) {
1923 long t = runtime->period_size * 2 / runtime->rate;
1924 tout = max(t, tout);
1926 tout = msecs_to_jiffies(tout * 1000);
1928 tout = schedule_timeout(tout);
1930 snd_pcm_stream_lock_irq(substream);
1931 group = snd_pcm_stream_group_ref(substream);
1932 snd_pcm_group_for_each_entry(s, substream) {
1933 if (s->runtime == to_check) {
1934 remove_wait_queue(&to_check->sleep, &wait);
1938 snd_pcm_group_unref(group, substream);
1940 if (card->shutdown) {
1945 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1948 dev_dbg(substream->pcm->card->dev,
1949 "playback drain error (DMA or IRQ trouble?)\n");
1950 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1958 snd_pcm_stream_unlock_irq(substream);
1966 * Immediately put all linked substreams into SETUP state.
1968 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1970 struct snd_pcm_runtime *runtime;
1973 if (PCM_RUNTIME_CHECK(substream))
1975 runtime = substream->runtime;
1977 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1978 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1981 snd_pcm_stream_lock_irq(substream);
1983 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1984 snd_pcm_pause(substream, 0);
1986 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1987 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1988 snd_pcm_stream_unlock_irq(substream);
1994 static bool is_pcm_file(struct file *file)
1996 struct inode *inode = file_inode(file);
1997 struct snd_pcm *pcm;
2000 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2002 minor = iminor(inode);
2003 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2005 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2008 snd_card_unref(pcm->card);
2015 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2018 struct snd_pcm_file *pcm_file;
2019 struct snd_pcm_substream *substream1;
2020 struct snd_pcm_group *group, *target_group;
2021 bool nonatomic = substream->pcm->nonatomic;
2022 struct fd f = fdget(fd);
2026 if (!is_pcm_file(f.file)) {
2030 pcm_file = f.file->private_data;
2031 substream1 = pcm_file->substream;
2032 group = kzalloc(sizeof(*group), GFP_KERNEL);
2037 snd_pcm_group_init(group);
2039 down_write(&snd_pcm_link_rwsem);
2040 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2041 substream->runtime->status->state != substream1->runtime->status->state ||
2042 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2046 if (snd_pcm_stream_linked(substream1)) {
2051 snd_pcm_stream_lock_irq(substream);
2052 if (!snd_pcm_stream_linked(substream)) {
2053 snd_pcm_group_assign(substream, group);
2054 group = NULL; /* assigned, don't free this one below */
2056 target_group = substream->group;
2057 snd_pcm_stream_unlock_irq(substream);
2059 snd_pcm_group_lock_irq(target_group, nonatomic);
2060 snd_pcm_stream_lock(substream1);
2061 snd_pcm_group_assign(substream1, target_group);
2062 refcount_inc(&target_group->refs);
2063 snd_pcm_stream_unlock(substream1);
2064 snd_pcm_group_unlock_irq(target_group, nonatomic);
2066 up_write(&snd_pcm_link_rwsem);
2074 static void relink_to_local(struct snd_pcm_substream *substream)
2076 snd_pcm_stream_lock(substream);
2077 snd_pcm_group_assign(substream, &substream->self_group);
2078 snd_pcm_stream_unlock(substream);
2081 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2083 struct snd_pcm_group *group;
2084 bool nonatomic = substream->pcm->nonatomic;
2085 bool do_free = false;
2088 down_write(&snd_pcm_link_rwsem);
2090 if (!snd_pcm_stream_linked(substream)) {
2095 group = substream->group;
2096 snd_pcm_group_lock_irq(group, nonatomic);
2098 relink_to_local(substream);
2099 refcount_dec(&group->refs);
2101 /* detach the last stream, too */
2102 if (list_is_singular(&group->substreams)) {
2103 relink_to_local(list_first_entry(&group->substreams,
2104 struct snd_pcm_substream,
2106 do_free = refcount_dec_and_test(&group->refs);
2109 snd_pcm_group_unlock_irq(group, nonatomic);
2114 up_write(&snd_pcm_link_rwsem);
2121 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2122 struct snd_pcm_hw_rule *rule)
2124 struct snd_interval t;
2125 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2126 hw_param_interval_c(params, rule->deps[1]), &t);
2127 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2130 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2131 struct snd_pcm_hw_rule *rule)
2133 struct snd_interval t;
2134 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2135 hw_param_interval_c(params, rule->deps[1]), &t);
2136 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2139 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2140 struct snd_pcm_hw_rule *rule)
2142 struct snd_interval t;
2143 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2144 hw_param_interval_c(params, rule->deps[1]),
2145 (unsigned long) rule->private, &t);
2146 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2149 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2150 struct snd_pcm_hw_rule *rule)
2152 struct snd_interval t;
2153 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2154 (unsigned long) rule->private,
2155 hw_param_interval_c(params, rule->deps[1]), &t);
2156 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2159 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2160 struct snd_pcm_hw_rule *rule)
2163 const struct snd_interval *i =
2164 hw_param_interval_c(params, rule->deps[0]);
2166 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2168 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2170 if (! snd_mask_test(mask, k))
2172 bits = snd_pcm_format_physical_width(k);
2174 continue; /* ignore invalid formats */
2175 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2176 snd_mask_reset(&m, k);
2178 return snd_mask_refine(mask, &m);
2181 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2182 struct snd_pcm_hw_rule *rule)
2184 struct snd_interval t;
2190 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2192 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2194 bits = snd_pcm_format_physical_width(k);
2196 continue; /* ignore invalid formats */
2197 if (t.min > (unsigned)bits)
2199 if (t.max < (unsigned)bits)
2203 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2206 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2207 #error "Change this table"
2210 static const unsigned int rates[] = {
2211 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2212 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2215 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2216 .count = ARRAY_SIZE(rates),
2220 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2221 struct snd_pcm_hw_rule *rule)
2223 struct snd_pcm_hardware *hw = rule->private;
2224 return snd_interval_list(hw_param_interval(params, rule->var),
2225 snd_pcm_known_rates.count,
2226 snd_pcm_known_rates.list, hw->rates);
2229 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2230 struct snd_pcm_hw_rule *rule)
2232 struct snd_interval t;
2233 struct snd_pcm_substream *substream = rule->private;
2235 t.max = substream->buffer_bytes_max;
2239 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2242 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2244 struct snd_pcm_runtime *runtime = substream->runtime;
2245 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2248 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2249 snd_mask_any(constrs_mask(constrs, k));
2252 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2253 snd_interval_any(constrs_interval(constrs, k));
2256 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2257 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2258 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2259 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2260 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2262 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2263 snd_pcm_hw_rule_format, NULL,
2264 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2267 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2268 snd_pcm_hw_rule_sample_bits, NULL,
2269 SNDRV_PCM_HW_PARAM_FORMAT,
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_div, NULL,
2275 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2278 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2279 snd_pcm_hw_rule_mul, NULL,
2280 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2283 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2284 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2285 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2288 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2289 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2290 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2293 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2294 snd_pcm_hw_rule_div, NULL,
2295 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2298 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2299 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2300 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2303 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2304 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2305 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2308 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2309 snd_pcm_hw_rule_div, NULL,
2310 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2313 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2314 snd_pcm_hw_rule_div, NULL,
2315 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2318 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2319 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2320 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2323 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2324 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2325 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2328 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2329 snd_pcm_hw_rule_mul, NULL,
2330 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2333 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2334 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2335 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2338 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2339 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2340 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2343 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2344 snd_pcm_hw_rule_muldivk, (void*) 8,
2345 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2348 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2349 snd_pcm_hw_rule_muldivk, (void*) 8,
2350 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2353 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2354 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2355 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2358 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2359 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2360 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2366 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2368 struct snd_pcm_runtime *runtime = substream->runtime;
2369 struct snd_pcm_hardware *hw = &runtime->hw;
2371 unsigned int mask = 0;
2373 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2374 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2375 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2376 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2377 if (hw_support_mmap(substream)) {
2378 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2379 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2380 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2381 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2382 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2383 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2385 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2389 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2393 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2397 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2398 hw->channels_min, hw->channels_max);
2402 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2403 hw->rate_min, hw->rate_max);
2407 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2408 hw->period_bytes_min, hw->period_bytes_max);
2412 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2413 hw->periods_min, hw->periods_max);
2417 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2418 hw->period_bytes_min, hw->buffer_bytes_max);
2422 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2423 snd_pcm_hw_rule_buffer_bytes_max, substream,
2424 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2429 if (runtime->dma_bytes) {
2430 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2435 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2436 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2437 snd_pcm_hw_rule_rate, hw,
2438 SNDRV_PCM_HW_PARAM_RATE, -1);
2443 /* FIXME: this belong to lowlevel */
2444 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2449 static void pcm_release_private(struct snd_pcm_substream *substream)
2451 if (snd_pcm_stream_linked(substream))
2452 snd_pcm_unlink(substream);
2455 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2457 substream->ref_count--;
2458 if (substream->ref_count > 0)
2461 snd_pcm_drop(substream);
2462 if (substream->hw_opened) {
2463 if (substream->ops->hw_free &&
2464 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2465 substream->ops->hw_free(substream);
2466 substream->ops->close(substream);
2467 substream->hw_opened = 0;
2469 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2470 pm_qos_remove_request(&substream->latency_pm_qos_req);
2471 if (substream->pcm_release) {
2472 substream->pcm_release(substream);
2473 substream->pcm_release = NULL;
2475 snd_pcm_detach_substream(substream);
2477 EXPORT_SYMBOL(snd_pcm_release_substream);
2479 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2481 struct snd_pcm_substream **rsubstream)
2483 struct snd_pcm_substream *substream;
2486 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2489 if (substream->ref_count > 1) {
2490 *rsubstream = substream;
2494 err = snd_pcm_hw_constraints_init(substream);
2496 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2500 if ((err = substream->ops->open(substream)) < 0)
2503 substream->hw_opened = 1;
2505 err = snd_pcm_hw_constraints_complete(substream);
2507 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2511 *rsubstream = substream;
2515 snd_pcm_release_substream(substream);
2518 EXPORT_SYMBOL(snd_pcm_open_substream);
2520 static int snd_pcm_open_file(struct file *file,
2521 struct snd_pcm *pcm,
2524 struct snd_pcm_file *pcm_file;
2525 struct snd_pcm_substream *substream;
2528 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2532 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2533 if (pcm_file == NULL) {
2534 snd_pcm_release_substream(substream);
2537 pcm_file->substream = substream;
2538 if (substream->ref_count == 1)
2539 substream->pcm_release = pcm_release_private;
2540 file->private_data = pcm_file;
2545 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2547 struct snd_pcm *pcm;
2548 int err = nonseekable_open(inode, file);
2551 pcm = snd_lookup_minor_data(iminor(inode),
2552 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2553 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2555 snd_card_unref(pcm->card);
2559 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2561 struct snd_pcm *pcm;
2562 int err = nonseekable_open(inode, file);
2565 pcm = snd_lookup_minor_data(iminor(inode),
2566 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2567 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2569 snd_card_unref(pcm->card);
2573 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2576 wait_queue_entry_t wait;
2582 err = snd_card_file_add(pcm->card, file);
2585 if (!try_module_get(pcm->card->module)) {
2589 init_waitqueue_entry(&wait, current);
2590 add_wait_queue(&pcm->open_wait, &wait);
2591 mutex_lock(&pcm->open_mutex);
2593 err = snd_pcm_open_file(file, pcm, stream);
2596 if (err == -EAGAIN) {
2597 if (file->f_flags & O_NONBLOCK) {
2603 set_current_state(TASK_INTERRUPTIBLE);
2604 mutex_unlock(&pcm->open_mutex);
2606 mutex_lock(&pcm->open_mutex);
2607 if (pcm->card->shutdown) {
2611 if (signal_pending(current)) {
2616 remove_wait_queue(&pcm->open_wait, &wait);
2617 mutex_unlock(&pcm->open_mutex);
2623 module_put(pcm->card->module);
2625 snd_card_file_remove(pcm->card, file);
2630 static int snd_pcm_release(struct inode *inode, struct file *file)
2632 struct snd_pcm *pcm;
2633 struct snd_pcm_substream *substream;
2634 struct snd_pcm_file *pcm_file;
2636 pcm_file = file->private_data;
2637 substream = pcm_file->substream;
2638 if (snd_BUG_ON(!substream))
2640 pcm = substream->pcm;
2641 mutex_lock(&pcm->open_mutex);
2642 snd_pcm_release_substream(substream);
2644 mutex_unlock(&pcm->open_mutex);
2645 wake_up(&pcm->open_wait);
2646 module_put(pcm->card->module);
2647 snd_card_file_remove(pcm->card, file);
2651 /* check and update PCM state; return 0 or a negative error
2652 * call this inside PCM lock
2654 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2656 switch (substream->runtime->status->state) {
2657 case SNDRV_PCM_STATE_DRAINING:
2658 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2661 case SNDRV_PCM_STATE_RUNNING:
2662 return snd_pcm_update_hw_ptr(substream);
2663 case SNDRV_PCM_STATE_PREPARED:
2664 case SNDRV_PCM_STATE_PAUSED:
2666 case SNDRV_PCM_STATE_SUSPENDED:
2668 case SNDRV_PCM_STATE_XRUN:
2675 /* increase the appl_ptr; returns the processed frames or a negative error */
2676 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2677 snd_pcm_uframes_t frames,
2678 snd_pcm_sframes_t avail)
2680 struct snd_pcm_runtime *runtime = substream->runtime;
2681 snd_pcm_sframes_t appl_ptr;
2686 if (frames > (snd_pcm_uframes_t)avail)
2688 appl_ptr = runtime->control->appl_ptr + frames;
2689 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2690 appl_ptr -= runtime->boundary;
2691 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2692 return ret < 0 ? ret : frames;
2695 /* decrease the appl_ptr; returns the processed frames or zero for error */
2696 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2697 snd_pcm_uframes_t frames,
2698 snd_pcm_sframes_t avail)
2700 struct snd_pcm_runtime *runtime = substream->runtime;
2701 snd_pcm_sframes_t appl_ptr;
2706 if (frames > (snd_pcm_uframes_t)avail)
2708 appl_ptr = runtime->control->appl_ptr - frames;
2710 appl_ptr += runtime->boundary;
2711 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2712 /* NOTE: we return zero for errors because PulseAudio gets depressed
2713 * upon receiving an error from rewind ioctl and stops processing
2714 * any longer. Returning zero means that no rewind is done, so
2715 * it's not absolutely wrong to answer like that.
2717 return ret < 0 ? 0 : frames;
2720 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2721 snd_pcm_uframes_t frames)
2723 snd_pcm_sframes_t ret;
2728 snd_pcm_stream_lock_irq(substream);
2729 ret = do_pcm_hwsync(substream);
2731 ret = rewind_appl_ptr(substream, frames,
2732 snd_pcm_hw_avail(substream));
2733 snd_pcm_stream_unlock_irq(substream);
2737 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2738 snd_pcm_uframes_t frames)
2740 snd_pcm_sframes_t ret;
2745 snd_pcm_stream_lock_irq(substream);
2746 ret = do_pcm_hwsync(substream);
2748 ret = forward_appl_ptr(substream, frames,
2749 snd_pcm_avail(substream));
2750 snd_pcm_stream_unlock_irq(substream);
2754 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2758 snd_pcm_stream_lock_irq(substream);
2759 err = do_pcm_hwsync(substream);
2760 snd_pcm_stream_unlock_irq(substream);
2764 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2765 snd_pcm_sframes_t *delay)
2768 snd_pcm_sframes_t n = 0;
2770 snd_pcm_stream_lock_irq(substream);
2771 err = do_pcm_hwsync(substream);
2773 n = snd_pcm_calc_delay(substream);
2774 snd_pcm_stream_unlock_irq(substream);
2780 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2781 struct snd_pcm_sync_ptr __user *_sync_ptr)
2783 struct snd_pcm_runtime *runtime = substream->runtime;
2784 struct snd_pcm_sync_ptr sync_ptr;
2785 volatile struct snd_pcm_mmap_status *status;
2786 volatile struct snd_pcm_mmap_control *control;
2789 memset(&sync_ptr, 0, sizeof(sync_ptr));
2790 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2792 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2794 status = runtime->status;
2795 control = runtime->control;
2796 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2797 err = snd_pcm_hwsync(substream);
2801 snd_pcm_stream_lock_irq(substream);
2802 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2803 err = pcm_lib_apply_appl_ptr(substream,
2804 sync_ptr.c.control.appl_ptr);
2806 snd_pcm_stream_unlock_irq(substream);
2810 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2812 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2813 control->avail_min = sync_ptr.c.control.avail_min;
2815 sync_ptr.c.control.avail_min = control->avail_min;
2816 sync_ptr.s.status.state = status->state;
2817 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2818 sync_ptr.s.status.tstamp = status->tstamp;
2819 sync_ptr.s.status.suspended_state = status->suspended_state;
2820 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2821 snd_pcm_stream_unlock_irq(substream);
2822 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2827 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2829 struct snd_pcm_runtime *runtime = substream->runtime;
2832 if (get_user(arg, _arg))
2834 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2836 runtime->tstamp_type = arg;
2840 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2841 struct snd_xferi __user *_xferi)
2843 struct snd_xferi xferi;
2844 struct snd_pcm_runtime *runtime = substream->runtime;
2845 snd_pcm_sframes_t result;
2847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2849 if (put_user(0, &_xferi->result))
2851 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2853 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2854 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2856 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2857 __put_user(result, &_xferi->result);
2858 return result < 0 ? result : 0;
2861 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2862 struct snd_xfern __user *_xfern)
2864 struct snd_xfern xfern;
2865 struct snd_pcm_runtime *runtime = substream->runtime;
2867 snd_pcm_sframes_t result;
2869 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2871 if (runtime->channels > 128)
2873 if (put_user(0, &_xfern->result))
2875 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2878 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2880 return PTR_ERR(bufs);
2881 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2882 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2884 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2886 __put_user(result, &_xfern->result);
2887 return result < 0 ? result : 0;
2890 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2891 snd_pcm_uframes_t __user *_frames)
2893 snd_pcm_uframes_t frames;
2894 snd_pcm_sframes_t result;
2896 if (get_user(frames, _frames))
2898 if (put_user(0, _frames))
2900 result = snd_pcm_rewind(substream, frames);
2901 __put_user(result, _frames);
2902 return result < 0 ? result : 0;
2905 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2906 snd_pcm_uframes_t __user *_frames)
2908 snd_pcm_uframes_t frames;
2909 snd_pcm_sframes_t result;
2911 if (get_user(frames, _frames))
2913 if (put_user(0, _frames))
2915 result = snd_pcm_forward(substream, frames);
2916 __put_user(result, _frames);
2917 return result < 0 ? result : 0;
2920 static int snd_pcm_common_ioctl(struct file *file,
2921 struct snd_pcm_substream *substream,
2922 unsigned int cmd, void __user *arg)
2924 struct snd_pcm_file *pcm_file = file->private_data;
2927 if (PCM_RUNTIME_CHECK(substream))
2930 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2935 case SNDRV_PCM_IOCTL_PVERSION:
2936 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2937 case SNDRV_PCM_IOCTL_INFO:
2938 return snd_pcm_info_user(substream, arg);
2939 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2941 case SNDRV_PCM_IOCTL_TTSTAMP:
2942 return snd_pcm_tstamp(substream, arg);
2943 case SNDRV_PCM_IOCTL_USER_PVERSION:
2944 if (get_user(pcm_file->user_pversion,
2945 (unsigned int __user *)arg))
2948 case SNDRV_PCM_IOCTL_HW_REFINE:
2949 return snd_pcm_hw_refine_user(substream, arg);
2950 case SNDRV_PCM_IOCTL_HW_PARAMS:
2951 return snd_pcm_hw_params_user(substream, arg);
2952 case SNDRV_PCM_IOCTL_HW_FREE:
2953 return snd_pcm_hw_free(substream);
2954 case SNDRV_PCM_IOCTL_SW_PARAMS:
2955 return snd_pcm_sw_params_user(substream, arg);
2956 case SNDRV_PCM_IOCTL_STATUS:
2957 return snd_pcm_status_user(substream, arg, false);
2958 case SNDRV_PCM_IOCTL_STATUS_EXT:
2959 return snd_pcm_status_user(substream, arg, true);
2960 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2961 return snd_pcm_channel_info_user(substream, arg);
2962 case SNDRV_PCM_IOCTL_PREPARE:
2963 return snd_pcm_prepare(substream, file);
2964 case SNDRV_PCM_IOCTL_RESET:
2965 return snd_pcm_reset(substream);
2966 case SNDRV_PCM_IOCTL_START:
2967 return snd_pcm_start_lock_irq(substream);
2968 case SNDRV_PCM_IOCTL_LINK:
2969 return snd_pcm_link(substream, (int)(unsigned long) arg);
2970 case SNDRV_PCM_IOCTL_UNLINK:
2971 return snd_pcm_unlink(substream);
2972 case SNDRV_PCM_IOCTL_RESUME:
2973 return snd_pcm_resume(substream);
2974 case SNDRV_PCM_IOCTL_XRUN:
2975 return snd_pcm_xrun(substream);
2976 case SNDRV_PCM_IOCTL_HWSYNC:
2977 return snd_pcm_hwsync(substream);
2978 case SNDRV_PCM_IOCTL_DELAY:
2980 snd_pcm_sframes_t delay;
2981 snd_pcm_sframes_t __user *res = arg;
2984 err = snd_pcm_delay(substream, &delay);
2987 if (put_user(delay, res))
2991 case SNDRV_PCM_IOCTL_SYNC_PTR:
2992 return snd_pcm_sync_ptr(substream, arg);
2993 #ifdef CONFIG_SND_SUPPORT_OLD_API
2994 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2995 return snd_pcm_hw_refine_old_user(substream, arg);
2996 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2997 return snd_pcm_hw_params_old_user(substream, arg);
2999 case SNDRV_PCM_IOCTL_DRAIN:
3000 return snd_pcm_drain(substream, file);
3001 case SNDRV_PCM_IOCTL_DROP:
3002 return snd_pcm_drop(substream);
3003 case SNDRV_PCM_IOCTL_PAUSE:
3004 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3006 (int)(unsigned long)arg);
3007 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3008 case SNDRV_PCM_IOCTL_READI_FRAMES:
3009 return snd_pcm_xferi_frames_ioctl(substream, arg);
3010 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3011 case SNDRV_PCM_IOCTL_READN_FRAMES:
3012 return snd_pcm_xfern_frames_ioctl(substream, arg);
3013 case SNDRV_PCM_IOCTL_REWIND:
3014 return snd_pcm_rewind_ioctl(substream, arg);
3015 case SNDRV_PCM_IOCTL_FORWARD:
3016 return snd_pcm_forward_ioctl(substream, arg);
3018 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3022 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3025 struct snd_pcm_file *pcm_file;
3027 pcm_file = file->private_data;
3029 if (((cmd >> 8) & 0xff) != 'A')
3032 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3033 (void __user *)arg);
3037 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3038 * @substream: PCM substream
3040 * @arg: IOCTL argument
3042 * The function is provided primarily for OSS layer and USB gadget drivers,
3043 * and it allows only the limited set of ioctls (hw_params, sw_params,
3044 * prepare, start, drain, drop, forward).
3046 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3047 unsigned int cmd, void *arg)
3049 snd_pcm_uframes_t *frames = arg;
3050 snd_pcm_sframes_t result;
3053 case SNDRV_PCM_IOCTL_FORWARD:
3055 /* provided only for OSS; capture-only and no value returned */
3056 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3058 result = snd_pcm_forward(substream, *frames);
3059 return result < 0 ? result : 0;
3061 case SNDRV_PCM_IOCTL_HW_PARAMS:
3062 return snd_pcm_hw_params(substream, arg);
3063 case SNDRV_PCM_IOCTL_SW_PARAMS:
3064 return snd_pcm_sw_params(substream, arg);
3065 case SNDRV_PCM_IOCTL_PREPARE:
3066 return snd_pcm_prepare(substream, NULL);
3067 case SNDRV_PCM_IOCTL_START:
3068 return snd_pcm_start_lock_irq(substream);
3069 case SNDRV_PCM_IOCTL_DRAIN:
3070 return snd_pcm_drain(substream, NULL);
3071 case SNDRV_PCM_IOCTL_DROP:
3072 return snd_pcm_drop(substream);
3073 case SNDRV_PCM_IOCTL_DELAY:
3074 return snd_pcm_delay(substream, frames);
3079 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3081 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3084 struct snd_pcm_file *pcm_file;
3085 struct snd_pcm_substream *substream;
3086 struct snd_pcm_runtime *runtime;
3087 snd_pcm_sframes_t result;
3089 pcm_file = file->private_data;
3090 substream = pcm_file->substream;
3091 if (PCM_RUNTIME_CHECK(substream))
3093 runtime = substream->runtime;
3094 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3096 if (!frame_aligned(runtime, count))
3098 count = bytes_to_frames(runtime, count);
3099 result = snd_pcm_lib_read(substream, buf, count);
3101 result = frames_to_bytes(runtime, result);
3105 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3106 size_t count, loff_t * offset)
3108 struct snd_pcm_file *pcm_file;
3109 struct snd_pcm_substream *substream;
3110 struct snd_pcm_runtime *runtime;
3111 snd_pcm_sframes_t result;
3113 pcm_file = file->private_data;
3114 substream = pcm_file->substream;
3115 if (PCM_RUNTIME_CHECK(substream))
3117 runtime = substream->runtime;
3118 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3120 if (!frame_aligned(runtime, count))
3122 count = bytes_to_frames(runtime, count);
3123 result = snd_pcm_lib_write(substream, buf, count);
3125 result = frames_to_bytes(runtime, result);
3129 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3131 struct snd_pcm_file *pcm_file;
3132 struct snd_pcm_substream *substream;
3133 struct snd_pcm_runtime *runtime;
3134 snd_pcm_sframes_t result;
3137 snd_pcm_uframes_t frames;
3139 pcm_file = iocb->ki_filp->private_data;
3140 substream = pcm_file->substream;
3141 if (PCM_RUNTIME_CHECK(substream))
3143 runtime = substream->runtime;
3144 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3146 if (!iter_is_iovec(to))
3148 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3150 if (!frame_aligned(runtime, to->iov->iov_len))
3152 frames = bytes_to_samples(runtime, to->iov->iov_len);
3153 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3156 for (i = 0; i < to->nr_segs; ++i)
3157 bufs[i] = to->iov[i].iov_base;
3158 result = snd_pcm_lib_readv(substream, bufs, frames);
3160 result = frames_to_bytes(runtime, result);
3165 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3167 struct snd_pcm_file *pcm_file;
3168 struct snd_pcm_substream *substream;
3169 struct snd_pcm_runtime *runtime;
3170 snd_pcm_sframes_t result;
3173 snd_pcm_uframes_t frames;
3175 pcm_file = iocb->ki_filp->private_data;
3176 substream = pcm_file->substream;
3177 if (PCM_RUNTIME_CHECK(substream))
3179 runtime = substream->runtime;
3180 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3182 if (!iter_is_iovec(from))
3184 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3185 !frame_aligned(runtime, from->iov->iov_len))
3187 frames = bytes_to_samples(runtime, from->iov->iov_len);
3188 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3191 for (i = 0; i < from->nr_segs; ++i)
3192 bufs[i] = from->iov[i].iov_base;
3193 result = snd_pcm_lib_writev(substream, bufs, frames);
3195 result = frames_to_bytes(runtime, result);
3200 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3202 struct snd_pcm_file *pcm_file;
3203 struct snd_pcm_substream *substream;
3204 struct snd_pcm_runtime *runtime;
3206 snd_pcm_uframes_t avail;
3208 pcm_file = file->private_data;
3210 substream = pcm_file->substream;
3211 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3212 ok = EPOLLOUT | EPOLLWRNORM;
3214 ok = EPOLLIN | EPOLLRDNORM;
3215 if (PCM_RUNTIME_CHECK(substream))
3216 return ok | EPOLLERR;
3218 runtime = substream->runtime;
3219 poll_wait(file, &runtime->sleep, wait);
3222 snd_pcm_stream_lock_irq(substream);
3223 avail = snd_pcm_avail(substream);
3224 switch (runtime->status->state) {
3225 case SNDRV_PCM_STATE_RUNNING:
3226 case SNDRV_PCM_STATE_PREPARED:
3227 case SNDRV_PCM_STATE_PAUSED:
3228 if (avail >= runtime->control->avail_min)
3231 case SNDRV_PCM_STATE_DRAINING:
3232 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3239 mask = ok | EPOLLERR;
3242 snd_pcm_stream_unlock_irq(substream);
3251 * Only on coherent architectures, we can mmap the status and the control records
3252 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3254 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3256 * mmap status record
3258 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3260 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3261 struct snd_pcm_runtime *runtime;
3263 if (substream == NULL)
3264 return VM_FAULT_SIGBUS;
3265 runtime = substream->runtime;
3266 vmf->page = virt_to_page(runtime->status);
3267 get_page(vmf->page);
3271 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3273 .fault = snd_pcm_mmap_status_fault,
3276 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3277 struct vm_area_struct *area)
3280 if (!(area->vm_flags & VM_READ))
3282 size = area->vm_end - area->vm_start;
3283 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3285 area->vm_ops = &snd_pcm_vm_ops_status;
3286 area->vm_private_data = substream;
3287 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3292 * mmap control record
3294 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3296 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3297 struct snd_pcm_runtime *runtime;
3299 if (substream == NULL)
3300 return VM_FAULT_SIGBUS;
3301 runtime = substream->runtime;
3302 vmf->page = virt_to_page(runtime->control);
3303 get_page(vmf->page);
3307 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3309 .fault = snd_pcm_mmap_control_fault,
3312 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3313 struct vm_area_struct *area)
3316 if (!(area->vm_flags & VM_READ))
3318 size = area->vm_end - area->vm_start;
3319 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3321 area->vm_ops = &snd_pcm_vm_ops_control;
3322 area->vm_private_data = substream;
3323 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3327 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3329 if (pcm_file->no_compat_mmap)
3331 /* See pcm_control_mmap_allowed() below.
3332 * Since older alsa-lib requires both status and control mmaps to be
3333 * coupled, we have to disable the status mmap for old alsa-lib, too.
3335 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3336 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3341 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3343 if (pcm_file->no_compat_mmap)
3345 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3346 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3347 * thus it effectively assures the manual update of appl_ptr.
3349 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3354 #else /* ! coherent mmap */
3356 * don't support mmap for status and control records.
3358 #define pcm_status_mmap_allowed(pcm_file) false
3359 #define pcm_control_mmap_allowed(pcm_file) false
3361 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3362 struct vm_area_struct *area)
3366 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3367 struct vm_area_struct *area)
3371 #endif /* coherent mmap */
3373 static inline struct page *
3374 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3376 void *vaddr = substream->runtime->dma_area + ofs;
3378 switch (substream->dma_buffer.dev.type) {
3379 #ifdef CONFIG_SND_DMA_SGBUF
3380 case SNDRV_DMA_TYPE_DEV_SG:
3381 case SNDRV_DMA_TYPE_DEV_UC_SG:
3382 return snd_pcm_sgbuf_ops_page(substream, ofs);
3383 #endif /* CONFIG_SND_DMA_SGBUF */
3384 case SNDRV_DMA_TYPE_VMALLOC:
3385 return vmalloc_to_page(vaddr);
3387 return virt_to_page(vaddr);
3392 * fault callback for mmapping a RAM page
3394 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3396 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3397 struct snd_pcm_runtime *runtime;
3398 unsigned long offset;
3402 if (substream == NULL)
3403 return VM_FAULT_SIGBUS;
3404 runtime = substream->runtime;
3405 offset = vmf->pgoff << PAGE_SHIFT;
3406 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3407 if (offset > dma_bytes - PAGE_SIZE)
3408 return VM_FAULT_SIGBUS;
3409 if (substream->ops->page)
3410 page = substream->ops->page(substream, offset);
3412 page = snd_pcm_default_page_ops(substream, offset);
3414 return VM_FAULT_SIGBUS;
3420 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3421 .open = snd_pcm_mmap_data_open,
3422 .close = snd_pcm_mmap_data_close,
3425 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3426 .open = snd_pcm_mmap_data_open,
3427 .close = snd_pcm_mmap_data_close,
3428 .fault = snd_pcm_mmap_data_fault,
3432 * mmap the DMA buffer on RAM
3436 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3437 * @substream: PCM substream
3440 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3441 * this function is invoked implicitly.
3443 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3444 struct vm_area_struct *area)
3446 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3447 #ifdef CONFIG_GENERIC_ALLOCATOR
3448 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3449 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3450 return remap_pfn_range(area, area->vm_start,
3451 substream->dma_buffer.addr >> PAGE_SHIFT,
3452 area->vm_end - area->vm_start, area->vm_page_prot);
3454 #endif /* CONFIG_GENERIC_ALLOCATOR */
3455 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3456 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3457 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3458 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3459 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3461 substream->runtime->dma_area,
3462 substream->runtime->dma_addr,
3463 substream->runtime->dma_bytes);
3464 #endif /* CONFIG_X86 */
3465 /* mmap with fault handler */
3466 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3469 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3472 * mmap the DMA buffer on I/O memory area
3474 #if SNDRV_PCM_INFO_MMAP_IOMEM
3476 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3477 * @substream: PCM substream
3480 * When your hardware uses the iomapped pages as the hardware buffer and
3481 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3482 * is supposed to work only on limited architectures.
3484 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3485 struct vm_area_struct *area)
3487 struct snd_pcm_runtime *runtime = substream->runtime;
3489 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3490 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3492 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3493 #endif /* SNDRV_PCM_INFO_MMAP */
3498 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3499 struct vm_area_struct *area)
3501 struct snd_pcm_runtime *runtime;
3503 unsigned long offset;
3507 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3508 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3511 if (!(area->vm_flags & VM_READ))
3514 runtime = substream->runtime;
3515 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3517 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3519 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3520 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3522 size = area->vm_end - area->vm_start;
3523 offset = area->vm_pgoff << PAGE_SHIFT;
3524 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3525 if ((size_t)size > dma_bytes)
3527 if (offset > dma_bytes - size)
3530 area->vm_ops = &snd_pcm_vm_ops_data;
3531 area->vm_private_data = substream;
3532 if (substream->ops->mmap)
3533 err = substream->ops->mmap(substream, area);
3535 err = snd_pcm_lib_default_mmap(substream, area);
3537 atomic_inc(&substream->mmap_count);
3540 EXPORT_SYMBOL(snd_pcm_mmap_data);
3542 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3544 struct snd_pcm_file * pcm_file;
3545 struct snd_pcm_substream *substream;
3546 unsigned long offset;
3548 pcm_file = file->private_data;
3549 substream = pcm_file->substream;
3550 if (PCM_RUNTIME_CHECK(substream))
3553 offset = area->vm_pgoff << PAGE_SHIFT;
3555 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3556 if (!pcm_status_mmap_allowed(pcm_file))
3558 return snd_pcm_mmap_status(substream, file, area);
3559 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3560 if (!pcm_control_mmap_allowed(pcm_file))
3562 return snd_pcm_mmap_control(substream, file, area);
3564 return snd_pcm_mmap_data(substream, file, area);
3569 static int snd_pcm_fasync(int fd, struct file * file, int on)
3571 struct snd_pcm_file * pcm_file;
3572 struct snd_pcm_substream *substream;
3573 struct snd_pcm_runtime *runtime;
3575 pcm_file = file->private_data;
3576 substream = pcm_file->substream;
3577 if (PCM_RUNTIME_CHECK(substream))
3579 runtime = substream->runtime;
3580 return fasync_helper(fd, file, on, &runtime->fasync);
3586 #ifdef CONFIG_COMPAT
3587 #include "pcm_compat.c"
3589 #define snd_pcm_ioctl_compat NULL
3593 * To be removed helpers to keep binary compatibility
3596 #ifdef CONFIG_SND_SUPPORT_OLD_API
3597 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3598 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3600 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3601 struct snd_pcm_hw_params_old *oparams)
3605 memset(params, 0, sizeof(*params));
3606 params->flags = oparams->flags;
3607 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3608 params->masks[i].bits[0] = oparams->masks[i];
3609 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3610 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3611 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3612 params->info = oparams->info;
3613 params->msbits = oparams->msbits;
3614 params->rate_num = oparams->rate_num;
3615 params->rate_den = oparams->rate_den;
3616 params->fifo_size = oparams->fifo_size;
3619 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3620 struct snd_pcm_hw_params *params)
3624 memset(oparams, 0, sizeof(*oparams));
3625 oparams->flags = params->flags;
3626 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3627 oparams->masks[i] = params->masks[i].bits[0];
3628 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3629 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3630 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3631 oparams->info = params->info;
3632 oparams->msbits = params->msbits;
3633 oparams->rate_num = params->rate_num;
3634 oparams->rate_den = params->rate_den;
3635 oparams->fifo_size = params->fifo_size;
3638 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3639 struct snd_pcm_hw_params_old __user * _oparams)
3641 struct snd_pcm_hw_params *params;
3642 struct snd_pcm_hw_params_old *oparams = NULL;
3645 params = kmalloc(sizeof(*params), GFP_KERNEL);
3649 oparams = memdup_user(_oparams, sizeof(*oparams));
3650 if (IS_ERR(oparams)) {
3651 err = PTR_ERR(oparams);
3654 snd_pcm_hw_convert_from_old_params(params, oparams);
3655 err = snd_pcm_hw_refine(substream, params);
3659 err = fixup_unreferenced_params(substream, params);
3663 snd_pcm_hw_convert_to_old_params(oparams, params);
3664 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3673 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3674 struct snd_pcm_hw_params_old __user * _oparams)
3676 struct snd_pcm_hw_params *params;
3677 struct snd_pcm_hw_params_old *oparams = NULL;
3680 params = kmalloc(sizeof(*params), GFP_KERNEL);
3684 oparams = memdup_user(_oparams, sizeof(*oparams));
3685 if (IS_ERR(oparams)) {
3686 err = PTR_ERR(oparams);
3690 snd_pcm_hw_convert_from_old_params(params, oparams);
3691 err = snd_pcm_hw_params(substream, params);
3695 snd_pcm_hw_convert_to_old_params(oparams, params);
3696 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3704 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3707 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3710 unsigned long pgoff,
3711 unsigned long flags)
3713 struct snd_pcm_file *pcm_file = file->private_data;
3714 struct snd_pcm_substream *substream = pcm_file->substream;
3715 struct snd_pcm_runtime *runtime = substream->runtime;
3716 unsigned long offset = pgoff << PAGE_SHIFT;
3719 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3720 return (unsigned long)runtime->status;
3721 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3722 return (unsigned long)runtime->control;
3724 return (unsigned long)runtime->dma_area + offset;
3728 # define snd_pcm_get_unmapped_area NULL
3735 const struct file_operations snd_pcm_f_ops[2] = {
3737 .owner = THIS_MODULE,
3738 .write = snd_pcm_write,
3739 .write_iter = snd_pcm_writev,
3740 .open = snd_pcm_playback_open,
3741 .release = snd_pcm_release,
3742 .llseek = no_llseek,
3743 .poll = snd_pcm_poll,
3744 .unlocked_ioctl = snd_pcm_ioctl,
3745 .compat_ioctl = snd_pcm_ioctl_compat,
3746 .mmap = snd_pcm_mmap,
3747 .fasync = snd_pcm_fasync,
3748 .get_unmapped_area = snd_pcm_get_unmapped_area,
3751 .owner = THIS_MODULE,
3752 .read = snd_pcm_read,
3753 .read_iter = snd_pcm_readv,
3754 .open = snd_pcm_capture_open,
3755 .release = snd_pcm_release,
3756 .llseek = no_llseek,
3757 .poll = snd_pcm_poll,
3758 .unlocked_ioctl = snd_pcm_ioctl,
3759 .compat_ioctl = snd_pcm_ioctl_compat,
3760 .mmap = snd_pcm_mmap,
3761 .fasync = snd_pcm_fasync,
3762 .get_unmapped_area = snd_pcm_get_unmapped_area,