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))
104 * snd_pcm_stream_lock - Lock the PCM stream
105 * @substream: PCM substream
107 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
108 * flag of the given substream. This also takes the global link rw lock
109 * (or rw sem), too, for avoiding the race with linked streams.
111 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
113 if (substream->pcm->nonatomic) {
114 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
115 mutex_lock(&substream->self_group.mutex);
117 read_lock(&snd_pcm_link_rwlock);
118 spin_lock(&substream->self_group.lock);
121 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
124 * snd_pcm_stream_lock - Unlock the PCM stream
125 * @substream: PCM substream
127 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
129 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
131 if (substream->pcm->nonatomic) {
132 mutex_unlock(&substream->self_group.mutex);
133 up_read(&snd_pcm_link_rwsem);
135 spin_unlock(&substream->self_group.lock);
136 read_unlock(&snd_pcm_link_rwlock);
139 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
142 * snd_pcm_stream_lock_irq - Lock the PCM stream
143 * @substream: PCM substream
145 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
146 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
147 * as snd_pcm_stream_lock().
149 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
151 if (!substream->pcm->nonatomic)
153 snd_pcm_stream_lock(substream);
155 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
158 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
159 * @substream: PCM substream
161 * This is a counter-part of snd_pcm_stream_lock_irq().
163 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
165 snd_pcm_stream_unlock(substream);
166 if (!substream->pcm->nonatomic)
169 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
171 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
173 unsigned long flags = 0;
174 if (!substream->pcm->nonatomic)
175 local_irq_save(flags);
176 snd_pcm_stream_lock(substream);
179 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
182 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
183 * @substream: PCM substream
186 * This is a counter-part of snd_pcm_stream_lock_irqsave().
188 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
191 snd_pcm_stream_unlock(substream);
192 if (!substream->pcm->nonatomic)
193 local_irq_restore(flags);
195 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
197 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
199 struct snd_pcm_runtime *runtime;
200 struct snd_pcm *pcm = substream->pcm;
201 struct snd_pcm_str *pstr = substream->pstr;
203 memset(info, 0, sizeof(*info));
204 info->card = pcm->card->number;
205 info->device = pcm->device;
206 info->stream = substream->stream;
207 info->subdevice = substream->number;
208 strlcpy(info->id, pcm->id, sizeof(info->id));
209 strlcpy(info->name, pcm->name, sizeof(info->name));
210 info->dev_class = pcm->dev_class;
211 info->dev_subclass = pcm->dev_subclass;
212 info->subdevices_count = pstr->substream_count;
213 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
214 strlcpy(info->subname, substream->name, sizeof(info->subname));
215 runtime = substream->runtime;
220 int snd_pcm_info_user(struct snd_pcm_substream *substream,
221 struct snd_pcm_info __user * _info)
223 struct snd_pcm_info *info;
226 info = kmalloc(sizeof(*info), GFP_KERNEL);
229 err = snd_pcm_info(substream, info);
231 if (copy_to_user(_info, info, sizeof(*info)))
238 static bool hw_support_mmap(struct snd_pcm_substream *substream)
240 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
242 /* architecture supports dma_mmap_coherent()? */
243 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
244 if (!substream->ops->mmap &&
245 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
251 static int constrain_mask_params(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
254 struct snd_pcm_hw_constraints *constrs =
255 &substream->runtime->hw_constraints;
258 struct snd_mask old_mask;
261 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
262 m = hw_param_mask(params, k);
263 if (snd_mask_empty(m))
266 /* This parameter is not requested to change by a caller. */
267 if (!(params->rmask & (1 << k)))
270 if (trace_hw_mask_param_enabled())
273 changed = snd_mask_refine(m, constrs_mask(constrs, k));
279 /* Set corresponding flag so that the caller gets it. */
280 trace_hw_mask_param(substream, k, 0, &old_mask, m);
281 params->cmask |= 1 << k;
287 static int constrain_interval_params(struct snd_pcm_substream *substream,
288 struct snd_pcm_hw_params *params)
290 struct snd_pcm_hw_constraints *constrs =
291 &substream->runtime->hw_constraints;
292 struct snd_interval *i;
294 struct snd_interval old_interval;
297 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
298 i = hw_param_interval(params, k);
299 if (snd_interval_empty(i))
302 /* This parameter is not requested to change by a caller. */
303 if (!(params->rmask & (1 << k)))
306 if (trace_hw_interval_param_enabled())
309 changed = snd_interval_refine(i, constrs_interval(constrs, k));
315 /* Set corresponding flag so that the caller gets it. */
316 trace_hw_interval_param(substream, k, 0, &old_interval, i);
317 params->cmask |= 1 << k;
323 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
324 struct snd_pcm_hw_params *params)
326 struct snd_pcm_hw_constraints *constrs =
327 &substream->runtime->hw_constraints;
329 unsigned int rstamps[constrs->rules_num];
330 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
332 struct snd_pcm_hw_rule *r;
334 struct snd_mask old_mask;
335 struct snd_interval old_interval;
340 * Each application of rule has own sequence number.
342 * Each member of 'rstamps' array represents the sequence number of
343 * recent application of corresponding rule.
345 for (k = 0; k < constrs->rules_num; k++)
349 * Each member of 'vstamps' array represents the sequence number of
350 * recent application of rule in which corresponding parameters were
353 * In initial state, elements corresponding to parameters requested by
354 * a caller is 1. For unrequested parameters, corresponding members
355 * have 0 so that the parameters are never changed anymore.
357 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
358 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
360 /* Due to the above design, actual sequence number starts at 2. */
363 /* Apply all rules in order. */
365 for (k = 0; k < constrs->rules_num; k++) {
366 r = &constrs->rules[k];
369 * Check condition bits of this rule. When the rule has
370 * some condition bits, parameter without the bits is
371 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
372 * is an example of the condition bits.
374 if (r->cond && !(r->cond & params->flags))
378 * The 'deps' array includes maximum three dependencies
379 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
380 * member of this array is a sentinel and should be
383 * This rule should be processed in this time when dependent
384 * parameters were changed at former applications of the other
387 for (d = 0; r->deps[d] >= 0; d++) {
388 if (vstamps[r->deps[d]] > rstamps[k])
394 if (trace_hw_mask_param_enabled()) {
395 if (hw_is_mask(r->var))
396 old_mask = *hw_param_mask(params, r->var);
398 if (trace_hw_interval_param_enabled()) {
399 if (hw_is_interval(r->var))
400 old_interval = *hw_param_interval(params, r->var);
403 changed = r->func(params, r);
408 * When the parameter is changed, notify it to the caller
409 * by corresponding returned bit, then preparing for next
412 if (changed && r->var >= 0) {
413 if (hw_is_mask(r->var)) {
414 trace_hw_mask_param(substream, r->var,
416 hw_param_mask(params, r->var));
418 if (hw_is_interval(r->var)) {
419 trace_hw_interval_param(substream, r->var,
420 k + 1, &old_interval,
421 hw_param_interval(params, r->var));
424 params->cmask |= (1 << r->var);
425 vstamps[r->var] = stamp;
429 rstamps[k] = stamp++;
432 /* Iterate to evaluate all rules till no parameters are changed. */
439 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
440 struct snd_pcm_hw_params *params)
442 const struct snd_interval *i;
443 const struct snd_mask *m;
446 if (!params->msbits) {
447 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
448 if (snd_interval_single(i))
449 params->msbits = snd_interval_value(i);
452 if (!params->rate_den) {
453 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
454 if (snd_interval_single(i)) {
455 params->rate_num = snd_interval_value(i);
456 params->rate_den = 1;
460 if (!params->fifo_size) {
461 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
462 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
463 if (snd_mask_single(m) && snd_interval_single(i)) {
464 err = substream->ops->ioctl(substream,
465 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
472 params->info = substream->runtime->hw.info;
473 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
474 SNDRV_PCM_INFO_DRAIN_TRIGGER);
475 if (!hw_support_mmap(substream))
476 params->info &= ~(SNDRV_PCM_INFO_MMAP |
477 SNDRV_PCM_INFO_MMAP_VALID);
483 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
484 struct snd_pcm_hw_params *params)
489 params->fifo_size = 0;
490 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
492 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
493 params->rate_num = 0;
494 params->rate_den = 0;
497 err = constrain_mask_params(substream, params);
501 err = constrain_interval_params(substream, params);
505 err = constrain_params_by_rules(substream, params);
513 EXPORT_SYMBOL(snd_pcm_hw_refine);
515 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
516 struct snd_pcm_hw_params __user * _params)
518 struct snd_pcm_hw_params *params;
521 params = memdup_user(_params, sizeof(*params));
523 return PTR_ERR(params);
525 err = snd_pcm_hw_refine(substream, params);
529 err = fixup_unreferenced_params(substream, params);
533 if (copy_to_user(_params, params, sizeof(*params)))
540 static int period_to_usecs(struct snd_pcm_runtime *runtime)
545 return -1; /* invalid */
547 /* take 75% of period time as the deadline */
548 usecs = (750000 / runtime->rate) * runtime->period_size;
549 usecs += ((750000 % runtime->rate) * runtime->period_size) /
555 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
557 snd_pcm_stream_lock_irq(substream);
558 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
559 substream->runtime->status->state = state;
560 snd_pcm_stream_unlock_irq(substream);
563 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
566 #ifdef CONFIG_SND_PCM_TIMER
567 if (substream->timer)
568 snd_timer_notify(substream->timer, event,
569 &substream->runtime->trigger_tstamp);
574 * snd_pcm_hw_param_choose - choose a configuration defined by @params
576 * @params: the hw_params instance
578 * Choose one configuration from configuration space defined by @params.
579 * The configuration chosen is that obtained fixing in this order:
580 * first access, first format, first subformat, min channels,
581 * min rate, min period time, max buffer size, min tick time
583 * Return: Zero if successful, or a negative error code on failure.
585 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
586 struct snd_pcm_hw_params *params)
588 static const int vars[] = {
589 SNDRV_PCM_HW_PARAM_ACCESS,
590 SNDRV_PCM_HW_PARAM_FORMAT,
591 SNDRV_PCM_HW_PARAM_SUBFORMAT,
592 SNDRV_PCM_HW_PARAM_CHANNELS,
593 SNDRV_PCM_HW_PARAM_RATE,
594 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
595 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
596 SNDRV_PCM_HW_PARAM_TICK_TIME,
600 struct snd_mask old_mask;
601 struct snd_interval old_interval;
604 for (v = vars; *v != -1; v++) {
605 /* Keep old parameter to trace. */
606 if (trace_hw_mask_param_enabled()) {
608 old_mask = *hw_param_mask(params, *v);
610 if (trace_hw_interval_param_enabled()) {
611 if (hw_is_interval(*v))
612 old_interval = *hw_param_interval(params, *v);
614 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
615 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
617 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
618 if (snd_BUG_ON(changed < 0))
623 /* Trace the changed parameter. */
624 if (hw_is_mask(*v)) {
625 trace_hw_mask_param(pcm, *v, 0, &old_mask,
626 hw_param_mask(params, *v));
628 if (hw_is_interval(*v)) {
629 trace_hw_interval_param(pcm, *v, 0, &old_interval,
630 hw_param_interval(params, *v));
637 /* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise
638 * block the further r/w operations
640 static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime)
642 if (!atomic_dec_unless_positive(&runtime->buffer_accessing))
644 mutex_lock(&runtime->buffer_mutex);
645 return 0; /* keep buffer_mutex, unlocked by below */
648 /* release buffer_mutex and clear r/w access flag */
649 static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime)
651 mutex_unlock(&runtime->buffer_mutex);
652 atomic_inc(&runtime->buffer_accessing);
655 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
656 #define is_oss_stream(substream) ((substream)->oss.oss)
658 #define is_oss_stream(substream) false
661 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
662 struct snd_pcm_hw_params *params)
664 struct snd_pcm_runtime *runtime;
667 snd_pcm_uframes_t frames;
669 if (PCM_RUNTIME_CHECK(substream))
671 runtime = substream->runtime;
672 err = snd_pcm_buffer_access_lock(runtime);
675 snd_pcm_stream_lock_irq(substream);
676 switch (runtime->status->state) {
677 case SNDRV_PCM_STATE_OPEN:
678 case SNDRV_PCM_STATE_SETUP:
679 case SNDRV_PCM_STATE_PREPARED:
680 if (!is_oss_stream(substream) &&
681 atomic_read(&substream->mmap_count))
688 snd_pcm_stream_unlock_irq(substream);
693 err = snd_pcm_hw_refine(substream, params);
697 err = snd_pcm_hw_params_choose(substream, params);
701 err = fixup_unreferenced_params(substream, params);
705 if (substream->ops->hw_params != NULL) {
706 err = substream->ops->hw_params(substream, params);
711 runtime->access = params_access(params);
712 runtime->format = params_format(params);
713 runtime->subformat = params_subformat(params);
714 runtime->channels = params_channels(params);
715 runtime->rate = params_rate(params);
716 runtime->period_size = params_period_size(params);
717 runtime->periods = params_periods(params);
718 runtime->buffer_size = params_buffer_size(params);
719 runtime->info = params->info;
720 runtime->rate_num = params->rate_num;
721 runtime->rate_den = params->rate_den;
722 runtime->no_period_wakeup =
723 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
724 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
726 bits = snd_pcm_format_physical_width(runtime->format);
727 runtime->sample_bits = bits;
728 bits *= runtime->channels;
729 runtime->frame_bits = bits;
731 while (bits % 8 != 0) {
735 runtime->byte_align = bits / 8;
736 runtime->min_align = frames;
738 /* Default sw params */
739 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
740 runtime->period_step = 1;
741 runtime->control->avail_min = runtime->period_size;
742 runtime->start_threshold = 1;
743 runtime->stop_threshold = runtime->buffer_size;
744 runtime->silence_threshold = 0;
745 runtime->silence_size = 0;
746 runtime->boundary = runtime->buffer_size;
747 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
748 runtime->boundary *= 2;
750 /* clear the buffer for avoiding possible kernel info leaks */
751 if (runtime->dma_area && !substream->ops->copy_user) {
752 size_t size = runtime->dma_bytes;
754 if (runtime->info & SNDRV_PCM_INFO_MMAP)
755 size = PAGE_ALIGN(size);
756 memset(runtime->dma_area, 0, size);
759 snd_pcm_timer_resolution_change(substream);
760 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
762 if (pm_qos_request_active(&substream->latency_pm_qos_req))
763 pm_qos_remove_request(&substream->latency_pm_qos_req);
764 if ((usecs = period_to_usecs(runtime)) >= 0)
765 pm_qos_add_request(&substream->latency_pm_qos_req,
766 PM_QOS_CPU_DMA_LATENCY, usecs);
770 /* hardware might be unusable from this time,
771 * so we force application to retry to set
772 * the correct hardware parameter settings
774 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
775 if (substream->ops->hw_free != NULL)
776 substream->ops->hw_free(substream);
779 snd_pcm_buffer_access_unlock(runtime);
783 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
784 struct snd_pcm_hw_params __user * _params)
786 struct snd_pcm_hw_params *params;
789 params = memdup_user(_params, sizeof(*params));
791 return PTR_ERR(params);
793 err = snd_pcm_hw_params(substream, params);
797 if (copy_to_user(_params, params, sizeof(*params)))
804 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
806 struct snd_pcm_runtime *runtime;
809 if (PCM_RUNTIME_CHECK(substream))
811 runtime = substream->runtime;
812 result = snd_pcm_buffer_access_lock(runtime);
815 snd_pcm_stream_lock_irq(substream);
816 switch (runtime->status->state) {
817 case SNDRV_PCM_STATE_SETUP:
818 case SNDRV_PCM_STATE_PREPARED:
819 if (atomic_read(&substream->mmap_count))
826 snd_pcm_stream_unlock_irq(substream);
829 if (substream->ops->hw_free)
830 result = substream->ops->hw_free(substream);
831 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
832 pm_qos_remove_request(&substream->latency_pm_qos_req);
834 snd_pcm_buffer_access_unlock(runtime);
838 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
839 struct snd_pcm_sw_params *params)
841 struct snd_pcm_runtime *runtime;
844 if (PCM_RUNTIME_CHECK(substream))
846 runtime = substream->runtime;
847 snd_pcm_stream_lock_irq(substream);
848 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
849 snd_pcm_stream_unlock_irq(substream);
852 snd_pcm_stream_unlock_irq(substream);
854 if (params->tstamp_mode < 0 ||
855 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
857 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
858 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
860 if (params->avail_min == 0)
862 if (params->silence_size >= runtime->boundary) {
863 if (params->silence_threshold != 0)
866 if (params->silence_size > params->silence_threshold)
868 if (params->silence_threshold > runtime->buffer_size)
872 snd_pcm_stream_lock_irq(substream);
873 runtime->tstamp_mode = params->tstamp_mode;
874 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
875 runtime->tstamp_type = params->tstamp_type;
876 runtime->period_step = params->period_step;
877 runtime->control->avail_min = params->avail_min;
878 runtime->start_threshold = params->start_threshold;
879 runtime->stop_threshold = params->stop_threshold;
880 runtime->silence_threshold = params->silence_threshold;
881 runtime->silence_size = params->silence_size;
882 params->boundary = runtime->boundary;
883 if (snd_pcm_running(substream)) {
884 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
885 runtime->silence_size > 0)
886 snd_pcm_playback_silence(substream, ULONG_MAX);
887 err = snd_pcm_update_state(substream, runtime);
889 snd_pcm_stream_unlock_irq(substream);
893 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
894 struct snd_pcm_sw_params __user * _params)
896 struct snd_pcm_sw_params params;
898 if (copy_from_user(¶ms, _params, sizeof(params)))
900 err = snd_pcm_sw_params(substream, ¶ms);
901 if (copy_to_user(_params, ¶ms, sizeof(params)))
906 int snd_pcm_status(struct snd_pcm_substream *substream,
907 struct snd_pcm_status *status)
909 struct snd_pcm_runtime *runtime = substream->runtime;
911 snd_pcm_stream_lock_irq(substream);
913 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
914 &runtime->audio_tstamp_config);
916 /* backwards compatible behavior */
917 if (runtime->audio_tstamp_config.type_requested ==
918 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
919 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
920 runtime->audio_tstamp_config.type_requested =
921 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
923 runtime->audio_tstamp_config.type_requested =
924 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
925 runtime->audio_tstamp_report.valid = 0;
927 runtime->audio_tstamp_report.valid = 1;
929 status->state = runtime->status->state;
930 status->suspended_state = runtime->status->suspended_state;
931 if (status->state == SNDRV_PCM_STATE_OPEN)
933 status->trigger_tstamp = runtime->trigger_tstamp;
934 if (snd_pcm_running(substream)) {
935 snd_pcm_update_hw_ptr(substream);
936 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
937 status->tstamp = runtime->status->tstamp;
938 status->driver_tstamp = runtime->driver_tstamp;
939 status->audio_tstamp =
940 runtime->status->audio_tstamp;
941 if (runtime->audio_tstamp_report.valid == 1)
942 /* backwards compatibility, no report provided in COMPAT mode */
943 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
944 &status->audio_tstamp_accuracy,
945 &runtime->audio_tstamp_report);
950 /* get tstamp only in fallback mode and only if enabled */
951 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
952 snd_pcm_gettime(runtime, &status->tstamp);
955 status->appl_ptr = runtime->control->appl_ptr;
956 status->hw_ptr = runtime->status->hw_ptr;
957 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
958 status->avail = snd_pcm_playback_avail(runtime);
959 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
960 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
961 status->delay = runtime->buffer_size - status->avail;
962 status->delay += runtime->delay;
966 status->avail = snd_pcm_capture_avail(runtime);
967 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
968 status->delay = status->avail + runtime->delay;
972 status->avail_max = runtime->avail_max;
973 status->overrange = runtime->overrange;
974 runtime->avail_max = 0;
975 runtime->overrange = 0;
977 snd_pcm_stream_unlock_irq(substream);
981 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
982 struct snd_pcm_status __user * _status,
985 struct snd_pcm_status status;
988 memset(&status, 0, sizeof(status));
990 * with extension, parameters are read/write,
991 * get audio_tstamp_data from user,
992 * ignore rest of status structure
994 if (ext && get_user(status.audio_tstamp_data,
995 (u32 __user *)(&_status->audio_tstamp_data)))
997 res = snd_pcm_status(substream, &status);
1000 if (copy_to_user(_status, &status, sizeof(status)))
1005 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1006 struct snd_pcm_channel_info * info)
1008 struct snd_pcm_runtime *runtime;
1009 unsigned int channel;
1011 channel = info->channel;
1012 runtime = substream->runtime;
1013 snd_pcm_stream_lock_irq(substream);
1014 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1015 snd_pcm_stream_unlock_irq(substream);
1018 snd_pcm_stream_unlock_irq(substream);
1019 if (channel >= runtime->channels)
1021 memset(info, 0, sizeof(*info));
1022 info->channel = channel;
1023 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1026 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1027 struct snd_pcm_channel_info __user * _info)
1029 struct snd_pcm_channel_info info;
1032 if (copy_from_user(&info, _info, sizeof(info)))
1034 res = snd_pcm_channel_info(substream, &info);
1037 if (copy_to_user(_info, &info, sizeof(info)))
1042 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1044 struct snd_pcm_runtime *runtime = substream->runtime;
1045 if (runtime->trigger_master == NULL)
1047 if (runtime->trigger_master == substream) {
1048 if (!runtime->trigger_tstamp_latched)
1049 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1051 snd_pcm_trigger_tstamp(runtime->trigger_master);
1052 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1054 runtime->trigger_master = NULL;
1058 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1059 int (*do_action)(struct snd_pcm_substream *substream, int state);
1060 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1061 void (*post_action)(struct snd_pcm_substream *substream, int state);
1065 * this functions is core for handling of linked stream
1066 * Note: the stream state might be changed also on failure
1067 * Note2: call with calling stream lock + link lock
1069 static int snd_pcm_action_group(const struct action_ops *ops,
1070 struct snd_pcm_substream *substream,
1071 int state, int stream_lock)
1073 struct snd_pcm_substream *s = NULL;
1074 struct snd_pcm_substream *s1;
1075 int res = 0, depth = 1;
1077 snd_pcm_group_for_each_entry(s, substream) {
1078 if (s != substream) {
1080 mutex_lock_nested(&s->runtime->buffer_mutex, depth);
1081 else if (s->pcm->nonatomic)
1082 mutex_lock_nested(&s->self_group.mutex, depth);
1084 spin_lock_nested(&s->self_group.lock, depth);
1087 res = ops->pre_action(s, state);
1091 snd_pcm_group_for_each_entry(s, substream) {
1092 res = ops->do_action(s, state);
1094 if (ops->undo_action) {
1095 snd_pcm_group_for_each_entry(s1, substream) {
1096 if (s1 == s) /* failed stream */
1098 ops->undo_action(s1, state);
1101 s = NULL; /* unlock all */
1105 snd_pcm_group_for_each_entry(s, substream) {
1106 ops->post_action(s, state);
1109 /* unlock streams */
1110 snd_pcm_group_for_each_entry(s1, substream) {
1111 if (s1 != substream) {
1113 mutex_unlock(&s1->runtime->buffer_mutex);
1114 else if (s1->pcm->nonatomic)
1115 mutex_unlock(&s1->self_group.mutex);
1117 spin_unlock(&s1->self_group.lock);
1119 if (s1 == s) /* end */
1126 * Note: call with stream lock
1128 static int snd_pcm_action_single(const struct action_ops *ops,
1129 struct snd_pcm_substream *substream,
1134 res = ops->pre_action(substream, state);
1137 res = ops->do_action(substream, state);
1139 ops->post_action(substream, state);
1140 else if (ops->undo_action)
1141 ops->undo_action(substream, state);
1146 * Note: call with stream lock
1148 static int snd_pcm_action(const struct action_ops *ops,
1149 struct snd_pcm_substream *substream,
1154 if (!snd_pcm_stream_linked(substream))
1155 return snd_pcm_action_single(ops, substream, state);
1157 if (substream->pcm->nonatomic) {
1158 if (!mutex_trylock(&substream->group->mutex)) {
1159 mutex_unlock(&substream->self_group.mutex);
1160 mutex_lock(&substream->group->mutex);
1161 mutex_lock(&substream->self_group.mutex);
1163 res = snd_pcm_action_group(ops, substream, state, 1);
1164 mutex_unlock(&substream->group->mutex);
1166 if (!spin_trylock(&substream->group->lock)) {
1167 spin_unlock(&substream->self_group.lock);
1168 spin_lock(&substream->group->lock);
1169 spin_lock(&substream->self_group.lock);
1171 res = snd_pcm_action_group(ops, substream, state, 1);
1172 spin_unlock(&substream->group->lock);
1178 * Note: don't use any locks before
1180 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1181 struct snd_pcm_substream *substream,
1186 snd_pcm_stream_lock_irq(substream);
1187 res = snd_pcm_action(ops, substream, state);
1188 snd_pcm_stream_unlock_irq(substream);
1194 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1195 struct snd_pcm_substream *substream,
1200 down_read(&snd_pcm_link_rwsem);
1201 res = snd_pcm_buffer_access_lock(substream->runtime);
1204 if (snd_pcm_stream_linked(substream))
1205 res = snd_pcm_action_group(ops, substream, state, 0);
1207 res = snd_pcm_action_single(ops, substream, state);
1208 snd_pcm_buffer_access_unlock(substream->runtime);
1210 up_read(&snd_pcm_link_rwsem);
1217 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1219 struct snd_pcm_runtime *runtime = substream->runtime;
1220 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1222 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1223 !snd_pcm_playback_data(substream))
1225 runtime->trigger_tstamp_latched = false;
1226 runtime->trigger_master = substream;
1230 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1232 if (substream->runtime->trigger_master != substream)
1234 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1237 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1239 if (substream->runtime->trigger_master == substream)
1240 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1243 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1245 struct snd_pcm_runtime *runtime = substream->runtime;
1246 snd_pcm_trigger_tstamp(substream);
1247 runtime->hw_ptr_jiffies = jiffies;
1248 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1250 runtime->status->state = state;
1251 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1252 runtime->silence_size > 0)
1253 snd_pcm_playback_silence(substream, ULONG_MAX);
1254 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1257 static const struct action_ops snd_pcm_action_start = {
1258 .pre_action = snd_pcm_pre_start,
1259 .do_action = snd_pcm_do_start,
1260 .undo_action = snd_pcm_undo_start,
1261 .post_action = snd_pcm_post_start
1265 * snd_pcm_start - start all linked streams
1266 * @substream: the PCM substream instance
1268 * Return: Zero if successful, or a negative error code.
1269 * The stream lock must be acquired before calling this function.
1271 int snd_pcm_start(struct snd_pcm_substream *substream)
1273 return snd_pcm_action(&snd_pcm_action_start, substream,
1274 SNDRV_PCM_STATE_RUNNING);
1277 /* take the stream lock and start the streams */
1278 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1280 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1281 SNDRV_PCM_STATE_RUNNING);
1287 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1289 struct snd_pcm_runtime *runtime = substream->runtime;
1290 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1292 runtime->trigger_master = substream;
1296 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1298 if (substream->runtime->trigger_master == substream &&
1299 snd_pcm_running(substream))
1300 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1301 return 0; /* unconditonally stop all substreams */
1304 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1306 struct snd_pcm_runtime *runtime = substream->runtime;
1307 if (runtime->status->state != state) {
1308 snd_pcm_trigger_tstamp(substream);
1309 runtime->status->state = state;
1310 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1312 wake_up(&runtime->sleep);
1313 wake_up(&runtime->tsleep);
1316 static const struct action_ops snd_pcm_action_stop = {
1317 .pre_action = snd_pcm_pre_stop,
1318 .do_action = snd_pcm_do_stop,
1319 .post_action = snd_pcm_post_stop
1323 * snd_pcm_stop - try to stop all running streams in the substream group
1324 * @substream: the PCM substream instance
1325 * @state: PCM state after stopping the stream
1327 * The state of each stream is then changed to the given state unconditionally.
1329 * Return: Zero if successful, or a negative error code.
1331 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1333 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1335 EXPORT_SYMBOL(snd_pcm_stop);
1338 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1339 * @substream: the PCM substream
1341 * After stopping, the state is changed to SETUP.
1342 * Unlike snd_pcm_stop(), this affects only the given stream.
1344 * Return: Zero if succesful, or a negative error code.
1346 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1348 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1349 SNDRV_PCM_STATE_SETUP);
1353 * snd_pcm_stop_xrun - stop the running streams as XRUN
1354 * @substream: the PCM substream instance
1356 * This stops the given running substream (and all linked substreams) as XRUN.
1357 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1359 * Return: Zero if successful, or a negative error code.
1361 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1363 unsigned long flags;
1366 snd_pcm_stream_lock_irqsave(substream, flags);
1367 if (snd_pcm_running(substream))
1368 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1369 snd_pcm_stream_unlock_irqrestore(substream, flags);
1372 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1377 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1379 struct snd_pcm_runtime *runtime = substream->runtime;
1380 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1383 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1385 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1387 runtime->trigger_master = substream;
1391 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1393 if (substream->runtime->trigger_master != substream)
1395 /* some drivers might use hw_ptr to recover from the pause -
1396 update the hw_ptr now */
1398 snd_pcm_update_hw_ptr(substream);
1399 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1400 * a delta between the current jiffies, this gives a large enough
1401 * delta, effectively to skip the check once.
1403 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1404 return substream->ops->trigger(substream,
1405 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1406 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1409 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1411 if (substream->runtime->trigger_master == substream)
1412 substream->ops->trigger(substream,
1413 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1414 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1417 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1419 struct snd_pcm_runtime *runtime = substream->runtime;
1420 snd_pcm_trigger_tstamp(substream);
1422 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1423 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1424 wake_up(&runtime->sleep);
1425 wake_up(&runtime->tsleep);
1427 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1428 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1432 static const struct action_ops snd_pcm_action_pause = {
1433 .pre_action = snd_pcm_pre_pause,
1434 .do_action = snd_pcm_do_pause,
1435 .undo_action = snd_pcm_undo_pause,
1436 .post_action = snd_pcm_post_pause
1440 * Push/release the pause for all linked streams.
1442 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1444 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1450 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1452 struct snd_pcm_runtime *runtime = substream->runtime;
1453 switch (runtime->status->state) {
1454 case SNDRV_PCM_STATE_SUSPENDED:
1456 /* unresumable PCM state; return -EBUSY for skipping suspend */
1457 case SNDRV_PCM_STATE_OPEN:
1458 case SNDRV_PCM_STATE_SETUP:
1459 case SNDRV_PCM_STATE_DISCONNECTED:
1462 runtime->trigger_master = substream;
1466 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1468 struct snd_pcm_runtime *runtime = substream->runtime;
1469 if (runtime->trigger_master != substream)
1471 if (! snd_pcm_running(substream))
1473 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1474 return 0; /* suspend unconditionally */
1477 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1479 struct snd_pcm_runtime *runtime = substream->runtime;
1480 snd_pcm_trigger_tstamp(substream);
1481 runtime->status->suspended_state = runtime->status->state;
1482 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1483 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1484 wake_up(&runtime->sleep);
1485 wake_up(&runtime->tsleep);
1488 static const struct action_ops snd_pcm_action_suspend = {
1489 .pre_action = snd_pcm_pre_suspend,
1490 .do_action = snd_pcm_do_suspend,
1491 .post_action = snd_pcm_post_suspend
1495 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1496 * @substream: the PCM substream
1498 * After this call, all streams are changed to SUSPENDED state.
1500 * Return: Zero if successful (or @substream is %NULL), or a negative error
1503 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1506 unsigned long flags;
1511 snd_pcm_stream_lock_irqsave(substream, flags);
1512 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1513 snd_pcm_stream_unlock_irqrestore(substream, flags);
1516 EXPORT_SYMBOL(snd_pcm_suspend);
1519 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1520 * @pcm: the PCM instance
1522 * After this call, all streams are changed to SUSPENDED state.
1524 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1526 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1528 struct snd_pcm_substream *substream;
1529 int stream, err = 0;
1534 for (stream = 0; stream < 2; stream++) {
1535 for (substream = pcm->streams[stream].substream;
1536 substream; substream = substream->next) {
1537 /* FIXME: the open/close code should lock this as well */
1538 if (substream->runtime == NULL)
1542 * Skip BE dai link PCM's that are internal and may
1543 * not have their substream ops set.
1545 if (!substream->ops)
1548 err = snd_pcm_suspend(substream);
1549 if (err < 0 && err != -EBUSY)
1555 EXPORT_SYMBOL(snd_pcm_suspend_all);
1559 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1561 struct snd_pcm_runtime *runtime = substream->runtime;
1562 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1564 runtime->trigger_master = substream;
1568 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1570 struct snd_pcm_runtime *runtime = substream->runtime;
1571 if (runtime->trigger_master != substream)
1573 /* DMA not running previously? */
1574 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1575 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1576 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1578 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1581 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1583 if (substream->runtime->trigger_master == substream &&
1584 snd_pcm_running(substream))
1585 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1588 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1590 struct snd_pcm_runtime *runtime = substream->runtime;
1591 snd_pcm_trigger_tstamp(substream);
1592 runtime->status->state = runtime->status->suspended_state;
1593 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1596 static const struct action_ops snd_pcm_action_resume = {
1597 .pre_action = snd_pcm_pre_resume,
1598 .do_action = snd_pcm_do_resume,
1599 .undo_action = snd_pcm_undo_resume,
1600 .post_action = snd_pcm_post_resume
1603 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1605 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1610 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1615 #endif /* CONFIG_PM */
1620 * Change the RUNNING stream(s) to XRUN state.
1622 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1624 struct snd_pcm_runtime *runtime = substream->runtime;
1627 snd_pcm_stream_lock_irq(substream);
1628 switch (runtime->status->state) {
1629 case SNDRV_PCM_STATE_XRUN:
1630 result = 0; /* already there */
1632 case SNDRV_PCM_STATE_RUNNING:
1633 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1638 snd_pcm_stream_unlock_irq(substream);
1645 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1647 struct snd_pcm_runtime *runtime = substream->runtime;
1648 switch (runtime->status->state) {
1649 case SNDRV_PCM_STATE_RUNNING:
1650 case SNDRV_PCM_STATE_PREPARED:
1651 case SNDRV_PCM_STATE_PAUSED:
1652 case SNDRV_PCM_STATE_SUSPENDED:
1659 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1661 struct snd_pcm_runtime *runtime = substream->runtime;
1662 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1665 snd_pcm_stream_lock_irq(substream);
1666 runtime->hw_ptr_base = 0;
1667 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1668 runtime->status->hw_ptr % runtime->period_size;
1669 runtime->silence_start = runtime->status->hw_ptr;
1670 runtime->silence_filled = 0;
1671 snd_pcm_stream_unlock_irq(substream);
1675 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1677 struct snd_pcm_runtime *runtime = substream->runtime;
1678 snd_pcm_stream_lock_irq(substream);
1679 runtime->control->appl_ptr = runtime->status->hw_ptr;
1680 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1681 runtime->silence_size > 0)
1682 snd_pcm_playback_silence(substream, ULONG_MAX);
1683 snd_pcm_stream_unlock_irq(substream);
1686 static const struct action_ops snd_pcm_action_reset = {
1687 .pre_action = snd_pcm_pre_reset,
1688 .do_action = snd_pcm_do_reset,
1689 .post_action = snd_pcm_post_reset
1692 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1694 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1700 /* we use the second argument for updating f_flags */
1701 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1704 struct snd_pcm_runtime *runtime = substream->runtime;
1705 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1706 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1708 if (snd_pcm_running(substream))
1710 substream->f_flags = f_flags;
1714 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1717 err = substream->ops->prepare(substream);
1720 return snd_pcm_do_reset(substream, 0);
1723 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1725 struct snd_pcm_runtime *runtime = substream->runtime;
1726 runtime->control->appl_ptr = runtime->status->hw_ptr;
1727 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1730 static const struct action_ops snd_pcm_action_prepare = {
1731 .pre_action = snd_pcm_pre_prepare,
1732 .do_action = snd_pcm_do_prepare,
1733 .post_action = snd_pcm_post_prepare
1737 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1738 * @substream: the PCM substream instance
1739 * @file: file to refer f_flags
1741 * Return: Zero if successful, or a negative error code.
1743 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1749 f_flags = file->f_flags;
1751 f_flags = substream->f_flags;
1753 snd_pcm_stream_lock_irq(substream);
1754 switch (substream->runtime->status->state) {
1755 case SNDRV_PCM_STATE_PAUSED:
1756 snd_pcm_pause(substream, 0);
1758 case SNDRV_PCM_STATE_SUSPENDED:
1759 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1762 snd_pcm_stream_unlock_irq(substream);
1764 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1765 substream, f_flags);
1772 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1774 struct snd_pcm_runtime *runtime = substream->runtime;
1775 switch (runtime->status->state) {
1776 case SNDRV_PCM_STATE_OPEN:
1777 case SNDRV_PCM_STATE_DISCONNECTED:
1778 case SNDRV_PCM_STATE_SUSPENDED:
1781 runtime->trigger_master = substream;
1785 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1787 struct snd_pcm_runtime *runtime = substream->runtime;
1788 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1789 switch (runtime->status->state) {
1790 case SNDRV_PCM_STATE_PREPARED:
1791 /* start playback stream if possible */
1792 if (! snd_pcm_playback_empty(substream)) {
1793 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1794 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1796 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1799 case SNDRV_PCM_STATE_RUNNING:
1800 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1802 case SNDRV_PCM_STATE_XRUN:
1803 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1809 /* stop running stream */
1810 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1811 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1812 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1813 snd_pcm_do_stop(substream, new_state);
1814 snd_pcm_post_stop(substream, new_state);
1818 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1819 runtime->trigger_master == substream &&
1820 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1821 return substream->ops->trigger(substream,
1822 SNDRV_PCM_TRIGGER_DRAIN);
1827 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1831 static const struct action_ops snd_pcm_action_drain_init = {
1832 .pre_action = snd_pcm_pre_drain_init,
1833 .do_action = snd_pcm_do_drain_init,
1834 .post_action = snd_pcm_post_drain_init
1837 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1840 * Drain the stream(s).
1841 * When the substream is linked, sync until the draining of all playback streams
1843 * After this call, all streams are supposed to be either SETUP or DRAINING
1844 * (capture only) state.
1846 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1849 struct snd_card *card;
1850 struct snd_pcm_runtime *runtime;
1851 struct snd_pcm_substream *s;
1852 wait_queue_entry_t wait;
1856 card = substream->pcm->card;
1857 runtime = substream->runtime;
1859 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1863 if (file->f_flags & O_NONBLOCK)
1865 } else if (substream->f_flags & O_NONBLOCK)
1868 down_read(&snd_pcm_link_rwsem);
1869 snd_pcm_stream_lock_irq(substream);
1871 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1872 snd_pcm_pause(substream, 0);
1874 /* pre-start/stop - all running streams are changed to DRAINING state */
1875 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1878 /* in non-blocking, we don't wait in ioctl but let caller poll */
1886 struct snd_pcm_runtime *to_check;
1887 if (signal_pending(current)) {
1888 result = -ERESTARTSYS;
1891 /* find a substream to drain */
1893 snd_pcm_group_for_each_entry(s, substream) {
1894 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1896 runtime = s->runtime;
1897 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1903 break; /* all drained */
1904 init_waitqueue_entry(&wait, current);
1905 add_wait_queue(&to_check->sleep, &wait);
1906 snd_pcm_stream_unlock_irq(substream);
1907 up_read(&snd_pcm_link_rwsem);
1908 if (runtime->no_period_wakeup)
1909 tout = MAX_SCHEDULE_TIMEOUT;
1912 if (runtime->rate) {
1913 long t = runtime->period_size * 2 / runtime->rate;
1914 tout = max(t, tout);
1916 tout = msecs_to_jiffies(tout * 1000);
1918 tout = schedule_timeout_interruptible(tout);
1919 down_read(&snd_pcm_link_rwsem);
1920 snd_pcm_stream_lock_irq(substream);
1921 remove_wait_queue(&to_check->sleep, &wait);
1922 if (card->shutdown) {
1927 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1930 dev_dbg(substream->pcm->card->dev,
1931 "playback drain error (DMA or IRQ trouble?)\n");
1932 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1940 snd_pcm_stream_unlock_irq(substream);
1941 up_read(&snd_pcm_link_rwsem);
1949 * Immediately put all linked substreams into SETUP state.
1951 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1953 struct snd_pcm_runtime *runtime;
1956 if (PCM_RUNTIME_CHECK(substream))
1958 runtime = substream->runtime;
1960 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1961 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1964 snd_pcm_stream_lock_irq(substream);
1966 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1967 snd_pcm_pause(substream, 0);
1969 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1970 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1971 snd_pcm_stream_unlock_irq(substream);
1977 static bool is_pcm_file(struct file *file)
1979 struct inode *inode = file_inode(file);
1982 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1984 minor = iminor(inode);
1985 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1986 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1992 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1995 struct snd_pcm_file *pcm_file;
1996 struct snd_pcm_substream *substream1;
1997 struct snd_pcm_group *group;
1998 struct fd f = fdget(fd);
2002 if (!is_pcm_file(f.file)) {
2006 pcm_file = f.file->private_data;
2007 substream1 = pcm_file->substream;
2008 if (substream == substream1) {
2013 group = kmalloc(sizeof(*group), GFP_KERNEL);
2018 down_write_nonfifo(&snd_pcm_link_rwsem);
2019 write_lock_irq(&snd_pcm_link_rwlock);
2020 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2021 substream->runtime->status->state != substream1->runtime->status->state ||
2022 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2026 if (snd_pcm_stream_linked(substream1)) {
2030 if (!snd_pcm_stream_linked(substream)) {
2031 substream->group = group;
2033 spin_lock_init(&substream->group->lock);
2034 mutex_init(&substream->group->mutex);
2035 INIT_LIST_HEAD(&substream->group->substreams);
2036 list_add_tail(&substream->link_list, &substream->group->substreams);
2037 substream->group->count = 1;
2039 list_add_tail(&substream1->link_list, &substream->group->substreams);
2040 substream->group->count++;
2041 substream1->group = substream->group;
2043 write_unlock_irq(&snd_pcm_link_rwlock);
2044 up_write(&snd_pcm_link_rwsem);
2046 snd_card_unref(substream1->pcm->card);
2053 static void relink_to_local(struct snd_pcm_substream *substream)
2055 substream->group = &substream->self_group;
2056 INIT_LIST_HEAD(&substream->self_group.substreams);
2057 list_add_tail(&substream->link_list, &substream->self_group.substreams);
2060 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2062 struct snd_pcm_substream *s;
2065 down_write_nonfifo(&snd_pcm_link_rwsem);
2066 write_lock_irq(&snd_pcm_link_rwlock);
2067 if (!snd_pcm_stream_linked(substream)) {
2071 list_del(&substream->link_list);
2072 substream->group->count--;
2073 if (substream->group->count == 1) { /* detach the last stream, too */
2074 snd_pcm_group_for_each_entry(s, substream) {
2078 kfree(substream->group);
2080 relink_to_local(substream);
2082 write_unlock_irq(&snd_pcm_link_rwlock);
2083 up_write(&snd_pcm_link_rwsem);
2090 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2091 struct snd_pcm_hw_rule *rule)
2093 struct snd_interval t;
2094 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2095 hw_param_interval_c(params, rule->deps[1]), &t);
2096 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2099 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2100 struct snd_pcm_hw_rule *rule)
2102 struct snd_interval t;
2103 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2104 hw_param_interval_c(params, rule->deps[1]), &t);
2105 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2108 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2109 struct snd_pcm_hw_rule *rule)
2111 struct snd_interval t;
2112 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2113 hw_param_interval_c(params, rule->deps[1]),
2114 (unsigned long) rule->private, &t);
2115 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2118 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2119 struct snd_pcm_hw_rule *rule)
2121 struct snd_interval t;
2122 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2123 (unsigned long) rule->private,
2124 hw_param_interval_c(params, rule->deps[1]), &t);
2125 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2128 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2129 struct snd_pcm_hw_rule *rule)
2132 const struct snd_interval *i =
2133 hw_param_interval_c(params, rule->deps[0]);
2135 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2137 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2139 if (! snd_mask_test(mask, k))
2141 bits = snd_pcm_format_physical_width(k);
2143 continue; /* ignore invalid formats */
2144 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2145 snd_mask_reset(&m, k);
2147 return snd_mask_refine(mask, &m);
2150 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2151 struct snd_pcm_hw_rule *rule)
2153 struct snd_interval t;
2159 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2161 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2163 bits = snd_pcm_format_physical_width(k);
2165 continue; /* ignore invalid formats */
2166 if (t.min > (unsigned)bits)
2168 if (t.max < (unsigned)bits)
2172 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2175 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2176 #error "Change this table"
2179 static const unsigned int rates[] = {
2180 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2181 48000, 64000, 88200, 96000, 176400, 192000
2184 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2185 .count = ARRAY_SIZE(rates),
2189 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2190 struct snd_pcm_hw_rule *rule)
2192 struct snd_pcm_hardware *hw = rule->private;
2193 return snd_interval_list(hw_param_interval(params, rule->var),
2194 snd_pcm_known_rates.count,
2195 snd_pcm_known_rates.list, hw->rates);
2198 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2199 struct snd_pcm_hw_rule *rule)
2201 struct snd_interval t;
2202 struct snd_pcm_substream *substream = rule->private;
2204 t.max = substream->buffer_bytes_max;
2208 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2211 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2213 struct snd_pcm_runtime *runtime = substream->runtime;
2214 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2217 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2218 snd_mask_any(constrs_mask(constrs, k));
2221 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2222 snd_interval_any(constrs_interval(constrs, k));
2225 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2226 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2227 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2228 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2229 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2231 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2232 snd_pcm_hw_rule_format, NULL,
2233 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2236 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2237 snd_pcm_hw_rule_sample_bits, NULL,
2238 SNDRV_PCM_HW_PARAM_FORMAT,
2239 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2242 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2243 snd_pcm_hw_rule_div, NULL,
2244 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2247 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2248 snd_pcm_hw_rule_mul, NULL,
2249 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2252 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2253 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2254 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2257 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2258 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2259 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2262 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2263 snd_pcm_hw_rule_div, NULL,
2264 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2267 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2268 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2269 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2272 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2273 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2274 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2277 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2278 snd_pcm_hw_rule_div, NULL,
2279 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2282 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2283 snd_pcm_hw_rule_div, NULL,
2284 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2287 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2288 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2289 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2292 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2293 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2294 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2297 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2298 snd_pcm_hw_rule_mul, NULL,
2299 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2302 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2303 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2304 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2307 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2308 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2309 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2312 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2313 snd_pcm_hw_rule_muldivk, (void*) 8,
2314 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2317 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2318 snd_pcm_hw_rule_muldivk, (void*) 8,
2319 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2322 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2323 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2324 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2327 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2328 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2329 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2335 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2337 struct snd_pcm_runtime *runtime = substream->runtime;
2338 struct snd_pcm_hardware *hw = &runtime->hw;
2340 unsigned int mask = 0;
2342 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2343 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2344 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2345 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2346 if (hw_support_mmap(substream)) {
2347 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2348 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2349 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2350 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2351 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2352 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2354 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2358 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2362 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2366 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2367 hw->channels_min, hw->channels_max);
2371 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2372 hw->rate_min, hw->rate_max);
2376 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2377 hw->period_bytes_min, hw->period_bytes_max);
2381 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2382 hw->periods_min, hw->periods_max);
2386 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2387 hw->period_bytes_min, hw->buffer_bytes_max);
2391 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2392 snd_pcm_hw_rule_buffer_bytes_max, substream,
2393 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2398 if (runtime->dma_bytes) {
2399 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2404 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2405 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2406 snd_pcm_hw_rule_rate, hw,
2407 SNDRV_PCM_HW_PARAM_RATE, -1);
2412 /* FIXME: this belong to lowlevel */
2413 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2418 static void pcm_release_private(struct snd_pcm_substream *substream)
2420 if (snd_pcm_stream_linked(substream))
2421 snd_pcm_unlink(substream);
2424 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2426 substream->ref_count--;
2427 if (substream->ref_count > 0)
2430 snd_pcm_drop(substream);
2431 if (substream->hw_opened) {
2432 if (substream->ops->hw_free &&
2433 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2434 substream->ops->hw_free(substream);
2435 substream->ops->close(substream);
2436 substream->hw_opened = 0;
2438 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2439 pm_qos_remove_request(&substream->latency_pm_qos_req);
2440 if (substream->pcm_release) {
2441 substream->pcm_release(substream);
2442 substream->pcm_release = NULL;
2444 snd_pcm_detach_substream(substream);
2446 EXPORT_SYMBOL(snd_pcm_release_substream);
2448 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2450 struct snd_pcm_substream **rsubstream)
2452 struct snd_pcm_substream *substream;
2455 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2458 if (substream->ref_count > 1) {
2459 *rsubstream = substream;
2463 err = snd_pcm_hw_constraints_init(substream);
2465 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2469 if ((err = substream->ops->open(substream)) < 0)
2472 substream->hw_opened = 1;
2474 err = snd_pcm_hw_constraints_complete(substream);
2476 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2480 *rsubstream = substream;
2484 snd_pcm_release_substream(substream);
2487 EXPORT_SYMBOL(snd_pcm_open_substream);
2489 static int snd_pcm_open_file(struct file *file,
2490 struct snd_pcm *pcm,
2493 struct snd_pcm_file *pcm_file;
2494 struct snd_pcm_substream *substream;
2497 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2501 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2502 if (pcm_file == NULL) {
2503 snd_pcm_release_substream(substream);
2506 pcm_file->substream = substream;
2507 if (substream->ref_count == 1) {
2508 substream->file = pcm_file;
2509 substream->pcm_release = pcm_release_private;
2511 file->private_data = pcm_file;
2516 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2518 struct snd_pcm *pcm;
2519 int err = nonseekable_open(inode, file);
2522 pcm = snd_lookup_minor_data(iminor(inode),
2523 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2524 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2526 snd_card_unref(pcm->card);
2530 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2532 struct snd_pcm *pcm;
2533 int err = nonseekable_open(inode, file);
2536 pcm = snd_lookup_minor_data(iminor(inode),
2537 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2538 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2540 snd_card_unref(pcm->card);
2544 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2547 wait_queue_entry_t wait;
2553 err = snd_card_file_add(pcm->card, file);
2556 if (!try_module_get(pcm->card->module)) {
2560 init_waitqueue_entry(&wait, current);
2561 add_wait_queue(&pcm->open_wait, &wait);
2562 mutex_lock(&pcm->open_mutex);
2564 err = snd_pcm_open_file(file, pcm, stream);
2567 if (err == -EAGAIN) {
2568 if (file->f_flags & O_NONBLOCK) {
2574 set_current_state(TASK_INTERRUPTIBLE);
2575 mutex_unlock(&pcm->open_mutex);
2577 mutex_lock(&pcm->open_mutex);
2578 if (pcm->card->shutdown) {
2582 if (signal_pending(current)) {
2587 remove_wait_queue(&pcm->open_wait, &wait);
2588 mutex_unlock(&pcm->open_mutex);
2594 module_put(pcm->card->module);
2596 snd_card_file_remove(pcm->card, file);
2601 static int snd_pcm_release(struct inode *inode, struct file *file)
2603 struct snd_pcm *pcm;
2604 struct snd_pcm_substream *substream;
2605 struct snd_pcm_file *pcm_file;
2607 pcm_file = file->private_data;
2608 substream = pcm_file->substream;
2609 if (snd_BUG_ON(!substream))
2611 pcm = substream->pcm;
2612 mutex_lock(&pcm->open_mutex);
2613 snd_pcm_release_substream(substream);
2615 mutex_unlock(&pcm->open_mutex);
2616 wake_up(&pcm->open_wait);
2617 module_put(pcm->card->module);
2618 snd_card_file_remove(pcm->card, file);
2622 /* check and update PCM state; return 0 or a negative error
2623 * call this inside PCM lock
2625 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2627 switch (substream->runtime->status->state) {
2628 case SNDRV_PCM_STATE_DRAINING:
2629 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2632 case SNDRV_PCM_STATE_RUNNING:
2633 return snd_pcm_update_hw_ptr(substream);
2634 case SNDRV_PCM_STATE_PREPARED:
2635 case SNDRV_PCM_STATE_PAUSED:
2637 case SNDRV_PCM_STATE_SUSPENDED:
2639 case SNDRV_PCM_STATE_XRUN:
2646 /* increase the appl_ptr; returns the processed frames or a negative error */
2647 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2648 snd_pcm_uframes_t frames,
2649 snd_pcm_sframes_t avail)
2651 struct snd_pcm_runtime *runtime = substream->runtime;
2652 snd_pcm_sframes_t appl_ptr;
2657 if (frames > (snd_pcm_uframes_t)avail)
2659 appl_ptr = runtime->control->appl_ptr + frames;
2660 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2661 appl_ptr -= runtime->boundary;
2662 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2663 return ret < 0 ? ret : frames;
2666 /* decrease the appl_ptr; returns the processed frames or zero for error */
2667 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2668 snd_pcm_uframes_t frames,
2669 snd_pcm_sframes_t avail)
2671 struct snd_pcm_runtime *runtime = substream->runtime;
2672 snd_pcm_sframes_t appl_ptr;
2677 if (frames > (snd_pcm_uframes_t)avail)
2679 appl_ptr = runtime->control->appl_ptr - frames;
2681 appl_ptr += runtime->boundary;
2682 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2683 /* NOTE: we return zero for errors because PulseAudio gets depressed
2684 * upon receiving an error from rewind ioctl and stops processing
2685 * any longer. Returning zero means that no rewind is done, so
2686 * it's not absolutely wrong to answer like that.
2688 return ret < 0 ? 0 : frames;
2691 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2692 snd_pcm_uframes_t frames)
2694 struct snd_pcm_runtime *runtime = substream->runtime;
2695 snd_pcm_sframes_t ret;
2700 snd_pcm_stream_lock_irq(substream);
2701 ret = do_pcm_hwsync(substream);
2703 ret = rewind_appl_ptr(substream, frames,
2704 snd_pcm_playback_hw_avail(runtime));
2705 snd_pcm_stream_unlock_irq(substream);
2709 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2710 snd_pcm_uframes_t frames)
2712 struct snd_pcm_runtime *runtime = substream->runtime;
2713 snd_pcm_sframes_t ret;
2718 snd_pcm_stream_lock_irq(substream);
2719 ret = do_pcm_hwsync(substream);
2721 ret = rewind_appl_ptr(substream, frames,
2722 snd_pcm_capture_hw_avail(runtime));
2723 snd_pcm_stream_unlock_irq(substream);
2727 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2728 snd_pcm_uframes_t frames)
2730 struct snd_pcm_runtime *runtime = substream->runtime;
2731 snd_pcm_sframes_t ret;
2736 snd_pcm_stream_lock_irq(substream);
2737 ret = do_pcm_hwsync(substream);
2739 ret = forward_appl_ptr(substream, frames,
2740 snd_pcm_playback_avail(runtime));
2741 snd_pcm_stream_unlock_irq(substream);
2745 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2746 snd_pcm_uframes_t frames)
2748 struct snd_pcm_runtime *runtime = substream->runtime;
2749 snd_pcm_sframes_t ret;
2754 snd_pcm_stream_lock_irq(substream);
2755 ret = do_pcm_hwsync(substream);
2757 ret = forward_appl_ptr(substream, frames,
2758 snd_pcm_capture_avail(runtime));
2759 snd_pcm_stream_unlock_irq(substream);
2763 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2767 snd_pcm_stream_lock_irq(substream);
2768 err = do_pcm_hwsync(substream);
2769 snd_pcm_stream_unlock_irq(substream);
2773 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2774 snd_pcm_sframes_t *delay)
2776 struct snd_pcm_runtime *runtime = substream->runtime;
2778 snd_pcm_sframes_t n = 0;
2780 snd_pcm_stream_lock_irq(substream);
2781 err = do_pcm_hwsync(substream);
2783 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2784 n = snd_pcm_playback_hw_avail(runtime);
2786 n = snd_pcm_capture_avail(runtime);
2787 n += runtime->delay;
2789 snd_pcm_stream_unlock_irq(substream);
2795 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2796 struct snd_pcm_sync_ptr __user *_sync_ptr)
2798 struct snd_pcm_runtime *runtime = substream->runtime;
2799 struct snd_pcm_sync_ptr sync_ptr;
2800 volatile struct snd_pcm_mmap_status *status;
2801 volatile struct snd_pcm_mmap_control *control;
2804 memset(&sync_ptr, 0, sizeof(sync_ptr));
2805 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2807 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2809 status = runtime->status;
2810 control = runtime->control;
2811 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2812 err = snd_pcm_hwsync(substream);
2816 snd_pcm_stream_lock_irq(substream);
2817 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2818 err = pcm_lib_apply_appl_ptr(substream,
2819 sync_ptr.c.control.appl_ptr);
2821 snd_pcm_stream_unlock_irq(substream);
2825 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2827 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2828 control->avail_min = sync_ptr.c.control.avail_min;
2830 sync_ptr.c.control.avail_min = control->avail_min;
2831 sync_ptr.s.status.state = status->state;
2832 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2833 sync_ptr.s.status.tstamp = status->tstamp;
2834 sync_ptr.s.status.suspended_state = status->suspended_state;
2835 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2836 snd_pcm_stream_unlock_irq(substream);
2837 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2842 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2844 struct snd_pcm_runtime *runtime = substream->runtime;
2847 if (get_user(arg, _arg))
2849 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2851 runtime->tstamp_type = arg;
2855 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2856 struct snd_xferi __user *_xferi)
2858 struct snd_xferi xferi;
2859 struct snd_pcm_runtime *runtime = substream->runtime;
2860 snd_pcm_sframes_t result;
2862 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2864 if (put_user(0, &_xferi->result))
2866 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2868 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2869 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2871 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2872 __put_user(result, &_xferi->result);
2873 return result < 0 ? result : 0;
2876 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2877 struct snd_xfern __user *_xfern)
2879 struct snd_xfern xfern;
2880 struct snd_pcm_runtime *runtime = substream->runtime;
2882 snd_pcm_sframes_t result;
2884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2886 if (runtime->channels > 128)
2888 if (put_user(0, &_xfern->result))
2890 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2893 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2895 return PTR_ERR(bufs);
2896 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2897 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2899 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2901 __put_user(result, &_xfern->result);
2902 return result < 0 ? result : 0;
2905 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2906 snd_pcm_uframes_t __user *_frames)
2908 snd_pcm_uframes_t frames;
2909 snd_pcm_sframes_t result;
2911 if (get_user(frames, _frames))
2913 if (put_user(0, _frames))
2915 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2916 result = snd_pcm_playback_rewind(substream, frames);
2918 result = snd_pcm_capture_rewind(substream, frames);
2919 __put_user(result, _frames);
2920 return result < 0 ? result : 0;
2923 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2924 snd_pcm_uframes_t __user *_frames)
2926 snd_pcm_uframes_t frames;
2927 snd_pcm_sframes_t result;
2929 if (get_user(frames, _frames))
2931 if (put_user(0, _frames))
2933 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2934 result = snd_pcm_playback_forward(substream, frames);
2936 result = snd_pcm_capture_forward(substream, frames);
2937 __put_user(result, _frames);
2938 return result < 0 ? result : 0;
2941 static int snd_pcm_common_ioctl(struct file *file,
2942 struct snd_pcm_substream *substream,
2943 unsigned int cmd, void __user *arg)
2945 struct snd_pcm_file *pcm_file = file->private_data;
2948 if (PCM_RUNTIME_CHECK(substream))
2951 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2956 case SNDRV_PCM_IOCTL_PVERSION:
2957 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2958 case SNDRV_PCM_IOCTL_INFO:
2959 return snd_pcm_info_user(substream, arg);
2960 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2962 case SNDRV_PCM_IOCTL_TTSTAMP:
2963 return snd_pcm_tstamp(substream, arg);
2964 case SNDRV_PCM_IOCTL_USER_PVERSION:
2965 if (get_user(pcm_file->user_pversion,
2966 (unsigned int __user *)arg))
2969 case SNDRV_PCM_IOCTL_HW_REFINE:
2970 return snd_pcm_hw_refine_user(substream, arg);
2971 case SNDRV_PCM_IOCTL_HW_PARAMS:
2972 return snd_pcm_hw_params_user(substream, arg);
2973 case SNDRV_PCM_IOCTL_HW_FREE:
2974 return snd_pcm_hw_free(substream);
2975 case SNDRV_PCM_IOCTL_SW_PARAMS:
2976 return snd_pcm_sw_params_user(substream, arg);
2977 case SNDRV_PCM_IOCTL_STATUS:
2978 return snd_pcm_status_user(substream, arg, false);
2979 case SNDRV_PCM_IOCTL_STATUS_EXT:
2980 return snd_pcm_status_user(substream, arg, true);
2981 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2982 return snd_pcm_channel_info_user(substream, arg);
2983 case SNDRV_PCM_IOCTL_PREPARE:
2984 return snd_pcm_prepare(substream, file);
2985 case SNDRV_PCM_IOCTL_RESET:
2986 return snd_pcm_reset(substream);
2987 case SNDRV_PCM_IOCTL_START:
2988 return snd_pcm_start_lock_irq(substream);
2989 case SNDRV_PCM_IOCTL_LINK:
2990 return snd_pcm_link(substream, (int)(unsigned long) arg);
2991 case SNDRV_PCM_IOCTL_UNLINK:
2992 return snd_pcm_unlink(substream);
2993 case SNDRV_PCM_IOCTL_RESUME:
2994 return snd_pcm_resume(substream);
2995 case SNDRV_PCM_IOCTL_XRUN:
2996 return snd_pcm_xrun(substream);
2997 case SNDRV_PCM_IOCTL_HWSYNC:
2998 return snd_pcm_hwsync(substream);
2999 case SNDRV_PCM_IOCTL_DELAY:
3001 snd_pcm_sframes_t delay;
3002 snd_pcm_sframes_t __user *res = arg;
3005 err = snd_pcm_delay(substream, &delay);
3008 if (put_user(delay, res))
3012 case SNDRV_PCM_IOCTL_SYNC_PTR:
3013 return snd_pcm_sync_ptr(substream, arg);
3014 #ifdef CONFIG_SND_SUPPORT_OLD_API
3015 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3016 return snd_pcm_hw_refine_old_user(substream, arg);
3017 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3018 return snd_pcm_hw_params_old_user(substream, arg);
3020 case SNDRV_PCM_IOCTL_DRAIN:
3021 return snd_pcm_drain(substream, file);
3022 case SNDRV_PCM_IOCTL_DROP:
3023 return snd_pcm_drop(substream);
3024 case SNDRV_PCM_IOCTL_PAUSE:
3025 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
3027 (int)(unsigned long)arg);
3028 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3029 case SNDRV_PCM_IOCTL_READI_FRAMES:
3030 return snd_pcm_xferi_frames_ioctl(substream, arg);
3031 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3032 case SNDRV_PCM_IOCTL_READN_FRAMES:
3033 return snd_pcm_xfern_frames_ioctl(substream, arg);
3034 case SNDRV_PCM_IOCTL_REWIND:
3035 return snd_pcm_rewind_ioctl(substream, arg);
3036 case SNDRV_PCM_IOCTL_FORWARD:
3037 return snd_pcm_forward_ioctl(substream, arg);
3039 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3043 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3046 struct snd_pcm_file *pcm_file;
3048 pcm_file = file->private_data;
3050 if (((cmd >> 8) & 0xff) != 'A')
3053 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3054 (void __user *)arg);
3058 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3059 * @substream: PCM substream
3061 * @arg: IOCTL argument
3063 * The function is provided primarily for OSS layer and USB gadget drivers,
3064 * and it allows only the limited set of ioctls (hw_params, sw_params,
3065 * prepare, start, drain, drop, forward).
3067 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3068 unsigned int cmd, void *arg)
3070 snd_pcm_uframes_t *frames = arg;
3071 snd_pcm_sframes_t result;
3074 case SNDRV_PCM_IOCTL_FORWARD:
3076 /* provided only for OSS; capture-only and no value returned */
3077 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3079 result = snd_pcm_capture_forward(substream, *frames);
3080 return result < 0 ? result : 0;
3082 case SNDRV_PCM_IOCTL_HW_PARAMS:
3083 return snd_pcm_hw_params(substream, arg);
3084 case SNDRV_PCM_IOCTL_SW_PARAMS:
3085 return snd_pcm_sw_params(substream, arg);
3086 case SNDRV_PCM_IOCTL_PREPARE:
3087 return snd_pcm_prepare(substream, NULL);
3088 case SNDRV_PCM_IOCTL_START:
3089 return snd_pcm_start_lock_irq(substream);
3090 case SNDRV_PCM_IOCTL_DRAIN:
3091 return snd_pcm_drain(substream, NULL);
3092 case SNDRV_PCM_IOCTL_DROP:
3093 return snd_pcm_drop(substream);
3094 case SNDRV_PCM_IOCTL_DELAY:
3095 return snd_pcm_delay(substream, frames);
3100 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3102 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3105 struct snd_pcm_file *pcm_file;
3106 struct snd_pcm_substream *substream;
3107 struct snd_pcm_runtime *runtime;
3108 snd_pcm_sframes_t result;
3110 pcm_file = file->private_data;
3111 substream = pcm_file->substream;
3112 if (PCM_RUNTIME_CHECK(substream))
3114 runtime = substream->runtime;
3115 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3117 if (!frame_aligned(runtime, count))
3119 count = bytes_to_frames(runtime, count);
3120 result = snd_pcm_lib_read(substream, buf, count);
3122 result = frames_to_bytes(runtime, result);
3126 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3127 size_t count, loff_t * offset)
3129 struct snd_pcm_file *pcm_file;
3130 struct snd_pcm_substream *substream;
3131 struct snd_pcm_runtime *runtime;
3132 snd_pcm_sframes_t result;
3134 pcm_file = file->private_data;
3135 substream = pcm_file->substream;
3136 if (PCM_RUNTIME_CHECK(substream))
3138 runtime = substream->runtime;
3139 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3141 if (!frame_aligned(runtime, count))
3143 count = bytes_to_frames(runtime, count);
3144 result = snd_pcm_lib_write(substream, buf, count);
3146 result = frames_to_bytes(runtime, result);
3150 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3152 struct snd_pcm_file *pcm_file;
3153 struct snd_pcm_substream *substream;
3154 struct snd_pcm_runtime *runtime;
3155 snd_pcm_sframes_t result;
3158 snd_pcm_uframes_t frames;
3160 pcm_file = iocb->ki_filp->private_data;
3161 substream = pcm_file->substream;
3162 if (PCM_RUNTIME_CHECK(substream))
3164 runtime = substream->runtime;
3165 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3167 if (!iter_is_iovec(to))
3169 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3171 if (!frame_aligned(runtime, to->iov->iov_len))
3173 frames = bytes_to_samples(runtime, to->iov->iov_len);
3174 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3177 for (i = 0; i < to->nr_segs; ++i)
3178 bufs[i] = to->iov[i].iov_base;
3179 result = snd_pcm_lib_readv(substream, bufs, frames);
3181 result = frames_to_bytes(runtime, result);
3186 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3188 struct snd_pcm_file *pcm_file;
3189 struct snd_pcm_substream *substream;
3190 struct snd_pcm_runtime *runtime;
3191 snd_pcm_sframes_t result;
3194 snd_pcm_uframes_t frames;
3196 pcm_file = iocb->ki_filp->private_data;
3197 substream = pcm_file->substream;
3198 if (PCM_RUNTIME_CHECK(substream))
3200 runtime = substream->runtime;
3201 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3203 if (!iter_is_iovec(from))
3205 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3206 !frame_aligned(runtime, from->iov->iov_len))
3208 frames = bytes_to_samples(runtime, from->iov->iov_len);
3209 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3212 for (i = 0; i < from->nr_segs; ++i)
3213 bufs[i] = from->iov[i].iov_base;
3214 result = snd_pcm_lib_writev(substream, bufs, frames);
3216 result = frames_to_bytes(runtime, result);
3221 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3223 struct snd_pcm_file *pcm_file;
3224 struct snd_pcm_substream *substream;
3225 struct snd_pcm_runtime *runtime;
3227 snd_pcm_uframes_t avail;
3229 pcm_file = file->private_data;
3231 substream = pcm_file->substream;
3232 if (PCM_RUNTIME_CHECK(substream))
3233 return POLLOUT | POLLWRNORM | POLLERR;
3234 runtime = substream->runtime;
3236 poll_wait(file, &runtime->sleep, wait);
3238 snd_pcm_stream_lock_irq(substream);
3239 avail = snd_pcm_playback_avail(runtime);
3240 switch (runtime->status->state) {
3241 case SNDRV_PCM_STATE_RUNNING:
3242 case SNDRV_PCM_STATE_PREPARED:
3243 case SNDRV_PCM_STATE_PAUSED:
3244 if (avail >= runtime->control->avail_min) {
3245 mask = POLLOUT | POLLWRNORM;
3249 case SNDRV_PCM_STATE_DRAINING:
3253 mask = POLLOUT | POLLWRNORM | POLLERR;
3256 snd_pcm_stream_unlock_irq(substream);
3260 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3262 struct snd_pcm_file *pcm_file;
3263 struct snd_pcm_substream *substream;
3264 struct snd_pcm_runtime *runtime;
3266 snd_pcm_uframes_t avail;
3268 pcm_file = file->private_data;
3270 substream = pcm_file->substream;
3271 if (PCM_RUNTIME_CHECK(substream))
3272 return POLLIN | POLLRDNORM | POLLERR;
3273 runtime = substream->runtime;
3275 poll_wait(file, &runtime->sleep, wait);
3277 snd_pcm_stream_lock_irq(substream);
3278 avail = snd_pcm_capture_avail(runtime);
3279 switch (runtime->status->state) {
3280 case SNDRV_PCM_STATE_RUNNING:
3281 case SNDRV_PCM_STATE_PREPARED:
3282 case SNDRV_PCM_STATE_PAUSED:
3283 if (avail >= runtime->control->avail_min) {
3284 mask = POLLIN | POLLRDNORM;
3289 case SNDRV_PCM_STATE_DRAINING:
3291 mask = POLLIN | POLLRDNORM;
3296 mask = POLLIN | POLLRDNORM | POLLERR;
3299 snd_pcm_stream_unlock_irq(substream);
3308 * Only on coherent architectures, we can mmap the status and the control records
3309 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3311 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3313 * mmap status record
3315 static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3317 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3318 struct snd_pcm_runtime *runtime;
3320 if (substream == NULL)
3321 return VM_FAULT_SIGBUS;
3322 runtime = substream->runtime;
3323 vmf->page = virt_to_page(runtime->status);
3324 get_page(vmf->page);
3328 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3330 .fault = snd_pcm_mmap_status_fault,
3333 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3334 struct vm_area_struct *area)
3337 if (!(area->vm_flags & VM_READ))
3339 size = area->vm_end - area->vm_start;
3340 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3342 area->vm_ops = &snd_pcm_vm_ops_status;
3343 area->vm_private_data = substream;
3344 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3349 * mmap control record
3351 static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3353 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3354 struct snd_pcm_runtime *runtime;
3356 if (substream == NULL)
3357 return VM_FAULT_SIGBUS;
3358 runtime = substream->runtime;
3359 vmf->page = virt_to_page(runtime->control);
3360 get_page(vmf->page);
3364 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3366 .fault = snd_pcm_mmap_control_fault,
3369 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3370 struct vm_area_struct *area)
3373 if (!(area->vm_flags & VM_READ))
3375 size = area->vm_end - area->vm_start;
3376 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3378 area->vm_ops = &snd_pcm_vm_ops_control;
3379 area->vm_private_data = substream;
3380 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3384 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3386 if (pcm_file->no_compat_mmap)
3388 /* See pcm_control_mmap_allowed() below.
3389 * Since older alsa-lib requires both status and control mmaps to be
3390 * coupled, we have to disable the status mmap for old alsa-lib, too.
3392 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3393 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3398 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3400 if (pcm_file->no_compat_mmap)
3402 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3403 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3404 * thus it effectively assures the manual update of appl_ptr.
3406 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3411 #else /* ! coherent mmap */
3413 * don't support mmap for status and control records.
3415 #define pcm_status_mmap_allowed(pcm_file) false
3416 #define pcm_control_mmap_allowed(pcm_file) false
3418 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3419 struct vm_area_struct *area)
3423 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3424 struct vm_area_struct *area)
3428 #endif /* coherent mmap */
3430 static inline struct page *
3431 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3433 void *vaddr = substream->runtime->dma_area + ofs;
3434 return virt_to_page(vaddr);
3438 * fault callback for mmapping a RAM page
3440 static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3442 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3443 struct snd_pcm_runtime *runtime;
3444 unsigned long offset;
3448 if (substream == NULL)
3449 return VM_FAULT_SIGBUS;
3450 runtime = substream->runtime;
3451 offset = vmf->pgoff << PAGE_SHIFT;
3452 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3453 if (offset > dma_bytes - PAGE_SIZE)
3454 return VM_FAULT_SIGBUS;
3455 if (substream->ops->page)
3456 page = substream->ops->page(substream, offset);
3458 page = snd_pcm_default_page_ops(substream, offset);
3460 return VM_FAULT_SIGBUS;
3466 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3467 .open = snd_pcm_mmap_data_open,
3468 .close = snd_pcm_mmap_data_close,
3471 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3472 .open = snd_pcm_mmap_data_open,
3473 .close = snd_pcm_mmap_data_close,
3474 .fault = snd_pcm_mmap_data_fault,
3478 * mmap the DMA buffer on RAM
3482 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3483 * @substream: PCM substream
3486 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3487 * this function is invoked implicitly.
3489 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3490 struct vm_area_struct *area)
3492 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3493 #ifdef CONFIG_GENERIC_ALLOCATOR
3494 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3495 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3496 return remap_pfn_range(area, area->vm_start,
3497 substream->dma_buffer.addr >> PAGE_SHIFT,
3498 area->vm_end - area->vm_start, area->vm_page_prot);
3500 #endif /* CONFIG_GENERIC_ALLOCATOR */
3501 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3502 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3503 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3504 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3506 substream->runtime->dma_area,
3507 substream->runtime->dma_addr,
3508 substream->runtime->dma_bytes);
3509 #endif /* CONFIG_X86 */
3510 /* mmap with fault handler */
3511 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3514 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3517 * mmap the DMA buffer on I/O memory area
3519 #if SNDRV_PCM_INFO_MMAP_IOMEM
3521 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3522 * @substream: PCM substream
3525 * When your hardware uses the iomapped pages as the hardware buffer and
3526 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3527 * is supposed to work only on limited architectures.
3529 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3530 struct vm_area_struct *area)
3532 struct snd_pcm_runtime *runtime = substream->runtime;;
3534 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3535 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3537 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3538 #endif /* SNDRV_PCM_INFO_MMAP */
3543 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3544 struct vm_area_struct *area)
3546 struct snd_pcm_runtime *runtime;
3548 unsigned long offset;
3552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3553 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3556 if (!(area->vm_flags & VM_READ))
3559 runtime = substream->runtime;
3560 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3562 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3564 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3565 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3567 size = area->vm_end - area->vm_start;
3568 offset = area->vm_pgoff << PAGE_SHIFT;
3569 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3570 if ((size_t)size > dma_bytes)
3572 if (offset > dma_bytes - size)
3575 area->vm_ops = &snd_pcm_vm_ops_data;
3576 area->vm_private_data = substream;
3577 if (substream->ops->mmap)
3578 err = substream->ops->mmap(substream, area);
3580 err = snd_pcm_lib_default_mmap(substream, area);
3582 atomic_inc(&substream->mmap_count);
3585 EXPORT_SYMBOL(snd_pcm_mmap_data);
3587 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3589 struct snd_pcm_file * pcm_file;
3590 struct snd_pcm_substream *substream;
3591 unsigned long offset;
3593 pcm_file = file->private_data;
3594 substream = pcm_file->substream;
3595 if (PCM_RUNTIME_CHECK(substream))
3598 offset = area->vm_pgoff << PAGE_SHIFT;
3600 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3601 if (!pcm_status_mmap_allowed(pcm_file))
3603 return snd_pcm_mmap_status(substream, file, area);
3604 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3605 if (!pcm_control_mmap_allowed(pcm_file))
3607 return snd_pcm_mmap_control(substream, file, area);
3609 return snd_pcm_mmap_data(substream, file, area);
3614 static int snd_pcm_fasync(int fd, struct file * file, int on)
3616 struct snd_pcm_file * pcm_file;
3617 struct snd_pcm_substream *substream;
3618 struct snd_pcm_runtime *runtime;
3620 pcm_file = file->private_data;
3621 substream = pcm_file->substream;
3622 if (PCM_RUNTIME_CHECK(substream))
3624 runtime = substream->runtime;
3625 return fasync_helper(fd, file, on, &runtime->fasync);
3631 #ifdef CONFIG_COMPAT
3632 #include "pcm_compat.c"
3634 #define snd_pcm_ioctl_compat NULL
3638 * To be removed helpers to keep binary compatibility
3641 #ifdef CONFIG_SND_SUPPORT_OLD_API
3642 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3643 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3645 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3646 struct snd_pcm_hw_params_old *oparams)
3650 memset(params, 0, sizeof(*params));
3651 params->flags = oparams->flags;
3652 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3653 params->masks[i].bits[0] = oparams->masks[i];
3654 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3655 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3656 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3657 params->info = oparams->info;
3658 params->msbits = oparams->msbits;
3659 params->rate_num = oparams->rate_num;
3660 params->rate_den = oparams->rate_den;
3661 params->fifo_size = oparams->fifo_size;
3664 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3665 struct snd_pcm_hw_params *params)
3669 memset(oparams, 0, sizeof(*oparams));
3670 oparams->flags = params->flags;
3671 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3672 oparams->masks[i] = params->masks[i].bits[0];
3673 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3674 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3675 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3676 oparams->info = params->info;
3677 oparams->msbits = params->msbits;
3678 oparams->rate_num = params->rate_num;
3679 oparams->rate_den = params->rate_den;
3680 oparams->fifo_size = params->fifo_size;
3683 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3684 struct snd_pcm_hw_params_old __user * _oparams)
3686 struct snd_pcm_hw_params *params;
3687 struct snd_pcm_hw_params_old *oparams = NULL;
3690 params = kmalloc(sizeof(*params), GFP_KERNEL);
3694 oparams = memdup_user(_oparams, sizeof(*oparams));
3695 if (IS_ERR(oparams)) {
3696 err = PTR_ERR(oparams);
3699 snd_pcm_hw_convert_from_old_params(params, oparams);
3700 err = snd_pcm_hw_refine(substream, params);
3704 err = fixup_unreferenced_params(substream, params);
3708 snd_pcm_hw_convert_to_old_params(oparams, params);
3709 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3718 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3719 struct snd_pcm_hw_params_old __user * _oparams)
3721 struct snd_pcm_hw_params *params;
3722 struct snd_pcm_hw_params_old *oparams = NULL;
3725 params = kmalloc(sizeof(*params), GFP_KERNEL);
3729 oparams = memdup_user(_oparams, sizeof(*oparams));
3730 if (IS_ERR(oparams)) {
3731 err = PTR_ERR(oparams);
3735 snd_pcm_hw_convert_from_old_params(params, oparams);
3736 err = snd_pcm_hw_params(substream, params);
3740 snd_pcm_hw_convert_to_old_params(oparams, params);
3741 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3749 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3752 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3755 unsigned long pgoff,
3756 unsigned long flags)
3758 struct snd_pcm_file *pcm_file = file->private_data;
3759 struct snd_pcm_substream *substream = pcm_file->substream;
3760 struct snd_pcm_runtime *runtime = substream->runtime;
3761 unsigned long offset = pgoff << PAGE_SHIFT;
3764 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3765 return (unsigned long)runtime->status;
3766 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3767 return (unsigned long)runtime->control;
3769 return (unsigned long)runtime->dma_area + offset;
3773 # define snd_pcm_get_unmapped_area NULL
3780 const struct file_operations snd_pcm_f_ops[2] = {
3782 .owner = THIS_MODULE,
3783 .write = snd_pcm_write,
3784 .write_iter = snd_pcm_writev,
3785 .open = snd_pcm_playback_open,
3786 .release = snd_pcm_release,
3787 .llseek = no_llseek,
3788 .poll = snd_pcm_playback_poll,
3789 .unlocked_ioctl = snd_pcm_ioctl,
3790 .compat_ioctl = snd_pcm_ioctl_compat,
3791 .mmap = snd_pcm_mmap,
3792 .fasync = snd_pcm_fasync,
3793 .get_unmapped_area = snd_pcm_get_unmapped_area,
3796 .owner = THIS_MODULE,
3797 .read = snd_pcm_read,
3798 .read_iter = snd_pcm_readv,
3799 .open = snd_pcm_capture_open,
3800 .release = snd_pcm_release,
3801 .llseek = no_llseek,
3802 .poll = snd_pcm_capture_poll,
3803 .unlocked_ioctl = snd_pcm_ioctl,
3804 .compat_ioctl = snd_pcm_ioctl_compat,
3805 .mmap = snd_pcm_mmap,
3806 .fasync = snd_pcm_fasync,
3807 .get_unmapped_area = snd_pcm_get_unmapped_area,