2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.h>
40 #include "pcm_local.h"
42 #ifdef CONFIG_SND_DEBUG
43 #define CREATE_TRACE_POINTS
44 #include "pcm_param_trace.h"
46 #define trace_hw_mask_param_enabled() 0
47 #define trace_hw_interval_param_enabled() 0
48 #define trace_hw_mask_param(substream, type, index, prev, curr)
49 #define trace_hw_interval_param(substream, type, index, prev, curr)
56 struct snd_pcm_hw_params_old
{
58 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
59 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
60 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
61 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
66 unsigned int rate_num
;
67 unsigned int rate_den
;
68 snd_pcm_uframes_t fifo_size
;
69 unsigned char reserved
[64];
72 #ifdef CONFIG_SND_SUPPORT_OLD_API
73 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
74 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
76 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
77 struct snd_pcm_hw_params_old __user
* _oparams
);
78 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
79 struct snd_pcm_hw_params_old __user
* _oparams
);
81 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
87 static DEFINE_RWLOCK(snd_pcm_link_rwlock
);
88 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
90 /* Writer in rwsem may block readers even during its waiting in queue,
91 * and this may lead to a deadlock when the code path takes read sem
92 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
93 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
94 * spin until it gets the lock.
96 static inline void down_write_nonblock(struct rw_semaphore
*lock
)
98 while (!down_write_trylock(lock
))
103 * snd_pcm_stream_lock - Lock the PCM stream
104 * @substream: PCM substream
106 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
107 * flag of the given substream. This also takes the global link rw lock
108 * (or rw sem), too, for avoiding the race with linked streams.
110 void snd_pcm_stream_lock(struct snd_pcm_substream
*substream
)
112 if (substream
->pcm
->nonatomic
) {
113 down_read_nested(&snd_pcm_link_rwsem
, SINGLE_DEPTH_NESTING
);
114 mutex_lock(&substream
->self_group
.mutex
);
116 read_lock(&snd_pcm_link_rwlock
);
117 spin_lock(&substream
->self_group
.lock
);
120 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock
);
123 * snd_pcm_stream_lock - Unlock the PCM stream
124 * @substream: PCM substream
126 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
128 void snd_pcm_stream_unlock(struct snd_pcm_substream
*substream
)
130 if (substream
->pcm
->nonatomic
) {
131 mutex_unlock(&substream
->self_group
.mutex
);
132 up_read(&snd_pcm_link_rwsem
);
134 spin_unlock(&substream
->self_group
.lock
);
135 read_unlock(&snd_pcm_link_rwlock
);
138 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock
);
141 * snd_pcm_stream_lock_irq - Lock the PCM stream
142 * @substream: PCM substream
144 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
145 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
146 * as snd_pcm_stream_lock().
148 void snd_pcm_stream_lock_irq(struct snd_pcm_substream
*substream
)
150 if (!substream
->pcm
->nonatomic
)
152 snd_pcm_stream_lock(substream
);
154 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq
);
157 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
158 * @substream: PCM substream
160 * This is a counter-part of snd_pcm_stream_lock_irq().
162 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream
*substream
)
164 snd_pcm_stream_unlock(substream
);
165 if (!substream
->pcm
->nonatomic
)
168 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq
);
170 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream
*substream
)
172 unsigned long flags
= 0;
173 if (!substream
->pcm
->nonatomic
)
174 local_irq_save(flags
);
175 snd_pcm_stream_lock(substream
);
178 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave
);
181 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
182 * @substream: PCM substream
185 * This is a counter-part of snd_pcm_stream_lock_irqsave().
187 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream
*substream
,
190 snd_pcm_stream_unlock(substream
);
191 if (!substream
->pcm
->nonatomic
)
192 local_irq_restore(flags
);
194 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore
);
196 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
198 struct snd_pcm
*pcm
= substream
->pcm
;
199 struct snd_pcm_str
*pstr
= substream
->pstr
;
201 memset(info
, 0, sizeof(*info
));
202 info
->card
= pcm
->card
->number
;
203 info
->device
= pcm
->device
;
204 info
->stream
= substream
->stream
;
205 info
->subdevice
= substream
->number
;
206 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
207 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
208 info
->dev_class
= pcm
->dev_class
;
209 info
->dev_subclass
= pcm
->dev_subclass
;
210 info
->subdevices_count
= pstr
->substream_count
;
211 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
212 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
217 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
218 struct snd_pcm_info __user
* _info
)
220 struct snd_pcm_info
*info
;
223 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
226 err
= snd_pcm_info(substream
, info
);
228 if (copy_to_user(_info
, info
, sizeof(*info
)))
235 static bool hw_support_mmap(struct snd_pcm_substream
*substream
)
237 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_MMAP
))
239 /* architecture supports dma_mmap_coherent()? */
240 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
241 if (!substream
->ops
->mmap
&&
242 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
248 static int constrain_mask_params(struct snd_pcm_substream
*substream
,
249 struct snd_pcm_hw_params
*params
)
251 struct snd_pcm_hw_constraints
*constrs
=
252 &substream
->runtime
->hw_constraints
;
255 struct snd_mask old_mask
;
258 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
259 m
= hw_param_mask(params
, k
);
260 if (snd_mask_empty(m
))
263 /* This parameter is not requested to change by a caller. */
264 if (!(params
->rmask
& (1 << k
)))
267 if (trace_hw_mask_param_enabled())
270 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
276 /* Set corresponding flag so that the caller gets it. */
277 trace_hw_mask_param(substream
, k
, 0, &old_mask
, m
);
278 params
->cmask
|= 1 << k
;
284 static int constrain_interval_params(struct snd_pcm_substream
*substream
,
285 struct snd_pcm_hw_params
*params
)
287 struct snd_pcm_hw_constraints
*constrs
=
288 &substream
->runtime
->hw_constraints
;
289 struct snd_interval
*i
;
291 struct snd_interval old_interval
;
294 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
295 i
= hw_param_interval(params
, k
);
296 if (snd_interval_empty(i
))
299 /* This parameter is not requested to change by a caller. */
300 if (!(params
->rmask
& (1 << k
)))
303 if (trace_hw_interval_param_enabled())
306 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
312 /* Set corresponding flag so that the caller gets it. */
313 trace_hw_interval_param(substream
, k
, 0, &old_interval
, i
);
314 params
->cmask
|= 1 << k
;
320 static int constrain_params_by_rules(struct snd_pcm_substream
*substream
,
321 struct snd_pcm_hw_params
*params
)
323 struct snd_pcm_hw_constraints
*constrs
=
324 &substream
->runtime
->hw_constraints
;
326 unsigned int rstamps
[constrs
->rules_num
];
327 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
329 struct snd_pcm_hw_rule
*r
;
331 struct snd_mask old_mask
;
332 struct snd_interval old_interval
;
337 * Each application of rule has own sequence number.
339 * Each member of 'rstamps' array represents the sequence number of
340 * recent application of corresponding rule.
342 for (k
= 0; k
< constrs
->rules_num
; k
++)
346 * Each member of 'vstamps' array represents the sequence number of
347 * recent application of rule in which corresponding parameters were
350 * In initial state, elements corresponding to parameters requested by
351 * a caller is 1. For unrequested parameters, corresponding members
352 * have 0 so that the parameters are never changed anymore.
354 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
355 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
357 /* Due to the above design, actual sequence number starts at 2. */
360 /* Apply all rules in order. */
362 for (k
= 0; k
< constrs
->rules_num
; k
++) {
363 r
= &constrs
->rules
[k
];
366 * Check condition bits of this rule. When the rule has
367 * some condition bits, parameter without the bits is
368 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
369 * is an example of the condition bits.
371 if (r
->cond
&& !(r
->cond
& params
->flags
))
375 * The 'deps' array includes maximum three dependencies
376 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
377 * member of this array is a sentinel and should be
380 * This rule should be processed in this time when dependent
381 * parameters were changed at former applications of the other
384 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
385 if (vstamps
[r
->deps
[d
]] > rstamps
[k
])
391 if (trace_hw_mask_param_enabled()) {
392 if (hw_is_mask(r
->var
))
393 old_mask
= *hw_param_mask(params
, r
->var
);
395 if (trace_hw_interval_param_enabled()) {
396 if (hw_is_interval(r
->var
))
397 old_interval
= *hw_param_interval(params
, r
->var
);
400 changed
= r
->func(params
, r
);
405 * When the parameter is changed, notify it to the caller
406 * by corresponding returned bit, then preparing for next
409 if (changed
&& r
->var
>= 0) {
410 if (hw_is_mask(r
->var
)) {
411 trace_hw_mask_param(substream
, r
->var
,
413 hw_param_mask(params
, r
->var
));
415 if (hw_is_interval(r
->var
)) {
416 trace_hw_interval_param(substream
, r
->var
,
417 k
+ 1, &old_interval
,
418 hw_param_interval(params
, r
->var
));
421 params
->cmask
|= (1 << r
->var
);
422 vstamps
[r
->var
] = stamp
;
426 rstamps
[k
] = stamp
++;
429 /* Iterate to evaluate all rules till no parameters are changed. */
436 static int fixup_unreferenced_params(struct snd_pcm_substream
*substream
,
437 struct snd_pcm_hw_params
*params
)
439 const struct snd_interval
*i
;
440 const struct snd_mask
*m
;
443 if (!params
->msbits
) {
444 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
445 if (snd_interval_single(i
))
446 params
->msbits
= snd_interval_value(i
);
449 if (!params
->rate_den
) {
450 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
451 if (snd_interval_single(i
)) {
452 params
->rate_num
= snd_interval_value(i
);
453 params
->rate_den
= 1;
457 if (!params
->fifo_size
) {
458 m
= hw_param_mask_c(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
459 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
460 if (snd_mask_single(m
) && snd_interval_single(i
)) {
461 err
= substream
->ops
->ioctl(substream
,
462 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
469 params
->info
= substream
->runtime
->hw
.info
;
470 params
->info
&= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES
|
471 SNDRV_PCM_INFO_DRAIN_TRIGGER
);
472 if (!hw_support_mmap(substream
))
473 params
->info
&= ~(SNDRV_PCM_INFO_MMAP
|
474 SNDRV_PCM_INFO_MMAP_VALID
);
480 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
481 struct snd_pcm_hw_params
*params
)
486 params
->fifo_size
= 0;
487 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
489 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
490 params
->rate_num
= 0;
491 params
->rate_den
= 0;
494 err
= constrain_mask_params(substream
, params
);
498 err
= constrain_interval_params(substream
, params
);
502 err
= constrain_params_by_rules(substream
, params
);
510 EXPORT_SYMBOL(snd_pcm_hw_refine
);
512 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
513 struct snd_pcm_hw_params __user
* _params
)
515 struct snd_pcm_hw_params
*params
;
518 params
= memdup_user(_params
, sizeof(*params
));
520 return PTR_ERR(params
);
522 err
= snd_pcm_hw_refine(substream
, params
);
526 err
= fixup_unreferenced_params(substream
, params
);
530 if (copy_to_user(_params
, params
, sizeof(*params
)))
537 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
542 return -1; /* invalid */
544 /* take 75% of period time as the deadline */
545 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
546 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
552 static void snd_pcm_set_state(struct snd_pcm_substream
*substream
, int state
)
554 snd_pcm_stream_lock_irq(substream
);
555 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DISCONNECTED
)
556 substream
->runtime
->status
->state
= state
;
557 snd_pcm_stream_unlock_irq(substream
);
560 static inline void snd_pcm_timer_notify(struct snd_pcm_substream
*substream
,
563 #ifdef CONFIG_SND_PCM_TIMER
564 if (substream
->timer
)
565 snd_timer_notify(substream
->timer
, event
,
566 &substream
->runtime
->trigger_tstamp
);
571 * snd_pcm_hw_param_choose - choose a configuration defined by @params
573 * @params: the hw_params instance
575 * Choose one configuration from configuration space defined by @params.
576 * The configuration chosen is that obtained fixing in this order:
577 * first access, first format, first subformat, min channels,
578 * min rate, min period time, max buffer size, min tick time
580 * Return: Zero if successful, or a negative error code on failure.
582 static int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
583 struct snd_pcm_hw_params
*params
)
585 static const int vars
[] = {
586 SNDRV_PCM_HW_PARAM_ACCESS
,
587 SNDRV_PCM_HW_PARAM_FORMAT
,
588 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
589 SNDRV_PCM_HW_PARAM_CHANNELS
,
590 SNDRV_PCM_HW_PARAM_RATE
,
591 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
592 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
593 SNDRV_PCM_HW_PARAM_TICK_TIME
,
597 struct snd_mask old_mask
;
598 struct snd_interval old_interval
;
601 for (v
= vars
; *v
!= -1; v
++) {
602 /* Keep old parameter to trace. */
603 if (trace_hw_mask_param_enabled()) {
605 old_mask
= *hw_param_mask(params
, *v
);
607 if (trace_hw_interval_param_enabled()) {
608 if (hw_is_interval(*v
))
609 old_interval
= *hw_param_interval(params
, *v
);
611 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
612 changed
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
614 changed
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
615 if (snd_BUG_ON(changed
< 0))
620 /* Trace the changed parameter. */
621 if (hw_is_mask(*v
)) {
622 trace_hw_mask_param(pcm
, *v
, 0, &old_mask
,
623 hw_param_mask(params
, *v
));
625 if (hw_is_interval(*v
)) {
626 trace_hw_interval_param(pcm
, *v
, 0, &old_interval
,
627 hw_param_interval(params
, *v
));
634 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
635 struct snd_pcm_hw_params
*params
)
637 struct snd_pcm_runtime
*runtime
;
640 snd_pcm_uframes_t frames
;
642 if (PCM_RUNTIME_CHECK(substream
))
644 runtime
= substream
->runtime
;
645 snd_pcm_stream_lock_irq(substream
);
646 switch (runtime
->status
->state
) {
647 case SNDRV_PCM_STATE_OPEN
:
648 case SNDRV_PCM_STATE_SETUP
:
649 case SNDRV_PCM_STATE_PREPARED
:
652 snd_pcm_stream_unlock_irq(substream
);
655 snd_pcm_stream_unlock_irq(substream
);
656 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
657 if (!substream
->oss
.oss
)
659 if (atomic_read(&substream
->mmap_count
))
663 err
= snd_pcm_hw_refine(substream
, params
);
667 err
= snd_pcm_hw_params_choose(substream
, params
);
671 err
= fixup_unreferenced_params(substream
, params
);
675 if (substream
->ops
->hw_params
!= NULL
) {
676 err
= substream
->ops
->hw_params(substream
, params
);
681 runtime
->access
= params_access(params
);
682 runtime
->format
= params_format(params
);
683 runtime
->subformat
= params_subformat(params
);
684 runtime
->channels
= params_channels(params
);
685 runtime
->rate
= params_rate(params
);
686 runtime
->period_size
= params_period_size(params
);
687 runtime
->periods
= params_periods(params
);
688 runtime
->buffer_size
= params_buffer_size(params
);
689 runtime
->info
= params
->info
;
690 runtime
->rate_num
= params
->rate_num
;
691 runtime
->rate_den
= params
->rate_den
;
692 runtime
->no_period_wakeup
=
693 (params
->info
& SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
) &&
694 (params
->flags
& SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
);
696 bits
= snd_pcm_format_physical_width(runtime
->format
);
697 runtime
->sample_bits
= bits
;
698 bits
*= runtime
->channels
;
699 runtime
->frame_bits
= bits
;
701 while (bits
% 8 != 0) {
705 runtime
->byte_align
= bits
/ 8;
706 runtime
->min_align
= frames
;
708 /* Default sw params */
709 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
710 runtime
->period_step
= 1;
711 runtime
->control
->avail_min
= runtime
->period_size
;
712 runtime
->start_threshold
= 1;
713 runtime
->stop_threshold
= runtime
->buffer_size
;
714 runtime
->silence_threshold
= 0;
715 runtime
->silence_size
= 0;
716 runtime
->boundary
= runtime
->buffer_size
;
717 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
718 runtime
->boundary
*= 2;
720 snd_pcm_timer_resolution_change(substream
);
721 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_SETUP
);
723 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
724 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
725 if ((usecs
= period_to_usecs(runtime
)) >= 0)
726 pm_qos_add_request(&substream
->latency_pm_qos_req
,
727 PM_QOS_CPU_DMA_LATENCY
, usecs
);
730 /* hardware might be unusable from this time,
731 so we force application to retry to set
732 the correct hardware parameter settings */
733 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
734 if (substream
->ops
->hw_free
!= NULL
)
735 substream
->ops
->hw_free(substream
);
739 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
740 struct snd_pcm_hw_params __user
* _params
)
742 struct snd_pcm_hw_params
*params
;
745 params
= memdup_user(_params
, sizeof(*params
));
747 return PTR_ERR(params
);
749 err
= snd_pcm_hw_params(substream
, params
);
753 if (copy_to_user(_params
, params
, sizeof(*params
)))
760 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
762 struct snd_pcm_runtime
*runtime
;
765 if (PCM_RUNTIME_CHECK(substream
))
767 runtime
= substream
->runtime
;
768 snd_pcm_stream_lock_irq(substream
);
769 switch (runtime
->status
->state
) {
770 case SNDRV_PCM_STATE_SETUP
:
771 case SNDRV_PCM_STATE_PREPARED
:
774 snd_pcm_stream_unlock_irq(substream
);
777 snd_pcm_stream_unlock_irq(substream
);
778 if (atomic_read(&substream
->mmap_count
))
780 if (substream
->ops
->hw_free
)
781 result
= substream
->ops
->hw_free(substream
);
782 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
783 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
787 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
788 struct snd_pcm_sw_params
*params
)
790 struct snd_pcm_runtime
*runtime
;
793 if (PCM_RUNTIME_CHECK(substream
))
795 runtime
= substream
->runtime
;
796 snd_pcm_stream_lock_irq(substream
);
797 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
798 snd_pcm_stream_unlock_irq(substream
);
801 snd_pcm_stream_unlock_irq(substream
);
803 if (params
->tstamp_mode
< 0 ||
804 params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
806 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
807 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
809 if (params
->avail_min
== 0)
811 if (params
->silence_size
>= runtime
->boundary
) {
812 if (params
->silence_threshold
!= 0)
815 if (params
->silence_size
> params
->silence_threshold
)
817 if (params
->silence_threshold
> runtime
->buffer_size
)
821 snd_pcm_stream_lock_irq(substream
);
822 runtime
->tstamp_mode
= params
->tstamp_mode
;
823 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
824 runtime
->tstamp_type
= params
->tstamp_type
;
825 runtime
->period_step
= params
->period_step
;
826 runtime
->control
->avail_min
= params
->avail_min
;
827 runtime
->start_threshold
= params
->start_threshold
;
828 runtime
->stop_threshold
= params
->stop_threshold
;
829 runtime
->silence_threshold
= params
->silence_threshold
;
830 runtime
->silence_size
= params
->silence_size
;
831 params
->boundary
= runtime
->boundary
;
832 if (snd_pcm_running(substream
)) {
833 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
834 runtime
->silence_size
> 0)
835 snd_pcm_playback_silence(substream
, ULONG_MAX
);
836 err
= snd_pcm_update_state(substream
, runtime
);
838 snd_pcm_stream_unlock_irq(substream
);
842 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
843 struct snd_pcm_sw_params __user
* _params
)
845 struct snd_pcm_sw_params params
;
847 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
849 err
= snd_pcm_sw_params(substream
, ¶ms
);
850 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
855 int snd_pcm_status(struct snd_pcm_substream
*substream
,
856 struct snd_pcm_status
*status
)
858 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
860 snd_pcm_stream_lock_irq(substream
);
862 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
863 &runtime
->audio_tstamp_config
);
865 /* backwards compatible behavior */
866 if (runtime
->audio_tstamp_config
.type_requested
==
867 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
868 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
869 runtime
->audio_tstamp_config
.type_requested
=
870 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
872 runtime
->audio_tstamp_config
.type_requested
=
873 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
874 runtime
->audio_tstamp_report
.valid
= 0;
876 runtime
->audio_tstamp_report
.valid
= 1;
878 status
->state
= runtime
->status
->state
;
879 status
->suspended_state
= runtime
->status
->suspended_state
;
880 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
882 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
883 if (snd_pcm_running(substream
)) {
884 snd_pcm_update_hw_ptr(substream
);
885 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
886 status
->tstamp
= runtime
->status
->tstamp
;
887 status
->driver_tstamp
= runtime
->driver_tstamp
;
888 status
->audio_tstamp
=
889 runtime
->status
->audio_tstamp
;
890 if (runtime
->audio_tstamp_report
.valid
== 1)
891 /* backwards compatibility, no report provided in COMPAT mode */
892 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
893 &status
->audio_tstamp_accuracy
,
894 &runtime
->audio_tstamp_report
);
899 /* get tstamp only in fallback mode and only if enabled */
900 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
901 snd_pcm_gettime(runtime
, &status
->tstamp
);
904 status
->appl_ptr
= runtime
->control
->appl_ptr
;
905 status
->hw_ptr
= runtime
->status
->hw_ptr
;
906 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
907 status
->avail
= snd_pcm_playback_avail(runtime
);
908 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
909 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
910 status
->delay
= runtime
->buffer_size
- status
->avail
;
911 status
->delay
+= runtime
->delay
;
915 status
->avail
= snd_pcm_capture_avail(runtime
);
916 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
917 status
->delay
= status
->avail
+ runtime
->delay
;
921 status
->avail_max
= runtime
->avail_max
;
922 status
->overrange
= runtime
->overrange
;
923 runtime
->avail_max
= 0;
924 runtime
->overrange
= 0;
926 snd_pcm_stream_unlock_irq(substream
);
930 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
931 struct snd_pcm_status __user
* _status
,
934 struct snd_pcm_status status
;
937 memset(&status
, 0, sizeof(status
));
939 * with extension, parameters are read/write,
940 * get audio_tstamp_data from user,
941 * ignore rest of status structure
943 if (ext
&& get_user(status
.audio_tstamp_data
,
944 (u32 __user
*)(&_status
->audio_tstamp_data
)))
946 res
= snd_pcm_status(substream
, &status
);
949 if (copy_to_user(_status
, &status
, sizeof(status
)))
954 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
955 struct snd_pcm_channel_info
* info
)
957 struct snd_pcm_runtime
*runtime
;
958 unsigned int channel
;
960 channel
= info
->channel
;
961 runtime
= substream
->runtime
;
962 snd_pcm_stream_lock_irq(substream
);
963 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
964 snd_pcm_stream_unlock_irq(substream
);
967 snd_pcm_stream_unlock_irq(substream
);
968 if (channel
>= runtime
->channels
)
970 memset(info
, 0, sizeof(*info
));
971 info
->channel
= channel
;
972 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
975 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
976 struct snd_pcm_channel_info __user
* _info
)
978 struct snd_pcm_channel_info info
;
981 if (copy_from_user(&info
, _info
, sizeof(info
)))
983 res
= snd_pcm_channel_info(substream
, &info
);
986 if (copy_to_user(_info
, &info
, sizeof(info
)))
991 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
993 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
994 if (runtime
->trigger_master
== NULL
)
996 if (runtime
->trigger_master
== substream
) {
997 if (!runtime
->trigger_tstamp_latched
)
998 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
1000 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
1001 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
1003 runtime
->trigger_master
= NULL
;
1007 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
1008 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
1009 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
1010 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
1014 * this functions is core for handling of linked stream
1015 * Note: the stream state might be changed also on failure
1016 * Note2: call with calling stream lock + link lock
1018 static int snd_pcm_action_group(const struct action_ops
*ops
,
1019 struct snd_pcm_substream
*substream
,
1020 int state
, int do_lock
)
1022 struct snd_pcm_substream
*s
= NULL
;
1023 struct snd_pcm_substream
*s1
;
1024 int res
= 0, depth
= 1;
1026 snd_pcm_group_for_each_entry(s
, substream
) {
1027 if (do_lock
&& s
!= substream
) {
1028 if (s
->pcm
->nonatomic
)
1029 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
1031 spin_lock_nested(&s
->self_group
.lock
, depth
);
1034 res
= ops
->pre_action(s
, state
);
1038 snd_pcm_group_for_each_entry(s
, substream
) {
1039 res
= ops
->do_action(s
, state
);
1041 if (ops
->undo_action
) {
1042 snd_pcm_group_for_each_entry(s1
, substream
) {
1043 if (s1
== s
) /* failed stream */
1045 ops
->undo_action(s1
, state
);
1048 s
= NULL
; /* unlock all */
1052 snd_pcm_group_for_each_entry(s
, substream
) {
1053 ops
->post_action(s
, state
);
1057 /* unlock streams */
1058 snd_pcm_group_for_each_entry(s1
, substream
) {
1059 if (s1
!= substream
) {
1060 if (s1
->pcm
->nonatomic
)
1061 mutex_unlock(&s1
->self_group
.mutex
);
1063 spin_unlock(&s1
->self_group
.lock
);
1065 if (s1
== s
) /* end */
1073 * Note: call with stream lock
1075 static int snd_pcm_action_single(const struct action_ops
*ops
,
1076 struct snd_pcm_substream
*substream
,
1081 res
= ops
->pre_action(substream
, state
);
1084 res
= ops
->do_action(substream
, state
);
1086 ops
->post_action(substream
, state
);
1087 else if (ops
->undo_action
)
1088 ops
->undo_action(substream
, state
);
1093 * Note: call with stream lock
1095 static int snd_pcm_action(const struct action_ops
*ops
,
1096 struct snd_pcm_substream
*substream
,
1101 if (!snd_pcm_stream_linked(substream
))
1102 return snd_pcm_action_single(ops
, substream
, state
);
1104 if (substream
->pcm
->nonatomic
) {
1105 if (!mutex_trylock(&substream
->group
->mutex
)) {
1106 mutex_unlock(&substream
->self_group
.mutex
);
1107 mutex_lock(&substream
->group
->mutex
);
1108 mutex_lock(&substream
->self_group
.mutex
);
1110 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1111 mutex_unlock(&substream
->group
->mutex
);
1113 if (!spin_trylock(&substream
->group
->lock
)) {
1114 spin_unlock(&substream
->self_group
.lock
);
1115 spin_lock(&substream
->group
->lock
);
1116 spin_lock(&substream
->self_group
.lock
);
1118 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1119 spin_unlock(&substream
->group
->lock
);
1125 * Note: don't use any locks before
1127 static int snd_pcm_action_lock_irq(const struct action_ops
*ops
,
1128 struct snd_pcm_substream
*substream
,
1133 snd_pcm_stream_lock_irq(substream
);
1134 res
= snd_pcm_action(ops
, substream
, state
);
1135 snd_pcm_stream_unlock_irq(substream
);
1141 static int snd_pcm_action_nonatomic(const struct action_ops
*ops
,
1142 struct snd_pcm_substream
*substream
,
1147 down_read(&snd_pcm_link_rwsem
);
1148 if (snd_pcm_stream_linked(substream
))
1149 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1151 res
= snd_pcm_action_single(ops
, substream
, state
);
1152 up_read(&snd_pcm_link_rwsem
);
1159 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1161 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1162 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1164 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1165 !snd_pcm_playback_data(substream
))
1167 runtime
->trigger_tstamp_latched
= false;
1168 runtime
->trigger_master
= substream
;
1172 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1174 if (substream
->runtime
->trigger_master
!= substream
)
1176 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1179 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1181 if (substream
->runtime
->trigger_master
== substream
)
1182 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1185 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1187 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1188 snd_pcm_trigger_tstamp(substream
);
1189 runtime
->hw_ptr_jiffies
= jiffies
;
1190 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1192 runtime
->status
->state
= state
;
1193 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1194 runtime
->silence_size
> 0)
1195 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1196 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTART
);
1199 static const struct action_ops snd_pcm_action_start
= {
1200 .pre_action
= snd_pcm_pre_start
,
1201 .do_action
= snd_pcm_do_start
,
1202 .undo_action
= snd_pcm_undo_start
,
1203 .post_action
= snd_pcm_post_start
1207 * snd_pcm_start - start all linked streams
1208 * @substream: the PCM substream instance
1210 * Return: Zero if successful, or a negative error code.
1211 * The stream lock must be acquired before calling this function.
1213 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1215 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1216 SNDRV_PCM_STATE_RUNNING
);
1219 /* take the stream lock and start the streams */
1220 static int snd_pcm_start_lock_irq(struct snd_pcm_substream
*substream
)
1222 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
,
1223 SNDRV_PCM_STATE_RUNNING
);
1229 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1231 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1232 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1234 runtime
->trigger_master
= substream
;
1238 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1240 if (substream
->runtime
->trigger_master
== substream
&&
1241 snd_pcm_running(substream
))
1242 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1243 return 0; /* unconditonally stop all substreams */
1246 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1248 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1249 if (runtime
->status
->state
!= state
) {
1250 snd_pcm_trigger_tstamp(substream
);
1251 runtime
->status
->state
= state
;
1252 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTOP
);
1254 wake_up(&runtime
->sleep
);
1255 wake_up(&runtime
->tsleep
);
1258 static const struct action_ops snd_pcm_action_stop
= {
1259 .pre_action
= snd_pcm_pre_stop
,
1260 .do_action
= snd_pcm_do_stop
,
1261 .post_action
= snd_pcm_post_stop
1265 * snd_pcm_stop - try to stop all running streams in the substream group
1266 * @substream: the PCM substream instance
1267 * @state: PCM state after stopping the stream
1269 * The state of each stream is then changed to the given state unconditionally.
1271 * Return: Zero if successful, or a negative error code.
1273 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1275 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1277 EXPORT_SYMBOL(snd_pcm_stop
);
1280 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1281 * @substream: the PCM substream
1283 * After stopping, the state is changed to SETUP.
1284 * Unlike snd_pcm_stop(), this affects only the given stream.
1286 * Return: Zero if succesful, or a negative error code.
1288 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1290 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1291 SNDRV_PCM_STATE_SETUP
);
1295 * snd_pcm_stop_xrun - stop the running streams as XRUN
1296 * @substream: the PCM substream instance
1298 * This stops the given running substream (and all linked substreams) as XRUN.
1299 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1301 * Return: Zero if successful, or a negative error code.
1303 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1305 unsigned long flags
;
1308 snd_pcm_stream_lock_irqsave(substream
, flags
);
1309 if (snd_pcm_running(substream
))
1310 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1311 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1314 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1319 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1321 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1322 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1325 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1327 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1329 runtime
->trigger_master
= substream
;
1333 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1335 if (substream
->runtime
->trigger_master
!= substream
)
1337 /* some drivers might use hw_ptr to recover from the pause -
1338 update the hw_ptr now */
1340 snd_pcm_update_hw_ptr(substream
);
1341 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1342 * a delta between the current jiffies, this gives a large enough
1343 * delta, effectively to skip the check once.
1345 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1346 return substream
->ops
->trigger(substream
,
1347 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1348 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1351 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1353 if (substream
->runtime
->trigger_master
== substream
)
1354 substream
->ops
->trigger(substream
,
1355 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1356 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1359 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1361 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1362 snd_pcm_trigger_tstamp(substream
);
1364 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1365 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MPAUSE
);
1366 wake_up(&runtime
->sleep
);
1367 wake_up(&runtime
->tsleep
);
1369 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1370 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MCONTINUE
);
1374 static const struct action_ops snd_pcm_action_pause
= {
1375 .pre_action
= snd_pcm_pre_pause
,
1376 .do_action
= snd_pcm_do_pause
,
1377 .undo_action
= snd_pcm_undo_pause
,
1378 .post_action
= snd_pcm_post_pause
1382 * Push/release the pause for all linked streams.
1384 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1386 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1392 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1394 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1395 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1397 runtime
->trigger_master
= substream
;
1401 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1403 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1404 if (runtime
->trigger_master
!= substream
)
1406 if (! snd_pcm_running(substream
))
1408 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1409 return 0; /* suspend unconditionally */
1412 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1414 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1415 snd_pcm_trigger_tstamp(substream
);
1416 runtime
->status
->suspended_state
= runtime
->status
->state
;
1417 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1418 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSUSPEND
);
1419 wake_up(&runtime
->sleep
);
1420 wake_up(&runtime
->tsleep
);
1423 static const struct action_ops snd_pcm_action_suspend
= {
1424 .pre_action
= snd_pcm_pre_suspend
,
1425 .do_action
= snd_pcm_do_suspend
,
1426 .post_action
= snd_pcm_post_suspend
1430 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1431 * @substream: the PCM substream
1433 * After this call, all streams are changed to SUSPENDED state.
1435 * Return: Zero if successful (or @substream is %NULL), or a negative error
1438 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1441 unsigned long flags
;
1446 snd_pcm_stream_lock_irqsave(substream
, flags
);
1447 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1448 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1451 EXPORT_SYMBOL(snd_pcm_suspend
);
1454 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1455 * @pcm: the PCM instance
1457 * After this call, all streams are changed to SUSPENDED state.
1459 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1461 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1463 struct snd_pcm_substream
*substream
;
1464 int stream
, err
= 0;
1469 for (stream
= 0; stream
< 2; stream
++) {
1470 for (substream
= pcm
->streams
[stream
].substream
;
1471 substream
; substream
= substream
->next
) {
1472 /* FIXME: the open/close code should lock this as well */
1473 if (substream
->runtime
== NULL
)
1475 err
= snd_pcm_suspend(substream
);
1476 if (err
< 0 && err
!= -EBUSY
)
1482 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1486 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1488 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1489 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1491 runtime
->trigger_master
= substream
;
1495 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1497 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1498 if (runtime
->trigger_master
!= substream
)
1500 /* DMA not running previously? */
1501 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1502 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1503 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1505 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1508 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1510 if (substream
->runtime
->trigger_master
== substream
&&
1511 snd_pcm_running(substream
))
1512 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1515 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1517 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1518 snd_pcm_trigger_tstamp(substream
);
1519 runtime
->status
->state
= runtime
->status
->suspended_state
;
1520 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MRESUME
);
1523 static const struct action_ops snd_pcm_action_resume
= {
1524 .pre_action
= snd_pcm_pre_resume
,
1525 .do_action
= snd_pcm_do_resume
,
1526 .undo_action
= snd_pcm_undo_resume
,
1527 .post_action
= snd_pcm_post_resume
1530 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1532 return snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1537 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1542 #endif /* CONFIG_PM */
1547 * Change the RUNNING stream(s) to XRUN state.
1549 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1551 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1554 snd_pcm_stream_lock_irq(substream
);
1555 switch (runtime
->status
->state
) {
1556 case SNDRV_PCM_STATE_XRUN
:
1557 result
= 0; /* already there */
1559 case SNDRV_PCM_STATE_RUNNING
:
1560 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1565 snd_pcm_stream_unlock_irq(substream
);
1572 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1574 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1575 switch (runtime
->status
->state
) {
1576 case SNDRV_PCM_STATE_RUNNING
:
1577 case SNDRV_PCM_STATE_PREPARED
:
1578 case SNDRV_PCM_STATE_PAUSED
:
1579 case SNDRV_PCM_STATE_SUSPENDED
:
1586 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1588 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1589 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1592 runtime
->hw_ptr_base
= 0;
1593 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1594 runtime
->status
->hw_ptr
% runtime
->period_size
;
1595 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1596 runtime
->silence_filled
= 0;
1600 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1602 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1603 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1604 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1605 runtime
->silence_size
> 0)
1606 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1609 static const struct action_ops snd_pcm_action_reset
= {
1610 .pre_action
= snd_pcm_pre_reset
,
1611 .do_action
= snd_pcm_do_reset
,
1612 .post_action
= snd_pcm_post_reset
1615 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1617 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1623 /* we use the second argument for updating f_flags */
1624 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1627 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1628 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1629 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1631 if (snd_pcm_running(substream
))
1633 substream
->f_flags
= f_flags
;
1637 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1640 err
= substream
->ops
->prepare(substream
);
1643 return snd_pcm_do_reset(substream
, 0);
1646 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1648 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1649 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1650 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1653 static const struct action_ops snd_pcm_action_prepare
= {
1654 .pre_action
= snd_pcm_pre_prepare
,
1655 .do_action
= snd_pcm_do_prepare
,
1656 .post_action
= snd_pcm_post_prepare
1660 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1661 * @substream: the PCM substream instance
1662 * @file: file to refer f_flags
1664 * Return: Zero if successful, or a negative error code.
1666 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1672 f_flags
= file
->f_flags
;
1674 f_flags
= substream
->f_flags
;
1676 snd_pcm_stream_lock_irq(substream
);
1677 switch (substream
->runtime
->status
->state
) {
1678 case SNDRV_PCM_STATE_PAUSED
:
1679 snd_pcm_pause(substream
, 0);
1681 case SNDRV_PCM_STATE_SUSPENDED
:
1682 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1685 snd_pcm_stream_unlock_irq(substream
);
1687 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1688 substream
, f_flags
);
1695 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1697 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1698 switch (runtime
->status
->state
) {
1699 case SNDRV_PCM_STATE_OPEN
:
1700 case SNDRV_PCM_STATE_DISCONNECTED
:
1701 case SNDRV_PCM_STATE_SUSPENDED
:
1704 runtime
->trigger_master
= substream
;
1708 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1710 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1711 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1712 switch (runtime
->status
->state
) {
1713 case SNDRV_PCM_STATE_PREPARED
:
1714 /* start playback stream if possible */
1715 if (! snd_pcm_playback_empty(substream
)) {
1716 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1717 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1719 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1722 case SNDRV_PCM_STATE_RUNNING
:
1723 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1725 case SNDRV_PCM_STATE_XRUN
:
1726 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1732 /* stop running stream */
1733 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1734 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1735 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1736 snd_pcm_do_stop(substream
, new_state
);
1737 snd_pcm_post_stop(substream
, new_state
);
1741 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1742 runtime
->trigger_master
== substream
&&
1743 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1744 return substream
->ops
->trigger(substream
,
1745 SNDRV_PCM_TRIGGER_DRAIN
);
1750 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1754 static const struct action_ops snd_pcm_action_drain_init
= {
1755 .pre_action
= snd_pcm_pre_drain_init
,
1756 .do_action
= snd_pcm_do_drain_init
,
1757 .post_action
= snd_pcm_post_drain_init
1760 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1763 * Drain the stream(s).
1764 * When the substream is linked, sync until the draining of all playback streams
1766 * After this call, all streams are supposed to be either SETUP or DRAINING
1767 * (capture only) state.
1769 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1772 struct snd_card
*card
;
1773 struct snd_pcm_runtime
*runtime
;
1774 struct snd_pcm_substream
*s
;
1775 wait_queue_entry_t wait
;
1779 card
= substream
->pcm
->card
;
1780 runtime
= substream
->runtime
;
1782 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1786 if (file
->f_flags
& O_NONBLOCK
)
1788 } else if (substream
->f_flags
& O_NONBLOCK
)
1791 down_read(&snd_pcm_link_rwsem
);
1792 snd_pcm_stream_lock_irq(substream
);
1794 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1795 snd_pcm_pause(substream
, 0);
1797 /* pre-start/stop - all running streams are changed to DRAINING state */
1798 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1801 /* in non-blocking, we don't wait in ioctl but let caller poll */
1809 struct snd_pcm_runtime
*to_check
;
1810 if (signal_pending(current
)) {
1811 result
= -ERESTARTSYS
;
1814 /* find a substream to drain */
1816 snd_pcm_group_for_each_entry(s
, substream
) {
1817 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1819 runtime
= s
->runtime
;
1820 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1826 break; /* all drained */
1827 init_waitqueue_entry(&wait
, current
);
1828 add_wait_queue(&to_check
->sleep
, &wait
);
1829 snd_pcm_stream_unlock_irq(substream
);
1830 up_read(&snd_pcm_link_rwsem
);
1831 if (runtime
->no_period_wakeup
)
1832 tout
= MAX_SCHEDULE_TIMEOUT
;
1835 if (runtime
->rate
) {
1836 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1837 tout
= max(t
, tout
);
1839 tout
= msecs_to_jiffies(tout
* 1000);
1841 tout
= schedule_timeout_interruptible(tout
);
1842 down_read(&snd_pcm_link_rwsem
);
1843 snd_pcm_stream_lock_irq(substream
);
1844 remove_wait_queue(&to_check
->sleep
, &wait
);
1845 if (card
->shutdown
) {
1850 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1853 dev_dbg(substream
->pcm
->card
->dev
,
1854 "playback drain error (DMA or IRQ trouble?)\n");
1855 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1863 snd_pcm_stream_unlock_irq(substream
);
1864 up_read(&snd_pcm_link_rwsem
);
1872 * Immediately put all linked substreams into SETUP state.
1874 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1876 struct snd_pcm_runtime
*runtime
;
1879 if (PCM_RUNTIME_CHECK(substream
))
1881 runtime
= substream
->runtime
;
1883 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1884 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1887 snd_pcm_stream_lock_irq(substream
);
1889 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1890 snd_pcm_pause(substream
, 0);
1892 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1893 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1894 snd_pcm_stream_unlock_irq(substream
);
1900 static bool is_pcm_file(struct file
*file
)
1902 struct inode
*inode
= file_inode(file
);
1905 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1907 minor
= iminor(inode
);
1908 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1909 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1915 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1918 struct snd_pcm_file
*pcm_file
;
1919 struct snd_pcm_substream
*substream1
;
1920 struct snd_pcm_group
*group
;
1921 struct fd f
= fdget(fd
);
1925 if (!is_pcm_file(f
.file
)) {
1929 pcm_file
= f
.file
->private_data
;
1930 substream1
= pcm_file
->substream
;
1931 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1936 down_write_nonblock(&snd_pcm_link_rwsem
);
1937 write_lock_irq(&snd_pcm_link_rwlock
);
1938 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1939 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1940 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1944 if (snd_pcm_stream_linked(substream1
)) {
1948 if (!snd_pcm_stream_linked(substream
)) {
1949 substream
->group
= group
;
1951 spin_lock_init(&substream
->group
->lock
);
1952 mutex_init(&substream
->group
->mutex
);
1953 INIT_LIST_HEAD(&substream
->group
->substreams
);
1954 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1955 substream
->group
->count
= 1;
1957 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1958 substream
->group
->count
++;
1959 substream1
->group
= substream
->group
;
1961 write_unlock_irq(&snd_pcm_link_rwlock
);
1962 up_write(&snd_pcm_link_rwsem
);
1964 snd_card_unref(substream1
->pcm
->card
);
1971 static void relink_to_local(struct snd_pcm_substream
*substream
)
1973 substream
->group
= &substream
->self_group
;
1974 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1975 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1978 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1980 struct snd_pcm_substream
*s
;
1983 down_write_nonblock(&snd_pcm_link_rwsem
);
1984 write_lock_irq(&snd_pcm_link_rwlock
);
1985 if (!snd_pcm_stream_linked(substream
)) {
1989 list_del(&substream
->link_list
);
1990 substream
->group
->count
--;
1991 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1992 snd_pcm_group_for_each_entry(s
, substream
) {
1996 kfree(substream
->group
);
1998 relink_to_local(substream
);
2000 write_unlock_irq(&snd_pcm_link_rwlock
);
2001 up_write(&snd_pcm_link_rwsem
);
2008 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
2009 struct snd_pcm_hw_rule
*rule
)
2011 struct snd_interval t
;
2012 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
2013 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2014 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2017 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
2018 struct snd_pcm_hw_rule
*rule
)
2020 struct snd_interval t
;
2021 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
2022 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2023 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2026 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
2027 struct snd_pcm_hw_rule
*rule
)
2029 struct snd_interval t
;
2030 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
2031 hw_param_interval_c(params
, rule
->deps
[1]),
2032 (unsigned long) rule
->private, &t
);
2033 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2036 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
2037 struct snd_pcm_hw_rule
*rule
)
2039 struct snd_interval t
;
2040 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
2041 (unsigned long) rule
->private,
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_format(struct snd_pcm_hw_params
*params
,
2047 struct snd_pcm_hw_rule
*rule
)
2050 const struct snd_interval
*i
=
2051 hw_param_interval_c(params
, rule
->deps
[0]);
2053 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
2055 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2057 if (! snd_mask_test(mask
, k
))
2059 bits
= snd_pcm_format_physical_width(k
);
2061 continue; /* ignore invalid formats */
2062 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
2063 snd_mask_reset(&m
, k
);
2065 return snd_mask_refine(mask
, &m
);
2068 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
2069 struct snd_pcm_hw_rule
*rule
)
2071 struct snd_interval t
;
2077 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2079 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
2081 bits
= snd_pcm_format_physical_width(k
);
2083 continue; /* ignore invalid formats */
2084 if (t
.min
> (unsigned)bits
)
2086 if (t
.max
< (unsigned)bits
)
2090 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2093 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2094 #error "Change this table"
2097 static const unsigned int rates
[] = {
2098 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2099 48000, 64000, 88200, 96000, 176400, 192000
2102 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
2103 .count
= ARRAY_SIZE(rates
),
2107 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
2108 struct snd_pcm_hw_rule
*rule
)
2110 struct snd_pcm_hardware
*hw
= rule
->private;
2111 return snd_interval_list(hw_param_interval(params
, rule
->var
),
2112 snd_pcm_known_rates
.count
,
2113 snd_pcm_known_rates
.list
, hw
->rates
);
2116 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
2117 struct snd_pcm_hw_rule
*rule
)
2119 struct snd_interval t
;
2120 struct snd_pcm_substream
*substream
= rule
->private;
2122 t
.max
= substream
->buffer_bytes_max
;
2126 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2129 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2131 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2132 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2135 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2136 snd_mask_any(constrs_mask(constrs
, k
));
2139 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2140 snd_interval_any(constrs_interval(constrs
, k
));
2143 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2144 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2145 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2146 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2147 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2149 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2150 snd_pcm_hw_rule_format
, NULL
,
2151 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2154 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2155 snd_pcm_hw_rule_sample_bits
, NULL
,
2156 SNDRV_PCM_HW_PARAM_FORMAT
,
2157 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2160 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2161 snd_pcm_hw_rule_div
, NULL
,
2162 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2165 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2166 snd_pcm_hw_rule_mul
, NULL
,
2167 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2170 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2171 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2172 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2175 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2176 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2177 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2180 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2181 snd_pcm_hw_rule_div
, NULL
,
2182 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2185 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2186 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2187 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2190 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2191 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2192 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2195 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2196 snd_pcm_hw_rule_div
, NULL
,
2197 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2200 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2201 snd_pcm_hw_rule_div
, NULL
,
2202 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2205 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2206 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2207 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2210 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2211 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2212 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2215 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2216 snd_pcm_hw_rule_mul
, NULL
,
2217 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2220 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2221 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2222 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2225 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2226 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2227 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2230 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2231 snd_pcm_hw_rule_muldivk
, (void*) 8,
2232 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2235 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2236 snd_pcm_hw_rule_muldivk
, (void*) 8,
2237 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2240 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2241 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2242 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2245 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2246 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2247 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2253 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2255 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2256 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2258 unsigned int mask
= 0;
2260 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2261 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2262 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2263 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2264 if (hw_support_mmap(substream
)) {
2265 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2266 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2267 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2268 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2269 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2270 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2272 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2276 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2280 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2284 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2285 hw
->channels_min
, hw
->channels_max
);
2289 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2290 hw
->rate_min
, hw
->rate_max
);
2294 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2295 hw
->period_bytes_min
, hw
->period_bytes_max
);
2299 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2300 hw
->periods_min
, hw
->periods_max
);
2304 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2305 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2309 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2310 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2311 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2316 if (runtime
->dma_bytes
) {
2317 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2322 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2323 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2324 snd_pcm_hw_rule_rate
, hw
,
2325 SNDRV_PCM_HW_PARAM_RATE
, -1);
2330 /* FIXME: this belong to lowlevel */
2331 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2336 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2338 snd_pcm_unlink(substream
);
2341 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2343 substream
->ref_count
--;
2344 if (substream
->ref_count
> 0)
2347 snd_pcm_drop(substream
);
2348 if (substream
->hw_opened
) {
2349 if (substream
->ops
->hw_free
&&
2350 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
2351 substream
->ops
->hw_free(substream
);
2352 substream
->ops
->close(substream
);
2353 substream
->hw_opened
= 0;
2355 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2356 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2357 if (substream
->pcm_release
) {
2358 substream
->pcm_release(substream
);
2359 substream
->pcm_release
= NULL
;
2361 snd_pcm_detach_substream(substream
);
2363 EXPORT_SYMBOL(snd_pcm_release_substream
);
2365 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2367 struct snd_pcm_substream
**rsubstream
)
2369 struct snd_pcm_substream
*substream
;
2372 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2375 if (substream
->ref_count
> 1) {
2376 *rsubstream
= substream
;
2380 err
= snd_pcm_hw_constraints_init(substream
);
2382 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2386 if ((err
= substream
->ops
->open(substream
)) < 0)
2389 substream
->hw_opened
= 1;
2391 err
= snd_pcm_hw_constraints_complete(substream
);
2393 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2397 *rsubstream
= substream
;
2401 snd_pcm_release_substream(substream
);
2404 EXPORT_SYMBOL(snd_pcm_open_substream
);
2406 static int snd_pcm_open_file(struct file
*file
,
2407 struct snd_pcm
*pcm
,
2410 struct snd_pcm_file
*pcm_file
;
2411 struct snd_pcm_substream
*substream
;
2414 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2418 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2419 if (pcm_file
== NULL
) {
2420 snd_pcm_release_substream(substream
);
2423 pcm_file
->substream
= substream
;
2424 if (substream
->ref_count
== 1) {
2425 substream
->file
= pcm_file
;
2426 substream
->pcm_release
= pcm_release_private
;
2428 file
->private_data
= pcm_file
;
2433 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2435 struct snd_pcm
*pcm
;
2436 int err
= nonseekable_open(inode
, file
);
2439 pcm
= snd_lookup_minor_data(iminor(inode
),
2440 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2441 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2443 snd_card_unref(pcm
->card
);
2447 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2449 struct snd_pcm
*pcm
;
2450 int err
= nonseekable_open(inode
, file
);
2453 pcm
= snd_lookup_minor_data(iminor(inode
),
2454 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2455 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2457 snd_card_unref(pcm
->card
);
2461 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2464 wait_queue_entry_t wait
;
2470 err
= snd_card_file_add(pcm
->card
, file
);
2473 if (!try_module_get(pcm
->card
->module
)) {
2477 init_waitqueue_entry(&wait
, current
);
2478 add_wait_queue(&pcm
->open_wait
, &wait
);
2479 mutex_lock(&pcm
->open_mutex
);
2481 err
= snd_pcm_open_file(file
, pcm
, stream
);
2484 if (err
== -EAGAIN
) {
2485 if (file
->f_flags
& O_NONBLOCK
) {
2491 set_current_state(TASK_INTERRUPTIBLE
);
2492 mutex_unlock(&pcm
->open_mutex
);
2494 mutex_lock(&pcm
->open_mutex
);
2495 if (pcm
->card
->shutdown
) {
2499 if (signal_pending(current
)) {
2504 remove_wait_queue(&pcm
->open_wait
, &wait
);
2505 mutex_unlock(&pcm
->open_mutex
);
2511 module_put(pcm
->card
->module
);
2513 snd_card_file_remove(pcm
->card
, file
);
2518 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2520 struct snd_pcm
*pcm
;
2521 struct snd_pcm_substream
*substream
;
2522 struct snd_pcm_file
*pcm_file
;
2524 pcm_file
= file
->private_data
;
2525 substream
= pcm_file
->substream
;
2526 if (snd_BUG_ON(!substream
))
2528 pcm
= substream
->pcm
;
2529 mutex_lock(&pcm
->open_mutex
);
2530 snd_pcm_release_substream(substream
);
2532 mutex_unlock(&pcm
->open_mutex
);
2533 wake_up(&pcm
->open_wait
);
2534 module_put(pcm
->card
->module
);
2535 snd_card_file_remove(pcm
->card
, file
);
2539 /* check and update PCM state; return 0 or a negative error
2540 * call this inside PCM lock
2542 static int do_pcm_hwsync(struct snd_pcm_substream
*substream
)
2544 switch (substream
->runtime
->status
->state
) {
2545 case SNDRV_PCM_STATE_DRAINING
:
2546 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2549 case SNDRV_PCM_STATE_RUNNING
:
2550 return snd_pcm_update_hw_ptr(substream
);
2551 case SNDRV_PCM_STATE_PREPARED
:
2552 case SNDRV_PCM_STATE_PAUSED
:
2554 case SNDRV_PCM_STATE_SUSPENDED
:
2556 case SNDRV_PCM_STATE_XRUN
:
2563 /* increase the appl_ptr; returns the processed frames or a negative error */
2564 static snd_pcm_sframes_t
forward_appl_ptr(struct snd_pcm_substream
*substream
,
2565 snd_pcm_uframes_t frames
,
2566 snd_pcm_sframes_t avail
)
2568 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2569 snd_pcm_sframes_t appl_ptr
;
2574 if (frames
> (snd_pcm_uframes_t
)avail
)
2576 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2577 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2578 appl_ptr
-= runtime
->boundary
;
2579 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2580 return ret
< 0 ? ret
: frames
;
2583 /* decrease the appl_ptr; returns the processed frames or a negative error */
2584 static snd_pcm_sframes_t
rewind_appl_ptr(struct snd_pcm_substream
*substream
,
2585 snd_pcm_uframes_t frames
,
2586 snd_pcm_sframes_t avail
)
2588 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2589 snd_pcm_sframes_t appl_ptr
;
2594 if (frames
> (snd_pcm_uframes_t
)avail
)
2596 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2598 appl_ptr
+= runtime
->boundary
;
2599 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2600 return ret
< 0 ? ret
: frames
;
2603 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2604 snd_pcm_uframes_t frames
)
2606 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2607 snd_pcm_sframes_t ret
;
2612 snd_pcm_stream_lock_irq(substream
);
2613 ret
= do_pcm_hwsync(substream
);
2615 ret
= rewind_appl_ptr(substream
, frames
,
2616 snd_pcm_playback_hw_avail(runtime
));
2617 snd_pcm_stream_unlock_irq(substream
);
2621 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2622 snd_pcm_uframes_t frames
)
2624 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2625 snd_pcm_sframes_t ret
;
2630 snd_pcm_stream_lock_irq(substream
);
2631 ret
= do_pcm_hwsync(substream
);
2633 ret
= rewind_appl_ptr(substream
, frames
,
2634 snd_pcm_capture_hw_avail(runtime
));
2635 snd_pcm_stream_unlock_irq(substream
);
2639 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2640 snd_pcm_uframes_t frames
)
2642 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2643 snd_pcm_sframes_t ret
;
2648 snd_pcm_stream_lock_irq(substream
);
2649 ret
= do_pcm_hwsync(substream
);
2651 ret
= forward_appl_ptr(substream
, frames
,
2652 snd_pcm_playback_avail(runtime
));
2653 snd_pcm_stream_unlock_irq(substream
);
2657 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2658 snd_pcm_uframes_t frames
)
2660 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2661 snd_pcm_sframes_t ret
;
2666 snd_pcm_stream_lock_irq(substream
);
2667 ret
= do_pcm_hwsync(substream
);
2669 ret
= forward_appl_ptr(substream
, frames
,
2670 snd_pcm_capture_avail(runtime
));
2671 snd_pcm_stream_unlock_irq(substream
);
2675 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2679 snd_pcm_stream_lock_irq(substream
);
2680 err
= do_pcm_hwsync(substream
);
2681 snd_pcm_stream_unlock_irq(substream
);
2685 static snd_pcm_sframes_t
snd_pcm_delay(struct snd_pcm_substream
*substream
)
2687 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2689 snd_pcm_sframes_t n
= 0;
2691 snd_pcm_stream_lock_irq(substream
);
2692 err
= do_pcm_hwsync(substream
);
2694 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2695 n
= snd_pcm_playback_hw_avail(runtime
);
2697 n
= snd_pcm_capture_avail(runtime
);
2698 n
+= runtime
->delay
;
2700 snd_pcm_stream_unlock_irq(substream
);
2701 return err
< 0 ? err
: n
;
2704 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2705 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2707 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2708 struct snd_pcm_sync_ptr sync_ptr
;
2709 volatile struct snd_pcm_mmap_status
*status
;
2710 volatile struct snd_pcm_mmap_control
*control
;
2713 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2714 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2716 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2718 status
= runtime
->status
;
2719 control
= runtime
->control
;
2720 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2721 err
= snd_pcm_hwsync(substream
);
2725 snd_pcm_stream_lock_irq(substream
);
2726 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
)) {
2727 err
= pcm_lib_apply_appl_ptr(substream
,
2728 sync_ptr
.c
.control
.appl_ptr
);
2730 snd_pcm_stream_unlock_irq(substream
);
2734 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2736 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2737 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2739 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2740 sync_ptr
.s
.status
.state
= status
->state
;
2741 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2742 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2743 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2744 snd_pcm_stream_unlock_irq(substream
);
2745 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2750 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2752 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2755 if (get_user(arg
, _arg
))
2757 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2759 runtime
->tstamp_type
= arg
;
2763 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream
*substream
,
2764 struct snd_xferi __user
*_xferi
)
2766 struct snd_xferi xferi
;
2767 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2768 snd_pcm_sframes_t result
;
2770 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2772 if (put_user(0, &_xferi
->result
))
2774 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2776 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2777 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2779 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2780 __put_user(result
, &_xferi
->result
);
2781 return result
< 0 ? result
: 0;
2784 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream
*substream
,
2785 struct snd_xfern __user
*_xfern
)
2787 struct snd_xfern xfern
;
2788 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2790 snd_pcm_sframes_t result
;
2792 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2794 if (runtime
->channels
> 128)
2796 if (put_user(0, &_xfern
->result
))
2798 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2801 bufs
= memdup_user(xfern
.bufs
, sizeof(void *) * runtime
->channels
);
2803 return PTR_ERR(bufs
);
2804 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2805 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2807 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2809 __put_user(result
, &_xfern
->result
);
2810 return result
< 0 ? result
: 0;
2813 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream
*substream
,
2814 snd_pcm_uframes_t __user
*_frames
)
2816 snd_pcm_uframes_t frames
;
2817 snd_pcm_sframes_t result
;
2819 if (get_user(frames
, _frames
))
2821 if (put_user(0, _frames
))
2823 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2824 result
= snd_pcm_playback_rewind(substream
, frames
);
2826 result
= snd_pcm_capture_rewind(substream
, frames
);
2827 __put_user(result
, _frames
);
2828 return result
< 0 ? result
: 0;
2831 static int snd_pcm_forward_ioctl(struct snd_pcm_substream
*substream
,
2832 snd_pcm_uframes_t __user
*_frames
)
2834 snd_pcm_uframes_t frames
;
2835 snd_pcm_sframes_t result
;
2837 if (get_user(frames
, _frames
))
2839 if (put_user(0, _frames
))
2841 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2842 result
= snd_pcm_playback_forward(substream
, frames
);
2844 result
= snd_pcm_capture_forward(substream
, frames
);
2845 __put_user(result
, _frames
);
2846 return result
< 0 ? result
: 0;
2849 static int snd_pcm_common_ioctl(struct file
*file
,
2850 struct snd_pcm_substream
*substream
,
2851 unsigned int cmd
, void __user
*arg
)
2853 struct snd_pcm_file
*pcm_file
= file
->private_data
;
2856 if (PCM_RUNTIME_CHECK(substream
))
2859 res
= snd_power_wait(substream
->pcm
->card
, SNDRV_CTL_POWER_D0
);
2864 case SNDRV_PCM_IOCTL_PVERSION
:
2865 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2866 case SNDRV_PCM_IOCTL_INFO
:
2867 return snd_pcm_info_user(substream
, arg
);
2868 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2870 case SNDRV_PCM_IOCTL_TTSTAMP
:
2871 return snd_pcm_tstamp(substream
, arg
);
2872 case SNDRV_PCM_IOCTL_USER_PVERSION
:
2873 if (get_user(pcm_file
->user_pversion
,
2874 (unsigned int __user
*)arg
))
2877 case SNDRV_PCM_IOCTL_HW_REFINE
:
2878 return snd_pcm_hw_refine_user(substream
, arg
);
2879 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2880 return snd_pcm_hw_params_user(substream
, arg
);
2881 case SNDRV_PCM_IOCTL_HW_FREE
:
2882 return snd_pcm_hw_free(substream
);
2883 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2884 return snd_pcm_sw_params_user(substream
, arg
);
2885 case SNDRV_PCM_IOCTL_STATUS
:
2886 return snd_pcm_status_user(substream
, arg
, false);
2887 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2888 return snd_pcm_status_user(substream
, arg
, true);
2889 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2890 return snd_pcm_channel_info_user(substream
, arg
);
2891 case SNDRV_PCM_IOCTL_PREPARE
:
2892 return snd_pcm_prepare(substream
, file
);
2893 case SNDRV_PCM_IOCTL_RESET
:
2894 return snd_pcm_reset(substream
);
2895 case SNDRV_PCM_IOCTL_START
:
2896 return snd_pcm_start_lock_irq(substream
);
2897 case SNDRV_PCM_IOCTL_LINK
:
2898 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2899 case SNDRV_PCM_IOCTL_UNLINK
:
2900 return snd_pcm_unlink(substream
);
2901 case SNDRV_PCM_IOCTL_RESUME
:
2902 return snd_pcm_resume(substream
);
2903 case SNDRV_PCM_IOCTL_XRUN
:
2904 return snd_pcm_xrun(substream
);
2905 case SNDRV_PCM_IOCTL_HWSYNC
:
2906 return snd_pcm_hwsync(substream
);
2907 case SNDRV_PCM_IOCTL_DELAY
:
2909 snd_pcm_sframes_t delay
= snd_pcm_delay(substream
);
2910 snd_pcm_sframes_t __user
*res
= arg
;
2914 if (put_user(delay
, res
))
2918 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2919 return snd_pcm_sync_ptr(substream
, arg
);
2920 #ifdef CONFIG_SND_SUPPORT_OLD_API
2921 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2922 return snd_pcm_hw_refine_old_user(substream
, arg
);
2923 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2924 return snd_pcm_hw_params_old_user(substream
, arg
);
2926 case SNDRV_PCM_IOCTL_DRAIN
:
2927 return snd_pcm_drain(substream
, file
);
2928 case SNDRV_PCM_IOCTL_DROP
:
2929 return snd_pcm_drop(substream
);
2930 case SNDRV_PCM_IOCTL_PAUSE
:
2931 return snd_pcm_action_lock_irq(&snd_pcm_action_pause
,
2933 (int)(unsigned long)arg
);
2934 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2935 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2936 return snd_pcm_xferi_frames_ioctl(substream
, arg
);
2937 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2938 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2939 return snd_pcm_xfern_frames_ioctl(substream
, arg
);
2940 case SNDRV_PCM_IOCTL_REWIND
:
2941 return snd_pcm_rewind_ioctl(substream
, arg
);
2942 case SNDRV_PCM_IOCTL_FORWARD
:
2943 return snd_pcm_forward_ioctl(substream
, arg
);
2945 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2949 static long snd_pcm_ioctl(struct file
*file
, unsigned int cmd
,
2952 struct snd_pcm_file
*pcm_file
;
2954 pcm_file
= file
->private_data
;
2956 if (((cmd
>> 8) & 0xff) != 'A')
2959 return snd_pcm_common_ioctl(file
, pcm_file
->substream
, cmd
,
2960 (void __user
*)arg
);
2964 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2965 * @substream: PCM substream
2967 * @arg: IOCTL argument
2969 * The function is provided primarily for OSS layer and USB gadget drivers,
2970 * and it allows only the limited set of ioctls (hw_params, sw_params,
2971 * prepare, start, drain, drop, forward).
2973 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2974 unsigned int cmd
, void *arg
)
2976 snd_pcm_uframes_t
*frames
= arg
;
2977 snd_pcm_sframes_t result
;
2980 case SNDRV_PCM_IOCTL_FORWARD
:
2982 /* provided only for OSS; capture-only and no value returned */
2983 if (substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
)
2985 result
= snd_pcm_capture_forward(substream
, *frames
);
2986 return result
< 0 ? result
: 0;
2988 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2989 return snd_pcm_hw_params(substream
, arg
);
2990 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2991 return snd_pcm_sw_params(substream
, arg
);
2992 case SNDRV_PCM_IOCTL_PREPARE
:
2993 return snd_pcm_prepare(substream
, NULL
);
2994 case SNDRV_PCM_IOCTL_START
:
2995 return snd_pcm_start_lock_irq(substream
);
2996 case SNDRV_PCM_IOCTL_DRAIN
:
2997 return snd_pcm_drain(substream
, NULL
);
2998 case SNDRV_PCM_IOCTL_DROP
:
2999 return snd_pcm_drop(substream
);
3000 case SNDRV_PCM_IOCTL_DELAY
:
3002 result
= snd_pcm_delay(substream
);
3012 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3014 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
3017 struct snd_pcm_file
*pcm_file
;
3018 struct snd_pcm_substream
*substream
;
3019 struct snd_pcm_runtime
*runtime
;
3020 snd_pcm_sframes_t result
;
3022 pcm_file
= file
->private_data
;
3023 substream
= pcm_file
->substream
;
3024 if (PCM_RUNTIME_CHECK(substream
))
3026 runtime
= substream
->runtime
;
3027 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3029 if (!frame_aligned(runtime
, count
))
3031 count
= bytes_to_frames(runtime
, count
);
3032 result
= snd_pcm_lib_read(substream
, buf
, count
);
3034 result
= frames_to_bytes(runtime
, result
);
3038 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3039 size_t count
, loff_t
* offset
)
3041 struct snd_pcm_file
*pcm_file
;
3042 struct snd_pcm_substream
*substream
;
3043 struct snd_pcm_runtime
*runtime
;
3044 snd_pcm_sframes_t result
;
3046 pcm_file
= file
->private_data
;
3047 substream
= pcm_file
->substream
;
3048 if (PCM_RUNTIME_CHECK(substream
))
3050 runtime
= substream
->runtime
;
3051 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3053 if (!frame_aligned(runtime
, count
))
3055 count
= bytes_to_frames(runtime
, count
);
3056 result
= snd_pcm_lib_write(substream
, buf
, count
);
3058 result
= frames_to_bytes(runtime
, result
);
3062 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3064 struct snd_pcm_file
*pcm_file
;
3065 struct snd_pcm_substream
*substream
;
3066 struct snd_pcm_runtime
*runtime
;
3067 snd_pcm_sframes_t result
;
3070 snd_pcm_uframes_t frames
;
3072 pcm_file
= iocb
->ki_filp
->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 (!iter_is_iovec(to
))
3081 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3083 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3085 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3086 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3089 for (i
= 0; i
< to
->nr_segs
; ++i
)
3090 bufs
[i
] = to
->iov
[i
].iov_base
;
3091 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3093 result
= frames_to_bytes(runtime
, result
);
3098 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3100 struct snd_pcm_file
*pcm_file
;
3101 struct snd_pcm_substream
*substream
;
3102 struct snd_pcm_runtime
*runtime
;
3103 snd_pcm_sframes_t result
;
3106 snd_pcm_uframes_t frames
;
3108 pcm_file
= iocb
->ki_filp
->private_data
;
3109 substream
= pcm_file
->substream
;
3110 if (PCM_RUNTIME_CHECK(substream
))
3112 runtime
= substream
->runtime
;
3113 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3115 if (!iter_is_iovec(from
))
3117 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3118 !frame_aligned(runtime
, from
->iov
->iov_len
))
3120 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3121 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3124 for (i
= 0; i
< from
->nr_segs
; ++i
)
3125 bufs
[i
] = from
->iov
[i
].iov_base
;
3126 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3128 result
= frames_to_bytes(runtime
, result
);
3133 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3135 struct snd_pcm_file
*pcm_file
;
3136 struct snd_pcm_substream
*substream
;
3137 struct snd_pcm_runtime
*runtime
;
3139 snd_pcm_uframes_t avail
;
3141 pcm_file
= file
->private_data
;
3143 substream
= pcm_file
->substream
;
3144 if (PCM_RUNTIME_CHECK(substream
))
3145 return POLLOUT
| POLLWRNORM
| POLLERR
;
3146 runtime
= substream
->runtime
;
3148 poll_wait(file
, &runtime
->sleep
, wait
);
3150 snd_pcm_stream_lock_irq(substream
);
3151 avail
= snd_pcm_playback_avail(runtime
);
3152 switch (runtime
->status
->state
) {
3153 case SNDRV_PCM_STATE_RUNNING
:
3154 case SNDRV_PCM_STATE_PREPARED
:
3155 case SNDRV_PCM_STATE_PAUSED
:
3156 if (avail
>= runtime
->control
->avail_min
) {
3157 mask
= POLLOUT
| POLLWRNORM
;
3161 case SNDRV_PCM_STATE_DRAINING
:
3165 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3168 snd_pcm_stream_unlock_irq(substream
);
3172 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3174 struct snd_pcm_file
*pcm_file
;
3175 struct snd_pcm_substream
*substream
;
3176 struct snd_pcm_runtime
*runtime
;
3178 snd_pcm_uframes_t avail
;
3180 pcm_file
= file
->private_data
;
3182 substream
= pcm_file
->substream
;
3183 if (PCM_RUNTIME_CHECK(substream
))
3184 return POLLIN
| POLLRDNORM
| POLLERR
;
3185 runtime
= substream
->runtime
;
3187 poll_wait(file
, &runtime
->sleep
, wait
);
3189 snd_pcm_stream_lock_irq(substream
);
3190 avail
= snd_pcm_capture_avail(runtime
);
3191 switch (runtime
->status
->state
) {
3192 case SNDRV_PCM_STATE_RUNNING
:
3193 case SNDRV_PCM_STATE_PREPARED
:
3194 case SNDRV_PCM_STATE_PAUSED
:
3195 if (avail
>= runtime
->control
->avail_min
) {
3196 mask
= POLLIN
| POLLRDNORM
;
3201 case SNDRV_PCM_STATE_DRAINING
:
3203 mask
= POLLIN
| POLLRDNORM
;
3208 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3211 snd_pcm_stream_unlock_irq(substream
);
3220 * Only on coherent architectures, we can mmap the status and the control records
3221 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3223 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3225 * mmap status record
3227 static int snd_pcm_mmap_status_fault(struct vm_fault
*vmf
)
3229 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3230 struct snd_pcm_runtime
*runtime
;
3232 if (substream
== NULL
)
3233 return VM_FAULT_SIGBUS
;
3234 runtime
= substream
->runtime
;
3235 vmf
->page
= virt_to_page(runtime
->status
);
3236 get_page(vmf
->page
);
3240 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3242 .fault
= snd_pcm_mmap_status_fault
,
3245 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3246 struct vm_area_struct
*area
)
3249 if (!(area
->vm_flags
& VM_READ
))
3251 size
= area
->vm_end
- area
->vm_start
;
3252 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3254 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3255 area
->vm_private_data
= substream
;
3256 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3261 * mmap control record
3263 static int snd_pcm_mmap_control_fault(struct vm_fault
*vmf
)
3265 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3266 struct snd_pcm_runtime
*runtime
;
3268 if (substream
== NULL
)
3269 return VM_FAULT_SIGBUS
;
3270 runtime
= substream
->runtime
;
3271 vmf
->page
= virt_to_page(runtime
->control
);
3272 get_page(vmf
->page
);
3276 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3278 .fault
= snd_pcm_mmap_control_fault
,
3281 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3282 struct vm_area_struct
*area
)
3285 if (!(area
->vm_flags
& VM_READ
))
3287 size
= area
->vm_end
- area
->vm_start
;
3288 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3290 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3291 area
->vm_private_data
= substream
;
3292 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3296 static bool pcm_status_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3298 if (pcm_file
->no_compat_mmap
)
3300 /* See pcm_control_mmap_allowed() below.
3301 * Since older alsa-lib requires both status and control mmaps to be
3302 * coupled, we have to disable the status mmap for old alsa-lib, too.
3304 if (pcm_file
->user_pversion
< SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3305 (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
))
3310 static bool pcm_control_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3312 if (pcm_file
->no_compat_mmap
)
3314 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3315 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3316 * thus it effectively assures the manual update of appl_ptr.
3318 if (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
)
3323 #else /* ! coherent mmap */
3325 * don't support mmap for status and control records.
3327 #define pcm_status_mmap_allowed(pcm_file) false
3328 #define pcm_control_mmap_allowed(pcm_file) false
3330 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3331 struct vm_area_struct
*area
)
3335 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3336 struct vm_area_struct
*area
)
3340 #endif /* coherent mmap */
3342 static inline struct page
*
3343 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3345 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3346 return virt_to_page(vaddr
);
3350 * fault callback for mmapping a RAM page
3352 static int snd_pcm_mmap_data_fault(struct vm_fault
*vmf
)
3354 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3355 struct snd_pcm_runtime
*runtime
;
3356 unsigned long offset
;
3360 if (substream
== NULL
)
3361 return VM_FAULT_SIGBUS
;
3362 runtime
= substream
->runtime
;
3363 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3364 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3365 if (offset
> dma_bytes
- PAGE_SIZE
)
3366 return VM_FAULT_SIGBUS
;
3367 if (substream
->ops
->page
)
3368 page
= substream
->ops
->page(substream
, offset
);
3370 page
= snd_pcm_default_page_ops(substream
, offset
);
3372 return VM_FAULT_SIGBUS
;
3378 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3379 .open
= snd_pcm_mmap_data_open
,
3380 .close
= snd_pcm_mmap_data_close
,
3383 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3384 .open
= snd_pcm_mmap_data_open
,
3385 .close
= snd_pcm_mmap_data_close
,
3386 .fault
= snd_pcm_mmap_data_fault
,
3390 * mmap the DMA buffer on RAM
3394 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3395 * @substream: PCM substream
3398 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3399 * this function is invoked implicitly.
3401 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3402 struct vm_area_struct
*area
)
3404 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3405 #ifdef CONFIG_GENERIC_ALLOCATOR
3406 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3407 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3408 return remap_pfn_range(area
, area
->vm_start
,
3409 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3410 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3412 #endif /* CONFIG_GENERIC_ALLOCATOR */
3413 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3414 if (IS_ENABLED(CONFIG_HAS_DMA
) && !substream
->ops
->page
&&
3415 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3416 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3418 substream
->runtime
->dma_area
,
3419 substream
->runtime
->dma_addr
,
3420 area
->vm_end
- area
->vm_start
);
3421 #endif /* CONFIG_X86 */
3422 /* mmap with fault handler */
3423 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3426 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3429 * mmap the DMA buffer on I/O memory area
3431 #if SNDRV_PCM_INFO_MMAP_IOMEM
3433 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3434 * @substream: PCM substream
3437 * When your hardware uses the iomapped pages as the hardware buffer and
3438 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3439 * is supposed to work only on limited architectures.
3441 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3442 struct vm_area_struct
*area
)
3444 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3446 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3447 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3449 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3450 #endif /* SNDRV_PCM_INFO_MMAP */
3455 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3456 struct vm_area_struct
*area
)
3458 struct snd_pcm_runtime
*runtime
;
3460 unsigned long offset
;
3464 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3465 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3468 if (!(area
->vm_flags
& VM_READ
))
3471 runtime
= substream
->runtime
;
3472 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3474 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3476 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3477 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3479 size
= area
->vm_end
- area
->vm_start
;
3480 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3481 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3482 if ((size_t)size
> dma_bytes
)
3484 if (offset
> dma_bytes
- size
)
3487 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3488 area
->vm_private_data
= substream
;
3489 if (substream
->ops
->mmap
)
3490 err
= substream
->ops
->mmap(substream
, area
);
3492 err
= snd_pcm_lib_default_mmap(substream
, area
);
3494 atomic_inc(&substream
->mmap_count
);
3497 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3499 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3501 struct snd_pcm_file
* pcm_file
;
3502 struct snd_pcm_substream
*substream
;
3503 unsigned long offset
;
3505 pcm_file
= file
->private_data
;
3506 substream
= pcm_file
->substream
;
3507 if (PCM_RUNTIME_CHECK(substream
))
3510 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3512 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3513 if (!pcm_status_mmap_allowed(pcm_file
))
3515 return snd_pcm_mmap_status(substream
, file
, area
);
3516 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3517 if (!pcm_control_mmap_allowed(pcm_file
))
3519 return snd_pcm_mmap_control(substream
, file
, area
);
3521 return snd_pcm_mmap_data(substream
, file
, area
);
3526 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3528 struct snd_pcm_file
* pcm_file
;
3529 struct snd_pcm_substream
*substream
;
3530 struct snd_pcm_runtime
*runtime
;
3532 pcm_file
= file
->private_data
;
3533 substream
= pcm_file
->substream
;
3534 if (PCM_RUNTIME_CHECK(substream
))
3536 runtime
= substream
->runtime
;
3537 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3543 #ifdef CONFIG_COMPAT
3544 #include "pcm_compat.c"
3546 #define snd_pcm_ioctl_compat NULL
3550 * To be removed helpers to keep binary compatibility
3553 #ifdef CONFIG_SND_SUPPORT_OLD_API
3554 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3555 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3557 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3558 struct snd_pcm_hw_params_old
*oparams
)
3562 memset(params
, 0, sizeof(*params
));
3563 params
->flags
= oparams
->flags
;
3564 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3565 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3566 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3567 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3568 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3569 params
->info
= oparams
->info
;
3570 params
->msbits
= oparams
->msbits
;
3571 params
->rate_num
= oparams
->rate_num
;
3572 params
->rate_den
= oparams
->rate_den
;
3573 params
->fifo_size
= oparams
->fifo_size
;
3576 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3577 struct snd_pcm_hw_params
*params
)
3581 memset(oparams
, 0, sizeof(*oparams
));
3582 oparams
->flags
= params
->flags
;
3583 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3584 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3585 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3586 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3587 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3588 oparams
->info
= params
->info
;
3589 oparams
->msbits
= params
->msbits
;
3590 oparams
->rate_num
= params
->rate_num
;
3591 oparams
->rate_den
= params
->rate_den
;
3592 oparams
->fifo_size
= params
->fifo_size
;
3595 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3596 struct snd_pcm_hw_params_old __user
* _oparams
)
3598 struct snd_pcm_hw_params
*params
;
3599 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3602 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3606 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3607 if (IS_ERR(oparams
)) {
3608 err
= PTR_ERR(oparams
);
3611 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3612 err
= snd_pcm_hw_refine(substream
, params
);
3616 err
= fixup_unreferenced_params(substream
, params
);
3620 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3621 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3630 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3631 struct snd_pcm_hw_params_old __user
* _oparams
)
3633 struct snd_pcm_hw_params
*params
;
3634 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3637 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3641 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3642 if (IS_ERR(oparams
)) {
3643 err
= PTR_ERR(oparams
);
3647 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3648 err
= snd_pcm_hw_params(substream
, params
);
3652 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3653 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3661 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3664 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3667 unsigned long pgoff
,
3668 unsigned long flags
)
3670 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3671 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3672 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3673 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3676 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3677 return (unsigned long)runtime
->status
;
3678 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3679 return (unsigned long)runtime
->control
;
3681 return (unsigned long)runtime
->dma_area
+ offset
;
3685 # define snd_pcm_get_unmapped_area NULL
3692 const struct file_operations snd_pcm_f_ops
[2] = {
3694 .owner
= THIS_MODULE
,
3695 .write
= snd_pcm_write
,
3696 .write_iter
= snd_pcm_writev
,
3697 .open
= snd_pcm_playback_open
,
3698 .release
= snd_pcm_release
,
3699 .llseek
= no_llseek
,
3700 .poll
= snd_pcm_playback_poll
,
3701 .unlocked_ioctl
= snd_pcm_ioctl
,
3702 .compat_ioctl
= snd_pcm_ioctl_compat
,
3703 .mmap
= snd_pcm_mmap
,
3704 .fasync
= snd_pcm_fasync
,
3705 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3708 .owner
= THIS_MODULE
,
3709 .read
= snd_pcm_read
,
3710 .read_iter
= snd_pcm_readv
,
3711 .open
= snd_pcm_capture_open
,
3712 .release
= snd_pcm_release
,
3713 .llseek
= no_llseek
,
3714 .poll
= snd_pcm_capture_poll
,
3715 .unlocked_ioctl
= snd_pcm_ioctl
,
3716 .compat_ioctl
= snd_pcm_ioctl_compat
,
3717 .mmap
= snd_pcm_mmap
,
3718 .fasync
= snd_pcm_fasync
,
3719 .get_unmapped_area
= snd_pcm_get_unmapped_area
,