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 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
638 struct snd_pcm_hw_params *params)
640 struct snd_pcm_runtime *runtime;
643 snd_pcm_uframes_t frames;
645 if (PCM_RUNTIME_CHECK(substream))
647 runtime = substream->runtime;
648 snd_pcm_stream_lock_irq(substream);
649 switch (runtime->status->state) {
650 case SNDRV_PCM_STATE_OPEN:
651 case SNDRV_PCM_STATE_SETUP:
652 case SNDRV_PCM_STATE_PREPARED:
655 snd_pcm_stream_unlock_irq(substream);
658 snd_pcm_stream_unlock_irq(substream);
659 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
660 if (!substream->oss.oss)
662 if (atomic_read(&substream->mmap_count))
666 err = snd_pcm_hw_refine(substream, params);
670 err = snd_pcm_hw_params_choose(substream, params);
674 err = fixup_unreferenced_params(substream, params);
678 if (substream->ops->hw_params != NULL) {
679 err = substream->ops->hw_params(substream, params);
684 runtime->access = params_access(params);
685 runtime->format = params_format(params);
686 runtime->subformat = params_subformat(params);
687 runtime->channels = params_channels(params);
688 runtime->rate = params_rate(params);
689 runtime->period_size = params_period_size(params);
690 runtime->periods = params_periods(params);
691 runtime->buffer_size = params_buffer_size(params);
692 runtime->info = params->info;
693 runtime->rate_num = params->rate_num;
694 runtime->rate_den = params->rate_den;
695 runtime->no_period_wakeup =
696 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
697 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
699 bits = snd_pcm_format_physical_width(runtime->format);
700 runtime->sample_bits = bits;
701 bits *= runtime->channels;
702 runtime->frame_bits = bits;
704 while (bits % 8 != 0) {
708 runtime->byte_align = bits / 8;
709 runtime->min_align = frames;
711 /* Default sw params */
712 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
713 runtime->period_step = 1;
714 runtime->control->avail_min = runtime->period_size;
715 runtime->start_threshold = 1;
716 runtime->stop_threshold = runtime->buffer_size;
717 runtime->silence_threshold = 0;
718 runtime->silence_size = 0;
719 runtime->boundary = runtime->buffer_size;
720 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
721 runtime->boundary *= 2;
723 /* clear the buffer for avoiding possible kernel info leaks */
724 if (runtime->dma_area && !substream->ops->copy_user) {
725 size_t size = runtime->dma_bytes;
727 if (runtime->info & SNDRV_PCM_INFO_MMAP)
728 size = PAGE_ALIGN(size);
729 memset(runtime->dma_area, 0, size);
732 snd_pcm_timer_resolution_change(substream);
733 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
735 if (pm_qos_request_active(&substream->latency_pm_qos_req))
736 pm_qos_remove_request(&substream->latency_pm_qos_req);
737 if ((usecs = period_to_usecs(runtime)) >= 0)
738 pm_qos_add_request(&substream->latency_pm_qos_req,
739 PM_QOS_CPU_DMA_LATENCY, usecs);
742 /* hardware might be unusable from this time,
743 so we force application to retry to set
744 the correct hardware parameter settings */
745 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
746 if (substream->ops->hw_free != NULL)
747 substream->ops->hw_free(substream);
751 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
752 struct snd_pcm_hw_params __user * _params)
754 struct snd_pcm_hw_params *params;
757 params = memdup_user(_params, sizeof(*params));
759 return PTR_ERR(params);
761 err = snd_pcm_hw_params(substream, params);
765 if (copy_to_user(_params, params, sizeof(*params)))
772 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
774 struct snd_pcm_runtime *runtime;
777 if (PCM_RUNTIME_CHECK(substream))
779 runtime = substream->runtime;
780 snd_pcm_stream_lock_irq(substream);
781 switch (runtime->status->state) {
782 case SNDRV_PCM_STATE_SETUP:
783 case SNDRV_PCM_STATE_PREPARED:
786 snd_pcm_stream_unlock_irq(substream);
789 snd_pcm_stream_unlock_irq(substream);
790 if (atomic_read(&substream->mmap_count))
792 if (substream->ops->hw_free)
793 result = substream->ops->hw_free(substream);
794 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
795 pm_qos_remove_request(&substream->latency_pm_qos_req);
799 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
800 struct snd_pcm_sw_params *params)
802 struct snd_pcm_runtime *runtime;
805 if (PCM_RUNTIME_CHECK(substream))
807 runtime = substream->runtime;
808 snd_pcm_stream_lock_irq(substream);
809 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
810 snd_pcm_stream_unlock_irq(substream);
813 snd_pcm_stream_unlock_irq(substream);
815 if (params->tstamp_mode < 0 ||
816 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
818 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
819 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
821 if (params->avail_min == 0)
823 if (params->silence_size >= runtime->boundary) {
824 if (params->silence_threshold != 0)
827 if (params->silence_size > params->silence_threshold)
829 if (params->silence_threshold > runtime->buffer_size)
833 snd_pcm_stream_lock_irq(substream);
834 runtime->tstamp_mode = params->tstamp_mode;
835 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
836 runtime->tstamp_type = params->tstamp_type;
837 runtime->period_step = params->period_step;
838 runtime->control->avail_min = params->avail_min;
839 runtime->start_threshold = params->start_threshold;
840 runtime->stop_threshold = params->stop_threshold;
841 runtime->silence_threshold = params->silence_threshold;
842 runtime->silence_size = params->silence_size;
843 params->boundary = runtime->boundary;
844 if (snd_pcm_running(substream)) {
845 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
846 runtime->silence_size > 0)
847 snd_pcm_playback_silence(substream, ULONG_MAX);
848 err = snd_pcm_update_state(substream, runtime);
850 snd_pcm_stream_unlock_irq(substream);
854 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
855 struct snd_pcm_sw_params __user * _params)
857 struct snd_pcm_sw_params params;
859 if (copy_from_user(¶ms, _params, sizeof(params)))
861 err = snd_pcm_sw_params(substream, ¶ms);
862 if (copy_to_user(_params, ¶ms, sizeof(params)))
867 int snd_pcm_status(struct snd_pcm_substream *substream,
868 struct snd_pcm_status *status)
870 struct snd_pcm_runtime *runtime = substream->runtime;
872 snd_pcm_stream_lock_irq(substream);
874 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
875 &runtime->audio_tstamp_config);
877 /* backwards compatible behavior */
878 if (runtime->audio_tstamp_config.type_requested ==
879 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
880 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
881 runtime->audio_tstamp_config.type_requested =
882 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
884 runtime->audio_tstamp_config.type_requested =
885 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
886 runtime->audio_tstamp_report.valid = 0;
888 runtime->audio_tstamp_report.valid = 1;
890 status->state = runtime->status->state;
891 status->suspended_state = runtime->status->suspended_state;
892 if (status->state == SNDRV_PCM_STATE_OPEN)
894 status->trigger_tstamp = runtime->trigger_tstamp;
895 if (snd_pcm_running(substream)) {
896 snd_pcm_update_hw_ptr(substream);
897 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
898 status->tstamp = runtime->status->tstamp;
899 status->driver_tstamp = runtime->driver_tstamp;
900 status->audio_tstamp =
901 runtime->status->audio_tstamp;
902 if (runtime->audio_tstamp_report.valid == 1)
903 /* backwards compatibility, no report provided in COMPAT mode */
904 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
905 &status->audio_tstamp_accuracy,
906 &runtime->audio_tstamp_report);
911 /* get tstamp only in fallback mode and only if enabled */
912 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
913 snd_pcm_gettime(runtime, &status->tstamp);
916 status->appl_ptr = runtime->control->appl_ptr;
917 status->hw_ptr = runtime->status->hw_ptr;
918 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
919 status->avail = snd_pcm_playback_avail(runtime);
920 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
921 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
922 status->delay = runtime->buffer_size - status->avail;
923 status->delay += runtime->delay;
927 status->avail = snd_pcm_capture_avail(runtime);
928 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
929 status->delay = status->avail + runtime->delay;
933 status->avail_max = runtime->avail_max;
934 status->overrange = runtime->overrange;
935 runtime->avail_max = 0;
936 runtime->overrange = 0;
938 snd_pcm_stream_unlock_irq(substream);
942 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
943 struct snd_pcm_status __user * _status,
946 struct snd_pcm_status status;
949 memset(&status, 0, sizeof(status));
951 * with extension, parameters are read/write,
952 * get audio_tstamp_data from user,
953 * ignore rest of status structure
955 if (ext && get_user(status.audio_tstamp_data,
956 (u32 __user *)(&_status->audio_tstamp_data)))
958 res = snd_pcm_status(substream, &status);
961 if (copy_to_user(_status, &status, sizeof(status)))
966 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
967 struct snd_pcm_channel_info * info)
969 struct snd_pcm_runtime *runtime;
970 unsigned int channel;
972 channel = info->channel;
973 runtime = substream->runtime;
974 snd_pcm_stream_lock_irq(substream);
975 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
976 snd_pcm_stream_unlock_irq(substream);
979 snd_pcm_stream_unlock_irq(substream);
980 if (channel >= runtime->channels)
982 memset(info, 0, sizeof(*info));
983 info->channel = channel;
984 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
987 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
988 struct snd_pcm_channel_info __user * _info)
990 struct snd_pcm_channel_info info;
993 if (copy_from_user(&info, _info, sizeof(info)))
995 res = snd_pcm_channel_info(substream, &info);
998 if (copy_to_user(_info, &info, sizeof(info)))
1003 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1005 struct snd_pcm_runtime *runtime = substream->runtime;
1006 if (runtime->trigger_master == NULL)
1008 if (runtime->trigger_master == substream) {
1009 if (!runtime->trigger_tstamp_latched)
1010 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1012 snd_pcm_trigger_tstamp(runtime->trigger_master);
1013 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1015 runtime->trigger_master = NULL;
1019 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1020 int (*do_action)(struct snd_pcm_substream *substream, int state);
1021 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1022 void (*post_action)(struct snd_pcm_substream *substream, int state);
1026 * this functions is core for handling of linked stream
1027 * Note: the stream state might be changed also on failure
1028 * Note2: call with calling stream lock + link lock
1030 static int snd_pcm_action_group(const struct action_ops *ops,
1031 struct snd_pcm_substream *substream,
1032 int state, int do_lock)
1034 struct snd_pcm_substream *s = NULL;
1035 struct snd_pcm_substream *s1;
1036 int res = 0, depth = 1;
1038 snd_pcm_group_for_each_entry(s, substream) {
1039 if (do_lock && s != substream) {
1040 if (s->pcm->nonatomic)
1041 mutex_lock_nested(&s->self_group.mutex, depth);
1043 spin_lock_nested(&s->self_group.lock, depth);
1046 res = ops->pre_action(s, state);
1050 snd_pcm_group_for_each_entry(s, substream) {
1051 res = ops->do_action(s, state);
1053 if (ops->undo_action) {
1054 snd_pcm_group_for_each_entry(s1, substream) {
1055 if (s1 == s) /* failed stream */
1057 ops->undo_action(s1, state);
1060 s = NULL; /* unlock all */
1064 snd_pcm_group_for_each_entry(s, substream) {
1065 ops->post_action(s, state);
1069 /* unlock streams */
1070 snd_pcm_group_for_each_entry(s1, substream) {
1071 if (s1 != substream) {
1072 if (s1->pcm->nonatomic)
1073 mutex_unlock(&s1->self_group.mutex);
1075 spin_unlock(&s1->self_group.lock);
1077 if (s1 == s) /* end */
1085 * Note: call with stream lock
1087 static int snd_pcm_action_single(const struct action_ops *ops,
1088 struct snd_pcm_substream *substream,
1093 res = ops->pre_action(substream, state);
1096 res = ops->do_action(substream, state);
1098 ops->post_action(substream, state);
1099 else if (ops->undo_action)
1100 ops->undo_action(substream, state);
1105 * Note: call with stream lock
1107 static int snd_pcm_action(const struct action_ops *ops,
1108 struct snd_pcm_substream *substream,
1113 if (!snd_pcm_stream_linked(substream))
1114 return snd_pcm_action_single(ops, substream, state);
1116 if (substream->pcm->nonatomic) {
1117 if (!mutex_trylock(&substream->group->mutex)) {
1118 mutex_unlock(&substream->self_group.mutex);
1119 mutex_lock(&substream->group->mutex);
1120 mutex_lock(&substream->self_group.mutex);
1122 res = snd_pcm_action_group(ops, substream, state, 1);
1123 mutex_unlock(&substream->group->mutex);
1125 if (!spin_trylock(&substream->group->lock)) {
1126 spin_unlock(&substream->self_group.lock);
1127 spin_lock(&substream->group->lock);
1128 spin_lock(&substream->self_group.lock);
1130 res = snd_pcm_action_group(ops, substream, state, 1);
1131 spin_unlock(&substream->group->lock);
1137 * Note: don't use any locks before
1139 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1140 struct snd_pcm_substream *substream,
1145 snd_pcm_stream_lock_irq(substream);
1146 res = snd_pcm_action(ops, substream, state);
1147 snd_pcm_stream_unlock_irq(substream);
1153 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1154 struct snd_pcm_substream *substream,
1159 down_read(&snd_pcm_link_rwsem);
1160 if (snd_pcm_stream_linked(substream))
1161 res = snd_pcm_action_group(ops, substream, state, 0);
1163 res = snd_pcm_action_single(ops, substream, state);
1164 up_read(&snd_pcm_link_rwsem);
1171 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1173 struct snd_pcm_runtime *runtime = substream->runtime;
1174 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1176 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1177 !snd_pcm_playback_data(substream))
1179 runtime->trigger_tstamp_latched = false;
1180 runtime->trigger_master = substream;
1184 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1186 if (substream->runtime->trigger_master != substream)
1188 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1191 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1193 if (substream->runtime->trigger_master == substream)
1194 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1197 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1199 struct snd_pcm_runtime *runtime = substream->runtime;
1200 snd_pcm_trigger_tstamp(substream);
1201 runtime->hw_ptr_jiffies = jiffies;
1202 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1204 runtime->status->state = state;
1205 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1206 runtime->silence_size > 0)
1207 snd_pcm_playback_silence(substream, ULONG_MAX);
1208 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1211 static const struct action_ops snd_pcm_action_start = {
1212 .pre_action = snd_pcm_pre_start,
1213 .do_action = snd_pcm_do_start,
1214 .undo_action = snd_pcm_undo_start,
1215 .post_action = snd_pcm_post_start
1219 * snd_pcm_start - start all linked streams
1220 * @substream: the PCM substream instance
1222 * Return: Zero if successful, or a negative error code.
1223 * The stream lock must be acquired before calling this function.
1225 int snd_pcm_start(struct snd_pcm_substream *substream)
1227 return snd_pcm_action(&snd_pcm_action_start, substream,
1228 SNDRV_PCM_STATE_RUNNING);
1231 /* take the stream lock and start the streams */
1232 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1234 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1235 SNDRV_PCM_STATE_RUNNING);
1241 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1243 struct snd_pcm_runtime *runtime = substream->runtime;
1244 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1246 runtime->trigger_master = substream;
1250 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1252 if (substream->runtime->trigger_master == substream &&
1253 snd_pcm_running(substream))
1254 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1255 return 0; /* unconditonally stop all substreams */
1258 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1260 struct snd_pcm_runtime *runtime = substream->runtime;
1261 if (runtime->status->state != state) {
1262 snd_pcm_trigger_tstamp(substream);
1263 runtime->status->state = state;
1264 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1266 wake_up(&runtime->sleep);
1267 wake_up(&runtime->tsleep);
1270 static const struct action_ops snd_pcm_action_stop = {
1271 .pre_action = snd_pcm_pre_stop,
1272 .do_action = snd_pcm_do_stop,
1273 .post_action = snd_pcm_post_stop
1277 * snd_pcm_stop - try to stop all running streams in the substream group
1278 * @substream: the PCM substream instance
1279 * @state: PCM state after stopping the stream
1281 * The state of each stream is then changed to the given state unconditionally.
1283 * Return: Zero if successful, or a negative error code.
1285 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1287 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1289 EXPORT_SYMBOL(snd_pcm_stop);
1292 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1293 * @substream: the PCM substream
1295 * After stopping, the state is changed to SETUP.
1296 * Unlike snd_pcm_stop(), this affects only the given stream.
1298 * Return: Zero if succesful, or a negative error code.
1300 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1302 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1303 SNDRV_PCM_STATE_SETUP);
1307 * snd_pcm_stop_xrun - stop the running streams as XRUN
1308 * @substream: the PCM substream instance
1310 * This stops the given running substream (and all linked substreams) as XRUN.
1311 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1313 * Return: Zero if successful, or a negative error code.
1315 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1317 unsigned long flags;
1320 snd_pcm_stream_lock_irqsave(substream, flags);
1321 if (snd_pcm_running(substream))
1322 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1323 snd_pcm_stream_unlock_irqrestore(substream, flags);
1326 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1331 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1333 struct snd_pcm_runtime *runtime = substream->runtime;
1334 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1337 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1339 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1341 runtime->trigger_master = substream;
1345 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1347 if (substream->runtime->trigger_master != substream)
1349 /* some drivers might use hw_ptr to recover from the pause -
1350 update the hw_ptr now */
1352 snd_pcm_update_hw_ptr(substream);
1353 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1354 * a delta between the current jiffies, this gives a large enough
1355 * delta, effectively to skip the check once.
1357 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1358 return substream->ops->trigger(substream,
1359 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1360 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1363 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1365 if (substream->runtime->trigger_master == substream)
1366 substream->ops->trigger(substream,
1367 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1368 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1371 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1373 struct snd_pcm_runtime *runtime = substream->runtime;
1374 snd_pcm_trigger_tstamp(substream);
1376 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1377 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1378 wake_up(&runtime->sleep);
1379 wake_up(&runtime->tsleep);
1381 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1382 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1386 static const struct action_ops snd_pcm_action_pause = {
1387 .pre_action = snd_pcm_pre_pause,
1388 .do_action = snd_pcm_do_pause,
1389 .undo_action = snd_pcm_undo_pause,
1390 .post_action = snd_pcm_post_pause
1394 * Push/release the pause for all linked streams.
1396 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1398 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1404 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1406 struct snd_pcm_runtime *runtime = substream->runtime;
1407 switch (runtime->status->state) {
1408 case SNDRV_PCM_STATE_SUSPENDED:
1410 /* unresumable PCM state; return -EBUSY for skipping suspend */
1411 case SNDRV_PCM_STATE_OPEN:
1412 case SNDRV_PCM_STATE_SETUP:
1413 case SNDRV_PCM_STATE_DISCONNECTED:
1416 runtime->trigger_master = substream;
1420 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1422 struct snd_pcm_runtime *runtime = substream->runtime;
1423 if (runtime->trigger_master != substream)
1425 if (! snd_pcm_running(substream))
1427 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1428 return 0; /* suspend unconditionally */
1431 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1433 struct snd_pcm_runtime *runtime = substream->runtime;
1434 snd_pcm_trigger_tstamp(substream);
1435 runtime->status->suspended_state = runtime->status->state;
1436 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1437 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1438 wake_up(&runtime->sleep);
1439 wake_up(&runtime->tsleep);
1442 static const struct action_ops snd_pcm_action_suspend = {
1443 .pre_action = snd_pcm_pre_suspend,
1444 .do_action = snd_pcm_do_suspend,
1445 .post_action = snd_pcm_post_suspend
1449 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1450 * @substream: the PCM substream
1452 * After this call, all streams are changed to SUSPENDED state.
1454 * Return: Zero if successful (or @substream is %NULL), or a negative error
1457 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1460 unsigned long flags;
1465 snd_pcm_stream_lock_irqsave(substream, flags);
1466 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1467 snd_pcm_stream_unlock_irqrestore(substream, flags);
1470 EXPORT_SYMBOL(snd_pcm_suspend);
1473 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1474 * @pcm: the PCM instance
1476 * After this call, all streams are changed to SUSPENDED state.
1478 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1480 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1482 struct snd_pcm_substream *substream;
1483 int stream, err = 0;
1488 for (stream = 0; stream < 2; stream++) {
1489 for (substream = pcm->streams[stream].substream;
1490 substream; substream = substream->next) {
1491 /* FIXME: the open/close code should lock this as well */
1492 if (substream->runtime == NULL)
1496 * Skip BE dai link PCM's that are internal and may
1497 * not have their substream ops set.
1499 if (!substream->ops)
1502 err = snd_pcm_suspend(substream);
1503 if (err < 0 && err != -EBUSY)
1509 EXPORT_SYMBOL(snd_pcm_suspend_all);
1513 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1515 struct snd_pcm_runtime *runtime = substream->runtime;
1516 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1518 runtime->trigger_master = substream;
1522 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1524 struct snd_pcm_runtime *runtime = substream->runtime;
1525 if (runtime->trigger_master != substream)
1527 /* DMA not running previously? */
1528 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1529 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1530 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1532 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1535 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1537 if (substream->runtime->trigger_master == substream &&
1538 snd_pcm_running(substream))
1539 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1542 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1544 struct snd_pcm_runtime *runtime = substream->runtime;
1545 snd_pcm_trigger_tstamp(substream);
1546 runtime->status->state = runtime->status->suspended_state;
1547 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1550 static const struct action_ops snd_pcm_action_resume = {
1551 .pre_action = snd_pcm_pre_resume,
1552 .do_action = snd_pcm_do_resume,
1553 .undo_action = snd_pcm_undo_resume,
1554 .post_action = snd_pcm_post_resume
1557 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1559 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1564 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1569 #endif /* CONFIG_PM */
1574 * Change the RUNNING stream(s) to XRUN state.
1576 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1578 struct snd_pcm_runtime *runtime = substream->runtime;
1581 snd_pcm_stream_lock_irq(substream);
1582 switch (runtime->status->state) {
1583 case SNDRV_PCM_STATE_XRUN:
1584 result = 0; /* already there */
1586 case SNDRV_PCM_STATE_RUNNING:
1587 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1592 snd_pcm_stream_unlock_irq(substream);
1599 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1601 struct snd_pcm_runtime *runtime = substream->runtime;
1602 switch (runtime->status->state) {
1603 case SNDRV_PCM_STATE_RUNNING:
1604 case SNDRV_PCM_STATE_PREPARED:
1605 case SNDRV_PCM_STATE_PAUSED:
1606 case SNDRV_PCM_STATE_SUSPENDED:
1613 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1615 struct snd_pcm_runtime *runtime = substream->runtime;
1616 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1619 runtime->hw_ptr_base = 0;
1620 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1621 runtime->status->hw_ptr % runtime->period_size;
1622 runtime->silence_start = runtime->status->hw_ptr;
1623 runtime->silence_filled = 0;
1627 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1629 struct snd_pcm_runtime *runtime = substream->runtime;
1630 runtime->control->appl_ptr = runtime->status->hw_ptr;
1631 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1632 runtime->silence_size > 0)
1633 snd_pcm_playback_silence(substream, ULONG_MAX);
1636 static const struct action_ops snd_pcm_action_reset = {
1637 .pre_action = snd_pcm_pre_reset,
1638 .do_action = snd_pcm_do_reset,
1639 .post_action = snd_pcm_post_reset
1642 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1644 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1650 /* we use the second argument for updating f_flags */
1651 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1654 struct snd_pcm_runtime *runtime = substream->runtime;
1655 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1656 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1658 if (snd_pcm_running(substream))
1660 substream->f_flags = f_flags;
1664 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1667 err = substream->ops->prepare(substream);
1670 return snd_pcm_do_reset(substream, 0);
1673 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1675 struct snd_pcm_runtime *runtime = substream->runtime;
1676 runtime->control->appl_ptr = runtime->status->hw_ptr;
1677 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1680 static const struct action_ops snd_pcm_action_prepare = {
1681 .pre_action = snd_pcm_pre_prepare,
1682 .do_action = snd_pcm_do_prepare,
1683 .post_action = snd_pcm_post_prepare
1687 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1688 * @substream: the PCM substream instance
1689 * @file: file to refer f_flags
1691 * Return: Zero if successful, or a negative error code.
1693 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1699 f_flags = file->f_flags;
1701 f_flags = substream->f_flags;
1703 snd_pcm_stream_lock_irq(substream);
1704 switch (substream->runtime->status->state) {
1705 case SNDRV_PCM_STATE_PAUSED:
1706 snd_pcm_pause(substream, 0);
1708 case SNDRV_PCM_STATE_SUSPENDED:
1709 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1712 snd_pcm_stream_unlock_irq(substream);
1714 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1715 substream, f_flags);
1722 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1724 struct snd_pcm_runtime *runtime = substream->runtime;
1725 switch (runtime->status->state) {
1726 case SNDRV_PCM_STATE_OPEN:
1727 case SNDRV_PCM_STATE_DISCONNECTED:
1728 case SNDRV_PCM_STATE_SUSPENDED:
1731 runtime->trigger_master = substream;
1735 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1737 struct snd_pcm_runtime *runtime = substream->runtime;
1738 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1739 switch (runtime->status->state) {
1740 case SNDRV_PCM_STATE_PREPARED:
1741 /* start playback stream if possible */
1742 if (! snd_pcm_playback_empty(substream)) {
1743 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1744 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1746 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1749 case SNDRV_PCM_STATE_RUNNING:
1750 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1752 case SNDRV_PCM_STATE_XRUN:
1753 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1759 /* stop running stream */
1760 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1761 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1762 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1763 snd_pcm_do_stop(substream, new_state);
1764 snd_pcm_post_stop(substream, new_state);
1768 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1769 runtime->trigger_master == substream &&
1770 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1771 return substream->ops->trigger(substream,
1772 SNDRV_PCM_TRIGGER_DRAIN);
1777 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1781 static const struct action_ops snd_pcm_action_drain_init = {
1782 .pre_action = snd_pcm_pre_drain_init,
1783 .do_action = snd_pcm_do_drain_init,
1784 .post_action = snd_pcm_post_drain_init
1787 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1790 * Drain the stream(s).
1791 * When the substream is linked, sync until the draining of all playback streams
1793 * After this call, all streams are supposed to be either SETUP or DRAINING
1794 * (capture only) state.
1796 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1799 struct snd_card *card;
1800 struct snd_pcm_runtime *runtime;
1801 struct snd_pcm_substream *s;
1802 wait_queue_entry_t wait;
1806 card = substream->pcm->card;
1807 runtime = substream->runtime;
1809 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1813 if (file->f_flags & O_NONBLOCK)
1815 } else if (substream->f_flags & O_NONBLOCK)
1818 down_read(&snd_pcm_link_rwsem);
1819 snd_pcm_stream_lock_irq(substream);
1821 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1822 snd_pcm_pause(substream, 0);
1824 /* pre-start/stop - all running streams are changed to DRAINING state */
1825 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1828 /* in non-blocking, we don't wait in ioctl but let caller poll */
1836 struct snd_pcm_runtime *to_check;
1837 if (signal_pending(current)) {
1838 result = -ERESTARTSYS;
1841 /* find a substream to drain */
1843 snd_pcm_group_for_each_entry(s, substream) {
1844 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1846 runtime = s->runtime;
1847 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1853 break; /* all drained */
1854 init_waitqueue_entry(&wait, current);
1855 add_wait_queue(&to_check->sleep, &wait);
1856 snd_pcm_stream_unlock_irq(substream);
1857 up_read(&snd_pcm_link_rwsem);
1858 if (runtime->no_period_wakeup)
1859 tout = MAX_SCHEDULE_TIMEOUT;
1862 if (runtime->rate) {
1863 long t = runtime->period_size * 2 / runtime->rate;
1864 tout = max(t, tout);
1866 tout = msecs_to_jiffies(tout * 1000);
1868 tout = schedule_timeout_interruptible(tout);
1869 down_read(&snd_pcm_link_rwsem);
1870 snd_pcm_stream_lock_irq(substream);
1871 remove_wait_queue(&to_check->sleep, &wait);
1872 if (card->shutdown) {
1877 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1880 dev_dbg(substream->pcm->card->dev,
1881 "playback drain error (DMA or IRQ trouble?)\n");
1882 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1890 snd_pcm_stream_unlock_irq(substream);
1891 up_read(&snd_pcm_link_rwsem);
1899 * Immediately put all linked substreams into SETUP state.
1901 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1903 struct snd_pcm_runtime *runtime;
1906 if (PCM_RUNTIME_CHECK(substream))
1908 runtime = substream->runtime;
1910 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1911 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1914 snd_pcm_stream_lock_irq(substream);
1916 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1917 snd_pcm_pause(substream, 0);
1919 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1920 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1921 snd_pcm_stream_unlock_irq(substream);
1927 static bool is_pcm_file(struct file *file)
1929 struct inode *inode = file_inode(file);
1932 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1934 minor = iminor(inode);
1935 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1936 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1942 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1945 struct snd_pcm_file *pcm_file;
1946 struct snd_pcm_substream *substream1;
1947 struct snd_pcm_group *group;
1948 struct fd f = fdget(fd);
1952 if (!is_pcm_file(f.file)) {
1956 pcm_file = f.file->private_data;
1957 substream1 = pcm_file->substream;
1958 if (substream == substream1) {
1963 group = kmalloc(sizeof(*group), GFP_KERNEL);
1968 down_write_nonfifo(&snd_pcm_link_rwsem);
1969 write_lock_irq(&snd_pcm_link_rwlock);
1970 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1971 substream->runtime->status->state != substream1->runtime->status->state ||
1972 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1976 if (snd_pcm_stream_linked(substream1)) {
1980 if (!snd_pcm_stream_linked(substream)) {
1981 substream->group = group;
1983 spin_lock_init(&substream->group->lock);
1984 mutex_init(&substream->group->mutex);
1985 INIT_LIST_HEAD(&substream->group->substreams);
1986 list_add_tail(&substream->link_list, &substream->group->substreams);
1987 substream->group->count = 1;
1989 list_add_tail(&substream1->link_list, &substream->group->substreams);
1990 substream->group->count++;
1991 substream1->group = substream->group;
1993 write_unlock_irq(&snd_pcm_link_rwlock);
1994 up_write(&snd_pcm_link_rwsem);
1996 snd_card_unref(substream1->pcm->card);
2003 static void relink_to_local(struct snd_pcm_substream *substream)
2005 substream->group = &substream->self_group;
2006 INIT_LIST_HEAD(&substream->self_group.substreams);
2007 list_add_tail(&substream->link_list, &substream->self_group.substreams);
2010 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2012 struct snd_pcm_substream *s;
2015 down_write_nonfifo(&snd_pcm_link_rwsem);
2016 write_lock_irq(&snd_pcm_link_rwlock);
2017 if (!snd_pcm_stream_linked(substream)) {
2021 list_del(&substream->link_list);
2022 substream->group->count--;
2023 if (substream->group->count == 1) { /* detach the last stream, too */
2024 snd_pcm_group_for_each_entry(s, substream) {
2028 kfree(substream->group);
2030 relink_to_local(substream);
2032 write_unlock_irq(&snd_pcm_link_rwlock);
2033 up_write(&snd_pcm_link_rwsem);
2040 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2041 struct snd_pcm_hw_rule *rule)
2043 struct snd_interval t;
2044 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2045 hw_param_interval_c(params, rule->deps[1]), &t);
2046 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2049 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2050 struct snd_pcm_hw_rule *rule)
2052 struct snd_interval t;
2053 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2054 hw_param_interval_c(params, rule->deps[1]), &t);
2055 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2058 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2059 struct snd_pcm_hw_rule *rule)
2061 struct snd_interval t;
2062 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2063 hw_param_interval_c(params, rule->deps[1]),
2064 (unsigned long) rule->private, &t);
2065 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2068 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2069 struct snd_pcm_hw_rule *rule)
2071 struct snd_interval t;
2072 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2073 (unsigned long) rule->private,
2074 hw_param_interval_c(params, rule->deps[1]), &t);
2075 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2078 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2079 struct snd_pcm_hw_rule *rule)
2082 const struct snd_interval *i =
2083 hw_param_interval_c(params, rule->deps[0]);
2085 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2087 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2089 if (! snd_mask_test(mask, k))
2091 bits = snd_pcm_format_physical_width(k);
2093 continue; /* ignore invalid formats */
2094 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2095 snd_mask_reset(&m, k);
2097 return snd_mask_refine(mask, &m);
2100 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2101 struct snd_pcm_hw_rule *rule)
2103 struct snd_interval t;
2109 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2111 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2113 bits = snd_pcm_format_physical_width(k);
2115 continue; /* ignore invalid formats */
2116 if (t.min > (unsigned)bits)
2118 if (t.max < (unsigned)bits)
2122 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2125 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2126 #error "Change this table"
2129 static const unsigned int rates[] = {
2130 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2131 48000, 64000, 88200, 96000, 176400, 192000
2134 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2135 .count = ARRAY_SIZE(rates),
2139 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2140 struct snd_pcm_hw_rule *rule)
2142 struct snd_pcm_hardware *hw = rule->private;
2143 return snd_interval_list(hw_param_interval(params, rule->var),
2144 snd_pcm_known_rates.count,
2145 snd_pcm_known_rates.list, hw->rates);
2148 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2149 struct snd_pcm_hw_rule *rule)
2151 struct snd_interval t;
2152 struct snd_pcm_substream *substream = rule->private;
2154 t.max = substream->buffer_bytes_max;
2158 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2161 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2163 struct snd_pcm_runtime *runtime = substream->runtime;
2164 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2167 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2168 snd_mask_any(constrs_mask(constrs, k));
2171 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2172 snd_interval_any(constrs_interval(constrs, k));
2175 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2176 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2177 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2178 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2179 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2181 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2182 snd_pcm_hw_rule_format, NULL,
2183 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2186 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2187 snd_pcm_hw_rule_sample_bits, NULL,
2188 SNDRV_PCM_HW_PARAM_FORMAT,
2189 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2192 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2193 snd_pcm_hw_rule_div, NULL,
2194 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2197 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2198 snd_pcm_hw_rule_mul, NULL,
2199 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2202 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2203 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2204 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2207 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2208 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2209 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2212 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2213 snd_pcm_hw_rule_div, NULL,
2214 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2217 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2218 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2219 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2222 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2223 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2224 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2227 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2228 snd_pcm_hw_rule_div, NULL,
2229 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2232 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2233 snd_pcm_hw_rule_div, NULL,
2234 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2237 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2238 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2239 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2242 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2243 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2244 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2247 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2248 snd_pcm_hw_rule_mul, NULL,
2249 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2252 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2253 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2254 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2257 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2258 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2259 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2262 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2263 snd_pcm_hw_rule_muldivk, (void*) 8,
2264 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2267 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2268 snd_pcm_hw_rule_muldivk, (void*) 8,
2269 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2272 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2273 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2274 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2277 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2278 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2279 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2285 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2287 struct snd_pcm_runtime *runtime = substream->runtime;
2288 struct snd_pcm_hardware *hw = &runtime->hw;
2290 unsigned int mask = 0;
2292 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2293 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2294 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2295 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2296 if (hw_support_mmap(substream)) {
2297 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2298 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2299 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2300 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2301 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2302 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2304 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2308 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2312 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2316 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2317 hw->channels_min, hw->channels_max);
2321 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2322 hw->rate_min, hw->rate_max);
2326 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2327 hw->period_bytes_min, hw->period_bytes_max);
2331 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2332 hw->periods_min, hw->periods_max);
2336 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2337 hw->period_bytes_min, hw->buffer_bytes_max);
2341 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2342 snd_pcm_hw_rule_buffer_bytes_max, substream,
2343 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2348 if (runtime->dma_bytes) {
2349 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2354 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2355 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2356 snd_pcm_hw_rule_rate, hw,
2357 SNDRV_PCM_HW_PARAM_RATE, -1);
2362 /* FIXME: this belong to lowlevel */
2363 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2368 static void pcm_release_private(struct snd_pcm_substream *substream)
2370 if (snd_pcm_stream_linked(substream))
2371 snd_pcm_unlink(substream);
2374 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2376 substream->ref_count--;
2377 if (substream->ref_count > 0)
2380 snd_pcm_drop(substream);
2381 if (substream->hw_opened) {
2382 if (substream->ops->hw_free &&
2383 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2384 substream->ops->hw_free(substream);
2385 substream->ops->close(substream);
2386 substream->hw_opened = 0;
2388 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2389 pm_qos_remove_request(&substream->latency_pm_qos_req);
2390 if (substream->pcm_release) {
2391 substream->pcm_release(substream);
2392 substream->pcm_release = NULL;
2394 snd_pcm_detach_substream(substream);
2396 EXPORT_SYMBOL(snd_pcm_release_substream);
2398 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2400 struct snd_pcm_substream **rsubstream)
2402 struct snd_pcm_substream *substream;
2405 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2408 if (substream->ref_count > 1) {
2409 *rsubstream = substream;
2413 err = snd_pcm_hw_constraints_init(substream);
2415 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2419 if ((err = substream->ops->open(substream)) < 0)
2422 substream->hw_opened = 1;
2424 err = snd_pcm_hw_constraints_complete(substream);
2426 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2430 *rsubstream = substream;
2434 snd_pcm_release_substream(substream);
2437 EXPORT_SYMBOL(snd_pcm_open_substream);
2439 static int snd_pcm_open_file(struct file *file,
2440 struct snd_pcm *pcm,
2443 struct snd_pcm_file *pcm_file;
2444 struct snd_pcm_substream *substream;
2447 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2451 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2452 if (pcm_file == NULL) {
2453 snd_pcm_release_substream(substream);
2456 pcm_file->substream = substream;
2457 if (substream->ref_count == 1) {
2458 substream->file = pcm_file;
2459 substream->pcm_release = pcm_release_private;
2461 file->private_data = pcm_file;
2466 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2468 struct snd_pcm *pcm;
2469 int err = nonseekable_open(inode, file);
2472 pcm = snd_lookup_minor_data(iminor(inode),
2473 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2474 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2476 snd_card_unref(pcm->card);
2480 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2482 struct snd_pcm *pcm;
2483 int err = nonseekable_open(inode, file);
2486 pcm = snd_lookup_minor_data(iminor(inode),
2487 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2488 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2490 snd_card_unref(pcm->card);
2494 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2497 wait_queue_entry_t wait;
2503 err = snd_card_file_add(pcm->card, file);
2506 if (!try_module_get(pcm->card->module)) {
2510 init_waitqueue_entry(&wait, current);
2511 add_wait_queue(&pcm->open_wait, &wait);
2512 mutex_lock(&pcm->open_mutex);
2514 err = snd_pcm_open_file(file, pcm, stream);
2517 if (err == -EAGAIN) {
2518 if (file->f_flags & O_NONBLOCK) {
2524 set_current_state(TASK_INTERRUPTIBLE);
2525 mutex_unlock(&pcm->open_mutex);
2527 mutex_lock(&pcm->open_mutex);
2528 if (pcm->card->shutdown) {
2532 if (signal_pending(current)) {
2537 remove_wait_queue(&pcm->open_wait, &wait);
2538 mutex_unlock(&pcm->open_mutex);
2544 module_put(pcm->card->module);
2546 snd_card_file_remove(pcm->card, file);
2551 static int snd_pcm_release(struct inode *inode, struct file *file)
2553 struct snd_pcm *pcm;
2554 struct snd_pcm_substream *substream;
2555 struct snd_pcm_file *pcm_file;
2557 pcm_file = file->private_data;
2558 substream = pcm_file->substream;
2559 if (snd_BUG_ON(!substream))
2561 pcm = substream->pcm;
2562 mutex_lock(&pcm->open_mutex);
2563 snd_pcm_release_substream(substream);
2565 mutex_unlock(&pcm->open_mutex);
2566 wake_up(&pcm->open_wait);
2567 module_put(pcm->card->module);
2568 snd_card_file_remove(pcm->card, file);
2572 /* check and update PCM state; return 0 or a negative error
2573 * call this inside PCM lock
2575 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2577 switch (substream->runtime->status->state) {
2578 case SNDRV_PCM_STATE_DRAINING:
2579 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2582 case SNDRV_PCM_STATE_RUNNING:
2583 return snd_pcm_update_hw_ptr(substream);
2584 case SNDRV_PCM_STATE_PREPARED:
2585 case SNDRV_PCM_STATE_PAUSED:
2587 case SNDRV_PCM_STATE_SUSPENDED:
2589 case SNDRV_PCM_STATE_XRUN:
2596 /* increase the appl_ptr; returns the processed frames or a negative error */
2597 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2598 snd_pcm_uframes_t frames,
2599 snd_pcm_sframes_t avail)
2601 struct snd_pcm_runtime *runtime = substream->runtime;
2602 snd_pcm_sframes_t appl_ptr;
2607 if (frames > (snd_pcm_uframes_t)avail)
2609 appl_ptr = runtime->control->appl_ptr + frames;
2610 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2611 appl_ptr -= runtime->boundary;
2612 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2613 return ret < 0 ? ret : frames;
2616 /* decrease the appl_ptr; returns the processed frames or zero for error */
2617 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2618 snd_pcm_uframes_t frames,
2619 snd_pcm_sframes_t avail)
2621 struct snd_pcm_runtime *runtime = substream->runtime;
2622 snd_pcm_sframes_t appl_ptr;
2627 if (frames > (snd_pcm_uframes_t)avail)
2629 appl_ptr = runtime->control->appl_ptr - frames;
2631 appl_ptr += runtime->boundary;
2632 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2633 /* NOTE: we return zero for errors because PulseAudio gets depressed
2634 * upon receiving an error from rewind ioctl and stops processing
2635 * any longer. Returning zero means that no rewind is done, so
2636 * it's not absolutely wrong to answer like that.
2638 return ret < 0 ? 0 : frames;
2641 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2642 snd_pcm_uframes_t frames)
2644 struct snd_pcm_runtime *runtime = substream->runtime;
2645 snd_pcm_sframes_t ret;
2650 snd_pcm_stream_lock_irq(substream);
2651 ret = do_pcm_hwsync(substream);
2653 ret = rewind_appl_ptr(substream, frames,
2654 snd_pcm_playback_hw_avail(runtime));
2655 snd_pcm_stream_unlock_irq(substream);
2659 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2660 snd_pcm_uframes_t frames)
2662 struct snd_pcm_runtime *runtime = substream->runtime;
2663 snd_pcm_sframes_t ret;
2668 snd_pcm_stream_lock_irq(substream);
2669 ret = do_pcm_hwsync(substream);
2671 ret = rewind_appl_ptr(substream, frames,
2672 snd_pcm_capture_hw_avail(runtime));
2673 snd_pcm_stream_unlock_irq(substream);
2677 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2678 snd_pcm_uframes_t frames)
2680 struct snd_pcm_runtime *runtime = substream->runtime;
2681 snd_pcm_sframes_t ret;
2686 snd_pcm_stream_lock_irq(substream);
2687 ret = do_pcm_hwsync(substream);
2689 ret = forward_appl_ptr(substream, frames,
2690 snd_pcm_playback_avail(runtime));
2691 snd_pcm_stream_unlock_irq(substream);
2695 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2696 snd_pcm_uframes_t frames)
2698 struct snd_pcm_runtime *runtime = substream->runtime;
2699 snd_pcm_sframes_t ret;
2704 snd_pcm_stream_lock_irq(substream);
2705 ret = do_pcm_hwsync(substream);
2707 ret = forward_appl_ptr(substream, frames,
2708 snd_pcm_capture_avail(runtime));
2709 snd_pcm_stream_unlock_irq(substream);
2713 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2717 snd_pcm_stream_lock_irq(substream);
2718 err = do_pcm_hwsync(substream);
2719 snd_pcm_stream_unlock_irq(substream);
2723 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2724 snd_pcm_sframes_t *delay)
2726 struct snd_pcm_runtime *runtime = substream->runtime;
2728 snd_pcm_sframes_t n = 0;
2730 snd_pcm_stream_lock_irq(substream);
2731 err = do_pcm_hwsync(substream);
2733 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2734 n = snd_pcm_playback_hw_avail(runtime);
2736 n = snd_pcm_capture_avail(runtime);
2737 n += runtime->delay;
2739 snd_pcm_stream_unlock_irq(substream);
2745 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2746 struct snd_pcm_sync_ptr __user *_sync_ptr)
2748 struct snd_pcm_runtime *runtime = substream->runtime;
2749 struct snd_pcm_sync_ptr sync_ptr;
2750 volatile struct snd_pcm_mmap_status *status;
2751 volatile struct snd_pcm_mmap_control *control;
2754 memset(&sync_ptr, 0, sizeof(sync_ptr));
2755 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2757 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2759 status = runtime->status;
2760 control = runtime->control;
2761 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2762 err = snd_pcm_hwsync(substream);
2766 snd_pcm_stream_lock_irq(substream);
2767 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2768 err = pcm_lib_apply_appl_ptr(substream,
2769 sync_ptr.c.control.appl_ptr);
2771 snd_pcm_stream_unlock_irq(substream);
2775 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2777 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2778 control->avail_min = sync_ptr.c.control.avail_min;
2780 sync_ptr.c.control.avail_min = control->avail_min;
2781 sync_ptr.s.status.state = status->state;
2782 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2783 sync_ptr.s.status.tstamp = status->tstamp;
2784 sync_ptr.s.status.suspended_state = status->suspended_state;
2785 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2786 snd_pcm_stream_unlock_irq(substream);
2787 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2792 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2794 struct snd_pcm_runtime *runtime = substream->runtime;
2797 if (get_user(arg, _arg))
2799 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2801 runtime->tstamp_type = arg;
2805 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
2806 struct snd_xferi __user *_xferi)
2808 struct snd_xferi xferi;
2809 struct snd_pcm_runtime *runtime = substream->runtime;
2810 snd_pcm_sframes_t result;
2812 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2814 if (put_user(0, &_xferi->result))
2816 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2818 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2819 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2821 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2822 __put_user(result, &_xferi->result);
2823 return result < 0 ? result : 0;
2826 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
2827 struct snd_xfern __user *_xfern)
2829 struct snd_xfern xfern;
2830 struct snd_pcm_runtime *runtime = substream->runtime;
2832 snd_pcm_sframes_t result;
2834 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2836 if (runtime->channels > 128)
2838 if (put_user(0, &_xfern->result))
2840 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2843 bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
2845 return PTR_ERR(bufs);
2846 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2847 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2849 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2851 __put_user(result, &_xfern->result);
2852 return result < 0 ? result : 0;
2855 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
2856 snd_pcm_uframes_t __user *_frames)
2858 snd_pcm_uframes_t frames;
2859 snd_pcm_sframes_t result;
2861 if (get_user(frames, _frames))
2863 if (put_user(0, _frames))
2865 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2866 result = snd_pcm_playback_rewind(substream, frames);
2868 result = snd_pcm_capture_rewind(substream, frames);
2869 __put_user(result, _frames);
2870 return result < 0 ? result : 0;
2873 static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
2874 snd_pcm_uframes_t __user *_frames)
2876 snd_pcm_uframes_t frames;
2877 snd_pcm_sframes_t result;
2879 if (get_user(frames, _frames))
2881 if (put_user(0, _frames))
2883 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2884 result = snd_pcm_playback_forward(substream, frames);
2886 result = snd_pcm_capture_forward(substream, frames);
2887 __put_user(result, _frames);
2888 return result < 0 ? result : 0;
2891 static int snd_pcm_common_ioctl(struct file *file,
2892 struct snd_pcm_substream *substream,
2893 unsigned int cmd, void __user *arg)
2895 struct snd_pcm_file *pcm_file = file->private_data;
2898 if (PCM_RUNTIME_CHECK(substream))
2901 res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
2906 case SNDRV_PCM_IOCTL_PVERSION:
2907 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2908 case SNDRV_PCM_IOCTL_INFO:
2909 return snd_pcm_info_user(substream, arg);
2910 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2912 case SNDRV_PCM_IOCTL_TTSTAMP:
2913 return snd_pcm_tstamp(substream, arg);
2914 case SNDRV_PCM_IOCTL_USER_PVERSION:
2915 if (get_user(pcm_file->user_pversion,
2916 (unsigned int __user *)arg))
2919 case SNDRV_PCM_IOCTL_HW_REFINE:
2920 return snd_pcm_hw_refine_user(substream, arg);
2921 case SNDRV_PCM_IOCTL_HW_PARAMS:
2922 return snd_pcm_hw_params_user(substream, arg);
2923 case SNDRV_PCM_IOCTL_HW_FREE:
2924 return snd_pcm_hw_free(substream);
2925 case SNDRV_PCM_IOCTL_SW_PARAMS:
2926 return snd_pcm_sw_params_user(substream, arg);
2927 case SNDRV_PCM_IOCTL_STATUS:
2928 return snd_pcm_status_user(substream, arg, false);
2929 case SNDRV_PCM_IOCTL_STATUS_EXT:
2930 return snd_pcm_status_user(substream, arg, true);
2931 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2932 return snd_pcm_channel_info_user(substream, arg);
2933 case SNDRV_PCM_IOCTL_PREPARE:
2934 return snd_pcm_prepare(substream, file);
2935 case SNDRV_PCM_IOCTL_RESET:
2936 return snd_pcm_reset(substream);
2937 case SNDRV_PCM_IOCTL_START:
2938 return snd_pcm_start_lock_irq(substream);
2939 case SNDRV_PCM_IOCTL_LINK:
2940 return snd_pcm_link(substream, (int)(unsigned long) arg);
2941 case SNDRV_PCM_IOCTL_UNLINK:
2942 return snd_pcm_unlink(substream);
2943 case SNDRV_PCM_IOCTL_RESUME:
2944 return snd_pcm_resume(substream);
2945 case SNDRV_PCM_IOCTL_XRUN:
2946 return snd_pcm_xrun(substream);
2947 case SNDRV_PCM_IOCTL_HWSYNC:
2948 return snd_pcm_hwsync(substream);
2949 case SNDRV_PCM_IOCTL_DELAY:
2951 snd_pcm_sframes_t delay;
2952 snd_pcm_sframes_t __user *res = arg;
2955 err = snd_pcm_delay(substream, &delay);
2958 if (put_user(delay, res))
2962 case SNDRV_PCM_IOCTL_SYNC_PTR:
2963 return snd_pcm_sync_ptr(substream, arg);
2964 #ifdef CONFIG_SND_SUPPORT_OLD_API
2965 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2966 return snd_pcm_hw_refine_old_user(substream, arg);
2967 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2968 return snd_pcm_hw_params_old_user(substream, arg);
2970 case SNDRV_PCM_IOCTL_DRAIN:
2971 return snd_pcm_drain(substream, file);
2972 case SNDRV_PCM_IOCTL_DROP:
2973 return snd_pcm_drop(substream);
2974 case SNDRV_PCM_IOCTL_PAUSE:
2975 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
2977 (int)(unsigned long)arg);
2978 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2979 case SNDRV_PCM_IOCTL_READI_FRAMES:
2980 return snd_pcm_xferi_frames_ioctl(substream, arg);
2981 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2982 case SNDRV_PCM_IOCTL_READN_FRAMES:
2983 return snd_pcm_xfern_frames_ioctl(substream, arg);
2984 case SNDRV_PCM_IOCTL_REWIND:
2985 return snd_pcm_rewind_ioctl(substream, arg);
2986 case SNDRV_PCM_IOCTL_FORWARD:
2987 return snd_pcm_forward_ioctl(substream, arg);
2989 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2993 static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
2996 struct snd_pcm_file *pcm_file;
2998 pcm_file = file->private_data;
3000 if (((cmd >> 8) & 0xff) != 'A')
3003 return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3004 (void __user *)arg);
3008 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3009 * @substream: PCM substream
3011 * @arg: IOCTL argument
3013 * The function is provided primarily for OSS layer and USB gadget drivers,
3014 * and it allows only the limited set of ioctls (hw_params, sw_params,
3015 * prepare, start, drain, drop, forward).
3017 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3018 unsigned int cmd, void *arg)
3020 snd_pcm_uframes_t *frames = arg;
3021 snd_pcm_sframes_t result;
3024 case SNDRV_PCM_IOCTL_FORWARD:
3026 /* provided only for OSS; capture-only and no value returned */
3027 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3029 result = snd_pcm_capture_forward(substream, *frames);
3030 return result < 0 ? result : 0;
3032 case SNDRV_PCM_IOCTL_HW_PARAMS:
3033 return snd_pcm_hw_params(substream, arg);
3034 case SNDRV_PCM_IOCTL_SW_PARAMS:
3035 return snd_pcm_sw_params(substream, arg);
3036 case SNDRV_PCM_IOCTL_PREPARE:
3037 return snd_pcm_prepare(substream, NULL);
3038 case SNDRV_PCM_IOCTL_START:
3039 return snd_pcm_start_lock_irq(substream);
3040 case SNDRV_PCM_IOCTL_DRAIN:
3041 return snd_pcm_drain(substream, NULL);
3042 case SNDRV_PCM_IOCTL_DROP:
3043 return snd_pcm_drop(substream);
3044 case SNDRV_PCM_IOCTL_DELAY:
3045 return snd_pcm_delay(substream, frames);
3050 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3052 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3055 struct snd_pcm_file *pcm_file;
3056 struct snd_pcm_substream *substream;
3057 struct snd_pcm_runtime *runtime;
3058 snd_pcm_sframes_t result;
3060 pcm_file = file->private_data;
3061 substream = pcm_file->substream;
3062 if (PCM_RUNTIME_CHECK(substream))
3064 runtime = substream->runtime;
3065 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3067 if (!frame_aligned(runtime, count))
3069 count = bytes_to_frames(runtime, count);
3070 result = snd_pcm_lib_read(substream, buf, count);
3072 result = frames_to_bytes(runtime, result);
3076 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3077 size_t count, loff_t * offset)
3079 struct snd_pcm_file *pcm_file;
3080 struct snd_pcm_substream *substream;
3081 struct snd_pcm_runtime *runtime;
3082 snd_pcm_sframes_t result;
3084 pcm_file = file->private_data;
3085 substream = pcm_file->substream;
3086 if (PCM_RUNTIME_CHECK(substream))
3088 runtime = substream->runtime;
3089 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3091 if (!frame_aligned(runtime, count))
3093 count = bytes_to_frames(runtime, count);
3094 result = snd_pcm_lib_write(substream, buf, count);
3096 result = frames_to_bytes(runtime, result);
3100 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3102 struct snd_pcm_file *pcm_file;
3103 struct snd_pcm_substream *substream;
3104 struct snd_pcm_runtime *runtime;
3105 snd_pcm_sframes_t result;
3108 snd_pcm_uframes_t frames;
3110 pcm_file = iocb->ki_filp->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 (!iter_is_iovec(to))
3119 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3121 if (!frame_aligned(runtime, to->iov->iov_len))
3123 frames = bytes_to_samples(runtime, to->iov->iov_len);
3124 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3127 for (i = 0; i < to->nr_segs; ++i)
3128 bufs[i] = to->iov[i].iov_base;
3129 result = snd_pcm_lib_readv(substream, bufs, frames);
3131 result = frames_to_bytes(runtime, result);
3136 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3138 struct snd_pcm_file *pcm_file;
3139 struct snd_pcm_substream *substream;
3140 struct snd_pcm_runtime *runtime;
3141 snd_pcm_sframes_t result;
3144 snd_pcm_uframes_t frames;
3146 pcm_file = iocb->ki_filp->private_data;
3147 substream = pcm_file->substream;
3148 if (PCM_RUNTIME_CHECK(substream))
3150 runtime = substream->runtime;
3151 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3153 if (!iter_is_iovec(from))
3155 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3156 !frame_aligned(runtime, from->iov->iov_len))
3158 frames = bytes_to_samples(runtime, from->iov->iov_len);
3159 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3162 for (i = 0; i < from->nr_segs; ++i)
3163 bufs[i] = from->iov[i].iov_base;
3164 result = snd_pcm_lib_writev(substream, bufs, frames);
3166 result = frames_to_bytes(runtime, result);
3171 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3173 struct snd_pcm_file *pcm_file;
3174 struct snd_pcm_substream *substream;
3175 struct snd_pcm_runtime *runtime;
3177 snd_pcm_uframes_t avail;
3179 pcm_file = file->private_data;
3181 substream = pcm_file->substream;
3182 if (PCM_RUNTIME_CHECK(substream))
3183 return POLLOUT | POLLWRNORM | POLLERR;
3184 runtime = substream->runtime;
3186 poll_wait(file, &runtime->sleep, wait);
3188 snd_pcm_stream_lock_irq(substream);
3189 avail = snd_pcm_playback_avail(runtime);
3190 switch (runtime->status->state) {
3191 case SNDRV_PCM_STATE_RUNNING:
3192 case SNDRV_PCM_STATE_PREPARED:
3193 case SNDRV_PCM_STATE_PAUSED:
3194 if (avail >= runtime->control->avail_min) {
3195 mask = POLLOUT | POLLWRNORM;
3199 case SNDRV_PCM_STATE_DRAINING:
3203 mask = POLLOUT | POLLWRNORM | POLLERR;
3206 snd_pcm_stream_unlock_irq(substream);
3210 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3212 struct snd_pcm_file *pcm_file;
3213 struct snd_pcm_substream *substream;
3214 struct snd_pcm_runtime *runtime;
3216 snd_pcm_uframes_t avail;
3218 pcm_file = file->private_data;
3220 substream = pcm_file->substream;
3221 if (PCM_RUNTIME_CHECK(substream))
3222 return POLLIN | POLLRDNORM | POLLERR;
3223 runtime = substream->runtime;
3225 poll_wait(file, &runtime->sleep, wait);
3227 snd_pcm_stream_lock_irq(substream);
3228 avail = snd_pcm_capture_avail(runtime);
3229 switch (runtime->status->state) {
3230 case SNDRV_PCM_STATE_RUNNING:
3231 case SNDRV_PCM_STATE_PREPARED:
3232 case SNDRV_PCM_STATE_PAUSED:
3233 if (avail >= runtime->control->avail_min) {
3234 mask = POLLIN | POLLRDNORM;
3239 case SNDRV_PCM_STATE_DRAINING:
3241 mask = POLLIN | POLLRDNORM;
3246 mask = POLLIN | POLLRDNORM | POLLERR;
3249 snd_pcm_stream_unlock_irq(substream);
3258 * Only on coherent architectures, we can mmap the status and the control records
3259 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3261 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3263 * mmap status record
3265 static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3267 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3268 struct snd_pcm_runtime *runtime;
3270 if (substream == NULL)
3271 return VM_FAULT_SIGBUS;
3272 runtime = substream->runtime;
3273 vmf->page = virt_to_page(runtime->status);
3274 get_page(vmf->page);
3278 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3280 .fault = snd_pcm_mmap_status_fault,
3283 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3284 struct vm_area_struct *area)
3287 if (!(area->vm_flags & VM_READ))
3289 size = area->vm_end - area->vm_start;
3290 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3292 area->vm_ops = &snd_pcm_vm_ops_status;
3293 area->vm_private_data = substream;
3294 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3299 * mmap control record
3301 static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3303 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3304 struct snd_pcm_runtime *runtime;
3306 if (substream == NULL)
3307 return VM_FAULT_SIGBUS;
3308 runtime = substream->runtime;
3309 vmf->page = virt_to_page(runtime->control);
3310 get_page(vmf->page);
3314 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3316 .fault = snd_pcm_mmap_control_fault,
3319 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3320 struct vm_area_struct *area)
3323 if (!(area->vm_flags & VM_READ))
3325 size = area->vm_end - area->vm_start;
3326 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3328 area->vm_ops = &snd_pcm_vm_ops_control;
3329 area->vm_private_data = substream;
3330 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3334 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3336 if (pcm_file->no_compat_mmap)
3338 /* See pcm_control_mmap_allowed() below.
3339 * Since older alsa-lib requires both status and control mmaps to be
3340 * coupled, we have to disable the status mmap for old alsa-lib, too.
3342 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3343 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3348 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3350 if (pcm_file->no_compat_mmap)
3352 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3353 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3354 * thus it effectively assures the manual update of appl_ptr.
3356 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3361 #else /* ! coherent mmap */
3363 * don't support mmap for status and control records.
3365 #define pcm_status_mmap_allowed(pcm_file) false
3366 #define pcm_control_mmap_allowed(pcm_file) false
3368 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3369 struct vm_area_struct *area)
3373 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3374 struct vm_area_struct *area)
3378 #endif /* coherent mmap */
3380 static inline struct page *
3381 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3383 void *vaddr = substream->runtime->dma_area + ofs;
3384 return virt_to_page(vaddr);
3388 * fault callback for mmapping a RAM page
3390 static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3392 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3393 struct snd_pcm_runtime *runtime;
3394 unsigned long offset;
3398 if (substream == NULL)
3399 return VM_FAULT_SIGBUS;
3400 runtime = substream->runtime;
3401 offset = vmf->pgoff << PAGE_SHIFT;
3402 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3403 if (offset > dma_bytes - PAGE_SIZE)
3404 return VM_FAULT_SIGBUS;
3405 if (substream->ops->page)
3406 page = substream->ops->page(substream, offset);
3408 page = snd_pcm_default_page_ops(substream, offset);
3410 return VM_FAULT_SIGBUS;
3416 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3417 .open = snd_pcm_mmap_data_open,
3418 .close = snd_pcm_mmap_data_close,
3421 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3422 .open = snd_pcm_mmap_data_open,
3423 .close = snd_pcm_mmap_data_close,
3424 .fault = snd_pcm_mmap_data_fault,
3428 * mmap the DMA buffer on RAM
3432 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3433 * @substream: PCM substream
3436 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3437 * this function is invoked implicitly.
3439 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3440 struct vm_area_struct *area)
3442 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3443 #ifdef CONFIG_GENERIC_ALLOCATOR
3444 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3445 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3446 return remap_pfn_range(area, area->vm_start,
3447 substream->dma_buffer.addr >> PAGE_SHIFT,
3448 area->vm_end - area->vm_start, area->vm_page_prot);
3450 #endif /* CONFIG_GENERIC_ALLOCATOR */
3451 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3452 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3453 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3454 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3456 substream->runtime->dma_area,
3457 substream->runtime->dma_addr,
3458 substream->runtime->dma_bytes);
3459 #endif /* CONFIG_X86 */
3460 /* mmap with fault handler */
3461 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3464 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3467 * mmap the DMA buffer on I/O memory area
3469 #if SNDRV_PCM_INFO_MMAP_IOMEM
3471 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3472 * @substream: PCM substream
3475 * When your hardware uses the iomapped pages as the hardware buffer and
3476 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3477 * is supposed to work only on limited architectures.
3479 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3480 struct vm_area_struct *area)
3482 struct snd_pcm_runtime *runtime = substream->runtime;;
3484 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3485 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3487 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3488 #endif /* SNDRV_PCM_INFO_MMAP */
3493 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3494 struct vm_area_struct *area)
3496 struct snd_pcm_runtime *runtime;
3498 unsigned long offset;
3502 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3503 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3506 if (!(area->vm_flags & VM_READ))
3509 runtime = substream->runtime;
3510 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3512 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3514 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3515 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3517 size = area->vm_end - area->vm_start;
3518 offset = area->vm_pgoff << PAGE_SHIFT;
3519 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3520 if ((size_t)size > dma_bytes)
3522 if (offset > dma_bytes - size)
3525 area->vm_ops = &snd_pcm_vm_ops_data;
3526 area->vm_private_data = substream;
3527 if (substream->ops->mmap)
3528 err = substream->ops->mmap(substream, area);
3530 err = snd_pcm_lib_default_mmap(substream, area);
3532 atomic_inc(&substream->mmap_count);
3535 EXPORT_SYMBOL(snd_pcm_mmap_data);
3537 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3539 struct snd_pcm_file * pcm_file;
3540 struct snd_pcm_substream *substream;
3541 unsigned long offset;
3543 pcm_file = file->private_data;
3544 substream = pcm_file->substream;
3545 if (PCM_RUNTIME_CHECK(substream))
3548 offset = area->vm_pgoff << PAGE_SHIFT;
3550 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3551 if (!pcm_status_mmap_allowed(pcm_file))
3553 return snd_pcm_mmap_status(substream, file, area);
3554 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3555 if (!pcm_control_mmap_allowed(pcm_file))
3557 return snd_pcm_mmap_control(substream, file, area);
3559 return snd_pcm_mmap_data(substream, file, area);
3564 static int snd_pcm_fasync(int fd, struct file * file, int on)
3566 struct snd_pcm_file * pcm_file;
3567 struct snd_pcm_substream *substream;
3568 struct snd_pcm_runtime *runtime;
3570 pcm_file = file->private_data;
3571 substream = pcm_file->substream;
3572 if (PCM_RUNTIME_CHECK(substream))
3574 runtime = substream->runtime;
3575 return fasync_helper(fd, file, on, &runtime->fasync);
3581 #ifdef CONFIG_COMPAT
3582 #include "pcm_compat.c"
3584 #define snd_pcm_ioctl_compat NULL
3588 * To be removed helpers to keep binary compatibility
3591 #ifdef CONFIG_SND_SUPPORT_OLD_API
3592 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3593 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3595 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3596 struct snd_pcm_hw_params_old *oparams)
3600 memset(params, 0, sizeof(*params));
3601 params->flags = oparams->flags;
3602 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3603 params->masks[i].bits[0] = oparams->masks[i];
3604 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3605 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3606 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3607 params->info = oparams->info;
3608 params->msbits = oparams->msbits;
3609 params->rate_num = oparams->rate_num;
3610 params->rate_den = oparams->rate_den;
3611 params->fifo_size = oparams->fifo_size;
3614 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3615 struct snd_pcm_hw_params *params)
3619 memset(oparams, 0, sizeof(*oparams));
3620 oparams->flags = params->flags;
3621 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3622 oparams->masks[i] = params->masks[i].bits[0];
3623 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3624 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3625 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3626 oparams->info = params->info;
3627 oparams->msbits = params->msbits;
3628 oparams->rate_num = params->rate_num;
3629 oparams->rate_den = params->rate_den;
3630 oparams->fifo_size = params->fifo_size;
3633 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3634 struct snd_pcm_hw_params_old __user * _oparams)
3636 struct snd_pcm_hw_params *params;
3637 struct snd_pcm_hw_params_old *oparams = NULL;
3640 params = kmalloc(sizeof(*params), GFP_KERNEL);
3644 oparams = memdup_user(_oparams, sizeof(*oparams));
3645 if (IS_ERR(oparams)) {
3646 err = PTR_ERR(oparams);
3649 snd_pcm_hw_convert_from_old_params(params, oparams);
3650 err = snd_pcm_hw_refine(substream, params);
3654 err = fixup_unreferenced_params(substream, params);
3658 snd_pcm_hw_convert_to_old_params(oparams, params);
3659 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3668 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3669 struct snd_pcm_hw_params_old __user * _oparams)
3671 struct snd_pcm_hw_params *params;
3672 struct snd_pcm_hw_params_old *oparams = NULL;
3675 params = kmalloc(sizeof(*params), GFP_KERNEL);
3679 oparams = memdup_user(_oparams, sizeof(*oparams));
3680 if (IS_ERR(oparams)) {
3681 err = PTR_ERR(oparams);
3685 snd_pcm_hw_convert_from_old_params(params, oparams);
3686 err = snd_pcm_hw_params(substream, params);
3690 snd_pcm_hw_convert_to_old_params(oparams, params);
3691 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3699 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3702 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3705 unsigned long pgoff,
3706 unsigned long flags)
3708 struct snd_pcm_file *pcm_file = file->private_data;
3709 struct snd_pcm_substream *substream = pcm_file->substream;
3710 struct snd_pcm_runtime *runtime = substream->runtime;
3711 unsigned long offset = pgoff << PAGE_SHIFT;
3714 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3715 return (unsigned long)runtime->status;
3716 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3717 return (unsigned long)runtime->control;
3719 return (unsigned long)runtime->dma_area + offset;
3723 # define snd_pcm_get_unmapped_area NULL
3730 const struct file_operations snd_pcm_f_ops[2] = {
3732 .owner = THIS_MODULE,
3733 .write = snd_pcm_write,
3734 .write_iter = snd_pcm_writev,
3735 .open = snd_pcm_playback_open,
3736 .release = snd_pcm_release,
3737 .llseek = no_llseek,
3738 .poll = snd_pcm_playback_poll,
3739 .unlocked_ioctl = snd_pcm_ioctl,
3740 .compat_ioctl = snd_pcm_ioctl_compat,
3741 .mmap = snd_pcm_mmap,
3742 .fasync = snd_pcm_fasync,
3743 .get_unmapped_area = snd_pcm_get_unmapped_area,
3746 .owner = THIS_MODULE,
3747 .read = snd_pcm_read,
3748 .read_iter = snd_pcm_readv,
3749 .open = snd_pcm_capture_open,
3750 .release = snd_pcm_release,
3751 .llseek = no_llseek,
3752 .poll = snd_pcm_capture_poll,
3753 .unlocked_ioctl = snd_pcm_ioctl,
3754 .compat_ioctl = snd_pcm_ioctl_compat,
3755 .mmap = snd_pcm_mmap,
3756 .fasync = snd_pcm_fasync,
3757 .get_unmapped_area = snd_pcm_get_unmapped_area,