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 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
183 struct snd_pcm *pcm = substream->pcm;
184 struct snd_pcm_str *pstr = substream->pstr;
186 memset(info, 0, sizeof(*info));
187 info->card = pcm->card->number;
188 info->device = pcm->device;
189 info->stream = substream->stream;
190 info->subdevice = substream->number;
191 strlcpy(info->id, pcm->id, sizeof(info->id));
192 strlcpy(info->name, pcm->name, sizeof(info->name));
193 info->dev_class = pcm->dev_class;
194 info->dev_subclass = pcm->dev_subclass;
195 info->subdevices_count = pstr->substream_count;
196 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
197 strlcpy(info->subname, substream->name, sizeof(info->subname));
202 int snd_pcm_info_user(struct snd_pcm_substream *substream,
203 struct snd_pcm_info __user * _info)
205 struct snd_pcm_info *info;
208 info = kmalloc(sizeof(*info), GFP_KERNEL);
211 err = snd_pcm_info(substream, info);
213 if (copy_to_user(_info, info, sizeof(*info)))
220 static bool hw_support_mmap(struct snd_pcm_substream *substream)
222 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
225 if (substream->ops->mmap ||
226 (substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV &&
227 substream->dma_buffer.dev.type != SNDRV_DMA_TYPE_DEV_UC))
230 return dma_can_mmap(substream->dma_buffer.dev.dev);
233 static int constrain_mask_params(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
236 struct snd_pcm_hw_constraints *constrs =
237 &substream->runtime->hw_constraints;
240 struct snd_mask old_mask;
243 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
244 m = hw_param_mask(params, k);
245 if (snd_mask_empty(m))
248 /* This parameter is not requested to change by a caller. */
249 if (!(params->rmask & (1 << k)))
252 if (trace_hw_mask_param_enabled())
255 changed = snd_mask_refine(m, constrs_mask(constrs, k));
261 /* Set corresponding flag so that the caller gets it. */
262 trace_hw_mask_param(substream, k, 0, &old_mask, m);
263 params->cmask |= 1 << k;
269 static int constrain_interval_params(struct snd_pcm_substream *substream,
270 struct snd_pcm_hw_params *params)
272 struct snd_pcm_hw_constraints *constrs =
273 &substream->runtime->hw_constraints;
274 struct snd_interval *i;
276 struct snd_interval old_interval;
279 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
280 i = hw_param_interval(params, k);
281 if (snd_interval_empty(i))
284 /* This parameter is not requested to change by a caller. */
285 if (!(params->rmask & (1 << k)))
288 if (trace_hw_interval_param_enabled())
291 changed = snd_interval_refine(i, constrs_interval(constrs, k));
297 /* Set corresponding flag so that the caller gets it. */
298 trace_hw_interval_param(substream, k, 0, &old_interval, i);
299 params->cmask |= 1 << k;
305 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
306 struct snd_pcm_hw_params *params)
308 struct snd_pcm_hw_constraints *constrs =
309 &substream->runtime->hw_constraints;
311 unsigned int *rstamps;
312 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
314 struct snd_pcm_hw_rule *r;
316 struct snd_mask old_mask;
317 struct snd_interval old_interval;
319 int changed, err = 0;
322 * Each application of rule has own sequence number.
324 * Each member of 'rstamps' array represents the sequence number of
325 * recent application of corresponding rule.
327 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
332 * Each member of 'vstamps' array represents the sequence number of
333 * recent application of rule in which corresponding parameters were
336 * In initial state, elements corresponding to parameters requested by
337 * a caller is 1. For unrequested parameters, corresponding members
338 * have 0 so that the parameters are never changed anymore.
340 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
341 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
343 /* Due to the above design, actual sequence number starts at 2. */
346 /* Apply all rules in order. */
348 for (k = 0; k < constrs->rules_num; k++) {
349 r = &constrs->rules[k];
352 * Check condition bits of this rule. When the rule has
353 * some condition bits, parameter without the bits is
354 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
355 * is an example of the condition bits.
357 if (r->cond && !(r->cond & params->flags))
361 * The 'deps' array includes maximum three dependencies
362 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
363 * member of this array is a sentinel and should be
366 * This rule should be processed in this time when dependent
367 * parameters were changed at former applications of the other
370 for (d = 0; r->deps[d] >= 0; d++) {
371 if (vstamps[r->deps[d]] > rstamps[k])
377 if (trace_hw_mask_param_enabled()) {
378 if (hw_is_mask(r->var))
379 old_mask = *hw_param_mask(params, r->var);
381 if (trace_hw_interval_param_enabled()) {
382 if (hw_is_interval(r->var))
383 old_interval = *hw_param_interval(params, r->var);
386 changed = r->func(params, r);
393 * When the parameter is changed, notify it to the caller
394 * by corresponding returned bit, then preparing for next
397 if (changed && r->var >= 0) {
398 if (hw_is_mask(r->var)) {
399 trace_hw_mask_param(substream, r->var,
401 hw_param_mask(params, r->var));
403 if (hw_is_interval(r->var)) {
404 trace_hw_interval_param(substream, r->var,
405 k + 1, &old_interval,
406 hw_param_interval(params, r->var));
409 params->cmask |= (1 << r->var);
410 vstamps[r->var] = stamp;
414 rstamps[k] = stamp++;
417 /* Iterate to evaluate all rules till no parameters are changed. */
426 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
427 struct snd_pcm_hw_params *params)
429 const struct snd_interval *i;
430 const struct snd_mask *m;
433 if (!params->msbits) {
434 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
435 if (snd_interval_single(i))
436 params->msbits = snd_interval_value(i);
439 if (!params->rate_den) {
440 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
441 if (snd_interval_single(i)) {
442 params->rate_num = snd_interval_value(i);
443 params->rate_den = 1;
447 if (!params->fifo_size) {
448 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
449 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
450 if (snd_mask_single(m) && snd_interval_single(i)) {
451 err = substream->ops->ioctl(substream,
452 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
459 params->info = substream->runtime->hw.info;
460 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
461 SNDRV_PCM_INFO_DRAIN_TRIGGER);
462 if (!hw_support_mmap(substream))
463 params->info &= ~(SNDRV_PCM_INFO_MMAP |
464 SNDRV_PCM_INFO_MMAP_VALID);
470 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params *params)
476 params->fifo_size = 0;
477 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
479 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
480 params->rate_num = 0;
481 params->rate_den = 0;
484 err = constrain_mask_params(substream, params);
488 err = constrain_interval_params(substream, params);
492 err = constrain_params_by_rules(substream, params);
500 EXPORT_SYMBOL(snd_pcm_hw_refine);
502 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
503 struct snd_pcm_hw_params __user * _params)
505 struct snd_pcm_hw_params *params;
508 params = memdup_user(_params, sizeof(*params));
510 return PTR_ERR(params);
512 err = snd_pcm_hw_refine(substream, params);
516 err = fixup_unreferenced_params(substream, params);
520 if (copy_to_user(_params, params, sizeof(*params)))
527 static int period_to_usecs(struct snd_pcm_runtime *runtime)
532 return -1; /* invalid */
534 /* take 75% of period time as the deadline */
535 usecs = (750000 / runtime->rate) * runtime->period_size;
536 usecs += ((750000 % runtime->rate) * runtime->period_size) /
542 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
544 snd_pcm_stream_lock_irq(substream);
545 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
546 substream->runtime->status->state = state;
547 snd_pcm_stream_unlock_irq(substream);
550 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
553 #ifdef CONFIG_SND_PCM_TIMER
554 if (substream->timer)
555 snd_timer_notify(substream->timer, event,
556 &substream->runtime->trigger_tstamp);
561 * snd_pcm_hw_param_choose - choose a configuration defined by @params
563 * @params: the hw_params instance
565 * Choose one configuration from configuration space defined by @params.
566 * The configuration chosen is that obtained fixing in this order:
567 * first access, first format, first subformat, min channels,
568 * min rate, min period time, max buffer size, min tick time
570 * Return: Zero if successful, or a negative error code on failure.
572 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
573 struct snd_pcm_hw_params *params)
575 static const int vars[] = {
576 SNDRV_PCM_HW_PARAM_ACCESS,
577 SNDRV_PCM_HW_PARAM_FORMAT,
578 SNDRV_PCM_HW_PARAM_SUBFORMAT,
579 SNDRV_PCM_HW_PARAM_CHANNELS,
580 SNDRV_PCM_HW_PARAM_RATE,
581 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
582 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
583 SNDRV_PCM_HW_PARAM_TICK_TIME,
587 struct snd_mask old_mask;
588 struct snd_interval old_interval;
591 for (v = vars; *v != -1; v++) {
592 /* Keep old parameter to trace. */
593 if (trace_hw_mask_param_enabled()) {
595 old_mask = *hw_param_mask(params, *v);
597 if (trace_hw_interval_param_enabled()) {
598 if (hw_is_interval(*v))
599 old_interval = *hw_param_interval(params, *v);
601 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
602 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
604 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
610 /* Trace the changed parameter. */
611 if (hw_is_mask(*v)) {
612 trace_hw_mask_param(pcm, *v, 0, &old_mask,
613 hw_param_mask(params, *v));
615 if (hw_is_interval(*v)) {
616 trace_hw_interval_param(pcm, *v, 0, &old_interval,
617 hw_param_interval(params, *v));
624 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
625 struct snd_pcm_hw_params *params)
627 struct snd_pcm_runtime *runtime;
630 snd_pcm_uframes_t frames;
632 if (PCM_RUNTIME_CHECK(substream))
634 runtime = substream->runtime;
635 snd_pcm_stream_lock_irq(substream);
636 switch (runtime->status->state) {
637 case SNDRV_PCM_STATE_OPEN:
638 case SNDRV_PCM_STATE_SETUP:
639 case SNDRV_PCM_STATE_PREPARED:
642 snd_pcm_stream_unlock_irq(substream);
645 snd_pcm_stream_unlock_irq(substream);
646 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
647 if (!substream->oss.oss)
649 if (atomic_read(&substream->mmap_count))
653 err = snd_pcm_hw_refine(substream, params);
657 err = snd_pcm_hw_params_choose(substream, params);
661 err = fixup_unreferenced_params(substream, params);
665 if (substream->ops->hw_params != NULL) {
666 err = substream->ops->hw_params(substream, params);
671 runtime->access = params_access(params);
672 runtime->format = params_format(params);
673 runtime->subformat = params_subformat(params);
674 runtime->channels = params_channels(params);
675 runtime->rate = params_rate(params);
676 runtime->period_size = params_period_size(params);
677 runtime->periods = params_periods(params);
678 runtime->buffer_size = params_buffer_size(params);
679 runtime->info = params->info;
680 runtime->rate_num = params->rate_num;
681 runtime->rate_den = params->rate_den;
682 runtime->no_period_wakeup =
683 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
684 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
686 bits = snd_pcm_format_physical_width(runtime->format);
687 runtime->sample_bits = bits;
688 bits *= runtime->channels;
689 runtime->frame_bits = bits;
691 while (bits % 8 != 0) {
695 runtime->byte_align = bits / 8;
696 runtime->min_align = frames;
698 /* Default sw params */
699 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
700 runtime->period_step = 1;
701 runtime->control->avail_min = runtime->period_size;
702 runtime->start_threshold = 1;
703 runtime->stop_threshold = runtime->buffer_size;
704 runtime->silence_threshold = 0;
705 runtime->silence_size = 0;
706 runtime->boundary = runtime->buffer_size;
707 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
708 runtime->boundary *= 2;
710 snd_pcm_timer_resolution_change(substream);
711 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
713 if (pm_qos_request_active(&substream->latency_pm_qos_req))
714 pm_qos_remove_request(&substream->latency_pm_qos_req);
715 if ((usecs = period_to_usecs(runtime)) >= 0)
716 pm_qos_add_request(&substream->latency_pm_qos_req,
717 PM_QOS_CPU_DMA_LATENCY, usecs);
720 /* hardware might be unusable from this time,
721 so we force application to retry to set
722 the correct hardware parameter settings */
723 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
724 if (substream->ops->hw_free != NULL)
725 substream->ops->hw_free(substream);
729 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
730 struct snd_pcm_hw_params __user * _params)
732 struct snd_pcm_hw_params *params;
735 params = memdup_user(_params, sizeof(*params));
737 return PTR_ERR(params);
739 err = snd_pcm_hw_params(substream, params);
743 if (copy_to_user(_params, params, sizeof(*params)))
750 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
752 struct snd_pcm_runtime *runtime;
755 if (PCM_RUNTIME_CHECK(substream))
757 runtime = substream->runtime;
758 snd_pcm_stream_lock_irq(substream);
759 switch (runtime->status->state) {
760 case SNDRV_PCM_STATE_SETUP:
761 case SNDRV_PCM_STATE_PREPARED:
764 snd_pcm_stream_unlock_irq(substream);
767 snd_pcm_stream_unlock_irq(substream);
768 if (atomic_read(&substream->mmap_count))
770 if (substream->ops->hw_free)
771 result = substream->ops->hw_free(substream);
772 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
773 pm_qos_remove_request(&substream->latency_pm_qos_req);
777 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
778 struct snd_pcm_sw_params *params)
780 struct snd_pcm_runtime *runtime;
783 if (PCM_RUNTIME_CHECK(substream))
785 runtime = substream->runtime;
786 snd_pcm_stream_lock_irq(substream);
787 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
788 snd_pcm_stream_unlock_irq(substream);
791 snd_pcm_stream_unlock_irq(substream);
793 if (params->tstamp_mode < 0 ||
794 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
796 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
797 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
799 if (params->avail_min == 0)
801 if (params->silence_size >= runtime->boundary) {
802 if (params->silence_threshold != 0)
805 if (params->silence_size > params->silence_threshold)
807 if (params->silence_threshold > runtime->buffer_size)
811 snd_pcm_stream_lock_irq(substream);
812 runtime->tstamp_mode = params->tstamp_mode;
813 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
814 runtime->tstamp_type = params->tstamp_type;
815 runtime->period_step = params->period_step;
816 runtime->control->avail_min = params->avail_min;
817 runtime->start_threshold = params->start_threshold;
818 runtime->stop_threshold = params->stop_threshold;
819 runtime->silence_threshold = params->silence_threshold;
820 runtime->silence_size = params->silence_size;
821 params->boundary = runtime->boundary;
822 if (snd_pcm_running(substream)) {
823 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
824 runtime->silence_size > 0)
825 snd_pcm_playback_silence(substream, ULONG_MAX);
826 err = snd_pcm_update_state(substream, runtime);
828 snd_pcm_stream_unlock_irq(substream);
832 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
833 struct snd_pcm_sw_params __user * _params)
835 struct snd_pcm_sw_params params;
837 if (copy_from_user(¶ms, _params, sizeof(params)))
839 err = snd_pcm_sw_params(substream, ¶ms);
840 if (copy_to_user(_params, ¶ms, sizeof(params)))
845 static inline snd_pcm_uframes_t
846 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
848 snd_pcm_uframes_t delay;
850 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
851 delay = snd_pcm_playback_hw_avail(substream->runtime);
853 delay = snd_pcm_capture_avail(substream->runtime);
854 return delay + substream->runtime->delay;
857 int snd_pcm_status(struct snd_pcm_substream *substream,
858 struct snd_pcm_status *status)
860 struct snd_pcm_runtime *runtime = substream->runtime;
862 snd_pcm_stream_lock_irq(substream);
864 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
865 &runtime->audio_tstamp_config);
867 /* backwards compatible behavior */
868 if (runtime->audio_tstamp_config.type_requested ==
869 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
870 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
871 runtime->audio_tstamp_config.type_requested =
872 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
874 runtime->audio_tstamp_config.type_requested =
875 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
876 runtime->audio_tstamp_report.valid = 0;
878 runtime->audio_tstamp_report.valid = 1;
880 status->state = runtime->status->state;
881 status->suspended_state = runtime->status->suspended_state;
882 if (status->state == SNDRV_PCM_STATE_OPEN)
884 status->trigger_tstamp = runtime->trigger_tstamp;
885 if (snd_pcm_running(substream)) {
886 snd_pcm_update_hw_ptr(substream);
887 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
888 status->tstamp = runtime->status->tstamp;
889 status->driver_tstamp = runtime->driver_tstamp;
890 status->audio_tstamp =
891 runtime->status->audio_tstamp;
892 if (runtime->audio_tstamp_report.valid == 1)
893 /* backwards compatibility, no report provided in COMPAT mode */
894 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
895 &status->audio_tstamp_accuracy,
896 &runtime->audio_tstamp_report);
901 /* get tstamp only in fallback mode and only if enabled */
902 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
903 snd_pcm_gettime(runtime, &status->tstamp);
906 status->appl_ptr = runtime->control->appl_ptr;
907 status->hw_ptr = runtime->status->hw_ptr;
908 status->avail = snd_pcm_avail(substream);
909 status->delay = snd_pcm_running(substream) ?
910 snd_pcm_calc_delay(substream) : 0;
911 status->avail_max = runtime->avail_max;
912 status->overrange = runtime->overrange;
913 runtime->avail_max = 0;
914 runtime->overrange = 0;
916 snd_pcm_stream_unlock_irq(substream);
920 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
921 struct snd_pcm_status __user * _status,
924 struct snd_pcm_status status;
927 memset(&status, 0, sizeof(status));
929 * with extension, parameters are read/write,
930 * get audio_tstamp_data from user,
931 * ignore rest of status structure
933 if (ext && get_user(status.audio_tstamp_data,
934 (u32 __user *)(&_status->audio_tstamp_data)))
936 res = snd_pcm_status(substream, &status);
939 if (copy_to_user(_status, &status, sizeof(status)))
944 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
945 struct snd_pcm_channel_info * info)
947 struct snd_pcm_runtime *runtime;
948 unsigned int channel;
950 channel = info->channel;
951 runtime = substream->runtime;
952 snd_pcm_stream_lock_irq(substream);
953 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
954 snd_pcm_stream_unlock_irq(substream);
957 snd_pcm_stream_unlock_irq(substream);
958 if (channel >= runtime->channels)
960 memset(info, 0, sizeof(*info));
961 info->channel = channel;
962 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
965 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
966 struct snd_pcm_channel_info __user * _info)
968 struct snd_pcm_channel_info info;
971 if (copy_from_user(&info, _info, sizeof(info)))
973 res = snd_pcm_channel_info(substream, &info);
976 if (copy_to_user(_info, &info, sizeof(info)))
981 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
983 struct snd_pcm_runtime *runtime = substream->runtime;
984 if (runtime->trigger_master == NULL)
986 if (runtime->trigger_master == substream) {
987 if (!runtime->trigger_tstamp_latched)
988 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
990 snd_pcm_trigger_tstamp(runtime->trigger_master);
991 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
993 runtime->trigger_master = NULL;
997 int (*pre_action)(struct snd_pcm_substream *substream, int state);
998 int (*do_action)(struct snd_pcm_substream *substream, int state);
999 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1000 void (*post_action)(struct snd_pcm_substream *substream, int state);
1004 * this functions is core for handling of linked stream
1005 * Note: the stream state might be changed also on failure
1006 * Note2: call with calling stream lock + link lock
1008 static int snd_pcm_action_group(const struct action_ops *ops,
1009 struct snd_pcm_substream *substream,
1010 int state, int do_lock)
1012 struct snd_pcm_substream *s = NULL;
1013 struct snd_pcm_substream *s1;
1014 int res = 0, depth = 1;
1016 snd_pcm_group_for_each_entry(s, substream) {
1017 if (do_lock && s != substream) {
1018 if (s->pcm->nonatomic)
1019 mutex_lock_nested(&s->self_group.mutex, depth);
1021 spin_lock_nested(&s->self_group.lock, depth);
1024 res = ops->pre_action(s, state);
1028 snd_pcm_group_for_each_entry(s, substream) {
1029 res = ops->do_action(s, state);
1031 if (ops->undo_action) {
1032 snd_pcm_group_for_each_entry(s1, substream) {
1033 if (s1 == s) /* failed stream */
1035 ops->undo_action(s1, state);
1038 s = NULL; /* unlock all */
1042 snd_pcm_group_for_each_entry(s, substream) {
1043 ops->post_action(s, state);
1047 /* unlock streams */
1048 snd_pcm_group_for_each_entry(s1, substream) {
1049 if (s1 != substream) {
1050 if (s1->pcm->nonatomic)
1051 mutex_unlock(&s1->self_group.mutex);
1053 spin_unlock(&s1->self_group.lock);
1055 if (s1 == s) /* end */
1063 * Note: call with stream lock
1065 static int snd_pcm_action_single(const struct action_ops *ops,
1066 struct snd_pcm_substream *substream,
1071 res = ops->pre_action(substream, state);
1074 res = ops->do_action(substream, state);
1076 ops->post_action(substream, state);
1077 else if (ops->undo_action)
1078 ops->undo_action(substream, state);
1082 static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1083 struct snd_pcm_group *new_group)
1085 substream->group = new_group;
1086 list_move(&substream->link_list, &new_group->substreams);
1090 * Unref and unlock the group, but keep the stream lock;
1091 * when the group becomes empty and no longer referred, destroy itself
1093 static void snd_pcm_group_unref(struct snd_pcm_group *group,
1094 struct snd_pcm_substream *substream)
1100 do_free = refcount_dec_and_test(&group->refs);
1101 snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1107 * Lock the group inside a stream lock and reference it;
1108 * return the locked group object, or NULL if not linked
1110 static struct snd_pcm_group *
1111 snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1113 bool nonatomic = substream->pcm->nonatomic;
1114 struct snd_pcm_group *group;
1118 if (!snd_pcm_stream_linked(substream))
1120 group = substream->group;
1121 /* block freeing the group object */
1122 refcount_inc(&group->refs);
1124 trylock = nonatomic ? mutex_trylock(&group->mutex) :
1125 spin_trylock(&group->lock);
1129 /* re-lock for avoiding ABBA deadlock */
1130 snd_pcm_stream_unlock(substream);
1131 snd_pcm_group_lock(group, nonatomic);
1132 snd_pcm_stream_lock(substream);
1134 /* check the group again; the above opens a small race window */
1135 if (substream->group == group)
1137 /* group changed, try again */
1138 snd_pcm_group_unref(group, substream);
1144 * Note: call with stream lock
1146 static int snd_pcm_action(const struct action_ops *ops,
1147 struct snd_pcm_substream *substream,
1150 struct snd_pcm_group *group;
1153 group = snd_pcm_stream_group_ref(substream);
1155 res = snd_pcm_action_group(ops, substream, state, 1);
1157 res = snd_pcm_action_single(ops, substream, state);
1158 snd_pcm_group_unref(group, substream);
1163 * Note: don't use any locks before
1165 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1166 struct snd_pcm_substream *substream,
1171 snd_pcm_stream_lock_irq(substream);
1172 res = snd_pcm_action(ops, substream, state);
1173 snd_pcm_stream_unlock_irq(substream);
1179 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1180 struct snd_pcm_substream *substream,
1185 /* Guarantee the group members won't change during non-atomic action */
1186 down_read(&snd_pcm_link_rwsem);
1187 if (snd_pcm_stream_linked(substream))
1188 res = snd_pcm_action_group(ops, substream, state, 0);
1190 res = snd_pcm_action_single(ops, substream, state);
1191 up_read(&snd_pcm_link_rwsem);
1198 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1200 struct snd_pcm_runtime *runtime = substream->runtime;
1201 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1203 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1204 !snd_pcm_playback_data(substream))
1206 runtime->trigger_tstamp_latched = false;
1207 runtime->trigger_master = substream;
1211 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1213 if (substream->runtime->trigger_master != substream)
1215 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1218 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1220 if (substream->runtime->trigger_master == substream)
1221 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1224 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1226 struct snd_pcm_runtime *runtime = substream->runtime;
1227 snd_pcm_trigger_tstamp(substream);
1228 runtime->hw_ptr_jiffies = jiffies;
1229 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1231 runtime->status->state = state;
1232 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1233 runtime->silence_size > 0)
1234 snd_pcm_playback_silence(substream, ULONG_MAX);
1235 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1238 static const struct action_ops snd_pcm_action_start = {
1239 .pre_action = snd_pcm_pre_start,
1240 .do_action = snd_pcm_do_start,
1241 .undo_action = snd_pcm_undo_start,
1242 .post_action = snd_pcm_post_start
1246 * snd_pcm_start - start all linked streams
1247 * @substream: the PCM substream instance
1249 * Return: Zero if successful, or a negative error code.
1250 * The stream lock must be acquired before calling this function.
1252 int snd_pcm_start(struct snd_pcm_substream *substream)
1254 return snd_pcm_action(&snd_pcm_action_start, substream,
1255 SNDRV_PCM_STATE_RUNNING);
1258 /* take the stream lock and start the streams */
1259 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1261 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1262 SNDRV_PCM_STATE_RUNNING);
1268 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1270 struct snd_pcm_runtime *runtime = substream->runtime;
1271 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1273 runtime->trigger_master = substream;
1277 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1279 if (substream->runtime->trigger_master == substream &&
1280 snd_pcm_running(substream))
1281 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1282 return 0; /* unconditonally stop all substreams */
1285 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1287 struct snd_pcm_runtime *runtime = substream->runtime;
1288 if (runtime->status->state != state) {
1289 snd_pcm_trigger_tstamp(substream);
1290 runtime->status->state = state;
1291 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1293 wake_up(&runtime->sleep);
1294 wake_up(&runtime->tsleep);
1297 static const struct action_ops snd_pcm_action_stop = {
1298 .pre_action = snd_pcm_pre_stop,
1299 .do_action = snd_pcm_do_stop,
1300 .post_action = snd_pcm_post_stop
1304 * snd_pcm_stop - try to stop all running streams in the substream group
1305 * @substream: the PCM substream instance
1306 * @state: PCM state after stopping the stream
1308 * The state of each stream is then changed to the given state unconditionally.
1310 * Return: Zero if successful, or a negative error code.
1312 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1314 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1316 EXPORT_SYMBOL(snd_pcm_stop);
1319 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1320 * @substream: the PCM substream
1322 * After stopping, the state is changed to SETUP.
1323 * Unlike snd_pcm_stop(), this affects only the given stream.
1325 * Return: Zero if succesful, or a negative error code.
1327 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1329 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1330 SNDRV_PCM_STATE_SETUP);
1334 * snd_pcm_stop_xrun - stop the running streams as XRUN
1335 * @substream: the PCM substream instance
1337 * This stops the given running substream (and all linked substreams) as XRUN.
1338 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1340 * Return: Zero if successful, or a negative error code.
1342 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1344 unsigned long flags;
1346 snd_pcm_stream_lock_irqsave(substream, flags);
1347 if (substream->runtime && snd_pcm_running(substream))
1348 __snd_pcm_xrun(substream);
1349 snd_pcm_stream_unlock_irqrestore(substream, flags);
1352 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1357 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1359 struct snd_pcm_runtime *runtime = substream->runtime;
1360 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1363 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1365 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1367 runtime->trigger_master = substream;
1371 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1373 if (substream->runtime->trigger_master != substream)
1375 /* some drivers might use hw_ptr to recover from the pause -
1376 update the hw_ptr now */
1378 snd_pcm_update_hw_ptr(substream);
1379 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1380 * a delta between the current jiffies, this gives a large enough
1381 * delta, effectively to skip the check once.
1383 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1384 return substream->ops->trigger(substream,
1385 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1386 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1389 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1391 if (substream->runtime->trigger_master == substream)
1392 substream->ops->trigger(substream,
1393 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1394 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1397 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1399 struct snd_pcm_runtime *runtime = substream->runtime;
1400 snd_pcm_trigger_tstamp(substream);
1402 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1403 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1404 wake_up(&runtime->sleep);
1405 wake_up(&runtime->tsleep);
1407 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1408 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1412 static const struct action_ops snd_pcm_action_pause = {
1413 .pre_action = snd_pcm_pre_pause,
1414 .do_action = snd_pcm_do_pause,
1415 .undo_action = snd_pcm_undo_pause,
1416 .post_action = snd_pcm_post_pause
1420 * Push/release the pause for all linked streams.
1422 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1424 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1430 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1432 struct snd_pcm_runtime *runtime = substream->runtime;
1433 switch (runtime->status->state) {
1434 case SNDRV_PCM_STATE_SUSPENDED:
1436 /* unresumable PCM state; return -EBUSY for skipping suspend */
1437 case SNDRV_PCM_STATE_OPEN:
1438 case SNDRV_PCM_STATE_SETUP:
1439 case SNDRV_PCM_STATE_DISCONNECTED:
1442 runtime->trigger_master = substream;
1446 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1448 struct snd_pcm_runtime *runtime = substream->runtime;
1449 if (runtime->trigger_master != substream)
1451 if (! snd_pcm_running(substream))
1453 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1454 return 0; /* suspend unconditionally */
1457 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1459 struct snd_pcm_runtime *runtime = substream->runtime;
1460 snd_pcm_trigger_tstamp(substream);
1461 runtime->status->suspended_state = runtime->status->state;
1462 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1463 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1464 wake_up(&runtime->sleep);
1465 wake_up(&runtime->tsleep);
1468 static const struct action_ops snd_pcm_action_suspend = {
1469 .pre_action = snd_pcm_pre_suspend,
1470 .do_action = snd_pcm_do_suspend,
1471 .post_action = snd_pcm_post_suspend
1475 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1476 * @substream: the PCM substream
1478 * After this call, all streams are changed to SUSPENDED state.
1480 * Return: Zero if successful, or a negative error code.
1482 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1485 unsigned long flags;
1487 snd_pcm_stream_lock_irqsave(substream, flags);
1488 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1489 snd_pcm_stream_unlock_irqrestore(substream, flags);
1494 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1495 * @pcm: the PCM instance
1497 * After this call, all streams are changed to SUSPENDED state.
1499 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1501 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1503 struct snd_pcm_substream *substream;
1504 int stream, err = 0;
1509 for (stream = 0; stream < 2; stream++) {
1510 for (substream = pcm->streams[stream].substream;
1511 substream; substream = substream->next) {
1512 /* FIXME: the open/close code should lock this as well */
1513 if (substream->runtime == NULL)
1517 * Skip BE dai link PCM's that are internal and may
1518 * not have their substream ops set.
1520 if (!substream->ops)
1523 err = snd_pcm_suspend(substream);
1524 if (err < 0 && err != -EBUSY)
1530 EXPORT_SYMBOL(snd_pcm_suspend_all);
1534 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1536 struct snd_pcm_runtime *runtime = substream->runtime;
1537 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1539 runtime->trigger_master = substream;
1543 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1545 struct snd_pcm_runtime *runtime = substream->runtime;
1546 if (runtime->trigger_master != substream)
1548 /* DMA not running previously? */
1549 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1550 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1551 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1553 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1556 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1558 if (substream->runtime->trigger_master == substream &&
1559 snd_pcm_running(substream))
1560 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1563 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1565 struct snd_pcm_runtime *runtime = substream->runtime;
1566 snd_pcm_trigger_tstamp(substream);
1567 runtime->status->state = runtime->status->suspended_state;
1568 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1571 static const struct action_ops snd_pcm_action_resume = {
1572 .pre_action = snd_pcm_pre_resume,
1573 .do_action = snd_pcm_do_resume,
1574 .undo_action = snd_pcm_undo_resume,
1575 .post_action = snd_pcm_post_resume
1578 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1580 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1585 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1590 #endif /* CONFIG_PM */
1595 * Change the RUNNING stream(s) to XRUN state.
1597 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1599 struct snd_pcm_runtime *runtime = substream->runtime;
1602 snd_pcm_stream_lock_irq(substream);
1603 switch (runtime->status->state) {
1604 case SNDRV_PCM_STATE_XRUN:
1605 result = 0; /* already there */
1607 case SNDRV_PCM_STATE_RUNNING:
1608 __snd_pcm_xrun(substream);
1614 snd_pcm_stream_unlock_irq(substream);
1621 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1623 struct snd_pcm_runtime *runtime = substream->runtime;
1624 switch (runtime->status->state) {
1625 case SNDRV_PCM_STATE_RUNNING:
1626 case SNDRV_PCM_STATE_PREPARED:
1627 case SNDRV_PCM_STATE_PAUSED:
1628 case SNDRV_PCM_STATE_SUSPENDED:
1635 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1637 struct snd_pcm_runtime *runtime = substream->runtime;
1638 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1641 runtime->hw_ptr_base = 0;
1642 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1643 runtime->status->hw_ptr % runtime->period_size;
1644 runtime->silence_start = runtime->status->hw_ptr;
1645 runtime->silence_filled = 0;
1649 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1651 struct snd_pcm_runtime *runtime = substream->runtime;
1652 runtime->control->appl_ptr = runtime->status->hw_ptr;
1653 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1654 runtime->silence_size > 0)
1655 snd_pcm_playback_silence(substream, ULONG_MAX);
1658 static const struct action_ops snd_pcm_action_reset = {
1659 .pre_action = snd_pcm_pre_reset,
1660 .do_action = snd_pcm_do_reset,
1661 .post_action = snd_pcm_post_reset
1664 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1666 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1672 /* we use the second argument for updating f_flags */
1673 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1678 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1680 if (snd_pcm_running(substream))
1682 substream->f_flags = f_flags;
1686 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1689 err = substream->ops->prepare(substream);
1692 return snd_pcm_do_reset(substream, 0);
1695 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1697 struct snd_pcm_runtime *runtime = substream->runtime;
1698 runtime->control->appl_ptr = runtime->status->hw_ptr;
1699 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1702 static const struct action_ops snd_pcm_action_prepare = {
1703 .pre_action = snd_pcm_pre_prepare,
1704 .do_action = snd_pcm_do_prepare,
1705 .post_action = snd_pcm_post_prepare
1709 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1710 * @substream: the PCM substream instance
1711 * @file: file to refer f_flags
1713 * Return: Zero if successful, or a negative error code.
1715 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1721 f_flags = file->f_flags;
1723 f_flags = substream->f_flags;
1725 snd_pcm_stream_lock_irq(substream);
1726 switch (substream->runtime->status->state) {
1727 case SNDRV_PCM_STATE_PAUSED:
1728 snd_pcm_pause(substream, 0);
1730 case SNDRV_PCM_STATE_SUSPENDED:
1731 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1734 snd_pcm_stream_unlock_irq(substream);
1736 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1737 substream, f_flags);
1744 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1746 struct snd_pcm_runtime *runtime = substream->runtime;
1747 switch (runtime->status->state) {
1748 case SNDRV_PCM_STATE_OPEN:
1749 case SNDRV_PCM_STATE_DISCONNECTED:
1750 case SNDRV_PCM_STATE_SUSPENDED:
1753 runtime->trigger_master = substream;
1757 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1759 struct snd_pcm_runtime *runtime = substream->runtime;
1760 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1761 switch (runtime->status->state) {
1762 case SNDRV_PCM_STATE_PREPARED:
1763 /* start playback stream if possible */
1764 if (! snd_pcm_playback_empty(substream)) {
1765 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1766 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1768 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1771 case SNDRV_PCM_STATE_RUNNING:
1772 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1774 case SNDRV_PCM_STATE_XRUN:
1775 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1781 /* stop running stream */
1782 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1783 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1784 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1785 snd_pcm_do_stop(substream, new_state);
1786 snd_pcm_post_stop(substream, new_state);
1790 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1791 runtime->trigger_master == substream &&
1792 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1793 return substream->ops->trigger(substream,
1794 SNDRV_PCM_TRIGGER_DRAIN);
1799 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1803 static const struct action_ops snd_pcm_action_drain_init = {
1804 .pre_action = snd_pcm_pre_drain_init,
1805 .do_action = snd_pcm_do_drain_init,
1806 .post_action = snd_pcm_post_drain_init
1810 * Drain the stream(s).
1811 * When the substream is linked, sync until the draining of all playback streams
1813 * After this call, all streams are supposed to be either SETUP or DRAINING
1814 * (capture only) state.
1816 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1819 struct snd_card *card;
1820 struct snd_pcm_runtime *runtime;
1821 struct snd_pcm_substream *s;
1822 struct snd_pcm_group *group;
1823 wait_queue_entry_t wait;
1827 card = substream->pcm->card;
1828 runtime = substream->runtime;
1830 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1834 if (file->f_flags & O_NONBLOCK)
1836 } else if (substream->f_flags & O_NONBLOCK)
1839 snd_pcm_stream_lock_irq(substream);
1841 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1842 snd_pcm_pause(substream, 0);
1844 /* pre-start/stop - all running streams are changed to DRAINING state */
1845 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1848 /* in non-blocking, we don't wait in ioctl but let caller poll */
1856 struct snd_pcm_runtime *to_check;
1857 if (signal_pending(current)) {
1858 result = -ERESTARTSYS;
1861 /* find a substream to drain */
1863 group = snd_pcm_stream_group_ref(substream);
1864 snd_pcm_group_for_each_entry(s, substream) {
1865 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1867 runtime = s->runtime;
1868 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1873 snd_pcm_group_unref(group, substream);
1875 break; /* all drained */
1876 init_waitqueue_entry(&wait, current);
1877 set_current_state(TASK_INTERRUPTIBLE);
1878 add_wait_queue(&to_check->sleep, &wait);
1879 snd_pcm_stream_unlock_irq(substream);
1880 if (runtime->no_period_wakeup)
1881 tout = MAX_SCHEDULE_TIMEOUT;
1884 if (runtime->rate) {
1885 long t = runtime->period_size * 2 / runtime->rate;
1886 tout = max(t, tout);
1888 tout = msecs_to_jiffies(tout * 1000);
1890 tout = schedule_timeout(tout);
1892 snd_pcm_stream_lock_irq(substream);
1893 group = snd_pcm_stream_group_ref(substream);
1894 snd_pcm_group_for_each_entry(s, substream) {
1895 if (s->runtime == to_check) {
1896 remove_wait_queue(&to_check->sleep, &wait);
1900 snd_pcm_group_unref(group, substream);
1902 if (card->shutdown) {
1907 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1910 dev_dbg(substream->pcm->card->dev,
1911 "playback drain error (DMA or IRQ trouble?)\n");
1912 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1920 snd_pcm_stream_unlock_irq(substream);
1928 * Immediately put all linked substreams into SETUP state.
1930 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1932 struct snd_pcm_runtime *runtime;
1935 if (PCM_RUNTIME_CHECK(substream))
1937 runtime = substream->runtime;
1939 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1940 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1943 snd_pcm_stream_lock_irq(substream);
1945 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1946 snd_pcm_pause(substream, 0);
1948 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1949 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1950 snd_pcm_stream_unlock_irq(substream);
1956 static bool is_pcm_file(struct file *file)
1958 struct inode *inode = file_inode(file);
1959 struct snd_pcm *pcm;
1962 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1964 minor = iminor(inode);
1965 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
1967 pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1970 snd_card_unref(pcm->card);
1977 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1980 struct snd_pcm_file *pcm_file;
1981 struct snd_pcm_substream *substream1;
1982 struct snd_pcm_group *group, *target_group;
1983 bool nonatomic = substream->pcm->nonatomic;
1984 struct fd f = fdget(fd);
1988 if (!is_pcm_file(f.file)) {
1992 pcm_file = f.file->private_data;
1993 substream1 = pcm_file->substream;
1994 group = kzalloc(sizeof(*group), GFP_KERNEL);
1999 snd_pcm_group_init(group);
2001 down_write(&snd_pcm_link_rwsem);
2002 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2003 substream->runtime->status->state != substream1->runtime->status->state ||
2004 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2008 if (snd_pcm_stream_linked(substream1)) {
2013 snd_pcm_stream_lock_irq(substream);
2014 if (!snd_pcm_stream_linked(substream)) {
2015 snd_pcm_group_assign(substream, group);
2016 group = NULL; /* assigned, don't free this one below */
2018 target_group = substream->group;
2019 snd_pcm_stream_unlock_irq(substream);
2021 snd_pcm_group_lock_irq(target_group, nonatomic);
2022 snd_pcm_stream_lock(substream1);
2023 snd_pcm_group_assign(substream1, target_group);
2024 refcount_inc(&target_group->refs);
2025 snd_pcm_stream_unlock(substream1);
2026 snd_pcm_group_unlock_irq(target_group, nonatomic);
2028 up_write(&snd_pcm_link_rwsem);
2036 static void relink_to_local(struct snd_pcm_substream *substream)
2038 snd_pcm_stream_lock(substream);
2039 snd_pcm_group_assign(substream, &substream->self_group);
2040 snd_pcm_stream_unlock(substream);
2043 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2045 struct snd_pcm_group *group;
2046 bool nonatomic = substream->pcm->nonatomic;
2047 bool do_free = false;
2050 down_write(&snd_pcm_link_rwsem);
2052 if (!snd_pcm_stream_linked(substream)) {
2057 group = substream->group;
2058 snd_pcm_group_lock_irq(group, nonatomic);
2060 relink_to_local(substream);
2061 refcount_dec(&group->refs);
2063 /* detach the last stream, too */
2064 if (list_is_singular(&group->substreams)) {
2065 relink_to_local(list_first_entry(&group->substreams,
2066 struct snd_pcm_substream,
2068 do_free = refcount_dec_and_test(&group->refs);
2071 snd_pcm_group_unlock_irq(group, nonatomic);
2076 up_write(&snd_pcm_link_rwsem);
2083 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2084 struct snd_pcm_hw_rule *rule)
2086 struct snd_interval t;
2087 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2088 hw_param_interval_c(params, rule->deps[1]), &t);
2089 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2092 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2093 struct snd_pcm_hw_rule *rule)
2095 struct snd_interval t;
2096 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2097 hw_param_interval_c(params, rule->deps[1]), &t);
2098 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2101 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2102 struct snd_pcm_hw_rule *rule)
2104 struct snd_interval t;
2105 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2106 hw_param_interval_c(params, rule->deps[1]),
2107 (unsigned long) rule->private, &t);
2108 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2111 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2112 struct snd_pcm_hw_rule *rule)
2114 struct snd_interval t;
2115 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2116 (unsigned long) rule->private,
2117 hw_param_interval_c(params, rule->deps[1]), &t);
2118 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2121 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2122 struct snd_pcm_hw_rule *rule)
2125 const struct snd_interval *i =
2126 hw_param_interval_c(params, rule->deps[0]);
2128 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2130 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2132 if (! snd_mask_test(mask, k))
2134 bits = snd_pcm_format_physical_width(k);
2136 continue; /* ignore invalid formats */
2137 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2138 snd_mask_reset(&m, k);
2140 return snd_mask_refine(mask, &m);
2143 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2144 struct snd_pcm_hw_rule *rule)
2146 struct snd_interval t;
2152 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2154 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2156 bits = snd_pcm_format_physical_width(k);
2158 continue; /* ignore invalid formats */
2159 if (t.min > (unsigned)bits)
2161 if (t.max < (unsigned)bits)
2165 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2168 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2169 #error "Change this table"
2172 static const unsigned int rates[] = {
2173 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2174 48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2177 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2178 .count = ARRAY_SIZE(rates),
2182 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2183 struct snd_pcm_hw_rule *rule)
2185 struct snd_pcm_hardware *hw = rule->private;
2186 return snd_interval_list(hw_param_interval(params, rule->var),
2187 snd_pcm_known_rates.count,
2188 snd_pcm_known_rates.list, hw->rates);
2191 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2192 struct snd_pcm_hw_rule *rule)
2194 struct snd_interval t;
2195 struct snd_pcm_substream *substream = rule->private;
2197 t.max = substream->buffer_bytes_max;
2201 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2204 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2206 struct snd_pcm_runtime *runtime = substream->runtime;
2207 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2210 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2211 snd_mask_any(constrs_mask(constrs, k));
2214 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2215 snd_interval_any(constrs_interval(constrs, k));
2218 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2219 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2220 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2221 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2222 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2224 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2225 snd_pcm_hw_rule_format, NULL,
2226 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2229 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2230 snd_pcm_hw_rule_sample_bits, NULL,
2231 SNDRV_PCM_HW_PARAM_FORMAT,
2232 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2235 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2236 snd_pcm_hw_rule_div, NULL,
2237 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2240 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2241 snd_pcm_hw_rule_mul, NULL,
2242 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2245 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2246 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2247 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2250 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2251 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2252 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2255 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2256 snd_pcm_hw_rule_div, NULL,
2257 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2260 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2261 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2262 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2265 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2266 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2267 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2270 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2271 snd_pcm_hw_rule_div, NULL,
2272 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2275 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2276 snd_pcm_hw_rule_div, NULL,
2277 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2280 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2281 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2282 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2285 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2286 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2287 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2290 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2291 snd_pcm_hw_rule_mul, NULL,
2292 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2295 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2296 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2297 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2300 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2301 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2302 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2305 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2306 snd_pcm_hw_rule_muldivk, (void*) 8,
2307 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2310 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2311 snd_pcm_hw_rule_muldivk, (void*) 8,
2312 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2315 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2316 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2317 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2320 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2321 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2322 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2328 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2330 struct snd_pcm_runtime *runtime = substream->runtime;
2331 struct snd_pcm_hardware *hw = &runtime->hw;
2333 unsigned int mask = 0;
2335 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2336 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2337 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2338 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2339 if (hw_support_mmap(substream)) {
2340 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2341 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2342 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2343 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2344 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2345 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2347 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2351 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2355 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2359 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2360 hw->channels_min, hw->channels_max);
2364 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2365 hw->rate_min, hw->rate_max);
2369 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2370 hw->period_bytes_min, hw->period_bytes_max);
2374 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2375 hw->periods_min, hw->periods_max);
2379 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2380 hw->period_bytes_min, hw->buffer_bytes_max);
2384 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2385 snd_pcm_hw_rule_buffer_bytes_max, substream,
2386 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2391 if (runtime->dma_bytes) {
2392 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2397 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2398 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2399 snd_pcm_hw_rule_rate, hw,
2400 SNDRV_PCM_HW_PARAM_RATE, -1);
2405 /* FIXME: this belong to lowlevel */
2406 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2411 static void pcm_release_private(struct snd_pcm_substream *substream)
2413 if (snd_pcm_stream_linked(substream))
2414 snd_pcm_unlink(substream);
2417 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2419 substream->ref_count--;
2420 if (substream->ref_count > 0)
2423 snd_pcm_drop(substream);
2424 if (substream->hw_opened) {
2425 if (substream->ops->hw_free &&
2426 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2427 substream->ops->hw_free(substream);
2428 substream->ops->close(substream);
2429 substream->hw_opened = 0;
2431 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2432 pm_qos_remove_request(&substream->latency_pm_qos_req);
2433 if (substream->pcm_release) {
2434 substream->pcm_release(substream);
2435 substream->pcm_release = NULL;
2437 snd_pcm_detach_substream(substream);
2439 EXPORT_SYMBOL(snd_pcm_release_substream);
2441 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2443 struct snd_pcm_substream **rsubstream)
2445 struct snd_pcm_substream *substream;
2448 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2451 if (substream->ref_count > 1) {
2452 *rsubstream = substream;
2456 err = snd_pcm_hw_constraints_init(substream);
2458 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2462 if ((err = substream->ops->open(substream)) < 0)
2465 substream->hw_opened = 1;
2467 err = snd_pcm_hw_constraints_complete(substream);
2469 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2473 *rsubstream = substream;
2477 snd_pcm_release_substream(substream);
2480 EXPORT_SYMBOL(snd_pcm_open_substream);
2482 static int snd_pcm_open_file(struct file *file,
2483 struct snd_pcm *pcm,
2486 struct snd_pcm_file *pcm_file;
2487 struct snd_pcm_substream *substream;
2490 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2494 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2495 if (pcm_file == NULL) {
2496 snd_pcm_release_substream(substream);
2499 pcm_file->substream = substream;
2500 if (substream->ref_count == 1)
2501 substream->pcm_release = pcm_release_private;
2502 file->private_data = pcm_file;
2507 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2509 struct snd_pcm *pcm;
2510 int err = nonseekable_open(inode, file);
2513 pcm = snd_lookup_minor_data(iminor(inode),
2514 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2515 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2517 snd_card_unref(pcm->card);
2521 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2523 struct snd_pcm *pcm;
2524 int err = nonseekable_open(inode, file);
2527 pcm = snd_lookup_minor_data(iminor(inode),
2528 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2529 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2531 snd_card_unref(pcm->card);
2535 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2538 wait_queue_entry_t wait;
2544 err = snd_card_file_add(pcm->card, file);
2547 if (!try_module_get(pcm->card->module)) {
2551 init_waitqueue_entry(&wait, current);
2552 add_wait_queue(&pcm->open_wait, &wait);
2553 mutex_lock(&pcm->open_mutex);
2555 err = snd_pcm_open_file(file, pcm, stream);
2558 if (err == -EAGAIN) {
2559 if (file->f_flags & O_NONBLOCK) {
2565 set_current_state(TASK_INTERRUPTIBLE);
2566 mutex_unlock(&pcm->open_mutex);
2568 mutex_lock(&pcm->open_mutex);
2569 if (pcm->card->shutdown) {
2573 if (signal_pending(current)) {
2578 remove_wait_queue(&pcm->open_wait, &wait);
2579 mutex_unlock(&pcm->open_mutex);
2585 module_put(pcm->card->module);
2587 snd_card_file_remove(pcm->card, file);
2592 static int snd_pcm_release(struct inode *inode, struct file *file)
2594 struct snd_pcm *pcm;
2595 struct snd_pcm_substream *substream;
2596 struct snd_pcm_file *pcm_file;
2598 pcm_file = file->private_data;
2599 substream = pcm_file->substream;
2600 if (snd_BUG_ON(!substream))
2602 pcm = substream->pcm;
2603 mutex_lock(&pcm->open_mutex);
2604 snd_pcm_release_substream(substream);
2606 mutex_unlock(&pcm->open_mutex);
2607 wake_up(&pcm->open_wait);
2608 module_put(pcm->card->module);
2609 snd_card_file_remove(pcm->card, file);
2613 /* check and update PCM state; return 0 or a negative error
2614 * call this inside PCM lock
2616 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2618 switch (substream->runtime->status->state) {
2619 case SNDRV_PCM_STATE_DRAINING:
2620 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2623 case SNDRV_PCM_STATE_RUNNING:
2624 return snd_pcm_update_hw_ptr(substream);
2625 case SNDRV_PCM_STATE_PREPARED:
2626 case SNDRV_PCM_STATE_PAUSED:
2628 case SNDRV_PCM_STATE_SUSPENDED:
2630 case SNDRV_PCM_STATE_XRUN:
2637 /* increase the appl_ptr; returns the processed frames or a negative error */
2638 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2639 snd_pcm_uframes_t frames,
2640 snd_pcm_sframes_t avail)
2642 struct snd_pcm_runtime *runtime = substream->runtime;
2643 snd_pcm_sframes_t appl_ptr;
2648 if (frames > (snd_pcm_uframes_t)avail)
2650 appl_ptr = runtime->control->appl_ptr + frames;
2651 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2652 appl_ptr -= runtime->boundary;
2653 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2654 return ret < 0 ? ret : frames;
2657 /* decrease the appl_ptr; returns the processed frames or zero for error */
2658 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2659 snd_pcm_uframes_t frames,
2660 snd_pcm_sframes_t avail)
2662 struct snd_pcm_runtime *runtime = substream->runtime;
2663 snd_pcm_sframes_t appl_ptr;
2668 if (frames > (snd_pcm_uframes_t)avail)
2670 appl_ptr = runtime->control->appl_ptr - frames;
2672 appl_ptr += runtime->boundary;
2673 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2674 /* NOTE: we return zero for errors because PulseAudio gets depressed
2675 * upon receiving an error from rewind ioctl and stops processing
2676 * any longer. Returning zero means that no rewind is done, so
2677 * it's not absolutely wrong to answer like that.
2679 return ret < 0 ? 0 : frames;
2682 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2683 snd_pcm_uframes_t frames)
2685 snd_pcm_sframes_t ret;
2690 snd_pcm_stream_lock_irq(substream);
2691 ret = do_pcm_hwsync(substream);
2693 ret = rewind_appl_ptr(substream, frames,
2694 snd_pcm_hw_avail(substream));
2695 snd_pcm_stream_unlock_irq(substream);
2699 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2700 snd_pcm_uframes_t frames)
2702 snd_pcm_sframes_t ret;
2707 snd_pcm_stream_lock_irq(substream);
2708 ret = do_pcm_hwsync(substream);
2710 ret = forward_appl_ptr(substream, frames,
2711 snd_pcm_avail(substream));
2712 snd_pcm_stream_unlock_irq(substream);
2716 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2720 snd_pcm_stream_lock_irq(substream);
2721 err = do_pcm_hwsync(substream);
2722 snd_pcm_stream_unlock_irq(substream);
2726 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2727 snd_pcm_sframes_t *delay)
2730 snd_pcm_sframes_t n = 0;
2732 snd_pcm_stream_lock_irq(substream);
2733 err = do_pcm_hwsync(substream);
2735 n = snd_pcm_calc_delay(substream);
2736 snd_pcm_stream_unlock_irq(substream);
2742 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2743 struct snd_pcm_sync_ptr __user *_sync_ptr)
2745 struct snd_pcm_runtime *runtime = substream->runtime;
2746 struct snd_pcm_sync_ptr sync_ptr;
2747 volatile struct snd_pcm_mmap_status *status;
2748 volatile struct snd_pcm_mmap_control *control;
2751 memset(&sync_ptr, 0, sizeof(sync_ptr));
2752 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2754 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2756 status = runtime->status;
2757 control = runtime->control;
2758 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2759 err = snd_pcm_hwsync(substream);
2763 snd_pcm_stream_lock_irq(substream);
2764 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2765 err = pcm_lib_apply_appl_ptr(substream,
2766 sync_ptr.c.control.appl_ptr);
2768 snd_pcm_stream_unlock_irq(substream);
2772 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2774 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2775 control->avail_min = sync_ptr.c.control.avail_min;
2777 sync_ptr.c.control.avail_min = control->avail_min;
2778 sync_ptr.s.status.state = status->state;
2779 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2780 sync_ptr.s.status.tstamp = status->tstamp;
2781 sync_ptr.s.status.suspended_state = status->suspended_state;
2782 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2783 snd_pcm_stream_unlock_irq(substream);
2784 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2789 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2791 struct snd_pcm_runtime *runtime = substream->runtime;
2794 if (get_user(arg, _arg))
2796 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2798 runtime->tstamp_type = arg;
2802 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2803 struct snd_xferi __user *_xferi)
2805 struct snd_xferi xferi;
2806 struct snd_pcm_runtime *runtime = substream->runtime;
2807 snd_pcm_sframes_t result;
2809 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2811 if (put_user(0, &_xferi->result))
2813 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2815 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2816 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2818 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2819 __put_user(result, &_xferi->result);
2820 return result < 0 ? result : 0;
2823 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2824 struct snd_xfern __user *_xfern)
2826 struct snd_xfern xfern;
2827 struct snd_pcm_runtime *runtime = substream->runtime;
2829 snd_pcm_sframes_t result;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2833 if (runtime->channels > 128)
2835 if (put_user(0, &_xfern->result))
2837 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2840 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2842 return PTR_ERR(bufs);
2843 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2844 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2846 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2848 __put_user(result, &_xfern->result);
2849 return result < 0 ? result : 0;
2852 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2853 snd_pcm_uframes_t __user *_frames)
2855 snd_pcm_uframes_t frames;
2856 snd_pcm_sframes_t result;
2858 if (get_user(frames, _frames))
2860 if (put_user(0, _frames))
2862 result = snd_pcm_rewind(substream, frames);
2863 __put_user(result, _frames);
2864 return result < 0 ? result : 0;
2867 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2868 snd_pcm_uframes_t __user *_frames)
2870 snd_pcm_uframes_t frames;
2871 snd_pcm_sframes_t result;
2873 if (get_user(frames, _frames))
2875 if (put_user(0, _frames))
2877 result = snd_pcm_forward(substream, frames);
2878 __put_user(result, _frames);
2879 return result < 0 ? result : 0;
2882 static int snd_pcm_common_ioctl(struct file *file,
2883 struct snd_pcm_substream *substream,
2884 unsigned int cmd, void __user *arg)
2886 struct snd_pcm_file *pcm_file = file->private_data;
2889 if (PCM_RUNTIME_CHECK(substream))
2892 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2897 case SNDRV_PCM_IOCTL_PVERSION:
2898 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2899 case SNDRV_PCM_IOCTL_INFO:
2900 return snd_pcm_info_user(substream, arg);
2901 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2903 case SNDRV_PCM_IOCTL_TTSTAMP:
2904 return snd_pcm_tstamp(substream, arg);
2905 case SNDRV_PCM_IOCTL_USER_PVERSION:
2906 if (get_user(pcm_file->user_pversion,
2907 (unsigned int __user *)arg))
2910 case SNDRV_PCM_IOCTL_HW_REFINE:
2911 return snd_pcm_hw_refine_user(substream, arg);
2912 case SNDRV_PCM_IOCTL_HW_PARAMS:
2913 return snd_pcm_hw_params_user(substream, arg);
2914 case SNDRV_PCM_IOCTL_HW_FREE:
2915 return snd_pcm_hw_free(substream);
2916 case SNDRV_PCM_IOCTL_SW_PARAMS:
2917 return snd_pcm_sw_params_user(substream, arg);
2918 case SNDRV_PCM_IOCTL_STATUS:
2919 return snd_pcm_status_user(substream, arg, false);
2920 case SNDRV_PCM_IOCTL_STATUS_EXT:
2921 return snd_pcm_status_user(substream, arg, true);
2922 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2923 return snd_pcm_channel_info_user(substream, arg);
2924 case SNDRV_PCM_IOCTL_PREPARE:
2925 return snd_pcm_prepare(substream, file);
2926 case SNDRV_PCM_IOCTL_RESET:
2927 return snd_pcm_reset(substream);
2928 case SNDRV_PCM_IOCTL_START:
2929 return snd_pcm_start_lock_irq(substream);
2930 case SNDRV_PCM_IOCTL_LINK:
2931 return snd_pcm_link(substream, (int)(unsigned long) arg);
2932 case SNDRV_PCM_IOCTL_UNLINK:
2933 return snd_pcm_unlink(substream);
2934 case SNDRV_PCM_IOCTL_RESUME:
2935 return snd_pcm_resume(substream);
2936 case SNDRV_PCM_IOCTL_XRUN:
2937 return snd_pcm_xrun(substream);
2938 case SNDRV_PCM_IOCTL_HWSYNC:
2939 return snd_pcm_hwsync(substream);
2940 case SNDRV_PCM_IOCTL_DELAY:
2942 snd_pcm_sframes_t delay;
2943 snd_pcm_sframes_t __user *res = arg;
2946 err = snd_pcm_delay(substream, &delay);
2949 if (put_user(delay, res))
2953 case SNDRV_PCM_IOCTL_SYNC_PTR:
2954 return snd_pcm_sync_ptr(substream, arg);
2955 #ifdef CONFIG_SND_SUPPORT_OLD_API
2956 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2957 return snd_pcm_hw_refine_old_user(substream, arg);
2958 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2959 return snd_pcm_hw_params_old_user(substream, arg);
2961 case SNDRV_PCM_IOCTL_DRAIN:
2962 return snd_pcm_drain(substream, file);
2963 case SNDRV_PCM_IOCTL_DROP:
2964 return snd_pcm_drop(substream);
2965 case SNDRV_PCM_IOCTL_PAUSE:
2966 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
2968 (int)(unsigned long)arg);
2969 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2970 case SNDRV_PCM_IOCTL_READI_FRAMES:
2971 return snd_pcm_xferi_frames_ioctl(substream, arg);
2972 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2973 case SNDRV_PCM_IOCTL_READN_FRAMES:
2974 return snd_pcm_xfern_frames_ioctl(substream, arg);
2975 case SNDRV_PCM_IOCTL_REWIND:
2976 return snd_pcm_rewind_ioctl(substream, arg);
2977 case SNDRV_PCM_IOCTL_FORWARD:
2978 return snd_pcm_forward_ioctl(substream, arg);
2980 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2984 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
2987 struct snd_pcm_file *pcm_file;
2989 pcm_file = file->private_data;
2991 if (((cmd >> 8) & 0xff) != 'A')
2994 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
2995 (void __user *)arg);
2999 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3000 * @substream: PCM substream
3002 * @arg: IOCTL argument
3004 * The function is provided primarily for OSS layer and USB gadget drivers,
3005 * and it allows only the limited set of ioctls (hw_params, sw_params,
3006 * prepare, start, drain, drop, forward).
3008 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3009 unsigned int cmd, void *arg)
3011 snd_pcm_uframes_t *frames = arg;
3012 snd_pcm_sframes_t result;
3015 case SNDRV_PCM_IOCTL_FORWARD:
3017 /* provided only for OSS; capture-only and no value returned */
3018 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3020 result = snd_pcm_forward(substream, *frames);
3021 return result < 0 ? result : 0;
3023 case SNDRV_PCM_IOCTL_HW_PARAMS:
3024 return snd_pcm_hw_params(substream, arg);
3025 case SNDRV_PCM_IOCTL_SW_PARAMS:
3026 return snd_pcm_sw_params(substream, arg);
3027 case SNDRV_PCM_IOCTL_PREPARE:
3028 return snd_pcm_prepare(substream, NULL);
3029 case SNDRV_PCM_IOCTL_START:
3030 return snd_pcm_start_lock_irq(substream);
3031 case SNDRV_PCM_IOCTL_DRAIN:
3032 return snd_pcm_drain(substream, NULL);
3033 case SNDRV_PCM_IOCTL_DROP:
3034 return snd_pcm_drop(substream);
3035 case SNDRV_PCM_IOCTL_DELAY:
3036 return snd_pcm_delay(substream, frames);
3041 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3043 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3046 struct snd_pcm_file *pcm_file;
3047 struct snd_pcm_substream *substream;
3048 struct snd_pcm_runtime *runtime;
3049 snd_pcm_sframes_t result;
3051 pcm_file = file->private_data;
3052 substream = pcm_file->substream;
3053 if (PCM_RUNTIME_CHECK(substream))
3055 runtime = substream->runtime;
3056 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3058 if (!frame_aligned(runtime, count))
3060 count = bytes_to_frames(runtime, count);
3061 result = snd_pcm_lib_read(substream, buf, count);
3063 result = frames_to_bytes(runtime, result);
3067 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3068 size_t count, loff_t * offset)
3070 struct snd_pcm_file *pcm_file;
3071 struct snd_pcm_substream *substream;
3072 struct snd_pcm_runtime *runtime;
3073 snd_pcm_sframes_t result;
3075 pcm_file = file->private_data;
3076 substream = pcm_file->substream;
3077 if (PCM_RUNTIME_CHECK(substream))
3079 runtime = substream->runtime;
3080 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3082 if (!frame_aligned(runtime, count))
3084 count = bytes_to_frames(runtime, count);
3085 result = snd_pcm_lib_write(substream, buf, count);
3087 result = frames_to_bytes(runtime, result);
3091 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
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;
3099 snd_pcm_uframes_t frames;
3101 pcm_file = iocb->ki_filp->private_data;
3102 substream = pcm_file->substream;
3103 if (PCM_RUNTIME_CHECK(substream))
3105 runtime = substream->runtime;
3106 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3108 if (!iter_is_iovec(to))
3110 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3112 if (!frame_aligned(runtime, to->iov->iov_len))
3114 frames = bytes_to_samples(runtime, to->iov->iov_len);
3115 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3118 for (i = 0; i < to->nr_segs; ++i)
3119 bufs[i] = to->iov[i].iov_base;
3120 result = snd_pcm_lib_readv(substream, bufs, frames);
3122 result = frames_to_bytes(runtime, result);
3127 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3129 struct snd_pcm_file *pcm_file;
3130 struct snd_pcm_substream *substream;
3131 struct snd_pcm_runtime *runtime;
3132 snd_pcm_sframes_t result;
3135 snd_pcm_uframes_t frames;
3137 pcm_file = iocb->ki_filp->private_data;
3138 substream = pcm_file->substream;
3139 if (PCM_RUNTIME_CHECK(substream))
3141 runtime = substream->runtime;
3142 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3144 if (!iter_is_iovec(from))
3146 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3147 !frame_aligned(runtime, from->iov->iov_len))
3149 frames = bytes_to_samples(runtime, from->iov->iov_len);
3150 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3153 for (i = 0; i < from->nr_segs; ++i)
3154 bufs[i] = from->iov[i].iov_base;
3155 result = snd_pcm_lib_writev(substream, bufs, frames);
3157 result = frames_to_bytes(runtime, result);
3162 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3164 struct snd_pcm_file *pcm_file;
3165 struct snd_pcm_substream *substream;
3166 struct snd_pcm_runtime *runtime;
3168 snd_pcm_uframes_t avail;
3170 pcm_file = file->private_data;
3172 substream = pcm_file->substream;
3173 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3174 ok = EPOLLOUT | EPOLLWRNORM;
3176 ok = EPOLLIN | EPOLLRDNORM;
3177 if (PCM_RUNTIME_CHECK(substream))
3178 return ok | EPOLLERR;
3180 runtime = substream->runtime;
3181 poll_wait(file, &runtime->sleep, wait);
3184 snd_pcm_stream_lock_irq(substream);
3185 avail = snd_pcm_avail(substream);
3186 switch (runtime->status->state) {
3187 case SNDRV_PCM_STATE_RUNNING:
3188 case SNDRV_PCM_STATE_PREPARED:
3189 case SNDRV_PCM_STATE_PAUSED:
3190 if (avail >= runtime->control->avail_min)
3193 case SNDRV_PCM_STATE_DRAINING:
3194 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3201 mask = ok | EPOLLERR;
3204 snd_pcm_stream_unlock_irq(substream);
3213 * Only on coherent architectures, we can mmap the status and the control records
3214 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3216 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3218 * mmap status record
3220 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3222 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3223 struct snd_pcm_runtime *runtime;
3225 if (substream == NULL)
3226 return VM_FAULT_SIGBUS;
3227 runtime = substream->runtime;
3228 vmf->page = virt_to_page(runtime->status);
3229 get_page(vmf->page);
3233 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3235 .fault = snd_pcm_mmap_status_fault,
3238 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3239 struct vm_area_struct *area)
3242 if (!(area->vm_flags & VM_READ))
3244 size = area->vm_end - area->vm_start;
3245 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3247 area->vm_ops = &snd_pcm_vm_ops_status;
3248 area->vm_private_data = substream;
3249 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3254 * mmap control record
3256 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3258 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3259 struct snd_pcm_runtime *runtime;
3261 if (substream == NULL)
3262 return VM_FAULT_SIGBUS;
3263 runtime = substream->runtime;
3264 vmf->page = virt_to_page(runtime->control);
3265 get_page(vmf->page);
3269 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3271 .fault = snd_pcm_mmap_control_fault,
3274 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3275 struct vm_area_struct *area)
3278 if (!(area->vm_flags & VM_READ))
3280 size = area->vm_end - area->vm_start;
3281 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3283 area->vm_ops = &snd_pcm_vm_ops_control;
3284 area->vm_private_data = substream;
3285 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3289 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3291 if (pcm_file->no_compat_mmap)
3293 /* See pcm_control_mmap_allowed() below.
3294 * Since older alsa-lib requires both status and control mmaps to be
3295 * coupled, we have to disable the status mmap for old alsa-lib, too.
3297 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3298 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3303 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3305 if (pcm_file->no_compat_mmap)
3307 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3308 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3309 * thus it effectively assures the manual update of appl_ptr.
3311 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3316 #else /* ! coherent mmap */
3318 * don't support mmap for status and control records.
3320 #define pcm_status_mmap_allowed(pcm_file) false
3321 #define pcm_control_mmap_allowed(pcm_file) false
3323 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3324 struct vm_area_struct *area)
3328 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3329 struct vm_area_struct *area)
3333 #endif /* coherent mmap */
3335 static inline struct page *
3336 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3338 void *vaddr = substream->runtime->dma_area + ofs;
3340 switch (substream->dma_buffer.dev.type) {
3341 #ifdef CONFIG_SND_DMA_SGBUF
3342 case SNDRV_DMA_TYPE_DEV_SG:
3343 case SNDRV_DMA_TYPE_DEV_UC_SG:
3344 return snd_pcm_sgbuf_ops_page(substream, ofs);
3345 #endif /* CONFIG_SND_DMA_SGBUF */
3346 case SNDRV_DMA_TYPE_VMALLOC:
3347 return vmalloc_to_page(vaddr);
3349 return virt_to_page(vaddr);
3354 * fault callback for mmapping a RAM page
3356 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3358 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3359 struct snd_pcm_runtime *runtime;
3360 unsigned long offset;
3364 if (substream == NULL)
3365 return VM_FAULT_SIGBUS;
3366 runtime = substream->runtime;
3367 offset = vmf->pgoff << PAGE_SHIFT;
3368 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3369 if (offset > dma_bytes - PAGE_SIZE)
3370 return VM_FAULT_SIGBUS;
3371 if (substream->ops->page)
3372 page = substream->ops->page(substream, offset);
3374 page = snd_pcm_default_page_ops(substream, offset);
3376 return VM_FAULT_SIGBUS;
3382 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3383 .open = snd_pcm_mmap_data_open,
3384 .close = snd_pcm_mmap_data_close,
3387 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3388 .open = snd_pcm_mmap_data_open,
3389 .close = snd_pcm_mmap_data_close,
3390 .fault = snd_pcm_mmap_data_fault,
3394 * mmap the DMA buffer on RAM
3398 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3399 * @substream: PCM substream
3402 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3403 * this function is invoked implicitly.
3405 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3406 struct vm_area_struct *area)
3408 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3409 #ifdef CONFIG_GENERIC_ALLOCATOR
3410 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3411 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3412 return remap_pfn_range(area, area->vm_start,
3413 substream->dma_buffer.addr >> PAGE_SHIFT,
3414 area->vm_end - area->vm_start, area->vm_page_prot);
3416 #endif /* CONFIG_GENERIC_ALLOCATOR */
3417 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3418 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3419 (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3420 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3421 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3423 substream->runtime->dma_area,
3424 substream->runtime->dma_addr,
3425 substream->runtime->dma_bytes);
3426 #endif /* CONFIG_X86 */
3427 /* mmap with fault handler */
3428 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3431 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3434 * mmap the DMA buffer on I/O memory area
3436 #if SNDRV_PCM_INFO_MMAP_IOMEM
3438 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3439 * @substream: PCM substream
3442 * When your hardware uses the iomapped pages as the hardware buffer and
3443 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3444 * is supposed to work only on limited architectures.
3446 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3447 struct vm_area_struct *area)
3449 struct snd_pcm_runtime *runtime = substream->runtime;
3451 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3452 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3454 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3455 #endif /* SNDRV_PCM_INFO_MMAP */
3460 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3461 struct vm_area_struct *area)
3463 struct snd_pcm_runtime *runtime;
3465 unsigned long offset;
3469 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3470 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3473 if (!(area->vm_flags & VM_READ))
3476 runtime = substream->runtime;
3477 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3479 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3481 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3482 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3484 size = area->vm_end - area->vm_start;
3485 offset = area->vm_pgoff << PAGE_SHIFT;
3486 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3487 if ((size_t)size > dma_bytes)
3489 if (offset > dma_bytes - size)
3492 area->vm_ops = &snd_pcm_vm_ops_data;
3493 area->vm_private_data = substream;
3494 if (substream->ops->mmap)
3495 err = substream->ops->mmap(substream, area);
3497 err = snd_pcm_lib_default_mmap(substream, area);
3499 atomic_inc(&substream->mmap_count);
3502 EXPORT_SYMBOL(snd_pcm_mmap_data);
3504 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3506 struct snd_pcm_file * pcm_file;
3507 struct snd_pcm_substream *substream;
3508 unsigned long offset;
3510 pcm_file = file->private_data;
3511 substream = pcm_file->substream;
3512 if (PCM_RUNTIME_CHECK(substream))
3515 offset = area->vm_pgoff << PAGE_SHIFT;
3517 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3518 if (!pcm_status_mmap_allowed(pcm_file))
3520 return snd_pcm_mmap_status(substream, file, area);
3521 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3522 if (!pcm_control_mmap_allowed(pcm_file))
3524 return snd_pcm_mmap_control(substream, file, area);
3526 return snd_pcm_mmap_data(substream, file, area);
3531 static int snd_pcm_fasync(int fd, struct file * file, int on)
3533 struct snd_pcm_file * pcm_file;
3534 struct snd_pcm_substream *substream;
3535 struct snd_pcm_runtime *runtime;
3537 pcm_file = file->private_data;
3538 substream = pcm_file->substream;
3539 if (PCM_RUNTIME_CHECK(substream))
3541 runtime = substream->runtime;
3542 return fasync_helper(fd, file, on, &runtime->fasync);
3548 #ifdef CONFIG_COMPAT
3549 #include "pcm_compat.c"
3551 #define snd_pcm_ioctl_compat NULL
3555 * To be removed helpers to keep binary compatibility
3558 #ifdef CONFIG_SND_SUPPORT_OLD_API
3559 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3560 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3562 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3563 struct snd_pcm_hw_params_old *oparams)
3567 memset(params, 0, sizeof(*params));
3568 params->flags = oparams->flags;
3569 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3570 params->masks[i].bits[0] = oparams->masks[i];
3571 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3572 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3573 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3574 params->info = oparams->info;
3575 params->msbits = oparams->msbits;
3576 params->rate_num = oparams->rate_num;
3577 params->rate_den = oparams->rate_den;
3578 params->fifo_size = oparams->fifo_size;
3581 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3582 struct snd_pcm_hw_params *params)
3586 memset(oparams, 0, sizeof(*oparams));
3587 oparams->flags = params->flags;
3588 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3589 oparams->masks[i] = params->masks[i].bits[0];
3590 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3591 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3592 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3593 oparams->info = params->info;
3594 oparams->msbits = params->msbits;
3595 oparams->rate_num = params->rate_num;
3596 oparams->rate_den = params->rate_den;
3597 oparams->fifo_size = params->fifo_size;
3600 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3601 struct snd_pcm_hw_params_old __user * _oparams)
3603 struct snd_pcm_hw_params *params;
3604 struct snd_pcm_hw_params_old *oparams = NULL;
3607 params = kmalloc(sizeof(*params), GFP_KERNEL);
3611 oparams = memdup_user(_oparams, sizeof(*oparams));
3612 if (IS_ERR(oparams)) {
3613 err = PTR_ERR(oparams);
3616 snd_pcm_hw_convert_from_old_params(params, oparams);
3617 err = snd_pcm_hw_refine(substream, params);
3621 err = fixup_unreferenced_params(substream, params);
3625 snd_pcm_hw_convert_to_old_params(oparams, params);
3626 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3635 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3636 struct snd_pcm_hw_params_old __user * _oparams)
3638 struct snd_pcm_hw_params *params;
3639 struct snd_pcm_hw_params_old *oparams = NULL;
3642 params = kmalloc(sizeof(*params), GFP_KERNEL);
3646 oparams = memdup_user(_oparams, sizeof(*oparams));
3647 if (IS_ERR(oparams)) {
3648 err = PTR_ERR(oparams);
3652 snd_pcm_hw_convert_from_old_params(params, oparams);
3653 err = snd_pcm_hw_params(substream, params);
3657 snd_pcm_hw_convert_to_old_params(oparams, params);
3658 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3666 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3669 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3672 unsigned long pgoff,
3673 unsigned long flags)
3675 struct snd_pcm_file *pcm_file = file->private_data;
3676 struct snd_pcm_substream *substream = pcm_file->substream;
3677 struct snd_pcm_runtime *runtime = substream->runtime;
3678 unsigned long offset = pgoff << PAGE_SHIFT;
3681 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3682 return (unsigned long)runtime->status;
3683 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3684 return (unsigned long)runtime->control;
3686 return (unsigned long)runtime->dma_area + offset;
3690 # define snd_pcm_get_unmapped_area NULL
3697 const struct file_operations snd_pcm_f_ops[2] = {
3699 .owner = THIS_MODULE,
3700 .write = snd_pcm_write,
3701 .write_iter = snd_pcm_writev,
3702 .open = snd_pcm_playback_open,
3703 .release = snd_pcm_release,
3704 .llseek = no_llseek,
3705 .poll = snd_pcm_poll,
3706 .unlocked_ioctl = snd_pcm_ioctl,
3707 .compat_ioctl = snd_pcm_ioctl_compat,
3708 .mmap = snd_pcm_mmap,
3709 .fasync = snd_pcm_fasync,
3710 .get_unmapped_area = snd_pcm_get_unmapped_area,
3713 .owner = THIS_MODULE,
3714 .read = snd_pcm_read,
3715 .read_iter = snd_pcm_readv,
3716 .open = snd_pcm_capture_open,
3717 .release = snd_pcm_release,
3718 .llseek = no_llseek,
3719 .poll = snd_pcm_poll,
3720 .unlocked_ioctl = snd_pcm_ioctl,
3721 .compat_ioctl = snd_pcm_ioctl_compat,
3722 .mmap = snd_pcm_mmap,
3723 .fasync = snd_pcm_fasync,
3724 .get_unmapped_area = snd_pcm_get_unmapped_area,