2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.h>
39 #include <linux/delay.h>
41 #include "pcm_local.h"
43 #ifdef CONFIG_SND_DEBUG
44 #define CREATE_TRACE_POINTS
45 #include "pcm_param_trace.h"
47 #define trace_hw_mask_param_enabled() 0
48 #define trace_hw_interval_param_enabled() 0
49 #define trace_hw_mask_param(substream, type, index, prev, curr)
50 #define trace_hw_interval_param(substream, type, index, prev, curr)
57 struct snd_pcm_hw_params_old
{
59 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
60 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
61 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
62 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
67 unsigned int rate_num
;
68 unsigned int rate_den
;
69 snd_pcm_uframes_t fifo_size
;
70 unsigned char reserved
[64];
73 #ifdef CONFIG_SND_SUPPORT_OLD_API
74 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
75 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
77 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
78 struct snd_pcm_hw_params_old __user
* _oparams
);
79 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
80 struct snd_pcm_hw_params_old __user
* _oparams
);
82 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
88 static DEFINE_RWLOCK(snd_pcm_link_rwlock
);
89 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
91 /* Writer in rwsem may block readers even during its waiting in queue,
92 * and this may lead to a deadlock when the code path takes read sem
93 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
94 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
95 * sleep until all the readers are completed without blocking by writer.
97 static inline void down_write_nonfifo(struct rw_semaphore
*lock
)
99 while (!down_write_trylock(lock
))
104 * snd_pcm_stream_lock - Lock the PCM stream
105 * @substream: PCM substream
107 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
108 * flag of the given substream. This also takes the global link rw lock
109 * (or rw sem), too, for avoiding the race with linked streams.
111 void snd_pcm_stream_lock(struct snd_pcm_substream
*substream
)
113 if (substream
->pcm
->nonatomic
) {
114 down_read_nested(&snd_pcm_link_rwsem
, SINGLE_DEPTH_NESTING
);
115 mutex_lock(&substream
->self_group
.mutex
);
117 read_lock(&snd_pcm_link_rwlock
);
118 spin_lock(&substream
->self_group
.lock
);
121 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock
);
124 * snd_pcm_stream_lock - Unlock the PCM stream
125 * @substream: PCM substream
127 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
129 void snd_pcm_stream_unlock(struct snd_pcm_substream
*substream
)
131 if (substream
->pcm
->nonatomic
) {
132 mutex_unlock(&substream
->self_group
.mutex
);
133 up_read(&snd_pcm_link_rwsem
);
135 spin_unlock(&substream
->self_group
.lock
);
136 read_unlock(&snd_pcm_link_rwlock
);
139 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock
);
142 * snd_pcm_stream_lock_irq - Lock the PCM stream
143 * @substream: PCM substream
145 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
146 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
147 * as snd_pcm_stream_lock().
149 void snd_pcm_stream_lock_irq(struct snd_pcm_substream
*substream
)
151 if (!substream
->pcm
->nonatomic
)
153 snd_pcm_stream_lock(substream
);
155 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq
);
158 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
159 * @substream: PCM substream
161 * This is a counter-part of snd_pcm_stream_lock_irq().
163 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream
*substream
)
165 snd_pcm_stream_unlock(substream
);
166 if (!substream
->pcm
->nonatomic
)
169 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq
);
171 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream
*substream
)
173 unsigned long flags
= 0;
174 if (!substream
->pcm
->nonatomic
)
175 local_irq_save(flags
);
176 snd_pcm_stream_lock(substream
);
179 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave
);
182 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
183 * @substream: PCM substream
186 * This is a counter-part of snd_pcm_stream_lock_irqsave().
188 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream
*substream
,
191 snd_pcm_stream_unlock(substream
);
192 if (!substream
->pcm
->nonatomic
)
193 local_irq_restore(flags
);
195 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore
);
197 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
199 struct snd_pcm
*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
));
218 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
219 struct snd_pcm_info __user
* _info
)
221 struct snd_pcm_info
*info
;
224 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
227 err
= snd_pcm_info(substream
, info
);
229 if (copy_to_user(_info
, info
, sizeof(*info
)))
236 static bool hw_support_mmap(struct snd_pcm_substream
*substream
)
238 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_MMAP
))
240 /* architecture supports dma_mmap_coherent()? */
241 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
242 if (!substream
->ops
->mmap
&&
243 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
249 static int constrain_mask_params(struct snd_pcm_substream
*substream
,
250 struct snd_pcm_hw_params
*params
)
252 struct snd_pcm_hw_constraints
*constrs
=
253 &substream
->runtime
->hw_constraints
;
256 struct snd_mask old_mask
;
259 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
260 m
= hw_param_mask(params
, k
);
261 if (snd_mask_empty(m
))
264 /* This parameter is not requested to change by a caller. */
265 if (!(params
->rmask
& (1 << k
)))
268 if (trace_hw_mask_param_enabled())
271 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
277 /* Set corresponding flag so that the caller gets it. */
278 trace_hw_mask_param(substream
, k
, 0, &old_mask
, m
);
279 params
->cmask
|= 1 << k
;
285 static int constrain_interval_params(struct snd_pcm_substream
*substream
,
286 struct snd_pcm_hw_params
*params
)
288 struct snd_pcm_hw_constraints
*constrs
=
289 &substream
->runtime
->hw_constraints
;
290 struct snd_interval
*i
;
292 struct snd_interval old_interval
;
295 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
296 i
= hw_param_interval(params
, k
);
297 if (snd_interval_empty(i
))
300 /* This parameter is not requested to change by a caller. */
301 if (!(params
->rmask
& (1 << k
)))
304 if (trace_hw_interval_param_enabled())
307 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
313 /* Set corresponding flag so that the caller gets it. */
314 trace_hw_interval_param(substream
, k
, 0, &old_interval
, i
);
315 params
->cmask
|= 1 << k
;
321 static int constrain_params_by_rules(struct snd_pcm_substream
*substream
,
322 struct snd_pcm_hw_params
*params
)
324 struct snd_pcm_hw_constraints
*constrs
=
325 &substream
->runtime
->hw_constraints
;
327 unsigned int rstamps
[constrs
->rules_num
];
328 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
330 struct snd_pcm_hw_rule
*r
;
332 struct snd_mask old_mask
;
333 struct snd_interval old_interval
;
338 * Each application of rule has own sequence number.
340 * Each member of 'rstamps' array represents the sequence number of
341 * recent application of corresponding rule.
343 for (k
= 0; k
< constrs
->rules_num
; k
++)
347 * Each member of 'vstamps' array represents the sequence number of
348 * recent application of rule in which corresponding parameters were
351 * In initial state, elements corresponding to parameters requested by
352 * a caller is 1. For unrequested parameters, corresponding members
353 * have 0 so that the parameters are never changed anymore.
355 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
356 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
358 /* Due to the above design, actual sequence number starts at 2. */
361 /* Apply all rules in order. */
363 for (k
= 0; k
< constrs
->rules_num
; k
++) {
364 r
= &constrs
->rules
[k
];
367 * Check condition bits of this rule. When the rule has
368 * some condition bits, parameter without the bits is
369 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
370 * is an example of the condition bits.
372 if (r
->cond
&& !(r
->cond
& params
->flags
))
376 * The 'deps' array includes maximum three dependencies
377 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
378 * member of this array is a sentinel and should be
381 * This rule should be processed in this time when dependent
382 * parameters were changed at former applications of the other
385 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
386 if (vstamps
[r
->deps
[d
]] > rstamps
[k
])
392 if (trace_hw_mask_param_enabled()) {
393 if (hw_is_mask(r
->var
))
394 old_mask
= *hw_param_mask(params
, r
->var
);
396 if (trace_hw_interval_param_enabled()) {
397 if (hw_is_interval(r
->var
))
398 old_interval
= *hw_param_interval(params
, r
->var
);
401 changed
= r
->func(params
, r
);
406 * When the parameter is changed, notify it to the caller
407 * by corresponding returned bit, then preparing for next
410 if (changed
&& r
->var
>= 0) {
411 if (hw_is_mask(r
->var
)) {
412 trace_hw_mask_param(substream
, r
->var
,
414 hw_param_mask(params
, r
->var
));
416 if (hw_is_interval(r
->var
)) {
417 trace_hw_interval_param(substream
, r
->var
,
418 k
+ 1, &old_interval
,
419 hw_param_interval(params
, r
->var
));
422 params
->cmask
|= (1 << r
->var
);
423 vstamps
[r
->var
] = stamp
;
427 rstamps
[k
] = stamp
++;
430 /* Iterate to evaluate all rules till no parameters are changed. */
437 static int fixup_unreferenced_params(struct snd_pcm_substream
*substream
,
438 struct snd_pcm_hw_params
*params
)
440 const struct snd_interval
*i
;
441 const struct snd_mask
*m
;
444 if (!params
->msbits
) {
445 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
446 if (snd_interval_single(i
))
447 params
->msbits
= snd_interval_value(i
);
450 if (!params
->rate_den
) {
451 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
452 if (snd_interval_single(i
)) {
453 params
->rate_num
= snd_interval_value(i
);
454 params
->rate_den
= 1;
458 if (!params
->fifo_size
) {
459 m
= hw_param_mask_c(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
460 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
461 if (snd_mask_single(m
) && snd_interval_single(i
)) {
462 err
= substream
->ops
->ioctl(substream
,
463 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
470 params
->info
= substream
->runtime
->hw
.info
;
471 params
->info
&= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES
|
472 SNDRV_PCM_INFO_DRAIN_TRIGGER
);
473 if (!hw_support_mmap(substream
))
474 params
->info
&= ~(SNDRV_PCM_INFO_MMAP
|
475 SNDRV_PCM_INFO_MMAP_VALID
);
481 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
482 struct snd_pcm_hw_params
*params
)
487 params
->fifo_size
= 0;
488 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
490 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
491 params
->rate_num
= 0;
492 params
->rate_den
= 0;
495 err
= constrain_mask_params(substream
, params
);
499 err
= constrain_interval_params(substream
, params
);
503 err
= constrain_params_by_rules(substream
, params
);
511 EXPORT_SYMBOL(snd_pcm_hw_refine
);
513 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
514 struct snd_pcm_hw_params __user
* _params
)
516 struct snd_pcm_hw_params
*params
;
519 params
= memdup_user(_params
, sizeof(*params
));
521 return PTR_ERR(params
);
523 err
= snd_pcm_hw_refine(substream
, params
);
527 err
= fixup_unreferenced_params(substream
, params
);
531 if (copy_to_user(_params
, params
, sizeof(*params
)))
538 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
543 return -1; /* invalid */
545 /* take 75% of period time as the deadline */
546 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
547 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
553 static void snd_pcm_set_state(struct snd_pcm_substream
*substream
, int state
)
555 snd_pcm_stream_lock_irq(substream
);
556 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DISCONNECTED
)
557 substream
->runtime
->status
->state
= state
;
558 snd_pcm_stream_unlock_irq(substream
);
561 static inline void snd_pcm_timer_notify(struct snd_pcm_substream
*substream
,
564 #ifdef CONFIG_SND_PCM_TIMER
565 if (substream
->timer
)
566 snd_timer_notify(substream
->timer
, event
,
567 &substream
->runtime
->trigger_tstamp
);
572 * snd_pcm_hw_param_choose - choose a configuration defined by @params
574 * @params: the hw_params instance
576 * Choose one configuration from configuration space defined by @params.
577 * The configuration chosen is that obtained fixing in this order:
578 * first access, first format, first subformat, min channels,
579 * min rate, min period time, max buffer size, min tick time
581 * Return: Zero if successful, or a negative error code on failure.
583 static int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
584 struct snd_pcm_hw_params
*params
)
586 static const int vars
[] = {
587 SNDRV_PCM_HW_PARAM_ACCESS
,
588 SNDRV_PCM_HW_PARAM_FORMAT
,
589 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
590 SNDRV_PCM_HW_PARAM_CHANNELS
,
591 SNDRV_PCM_HW_PARAM_RATE
,
592 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
593 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
594 SNDRV_PCM_HW_PARAM_TICK_TIME
,
598 struct snd_mask old_mask
;
599 struct snd_interval old_interval
;
602 for (v
= vars
; *v
!= -1; v
++) {
603 /* Keep old parameter to trace. */
604 if (trace_hw_mask_param_enabled()) {
606 old_mask
= *hw_param_mask(params
, *v
);
608 if (trace_hw_interval_param_enabled()) {
609 if (hw_is_interval(*v
))
610 old_interval
= *hw_param_interval(params
, *v
);
612 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
613 changed
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
615 changed
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
616 if (snd_BUG_ON(changed
< 0))
621 /* Trace the changed parameter. */
622 if (hw_is_mask(*v
)) {
623 trace_hw_mask_param(pcm
, *v
, 0, &old_mask
,
624 hw_param_mask(params
, *v
));
626 if (hw_is_interval(*v
)) {
627 trace_hw_interval_param(pcm
, *v
, 0, &old_interval
,
628 hw_param_interval(params
, *v
));
635 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
636 struct snd_pcm_hw_params
*params
)
638 struct snd_pcm_runtime
*runtime
;
641 snd_pcm_uframes_t frames
;
643 if (PCM_RUNTIME_CHECK(substream
))
645 runtime
= substream
->runtime
;
646 snd_pcm_stream_lock_irq(substream
);
647 switch (runtime
->status
->state
) {
648 case SNDRV_PCM_STATE_OPEN
:
649 case SNDRV_PCM_STATE_SETUP
:
650 case SNDRV_PCM_STATE_PREPARED
:
653 snd_pcm_stream_unlock_irq(substream
);
656 snd_pcm_stream_unlock_irq(substream
);
657 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
658 if (!substream
->oss
.oss
)
660 if (atomic_read(&substream
->mmap_count
))
664 err
= snd_pcm_hw_refine(substream
, params
);
668 err
= snd_pcm_hw_params_choose(substream
, params
);
672 err
= fixup_unreferenced_params(substream
, params
);
676 if (substream
->ops
->hw_params
!= NULL
) {
677 err
= substream
->ops
->hw_params(substream
, params
);
682 runtime
->access
= params_access(params
);
683 runtime
->format
= params_format(params
);
684 runtime
->subformat
= params_subformat(params
);
685 runtime
->channels
= params_channels(params
);
686 runtime
->rate
= params_rate(params
);
687 runtime
->period_size
= params_period_size(params
);
688 runtime
->periods
= params_periods(params
);
689 runtime
->buffer_size
= params_buffer_size(params
);
690 runtime
->info
= params
->info
;
691 runtime
->rate_num
= params
->rate_num
;
692 runtime
->rate_den
= params
->rate_den
;
693 runtime
->no_period_wakeup
=
694 (params
->info
& SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
) &&
695 (params
->flags
& SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
);
697 bits
= snd_pcm_format_physical_width(runtime
->format
);
698 runtime
->sample_bits
= bits
;
699 bits
*= runtime
->channels
;
700 runtime
->frame_bits
= bits
;
702 while (bits
% 8 != 0) {
706 runtime
->byte_align
= bits
/ 8;
707 runtime
->min_align
= frames
;
709 /* Default sw params */
710 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
711 runtime
->period_step
= 1;
712 runtime
->control
->avail_min
= runtime
->period_size
;
713 runtime
->start_threshold
= 1;
714 runtime
->stop_threshold
= runtime
->buffer_size
;
715 runtime
->silence_threshold
= 0;
716 runtime
->silence_size
= 0;
717 runtime
->boundary
= runtime
->buffer_size
;
718 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
719 runtime
->boundary
*= 2;
721 snd_pcm_timer_resolution_change(substream
);
722 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_SETUP
);
724 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
725 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
726 if ((usecs
= period_to_usecs(runtime
)) >= 0)
727 pm_qos_add_request(&substream
->latency_pm_qos_req
,
728 PM_QOS_CPU_DMA_LATENCY
, usecs
);
731 /* hardware might be unusable from this time,
732 so we force application to retry to set
733 the correct hardware parameter settings */
734 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
735 if (substream
->ops
->hw_free
!= NULL
)
736 substream
->ops
->hw_free(substream
);
740 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
741 struct snd_pcm_hw_params __user
* _params
)
743 struct snd_pcm_hw_params
*params
;
746 params
= memdup_user(_params
, sizeof(*params
));
748 return PTR_ERR(params
);
750 err
= snd_pcm_hw_params(substream
, params
);
754 if (copy_to_user(_params
, params
, sizeof(*params
)))
761 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
763 struct snd_pcm_runtime
*runtime
;
766 if (PCM_RUNTIME_CHECK(substream
))
768 runtime
= substream
->runtime
;
769 snd_pcm_stream_lock_irq(substream
);
770 switch (runtime
->status
->state
) {
771 case SNDRV_PCM_STATE_SETUP
:
772 case SNDRV_PCM_STATE_PREPARED
:
775 snd_pcm_stream_unlock_irq(substream
);
778 snd_pcm_stream_unlock_irq(substream
);
779 if (atomic_read(&substream
->mmap_count
))
781 if (substream
->ops
->hw_free
)
782 result
= substream
->ops
->hw_free(substream
);
783 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
784 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
788 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
789 struct snd_pcm_sw_params
*params
)
791 struct snd_pcm_runtime
*runtime
;
794 if (PCM_RUNTIME_CHECK(substream
))
796 runtime
= substream
->runtime
;
797 snd_pcm_stream_lock_irq(substream
);
798 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
799 snd_pcm_stream_unlock_irq(substream
);
802 snd_pcm_stream_unlock_irq(substream
);
804 if (params
->tstamp_mode
< 0 ||
805 params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
807 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
808 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
810 if (params
->avail_min
== 0)
812 if (params
->silence_size
>= runtime
->boundary
) {
813 if (params
->silence_threshold
!= 0)
816 if (params
->silence_size
> params
->silence_threshold
)
818 if (params
->silence_threshold
> runtime
->buffer_size
)
822 snd_pcm_stream_lock_irq(substream
);
823 runtime
->tstamp_mode
= params
->tstamp_mode
;
824 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
825 runtime
->tstamp_type
= params
->tstamp_type
;
826 runtime
->period_step
= params
->period_step
;
827 runtime
->control
->avail_min
= params
->avail_min
;
828 runtime
->start_threshold
= params
->start_threshold
;
829 runtime
->stop_threshold
= params
->stop_threshold
;
830 runtime
->silence_threshold
= params
->silence_threshold
;
831 runtime
->silence_size
= params
->silence_size
;
832 params
->boundary
= runtime
->boundary
;
833 if (snd_pcm_running(substream
)) {
834 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
835 runtime
->silence_size
> 0)
836 snd_pcm_playback_silence(substream
, ULONG_MAX
);
837 err
= snd_pcm_update_state(substream
, runtime
);
839 snd_pcm_stream_unlock_irq(substream
);
843 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
844 struct snd_pcm_sw_params __user
* _params
)
846 struct snd_pcm_sw_params params
;
848 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
850 err
= snd_pcm_sw_params(substream
, ¶ms
);
851 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
856 int snd_pcm_status(struct snd_pcm_substream
*substream
,
857 struct snd_pcm_status
*status
)
859 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
861 snd_pcm_stream_lock_irq(substream
);
863 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
864 &runtime
->audio_tstamp_config
);
866 /* backwards compatible behavior */
867 if (runtime
->audio_tstamp_config
.type_requested
==
868 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
869 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
870 runtime
->audio_tstamp_config
.type_requested
=
871 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
873 runtime
->audio_tstamp_config
.type_requested
=
874 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
875 runtime
->audio_tstamp_report
.valid
= 0;
877 runtime
->audio_tstamp_report
.valid
= 1;
879 status
->state
= runtime
->status
->state
;
880 status
->suspended_state
= runtime
->status
->suspended_state
;
881 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
883 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
884 if (snd_pcm_running(substream
)) {
885 snd_pcm_update_hw_ptr(substream
);
886 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
887 status
->tstamp
= runtime
->status
->tstamp
;
888 status
->driver_tstamp
= runtime
->driver_tstamp
;
889 status
->audio_tstamp
=
890 runtime
->status
->audio_tstamp
;
891 if (runtime
->audio_tstamp_report
.valid
== 1)
892 /* backwards compatibility, no report provided in COMPAT mode */
893 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
894 &status
->audio_tstamp_accuracy
,
895 &runtime
->audio_tstamp_report
);
900 /* get tstamp only in fallback mode and only if enabled */
901 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
902 snd_pcm_gettime(runtime
, &status
->tstamp
);
905 status
->appl_ptr
= runtime
->control
->appl_ptr
;
906 status
->hw_ptr
= runtime
->status
->hw_ptr
;
907 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
908 status
->avail
= snd_pcm_playback_avail(runtime
);
909 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
910 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
911 status
->delay
= runtime
->buffer_size
- status
->avail
;
912 status
->delay
+= runtime
->delay
;
916 status
->avail
= snd_pcm_capture_avail(runtime
);
917 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
918 status
->delay
= status
->avail
+ runtime
->delay
;
922 status
->avail_max
= runtime
->avail_max
;
923 status
->overrange
= runtime
->overrange
;
924 runtime
->avail_max
= 0;
925 runtime
->overrange
= 0;
927 snd_pcm_stream_unlock_irq(substream
);
931 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
932 struct snd_pcm_status __user
* _status
,
935 struct snd_pcm_status status
;
938 memset(&status
, 0, sizeof(status
));
940 * with extension, parameters are read/write,
941 * get audio_tstamp_data from user,
942 * ignore rest of status structure
944 if (ext
&& get_user(status
.audio_tstamp_data
,
945 (u32 __user
*)(&_status
->audio_tstamp_data
)))
947 res
= snd_pcm_status(substream
, &status
);
950 if (copy_to_user(_status
, &status
, sizeof(status
)))
955 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
956 struct snd_pcm_channel_info
* info
)
958 struct snd_pcm_runtime
*runtime
;
959 unsigned int channel
;
961 channel
= info
->channel
;
962 runtime
= substream
->runtime
;
963 snd_pcm_stream_lock_irq(substream
);
964 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
965 snd_pcm_stream_unlock_irq(substream
);
968 snd_pcm_stream_unlock_irq(substream
);
969 if (channel
>= runtime
->channels
)
971 memset(info
, 0, sizeof(*info
));
972 info
->channel
= channel
;
973 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
976 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
977 struct snd_pcm_channel_info __user
* _info
)
979 struct snd_pcm_channel_info info
;
982 if (copy_from_user(&info
, _info
, sizeof(info
)))
984 res
= snd_pcm_channel_info(substream
, &info
);
987 if (copy_to_user(_info
, &info
, sizeof(info
)))
992 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
994 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
995 if (runtime
->trigger_master
== NULL
)
997 if (runtime
->trigger_master
== substream
) {
998 if (!runtime
->trigger_tstamp_latched
)
999 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
1001 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
1002 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
1004 runtime
->trigger_master
= NULL
;
1008 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
1009 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
1010 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
1011 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
1015 * this functions is core for handling of linked stream
1016 * Note: the stream state might be changed also on failure
1017 * Note2: call with calling stream lock + link lock
1019 static int snd_pcm_action_group(const struct action_ops
*ops
,
1020 struct snd_pcm_substream
*substream
,
1021 int state
, int do_lock
)
1023 struct snd_pcm_substream
*s
= NULL
;
1024 struct snd_pcm_substream
*s1
;
1025 int res
= 0, depth
= 1;
1027 snd_pcm_group_for_each_entry(s
, substream
) {
1028 if (do_lock
&& s
!= substream
) {
1029 if (s
->pcm
->nonatomic
)
1030 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
1032 spin_lock_nested(&s
->self_group
.lock
, depth
);
1035 res
= ops
->pre_action(s
, state
);
1039 snd_pcm_group_for_each_entry(s
, substream
) {
1040 res
= ops
->do_action(s
, state
);
1042 if (ops
->undo_action
) {
1043 snd_pcm_group_for_each_entry(s1
, substream
) {
1044 if (s1
== s
) /* failed stream */
1046 ops
->undo_action(s1
, state
);
1049 s
= NULL
; /* unlock all */
1053 snd_pcm_group_for_each_entry(s
, substream
) {
1054 ops
->post_action(s
, state
);
1058 /* unlock streams */
1059 snd_pcm_group_for_each_entry(s1
, substream
) {
1060 if (s1
!= substream
) {
1061 if (s1
->pcm
->nonatomic
)
1062 mutex_unlock(&s1
->self_group
.mutex
);
1064 spin_unlock(&s1
->self_group
.lock
);
1066 if (s1
== s
) /* end */
1074 * Note: call with stream lock
1076 static int snd_pcm_action_single(const struct action_ops
*ops
,
1077 struct snd_pcm_substream
*substream
,
1082 res
= ops
->pre_action(substream
, state
);
1085 res
= ops
->do_action(substream
, state
);
1087 ops
->post_action(substream
, state
);
1088 else if (ops
->undo_action
)
1089 ops
->undo_action(substream
, state
);
1094 * Note: call with stream lock
1096 static int snd_pcm_action(const struct action_ops
*ops
,
1097 struct snd_pcm_substream
*substream
,
1102 if (!snd_pcm_stream_linked(substream
))
1103 return snd_pcm_action_single(ops
, substream
, state
);
1105 if (substream
->pcm
->nonatomic
) {
1106 if (!mutex_trylock(&substream
->group
->mutex
)) {
1107 mutex_unlock(&substream
->self_group
.mutex
);
1108 mutex_lock(&substream
->group
->mutex
);
1109 mutex_lock(&substream
->self_group
.mutex
);
1111 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1112 mutex_unlock(&substream
->group
->mutex
);
1114 if (!spin_trylock(&substream
->group
->lock
)) {
1115 spin_unlock(&substream
->self_group
.lock
);
1116 spin_lock(&substream
->group
->lock
);
1117 spin_lock(&substream
->self_group
.lock
);
1119 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1120 spin_unlock(&substream
->group
->lock
);
1126 * Note: don't use any locks before
1128 static int snd_pcm_action_lock_irq(const struct action_ops
*ops
,
1129 struct snd_pcm_substream
*substream
,
1134 snd_pcm_stream_lock_irq(substream
);
1135 res
= snd_pcm_action(ops
, substream
, state
);
1136 snd_pcm_stream_unlock_irq(substream
);
1142 static int snd_pcm_action_nonatomic(const struct action_ops
*ops
,
1143 struct snd_pcm_substream
*substream
,
1148 down_read(&snd_pcm_link_rwsem
);
1149 if (snd_pcm_stream_linked(substream
))
1150 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1152 res
= snd_pcm_action_single(ops
, substream
, state
);
1153 up_read(&snd_pcm_link_rwsem
);
1160 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1162 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1163 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1165 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1166 !snd_pcm_playback_data(substream
))
1168 runtime
->trigger_tstamp_latched
= false;
1169 runtime
->trigger_master
= substream
;
1173 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1175 if (substream
->runtime
->trigger_master
!= substream
)
1177 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1180 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1182 if (substream
->runtime
->trigger_master
== substream
)
1183 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1186 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1188 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1189 snd_pcm_trigger_tstamp(substream
);
1190 runtime
->hw_ptr_jiffies
= jiffies
;
1191 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1193 runtime
->status
->state
= state
;
1194 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1195 runtime
->silence_size
> 0)
1196 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1197 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTART
);
1200 static const struct action_ops snd_pcm_action_start
= {
1201 .pre_action
= snd_pcm_pre_start
,
1202 .do_action
= snd_pcm_do_start
,
1203 .undo_action
= snd_pcm_undo_start
,
1204 .post_action
= snd_pcm_post_start
1208 * snd_pcm_start - start all linked streams
1209 * @substream: the PCM substream instance
1211 * Return: Zero if successful, or a negative error code.
1212 * The stream lock must be acquired before calling this function.
1214 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1216 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1217 SNDRV_PCM_STATE_RUNNING
);
1220 /* take the stream lock and start the streams */
1221 static int snd_pcm_start_lock_irq(struct snd_pcm_substream
*substream
)
1223 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
,
1224 SNDRV_PCM_STATE_RUNNING
);
1230 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1232 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1233 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1235 runtime
->trigger_master
= substream
;
1239 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1241 if (substream
->runtime
->trigger_master
== substream
&&
1242 snd_pcm_running(substream
))
1243 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1244 return 0; /* unconditonally stop all substreams */
1247 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1249 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1250 if (runtime
->status
->state
!= state
) {
1251 snd_pcm_trigger_tstamp(substream
);
1252 runtime
->status
->state
= state
;
1253 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTOP
);
1255 wake_up(&runtime
->sleep
);
1256 wake_up(&runtime
->tsleep
);
1259 static const struct action_ops snd_pcm_action_stop
= {
1260 .pre_action
= snd_pcm_pre_stop
,
1261 .do_action
= snd_pcm_do_stop
,
1262 .post_action
= snd_pcm_post_stop
1266 * snd_pcm_stop - try to stop all running streams in the substream group
1267 * @substream: the PCM substream instance
1268 * @state: PCM state after stopping the stream
1270 * The state of each stream is then changed to the given state unconditionally.
1272 * Return: Zero if successful, or a negative error code.
1274 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1276 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1278 EXPORT_SYMBOL(snd_pcm_stop
);
1281 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1282 * @substream: the PCM substream
1284 * After stopping, the state is changed to SETUP.
1285 * Unlike snd_pcm_stop(), this affects only the given stream.
1287 * Return: Zero if succesful, or a negative error code.
1289 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1291 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1292 SNDRV_PCM_STATE_SETUP
);
1296 * snd_pcm_stop_xrun - stop the running streams as XRUN
1297 * @substream: the PCM substream instance
1299 * This stops the given running substream (and all linked substreams) as XRUN.
1300 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1302 * Return: Zero if successful, or a negative error code.
1304 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1306 unsigned long flags
;
1309 snd_pcm_stream_lock_irqsave(substream
, flags
);
1310 if (snd_pcm_running(substream
))
1311 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1312 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1315 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1320 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1322 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1323 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1326 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1328 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1330 runtime
->trigger_master
= substream
;
1334 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1336 if (substream
->runtime
->trigger_master
!= substream
)
1338 /* some drivers might use hw_ptr to recover from the pause -
1339 update the hw_ptr now */
1341 snd_pcm_update_hw_ptr(substream
);
1342 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1343 * a delta between the current jiffies, this gives a large enough
1344 * delta, effectively to skip the check once.
1346 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1347 return substream
->ops
->trigger(substream
,
1348 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1349 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1352 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1354 if (substream
->runtime
->trigger_master
== substream
)
1355 substream
->ops
->trigger(substream
,
1356 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1357 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1360 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1362 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1363 snd_pcm_trigger_tstamp(substream
);
1365 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1366 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MPAUSE
);
1367 wake_up(&runtime
->sleep
);
1368 wake_up(&runtime
->tsleep
);
1370 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1371 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MCONTINUE
);
1375 static const struct action_ops snd_pcm_action_pause
= {
1376 .pre_action
= snd_pcm_pre_pause
,
1377 .do_action
= snd_pcm_do_pause
,
1378 .undo_action
= snd_pcm_undo_pause
,
1379 .post_action
= snd_pcm_post_pause
1383 * Push/release the pause for all linked streams.
1385 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1387 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1393 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1395 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1396 switch (runtime
->status
->state
) {
1397 case SNDRV_PCM_STATE_SUSPENDED
:
1399 /* unresumable PCM state; return -EBUSY for skipping suspend */
1400 case SNDRV_PCM_STATE_OPEN
:
1401 case SNDRV_PCM_STATE_SETUP
:
1402 case SNDRV_PCM_STATE_DISCONNECTED
:
1405 runtime
->trigger_master
= substream
;
1409 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1411 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1412 if (runtime
->trigger_master
!= substream
)
1414 if (! snd_pcm_running(substream
))
1416 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1417 return 0; /* suspend unconditionally */
1420 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1422 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1423 snd_pcm_trigger_tstamp(substream
);
1424 runtime
->status
->suspended_state
= runtime
->status
->state
;
1425 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1426 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSUSPEND
);
1427 wake_up(&runtime
->sleep
);
1428 wake_up(&runtime
->tsleep
);
1431 static const struct action_ops snd_pcm_action_suspend
= {
1432 .pre_action
= snd_pcm_pre_suspend
,
1433 .do_action
= snd_pcm_do_suspend
,
1434 .post_action
= snd_pcm_post_suspend
1438 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1439 * @substream: the PCM substream
1441 * After this call, all streams are changed to SUSPENDED state.
1443 * Return: Zero if successful (or @substream is %NULL), or a negative error
1446 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1449 unsigned long flags
;
1454 snd_pcm_stream_lock_irqsave(substream
, flags
);
1455 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1456 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1459 EXPORT_SYMBOL(snd_pcm_suspend
);
1462 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1463 * @pcm: the PCM instance
1465 * After this call, all streams are changed to SUSPENDED state.
1467 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1469 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1471 struct snd_pcm_substream
*substream
;
1472 int stream
, err
= 0;
1477 for (stream
= 0; stream
< 2; stream
++) {
1478 for (substream
= pcm
->streams
[stream
].substream
;
1479 substream
; substream
= substream
->next
) {
1480 /* FIXME: the open/close code should lock this as well */
1481 if (substream
->runtime
== NULL
)
1485 * Skip BE dai link PCM's that are internal and may
1486 * not have their substream ops set.
1488 if (!substream
->ops
)
1491 err
= snd_pcm_suspend(substream
);
1492 if (err
< 0 && err
!= -EBUSY
)
1498 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1502 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1504 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1505 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1507 runtime
->trigger_master
= substream
;
1511 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1513 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1514 if (runtime
->trigger_master
!= substream
)
1516 /* DMA not running previously? */
1517 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1518 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1519 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1521 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1524 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1526 if (substream
->runtime
->trigger_master
== substream
&&
1527 snd_pcm_running(substream
))
1528 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1531 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1533 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1534 snd_pcm_trigger_tstamp(substream
);
1535 runtime
->status
->state
= runtime
->status
->suspended_state
;
1536 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MRESUME
);
1539 static const struct action_ops snd_pcm_action_resume
= {
1540 .pre_action
= snd_pcm_pre_resume
,
1541 .do_action
= snd_pcm_do_resume
,
1542 .undo_action
= snd_pcm_undo_resume
,
1543 .post_action
= snd_pcm_post_resume
1546 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1548 return snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1553 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1558 #endif /* CONFIG_PM */
1563 * Change the RUNNING stream(s) to XRUN state.
1565 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1567 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1570 snd_pcm_stream_lock_irq(substream
);
1571 switch (runtime
->status
->state
) {
1572 case SNDRV_PCM_STATE_XRUN
:
1573 result
= 0; /* already there */
1575 case SNDRV_PCM_STATE_RUNNING
:
1576 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1581 snd_pcm_stream_unlock_irq(substream
);
1588 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1590 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1591 switch (runtime
->status
->state
) {
1592 case SNDRV_PCM_STATE_RUNNING
:
1593 case SNDRV_PCM_STATE_PREPARED
:
1594 case SNDRV_PCM_STATE_PAUSED
:
1595 case SNDRV_PCM_STATE_SUSPENDED
:
1602 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1604 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1605 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1608 runtime
->hw_ptr_base
= 0;
1609 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1610 runtime
->status
->hw_ptr
% runtime
->period_size
;
1611 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1612 runtime
->silence_filled
= 0;
1616 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1618 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1619 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1620 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1621 runtime
->silence_size
> 0)
1622 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1625 static const struct action_ops snd_pcm_action_reset
= {
1626 .pre_action
= snd_pcm_pre_reset
,
1627 .do_action
= snd_pcm_do_reset
,
1628 .post_action
= snd_pcm_post_reset
1631 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1633 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1639 /* we use the second argument for updating f_flags */
1640 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1643 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1644 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1645 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1647 if (snd_pcm_running(substream
))
1649 substream
->f_flags
= f_flags
;
1653 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1656 err
= substream
->ops
->prepare(substream
);
1659 return snd_pcm_do_reset(substream
, 0);
1662 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1664 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1665 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1666 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1669 static const struct action_ops snd_pcm_action_prepare
= {
1670 .pre_action
= snd_pcm_pre_prepare
,
1671 .do_action
= snd_pcm_do_prepare
,
1672 .post_action
= snd_pcm_post_prepare
1676 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1677 * @substream: the PCM substream instance
1678 * @file: file to refer f_flags
1680 * Return: Zero if successful, or a negative error code.
1682 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1688 f_flags
= file
->f_flags
;
1690 f_flags
= substream
->f_flags
;
1692 snd_pcm_stream_lock_irq(substream
);
1693 switch (substream
->runtime
->status
->state
) {
1694 case SNDRV_PCM_STATE_PAUSED
:
1695 snd_pcm_pause(substream
, 0);
1697 case SNDRV_PCM_STATE_SUSPENDED
:
1698 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1701 snd_pcm_stream_unlock_irq(substream
);
1703 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1704 substream
, f_flags
);
1711 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1713 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1714 switch (runtime
->status
->state
) {
1715 case SNDRV_PCM_STATE_OPEN
:
1716 case SNDRV_PCM_STATE_DISCONNECTED
:
1717 case SNDRV_PCM_STATE_SUSPENDED
:
1720 runtime
->trigger_master
= substream
;
1724 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1726 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1727 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1728 switch (runtime
->status
->state
) {
1729 case SNDRV_PCM_STATE_PREPARED
:
1730 /* start playback stream if possible */
1731 if (! snd_pcm_playback_empty(substream
)) {
1732 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1733 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1735 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1738 case SNDRV_PCM_STATE_RUNNING
:
1739 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1741 case SNDRV_PCM_STATE_XRUN
:
1742 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1748 /* stop running stream */
1749 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1750 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1751 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1752 snd_pcm_do_stop(substream
, new_state
);
1753 snd_pcm_post_stop(substream
, new_state
);
1757 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1758 runtime
->trigger_master
== substream
&&
1759 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1760 return substream
->ops
->trigger(substream
,
1761 SNDRV_PCM_TRIGGER_DRAIN
);
1766 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1770 static const struct action_ops snd_pcm_action_drain_init
= {
1771 .pre_action
= snd_pcm_pre_drain_init
,
1772 .do_action
= snd_pcm_do_drain_init
,
1773 .post_action
= snd_pcm_post_drain_init
1776 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1779 * Drain the stream(s).
1780 * When the substream is linked, sync until the draining of all playback streams
1782 * After this call, all streams are supposed to be either SETUP or DRAINING
1783 * (capture only) state.
1785 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1788 struct snd_card
*card
;
1789 struct snd_pcm_runtime
*runtime
;
1790 struct snd_pcm_substream
*s
;
1791 wait_queue_entry_t wait
;
1795 card
= substream
->pcm
->card
;
1796 runtime
= substream
->runtime
;
1798 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1802 if (file
->f_flags
& O_NONBLOCK
)
1804 } else if (substream
->f_flags
& O_NONBLOCK
)
1807 down_read(&snd_pcm_link_rwsem
);
1808 snd_pcm_stream_lock_irq(substream
);
1810 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1811 snd_pcm_pause(substream
, 0);
1813 /* pre-start/stop - all running streams are changed to DRAINING state */
1814 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1817 /* in non-blocking, we don't wait in ioctl but let caller poll */
1825 struct snd_pcm_runtime
*to_check
;
1826 if (signal_pending(current
)) {
1827 result
= -ERESTARTSYS
;
1830 /* find a substream to drain */
1832 snd_pcm_group_for_each_entry(s
, substream
) {
1833 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1835 runtime
= s
->runtime
;
1836 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1842 break; /* all drained */
1843 init_waitqueue_entry(&wait
, current
);
1844 add_wait_queue(&to_check
->sleep
, &wait
);
1845 snd_pcm_stream_unlock_irq(substream
);
1846 up_read(&snd_pcm_link_rwsem
);
1847 if (runtime
->no_period_wakeup
)
1848 tout
= MAX_SCHEDULE_TIMEOUT
;
1851 if (runtime
->rate
) {
1852 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1853 tout
= max(t
, tout
);
1855 tout
= msecs_to_jiffies(tout
* 1000);
1857 tout
= schedule_timeout_interruptible(tout
);
1858 down_read(&snd_pcm_link_rwsem
);
1859 snd_pcm_stream_lock_irq(substream
);
1860 remove_wait_queue(&to_check
->sleep
, &wait
);
1861 if (card
->shutdown
) {
1866 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1869 dev_dbg(substream
->pcm
->card
->dev
,
1870 "playback drain error (DMA or IRQ trouble?)\n");
1871 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1879 snd_pcm_stream_unlock_irq(substream
);
1880 up_read(&snd_pcm_link_rwsem
);
1888 * Immediately put all linked substreams into SETUP state.
1890 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1892 struct snd_pcm_runtime
*runtime
;
1895 if (PCM_RUNTIME_CHECK(substream
))
1897 runtime
= substream
->runtime
;
1899 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1900 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1903 snd_pcm_stream_lock_irq(substream
);
1905 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1906 snd_pcm_pause(substream
, 0);
1908 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1909 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1910 snd_pcm_stream_unlock_irq(substream
);
1916 static bool is_pcm_file(struct file
*file
)
1918 struct inode
*inode
= file_inode(file
);
1921 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1923 minor
= iminor(inode
);
1924 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1925 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1931 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1934 struct snd_pcm_file
*pcm_file
;
1935 struct snd_pcm_substream
*substream1
;
1936 struct snd_pcm_group
*group
;
1937 struct fd f
= fdget(fd
);
1941 if (!is_pcm_file(f
.file
)) {
1945 pcm_file
= f
.file
->private_data
;
1946 substream1
= pcm_file
->substream
;
1947 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1952 down_write_nonfifo(&snd_pcm_link_rwsem
);
1953 write_lock_irq(&snd_pcm_link_rwlock
);
1954 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1955 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1956 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1960 if (snd_pcm_stream_linked(substream1
)) {
1964 if (!snd_pcm_stream_linked(substream
)) {
1965 substream
->group
= group
;
1967 spin_lock_init(&substream
->group
->lock
);
1968 mutex_init(&substream
->group
->mutex
);
1969 INIT_LIST_HEAD(&substream
->group
->substreams
);
1970 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1971 substream
->group
->count
= 1;
1973 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1974 substream
->group
->count
++;
1975 substream1
->group
= substream
->group
;
1977 write_unlock_irq(&snd_pcm_link_rwlock
);
1978 up_write(&snd_pcm_link_rwsem
);
1980 snd_card_unref(substream1
->pcm
->card
);
1987 static void relink_to_local(struct snd_pcm_substream
*substream
)
1989 substream
->group
= &substream
->self_group
;
1990 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1991 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1994 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1996 struct snd_pcm_substream
*s
;
1999 down_write_nonfifo(&snd_pcm_link_rwsem
);
2000 write_lock_irq(&snd_pcm_link_rwlock
);
2001 if (!snd_pcm_stream_linked(substream
)) {
2005 list_del(&substream
->link_list
);
2006 substream
->group
->count
--;
2007 if (substream
->group
->count
== 1) { /* detach the last stream, too */
2008 snd_pcm_group_for_each_entry(s
, substream
) {
2012 kfree(substream
->group
);
2014 relink_to_local(substream
);
2016 write_unlock_irq(&snd_pcm_link_rwlock
);
2017 up_write(&snd_pcm_link_rwsem
);
2024 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
2025 struct snd_pcm_hw_rule
*rule
)
2027 struct snd_interval t
;
2028 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
2029 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2030 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2033 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
2034 struct snd_pcm_hw_rule
*rule
)
2036 struct snd_interval t
;
2037 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
2038 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2039 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2042 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
2043 struct snd_pcm_hw_rule
*rule
)
2045 struct snd_interval t
;
2046 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
2047 hw_param_interval_c(params
, rule
->deps
[1]),
2048 (unsigned long) rule
->private, &t
);
2049 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2052 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
2053 struct snd_pcm_hw_rule
*rule
)
2055 struct snd_interval t
;
2056 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
2057 (unsigned long) rule
->private,
2058 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2059 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2062 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
2063 struct snd_pcm_hw_rule
*rule
)
2066 const struct snd_interval
*i
=
2067 hw_param_interval_c(params
, rule
->deps
[0]);
2069 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
2071 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2073 if (! snd_mask_test(mask
, k
))
2075 bits
= snd_pcm_format_physical_width(k
);
2077 continue; /* ignore invalid formats */
2078 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
2079 snd_mask_reset(&m
, k
);
2081 return snd_mask_refine(mask
, &m
);
2084 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
2085 struct snd_pcm_hw_rule
*rule
)
2087 struct snd_interval t
;
2093 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2095 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
2097 bits
= snd_pcm_format_physical_width(k
);
2099 continue; /* ignore invalid formats */
2100 if (t
.min
> (unsigned)bits
)
2102 if (t
.max
< (unsigned)bits
)
2106 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2109 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2110 #error "Change this table"
2113 static const unsigned int rates
[] = {
2114 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2115 48000, 64000, 88200, 96000, 176400, 192000
2118 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
2119 .count
= ARRAY_SIZE(rates
),
2123 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
2124 struct snd_pcm_hw_rule
*rule
)
2126 struct snd_pcm_hardware
*hw
= rule
->private;
2127 return snd_interval_list(hw_param_interval(params
, rule
->var
),
2128 snd_pcm_known_rates
.count
,
2129 snd_pcm_known_rates
.list
, hw
->rates
);
2132 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
2133 struct snd_pcm_hw_rule
*rule
)
2135 struct snd_interval t
;
2136 struct snd_pcm_substream
*substream
= rule
->private;
2138 t
.max
= substream
->buffer_bytes_max
;
2142 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2145 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2147 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2148 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2151 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2152 snd_mask_any(constrs_mask(constrs
, k
));
2155 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2156 snd_interval_any(constrs_interval(constrs
, k
));
2159 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2160 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2161 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2162 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2163 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2165 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2166 snd_pcm_hw_rule_format
, NULL
,
2167 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2170 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2171 snd_pcm_hw_rule_sample_bits
, NULL
,
2172 SNDRV_PCM_HW_PARAM_FORMAT
,
2173 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2176 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2177 snd_pcm_hw_rule_div
, NULL
,
2178 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2181 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2182 snd_pcm_hw_rule_mul
, NULL
,
2183 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2186 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2187 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2188 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2191 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2192 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2193 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2196 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2197 snd_pcm_hw_rule_div
, NULL
,
2198 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2201 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2202 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2203 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2206 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2207 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2208 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2211 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2212 snd_pcm_hw_rule_div
, NULL
,
2213 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2216 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2217 snd_pcm_hw_rule_div
, NULL
,
2218 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2221 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2222 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2223 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2226 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2227 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2228 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2231 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2232 snd_pcm_hw_rule_mul
, NULL
,
2233 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2236 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2237 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2238 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2241 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2242 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2243 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2246 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2247 snd_pcm_hw_rule_muldivk
, (void*) 8,
2248 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2251 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2252 snd_pcm_hw_rule_muldivk
, (void*) 8,
2253 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2256 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2257 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2258 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2261 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2262 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2263 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2269 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2271 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2272 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2274 unsigned int mask
= 0;
2276 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2277 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2278 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2279 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2280 if (hw_support_mmap(substream
)) {
2281 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2282 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2283 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2284 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2285 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2286 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2288 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2292 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2296 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2300 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2301 hw
->channels_min
, hw
->channels_max
);
2305 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2306 hw
->rate_min
, hw
->rate_max
);
2310 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2311 hw
->period_bytes_min
, hw
->period_bytes_max
);
2315 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2316 hw
->periods_min
, hw
->periods_max
);
2320 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2321 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2325 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2326 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2327 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2332 if (runtime
->dma_bytes
) {
2333 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2338 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2339 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2340 snd_pcm_hw_rule_rate
, hw
,
2341 SNDRV_PCM_HW_PARAM_RATE
, -1);
2346 /* FIXME: this belong to lowlevel */
2347 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2352 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2354 if (snd_pcm_stream_linked(substream
))
2355 snd_pcm_unlink(substream
);
2358 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2360 substream
->ref_count
--;
2361 if (substream
->ref_count
> 0)
2364 snd_pcm_drop(substream
);
2365 if (substream
->hw_opened
) {
2366 if (substream
->ops
->hw_free
&&
2367 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
2368 substream
->ops
->hw_free(substream
);
2369 substream
->ops
->close(substream
);
2370 substream
->hw_opened
= 0;
2372 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2373 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2374 if (substream
->pcm_release
) {
2375 substream
->pcm_release(substream
);
2376 substream
->pcm_release
= NULL
;
2378 snd_pcm_detach_substream(substream
);
2380 EXPORT_SYMBOL(snd_pcm_release_substream
);
2382 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2384 struct snd_pcm_substream
**rsubstream
)
2386 struct snd_pcm_substream
*substream
;
2389 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2392 if (substream
->ref_count
> 1) {
2393 *rsubstream
= substream
;
2397 err
= snd_pcm_hw_constraints_init(substream
);
2399 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2403 if ((err
= substream
->ops
->open(substream
)) < 0)
2406 substream
->hw_opened
= 1;
2408 err
= snd_pcm_hw_constraints_complete(substream
);
2410 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2414 *rsubstream
= substream
;
2418 snd_pcm_release_substream(substream
);
2421 EXPORT_SYMBOL(snd_pcm_open_substream
);
2423 static int snd_pcm_open_file(struct file
*file
,
2424 struct snd_pcm
*pcm
,
2427 struct snd_pcm_file
*pcm_file
;
2428 struct snd_pcm_substream
*substream
;
2431 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2435 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2436 if (pcm_file
== NULL
) {
2437 snd_pcm_release_substream(substream
);
2440 pcm_file
->substream
= substream
;
2441 if (substream
->ref_count
== 1) {
2442 substream
->file
= pcm_file
;
2443 substream
->pcm_release
= pcm_release_private
;
2445 file
->private_data
= pcm_file
;
2450 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2452 struct snd_pcm
*pcm
;
2453 int err
= nonseekable_open(inode
, file
);
2456 pcm
= snd_lookup_minor_data(iminor(inode
),
2457 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2458 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2460 snd_card_unref(pcm
->card
);
2464 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2466 struct snd_pcm
*pcm
;
2467 int err
= nonseekable_open(inode
, file
);
2470 pcm
= snd_lookup_minor_data(iminor(inode
),
2471 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2472 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2474 snd_card_unref(pcm
->card
);
2478 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2481 wait_queue_entry_t wait
;
2487 err
= snd_card_file_add(pcm
->card
, file
);
2490 if (!try_module_get(pcm
->card
->module
)) {
2494 init_waitqueue_entry(&wait
, current
);
2495 add_wait_queue(&pcm
->open_wait
, &wait
);
2496 mutex_lock(&pcm
->open_mutex
);
2498 err
= snd_pcm_open_file(file
, pcm
, stream
);
2501 if (err
== -EAGAIN
) {
2502 if (file
->f_flags
& O_NONBLOCK
) {
2508 set_current_state(TASK_INTERRUPTIBLE
);
2509 mutex_unlock(&pcm
->open_mutex
);
2511 mutex_lock(&pcm
->open_mutex
);
2512 if (pcm
->card
->shutdown
) {
2516 if (signal_pending(current
)) {
2521 remove_wait_queue(&pcm
->open_wait
, &wait
);
2522 mutex_unlock(&pcm
->open_mutex
);
2528 module_put(pcm
->card
->module
);
2530 snd_card_file_remove(pcm
->card
, file
);
2535 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2537 struct snd_pcm
*pcm
;
2538 struct snd_pcm_substream
*substream
;
2539 struct snd_pcm_file
*pcm_file
;
2541 pcm_file
= file
->private_data
;
2542 substream
= pcm_file
->substream
;
2543 if (snd_BUG_ON(!substream
))
2545 pcm
= substream
->pcm
;
2546 mutex_lock(&pcm
->open_mutex
);
2547 snd_pcm_release_substream(substream
);
2549 mutex_unlock(&pcm
->open_mutex
);
2550 wake_up(&pcm
->open_wait
);
2551 module_put(pcm
->card
->module
);
2552 snd_card_file_remove(pcm
->card
, file
);
2556 /* check and update PCM state; return 0 or a negative error
2557 * call this inside PCM lock
2559 static int do_pcm_hwsync(struct snd_pcm_substream
*substream
)
2561 switch (substream
->runtime
->status
->state
) {
2562 case SNDRV_PCM_STATE_DRAINING
:
2563 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2566 case SNDRV_PCM_STATE_RUNNING
:
2567 return snd_pcm_update_hw_ptr(substream
);
2568 case SNDRV_PCM_STATE_PREPARED
:
2569 case SNDRV_PCM_STATE_PAUSED
:
2571 case SNDRV_PCM_STATE_SUSPENDED
:
2573 case SNDRV_PCM_STATE_XRUN
:
2580 /* increase the appl_ptr; returns the processed frames or a negative error */
2581 static snd_pcm_sframes_t
forward_appl_ptr(struct snd_pcm_substream
*substream
,
2582 snd_pcm_uframes_t frames
,
2583 snd_pcm_sframes_t avail
)
2585 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2586 snd_pcm_sframes_t appl_ptr
;
2591 if (frames
> (snd_pcm_uframes_t
)avail
)
2593 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2594 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2595 appl_ptr
-= runtime
->boundary
;
2596 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2597 return ret
< 0 ? ret
: frames
;
2600 /* decrease the appl_ptr; returns the processed frames or zero for error */
2601 static snd_pcm_sframes_t
rewind_appl_ptr(struct snd_pcm_substream
*substream
,
2602 snd_pcm_uframes_t frames
,
2603 snd_pcm_sframes_t avail
)
2605 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2606 snd_pcm_sframes_t appl_ptr
;
2611 if (frames
> (snd_pcm_uframes_t
)avail
)
2613 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2615 appl_ptr
+= runtime
->boundary
;
2616 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2617 /* NOTE: we return zero for errors because PulseAudio gets depressed
2618 * upon receiving an error from rewind ioctl and stops processing
2619 * any longer. Returning zero means that no rewind is done, so
2620 * it's not absolutely wrong to answer like that.
2622 return ret
< 0 ? 0 : frames
;
2625 static snd_pcm_sframes_t
snd_pcm_playback_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_playback_hw_avail(runtime
));
2639 snd_pcm_stream_unlock_irq(substream
);
2643 static snd_pcm_sframes_t
snd_pcm_capture_rewind(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
= rewind_appl_ptr(substream
, frames
,
2656 snd_pcm_capture_hw_avail(runtime
));
2657 snd_pcm_stream_unlock_irq(substream
);
2661 static snd_pcm_sframes_t
snd_pcm_playback_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_playback_avail(runtime
));
2675 snd_pcm_stream_unlock_irq(substream
);
2679 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2680 snd_pcm_uframes_t frames
)
2682 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2683 snd_pcm_sframes_t ret
;
2688 snd_pcm_stream_lock_irq(substream
);
2689 ret
= do_pcm_hwsync(substream
);
2691 ret
= forward_appl_ptr(substream
, frames
,
2692 snd_pcm_capture_avail(runtime
));
2693 snd_pcm_stream_unlock_irq(substream
);
2697 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2701 snd_pcm_stream_lock_irq(substream
);
2702 err
= do_pcm_hwsync(substream
);
2703 snd_pcm_stream_unlock_irq(substream
);
2707 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2708 snd_pcm_sframes_t
*delay
)
2710 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2712 snd_pcm_sframes_t n
= 0;
2714 snd_pcm_stream_lock_irq(substream
);
2715 err
= do_pcm_hwsync(substream
);
2717 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2718 n
= snd_pcm_playback_hw_avail(runtime
);
2720 n
= snd_pcm_capture_avail(runtime
);
2721 n
+= runtime
->delay
;
2723 snd_pcm_stream_unlock_irq(substream
);
2729 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2730 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2732 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2733 struct snd_pcm_sync_ptr sync_ptr
;
2734 volatile struct snd_pcm_mmap_status
*status
;
2735 volatile struct snd_pcm_mmap_control
*control
;
2738 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2739 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2741 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2743 status
= runtime
->status
;
2744 control
= runtime
->control
;
2745 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2746 err
= snd_pcm_hwsync(substream
);
2750 snd_pcm_stream_lock_irq(substream
);
2751 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
)) {
2752 err
= pcm_lib_apply_appl_ptr(substream
,
2753 sync_ptr
.c
.control
.appl_ptr
);
2755 snd_pcm_stream_unlock_irq(substream
);
2759 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2761 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2762 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2764 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2765 sync_ptr
.s
.status
.state
= status
->state
;
2766 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2767 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2768 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2769 sync_ptr
.s
.status
.audio_tstamp
= status
->audio_tstamp
;
2770 snd_pcm_stream_unlock_irq(substream
);
2771 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2776 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2778 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2781 if (get_user(arg
, _arg
))
2783 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2785 runtime
->tstamp_type
= arg
;
2789 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream
*substream
,
2790 struct snd_xferi __user
*_xferi
)
2792 struct snd_xferi xferi
;
2793 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2794 snd_pcm_sframes_t result
;
2796 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2798 if (put_user(0, &_xferi
->result
))
2800 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2802 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2803 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2805 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2806 __put_user(result
, &_xferi
->result
);
2807 return result
< 0 ? result
: 0;
2810 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream
*substream
,
2811 struct snd_xfern __user
*_xfern
)
2813 struct snd_xfern xfern
;
2814 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2816 snd_pcm_sframes_t result
;
2818 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2820 if (runtime
->channels
> 128)
2822 if (put_user(0, &_xfern
->result
))
2824 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2827 bufs
= memdup_user(xfern
.bufs
, sizeof(void *) * runtime
->channels
);
2829 return PTR_ERR(bufs
);
2830 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2831 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2833 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2835 __put_user(result
, &_xfern
->result
);
2836 return result
< 0 ? result
: 0;
2839 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream
*substream
,
2840 snd_pcm_uframes_t __user
*_frames
)
2842 snd_pcm_uframes_t frames
;
2843 snd_pcm_sframes_t result
;
2845 if (get_user(frames
, _frames
))
2847 if (put_user(0, _frames
))
2849 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2850 result
= snd_pcm_playback_rewind(substream
, frames
);
2852 result
= snd_pcm_capture_rewind(substream
, frames
);
2853 __put_user(result
, _frames
);
2854 return result
< 0 ? result
: 0;
2857 static int snd_pcm_forward_ioctl(struct snd_pcm_substream
*substream
,
2858 snd_pcm_uframes_t __user
*_frames
)
2860 snd_pcm_uframes_t frames
;
2861 snd_pcm_sframes_t result
;
2863 if (get_user(frames
, _frames
))
2865 if (put_user(0, _frames
))
2867 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2868 result
= snd_pcm_playback_forward(substream
, frames
);
2870 result
= snd_pcm_capture_forward(substream
, frames
);
2871 __put_user(result
, _frames
);
2872 return result
< 0 ? result
: 0;
2875 static int snd_pcm_common_ioctl(struct file
*file
,
2876 struct snd_pcm_substream
*substream
,
2877 unsigned int cmd
, void __user
*arg
)
2879 struct snd_pcm_file
*pcm_file
= file
->private_data
;
2882 if (PCM_RUNTIME_CHECK(substream
))
2885 res
= snd_power_wait(substream
->pcm
->card
, SNDRV_CTL_POWER_D0
);
2890 case SNDRV_PCM_IOCTL_PVERSION
:
2891 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2892 case SNDRV_PCM_IOCTL_INFO
:
2893 return snd_pcm_info_user(substream
, arg
);
2894 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2896 case SNDRV_PCM_IOCTL_TTSTAMP
:
2897 return snd_pcm_tstamp(substream
, arg
);
2898 case SNDRV_PCM_IOCTL_USER_PVERSION
:
2899 if (get_user(pcm_file
->user_pversion
,
2900 (unsigned int __user
*)arg
))
2903 case SNDRV_PCM_IOCTL_HW_REFINE
:
2904 return snd_pcm_hw_refine_user(substream
, arg
);
2905 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2906 return snd_pcm_hw_params_user(substream
, arg
);
2907 case SNDRV_PCM_IOCTL_HW_FREE
:
2908 return snd_pcm_hw_free(substream
);
2909 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2910 return snd_pcm_sw_params_user(substream
, arg
);
2911 case SNDRV_PCM_IOCTL_STATUS
:
2912 return snd_pcm_status_user(substream
, arg
, false);
2913 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2914 return snd_pcm_status_user(substream
, arg
, true);
2915 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2916 return snd_pcm_channel_info_user(substream
, arg
);
2917 case SNDRV_PCM_IOCTL_PREPARE
:
2918 return snd_pcm_prepare(substream
, file
);
2919 case SNDRV_PCM_IOCTL_RESET
:
2920 return snd_pcm_reset(substream
);
2921 case SNDRV_PCM_IOCTL_START
:
2922 return snd_pcm_start_lock_irq(substream
);
2923 case SNDRV_PCM_IOCTL_LINK
:
2924 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2925 case SNDRV_PCM_IOCTL_UNLINK
:
2926 return snd_pcm_unlink(substream
);
2927 case SNDRV_PCM_IOCTL_RESUME
:
2928 return snd_pcm_resume(substream
);
2929 case SNDRV_PCM_IOCTL_XRUN
:
2930 return snd_pcm_xrun(substream
);
2931 case SNDRV_PCM_IOCTL_HWSYNC
:
2932 return snd_pcm_hwsync(substream
);
2933 case SNDRV_PCM_IOCTL_DELAY
:
2935 snd_pcm_sframes_t delay
;
2936 snd_pcm_sframes_t __user
*res
= arg
;
2939 err
= snd_pcm_delay(substream
, &delay
);
2942 if (put_user(delay
, res
))
2946 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2947 return snd_pcm_sync_ptr(substream
, arg
);
2948 #ifdef CONFIG_SND_SUPPORT_OLD_API
2949 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2950 return snd_pcm_hw_refine_old_user(substream
, arg
);
2951 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2952 return snd_pcm_hw_params_old_user(substream
, arg
);
2954 case SNDRV_PCM_IOCTL_DRAIN
:
2955 return snd_pcm_drain(substream
, file
);
2956 case SNDRV_PCM_IOCTL_DROP
:
2957 return snd_pcm_drop(substream
);
2958 case SNDRV_PCM_IOCTL_PAUSE
:
2959 return snd_pcm_action_lock_irq(&snd_pcm_action_pause
,
2961 (int)(unsigned long)arg
);
2962 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2963 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2964 return snd_pcm_xferi_frames_ioctl(substream
, arg
);
2965 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2966 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2967 return snd_pcm_xfern_frames_ioctl(substream
, arg
);
2968 case SNDRV_PCM_IOCTL_REWIND
:
2969 return snd_pcm_rewind_ioctl(substream
, arg
);
2970 case SNDRV_PCM_IOCTL_FORWARD
:
2971 return snd_pcm_forward_ioctl(substream
, arg
);
2973 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2977 static long snd_pcm_ioctl(struct file
*file
, unsigned int cmd
,
2980 struct snd_pcm_file
*pcm_file
;
2982 pcm_file
= file
->private_data
;
2984 if (((cmd
>> 8) & 0xff) != 'A')
2987 return snd_pcm_common_ioctl(file
, pcm_file
->substream
, cmd
,
2988 (void __user
*)arg
);
2992 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2993 * @substream: PCM substream
2995 * @arg: IOCTL argument
2997 * The function is provided primarily for OSS layer and USB gadget drivers,
2998 * and it allows only the limited set of ioctls (hw_params, sw_params,
2999 * prepare, start, drain, drop, forward).
3001 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
3002 unsigned int cmd
, void *arg
)
3004 snd_pcm_uframes_t
*frames
= arg
;
3005 snd_pcm_sframes_t result
;
3008 case SNDRV_PCM_IOCTL_FORWARD
:
3010 /* provided only for OSS; capture-only and no value returned */
3011 if (substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
)
3013 result
= snd_pcm_capture_forward(substream
, *frames
);
3014 return result
< 0 ? result
: 0;
3016 case SNDRV_PCM_IOCTL_HW_PARAMS
:
3017 return snd_pcm_hw_params(substream
, arg
);
3018 case SNDRV_PCM_IOCTL_SW_PARAMS
:
3019 return snd_pcm_sw_params(substream
, arg
);
3020 case SNDRV_PCM_IOCTL_PREPARE
:
3021 return snd_pcm_prepare(substream
, NULL
);
3022 case SNDRV_PCM_IOCTL_START
:
3023 return snd_pcm_start_lock_irq(substream
);
3024 case SNDRV_PCM_IOCTL_DRAIN
:
3025 return snd_pcm_drain(substream
, NULL
);
3026 case SNDRV_PCM_IOCTL_DROP
:
3027 return snd_pcm_drop(substream
);
3028 case SNDRV_PCM_IOCTL_DELAY
:
3029 return snd_pcm_delay(substream
, frames
);
3034 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3036 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
3039 struct snd_pcm_file
*pcm_file
;
3040 struct snd_pcm_substream
*substream
;
3041 struct snd_pcm_runtime
*runtime
;
3042 snd_pcm_sframes_t result
;
3044 pcm_file
= file
->private_data
;
3045 substream
= pcm_file
->substream
;
3046 if (PCM_RUNTIME_CHECK(substream
))
3048 runtime
= substream
->runtime
;
3049 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3051 if (!frame_aligned(runtime
, count
))
3053 count
= bytes_to_frames(runtime
, count
);
3054 result
= snd_pcm_lib_read(substream
, buf
, count
);
3056 result
= frames_to_bytes(runtime
, result
);
3060 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3061 size_t count
, loff_t
* offset
)
3063 struct snd_pcm_file
*pcm_file
;
3064 struct snd_pcm_substream
*substream
;
3065 struct snd_pcm_runtime
*runtime
;
3066 snd_pcm_sframes_t result
;
3068 pcm_file
= file
->private_data
;
3069 substream
= pcm_file
->substream
;
3070 if (PCM_RUNTIME_CHECK(substream
))
3072 runtime
= substream
->runtime
;
3073 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3075 if (!frame_aligned(runtime
, count
))
3077 count
= bytes_to_frames(runtime
, count
);
3078 result
= snd_pcm_lib_write(substream
, buf
, count
);
3080 result
= frames_to_bytes(runtime
, result
);
3084 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3086 struct snd_pcm_file
*pcm_file
;
3087 struct snd_pcm_substream
*substream
;
3088 struct snd_pcm_runtime
*runtime
;
3089 snd_pcm_sframes_t result
;
3092 snd_pcm_uframes_t frames
;
3094 pcm_file
= iocb
->ki_filp
->private_data
;
3095 substream
= pcm_file
->substream
;
3096 if (PCM_RUNTIME_CHECK(substream
))
3098 runtime
= substream
->runtime
;
3099 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3101 if (!iter_is_iovec(to
))
3103 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3105 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3107 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3108 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3111 for (i
= 0; i
< to
->nr_segs
; ++i
)
3112 bufs
[i
] = to
->iov
[i
].iov_base
;
3113 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3115 result
= frames_to_bytes(runtime
, result
);
3120 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3122 struct snd_pcm_file
*pcm_file
;
3123 struct snd_pcm_substream
*substream
;
3124 struct snd_pcm_runtime
*runtime
;
3125 snd_pcm_sframes_t result
;
3128 snd_pcm_uframes_t frames
;
3130 pcm_file
= iocb
->ki_filp
->private_data
;
3131 substream
= pcm_file
->substream
;
3132 if (PCM_RUNTIME_CHECK(substream
))
3134 runtime
= substream
->runtime
;
3135 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3137 if (!iter_is_iovec(from
))
3139 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3140 !frame_aligned(runtime
, from
->iov
->iov_len
))
3142 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3143 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3146 for (i
= 0; i
< from
->nr_segs
; ++i
)
3147 bufs
[i
] = from
->iov
[i
].iov_base
;
3148 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3150 result
= frames_to_bytes(runtime
, result
);
3155 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3157 struct snd_pcm_file
*pcm_file
;
3158 struct snd_pcm_substream
*substream
;
3159 struct snd_pcm_runtime
*runtime
;
3161 snd_pcm_uframes_t avail
;
3163 pcm_file
= file
->private_data
;
3165 substream
= pcm_file
->substream
;
3166 if (PCM_RUNTIME_CHECK(substream
))
3167 return POLLOUT
| POLLWRNORM
| POLLERR
;
3168 runtime
= substream
->runtime
;
3170 poll_wait(file
, &runtime
->sleep
, wait
);
3172 snd_pcm_stream_lock_irq(substream
);
3173 avail
= snd_pcm_playback_avail(runtime
);
3174 switch (runtime
->status
->state
) {
3175 case SNDRV_PCM_STATE_RUNNING
:
3176 case SNDRV_PCM_STATE_PREPARED
:
3177 case SNDRV_PCM_STATE_PAUSED
:
3178 if (avail
>= runtime
->control
->avail_min
) {
3179 mask
= POLLOUT
| POLLWRNORM
;
3183 case SNDRV_PCM_STATE_DRAINING
:
3187 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3190 snd_pcm_stream_unlock_irq(substream
);
3194 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3196 struct snd_pcm_file
*pcm_file
;
3197 struct snd_pcm_substream
*substream
;
3198 struct snd_pcm_runtime
*runtime
;
3200 snd_pcm_uframes_t avail
;
3202 pcm_file
= file
->private_data
;
3204 substream
= pcm_file
->substream
;
3205 if (PCM_RUNTIME_CHECK(substream
))
3206 return POLLIN
| POLLRDNORM
| POLLERR
;
3207 runtime
= substream
->runtime
;
3209 poll_wait(file
, &runtime
->sleep
, wait
);
3211 snd_pcm_stream_lock_irq(substream
);
3212 avail
= snd_pcm_capture_avail(runtime
);
3213 switch (runtime
->status
->state
) {
3214 case SNDRV_PCM_STATE_RUNNING
:
3215 case SNDRV_PCM_STATE_PREPARED
:
3216 case SNDRV_PCM_STATE_PAUSED
:
3217 if (avail
>= runtime
->control
->avail_min
) {
3218 mask
= POLLIN
| POLLRDNORM
;
3223 case SNDRV_PCM_STATE_DRAINING
:
3225 mask
= POLLIN
| POLLRDNORM
;
3230 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3233 snd_pcm_stream_unlock_irq(substream
);
3242 * Only on coherent architectures, we can mmap the status and the control records
3243 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3245 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3247 * mmap status record
3249 static int snd_pcm_mmap_status_fault(struct vm_fault
*vmf
)
3251 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3252 struct snd_pcm_runtime
*runtime
;
3254 if (substream
== NULL
)
3255 return VM_FAULT_SIGBUS
;
3256 runtime
= substream
->runtime
;
3257 vmf
->page
= virt_to_page(runtime
->status
);
3258 get_page(vmf
->page
);
3262 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3264 .fault
= snd_pcm_mmap_status_fault
,
3267 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3268 struct vm_area_struct
*area
)
3271 if (!(area
->vm_flags
& VM_READ
))
3273 size
= area
->vm_end
- area
->vm_start
;
3274 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3276 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3277 area
->vm_private_data
= substream
;
3278 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3283 * mmap control record
3285 static int snd_pcm_mmap_control_fault(struct vm_fault
*vmf
)
3287 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3288 struct snd_pcm_runtime
*runtime
;
3290 if (substream
== NULL
)
3291 return VM_FAULT_SIGBUS
;
3292 runtime
= substream
->runtime
;
3293 vmf
->page
= virt_to_page(runtime
->control
);
3294 get_page(vmf
->page
);
3298 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3300 .fault
= snd_pcm_mmap_control_fault
,
3303 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3304 struct vm_area_struct
*area
)
3307 if (!(area
->vm_flags
& VM_READ
))
3309 size
= area
->vm_end
- area
->vm_start
;
3310 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3312 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3313 area
->vm_private_data
= substream
;
3314 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3318 static bool pcm_status_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3320 if (pcm_file
->no_compat_mmap
)
3322 /* See pcm_control_mmap_allowed() below.
3323 * Since older alsa-lib requires both status and control mmaps to be
3324 * coupled, we have to disable the status mmap for old alsa-lib, too.
3326 if (pcm_file
->user_pversion
< SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3327 (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
))
3332 static bool pcm_control_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3334 if (pcm_file
->no_compat_mmap
)
3336 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3337 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3338 * thus it effectively assures the manual update of appl_ptr.
3340 if (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
)
3345 #else /* ! coherent mmap */
3347 * don't support mmap for status and control records.
3349 #define pcm_status_mmap_allowed(pcm_file) false
3350 #define pcm_control_mmap_allowed(pcm_file) false
3352 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3353 struct vm_area_struct
*area
)
3357 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3358 struct vm_area_struct
*area
)
3362 #endif /* coherent mmap */
3364 static inline struct page
*
3365 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3367 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3368 return virt_to_page(vaddr
);
3372 * fault callback for mmapping a RAM page
3374 static int snd_pcm_mmap_data_fault(struct vm_fault
*vmf
)
3376 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3377 struct snd_pcm_runtime
*runtime
;
3378 unsigned long offset
;
3382 if (substream
== NULL
)
3383 return VM_FAULT_SIGBUS
;
3384 runtime
= substream
->runtime
;
3385 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3386 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3387 if (offset
> dma_bytes
- PAGE_SIZE
)
3388 return VM_FAULT_SIGBUS
;
3389 if (substream
->ops
->page
)
3390 page
= substream
->ops
->page(substream
, offset
);
3392 page
= snd_pcm_default_page_ops(substream
, offset
);
3394 return VM_FAULT_SIGBUS
;
3400 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3401 .open
= snd_pcm_mmap_data_open
,
3402 .close
= snd_pcm_mmap_data_close
,
3405 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3406 .open
= snd_pcm_mmap_data_open
,
3407 .close
= snd_pcm_mmap_data_close
,
3408 .fault
= snd_pcm_mmap_data_fault
,
3412 * mmap the DMA buffer on RAM
3416 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3417 * @substream: PCM substream
3420 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3421 * this function is invoked implicitly.
3423 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3424 struct vm_area_struct
*area
)
3426 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3427 #ifdef CONFIG_GENERIC_ALLOCATOR
3428 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3429 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3430 return remap_pfn_range(area
, area
->vm_start
,
3431 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3432 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3434 #endif /* CONFIG_GENERIC_ALLOCATOR */
3435 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3436 if (IS_ENABLED(CONFIG_HAS_DMA
) && !substream
->ops
->page
&&
3437 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3438 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3440 substream
->runtime
->dma_area
,
3441 substream
->runtime
->dma_addr
,
3442 substream
->runtime
->dma_bytes
);
3443 #endif /* CONFIG_X86 */
3444 /* mmap with fault handler */
3445 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3448 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3451 * mmap the DMA buffer on I/O memory area
3453 #if SNDRV_PCM_INFO_MMAP_IOMEM
3455 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3456 * @substream: PCM substream
3459 * When your hardware uses the iomapped pages as the hardware buffer and
3460 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3461 * is supposed to work only on limited architectures.
3463 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3464 struct vm_area_struct
*area
)
3466 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3468 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3469 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3471 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3472 #endif /* SNDRV_PCM_INFO_MMAP */
3477 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3478 struct vm_area_struct
*area
)
3480 struct snd_pcm_runtime
*runtime
;
3482 unsigned long offset
;
3486 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3487 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3490 if (!(area
->vm_flags
& VM_READ
))
3493 runtime
= substream
->runtime
;
3494 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3496 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3498 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3499 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3501 size
= area
->vm_end
- area
->vm_start
;
3502 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3503 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3504 if ((size_t)size
> dma_bytes
)
3506 if (offset
> dma_bytes
- size
)
3509 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3510 area
->vm_private_data
= substream
;
3511 if (substream
->ops
->mmap
)
3512 err
= substream
->ops
->mmap(substream
, area
);
3514 err
= snd_pcm_lib_default_mmap(substream
, area
);
3516 atomic_inc(&substream
->mmap_count
);
3519 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3521 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3523 struct snd_pcm_file
* pcm_file
;
3524 struct snd_pcm_substream
*substream
;
3525 unsigned long offset
;
3527 pcm_file
= file
->private_data
;
3528 substream
= pcm_file
->substream
;
3529 if (PCM_RUNTIME_CHECK(substream
))
3532 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3534 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3535 if (!pcm_status_mmap_allowed(pcm_file
))
3537 return snd_pcm_mmap_status(substream
, file
, area
);
3538 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3539 if (!pcm_control_mmap_allowed(pcm_file
))
3541 return snd_pcm_mmap_control(substream
, file
, area
);
3543 return snd_pcm_mmap_data(substream
, file
, area
);
3548 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3550 struct snd_pcm_file
* pcm_file
;
3551 struct snd_pcm_substream
*substream
;
3552 struct snd_pcm_runtime
*runtime
;
3554 pcm_file
= file
->private_data
;
3555 substream
= pcm_file
->substream
;
3556 if (PCM_RUNTIME_CHECK(substream
))
3558 runtime
= substream
->runtime
;
3559 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3565 #ifdef CONFIG_COMPAT
3566 #include "pcm_compat.c"
3568 #define snd_pcm_ioctl_compat NULL
3572 * To be removed helpers to keep binary compatibility
3575 #ifdef CONFIG_SND_SUPPORT_OLD_API
3576 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3577 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3579 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3580 struct snd_pcm_hw_params_old
*oparams
)
3584 memset(params
, 0, sizeof(*params
));
3585 params
->flags
= oparams
->flags
;
3586 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3587 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3588 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3589 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3590 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3591 params
->info
= oparams
->info
;
3592 params
->msbits
= oparams
->msbits
;
3593 params
->rate_num
= oparams
->rate_num
;
3594 params
->rate_den
= oparams
->rate_den
;
3595 params
->fifo_size
= oparams
->fifo_size
;
3598 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3599 struct snd_pcm_hw_params
*params
)
3603 memset(oparams
, 0, sizeof(*oparams
));
3604 oparams
->flags
= params
->flags
;
3605 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3606 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3607 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3608 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3609 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3610 oparams
->info
= params
->info
;
3611 oparams
->msbits
= params
->msbits
;
3612 oparams
->rate_num
= params
->rate_num
;
3613 oparams
->rate_den
= params
->rate_den
;
3614 oparams
->fifo_size
= params
->fifo_size
;
3617 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3618 struct snd_pcm_hw_params_old __user
* _oparams
)
3620 struct snd_pcm_hw_params
*params
;
3621 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3624 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3628 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3629 if (IS_ERR(oparams
)) {
3630 err
= PTR_ERR(oparams
);
3633 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3634 err
= snd_pcm_hw_refine(substream
, params
);
3638 err
= fixup_unreferenced_params(substream
, params
);
3642 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3643 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3652 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3653 struct snd_pcm_hw_params_old __user
* _oparams
)
3655 struct snd_pcm_hw_params
*params
;
3656 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3659 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3663 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3664 if (IS_ERR(oparams
)) {
3665 err
= PTR_ERR(oparams
);
3669 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3670 err
= snd_pcm_hw_params(substream
, params
);
3674 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3675 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3683 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3686 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3689 unsigned long pgoff
,
3690 unsigned long flags
)
3692 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3693 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3694 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3695 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3698 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3699 return (unsigned long)runtime
->status
;
3700 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3701 return (unsigned long)runtime
->control
;
3703 return (unsigned long)runtime
->dma_area
+ offset
;
3707 # define snd_pcm_get_unmapped_area NULL
3714 const struct file_operations snd_pcm_f_ops
[2] = {
3716 .owner
= THIS_MODULE
,
3717 .write
= snd_pcm_write
,
3718 .write_iter
= snd_pcm_writev
,
3719 .open
= snd_pcm_playback_open
,
3720 .release
= snd_pcm_release
,
3721 .llseek
= no_llseek
,
3722 .poll
= snd_pcm_playback_poll
,
3723 .unlocked_ioctl
= snd_pcm_ioctl
,
3724 .compat_ioctl
= snd_pcm_ioctl_compat
,
3725 .mmap
= snd_pcm_mmap
,
3726 .fasync
= snd_pcm_fasync
,
3727 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3730 .owner
= THIS_MODULE
,
3731 .read
= snd_pcm_read
,
3732 .read_iter
= snd_pcm_readv
,
3733 .open
= snd_pcm_capture_open
,
3734 .release
= snd_pcm_release
,
3735 .llseek
= no_llseek
,
3736 .poll
= snd_pcm_capture_poll
,
3737 .unlocked_ioctl
= snd_pcm_ioctl
,
3738 .compat_ioctl
= snd_pcm_ioctl_compat
,
3739 .mmap
= snd_pcm_mmap
,
3740 .fasync
= snd_pcm_fasync
,
3741 .get_unmapped_area
= snd_pcm_get_unmapped_area
,