2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.h>
39 #include <linux/delay.h>
41 #include "pcm_local.h"
43 #ifdef CONFIG_SND_DEBUG
44 #define CREATE_TRACE_POINTS
45 #include "pcm_param_trace.h"
47 #define trace_hw_mask_param_enabled() 0
48 #define trace_hw_interval_param_enabled() 0
49 #define trace_hw_mask_param(substream, type, index, prev, curr)
50 #define trace_hw_interval_param(substream, type, index, prev, curr)
57 struct snd_pcm_hw_params_old
{
59 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
60 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
61 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
62 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
67 unsigned int rate_num
;
68 unsigned int rate_den
;
69 snd_pcm_uframes_t fifo_size
;
70 unsigned char reserved
[64];
73 #ifdef CONFIG_SND_SUPPORT_OLD_API
74 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
75 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
77 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
78 struct snd_pcm_hw_params_old __user
* _oparams
);
79 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
80 struct snd_pcm_hw_params_old __user
* _oparams
);
82 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
88 static DEFINE_RWLOCK(snd_pcm_link_rwlock
);
89 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
91 /* Writer in rwsem may block readers even during its waiting in queue,
92 * and this may lead to a deadlock when the code path takes read sem
93 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
94 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
95 * sleep until all the readers are completed without blocking by writer.
97 static inline void down_write_nonfifo(struct rw_semaphore
*lock
)
99 while (!down_write_trylock(lock
))
104 * snd_pcm_stream_lock - Lock the PCM stream
105 * @substream: PCM substream
107 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
108 * flag of the given substream. This also takes the global link rw lock
109 * (or rw sem), too, for avoiding the race with linked streams.
111 void snd_pcm_stream_lock(struct snd_pcm_substream
*substream
)
113 if (substream
->pcm
->nonatomic
) {
114 down_read_nested(&snd_pcm_link_rwsem
, SINGLE_DEPTH_NESTING
);
115 mutex_lock(&substream
->self_group
.mutex
);
117 read_lock(&snd_pcm_link_rwlock
);
118 spin_lock(&substream
->self_group
.lock
);
121 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock
);
124 * snd_pcm_stream_lock - Unlock the PCM stream
125 * @substream: PCM substream
127 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
129 void snd_pcm_stream_unlock(struct snd_pcm_substream
*substream
)
131 if (substream
->pcm
->nonatomic
) {
132 mutex_unlock(&substream
->self_group
.mutex
);
133 up_read(&snd_pcm_link_rwsem
);
135 spin_unlock(&substream
->self_group
.lock
);
136 read_unlock(&snd_pcm_link_rwlock
);
139 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock
);
142 * snd_pcm_stream_lock_irq - Lock the PCM stream
143 * @substream: PCM substream
145 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
146 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
147 * as snd_pcm_stream_lock().
149 void snd_pcm_stream_lock_irq(struct snd_pcm_substream
*substream
)
151 if (!substream
->pcm
->nonatomic
)
153 snd_pcm_stream_lock(substream
);
155 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq
);
158 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
159 * @substream: PCM substream
161 * This is a counter-part of snd_pcm_stream_lock_irq().
163 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream
*substream
)
165 snd_pcm_stream_unlock(substream
);
166 if (!substream
->pcm
->nonatomic
)
169 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq
);
171 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream
*substream
)
173 unsigned long flags
= 0;
174 if (!substream
->pcm
->nonatomic
)
175 local_irq_save(flags
);
176 snd_pcm_stream_lock(substream
);
179 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave
);
182 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
183 * @substream: PCM substream
186 * This is a counter-part of snd_pcm_stream_lock_irqsave().
188 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream
*substream
,
191 snd_pcm_stream_unlock(substream
);
192 if (!substream
->pcm
->nonatomic
)
193 local_irq_restore(flags
);
195 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore
);
197 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
199 struct snd_pcm_runtime
*runtime
;
200 struct snd_pcm
*pcm
= substream
->pcm
;
201 struct snd_pcm_str
*pstr
= substream
->pstr
;
203 memset(info
, 0, sizeof(*info
));
204 info
->card
= pcm
->card
->number
;
205 info
->device
= pcm
->device
;
206 info
->stream
= substream
->stream
;
207 info
->subdevice
= substream
->number
;
208 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
209 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
210 info
->dev_class
= pcm
->dev_class
;
211 info
->dev_subclass
= pcm
->dev_subclass
;
212 info
->subdevices_count
= pstr
->substream_count
;
213 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
214 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
215 runtime
= substream
->runtime
;
220 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
221 struct snd_pcm_info __user
* _info
)
223 struct snd_pcm_info
*info
;
226 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
229 err
= snd_pcm_info(substream
, info
);
231 if (copy_to_user(_info
, info
, sizeof(*info
)))
238 static bool hw_support_mmap(struct snd_pcm_substream
*substream
)
240 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_MMAP
))
242 /* architecture supports dma_mmap_coherent()? */
243 #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
244 if (!substream
->ops
->mmap
&&
245 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
251 static int constrain_mask_params(struct snd_pcm_substream
*substream
,
252 struct snd_pcm_hw_params
*params
)
254 struct snd_pcm_hw_constraints
*constrs
=
255 &substream
->runtime
->hw_constraints
;
258 struct snd_mask old_mask
;
261 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
262 m
= hw_param_mask(params
, k
);
263 if (snd_mask_empty(m
))
266 /* This parameter is not requested to change by a caller. */
267 if (!(params
->rmask
& (1 << k
)))
270 if (trace_hw_mask_param_enabled())
273 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
279 /* Set corresponding flag so that the caller gets it. */
280 trace_hw_mask_param(substream
, k
, 0, &old_mask
, m
);
281 params
->cmask
|= 1 << k
;
287 static int constrain_interval_params(struct snd_pcm_substream
*substream
,
288 struct snd_pcm_hw_params
*params
)
290 struct snd_pcm_hw_constraints
*constrs
=
291 &substream
->runtime
->hw_constraints
;
292 struct snd_interval
*i
;
294 struct snd_interval old_interval
;
297 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
298 i
= hw_param_interval(params
, k
);
299 if (snd_interval_empty(i
))
302 /* This parameter is not requested to change by a caller. */
303 if (!(params
->rmask
& (1 << k
)))
306 if (trace_hw_interval_param_enabled())
309 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
315 /* Set corresponding flag so that the caller gets it. */
316 trace_hw_interval_param(substream
, k
, 0, &old_interval
, i
);
317 params
->cmask
|= 1 << k
;
323 static int constrain_params_by_rules(struct snd_pcm_substream
*substream
,
324 struct snd_pcm_hw_params
*params
)
326 struct snd_pcm_hw_constraints
*constrs
=
327 &substream
->runtime
->hw_constraints
;
329 unsigned int rstamps
[constrs
->rules_num
];
330 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
332 struct snd_pcm_hw_rule
*r
;
334 struct snd_mask old_mask
;
335 struct snd_interval old_interval
;
340 * Each application of rule has own sequence number.
342 * Each member of 'rstamps' array represents the sequence number of
343 * recent application of corresponding rule.
345 for (k
= 0; k
< constrs
->rules_num
; k
++)
349 * Each member of 'vstamps' array represents the sequence number of
350 * recent application of rule in which corresponding parameters were
353 * In initial state, elements corresponding to parameters requested by
354 * a caller is 1. For unrequested parameters, corresponding members
355 * have 0 so that the parameters are never changed anymore.
357 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
358 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
360 /* Due to the above design, actual sequence number starts at 2. */
363 /* Apply all rules in order. */
365 for (k
= 0; k
< constrs
->rules_num
; k
++) {
366 r
= &constrs
->rules
[k
];
369 * Check condition bits of this rule. When the rule has
370 * some condition bits, parameter without the bits is
371 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
372 * is an example of the condition bits.
374 if (r
->cond
&& !(r
->cond
& params
->flags
))
378 * The 'deps' array includes maximum three dependencies
379 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
380 * member of this array is a sentinel and should be
383 * This rule should be processed in this time when dependent
384 * parameters were changed at former applications of the other
387 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
388 if (vstamps
[r
->deps
[d
]] > rstamps
[k
])
394 if (trace_hw_mask_param_enabled()) {
395 if (hw_is_mask(r
->var
))
396 old_mask
= *hw_param_mask(params
, r
->var
);
398 if (trace_hw_interval_param_enabled()) {
399 if (hw_is_interval(r
->var
))
400 old_interval
= *hw_param_interval(params
, r
->var
);
403 changed
= r
->func(params
, r
);
408 * When the parameter is changed, notify it to the caller
409 * by corresponding returned bit, then preparing for next
412 if (changed
&& r
->var
>= 0) {
413 if (hw_is_mask(r
->var
)) {
414 trace_hw_mask_param(substream
, r
->var
,
416 hw_param_mask(params
, r
->var
));
418 if (hw_is_interval(r
->var
)) {
419 trace_hw_interval_param(substream
, r
->var
,
420 k
+ 1, &old_interval
,
421 hw_param_interval(params
, r
->var
));
424 params
->cmask
|= (1 << r
->var
);
425 vstamps
[r
->var
] = stamp
;
429 rstamps
[k
] = stamp
++;
432 /* Iterate to evaluate all rules till no parameters are changed. */
439 static int fixup_unreferenced_params(struct snd_pcm_substream
*substream
,
440 struct snd_pcm_hw_params
*params
)
442 const struct snd_interval
*i
;
443 const struct snd_mask
*m
;
446 if (!params
->msbits
) {
447 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
448 if (snd_interval_single(i
))
449 params
->msbits
= snd_interval_value(i
);
452 if (!params
->rate_den
) {
453 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_RATE
);
454 if (snd_interval_single(i
)) {
455 params
->rate_num
= snd_interval_value(i
);
456 params
->rate_den
= 1;
460 if (!params
->fifo_size
) {
461 m
= hw_param_mask_c(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
462 i
= hw_param_interval_c(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
463 if (snd_mask_single(m
) && snd_interval_single(i
)) {
464 err
= substream
->ops
->ioctl(substream
,
465 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
472 params
->info
= substream
->runtime
->hw
.info
;
473 params
->info
&= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES
|
474 SNDRV_PCM_INFO_DRAIN_TRIGGER
);
475 if (!hw_support_mmap(substream
))
476 params
->info
&= ~(SNDRV_PCM_INFO_MMAP
|
477 SNDRV_PCM_INFO_MMAP_VALID
);
483 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
484 struct snd_pcm_hw_params
*params
)
489 params
->fifo_size
= 0;
490 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
492 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
493 params
->rate_num
= 0;
494 params
->rate_den
= 0;
497 err
= constrain_mask_params(substream
, params
);
501 err
= constrain_interval_params(substream
, params
);
505 err
= constrain_params_by_rules(substream
, params
);
513 EXPORT_SYMBOL(snd_pcm_hw_refine
);
515 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
516 struct snd_pcm_hw_params __user
* _params
)
518 struct snd_pcm_hw_params
*params
;
521 params
= memdup_user(_params
, sizeof(*params
));
523 return PTR_ERR(params
);
525 err
= snd_pcm_hw_refine(substream
, params
);
529 err
= fixup_unreferenced_params(substream
, params
);
533 if (copy_to_user(_params
, params
, sizeof(*params
)))
540 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
545 return -1; /* invalid */
547 /* take 75% of period time as the deadline */
548 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
549 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
555 static void snd_pcm_set_state(struct snd_pcm_substream
*substream
, int state
)
557 snd_pcm_stream_lock_irq(substream
);
558 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DISCONNECTED
)
559 substream
->runtime
->status
->state
= state
;
560 snd_pcm_stream_unlock_irq(substream
);
563 static inline void snd_pcm_timer_notify(struct snd_pcm_substream
*substream
,
566 #ifdef CONFIG_SND_PCM_TIMER
567 if (substream
->timer
)
568 snd_timer_notify(substream
->timer
, event
,
569 &substream
->runtime
->trigger_tstamp
);
574 * snd_pcm_hw_param_choose - choose a configuration defined by @params
576 * @params: the hw_params instance
578 * Choose one configuration from configuration space defined by @params.
579 * The configuration chosen is that obtained fixing in this order:
580 * first access, first format, first subformat, min channels,
581 * min rate, min period time, max buffer size, min tick time
583 * Return: Zero if successful, or a negative error code on failure.
585 static int snd_pcm_hw_params_choose(struct snd_pcm_substream
*pcm
,
586 struct snd_pcm_hw_params
*params
)
588 static const int vars
[] = {
589 SNDRV_PCM_HW_PARAM_ACCESS
,
590 SNDRV_PCM_HW_PARAM_FORMAT
,
591 SNDRV_PCM_HW_PARAM_SUBFORMAT
,
592 SNDRV_PCM_HW_PARAM_CHANNELS
,
593 SNDRV_PCM_HW_PARAM_RATE
,
594 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
595 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
596 SNDRV_PCM_HW_PARAM_TICK_TIME
,
600 struct snd_mask old_mask
;
601 struct snd_interval old_interval
;
604 for (v
= vars
; *v
!= -1; v
++) {
605 /* Keep old parameter to trace. */
606 if (trace_hw_mask_param_enabled()) {
608 old_mask
= *hw_param_mask(params
, *v
);
610 if (trace_hw_interval_param_enabled()) {
611 if (hw_is_interval(*v
))
612 old_interval
= *hw_param_interval(params
, *v
);
614 if (*v
!= SNDRV_PCM_HW_PARAM_BUFFER_SIZE
)
615 changed
= snd_pcm_hw_param_first(pcm
, params
, *v
, NULL
);
617 changed
= snd_pcm_hw_param_last(pcm
, params
, *v
, NULL
);
618 if (snd_BUG_ON(changed
< 0))
623 /* Trace the changed parameter. */
624 if (hw_is_mask(*v
)) {
625 trace_hw_mask_param(pcm
, *v
, 0, &old_mask
,
626 hw_param_mask(params
, *v
));
628 if (hw_is_interval(*v
)) {
629 trace_hw_interval_param(pcm
, *v
, 0, &old_interval
,
630 hw_param_interval(params
, *v
));
637 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
638 struct snd_pcm_hw_params
*params
)
640 struct snd_pcm_runtime
*runtime
;
643 snd_pcm_uframes_t frames
;
645 if (PCM_RUNTIME_CHECK(substream
))
647 runtime
= substream
->runtime
;
648 snd_pcm_stream_lock_irq(substream
);
649 switch (runtime
->status
->state
) {
650 case SNDRV_PCM_STATE_OPEN
:
651 case SNDRV_PCM_STATE_SETUP
:
652 case SNDRV_PCM_STATE_PREPARED
:
655 snd_pcm_stream_unlock_irq(substream
);
658 snd_pcm_stream_unlock_irq(substream
);
659 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
660 if (!substream
->oss
.oss
)
662 if (atomic_read(&substream
->mmap_count
))
666 err
= snd_pcm_hw_refine(substream
, params
);
670 err
= snd_pcm_hw_params_choose(substream
, params
);
674 err
= fixup_unreferenced_params(substream
, params
);
678 if (substream
->ops
->hw_params
!= NULL
) {
679 err
= substream
->ops
->hw_params(substream
, params
);
684 runtime
->access
= params_access(params
);
685 runtime
->format
= params_format(params
);
686 runtime
->subformat
= params_subformat(params
);
687 runtime
->channels
= params_channels(params
);
688 runtime
->rate
= params_rate(params
);
689 runtime
->period_size
= params_period_size(params
);
690 runtime
->periods
= params_periods(params
);
691 runtime
->buffer_size
= params_buffer_size(params
);
692 runtime
->info
= params
->info
;
693 runtime
->rate_num
= params
->rate_num
;
694 runtime
->rate_den
= params
->rate_den
;
695 runtime
->no_period_wakeup
=
696 (params
->info
& SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
) &&
697 (params
->flags
& SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
);
699 bits
= snd_pcm_format_physical_width(runtime
->format
);
700 runtime
->sample_bits
= bits
;
701 bits
*= runtime
->channels
;
702 runtime
->frame_bits
= bits
;
704 while (bits
% 8 != 0) {
708 runtime
->byte_align
= bits
/ 8;
709 runtime
->min_align
= frames
;
711 /* Default sw params */
712 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
713 runtime
->period_step
= 1;
714 runtime
->control
->avail_min
= runtime
->period_size
;
715 runtime
->start_threshold
= 1;
716 runtime
->stop_threshold
= runtime
->buffer_size
;
717 runtime
->silence_threshold
= 0;
718 runtime
->silence_size
= 0;
719 runtime
->boundary
= runtime
->buffer_size
;
720 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
721 runtime
->boundary
*= 2;
723 snd_pcm_timer_resolution_change(substream
);
724 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_SETUP
);
726 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
727 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
728 if ((usecs
= period_to_usecs(runtime
)) >= 0)
729 pm_qos_add_request(&substream
->latency_pm_qos_req
,
730 PM_QOS_CPU_DMA_LATENCY
, usecs
);
733 /* hardware might be unusable from this time,
734 so we force application to retry to set
735 the correct hardware parameter settings */
736 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
737 if (substream
->ops
->hw_free
!= NULL
)
738 substream
->ops
->hw_free(substream
);
742 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
743 struct snd_pcm_hw_params __user
* _params
)
745 struct snd_pcm_hw_params
*params
;
748 params
= memdup_user(_params
, sizeof(*params
));
750 return PTR_ERR(params
);
752 err
= snd_pcm_hw_params(substream
, params
);
756 if (copy_to_user(_params
, params
, sizeof(*params
)))
763 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
765 struct snd_pcm_runtime
*runtime
;
768 if (PCM_RUNTIME_CHECK(substream
))
770 runtime
= substream
->runtime
;
771 snd_pcm_stream_lock_irq(substream
);
772 switch (runtime
->status
->state
) {
773 case SNDRV_PCM_STATE_SETUP
:
774 case SNDRV_PCM_STATE_PREPARED
:
777 snd_pcm_stream_unlock_irq(substream
);
780 snd_pcm_stream_unlock_irq(substream
);
781 if (atomic_read(&substream
->mmap_count
))
783 if (substream
->ops
->hw_free
)
784 result
= substream
->ops
->hw_free(substream
);
785 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
786 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
790 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
791 struct snd_pcm_sw_params
*params
)
793 struct snd_pcm_runtime
*runtime
;
796 if (PCM_RUNTIME_CHECK(substream
))
798 runtime
= substream
->runtime
;
799 snd_pcm_stream_lock_irq(substream
);
800 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
801 snd_pcm_stream_unlock_irq(substream
);
804 snd_pcm_stream_unlock_irq(substream
);
806 if (params
->tstamp_mode
< 0 ||
807 params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
809 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
810 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
812 if (params
->avail_min
== 0)
814 if (params
->silence_size
>= runtime
->boundary
) {
815 if (params
->silence_threshold
!= 0)
818 if (params
->silence_size
> params
->silence_threshold
)
820 if (params
->silence_threshold
> runtime
->buffer_size
)
824 snd_pcm_stream_lock_irq(substream
);
825 runtime
->tstamp_mode
= params
->tstamp_mode
;
826 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
827 runtime
->tstamp_type
= params
->tstamp_type
;
828 runtime
->period_step
= params
->period_step
;
829 runtime
->control
->avail_min
= params
->avail_min
;
830 runtime
->start_threshold
= params
->start_threshold
;
831 runtime
->stop_threshold
= params
->stop_threshold
;
832 runtime
->silence_threshold
= params
->silence_threshold
;
833 runtime
->silence_size
= params
->silence_size
;
834 params
->boundary
= runtime
->boundary
;
835 if (snd_pcm_running(substream
)) {
836 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
837 runtime
->silence_size
> 0)
838 snd_pcm_playback_silence(substream
, ULONG_MAX
);
839 err
= snd_pcm_update_state(substream
, runtime
);
841 snd_pcm_stream_unlock_irq(substream
);
845 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
846 struct snd_pcm_sw_params __user
* _params
)
848 struct snd_pcm_sw_params params
;
850 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
852 err
= snd_pcm_sw_params(substream
, ¶ms
);
853 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
858 int snd_pcm_status(struct snd_pcm_substream
*substream
,
859 struct snd_pcm_status
*status
)
861 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
863 snd_pcm_stream_lock_irq(substream
);
865 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
866 &runtime
->audio_tstamp_config
);
868 /* backwards compatible behavior */
869 if (runtime
->audio_tstamp_config
.type_requested
==
870 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
871 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
872 runtime
->audio_tstamp_config
.type_requested
=
873 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
875 runtime
->audio_tstamp_config
.type_requested
=
876 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
877 runtime
->audio_tstamp_report
.valid
= 0;
879 runtime
->audio_tstamp_report
.valid
= 1;
881 status
->state
= runtime
->status
->state
;
882 status
->suspended_state
= runtime
->status
->suspended_state
;
883 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
885 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
886 if (snd_pcm_running(substream
)) {
887 snd_pcm_update_hw_ptr(substream
);
888 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
889 status
->tstamp
= runtime
->status
->tstamp
;
890 status
->driver_tstamp
= runtime
->driver_tstamp
;
891 status
->audio_tstamp
=
892 runtime
->status
->audio_tstamp
;
893 if (runtime
->audio_tstamp_report
.valid
== 1)
894 /* backwards compatibility, no report provided in COMPAT mode */
895 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
896 &status
->audio_tstamp_accuracy
,
897 &runtime
->audio_tstamp_report
);
902 /* get tstamp only in fallback mode and only if enabled */
903 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
904 snd_pcm_gettime(runtime
, &status
->tstamp
);
907 status
->appl_ptr
= runtime
->control
->appl_ptr
;
908 status
->hw_ptr
= runtime
->status
->hw_ptr
;
909 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
910 status
->avail
= snd_pcm_playback_avail(runtime
);
911 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
912 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
913 status
->delay
= runtime
->buffer_size
- status
->avail
;
914 status
->delay
+= runtime
->delay
;
918 status
->avail
= snd_pcm_capture_avail(runtime
);
919 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
920 status
->delay
= status
->avail
+ runtime
->delay
;
924 status
->avail_max
= runtime
->avail_max
;
925 status
->overrange
= runtime
->overrange
;
926 runtime
->avail_max
= 0;
927 runtime
->overrange
= 0;
929 snd_pcm_stream_unlock_irq(substream
);
933 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
934 struct snd_pcm_status __user
* _status
,
937 struct snd_pcm_status status
;
940 memset(&status
, 0, sizeof(status
));
942 * with extension, parameters are read/write,
943 * get audio_tstamp_data from user,
944 * ignore rest of status structure
946 if (ext
&& get_user(status
.audio_tstamp_data
,
947 (u32 __user
*)(&_status
->audio_tstamp_data
)))
949 res
= snd_pcm_status(substream
, &status
);
952 if (copy_to_user(_status
, &status
, sizeof(status
)))
957 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
958 struct snd_pcm_channel_info
* info
)
960 struct snd_pcm_runtime
*runtime
;
961 unsigned int channel
;
963 channel
= info
->channel
;
964 runtime
= substream
->runtime
;
965 snd_pcm_stream_lock_irq(substream
);
966 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
967 snd_pcm_stream_unlock_irq(substream
);
970 snd_pcm_stream_unlock_irq(substream
);
971 if (channel
>= runtime
->channels
)
973 memset(info
, 0, sizeof(*info
));
974 info
->channel
= channel
;
975 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
978 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
979 struct snd_pcm_channel_info __user
* _info
)
981 struct snd_pcm_channel_info info
;
984 if (copy_from_user(&info
, _info
, sizeof(info
)))
986 res
= snd_pcm_channel_info(substream
, &info
);
989 if (copy_to_user(_info
, &info
, sizeof(info
)))
994 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
996 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
997 if (runtime
->trigger_master
== NULL
)
999 if (runtime
->trigger_master
== substream
) {
1000 if (!runtime
->trigger_tstamp_latched
)
1001 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
1003 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
1004 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
1006 runtime
->trigger_master
= NULL
;
1010 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
1011 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
1012 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
1013 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
1017 * this functions is core for handling of linked stream
1018 * Note: the stream state might be changed also on failure
1019 * Note2: call with calling stream lock + link lock
1021 static int snd_pcm_action_group(const struct action_ops
*ops
,
1022 struct snd_pcm_substream
*substream
,
1023 int state
, int do_lock
)
1025 struct snd_pcm_substream
*s
= NULL
;
1026 struct snd_pcm_substream
*s1
;
1027 int res
= 0, depth
= 1;
1029 snd_pcm_group_for_each_entry(s
, substream
) {
1030 if (do_lock
&& s
!= substream
) {
1031 if (s
->pcm
->nonatomic
)
1032 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
1034 spin_lock_nested(&s
->self_group
.lock
, depth
);
1037 res
= ops
->pre_action(s
, state
);
1041 snd_pcm_group_for_each_entry(s
, substream
) {
1042 res
= ops
->do_action(s
, state
);
1044 if (ops
->undo_action
) {
1045 snd_pcm_group_for_each_entry(s1
, substream
) {
1046 if (s1
== s
) /* failed stream */
1048 ops
->undo_action(s1
, state
);
1051 s
= NULL
; /* unlock all */
1055 snd_pcm_group_for_each_entry(s
, substream
) {
1056 ops
->post_action(s
, state
);
1060 /* unlock streams */
1061 snd_pcm_group_for_each_entry(s1
, substream
) {
1062 if (s1
!= substream
) {
1063 if (s1
->pcm
->nonatomic
)
1064 mutex_unlock(&s1
->self_group
.mutex
);
1066 spin_unlock(&s1
->self_group
.lock
);
1068 if (s1
== s
) /* end */
1076 * Note: call with stream lock
1078 static int snd_pcm_action_single(const struct action_ops
*ops
,
1079 struct snd_pcm_substream
*substream
,
1084 res
= ops
->pre_action(substream
, state
);
1087 res
= ops
->do_action(substream
, state
);
1089 ops
->post_action(substream
, state
);
1090 else if (ops
->undo_action
)
1091 ops
->undo_action(substream
, state
);
1096 * Note: call with stream lock
1098 static int snd_pcm_action(const struct action_ops
*ops
,
1099 struct snd_pcm_substream
*substream
,
1104 if (!snd_pcm_stream_linked(substream
))
1105 return snd_pcm_action_single(ops
, substream
, state
);
1107 if (substream
->pcm
->nonatomic
) {
1108 if (!mutex_trylock(&substream
->group
->mutex
)) {
1109 mutex_unlock(&substream
->self_group
.mutex
);
1110 mutex_lock(&substream
->group
->mutex
);
1111 mutex_lock(&substream
->self_group
.mutex
);
1113 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1114 mutex_unlock(&substream
->group
->mutex
);
1116 if (!spin_trylock(&substream
->group
->lock
)) {
1117 spin_unlock(&substream
->self_group
.lock
);
1118 spin_lock(&substream
->group
->lock
);
1119 spin_lock(&substream
->self_group
.lock
);
1121 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
1122 spin_unlock(&substream
->group
->lock
);
1128 * Note: don't use any locks before
1130 static int snd_pcm_action_lock_irq(const struct action_ops
*ops
,
1131 struct snd_pcm_substream
*substream
,
1136 snd_pcm_stream_lock_irq(substream
);
1137 res
= snd_pcm_action(ops
, substream
, state
);
1138 snd_pcm_stream_unlock_irq(substream
);
1144 static int snd_pcm_action_nonatomic(const struct action_ops
*ops
,
1145 struct snd_pcm_substream
*substream
,
1150 down_read(&snd_pcm_link_rwsem
);
1151 if (snd_pcm_stream_linked(substream
))
1152 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1154 res
= snd_pcm_action_single(ops
, substream
, state
);
1155 up_read(&snd_pcm_link_rwsem
);
1162 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1164 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1165 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1167 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1168 !snd_pcm_playback_data(substream
))
1170 runtime
->trigger_tstamp_latched
= false;
1171 runtime
->trigger_master
= substream
;
1175 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1177 if (substream
->runtime
->trigger_master
!= substream
)
1179 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1182 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1184 if (substream
->runtime
->trigger_master
== substream
)
1185 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1188 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1190 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1191 snd_pcm_trigger_tstamp(substream
);
1192 runtime
->hw_ptr_jiffies
= jiffies
;
1193 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1195 runtime
->status
->state
= state
;
1196 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1197 runtime
->silence_size
> 0)
1198 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1199 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTART
);
1202 static const struct action_ops snd_pcm_action_start
= {
1203 .pre_action
= snd_pcm_pre_start
,
1204 .do_action
= snd_pcm_do_start
,
1205 .undo_action
= snd_pcm_undo_start
,
1206 .post_action
= snd_pcm_post_start
1210 * snd_pcm_start - start all linked streams
1211 * @substream: the PCM substream instance
1213 * Return: Zero if successful, or a negative error code.
1214 * The stream lock must be acquired before calling this function.
1216 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1218 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1219 SNDRV_PCM_STATE_RUNNING
);
1222 /* take the stream lock and start the streams */
1223 static int snd_pcm_start_lock_irq(struct snd_pcm_substream
*substream
)
1225 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
,
1226 SNDRV_PCM_STATE_RUNNING
);
1232 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1234 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1235 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1237 runtime
->trigger_master
= substream
;
1241 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1243 if (substream
->runtime
->trigger_master
== substream
&&
1244 snd_pcm_running(substream
))
1245 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1246 return 0; /* unconditonally stop all substreams */
1249 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1251 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1252 if (runtime
->status
->state
!= state
) {
1253 snd_pcm_trigger_tstamp(substream
);
1254 runtime
->status
->state
= state
;
1255 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSTOP
);
1257 wake_up(&runtime
->sleep
);
1258 wake_up(&runtime
->tsleep
);
1261 static const struct action_ops snd_pcm_action_stop
= {
1262 .pre_action
= snd_pcm_pre_stop
,
1263 .do_action
= snd_pcm_do_stop
,
1264 .post_action
= snd_pcm_post_stop
1268 * snd_pcm_stop - try to stop all running streams in the substream group
1269 * @substream: the PCM substream instance
1270 * @state: PCM state after stopping the stream
1272 * The state of each stream is then changed to the given state unconditionally.
1274 * Return: Zero if successful, or a negative error code.
1276 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1278 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1280 EXPORT_SYMBOL(snd_pcm_stop
);
1283 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1284 * @substream: the PCM substream
1286 * After stopping, the state is changed to SETUP.
1287 * Unlike snd_pcm_stop(), this affects only the given stream.
1289 * Return: Zero if succesful, or a negative error code.
1291 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1293 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1294 SNDRV_PCM_STATE_SETUP
);
1298 * snd_pcm_stop_xrun - stop the running streams as XRUN
1299 * @substream: the PCM substream instance
1301 * This stops the given running substream (and all linked substreams) as XRUN.
1302 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1304 * Return: Zero if successful, or a negative error code.
1306 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1308 unsigned long flags
;
1311 snd_pcm_stream_lock_irqsave(substream
, flags
);
1312 if (snd_pcm_running(substream
))
1313 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1314 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1317 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1322 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1324 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1325 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1328 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1330 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1332 runtime
->trigger_master
= substream
;
1336 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1338 if (substream
->runtime
->trigger_master
!= substream
)
1340 /* some drivers might use hw_ptr to recover from the pause -
1341 update the hw_ptr now */
1343 snd_pcm_update_hw_ptr(substream
);
1344 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1345 * a delta between the current jiffies, this gives a large enough
1346 * delta, effectively to skip the check once.
1348 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1349 return substream
->ops
->trigger(substream
,
1350 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1351 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1354 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1356 if (substream
->runtime
->trigger_master
== substream
)
1357 substream
->ops
->trigger(substream
,
1358 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1359 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1362 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1364 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1365 snd_pcm_trigger_tstamp(substream
);
1367 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1368 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MPAUSE
);
1369 wake_up(&runtime
->sleep
);
1370 wake_up(&runtime
->tsleep
);
1372 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1373 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MCONTINUE
);
1377 static const struct action_ops snd_pcm_action_pause
= {
1378 .pre_action
= snd_pcm_pre_pause
,
1379 .do_action
= snd_pcm_do_pause
,
1380 .undo_action
= snd_pcm_undo_pause
,
1381 .post_action
= snd_pcm_post_pause
1385 * Push/release the pause for all linked streams.
1387 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1389 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1395 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1397 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1398 switch (runtime
->status
->state
) {
1399 case SNDRV_PCM_STATE_SUSPENDED
:
1401 /* unresumable PCM state; return -EBUSY for skipping suspend */
1402 case SNDRV_PCM_STATE_OPEN
:
1403 case SNDRV_PCM_STATE_SETUP
:
1404 case SNDRV_PCM_STATE_DISCONNECTED
:
1407 runtime
->trigger_master
= substream
;
1411 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1413 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1414 if (runtime
->trigger_master
!= substream
)
1416 if (! snd_pcm_running(substream
))
1418 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1419 return 0; /* suspend unconditionally */
1422 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1424 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1425 snd_pcm_trigger_tstamp(substream
);
1426 runtime
->status
->suspended_state
= runtime
->status
->state
;
1427 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1428 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MSUSPEND
);
1429 wake_up(&runtime
->sleep
);
1430 wake_up(&runtime
->tsleep
);
1433 static const struct action_ops snd_pcm_action_suspend
= {
1434 .pre_action
= snd_pcm_pre_suspend
,
1435 .do_action
= snd_pcm_do_suspend
,
1436 .post_action
= snd_pcm_post_suspend
1440 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1441 * @substream: the PCM substream
1443 * After this call, all streams are changed to SUSPENDED state.
1445 * Return: Zero if successful (or @substream is %NULL), or a negative error
1448 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1451 unsigned long flags
;
1456 snd_pcm_stream_lock_irqsave(substream
, flags
);
1457 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1458 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1461 EXPORT_SYMBOL(snd_pcm_suspend
);
1464 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1465 * @pcm: the PCM instance
1467 * After this call, all streams are changed to SUSPENDED state.
1469 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1471 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1473 struct snd_pcm_substream
*substream
;
1474 int stream
, err
= 0;
1479 for (stream
= 0; stream
< 2; stream
++) {
1480 for (substream
= pcm
->streams
[stream
].substream
;
1481 substream
; substream
= substream
->next
) {
1482 /* FIXME: the open/close code should lock this as well */
1483 if (substream
->runtime
== NULL
)
1487 * Skip BE dai link PCM's that are internal and may
1488 * not have their substream ops set.
1490 if (!substream
->ops
)
1493 err
= snd_pcm_suspend(substream
);
1494 if (err
< 0 && err
!= -EBUSY
)
1500 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1504 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1506 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1507 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1509 runtime
->trigger_master
= substream
;
1513 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1515 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1516 if (runtime
->trigger_master
!= substream
)
1518 /* DMA not running previously? */
1519 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1520 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1521 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1523 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1526 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1528 if (substream
->runtime
->trigger_master
== substream
&&
1529 snd_pcm_running(substream
))
1530 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1533 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1535 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1536 snd_pcm_trigger_tstamp(substream
);
1537 runtime
->status
->state
= runtime
->status
->suspended_state
;
1538 snd_pcm_timer_notify(substream
, SNDRV_TIMER_EVENT_MRESUME
);
1541 static const struct action_ops snd_pcm_action_resume
= {
1542 .pre_action
= snd_pcm_pre_resume
,
1543 .do_action
= snd_pcm_do_resume
,
1544 .undo_action
= snd_pcm_undo_resume
,
1545 .post_action
= snd_pcm_post_resume
1548 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1550 return snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1555 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1560 #endif /* CONFIG_PM */
1565 * Change the RUNNING stream(s) to XRUN state.
1567 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1569 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1572 snd_pcm_stream_lock_irq(substream
);
1573 switch (runtime
->status
->state
) {
1574 case SNDRV_PCM_STATE_XRUN
:
1575 result
= 0; /* already there */
1577 case SNDRV_PCM_STATE_RUNNING
:
1578 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1583 snd_pcm_stream_unlock_irq(substream
);
1590 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1592 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1593 switch (runtime
->status
->state
) {
1594 case SNDRV_PCM_STATE_RUNNING
:
1595 case SNDRV_PCM_STATE_PREPARED
:
1596 case SNDRV_PCM_STATE_PAUSED
:
1597 case SNDRV_PCM_STATE_SUSPENDED
:
1604 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1606 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1607 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1610 runtime
->hw_ptr_base
= 0;
1611 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1612 runtime
->status
->hw_ptr
% runtime
->period_size
;
1613 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1614 runtime
->silence_filled
= 0;
1618 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1620 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1621 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1622 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1623 runtime
->silence_size
> 0)
1624 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1627 static const struct action_ops snd_pcm_action_reset
= {
1628 .pre_action
= snd_pcm_pre_reset
,
1629 .do_action
= snd_pcm_do_reset
,
1630 .post_action
= snd_pcm_post_reset
1633 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1635 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1641 /* we use the second argument for updating f_flags */
1642 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1645 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1646 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1647 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1649 if (snd_pcm_running(substream
))
1651 substream
->f_flags
= f_flags
;
1655 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1658 err
= substream
->ops
->prepare(substream
);
1661 return snd_pcm_do_reset(substream
, 0);
1664 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1666 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1667 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1668 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1671 static const struct action_ops snd_pcm_action_prepare
= {
1672 .pre_action
= snd_pcm_pre_prepare
,
1673 .do_action
= snd_pcm_do_prepare
,
1674 .post_action
= snd_pcm_post_prepare
1678 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1679 * @substream: the PCM substream instance
1680 * @file: file to refer f_flags
1682 * Return: Zero if successful, or a negative error code.
1684 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1690 f_flags
= file
->f_flags
;
1692 f_flags
= substream
->f_flags
;
1694 snd_pcm_stream_lock_irq(substream
);
1695 switch (substream
->runtime
->status
->state
) {
1696 case SNDRV_PCM_STATE_PAUSED
:
1697 snd_pcm_pause(substream
, 0);
1699 case SNDRV_PCM_STATE_SUSPENDED
:
1700 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1703 snd_pcm_stream_unlock_irq(substream
);
1705 return snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1706 substream
, f_flags
);
1713 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1715 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1716 switch (runtime
->status
->state
) {
1717 case SNDRV_PCM_STATE_OPEN
:
1718 case SNDRV_PCM_STATE_DISCONNECTED
:
1719 case SNDRV_PCM_STATE_SUSPENDED
:
1722 runtime
->trigger_master
= substream
;
1726 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1728 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1729 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1730 switch (runtime
->status
->state
) {
1731 case SNDRV_PCM_STATE_PREPARED
:
1732 /* start playback stream if possible */
1733 if (! snd_pcm_playback_empty(substream
)) {
1734 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1735 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1737 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1740 case SNDRV_PCM_STATE_RUNNING
:
1741 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1743 case SNDRV_PCM_STATE_XRUN
:
1744 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1750 /* stop running stream */
1751 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1752 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1753 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1754 snd_pcm_do_stop(substream
, new_state
);
1755 snd_pcm_post_stop(substream
, new_state
);
1759 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1760 runtime
->trigger_master
== substream
&&
1761 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1762 return substream
->ops
->trigger(substream
,
1763 SNDRV_PCM_TRIGGER_DRAIN
);
1768 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1772 static const struct action_ops snd_pcm_action_drain_init
= {
1773 .pre_action
= snd_pcm_pre_drain_init
,
1774 .do_action
= snd_pcm_do_drain_init
,
1775 .post_action
= snd_pcm_post_drain_init
1778 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1781 * Drain the stream(s).
1782 * When the substream is linked, sync until the draining of all playback streams
1784 * After this call, all streams are supposed to be either SETUP or DRAINING
1785 * (capture only) state.
1787 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1790 struct snd_card
*card
;
1791 struct snd_pcm_runtime
*runtime
;
1792 struct snd_pcm_substream
*s
;
1793 wait_queue_entry_t wait
;
1797 card
= substream
->pcm
->card
;
1798 runtime
= substream
->runtime
;
1800 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1804 if (file
->f_flags
& O_NONBLOCK
)
1806 } else if (substream
->f_flags
& O_NONBLOCK
)
1809 down_read(&snd_pcm_link_rwsem
);
1810 snd_pcm_stream_lock_irq(substream
);
1812 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1813 snd_pcm_pause(substream
, 0);
1815 /* pre-start/stop - all running streams are changed to DRAINING state */
1816 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1819 /* in non-blocking, we don't wait in ioctl but let caller poll */
1827 struct snd_pcm_runtime
*to_check
;
1828 if (signal_pending(current
)) {
1829 result
= -ERESTARTSYS
;
1832 /* find a substream to drain */
1834 snd_pcm_group_for_each_entry(s
, substream
) {
1835 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1837 runtime
= s
->runtime
;
1838 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1844 break; /* all drained */
1845 init_waitqueue_entry(&wait
, current
);
1846 add_wait_queue(&to_check
->sleep
, &wait
);
1847 snd_pcm_stream_unlock_irq(substream
);
1848 up_read(&snd_pcm_link_rwsem
);
1849 if (runtime
->no_period_wakeup
)
1850 tout
= MAX_SCHEDULE_TIMEOUT
;
1853 if (runtime
->rate
) {
1854 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1855 tout
= max(t
, tout
);
1857 tout
= msecs_to_jiffies(tout
* 1000);
1859 tout
= schedule_timeout_interruptible(tout
);
1860 down_read(&snd_pcm_link_rwsem
);
1861 snd_pcm_stream_lock_irq(substream
);
1862 remove_wait_queue(&to_check
->sleep
, &wait
);
1863 if (card
->shutdown
) {
1868 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1871 dev_dbg(substream
->pcm
->card
->dev
,
1872 "playback drain error (DMA or IRQ trouble?)\n");
1873 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1881 snd_pcm_stream_unlock_irq(substream
);
1882 up_read(&snd_pcm_link_rwsem
);
1890 * Immediately put all linked substreams into SETUP state.
1892 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1894 struct snd_pcm_runtime
*runtime
;
1897 if (PCM_RUNTIME_CHECK(substream
))
1899 runtime
= substream
->runtime
;
1901 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1902 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1905 snd_pcm_stream_lock_irq(substream
);
1907 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1908 snd_pcm_pause(substream
, 0);
1910 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1911 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1912 snd_pcm_stream_unlock_irq(substream
);
1918 static bool is_pcm_file(struct file
*file
)
1920 struct inode
*inode
= file_inode(file
);
1923 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1925 minor
= iminor(inode
);
1926 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1927 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1933 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1936 struct snd_pcm_file
*pcm_file
;
1937 struct snd_pcm_substream
*substream1
;
1938 struct snd_pcm_group
*group
;
1939 struct fd f
= fdget(fd
);
1943 if (!is_pcm_file(f
.file
)) {
1947 pcm_file
= f
.file
->private_data
;
1948 substream1
= pcm_file
->substream
;
1949 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1954 down_write_nonfifo(&snd_pcm_link_rwsem
);
1955 write_lock_irq(&snd_pcm_link_rwlock
);
1956 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1957 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1958 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1962 if (snd_pcm_stream_linked(substream1
)) {
1966 if (!snd_pcm_stream_linked(substream
)) {
1967 substream
->group
= group
;
1969 spin_lock_init(&substream
->group
->lock
);
1970 mutex_init(&substream
->group
->mutex
);
1971 INIT_LIST_HEAD(&substream
->group
->substreams
);
1972 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1973 substream
->group
->count
= 1;
1975 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1976 substream
->group
->count
++;
1977 substream1
->group
= substream
->group
;
1979 write_unlock_irq(&snd_pcm_link_rwlock
);
1980 up_write(&snd_pcm_link_rwsem
);
1982 snd_card_unref(substream1
->pcm
->card
);
1989 static void relink_to_local(struct snd_pcm_substream
*substream
)
1991 substream
->group
= &substream
->self_group
;
1992 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1993 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1996 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1998 struct snd_pcm_substream
*s
;
2001 down_write_nonfifo(&snd_pcm_link_rwsem
);
2002 write_lock_irq(&snd_pcm_link_rwlock
);
2003 if (!snd_pcm_stream_linked(substream
)) {
2007 list_del(&substream
->link_list
);
2008 substream
->group
->count
--;
2009 if (substream
->group
->count
== 1) { /* detach the last stream, too */
2010 snd_pcm_group_for_each_entry(s
, substream
) {
2014 kfree(substream
->group
);
2016 relink_to_local(substream
);
2018 write_unlock_irq(&snd_pcm_link_rwlock
);
2019 up_write(&snd_pcm_link_rwsem
);
2026 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
2027 struct snd_pcm_hw_rule
*rule
)
2029 struct snd_interval t
;
2030 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
2031 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2032 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2035 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
2036 struct snd_pcm_hw_rule
*rule
)
2038 struct snd_interval t
;
2039 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
2040 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2041 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2044 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
2045 struct snd_pcm_hw_rule
*rule
)
2047 struct snd_interval t
;
2048 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
2049 hw_param_interval_c(params
, rule
->deps
[1]),
2050 (unsigned long) rule
->private, &t
);
2051 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2054 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
2055 struct snd_pcm_hw_rule
*rule
)
2057 struct snd_interval t
;
2058 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
2059 (unsigned long) rule
->private,
2060 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
2061 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2064 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
2065 struct snd_pcm_hw_rule
*rule
)
2068 const struct snd_interval
*i
=
2069 hw_param_interval_c(params
, rule
->deps
[0]);
2071 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
2073 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2075 if (! snd_mask_test(mask
, k
))
2077 bits
= snd_pcm_format_physical_width(k
);
2079 continue; /* ignore invalid formats */
2080 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
2081 snd_mask_reset(&m
, k
);
2083 return snd_mask_refine(mask
, &m
);
2086 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
2087 struct snd_pcm_hw_rule
*rule
)
2089 struct snd_interval t
;
2095 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
2097 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
2099 bits
= snd_pcm_format_physical_width(k
);
2101 continue; /* ignore invalid formats */
2102 if (t
.min
> (unsigned)bits
)
2104 if (t
.max
< (unsigned)bits
)
2108 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2111 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2112 #error "Change this table"
2115 static const unsigned int rates
[] = {
2116 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2117 48000, 64000, 88200, 96000, 176400, 192000
2120 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
2121 .count
= ARRAY_SIZE(rates
),
2125 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
2126 struct snd_pcm_hw_rule
*rule
)
2128 struct snd_pcm_hardware
*hw
= rule
->private;
2129 return snd_interval_list(hw_param_interval(params
, rule
->var
),
2130 snd_pcm_known_rates
.count
,
2131 snd_pcm_known_rates
.list
, hw
->rates
);
2134 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
2135 struct snd_pcm_hw_rule
*rule
)
2137 struct snd_interval t
;
2138 struct snd_pcm_substream
*substream
= rule
->private;
2140 t
.max
= substream
->buffer_bytes_max
;
2144 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2147 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2149 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2150 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2153 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2154 snd_mask_any(constrs_mask(constrs
, k
));
2157 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2158 snd_interval_any(constrs_interval(constrs
, k
));
2161 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2162 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2163 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2164 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2165 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2167 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2168 snd_pcm_hw_rule_format
, NULL
,
2169 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2172 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2173 snd_pcm_hw_rule_sample_bits
, NULL
,
2174 SNDRV_PCM_HW_PARAM_FORMAT
,
2175 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2178 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2179 snd_pcm_hw_rule_div
, NULL
,
2180 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2183 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2184 snd_pcm_hw_rule_mul
, NULL
,
2185 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2188 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2189 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2190 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2193 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2194 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2195 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2198 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2199 snd_pcm_hw_rule_div
, NULL
,
2200 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2203 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2204 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2205 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2208 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2209 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2210 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2213 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2214 snd_pcm_hw_rule_div
, NULL
,
2215 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2218 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2219 snd_pcm_hw_rule_div
, NULL
,
2220 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2223 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2224 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2225 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2228 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2229 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2230 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2233 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2234 snd_pcm_hw_rule_mul
, NULL
,
2235 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2238 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2239 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2240 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2243 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2244 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2245 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2248 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2249 snd_pcm_hw_rule_muldivk
, (void*) 8,
2250 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2253 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2254 snd_pcm_hw_rule_muldivk
, (void*) 8,
2255 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2258 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2259 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2260 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2263 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2264 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2265 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2271 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2273 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2274 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2276 unsigned int mask
= 0;
2278 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2279 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2280 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2281 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2282 if (hw_support_mmap(substream
)) {
2283 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2284 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2285 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2286 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2287 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2288 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2290 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2294 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2298 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2302 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2303 hw
->channels_min
, hw
->channels_max
);
2307 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2308 hw
->rate_min
, hw
->rate_max
);
2312 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2313 hw
->period_bytes_min
, hw
->period_bytes_max
);
2317 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2318 hw
->periods_min
, hw
->periods_max
);
2322 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2323 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2327 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2328 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2329 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2334 if (runtime
->dma_bytes
) {
2335 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2340 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2341 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2342 snd_pcm_hw_rule_rate
, hw
,
2343 SNDRV_PCM_HW_PARAM_RATE
, -1);
2348 /* FIXME: this belong to lowlevel */
2349 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2354 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2356 if (snd_pcm_stream_linked(substream
))
2357 snd_pcm_unlink(substream
);
2360 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2362 substream
->ref_count
--;
2363 if (substream
->ref_count
> 0)
2366 snd_pcm_drop(substream
);
2367 if (substream
->hw_opened
) {
2368 if (substream
->ops
->hw_free
&&
2369 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_OPEN
)
2370 substream
->ops
->hw_free(substream
);
2371 substream
->ops
->close(substream
);
2372 substream
->hw_opened
= 0;
2374 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2375 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2376 if (substream
->pcm_release
) {
2377 substream
->pcm_release(substream
);
2378 substream
->pcm_release
= NULL
;
2380 snd_pcm_detach_substream(substream
);
2382 EXPORT_SYMBOL(snd_pcm_release_substream
);
2384 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2386 struct snd_pcm_substream
**rsubstream
)
2388 struct snd_pcm_substream
*substream
;
2391 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2394 if (substream
->ref_count
> 1) {
2395 *rsubstream
= substream
;
2399 err
= snd_pcm_hw_constraints_init(substream
);
2401 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2405 if ((err
= substream
->ops
->open(substream
)) < 0)
2408 substream
->hw_opened
= 1;
2410 err
= snd_pcm_hw_constraints_complete(substream
);
2412 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2416 *rsubstream
= substream
;
2420 snd_pcm_release_substream(substream
);
2423 EXPORT_SYMBOL(snd_pcm_open_substream
);
2425 static int snd_pcm_open_file(struct file
*file
,
2426 struct snd_pcm
*pcm
,
2429 struct snd_pcm_file
*pcm_file
;
2430 struct snd_pcm_substream
*substream
;
2433 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2437 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2438 if (pcm_file
== NULL
) {
2439 snd_pcm_release_substream(substream
);
2442 pcm_file
->substream
= substream
;
2443 if (substream
->ref_count
== 1) {
2444 substream
->file
= pcm_file
;
2445 substream
->pcm_release
= pcm_release_private
;
2447 file
->private_data
= pcm_file
;
2452 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2454 struct snd_pcm
*pcm
;
2455 int err
= nonseekable_open(inode
, file
);
2458 pcm
= snd_lookup_minor_data(iminor(inode
),
2459 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2460 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2462 snd_card_unref(pcm
->card
);
2466 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2468 struct snd_pcm
*pcm
;
2469 int err
= nonseekable_open(inode
, file
);
2472 pcm
= snd_lookup_minor_data(iminor(inode
),
2473 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2474 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2476 snd_card_unref(pcm
->card
);
2480 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2483 wait_queue_entry_t wait
;
2489 err
= snd_card_file_add(pcm
->card
, file
);
2492 if (!try_module_get(pcm
->card
->module
)) {
2496 init_waitqueue_entry(&wait
, current
);
2497 add_wait_queue(&pcm
->open_wait
, &wait
);
2498 mutex_lock(&pcm
->open_mutex
);
2500 err
= snd_pcm_open_file(file
, pcm
, stream
);
2503 if (err
== -EAGAIN
) {
2504 if (file
->f_flags
& O_NONBLOCK
) {
2510 set_current_state(TASK_INTERRUPTIBLE
);
2511 mutex_unlock(&pcm
->open_mutex
);
2513 mutex_lock(&pcm
->open_mutex
);
2514 if (pcm
->card
->shutdown
) {
2518 if (signal_pending(current
)) {
2523 remove_wait_queue(&pcm
->open_wait
, &wait
);
2524 mutex_unlock(&pcm
->open_mutex
);
2530 module_put(pcm
->card
->module
);
2532 snd_card_file_remove(pcm
->card
, file
);
2537 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2539 struct snd_pcm
*pcm
;
2540 struct snd_pcm_substream
*substream
;
2541 struct snd_pcm_file
*pcm_file
;
2543 pcm_file
= file
->private_data
;
2544 substream
= pcm_file
->substream
;
2545 if (snd_BUG_ON(!substream
))
2547 pcm
= substream
->pcm
;
2548 mutex_lock(&pcm
->open_mutex
);
2549 snd_pcm_release_substream(substream
);
2551 mutex_unlock(&pcm
->open_mutex
);
2552 wake_up(&pcm
->open_wait
);
2553 module_put(pcm
->card
->module
);
2554 snd_card_file_remove(pcm
->card
, file
);
2558 /* check and update PCM state; return 0 or a negative error
2559 * call this inside PCM lock
2561 static int do_pcm_hwsync(struct snd_pcm_substream
*substream
)
2563 switch (substream
->runtime
->status
->state
) {
2564 case SNDRV_PCM_STATE_DRAINING
:
2565 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2568 case SNDRV_PCM_STATE_RUNNING
:
2569 return snd_pcm_update_hw_ptr(substream
);
2570 case SNDRV_PCM_STATE_PREPARED
:
2571 case SNDRV_PCM_STATE_PAUSED
:
2573 case SNDRV_PCM_STATE_SUSPENDED
:
2575 case SNDRV_PCM_STATE_XRUN
:
2582 /* increase the appl_ptr; returns the processed frames or a negative error */
2583 static snd_pcm_sframes_t
forward_appl_ptr(struct snd_pcm_substream
*substream
,
2584 snd_pcm_uframes_t frames
,
2585 snd_pcm_sframes_t avail
)
2587 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2588 snd_pcm_sframes_t appl_ptr
;
2593 if (frames
> (snd_pcm_uframes_t
)avail
)
2595 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2596 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2597 appl_ptr
-= runtime
->boundary
;
2598 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2599 return ret
< 0 ? ret
: frames
;
2602 /* decrease the appl_ptr; returns the processed frames or zero for error */
2603 static snd_pcm_sframes_t
rewind_appl_ptr(struct snd_pcm_substream
*substream
,
2604 snd_pcm_uframes_t frames
,
2605 snd_pcm_sframes_t avail
)
2607 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2608 snd_pcm_sframes_t appl_ptr
;
2613 if (frames
> (snd_pcm_uframes_t
)avail
)
2615 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2617 appl_ptr
+= runtime
->boundary
;
2618 ret
= pcm_lib_apply_appl_ptr(substream
, appl_ptr
);
2619 /* NOTE: we return zero for errors because PulseAudio gets depressed
2620 * upon receiving an error from rewind ioctl and stops processing
2621 * any longer. Returning zero means that no rewind is done, so
2622 * it's not absolutely wrong to answer like that.
2624 return ret
< 0 ? 0 : frames
;
2627 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2628 snd_pcm_uframes_t frames
)
2630 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2631 snd_pcm_sframes_t ret
;
2636 snd_pcm_stream_lock_irq(substream
);
2637 ret
= do_pcm_hwsync(substream
);
2639 ret
= rewind_appl_ptr(substream
, frames
,
2640 snd_pcm_playback_hw_avail(runtime
));
2641 snd_pcm_stream_unlock_irq(substream
);
2645 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2646 snd_pcm_uframes_t frames
)
2648 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2649 snd_pcm_sframes_t ret
;
2654 snd_pcm_stream_lock_irq(substream
);
2655 ret
= do_pcm_hwsync(substream
);
2657 ret
= rewind_appl_ptr(substream
, frames
,
2658 snd_pcm_capture_hw_avail(runtime
));
2659 snd_pcm_stream_unlock_irq(substream
);
2663 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2664 snd_pcm_uframes_t frames
)
2666 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2667 snd_pcm_sframes_t ret
;
2672 snd_pcm_stream_lock_irq(substream
);
2673 ret
= do_pcm_hwsync(substream
);
2675 ret
= forward_appl_ptr(substream
, frames
,
2676 snd_pcm_playback_avail(runtime
));
2677 snd_pcm_stream_unlock_irq(substream
);
2681 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2682 snd_pcm_uframes_t frames
)
2684 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2685 snd_pcm_sframes_t ret
;
2690 snd_pcm_stream_lock_irq(substream
);
2691 ret
= do_pcm_hwsync(substream
);
2693 ret
= forward_appl_ptr(substream
, frames
,
2694 snd_pcm_capture_avail(runtime
));
2695 snd_pcm_stream_unlock_irq(substream
);
2699 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2703 snd_pcm_stream_lock_irq(substream
);
2704 err
= do_pcm_hwsync(substream
);
2705 snd_pcm_stream_unlock_irq(substream
);
2709 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2710 snd_pcm_sframes_t
*delay
)
2712 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2714 snd_pcm_sframes_t n
= 0;
2716 snd_pcm_stream_lock_irq(substream
);
2717 err
= do_pcm_hwsync(substream
);
2719 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2720 n
= snd_pcm_playback_hw_avail(runtime
);
2722 n
= snd_pcm_capture_avail(runtime
);
2723 n
+= runtime
->delay
;
2725 snd_pcm_stream_unlock_irq(substream
);
2731 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2732 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2734 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2735 struct snd_pcm_sync_ptr sync_ptr
;
2736 volatile struct snd_pcm_mmap_status
*status
;
2737 volatile struct snd_pcm_mmap_control
*control
;
2740 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2741 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2743 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2745 status
= runtime
->status
;
2746 control
= runtime
->control
;
2747 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2748 err
= snd_pcm_hwsync(substream
);
2752 snd_pcm_stream_lock_irq(substream
);
2753 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
)) {
2754 err
= pcm_lib_apply_appl_ptr(substream
,
2755 sync_ptr
.c
.control
.appl_ptr
);
2757 snd_pcm_stream_unlock_irq(substream
);
2761 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2763 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2764 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2766 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2767 sync_ptr
.s
.status
.state
= status
->state
;
2768 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2769 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2770 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2771 sync_ptr
.s
.status
.audio_tstamp
= status
->audio_tstamp
;
2772 snd_pcm_stream_unlock_irq(substream
);
2773 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2778 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2780 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2783 if (get_user(arg
, _arg
))
2785 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2787 runtime
->tstamp_type
= arg
;
2791 static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream
*substream
,
2792 struct snd_xferi __user
*_xferi
)
2794 struct snd_xferi xferi
;
2795 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2796 snd_pcm_sframes_t result
;
2798 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2800 if (put_user(0, &_xferi
->result
))
2802 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2804 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2805 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2807 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2808 __put_user(result
, &_xferi
->result
);
2809 return result
< 0 ? result
: 0;
2812 static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream
*substream
,
2813 struct snd_xfern __user
*_xfern
)
2815 struct snd_xfern xfern
;
2816 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2818 snd_pcm_sframes_t result
;
2820 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2822 if (runtime
->channels
> 128)
2824 if (put_user(0, &_xfern
->result
))
2826 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2829 bufs
= memdup_user(xfern
.bufs
, sizeof(void *) * runtime
->channels
);
2831 return PTR_ERR(bufs
);
2832 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2833 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2835 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2837 __put_user(result
, &_xfern
->result
);
2838 return result
< 0 ? result
: 0;
2841 static int snd_pcm_rewind_ioctl(struct snd_pcm_substream
*substream
,
2842 snd_pcm_uframes_t __user
*_frames
)
2844 snd_pcm_uframes_t frames
;
2845 snd_pcm_sframes_t result
;
2847 if (get_user(frames
, _frames
))
2849 if (put_user(0, _frames
))
2851 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2852 result
= snd_pcm_playback_rewind(substream
, frames
);
2854 result
= snd_pcm_capture_rewind(substream
, frames
);
2855 __put_user(result
, _frames
);
2856 return result
< 0 ? result
: 0;
2859 static int snd_pcm_forward_ioctl(struct snd_pcm_substream
*substream
,
2860 snd_pcm_uframes_t __user
*_frames
)
2862 snd_pcm_uframes_t frames
;
2863 snd_pcm_sframes_t result
;
2865 if (get_user(frames
, _frames
))
2867 if (put_user(0, _frames
))
2869 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2870 result
= snd_pcm_playback_forward(substream
, frames
);
2872 result
= snd_pcm_capture_forward(substream
, frames
);
2873 __put_user(result
, _frames
);
2874 return result
< 0 ? result
: 0;
2877 static int snd_pcm_common_ioctl(struct file
*file
,
2878 struct snd_pcm_substream
*substream
,
2879 unsigned int cmd
, void __user
*arg
)
2881 struct snd_pcm_file
*pcm_file
= file
->private_data
;
2884 if (PCM_RUNTIME_CHECK(substream
))
2887 res
= snd_power_wait(substream
->pcm
->card
, SNDRV_CTL_POWER_D0
);
2892 case SNDRV_PCM_IOCTL_PVERSION
:
2893 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2894 case SNDRV_PCM_IOCTL_INFO
:
2895 return snd_pcm_info_user(substream
, arg
);
2896 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2898 case SNDRV_PCM_IOCTL_TTSTAMP
:
2899 return snd_pcm_tstamp(substream
, arg
);
2900 case SNDRV_PCM_IOCTL_USER_PVERSION
:
2901 if (get_user(pcm_file
->user_pversion
,
2902 (unsigned int __user
*)arg
))
2905 case SNDRV_PCM_IOCTL_HW_REFINE
:
2906 return snd_pcm_hw_refine_user(substream
, arg
);
2907 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2908 return snd_pcm_hw_params_user(substream
, arg
);
2909 case SNDRV_PCM_IOCTL_HW_FREE
:
2910 return snd_pcm_hw_free(substream
);
2911 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2912 return snd_pcm_sw_params_user(substream
, arg
);
2913 case SNDRV_PCM_IOCTL_STATUS
:
2914 return snd_pcm_status_user(substream
, arg
, false);
2915 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2916 return snd_pcm_status_user(substream
, arg
, true);
2917 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2918 return snd_pcm_channel_info_user(substream
, arg
);
2919 case SNDRV_PCM_IOCTL_PREPARE
:
2920 return snd_pcm_prepare(substream
, file
);
2921 case SNDRV_PCM_IOCTL_RESET
:
2922 return snd_pcm_reset(substream
);
2923 case SNDRV_PCM_IOCTL_START
:
2924 return snd_pcm_start_lock_irq(substream
);
2925 case SNDRV_PCM_IOCTL_LINK
:
2926 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2927 case SNDRV_PCM_IOCTL_UNLINK
:
2928 return snd_pcm_unlink(substream
);
2929 case SNDRV_PCM_IOCTL_RESUME
:
2930 return snd_pcm_resume(substream
);
2931 case SNDRV_PCM_IOCTL_XRUN
:
2932 return snd_pcm_xrun(substream
);
2933 case SNDRV_PCM_IOCTL_HWSYNC
:
2934 return snd_pcm_hwsync(substream
);
2935 case SNDRV_PCM_IOCTL_DELAY
:
2937 snd_pcm_sframes_t delay
;
2938 snd_pcm_sframes_t __user
*res
= arg
;
2941 err
= snd_pcm_delay(substream
, &delay
);
2944 if (put_user(delay
, res
))
2948 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2949 return snd_pcm_sync_ptr(substream
, arg
);
2950 #ifdef CONFIG_SND_SUPPORT_OLD_API
2951 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2952 return snd_pcm_hw_refine_old_user(substream
, arg
);
2953 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2954 return snd_pcm_hw_params_old_user(substream
, arg
);
2956 case SNDRV_PCM_IOCTL_DRAIN
:
2957 return snd_pcm_drain(substream
, file
);
2958 case SNDRV_PCM_IOCTL_DROP
:
2959 return snd_pcm_drop(substream
);
2960 case SNDRV_PCM_IOCTL_PAUSE
:
2961 return snd_pcm_action_lock_irq(&snd_pcm_action_pause
,
2963 (int)(unsigned long)arg
);
2964 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2965 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2966 return snd_pcm_xferi_frames_ioctl(substream
, arg
);
2967 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2968 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2969 return snd_pcm_xfern_frames_ioctl(substream
, arg
);
2970 case SNDRV_PCM_IOCTL_REWIND
:
2971 return snd_pcm_rewind_ioctl(substream
, arg
);
2972 case SNDRV_PCM_IOCTL_FORWARD
:
2973 return snd_pcm_forward_ioctl(substream
, arg
);
2975 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2979 static long snd_pcm_ioctl(struct file
*file
, unsigned int cmd
,
2982 struct snd_pcm_file
*pcm_file
;
2984 pcm_file
= file
->private_data
;
2986 if (((cmd
>> 8) & 0xff) != 'A')
2989 return snd_pcm_common_ioctl(file
, pcm_file
->substream
, cmd
,
2990 (void __user
*)arg
);
2994 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
2995 * @substream: PCM substream
2997 * @arg: IOCTL argument
2999 * The function is provided primarily for OSS layer and USB gadget drivers,
3000 * and it allows only the limited set of ioctls (hw_params, sw_params,
3001 * prepare, start, drain, drop, forward).
3003 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
3004 unsigned int cmd
, void *arg
)
3006 snd_pcm_uframes_t
*frames
= arg
;
3007 snd_pcm_sframes_t result
;
3010 case SNDRV_PCM_IOCTL_FORWARD
:
3012 /* provided only for OSS; capture-only and no value returned */
3013 if (substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
)
3015 result
= snd_pcm_capture_forward(substream
, *frames
);
3016 return result
< 0 ? result
: 0;
3018 case SNDRV_PCM_IOCTL_HW_PARAMS
:
3019 return snd_pcm_hw_params(substream
, arg
);
3020 case SNDRV_PCM_IOCTL_SW_PARAMS
:
3021 return snd_pcm_sw_params(substream
, arg
);
3022 case SNDRV_PCM_IOCTL_PREPARE
:
3023 return snd_pcm_prepare(substream
, NULL
);
3024 case SNDRV_PCM_IOCTL_START
:
3025 return snd_pcm_start_lock_irq(substream
);
3026 case SNDRV_PCM_IOCTL_DRAIN
:
3027 return snd_pcm_drain(substream
, NULL
);
3028 case SNDRV_PCM_IOCTL_DROP
:
3029 return snd_pcm_drop(substream
);
3030 case SNDRV_PCM_IOCTL_DELAY
:
3031 return snd_pcm_delay(substream
, frames
);
3036 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3038 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
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_read(substream
, buf
, count
);
3058 result
= frames_to_bytes(runtime
, result
);
3062 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3063 size_t count
, loff_t
* offset
)
3065 struct snd_pcm_file
*pcm_file
;
3066 struct snd_pcm_substream
*substream
;
3067 struct snd_pcm_runtime
*runtime
;
3068 snd_pcm_sframes_t result
;
3070 pcm_file
= file
->private_data
;
3071 substream
= pcm_file
->substream
;
3072 if (PCM_RUNTIME_CHECK(substream
))
3074 runtime
= substream
->runtime
;
3075 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3077 if (!frame_aligned(runtime
, count
))
3079 count
= bytes_to_frames(runtime
, count
);
3080 result
= snd_pcm_lib_write(substream
, buf
, count
);
3082 result
= frames_to_bytes(runtime
, result
);
3086 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3088 struct snd_pcm_file
*pcm_file
;
3089 struct snd_pcm_substream
*substream
;
3090 struct snd_pcm_runtime
*runtime
;
3091 snd_pcm_sframes_t result
;
3094 snd_pcm_uframes_t frames
;
3096 pcm_file
= iocb
->ki_filp
->private_data
;
3097 substream
= pcm_file
->substream
;
3098 if (PCM_RUNTIME_CHECK(substream
))
3100 runtime
= substream
->runtime
;
3101 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3103 if (!iter_is_iovec(to
))
3105 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3107 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3109 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3110 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3113 for (i
= 0; i
< to
->nr_segs
; ++i
)
3114 bufs
[i
] = to
->iov
[i
].iov_base
;
3115 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3117 result
= frames_to_bytes(runtime
, result
);
3122 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3124 struct snd_pcm_file
*pcm_file
;
3125 struct snd_pcm_substream
*substream
;
3126 struct snd_pcm_runtime
*runtime
;
3127 snd_pcm_sframes_t result
;
3130 snd_pcm_uframes_t frames
;
3132 pcm_file
= iocb
->ki_filp
->private_data
;
3133 substream
= pcm_file
->substream
;
3134 if (PCM_RUNTIME_CHECK(substream
))
3136 runtime
= substream
->runtime
;
3137 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3139 if (!iter_is_iovec(from
))
3141 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3142 !frame_aligned(runtime
, from
->iov
->iov_len
))
3144 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3145 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3148 for (i
= 0; i
< from
->nr_segs
; ++i
)
3149 bufs
[i
] = from
->iov
[i
].iov_base
;
3150 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3152 result
= frames_to_bytes(runtime
, result
);
3157 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3159 struct snd_pcm_file
*pcm_file
;
3160 struct snd_pcm_substream
*substream
;
3161 struct snd_pcm_runtime
*runtime
;
3163 snd_pcm_uframes_t avail
;
3165 pcm_file
= file
->private_data
;
3167 substream
= pcm_file
->substream
;
3168 if (PCM_RUNTIME_CHECK(substream
))
3169 return POLLOUT
| POLLWRNORM
| POLLERR
;
3170 runtime
= substream
->runtime
;
3172 poll_wait(file
, &runtime
->sleep
, wait
);
3174 snd_pcm_stream_lock_irq(substream
);
3175 avail
= snd_pcm_playback_avail(runtime
);
3176 switch (runtime
->status
->state
) {
3177 case SNDRV_PCM_STATE_RUNNING
:
3178 case SNDRV_PCM_STATE_PREPARED
:
3179 case SNDRV_PCM_STATE_PAUSED
:
3180 if (avail
>= runtime
->control
->avail_min
) {
3181 mask
= POLLOUT
| POLLWRNORM
;
3185 case SNDRV_PCM_STATE_DRAINING
:
3189 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3192 snd_pcm_stream_unlock_irq(substream
);
3196 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3198 struct snd_pcm_file
*pcm_file
;
3199 struct snd_pcm_substream
*substream
;
3200 struct snd_pcm_runtime
*runtime
;
3202 snd_pcm_uframes_t avail
;
3204 pcm_file
= file
->private_data
;
3206 substream
= pcm_file
->substream
;
3207 if (PCM_RUNTIME_CHECK(substream
))
3208 return POLLIN
| POLLRDNORM
| POLLERR
;
3209 runtime
= substream
->runtime
;
3211 poll_wait(file
, &runtime
->sleep
, wait
);
3213 snd_pcm_stream_lock_irq(substream
);
3214 avail
= snd_pcm_capture_avail(runtime
);
3215 switch (runtime
->status
->state
) {
3216 case SNDRV_PCM_STATE_RUNNING
:
3217 case SNDRV_PCM_STATE_PREPARED
:
3218 case SNDRV_PCM_STATE_PAUSED
:
3219 if (avail
>= runtime
->control
->avail_min
) {
3220 mask
= POLLIN
| POLLRDNORM
;
3225 case SNDRV_PCM_STATE_DRAINING
:
3227 mask
= POLLIN
| POLLRDNORM
;
3232 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3235 snd_pcm_stream_unlock_irq(substream
);
3244 * Only on coherent architectures, we can mmap the status and the control records
3245 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3247 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3249 * mmap status record
3251 static int snd_pcm_mmap_status_fault(struct vm_fault
*vmf
)
3253 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3254 struct snd_pcm_runtime
*runtime
;
3256 if (substream
== NULL
)
3257 return VM_FAULT_SIGBUS
;
3258 runtime
= substream
->runtime
;
3259 vmf
->page
= virt_to_page(runtime
->status
);
3260 get_page(vmf
->page
);
3264 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3266 .fault
= snd_pcm_mmap_status_fault
,
3269 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3270 struct vm_area_struct
*area
)
3273 if (!(area
->vm_flags
& VM_READ
))
3275 size
= area
->vm_end
- area
->vm_start
;
3276 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3278 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3279 area
->vm_private_data
= substream
;
3280 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3285 * mmap control record
3287 static int snd_pcm_mmap_control_fault(struct vm_fault
*vmf
)
3289 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3290 struct snd_pcm_runtime
*runtime
;
3292 if (substream
== NULL
)
3293 return VM_FAULT_SIGBUS
;
3294 runtime
= substream
->runtime
;
3295 vmf
->page
= virt_to_page(runtime
->control
);
3296 get_page(vmf
->page
);
3300 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3302 .fault
= snd_pcm_mmap_control_fault
,
3305 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3306 struct vm_area_struct
*area
)
3309 if (!(area
->vm_flags
& VM_READ
))
3311 size
= area
->vm_end
- area
->vm_start
;
3312 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3314 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3315 area
->vm_private_data
= substream
;
3316 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3320 static bool pcm_status_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3322 if (pcm_file
->no_compat_mmap
)
3324 /* See pcm_control_mmap_allowed() below.
3325 * Since older alsa-lib requires both status and control mmaps to be
3326 * coupled, we have to disable the status mmap for old alsa-lib, too.
3328 if (pcm_file
->user_pversion
< SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3329 (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
))
3334 static bool pcm_control_mmap_allowed(struct snd_pcm_file
*pcm_file
)
3336 if (pcm_file
->no_compat_mmap
)
3338 /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3339 * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3340 * thus it effectively assures the manual update of appl_ptr.
3342 if (pcm_file
->substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_SYNC_APPLPTR
)
3347 #else /* ! coherent mmap */
3349 * don't support mmap for status and control records.
3351 #define pcm_status_mmap_allowed(pcm_file) false
3352 #define pcm_control_mmap_allowed(pcm_file) false
3354 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3355 struct vm_area_struct
*area
)
3359 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3360 struct vm_area_struct
*area
)
3364 #endif /* coherent mmap */
3366 static inline struct page
*
3367 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3369 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3370 return virt_to_page(vaddr
);
3374 * fault callback for mmapping a RAM page
3376 static int snd_pcm_mmap_data_fault(struct vm_fault
*vmf
)
3378 struct snd_pcm_substream
*substream
= vmf
->vma
->vm_private_data
;
3379 struct snd_pcm_runtime
*runtime
;
3380 unsigned long offset
;
3384 if (substream
== NULL
)
3385 return VM_FAULT_SIGBUS
;
3386 runtime
= substream
->runtime
;
3387 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3388 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3389 if (offset
> dma_bytes
- PAGE_SIZE
)
3390 return VM_FAULT_SIGBUS
;
3391 if (substream
->ops
->page
)
3392 page
= substream
->ops
->page(substream
, offset
);
3394 page
= snd_pcm_default_page_ops(substream
, offset
);
3396 return VM_FAULT_SIGBUS
;
3402 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3403 .open
= snd_pcm_mmap_data_open
,
3404 .close
= snd_pcm_mmap_data_close
,
3407 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3408 .open
= snd_pcm_mmap_data_open
,
3409 .close
= snd_pcm_mmap_data_close
,
3410 .fault
= snd_pcm_mmap_data_fault
,
3414 * mmap the DMA buffer on RAM
3418 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3419 * @substream: PCM substream
3422 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3423 * this function is invoked implicitly.
3425 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3426 struct vm_area_struct
*area
)
3428 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3429 #ifdef CONFIG_GENERIC_ALLOCATOR
3430 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3431 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3432 return remap_pfn_range(area
, area
->vm_start
,
3433 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3434 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3436 #endif /* CONFIG_GENERIC_ALLOCATOR */
3437 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3438 if (IS_ENABLED(CONFIG_HAS_DMA
) && !substream
->ops
->page
&&
3439 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3440 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3442 substream
->runtime
->dma_area
,
3443 substream
->runtime
->dma_addr
,
3444 substream
->runtime
->dma_bytes
);
3445 #endif /* CONFIG_X86 */
3446 /* mmap with fault handler */
3447 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3450 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3453 * mmap the DMA buffer on I/O memory area
3455 #if SNDRV_PCM_INFO_MMAP_IOMEM
3457 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3458 * @substream: PCM substream
3461 * When your hardware uses the iomapped pages as the hardware buffer and
3462 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3463 * is supposed to work only on limited architectures.
3465 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3466 struct vm_area_struct
*area
)
3468 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3470 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3471 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3473 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3474 #endif /* SNDRV_PCM_INFO_MMAP */
3479 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3480 struct vm_area_struct
*area
)
3482 struct snd_pcm_runtime
*runtime
;
3484 unsigned long offset
;
3488 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3489 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3492 if (!(area
->vm_flags
& VM_READ
))
3495 runtime
= substream
->runtime
;
3496 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3498 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3500 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3501 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3503 size
= area
->vm_end
- area
->vm_start
;
3504 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3505 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3506 if ((size_t)size
> dma_bytes
)
3508 if (offset
> dma_bytes
- size
)
3511 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3512 area
->vm_private_data
= substream
;
3513 if (substream
->ops
->mmap
)
3514 err
= substream
->ops
->mmap(substream
, area
);
3516 err
= snd_pcm_lib_default_mmap(substream
, area
);
3518 atomic_inc(&substream
->mmap_count
);
3521 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3523 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3525 struct snd_pcm_file
* pcm_file
;
3526 struct snd_pcm_substream
*substream
;
3527 unsigned long offset
;
3529 pcm_file
= file
->private_data
;
3530 substream
= pcm_file
->substream
;
3531 if (PCM_RUNTIME_CHECK(substream
))
3534 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3536 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3537 if (!pcm_status_mmap_allowed(pcm_file
))
3539 return snd_pcm_mmap_status(substream
, file
, area
);
3540 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3541 if (!pcm_control_mmap_allowed(pcm_file
))
3543 return snd_pcm_mmap_control(substream
, file
, area
);
3545 return snd_pcm_mmap_data(substream
, file
, area
);
3550 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3552 struct snd_pcm_file
* pcm_file
;
3553 struct snd_pcm_substream
*substream
;
3554 struct snd_pcm_runtime
*runtime
;
3556 pcm_file
= file
->private_data
;
3557 substream
= pcm_file
->substream
;
3558 if (PCM_RUNTIME_CHECK(substream
))
3560 runtime
= substream
->runtime
;
3561 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3567 #ifdef CONFIG_COMPAT
3568 #include "pcm_compat.c"
3570 #define snd_pcm_ioctl_compat NULL
3574 * To be removed helpers to keep binary compatibility
3577 #ifdef CONFIG_SND_SUPPORT_OLD_API
3578 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3579 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3581 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3582 struct snd_pcm_hw_params_old
*oparams
)
3586 memset(params
, 0, sizeof(*params
));
3587 params
->flags
= oparams
->flags
;
3588 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3589 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3590 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3591 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3592 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3593 params
->info
= oparams
->info
;
3594 params
->msbits
= oparams
->msbits
;
3595 params
->rate_num
= oparams
->rate_num
;
3596 params
->rate_den
= oparams
->rate_den
;
3597 params
->fifo_size
= oparams
->fifo_size
;
3600 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3601 struct snd_pcm_hw_params
*params
)
3605 memset(oparams
, 0, sizeof(*oparams
));
3606 oparams
->flags
= params
->flags
;
3607 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3608 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3609 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3610 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3611 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3612 oparams
->info
= params
->info
;
3613 oparams
->msbits
= params
->msbits
;
3614 oparams
->rate_num
= params
->rate_num
;
3615 oparams
->rate_den
= params
->rate_den
;
3616 oparams
->fifo_size
= params
->fifo_size
;
3619 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3620 struct snd_pcm_hw_params_old __user
* _oparams
)
3622 struct snd_pcm_hw_params
*params
;
3623 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3626 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3630 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3631 if (IS_ERR(oparams
)) {
3632 err
= PTR_ERR(oparams
);
3635 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3636 err
= snd_pcm_hw_refine(substream
, params
);
3640 err
= fixup_unreferenced_params(substream
, params
);
3644 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3645 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3654 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3655 struct snd_pcm_hw_params_old __user
* _oparams
)
3657 struct snd_pcm_hw_params
*params
;
3658 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3661 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3665 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3666 if (IS_ERR(oparams
)) {
3667 err
= PTR_ERR(oparams
);
3671 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3672 err
= snd_pcm_hw_params(substream
, params
);
3676 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3677 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
)))
3685 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3688 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3691 unsigned long pgoff
,
3692 unsigned long flags
)
3694 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3695 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3696 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3697 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3700 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3701 return (unsigned long)runtime
->status
;
3702 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3703 return (unsigned long)runtime
->control
;
3705 return (unsigned long)runtime
->dma_area
+ offset
;
3709 # define snd_pcm_get_unmapped_area NULL
3716 const struct file_operations snd_pcm_f_ops
[2] = {
3718 .owner
= THIS_MODULE
,
3719 .write
= snd_pcm_write
,
3720 .write_iter
= snd_pcm_writev
,
3721 .open
= snd_pcm_playback_open
,
3722 .release
= snd_pcm_release
,
3723 .llseek
= no_llseek
,
3724 .poll
= snd_pcm_playback_poll
,
3725 .unlocked_ioctl
= snd_pcm_ioctl
,
3726 .compat_ioctl
= snd_pcm_ioctl_compat
,
3727 .mmap
= snd_pcm_mmap
,
3728 .fasync
= snd_pcm_fasync
,
3729 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3732 .owner
= THIS_MODULE
,
3733 .read
= snd_pcm_read
,
3734 .read_iter
= snd_pcm_readv
,
3735 .open
= snd_pcm_capture_open
,
3736 .release
= snd_pcm_release
,
3737 .llseek
= no_llseek
,
3738 .poll
= snd_pcm_capture_poll
,
3739 .unlocked_ioctl
= snd_pcm_ioctl
,
3740 .compat_ioctl
= snd_pcm_ioctl_compat
,
3741 .mmap
= snd_pcm_mmap
,
3742 .fasync
= snd_pcm_fasync
,
3743 .get_unmapped_area
= snd_pcm_get_unmapped_area
,