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>
40 #include "pcm_local.h"
42 #ifdef CONFIG_SND_DEBUG
43 #define CREATE_TRACE_POINTS
44 #include "pcm_param_trace.h"
46 #define trace_hw_mask_param_enabled() 0
47 #define trace_hw_interval_param_enabled() 0
48 #define trace_hw_mask_param(substream, type, index, prev, curr)
49 #define trace_hw_interval_param(substream, type, index, prev, curr)
56 struct snd_pcm_hw_params_old {
58 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
59 SNDRV_PCM_HW_PARAM_ACCESS + 1];
60 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
61 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
66 unsigned int rate_num;
67 unsigned int rate_den;
68 snd_pcm_uframes_t fifo_size;
69 unsigned char reserved[64];
72 #ifdef CONFIG_SND_SUPPORT_OLD_API
73 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
74 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
76 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
77 struct snd_pcm_hw_params_old __user * _oparams);
78 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
79 struct snd_pcm_hw_params_old __user * _oparams);
81 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
87 static DEFINE_RWLOCK(snd_pcm_link_rwlock);
88 static DECLARE_RWSEM(snd_pcm_link_rwsem);
90 /* Writer in rwsem may block readers even during its waiting in queue,
91 * and this may lead to a deadlock when the code path takes read sem
92 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
93 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
94 * spin until it gets the lock.
96 static inline void down_write_nonblock(struct rw_semaphore *lock)
98 while (!down_write_trylock(lock))
103 * snd_pcm_stream_lock - Lock the PCM stream
104 * @substream: PCM substream
106 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
107 * flag of the given substream. This also takes the global link rw lock
108 * (or rw sem), too, for avoiding the race with linked streams.
110 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
112 if (substream->pcm->nonatomic) {
113 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
114 mutex_lock(&substream->self_group.mutex);
116 read_lock(&snd_pcm_link_rwlock);
117 spin_lock(&substream->self_group.lock);
120 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
123 * snd_pcm_stream_lock - Unlock the PCM stream
124 * @substream: PCM substream
126 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
128 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
130 if (substream->pcm->nonatomic) {
131 mutex_unlock(&substream->self_group.mutex);
132 up_read(&snd_pcm_link_rwsem);
134 spin_unlock(&substream->self_group.lock);
135 read_unlock(&snd_pcm_link_rwlock);
138 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
141 * snd_pcm_stream_lock_irq - Lock the PCM stream
142 * @substream: PCM substream
144 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
145 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
146 * as snd_pcm_stream_lock().
148 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
150 if (!substream->pcm->nonatomic)
152 snd_pcm_stream_lock(substream);
154 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
157 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
158 * @substream: PCM substream
160 * This is a counter-part of snd_pcm_stream_lock_irq().
162 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
164 snd_pcm_stream_unlock(substream);
165 if (!substream->pcm->nonatomic)
168 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
170 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
172 unsigned long flags = 0;
173 if (!substream->pcm->nonatomic)
174 local_irq_save(flags);
175 snd_pcm_stream_lock(substream);
178 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
181 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
182 * @substream: PCM substream
185 * This is a counter-part of snd_pcm_stream_lock_irqsave().
187 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
190 snd_pcm_stream_unlock(substream);
191 if (!substream->pcm->nonatomic)
192 local_irq_restore(flags);
194 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
196 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
198 struct snd_pcm_runtime *runtime;
199 struct snd_pcm *pcm = substream->pcm;
200 struct snd_pcm_str *pstr = substream->pstr;
202 memset(info, 0, sizeof(*info));
203 info->card = pcm->card->number;
204 info->device = pcm->device;
205 info->stream = substream->stream;
206 info->subdevice = substream->number;
207 strlcpy(info->id, pcm->id, sizeof(info->id));
208 strlcpy(info->name, pcm->name, sizeof(info->name));
209 info->dev_class = pcm->dev_class;
210 info->dev_subclass = pcm->dev_subclass;
211 info->subdevices_count = pstr->substream_count;
212 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
213 strlcpy(info->subname, substream->name, sizeof(info->subname));
214 runtime = substream->runtime;
219 int snd_pcm_info_user(struct snd_pcm_substream *substream,
220 struct snd_pcm_info __user * _info)
222 struct snd_pcm_info *info;
225 info = kmalloc(sizeof(*info), GFP_KERNEL);
228 err = snd_pcm_info(substream, info);
230 if (copy_to_user(_info, info, sizeof(*info)))
237 static bool hw_support_mmap(struct snd_pcm_substream *substream)
239 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
241 /* architecture supports dma_mmap_coherent()? */
242 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
243 if (!substream->ops->mmap &&
244 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
250 static int constrain_mask_params(struct snd_pcm_substream *substream,
251 struct snd_pcm_hw_params *params)
253 struct snd_pcm_hw_constraints *constrs =
254 &substream->runtime->hw_constraints;
257 struct snd_mask old_mask;
260 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
261 m = hw_param_mask(params, k);
262 if (snd_mask_empty(m))
265 /* This parameter is not requested to change by a caller. */
266 if (!(params->rmask & (1 << k)))
269 if (trace_hw_mask_param_enabled())
272 changed = snd_mask_refine(m, constrs_mask(constrs, k));
278 /* Set corresponding flag so that the caller gets it. */
279 trace_hw_mask_param(substream, k, 0, &old_mask, m);
280 params->cmask |= 1 << k;
286 static int constrain_interval_params(struct snd_pcm_substream *substream,
287 struct snd_pcm_hw_params *params)
289 struct snd_pcm_hw_constraints *constrs =
290 &substream->runtime->hw_constraints;
291 struct snd_interval *i;
293 struct snd_interval old_interval;
296 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
297 i = hw_param_interval(params, k);
298 if (snd_interval_empty(i))
301 /* This parameter is not requested to change by a caller. */
302 if (!(params->rmask & (1 << k)))
305 if (trace_hw_interval_param_enabled())
308 changed = snd_interval_refine(i, constrs_interval(constrs, k));
314 /* Set corresponding flag so that the caller gets it. */
315 trace_hw_interval_param(substream, k, 0, &old_interval, i);
316 params->cmask |= 1 << k;
322 static int constrain_params_by_rules(struct snd_pcm_substream *substream,
323 struct snd_pcm_hw_params *params)
325 struct snd_pcm_hw_constraints *constrs =
326 &substream->runtime->hw_constraints;
328 unsigned int rstamps[constrs->rules_num];
329 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
331 struct snd_pcm_hw_rule *r;
333 struct snd_mask old_mask;
334 struct snd_interval old_interval;
339 * Each application of rule has own sequence number.
341 * Each member of 'rstamps' array represents the sequence number of
342 * recent application of corresponding rule.
344 for (k = 0; k < constrs->rules_num; k++)
348 * Each member of 'vstamps' array represents the sequence number of
349 * recent application of rule in which corresponding parameters were
352 * In initial state, elements corresponding to parameters requested by
353 * a caller is 1. For unrequested parameters, corresponding members
354 * have 0 so that the parameters are never changed anymore.
356 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
357 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
359 /* Due to the above design, actual sequence number starts at 2. */
362 /* Apply all rules in order. */
364 for (k = 0; k < constrs->rules_num; k++) {
365 r = &constrs->rules[k];
368 * Check condition bits of this rule. When the rule has
369 * some condition bits, parameter without the bits is
370 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
371 * is an example of the condition bits.
373 if (r->cond && !(r->cond & params->flags))
377 * The 'deps' array includes maximum three dependencies
378 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
379 * member of this array is a sentinel and should be
382 * This rule should be processed in this time when dependent
383 * parameters were changed at former applications of the other
386 for (d = 0; r->deps[d] >= 0; d++) {
387 if (vstamps[r->deps[d]] > rstamps[k])
393 if (trace_hw_mask_param_enabled()) {
394 if (hw_is_mask(r->var))
395 old_mask = *hw_param_mask(params, r->var);
397 if (trace_hw_interval_param_enabled()) {
398 if (hw_is_interval(r->var))
399 old_interval = *hw_param_interval(params, r->var);
402 changed = r->func(params, r);
407 * When the parameter is changed, notify it to the caller
408 * by corresponding returned bit, then preparing for next
411 if (changed && r->var >= 0) {
412 if (hw_is_mask(r->var)) {
413 trace_hw_mask_param(substream, r->var,
415 hw_param_mask(params, r->var));
417 if (hw_is_interval(r->var)) {
418 trace_hw_interval_param(substream, r->var,
419 k + 1, &old_interval,
420 hw_param_interval(params, r->var));
423 params->cmask |= (1 << r->var);
424 vstamps[r->var] = stamp;
428 rstamps[k] = stamp++;
431 /* Iterate to evaluate all rules till no parameters are changed. */
438 static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
439 struct snd_pcm_hw_params *params)
441 const struct snd_interval *i;
442 const struct snd_mask *m;
445 if (!params->msbits) {
446 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
447 if (snd_interval_single(i))
448 params->msbits = snd_interval_value(i);
451 if (!params->rate_den) {
452 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
453 if (snd_interval_single(i)) {
454 params->rate_num = snd_interval_value(i);
455 params->rate_den = 1;
459 if (!params->fifo_size) {
460 m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
461 i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
462 if (snd_mask_single(m) && snd_interval_single(i)) {
463 err = substream->ops->ioctl(substream,
464 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
471 params->info = substream->runtime->hw.info;
472 params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
473 SNDRV_PCM_INFO_DRAIN_TRIGGER);
474 if (!hw_support_mmap(substream))
475 params->info &= ~(SNDRV_PCM_INFO_MMAP |
476 SNDRV_PCM_INFO_MMAP_VALID);
482 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
483 struct snd_pcm_hw_params *params)
488 params->fifo_size = 0;
489 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
491 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
492 params->rate_num = 0;
493 params->rate_den = 0;
496 err = constrain_mask_params(substream, params);
500 err = constrain_interval_params(substream, params);
504 err = constrain_params_by_rules(substream, params);
512 EXPORT_SYMBOL(snd_pcm_hw_refine);
514 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
515 struct snd_pcm_hw_params __user * _params)
517 struct snd_pcm_hw_params *params;
520 params = memdup_user(_params, sizeof(*params));
522 return PTR_ERR(params);
524 err = snd_pcm_hw_refine(substream, params);
528 err = fixup_unreferenced_params(substream, params);
532 if (copy_to_user(_params, params, sizeof(*params)))
539 static int period_to_usecs(struct snd_pcm_runtime *runtime)
544 return -1; /* invalid */
546 /* take 75% of period time as the deadline */
547 usecs = (750000 / runtime->rate) * runtime->period_size;
548 usecs += ((750000 % runtime->rate) * runtime->period_size) /
554 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
556 snd_pcm_stream_lock_irq(substream);
557 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
558 substream->runtime->status->state = state;
559 snd_pcm_stream_unlock_irq(substream);
562 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
565 #ifdef CONFIG_SND_PCM_TIMER
566 if (substream->timer)
567 snd_timer_notify(substream->timer, event,
568 &substream->runtime->trigger_tstamp);
573 * snd_pcm_hw_param_choose - choose a configuration defined by @params
575 * @params: the hw_params instance
577 * Choose one configuration from configuration space defined by @params.
578 * The configuration chosen is that obtained fixing in this order:
579 * first access, first format, first subformat, min channels,
580 * min rate, min period time, max buffer size, min tick time
582 * Return: Zero if successful, or a negative error code on failure.
584 static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
585 struct snd_pcm_hw_params *params)
587 static const int vars[] = {
588 SNDRV_PCM_HW_PARAM_ACCESS,
589 SNDRV_PCM_HW_PARAM_FORMAT,
590 SNDRV_PCM_HW_PARAM_SUBFORMAT,
591 SNDRV_PCM_HW_PARAM_CHANNELS,
592 SNDRV_PCM_HW_PARAM_RATE,
593 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
594 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
595 SNDRV_PCM_HW_PARAM_TICK_TIME,
599 struct snd_mask old_mask;
600 struct snd_interval old_interval;
603 for (v = vars; *v != -1; v++) {
604 /* Keep old parameter to trace. */
605 if (trace_hw_mask_param_enabled()) {
607 old_mask = *hw_param_mask(params, *v);
609 if (trace_hw_interval_param_enabled()) {
610 if (hw_is_interval(*v))
611 old_interval = *hw_param_interval(params, *v);
613 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
614 changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
616 changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
617 if (snd_BUG_ON(changed < 0))
622 /* Trace the changed parameter. */
623 if (hw_is_mask(*v)) {
624 trace_hw_mask_param(pcm, *v, 0, &old_mask,
625 hw_param_mask(params, *v));
627 if (hw_is_interval(*v)) {
628 trace_hw_interval_param(pcm, *v, 0, &old_interval,
629 hw_param_interval(params, *v));
636 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
637 struct snd_pcm_hw_params *params)
639 struct snd_pcm_runtime *runtime;
642 snd_pcm_uframes_t frames;
644 if (PCM_RUNTIME_CHECK(substream))
646 runtime = substream->runtime;
647 snd_pcm_stream_lock_irq(substream);
648 switch (runtime->status->state) {
649 case SNDRV_PCM_STATE_OPEN:
650 case SNDRV_PCM_STATE_SETUP:
651 case SNDRV_PCM_STATE_PREPARED:
654 snd_pcm_stream_unlock_irq(substream);
657 snd_pcm_stream_unlock_irq(substream);
658 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
659 if (!substream->oss.oss)
661 if (atomic_read(&substream->mmap_count))
665 err = snd_pcm_hw_refine(substream, params);
669 err = snd_pcm_hw_params_choose(substream, params);
673 err = fixup_unreferenced_params(substream, params);
677 if (substream->ops->hw_params != NULL) {
678 err = substream->ops->hw_params(substream, params);
683 runtime->access = params_access(params);
684 runtime->format = params_format(params);
685 runtime->subformat = params_subformat(params);
686 runtime->channels = params_channels(params);
687 runtime->rate = params_rate(params);
688 runtime->period_size = params_period_size(params);
689 runtime->periods = params_periods(params);
690 runtime->buffer_size = params_buffer_size(params);
691 runtime->info = params->info;
692 runtime->rate_num = params->rate_num;
693 runtime->rate_den = params->rate_den;
694 runtime->no_period_wakeup =
695 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
696 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
698 bits = snd_pcm_format_physical_width(runtime->format);
699 runtime->sample_bits = bits;
700 bits *= runtime->channels;
701 runtime->frame_bits = bits;
703 while (bits % 8 != 0) {
707 runtime->byte_align = bits / 8;
708 runtime->min_align = frames;
710 /* Default sw params */
711 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
712 runtime->period_step = 1;
713 runtime->control->avail_min = runtime->period_size;
714 runtime->start_threshold = 1;
715 runtime->stop_threshold = runtime->buffer_size;
716 runtime->silence_threshold = 0;
717 runtime->silence_size = 0;
718 runtime->boundary = runtime->buffer_size;
719 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
720 runtime->boundary *= 2;
722 snd_pcm_timer_resolution_change(substream);
723 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
725 if (pm_qos_request_active(&substream->latency_pm_qos_req))
726 pm_qos_remove_request(&substream->latency_pm_qos_req);
727 if ((usecs = period_to_usecs(runtime)) >= 0)
728 pm_qos_add_request(&substream->latency_pm_qos_req,
729 PM_QOS_CPU_DMA_LATENCY, usecs);
732 /* hardware might be unusable from this time,
733 so we force application to retry to set
734 the correct hardware parameter settings */
735 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
736 if (substream->ops->hw_free != NULL)
737 substream->ops->hw_free(substream);
741 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
742 struct snd_pcm_hw_params __user * _params)
744 struct snd_pcm_hw_params *params;
747 params = memdup_user(_params, sizeof(*params));
749 return PTR_ERR(params);
751 err = snd_pcm_hw_params(substream, params);
755 if (copy_to_user(_params, params, sizeof(*params)))
762 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
764 struct snd_pcm_runtime *runtime;
767 if (PCM_RUNTIME_CHECK(substream))
769 runtime = substream->runtime;
770 snd_pcm_stream_lock_irq(substream);
771 switch (runtime->status->state) {
772 case SNDRV_PCM_STATE_SETUP:
773 case SNDRV_PCM_STATE_PREPARED:
776 snd_pcm_stream_unlock_irq(substream);
779 snd_pcm_stream_unlock_irq(substream);
780 if (atomic_read(&substream->mmap_count))
782 if (substream->ops->hw_free)
783 result = substream->ops->hw_free(substream);
784 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
785 pm_qos_remove_request(&substream->latency_pm_qos_req);
789 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
790 struct snd_pcm_sw_params *params)
792 struct snd_pcm_runtime *runtime;
795 if (PCM_RUNTIME_CHECK(substream))
797 runtime = substream->runtime;
798 snd_pcm_stream_lock_irq(substream);
799 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
800 snd_pcm_stream_unlock_irq(substream);
803 snd_pcm_stream_unlock_irq(substream);
805 if (params->tstamp_mode < 0 ||
806 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
808 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
809 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
811 if (params->avail_min == 0)
813 if (params->silence_size >= runtime->boundary) {
814 if (params->silence_threshold != 0)
817 if (params->silence_size > params->silence_threshold)
819 if (params->silence_threshold > runtime->buffer_size)
823 snd_pcm_stream_lock_irq(substream);
824 runtime->tstamp_mode = params->tstamp_mode;
825 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
826 runtime->tstamp_type = params->tstamp_type;
827 runtime->period_step = params->period_step;
828 runtime->control->avail_min = params->avail_min;
829 runtime->start_threshold = params->start_threshold;
830 runtime->stop_threshold = params->stop_threshold;
831 runtime->silence_threshold = params->silence_threshold;
832 runtime->silence_size = params->silence_size;
833 params->boundary = runtime->boundary;
834 if (snd_pcm_running(substream)) {
835 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
836 runtime->silence_size > 0)
837 snd_pcm_playback_silence(substream, ULONG_MAX);
838 err = snd_pcm_update_state(substream, runtime);
840 snd_pcm_stream_unlock_irq(substream);
844 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
845 struct snd_pcm_sw_params __user * _params)
847 struct snd_pcm_sw_params params;
849 if (copy_from_user(¶ms, _params, sizeof(params)))
851 err = snd_pcm_sw_params(substream, ¶ms);
852 if (copy_to_user(_params, ¶ms, sizeof(params)))
857 int snd_pcm_status(struct snd_pcm_substream *substream,
858 struct snd_pcm_status *status)
860 struct snd_pcm_runtime *runtime = substream->runtime;
862 snd_pcm_stream_lock_irq(substream);
864 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
865 &runtime->audio_tstamp_config);
867 /* backwards compatible behavior */
868 if (runtime->audio_tstamp_config.type_requested ==
869 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
870 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
871 runtime->audio_tstamp_config.type_requested =
872 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
874 runtime->audio_tstamp_config.type_requested =
875 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
876 runtime->audio_tstamp_report.valid = 0;
878 runtime->audio_tstamp_report.valid = 1;
880 status->state = runtime->status->state;
881 status->suspended_state = runtime->status->suspended_state;
882 if (status->state == SNDRV_PCM_STATE_OPEN)
884 status->trigger_tstamp = runtime->trigger_tstamp;
885 if (snd_pcm_running(substream)) {
886 snd_pcm_update_hw_ptr(substream);
887 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
888 status->tstamp = runtime->status->tstamp;
889 status->driver_tstamp = runtime->driver_tstamp;
890 status->audio_tstamp =
891 runtime->status->audio_tstamp;
892 if (runtime->audio_tstamp_report.valid == 1)
893 /* backwards compatibility, no report provided in COMPAT mode */
894 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
895 &status->audio_tstamp_accuracy,
896 &runtime->audio_tstamp_report);
901 /* get tstamp only in fallback mode and only if enabled */
902 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
903 snd_pcm_gettime(runtime, &status->tstamp);
906 status->appl_ptr = runtime->control->appl_ptr;
907 status->hw_ptr = runtime->status->hw_ptr;
908 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
909 status->avail = snd_pcm_playback_avail(runtime);
910 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
911 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
912 status->delay = runtime->buffer_size - status->avail;
913 status->delay += runtime->delay;
917 status->avail = snd_pcm_capture_avail(runtime);
918 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
919 status->delay = status->avail + runtime->delay;
923 status->avail_max = runtime->avail_max;
924 status->overrange = runtime->overrange;
925 runtime->avail_max = 0;
926 runtime->overrange = 0;
928 snd_pcm_stream_unlock_irq(substream);
932 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
933 struct snd_pcm_status __user * _status,
936 struct snd_pcm_status status;
939 memset(&status, 0, sizeof(status));
941 * with extension, parameters are read/write,
942 * get audio_tstamp_data from user,
943 * ignore rest of status structure
945 if (ext && get_user(status.audio_tstamp_data,
946 (u32 __user *)(&_status->audio_tstamp_data)))
948 res = snd_pcm_status(substream, &status);
951 if (copy_to_user(_status, &status, sizeof(status)))
956 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
957 struct snd_pcm_channel_info * info)
959 struct snd_pcm_runtime *runtime;
960 unsigned int channel;
962 channel = info->channel;
963 runtime = substream->runtime;
964 snd_pcm_stream_lock_irq(substream);
965 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
966 snd_pcm_stream_unlock_irq(substream);
969 snd_pcm_stream_unlock_irq(substream);
970 if (channel >= runtime->channels)
972 memset(info, 0, sizeof(*info));
973 info->channel = channel;
974 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
977 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
978 struct snd_pcm_channel_info __user * _info)
980 struct snd_pcm_channel_info info;
983 if (copy_from_user(&info, _info, sizeof(info)))
985 res = snd_pcm_channel_info(substream, &info);
988 if (copy_to_user(_info, &info, sizeof(info)))
993 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
995 struct snd_pcm_runtime *runtime = substream->runtime;
996 if (runtime->trigger_master == NULL)
998 if (runtime->trigger_master == substream) {
999 if (!runtime->trigger_tstamp_latched)
1000 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1002 snd_pcm_trigger_tstamp(runtime->trigger_master);
1003 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1005 runtime->trigger_master = NULL;
1009 int (*pre_action)(struct snd_pcm_substream *substream, int state);
1010 int (*do_action)(struct snd_pcm_substream *substream, int state);
1011 void (*undo_action)(struct snd_pcm_substream *substream, int state);
1012 void (*post_action)(struct snd_pcm_substream *substream, int state);
1016 * this functions is core for handling of linked stream
1017 * Note: the stream state might be changed also on failure
1018 * Note2: call with calling stream lock + link lock
1020 static int snd_pcm_action_group(const struct action_ops *ops,
1021 struct snd_pcm_substream *substream,
1022 int state, int do_lock)
1024 struct snd_pcm_substream *s = NULL;
1025 struct snd_pcm_substream *s1;
1026 int res = 0, depth = 1;
1028 snd_pcm_group_for_each_entry(s, substream) {
1029 if (do_lock && s != substream) {
1030 if (s->pcm->nonatomic)
1031 mutex_lock_nested(&s->self_group.mutex, depth);
1033 spin_lock_nested(&s->self_group.lock, depth);
1036 res = ops->pre_action(s, state);
1040 snd_pcm_group_for_each_entry(s, substream) {
1041 res = ops->do_action(s, state);
1043 if (ops->undo_action) {
1044 snd_pcm_group_for_each_entry(s1, substream) {
1045 if (s1 == s) /* failed stream */
1047 ops->undo_action(s1, state);
1050 s = NULL; /* unlock all */
1054 snd_pcm_group_for_each_entry(s, substream) {
1055 ops->post_action(s, state);
1059 /* unlock streams */
1060 snd_pcm_group_for_each_entry(s1, substream) {
1061 if (s1 != substream) {
1062 if (s1->pcm->nonatomic)
1063 mutex_unlock(&s1->self_group.mutex);
1065 spin_unlock(&s1->self_group.lock);
1067 if (s1 == s) /* end */
1075 * Note: call with stream lock
1077 static int snd_pcm_action_single(const struct action_ops *ops,
1078 struct snd_pcm_substream *substream,
1083 res = ops->pre_action(substream, state);
1086 res = ops->do_action(substream, state);
1088 ops->post_action(substream, state);
1089 else if (ops->undo_action)
1090 ops->undo_action(substream, state);
1095 * Note: call with stream lock
1097 static int snd_pcm_action(const struct action_ops *ops,
1098 struct snd_pcm_substream *substream,
1103 if (!snd_pcm_stream_linked(substream))
1104 return snd_pcm_action_single(ops, substream, state);
1106 if (substream->pcm->nonatomic) {
1107 if (!mutex_trylock(&substream->group->mutex)) {
1108 mutex_unlock(&substream->self_group.mutex);
1109 mutex_lock(&substream->group->mutex);
1110 mutex_lock(&substream->self_group.mutex);
1112 res = snd_pcm_action_group(ops, substream, state, 1);
1113 mutex_unlock(&substream->group->mutex);
1115 if (!spin_trylock(&substream->group->lock)) {
1116 spin_unlock(&substream->self_group.lock);
1117 spin_lock(&substream->group->lock);
1118 spin_lock(&substream->self_group.lock);
1120 res = snd_pcm_action_group(ops, substream, state, 1);
1121 spin_unlock(&substream->group->lock);
1127 * Note: don't use any locks before
1129 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1130 struct snd_pcm_substream *substream,
1135 snd_pcm_stream_lock_irq(substream);
1136 res = snd_pcm_action(ops, substream, state);
1137 snd_pcm_stream_unlock_irq(substream);
1143 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1144 struct snd_pcm_substream *substream,
1149 down_read(&snd_pcm_link_rwsem);
1150 if (snd_pcm_stream_linked(substream))
1151 res = snd_pcm_action_group(ops, substream, state, 0);
1153 res = snd_pcm_action_single(ops, substream, state);
1154 up_read(&snd_pcm_link_rwsem);
1161 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1163 struct snd_pcm_runtime *runtime = substream->runtime;
1164 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1166 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1167 !snd_pcm_playback_data(substream))
1169 runtime->trigger_tstamp_latched = false;
1170 runtime->trigger_master = substream;
1174 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1176 if (substream->runtime->trigger_master != substream)
1178 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1181 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1183 if (substream->runtime->trigger_master == substream)
1184 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1187 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1189 struct snd_pcm_runtime *runtime = substream->runtime;
1190 snd_pcm_trigger_tstamp(substream);
1191 runtime->hw_ptr_jiffies = jiffies;
1192 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1194 runtime->status->state = state;
1195 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1196 runtime->silence_size > 0)
1197 snd_pcm_playback_silence(substream, ULONG_MAX);
1198 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1201 static const struct action_ops snd_pcm_action_start = {
1202 .pre_action = snd_pcm_pre_start,
1203 .do_action = snd_pcm_do_start,
1204 .undo_action = snd_pcm_undo_start,
1205 .post_action = snd_pcm_post_start
1209 * snd_pcm_start - start all linked streams
1210 * @substream: the PCM substream instance
1212 * Return: Zero if successful, or a negative error code.
1213 * The stream lock must be acquired before calling this function.
1215 int snd_pcm_start(struct snd_pcm_substream *substream)
1217 return snd_pcm_action(&snd_pcm_action_start, substream,
1218 SNDRV_PCM_STATE_RUNNING);
1221 /* take the stream lock and start the streams */
1222 static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1224 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1225 SNDRV_PCM_STATE_RUNNING);
1231 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1236 runtime->trigger_master = substream;
1240 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1242 if (substream->runtime->trigger_master == substream &&
1243 snd_pcm_running(substream))
1244 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1245 return 0; /* unconditonally stop all substreams */
1248 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1250 struct snd_pcm_runtime *runtime = substream->runtime;
1251 if (runtime->status->state != state) {
1252 snd_pcm_trigger_tstamp(substream);
1253 runtime->status->state = state;
1254 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1256 wake_up(&runtime->sleep);
1257 wake_up(&runtime->tsleep);
1260 static const struct action_ops snd_pcm_action_stop = {
1261 .pre_action = snd_pcm_pre_stop,
1262 .do_action = snd_pcm_do_stop,
1263 .post_action = snd_pcm_post_stop
1267 * snd_pcm_stop - try to stop all running streams in the substream group
1268 * @substream: the PCM substream instance
1269 * @state: PCM state after stopping the stream
1271 * The state of each stream is then changed to the given state unconditionally.
1273 * Return: Zero if successful, or a negative error code.
1275 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1277 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1279 EXPORT_SYMBOL(snd_pcm_stop);
1282 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1283 * @substream: the PCM substream
1285 * After stopping, the state is changed to SETUP.
1286 * Unlike snd_pcm_stop(), this affects only the given stream.
1288 * Return: Zero if succesful, or a negative error code.
1290 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1292 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1293 SNDRV_PCM_STATE_SETUP);
1297 * snd_pcm_stop_xrun - stop the running streams as XRUN
1298 * @substream: the PCM substream instance
1300 * This stops the given running substream (and all linked substreams) as XRUN.
1301 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1303 * Return: Zero if successful, or a negative error code.
1305 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1307 unsigned long flags;
1310 snd_pcm_stream_lock_irqsave(substream, flags);
1311 if (snd_pcm_running(substream))
1312 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1313 snd_pcm_stream_unlock_irqrestore(substream, flags);
1316 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1321 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1323 struct snd_pcm_runtime *runtime = substream->runtime;
1324 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1327 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1329 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1331 runtime->trigger_master = substream;
1335 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1337 if (substream->runtime->trigger_master != substream)
1339 /* some drivers might use hw_ptr to recover from the pause -
1340 update the hw_ptr now */
1342 snd_pcm_update_hw_ptr(substream);
1343 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1344 * a delta between the current jiffies, this gives a large enough
1345 * delta, effectively to skip the check once.
1347 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1348 return substream->ops->trigger(substream,
1349 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1350 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1353 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1355 if (substream->runtime->trigger_master == substream)
1356 substream->ops->trigger(substream,
1357 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1358 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1361 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1363 struct snd_pcm_runtime *runtime = substream->runtime;
1364 snd_pcm_trigger_tstamp(substream);
1366 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1367 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1368 wake_up(&runtime->sleep);
1369 wake_up(&runtime->tsleep);
1371 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1372 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1376 static const struct action_ops snd_pcm_action_pause = {
1377 .pre_action = snd_pcm_pre_pause,
1378 .do_action = snd_pcm_do_pause,
1379 .undo_action = snd_pcm_undo_pause,
1380 .post_action = snd_pcm_post_pause
1384 * Push/release the pause for all linked streams.
1386 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1388 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1394 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1396 struct snd_pcm_runtime *runtime = substream->runtime;
1397 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1399 runtime->trigger_master = substream;
1403 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1405 struct snd_pcm_runtime *runtime = substream->runtime;
1406 if (runtime->trigger_master != substream)
1408 if (! snd_pcm_running(substream))
1410 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1411 return 0; /* suspend unconditionally */
1414 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1416 struct snd_pcm_runtime *runtime = substream->runtime;
1417 snd_pcm_trigger_tstamp(substream);
1418 runtime->status->suspended_state = runtime->status->state;
1419 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1420 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1421 wake_up(&runtime->sleep);
1422 wake_up(&runtime->tsleep);
1425 static const struct action_ops snd_pcm_action_suspend = {
1426 .pre_action = snd_pcm_pre_suspend,
1427 .do_action = snd_pcm_do_suspend,
1428 .post_action = snd_pcm_post_suspend
1432 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1433 * @substream: the PCM substream
1435 * After this call, all streams are changed to SUSPENDED state.
1437 * Return: Zero if successful (or @substream is %NULL), or a negative error
1440 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1443 unsigned long flags;
1448 snd_pcm_stream_lock_irqsave(substream, flags);
1449 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1450 snd_pcm_stream_unlock_irqrestore(substream, flags);
1453 EXPORT_SYMBOL(snd_pcm_suspend);
1456 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1457 * @pcm: the PCM instance
1459 * After this call, all streams are changed to SUSPENDED state.
1461 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1463 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1465 struct snd_pcm_substream *substream;
1466 int stream, err = 0;
1471 for (stream = 0; stream < 2; stream++) {
1472 for (substream = pcm->streams[stream].substream;
1473 substream; substream = substream->next) {
1474 /* FIXME: the open/close code should lock this as well */
1475 if (substream->runtime == NULL)
1477 err = snd_pcm_suspend(substream);
1478 if (err < 0 && err != -EBUSY)
1484 EXPORT_SYMBOL(snd_pcm_suspend_all);
1488 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1491 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1493 runtime->trigger_master = substream;
1497 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1499 struct snd_pcm_runtime *runtime = substream->runtime;
1500 if (runtime->trigger_master != substream)
1502 /* DMA not running previously? */
1503 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1504 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1505 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1507 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1510 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1512 if (substream->runtime->trigger_master == substream &&
1513 snd_pcm_running(substream))
1514 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1517 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1519 struct snd_pcm_runtime *runtime = substream->runtime;
1520 snd_pcm_trigger_tstamp(substream);
1521 runtime->status->state = runtime->status->suspended_state;
1522 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1525 static const struct action_ops snd_pcm_action_resume = {
1526 .pre_action = snd_pcm_pre_resume,
1527 .do_action = snd_pcm_do_resume,
1528 .undo_action = snd_pcm_undo_resume,
1529 .post_action = snd_pcm_post_resume
1532 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1534 return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1539 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1544 #endif /* CONFIG_PM */
1549 * Change the RUNNING stream(s) to XRUN state.
1551 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1553 struct snd_pcm_runtime *runtime = substream->runtime;
1556 snd_pcm_stream_lock_irq(substream);
1557 switch (runtime->status->state) {
1558 case SNDRV_PCM_STATE_XRUN:
1559 result = 0; /* already there */
1561 case SNDRV_PCM_STATE_RUNNING:
1562 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1567 snd_pcm_stream_unlock_irq(substream);
1574 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1576 struct snd_pcm_runtime *runtime = substream->runtime;
1577 switch (runtime->status->state) {
1578 case SNDRV_PCM_STATE_RUNNING:
1579 case SNDRV_PCM_STATE_PREPARED:
1580 case SNDRV_PCM_STATE_PAUSED:
1581 case SNDRV_PCM_STATE_SUSPENDED:
1588 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1590 struct snd_pcm_runtime *runtime = substream->runtime;
1591 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1594 runtime->hw_ptr_base = 0;
1595 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1596 runtime->status->hw_ptr % runtime->period_size;
1597 runtime->silence_start = runtime->status->hw_ptr;
1598 runtime->silence_filled = 0;
1602 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1604 struct snd_pcm_runtime *runtime = substream->runtime;
1605 runtime->control->appl_ptr = runtime->status->hw_ptr;
1606 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1607 runtime->silence_size > 0)
1608 snd_pcm_playback_silence(substream, ULONG_MAX);
1611 static const struct action_ops snd_pcm_action_reset = {
1612 .pre_action = snd_pcm_pre_reset,
1613 .do_action = snd_pcm_do_reset,
1614 .post_action = snd_pcm_post_reset
1617 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1619 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1625 /* we use the second argument for updating f_flags */
1626 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1629 struct snd_pcm_runtime *runtime = substream->runtime;
1630 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1631 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1633 if (snd_pcm_running(substream))
1635 substream->f_flags = f_flags;
1639 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1642 err = substream->ops->prepare(substream);
1645 return snd_pcm_do_reset(substream, 0);
1648 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1650 struct snd_pcm_runtime *runtime = substream->runtime;
1651 runtime->control->appl_ptr = runtime->status->hw_ptr;
1652 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1655 static const struct action_ops snd_pcm_action_prepare = {
1656 .pre_action = snd_pcm_pre_prepare,
1657 .do_action = snd_pcm_do_prepare,
1658 .post_action = snd_pcm_post_prepare
1662 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1663 * @substream: the PCM substream instance
1664 * @file: file to refer f_flags
1666 * Return: Zero if successful, or a negative error code.
1668 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1674 f_flags = file->f_flags;
1676 f_flags = substream->f_flags;
1678 snd_pcm_stream_lock_irq(substream);
1679 switch (substream->runtime->status->state) {
1680 case SNDRV_PCM_STATE_PAUSED:
1681 snd_pcm_pause(substream, 0);
1683 case SNDRV_PCM_STATE_SUSPENDED:
1684 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1687 snd_pcm_stream_unlock_irq(substream);
1689 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1690 substream, f_flags);
1697 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1699 struct snd_pcm_runtime *runtime = substream->runtime;
1700 switch (runtime->status->state) {
1701 case SNDRV_PCM_STATE_OPEN:
1702 case SNDRV_PCM_STATE_DISCONNECTED:
1703 case SNDRV_PCM_STATE_SUSPENDED:
1706 runtime->trigger_master = substream;
1710 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1712 struct snd_pcm_runtime *runtime = substream->runtime;
1713 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1714 switch (runtime->status->state) {
1715 case SNDRV_PCM_STATE_PREPARED:
1716 /* start playback stream if possible */
1717 if (! snd_pcm_playback_empty(substream)) {
1718 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1719 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1721 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1724 case SNDRV_PCM_STATE_RUNNING:
1725 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1727 case SNDRV_PCM_STATE_XRUN:
1728 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1734 /* stop running stream */
1735 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1736 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1737 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1738 snd_pcm_do_stop(substream, new_state);
1739 snd_pcm_post_stop(substream, new_state);
1743 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1744 runtime->trigger_master == substream &&
1745 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1746 return substream->ops->trigger(substream,
1747 SNDRV_PCM_TRIGGER_DRAIN);
1752 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1756 static const struct action_ops snd_pcm_action_drain_init = {
1757 .pre_action = snd_pcm_pre_drain_init,
1758 .do_action = snd_pcm_do_drain_init,
1759 .post_action = snd_pcm_post_drain_init
1762 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1765 * Drain the stream(s).
1766 * When the substream is linked, sync until the draining of all playback streams
1768 * After this call, all streams are supposed to be either SETUP or DRAINING
1769 * (capture only) state.
1771 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1774 struct snd_card *card;
1775 struct snd_pcm_runtime *runtime;
1776 struct snd_pcm_substream *s;
1777 wait_queue_entry_t wait;
1781 card = substream->pcm->card;
1782 runtime = substream->runtime;
1784 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1788 if (file->f_flags & O_NONBLOCK)
1790 } else if (substream->f_flags & O_NONBLOCK)
1793 down_read(&snd_pcm_link_rwsem);
1794 snd_pcm_stream_lock_irq(substream);
1796 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1797 snd_pcm_pause(substream, 0);
1799 /* pre-start/stop - all running streams are changed to DRAINING state */
1800 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1803 /* in non-blocking, we don't wait in ioctl but let caller poll */
1811 struct snd_pcm_runtime *to_check;
1812 if (signal_pending(current)) {
1813 result = -ERESTARTSYS;
1816 /* find a substream to drain */
1818 snd_pcm_group_for_each_entry(s, substream) {
1819 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1821 runtime = s->runtime;
1822 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1828 break; /* all drained */
1829 init_waitqueue_entry(&wait, current);
1830 add_wait_queue(&to_check->sleep, &wait);
1831 snd_pcm_stream_unlock_irq(substream);
1832 up_read(&snd_pcm_link_rwsem);
1833 snd_power_unlock(card);
1834 if (runtime->no_period_wakeup)
1835 tout = MAX_SCHEDULE_TIMEOUT;
1838 if (runtime->rate) {
1839 long t = runtime->period_size * 2 / runtime->rate;
1840 tout = max(t, tout);
1842 tout = msecs_to_jiffies(tout * 1000);
1844 tout = schedule_timeout_interruptible(tout);
1845 snd_power_lock(card);
1846 down_read(&snd_pcm_link_rwsem);
1847 snd_pcm_stream_lock_irq(substream);
1848 remove_wait_queue(&to_check->sleep, &wait);
1849 if (card->shutdown) {
1854 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1857 dev_dbg(substream->pcm->card->dev,
1858 "playback drain error (DMA or IRQ trouble?)\n");
1859 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1867 snd_pcm_stream_unlock_irq(substream);
1868 up_read(&snd_pcm_link_rwsem);
1876 * Immediately put all linked substreams into SETUP state.
1878 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1880 struct snd_pcm_runtime *runtime;
1883 if (PCM_RUNTIME_CHECK(substream))
1885 runtime = substream->runtime;
1887 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1888 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1891 snd_pcm_stream_lock_irq(substream);
1893 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1894 snd_pcm_pause(substream, 0);
1896 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1897 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1898 snd_pcm_stream_unlock_irq(substream);
1904 static bool is_pcm_file(struct file *file)
1906 struct inode *inode = file_inode(file);
1909 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1911 minor = iminor(inode);
1912 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1913 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1919 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1922 struct snd_pcm_file *pcm_file;
1923 struct snd_pcm_substream *substream1;
1924 struct snd_pcm_group *group;
1925 struct fd f = fdget(fd);
1929 if (!is_pcm_file(f.file)) {
1933 pcm_file = f.file->private_data;
1934 substream1 = pcm_file->substream;
1935 group = kmalloc(sizeof(*group), GFP_KERNEL);
1940 down_write_nonblock(&snd_pcm_link_rwsem);
1941 write_lock_irq(&snd_pcm_link_rwlock);
1942 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1943 substream->runtime->status->state != substream1->runtime->status->state ||
1944 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1948 if (snd_pcm_stream_linked(substream1)) {
1952 if (!snd_pcm_stream_linked(substream)) {
1953 substream->group = group;
1955 spin_lock_init(&substream->group->lock);
1956 mutex_init(&substream->group->mutex);
1957 INIT_LIST_HEAD(&substream->group->substreams);
1958 list_add_tail(&substream->link_list, &substream->group->substreams);
1959 substream->group->count = 1;
1961 list_add_tail(&substream1->link_list, &substream->group->substreams);
1962 substream->group->count++;
1963 substream1->group = substream->group;
1965 write_unlock_irq(&snd_pcm_link_rwlock);
1966 up_write(&snd_pcm_link_rwsem);
1968 snd_card_unref(substream1->pcm->card);
1975 static void relink_to_local(struct snd_pcm_substream *substream)
1977 substream->group = &substream->self_group;
1978 INIT_LIST_HEAD(&substream->self_group.substreams);
1979 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1982 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1984 struct snd_pcm_substream *s;
1987 down_write_nonblock(&snd_pcm_link_rwsem);
1988 write_lock_irq(&snd_pcm_link_rwlock);
1989 if (!snd_pcm_stream_linked(substream)) {
1993 list_del(&substream->link_list);
1994 substream->group->count--;
1995 if (substream->group->count == 1) { /* detach the last stream, too */
1996 snd_pcm_group_for_each_entry(s, substream) {
2000 kfree(substream->group);
2002 relink_to_local(substream);
2004 write_unlock_irq(&snd_pcm_link_rwlock);
2005 up_write(&snd_pcm_link_rwsem);
2012 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2013 struct snd_pcm_hw_rule *rule)
2015 struct snd_interval t;
2016 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2017 hw_param_interval_c(params, rule->deps[1]), &t);
2018 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2021 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2022 struct snd_pcm_hw_rule *rule)
2024 struct snd_interval t;
2025 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2026 hw_param_interval_c(params, rule->deps[1]), &t);
2027 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2030 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2031 struct snd_pcm_hw_rule *rule)
2033 struct snd_interval t;
2034 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2035 hw_param_interval_c(params, rule->deps[1]),
2036 (unsigned long) rule->private, &t);
2037 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2040 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2041 struct snd_pcm_hw_rule *rule)
2043 struct snd_interval t;
2044 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2045 (unsigned long) rule->private,
2046 hw_param_interval_c(params, rule->deps[1]), &t);
2047 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2050 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2051 struct snd_pcm_hw_rule *rule)
2054 const struct snd_interval *i =
2055 hw_param_interval_c(params, rule->deps[0]);
2057 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2059 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2061 if (! snd_mask_test(mask, k))
2063 bits = snd_pcm_format_physical_width(k);
2065 continue; /* ignore invalid formats */
2066 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2067 snd_mask_reset(&m, k);
2069 return snd_mask_refine(mask, &m);
2072 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2073 struct snd_pcm_hw_rule *rule)
2075 struct snd_interval t;
2081 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
2083 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2085 bits = snd_pcm_format_physical_width(k);
2087 continue; /* ignore invalid formats */
2088 if (t.min > (unsigned)bits)
2090 if (t.max < (unsigned)bits)
2094 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2097 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2098 #error "Change this table"
2101 static const unsigned int rates[] = {
2102 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2103 48000, 64000, 88200, 96000, 176400, 192000
2106 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2107 .count = ARRAY_SIZE(rates),
2111 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2112 struct snd_pcm_hw_rule *rule)
2114 struct snd_pcm_hardware *hw = rule->private;
2115 return snd_interval_list(hw_param_interval(params, rule->var),
2116 snd_pcm_known_rates.count,
2117 snd_pcm_known_rates.list, hw->rates);
2120 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2121 struct snd_pcm_hw_rule *rule)
2123 struct snd_interval t;
2124 struct snd_pcm_substream *substream = rule->private;
2126 t.max = substream->buffer_bytes_max;
2130 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2133 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2135 struct snd_pcm_runtime *runtime = substream->runtime;
2136 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2139 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2140 snd_mask_any(constrs_mask(constrs, k));
2143 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2144 snd_interval_any(constrs_interval(constrs, k));
2147 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2148 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2149 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2150 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2151 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2153 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2154 snd_pcm_hw_rule_format, NULL,
2155 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2158 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2159 snd_pcm_hw_rule_sample_bits, NULL,
2160 SNDRV_PCM_HW_PARAM_FORMAT,
2161 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2164 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2165 snd_pcm_hw_rule_div, NULL,
2166 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2169 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2170 snd_pcm_hw_rule_mul, NULL,
2171 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2174 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2175 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2176 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2179 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2180 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2181 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2184 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2185 snd_pcm_hw_rule_div, NULL,
2186 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2189 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2190 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2191 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2194 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2195 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2196 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2199 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2200 snd_pcm_hw_rule_div, NULL,
2201 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2204 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2205 snd_pcm_hw_rule_div, NULL,
2206 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2209 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2210 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2211 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2214 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2215 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2216 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2219 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2220 snd_pcm_hw_rule_mul, NULL,
2221 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2224 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2225 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2226 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2229 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2230 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2231 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2234 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2235 snd_pcm_hw_rule_muldivk, (void*) 8,
2236 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2239 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2240 snd_pcm_hw_rule_muldivk, (void*) 8,
2241 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2244 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2245 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2246 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2249 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2250 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2251 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2257 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2259 struct snd_pcm_runtime *runtime = substream->runtime;
2260 struct snd_pcm_hardware *hw = &runtime->hw;
2262 unsigned int mask = 0;
2264 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2265 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2266 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2267 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2268 if (hw_support_mmap(substream)) {
2269 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2270 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2271 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2272 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2273 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2274 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2276 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2280 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2284 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2288 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2289 hw->channels_min, hw->channels_max);
2293 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2294 hw->rate_min, hw->rate_max);
2298 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2299 hw->period_bytes_min, hw->period_bytes_max);
2303 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2304 hw->periods_min, hw->periods_max);
2308 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2309 hw->period_bytes_min, hw->buffer_bytes_max);
2313 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2314 snd_pcm_hw_rule_buffer_bytes_max, substream,
2315 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2320 if (runtime->dma_bytes) {
2321 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2326 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2327 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2328 snd_pcm_hw_rule_rate, hw,
2329 SNDRV_PCM_HW_PARAM_RATE, -1);
2334 /* FIXME: this belong to lowlevel */
2335 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2340 static void pcm_release_private(struct snd_pcm_substream *substream)
2342 snd_pcm_unlink(substream);
2345 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2347 substream->ref_count--;
2348 if (substream->ref_count > 0)
2351 snd_pcm_drop(substream);
2352 if (substream->hw_opened) {
2353 if (substream->ops->hw_free &&
2354 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2355 substream->ops->hw_free(substream);
2356 substream->ops->close(substream);
2357 substream->hw_opened = 0;
2359 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2360 pm_qos_remove_request(&substream->latency_pm_qos_req);
2361 if (substream->pcm_release) {
2362 substream->pcm_release(substream);
2363 substream->pcm_release = NULL;
2365 snd_pcm_detach_substream(substream);
2367 EXPORT_SYMBOL(snd_pcm_release_substream);
2369 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2371 struct snd_pcm_substream **rsubstream)
2373 struct snd_pcm_substream *substream;
2376 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2379 if (substream->ref_count > 1) {
2380 *rsubstream = substream;
2384 err = snd_pcm_hw_constraints_init(substream);
2386 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2390 if ((err = substream->ops->open(substream)) < 0)
2393 substream->hw_opened = 1;
2395 err = snd_pcm_hw_constraints_complete(substream);
2397 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2401 *rsubstream = substream;
2405 snd_pcm_release_substream(substream);
2408 EXPORT_SYMBOL(snd_pcm_open_substream);
2410 static int snd_pcm_open_file(struct file *file,
2411 struct snd_pcm *pcm,
2414 struct snd_pcm_file *pcm_file;
2415 struct snd_pcm_substream *substream;
2418 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2422 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2423 if (pcm_file == NULL) {
2424 snd_pcm_release_substream(substream);
2427 pcm_file->substream = substream;
2428 if (substream->ref_count == 1) {
2429 substream->file = pcm_file;
2430 substream->pcm_release = pcm_release_private;
2432 file->private_data = pcm_file;
2437 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2439 struct snd_pcm *pcm;
2440 int err = nonseekable_open(inode, file);
2443 pcm = snd_lookup_minor_data(iminor(inode),
2444 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2445 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2447 snd_card_unref(pcm->card);
2451 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2453 struct snd_pcm *pcm;
2454 int err = nonseekable_open(inode, file);
2457 pcm = snd_lookup_minor_data(iminor(inode),
2458 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2459 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2461 snd_card_unref(pcm->card);
2465 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2468 wait_queue_entry_t wait;
2474 err = snd_card_file_add(pcm->card, file);
2477 if (!try_module_get(pcm->card->module)) {
2481 init_waitqueue_entry(&wait, current);
2482 add_wait_queue(&pcm->open_wait, &wait);
2483 mutex_lock(&pcm->open_mutex);
2485 err = snd_pcm_open_file(file, pcm, stream);
2488 if (err == -EAGAIN) {
2489 if (file->f_flags & O_NONBLOCK) {
2495 set_current_state(TASK_INTERRUPTIBLE);
2496 mutex_unlock(&pcm->open_mutex);
2498 mutex_lock(&pcm->open_mutex);
2499 if (pcm->card->shutdown) {
2503 if (signal_pending(current)) {
2508 remove_wait_queue(&pcm->open_wait, &wait);
2509 mutex_unlock(&pcm->open_mutex);
2515 module_put(pcm->card->module);
2517 snd_card_file_remove(pcm->card, file);
2522 static int snd_pcm_release(struct inode *inode, struct file *file)
2524 struct snd_pcm *pcm;
2525 struct snd_pcm_substream *substream;
2526 struct snd_pcm_file *pcm_file;
2528 pcm_file = file->private_data;
2529 substream = pcm_file->substream;
2530 if (snd_BUG_ON(!substream))
2532 pcm = substream->pcm;
2533 mutex_lock(&pcm->open_mutex);
2534 snd_pcm_release_substream(substream);
2536 mutex_unlock(&pcm->open_mutex);
2537 wake_up(&pcm->open_wait);
2538 module_put(pcm->card->module);
2539 snd_card_file_remove(pcm->card, file);
2543 /* check and update PCM state; return 0 or a negative error
2544 * call this inside PCM lock
2546 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2548 switch (substream->runtime->status->state) {
2549 case SNDRV_PCM_STATE_DRAINING:
2550 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2553 case SNDRV_PCM_STATE_RUNNING:
2554 return snd_pcm_update_hw_ptr(substream);
2555 case SNDRV_PCM_STATE_PREPARED:
2556 case SNDRV_PCM_STATE_PAUSED:
2558 case SNDRV_PCM_STATE_SUSPENDED:
2560 case SNDRV_PCM_STATE_XRUN:
2567 /* increase the appl_ptr; returns the processed frames or a negative error */
2568 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2569 snd_pcm_uframes_t frames,
2570 snd_pcm_sframes_t avail)
2572 struct snd_pcm_runtime *runtime = substream->runtime;
2573 snd_pcm_sframes_t appl_ptr;
2578 if (frames > (snd_pcm_uframes_t)avail)
2580 appl_ptr = runtime->control->appl_ptr + frames;
2581 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2582 appl_ptr -= runtime->boundary;
2583 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2584 return ret < 0 ? ret : frames;
2587 /* decrease the appl_ptr; returns the processed frames or a negative error */
2588 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2589 snd_pcm_uframes_t frames,
2590 snd_pcm_sframes_t avail)
2592 struct snd_pcm_runtime *runtime = substream->runtime;
2593 snd_pcm_sframes_t appl_ptr;
2598 if (frames > (snd_pcm_uframes_t)avail)
2600 appl_ptr = runtime->control->appl_ptr - frames;
2602 appl_ptr += runtime->boundary;
2603 ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2604 return ret < 0 ? ret : frames;
2607 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2608 snd_pcm_uframes_t frames)
2610 struct snd_pcm_runtime *runtime = substream->runtime;
2611 snd_pcm_sframes_t ret;
2616 snd_pcm_stream_lock_irq(substream);
2617 ret = do_pcm_hwsync(substream);
2619 ret = rewind_appl_ptr(substream, frames,
2620 snd_pcm_playback_hw_avail(runtime));
2621 snd_pcm_stream_unlock_irq(substream);
2625 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2626 snd_pcm_uframes_t frames)
2628 struct snd_pcm_runtime *runtime = substream->runtime;
2629 snd_pcm_sframes_t ret;
2634 snd_pcm_stream_lock_irq(substream);
2635 ret = do_pcm_hwsync(substream);
2637 ret = rewind_appl_ptr(substream, frames,
2638 snd_pcm_capture_hw_avail(runtime));
2639 snd_pcm_stream_unlock_irq(substream);
2643 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2644 snd_pcm_uframes_t frames)
2646 struct snd_pcm_runtime *runtime = substream->runtime;
2647 snd_pcm_sframes_t ret;
2652 snd_pcm_stream_lock_irq(substream);
2653 ret = do_pcm_hwsync(substream);
2655 ret = forward_appl_ptr(substream, frames,
2656 snd_pcm_playback_avail(runtime));
2657 snd_pcm_stream_unlock_irq(substream);
2661 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2662 snd_pcm_uframes_t frames)
2664 struct snd_pcm_runtime *runtime = substream->runtime;
2665 snd_pcm_sframes_t ret;
2670 snd_pcm_stream_lock_irq(substream);
2671 ret = do_pcm_hwsync(substream);
2673 ret = forward_appl_ptr(substream, frames,
2674 snd_pcm_capture_avail(runtime));
2675 snd_pcm_stream_unlock_irq(substream);
2679 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2683 snd_pcm_stream_lock_irq(substream);
2684 err = do_pcm_hwsync(substream);
2685 snd_pcm_stream_unlock_irq(substream);
2689 static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
2691 struct snd_pcm_runtime *runtime = substream->runtime;
2693 snd_pcm_sframes_t n = 0;
2695 snd_pcm_stream_lock_irq(substream);
2696 err = do_pcm_hwsync(substream);
2698 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2699 n = snd_pcm_playback_hw_avail(runtime);
2701 n = snd_pcm_capture_avail(runtime);
2702 n += runtime->delay;
2704 snd_pcm_stream_unlock_irq(substream);
2705 return err < 0 ? err : n;
2708 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2709 struct snd_pcm_sync_ptr __user *_sync_ptr)
2711 struct snd_pcm_runtime *runtime = substream->runtime;
2712 struct snd_pcm_sync_ptr sync_ptr;
2713 volatile struct snd_pcm_mmap_status *status;
2714 volatile struct snd_pcm_mmap_control *control;
2717 memset(&sync_ptr, 0, sizeof(sync_ptr));
2718 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2720 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2722 status = runtime->status;
2723 control = runtime->control;
2724 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2725 err = snd_pcm_hwsync(substream);
2729 snd_pcm_stream_lock_irq(substream);
2730 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2731 err = pcm_lib_apply_appl_ptr(substream,
2732 sync_ptr.c.control.appl_ptr);
2734 snd_pcm_stream_unlock_irq(substream);
2738 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2740 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2741 control->avail_min = sync_ptr.c.control.avail_min;
2743 sync_ptr.c.control.avail_min = control->avail_min;
2744 sync_ptr.s.status.state = status->state;
2745 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2746 sync_ptr.s.status.tstamp = status->tstamp;
2747 sync_ptr.s.status.suspended_state = status->suspended_state;
2748 snd_pcm_stream_unlock_irq(substream);
2749 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2754 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2756 struct snd_pcm_runtime *runtime = substream->runtime;
2759 if (get_user(arg, _arg))
2761 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2763 runtime->tstamp_type = arg;
2767 static int snd_pcm_common_ioctl(struct file *file,
2768 struct snd_pcm_substream *substream,
2769 unsigned int cmd, void __user *arg)
2771 struct snd_pcm_file *pcm_file = file->private_data;
2774 case SNDRV_PCM_IOCTL_PVERSION:
2775 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2776 case SNDRV_PCM_IOCTL_INFO:
2777 return snd_pcm_info_user(substream, arg);
2778 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2780 case SNDRV_PCM_IOCTL_TTSTAMP:
2781 return snd_pcm_tstamp(substream, arg);
2782 case SNDRV_PCM_IOCTL_USER_PVERSION:
2783 if (get_user(pcm_file->user_pversion,
2784 (unsigned int __user *)arg))
2787 case SNDRV_PCM_IOCTL_HW_REFINE:
2788 return snd_pcm_hw_refine_user(substream, arg);
2789 case SNDRV_PCM_IOCTL_HW_PARAMS:
2790 return snd_pcm_hw_params_user(substream, arg);
2791 case SNDRV_PCM_IOCTL_HW_FREE:
2792 return snd_pcm_hw_free(substream);
2793 case SNDRV_PCM_IOCTL_SW_PARAMS:
2794 return snd_pcm_sw_params_user(substream, arg);
2795 case SNDRV_PCM_IOCTL_STATUS:
2796 return snd_pcm_status_user(substream, arg, false);
2797 case SNDRV_PCM_IOCTL_STATUS_EXT:
2798 return snd_pcm_status_user(substream, arg, true);
2799 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2800 return snd_pcm_channel_info_user(substream, arg);
2801 case SNDRV_PCM_IOCTL_PREPARE:
2802 return snd_pcm_prepare(substream, file);
2803 case SNDRV_PCM_IOCTL_RESET:
2804 return snd_pcm_reset(substream);
2805 case SNDRV_PCM_IOCTL_START:
2806 return snd_pcm_start_lock_irq(substream);
2807 case SNDRV_PCM_IOCTL_LINK:
2808 return snd_pcm_link(substream, (int)(unsigned long) arg);
2809 case SNDRV_PCM_IOCTL_UNLINK:
2810 return snd_pcm_unlink(substream);
2811 case SNDRV_PCM_IOCTL_RESUME:
2812 return snd_pcm_resume(substream);
2813 case SNDRV_PCM_IOCTL_XRUN:
2814 return snd_pcm_xrun(substream);
2815 case SNDRV_PCM_IOCTL_HWSYNC:
2816 return snd_pcm_hwsync(substream);
2817 case SNDRV_PCM_IOCTL_DELAY:
2819 snd_pcm_sframes_t delay = snd_pcm_delay(substream);
2820 snd_pcm_sframes_t __user *res = arg;
2824 if (put_user(delay, res))
2828 case SNDRV_PCM_IOCTL_SYNC_PTR:
2829 return snd_pcm_sync_ptr(substream, arg);
2830 #ifdef CONFIG_SND_SUPPORT_OLD_API
2831 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2832 return snd_pcm_hw_refine_old_user(substream, arg);
2833 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2834 return snd_pcm_hw_params_old_user(substream, arg);
2836 case SNDRV_PCM_IOCTL_DRAIN:
2837 return snd_pcm_drain(substream, file);
2838 case SNDRV_PCM_IOCTL_DROP:
2839 return snd_pcm_drop(substream);
2840 case SNDRV_PCM_IOCTL_PAUSE:
2841 return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
2843 (int)(unsigned long)arg);
2845 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2849 static int snd_pcm_common_ioctl1(struct file *file,
2850 struct snd_pcm_substream *substream,
2851 unsigned int cmd, void __user *arg)
2853 struct snd_card *card = substream->pcm->card;
2856 snd_power_lock(card);
2857 res = snd_power_wait(card, SNDRV_CTL_POWER_D0);
2859 res = snd_pcm_common_ioctl(file, substream, cmd, arg);
2860 snd_power_unlock(card);
2864 static int snd_pcm_playback_ioctl1(struct file *file,
2865 struct snd_pcm_substream *substream,
2866 unsigned int cmd, void __user *arg)
2868 if (PCM_RUNTIME_CHECK(substream))
2870 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2873 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2875 struct snd_xferi xferi;
2876 struct snd_xferi __user *_xferi = arg;
2877 struct snd_pcm_runtime *runtime = substream->runtime;
2878 snd_pcm_sframes_t result;
2879 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2881 if (put_user(0, &_xferi->result))
2883 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2885 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2886 __put_user(result, &_xferi->result);
2887 return result < 0 ? result : 0;
2889 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2891 struct snd_xfern xfern;
2892 struct snd_xfern __user *_xfern = arg;
2893 struct snd_pcm_runtime *runtime = substream->runtime;
2895 snd_pcm_sframes_t result;
2896 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2898 if (runtime->channels > 128)
2900 if (put_user(0, &_xfern->result))
2902 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2905 bufs = memdup_user(xfern.bufs,
2906 sizeof(void *) * runtime->channels);
2908 return PTR_ERR(bufs);
2909 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2911 __put_user(result, &_xfern->result);
2912 return result < 0 ? result : 0;
2914 case SNDRV_PCM_IOCTL_REWIND:
2916 snd_pcm_uframes_t frames;
2917 snd_pcm_uframes_t __user *_frames = arg;
2918 snd_pcm_sframes_t result;
2919 if (get_user(frames, _frames))
2921 if (put_user(0, _frames))
2923 result = snd_pcm_playback_rewind(substream, frames);
2924 __put_user(result, _frames);
2925 return result < 0 ? result : 0;
2927 case SNDRV_PCM_IOCTL_FORWARD:
2929 snd_pcm_uframes_t frames;
2930 snd_pcm_uframes_t __user *_frames = arg;
2931 snd_pcm_sframes_t result;
2932 if (get_user(frames, _frames))
2934 if (put_user(0, _frames))
2936 result = snd_pcm_playback_forward(substream, frames);
2937 __put_user(result, _frames);
2938 return result < 0 ? result : 0;
2941 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2944 static int snd_pcm_capture_ioctl1(struct file *file,
2945 struct snd_pcm_substream *substream,
2946 unsigned int cmd, void __user *arg)
2948 if (PCM_RUNTIME_CHECK(substream))
2950 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2953 case SNDRV_PCM_IOCTL_READI_FRAMES:
2955 struct snd_xferi xferi;
2956 struct snd_xferi __user *_xferi = arg;
2957 struct snd_pcm_runtime *runtime = substream->runtime;
2958 snd_pcm_sframes_t result;
2959 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2961 if (put_user(0, &_xferi->result))
2963 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2965 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2966 __put_user(result, &_xferi->result);
2967 return result < 0 ? result : 0;
2969 case SNDRV_PCM_IOCTL_READN_FRAMES:
2971 struct snd_xfern xfern;
2972 struct snd_xfern __user *_xfern = arg;
2973 struct snd_pcm_runtime *runtime = substream->runtime;
2975 snd_pcm_sframes_t result;
2976 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2978 if (runtime->channels > 128)
2980 if (put_user(0, &_xfern->result))
2982 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2985 bufs = memdup_user(xfern.bufs,
2986 sizeof(void *) * runtime->channels);
2988 return PTR_ERR(bufs);
2989 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2991 __put_user(result, &_xfern->result);
2992 return result < 0 ? result : 0;
2994 case SNDRV_PCM_IOCTL_REWIND:
2996 snd_pcm_uframes_t frames;
2997 snd_pcm_uframes_t __user *_frames = arg;
2998 snd_pcm_sframes_t result;
2999 if (get_user(frames, _frames))
3001 if (put_user(0, _frames))
3003 result = snd_pcm_capture_rewind(substream, frames);
3004 __put_user(result, _frames);
3005 return result < 0 ? result : 0;
3007 case SNDRV_PCM_IOCTL_FORWARD:
3009 snd_pcm_uframes_t frames;
3010 snd_pcm_uframes_t __user *_frames = arg;
3011 snd_pcm_sframes_t result;
3012 if (get_user(frames, _frames))
3014 if (put_user(0, _frames))
3016 result = snd_pcm_capture_forward(substream, frames);
3017 __put_user(result, _frames);
3018 return result < 0 ? result : 0;
3021 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
3024 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
3027 struct snd_pcm_file *pcm_file;
3029 pcm_file = file->private_data;
3031 if (((cmd >> 8) & 0xff) != 'A')
3034 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3035 (void __user *)arg);
3038 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3041 struct snd_pcm_file *pcm_file;
3043 pcm_file = file->private_data;
3045 if (((cmd >> 8) & 0xff) != 'A')
3048 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3049 (void __user *)arg);
3053 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3054 * @substream: PCM substream
3056 * @arg: IOCTL argument
3058 * The function is provided primarily for OSS layer and USB gadget drivers,
3059 * and it allows only the limited set of ioctls (hw_params, sw_params,
3060 * prepare, start, drain, drop, forward).
3062 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3063 unsigned int cmd, void *arg)
3065 snd_pcm_uframes_t *frames = arg;
3066 snd_pcm_sframes_t result;
3069 case SNDRV_PCM_IOCTL_FORWARD:
3071 /* provided only for OSS; capture-only and no value returned */
3072 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3074 result = snd_pcm_capture_forward(substream, *frames);
3075 return result < 0 ? result : 0;
3077 case SNDRV_PCM_IOCTL_HW_PARAMS:
3078 return snd_pcm_hw_params(substream, arg);
3079 case SNDRV_PCM_IOCTL_SW_PARAMS:
3080 return snd_pcm_sw_params(substream, arg);
3081 case SNDRV_PCM_IOCTL_PREPARE:
3082 return snd_pcm_prepare(substream, NULL);
3083 case SNDRV_PCM_IOCTL_START:
3084 return snd_pcm_start_lock_irq(substream);
3085 case SNDRV_PCM_IOCTL_DRAIN:
3086 return snd_pcm_drain(substream, NULL);
3087 case SNDRV_PCM_IOCTL_DROP:
3088 return snd_pcm_drop(substream);
3089 case SNDRV_PCM_IOCTL_DELAY:
3091 result = snd_pcm_delay(substream);
3101 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3103 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3106 struct snd_pcm_file *pcm_file;
3107 struct snd_pcm_substream *substream;
3108 struct snd_pcm_runtime *runtime;
3109 snd_pcm_sframes_t result;
3111 pcm_file = file->private_data;
3112 substream = pcm_file->substream;
3113 if (PCM_RUNTIME_CHECK(substream))
3115 runtime = substream->runtime;
3116 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3118 if (!frame_aligned(runtime, count))
3120 count = bytes_to_frames(runtime, count);
3121 result = snd_pcm_lib_read(substream, buf, count);
3123 result = frames_to_bytes(runtime, result);
3127 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3128 size_t count, loff_t * offset)
3130 struct snd_pcm_file *pcm_file;
3131 struct snd_pcm_substream *substream;
3132 struct snd_pcm_runtime *runtime;
3133 snd_pcm_sframes_t result;
3135 pcm_file = file->private_data;
3136 substream = pcm_file->substream;
3137 if (PCM_RUNTIME_CHECK(substream))
3139 runtime = substream->runtime;
3140 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3142 if (!frame_aligned(runtime, count))
3144 count = bytes_to_frames(runtime, count);
3145 result = snd_pcm_lib_write(substream, buf, count);
3147 result = frames_to_bytes(runtime, result);
3151 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3153 struct snd_pcm_file *pcm_file;
3154 struct snd_pcm_substream *substream;
3155 struct snd_pcm_runtime *runtime;
3156 snd_pcm_sframes_t result;
3159 snd_pcm_uframes_t frames;
3161 pcm_file = iocb->ki_filp->private_data;
3162 substream = pcm_file->substream;
3163 if (PCM_RUNTIME_CHECK(substream))
3165 runtime = substream->runtime;
3166 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3168 if (!iter_is_iovec(to))
3170 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3172 if (!frame_aligned(runtime, to->iov->iov_len))
3174 frames = bytes_to_samples(runtime, to->iov->iov_len);
3175 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3178 for (i = 0; i < to->nr_segs; ++i)
3179 bufs[i] = to->iov[i].iov_base;
3180 result = snd_pcm_lib_readv(substream, bufs, frames);
3182 result = frames_to_bytes(runtime, result);
3187 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3189 struct snd_pcm_file *pcm_file;
3190 struct snd_pcm_substream *substream;
3191 struct snd_pcm_runtime *runtime;
3192 snd_pcm_sframes_t result;
3195 snd_pcm_uframes_t frames;
3197 pcm_file = iocb->ki_filp->private_data;
3198 substream = pcm_file->substream;
3199 if (PCM_RUNTIME_CHECK(substream))
3201 runtime = substream->runtime;
3202 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3204 if (!iter_is_iovec(from))
3206 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3207 !frame_aligned(runtime, from->iov->iov_len))
3209 frames = bytes_to_samples(runtime, from->iov->iov_len);
3210 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3213 for (i = 0; i < from->nr_segs; ++i)
3214 bufs[i] = from->iov[i].iov_base;
3215 result = snd_pcm_lib_writev(substream, bufs, frames);
3217 result = frames_to_bytes(runtime, result);
3222 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3224 struct snd_pcm_file *pcm_file;
3225 struct snd_pcm_substream *substream;
3226 struct snd_pcm_runtime *runtime;
3228 snd_pcm_uframes_t avail;
3230 pcm_file = file->private_data;
3232 substream = pcm_file->substream;
3233 if (PCM_RUNTIME_CHECK(substream))
3234 return POLLOUT | POLLWRNORM | POLLERR;
3235 runtime = substream->runtime;
3237 poll_wait(file, &runtime->sleep, wait);
3239 snd_pcm_stream_lock_irq(substream);
3240 avail = snd_pcm_playback_avail(runtime);
3241 switch (runtime->status->state) {
3242 case SNDRV_PCM_STATE_RUNNING:
3243 case SNDRV_PCM_STATE_PREPARED:
3244 case SNDRV_PCM_STATE_PAUSED:
3245 if (avail >= runtime->control->avail_min) {
3246 mask = POLLOUT | POLLWRNORM;
3250 case SNDRV_PCM_STATE_DRAINING:
3254 mask = POLLOUT | POLLWRNORM | POLLERR;
3257 snd_pcm_stream_unlock_irq(substream);
3261 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3263 struct snd_pcm_file *pcm_file;
3264 struct snd_pcm_substream *substream;
3265 struct snd_pcm_runtime *runtime;
3267 snd_pcm_uframes_t avail;
3269 pcm_file = file->private_data;
3271 substream = pcm_file->substream;
3272 if (PCM_RUNTIME_CHECK(substream))
3273 return POLLIN | POLLRDNORM | POLLERR;
3274 runtime = substream->runtime;
3276 poll_wait(file, &runtime->sleep, wait);
3278 snd_pcm_stream_lock_irq(substream);
3279 avail = snd_pcm_capture_avail(runtime);
3280 switch (runtime->status->state) {
3281 case SNDRV_PCM_STATE_RUNNING:
3282 case SNDRV_PCM_STATE_PREPARED:
3283 case SNDRV_PCM_STATE_PAUSED:
3284 if (avail >= runtime->control->avail_min) {
3285 mask = POLLIN | POLLRDNORM;
3290 case SNDRV_PCM_STATE_DRAINING:
3292 mask = POLLIN | POLLRDNORM;
3297 mask = POLLIN | POLLRDNORM | POLLERR;
3300 snd_pcm_stream_unlock_irq(substream);
3309 * Only on coherent architectures, we can mmap the status and the control records
3310 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3312 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3314 * mmap status record
3316 static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3318 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3319 struct snd_pcm_runtime *runtime;
3321 if (substream == NULL)
3322 return VM_FAULT_SIGBUS;
3323 runtime = substream->runtime;
3324 vmf->page = virt_to_page(runtime->status);
3325 get_page(vmf->page);
3329 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3331 .fault = snd_pcm_mmap_status_fault,
3334 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3335 struct vm_area_struct *area)
3338 if (!(area->vm_flags & VM_READ))
3340 size = area->vm_end - area->vm_start;
3341 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3343 area->vm_ops = &snd_pcm_vm_ops_status;
3344 area->vm_private_data = substream;
3345 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3350 * mmap control record
3352 static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3354 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3355 struct snd_pcm_runtime *runtime;
3357 if (substream == NULL)
3358 return VM_FAULT_SIGBUS;
3359 runtime = substream->runtime;
3360 vmf->page = virt_to_page(runtime->control);
3361 get_page(vmf->page);
3365 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3367 .fault = snd_pcm_mmap_control_fault,
3370 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3371 struct vm_area_struct *area)
3374 if (!(area->vm_flags & VM_READ))
3376 size = area->vm_end - area->vm_start;
3377 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3379 area->vm_ops = &snd_pcm_vm_ops_control;
3380 area->vm_private_data = substream;
3381 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3385 static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3387 if (pcm_file->no_compat_mmap)
3389 /* See pcm_control_mmap_allowed() below.
3390 * Since older alsa-lib requires both status and control mmaps to be
3391 * coupled, we have to disable the status mmap for old alsa-lib, too.
3393 if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3394 (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3399 static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3401 if (pcm_file->no_compat_mmap)
3403 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3404 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3405 * thus it effectively assures the manual update of appl_ptr.
3407 if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3412 #else /* ! coherent mmap */
3414 * don't support mmap for status and control records.
3416 #define pcm_status_mmap_allowed(pcm_file) false
3417 #define pcm_control_mmap_allowed(pcm_file) false
3419 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3420 struct vm_area_struct *area)
3424 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3425 struct vm_area_struct *area)
3429 #endif /* coherent mmap */
3431 static inline struct page *
3432 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3434 void *vaddr = substream->runtime->dma_area + ofs;
3435 return virt_to_page(vaddr);
3439 * fault callback for mmapping a RAM page
3441 static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3443 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3444 struct snd_pcm_runtime *runtime;
3445 unsigned long offset;
3449 if (substream == NULL)
3450 return VM_FAULT_SIGBUS;
3451 runtime = substream->runtime;
3452 offset = vmf->pgoff << PAGE_SHIFT;
3453 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3454 if (offset > dma_bytes - PAGE_SIZE)
3455 return VM_FAULT_SIGBUS;
3456 if (substream->ops->page)
3457 page = substream->ops->page(substream, offset);
3459 page = snd_pcm_default_page_ops(substream, offset);
3461 return VM_FAULT_SIGBUS;
3467 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3468 .open = snd_pcm_mmap_data_open,
3469 .close = snd_pcm_mmap_data_close,
3472 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3473 .open = snd_pcm_mmap_data_open,
3474 .close = snd_pcm_mmap_data_close,
3475 .fault = snd_pcm_mmap_data_fault,
3479 * mmap the DMA buffer on RAM
3483 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3484 * @substream: PCM substream
3487 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3488 * this function is invoked implicitly.
3490 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3491 struct vm_area_struct *area)
3493 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3494 #ifdef CONFIG_GENERIC_ALLOCATOR
3495 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3496 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3497 return remap_pfn_range(area, area->vm_start,
3498 substream->dma_buffer.addr >> PAGE_SHIFT,
3499 area->vm_end - area->vm_start, area->vm_page_prot);
3501 #endif /* CONFIG_GENERIC_ALLOCATOR */
3502 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3503 if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3504 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3505 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3507 substream->runtime->dma_area,
3508 substream->runtime->dma_addr,
3509 area->vm_end - area->vm_start);
3510 #endif /* CONFIG_X86 */
3511 /* mmap with fault handler */
3512 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3515 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3518 * mmap the DMA buffer on I/O memory area
3520 #if SNDRV_PCM_INFO_MMAP_IOMEM
3522 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3523 * @substream: PCM substream
3526 * When your hardware uses the iomapped pages as the hardware buffer and
3527 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3528 * is supposed to work only on limited architectures.
3530 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3531 struct vm_area_struct *area)
3533 struct snd_pcm_runtime *runtime = substream->runtime;;
3535 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3536 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3538 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3539 #endif /* SNDRV_PCM_INFO_MMAP */
3544 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3545 struct vm_area_struct *area)
3547 struct snd_pcm_runtime *runtime;
3549 unsigned long offset;
3553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3554 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3557 if (!(area->vm_flags & VM_READ))
3560 runtime = substream->runtime;
3561 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3563 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3565 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3566 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3568 size = area->vm_end - area->vm_start;
3569 offset = area->vm_pgoff << PAGE_SHIFT;
3570 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3571 if ((size_t)size > dma_bytes)
3573 if (offset > dma_bytes - size)
3576 area->vm_ops = &snd_pcm_vm_ops_data;
3577 area->vm_private_data = substream;
3578 if (substream->ops->mmap)
3579 err = substream->ops->mmap(substream, area);
3581 err = snd_pcm_lib_default_mmap(substream, area);
3583 atomic_inc(&substream->mmap_count);
3586 EXPORT_SYMBOL(snd_pcm_mmap_data);
3588 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3590 struct snd_pcm_file * pcm_file;
3591 struct snd_pcm_substream *substream;
3592 unsigned long offset;
3594 pcm_file = file->private_data;
3595 substream = pcm_file->substream;
3596 if (PCM_RUNTIME_CHECK(substream))
3599 offset = area->vm_pgoff << PAGE_SHIFT;
3601 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3602 if (!pcm_status_mmap_allowed(pcm_file))
3604 return snd_pcm_mmap_status(substream, file, area);
3605 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3606 if (!pcm_control_mmap_allowed(pcm_file))
3608 return snd_pcm_mmap_control(substream, file, area);
3610 return snd_pcm_mmap_data(substream, file, area);
3615 static int snd_pcm_fasync(int fd, struct file * file, int on)
3617 struct snd_pcm_file * pcm_file;
3618 struct snd_pcm_substream *substream;
3619 struct snd_pcm_runtime *runtime;
3621 pcm_file = file->private_data;
3622 substream = pcm_file->substream;
3623 if (PCM_RUNTIME_CHECK(substream))
3625 runtime = substream->runtime;
3626 return fasync_helper(fd, file, on, &runtime->fasync);
3632 #ifdef CONFIG_COMPAT
3633 #include "pcm_compat.c"
3635 #define snd_pcm_ioctl_compat NULL
3639 * To be removed helpers to keep binary compatibility
3642 #ifdef CONFIG_SND_SUPPORT_OLD_API
3643 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3644 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3646 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3647 struct snd_pcm_hw_params_old *oparams)
3651 memset(params, 0, sizeof(*params));
3652 params->flags = oparams->flags;
3653 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3654 params->masks[i].bits[0] = oparams->masks[i];
3655 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3656 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3657 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3658 params->info = oparams->info;
3659 params->msbits = oparams->msbits;
3660 params->rate_num = oparams->rate_num;
3661 params->rate_den = oparams->rate_den;
3662 params->fifo_size = oparams->fifo_size;
3665 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3666 struct snd_pcm_hw_params *params)
3670 memset(oparams, 0, sizeof(*oparams));
3671 oparams->flags = params->flags;
3672 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3673 oparams->masks[i] = params->masks[i].bits[0];
3674 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3675 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3676 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3677 oparams->info = params->info;
3678 oparams->msbits = params->msbits;
3679 oparams->rate_num = params->rate_num;
3680 oparams->rate_den = params->rate_den;
3681 oparams->fifo_size = params->fifo_size;
3684 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3685 struct snd_pcm_hw_params_old __user * _oparams)
3687 struct snd_pcm_hw_params *params;
3688 struct snd_pcm_hw_params_old *oparams = NULL;
3691 params = kmalloc(sizeof(*params), GFP_KERNEL);
3695 oparams = memdup_user(_oparams, sizeof(*oparams));
3696 if (IS_ERR(oparams)) {
3697 err = PTR_ERR(oparams);
3700 snd_pcm_hw_convert_from_old_params(params, oparams);
3701 err = snd_pcm_hw_refine(substream, params);
3705 err = fixup_unreferenced_params(substream, params);
3709 snd_pcm_hw_convert_to_old_params(oparams, params);
3710 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3719 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3720 struct snd_pcm_hw_params_old __user * _oparams)
3722 struct snd_pcm_hw_params *params;
3723 struct snd_pcm_hw_params_old *oparams = NULL;
3726 params = kmalloc(sizeof(*params), GFP_KERNEL);
3730 oparams = memdup_user(_oparams, sizeof(*oparams));
3731 if (IS_ERR(oparams)) {
3732 err = PTR_ERR(oparams);
3736 snd_pcm_hw_convert_from_old_params(params, oparams);
3737 err = snd_pcm_hw_params(substream, params);
3741 snd_pcm_hw_convert_to_old_params(oparams, params);
3742 if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3750 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3753 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3756 unsigned long pgoff,
3757 unsigned long flags)
3759 struct snd_pcm_file *pcm_file = file->private_data;
3760 struct snd_pcm_substream *substream = pcm_file->substream;
3761 struct snd_pcm_runtime *runtime = substream->runtime;
3762 unsigned long offset = pgoff << PAGE_SHIFT;
3765 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3766 return (unsigned long)runtime->status;
3767 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3768 return (unsigned long)runtime->control;
3770 return (unsigned long)runtime->dma_area + offset;
3774 # define snd_pcm_get_unmapped_area NULL
3781 const struct file_operations snd_pcm_f_ops[2] = {
3783 .owner = THIS_MODULE,
3784 .write = snd_pcm_write,
3785 .write_iter = snd_pcm_writev,
3786 .open = snd_pcm_playback_open,
3787 .release = snd_pcm_release,
3788 .llseek = no_llseek,
3789 .poll = snd_pcm_playback_poll,
3790 .unlocked_ioctl = snd_pcm_playback_ioctl,
3791 .compat_ioctl = snd_pcm_ioctl_compat,
3792 .mmap = snd_pcm_mmap,
3793 .fasync = snd_pcm_fasync,
3794 .get_unmapped_area = snd_pcm_get_unmapped_area,
3797 .owner = THIS_MODULE,
3798 .read = snd_pcm_read,
3799 .read_iter = snd_pcm_readv,
3800 .open = snd_pcm_capture_open,
3801 .release = snd_pcm_release,
3802 .llseek = no_llseek,
3803 .poll = snd_pcm_capture_poll,
3804 .unlocked_ioctl = snd_pcm_capture_ioctl,
3805 .compat_ioctl = snd_pcm_ioctl_compat,
3806 .mmap = snd_pcm_mmap,
3807 .fasync = snd_pcm_fasync,
3808 .get_unmapped_area = snd_pcm_get_unmapped_area,