2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.h>
39 #include <linux/delay.h>
41 #include "pcm_local.h"
43 #ifdef CONFIG_SND_DEBUG
44 #define CREATE_TRACE_POINTS
45 #include "pcm_param_trace.h"
47 #define trace_hw_mask_param_enabled() 0
48 #define trace_hw_interval_param_enabled() 0
49 #define trace_hw_mask_param(substream, type, index, prev, curr)
50 #define trace_hw_interval_param(substream, type, index, prev, curr)
57 struct snd_pcm_hw_params_old {
59 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
60 SNDRV_PCM_HW_PARAM_ACCESS + 1];
61 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
62 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
67 unsigned int rate_num;
68 unsigned int rate_den;
69 snd_pcm_uframes_t fifo_size;
70 unsigned char reserved[64];
73 #ifdef CONFIG_SND_SUPPORT_OLD_API
74 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
75 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
77 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
78 struct snd_pcm_hw_params_old __user * _oparams);
79 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params_old __user * _oparams);
82 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
88 static DEFINE_RWLOCK(snd_pcm_link_rwlock);
89 static DECLARE_RWSEM(snd_pcm_link_rwsem);
91 /* Writer in rwsem may block readers even during its waiting in queue,
92 * and this may lead to a deadlock when the code path takes read sem
93 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
94 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
95 * sleep until all the readers are completed without blocking by writer.
97 static inline void down_write_nonfifo(struct rw_semaphore *lock)
99 while (!down_write_trylock(lock))
103 #define PCM_LOCK_DEFAULT 0
104 #define PCM_LOCK_IRQ 1
105 #define PCM_LOCK_IRQSAVE 2
107 static unsigned long __snd_pcm_stream_lock_mode(struct snd_pcm_substream *substream,
110 unsigned long flags = 0;
111 if (substream->pcm->nonatomic) {
112 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
113 mutex_lock(&substream->self_group.mutex);
116 case PCM_LOCK_DEFAULT:
117 read_lock(&snd_pcm_link_rwlock);
120 read_lock_irq(&snd_pcm_link_rwlock);
122 case PCM_LOCK_IRQSAVE:
123 read_lock_irqsave(&snd_pcm_link_rwlock, flags);
126 spin_lock(&substream->self_group.lock);
131 static void __snd_pcm_stream_unlock_mode(struct snd_pcm_substream *substream,
132 unsigned int mode, unsigned long flags)
134 if (substream->pcm->nonatomic) {
135 mutex_unlock(&substream->self_group.mutex);
136 up_read(&snd_pcm_link_rwsem);
138 spin_unlock(&substream->self_group.lock);
141 case PCM_LOCK_DEFAULT:
142 read_unlock(&snd_pcm_link_rwlock);
145 read_unlock_irq(&snd_pcm_link_rwlock);
147 case PCM_LOCK_IRQSAVE:
148 read_unlock_irqrestore(&snd_pcm_link_rwlock, flags);
155 * snd_pcm_stream_lock - Lock the PCM stream
156 * @substream: PCM substream
158 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
159 * flag of the given substream. This also takes the global link rw lock
160 * (or rw sem), too, for avoiding the race with linked streams.
162 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
164 __snd_pcm_stream_lock_mode(substream, PCM_LOCK_DEFAULT);
166 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
169 * snd_pcm_stream_lock - Unlock the PCM stream
170 * @substream: PCM substream
172 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
174 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
176 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_DEFAULT, 0);
178 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
181 * snd_pcm_stream_lock_irq - Lock the PCM stream
182 * @substream: PCM substream
184 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
185 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
186 * as snd_pcm_stream_lock().
188 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
190 __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQ);
192 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
195 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
196 * @substream: PCM substream
198 * This is a counter-part of snd_pcm_stream_lock_irq().
200 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
202 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQ, 0);
204 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
206 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
208 return __snd_pcm_stream_lock_mode(substream, PCM_LOCK_IRQSAVE);
210 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
213 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
214 * @substream: PCM substream
217 * This is a counter-part of snd_pcm_stream_lock_irqsave().
219 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
222 __snd_pcm_stream_unlock_mode(substream, PCM_LOCK_IRQSAVE, flags);
224 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
226 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
228 struct snd_pcm *pcm = substream->pcm;
229 struct snd_pcm_str *pstr = substream->pstr;
231 memset(info, 0, sizeof(*info));
232 info->card = pcm->card->number;
233 info->device = pcm->device;
234 info->stream = substream->stream;
235 info->subdevice = substream->number;
236 strlcpy(info->id, pcm->id, sizeof(info->id));
237 strlcpy(info->name, pcm->name, sizeof(info->name));
238 info->dev_class = pcm->dev_class;
239 info->dev_subclass = pcm->dev_subclass;
240 info->subdevices_count = pstr->substream_count;
241 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
242 strlcpy(info->subname, substream->name, sizeof(info->subname));
247 int snd_pcm_info_user(struct snd_pcm_substream *substream,
248 struct snd_pcm_info __user * _info)
250 struct snd_pcm_info *info;
253 info = kmalloc(sizeof(*info), GFP_KERNEL);
256 err = snd_pcm_info(substream, info);
258 if (copy_to_user(_info, info, sizeof(*info)))
265 static bool hw_support_mmap(struct snd_pcm_substream *substream)
267 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
269 /* architecture supports dma_mmap_coherent()? */
270 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
271 if (!substream->ops->mmap &&
272 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
278 static int constrain_mask_params(struct snd_pcm_substream *substream,
279 struct snd_pcm_hw_params *params)
281 struct snd_pcm_hw_constraints *constrs =
282 &substream->runtime->hw_constraints;
285 struct snd_mask old_mask;
288 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
289 m = hw_param_mask(params, k);
290 if (snd_mask_empty(m))
293 /* This parameter is not requested to change by a caller. */
294 if (!(params->rmask & (1 << k)))
297 if (trace_hw_mask_param_enabled())
300 changed = snd_mask_refine(m, constrs_mask(constrs, k));
306 /* Set corresponding flag so that the caller gets it. */
307 trace_hw_mask_param(substream, k, 0, &old_mask, m);
308 params->cmask |= 1 << k;
314 static int constrain_interval_params(struct snd_pcm_substream *substream,
315 struct snd_pcm_hw_params *params)
317 struct snd_pcm_hw_constraints *constrs =
318 &substream->runtime->hw_constraints;
319 struct snd_interval *i;
321 struct snd_interval old_interval;
324 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
325 i = hw_param_interval(params, k);
326 if (snd_interval_empty(i))
329 /* This parameter is not requested to change by a caller. */
330 if (!(params->rmask & (1 << k)))
333 if (trace_hw_interval_param_enabled())
336 changed = snd_interval_refine(i, constrs_interval(constrs, k));
342 /* Set corresponding flag so that the caller gets it. */
343 trace_hw_interval_param(substream, k, 0, &old_interval, i);
344 params->cmask |= 1 << k;
350 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
351 struct snd_pcm_hw_params *params)
353 struct snd_pcm_hw_constraints *constrs =
354 &substream->runtime->hw_constraints;
356 unsigned int *rstamps;
357 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
359 struct snd_pcm_hw_rule *r;
361 struct snd_mask old_mask;
362 struct snd_interval old_interval;
364 int changed, err = 0;
367 * Each application of rule has own sequence number.
369 * Each member of 'rstamps' array represents the sequence number of
370 * recent application of corresponding rule.
372 rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
377 * Each member of 'vstamps' array represents the sequence number of
378 * recent application of rule in which corresponding parameters were
381 * In initial state, elements corresponding to parameters requested by
382 * a caller is 1. For unrequested parameters, corresponding members
383 * have 0 so that the parameters are never changed anymore.
385 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
386 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
388 /* Due to the above design, actual sequence number starts at 2. */
391 /* Apply all rules in order. */
393 for (k = 0; k < constrs->rules_num; k++) {
394 r = &constrs->rules[k];
397 * Check condition bits of this rule. When the rule has
398 * some condition bits, parameter without the bits is
399 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
400 * is an example of the condition bits.
402 if (r->cond && !(r->cond & params->flags))
406 * The 'deps' array includes maximum three dependencies
407 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
408 * member of this array is a sentinel and should be
411 * This rule should be processed in this time when dependent
412 * parameters were changed at former applications of the other
415 for (d = 0; r->deps[d] >= 0; d++) {
416 if (vstamps[r->deps[d]] > rstamps[k])
422 if (trace_hw_mask_param_enabled()) {
423 if (hw_is_mask(r->var))
424 old_mask = *hw_param_mask(params, r->var);
426 if (trace_hw_interval_param_enabled()) {
427 if (hw_is_interval(r->var))
428 old_interval = *hw_param_interval(params, r->var);
431 changed = r->func(params, r);
438 * When the parameter is changed, notify it to the caller
439 * by corresponding returned bit, then preparing for next
442 if (changed && r->var >= 0) {
443 if (hw_is_mask(r->var)) {
444 trace_hw_mask_param(substream, r->var,
446 hw_param_mask(params, r->var));
448 if (hw_is_interval(r->var)) {
449 trace_hw_interval_param(substream, r->var,
450 k + 1, &old_interval,
451 hw_param_interval(params, r->var));
454 params->cmask |= (1 << r->var);
455 vstamps[r->var] = stamp;
459 rstamps[k] = stamp++;
462 /* Iterate to evaluate all rules till no parameters are changed. */
471 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
472 struct snd_pcm_hw_params *params)
474 const struct snd_interval *i;
475 const struct snd_mask *m;
478 if (!params->msbits) {
479 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
480 if (snd_interval_single(i))
481 params->msbits = snd_interval_value(i);
484 if (!params->rate_den) {
485 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
486 if (snd_interval_single(i)) {
487 params->rate_num = snd_interval_value(i);
488 params->rate_den = 1;
492 if (!params->fifo_size) {
493 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
494 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
495 if (snd_mask_single(m) && snd_interval_single(i)) {
496 err = substream->ops->ioctl(substream,
497 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
504 params->info = substream->runtime->hw.info;
505 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
506 SNDRV_PCM_INFO_DRAIN_TRIGGER);
507 if (!hw_support_mmap(substream))
508 params->info &= ~(SNDRV_PCM_INFO_MMAP |
509 SNDRV_PCM_INFO_MMAP_VALID);
515 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
516 struct snd_pcm_hw_params *params)
521 params->fifo_size = 0;
522 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
524 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
525 params->rate_num = 0;
526 params->rate_den = 0;
529 err = constrain_mask_params(substream, params);
533 err = constrain_interval_params(substream, params);
537 err = constrain_params_by_rules(substream, params);
545 EXPORT_SYMBOL(snd_pcm_hw_refine);
547 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
548 struct snd_pcm_hw_params __user * _params)
550 struct snd_pcm_hw_params *params;
553 params = memdup_user(_params, sizeof(*params));
555 return PTR_ERR(params);
557 err = snd_pcm_hw_refine(substream, params);
561 err = fixup_unreferenced_params(substream, params);
565 if (copy_to_user(_params, params, sizeof(*params)))
572 static int period_to_usecs(struct snd_pcm_runtime *runtime)
577 return -1; /* invalid */
579 /* take 75% of period time as the deadline */
580 usecs = (750000 / runtime->rate) * runtime->period_size;
581 usecs += ((750000 % runtime->rate) * runtime->period_size) /
587 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
589 snd_pcm_stream_lock_irq(substream);
590 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
591 substream->runtime->status->state = state;
592 snd_pcm_stream_unlock_irq(substream);
595 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
598 #ifdef CONFIG_SND_PCM_TIMER
599 if (substream->timer)
600 snd_timer_notify(substream->timer, event,
601 &substream->runtime->trigger_tstamp);
606 * snd_pcm_hw_param_choose - choose a configuration defined by @params
608 * @params: the hw_params instance
610 * Choose one configuration from configuration space defined by @params.
611 * The configuration chosen is that obtained fixing in this order:
612 * first access, first format, first subformat, min channels,
613 * min rate, min period time, max buffer size, min tick time
615 * Return: Zero if successful, or a negative error code on failure.
617 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
618 struct snd_pcm_hw_params *params)
620 static const int vars[] = {
621 SNDRV_PCM_HW_PARAM_ACCESS,
622 SNDRV_PCM_HW_PARAM_FORMAT,
623 SNDRV_PCM_HW_PARAM_SUBFORMAT,
624 SNDRV_PCM_HW_PARAM_CHANNELS,
625 SNDRV_PCM_HW_PARAM_RATE,
626 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
627 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
628 SNDRV_PCM_HW_PARAM_TICK_TIME,
632 struct snd_mask old_mask;
633 struct snd_interval old_interval;
636 for (v = vars; *v != -1; v++) {
637 /* Keep old parameter to trace. */
638 if (trace_hw_mask_param_enabled()) {
640 old_mask = *hw_param_mask(params, *v);
642 if (trace_hw_interval_param_enabled()) {
643 if (hw_is_interval(*v))
644 old_interval = *hw_param_interval(params, *v);
646 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
647 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
649 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
655 /* Trace the changed parameter. */
656 if (hw_is_mask(*v)) {
657 trace_hw_mask_param(pcm, *v, 0, &old_mask,
658 hw_param_mask(params, *v));
660 if (hw_is_interval(*v)) {
661 trace_hw_interval_param(pcm, *v, 0, &old_interval,
662 hw_param_interval(params, *v));
669 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
670 struct snd_pcm_hw_params *params)
672 struct snd_pcm_runtime *runtime;
675 snd_pcm_uframes_t frames;
677 if (PCM_RUNTIME_CHECK(substream))
679 runtime = substream->runtime;
680 snd_pcm_stream_lock_irq(substream);
681 switch (runtime->status->state) {
682 case SNDRV_PCM_STATE_OPEN:
683 case SNDRV_PCM_STATE_SETUP:
684 case SNDRV_PCM_STATE_PREPARED:
687 snd_pcm_stream_unlock_irq(substream);
690 snd_pcm_stream_unlock_irq(substream);
691 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
692 if (!substream->oss.oss)
694 if (atomic_read(&substream->mmap_count))
698 err = snd_pcm_hw_refine(substream, params);
702 err = snd_pcm_hw_params_choose(substream, params);
706 err = fixup_unreferenced_params(substream, params);
710 if (substream->ops->hw_params != NULL) {
711 err = substream->ops->hw_params(substream, params);
716 runtime->access = params_access(params);
717 runtime->format = params_format(params);
718 runtime->subformat = params_subformat(params);
719 runtime->channels = params_channels(params);
720 runtime->rate = params_rate(params);
721 runtime->period_size = params_period_size(params);
722 runtime->periods = params_periods(params);
723 runtime->buffer_size = params_buffer_size(params);
724 runtime->info = params->info;
725 runtime->rate_num = params->rate_num;
726 runtime->rate_den = params->rate_den;
727 runtime->no_period_wakeup =
728 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
729 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
731 bits = snd_pcm_format_physical_width(runtime->format);
732 runtime->sample_bits = bits;
733 bits *= runtime->channels;
734 runtime->frame_bits = bits;
736 while (bits % 8 != 0) {
740 runtime->byte_align = bits / 8;
741 runtime->min_align = frames;
743 /* Default sw params */
744 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
745 runtime->period_step = 1;
746 runtime->control->avail_min = runtime->period_size;
747 runtime->start_threshold = 1;
748 runtime->stop_threshold = runtime->buffer_size;
749 runtime->silence_threshold = 0;
750 runtime->silence_size = 0;
751 runtime->boundary = runtime->buffer_size;
752 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
753 runtime->boundary *= 2;
755 snd_pcm_timer_resolution_change(substream);
756 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
758 if (pm_qos_request_active(&substream->latency_pm_qos_req))
759 pm_qos_remove_request(&substream->latency_pm_qos_req);
760 if ((usecs = period_to_usecs(runtime)) >= 0)
761 pm_qos_add_request(&substream->latency_pm_qos_req,
762 PM_QOS_CPU_DMA_LATENCY, usecs);
765 /* hardware might be unusable from this time,
766 so we force application to retry to set
767 the correct hardware parameter settings */
768 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
769 if (substream->ops->hw_free != NULL)
770 substream->ops->hw_free(substream);
774 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
775 struct snd_pcm_hw_params __user * _params)
777 struct snd_pcm_hw_params *params;
780 params = memdup_user(_params, sizeof(*params));
782 return PTR_ERR(params);
784 err = snd_pcm_hw_params(substream, params);
788 if (copy_to_user(_params, params, sizeof(*params)))
795 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
797 struct snd_pcm_runtime *runtime;
800 if (PCM_RUNTIME_CHECK(substream))
802 runtime = substream->runtime;
803 snd_pcm_stream_lock_irq(substream);
804 switch (runtime->status->state) {
805 case SNDRV_PCM_STATE_SETUP:
806 case SNDRV_PCM_STATE_PREPARED:
809 snd_pcm_stream_unlock_irq(substream);
812 snd_pcm_stream_unlock_irq(substream);
813 if (atomic_read(&substream->mmap_count))
815 if (substream->ops->hw_free)
816 result = substream->ops->hw_free(substream);
817 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
818 pm_qos_remove_request(&substream->latency_pm_qos_req);
822 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
823 struct snd_pcm_sw_params *params)
825 struct snd_pcm_runtime *runtime;
828 if (PCM_RUNTIME_CHECK(substream))
830 runtime = substream->runtime;
831 snd_pcm_stream_lock_irq(substream);
832 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
833 snd_pcm_stream_unlock_irq(substream);
836 snd_pcm_stream_unlock_irq(substream);
838 if (params->tstamp_mode < 0 ||
839 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
841 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
842 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
844 if (params->avail_min == 0)
846 if (params->silence_size >= runtime->boundary) {
847 if (params->silence_threshold != 0)
850 if (params->silence_size > params->silence_threshold)
852 if (params->silence_threshold > runtime->buffer_size)
856 snd_pcm_stream_lock_irq(substream);
857 runtime->tstamp_mode = params->tstamp_mode;
858 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
859 runtime->tstamp_type = params->tstamp_type;
860 runtime->period_step = params->period_step;
861 runtime->control->avail_min = params->avail_min;
862 runtime->start_threshold = params->start_threshold;
863 runtime->stop_threshold = params->stop_threshold;
864 runtime->silence_threshold = params->silence_threshold;
865 runtime->silence_size = params->silence_size;
866 params->boundary = runtime->boundary;
867 if (snd_pcm_running(substream)) {
868 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
869 runtime->silence_size > 0)
870 snd_pcm_playback_silence(substream, ULONG_MAX);
871 err = snd_pcm_update_state(substream, runtime);
873 snd_pcm_stream_unlock_irq(substream);
877 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
878 struct snd_pcm_sw_params __user * _params)
880 struct snd_pcm_sw_params params;
882 if (copy_from_user(¶ms, _params, sizeof(params)))
884 err = snd_pcm_sw_params(substream, ¶ms);
885 if (copy_to_user(_params, ¶ms, sizeof(params)))
890 static inline snd_pcm_uframes_t
891 snd_pcm_calc_delay(struct snd_pcm_substream *substream)
893 snd_pcm_uframes_t delay;
895 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
896 delay = snd_pcm_playback_hw_avail(substream->runtime);
898 delay = snd_pcm_capture_avail(substream->runtime);
899 return delay + substream->runtime->delay;
902 int snd_pcm_status(struct snd_pcm_substream *substream,
903 struct snd_pcm_status *status)
905 struct snd_pcm_runtime *runtime = substream->runtime;
907 snd_pcm_stream_lock_irq(substream);
909 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
910 &runtime->audio_tstamp_config);
912 /* backwards compatible behavior */
913 if (runtime->audio_tstamp_config.type_requested ==
914 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
915 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
916 runtime->audio_tstamp_config.type_requested =
917 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
919 runtime->audio_tstamp_config.type_requested =
920 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
921 runtime->audio_tstamp_report.valid = 0;
923 runtime->audio_tstamp_report.valid = 1;
925 status->state = runtime->status->state;
926 status->suspended_state = runtime->status->suspended_state;
927 if (status->state == SNDRV_PCM_STATE_OPEN)
929 status->trigger_tstamp = runtime->trigger_tstamp;
930 if (snd_pcm_running(substream)) {
931 snd_pcm_update_hw_ptr(substream);
932 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
933 status->tstamp = runtime->status->tstamp;
934 status->driver_tstamp = runtime->driver_tstamp;
935 status->audio_tstamp =
936 runtime->status->audio_tstamp;
937 if (runtime->audio_tstamp_report.valid == 1)
938 /* backwards compatibility, no report provided in COMPAT mode */
939 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
940 &status->audio_tstamp_accuracy,
941 &runtime->audio_tstamp_report);
946 /* get tstamp only in fallback mode and only if enabled */
947 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
948 snd_pcm_gettime(runtime, &status->tstamp);
951 status->appl_ptr = runtime->control->appl_ptr;
952 status->hw_ptr = runtime->status->hw_ptr;
953 status->avail = snd_pcm_avail(substream);
954 status->delay = snd_pcm_running(substream) ?
955 snd_pcm_calc_delay(substream) : 0;
956 status->avail_max = runtime->avail_max;
957 status->overrange = runtime->overrange;
958 runtime->avail_max = 0;
959 runtime->overrange = 0;
961 snd_pcm_stream_unlock_irq(substream);
965 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
966 struct snd_pcm_status __user * _status,
969 struct snd_pcm_status status;
972 memset(&status, 0, sizeof(status));
974 * with extension, parameters are read/write,
975 * get audio_tstamp_data from user,
976 * ignore rest of status structure
978 if (ext && get_user(status.audio_tstamp_data,
979 (u32 __user *)(&_status->audio_tstamp_data)))
981 res = snd_pcm_status(substream, &status);
984 if (copy_to_user(_status, &status, sizeof(status)))
989 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
990 struct snd_pcm_channel_info * info)
992 struct snd_pcm_runtime *runtime;
993 unsigned int channel;
995 channel = info->channel;
996 runtime = substream->runtime;
997 snd_pcm_stream_lock_irq(substream);
998 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
999 snd_pcm_stream_unlock_irq(substream);
1002 snd_pcm_stream_unlock_irq(substream);
1003 if (channel >= runtime->channels)
1005 memset(info, 0, sizeof(*info));
1006 info->channel = channel;
1007 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1010 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1011 struct snd_pcm_channel_info __user * _info)
1013 struct snd_pcm_channel_info info;
1016 if (copy_from_user(&info, _info, sizeof(info)))
1018 res = snd_pcm_channel_info(substream, &info);
1021 if (copy_to_user(_info, &info, sizeof(info)))
1026 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1028 struct snd_pcm_runtime *runtime = substream->runtime;
1029 if (runtime->trigger_master == NULL)
1031 if (runtime->trigger_master == substream) {
1032 if (!runtime->trigger_tstamp_latched)
1033 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1035 snd_pcm_trigger_tstamp(runtime->trigger_master);
1036 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1038 runtime->trigger_master = NULL;
1042 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1043 int (*do_action)(struct snd_pcm_substream *substream, int state);
1044 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1045 void (*post_action)(struct snd_pcm_substream *substream, int state);
1049 * this functions is core for handling of linked stream
1050 * Note: the stream state might be changed also on failure
1051 * Note2: call with calling stream lock + link lock
1053 static int snd_pcm_action_group(const struct action_ops *ops,
1054 struct snd_pcm_substream *substream,
1055 int state, int do_lock)
1057 struct snd_pcm_substream *s = NULL;
1058 struct snd_pcm_substream *s1;
1059 int res = 0, depth = 1;
1061 snd_pcm_group_for_each_entry(s, substream) {
1062 if (do_lock && s != substream) {
1063 if (s->pcm->nonatomic)
1064 mutex_lock_nested(&s->self_group.mutex, depth);
1066 spin_lock_nested(&s->self_group.lock, depth);
1069 res = ops->pre_action(s, state);
1073 snd_pcm_group_for_each_entry(s, substream) {
1074 res = ops->do_action(s, state);
1076 if (ops->undo_action) {
1077 snd_pcm_group_for_each_entry(s1, substream) {
1078 if (s1 == s) /* failed stream */
1080 ops->undo_action(s1, state);
1083 s = NULL; /* unlock all */
1087 snd_pcm_group_for_each_entry(s, substream) {
1088 ops->post_action(s, state);
1092 /* unlock streams */
1093 snd_pcm_group_for_each_entry(s1, substream) {
1094 if (s1 != substream) {
1095 if (s1->pcm->nonatomic)
1096 mutex_unlock(&s1->self_group.mutex);
1098 spin_unlock(&s1->self_group.lock);
1100 if (s1 == s) /* end */
1108 * Note: call with stream lock
1110 static int snd_pcm_action_single(const struct action_ops *ops,
1111 struct snd_pcm_substream *substream,
1116 res = ops->pre_action(substream, state);
1119 res = ops->do_action(substream, state);
1121 ops->post_action(substream, state);
1122 else if (ops->undo_action)
1123 ops->undo_action(substream, state);
1128 * Note: call with stream lock
1130 static int snd_pcm_action(const struct action_ops *ops,
1131 struct snd_pcm_substream *substream,
1136 if (!snd_pcm_stream_linked(substream))
1137 return snd_pcm_action_single(ops, substream, state);
1139 if (substream->pcm->nonatomic) {
1140 if (!mutex_trylock(&substream->group->mutex)) {
1141 mutex_unlock(&substream->self_group.mutex);
1142 mutex_lock(&substream->group->mutex);
1143 mutex_lock(&substream->self_group.mutex);
1145 res = snd_pcm_action_group(ops, substream, state, 1);
1146 mutex_unlock(&substream->group->mutex);
1148 if (!spin_trylock(&substream->group->lock)) {
1149 spin_unlock(&substream->self_group.lock);
1150 spin_lock(&substream->group->lock);
1151 spin_lock(&substream->self_group.lock);
1153 res = snd_pcm_action_group(ops, substream, state, 1);
1154 spin_unlock(&substream->group->lock);
1160 * Note: don't use any locks before
1162 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1163 struct snd_pcm_substream *substream,
1168 snd_pcm_stream_lock_irq(substream);
1169 res = snd_pcm_action(ops, substream, state);
1170 snd_pcm_stream_unlock_irq(substream);
1176 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1177 struct snd_pcm_substream *substream,
1182 down_read(&snd_pcm_link_rwsem);
1183 if (snd_pcm_stream_linked(substream))
1184 res = snd_pcm_action_group(ops, substream, state, 0);
1186 res = snd_pcm_action_single(ops, substream, state);
1187 up_read(&snd_pcm_link_rwsem);
1194 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1196 struct snd_pcm_runtime *runtime = substream->runtime;
1197 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1199 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1200 !snd_pcm_playback_data(substream))
1202 runtime->trigger_tstamp_latched = false;
1203 runtime->trigger_master = substream;
1207 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1209 if (substream->runtime->trigger_master != substream)
1211 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1214 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1216 if (substream->runtime->trigger_master == substream)
1217 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1220 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1222 struct snd_pcm_runtime *runtime = substream->runtime;
1223 snd_pcm_trigger_tstamp(substream);
1224 runtime->hw_ptr_jiffies = jiffies;
1225 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1227 runtime->status->state = state;
1228 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1229 runtime->silence_size > 0)
1230 snd_pcm_playback_silence(substream, ULONG_MAX);
1231 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1234 static const struct action_ops snd_pcm_action_start = {
1235 .pre_action = snd_pcm_pre_start,
1236 .do_action = snd_pcm_do_start,
1237 .undo_action = snd_pcm_undo_start,
1238 .post_action = snd_pcm_post_start
1242 * snd_pcm_start - start all linked streams
1243 * @substream: the PCM substream instance
1245 * Return: Zero if successful, or a negative error code.
1246 * The stream lock must be acquired before calling this function.
1248 int snd_pcm_start(struct snd_pcm_substream *substream)
1250 return snd_pcm_action(&snd_pcm_action_start, substream,
1251 SNDRV_PCM_STATE_RUNNING);
1254 /* take the stream lock and start the streams */
1255 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1257 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1258 SNDRV_PCM_STATE_RUNNING);
1264 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1266 struct snd_pcm_runtime *runtime = substream->runtime;
1267 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1269 runtime->trigger_master = substream;
1273 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1275 if (substream->runtime->trigger_master == substream &&
1276 snd_pcm_running(substream))
1277 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1278 return 0; /* unconditonally stop all substreams */
1281 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1283 struct snd_pcm_runtime *runtime = substream->runtime;
1284 if (runtime->status->state != state) {
1285 snd_pcm_trigger_tstamp(substream);
1286 runtime->status->state = state;
1287 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1289 wake_up(&runtime->sleep);
1290 wake_up(&runtime->tsleep);
1293 static const struct action_ops snd_pcm_action_stop = {
1294 .pre_action = snd_pcm_pre_stop,
1295 .do_action = snd_pcm_do_stop,
1296 .post_action = snd_pcm_post_stop
1300 * snd_pcm_stop - try to stop all running streams in the substream group
1301 * @substream: the PCM substream instance
1302 * @state: PCM state after stopping the stream
1304 * The state of each stream is then changed to the given state unconditionally.
1306 * Return: Zero if successful, or a negative error code.
1308 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1310 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1312 EXPORT_SYMBOL(snd_pcm_stop);
1315 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1316 * @substream: the PCM substream
1318 * After stopping, the state is changed to SETUP.
1319 * Unlike snd_pcm_stop(), this affects only the given stream.
1321 * Return: Zero if succesful, or a negative error code.
1323 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1325 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1326 SNDRV_PCM_STATE_SETUP);
1330 * snd_pcm_stop_xrun - stop the running streams as XRUN
1331 * @substream: the PCM substream instance
1333 * This stops the given running substream (and all linked substreams) as XRUN.
1334 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1336 * Return: Zero if successful, or a negative error code.
1338 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1340 unsigned long flags;
1342 snd_pcm_stream_lock_irqsave(substream, flags);
1343 if (substream->runtime && snd_pcm_running(substream))
1344 __snd_pcm_xrun(substream);
1345 snd_pcm_stream_unlock_irqrestore(substream, flags);
1348 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1353 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1355 struct snd_pcm_runtime *runtime = substream->runtime;
1356 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1359 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1361 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1363 runtime->trigger_master = substream;
1367 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1369 if (substream->runtime->trigger_master != substream)
1371 /* some drivers might use hw_ptr to recover from the pause -
1372 update the hw_ptr now */
1374 snd_pcm_update_hw_ptr(substream);
1375 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1376 * a delta between the current jiffies, this gives a large enough
1377 * delta, effectively to skip the check once.
1379 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1380 return substream->ops->trigger(substream,
1381 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1382 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1385 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1387 if (substream->runtime->trigger_master == substream)
1388 substream->ops->trigger(substream,
1389 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1390 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1393 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1395 struct snd_pcm_runtime *runtime = substream->runtime;
1396 snd_pcm_trigger_tstamp(substream);
1398 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1399 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1400 wake_up(&runtime->sleep);
1401 wake_up(&runtime->tsleep);
1403 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1404 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1408 static const struct action_ops snd_pcm_action_pause = {
1409 .pre_action = snd_pcm_pre_pause,
1410 .do_action = snd_pcm_do_pause,
1411 .undo_action = snd_pcm_undo_pause,
1412 .post_action = snd_pcm_post_pause
1416 * Push/release the pause for all linked streams.
1418 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1420 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1426 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1428 struct snd_pcm_runtime *runtime = substream->runtime;
1429 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1431 runtime->trigger_master = substream;
1435 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1437 struct snd_pcm_runtime *runtime = substream->runtime;
1438 if (runtime->trigger_master != substream)
1440 if (! snd_pcm_running(substream))
1442 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1443 return 0; /* suspend unconditionally */
1446 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1448 struct snd_pcm_runtime *runtime = substream->runtime;
1449 snd_pcm_trigger_tstamp(substream);
1450 runtime->status->suspended_state = runtime->status->state;
1451 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1452 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1453 wake_up(&runtime->sleep);
1454 wake_up(&runtime->tsleep);
1457 static const struct action_ops snd_pcm_action_suspend = {
1458 .pre_action = snd_pcm_pre_suspend,
1459 .do_action = snd_pcm_do_suspend,
1460 .post_action = snd_pcm_post_suspend
1464 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1465 * @substream: the PCM substream
1467 * After this call, all streams are changed to SUSPENDED state.
1469 * Return: Zero if successful, or a negative error code.
1471 static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1474 unsigned long flags;
1476 snd_pcm_stream_lock_irqsave(substream, flags);
1477 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1478 snd_pcm_stream_unlock_irqrestore(substream, flags);
1483 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1484 * @pcm: the PCM instance
1486 * After this call, all streams are changed to SUSPENDED state.
1488 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1490 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1492 struct snd_pcm_substream *substream;
1493 int stream, err = 0;
1498 for (stream = 0; stream < 2; stream++) {
1499 for (substream = pcm->streams[stream].substream;
1500 substream; substream = substream->next) {
1501 /* FIXME: the open/close code should lock this as well */
1502 if (substream->runtime == NULL)
1504 err = snd_pcm_suspend(substream);
1505 if (err < 0 && err != -EBUSY)
1511 EXPORT_SYMBOL(snd_pcm_suspend_all);
1515 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1517 struct snd_pcm_runtime *runtime = substream->runtime;
1518 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1520 runtime->trigger_master = substream;
1524 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1526 struct snd_pcm_runtime *runtime = substream->runtime;
1527 if (runtime->trigger_master != substream)
1529 /* DMA not running previously? */
1530 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1531 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1532 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1534 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1537 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1539 if (substream->runtime->trigger_master == substream &&
1540 snd_pcm_running(substream))
1541 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1544 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1546 struct snd_pcm_runtime *runtime = substream->runtime;
1547 snd_pcm_trigger_tstamp(substream);
1548 runtime->status->state = runtime->status->suspended_state;
1549 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1552 static const struct action_ops snd_pcm_action_resume = {
1553 .pre_action = snd_pcm_pre_resume,
1554 .do_action = snd_pcm_do_resume,
1555 .undo_action = snd_pcm_undo_resume,
1556 .post_action = snd_pcm_post_resume
1559 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1561 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1566 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1571 #endif /* CONFIG_PM */
1576 * Change the RUNNING stream(s) to XRUN state.
1578 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1580 struct snd_pcm_runtime *runtime = substream->runtime;
1583 snd_pcm_stream_lock_irq(substream);
1584 switch (runtime->status->state) {
1585 case SNDRV_PCM_STATE_XRUN:
1586 result = 0; /* already there */
1588 case SNDRV_PCM_STATE_RUNNING:
1589 __snd_pcm_xrun(substream);
1595 snd_pcm_stream_unlock_irq(substream);
1602 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1604 struct snd_pcm_runtime *runtime = substream->runtime;
1605 switch (runtime->status->state) {
1606 case SNDRV_PCM_STATE_RUNNING:
1607 case SNDRV_PCM_STATE_PREPARED:
1608 case SNDRV_PCM_STATE_PAUSED:
1609 case SNDRV_PCM_STATE_SUSPENDED:
1616 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1618 struct snd_pcm_runtime *runtime = substream->runtime;
1619 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1622 runtime->hw_ptr_base = 0;
1623 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1624 runtime->status->hw_ptr % runtime->period_size;
1625 runtime->silence_start = runtime->status->hw_ptr;
1626 runtime->silence_filled = 0;
1630 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1632 struct snd_pcm_runtime *runtime = substream->runtime;
1633 runtime->control->appl_ptr = runtime->status->hw_ptr;
1634 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1635 runtime->silence_size > 0)
1636 snd_pcm_playback_silence(substream, ULONG_MAX);
1639 static const struct action_ops snd_pcm_action_reset = {
1640 .pre_action = snd_pcm_pre_reset,
1641 .do_action = snd_pcm_do_reset,
1642 .post_action = snd_pcm_post_reset
1645 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1647 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1653 /* we use the second argument for updating f_flags */
1654 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1657 struct snd_pcm_runtime *runtime = substream->runtime;
1658 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1659 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1661 if (snd_pcm_running(substream))
1663 substream->f_flags = f_flags;
1667 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1670 err = substream->ops->prepare(substream);
1673 return snd_pcm_do_reset(substream, 0);
1676 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1678 struct snd_pcm_runtime *runtime = substream->runtime;
1679 runtime->control->appl_ptr = runtime->status->hw_ptr;
1680 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1683 static const struct action_ops snd_pcm_action_prepare = {
1684 .pre_action = snd_pcm_pre_prepare,
1685 .do_action = snd_pcm_do_prepare,
1686 .post_action = snd_pcm_post_prepare
1690 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1691 * @substream: the PCM substream instance
1692 * @file: file to refer f_flags
1694 * Return: Zero if successful, or a negative error code.
1696 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1702 f_flags = file->f_flags;
1704 f_flags = substream->f_flags;
1706 snd_pcm_stream_lock_irq(substream);
1707 switch (substream->runtime->status->state) {
1708 case SNDRV_PCM_STATE_PAUSED:
1709 snd_pcm_pause(substream, 0);
1711 case SNDRV_PCM_STATE_SUSPENDED:
1712 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1715 snd_pcm_stream_unlock_irq(substream);
1717 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1718 substream, f_flags);
1725 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1727 struct snd_pcm_runtime *runtime = substream->runtime;
1728 switch (runtime->status->state) {
1729 case SNDRV_PCM_STATE_OPEN:
1730 case SNDRV_PCM_STATE_DISCONNECTED:
1731 case SNDRV_PCM_STATE_SUSPENDED:
1734 runtime->trigger_master = substream;
1738 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1740 struct snd_pcm_runtime *runtime = substream->runtime;
1741 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1742 switch (runtime->status->state) {
1743 case SNDRV_PCM_STATE_PREPARED:
1744 /* start playback stream if possible */
1745 if (! snd_pcm_playback_empty(substream)) {
1746 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1747 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1749 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1752 case SNDRV_PCM_STATE_RUNNING:
1753 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1755 case SNDRV_PCM_STATE_XRUN:
1756 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1762 /* stop running stream */
1763 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1764 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1765 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1766 snd_pcm_do_stop(substream, new_state);
1767 snd_pcm_post_stop(substream, new_state);
1771 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1772 runtime->trigger_master == substream &&
1773 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1774 return substream->ops->trigger(substream,
1775 SNDRV_PCM_TRIGGER_DRAIN);
1780 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1784 static const struct action_ops snd_pcm_action_drain_init = {
1785 .pre_action = snd_pcm_pre_drain_init,
1786 .do_action = snd_pcm_do_drain_init,
1787 .post_action = snd_pcm_post_drain_init
1790 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1793 * Drain the stream(s).
1794 * When the substream is linked, sync until the draining of all playback streams
1796 * After this call, all streams are supposed to be either SETUP or DRAINING
1797 * (capture only) state.
1799 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1802 struct snd_card *card;
1803 struct snd_pcm_runtime *runtime;
1804 struct snd_pcm_substream *s;
1805 wait_queue_entry_t wait;
1809 card = substream->pcm->card;
1810 runtime = substream->runtime;
1812 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1816 if (file->f_flags & O_NONBLOCK)
1818 } else if (substream->f_flags & O_NONBLOCK)
1821 down_read(&snd_pcm_link_rwsem);
1822 snd_pcm_stream_lock_irq(substream);
1824 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1825 snd_pcm_pause(substream, 0);
1827 /* pre-start/stop - all running streams are changed to DRAINING state */
1828 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1831 /* in non-blocking, we don't wait in ioctl but let caller poll */
1839 struct snd_pcm_runtime *to_check;
1840 if (signal_pending(current)) {
1841 result = -ERESTARTSYS;
1844 /* find a substream to drain */
1846 snd_pcm_group_for_each_entry(s, substream) {
1847 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1849 runtime = s->runtime;
1850 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1856 break; /* all drained */
1857 init_waitqueue_entry(&wait, current);
1858 add_wait_queue(&to_check->sleep, &wait);
1859 snd_pcm_stream_unlock_irq(substream);
1860 up_read(&snd_pcm_link_rwsem);
1861 if (runtime->no_period_wakeup)
1862 tout = MAX_SCHEDULE_TIMEOUT;
1865 if (runtime->rate) {
1866 long t = runtime->period_size * 2 / runtime->rate;
1867 tout = max(t, tout);
1869 tout = msecs_to_jiffies(tout * 1000);
1871 tout = schedule_timeout_interruptible(tout);
1872 down_read(&snd_pcm_link_rwsem);
1873 snd_pcm_stream_lock_irq(substream);
1874 remove_wait_queue(&to_check->sleep, &wait);
1875 if (card->shutdown) {
1880 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1883 dev_dbg(substream->pcm->card->dev,
1884 "playback drain error (DMA or IRQ trouble?)\n");
1885 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1893 snd_pcm_stream_unlock_irq(substream);
1894 up_read(&snd_pcm_link_rwsem);
1902 * Immediately put all linked substreams into SETUP state.
1904 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1906 struct snd_pcm_runtime *runtime;
1909 if (PCM_RUNTIME_CHECK(substream))
1911 runtime = substream->runtime;
1913 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1914 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1917 snd_pcm_stream_lock_irq(substream);
1919 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1920 snd_pcm_pause(substream, 0);
1922 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1923 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1924 snd_pcm_stream_unlock_irq(substream);
1930 static bool is_pcm_file(struct file *file)
1932 struct inode *inode = file_inode(file);
1935 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1937 minor = iminor(inode);
1938 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1939 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1945 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1948 struct snd_pcm_file *pcm_file;
1949 struct snd_pcm_substream *substream1;
1950 struct snd_pcm_group *group;
1951 struct fd f = fdget(fd);
1955 if (!is_pcm_file(f.file)) {
1959 pcm_file = f.file->private_data;
1960 substream1 = pcm_file->substream;
1961 group = kmalloc(sizeof(*group), GFP_KERNEL);
1966 down_write_nonfifo(&snd_pcm_link_rwsem);
1967 write_lock_irq(&snd_pcm_link_rwlock);
1968 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1969 substream->runtime->status->state != substream1->runtime->status->state ||
1970 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1974 if (snd_pcm_stream_linked(substream1)) {
1978 if (!snd_pcm_stream_linked(substream)) {
1979 substream->group = group;
1981 spin_lock_init(&substream->group->lock);
1982 mutex_init(&substream->group->mutex);
1983 INIT_LIST_HEAD(&substream->group->substreams);
1984 list_add_tail(&substream->link_list, &substream->group->substreams);
1985 substream->group->count = 1;
1987 list_add_tail(&substream1->link_list, &substream->group->substreams);
1988 substream->group->count++;
1989 substream1->group = substream->group;
1991 write_unlock_irq(&snd_pcm_link_rwlock);
1992 up_write(&snd_pcm_link_rwsem);
1994 snd_card_unref(substream1->pcm->card);
2001 static void relink_to_local(struct snd_pcm_substream *substream)
2003 substream->group = &substream->self_group;
2004 INIT_LIST_HEAD(&substream->self_group.substreams);
2005 list_add_tail(&substream->link_list, &substream->self_group.substreams);
2008 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2010 struct snd_pcm_substream *s;
2013 down_write_nonfifo(&snd_pcm_link_rwsem);
2014 write_lock_irq(&snd_pcm_link_rwlock);
2015 if (!snd_pcm_stream_linked(substream)) {
2019 list_del(&substream->link_list);
2020 substream->group->count--;
2021 if (substream->group->count == 1) { /* detach the last stream, too */
2022 snd_pcm_group_for_each_entry(s, substream) {
2026 kfree(substream->group);
2028 relink_to_local(substream);
2030 write_unlock_irq(&snd_pcm_link_rwlock);
2031 up_write(&snd_pcm_link_rwsem);
2038 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2039 struct snd_pcm_hw_rule *rule)
2041 struct snd_interval t;
2042 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2043 hw_param_interval_c(params, rule->deps[1]), &t);
2044 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2047 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2048 struct snd_pcm_hw_rule *rule)
2050 struct snd_interval t;
2051 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2052 hw_param_interval_c(params, rule->deps[1]), &t);
2053 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2056 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2057 struct snd_pcm_hw_rule *rule)
2059 struct snd_interval t;
2060 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2061 hw_param_interval_c(params, rule->deps[1]),
2062 (unsigned long) rule->private, &t);
2063 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2066 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2067 struct snd_pcm_hw_rule *rule)
2069 struct snd_interval t;
2070 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2071 (unsigned long) rule->private,
2072 hw_param_interval_c(params, rule->deps[1]), &t);
2073 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2076 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2077 struct snd_pcm_hw_rule *rule)
2080 const struct snd_interval *i =
2081 hw_param_interval_c(params, rule->deps[0]);
2083 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2085 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2087 if (! snd_mask_test(mask, k))
2089 bits = snd_pcm_format_physical_width(k);
2091 continue; /* ignore invalid formats */
2092 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2093 snd_mask_reset(&m, k);
2095 return snd_mask_refine(mask, &m);
2098 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2099 struct snd_pcm_hw_rule *rule)
2101 struct snd_interval t;
2107 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2109 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2111 bits = snd_pcm_format_physical_width(k);
2113 continue; /* ignore invalid formats */
2114 if (t.min > (unsigned)bits)
2116 if (t.max < (unsigned)bits)
2120 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2123 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2124 #error "Change this table"
2127 static const unsigned int rates[] = {
2128 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2129 48000, 64000, 88200, 96000, 176400, 192000
2132 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2133 .count = ARRAY_SIZE(rates),
2137 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2138 struct snd_pcm_hw_rule *rule)
2140 struct snd_pcm_hardware *hw = rule->private;
2141 return snd_interval_list(hw_param_interval(params, rule->var),
2142 snd_pcm_known_rates.count,
2143 snd_pcm_known_rates.list, hw->rates);
2146 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2147 struct snd_pcm_hw_rule *rule)
2149 struct snd_interval t;
2150 struct snd_pcm_substream *substream = rule->private;
2152 t.max = substream->buffer_bytes_max;
2156 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2159 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2161 struct snd_pcm_runtime *runtime = substream->runtime;
2162 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2165 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2166 snd_mask_any(constrs_mask(constrs, k));
2169 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2170 snd_interval_any(constrs_interval(constrs, k));
2173 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2174 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2175 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2176 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2177 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2179 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2180 snd_pcm_hw_rule_format, NULL,
2181 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2184 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2185 snd_pcm_hw_rule_sample_bits, NULL,
2186 SNDRV_PCM_HW_PARAM_FORMAT,
2187 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2190 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2191 snd_pcm_hw_rule_div, NULL,
2192 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2195 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2196 snd_pcm_hw_rule_mul, NULL,
2197 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2200 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2201 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2202 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2205 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2206 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2207 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2210 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2211 snd_pcm_hw_rule_div, NULL,
2212 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2215 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2216 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2217 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2220 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2221 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2222 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2225 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2226 snd_pcm_hw_rule_div, NULL,
2227 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2230 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2231 snd_pcm_hw_rule_div, NULL,
2232 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2235 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2236 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2237 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2240 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2241 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2242 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2245 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2246 snd_pcm_hw_rule_mul, NULL,
2247 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2250 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2251 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2252 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2255 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2256 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2257 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2260 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2261 snd_pcm_hw_rule_muldivk, (void*) 8,
2262 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2265 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2266 snd_pcm_hw_rule_muldivk, (void*) 8,
2267 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2270 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2271 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2272 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2275 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2276 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2277 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2283 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2285 struct snd_pcm_runtime *runtime = substream->runtime;
2286 struct snd_pcm_hardware *hw = &runtime->hw;
2288 unsigned int mask = 0;
2290 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2291 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2292 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2293 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2294 if (hw_support_mmap(substream)) {
2295 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2296 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2297 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2298 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2299 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2300 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2302 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2306 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2310 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2314 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2315 hw->channels_min, hw->channels_max);
2319 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2320 hw->rate_min, hw->rate_max);
2324 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2325 hw->period_bytes_min, hw->period_bytes_max);
2329 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2330 hw->periods_min, hw->periods_max);
2334 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2335 hw->period_bytes_min, hw->buffer_bytes_max);
2339 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2340 snd_pcm_hw_rule_buffer_bytes_max, substream,
2341 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2346 if (runtime->dma_bytes) {
2347 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2352 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2353 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2354 snd_pcm_hw_rule_rate, hw,
2355 SNDRV_PCM_HW_PARAM_RATE, -1);
2360 /* FIXME: this belong to lowlevel */
2361 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2366 static void pcm_release_private(struct snd_pcm_substream *substream)
2368 if (snd_pcm_stream_linked(substream))
2369 snd_pcm_unlink(substream);
2372 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2374 substream->ref_count--;
2375 if (substream->ref_count > 0)
2378 snd_pcm_drop(substream);
2379 if (substream->hw_opened) {
2380 if (substream->ops->hw_free &&
2381 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2382 substream->ops->hw_free(substream);
2383 substream->ops->close(substream);
2384 substream->hw_opened = 0;
2386 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2387 pm_qos_remove_request(&substream->latency_pm_qos_req);
2388 if (substream->pcm_release) {
2389 substream->pcm_release(substream);
2390 substream->pcm_release = NULL;
2392 snd_pcm_detach_substream(substream);
2394 EXPORT_SYMBOL(snd_pcm_release_substream);
2396 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2398 struct snd_pcm_substream **rsubstream)
2400 struct snd_pcm_substream *substream;
2403 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2406 if (substream->ref_count > 1) {
2407 *rsubstream = substream;
2411 err = snd_pcm_hw_constraints_init(substream);
2413 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2417 if ((err = substream->ops->open(substream)) < 0)
2420 substream->hw_opened = 1;
2422 err = snd_pcm_hw_constraints_complete(substream);
2424 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2428 *rsubstream = substream;
2432 snd_pcm_release_substream(substream);
2435 EXPORT_SYMBOL(snd_pcm_open_substream);
2437 static int snd_pcm_open_file(struct file *file,
2438 struct snd_pcm *pcm,
2441 struct snd_pcm_file *pcm_file;
2442 struct snd_pcm_substream *substream;
2445 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2449 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2450 if (pcm_file == NULL) {
2451 snd_pcm_release_substream(substream);
2454 pcm_file->substream = substream;
2455 if (substream->ref_count == 1)
2456 substream->pcm_release = pcm_release_private;
2457 file->private_data = pcm_file;
2462 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2464 struct snd_pcm *pcm;
2465 int err = nonseekable_open(inode, file);
2468 pcm = snd_lookup_minor_data(iminor(inode),
2469 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2470 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2472 snd_card_unref(pcm->card);
2476 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2478 struct snd_pcm *pcm;
2479 int err = nonseekable_open(inode, file);
2482 pcm = snd_lookup_minor_data(iminor(inode),
2483 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2484 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2486 snd_card_unref(pcm->card);
2490 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2493 wait_queue_entry_t wait;
2499 err = snd_card_file_add(pcm->card, file);
2502 if (!try_module_get(pcm->card->module)) {
2506 init_waitqueue_entry(&wait, current);
2507 add_wait_queue(&pcm->open_wait, &wait);
2508 mutex_lock(&pcm->open_mutex);
2510 err = snd_pcm_open_file(file, pcm, stream);
2513 if (err == -EAGAIN) {
2514 if (file->f_flags & O_NONBLOCK) {
2520 set_current_state(TASK_INTERRUPTIBLE);
2521 mutex_unlock(&pcm->open_mutex);
2523 mutex_lock(&pcm->open_mutex);
2524 if (pcm->card->shutdown) {
2528 if (signal_pending(current)) {
2533 remove_wait_queue(&pcm->open_wait, &wait);
2534 mutex_unlock(&pcm->open_mutex);
2540 module_put(pcm->card->module);
2542 snd_card_file_remove(pcm->card, file);
2547 static int snd_pcm_release(struct inode *inode, struct file *file)
2549 struct snd_pcm *pcm;
2550 struct snd_pcm_substream *substream;
2551 struct snd_pcm_file *pcm_file;
2553 pcm_file = file->private_data;
2554 substream = pcm_file->substream;
2555 if (snd_BUG_ON(!substream))
2557 pcm = substream->pcm;
2558 mutex_lock(&pcm->open_mutex);
2559 snd_pcm_release_substream(substream);
2561 mutex_unlock(&pcm->open_mutex);
2562 wake_up(&pcm->open_wait);
2563 module_put(pcm->card->module);
2564 snd_card_file_remove(pcm->card, file);
2568 /* check and update PCM state; return 0 or a negative error
2569 * call this inside PCM lock
2571 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2573 switch (substream->runtime->status->state) {
2574 case SNDRV_PCM_STATE_DRAINING:
2575 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2578 case SNDRV_PCM_STATE_RUNNING:
2579 return snd_pcm_update_hw_ptr(substream);
2580 case SNDRV_PCM_STATE_PREPARED:
2581 case SNDRV_PCM_STATE_PAUSED:
2583 case SNDRV_PCM_STATE_SUSPENDED:
2585 case SNDRV_PCM_STATE_XRUN:
2592 /* increase the appl_ptr; returns the processed frames or a negative error */
2593 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2594 snd_pcm_uframes_t frames,
2595 snd_pcm_sframes_t avail)
2597 struct snd_pcm_runtime *runtime = substream->runtime;
2598 snd_pcm_sframes_t appl_ptr;
2603 if (frames > (snd_pcm_uframes_t)avail)
2605 appl_ptr = runtime->control->appl_ptr + frames;
2606 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2607 appl_ptr -= runtime->boundary;
2608 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2609 return ret < 0 ? ret : frames;
2612 /* decrease the appl_ptr; returns the processed frames or zero for error */
2613 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2614 snd_pcm_uframes_t frames,
2615 snd_pcm_sframes_t avail)
2617 struct snd_pcm_runtime *runtime = substream->runtime;
2618 snd_pcm_sframes_t appl_ptr;
2623 if (frames > (snd_pcm_uframes_t)avail)
2625 appl_ptr = runtime->control->appl_ptr - frames;
2627 appl_ptr += runtime->boundary;
2628 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2629 /* NOTE: we return zero for errors because PulseAudio gets depressed
2630 * upon receiving an error from rewind ioctl and stops processing
2631 * any longer. Returning zero means that no rewind is done, so
2632 * it's not absolutely wrong to answer like that.
2634 return ret < 0 ? 0 : frames;
2637 static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2638 snd_pcm_uframes_t frames)
2640 snd_pcm_sframes_t ret;
2645 snd_pcm_stream_lock_irq(substream);
2646 ret = do_pcm_hwsync(substream);
2648 ret = rewind_appl_ptr(substream, frames,
2649 snd_pcm_hw_avail(substream));
2650 snd_pcm_stream_unlock_irq(substream);
2654 static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2655 snd_pcm_uframes_t frames)
2657 snd_pcm_sframes_t ret;
2662 snd_pcm_stream_lock_irq(substream);
2663 ret = do_pcm_hwsync(substream);
2665 ret = forward_appl_ptr(substream, frames,
2666 snd_pcm_avail(substream));
2667 snd_pcm_stream_unlock_irq(substream);
2671 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2675 snd_pcm_stream_lock_irq(substream);
2676 err = do_pcm_hwsync(substream);
2677 snd_pcm_stream_unlock_irq(substream);
2681 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2682 snd_pcm_sframes_t *delay)
2685 snd_pcm_sframes_t n = 0;
2687 snd_pcm_stream_lock_irq(substream);
2688 err = do_pcm_hwsync(substream);
2690 n = snd_pcm_calc_delay(substream);
2691 snd_pcm_stream_unlock_irq(substream);
2697 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2698 struct snd_pcm_sync_ptr __user *_sync_ptr)
2700 struct snd_pcm_runtime *runtime = substream->runtime;
2701 struct snd_pcm_sync_ptr sync_ptr;
2702 volatile struct snd_pcm_mmap_status *status;
2703 volatile struct snd_pcm_mmap_control *control;
2706 memset(&sync_ptr, 0, sizeof(sync_ptr));
2707 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2709 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2711 status = runtime->status;
2712 control = runtime->control;
2713 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2714 err = snd_pcm_hwsync(substream);
2718 snd_pcm_stream_lock_irq(substream);
2719 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2720 err = pcm_lib_apply_appl_ptr(substream,
2721 sync_ptr.c.control.appl_ptr);
2723 snd_pcm_stream_unlock_irq(substream);
2727 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2729 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2730 control->avail_min = sync_ptr.c.control.avail_min;
2732 sync_ptr.c.control.avail_min = control->avail_min;
2733 sync_ptr.s.status.state = status->state;
2734 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2735 sync_ptr.s.status.tstamp = status->tstamp;
2736 sync_ptr.s.status.suspended_state = status->suspended_state;
2737 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2738 snd_pcm_stream_unlock_irq(substream);
2739 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2744 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2746 struct snd_pcm_runtime *runtime = substream->runtime;
2749 if (get_user(arg, _arg))
2751 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2753 runtime->tstamp_type = arg;
2757 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2758 struct snd_xferi __user *_xferi)
2760 struct snd_xferi xferi;
2761 struct snd_pcm_runtime *runtime = substream->runtime;
2762 snd_pcm_sframes_t result;
2764 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2766 if (put_user(0, &_xferi->result))
2768 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2770 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2771 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2773 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2774 __put_user(result, &_xferi->result);
2775 return result < 0 ? result : 0;
2778 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2779 struct snd_xfern __user *_xfern)
2781 struct snd_xfern xfern;
2782 struct snd_pcm_runtime *runtime = substream->runtime;
2784 snd_pcm_sframes_t result;
2786 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2788 if (runtime->channels > 128)
2790 if (put_user(0, &_xfern->result))
2792 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2795 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2797 return PTR_ERR(bufs);
2798 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2799 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2801 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2803 __put_user(result, &_xfern->result);
2804 return result < 0 ? result : 0;
2807 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2808 snd_pcm_uframes_t __user *_frames)
2810 snd_pcm_uframes_t frames;
2811 snd_pcm_sframes_t result;
2813 if (get_user(frames, _frames))
2815 if (put_user(0, _frames))
2817 result = snd_pcm_rewind(substream, frames);
2818 __put_user(result, _frames);
2819 return result < 0 ? result : 0;
2822 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2823 snd_pcm_uframes_t __user *_frames)
2825 snd_pcm_uframes_t frames;
2826 snd_pcm_sframes_t result;
2828 if (get_user(frames, _frames))
2830 if (put_user(0, _frames))
2832 result = snd_pcm_forward(substream, frames);
2833 __put_user(result, _frames);
2834 return result < 0 ? result : 0;
2837 static int snd_pcm_common_ioctl(struct file *file,
2838 struct snd_pcm_substream *substream,
2839 unsigned int cmd, void __user *arg)
2841 struct snd_pcm_file *pcm_file = file->private_data;
2844 if (PCM_RUNTIME_CHECK(substream))
2847 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2852 case SNDRV_PCM_IOCTL_PVERSION:
2853 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2854 case SNDRV_PCM_IOCTL_INFO:
2855 return snd_pcm_info_user(substream, arg);
2856 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2858 case SNDRV_PCM_IOCTL_TTSTAMP:
2859 return snd_pcm_tstamp(substream, arg);
2860 case SNDRV_PCM_IOCTL_USER_PVERSION:
2861 if (get_user(pcm_file->user_pversion,
2862 (unsigned int __user *)arg))
2865 case SNDRV_PCM_IOCTL_HW_REFINE:
2866 return snd_pcm_hw_refine_user(substream, arg);
2867 case SNDRV_PCM_IOCTL_HW_PARAMS:
2868 return snd_pcm_hw_params_user(substream, arg);
2869 case SNDRV_PCM_IOCTL_HW_FREE:
2870 return snd_pcm_hw_free(substream);
2871 case SNDRV_PCM_IOCTL_SW_PARAMS:
2872 return snd_pcm_sw_params_user(substream, arg);
2873 case SNDRV_PCM_IOCTL_STATUS:
2874 return snd_pcm_status_user(substream, arg, false);
2875 case SNDRV_PCM_IOCTL_STATUS_EXT:
2876 return snd_pcm_status_user(substream, arg, true);
2877 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2878 return snd_pcm_channel_info_user(substream, arg);
2879 case SNDRV_PCM_IOCTL_PREPARE:
2880 return snd_pcm_prepare(substream, file);
2881 case SNDRV_PCM_IOCTL_RESET:
2882 return snd_pcm_reset(substream);
2883 case SNDRV_PCM_IOCTL_START:
2884 return snd_pcm_start_lock_irq(substream);
2885 case SNDRV_PCM_IOCTL_LINK:
2886 return snd_pcm_link(substream, (int)(unsigned long) arg);
2887 case SNDRV_PCM_IOCTL_UNLINK:
2888 return snd_pcm_unlink(substream);
2889 case SNDRV_PCM_IOCTL_RESUME:
2890 return snd_pcm_resume(substream);
2891 case SNDRV_PCM_IOCTL_XRUN:
2892 return snd_pcm_xrun(substream);
2893 case SNDRV_PCM_IOCTL_HWSYNC:
2894 return snd_pcm_hwsync(substream);
2895 case SNDRV_PCM_IOCTL_DELAY:
2897 snd_pcm_sframes_t delay;
2898 snd_pcm_sframes_t __user *res = arg;
2901 err = snd_pcm_delay(substream, &delay);
2904 if (put_user(delay, res))
2908 case SNDRV_PCM_IOCTL_SYNC_PTR:
2909 return snd_pcm_sync_ptr(substream, arg);
2910 #ifdef CONFIG_SND_SUPPORT_OLD_API
2911 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2912 return snd_pcm_hw_refine_old_user(substream, arg);
2913 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2914 return snd_pcm_hw_params_old_user(substream, arg);
2916 case SNDRV_PCM_IOCTL_DRAIN:
2917 return snd_pcm_drain(substream, file);
2918 case SNDRV_PCM_IOCTL_DROP:
2919 return snd_pcm_drop(substream);
2920 case SNDRV_PCM_IOCTL_PAUSE:
2921 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
2923 (int)(unsigned long)arg);
2924 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2925 case SNDRV_PCM_IOCTL_READI_FRAMES:
2926 return snd_pcm_xferi_frames_ioctl(substream, arg);
2927 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2928 case SNDRV_PCM_IOCTL_READN_FRAMES:
2929 return snd_pcm_xfern_frames_ioctl(substream, arg);
2930 case SNDRV_PCM_IOCTL_REWIND:
2931 return snd_pcm_rewind_ioctl(substream, arg);
2932 case SNDRV_PCM_IOCTL_FORWARD:
2933 return snd_pcm_forward_ioctl(substream, arg);
2935 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2939 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
2942 struct snd_pcm_file *pcm_file;
2944 pcm_file = file->private_data;
2946 if (((cmd >> 8) & 0xff) != 'A')
2949 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
2950 (void __user *)arg);
2954 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2955 * @substream: PCM substream
2957 * @arg: IOCTL argument
2959 * The function is provided primarily for OSS layer and USB gadget drivers,
2960 * and it allows only the limited set of ioctls (hw_params, sw_params,
2961 * prepare, start, drain, drop, forward).
2963 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2964 unsigned int cmd, void *arg)
2966 snd_pcm_uframes_t *frames = arg;
2967 snd_pcm_sframes_t result;
2970 case SNDRV_PCM_IOCTL_FORWARD:
2972 /* provided only for OSS; capture-only and no value returned */
2973 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
2975 result = snd_pcm_forward(substream, *frames);
2976 return result < 0 ? result : 0;
2978 case SNDRV_PCM_IOCTL_HW_PARAMS:
2979 return snd_pcm_hw_params(substream, arg);
2980 case SNDRV_PCM_IOCTL_SW_PARAMS:
2981 return snd_pcm_sw_params(substream, arg);
2982 case SNDRV_PCM_IOCTL_PREPARE:
2983 return snd_pcm_prepare(substream, NULL);
2984 case SNDRV_PCM_IOCTL_START:
2985 return snd_pcm_start_lock_irq(substream);
2986 case SNDRV_PCM_IOCTL_DRAIN:
2987 return snd_pcm_drain(substream, NULL);
2988 case SNDRV_PCM_IOCTL_DROP:
2989 return snd_pcm_drop(substream);
2990 case SNDRV_PCM_IOCTL_DELAY:
2991 return snd_pcm_delay(substream, frames);
2996 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2998 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3001 struct snd_pcm_file *pcm_file;
3002 struct snd_pcm_substream *substream;
3003 struct snd_pcm_runtime *runtime;
3004 snd_pcm_sframes_t result;
3006 pcm_file = file->private_data;
3007 substream = pcm_file->substream;
3008 if (PCM_RUNTIME_CHECK(substream))
3010 runtime = substream->runtime;
3011 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3013 if (!frame_aligned(runtime, count))
3015 count = bytes_to_frames(runtime, count);
3016 result = snd_pcm_lib_read(substream, buf, count);
3018 result = frames_to_bytes(runtime, result);
3022 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3023 size_t count, loff_t * offset)
3025 struct snd_pcm_file *pcm_file;
3026 struct snd_pcm_substream *substream;
3027 struct snd_pcm_runtime *runtime;
3028 snd_pcm_sframes_t result;
3030 pcm_file = file->private_data;
3031 substream = pcm_file->substream;
3032 if (PCM_RUNTIME_CHECK(substream))
3034 runtime = substream->runtime;
3035 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3037 if (!frame_aligned(runtime, count))
3039 count = bytes_to_frames(runtime, count);
3040 result = snd_pcm_lib_write(substream, buf, count);
3042 result = frames_to_bytes(runtime, result);
3046 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3048 struct snd_pcm_file *pcm_file;
3049 struct snd_pcm_substream *substream;
3050 struct snd_pcm_runtime *runtime;
3051 snd_pcm_sframes_t result;
3054 snd_pcm_uframes_t frames;
3056 pcm_file = iocb->ki_filp->private_data;
3057 substream = pcm_file->substream;
3058 if (PCM_RUNTIME_CHECK(substream))
3060 runtime = substream->runtime;
3061 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3063 if (!iter_is_iovec(to))
3065 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3067 if (!frame_aligned(runtime, to->iov->iov_len))
3069 frames = bytes_to_samples(runtime, to->iov->iov_len);
3070 bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3073 for (i = 0; i < to->nr_segs; ++i)
3074 bufs[i] = to->iov[i].iov_base;
3075 result = snd_pcm_lib_readv(substream, bufs, frames);
3077 result = frames_to_bytes(runtime, result);
3082 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3084 struct snd_pcm_file *pcm_file;
3085 struct snd_pcm_substream *substream;
3086 struct snd_pcm_runtime *runtime;
3087 snd_pcm_sframes_t result;
3090 snd_pcm_uframes_t frames;
3092 pcm_file = iocb->ki_filp->private_data;
3093 substream = pcm_file->substream;
3094 if (PCM_RUNTIME_CHECK(substream))
3096 runtime = substream->runtime;
3097 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3099 if (!iter_is_iovec(from))
3101 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3102 !frame_aligned(runtime, from->iov->iov_len))
3104 frames = bytes_to_samples(runtime, from->iov->iov_len);
3105 bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3108 for (i = 0; i < from->nr_segs; ++i)
3109 bufs[i] = from->iov[i].iov_base;
3110 result = snd_pcm_lib_writev(substream, bufs, frames);
3112 result = frames_to_bytes(runtime, result);
3117 static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3119 struct snd_pcm_file *pcm_file;
3120 struct snd_pcm_substream *substream;
3121 struct snd_pcm_runtime *runtime;
3123 snd_pcm_uframes_t avail;
3125 pcm_file = file->private_data;
3127 substream = pcm_file->substream;
3128 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3129 ok = EPOLLOUT | EPOLLWRNORM;
3131 ok = EPOLLIN | EPOLLRDNORM;
3132 if (PCM_RUNTIME_CHECK(substream))
3133 return ok | EPOLLERR;
3135 runtime = substream->runtime;
3136 poll_wait(file, &runtime->sleep, wait);
3139 snd_pcm_stream_lock_irq(substream);
3140 avail = snd_pcm_avail(substream);
3141 switch (runtime->status->state) {
3142 case SNDRV_PCM_STATE_RUNNING:
3143 case SNDRV_PCM_STATE_PREPARED:
3144 case SNDRV_PCM_STATE_PAUSED:
3145 if (avail >= runtime->control->avail_min)
3148 case SNDRV_PCM_STATE_DRAINING:
3149 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3156 mask = ok | EPOLLERR;
3159 snd_pcm_stream_unlock_irq(substream);
3168 * Only on coherent architectures, we can mmap the status and the control records
3169 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3171 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3173 * mmap status record
3175 static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3177 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3178 struct snd_pcm_runtime *runtime;
3180 if (substream == NULL)
3181 return VM_FAULT_SIGBUS;
3182 runtime = substream->runtime;
3183 vmf->page = virt_to_page(runtime->status);
3184 get_page(vmf->page);
3188 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3190 .fault = snd_pcm_mmap_status_fault,
3193 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3194 struct vm_area_struct *area)
3197 if (!(area->vm_flags & VM_READ))
3199 size = area->vm_end - area->vm_start;
3200 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3202 area->vm_ops = &snd_pcm_vm_ops_status;
3203 area->vm_private_data = substream;
3204 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3209 * mmap control record
3211 static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3213 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3214 struct snd_pcm_runtime *runtime;
3216 if (substream == NULL)
3217 return VM_FAULT_SIGBUS;
3218 runtime = substream->runtime;
3219 vmf->page = virt_to_page(runtime->control);
3220 get_page(vmf->page);
3224 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3226 .fault = snd_pcm_mmap_control_fault,
3229 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3230 struct vm_area_struct *area)
3233 if (!(area->vm_flags & VM_READ))
3235 size = area->vm_end - area->vm_start;
3236 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3238 area->vm_ops = &snd_pcm_vm_ops_control;
3239 area->vm_private_data = substream;
3240 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3244 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3246 if (pcm_file->no_compat_mmap)
3248 /* See pcm_control_mmap_allowed() below.
3249 * Since older alsa-lib requires both status and control mmaps to be
3250 * coupled, we have to disable the status mmap for old alsa-lib, too.
3252 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3253 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3258 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3260 if (pcm_file->no_compat_mmap)
3262 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3263 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3264 * thus it effectively assures the manual update of appl_ptr.
3266 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3271 #else /* ! coherent mmap */
3273 * don't support mmap for status and control records.
3275 #define pcm_status_mmap_allowed(pcm_file) false
3276 #define pcm_control_mmap_allowed(pcm_file) false
3278 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3279 struct vm_area_struct *area)
3283 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3284 struct vm_area_struct *area)
3288 #endif /* coherent mmap */
3290 static inline struct page *
3291 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3293 void *vaddr = substream->runtime->dma_area + ofs;
3294 return virt_to_page(vaddr);
3298 * fault callback for mmapping a RAM page
3300 static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3302 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3303 struct snd_pcm_runtime *runtime;
3304 unsigned long offset;
3308 if (substream == NULL)
3309 return VM_FAULT_SIGBUS;
3310 runtime = substream->runtime;
3311 offset = vmf->pgoff << PAGE_SHIFT;
3312 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3313 if (offset > dma_bytes - PAGE_SIZE)
3314 return VM_FAULT_SIGBUS;
3315 if (substream->ops->page)
3316 page = substream->ops->page(substream, offset);
3318 page = snd_pcm_default_page_ops(substream, offset);
3320 return VM_FAULT_SIGBUS;
3326 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3327 .open = snd_pcm_mmap_data_open,
3328 .close = snd_pcm_mmap_data_close,
3331 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3332 .open = snd_pcm_mmap_data_open,
3333 .close = snd_pcm_mmap_data_close,
3334 .fault = snd_pcm_mmap_data_fault,
3338 * mmap the DMA buffer on RAM
3342 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3343 * @substream: PCM substream
3346 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3347 * this function is invoked implicitly.
3349 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3350 struct vm_area_struct *area)
3352 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3353 #ifdef CONFIG_GENERIC_ALLOCATOR
3354 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3355 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3356 return remap_pfn_range(area, area->vm_start,
3357 substream->dma_buffer.addr >> PAGE_SHIFT,
3358 area->vm_end - area->vm_start, area->vm_page_prot);
3360 #endif /* CONFIG_GENERIC_ALLOCATOR */
3361 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3362 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3363 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3364 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3366 substream->runtime->dma_area,
3367 substream->runtime->dma_addr,
3368 substream->runtime->dma_bytes);
3369 #endif /* CONFIG_X86 */
3370 /* mmap with fault handler */
3371 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3374 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3377 * mmap the DMA buffer on I/O memory area
3379 #if SNDRV_PCM_INFO_MMAP_IOMEM
3381 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3382 * @substream: PCM substream
3385 * When your hardware uses the iomapped pages as the hardware buffer and
3386 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3387 * is supposed to work only on limited architectures.
3389 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3390 struct vm_area_struct *area)
3392 struct snd_pcm_runtime *runtime = substream->runtime;
3394 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3395 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3397 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3398 #endif /* SNDRV_PCM_INFO_MMAP */
3403 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3404 struct vm_area_struct *area)
3406 struct snd_pcm_runtime *runtime;
3408 unsigned long offset;
3412 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3413 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3416 if (!(area->vm_flags & VM_READ))
3419 runtime = substream->runtime;
3420 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3422 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3424 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3425 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3427 size = area->vm_end - area->vm_start;
3428 offset = area->vm_pgoff << PAGE_SHIFT;
3429 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3430 if ((size_t)size > dma_bytes)
3432 if (offset > dma_bytes - size)
3435 area->vm_ops = &snd_pcm_vm_ops_data;
3436 area->vm_private_data = substream;
3437 if (substream->ops->mmap)
3438 err = substream->ops->mmap(substream, area);
3440 err = snd_pcm_lib_default_mmap(substream, area);
3442 atomic_inc(&substream->mmap_count);
3445 EXPORT_SYMBOL(snd_pcm_mmap_data);
3447 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3449 struct snd_pcm_file * pcm_file;
3450 struct snd_pcm_substream *substream;
3451 unsigned long offset;
3453 pcm_file = file->private_data;
3454 substream = pcm_file->substream;
3455 if (PCM_RUNTIME_CHECK(substream))
3458 offset = area->vm_pgoff << PAGE_SHIFT;
3460 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3461 if (!pcm_status_mmap_allowed(pcm_file))
3463 return snd_pcm_mmap_status(substream, file, area);
3464 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3465 if (!pcm_control_mmap_allowed(pcm_file))
3467 return snd_pcm_mmap_control(substream, file, area);
3469 return snd_pcm_mmap_data(substream, file, area);
3474 static int snd_pcm_fasync(int fd, struct file * file, int on)
3476 struct snd_pcm_file * pcm_file;
3477 struct snd_pcm_substream *substream;
3478 struct snd_pcm_runtime *runtime;
3480 pcm_file = file->private_data;
3481 substream = pcm_file->substream;
3482 if (PCM_RUNTIME_CHECK(substream))
3484 runtime = substream->runtime;
3485 return fasync_helper(fd, file, on, &runtime->fasync);
3491 #ifdef CONFIG_COMPAT
3492 #include "pcm_compat.c"
3494 #define snd_pcm_ioctl_compat NULL
3498 * To be removed helpers to keep binary compatibility
3501 #ifdef CONFIG_SND_SUPPORT_OLD_API
3502 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3503 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3505 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3506 struct snd_pcm_hw_params_old *oparams)
3510 memset(params, 0, sizeof(*params));
3511 params->flags = oparams->flags;
3512 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3513 params->masks[i].bits[0] = oparams->masks[i];
3514 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3515 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3516 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3517 params->info = oparams->info;
3518 params->msbits = oparams->msbits;
3519 params->rate_num = oparams->rate_num;
3520 params->rate_den = oparams->rate_den;
3521 params->fifo_size = oparams->fifo_size;
3524 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3525 struct snd_pcm_hw_params *params)
3529 memset(oparams, 0, sizeof(*oparams));
3530 oparams->flags = params->flags;
3531 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3532 oparams->masks[i] = params->masks[i].bits[0];
3533 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3534 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3535 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3536 oparams->info = params->info;
3537 oparams->msbits = params->msbits;
3538 oparams->rate_num = params->rate_num;
3539 oparams->rate_den = params->rate_den;
3540 oparams->fifo_size = params->fifo_size;
3543 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3544 struct snd_pcm_hw_params_old __user * _oparams)
3546 struct snd_pcm_hw_params *params;
3547 struct snd_pcm_hw_params_old *oparams = NULL;
3550 params = kmalloc(sizeof(*params), GFP_KERNEL);
3554 oparams = memdup_user(_oparams, sizeof(*oparams));
3555 if (IS_ERR(oparams)) {
3556 err = PTR_ERR(oparams);
3559 snd_pcm_hw_convert_from_old_params(params, oparams);
3560 err = snd_pcm_hw_refine(substream, params);
3564 err = fixup_unreferenced_params(substream, params);
3568 snd_pcm_hw_convert_to_old_params(oparams, params);
3569 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3578 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3579 struct snd_pcm_hw_params_old __user * _oparams)
3581 struct snd_pcm_hw_params *params;
3582 struct snd_pcm_hw_params_old *oparams = NULL;
3585 params = kmalloc(sizeof(*params), GFP_KERNEL);
3589 oparams = memdup_user(_oparams, sizeof(*oparams));
3590 if (IS_ERR(oparams)) {
3591 err = PTR_ERR(oparams);
3595 snd_pcm_hw_convert_from_old_params(params, oparams);
3596 err = snd_pcm_hw_params(substream, params);
3600 snd_pcm_hw_convert_to_old_params(oparams, params);
3601 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3609 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3612 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3615 unsigned long pgoff,
3616 unsigned long flags)
3618 struct snd_pcm_file *pcm_file = file->private_data;
3619 struct snd_pcm_substream *substream = pcm_file->substream;
3620 struct snd_pcm_runtime *runtime = substream->runtime;
3621 unsigned long offset = pgoff << PAGE_SHIFT;
3624 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3625 return (unsigned long)runtime->status;
3626 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3627 return (unsigned long)runtime->control;
3629 return (unsigned long)runtime->dma_area + offset;
3633 # define snd_pcm_get_unmapped_area NULL
3640 const struct file_operations snd_pcm_f_ops[2] = {
3642 .owner = THIS_MODULE,
3643 .write = snd_pcm_write,
3644 .write_iter = snd_pcm_writev,
3645 .open = snd_pcm_playback_open,
3646 .release = snd_pcm_release,
3647 .llseek = no_llseek,
3648 .poll = snd_pcm_poll,
3649 .unlocked_ioctl = snd_pcm_ioctl,
3650 .compat_ioctl = snd_pcm_ioctl_compat,
3651 .mmap = snd_pcm_mmap,
3652 .fasync = snd_pcm_fasync,
3653 .get_unmapped_area = snd_pcm_get_unmapped_area,
3656 .owner = THIS_MODULE,
3657 .read = snd_pcm_read,
3658 .read_iter = snd_pcm_readv,
3659 .open = snd_pcm_capture_open,
3660 .release = snd_pcm_release,
3661 .llseek = no_llseek,
3662 .poll = snd_pcm_poll,
3663 .unlocked_ioctl = snd_pcm_ioctl,
3664 .compat_ioctl = snd_pcm_ioctl_compat,
3665 .mmap = snd_pcm_mmap,
3666 .fasync = snd_pcm_fasync,
3667 .get_unmapped_area = snd_pcm_get_unmapped_area,