1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Digital Audio (PCM) abstract layer
4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8 #include <linux/module.h>
9 #include <linux/file.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/time.h>
13 #include <linux/pm_qos.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/vmalloc.h>
17 #include <sound/core.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/timer.h>
23 #include <sound/minors.h>
24 #include <linux/uio.h>
25 #include <linux/delay.h>
27 #include "pcm_local.h"
29 #ifdef CONFIG_SND_DEBUG
30 #define CREATE_TRACE_POINTS
31 #include "pcm_param_trace.h"
33 #define trace_hw_mask_param_enabled() 0
34 #define trace_hw_interval_param_enabled() 0
35 #define trace_hw_mask_param(substream, type, index, prev, curr)
36 #define trace_hw_interval_param(substream, type, index, prev, curr)
43 struct snd_pcm_hw_params_old {
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 static DECLARE_RWSEM(snd_pcm_link_rwsem);
76 void snd_pcm_group_init(struct snd_pcm_group *group)
78 spin_lock_init(&group->lock);
79 mutex_init(&group->mutex);
80 INIT_LIST_HEAD(&group->substreams);
81 refcount_set(&group->refs, 1);
84 /* define group lock helpers */
85 #define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
86 static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
89 mutex_ ## mutex_action(&group->mutex); \
91 spin_ ## action(&group->lock); \
94 DEFINE_PCM_GROUP_LOCK(lock, lock);
95 DEFINE_PCM_GROUP_LOCK(unlock, unlock);
96 DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
97 DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
100 * snd_pcm_stream_lock - Lock the PCM stream
101 * @substream: PCM substream
103 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
104 * flag of the given substream. This also takes the global link rw lock
105 * (or rw sem), too, for avoiding the race with linked streams.
107 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
109 snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
111 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
114 * snd_pcm_stream_lock - Unlock the PCM stream
115 * @substream: PCM substream
117 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
119 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
121 snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
123 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
126 * snd_pcm_stream_lock_irq - Lock the PCM stream
127 * @substream: PCM substream
129 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
130 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
131 * as snd_pcm_stream_lock().
133 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
135 snd_pcm_group_lock_irq(&substream->self_group,
136 substream->pcm->nonatomic);
138 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
141 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
142 * @substream: PCM substream
144 * This is a counter-part of snd_pcm_stream_lock_irq().
146 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
148 snd_pcm_group_unlock_irq(&substream->self_group,
149 substream->pcm->nonatomic);
151 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
153 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
155 unsigned long flags = 0;
156 if (substream->pcm->nonatomic)
157 mutex_lock(&substream->self_group.mutex);
159 spin_lock_irqsave(&substream->self_group.lock, flags);
162 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
165 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
166 * @substream: PCM substream
169 * This is a counter-part of snd_pcm_stream_lock_irqsave().
171 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
174 if (substream->pcm->nonatomic)
175 mutex_unlock(&substream->self_group.mutex);
177 spin_unlock_irqrestore(&substream->self_group.lock, flags);
179 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
181 /* Run PCM ioctl ops */
182 static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
183 unsigned cmd, void *arg)
185 if (substream->ops->ioctl)
186 return substream->ops->ioctl(substream, cmd, arg);
188 return snd_pcm_lib_ioctl(substream, cmd, arg);
191 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
193 struct snd_pcm *pcm = substream->pcm;
194 struct snd_pcm_str *pstr = substream->pstr;
196 memset(info, 0, sizeof(*info));
197 info->card = pcm->card->number;
198 info->device = pcm->device;
199 info->stream = substream->stream;
200 info->subdevice = substream->number;
201 strlcpy(info->id, pcm->id, sizeof(info->id));
202 strlcpy(info->name, pcm->name, sizeof(info->name));
203 info->dev_class = pcm->dev_class;
204 info->dev_subclass = pcm->dev_subclass;
205 info->subdevices_count = pstr->substream_count;
206 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
207 strlcpy(info->subname, substream->name, sizeof(info->subname));
212 int snd_pcm_info_user(struct snd_pcm_substream *substream,
213 struct snd_pcm_info __user * _info)
215 struct snd_pcm_info *info;
218 info = kmalloc(sizeof(*info), GFP_KERNEL);
221 err = snd_pcm_info(substream, info);
223 if (copy_to_user(_info, info, sizeof(*info)))
230 static bool hw_support_mmap(struct snd_pcm_substream *substream)
232 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
235 if (substream->ops->mmap ||
236 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
237 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
240 return dma_can_mmap(substream->dma_buffer.dev.dev);
243 static int constrain_mask_params(struct snd_pcm_substream *substream,
244 struct snd_pcm_hw_params *params)
246 struct snd_pcm_hw_constraints *constrs =
247 &substream->runtime->hw_constraints;
250 struct snd_mask old_mask;
253 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
254 m = hw_param_mask(params, k);
255 if (snd_mask_empty(m))
258 /* This parameter is not requested to change by a caller. */
259 if (!(params->rmask & (1 << k)))
262 if (trace_hw_mask_param_enabled())
265 changed = snd_mask_refine(m, constrs_mask(constrs, k));
271 /* Set corresponding flag so that the caller gets it. */
272 trace_hw_mask_param(substream, k, 0, &old_mask, m);
273 params->cmask |= 1 << k;
279 static int constrain_interval_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params)
282 struct snd_pcm_hw_constraints *constrs =
283 &substream->runtime->hw_constraints;
284 struct snd_interval *i;
286 struct snd_interval old_interval;
289 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
290 i = hw_param_interval(params, k);
291 if (snd_interval_empty(i))
294 /* This parameter is not requested to change by a caller. */
295 if (!(params->rmask & (1 << k)))
298 if (trace_hw_interval_param_enabled())
301 changed = snd_interval_refine(i, constrs_interval(constrs, k));
307 /* Set corresponding flag so that the caller gets it. */
308 trace_hw_interval_param(substream, k, 0, &old_interval, i);
309 params->cmask |= 1 << k;
315 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
316 struct snd_pcm_hw_params *params)
318 struct snd_pcm_hw_constraints *constrs =
319 &substream->runtime->hw_constraints;
321 unsigned int *rstamps;
322 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
324 struct snd_pcm_hw_rule *r;
326 struct snd_mask old_mask;
327 struct snd_interval old_interval;
329 int changed, err = 0;
332 * Each application of rule has own sequence number.
334 * Each member of 'rstamps' array represents the sequence number of
335 * recent application of corresponding rule.
337 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
342 * Each member of 'vstamps' array represents the sequence number of
343 * recent application of rule in which corresponding parameters were
346 * In initial state, elements corresponding to parameters requested by
347 * a caller is 1. For unrequested parameters, corresponding members
348 * have 0 so that the parameters are never changed anymore.
350 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
351 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
353 /* Due to the above design, actual sequence number starts at 2. */
356 /* Apply all rules in order. */
358 for (k = 0; k < constrs->rules_num; k++) {
359 r = &constrs->rules[k];
362 * Check condition bits of this rule. When the rule has
363 * some condition bits, parameter without the bits is
364 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
365 * is an example of the condition bits.
367 if (r->cond && !(r->cond & params->flags))
371 * The 'deps' array includes maximum three dependencies
372 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
373 * member of this array is a sentinel and should be
376 * This rule should be processed in this time when dependent
377 * parameters were changed at former applications of the other
380 for (d = 0; r->deps[d] >= 0; d++) {
381 if (vstamps[r->deps[d]] > rstamps[k])
387 if (trace_hw_mask_param_enabled()) {
388 if (hw_is_mask(r->var))
389 old_mask = *hw_param_mask(params, r->var);
391 if (trace_hw_interval_param_enabled()) {
392 if (hw_is_interval(r->var))
393 old_interval = *hw_param_interval(params, r->var);
396 changed = r->func(params, r);
403 * When the parameter is changed, notify it to the caller
404 * by corresponding returned bit, then preparing for next
407 if (changed && r->var >= 0) {
408 if (hw_is_mask(r->var)) {
409 trace_hw_mask_param(substream, r->var,
411 hw_param_mask(params, r->var));
413 if (hw_is_interval(r->var)) {
414 trace_hw_interval_param(substream, r->var,
415 k + 1, &old_interval,
416 hw_param_interval(params, r->var));
419 params->cmask |= (1 << r->var);
420 vstamps[r->var] = stamp;
424 rstamps[k] = stamp++;
427 /* Iterate to evaluate all rules till no parameters are changed. */
436 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
437 struct snd_pcm_hw_params *params)
439 const struct snd_interval *i;
440 const struct snd_mask *m;
443 if (!params->msbits) {
444 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
445 if (snd_interval_single(i))
446 params->msbits = snd_interval_value(i);
449 if (!params->rate_den) {
450 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
451 if (snd_interval_single(i)) {
452 params->rate_num = snd_interval_value(i);
453 params->rate_den = 1;
457 if (!params->fifo_size) {
458 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
459 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
460 if (snd_mask_single(m) && snd_interval_single(i)) {
461 err = snd_pcm_ops_ioctl(substream,
462 SNDRV_PCM_IOCTL1_FIFO_SIZE,
470 params->info = substream->runtime->hw.info;
471 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
472 SNDRV_PCM_INFO_DRAIN_TRIGGER);
473 if (!hw_support_mmap(substream))
474 params->info &= ~(SNDRV_PCM_INFO_MMAP |
475 SNDRV_PCM_INFO_MMAP_VALID);
481 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params)
487 params->fifo_size = 0;
488 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
490 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
491 params->rate_num = 0;
492 params->rate_den = 0;
495 err = constrain_mask_params(substream, params);
499 err = constrain_interval_params(substream, params);
503 err = constrain_params_by_rules(substream, params);
511 EXPORT_SYMBOL(snd_pcm_hw_refine);
513 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
514 struct snd_pcm_hw_params __user * _params)
516 struct snd_pcm_hw_params *params;
519 params = memdup_user(_params, sizeof(*params));
521 return PTR_ERR(params);
523 err = snd_pcm_hw_refine(substream, params);
527 err = fixup_unreferenced_params(substream, params);
531 if (copy_to_user(_params, params, sizeof(*params)))
538 static int period_to_usecs(struct snd_pcm_runtime *runtime)
543 return -1; /* invalid */
545 /* take 75% of period time as the deadline */
546 usecs = (750000 / runtime->rate) * runtime->period_size;
547 usecs += ((750000 % runtime->rate) * runtime->period_size) /
553 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
555 snd_pcm_stream_lock_irq(substream);
556 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
557 substream->runtime->status->state = state;
558 snd_pcm_stream_unlock_irq(substream);
561 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
564 #ifdef CONFIG_SND_PCM_TIMER
565 if (substream->timer)
566 snd_timer_notify(substream->timer, event,
567 &substream->runtime->trigger_tstamp);
572 * snd_pcm_hw_param_choose - choose a configuration defined by @params
574 * @params: the hw_params instance
576 * Choose one configuration from configuration space defined by @params.
577 * The configuration chosen is that obtained fixing in this order:
578 * first access, first format, first subformat, min channels,
579 * min rate, min period time, max buffer size, min tick time
581 * Return: Zero if successful, or a negative error code on failure.
583 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
584 struct snd_pcm_hw_params *params)
586 static const int vars[] = {
587 SNDRV_PCM_HW_PARAM_ACCESS,
588 SNDRV_PCM_HW_PARAM_FORMAT,
589 SNDRV_PCM_HW_PARAM_SUBFORMAT,
590 SNDRV_PCM_HW_PARAM_CHANNELS,
591 SNDRV_PCM_HW_PARAM_RATE,
592 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
593 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
594 SNDRV_PCM_HW_PARAM_TICK_TIME,
598 struct snd_mask old_mask;
599 struct snd_interval old_interval;
602 for (v = vars; *v != -1; v++) {
603 /* Keep old parameter to trace. */
604 if (trace_hw_mask_param_enabled()) {
606 old_mask = *hw_param_mask(params, *v);
608 if (trace_hw_interval_param_enabled()) {
609 if (hw_is_interval(*v))
610 old_interval = *hw_param_interval(params, *v);
612 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
613 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
615 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
621 /* Trace the changed parameter. */
622 if (hw_is_mask(*v)) {
623 trace_hw_mask_param(pcm, *v, 0, &old_mask,
624 hw_param_mask(params, *v));
626 if (hw_is_interval(*v)) {
627 trace_hw_interval_param(pcm, *v, 0, &old_interval,
628 hw_param_interval(params, *v));
635 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
636 struct snd_pcm_hw_params *params)
638 struct snd_pcm_runtime *runtime;
641 snd_pcm_uframes_t frames;
643 if (PCM_RUNTIME_CHECK(substream))
645 runtime = substream->runtime;
646 snd_pcm_stream_lock_irq(substream);
647 switch (runtime->status->state) {
648 case SNDRV_PCM_STATE_OPEN:
649 case SNDRV_PCM_STATE_SETUP:
650 case SNDRV_PCM_STATE_PREPARED:
653 snd_pcm_stream_unlock_irq(substream);
656 snd_pcm_stream_unlock_irq(substream);
657 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
658 if (!substream->oss.oss)
660 if (atomic_read(&substream->mmap_count))
664 err = snd_pcm_hw_refine(substream, params);
668 err = snd_pcm_hw_params_choose(substream, params);
672 err = fixup_unreferenced_params(substream, params);
676 if (substream->managed_buffer_alloc) {
677 err = snd_pcm_lib_malloc_pages(substream,
678 params_buffer_bytes(params));
681 runtime->buffer_changed = err > 0;
684 if (substream->ops->hw_params != NULL) {
685 err = substream->ops->hw_params(substream, params);
690 runtime->access = params_access(params);
691 runtime->format = params_format(params);
692 runtime->subformat = params_subformat(params);
693 runtime->channels = params_channels(params);
694 runtime->rate = params_rate(params);
695 runtime->period_size = params_period_size(params);
696 runtime->periods = params_periods(params);
697 runtime->buffer_size = params_buffer_size(params);
698 runtime->info = params->info;
699 runtime->rate_num = params->rate_num;
700 runtime->rate_den = params->rate_den;
701 runtime->no_period_wakeup =
702 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
703 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
705 bits = snd_pcm_format_physical_width(runtime->format);
706 runtime->sample_bits = bits;
707 bits *= runtime->channels;
708 runtime->frame_bits = bits;
710 while (bits % 8 != 0) {
714 runtime->byte_align = bits / 8;
715 runtime->min_align = frames;
717 /* Default sw params */
718 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
719 runtime->period_step = 1;
720 runtime->control->avail_min = runtime->period_size;
721 runtime->start_threshold = 1;
722 runtime->stop_threshold = runtime->buffer_size;
723 runtime->silence_threshold = 0;
724 runtime->silence_size = 0;
725 runtime->boundary = runtime->buffer_size;
726 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
727 runtime->boundary *= 2;
729 snd_pcm_timer_resolution_change(substream);
730 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
732 if (pm_qos_request_active(&substream->latency_pm_qos_req))
733 pm_qos_remove_request(&substream->latency_pm_qos_req);
734 if ((usecs = period_to_usecs(runtime)) >= 0)
735 pm_qos_add_request(&substream->latency_pm_qos_req,
736 PM_QOS_CPU_DMA_LATENCY, usecs);
739 /* hardware might be unusable from this time,
740 so we force application to retry to set
741 the correct hardware parameter settings */
742 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
743 if (substream->ops->hw_free != NULL)
744 substream->ops->hw_free(substream);
745 if (substream->managed_buffer_alloc)
746 snd_pcm_lib_free_pages(substream);
750 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
751 struct snd_pcm_hw_params __user * _params)
753 struct snd_pcm_hw_params *params;
756 params = memdup_user(_params, sizeof(*params));
758 return PTR_ERR(params);
760 err = snd_pcm_hw_params(substream, params);
764 if (copy_to_user(_params, params, sizeof(*params)))
771 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
773 struct snd_pcm_runtime *runtime;
776 if (PCM_RUNTIME_CHECK(substream))
778 runtime = substream->runtime;
779 snd_pcm_stream_lock_irq(substream);
780 switch (runtime->status->state) {
781 case SNDRV_PCM_STATE_SETUP:
782 case SNDRV_PCM_STATE_PREPARED:
785 snd_pcm_stream_unlock_irq(substream);
788 snd_pcm_stream_unlock_irq(substream);
789 if (atomic_read(&substream->mmap_count))
791 if (substream->ops->hw_free)
792 result = substream->ops->hw_free(substream);
793 if (substream->managed_buffer_alloc)
794 snd_pcm_lib_free_pages(substream);
795 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
796 pm_qos_remove_request(&substream->latency_pm_qos_req);
800 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
801 struct snd_pcm_sw_params *params)
803 struct snd_pcm_runtime *runtime;
806 if (PCM_RUNTIME_CHECK(substream))
808 runtime = substream->runtime;
809 snd_pcm_stream_lock_irq(substream);
810 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
811 snd_pcm_stream_unlock_irq(substream);
814 snd_pcm_stream_unlock_irq(substream);
816 if (params->tstamp_mode < 0 ||
817 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
819 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
820 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
822 if (params->avail_min == 0)
824 if (params->silence_size >= runtime->boundary) {
825 if (params->silence_threshold != 0)
828 if (params->silence_size > params->silence_threshold)
830 if (params->silence_threshold > runtime->buffer_size)
834 snd_pcm_stream_lock_irq(substream);
835 runtime->tstamp_mode = params->tstamp_mode;
836 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
837 runtime->tstamp_type = params->tstamp_type;
838 runtime->period_step = params->period_step;
839 runtime->control->avail_min = params->avail_min;
840 runtime->start_threshold = params->start_threshold;
841 runtime->stop_threshold = params->stop_threshold;
842 runtime->silence_threshold = params->silence_threshold;
843 runtime->silence_size = params->silence_size;
844 params->boundary = runtime->boundary;
845 if (snd_pcm_running(substream)) {
846 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
847 runtime->silence_size > 0)
848 snd_pcm_playback_silence(substream, ULONG_MAX);
849 err = snd_pcm_update_state(substream, runtime);
851 snd_pcm_stream_unlock_irq(substream);
855 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
856 struct snd_pcm_sw_params __user * _params)
858 struct snd_pcm_sw_params params;
860 if (copy_from_user(¶ms, _params, sizeof(params)))
862 err = snd_pcm_sw_params(substream, ¶ms);
863 if (copy_to_user(_params, ¶ms, sizeof(params)))
868 static inline snd_pcm_uframes_t
869 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
871 snd_pcm_uframes_t delay;
873 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
874 delay = snd_pcm_playback_hw_avail(substream->runtime);
876 delay = snd_pcm_capture_avail(substream->runtime);
877 return delay + substream->runtime->delay;
880 int snd_pcm_status(struct snd_pcm_substream *substream,
881 struct snd_pcm_status *status)
883 struct snd_pcm_runtime *runtime = substream->runtime;
885 snd_pcm_stream_lock_irq(substream);
887 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
888 &runtime->audio_tstamp_config);
890 /* backwards compatible behavior */
891 if (runtime->audio_tstamp_config.type_requested ==
892 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
893 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
894 runtime->audio_tstamp_config.type_requested =
895 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
897 runtime->audio_tstamp_config.type_requested =
898 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
899 runtime->audio_tstamp_report.valid = 0;
901 runtime->audio_tstamp_report.valid = 1;
903 status->state = runtime->status->state;
904 status->suspended_state = runtime->status->suspended_state;
905 if (status->state == SNDRV_PCM_STATE_OPEN)
907 status->trigger_tstamp = runtime->trigger_tstamp;
908 if (snd_pcm_running(substream)) {
909 snd_pcm_update_hw_ptr(substream);
910 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
911 status->tstamp = runtime->status->tstamp;
912 status->driver_tstamp = runtime->driver_tstamp;
913 status->audio_tstamp =
914 runtime->status->audio_tstamp;
915 if (runtime->audio_tstamp_report.valid == 1)
916 /* backwards compatibility, no report provided in COMPAT mode */
917 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
918 &status->audio_tstamp_accuracy,
919 &runtime->audio_tstamp_report);
924 /* get tstamp only in fallback mode and only if enabled */
925 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
926 snd_pcm_gettime(runtime, &status->tstamp);
929 status->appl_ptr = runtime->control->appl_ptr;
930 status->hw_ptr = runtime->status->hw_ptr;
931 status->avail = snd_pcm_avail(substream);
932 status->delay = snd_pcm_running(substream) ?
933 snd_pcm_calc_delay(substream) : 0;
934 status->avail_max = runtime->avail_max;
935 status->overrange = runtime->overrange;
936 runtime->avail_max = 0;
937 runtime->overrange = 0;
939 snd_pcm_stream_unlock_irq(substream);
943 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
944 struct snd_pcm_status __user * _status,
947 struct snd_pcm_status status;
950 memset(&status, 0, sizeof(status));
952 * with extension, parameters are read/write,
953 * get audio_tstamp_data from user,
954 * ignore rest of status structure
956 if (ext && get_user(status.audio_tstamp_data,
957 (u32 __user *)(&_status->audio_tstamp_data)))
959 res = snd_pcm_status(substream, &status);
962 if (copy_to_user(_status, &status, sizeof(status)))
967 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
968 struct snd_pcm_channel_info * info)
970 struct snd_pcm_runtime *runtime;
971 unsigned int channel;
973 channel = info->channel;
974 runtime = substream->runtime;
975 snd_pcm_stream_lock_irq(substream);
976 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
977 snd_pcm_stream_unlock_irq(substream);
980 snd_pcm_stream_unlock_irq(substream);
981 if (channel >= runtime->channels)
983 memset(info, 0, sizeof(*info));
984 info->channel = channel;
985 return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
988 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
989 struct snd_pcm_channel_info __user * _info)
991 struct snd_pcm_channel_info info;
994 if (copy_from_user(&info, _info, sizeof(info)))
996 res = snd_pcm_channel_info(substream, &info);
999 if (copy_to_user(_info, &info, sizeof(info)))
1004 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1006 struct snd_pcm_runtime *runtime = substream->runtime;
1007 if (runtime->trigger_master == NULL)
1009 if (runtime->trigger_master == substream) {
1010 if (!runtime->trigger_tstamp_latched)
1011 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1013 snd_pcm_trigger_tstamp(runtime->trigger_master);
1014 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1016 runtime->trigger_master = NULL;
1020 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1021 int (*do_action)(struct snd_pcm_substream *substream, int state);
1022 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1023 void (*post_action)(struct snd_pcm_substream *substream, int state);
1027 * this functions is core for handling of linked stream
1028 * Note: the stream state might be changed also on failure
1029 * Note2: call with calling stream lock + link lock
1031 static int snd_pcm_action_group(const struct action_ops *ops,
1032 struct snd_pcm_substream *substream,
1033 int state, int do_lock)
1035 struct snd_pcm_substream *s = NULL;
1036 struct snd_pcm_substream *s1;
1037 int res = 0, depth = 1;
1039 snd_pcm_group_for_each_entry(s, substream) {
1040 if (do_lock && s != substream) {
1041 if (s->pcm->nonatomic)
1042 mutex_lock_nested(&s->self_group.mutex, depth);
1044 spin_lock_nested(&s->self_group.lock, depth);
1047 res = ops->pre_action(s, state);
1051 snd_pcm_group_for_each_entry(s, substream) {
1052 res = ops->do_action(s, state);
1054 if (ops->undo_action) {
1055 snd_pcm_group_for_each_entry(s1, substream) {
1056 if (s1 == s) /* failed stream */
1058 ops->undo_action(s1, state);
1061 s = NULL; /* unlock all */
1065 snd_pcm_group_for_each_entry(s, substream) {
1066 ops->post_action(s, state);
1070 /* unlock streams */
1071 snd_pcm_group_for_each_entry(s1, substream) {
1072 if (s1 != substream) {
1073 if (s1->pcm->nonatomic)
1074 mutex_unlock(&s1->self_group.mutex);
1076 spin_unlock(&s1->self_group.lock);
1078 if (s1 == s) /* end */
1086 * Note: call with stream lock
1088 static int snd_pcm_action_single(const struct action_ops *ops,
1089 struct snd_pcm_substream *substream,
1094 res = ops->pre_action(substream, state);
1097 res = ops->do_action(substream, state);
1099 ops->post_action(substream, state);
1100 else if (ops->undo_action)
1101 ops->undo_action(substream, state);
1105 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1106 struct snd_pcm_group *new_group)
1108 substream->group = new_group;
1109 list_move(&substream->link_list, &new_group->substreams);
1113 * Unref and unlock the group, but keep the stream lock;
1114 * when the group becomes empty and no longer referred, destroy itself
1116 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1117 struct snd_pcm_substream *substream)
1123 do_free = refcount_dec_and_test(&group->refs);
1124 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1130 * Lock the group inside a stream lock and reference it;
1131 * return the locked group object, or NULL if not linked
1133 static struct snd_pcm_group *
1134 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1136 bool nonatomic = substream->pcm->nonatomic;
1137 struct snd_pcm_group *group;
1141 if (!snd_pcm_stream_linked(substream))
1143 group = substream->group;
1144 /* block freeing the group object */
1145 refcount_inc(&group->refs);
1147 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1148 spin_trylock(&group->lock);
1152 /* re-lock for avoiding ABBA deadlock */
1153 snd_pcm_stream_unlock(substream);
1154 snd_pcm_group_lock(group, nonatomic);
1155 snd_pcm_stream_lock(substream);
1157 /* check the group again; the above opens a small race window */
1158 if (substream->group == group)
1160 /* group changed, try again */
1161 snd_pcm_group_unref(group, substream);
1167 * Note: call with stream lock
1169 static int snd_pcm_action(const struct action_ops *ops,
1170 struct snd_pcm_substream *substream,
1173 struct snd_pcm_group *group;
1176 group = snd_pcm_stream_group_ref(substream);
1178 res = snd_pcm_action_group(ops, substream, state, 1);
1180 res = snd_pcm_action_single(ops, substream, state);
1181 snd_pcm_group_unref(group, substream);
1186 * Note: don't use any locks before
1188 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1189 struct snd_pcm_substream *substream,
1194 snd_pcm_stream_lock_irq(substream);
1195 res = snd_pcm_action(ops, substream, state);
1196 snd_pcm_stream_unlock_irq(substream);
1202 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1203 struct snd_pcm_substream *substream,
1208 /* Guarantee the group members won't change during non-atomic action */
1209 down_read(&snd_pcm_link_rwsem);
1210 if (snd_pcm_stream_linked(substream))
1211 res = snd_pcm_action_group(ops, substream, state, 0);
1213 res = snd_pcm_action_single(ops, substream, state);
1214 up_read(&snd_pcm_link_rwsem);
1221 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1223 struct snd_pcm_runtime *runtime = substream->runtime;
1224 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1227 !snd_pcm_playback_data(substream))
1229 runtime->trigger_tstamp_latched = false;
1230 runtime->trigger_master = substream;
1234 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1236 if (substream->runtime->trigger_master != substream)
1238 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1241 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1243 if (substream->runtime->trigger_master == substream)
1244 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1247 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1249 struct snd_pcm_runtime *runtime = substream->runtime;
1250 snd_pcm_trigger_tstamp(substream);
1251 runtime->hw_ptr_jiffies = jiffies;
1252 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1254 runtime->status->state = state;
1255 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1256 runtime->silence_size > 0)
1257 snd_pcm_playback_silence(substream, ULONG_MAX);
1258 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1261 static const struct action_ops snd_pcm_action_start = {
1262 .pre_action = snd_pcm_pre_start,
1263 .do_action = snd_pcm_do_start,
1264 .undo_action = snd_pcm_undo_start,
1265 .post_action = snd_pcm_post_start
1269 * snd_pcm_start - start all linked streams
1270 * @substream: the PCM substream instance
1272 * Return: Zero if successful, or a negative error code.
1273 * The stream lock must be acquired before calling this function.
1275 int snd_pcm_start(struct snd_pcm_substream *substream)
1277 return snd_pcm_action(&snd_pcm_action_start, substream,
1278 SNDRV_PCM_STATE_RUNNING);
1281 /* take the stream lock and start the streams */
1282 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1284 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1285 SNDRV_PCM_STATE_RUNNING);
1291 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1293 struct snd_pcm_runtime *runtime = substream->runtime;
1294 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1296 runtime->trigger_master = substream;
1300 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1302 if (substream->runtime->trigger_master == substream &&
1303 snd_pcm_running(substream))
1304 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1305 return 0; /* unconditonally stop all substreams */
1308 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1310 struct snd_pcm_runtime *runtime = substream->runtime;
1311 if (runtime->status->state != state) {
1312 snd_pcm_trigger_tstamp(substream);
1313 runtime->status->state = state;
1314 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1316 wake_up(&runtime->sleep);
1317 wake_up(&runtime->tsleep);
1320 static const struct action_ops snd_pcm_action_stop = {
1321 .pre_action = snd_pcm_pre_stop,
1322 .do_action = snd_pcm_do_stop,
1323 .post_action = snd_pcm_post_stop
1327 * snd_pcm_stop - try to stop all running streams in the substream group
1328 * @substream: the PCM substream instance
1329 * @state: PCM state after stopping the stream
1331 * The state of each stream is then changed to the given state unconditionally.
1333 * Return: Zero if successful, or a negative error code.
1335 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1337 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1339 EXPORT_SYMBOL(snd_pcm_stop);
1342 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1343 * @substream: the PCM substream
1345 * After stopping, the state is changed to SETUP.
1346 * Unlike snd_pcm_stop(), this affects only the given stream.
1348 * Return: Zero if succesful, or a negative error code.
1350 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1352 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1353 SNDRV_PCM_STATE_SETUP);
1357 * snd_pcm_stop_xrun - stop the running streams as XRUN
1358 * @substream: the PCM substream instance
1360 * This stops the given running substream (and all linked substreams) as XRUN.
1361 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1363 * Return: Zero if successful, or a negative error code.
1365 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1367 unsigned long flags;
1369 snd_pcm_stream_lock_irqsave(substream, flags);
1370 if (substream->runtime && snd_pcm_running(substream))
1371 __snd_pcm_xrun(substream);
1372 snd_pcm_stream_unlock_irqrestore(substream, flags);
1375 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1380 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1382 struct snd_pcm_runtime *runtime = substream->runtime;
1383 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1386 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1388 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1390 runtime->trigger_master = substream;
1394 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1396 if (substream->runtime->trigger_master != substream)
1398 /* some drivers might use hw_ptr to recover from the pause -
1399 update the hw_ptr now */
1401 snd_pcm_update_hw_ptr(substream);
1402 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1403 * a delta between the current jiffies, this gives a large enough
1404 * delta, effectively to skip the check once.
1406 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1407 return substream->ops->trigger(substream,
1408 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1409 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1412 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1414 if (substream->runtime->trigger_master == substream)
1415 substream->ops->trigger(substream,
1416 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1417 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1420 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1422 struct snd_pcm_runtime *runtime = substream->runtime;
1423 snd_pcm_trigger_tstamp(substream);
1425 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1426 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1427 wake_up(&runtime->sleep);
1428 wake_up(&runtime->tsleep);
1430 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1431 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1435 static const struct action_ops snd_pcm_action_pause = {
1436 .pre_action = snd_pcm_pre_pause,
1437 .do_action = snd_pcm_do_pause,
1438 .undo_action = snd_pcm_undo_pause,
1439 .post_action = snd_pcm_post_pause
1443 * Push/release the pause for all linked streams.
1445 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1447 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1453 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1455 struct snd_pcm_runtime *runtime = substream->runtime;
1456 switch (runtime->status->state) {
1457 case SNDRV_PCM_STATE_SUSPENDED:
1459 /* unresumable PCM state; return -EBUSY for skipping suspend */
1460 case SNDRV_PCM_STATE_OPEN:
1461 case SNDRV_PCM_STATE_SETUP:
1462 case SNDRV_PCM_STATE_DISCONNECTED:
1465 runtime->trigger_master = substream;
1469 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1471 struct snd_pcm_runtime *runtime = substream->runtime;
1472 if (runtime->trigger_master != substream)
1474 if (! snd_pcm_running(substream))
1476 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1477 return 0; /* suspend unconditionally */
1480 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1482 struct snd_pcm_runtime *runtime = substream->runtime;
1483 snd_pcm_trigger_tstamp(substream);
1484 runtime->status->suspended_state = runtime->status->state;
1485 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1486 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1487 wake_up(&runtime->sleep);
1488 wake_up(&runtime->tsleep);
1491 static const struct action_ops snd_pcm_action_suspend = {
1492 .pre_action = snd_pcm_pre_suspend,
1493 .do_action = snd_pcm_do_suspend,
1494 .post_action = snd_pcm_post_suspend
1498 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1499 * @substream: the PCM substream
1501 * After this call, all streams are changed to SUSPENDED state.
1503 * Return: Zero if successful, or a negative error code.
1505 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1508 unsigned long flags;
1510 snd_pcm_stream_lock_irqsave(substream, flags);
1511 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1512 snd_pcm_stream_unlock_irqrestore(substream, flags);
1517 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1518 * @pcm: the PCM instance
1520 * After this call, all streams are changed to SUSPENDED state.
1522 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1524 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1526 struct snd_pcm_substream *substream;
1527 int stream, err = 0;
1532 for (stream = 0; stream < 2; stream++) {
1533 for (substream = pcm->streams[stream].substream;
1534 substream; substream = substream->next) {
1535 /* FIXME: the open/close code should lock this as well */
1536 if (substream->runtime == NULL)
1540 * Skip BE dai link PCM's that are internal and may
1541 * not have their substream ops set.
1543 if (!substream->ops)
1546 err = snd_pcm_suspend(substream);
1547 if (err < 0 && err != -EBUSY)
1553 EXPORT_SYMBOL(snd_pcm_suspend_all);
1557 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1559 struct snd_pcm_runtime *runtime = substream->runtime;
1560 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1562 runtime->trigger_master = substream;
1566 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1568 struct snd_pcm_runtime *runtime = substream->runtime;
1569 if (runtime->trigger_master != substream)
1571 /* DMA not running previously? */
1572 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1573 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1574 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1576 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1579 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1581 if (substream->runtime->trigger_master == substream &&
1582 snd_pcm_running(substream))
1583 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1586 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1588 struct snd_pcm_runtime *runtime = substream->runtime;
1589 snd_pcm_trigger_tstamp(substream);
1590 runtime->status->state = runtime->status->suspended_state;
1591 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1594 static const struct action_ops snd_pcm_action_resume = {
1595 .pre_action = snd_pcm_pre_resume,
1596 .do_action = snd_pcm_do_resume,
1597 .undo_action = snd_pcm_undo_resume,
1598 .post_action = snd_pcm_post_resume
1601 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1603 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1608 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1613 #endif /* CONFIG_PM */
1618 * Change the RUNNING stream(s) to XRUN state.
1620 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1622 struct snd_pcm_runtime *runtime = substream->runtime;
1625 snd_pcm_stream_lock_irq(substream);
1626 switch (runtime->status->state) {
1627 case SNDRV_PCM_STATE_XRUN:
1628 result = 0; /* already there */
1630 case SNDRV_PCM_STATE_RUNNING:
1631 __snd_pcm_xrun(substream);
1637 snd_pcm_stream_unlock_irq(substream);
1644 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1646 struct snd_pcm_runtime *runtime = substream->runtime;
1647 switch (runtime->status->state) {
1648 case SNDRV_PCM_STATE_RUNNING:
1649 case SNDRV_PCM_STATE_PREPARED:
1650 case SNDRV_PCM_STATE_PAUSED:
1651 case SNDRV_PCM_STATE_SUSPENDED:
1658 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1660 struct snd_pcm_runtime *runtime = substream->runtime;
1661 int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1664 runtime->hw_ptr_base = 0;
1665 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1666 runtime->status->hw_ptr % runtime->period_size;
1667 runtime->silence_start = runtime->status->hw_ptr;
1668 runtime->silence_filled = 0;
1672 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1674 struct snd_pcm_runtime *runtime = substream->runtime;
1675 runtime->control->appl_ptr = runtime->status->hw_ptr;
1676 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1677 runtime->silence_size > 0)
1678 snd_pcm_playback_silence(substream, ULONG_MAX);
1681 static const struct action_ops snd_pcm_action_reset = {
1682 .pre_action = snd_pcm_pre_reset,
1683 .do_action = snd_pcm_do_reset,
1684 .post_action = snd_pcm_post_reset
1687 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1689 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1695 /* we use the second argument for updating f_flags */
1696 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1699 struct snd_pcm_runtime *runtime = substream->runtime;
1700 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1701 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1703 if (snd_pcm_running(substream))
1705 substream->f_flags = f_flags;
1709 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1712 err = substream->ops->prepare(substream);
1715 return snd_pcm_do_reset(substream, 0);
1718 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1720 struct snd_pcm_runtime *runtime = substream->runtime;
1721 runtime->control->appl_ptr = runtime->status->hw_ptr;
1722 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1725 static const struct action_ops snd_pcm_action_prepare = {
1726 .pre_action = snd_pcm_pre_prepare,
1727 .do_action = snd_pcm_do_prepare,
1728 .post_action = snd_pcm_post_prepare
1732 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1733 * @substream: the PCM substream instance
1734 * @file: file to refer f_flags
1736 * Return: Zero if successful, or a negative error code.
1738 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1744 f_flags = file->f_flags;
1746 f_flags = substream->f_flags;
1748 snd_pcm_stream_lock_irq(substream);
1749 switch (substream->runtime->status->state) {
1750 case SNDRV_PCM_STATE_PAUSED:
1751 snd_pcm_pause(substream, 0);
1753 case SNDRV_PCM_STATE_SUSPENDED:
1754 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1757 snd_pcm_stream_unlock_irq(substream);
1759 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1760 substream, f_flags);
1767 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1769 struct snd_pcm_runtime *runtime = substream->runtime;
1770 switch (runtime->status->state) {
1771 case SNDRV_PCM_STATE_OPEN:
1772 case SNDRV_PCM_STATE_DISCONNECTED:
1773 case SNDRV_PCM_STATE_SUSPENDED:
1776 runtime->trigger_master = substream;
1780 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1782 struct snd_pcm_runtime *runtime = substream->runtime;
1783 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1784 switch (runtime->status->state) {
1785 case SNDRV_PCM_STATE_PREPARED:
1786 /* start playback stream if possible */
1787 if (! snd_pcm_playback_empty(substream)) {
1788 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1789 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1791 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1794 case SNDRV_PCM_STATE_RUNNING:
1795 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1797 case SNDRV_PCM_STATE_XRUN:
1798 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1804 /* stop running stream */
1805 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1806 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1807 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1808 snd_pcm_do_stop(substream, new_state);
1809 snd_pcm_post_stop(substream, new_state);
1813 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1814 runtime->trigger_master == substream &&
1815 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1816 return substream->ops->trigger(substream,
1817 SNDRV_PCM_TRIGGER_DRAIN);
1822 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1826 static const struct action_ops snd_pcm_action_drain_init = {
1827 .pre_action = snd_pcm_pre_drain_init,
1828 .do_action = snd_pcm_do_drain_init,
1829 .post_action = snd_pcm_post_drain_init
1833 * Drain the stream(s).
1834 * When the substream is linked, sync until the draining of all playback streams
1836 * After this call, all streams are supposed to be either SETUP or DRAINING
1837 * (capture only) state.
1839 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1842 struct snd_card *card;
1843 struct snd_pcm_runtime *runtime;
1844 struct snd_pcm_substream *s;
1845 struct snd_pcm_group *group;
1846 wait_queue_entry_t wait;
1850 card = substream->pcm->card;
1851 runtime = substream->runtime;
1853 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1857 if (file->f_flags & O_NONBLOCK)
1859 } else if (substream->f_flags & O_NONBLOCK)
1862 snd_pcm_stream_lock_irq(substream);
1864 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1865 snd_pcm_pause(substream, 0);
1867 /* pre-start/stop - all running streams are changed to DRAINING state */
1868 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1871 /* in non-blocking, we don't wait in ioctl but let caller poll */
1879 struct snd_pcm_runtime *to_check;
1880 if (signal_pending(current)) {
1881 result = -ERESTARTSYS;
1884 /* find a substream to drain */
1886 group = snd_pcm_stream_group_ref(substream);
1887 snd_pcm_group_for_each_entry(s, substream) {
1888 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1890 runtime = s->runtime;
1891 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1896 snd_pcm_group_unref(group, substream);
1898 break; /* all drained */
1899 init_waitqueue_entry(&wait, current);
1900 set_current_state(TASK_INTERRUPTIBLE);
1901 add_wait_queue(&to_check->sleep, &wait);
1902 snd_pcm_stream_unlock_irq(substream);
1903 if (runtime->no_period_wakeup)
1904 tout = MAX_SCHEDULE_TIMEOUT;
1907 if (runtime->rate) {
1908 long t = runtime->period_size * 2 / runtime->rate;
1909 tout = max(t, tout);
1911 tout = msecs_to_jiffies(tout * 1000);
1913 tout = schedule_timeout(tout);
1915 snd_pcm_stream_lock_irq(substream);
1916 group = snd_pcm_stream_group_ref(substream);
1917 snd_pcm_group_for_each_entry(s, substream) {
1918 if (s->runtime == to_check) {
1919 remove_wait_queue(&to_check->sleep, &wait);
1923 snd_pcm_group_unref(group, substream);
1925 if (card->shutdown) {
1930 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1933 dev_dbg(substream->pcm->card->dev,
1934 "playback drain error (DMA or IRQ trouble?)\n");
1935 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1943 snd_pcm_stream_unlock_irq(substream);
1951 * Immediately put all linked substreams into SETUP state.
1953 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1955 struct snd_pcm_runtime *runtime;
1958 if (PCM_RUNTIME_CHECK(substream))
1960 runtime = substream->runtime;
1962 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1963 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1966 snd_pcm_stream_lock_irq(substream);
1968 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1969 snd_pcm_pause(substream, 0);
1971 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1972 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1973 snd_pcm_stream_unlock_irq(substream);
1979 static bool is_pcm_file(struct file *file)
1981 struct inode *inode = file_inode(file);
1982 struct snd_pcm *pcm;
1985 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1987 minor = iminor(inode);
1988 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
1990 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1993 snd_card_unref(pcm->card);
2000 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2003 struct snd_pcm_file *pcm_file;
2004 struct snd_pcm_substream *substream1;
2005 struct snd_pcm_group *group, *target_group;
2006 bool nonatomic = substream->pcm->nonatomic;
2007 struct fd f = fdget(fd);
2011 if (!is_pcm_file(f.file)) {
2015 pcm_file = f.file->private_data;
2016 substream1 = pcm_file->substream;
2017 group = kzalloc(sizeof(*group), GFP_KERNEL);
2022 snd_pcm_group_init(group);
2024 down_write(&snd_pcm_link_rwsem);
2025 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2026 substream->runtime->status->state != substream1->runtime->status->state ||
2027 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2031 if (snd_pcm_stream_linked(substream1)) {
2036 snd_pcm_stream_lock_irq(substream);
2037 if (!snd_pcm_stream_linked(substream)) {
2038 snd_pcm_group_assign(substream, group);
2039 group = NULL; /* assigned, don't free this one below */
2041 target_group = substream->group;
2042 snd_pcm_stream_unlock_irq(substream);
2044 snd_pcm_group_lock_irq(target_group, nonatomic);
2045 snd_pcm_stream_lock(substream1);
2046 snd_pcm_group_assign(substream1, target_group);
2047 refcount_inc(&target_group->refs);
2048 snd_pcm_stream_unlock(substream1);
2049 snd_pcm_group_unlock_irq(target_group, nonatomic);
2051 up_write(&snd_pcm_link_rwsem);
2059 static void relink_to_local(struct snd_pcm_substream *substream)
2061 snd_pcm_stream_lock(substream);
2062 snd_pcm_group_assign(substream, &substream->self_group);
2063 snd_pcm_stream_unlock(substream);
2066 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2068 struct snd_pcm_group *group;
2069 bool nonatomic = substream->pcm->nonatomic;
2070 bool do_free = false;
2073 down_write(&snd_pcm_link_rwsem);
2075 if (!snd_pcm_stream_linked(substream)) {
2080 group = substream->group;
2081 snd_pcm_group_lock_irq(group, nonatomic);
2083 relink_to_local(substream);
2084 refcount_dec(&group->refs);
2086 /* detach the last stream, too */
2087 if (list_is_singular(&group->substreams)) {
2088 relink_to_local(list_first_entry(&group->substreams,
2089 struct snd_pcm_substream,
2091 do_free = refcount_dec_and_test(&group->refs);
2094 snd_pcm_group_unlock_irq(group, nonatomic);
2099 up_write(&snd_pcm_link_rwsem);
2106 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2107 struct snd_pcm_hw_rule *rule)
2109 struct snd_interval t;
2110 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2111 hw_param_interval_c(params, rule->deps[1]), &t);
2112 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2115 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2116 struct snd_pcm_hw_rule *rule)
2118 struct snd_interval t;
2119 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2120 hw_param_interval_c(params, rule->deps[1]), &t);
2121 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2124 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2125 struct snd_pcm_hw_rule *rule)
2127 struct snd_interval t;
2128 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2129 hw_param_interval_c(params, rule->deps[1]),
2130 (unsigned long) rule->private, &t);
2131 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2134 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2135 struct snd_pcm_hw_rule *rule)
2137 struct snd_interval t;
2138 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2139 (unsigned long) rule->private,
2140 hw_param_interval_c(params, rule->deps[1]), &t);
2141 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2144 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2145 struct snd_pcm_hw_rule *rule)
2148 const struct snd_interval *i =
2149 hw_param_interval_c(params, rule->deps[0]);
2151 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2153 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2155 if (! snd_mask_test(mask, k))
2157 bits = snd_pcm_format_physical_width(k);
2159 continue; /* ignore invalid formats */
2160 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2161 snd_mask_reset(&m, k);
2163 return snd_mask_refine(mask, &m);
2166 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2167 struct snd_pcm_hw_rule *rule)
2169 struct snd_interval t;
2175 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2177 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2179 bits = snd_pcm_format_physical_width(k);
2181 continue; /* ignore invalid formats */
2182 if (t.min > (unsigned)bits)
2184 if (t.max < (unsigned)bits)
2188 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2191 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2192 #error "Change this table"
2195 static const unsigned int rates[] = {
2196 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2197 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2200 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2201 .count = ARRAY_SIZE(rates),
2205 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2206 struct snd_pcm_hw_rule *rule)
2208 struct snd_pcm_hardware *hw = rule->private;
2209 return snd_interval_list(hw_param_interval(params, rule->var),
2210 snd_pcm_known_rates.count,
2211 snd_pcm_known_rates.list, hw->rates);
2214 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2215 struct snd_pcm_hw_rule *rule)
2217 struct snd_interval t;
2218 struct snd_pcm_substream *substream = rule->private;
2220 t.max = substream->buffer_bytes_max;
2224 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2227 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2229 struct snd_pcm_runtime *runtime = substream->runtime;
2230 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2233 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2234 snd_mask_any(constrs_mask(constrs, k));
2237 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2238 snd_interval_any(constrs_interval(constrs, k));
2241 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2242 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2243 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2244 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2245 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2247 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2248 snd_pcm_hw_rule_format, NULL,
2249 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2252 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2253 snd_pcm_hw_rule_sample_bits, NULL,
2254 SNDRV_PCM_HW_PARAM_FORMAT,
2255 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2258 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2259 snd_pcm_hw_rule_div, NULL,
2260 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2263 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2264 snd_pcm_hw_rule_mul, NULL,
2265 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2268 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2269 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2270 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2273 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2274 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2275 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2278 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2279 snd_pcm_hw_rule_div, NULL,
2280 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2283 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2284 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2285 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2288 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2289 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2290 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2293 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2294 snd_pcm_hw_rule_div, NULL,
2295 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2298 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2299 snd_pcm_hw_rule_div, NULL,
2300 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2303 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2304 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2305 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2308 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2309 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2310 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2313 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2314 snd_pcm_hw_rule_mul, NULL,
2315 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2318 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2319 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2320 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2323 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2324 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2325 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2328 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2329 snd_pcm_hw_rule_muldivk, (void*) 8,
2330 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2333 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2334 snd_pcm_hw_rule_muldivk, (void*) 8,
2335 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2338 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2339 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2340 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2343 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2344 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2345 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2351 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2353 struct snd_pcm_runtime *runtime = substream->runtime;
2354 struct snd_pcm_hardware *hw = &runtime->hw;
2356 unsigned int mask = 0;
2358 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2359 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2360 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2361 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2362 if (hw_support_mmap(substream)) {
2363 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2364 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2365 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2366 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2367 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2368 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2370 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2374 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2378 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2382 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2383 hw->channels_min, hw->channels_max);
2387 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2388 hw->rate_min, hw->rate_max);
2392 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2393 hw->period_bytes_min, hw->period_bytes_max);
2397 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2398 hw->periods_min, hw->periods_max);
2402 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2403 hw->period_bytes_min, hw->buffer_bytes_max);
2407 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2408 snd_pcm_hw_rule_buffer_bytes_max, substream,
2409 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2414 if (runtime->dma_bytes) {
2415 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2420 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2421 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2422 snd_pcm_hw_rule_rate, hw,
2423 SNDRV_PCM_HW_PARAM_RATE, -1);
2428 /* FIXME: this belong to lowlevel */
2429 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2434 static void pcm_release_private(struct snd_pcm_substream *substream)
2436 if (snd_pcm_stream_linked(substream))
2437 snd_pcm_unlink(substream);
2440 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2442 substream->ref_count--;
2443 if (substream->ref_count > 0)
2446 snd_pcm_drop(substream);
2447 if (substream->hw_opened) {
2448 if (substream->ops->hw_free &&
2449 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2450 substream->ops->hw_free(substream);
2451 substream->ops->close(substream);
2452 substream->hw_opened = 0;
2454 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2455 pm_qos_remove_request(&substream->latency_pm_qos_req);
2456 if (substream->pcm_release) {
2457 substream->pcm_release(substream);
2458 substream->pcm_release = NULL;
2460 snd_pcm_detach_substream(substream);
2462 EXPORT_SYMBOL(snd_pcm_release_substream);
2464 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2466 struct snd_pcm_substream **rsubstream)
2468 struct snd_pcm_substream *substream;
2471 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2474 if (substream->ref_count > 1) {
2475 *rsubstream = substream;
2479 err = snd_pcm_hw_constraints_init(substream);
2481 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2485 if ((err = substream->ops->open(substream)) < 0)
2488 substream->hw_opened = 1;
2490 err = snd_pcm_hw_constraints_complete(substream);
2492 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2496 *rsubstream = substream;
2500 snd_pcm_release_substream(substream);
2503 EXPORT_SYMBOL(snd_pcm_open_substream);
2505 static int snd_pcm_open_file(struct file *file,
2506 struct snd_pcm *pcm,
2509 struct snd_pcm_file *pcm_file;
2510 struct snd_pcm_substream *substream;
2513 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2517 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2518 if (pcm_file == NULL) {
2519 snd_pcm_release_substream(substream);
2522 pcm_file->substream = substream;
2523 if (substream->ref_count == 1)
2524 substream->pcm_release = pcm_release_private;
2525 file->private_data = pcm_file;
2530 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2532 struct snd_pcm *pcm;
2533 int err = nonseekable_open(inode, file);
2536 pcm = snd_lookup_minor_data(iminor(inode),
2537 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2538 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2540 snd_card_unref(pcm->card);
2544 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2546 struct snd_pcm *pcm;
2547 int err = nonseekable_open(inode, file);
2550 pcm = snd_lookup_minor_data(iminor(inode),
2551 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2552 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2554 snd_card_unref(pcm->card);
2558 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2561 wait_queue_entry_t wait;
2567 err = snd_card_file_add(pcm->card, file);
2570 if (!try_module_get(pcm->card->module)) {
2574 init_waitqueue_entry(&wait, current);
2575 add_wait_queue(&pcm->open_wait, &wait);
2576 mutex_lock(&pcm->open_mutex);
2578 err = snd_pcm_open_file(file, pcm, stream);
2581 if (err == -EAGAIN) {
2582 if (file->f_flags & O_NONBLOCK) {
2588 set_current_state(TASK_INTERRUPTIBLE);
2589 mutex_unlock(&pcm->open_mutex);
2591 mutex_lock(&pcm->open_mutex);
2592 if (pcm->card->shutdown) {
2596 if (signal_pending(current)) {
2601 remove_wait_queue(&pcm->open_wait, &wait);
2602 mutex_unlock(&pcm->open_mutex);
2608 module_put(pcm->card->module);
2610 snd_card_file_remove(pcm->card, file);
2615 static int snd_pcm_release(struct inode *inode, struct file *file)
2617 struct snd_pcm *pcm;
2618 struct snd_pcm_substream *substream;
2619 struct snd_pcm_file *pcm_file;
2621 pcm_file = file->private_data;
2622 substream = pcm_file->substream;
2623 if (snd_BUG_ON(!substream))
2625 pcm = substream->pcm;
2626 mutex_lock(&pcm->open_mutex);
2627 snd_pcm_release_substream(substream);
2629 mutex_unlock(&pcm->open_mutex);
2630 wake_up(&pcm->open_wait);
2631 module_put(pcm->card->module);
2632 snd_card_file_remove(pcm->card, file);
2636 /* check and update PCM state; return 0 or a negative error
2637 * call this inside PCM lock
2639 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2641 switch (substream->runtime->status->state) {
2642 case SNDRV_PCM_STATE_DRAINING:
2643 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2646 case SNDRV_PCM_STATE_RUNNING:
2647 return snd_pcm_update_hw_ptr(substream);
2648 case SNDRV_PCM_STATE_PREPARED:
2649 case SNDRV_PCM_STATE_PAUSED:
2651 case SNDRV_PCM_STATE_SUSPENDED:
2653 case SNDRV_PCM_STATE_XRUN:
2660 /* increase the appl_ptr; returns the processed frames or a negative error */
2661 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2662 snd_pcm_uframes_t frames,
2663 snd_pcm_sframes_t avail)
2665 struct snd_pcm_runtime *runtime = substream->runtime;
2666 snd_pcm_sframes_t appl_ptr;
2671 if (frames > (snd_pcm_uframes_t)avail)
2673 appl_ptr = runtime->control->appl_ptr + frames;
2674 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2675 appl_ptr -= runtime->boundary;
2676 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2677 return ret < 0 ? ret : frames;
2680 /* decrease the appl_ptr; returns the processed frames or zero for error */
2681 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2682 snd_pcm_uframes_t frames,
2683 snd_pcm_sframes_t avail)
2685 struct snd_pcm_runtime *runtime = substream->runtime;
2686 snd_pcm_sframes_t appl_ptr;
2691 if (frames > (snd_pcm_uframes_t)avail)
2693 appl_ptr = runtime->control->appl_ptr - frames;
2695 appl_ptr += runtime->boundary;
2696 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2697 /* NOTE: we return zero for errors because PulseAudio gets depressed
2698 * upon receiving an error from rewind ioctl and stops processing
2699 * any longer. Returning zero means that no rewind is done, so
2700 * it's not absolutely wrong to answer like that.
2702 return ret < 0 ? 0 : frames;
2705 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2706 snd_pcm_uframes_t frames)
2708 snd_pcm_sframes_t ret;
2713 snd_pcm_stream_lock_irq(substream);
2714 ret = do_pcm_hwsync(substream);
2716 ret = rewind_appl_ptr(substream, frames,
2717 snd_pcm_hw_avail(substream));
2718 snd_pcm_stream_unlock_irq(substream);
2722 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2723 snd_pcm_uframes_t frames)
2725 snd_pcm_sframes_t ret;
2730 snd_pcm_stream_lock_irq(substream);
2731 ret = do_pcm_hwsync(substream);
2733 ret = forward_appl_ptr(substream, frames,
2734 snd_pcm_avail(substream));
2735 snd_pcm_stream_unlock_irq(substream);
2739 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2743 snd_pcm_stream_lock_irq(substream);
2744 err = do_pcm_hwsync(substream);
2745 snd_pcm_stream_unlock_irq(substream);
2749 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2750 snd_pcm_sframes_t *delay)
2753 snd_pcm_sframes_t n = 0;
2755 snd_pcm_stream_lock_irq(substream);
2756 err = do_pcm_hwsync(substream);
2758 n = snd_pcm_calc_delay(substream);
2759 snd_pcm_stream_unlock_irq(substream);
2765 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2766 struct snd_pcm_sync_ptr __user *_sync_ptr)
2768 struct snd_pcm_runtime *runtime = substream->runtime;
2769 struct snd_pcm_sync_ptr sync_ptr;
2770 volatile struct snd_pcm_mmap_status *status;
2771 volatile struct snd_pcm_mmap_control *control;
2774 memset(&sync_ptr, 0, sizeof(sync_ptr));
2775 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2777 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2779 status = runtime->status;
2780 control = runtime->control;
2781 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2782 err = snd_pcm_hwsync(substream);
2786 snd_pcm_stream_lock_irq(substream);
2787 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2788 err = pcm_lib_apply_appl_ptr(substream,
2789 sync_ptr.c.control.appl_ptr);
2791 snd_pcm_stream_unlock_irq(substream);
2795 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2797 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2798 control->avail_min = sync_ptr.c.control.avail_min;
2800 sync_ptr.c.control.avail_min = control->avail_min;
2801 sync_ptr.s.status.state = status->state;
2802 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2803 sync_ptr.s.status.tstamp = status->tstamp;
2804 sync_ptr.s.status.suspended_state = status->suspended_state;
2805 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2806 snd_pcm_stream_unlock_irq(substream);
2807 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2812 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2814 struct snd_pcm_runtime *runtime = substream->runtime;
2817 if (get_user(arg, _arg))
2819 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2821 runtime->tstamp_type = arg;
2825 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2826 struct snd_xferi __user *_xferi)
2828 struct snd_xferi xferi;
2829 struct snd_pcm_runtime *runtime = substream->runtime;
2830 snd_pcm_sframes_t result;
2832 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2834 if (put_user(0, &_xferi->result))
2836 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2838 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2839 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2841 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2842 __put_user(result, &_xferi->result);
2843 return result < 0 ? result : 0;
2846 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2847 struct snd_xfern __user *_xfern)
2849 struct snd_xfern xfern;
2850 struct snd_pcm_runtime *runtime = substream->runtime;
2852 snd_pcm_sframes_t result;
2854 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2856 if (runtime->channels > 128)
2858 if (put_user(0, &_xfern->result))
2860 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2863 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2865 return PTR_ERR(bufs);
2866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2867 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2869 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2871 __put_user(result, &_xfern->result);
2872 return result < 0 ? result : 0;
2875 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2876 snd_pcm_uframes_t __user *_frames)
2878 snd_pcm_uframes_t frames;
2879 snd_pcm_sframes_t result;
2881 if (get_user(frames, _frames))
2883 if (put_user(0, _frames))
2885 result = snd_pcm_rewind(substream, frames);
2886 __put_user(result, _frames);
2887 return result < 0 ? result : 0;
2890 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2891 snd_pcm_uframes_t __user *_frames)
2893 snd_pcm_uframes_t frames;
2894 snd_pcm_sframes_t result;
2896 if (get_user(frames, _frames))
2898 if (put_user(0, _frames))
2900 result = snd_pcm_forward(substream, frames);
2901 __put_user(result, _frames);
2902 return result < 0 ? result : 0;
2905 static int snd_pcm_common_ioctl(struct file *file,
2906 struct snd_pcm_substream *substream,
2907 unsigned int cmd, void __user *arg)
2909 struct snd_pcm_file *pcm_file = file->private_data;
2912 if (PCM_RUNTIME_CHECK(substream))
2915 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2920 case SNDRV_PCM_IOCTL_PVERSION:
2921 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2922 case SNDRV_PCM_IOCTL_INFO:
2923 return snd_pcm_info_user(substream, arg);
2924 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2926 case SNDRV_PCM_IOCTL_TTSTAMP:
2927 return snd_pcm_tstamp(substream, arg);
2928 case SNDRV_PCM_IOCTL_USER_PVERSION:
2929 if (get_user(pcm_file->user_pversion,
2930 (unsigned int __user *)arg))
2933 case SNDRV_PCM_IOCTL_HW_REFINE:
2934 return snd_pcm_hw_refine_user(substream, arg);
2935 case SNDRV_PCM_IOCTL_HW_PARAMS:
2936 return snd_pcm_hw_params_user(substream, arg);
2937 case SNDRV_PCM_IOCTL_HW_FREE:
2938 return snd_pcm_hw_free(substream);
2939 case SNDRV_PCM_IOCTL_SW_PARAMS:
2940 return snd_pcm_sw_params_user(substream, arg);
2941 case SNDRV_PCM_IOCTL_STATUS:
2942 return snd_pcm_status_user(substream, arg, false);
2943 case SNDRV_PCM_IOCTL_STATUS_EXT:
2944 return snd_pcm_status_user(substream, arg, true);
2945 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2946 return snd_pcm_channel_info_user(substream, arg);
2947 case SNDRV_PCM_IOCTL_PREPARE:
2948 return snd_pcm_prepare(substream, file);
2949 case SNDRV_PCM_IOCTL_RESET:
2950 return snd_pcm_reset(substream);
2951 case SNDRV_PCM_IOCTL_START:
2952 return snd_pcm_start_lock_irq(substream);
2953 case SNDRV_PCM_IOCTL_LINK:
2954 return snd_pcm_link(substream, (int)(unsigned long) arg);
2955 case SNDRV_PCM_IOCTL_UNLINK:
2956 return snd_pcm_unlink(substream);
2957 case SNDRV_PCM_IOCTL_RESUME:
2958 return snd_pcm_resume(substream);
2959 case SNDRV_PCM_IOCTL_XRUN:
2960 return snd_pcm_xrun(substream);
2961 case SNDRV_PCM_IOCTL_HWSYNC:
2962 return snd_pcm_hwsync(substream);
2963 case SNDRV_PCM_IOCTL_DELAY:
2965 snd_pcm_sframes_t delay;
2966 snd_pcm_sframes_t __user *res = arg;
2969 err = snd_pcm_delay(substream, &delay);
2972 if (put_user(delay, res))
2976 case SNDRV_PCM_IOCTL_SYNC_PTR:
2977 return snd_pcm_sync_ptr(substream, arg);
2978 #ifdef CONFIG_SND_SUPPORT_OLD_API
2979 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2980 return snd_pcm_hw_refine_old_user(substream, arg);
2981 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2982 return snd_pcm_hw_params_old_user(substream, arg);
2984 case SNDRV_PCM_IOCTL_DRAIN:
2985 return snd_pcm_drain(substream, file);
2986 case SNDRV_PCM_IOCTL_DROP:
2987 return snd_pcm_drop(substream);
2988 case SNDRV_PCM_IOCTL_PAUSE:
2989 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
2991 (int)(unsigned long)arg);
2992 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2993 case SNDRV_PCM_IOCTL_READI_FRAMES:
2994 return snd_pcm_xferi_frames_ioctl(substream, arg);
2995 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2996 case SNDRV_PCM_IOCTL_READN_FRAMES:
2997 return snd_pcm_xfern_frames_ioctl(substream, arg);
2998 case SNDRV_PCM_IOCTL_REWIND:
2999 return snd_pcm_rewind_ioctl(substream, arg);
3000 case SNDRV_PCM_IOCTL_FORWARD:
3001 return snd_pcm_forward_ioctl(substream, arg);
3003 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3007 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3010 struct snd_pcm_file *pcm_file;
3012 pcm_file = file->private_data;
3014 if (((cmd >> 8) & 0xff) != 'A')
3017 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3018 (void __user *)arg);
3022 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3023 * @substream: PCM substream
3025 * @arg: IOCTL argument
3027 * The function is provided primarily for OSS layer and USB gadget drivers,
3028 * and it allows only the limited set of ioctls (hw_params, sw_params,
3029 * prepare, start, drain, drop, forward).
3031 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3032 unsigned int cmd, void *arg)
3034 snd_pcm_uframes_t *frames = arg;
3035 snd_pcm_sframes_t result;
3038 case SNDRV_PCM_IOCTL_FORWARD:
3040 /* provided only for OSS; capture-only and no value returned */
3041 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3043 result = snd_pcm_forward(substream, *frames);
3044 return result < 0 ? result : 0;
3046 case SNDRV_PCM_IOCTL_HW_PARAMS:
3047 return snd_pcm_hw_params(substream, arg);
3048 case SNDRV_PCM_IOCTL_SW_PARAMS:
3049 return snd_pcm_sw_params(substream, arg);
3050 case SNDRV_PCM_IOCTL_PREPARE:
3051 return snd_pcm_prepare(substream, NULL);
3052 case SNDRV_PCM_IOCTL_START:
3053 return snd_pcm_start_lock_irq(substream);
3054 case SNDRV_PCM_IOCTL_DRAIN:
3055 return snd_pcm_drain(substream, NULL);
3056 case SNDRV_PCM_IOCTL_DROP:
3057 return snd_pcm_drop(substream);
3058 case SNDRV_PCM_IOCTL_DELAY:
3059 return snd_pcm_delay(substream, frames);
3064 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3066 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3069 struct snd_pcm_file *pcm_file;
3070 struct snd_pcm_substream *substream;
3071 struct snd_pcm_runtime *runtime;
3072 snd_pcm_sframes_t result;
3074 pcm_file = file->private_data;
3075 substream = pcm_file->substream;
3076 if (PCM_RUNTIME_CHECK(substream))
3078 runtime = substream->runtime;
3079 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3081 if (!frame_aligned(runtime, count))
3083 count = bytes_to_frames(runtime, count);
3084 result = snd_pcm_lib_read(substream, buf, count);
3086 result = frames_to_bytes(runtime, result);
3090 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3091 size_t count, loff_t * offset)
3093 struct snd_pcm_file *pcm_file;
3094 struct snd_pcm_substream *substream;
3095 struct snd_pcm_runtime *runtime;
3096 snd_pcm_sframes_t result;
3098 pcm_file = file->private_data;
3099 substream = pcm_file->substream;
3100 if (PCM_RUNTIME_CHECK(substream))
3102 runtime = substream->runtime;
3103 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3105 if (!frame_aligned(runtime, count))
3107 count = bytes_to_frames(runtime, count);
3108 result = snd_pcm_lib_write(substream, buf, count);
3110 result = frames_to_bytes(runtime, result);
3114 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3116 struct snd_pcm_file *pcm_file;
3117 struct snd_pcm_substream *substream;
3118 struct snd_pcm_runtime *runtime;
3119 snd_pcm_sframes_t result;
3122 snd_pcm_uframes_t frames;
3124 pcm_file = iocb->ki_filp->private_data;
3125 substream = pcm_file->substream;
3126 if (PCM_RUNTIME_CHECK(substream))
3128 runtime = substream->runtime;
3129 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3131 if (!iter_is_iovec(to))
3133 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3135 if (!frame_aligned(runtime, to->iov->iov_len))
3137 frames = bytes_to_samples(runtime, to->iov->iov_len);
3138 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3141 for (i = 0; i < to->nr_segs; ++i)
3142 bufs[i] = to->iov[i].iov_base;
3143 result = snd_pcm_lib_readv(substream, bufs, frames);
3145 result = frames_to_bytes(runtime, result);
3150 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3152 struct snd_pcm_file *pcm_file;
3153 struct snd_pcm_substream *substream;
3154 struct snd_pcm_runtime *runtime;
3155 snd_pcm_sframes_t result;
3158 snd_pcm_uframes_t frames;
3160 pcm_file = iocb->ki_filp->private_data;
3161 substream = pcm_file->substream;
3162 if (PCM_RUNTIME_CHECK(substream))
3164 runtime = substream->runtime;
3165 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3167 if (!iter_is_iovec(from))
3169 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3170 !frame_aligned(runtime, from->iov->iov_len))
3172 frames = bytes_to_samples(runtime, from->iov->iov_len);
3173 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3176 for (i = 0; i < from->nr_segs; ++i)
3177 bufs[i] = from->iov[i].iov_base;
3178 result = snd_pcm_lib_writev(substream, bufs, frames);
3180 result = frames_to_bytes(runtime, result);
3185 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3187 struct snd_pcm_file *pcm_file;
3188 struct snd_pcm_substream *substream;
3189 struct snd_pcm_runtime *runtime;
3191 snd_pcm_uframes_t avail;
3193 pcm_file = file->private_data;
3195 substream = pcm_file->substream;
3196 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3197 ok = EPOLLOUT | EPOLLWRNORM;
3199 ok = EPOLLIN | EPOLLRDNORM;
3200 if (PCM_RUNTIME_CHECK(substream))
3201 return ok | EPOLLERR;
3203 runtime = substream->runtime;
3204 poll_wait(file, &runtime->sleep, wait);
3207 snd_pcm_stream_lock_irq(substream);
3208 avail = snd_pcm_avail(substream);
3209 switch (runtime->status->state) {
3210 case SNDRV_PCM_STATE_RUNNING:
3211 case SNDRV_PCM_STATE_PREPARED:
3212 case SNDRV_PCM_STATE_PAUSED:
3213 if (avail >= runtime->control->avail_min)
3216 case SNDRV_PCM_STATE_DRAINING:
3217 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3224 mask = ok | EPOLLERR;
3227 snd_pcm_stream_unlock_irq(substream);
3236 * Only on coherent architectures, we can mmap the status and the control records
3237 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3239 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3241 * mmap status record
3243 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3245 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3246 struct snd_pcm_runtime *runtime;
3248 if (substream == NULL)
3249 return VM_FAULT_SIGBUS;
3250 runtime = substream->runtime;
3251 vmf->page = virt_to_page(runtime->status);
3252 get_page(vmf->page);
3256 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3258 .fault = snd_pcm_mmap_status_fault,
3261 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3262 struct vm_area_struct *area)
3265 if (!(area->vm_flags & VM_READ))
3267 size = area->vm_end - area->vm_start;
3268 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3270 area->vm_ops = &snd_pcm_vm_ops_status;
3271 area->vm_private_data = substream;
3272 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3277 * mmap control record
3279 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3281 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3282 struct snd_pcm_runtime *runtime;
3284 if (substream == NULL)
3285 return VM_FAULT_SIGBUS;
3286 runtime = substream->runtime;
3287 vmf->page = virt_to_page(runtime->control);
3288 get_page(vmf->page);
3292 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3294 .fault = snd_pcm_mmap_control_fault,
3297 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3298 struct vm_area_struct *area)
3301 if (!(area->vm_flags & VM_READ))
3303 size = area->vm_end - area->vm_start;
3304 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3306 area->vm_ops = &snd_pcm_vm_ops_control;
3307 area->vm_private_data = substream;
3308 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3312 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3314 if (pcm_file->no_compat_mmap)
3316 /* See pcm_control_mmap_allowed() below.
3317 * Since older alsa-lib requires both status and control mmaps to be
3318 * coupled, we have to disable the status mmap for old alsa-lib, too.
3320 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3321 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3326 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3328 if (pcm_file->no_compat_mmap)
3330 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3331 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3332 * thus it effectively assures the manual update of appl_ptr.
3334 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3339 #else /* ! coherent mmap */
3341 * don't support mmap for status and control records.
3343 #define pcm_status_mmap_allowed(pcm_file) false
3344 #define pcm_control_mmap_allowed(pcm_file) false
3346 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3347 struct vm_area_struct *area)
3351 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3352 struct vm_area_struct *area)
3356 #endif /* coherent mmap */
3358 static inline struct page *
3359 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3361 void *vaddr = substream->runtime->dma_area + ofs;
3363 switch (substream->dma_buffer.dev.type) {
3364 #ifdef CONFIG_SND_DMA_SGBUF
3365 case SNDRV_DMA_TYPE_DEV_SG:
3366 case SNDRV_DMA_TYPE_DEV_UC_SG:
3367 return snd_pcm_sgbuf_ops_page(substream, ofs);
3368 #endif /* CONFIG_SND_DMA_SGBUF */
3369 case SNDRV_DMA_TYPE_VMALLOC:
3370 return vmalloc_to_page(vaddr);
3372 return virt_to_page(vaddr);
3377 * fault callback for mmapping a RAM page
3379 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3381 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3382 struct snd_pcm_runtime *runtime;
3383 unsigned long offset;
3387 if (substream == NULL)
3388 return VM_FAULT_SIGBUS;
3389 runtime = substream->runtime;
3390 offset = vmf->pgoff << PAGE_SHIFT;
3391 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3392 if (offset > dma_bytes - PAGE_SIZE)
3393 return VM_FAULT_SIGBUS;
3394 if (substream->ops->page)
3395 page = substream->ops->page(substream, offset);
3397 page = snd_pcm_default_page_ops(substream, offset);
3399 return VM_FAULT_SIGBUS;
3405 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3406 .open = snd_pcm_mmap_data_open,
3407 .close = snd_pcm_mmap_data_close,
3410 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3411 .open = snd_pcm_mmap_data_open,
3412 .close = snd_pcm_mmap_data_close,
3413 .fault = snd_pcm_mmap_data_fault,
3417 * mmap the DMA buffer on RAM
3421 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3422 * @substream: PCM substream
3425 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3426 * this function is invoked implicitly.
3428 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3429 struct vm_area_struct *area)
3431 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3432 #ifdef CONFIG_GENERIC_ALLOCATOR
3433 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3434 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3435 return remap_pfn_range(area, area->vm_start,
3436 substream->dma_buffer.addr >> PAGE_SHIFT,
3437 area->vm_end - area->vm_start, area->vm_page_prot);
3439 #endif /* CONFIG_GENERIC_ALLOCATOR */
3440 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3441 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3442 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3443 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3444 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3446 substream->runtime->dma_area,
3447 substream->runtime->dma_addr,
3448 substream->runtime->dma_bytes);
3449 #endif /* CONFIG_X86 */
3450 /* mmap with fault handler */
3451 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3454 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3457 * mmap the DMA buffer on I/O memory area
3459 #if SNDRV_PCM_INFO_MMAP_IOMEM
3461 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3462 * @substream: PCM substream
3465 * When your hardware uses the iomapped pages as the hardware buffer and
3466 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3467 * is supposed to work only on limited architectures.
3469 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3470 struct vm_area_struct *area)
3472 struct snd_pcm_runtime *runtime = substream->runtime;
3474 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3475 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3477 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3478 #endif /* SNDRV_PCM_INFO_MMAP */
3483 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3484 struct vm_area_struct *area)
3486 struct snd_pcm_runtime *runtime;
3488 unsigned long offset;
3492 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3493 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3496 if (!(area->vm_flags & VM_READ))
3499 runtime = substream->runtime;
3500 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3502 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3504 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3505 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3507 size = area->vm_end - area->vm_start;
3508 offset = area->vm_pgoff << PAGE_SHIFT;
3509 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3510 if ((size_t)size > dma_bytes)
3512 if (offset > dma_bytes - size)
3515 area->vm_ops = &snd_pcm_vm_ops_data;
3516 area->vm_private_data = substream;
3517 if (substream->ops->mmap)
3518 err = substream->ops->mmap(substream, area);
3520 err = snd_pcm_lib_default_mmap(substream, area);
3522 atomic_inc(&substream->mmap_count);
3525 EXPORT_SYMBOL(snd_pcm_mmap_data);
3527 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3529 struct snd_pcm_file * pcm_file;
3530 struct snd_pcm_substream *substream;
3531 unsigned long offset;
3533 pcm_file = file->private_data;
3534 substream = pcm_file->substream;
3535 if (PCM_RUNTIME_CHECK(substream))
3538 offset = area->vm_pgoff << PAGE_SHIFT;
3540 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3541 if (!pcm_status_mmap_allowed(pcm_file))
3543 return snd_pcm_mmap_status(substream, file, area);
3544 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3545 if (!pcm_control_mmap_allowed(pcm_file))
3547 return snd_pcm_mmap_control(substream, file, area);
3549 return snd_pcm_mmap_data(substream, file, area);
3554 static int snd_pcm_fasync(int fd, struct file * file, int on)
3556 struct snd_pcm_file * pcm_file;
3557 struct snd_pcm_substream *substream;
3558 struct snd_pcm_runtime *runtime;
3560 pcm_file = file->private_data;
3561 substream = pcm_file->substream;
3562 if (PCM_RUNTIME_CHECK(substream))
3564 runtime = substream->runtime;
3565 return fasync_helper(fd, file, on, &runtime->fasync);
3571 #ifdef CONFIG_COMPAT
3572 #include "pcm_compat.c"
3574 #define snd_pcm_ioctl_compat NULL
3578 * To be removed helpers to keep binary compatibility
3581 #ifdef CONFIG_SND_SUPPORT_OLD_API
3582 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3583 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3585 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3586 struct snd_pcm_hw_params_old *oparams)
3590 memset(params, 0, sizeof(*params));
3591 params->flags = oparams->flags;
3592 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3593 params->masks[i].bits[0] = oparams->masks[i];
3594 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3595 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3596 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3597 params->info = oparams->info;
3598 params->msbits = oparams->msbits;
3599 params->rate_num = oparams->rate_num;
3600 params->rate_den = oparams->rate_den;
3601 params->fifo_size = oparams->fifo_size;
3604 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3605 struct snd_pcm_hw_params *params)
3609 memset(oparams, 0, sizeof(*oparams));
3610 oparams->flags = params->flags;
3611 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3612 oparams->masks[i] = params->masks[i].bits[0];
3613 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3614 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3615 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3616 oparams->info = params->info;
3617 oparams->msbits = params->msbits;
3618 oparams->rate_num = params->rate_num;
3619 oparams->rate_den = params->rate_den;
3620 oparams->fifo_size = params->fifo_size;
3623 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3624 struct snd_pcm_hw_params_old __user * _oparams)
3626 struct snd_pcm_hw_params *params;
3627 struct snd_pcm_hw_params_old *oparams = NULL;
3630 params = kmalloc(sizeof(*params), GFP_KERNEL);
3634 oparams = memdup_user(_oparams, sizeof(*oparams));
3635 if (IS_ERR(oparams)) {
3636 err = PTR_ERR(oparams);
3639 snd_pcm_hw_convert_from_old_params(params, oparams);
3640 err = snd_pcm_hw_refine(substream, params);
3644 err = fixup_unreferenced_params(substream, params);
3648 snd_pcm_hw_convert_to_old_params(oparams, params);
3649 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3658 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3659 struct snd_pcm_hw_params_old __user * _oparams)
3661 struct snd_pcm_hw_params *params;
3662 struct snd_pcm_hw_params_old *oparams = NULL;
3665 params = kmalloc(sizeof(*params), GFP_KERNEL);
3669 oparams = memdup_user(_oparams, sizeof(*oparams));
3670 if (IS_ERR(oparams)) {
3671 err = PTR_ERR(oparams);
3675 snd_pcm_hw_convert_from_old_params(params, oparams);
3676 err = snd_pcm_hw_params(substream, params);
3680 snd_pcm_hw_convert_to_old_params(oparams, params);
3681 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3689 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3692 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3695 unsigned long pgoff,
3696 unsigned long flags)
3698 struct snd_pcm_file *pcm_file = file->private_data;
3699 struct snd_pcm_substream *substream = pcm_file->substream;
3700 struct snd_pcm_runtime *runtime = substream->runtime;
3701 unsigned long offset = pgoff << PAGE_SHIFT;
3704 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3705 return (unsigned long)runtime->status;
3706 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3707 return (unsigned long)runtime->control;
3709 return (unsigned long)runtime->dma_area + offset;
3713 # define snd_pcm_get_unmapped_area NULL
3720 const struct file_operations snd_pcm_f_ops[2] = {
3722 .owner = THIS_MODULE,
3723 .write = snd_pcm_write,
3724 .write_iter = snd_pcm_writev,
3725 .open = snd_pcm_playback_open,
3726 .release = snd_pcm_release,
3727 .llseek = no_llseek,
3728 .poll = snd_pcm_poll,
3729 .unlocked_ioctl = snd_pcm_ioctl,
3730 .compat_ioctl = snd_pcm_ioctl_compat,
3731 .mmap = snd_pcm_mmap,
3732 .fasync = snd_pcm_fasync,
3733 .get_unmapped_area = snd_pcm_get_unmapped_area,
3736 .owner = THIS_MODULE,
3737 .read = snd_pcm_read,
3738 .read_iter = snd_pcm_readv,
3739 .open = snd_pcm_capture_open,
3740 .release = snd_pcm_release,
3741 .llseek = no_llseek,
3742 .poll = snd_pcm_poll,
3743 .unlocked_ioctl = snd_pcm_ioctl,
3744 .compat_ioctl = snd_pcm_ioctl_compat,
3745 .mmap = snd_pcm_mmap,
3746 .fasync = snd_pcm_fasync,
3747 .get_unmapped_area = snd_pcm_get_unmapped_area,