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 /* clear the buffer for avoiding possible kernel info leaks */
722 if (runtime
->dma_area
&& !substream
->ops
->copy_user
)
723 memset(runtime
->dma_area
, 0, runtime
->dma_bytes
);
725 snd_pcm_timer_resolution_change(substream
);
726 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_SETUP
);
728 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
729 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
730 if ((usecs
= period_to_usecs(runtime
)) >= 0)
731 pm_qos_add_request(&substream
->latency_pm_qos_req
,
732 PM_QOS_CPU_DMA_LATENCY
, usecs
);
735 /* hardware might be unusable from this time,
736 so we force application to retry to set
737 the correct hardware parameter settings */
738 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
739 if (substream
->ops
->hw_free
!= NULL
)
740 substream
->ops
->hw_free(substream
);
744 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
745 struct snd_pcm_hw_params __user
* _params
)
747 struct snd_pcm_hw_params
*params
;
750 params
= memdup_user(_params
, sizeof(*params
));
752 return PTR_ERR(params
);
754 err
= snd_pcm_hw_params(substream
, params
);
758 if (copy_to_user(_params
, params
, sizeof(*params
)))
765 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
767 struct snd_pcm_runtime
*runtime
;
770 if (PCM_RUNTIME_CHECK(substream
))
772 runtime
= substream
->runtime
;
773 snd_pcm_stream_lock_irq(substream
);
774 switch (runtime
->status
->state
) {
775 case SNDRV_PCM_STATE_SETUP
:
776 case SNDRV_PCM_STATE_PREPARED
:
779 snd_pcm_stream_unlock_irq(substream
);
782 snd_pcm_stream_unlock_irq(substream
);
783 if (atomic_read(&substream
->mmap_count
))
785 if (substream
->ops
->hw_free
)
786 result
= substream
->ops
->hw_free(substream
);
787 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
788 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
792 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
793 struct snd_pcm_sw_params
*params
)
795 struct snd_pcm_runtime
*runtime
;
798 if (PCM_RUNTIME_CHECK(substream
))
800 runtime
= substream
->runtime
;
801 snd_pcm_stream_lock_irq(substream
);
802 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
803 snd_pcm_stream_unlock_irq(substream
);
806 snd_pcm_stream_unlock_irq(substream
);
808 if (params
->tstamp_mode
< 0 ||
809 params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
811 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
812 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
814 if (params
->avail_min
== 0)
816 if (params
->silence_size
>= runtime
->boundary
) {
817 if (params
->silence_threshold
!= 0)
820 if (params
->silence_size
> params
->silence_threshold
)
822 if (params
->silence_threshold
> runtime
->buffer_size
)
826 snd_pcm_stream_lock_irq(substream
);
827 runtime
->tstamp_mode
= params
->tstamp_mode
;
828 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
829 runtime
->tstamp_type
= params
->tstamp_type
;
830 runtime
->period_step
= params
->period_step
;
831 runtime
->control
->avail_min
= params
->avail_min
;
832 runtime
->start_threshold
= params
->start_threshold
;
833 runtime
->stop_threshold
= params
->stop_threshold
;
834 runtime
->silence_threshold
= params
->silence_threshold
;
835 runtime
->silence_size
= params
->silence_size
;
836 params
->boundary
= runtime
->boundary
;
837 if (snd_pcm_running(substream
)) {
838 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
839 runtime
->silence_size
> 0)
840 snd_pcm_playback_silence(substream
, ULONG_MAX
);
841 err
= snd_pcm_update_state(substream
, runtime
);
843 snd_pcm_stream_unlock_irq(substream
);
847 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
848 struct snd_pcm_sw_params __user
* _params
)
850 struct snd_pcm_sw_params params
;
852 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
854 err
= snd_pcm_sw_params(substream
, ¶ms
);
855 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
860 int snd_pcm_status(struct snd_pcm_substream
*substream
,
861 struct snd_pcm_status
*status
)
863 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
865 snd_pcm_stream_lock_irq(substream
);
867 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
868 &runtime
->audio_tstamp_config
);
870 /* backwards compatible behavior */
871 if (runtime
->audio_tstamp_config
.type_requested
==
872 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
873 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
874 runtime
->audio_tstamp_config
.type_requested
=
875 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
877 runtime
->audio_tstamp_config
.type_requested
=
878 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
879 runtime
->audio_tstamp_report
.valid
= 0;
881 runtime
->audio_tstamp_report
.valid
= 1;
883 status
->state
= runtime
->status
->state
;
884 status
->suspended_state
= runtime
->status
->suspended_state
;
885 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
887 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
888 if (snd_pcm_running(substream
)) {
889 snd_pcm_update_hw_ptr(substream
);
890 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
891 status
->tstamp
= runtime
->status
->tstamp
;
892 status
->driver_tstamp
= runtime
->driver_tstamp
;
893 status
->audio_tstamp
=
894 runtime
->status
->audio_tstamp
;
895 if (runtime
->audio_tstamp_report
.valid
== 1)
896 /* backwards compatibility, no report provided in COMPAT mode */
897 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
898 &status
->audio_tstamp_accuracy
,
899 &runtime
->audio_tstamp_report
);
904 /* get tstamp only in fallback mode and only if enabled */
905 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
906 snd_pcm_gettime(runtime
, &status
->tstamp
);
909 status
->appl_ptr
= runtime
->control
->appl_ptr
;
910 status
->hw_ptr
= runtime
->status
->hw_ptr
;
911 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
912 status
->avail
= snd_pcm_playback_avail(runtime
);
913 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
914 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
915 status
->delay
= runtime
->buffer_size
- status
->avail
;
916 status
->delay
+= runtime
->delay
;
920 status
->avail
= snd_pcm_capture_avail(runtime
);
921 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
922 status
->delay
= status
->avail
+ runtime
->delay
;
926 status
->avail_max
= runtime
->avail_max
;
927 status
->overrange
= runtime
->overrange
;
928 runtime
->avail_max
= 0;
929 runtime
->overrange
= 0;
931 snd_pcm_stream_unlock_irq(substream
);
935 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
936 struct snd_pcm_status __user
* _status
,
939 struct snd_pcm_status status
;
942 memset(&status
, 0, sizeof(status
));
944 * with extension, parameters are read/write,
945 * get audio_tstamp_data from user,
946 * ignore rest of status structure
948 if (ext
&& get_user(status
.audio_tstamp_data
,
949 (u32 __user
*)(&_status
->audio_tstamp_data
)))
951 res
= snd_pcm_status(substream
, &status
);
954 if (copy_to_user(_status
, &status
, sizeof(status
)))
959 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
960 struct snd_pcm_channel_info
* info
)
962 struct snd_pcm_runtime
*runtime
;
963 unsigned int channel
;
965 channel
= info
->channel
;
966 runtime
= substream
->runtime
;
967 snd_pcm_stream_lock_irq(substream
);
968 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
969 snd_pcm_stream_unlock_irq(substream
);
972 snd_pcm_stream_unlock_irq(substream
);
973 if (channel
>= runtime
->channels
)
975 memset(info
, 0, sizeof(*info
));
976 info
->channel
= channel
;
977 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
980 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
981 struct snd_pcm_channel_info __user
* _info
)
983 struct snd_pcm_channel_info info
;
986 if (copy_from_user(&info
, _info
, sizeof(info
)))
988 res
= snd_pcm_channel_info(substream
, &info
);
991 if (copy_to_user(_info
, &info
, sizeof(info
)))
996 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
998 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
999 if (runtime
->trigger_master
== NULL
)
1001 if (runtime
->trigger_master
== substream
) {
1002 if (!runtime
->trigger_tstamp_latched
)
1003 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
1005 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
1006 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
1008 runtime
->trigger_master
= NULL
;
1012 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
1013 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
1014 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
1015 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
1019 * this functions is core for handling of linked stream
1020 * Note: the stream state might be changed also on failure
1021 * Note2: call with calling stream lock + link lock
1023 static int snd_pcm_action_group(const struct action_ops
*ops
,
1024 struct snd_pcm_substream
*substream
,
1025 int state
, int do_lock
)
1027 struct snd_pcm_substream
*s
= NULL
;
1028 struct snd_pcm_substream
*s1
;
1029 int res
= 0, depth
= 1;
1031 snd_pcm_group_for_each_entry(s
, substream
) {
1032 if (do_lock
&& s
!= substream
) {
1033 if (s
->pcm
->nonatomic
)
1034 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
1036 spin_lock_nested(&s
->self_group
.lock
, depth
);
1039 res
= ops
->pre_action(s
, state
);
1043 snd_pcm_group_for_each_entry(s
, substream
) {
1044 res
= ops
->do_action(s
, state
);
1046 if (ops
->undo_action
) {
1047 snd_pcm_group_for_each_entry(s1
, substream
) {
1048 if (s1
== s
) /* failed stream */
1050 ops
->undo_action(s1
, state
);
1053 s
= NULL
; /* unlock all */
1057 snd_pcm_group_for_each_entry(s
, substream
) {
1058 ops
->post_action(s
, state
);
1062 /* unlock streams */
1063 snd_pcm_group_for_each_entry(s1
, substream
) {
1064 if (s1
!= substream
) {
1065 if (s1
->pcm
->nonatomic
)
1066 mutex_unlock(&s1
->self_group
.mutex
);
1068 spin_unlock(&s1
->self_group
.lock
);
1070 if (s1
== s
) /* end */
1078 * Note: call with stream lock
1080 static int snd_pcm_action_single(const struct action_ops
*ops
,
1081 struct snd_pcm_substream
*substream
,
1086 res
= ops
->pre_action(substream
, state
);
1089 res
= ops
->do_action(substream
, state
);
1091 ops
->post_action(substream
, state
);
1092 else if (ops
->undo_action
)
1093 ops
->undo_action(substream
, state
);
1098 * Note: call with stream lock
1100 static int snd_pcm_action(const struct action_ops
*ops
,
1101 struct snd_pcm_substream
*substream
,
1106 if (!snd_pcm_stream_linked(substream
))
1107 return snd_pcm_action_single(ops
, substream
, state
);
1109 if (substream
->pcm
->nonatomic
) {
1110 if (!mutex_trylock(&substream
->group
->mutex
)) {
1111 mutex_unlock(&substream
->self_group
.mutex
);
1112 mutex_lock(&substream
->group
->mutex
);
1113 mutex_lock(&substream
->self_group
.mutex
);
1115 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1116 mutex_unlock(&substream
->group
->mutex
);
1118 if (!spin_trylock(&substream
->group
->lock
)) {
1119 spin_unlock(&substream
->self_group
.lock
);
1120 spin_lock(&substream
->group
->lock
);
1121 spin_lock(&substream
->self_group
.lock
);
1123 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1124 spin_unlock(&substream
->group
->lock
);
1130 * Note: don't use any locks before
1132 static int snd_pcm_action_lock_irq(const struct action_ops
*ops
,
1133 struct snd_pcm_substream
*substream
,
1138 snd_pcm_stream_lock_irq(substream
);
1139 res
= snd_pcm_action(ops
, substream
, state
);
1140 snd_pcm_stream_unlock_irq(substream
);
1146 static int snd_pcm_action_nonatomic(const struct action_ops
*ops
,
1147 struct snd_pcm_substream
*substream
,
1152 down_read(&snd_pcm_link_rwsem
);
1153 if (snd_pcm_stream_linked(substream
))
1154 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1156 res
= snd_pcm_action_single(ops
, substream
, state
);
1157 up_read(&snd_pcm_link_rwsem
);
1164 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1166 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1167 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1169 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1170 !snd_pcm_playback_data(substream
))
1172 runtime
->trigger_tstamp_latched
= false;
1173 runtime
->trigger_master
= substream
;
1177 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1179 if (substream
->runtime
->trigger_master
!= substream
)
1181 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1184 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1186 if (substream
->runtime
->trigger_master
== substream
)
1187 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1190 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1192 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1193 snd_pcm_trigger_tstamp(substream
);
1194 runtime
->hw_ptr_jiffies
= jiffies
;
1195 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1197 runtime
->status
->state
= state
;
1198 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1199 runtime
->silence_size
> 0)
1200 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1201 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTART
);
1204 static const struct action_ops snd_pcm_action_start
= {
1205 .pre_action
= snd_pcm_pre_start
,
1206 .do_action
= snd_pcm_do_start
,
1207 .undo_action
= snd_pcm_undo_start
,
1208 .post_action
= snd_pcm_post_start
1212 * snd_pcm_start - start all linked streams
1213 * @substream: the PCM substream instance
1215 * Return: Zero if successful, or a negative error code.
1216 * The stream lock must be acquired before calling this function.
1218 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1220 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1221 SNDRV_PCM_STATE_RUNNING
);
1224 /* take the stream lock and start the streams */
1225 static int snd_pcm_start_lock_irq(struct snd_pcm_substream
*substream
)
1227 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
,
1228 SNDRV_PCM_STATE_RUNNING
);
1234 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1236 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1237 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1239 runtime
->trigger_master
= substream
;
1243 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1245 if (substream
->runtime
->trigger_master
== substream
&&
1246 snd_pcm_running(substream
))
1247 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1248 return 0; /* unconditonally stop all substreams */
1251 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1253 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1254 if (runtime
->status
->state
!= state
) {
1255 snd_pcm_trigger_tstamp(substream
);
1256 runtime
->status
->state
= state
;
1257 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTOP
);
1259 wake_up(&runtime
->sleep
);
1260 wake_up(&runtime
->tsleep
);
1263 static const struct action_ops snd_pcm_action_stop
= {
1264 .pre_action
= snd_pcm_pre_stop
,
1265 .do_action
= snd_pcm_do_stop
,
1266 .post_action
= snd_pcm_post_stop
1270 * snd_pcm_stop - try to stop all running streams in the substream group
1271 * @substream: the PCM substream instance
1272 * @state: PCM state after stopping the stream
1274 * The state of each stream is then changed to the given state unconditionally.
1276 * Return: Zero if successful, or a negative error code.
1278 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1280 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1282 EXPORT_SYMBOL(snd_pcm_stop
);
1285 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1286 * @substream: the PCM substream
1288 * After stopping, the state is changed to SETUP.
1289 * Unlike snd_pcm_stop(), this affects only the given stream.
1291 * Return: Zero if succesful, or a negative error code.
1293 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1295 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1296 SNDRV_PCM_STATE_SETUP
);
1300 * snd_pcm_stop_xrun - stop the running streams as XRUN
1301 * @substream: the PCM substream instance
1303 * This stops the given running substream (and all linked substreams) as XRUN.
1304 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1306 * Return: Zero if successful, or a negative error code.
1308 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1310 unsigned long flags
;
1313 snd_pcm_stream_lock_irqsave(substream
, flags
);
1314 if (snd_pcm_running(substream
))
1315 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1316 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1319 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1324 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1326 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1327 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1330 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1332 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1334 runtime
->trigger_master
= substream
;
1338 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1340 if (substream
->runtime
->trigger_master
!= substream
)
1342 /* some drivers might use hw_ptr to recover from the pause -
1343 update the hw_ptr now */
1345 snd_pcm_update_hw_ptr(substream
);
1346 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1347 * a delta between the current jiffies, this gives a large enough
1348 * delta, effectively to skip the check once.
1350 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1351 return substream
->ops
->trigger(substream
,
1352 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1353 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1356 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1358 if (substream
->runtime
->trigger_master
== substream
)
1359 substream
->ops
->trigger(substream
,
1360 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1361 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1364 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1366 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1367 snd_pcm_trigger_tstamp(substream
);
1369 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1370 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MPAUSE
);
1371 wake_up(&runtime
->sleep
);
1372 wake_up(&runtime
->tsleep
);
1374 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1375 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MCONTINUE
);
1379 static const struct action_ops snd_pcm_action_pause
= {
1380 .pre_action
= snd_pcm_pre_pause
,
1381 .do_action
= snd_pcm_do_pause
,
1382 .undo_action
= snd_pcm_undo_pause
,
1383 .post_action
= snd_pcm_post_pause
1387 * Push/release the pause for all linked streams.
1389 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1391 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1397 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1399 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1400 switch (runtime
->status
->state
) {
1401 case SNDRV_PCM_STATE_SUSPENDED
:
1403 /* unresumable PCM state; return -EBUSY for skipping suspend */
1404 case SNDRV_PCM_STATE_OPEN
:
1405 case SNDRV_PCM_STATE_SETUP
:
1406 case SNDRV_PCM_STATE_DISCONNECTED
:
1409 runtime
->trigger_master
= substream
;
1413 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1415 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1416 if (runtime
->trigger_master
!= substream
)
1418 if (! snd_pcm_running(substream
))
1420 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1421 return 0; /* suspend unconditionally */
1424 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1426 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1427 snd_pcm_trigger_tstamp(substream
);
1428 runtime
->status
->suspended_state
= runtime
->status
->state
;
1429 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1430 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSUSPEND
);
1431 wake_up(&runtime
->sleep
);
1432 wake_up(&runtime
->tsleep
);
1435 static const struct action_ops snd_pcm_action_suspend
= {
1436 .pre_action
= snd_pcm_pre_suspend
,
1437 .do_action
= snd_pcm_do_suspend
,
1438 .post_action
= snd_pcm_post_suspend
1442 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1443 * @substream: the PCM substream
1445 * After this call, all streams are changed to SUSPENDED state.
1447 * Return: Zero if successful (or @substream is %NULL), or a negative error
1450 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1453 unsigned long flags
;
1458 snd_pcm_stream_lock_irqsave(substream
, flags
);
1459 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1460 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1463 EXPORT_SYMBOL(snd_pcm_suspend
);
1466 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1467 * @pcm: the PCM instance
1469 * After this call, all streams are changed to SUSPENDED state.
1471 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1473 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1475 struct snd_pcm_substream
*substream
;
1476 int stream
, err
= 0;
1481 for (stream
= 0; stream
< 2; stream
++) {
1482 for (substream
= pcm
->streams
[stream
].substream
;
1483 substream
; substream
= substream
->next
) {
1484 /* FIXME: the open/close code should lock this as well */
1485 if (substream
->runtime
== NULL
)
1489 * Skip BE dai link PCM's that are internal and may
1490 * not have their substream ops set.
1492 if (!substream
->ops
)
1495 err
= snd_pcm_suspend(substream
);
1496 if (err
< 0 && err
!= -EBUSY
)
1502 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1506 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1508 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1509 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1511 runtime
->trigger_master
= substream
;
1515 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1517 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1518 if (runtime
->trigger_master
!= substream
)
1520 /* DMA not running previously? */
1521 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1522 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1523 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1525 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1528 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1530 if (substream
->runtime
->trigger_master
== substream
&&
1531 snd_pcm_running(substream
))
1532 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1535 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1537 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1538 snd_pcm_trigger_tstamp(substream
);
1539 runtime
->status
->state
= runtime
->status
->suspended_state
;
1540 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MRESUME
);
1543 static const struct action_ops snd_pcm_action_resume
= {
1544 .pre_action
= snd_pcm_pre_resume
,
1545 .do_action
= snd_pcm_do_resume
,
1546 .undo_action
= snd_pcm_undo_resume
,
1547 .post_action
= snd_pcm_post_resume
1550 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1552 return snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1557 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1562 #endif /* CONFIG_PM */
1567 * Change the RUNNING stream(s) to XRUN state.
1569 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1571 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1574 snd_pcm_stream_lock_irq(substream
);
1575 switch (runtime
->status
->state
) {
1576 case SNDRV_PCM_STATE_XRUN
:
1577 result
= 0; /* already there */
1579 case SNDRV_PCM_STATE_RUNNING
:
1580 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1585 snd_pcm_stream_unlock_irq(substream
);
1592 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1594 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1595 switch (runtime
->status
->state
) {
1596 case SNDRV_PCM_STATE_RUNNING
:
1597 case SNDRV_PCM_STATE_PREPARED
:
1598 case SNDRV_PCM_STATE_PAUSED
:
1599 case SNDRV_PCM_STATE_SUSPENDED
:
1606 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1608 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1609 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1612 runtime
->hw_ptr_base
= 0;
1613 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1614 runtime
->status
->hw_ptr
% runtime
->period_size
;
1615 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1616 runtime
->silence_filled
= 0;
1620 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1622 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1623 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1624 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1625 runtime
->silence_size
> 0)
1626 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1629 static const struct action_ops snd_pcm_action_reset
= {
1630 .pre_action
= snd_pcm_pre_reset
,
1631 .do_action
= snd_pcm_do_reset
,
1632 .post_action
= snd_pcm_post_reset
1635 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1637 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1643 /* we use the second argument for updating f_flags */
1644 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1647 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1648 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1649 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1651 if (snd_pcm_running(substream
))
1653 substream
->f_flags
= f_flags
;
1657 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1660 err
= substream
->ops
->prepare(substream
);
1663 return snd_pcm_do_reset(substream
, 0);
1666 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1668 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1669 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1670 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1673 static const struct action_ops snd_pcm_action_prepare
= {
1674 .pre_action
= snd_pcm_pre_prepare
,
1675 .do_action
= snd_pcm_do_prepare
,
1676 .post_action
= snd_pcm_post_prepare
1680 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1681 * @substream: the PCM substream instance
1682 * @file: file to refer f_flags
1684 * Return: Zero if successful, or a negative error code.
1686 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1692 f_flags
= file
->f_flags
;
1694 f_flags
= substream
->f_flags
;
1696 snd_pcm_stream_lock_irq(substream
);
1697 switch (substream
->runtime
->status
->state
) {
1698 case SNDRV_PCM_STATE_PAUSED
:
1699 snd_pcm_pause(substream
, 0);
1701 case SNDRV_PCM_STATE_SUSPENDED
:
1702 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1705 snd_pcm_stream_unlock_irq(substream
);
1707 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1708 substream
, f_flags
);
1715 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1717 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1718 switch (runtime
->status
->state
) {
1719 case SNDRV_PCM_STATE_OPEN
:
1720 case SNDRV_PCM_STATE_DISCONNECTED
:
1721 case SNDRV_PCM_STATE_SUSPENDED
:
1724 runtime
->trigger_master
= substream
;
1728 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1730 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1731 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1732 switch (runtime
->status
->state
) {
1733 case SNDRV_PCM_STATE_PREPARED
:
1734 /* start playback stream if possible */
1735 if (! snd_pcm_playback_empty(substream
)) {
1736 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1737 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1739 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1742 case SNDRV_PCM_STATE_RUNNING
:
1743 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1745 case SNDRV_PCM_STATE_XRUN
:
1746 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1752 /* stop running stream */
1753 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1754 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1755 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1756 snd_pcm_do_stop(substream
, new_state
);
1757 snd_pcm_post_stop(substream
, new_state
);
1761 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1762 runtime
->trigger_master
== substream
&&
1763 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1764 return substream
->ops
->trigger(substream
,
1765 SNDRV_PCM_TRIGGER_DRAIN
);
1770 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1774 static const struct action_ops snd_pcm_action_drain_init
= {
1775 .pre_action
= snd_pcm_pre_drain_init
,
1776 .do_action
= snd_pcm_do_drain_init
,
1777 .post_action
= snd_pcm_post_drain_init
1780 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1783 * Drain the stream(s).
1784 * When the substream is linked, sync until the draining of all playback streams
1786 * After this call, all streams are supposed to be either SETUP or DRAINING
1787 * (capture only) state.
1789 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1792 struct snd_card
*card
;
1793 struct snd_pcm_runtime
*runtime
;
1794 struct snd_pcm_substream
*s
;
1795 wait_queue_entry_t wait
;
1799 card
= substream
->pcm
->card
;
1800 runtime
= substream
->runtime
;
1802 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1806 if (file
->f_flags
& O_NONBLOCK
)
1808 } else if (substream
->f_flags
& O_NONBLOCK
)
1811 down_read(&snd_pcm_link_rwsem
);
1812 snd_pcm_stream_lock_irq(substream
);
1814 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1815 snd_pcm_pause(substream
, 0);
1817 /* pre-start/stop - all running streams are changed to DRAINING state */
1818 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1821 /* in non-blocking, we don't wait in ioctl but let caller poll */
1829 struct snd_pcm_runtime
*to_check
;
1830 if (signal_pending(current
)) {
1831 result
= -ERESTARTSYS
;
1834 /* find a substream to drain */
1836 snd_pcm_group_for_each_entry(s
, substream
) {
1837 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1839 runtime
= s
->runtime
;
1840 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1846 break; /* all drained */
1847 init_waitqueue_entry(&wait
, current
);
1848 add_wait_queue(&to_check
->sleep
, &wait
);
1849 snd_pcm_stream_unlock_irq(substream
);
1850 up_read(&snd_pcm_link_rwsem
);
1851 if (runtime
->no_period_wakeup
)
1852 tout
= MAX_SCHEDULE_TIMEOUT
;
1855 if (runtime
->rate
) {
1856 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1857 tout
= max(t
, tout
);
1859 tout
= msecs_to_jiffies(tout
* 1000);
1861 tout
= schedule_timeout_interruptible(tout
);
1862 down_read(&snd_pcm_link_rwsem
);
1863 snd_pcm_stream_lock_irq(substream
);
1864 remove_wait_queue(&to_check
->sleep
, &wait
);
1865 if (card
->shutdown
) {
1870 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1873 dev_dbg(substream
->pcm
->card
->dev
,
1874 "playback drain error (DMA or IRQ trouble?)\n");
1875 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1883 snd_pcm_stream_unlock_irq(substream
);
1884 up_read(&snd_pcm_link_rwsem
);
1892 * Immediately put all linked substreams into SETUP state.
1894 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1896 struct snd_pcm_runtime
*runtime
;
1899 if (PCM_RUNTIME_CHECK(substream
))
1901 runtime
= substream
->runtime
;
1903 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1904 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1907 snd_pcm_stream_lock_irq(substream
);
1909 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1910 snd_pcm_pause(substream
, 0);
1912 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1913 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1914 snd_pcm_stream_unlock_irq(substream
);
1920 static bool is_pcm_file(struct file
*file
)
1922 struct inode
*inode
= file_inode(file
);
1925 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1927 minor
= iminor(inode
);
1928 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1929 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1935 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1938 struct snd_pcm_file
*pcm_file
;
1939 struct snd_pcm_substream
*substream1
;
1940 struct snd_pcm_group
*group
;
1941 struct fd f
= fdget(fd
);
1945 if (!is_pcm_file(f
.file
)) {
1949 pcm_file
= f
.file
->private_data
;
1950 substream1
= pcm_file
->substream
;
1951 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1956 down_write_nonfifo(&snd_pcm_link_rwsem
);
1957 write_lock_irq(&snd_pcm_link_rwlock
);
1958 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1959 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1960 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1964 if (snd_pcm_stream_linked(substream1
)) {
1968 if (!snd_pcm_stream_linked(substream
)) {
1969 substream
->group
= group
;
1971 spin_lock_init(&substream
->group
->lock
);
1972 mutex_init(&substream
->group
->mutex
);
1973 INIT_LIST_HEAD(&substream
->group
->substreams
);
1974 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1975 substream
->group
->count
= 1;
1977 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1978 substream
->group
->count
++;
1979 substream1
->group
= substream
->group
;
1981 write_unlock_irq(&snd_pcm_link_rwlock
);
1982 up_write(&snd_pcm_link_rwsem
);
1984 snd_card_unref(substream1
->pcm
->card
);
1991 static void relink_to_local(struct snd_pcm_substream
*substream
)
1993 substream
->group
= &substream
->self_group
;
1994 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1995 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1998 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
2000 struct snd_pcm_substream
*s
;
2003 down_write_nonfifo(&snd_pcm_link_rwsem
);
2004 write_lock_irq(&snd_pcm_link_rwlock
);
2005 if (!snd_pcm_stream_linked(substream
)) {
2009 list_del(&substream
->link_list
);
2010 substream
->group
->count
--;
2011 if (substream
->group
->count
== 1) { /* detach the last stream, too */
2012 snd_pcm_group_for_each_entry(s
, substream
) {
2016 kfree(substream
->group
);
2018 relink_to_local(substream
);
2020 write_unlock_irq(&snd_pcm_link_rwlock
);
2021 up_write(&snd_pcm_link_rwsem
);
2028 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
2029 struct snd_pcm_hw_rule
*rule
)
2031 struct snd_interval t
;
2032 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
2033 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2034 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2037 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
2038 struct snd_pcm_hw_rule
*rule
)
2040 struct snd_interval t
;
2041 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
2042 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2043 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2046 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
2047 struct snd_pcm_hw_rule
*rule
)
2049 struct snd_interval t
;
2050 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
2051 hw_param_interval_c(params
, rule
->deps
[1]),
2052 (unsigned long) rule
->private, &t
);
2053 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2056 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
2057 struct snd_pcm_hw_rule
*rule
)
2059 struct snd_interval t
;
2060 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
2061 (unsigned long) rule
->private,
2062 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2063 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2066 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
2067 struct snd_pcm_hw_rule
*rule
)
2070 const struct snd_interval
*i
=
2071 hw_param_interval_c(params
, rule
->deps
[0]);
2073 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
2075 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2077 if (! snd_mask_test(mask
, k
))
2079 bits
= snd_pcm_format_physical_width(k
);
2081 continue; /* ignore invalid formats */
2082 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
2083 snd_mask_reset(&m
, k
);
2085 return snd_mask_refine(mask
, &m
);
2088 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
2089 struct snd_pcm_hw_rule
*rule
)
2091 struct snd_interval t
;
2097 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2099 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
2101 bits
= snd_pcm_format_physical_width(k
);
2103 continue; /* ignore invalid formats */
2104 if (t
.min
> (unsigned)bits
)
2106 if (t
.max
< (unsigned)bits
)
2110 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2113 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2114 #error "Change this table"
2117 static const unsigned int rates
[] = {
2118 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2119 48000, 64000, 88200, 96000, 176400, 192000
2122 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
2123 .count
= ARRAY_SIZE(rates
),
2127 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
2128 struct snd_pcm_hw_rule
*rule
)
2130 struct snd_pcm_hardware
*hw
= rule
->private;
2131 return snd_interval_list(hw_param_interval(params
, rule
->var
),
2132 snd_pcm_known_rates
.count
,
2133 snd_pcm_known_rates
.list
, hw
->rates
);
2136 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
2137 struct snd_pcm_hw_rule
*rule
)
2139 struct snd_interval t
;
2140 struct snd_pcm_substream
*substream
= rule
->private;
2142 t
.max
= substream
->buffer_bytes_max
;
2146 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2149 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2151 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2152 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2155 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2156 snd_mask_any(constrs_mask(constrs
, k
));
2159 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2160 snd_interval_any(constrs_interval(constrs
, k
));
2163 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2164 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2165 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2166 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2167 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2169 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2170 snd_pcm_hw_rule_format
, NULL
,
2171 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2174 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2175 snd_pcm_hw_rule_sample_bits
, NULL
,
2176 SNDRV_PCM_HW_PARAM_FORMAT
,
2177 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2180 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2181 snd_pcm_hw_rule_div
, NULL
,
2182 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2185 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2186 snd_pcm_hw_rule_mul
, NULL
,
2187 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2190 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2191 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2192 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2195 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2196 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2197 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2200 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2201 snd_pcm_hw_rule_div
, NULL
,
2202 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2205 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2206 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2207 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2210 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2211 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2212 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2215 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2216 snd_pcm_hw_rule_div
, NULL
,
2217 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2220 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2221 snd_pcm_hw_rule_div
, NULL
,
2222 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2225 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2226 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2227 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2230 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2231 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2232 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2235 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2236 snd_pcm_hw_rule_mul
, NULL
,
2237 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2240 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2241 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2242 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2245 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2246 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2247 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2250 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2251 snd_pcm_hw_rule_muldivk
, (void*) 8,
2252 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2255 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2256 snd_pcm_hw_rule_muldivk
, (void*) 8,
2257 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2260 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2261 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2262 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2265 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2266 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2267 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2273 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2275 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2276 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2278 unsigned int mask
= 0;
2280 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2281 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2282 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2283 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2284 if (hw_support_mmap(substream
)) {
2285 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2286 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2287 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2288 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2289 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2290 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2292 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2296 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2300 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2304 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2305 hw
->channels_min
, hw
->channels_max
);
2309 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2310 hw
->rate_min
, hw
->rate_max
);
2314 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2315 hw
->period_bytes_min
, hw
->period_bytes_max
);
2319 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2320 hw
->periods_min
, hw
->periods_max
);
2324 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2325 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2329 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2330 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2331 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2336 if (runtime
->dma_bytes
) {
2337 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2342 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2343 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2344 snd_pcm_hw_rule_rate
, hw
,
2345 SNDRV_PCM_HW_PARAM_RATE
, -1);
2350 /* FIXME: this belong to lowlevel */
2351 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2356 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2358 if (snd_pcm_stream_linked(substream
))
2359 snd_pcm_unlink(substream
);
2362 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2364 substream
->ref_count
--;
2365 if (substream
->ref_count
> 0)
2368 snd_pcm_drop(substream
);
2369 if (substream
->hw_opened
) {
2370 if (substream
->ops
->hw_free
&&
2371 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
2372 substream
->ops
->hw_free(substream
);
2373 substream
->ops
->close(substream
);
2374 substream
->hw_opened
= 0;
2376 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2377 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2378 if (substream
->pcm_release
) {
2379 substream
->pcm_release(substream
);
2380 substream
->pcm_release
= NULL
;
2382 snd_pcm_detach_substream(substream
);
2384 EXPORT_SYMBOL(snd_pcm_release_substream
);
2386 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2388 struct snd_pcm_substream
**rsubstream
)
2390 struct snd_pcm_substream
*substream
;
2393 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2396 if (substream
->ref_count
> 1) {
2397 *rsubstream
= substream
;
2401 err
= snd_pcm_hw_constraints_init(substream
);
2403 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2407 if ((err
= substream
->ops
->open(substream
)) < 0)
2410 substream
->hw_opened
= 1;
2412 err
= snd_pcm_hw_constraints_complete(substream
);
2414 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2418 *rsubstream
= substream
;
2422 snd_pcm_release_substream(substream
);
2425 EXPORT_SYMBOL(snd_pcm_open_substream
);
2427 static int snd_pcm_open_file(struct file
*file
,
2428 struct snd_pcm
*pcm
,
2431 struct snd_pcm_file
*pcm_file
;
2432 struct snd_pcm_substream
*substream
;
2435 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2439 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2440 if (pcm_file
== NULL
) {
2441 snd_pcm_release_substream(substream
);
2444 pcm_file
->substream
= substream
;
2445 if (substream
->ref_count
== 1) {
2446 substream
->file
= pcm_file
;
2447 substream
->pcm_release
= pcm_release_private
;
2449 file
->private_data
= pcm_file
;
2454 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2456 struct snd_pcm
*pcm
;
2457 int err
= nonseekable_open(inode
, file
);
2460 pcm
= snd_lookup_minor_data(iminor(inode
),
2461 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2462 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2464 snd_card_unref(pcm
->card
);
2468 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2470 struct snd_pcm
*pcm
;
2471 int err
= nonseekable_open(inode
, file
);
2474 pcm
= snd_lookup_minor_data(iminor(inode
),
2475 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2476 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2478 snd_card_unref(pcm
->card
);
2482 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2485 wait_queue_entry_t wait
;
2491 err
= snd_card_file_add(pcm
->card
, file
);
2494 if (!try_module_get(pcm
->card
->module
)) {
2498 init_waitqueue_entry(&wait
, current
);
2499 add_wait_queue(&pcm
->open_wait
, &wait
);
2500 mutex_lock(&pcm
->open_mutex
);
2502 err
= snd_pcm_open_file(file
, pcm
, stream
);
2505 if (err
== -EAGAIN
) {
2506 if (file
->f_flags
& O_NONBLOCK
) {
2512 set_current_state(TASK_INTERRUPTIBLE
);
2513 mutex_unlock(&pcm
->open_mutex
);
2515 mutex_lock(&pcm
->open_mutex
);
2516 if (pcm
->card
->shutdown
) {
2520 if (signal_pending(current
)) {
2525 remove_wait_queue(&pcm
->open_wait
, &wait
);
2526 mutex_unlock(&pcm
->open_mutex
);
2532 module_put(pcm
->card
->module
);
2534 snd_card_file_remove(pcm
->card
, file
);
2539 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2541 struct snd_pcm
*pcm
;
2542 struct snd_pcm_substream
*substream
;
2543 struct snd_pcm_file
*pcm_file
;
2545 pcm_file
= file
->private_data
;
2546 substream
= pcm_file
->substream
;
2547 if (snd_BUG_ON(!substream
))
2549 pcm
= substream
->pcm
;
2550 mutex_lock(&pcm
->open_mutex
);
2551 snd_pcm_release_substream(substream
);
2553 mutex_unlock(&pcm
->open_mutex
);
2554 wake_up(&pcm
->open_wait
);
2555 module_put(pcm
->card
->module
);
2556 snd_card_file_remove(pcm
->card
, file
);
2560 /* check and update PCM state; return 0 or a negative error
2561 * call this inside PCM lock
2563 static int do_pcm_hwsync(struct snd_pcm_substream
*substream
)
2565 switch (substream
->runtime
->status
->state
) {
2566 case SNDRV_PCM_STATE_DRAINING
:
2567 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2570 case SNDRV_PCM_STATE_RUNNING
:
2571 return snd_pcm_update_hw_ptr(substream
);
2572 case SNDRV_PCM_STATE_PREPARED
:
2573 case SNDRV_PCM_STATE_PAUSED
:
2575 case SNDRV_PCM_STATE_SUSPENDED
:
2577 case SNDRV_PCM_STATE_XRUN
:
2584 /* increase the appl_ptr; returns the processed frames or a negative error */
2585 static snd_pcm_sframes_t
forward_appl_ptr(struct snd_pcm_substream
*substream
,
2586 snd_pcm_uframes_t frames
,
2587 snd_pcm_sframes_t avail
)
2589 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2590 snd_pcm_sframes_t appl_ptr
;
2595 if (frames
> (snd_pcm_uframes_t
)avail
)
2597 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2598 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2599 appl_ptr
-= runtime
->boundary
;
2600 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2601 return ret
< 0 ? ret
: frames
;
2604 /* decrease the appl_ptr; returns the processed frames or zero for error */
2605 static snd_pcm_sframes_t
rewind_appl_ptr(struct snd_pcm_substream
*substream
,
2606 snd_pcm_uframes_t frames
,
2607 snd_pcm_sframes_t avail
)
2609 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2610 snd_pcm_sframes_t appl_ptr
;
2615 if (frames
> (snd_pcm_uframes_t
)avail
)
2617 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2619 appl_ptr
+= runtime
->boundary
;
2620 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2621 /* NOTE: we return zero for errors because PulseAudio gets depressed
2622 * upon receiving an error from rewind ioctl and stops processing
2623 * any longer. Returning zero means that no rewind is done, so
2624 * it's not absolutely wrong to answer like that.
2626 return ret
< 0 ? 0 : frames
;
2629 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2630 snd_pcm_uframes_t frames
)
2632 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2633 snd_pcm_sframes_t ret
;
2638 snd_pcm_stream_lock_irq(substream
);
2639 ret
= do_pcm_hwsync(substream
);
2641 ret
= rewind_appl_ptr(substream
, frames
,
2642 snd_pcm_playback_hw_avail(runtime
));
2643 snd_pcm_stream_unlock_irq(substream
);
2647 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2648 snd_pcm_uframes_t frames
)
2650 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2651 snd_pcm_sframes_t ret
;
2656 snd_pcm_stream_lock_irq(substream
);
2657 ret
= do_pcm_hwsync(substream
);
2659 ret
= rewind_appl_ptr(substream
, frames
,
2660 snd_pcm_capture_hw_avail(runtime
));
2661 snd_pcm_stream_unlock_irq(substream
);
2665 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2666 snd_pcm_uframes_t frames
)
2668 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2669 snd_pcm_sframes_t ret
;
2674 snd_pcm_stream_lock_irq(substream
);
2675 ret
= do_pcm_hwsync(substream
);
2677 ret
= forward_appl_ptr(substream
, frames
,
2678 snd_pcm_playback_avail(runtime
));
2679 snd_pcm_stream_unlock_irq(substream
);
2683 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2684 snd_pcm_uframes_t frames
)
2686 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2687 snd_pcm_sframes_t ret
;
2692 snd_pcm_stream_lock_irq(substream
);
2693 ret
= do_pcm_hwsync(substream
);
2695 ret
= forward_appl_ptr(substream
, frames
,
2696 snd_pcm_capture_avail(runtime
));
2697 snd_pcm_stream_unlock_irq(substream
);
2701 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2705 snd_pcm_stream_lock_irq(substream
);
2706 err
= do_pcm_hwsync(substream
);
2707 snd_pcm_stream_unlock_irq(substream
);
2711 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2712 snd_pcm_sframes_t
*delay
)
2714 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2716 snd_pcm_sframes_t n
= 0;
2718 snd_pcm_stream_lock_irq(substream
);
2719 err
= do_pcm_hwsync(substream
);
2721 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2722 n
= snd_pcm_playback_hw_avail(runtime
);
2724 n
= snd_pcm_capture_avail(runtime
);
2725 n
+= runtime
->delay
;
2727 snd_pcm_stream_unlock_irq(substream
);
2733 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2734 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2736 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2737 struct snd_pcm_sync_ptr sync_ptr
;
2738 volatile struct snd_pcm_mmap_status
*status
;
2739 volatile struct snd_pcm_mmap_control
*control
;
2742 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2743 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2745 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2747 status
= runtime
->status
;
2748 control
= runtime
->control
;
2749 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2750 err
= snd_pcm_hwsync(substream
);
2754 snd_pcm_stream_lock_irq(substream
);
2755 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
)) {
2756 err
= pcm_lib_apply_appl_ptr(substream
,
2757 sync_ptr
.c
.control
.appl_ptr
);
2759 snd_pcm_stream_unlock_irq(substream
);
2763 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2765 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2766 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2768 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2769 sync_ptr
.s
.status
.state
= status
->state
;
2770 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2771 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2772 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2773 sync_ptr
.s
.status
.audio_tstamp
= status
->audio_tstamp
;
2774 snd_pcm_stream_unlock_irq(substream
);
2775 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2780 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2782 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2785 if (get_user(arg
, _arg
))
2787 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2789 runtime
->tstamp_type
= arg
;
2793 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream
*substream
,
2794 struct snd_xferi __user
*_xferi
)
2796 struct snd_xferi xferi
;
2797 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2798 snd_pcm_sframes_t result
;
2800 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2802 if (put_user(0, &_xferi
->result
))
2804 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2806 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2807 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2809 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2810 __put_user(result
, &_xferi
->result
);
2811 return result
< 0 ? result
: 0;
2814 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream
*substream
,
2815 struct snd_xfern __user
*_xfern
)
2817 struct snd_xfern xfern
;
2818 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2820 snd_pcm_sframes_t result
;
2822 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2824 if (runtime
->channels
> 128)
2826 if (put_user(0, &_xfern
->result
))
2828 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2831 bufs
= memdup_user(xfern
.bufs
, sizeof(void *) * runtime
->channels
);
2833 return PTR_ERR(bufs
);
2834 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2835 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2837 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2839 __put_user(result
, &_xfern
->result
);
2840 return result
< 0 ? result
: 0;
2843 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream
*substream
,
2844 snd_pcm_uframes_t __user
*_frames
)
2846 snd_pcm_uframes_t frames
;
2847 snd_pcm_sframes_t result
;
2849 if (get_user(frames
, _frames
))
2851 if (put_user(0, _frames
))
2853 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2854 result
= snd_pcm_playback_rewind(substream
, frames
);
2856 result
= snd_pcm_capture_rewind(substream
, frames
);
2857 __put_user(result
, _frames
);
2858 return result
< 0 ? result
: 0;
2861 static int snd_pcm_forward_ioctl(struct snd_pcm_substream
*substream
,
2862 snd_pcm_uframes_t __user
*_frames
)
2864 snd_pcm_uframes_t frames
;
2865 snd_pcm_sframes_t result
;
2867 if (get_user(frames
, _frames
))
2869 if (put_user(0, _frames
))
2871 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2872 result
= snd_pcm_playback_forward(substream
, frames
);
2874 result
= snd_pcm_capture_forward(substream
, frames
);
2875 __put_user(result
, _frames
);
2876 return result
< 0 ? result
: 0;
2879 static int snd_pcm_common_ioctl(struct file
*file
,
2880 struct snd_pcm_substream
*substream
,
2881 unsigned int cmd
, void __user
*arg
)
2883 struct snd_pcm_file
*pcm_file
= file
->private_data
;
2886 if (PCM_RUNTIME_CHECK(substream
))
2889 res
= snd_power_wait(substream
->pcm
->card
, SNDRV_CTL_POWER_D0
);
2894 case SNDRV_PCM_IOCTL_PVERSION
:
2895 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2896 case SNDRV_PCM_IOCTL_INFO
:
2897 return snd_pcm_info_user(substream
, arg
);
2898 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2900 case SNDRV_PCM_IOCTL_TTSTAMP
:
2901 return snd_pcm_tstamp(substream
, arg
);
2902 case SNDRV_PCM_IOCTL_USER_PVERSION
:
2903 if (get_user(pcm_file
->user_pversion
,
2904 (unsigned int __user
*)arg
))
2907 case SNDRV_PCM_IOCTL_HW_REFINE
:
2908 return snd_pcm_hw_refine_user(substream
, arg
);
2909 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2910 return snd_pcm_hw_params_user(substream
, arg
);
2911 case SNDRV_PCM_IOCTL_HW_FREE
:
2912 return snd_pcm_hw_free(substream
);
2913 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2914 return snd_pcm_sw_params_user(substream
, arg
);
2915 case SNDRV_PCM_IOCTL_STATUS
:
2916 return snd_pcm_status_user(substream
, arg
, false);
2917 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2918 return snd_pcm_status_user(substream
, arg
, true);
2919 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2920 return snd_pcm_channel_info_user(substream
, arg
);
2921 case SNDRV_PCM_IOCTL_PREPARE
:
2922 return snd_pcm_prepare(substream
, file
);
2923 case SNDRV_PCM_IOCTL_RESET
:
2924 return snd_pcm_reset(substream
);
2925 case SNDRV_PCM_IOCTL_START
:
2926 return snd_pcm_start_lock_irq(substream
);
2927 case SNDRV_PCM_IOCTL_LINK
:
2928 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2929 case SNDRV_PCM_IOCTL_UNLINK
:
2930 return snd_pcm_unlink(substream
);
2931 case SNDRV_PCM_IOCTL_RESUME
:
2932 return snd_pcm_resume(substream
);
2933 case SNDRV_PCM_IOCTL_XRUN
:
2934 return snd_pcm_xrun(substream
);
2935 case SNDRV_PCM_IOCTL_HWSYNC
:
2936 return snd_pcm_hwsync(substream
);
2937 case SNDRV_PCM_IOCTL_DELAY
:
2939 snd_pcm_sframes_t delay
;
2940 snd_pcm_sframes_t __user
*res
= arg
;
2943 err
= snd_pcm_delay(substream
, &delay
);
2946 if (put_user(delay
, res
))
2950 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2951 return snd_pcm_sync_ptr(substream
, arg
);
2952 #ifdef CONFIG_SND_SUPPORT_OLD_API
2953 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2954 return snd_pcm_hw_refine_old_user(substream
, arg
);
2955 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2956 return snd_pcm_hw_params_old_user(substream
, arg
);
2958 case SNDRV_PCM_IOCTL_DRAIN
:
2959 return snd_pcm_drain(substream
, file
);
2960 case SNDRV_PCM_IOCTL_DROP
:
2961 return snd_pcm_drop(substream
);
2962 case SNDRV_PCM_IOCTL_PAUSE
:
2963 return snd_pcm_action_lock_irq(&snd_pcm_action_pause
,
2965 (int)(unsigned long)arg
);
2966 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2967 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2968 return snd_pcm_xferi_frames_ioctl(substream
, arg
);
2969 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2970 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2971 return snd_pcm_xfern_frames_ioctl(substream
, arg
);
2972 case SNDRV_PCM_IOCTL_REWIND
:
2973 return snd_pcm_rewind_ioctl(substream
, arg
);
2974 case SNDRV_PCM_IOCTL_FORWARD
:
2975 return snd_pcm_forward_ioctl(substream
, arg
);
2977 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2981 static long snd_pcm_ioctl(struct file
*file
, unsigned int cmd
,
2984 struct snd_pcm_file
*pcm_file
;
2986 pcm_file
= file
->private_data
;
2988 if (((cmd
>> 8) & 0xff) != 'A')
2991 return snd_pcm_common_ioctl(file
, pcm_file
->substream
, cmd
,
2992 (void __user
*)arg
);
2996 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2997 * @substream: PCM substream
2999 * @arg: IOCTL argument
3001 * The function is provided primarily for OSS layer and USB gadget drivers,
3002 * and it allows only the limited set of ioctls (hw_params, sw_params,
3003 * prepare, start, drain, drop, forward).
3005 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
3006 unsigned int cmd
, void *arg
)
3008 snd_pcm_uframes_t
*frames
= arg
;
3009 snd_pcm_sframes_t result
;
3012 case SNDRV_PCM_IOCTL_FORWARD
:
3014 /* provided only for OSS; capture-only and no value returned */
3015 if (substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
)
3017 result
= snd_pcm_capture_forward(substream
, *frames
);
3018 return result
< 0 ? result
: 0;
3020 case SNDRV_PCM_IOCTL_HW_PARAMS
:
3021 return snd_pcm_hw_params(substream
, arg
);
3022 case SNDRV_PCM_IOCTL_SW_PARAMS
:
3023 return snd_pcm_sw_params(substream
, arg
);
3024 case SNDRV_PCM_IOCTL_PREPARE
:
3025 return snd_pcm_prepare(substream
, NULL
);
3026 case SNDRV_PCM_IOCTL_START
:
3027 return snd_pcm_start_lock_irq(substream
);
3028 case SNDRV_PCM_IOCTL_DRAIN
:
3029 return snd_pcm_drain(substream
, NULL
);
3030 case SNDRV_PCM_IOCTL_DROP
:
3031 return snd_pcm_drop(substream
);
3032 case SNDRV_PCM_IOCTL_DELAY
:
3033 return snd_pcm_delay(substream
, frames
);
3038 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3040 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
3043 struct snd_pcm_file
*pcm_file
;
3044 struct snd_pcm_substream
*substream
;
3045 struct snd_pcm_runtime
*runtime
;
3046 snd_pcm_sframes_t result
;
3048 pcm_file
= file
->private_data
;
3049 substream
= pcm_file
->substream
;
3050 if (PCM_RUNTIME_CHECK(substream
))
3052 runtime
= substream
->runtime
;
3053 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3055 if (!frame_aligned(runtime
, count
))
3057 count
= bytes_to_frames(runtime
, count
);
3058 result
= snd_pcm_lib_read(substream
, buf
, count
);
3060 result
= frames_to_bytes(runtime
, result
);
3064 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3065 size_t count
, loff_t
* offset
)
3067 struct snd_pcm_file
*pcm_file
;
3068 struct snd_pcm_substream
*substream
;
3069 struct snd_pcm_runtime
*runtime
;
3070 snd_pcm_sframes_t result
;
3072 pcm_file
= file
->private_data
;
3073 substream
= pcm_file
->substream
;
3074 if (PCM_RUNTIME_CHECK(substream
))
3076 runtime
= substream
->runtime
;
3077 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3079 if (!frame_aligned(runtime
, count
))
3081 count
= bytes_to_frames(runtime
, count
);
3082 result
= snd_pcm_lib_write(substream
, buf
, count
);
3084 result
= frames_to_bytes(runtime
, result
);
3088 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3090 struct snd_pcm_file
*pcm_file
;
3091 struct snd_pcm_substream
*substream
;
3092 struct snd_pcm_runtime
*runtime
;
3093 snd_pcm_sframes_t result
;
3096 snd_pcm_uframes_t frames
;
3098 pcm_file
= iocb
->ki_filp
->private_data
;
3099 substream
= pcm_file
->substream
;
3100 if (PCM_RUNTIME_CHECK(substream
))
3102 runtime
= substream
->runtime
;
3103 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3105 if (!iter_is_iovec(to
))
3107 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3109 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3111 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3112 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3115 for (i
= 0; i
< to
->nr_segs
; ++i
)
3116 bufs
[i
] = to
->iov
[i
].iov_base
;
3117 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3119 result
= frames_to_bytes(runtime
, result
);
3124 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3126 struct snd_pcm_file
*pcm_file
;
3127 struct snd_pcm_substream
*substream
;
3128 struct snd_pcm_runtime
*runtime
;
3129 snd_pcm_sframes_t result
;
3132 snd_pcm_uframes_t frames
;
3134 pcm_file
= iocb
->ki_filp
->private_data
;
3135 substream
= pcm_file
->substream
;
3136 if (PCM_RUNTIME_CHECK(substream
))
3138 runtime
= substream
->runtime
;
3139 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3141 if (!iter_is_iovec(from
))
3143 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3144 !frame_aligned(runtime
, from
->iov
->iov_len
))
3146 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3147 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3150 for (i
= 0; i
< from
->nr_segs
; ++i
)
3151 bufs
[i
] = from
->iov
[i
].iov_base
;
3152 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3154 result
= frames_to_bytes(runtime
, result
);
3159 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3161 struct snd_pcm_file
*pcm_file
;
3162 struct snd_pcm_substream
*substream
;
3163 struct snd_pcm_runtime
*runtime
;
3165 snd_pcm_uframes_t avail
;
3167 pcm_file
= file
->private_data
;
3169 substream
= pcm_file
->substream
;
3170 if (PCM_RUNTIME_CHECK(substream
))
3171 return POLLOUT
| POLLWRNORM
| POLLERR
;
3172 runtime
= substream
->runtime
;
3174 poll_wait(file
, &runtime
->sleep
, wait
);
3176 snd_pcm_stream_lock_irq(substream
);
3177 avail
= snd_pcm_playback_avail(runtime
);
3178 switch (runtime
->status
->state
) {
3179 case SNDRV_PCM_STATE_RUNNING
:
3180 case SNDRV_PCM_STATE_PREPARED
:
3181 case SNDRV_PCM_STATE_PAUSED
:
3182 if (avail
>= runtime
->control
->avail_min
) {
3183 mask
= POLLOUT
| POLLWRNORM
;
3187 case SNDRV_PCM_STATE_DRAINING
:
3191 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3194 snd_pcm_stream_unlock_irq(substream
);
3198 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3200 struct snd_pcm_file
*pcm_file
;
3201 struct snd_pcm_substream
*substream
;
3202 struct snd_pcm_runtime
*runtime
;
3204 snd_pcm_uframes_t avail
;
3206 pcm_file
= file
->private_data
;
3208 substream
= pcm_file
->substream
;
3209 if (PCM_RUNTIME_CHECK(substream
))
3210 return POLLIN
| POLLRDNORM
| POLLERR
;
3211 runtime
= substream
->runtime
;
3213 poll_wait(file
, &runtime
->sleep
, wait
);
3215 snd_pcm_stream_lock_irq(substream
);
3216 avail
= snd_pcm_capture_avail(runtime
);
3217 switch (runtime
->status
->state
) {
3218 case SNDRV_PCM_STATE_RUNNING
:
3219 case SNDRV_PCM_STATE_PREPARED
:
3220 case SNDRV_PCM_STATE_PAUSED
:
3221 if (avail
>= runtime
->control
->avail_min
) {
3222 mask
= POLLIN
| POLLRDNORM
;
3227 case SNDRV_PCM_STATE_DRAINING
:
3229 mask
= POLLIN
| POLLRDNORM
;
3234 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3237 snd_pcm_stream_unlock_irq(substream
);
3246 * Only on coherent architectures, we can mmap the status and the control records
3247 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3249 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3251 * mmap status record
3253 static int snd_pcm_mmap_status_fault(struct vm_fault
*vmf
)
3255 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3256 struct snd_pcm_runtime
*runtime
;
3258 if (substream
== NULL
)
3259 return VM_FAULT_SIGBUS
;
3260 runtime
= substream
->runtime
;
3261 vmf
->page
= virt_to_page(runtime
->status
);
3262 get_page(vmf
->page
);
3266 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3268 .fault
= snd_pcm_mmap_status_fault
,
3271 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3272 struct vm_area_struct
*area
)
3275 if (!(area
->vm_flags
& VM_READ
))
3277 size
= area
->vm_end
- area
->vm_start
;
3278 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3280 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3281 area
->vm_private_data
= substream
;
3282 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3287 * mmap control record
3289 static int snd_pcm_mmap_control_fault(struct vm_fault
*vmf
)
3291 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3292 struct snd_pcm_runtime
*runtime
;
3294 if (substream
== NULL
)
3295 return VM_FAULT_SIGBUS
;
3296 runtime
= substream
->runtime
;
3297 vmf
->page
= virt_to_page(runtime
->control
);
3298 get_page(vmf
->page
);
3302 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3304 .fault
= snd_pcm_mmap_control_fault
,
3307 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3308 struct vm_area_struct
*area
)
3311 if (!(area
->vm_flags
& VM_READ
))
3313 size
= area
->vm_end
- area
->vm_start
;
3314 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3316 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3317 area
->vm_private_data
= substream
;
3318 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3322 static bool pcm_status_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3324 if (pcm_file
->no_compat_mmap
)
3326 /* See pcm_control_mmap_allowed() below.
3327 * Since older alsa-lib requires both status and control mmaps to be
3328 * coupled, we have to disable the status mmap for old alsa-lib, too.
3330 if (pcm_file
->user_pversion
< SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3331 (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
))
3336 static bool pcm_control_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3338 if (pcm_file
->no_compat_mmap
)
3340 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3341 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3342 * thus it effectively assures the manual update of appl_ptr.
3344 if (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
)
3349 #else /* ! coherent mmap */
3351 * don't support mmap for status and control records.
3353 #define pcm_status_mmap_allowed(pcm_file) false
3354 #define pcm_control_mmap_allowed(pcm_file) false
3356 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3357 struct vm_area_struct
*area
)
3361 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3362 struct vm_area_struct
*area
)
3366 #endif /* coherent mmap */
3368 static inline struct page
*
3369 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3371 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3372 return virt_to_page(vaddr
);
3376 * fault callback for mmapping a RAM page
3378 static int snd_pcm_mmap_data_fault(struct vm_fault
*vmf
)
3380 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3381 struct snd_pcm_runtime
*runtime
;
3382 unsigned long offset
;
3386 if (substream
== NULL
)
3387 return VM_FAULT_SIGBUS
;
3388 runtime
= substream
->runtime
;
3389 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3390 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3391 if (offset
> dma_bytes
- PAGE_SIZE
)
3392 return VM_FAULT_SIGBUS
;
3393 if (substream
->ops
->page
)
3394 page
= substream
->ops
->page(substream
, offset
);
3396 page
= snd_pcm_default_page_ops(substream
, offset
);
3398 return VM_FAULT_SIGBUS
;
3404 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3405 .open
= snd_pcm_mmap_data_open
,
3406 .close
= snd_pcm_mmap_data_close
,
3409 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3410 .open
= snd_pcm_mmap_data_open
,
3411 .close
= snd_pcm_mmap_data_close
,
3412 .fault
= snd_pcm_mmap_data_fault
,
3416 * mmap the DMA buffer on RAM
3420 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3421 * @substream: PCM substream
3424 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3425 * this function is invoked implicitly.
3427 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3428 struct vm_area_struct
*area
)
3430 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3431 #ifdef CONFIG_GENERIC_ALLOCATOR
3432 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3433 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3434 return remap_pfn_range(area
, area
->vm_start
,
3435 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3436 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3438 #endif /* CONFIG_GENERIC_ALLOCATOR */
3439 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3440 if (IS_ENABLED(CONFIG_HAS_DMA
) && !substream
->ops
->page
&&
3441 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3442 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3444 substream
->runtime
->dma_area
,
3445 substream
->runtime
->dma_addr
,
3446 substream
->runtime
->dma_bytes
);
3447 #endif /* CONFIG_X86 */
3448 /* mmap with fault handler */
3449 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3452 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3455 * mmap the DMA buffer on I/O memory area
3457 #if SNDRV_PCM_INFO_MMAP_IOMEM
3459 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3460 * @substream: PCM substream
3463 * When your hardware uses the iomapped pages as the hardware buffer and
3464 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3465 * is supposed to work only on limited architectures.
3467 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3468 struct vm_area_struct
*area
)
3470 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3472 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3473 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3475 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3476 #endif /* SNDRV_PCM_INFO_MMAP */
3481 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3482 struct vm_area_struct
*area
)
3484 struct snd_pcm_runtime
*runtime
;
3486 unsigned long offset
;
3490 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3491 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3494 if (!(area
->vm_flags
& VM_READ
))
3497 runtime
= substream
->runtime
;
3498 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3500 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3502 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3503 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3505 size
= area
->vm_end
- area
->vm_start
;
3506 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3507 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3508 if ((size_t)size
> dma_bytes
)
3510 if (offset
> dma_bytes
- size
)
3513 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3514 area
->vm_private_data
= substream
;
3515 if (substream
->ops
->mmap
)
3516 err
= substream
->ops
->mmap(substream
, area
);
3518 err
= snd_pcm_lib_default_mmap(substream
, area
);
3520 atomic_inc(&substream
->mmap_count
);
3523 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3525 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3527 struct snd_pcm_file
* pcm_file
;
3528 struct snd_pcm_substream
*substream
;
3529 unsigned long offset
;
3531 pcm_file
= file
->private_data
;
3532 substream
= pcm_file
->substream
;
3533 if (PCM_RUNTIME_CHECK(substream
))
3536 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3538 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3539 if (!pcm_status_mmap_allowed(pcm_file
))
3541 return snd_pcm_mmap_status(substream
, file
, area
);
3542 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3543 if (!pcm_control_mmap_allowed(pcm_file
))
3545 return snd_pcm_mmap_control(substream
, file
, area
);
3547 return snd_pcm_mmap_data(substream
, file
, area
);
3552 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3554 struct snd_pcm_file
* pcm_file
;
3555 struct snd_pcm_substream
*substream
;
3556 struct snd_pcm_runtime
*runtime
;
3558 pcm_file
= file
->private_data
;
3559 substream
= pcm_file
->substream
;
3560 if (PCM_RUNTIME_CHECK(substream
))
3562 runtime
= substream
->runtime
;
3563 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3569 #ifdef CONFIG_COMPAT
3570 #include "pcm_compat.c"
3572 #define snd_pcm_ioctl_compat NULL
3576 * To be removed helpers to keep binary compatibility
3579 #ifdef CONFIG_SND_SUPPORT_OLD_API
3580 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3581 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3583 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3584 struct snd_pcm_hw_params_old
*oparams
)
3588 memset(params
, 0, sizeof(*params
));
3589 params
->flags
= oparams
->flags
;
3590 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3591 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3592 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3593 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3594 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3595 params
->info
= oparams
->info
;
3596 params
->msbits
= oparams
->msbits
;
3597 params
->rate_num
= oparams
->rate_num
;
3598 params
->rate_den
= oparams
->rate_den
;
3599 params
->fifo_size
= oparams
->fifo_size
;
3602 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3603 struct snd_pcm_hw_params
*params
)
3607 memset(oparams
, 0, sizeof(*oparams
));
3608 oparams
->flags
= params
->flags
;
3609 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3610 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3611 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3612 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3613 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3614 oparams
->info
= params
->info
;
3615 oparams
->msbits
= params
->msbits
;
3616 oparams
->rate_num
= params
->rate_num
;
3617 oparams
->rate_den
= params
->rate_den
;
3618 oparams
->fifo_size
= params
->fifo_size
;
3621 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3622 struct snd_pcm_hw_params_old __user
* _oparams
)
3624 struct snd_pcm_hw_params
*params
;
3625 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3628 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3632 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3633 if (IS_ERR(oparams
)) {
3634 err
= PTR_ERR(oparams
);
3637 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3638 err
= snd_pcm_hw_refine(substream
, params
);
3642 err
= fixup_unreferenced_params(substream
, params
);
3646 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3647 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3656 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3657 struct snd_pcm_hw_params_old __user
* _oparams
)
3659 struct snd_pcm_hw_params
*params
;
3660 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3663 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3667 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3668 if (IS_ERR(oparams
)) {
3669 err
= PTR_ERR(oparams
);
3673 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3674 err
= snd_pcm_hw_params(substream
, params
);
3678 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3679 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3687 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3690 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3693 unsigned long pgoff
,
3694 unsigned long flags
)
3696 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3697 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3698 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3699 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3702 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3703 return (unsigned long)runtime
->status
;
3704 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3705 return (unsigned long)runtime
->control
;
3707 return (unsigned long)runtime
->dma_area
+ offset
;
3711 # define snd_pcm_get_unmapped_area NULL
3718 const struct file_operations snd_pcm_f_ops
[2] = {
3720 .owner
= THIS_MODULE
,
3721 .write
= snd_pcm_write
,
3722 .write_iter
= snd_pcm_writev
,
3723 .open
= snd_pcm_playback_open
,
3724 .release
= snd_pcm_release
,
3725 .llseek
= no_llseek
,
3726 .poll
= snd_pcm_playback_poll
,
3727 .unlocked_ioctl
= snd_pcm_ioctl
,
3728 .compat_ioctl
= snd_pcm_ioctl_compat
,
3729 .mmap
= snd_pcm_mmap
,
3730 .fasync
= snd_pcm_fasync
,
3731 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3734 .owner
= THIS_MODULE
,
3735 .read
= snd_pcm_read
,
3736 .read_iter
= snd_pcm_readv
,
3737 .open
= snd_pcm_capture_open
,
3738 .release
= snd_pcm_release
,
3739 .llseek
= no_llseek
,
3740 .poll
= snd_pcm_capture_poll
,
3741 .unlocked_ioctl
= snd_pcm_ioctl
,
3742 .compat_ioctl
= snd_pcm_ioctl_compat
,
3743 .mmap
= snd_pcm_mmap
,
3744 .fasync
= snd_pcm_fasync
,
3745 .get_unmapped_area
= snd_pcm_get_unmapped_area
,