2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.h>
39 #include <linux/delay.h>
41 #include "pcm_local.h"
43 #ifdef CONFIG_SND_DEBUG
44 #define CREATE_TRACE_POINTS
45 #include "pcm_param_trace.h"
47 #define trace_hw_mask_param_enabled() 0
48 #define trace_hw_interval_param_enabled() 0
49 #define trace_hw_mask_param(substream, type, index, prev, curr)
50 #define trace_hw_interval_param(substream, type, index, prev, curr)
57 struct snd_pcm_hw_params_old {
59 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
60 SNDRV_PCM_HW_PARAM_ACCESS + 1];
61 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
62 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
67 unsigned int rate_num;
68 unsigned int rate_den;
69 snd_pcm_uframes_t fifo_size;
70 unsigned char reserved[64];
73 #ifdef CONFIG_SND_SUPPORT_OLD_API
74 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
75 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
77 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
78 struct snd_pcm_hw_params_old __user * _oparams);
79 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params_old __user * _oparams);
82 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
88 static DECLARE_RWSEM(snd_pcm_link_rwsem);
90 void snd_pcm_group_init(struct snd_pcm_group *group)
92 spin_lock_init(&group->lock);
93 mutex_init(&group->mutex);
94 INIT_LIST_HEAD(&group->substreams);
95 refcount_set(&group->refs, 0);
98 /* define group lock helpers */
99 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
100 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
103 mutex_ ## mutex_action(&group->mutex); \
105 spin_ ## action(&group->lock); \
108 DEFINE_PCM_GROUP_LOCK(lock, lock);
109 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
110 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
111 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
113 #define PCM_LOCK_DEFAULT 0
114 #define PCM_LOCK_IRQ 1
115 #define PCM_LOCK_IRQSAVE 2
117 static unsigned long __snd_pcm_stream_lock_mode(struct snd_pcm_substream *substream,
120 unsigned long flags = 0;
121 if (substream->pcm->nonatomic) {
122 mutex_lock(&substream->self_group.mutex);
125 case PCM_LOCK_DEFAULT:
126 spin_lock(&substream->self_group.lock);
129 spin_lock_irq(&substream->self_group.lock);
131 case PCM_LOCK_IRQSAVE:
132 spin_lock_irqsave(&substream->self_group.lock, flags);
139 static void __snd_pcm_stream_unlock_mode(struct snd_pcm_substream *substream,
140 unsigned int mode, unsigned long flags)
142 if (substream->pcm->nonatomic) {
143 mutex_unlock(&substream->self_group.mutex);
146 case PCM_LOCK_DEFAULT:
147 spin_unlock(&substream->self_group.lock);
150 spin_unlock_irq(&substream->self_group.lock);
152 case PCM_LOCK_IRQSAVE:
153 spin_unlock_irqrestore(&substream->self_group.lock, flags);
160 * snd_pcm_stream_lock - Lock the PCM stream
161 * @substream: PCM substream
163 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
164 * flag of the given substream. This also takes the global link rw lock
165 * (or rw sem), too, for avoiding the race with linked streams.
167 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
169 __snd_pcm_stream_lock_mode(substream, PCM_LOCK_DEFAULT);
171 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
174 * snd_pcm_stream_lock - Unlock the PCM stream
175 * @substream: PCM substream
177 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
179 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
181 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_DEFAULT, 0);
183 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
186 * snd_pcm_stream_lock_irq - Lock the PCM stream
187 * @substream: PCM substream
189 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
190 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
191 * as snd_pcm_stream_lock().
193 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
195 __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQ);
197 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
200 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
201 * @substream: PCM substream
203 * This is a counter-part of snd_pcm_stream_lock_irq().
205 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
207 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQ, 0);
209 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
211 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
213 return __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQSAVE);
215 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
218 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
219 * @substream: PCM substream
222 * This is a counter-part of snd_pcm_stream_lock_irqsave().
224 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
227 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQSAVE, flags);
229 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
231 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
233 struct snd_pcm *pcm = substream->pcm;
234 struct snd_pcm_str *pstr = substream->pstr;
236 memset(info, 0, sizeof(*info));
237 info->card = pcm->card->number;
238 info->device = pcm->device;
239 info->stream = substream->stream;
240 info->subdevice = substream->number;
241 strlcpy(info->id, pcm->id, sizeof(info->id));
242 strlcpy(info->name, pcm->name, sizeof(info->name));
243 info->dev_class = pcm->dev_class;
244 info->dev_subclass = pcm->dev_subclass;
245 info->subdevices_count = pstr->substream_count;
246 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
247 strlcpy(info->subname, substream->name, sizeof(info->subname));
252 int snd_pcm_info_user(struct snd_pcm_substream *substream,
253 struct snd_pcm_info __user * _info)
255 struct snd_pcm_info *info;
258 info = kmalloc(sizeof(*info), GFP_KERNEL);
261 err = snd_pcm_info(substream, info);
263 if (copy_to_user(_info, info, sizeof(*info)))
270 static bool hw_support_mmap(struct snd_pcm_substream *substream)
272 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
274 /* architecture supports dma_mmap_coherent()? */
275 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
276 if (!substream->ops->mmap &&
277 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
283 static int constrain_mask_params(struct snd_pcm_substream *substream,
284 struct snd_pcm_hw_params *params)
286 struct snd_pcm_hw_constraints *constrs =
287 &substream->runtime->hw_constraints;
290 struct snd_mask old_mask;
293 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
294 m = hw_param_mask(params, k);
295 if (snd_mask_empty(m))
298 /* This parameter is not requested to change by a caller. */
299 if (!(params->rmask & (1 << k)))
302 if (trace_hw_mask_param_enabled())
305 changed = snd_mask_refine(m, constrs_mask(constrs, k));
311 /* Set corresponding flag so that the caller gets it. */
312 trace_hw_mask_param(substream, k, 0, &old_mask, m);
313 params->cmask |= 1 << k;
319 static int constrain_interval_params(struct snd_pcm_substream *substream,
320 struct snd_pcm_hw_params *params)
322 struct snd_pcm_hw_constraints *constrs =
323 &substream->runtime->hw_constraints;
324 struct snd_interval *i;
326 struct snd_interval old_interval;
329 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
330 i = hw_param_interval(params, k);
331 if (snd_interval_empty(i))
334 /* This parameter is not requested to change by a caller. */
335 if (!(params->rmask & (1 << k)))
338 if (trace_hw_interval_param_enabled())
341 changed = snd_interval_refine(i, constrs_interval(constrs, k));
347 /* Set corresponding flag so that the caller gets it. */
348 trace_hw_interval_param(substream, k, 0, &old_interval, i);
349 params->cmask |= 1 << k;
355 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
356 struct snd_pcm_hw_params *params)
358 struct snd_pcm_hw_constraints *constrs =
359 &substream->runtime->hw_constraints;
361 unsigned int *rstamps;
362 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
364 struct snd_pcm_hw_rule *r;
366 struct snd_mask old_mask;
367 struct snd_interval old_interval;
369 int changed, err = 0;
372 * Each application of rule has own sequence number.
374 * Each member of 'rstamps' array represents the sequence number of
375 * recent application of corresponding rule.
377 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
382 * Each member of 'vstamps' array represents the sequence number of
383 * recent application of rule in which corresponding parameters were
386 * In initial state, elements corresponding to parameters requested by
387 * a caller is 1. For unrequested parameters, corresponding members
388 * have 0 so that the parameters are never changed anymore.
390 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
391 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
393 /* Due to the above design, actual sequence number starts at 2. */
396 /* Apply all rules in order. */
398 for (k = 0; k < constrs->rules_num; k++) {
399 r = &constrs->rules[k];
402 * Check condition bits of this rule. When the rule has
403 * some condition bits, parameter without the bits is
404 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
405 * is an example of the condition bits.
407 if (r->cond && !(r->cond & params->flags))
411 * The 'deps' array includes maximum three dependencies
412 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
413 * member of this array is a sentinel and should be
416 * This rule should be processed in this time when dependent
417 * parameters were changed at former applications of the other
420 for (d = 0; r->deps[d] >= 0; d++) {
421 if (vstamps[r->deps[d]] > rstamps[k])
427 if (trace_hw_mask_param_enabled()) {
428 if (hw_is_mask(r->var))
429 old_mask = *hw_param_mask(params, r->var);
431 if (trace_hw_interval_param_enabled()) {
432 if (hw_is_interval(r->var))
433 old_interval = *hw_param_interval(params, r->var);
436 changed = r->func(params, r);
443 * When the parameter is changed, notify it to the caller
444 * by corresponding returned bit, then preparing for next
447 if (changed && r->var >= 0) {
448 if (hw_is_mask(r->var)) {
449 trace_hw_mask_param(substream, r->var,
451 hw_param_mask(params, r->var));
453 if (hw_is_interval(r->var)) {
454 trace_hw_interval_param(substream, r->var,
455 k + 1, &old_interval,
456 hw_param_interval(params, r->var));
459 params->cmask |= (1 << r->var);
460 vstamps[r->var] = stamp;
464 rstamps[k] = stamp++;
467 /* Iterate to evaluate all rules till no parameters are changed. */
476 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
477 struct snd_pcm_hw_params *params)
479 const struct snd_interval *i;
480 const struct snd_mask *m;
483 if (!params->msbits) {
484 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
485 if (snd_interval_single(i))
486 params->msbits = snd_interval_value(i);
489 if (!params->rate_den) {
490 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
491 if (snd_interval_single(i)) {
492 params->rate_num = snd_interval_value(i);
493 params->rate_den = 1;
497 if (!params->fifo_size) {
498 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
499 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
500 if (snd_mask_single(m) && snd_interval_single(i)) {
501 err = substream->ops->ioctl(substream,
502 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
509 params->info = substream->runtime->hw.info;
510 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
511 SNDRV_PCM_INFO_DRAIN_TRIGGER);
512 if (!hw_support_mmap(substream))
513 params->info &= ~(SNDRV_PCM_INFO_MMAP |
514 SNDRV_PCM_INFO_MMAP_VALID);
520 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
521 struct snd_pcm_hw_params *params)
526 params->fifo_size = 0;
527 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
529 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
530 params->rate_num = 0;
531 params->rate_den = 0;
534 err = constrain_mask_params(substream, params);
538 err = constrain_interval_params(substream, params);
542 err = constrain_params_by_rules(substream, params);
550 EXPORT_SYMBOL(snd_pcm_hw_refine);
552 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
553 struct snd_pcm_hw_params __user * _params)
555 struct snd_pcm_hw_params *params;
558 params = memdup_user(_params, sizeof(*params));
560 return PTR_ERR(params);
562 err = snd_pcm_hw_refine(substream, params);
566 err = fixup_unreferenced_params(substream, params);
570 if (copy_to_user(_params, params, sizeof(*params)))
577 static int period_to_usecs(struct snd_pcm_runtime *runtime)
582 return -1; /* invalid */
584 /* take 75% of period time as the deadline */
585 usecs = (750000 / runtime->rate) * runtime->period_size;
586 usecs += ((750000 % runtime->rate) * runtime->period_size) /
592 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
594 snd_pcm_stream_lock_irq(substream);
595 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
596 substream->runtime->status->state = state;
597 snd_pcm_stream_unlock_irq(substream);
600 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
603 #ifdef CONFIG_SND_PCM_TIMER
604 if (substream->timer)
605 snd_timer_notify(substream->timer, event,
606 &substream->runtime->trigger_tstamp);
611 * snd_pcm_hw_param_choose - choose a configuration defined by @params
613 * @params: the hw_params instance
615 * Choose one configuration from configuration space defined by @params.
616 * The configuration chosen is that obtained fixing in this order:
617 * first access, first format, first subformat, min channels,
618 * min rate, min period time, max buffer size, min tick time
620 * Return: Zero if successful, or a negative error code on failure.
622 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
623 struct snd_pcm_hw_params *params)
625 static const int vars[] = {
626 SNDRV_PCM_HW_PARAM_ACCESS,
627 SNDRV_PCM_HW_PARAM_FORMAT,
628 SNDRV_PCM_HW_PARAM_SUBFORMAT,
629 SNDRV_PCM_HW_PARAM_CHANNELS,
630 SNDRV_PCM_HW_PARAM_RATE,
631 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
632 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
633 SNDRV_PCM_HW_PARAM_TICK_TIME,
637 struct snd_mask old_mask;
638 struct snd_interval old_interval;
641 for (v = vars; *v != -1; v++) {
642 /* Keep old parameter to trace. */
643 if (trace_hw_mask_param_enabled()) {
645 old_mask = *hw_param_mask(params, *v);
647 if (trace_hw_interval_param_enabled()) {
648 if (hw_is_interval(*v))
649 old_interval = *hw_param_interval(params, *v);
651 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
652 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
654 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
660 /* Trace the changed parameter. */
661 if (hw_is_mask(*v)) {
662 trace_hw_mask_param(pcm, *v, 0, &old_mask,
663 hw_param_mask(params, *v));
665 if (hw_is_interval(*v)) {
666 trace_hw_interval_param(pcm, *v, 0, &old_interval,
667 hw_param_interval(params, *v));
674 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
675 struct snd_pcm_hw_params *params)
677 struct snd_pcm_runtime *runtime;
680 snd_pcm_uframes_t frames;
682 if (PCM_RUNTIME_CHECK(substream))
684 runtime = substream->runtime;
685 snd_pcm_stream_lock_irq(substream);
686 switch (runtime->status->state) {
687 case SNDRV_PCM_STATE_OPEN:
688 case SNDRV_PCM_STATE_SETUP:
689 case SNDRV_PCM_STATE_PREPARED:
692 snd_pcm_stream_unlock_irq(substream);
695 snd_pcm_stream_unlock_irq(substream);
696 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
697 if (!substream->oss.oss)
699 if (atomic_read(&substream->mmap_count))
703 err = snd_pcm_hw_refine(substream, params);
707 err = snd_pcm_hw_params_choose(substream, params);
711 err = fixup_unreferenced_params(substream, params);
715 if (substream->ops->hw_params != NULL) {
716 err = substream->ops->hw_params(substream, params);
721 runtime->access = params_access(params);
722 runtime->format = params_format(params);
723 runtime->subformat = params_subformat(params);
724 runtime->channels = params_channels(params);
725 runtime->rate = params_rate(params);
726 runtime->period_size = params_period_size(params);
727 runtime->periods = params_periods(params);
728 runtime->buffer_size = params_buffer_size(params);
729 runtime->info = params->info;
730 runtime->rate_num = params->rate_num;
731 runtime->rate_den = params->rate_den;
732 runtime->no_period_wakeup =
733 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
734 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
736 bits = snd_pcm_format_physical_width(runtime->format);
737 runtime->sample_bits = bits;
738 bits *= runtime->channels;
739 runtime->frame_bits = bits;
741 while (bits % 8 != 0) {
745 runtime->byte_align = bits / 8;
746 runtime->min_align = frames;
748 /* Default sw params */
749 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
750 runtime->period_step = 1;
751 runtime->control->avail_min = runtime->period_size;
752 runtime->start_threshold = 1;
753 runtime->stop_threshold = runtime->buffer_size;
754 runtime->silence_threshold = 0;
755 runtime->silence_size = 0;
756 runtime->boundary = runtime->buffer_size;
757 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
758 runtime->boundary *= 2;
760 snd_pcm_timer_resolution_change(substream);
761 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
763 if (pm_qos_request_active(&substream->latency_pm_qos_req))
764 pm_qos_remove_request(&substream->latency_pm_qos_req);
765 if ((usecs = period_to_usecs(runtime)) >= 0)
766 pm_qos_add_request(&substream->latency_pm_qos_req,
767 PM_QOS_CPU_DMA_LATENCY, usecs);
770 /* hardware might be unusable from this time,
771 so we force application to retry to set
772 the correct hardware parameter settings */
773 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
774 if (substream->ops->hw_free != NULL)
775 substream->ops->hw_free(substream);
779 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
780 struct snd_pcm_hw_params __user * _params)
782 struct snd_pcm_hw_params *params;
785 params = memdup_user(_params, sizeof(*params));
787 return PTR_ERR(params);
789 err = snd_pcm_hw_params(substream, params);
793 if (copy_to_user(_params, params, sizeof(*params)))
800 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
802 struct snd_pcm_runtime *runtime;
805 if (PCM_RUNTIME_CHECK(substream))
807 runtime = substream->runtime;
808 snd_pcm_stream_lock_irq(substream);
809 switch (runtime->status->state) {
810 case SNDRV_PCM_STATE_SETUP:
811 case SNDRV_PCM_STATE_PREPARED:
814 snd_pcm_stream_unlock_irq(substream);
817 snd_pcm_stream_unlock_irq(substream);
818 if (atomic_read(&substream->mmap_count))
820 if (substream->ops->hw_free)
821 result = substream->ops->hw_free(substream);
822 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
823 pm_qos_remove_request(&substream->latency_pm_qos_req);
827 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
828 struct snd_pcm_sw_params *params)
830 struct snd_pcm_runtime *runtime;
833 if (PCM_RUNTIME_CHECK(substream))
835 runtime = substream->runtime;
836 snd_pcm_stream_lock_irq(substream);
837 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
838 snd_pcm_stream_unlock_irq(substream);
841 snd_pcm_stream_unlock_irq(substream);
843 if (params->tstamp_mode < 0 ||
844 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
846 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
847 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
849 if (params->avail_min == 0)
851 if (params->silence_size >= runtime->boundary) {
852 if (params->silence_threshold != 0)
855 if (params->silence_size > params->silence_threshold)
857 if (params->silence_threshold > runtime->buffer_size)
861 snd_pcm_stream_lock_irq(substream);
862 runtime->tstamp_mode = params->tstamp_mode;
863 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
864 runtime->tstamp_type = params->tstamp_type;
865 runtime->period_step = params->period_step;
866 runtime->control->avail_min = params->avail_min;
867 runtime->start_threshold = params->start_threshold;
868 runtime->stop_threshold = params->stop_threshold;
869 runtime->silence_threshold = params->silence_threshold;
870 runtime->silence_size = params->silence_size;
871 params->boundary = runtime->boundary;
872 if (snd_pcm_running(substream)) {
873 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
874 runtime->silence_size > 0)
875 snd_pcm_playback_silence(substream, ULONG_MAX);
876 err = snd_pcm_update_state(substream, runtime);
878 snd_pcm_stream_unlock_irq(substream);
882 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
883 struct snd_pcm_sw_params __user * _params)
885 struct snd_pcm_sw_params params;
887 if (copy_from_user(¶ms, _params, sizeof(params)))
889 err = snd_pcm_sw_params(substream, ¶ms);
890 if (copy_to_user(_params, ¶ms, sizeof(params)))
895 static inline snd_pcm_uframes_t
896 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
898 snd_pcm_uframes_t delay;
900 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
901 delay = snd_pcm_playback_hw_avail(substream->runtime);
903 delay = snd_pcm_capture_avail(substream->runtime);
904 return delay + substream->runtime->delay;
907 int snd_pcm_status(struct snd_pcm_substream *substream,
908 struct snd_pcm_status *status)
910 struct snd_pcm_runtime *runtime = substream->runtime;
912 snd_pcm_stream_lock_irq(substream);
914 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
915 &runtime->audio_tstamp_config);
917 /* backwards compatible behavior */
918 if (runtime->audio_tstamp_config.type_requested ==
919 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
920 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
921 runtime->audio_tstamp_config.type_requested =
922 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
924 runtime->audio_tstamp_config.type_requested =
925 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
926 runtime->audio_tstamp_report.valid = 0;
928 runtime->audio_tstamp_report.valid = 1;
930 status->state = runtime->status->state;
931 status->suspended_state = runtime->status->suspended_state;
932 if (status->state == SNDRV_PCM_STATE_OPEN)
934 status->trigger_tstamp = runtime->trigger_tstamp;
935 if (snd_pcm_running(substream)) {
936 snd_pcm_update_hw_ptr(substream);
937 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
938 status->tstamp = runtime->status->tstamp;
939 status->driver_tstamp = runtime->driver_tstamp;
940 status->audio_tstamp =
941 runtime->status->audio_tstamp;
942 if (runtime->audio_tstamp_report.valid == 1)
943 /* backwards compatibility, no report provided in COMPAT mode */
944 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
945 &status->audio_tstamp_accuracy,
946 &runtime->audio_tstamp_report);
951 /* get tstamp only in fallback mode and only if enabled */
952 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
953 snd_pcm_gettime(runtime, &status->tstamp);
956 status->appl_ptr = runtime->control->appl_ptr;
957 status->hw_ptr = runtime->status->hw_ptr;
958 status->avail = snd_pcm_avail(substream);
959 status->delay = snd_pcm_running(substream) ?
960 snd_pcm_calc_delay(substream) : 0;
961 status->avail_max = runtime->avail_max;
962 status->overrange = runtime->overrange;
963 runtime->avail_max = 0;
964 runtime->overrange = 0;
966 snd_pcm_stream_unlock_irq(substream);
970 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
971 struct snd_pcm_status __user * _status,
974 struct snd_pcm_status status;
977 memset(&status, 0, sizeof(status));
979 * with extension, parameters are read/write,
980 * get audio_tstamp_data from user,
981 * ignore rest of status structure
983 if (ext && get_user(status.audio_tstamp_data,
984 (u32 __user *)(&_status->audio_tstamp_data)))
986 res = snd_pcm_status(substream, &status);
989 if (copy_to_user(_status, &status, sizeof(status)))
994 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
995 struct snd_pcm_channel_info * info)
997 struct snd_pcm_runtime *runtime;
998 unsigned int channel;
1000 channel = info->channel;
1001 runtime = substream->runtime;
1002 snd_pcm_stream_lock_irq(substream);
1003 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1004 snd_pcm_stream_unlock_irq(substream);
1007 snd_pcm_stream_unlock_irq(substream);
1008 if (channel >= runtime->channels)
1010 memset(info, 0, sizeof(*info));
1011 info->channel = channel;
1012 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1015 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1016 struct snd_pcm_channel_info __user * _info)
1018 struct snd_pcm_channel_info info;
1021 if (copy_from_user(&info, _info, sizeof(info)))
1023 res = snd_pcm_channel_info(substream, &info);
1026 if (copy_to_user(_info, &info, sizeof(info)))
1031 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1033 struct snd_pcm_runtime *runtime = substream->runtime;
1034 if (runtime->trigger_master == NULL)
1036 if (runtime->trigger_master == substream) {
1037 if (!runtime->trigger_tstamp_latched)
1038 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1040 snd_pcm_trigger_tstamp(runtime->trigger_master);
1041 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1043 runtime->trigger_master = NULL;
1047 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1048 int (*do_action)(struct snd_pcm_substream *substream, int state);
1049 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1050 void (*post_action)(struct snd_pcm_substream *substream, int state);
1054 * this functions is core for handling of linked stream
1055 * Note: the stream state might be changed also on failure
1056 * Note2: call with calling stream lock + link lock
1058 static int snd_pcm_action_group(const struct action_ops *ops,
1059 struct snd_pcm_substream *substream,
1060 int state, int do_lock)
1062 struct snd_pcm_substream *s = NULL;
1063 struct snd_pcm_substream *s1;
1064 int res = 0, depth = 1;
1066 snd_pcm_group_for_each_entry(s, substream) {
1067 if (do_lock && s != substream) {
1068 if (s->pcm->nonatomic)
1069 mutex_lock_nested(&s->self_group.mutex, depth);
1071 spin_lock_nested(&s->self_group.lock, depth);
1074 res = ops->pre_action(s, state);
1078 snd_pcm_group_for_each_entry(s, substream) {
1079 res = ops->do_action(s, state);
1081 if (ops->undo_action) {
1082 snd_pcm_group_for_each_entry(s1, substream) {
1083 if (s1 == s) /* failed stream */
1085 ops->undo_action(s1, state);
1088 s = NULL; /* unlock all */
1092 snd_pcm_group_for_each_entry(s, substream) {
1093 ops->post_action(s, state);
1097 /* unlock streams */
1098 snd_pcm_group_for_each_entry(s1, substream) {
1099 if (s1 != substream) {
1100 if (s1->pcm->nonatomic)
1101 mutex_unlock(&s1->self_group.mutex);
1103 spin_unlock(&s1->self_group.lock);
1105 if (s1 == s) /* end */
1113 * Note: call with stream lock
1115 static int snd_pcm_action_single(const struct action_ops *ops,
1116 struct snd_pcm_substream *substream,
1121 res = ops->pre_action(substream, state);
1124 res = ops->do_action(substream, state);
1126 ops->post_action(substream, state);
1127 else if (ops->undo_action)
1128 ops->undo_action(substream, state);
1132 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1133 struct snd_pcm_group *new_group)
1135 substream->group = new_group;
1136 list_move(&substream->link_list, &new_group->substreams);
1140 * Unref and unlock the group, but keep the stream lock;
1141 * when the group becomes empty and no longer referred, destroy itself
1143 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1144 struct snd_pcm_substream *substream)
1150 do_free = refcount_dec_and_test(&group->refs) &&
1151 list_empty(&group->substreams);
1152 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1158 * Lock the group inside a stream lock and reference it;
1159 * return the locked group object, or NULL if not linked
1161 static struct snd_pcm_group *
1162 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1164 bool nonatomic = substream->pcm->nonatomic;
1165 struct snd_pcm_group *group;
1169 if (!snd_pcm_stream_linked(substream))
1171 group = substream->group;
1172 /* block freeing the group object */
1173 refcount_inc(&group->refs);
1175 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1176 spin_trylock(&group->lock);
1180 /* re-lock for avoiding ABBA deadlock */
1181 snd_pcm_stream_unlock(substream);
1182 snd_pcm_group_lock(group, nonatomic);
1183 snd_pcm_stream_lock(substream);
1185 /* check the group again; the above opens a small race window */
1186 if (substream->group == group)
1188 /* group changed, try again */
1189 snd_pcm_group_unref(group, substream);
1195 * Note: call with stream lock
1197 static int snd_pcm_action(const struct action_ops *ops,
1198 struct snd_pcm_substream *substream,
1201 struct snd_pcm_group *group;
1204 group = snd_pcm_stream_group_ref(substream);
1206 res = snd_pcm_action_group(ops, substream, state, 1);
1208 res = snd_pcm_action_single(ops, substream, state);
1209 snd_pcm_group_unref(group, substream);
1214 * Note: don't use any locks before
1216 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1217 struct snd_pcm_substream *substream,
1222 snd_pcm_stream_lock_irq(substream);
1223 res = snd_pcm_action(ops, substream, state);
1224 snd_pcm_stream_unlock_irq(substream);
1230 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1231 struct snd_pcm_substream *substream,
1236 /* Guarantee the group members won't change during non-atomic action */
1237 down_read(&snd_pcm_link_rwsem);
1238 if (snd_pcm_stream_linked(substream))
1239 res = snd_pcm_action_group(ops, substream, state, 0);
1241 res = snd_pcm_action_single(ops, substream, state);
1242 up_read(&snd_pcm_link_rwsem);
1249 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1251 struct snd_pcm_runtime *runtime = substream->runtime;
1252 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1254 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1255 !snd_pcm_playback_data(substream))
1257 runtime->trigger_tstamp_latched = false;
1258 runtime->trigger_master = substream;
1262 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1264 if (substream->runtime->trigger_master != substream)
1266 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1269 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1271 if (substream->runtime->trigger_master == substream)
1272 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1275 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1277 struct snd_pcm_runtime *runtime = substream->runtime;
1278 snd_pcm_trigger_tstamp(substream);
1279 runtime->hw_ptr_jiffies = jiffies;
1280 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1282 runtime->status->state = state;
1283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1284 runtime->silence_size > 0)
1285 snd_pcm_playback_silence(substream, ULONG_MAX);
1286 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1289 static const struct action_ops snd_pcm_action_start = {
1290 .pre_action = snd_pcm_pre_start,
1291 .do_action = snd_pcm_do_start,
1292 .undo_action = snd_pcm_undo_start,
1293 .post_action = snd_pcm_post_start
1297 * snd_pcm_start - start all linked streams
1298 * @substream: the PCM substream instance
1300 * Return: Zero if successful, or a negative error code.
1301 * The stream lock must be acquired before calling this function.
1303 int snd_pcm_start(struct snd_pcm_substream *substream)
1305 return snd_pcm_action(&snd_pcm_action_start, substream,
1306 SNDRV_PCM_STATE_RUNNING);
1309 /* take the stream lock and start the streams */
1310 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1312 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1313 SNDRV_PCM_STATE_RUNNING);
1319 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1321 struct snd_pcm_runtime *runtime = substream->runtime;
1322 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1324 runtime->trigger_master = substream;
1328 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1330 if (substream->runtime->trigger_master == substream &&
1331 snd_pcm_running(substream))
1332 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1333 return 0; /* unconditonally stop all substreams */
1336 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1338 struct snd_pcm_runtime *runtime = substream->runtime;
1339 if (runtime->status->state != state) {
1340 snd_pcm_trigger_tstamp(substream);
1341 runtime->status->state = state;
1342 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1344 wake_up(&runtime->sleep);
1345 wake_up(&runtime->tsleep);
1348 static const struct action_ops snd_pcm_action_stop = {
1349 .pre_action = snd_pcm_pre_stop,
1350 .do_action = snd_pcm_do_stop,
1351 .post_action = snd_pcm_post_stop
1355 * snd_pcm_stop - try to stop all running streams in the substream group
1356 * @substream: the PCM substream instance
1357 * @state: PCM state after stopping the stream
1359 * The state of each stream is then changed to the given state unconditionally.
1361 * Return: Zero if successful, or a negative error code.
1363 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1365 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1367 EXPORT_SYMBOL(snd_pcm_stop);
1370 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1371 * @substream: the PCM substream
1373 * After stopping, the state is changed to SETUP.
1374 * Unlike snd_pcm_stop(), this affects only the given stream.
1376 * Return: Zero if succesful, or a negative error code.
1378 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1380 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1381 SNDRV_PCM_STATE_SETUP);
1385 * snd_pcm_stop_xrun - stop the running streams as XRUN
1386 * @substream: the PCM substream instance
1388 * This stops the given running substream (and all linked substreams) as XRUN.
1389 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1391 * Return: Zero if successful, or a negative error code.
1393 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1395 unsigned long flags;
1397 snd_pcm_stream_lock_irqsave(substream, flags);
1398 if (substream->runtime && snd_pcm_running(substream))
1399 __snd_pcm_xrun(substream);
1400 snd_pcm_stream_unlock_irqrestore(substream, flags);
1403 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1408 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1410 struct snd_pcm_runtime *runtime = substream->runtime;
1411 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1414 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1416 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1418 runtime->trigger_master = substream;
1422 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1424 if (substream->runtime->trigger_master != substream)
1426 /* some drivers might use hw_ptr to recover from the pause -
1427 update the hw_ptr now */
1429 snd_pcm_update_hw_ptr(substream);
1430 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1431 * a delta between the current jiffies, this gives a large enough
1432 * delta, effectively to skip the check once.
1434 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1435 return substream->ops->trigger(substream,
1436 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1437 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1440 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1442 if (substream->runtime->trigger_master == substream)
1443 substream->ops->trigger(substream,
1444 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1445 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1448 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1450 struct snd_pcm_runtime *runtime = substream->runtime;
1451 snd_pcm_trigger_tstamp(substream);
1453 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1454 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1455 wake_up(&runtime->sleep);
1456 wake_up(&runtime->tsleep);
1458 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1459 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1463 static const struct action_ops snd_pcm_action_pause = {
1464 .pre_action = snd_pcm_pre_pause,
1465 .do_action = snd_pcm_do_pause,
1466 .undo_action = snd_pcm_undo_pause,
1467 .post_action = snd_pcm_post_pause
1471 * Push/release the pause for all linked streams.
1473 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1475 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1481 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1483 struct snd_pcm_runtime *runtime = substream->runtime;
1484 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1486 runtime->trigger_master = substream;
1490 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1492 struct snd_pcm_runtime *runtime = substream->runtime;
1493 if (runtime->trigger_master != substream)
1495 if (! snd_pcm_running(substream))
1497 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1498 return 0; /* suspend unconditionally */
1501 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1503 struct snd_pcm_runtime *runtime = substream->runtime;
1504 snd_pcm_trigger_tstamp(substream);
1505 runtime->status->suspended_state = runtime->status->state;
1506 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1507 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1508 wake_up(&runtime->sleep);
1509 wake_up(&runtime->tsleep);
1512 static const struct action_ops snd_pcm_action_suspend = {
1513 .pre_action = snd_pcm_pre_suspend,
1514 .do_action = snd_pcm_do_suspend,
1515 .post_action = snd_pcm_post_suspend
1519 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1520 * @substream: the PCM substream
1522 * After this call, all streams are changed to SUSPENDED state.
1524 * Return: Zero if successful (or @substream is %NULL), or a negative error
1527 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1530 unsigned long flags;
1535 snd_pcm_stream_lock_irqsave(substream, flags);
1536 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1537 snd_pcm_stream_unlock_irqrestore(substream, flags);
1540 EXPORT_SYMBOL(snd_pcm_suspend);
1543 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1544 * @pcm: the PCM instance
1546 * After this call, all streams are changed to SUSPENDED state.
1548 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1550 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1552 struct snd_pcm_substream *substream;
1553 int stream, err = 0;
1558 for (stream = 0; stream < 2; stream++) {
1559 for (substream = pcm->streams[stream].substream;
1560 substream; substream = substream->next) {
1561 /* FIXME: the open/close code should lock this as well */
1562 if (substream->runtime == NULL)
1564 err = snd_pcm_suspend(substream);
1565 if (err < 0 && err != -EBUSY)
1571 EXPORT_SYMBOL(snd_pcm_suspend_all);
1575 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1577 struct snd_pcm_runtime *runtime = substream->runtime;
1578 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1580 runtime->trigger_master = substream;
1584 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1586 struct snd_pcm_runtime *runtime = substream->runtime;
1587 if (runtime->trigger_master != substream)
1589 /* DMA not running previously? */
1590 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1591 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1592 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1594 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1597 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1599 if (substream->runtime->trigger_master == substream &&
1600 snd_pcm_running(substream))
1601 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1604 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1606 struct snd_pcm_runtime *runtime = substream->runtime;
1607 snd_pcm_trigger_tstamp(substream);
1608 runtime->status->state = runtime->status->suspended_state;
1609 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1612 static const struct action_ops snd_pcm_action_resume = {
1613 .pre_action = snd_pcm_pre_resume,
1614 .do_action = snd_pcm_do_resume,
1615 .undo_action = snd_pcm_undo_resume,
1616 .post_action = snd_pcm_post_resume
1619 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1621 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1626 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1631 #endif /* CONFIG_PM */
1636 * Change the RUNNING stream(s) to XRUN state.
1638 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1640 struct snd_pcm_runtime *runtime = substream->runtime;
1643 snd_pcm_stream_lock_irq(substream);
1644 switch (runtime->status->state) {
1645 case SNDRV_PCM_STATE_XRUN:
1646 result = 0; /* already there */
1648 case SNDRV_PCM_STATE_RUNNING:
1649 __snd_pcm_xrun(substream);
1655 snd_pcm_stream_unlock_irq(substream);
1662 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1664 struct snd_pcm_runtime *runtime = substream->runtime;
1665 switch (runtime->status->state) {
1666 case SNDRV_PCM_STATE_RUNNING:
1667 case SNDRV_PCM_STATE_PREPARED:
1668 case SNDRV_PCM_STATE_PAUSED:
1669 case SNDRV_PCM_STATE_SUSPENDED:
1676 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1678 struct snd_pcm_runtime *runtime = substream->runtime;
1679 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1682 runtime->hw_ptr_base = 0;
1683 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1684 runtime->status->hw_ptr % runtime->period_size;
1685 runtime->silence_start = runtime->status->hw_ptr;
1686 runtime->silence_filled = 0;
1690 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1692 struct snd_pcm_runtime *runtime = substream->runtime;
1693 runtime->control->appl_ptr = runtime->status->hw_ptr;
1694 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1695 runtime->silence_size > 0)
1696 snd_pcm_playback_silence(substream, ULONG_MAX);
1699 static const struct action_ops snd_pcm_action_reset = {
1700 .pre_action = snd_pcm_pre_reset,
1701 .do_action = snd_pcm_do_reset,
1702 .post_action = snd_pcm_post_reset
1705 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1707 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1713 /* we use the second argument for updating f_flags */
1714 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1717 struct snd_pcm_runtime *runtime = substream->runtime;
1718 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1719 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1721 if (snd_pcm_running(substream))
1723 substream->f_flags = f_flags;
1727 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1730 err = substream->ops->prepare(substream);
1733 return snd_pcm_do_reset(substream, 0);
1736 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1738 struct snd_pcm_runtime *runtime = substream->runtime;
1739 runtime->control->appl_ptr = runtime->status->hw_ptr;
1740 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1743 static const struct action_ops snd_pcm_action_prepare = {
1744 .pre_action = snd_pcm_pre_prepare,
1745 .do_action = snd_pcm_do_prepare,
1746 .post_action = snd_pcm_post_prepare
1750 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1751 * @substream: the PCM substream instance
1752 * @file: file to refer f_flags
1754 * Return: Zero if successful, or a negative error code.
1756 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1762 f_flags = file->f_flags;
1764 f_flags = substream->f_flags;
1766 snd_pcm_stream_lock_irq(substream);
1767 switch (substream->runtime->status->state) {
1768 case SNDRV_PCM_STATE_PAUSED:
1769 snd_pcm_pause(substream, 0);
1771 case SNDRV_PCM_STATE_SUSPENDED:
1772 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1775 snd_pcm_stream_unlock_irq(substream);
1777 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1778 substream, f_flags);
1785 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1787 struct snd_pcm_runtime *runtime = substream->runtime;
1788 switch (runtime->status->state) {
1789 case SNDRV_PCM_STATE_OPEN:
1790 case SNDRV_PCM_STATE_DISCONNECTED:
1791 case SNDRV_PCM_STATE_SUSPENDED:
1794 runtime->trigger_master = substream;
1798 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1800 struct snd_pcm_runtime *runtime = substream->runtime;
1801 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1802 switch (runtime->status->state) {
1803 case SNDRV_PCM_STATE_PREPARED:
1804 /* start playback stream if possible */
1805 if (! snd_pcm_playback_empty(substream)) {
1806 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1807 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1809 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1812 case SNDRV_PCM_STATE_RUNNING:
1813 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1815 case SNDRV_PCM_STATE_XRUN:
1816 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1822 /* stop running stream */
1823 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1824 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1825 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1826 snd_pcm_do_stop(substream, new_state);
1827 snd_pcm_post_stop(substream, new_state);
1831 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1832 runtime->trigger_master == substream &&
1833 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1834 return substream->ops->trigger(substream,
1835 SNDRV_PCM_TRIGGER_DRAIN);
1840 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1844 static const struct action_ops snd_pcm_action_drain_init = {
1845 .pre_action = snd_pcm_pre_drain_init,
1846 .do_action = snd_pcm_do_drain_init,
1847 .post_action = snd_pcm_post_drain_init
1850 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1853 * Drain the stream(s).
1854 * When the substream is linked, sync until the draining of all playback streams
1856 * After this call, all streams are supposed to be either SETUP or DRAINING
1857 * (capture only) state.
1859 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1862 struct snd_card *card;
1863 struct snd_pcm_runtime *runtime;
1864 struct snd_pcm_substream *s;
1865 struct snd_pcm_group *group;
1866 wait_queue_entry_t wait;
1870 card = substream->pcm->card;
1871 runtime = substream->runtime;
1873 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1877 if (file->f_flags & O_NONBLOCK)
1879 } else if (substream->f_flags & O_NONBLOCK)
1882 snd_pcm_stream_lock_irq(substream);
1884 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1885 snd_pcm_pause(substream, 0);
1887 /* pre-start/stop - all running streams are changed to DRAINING state */
1888 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1891 /* in non-blocking, we don't wait in ioctl but let caller poll */
1899 struct snd_pcm_runtime *to_check;
1900 if (signal_pending(current)) {
1901 result = -ERESTARTSYS;
1904 /* find a substream to drain */
1906 group = snd_pcm_stream_group_ref(substream);
1907 snd_pcm_group_for_each_entry(s, substream) {
1908 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1910 runtime = s->runtime;
1911 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1916 snd_pcm_group_unref(group, substream);
1918 break; /* all drained */
1919 init_waitqueue_entry(&wait, current);
1920 add_wait_queue(&to_check->sleep, &wait);
1921 snd_pcm_stream_unlock_irq(substream);
1922 if (runtime->no_period_wakeup)
1923 tout = MAX_SCHEDULE_TIMEOUT;
1926 if (runtime->rate) {
1927 long t = runtime->period_size * 2 / runtime->rate;
1928 tout = max(t, tout);
1930 tout = msecs_to_jiffies(tout * 1000);
1932 tout = schedule_timeout_interruptible(tout);
1934 snd_pcm_stream_lock_irq(substream);
1935 group = snd_pcm_stream_group_ref(substream);
1936 snd_pcm_group_for_each_entry(s, substream) {
1937 if (s->runtime == to_check) {
1938 remove_wait_queue(&to_check->sleep, &wait);
1942 snd_pcm_group_unref(group, substream);
1944 if (card->shutdown) {
1949 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1952 dev_dbg(substream->pcm->card->dev,
1953 "playback drain error (DMA or IRQ trouble?)\n");
1954 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1962 snd_pcm_stream_unlock_irq(substream);
1970 * Immediately put all linked substreams into SETUP state.
1972 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1974 struct snd_pcm_runtime *runtime;
1977 if (PCM_RUNTIME_CHECK(substream))
1979 runtime = substream->runtime;
1981 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1982 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1985 snd_pcm_stream_lock_irq(substream);
1987 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1988 snd_pcm_pause(substream, 0);
1990 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1991 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1992 snd_pcm_stream_unlock_irq(substream);
1998 static bool is_pcm_file(struct file *file)
2000 struct inode *inode = file_inode(file);
2001 struct snd_pcm *pcm;
2004 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2006 minor = iminor(inode);
2007 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2009 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2012 snd_card_unref(pcm->card);
2019 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2022 struct snd_pcm_file *pcm_file;
2023 struct snd_pcm_substream *substream1;
2024 struct snd_pcm_group *group, *target_group;
2025 bool nonatomic = substream->pcm->nonatomic;
2026 struct fd f = fdget(fd);
2030 if (!is_pcm_file(f.file)) {
2034 pcm_file = f.file->private_data;
2035 substream1 = pcm_file->substream;
2036 group = kzalloc(sizeof(*group), GFP_KERNEL);
2041 snd_pcm_group_init(group);
2043 down_write(&snd_pcm_link_rwsem);
2044 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2045 substream->runtime->status->state != substream1->runtime->status->state ||
2046 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2050 if (snd_pcm_stream_linked(substream1)) {
2055 snd_pcm_stream_lock_irq(substream);
2056 if (!snd_pcm_stream_linked(substream)) {
2057 snd_pcm_group_assign(substream, group);
2058 group = NULL; /* assigned, don't free this one below */
2060 target_group = substream->group;
2061 snd_pcm_stream_unlock_irq(substream);
2063 snd_pcm_group_lock_irq(target_group, nonatomic);
2064 snd_pcm_stream_lock(substream1);
2065 snd_pcm_group_assign(substream1, target_group);
2066 snd_pcm_stream_unlock(substream1);
2067 snd_pcm_group_unlock_irq(target_group, nonatomic);
2069 up_write(&snd_pcm_link_rwsem);
2077 static void relink_to_local(struct snd_pcm_substream *substream)
2079 snd_pcm_stream_lock(substream);
2080 snd_pcm_group_assign(substream, &substream->self_group);
2081 snd_pcm_stream_unlock(substream);
2084 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2086 struct snd_pcm_group *group;
2087 bool nonatomic = substream->pcm->nonatomic;
2088 bool do_free = false;
2091 down_write(&snd_pcm_link_rwsem);
2093 if (!snd_pcm_stream_linked(substream)) {
2098 group = substream->group;
2099 snd_pcm_group_lock_irq(group, nonatomic);
2101 relink_to_local(substream);
2103 /* detach the last stream, too */
2104 if (list_is_singular(&group->substreams)) {
2105 relink_to_local(list_first_entry(&group->substreams,
2106 struct snd_pcm_substream,
2108 do_free = !refcount_read(&group->refs);
2111 snd_pcm_group_unlock_irq(group, nonatomic);
2116 up_write(&snd_pcm_link_rwsem);
2123 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2124 struct snd_pcm_hw_rule *rule)
2126 struct snd_interval t;
2127 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2128 hw_param_interval_c(params, rule->deps[1]), &t);
2129 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2132 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2133 struct snd_pcm_hw_rule *rule)
2135 struct snd_interval t;
2136 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2137 hw_param_interval_c(params, rule->deps[1]), &t);
2138 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2141 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2142 struct snd_pcm_hw_rule *rule)
2144 struct snd_interval t;
2145 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2146 hw_param_interval_c(params, rule->deps[1]),
2147 (unsigned long) rule->private, &t);
2148 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2151 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2152 struct snd_pcm_hw_rule *rule)
2154 struct snd_interval t;
2155 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2156 (unsigned long) rule->private,
2157 hw_param_interval_c(params, rule->deps[1]), &t);
2158 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2161 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2162 struct snd_pcm_hw_rule *rule)
2165 const struct snd_interval *i =
2166 hw_param_interval_c(params, rule->deps[0]);
2168 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2170 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2172 if (! snd_mask_test(mask, k))
2174 bits = snd_pcm_format_physical_width(k);
2176 continue; /* ignore invalid formats */
2177 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2178 snd_mask_reset(&m, k);
2180 return snd_mask_refine(mask, &m);
2183 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2184 struct snd_pcm_hw_rule *rule)
2186 struct snd_interval t;
2192 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2194 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2196 bits = snd_pcm_format_physical_width(k);
2198 continue; /* ignore invalid formats */
2199 if (t.min > (unsigned)bits)
2201 if (t.max < (unsigned)bits)
2205 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2208 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2209 #error "Change this table"
2212 static const unsigned int rates[] = {
2213 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2214 48000, 64000, 88200, 96000, 176400, 192000
2217 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2218 .count = ARRAY_SIZE(rates),
2222 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2223 struct snd_pcm_hw_rule *rule)
2225 struct snd_pcm_hardware *hw = rule->private;
2226 return snd_interval_list(hw_param_interval(params, rule->var),
2227 snd_pcm_known_rates.count,
2228 snd_pcm_known_rates.list, hw->rates);
2231 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2232 struct snd_pcm_hw_rule *rule)
2234 struct snd_interval t;
2235 struct snd_pcm_substream *substream = rule->private;
2237 t.max = substream->buffer_bytes_max;
2241 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2244 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2246 struct snd_pcm_runtime *runtime = substream->runtime;
2247 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2250 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2251 snd_mask_any(constrs_mask(constrs, k));
2254 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2255 snd_interval_any(constrs_interval(constrs, k));
2258 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2259 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2260 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2261 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2262 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2264 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2265 snd_pcm_hw_rule_format, NULL,
2266 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2269 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2270 snd_pcm_hw_rule_sample_bits, NULL,
2271 SNDRV_PCM_HW_PARAM_FORMAT,
2272 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2275 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2276 snd_pcm_hw_rule_div, NULL,
2277 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2280 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2281 snd_pcm_hw_rule_mul, NULL,
2282 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2285 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2286 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2287 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2290 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2291 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2292 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2295 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2296 snd_pcm_hw_rule_div, NULL,
2297 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2300 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2301 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2302 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2305 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2306 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2307 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2310 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2311 snd_pcm_hw_rule_div, NULL,
2312 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2315 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2316 snd_pcm_hw_rule_div, NULL,
2317 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2320 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2321 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2322 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2325 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2326 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2327 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2330 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2331 snd_pcm_hw_rule_mul, NULL,
2332 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2335 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2336 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2337 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2340 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2341 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2342 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2345 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2346 snd_pcm_hw_rule_muldivk, (void*) 8,
2347 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2350 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2351 snd_pcm_hw_rule_muldivk, (void*) 8,
2352 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2355 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2356 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2357 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2360 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2361 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2362 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2368 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2370 struct snd_pcm_runtime *runtime = substream->runtime;
2371 struct snd_pcm_hardware *hw = &runtime->hw;
2373 unsigned int mask = 0;
2375 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2376 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2377 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2378 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2379 if (hw_support_mmap(substream)) {
2380 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2381 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2382 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2383 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2384 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2385 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2387 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2391 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2395 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2399 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2400 hw->channels_min, hw->channels_max);
2404 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2405 hw->rate_min, hw->rate_max);
2409 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2410 hw->period_bytes_min, hw->period_bytes_max);
2414 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2415 hw->periods_min, hw->periods_max);
2419 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2420 hw->period_bytes_min, hw->buffer_bytes_max);
2424 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2425 snd_pcm_hw_rule_buffer_bytes_max, substream,
2426 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2431 if (runtime->dma_bytes) {
2432 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2437 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2438 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2439 snd_pcm_hw_rule_rate, hw,
2440 SNDRV_PCM_HW_PARAM_RATE, -1);
2445 /* FIXME: this belong to lowlevel */
2446 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2451 static void pcm_release_private(struct snd_pcm_substream *substream)
2453 if (snd_pcm_stream_linked(substream))
2454 snd_pcm_unlink(substream);
2457 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2459 substream->ref_count--;
2460 if (substream->ref_count > 0)
2463 snd_pcm_drop(substream);
2464 if (substream->hw_opened) {
2465 if (substream->ops->hw_free &&
2466 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2467 substream->ops->hw_free(substream);
2468 substream->ops->close(substream);
2469 substream->hw_opened = 0;
2471 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2472 pm_qos_remove_request(&substream->latency_pm_qos_req);
2473 if (substream->pcm_release) {
2474 substream->pcm_release(substream);
2475 substream->pcm_release = NULL;
2477 snd_pcm_detach_substream(substream);
2479 EXPORT_SYMBOL(snd_pcm_release_substream);
2481 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2483 struct snd_pcm_substream **rsubstream)
2485 struct snd_pcm_substream *substream;
2488 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2491 if (substream->ref_count > 1) {
2492 *rsubstream = substream;
2496 err = snd_pcm_hw_constraints_init(substream);
2498 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2502 if ((err = substream->ops->open(substream)) < 0)
2505 substream->hw_opened = 1;
2507 err = snd_pcm_hw_constraints_complete(substream);
2509 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2513 *rsubstream = substream;
2517 snd_pcm_release_substream(substream);
2520 EXPORT_SYMBOL(snd_pcm_open_substream);
2522 static int snd_pcm_open_file(struct file *file,
2523 struct snd_pcm *pcm,
2526 struct snd_pcm_file *pcm_file;
2527 struct snd_pcm_substream *substream;
2530 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2534 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2535 if (pcm_file == NULL) {
2536 snd_pcm_release_substream(substream);
2539 pcm_file->substream = substream;
2540 if (substream->ref_count == 1) {
2541 substream->file = pcm_file;
2542 substream->pcm_release = pcm_release_private;
2544 file->private_data = pcm_file;
2549 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2551 struct snd_pcm *pcm;
2552 int err = nonseekable_open(inode, file);
2555 pcm = snd_lookup_minor_data(iminor(inode),
2556 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2557 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2559 snd_card_unref(pcm->card);
2563 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2565 struct snd_pcm *pcm;
2566 int err = nonseekable_open(inode, file);
2569 pcm = snd_lookup_minor_data(iminor(inode),
2570 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2571 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2573 snd_card_unref(pcm->card);
2577 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2580 wait_queue_entry_t wait;
2586 err = snd_card_file_add(pcm->card, file);
2589 if (!try_module_get(pcm->card->module)) {
2593 init_waitqueue_entry(&wait, current);
2594 add_wait_queue(&pcm->open_wait, &wait);
2595 mutex_lock(&pcm->open_mutex);
2597 err = snd_pcm_open_file(file, pcm, stream);
2600 if (err == -EAGAIN) {
2601 if (file->f_flags & O_NONBLOCK) {
2607 set_current_state(TASK_INTERRUPTIBLE);
2608 mutex_unlock(&pcm->open_mutex);
2610 mutex_lock(&pcm->open_mutex);
2611 if (pcm->card->shutdown) {
2615 if (signal_pending(current)) {
2620 remove_wait_queue(&pcm->open_wait, &wait);
2621 mutex_unlock(&pcm->open_mutex);
2627 module_put(pcm->card->module);
2629 snd_card_file_remove(pcm->card, file);
2634 static int snd_pcm_release(struct inode *inode, struct file *file)
2636 struct snd_pcm *pcm;
2637 struct snd_pcm_substream *substream;
2638 struct snd_pcm_file *pcm_file;
2640 pcm_file = file->private_data;
2641 substream = pcm_file->substream;
2642 if (snd_BUG_ON(!substream))
2644 pcm = substream->pcm;
2645 mutex_lock(&pcm->open_mutex);
2646 snd_pcm_release_substream(substream);
2648 mutex_unlock(&pcm->open_mutex);
2649 wake_up(&pcm->open_wait);
2650 module_put(pcm->card->module);
2651 snd_card_file_remove(pcm->card, file);
2655 /* check and update PCM state; return 0 or a negative error
2656 * call this inside PCM lock
2658 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2660 switch (substream->runtime->status->state) {
2661 case SNDRV_PCM_STATE_DRAINING:
2662 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2665 case SNDRV_PCM_STATE_RUNNING:
2666 return snd_pcm_update_hw_ptr(substream);
2667 case SNDRV_PCM_STATE_PREPARED:
2668 case SNDRV_PCM_STATE_PAUSED:
2670 case SNDRV_PCM_STATE_SUSPENDED:
2672 case SNDRV_PCM_STATE_XRUN:
2679 /* increase the appl_ptr; returns the processed frames or a negative error */
2680 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2681 snd_pcm_uframes_t frames,
2682 snd_pcm_sframes_t avail)
2684 struct snd_pcm_runtime *runtime = substream->runtime;
2685 snd_pcm_sframes_t appl_ptr;
2690 if (frames > (snd_pcm_uframes_t)avail)
2692 appl_ptr = runtime->control->appl_ptr + frames;
2693 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2694 appl_ptr -= runtime->boundary;
2695 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2696 return ret < 0 ? ret : frames;
2699 /* decrease the appl_ptr; returns the processed frames or zero for error */
2700 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2701 snd_pcm_uframes_t frames,
2702 snd_pcm_sframes_t avail)
2704 struct snd_pcm_runtime *runtime = substream->runtime;
2705 snd_pcm_sframes_t appl_ptr;
2710 if (frames > (snd_pcm_uframes_t)avail)
2712 appl_ptr = runtime->control->appl_ptr - frames;
2714 appl_ptr += runtime->boundary;
2715 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2716 /* NOTE: we return zero for errors because PulseAudio gets depressed
2717 * upon receiving an error from rewind ioctl and stops processing
2718 * any longer. Returning zero means that no rewind is done, so
2719 * it's not absolutely wrong to answer like that.
2721 return ret < 0 ? 0 : frames;
2724 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2725 snd_pcm_uframes_t frames)
2727 snd_pcm_sframes_t ret;
2732 snd_pcm_stream_lock_irq(substream);
2733 ret = do_pcm_hwsync(substream);
2735 ret = rewind_appl_ptr(substream, frames,
2736 snd_pcm_hw_avail(substream));
2737 snd_pcm_stream_unlock_irq(substream);
2741 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2742 snd_pcm_uframes_t frames)
2744 snd_pcm_sframes_t ret;
2749 snd_pcm_stream_lock_irq(substream);
2750 ret = do_pcm_hwsync(substream);
2752 ret = forward_appl_ptr(substream, frames,
2753 snd_pcm_avail(substream));
2754 snd_pcm_stream_unlock_irq(substream);
2758 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2762 snd_pcm_stream_lock_irq(substream);
2763 err = do_pcm_hwsync(substream);
2764 snd_pcm_stream_unlock_irq(substream);
2768 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2769 snd_pcm_sframes_t *delay)
2772 snd_pcm_sframes_t n = 0;
2774 snd_pcm_stream_lock_irq(substream);
2775 err = do_pcm_hwsync(substream);
2777 n = snd_pcm_calc_delay(substream);
2778 snd_pcm_stream_unlock_irq(substream);
2784 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2785 struct snd_pcm_sync_ptr __user *_sync_ptr)
2787 struct snd_pcm_runtime *runtime = substream->runtime;
2788 struct snd_pcm_sync_ptr sync_ptr;
2789 volatile struct snd_pcm_mmap_status *status;
2790 volatile struct snd_pcm_mmap_control *control;
2793 memset(&sync_ptr, 0, sizeof(sync_ptr));
2794 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2796 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2798 status = runtime->status;
2799 control = runtime->control;
2800 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2801 err = snd_pcm_hwsync(substream);
2805 snd_pcm_stream_lock_irq(substream);
2806 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2807 err = pcm_lib_apply_appl_ptr(substream,
2808 sync_ptr.c.control.appl_ptr);
2810 snd_pcm_stream_unlock_irq(substream);
2814 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2816 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2817 control->avail_min = sync_ptr.c.control.avail_min;
2819 sync_ptr.c.control.avail_min = control->avail_min;
2820 sync_ptr.s.status.state = status->state;
2821 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2822 sync_ptr.s.status.tstamp = status->tstamp;
2823 sync_ptr.s.status.suspended_state = status->suspended_state;
2824 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2825 snd_pcm_stream_unlock_irq(substream);
2826 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2831 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2833 struct snd_pcm_runtime *runtime = substream->runtime;
2836 if (get_user(arg, _arg))
2838 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2840 runtime->tstamp_type = arg;
2844 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2845 struct snd_xferi __user *_xferi)
2847 struct snd_xferi xferi;
2848 struct snd_pcm_runtime *runtime = substream->runtime;
2849 snd_pcm_sframes_t result;
2851 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2853 if (put_user(0, &_xferi->result))
2855 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2857 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2858 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2860 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2861 __put_user(result, &_xferi->result);
2862 return result < 0 ? result : 0;
2865 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2866 struct snd_xfern __user *_xfern)
2868 struct snd_xfern xfern;
2869 struct snd_pcm_runtime *runtime = substream->runtime;
2871 snd_pcm_sframes_t result;
2873 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2875 if (runtime->channels > 128)
2877 if (put_user(0, &_xfern->result))
2879 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2882 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2884 return PTR_ERR(bufs);
2885 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2886 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2888 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2890 __put_user(result, &_xfern->result);
2891 return result < 0 ? result : 0;
2894 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2895 snd_pcm_uframes_t __user *_frames)
2897 snd_pcm_uframes_t frames;
2898 snd_pcm_sframes_t result;
2900 if (get_user(frames, _frames))
2902 if (put_user(0, _frames))
2904 result = snd_pcm_rewind(substream, frames);
2905 __put_user(result, _frames);
2906 return result < 0 ? result : 0;
2909 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2910 snd_pcm_uframes_t __user *_frames)
2912 snd_pcm_uframes_t frames;
2913 snd_pcm_sframes_t result;
2915 if (get_user(frames, _frames))
2917 if (put_user(0, _frames))
2919 result = snd_pcm_forward(substream, frames);
2920 __put_user(result, _frames);
2921 return result < 0 ? result : 0;
2924 static int snd_pcm_common_ioctl(struct file *file,
2925 struct snd_pcm_substream *substream,
2926 unsigned int cmd, void __user *arg)
2928 struct snd_pcm_file *pcm_file = file->private_data;
2931 if (PCM_RUNTIME_CHECK(substream))
2934 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2939 case SNDRV_PCM_IOCTL_PVERSION:
2940 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2941 case SNDRV_PCM_IOCTL_INFO:
2942 return snd_pcm_info_user(substream, arg);
2943 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2945 case SNDRV_PCM_IOCTL_TTSTAMP:
2946 return snd_pcm_tstamp(substream, arg);
2947 case SNDRV_PCM_IOCTL_USER_PVERSION:
2948 if (get_user(pcm_file->user_pversion,
2949 (unsigned int __user *)arg))
2952 case SNDRV_PCM_IOCTL_HW_REFINE:
2953 return snd_pcm_hw_refine_user(substream, arg);
2954 case SNDRV_PCM_IOCTL_HW_PARAMS:
2955 return snd_pcm_hw_params_user(substream, arg);
2956 case SNDRV_PCM_IOCTL_HW_FREE:
2957 return snd_pcm_hw_free(substream);
2958 case SNDRV_PCM_IOCTL_SW_PARAMS:
2959 return snd_pcm_sw_params_user(substream, arg);
2960 case SNDRV_PCM_IOCTL_STATUS:
2961 return snd_pcm_status_user(substream, arg, false);
2962 case SNDRV_PCM_IOCTL_STATUS_EXT:
2963 return snd_pcm_status_user(substream, arg, true);
2964 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2965 return snd_pcm_channel_info_user(substream, arg);
2966 case SNDRV_PCM_IOCTL_PREPARE:
2967 return snd_pcm_prepare(substream, file);
2968 case SNDRV_PCM_IOCTL_RESET:
2969 return snd_pcm_reset(substream);
2970 case SNDRV_PCM_IOCTL_START:
2971 return snd_pcm_start_lock_irq(substream);
2972 case SNDRV_PCM_IOCTL_LINK:
2973 return snd_pcm_link(substream, (int)(unsigned long) arg);
2974 case SNDRV_PCM_IOCTL_UNLINK:
2975 return snd_pcm_unlink(substream);
2976 case SNDRV_PCM_IOCTL_RESUME:
2977 return snd_pcm_resume(substream);
2978 case SNDRV_PCM_IOCTL_XRUN:
2979 return snd_pcm_xrun(substream);
2980 case SNDRV_PCM_IOCTL_HWSYNC:
2981 return snd_pcm_hwsync(substream);
2982 case SNDRV_PCM_IOCTL_DELAY:
2984 snd_pcm_sframes_t delay;
2985 snd_pcm_sframes_t __user *res = arg;
2988 err = snd_pcm_delay(substream, &delay);
2991 if (put_user(delay, res))
2995 case SNDRV_PCM_IOCTL_SYNC_PTR:
2996 return snd_pcm_sync_ptr(substream, arg);
2997 #ifdef CONFIG_SND_SUPPORT_OLD_API
2998 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2999 return snd_pcm_hw_refine_old_user(substream, arg);
3000 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3001 return snd_pcm_hw_params_old_user(substream, arg);
3003 case SNDRV_PCM_IOCTL_DRAIN:
3004 return snd_pcm_drain(substream, file);
3005 case SNDRV_PCM_IOCTL_DROP:
3006 return snd_pcm_drop(substream);
3007 case SNDRV_PCM_IOCTL_PAUSE:
3008 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3010 (int)(unsigned long)arg);
3011 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3012 case SNDRV_PCM_IOCTL_READI_FRAMES:
3013 return snd_pcm_xferi_frames_ioctl(substream, arg);
3014 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3015 case SNDRV_PCM_IOCTL_READN_FRAMES:
3016 return snd_pcm_xfern_frames_ioctl(substream, arg);
3017 case SNDRV_PCM_IOCTL_REWIND:
3018 return snd_pcm_rewind_ioctl(substream, arg);
3019 case SNDRV_PCM_IOCTL_FORWARD:
3020 return snd_pcm_forward_ioctl(substream, arg);
3022 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3026 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3029 struct snd_pcm_file *pcm_file;
3031 pcm_file = file->private_data;
3033 if (((cmd >> 8) & 0xff) != 'A')
3036 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3037 (void __user *)arg);
3041 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3042 * @substream: PCM substream
3044 * @arg: IOCTL argument
3046 * The function is provided primarily for OSS layer and USB gadget drivers,
3047 * and it allows only the limited set of ioctls (hw_params, sw_params,
3048 * prepare, start, drain, drop, forward).
3050 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3051 unsigned int cmd, void *arg)
3053 snd_pcm_uframes_t *frames = arg;
3054 snd_pcm_sframes_t result;
3057 case SNDRV_PCM_IOCTL_FORWARD:
3059 /* provided only for OSS; capture-only and no value returned */
3060 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3062 result = snd_pcm_forward(substream, *frames);
3063 return result < 0 ? result : 0;
3065 case SNDRV_PCM_IOCTL_HW_PARAMS:
3066 return snd_pcm_hw_params(substream, arg);
3067 case SNDRV_PCM_IOCTL_SW_PARAMS:
3068 return snd_pcm_sw_params(substream, arg);
3069 case SNDRV_PCM_IOCTL_PREPARE:
3070 return snd_pcm_prepare(substream, NULL);
3071 case SNDRV_PCM_IOCTL_START:
3072 return snd_pcm_start_lock_irq(substream);
3073 case SNDRV_PCM_IOCTL_DRAIN:
3074 return snd_pcm_drain(substream, NULL);
3075 case SNDRV_PCM_IOCTL_DROP:
3076 return snd_pcm_drop(substream);
3077 case SNDRV_PCM_IOCTL_DELAY:
3078 return snd_pcm_delay(substream, frames);
3083 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3085 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3088 struct snd_pcm_file *pcm_file;
3089 struct snd_pcm_substream *substream;
3090 struct snd_pcm_runtime *runtime;
3091 snd_pcm_sframes_t result;
3093 pcm_file = file->private_data;
3094 substream = pcm_file->substream;
3095 if (PCM_RUNTIME_CHECK(substream))
3097 runtime = substream->runtime;
3098 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3100 if (!frame_aligned(runtime, count))
3102 count = bytes_to_frames(runtime, count);
3103 result = snd_pcm_lib_read(substream, buf, count);
3105 result = frames_to_bytes(runtime, result);
3109 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3110 size_t count, loff_t * offset)
3112 struct snd_pcm_file *pcm_file;
3113 struct snd_pcm_substream *substream;
3114 struct snd_pcm_runtime *runtime;
3115 snd_pcm_sframes_t result;
3117 pcm_file = file->private_data;
3118 substream = pcm_file->substream;
3119 if (PCM_RUNTIME_CHECK(substream))
3121 runtime = substream->runtime;
3122 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3124 if (!frame_aligned(runtime, count))
3126 count = bytes_to_frames(runtime, count);
3127 result = snd_pcm_lib_write(substream, buf, count);
3129 result = frames_to_bytes(runtime, result);
3133 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3135 struct snd_pcm_file *pcm_file;
3136 struct snd_pcm_substream *substream;
3137 struct snd_pcm_runtime *runtime;
3138 snd_pcm_sframes_t result;
3141 snd_pcm_uframes_t frames;
3143 pcm_file = iocb->ki_filp->private_data;
3144 substream = pcm_file->substream;
3145 if (PCM_RUNTIME_CHECK(substream))
3147 runtime = substream->runtime;
3148 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3150 if (!iter_is_iovec(to))
3152 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3154 if (!frame_aligned(runtime, to->iov->iov_len))
3156 frames = bytes_to_samples(runtime, to->iov->iov_len);
3157 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3160 for (i = 0; i < to->nr_segs; ++i)
3161 bufs[i] = to->iov[i].iov_base;
3162 result = snd_pcm_lib_readv(substream, bufs, frames);
3164 result = frames_to_bytes(runtime, result);
3169 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3171 struct snd_pcm_file *pcm_file;
3172 struct snd_pcm_substream *substream;
3173 struct snd_pcm_runtime *runtime;
3174 snd_pcm_sframes_t result;
3177 snd_pcm_uframes_t frames;
3179 pcm_file = iocb->ki_filp->private_data;
3180 substream = pcm_file->substream;
3181 if (PCM_RUNTIME_CHECK(substream))
3183 runtime = substream->runtime;
3184 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3186 if (!iter_is_iovec(from))
3188 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3189 !frame_aligned(runtime, from->iov->iov_len))
3191 frames = bytes_to_samples(runtime, from->iov->iov_len);
3192 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3195 for (i = 0; i < from->nr_segs; ++i)
3196 bufs[i] = from->iov[i].iov_base;
3197 result = snd_pcm_lib_writev(substream, bufs, frames);
3199 result = frames_to_bytes(runtime, result);
3204 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3206 struct snd_pcm_file *pcm_file;
3207 struct snd_pcm_substream *substream;
3208 struct snd_pcm_runtime *runtime;
3210 snd_pcm_uframes_t avail;
3212 pcm_file = file->private_data;
3214 substream = pcm_file->substream;
3215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3216 ok = EPOLLOUT | EPOLLWRNORM;
3218 ok = EPOLLIN | EPOLLRDNORM;
3219 if (PCM_RUNTIME_CHECK(substream))
3220 return ok | EPOLLERR;
3222 runtime = substream->runtime;
3223 poll_wait(file, &runtime->sleep, wait);
3226 snd_pcm_stream_lock_irq(substream);
3227 avail = snd_pcm_avail(substream);
3228 switch (runtime->status->state) {
3229 case SNDRV_PCM_STATE_RUNNING:
3230 case SNDRV_PCM_STATE_PREPARED:
3231 case SNDRV_PCM_STATE_PAUSED:
3232 if (avail >= runtime->control->avail_min)
3235 case SNDRV_PCM_STATE_DRAINING:
3236 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3243 mask = ok | EPOLLERR;
3246 snd_pcm_stream_unlock_irq(substream);
3255 * Only on coherent architectures, we can mmap the status and the control records
3256 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3258 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3260 * mmap status record
3262 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3264 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3265 struct snd_pcm_runtime *runtime;
3267 if (substream == NULL)
3268 return VM_FAULT_SIGBUS;
3269 runtime = substream->runtime;
3270 vmf->page = virt_to_page(runtime->status);
3271 get_page(vmf->page);
3275 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3277 .fault = snd_pcm_mmap_status_fault,
3280 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3281 struct vm_area_struct *area)
3284 if (!(area->vm_flags & VM_READ))
3286 size = area->vm_end - area->vm_start;
3287 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3289 area->vm_ops = &snd_pcm_vm_ops_status;
3290 area->vm_private_data = substream;
3291 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3296 * mmap control record
3298 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3300 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3301 struct snd_pcm_runtime *runtime;
3303 if (substream == NULL)
3304 return VM_FAULT_SIGBUS;
3305 runtime = substream->runtime;
3306 vmf->page = virt_to_page(runtime->control);
3307 get_page(vmf->page);
3311 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3313 .fault = snd_pcm_mmap_control_fault,
3316 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3317 struct vm_area_struct *area)
3320 if (!(area->vm_flags & VM_READ))
3322 size = area->vm_end - area->vm_start;
3323 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3325 area->vm_ops = &snd_pcm_vm_ops_control;
3326 area->vm_private_data = substream;
3327 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3331 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3333 if (pcm_file->no_compat_mmap)
3335 /* See pcm_control_mmap_allowed() below.
3336 * Since older alsa-lib requires both status and control mmaps to be
3337 * coupled, we have to disable the status mmap for old alsa-lib, too.
3339 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3340 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3345 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3347 if (pcm_file->no_compat_mmap)
3349 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3350 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3351 * thus it effectively assures the manual update of appl_ptr.
3353 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3358 #else /* ! coherent mmap */
3360 * don't support mmap for status and control records.
3362 #define pcm_status_mmap_allowed(pcm_file) false
3363 #define pcm_control_mmap_allowed(pcm_file) false
3365 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3366 struct vm_area_struct *area)
3370 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3371 struct vm_area_struct *area)
3375 #endif /* coherent mmap */
3377 static inline struct page *
3378 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3380 void *vaddr = substream->runtime->dma_area + ofs;
3381 return virt_to_page(vaddr);
3385 * fault callback for mmapping a RAM page
3387 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3389 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3390 struct snd_pcm_runtime *runtime;
3391 unsigned long offset;
3395 if (substream == NULL)
3396 return VM_FAULT_SIGBUS;
3397 runtime = substream->runtime;
3398 offset = vmf->pgoff << PAGE_SHIFT;
3399 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3400 if (offset > dma_bytes - PAGE_SIZE)
3401 return VM_FAULT_SIGBUS;
3402 if (substream->ops->page)
3403 page = substream->ops->page(substream, offset);
3405 page = snd_pcm_default_page_ops(substream, offset);
3407 return VM_FAULT_SIGBUS;
3413 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3414 .open = snd_pcm_mmap_data_open,
3415 .close = snd_pcm_mmap_data_close,
3418 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3419 .open = snd_pcm_mmap_data_open,
3420 .close = snd_pcm_mmap_data_close,
3421 .fault = snd_pcm_mmap_data_fault,
3425 * mmap the DMA buffer on RAM
3429 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3430 * @substream: PCM substream
3433 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3434 * this function is invoked implicitly.
3436 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3437 struct vm_area_struct *area)
3439 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3440 #ifdef CONFIG_GENERIC_ALLOCATOR
3441 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3442 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3443 return remap_pfn_range(area, area->vm_start,
3444 substream->dma_buffer.addr >> PAGE_SHIFT,
3445 area->vm_end - area->vm_start, area->vm_page_prot);
3447 #endif /* CONFIG_GENERIC_ALLOCATOR */
3448 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3449 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3450 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3451 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3453 substream->runtime->dma_area,
3454 substream->runtime->dma_addr,
3455 substream->runtime->dma_bytes);
3456 #endif /* CONFIG_X86 */
3457 /* mmap with fault handler */
3458 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3461 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3464 * mmap the DMA buffer on I/O memory area
3466 #if SNDRV_PCM_INFO_MMAP_IOMEM
3468 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3469 * @substream: PCM substream
3472 * When your hardware uses the iomapped pages as the hardware buffer and
3473 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3474 * is supposed to work only on limited architectures.
3476 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3477 struct vm_area_struct *area)
3479 struct snd_pcm_runtime *runtime = substream->runtime;
3481 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3482 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3484 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3485 #endif /* SNDRV_PCM_INFO_MMAP */
3490 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3491 struct vm_area_struct *area)
3493 struct snd_pcm_runtime *runtime;
3495 unsigned long offset;
3499 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3500 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3503 if (!(area->vm_flags & VM_READ))
3506 runtime = substream->runtime;
3507 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3509 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3511 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3512 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3514 size = area->vm_end - area->vm_start;
3515 offset = area->vm_pgoff << PAGE_SHIFT;
3516 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3517 if ((size_t)size > dma_bytes)
3519 if (offset > dma_bytes - size)
3522 area->vm_ops = &snd_pcm_vm_ops_data;
3523 area->vm_private_data = substream;
3524 if (substream->ops->mmap)
3525 err = substream->ops->mmap(substream, area);
3527 err = snd_pcm_lib_default_mmap(substream, area);
3529 atomic_inc(&substream->mmap_count);
3532 EXPORT_SYMBOL(snd_pcm_mmap_data);
3534 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3536 struct snd_pcm_file * pcm_file;
3537 struct snd_pcm_substream *substream;
3538 unsigned long offset;
3540 pcm_file = file->private_data;
3541 substream = pcm_file->substream;
3542 if (PCM_RUNTIME_CHECK(substream))
3545 offset = area->vm_pgoff << PAGE_SHIFT;
3547 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3548 if (!pcm_status_mmap_allowed(pcm_file))
3550 return snd_pcm_mmap_status(substream, file, area);
3551 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3552 if (!pcm_control_mmap_allowed(pcm_file))
3554 return snd_pcm_mmap_control(substream, file, area);
3556 return snd_pcm_mmap_data(substream, file, area);
3561 static int snd_pcm_fasync(int fd, struct file * file, int on)
3563 struct snd_pcm_file * pcm_file;
3564 struct snd_pcm_substream *substream;
3565 struct snd_pcm_runtime *runtime;
3567 pcm_file = file->private_data;
3568 substream = pcm_file->substream;
3569 if (PCM_RUNTIME_CHECK(substream))
3571 runtime = substream->runtime;
3572 return fasync_helper(fd, file, on, &runtime->fasync);
3578 #ifdef CONFIG_COMPAT
3579 #include "pcm_compat.c"
3581 #define snd_pcm_ioctl_compat NULL
3585 * To be removed helpers to keep binary compatibility
3588 #ifdef CONFIG_SND_SUPPORT_OLD_API
3589 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3590 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3592 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3593 struct snd_pcm_hw_params_old *oparams)
3597 memset(params, 0, sizeof(*params));
3598 params->flags = oparams->flags;
3599 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3600 params->masks[i].bits[0] = oparams->masks[i];
3601 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3602 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3603 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3604 params->info = oparams->info;
3605 params->msbits = oparams->msbits;
3606 params->rate_num = oparams->rate_num;
3607 params->rate_den = oparams->rate_den;
3608 params->fifo_size = oparams->fifo_size;
3611 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3612 struct snd_pcm_hw_params *params)
3616 memset(oparams, 0, sizeof(*oparams));
3617 oparams->flags = params->flags;
3618 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3619 oparams->masks[i] = params->masks[i].bits[0];
3620 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3621 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3622 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3623 oparams->info = params->info;
3624 oparams->msbits = params->msbits;
3625 oparams->rate_num = params->rate_num;
3626 oparams->rate_den = params->rate_den;
3627 oparams->fifo_size = params->fifo_size;
3630 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3631 struct snd_pcm_hw_params_old __user * _oparams)
3633 struct snd_pcm_hw_params *params;
3634 struct snd_pcm_hw_params_old *oparams = NULL;
3637 params = kmalloc(sizeof(*params), GFP_KERNEL);
3641 oparams = memdup_user(_oparams, sizeof(*oparams));
3642 if (IS_ERR(oparams)) {
3643 err = PTR_ERR(oparams);
3646 snd_pcm_hw_convert_from_old_params(params, oparams);
3647 err = snd_pcm_hw_refine(substream, params);
3651 err = fixup_unreferenced_params(substream, params);
3655 snd_pcm_hw_convert_to_old_params(oparams, params);
3656 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3665 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3666 struct snd_pcm_hw_params_old __user * _oparams)
3668 struct snd_pcm_hw_params *params;
3669 struct snd_pcm_hw_params_old *oparams = NULL;
3672 params = kmalloc(sizeof(*params), GFP_KERNEL);
3676 oparams = memdup_user(_oparams, sizeof(*oparams));
3677 if (IS_ERR(oparams)) {
3678 err = PTR_ERR(oparams);
3682 snd_pcm_hw_convert_from_old_params(params, oparams);
3683 err = snd_pcm_hw_params(substream, params);
3687 snd_pcm_hw_convert_to_old_params(oparams, params);
3688 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3696 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3699 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3702 unsigned long pgoff,
3703 unsigned long flags)
3705 struct snd_pcm_file *pcm_file = file->private_data;
3706 struct snd_pcm_substream *substream = pcm_file->substream;
3707 struct snd_pcm_runtime *runtime = substream->runtime;
3708 unsigned long offset = pgoff << PAGE_SHIFT;
3711 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3712 return (unsigned long)runtime->status;
3713 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3714 return (unsigned long)runtime->control;
3716 return (unsigned long)runtime->dma_area + offset;
3720 # define snd_pcm_get_unmapped_area NULL
3727 const struct file_operations snd_pcm_f_ops[2] = {
3729 .owner = THIS_MODULE,
3730 .write = snd_pcm_write,
3731 .write_iter = snd_pcm_writev,
3732 .open = snd_pcm_playback_open,
3733 .release = snd_pcm_release,
3734 .llseek = no_llseek,
3735 .poll = snd_pcm_poll,
3736 .unlocked_ioctl = snd_pcm_ioctl,
3737 .compat_ioctl = snd_pcm_ioctl_compat,
3738 .mmap = snd_pcm_mmap,
3739 .fasync = snd_pcm_fasync,
3740 .get_unmapped_area = snd_pcm_get_unmapped_area,
3743 .owner = THIS_MODULE,
3744 .read = snd_pcm_read,
3745 .read_iter = snd_pcm_readv,
3746 .open = snd_pcm_capture_open,
3747 .release = snd_pcm_release,
3748 .llseek = no_llseek,
3749 .poll = snd_pcm_poll,
3750 .unlocked_ioctl = snd_pcm_ioctl,
3751 .compat_ioctl = snd_pcm_ioctl_compat,
3752 .mmap = snd_pcm_mmap,
3753 .fasync = snd_pcm_fasync,
3754 .get_unmapped_area = snd_pcm_get_unmapped_area,