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/time.h>
27 #include <linux/pm_qos.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
37 #include <linux/uio.h>
43 struct snd_pcm_hw_params_old
{
45 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
46 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
47 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
53 unsigned int rate_num
;
54 unsigned int rate_den
;
55 snd_pcm_uframes_t fifo_size
;
56 unsigned char reserved
[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
64 struct snd_pcm_hw_params_old __user
* _oparams
);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
66 struct snd_pcm_hw_params_old __user
* _oparams
);
68 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
74 static DEFINE_RWLOCK(snd_pcm_link_rwlock
);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
78 * snd_pcm_stream_lock - Lock the PCM stream
79 * @substream: PCM substream
81 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
82 * flag of the given substream. This also takes the global link rw lock
83 * (or rw sem), too, for avoiding the race with linked streams.
85 void snd_pcm_stream_lock(struct snd_pcm_substream
*substream
)
87 if (substream
->pcm
->nonatomic
) {
88 down_read_nested(&snd_pcm_link_rwsem
, SINGLE_DEPTH_NESTING
);
89 mutex_lock(&substream
->self_group
.mutex
);
91 read_lock(&snd_pcm_link_rwlock
);
92 spin_lock(&substream
->self_group
.lock
);
95 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock
);
98 * snd_pcm_stream_lock - Unlock the PCM stream
99 * @substream: PCM substream
101 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
103 void snd_pcm_stream_unlock(struct snd_pcm_substream
*substream
)
105 if (substream
->pcm
->nonatomic
) {
106 mutex_unlock(&substream
->self_group
.mutex
);
107 up_read(&snd_pcm_link_rwsem
);
109 spin_unlock(&substream
->self_group
.lock
);
110 read_unlock(&snd_pcm_link_rwlock
);
113 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock
);
116 * snd_pcm_stream_lock_irq - Lock the PCM stream
117 * @substream: PCM substream
119 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
120 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
121 * as snd_pcm_stream_lock().
123 void snd_pcm_stream_lock_irq(struct snd_pcm_substream
*substream
)
125 if (!substream
->pcm
->nonatomic
)
127 snd_pcm_stream_lock(substream
);
129 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq
);
132 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
133 * @substream: PCM substream
135 * This is a counter-part of snd_pcm_stream_lock_irq().
137 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream
*substream
)
139 snd_pcm_stream_unlock(substream
);
140 if (!substream
->pcm
->nonatomic
)
143 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq
);
145 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream
*substream
)
147 unsigned long flags
= 0;
148 if (!substream
->pcm
->nonatomic
)
149 local_irq_save(flags
);
150 snd_pcm_stream_lock(substream
);
153 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave
);
156 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
157 * @substream: PCM substream
160 * This is a counter-part of snd_pcm_stream_lock_irqsave().
162 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream
*substream
,
165 snd_pcm_stream_unlock(substream
);
166 if (!substream
->pcm
->nonatomic
)
167 local_irq_restore(flags
);
169 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore
);
171 static inline mm_segment_t
snd_enter_user(void)
173 mm_segment_t fs
= get_fs();
178 static inline void snd_leave_user(mm_segment_t fs
)
185 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
187 struct snd_pcm_runtime
*runtime
;
188 struct snd_pcm
*pcm
= substream
->pcm
;
189 struct snd_pcm_str
*pstr
= substream
->pstr
;
191 memset(info
, 0, sizeof(*info
));
192 info
->card
= pcm
->card
->number
;
193 info
->device
= pcm
->device
;
194 info
->stream
= substream
->stream
;
195 info
->subdevice
= substream
->number
;
196 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
197 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
198 info
->dev_class
= pcm
->dev_class
;
199 info
->dev_subclass
= pcm
->dev_subclass
;
200 info
->subdevices_count
= pstr
->substream_count
;
201 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
202 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
203 runtime
= substream
->runtime
;
204 /* AB: FIXME!!! This is definitely nonsense */
206 info
->sync
= runtime
->sync
;
207 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
212 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
213 struct snd_pcm_info __user
* _info
)
215 struct snd_pcm_info
*info
;
218 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
221 err
= snd_pcm_info(substream
, info
);
223 if (copy_to_user(_info
, info
, sizeof(*info
)))
230 static bool hw_support_mmap(struct snd_pcm_substream
*substream
)
232 if (!(substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_MMAP
))
234 /* check architectures that return -EINVAL from dma_mmap_coherent() */
235 /* FIXME: this should be some global flag */
236 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
237 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
238 if (!substream
->ops
->mmap
&&
239 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
248 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
249 static const char * const snd_pcm_hw_param_names
[] = {
253 HW_PARAM(SAMPLE_BITS
),
254 HW_PARAM(FRAME_BITS
),
257 HW_PARAM(PERIOD_TIME
),
258 HW_PARAM(PERIOD_SIZE
),
259 HW_PARAM(PERIOD_BYTES
),
261 HW_PARAM(BUFFER_TIME
),
262 HW_PARAM(BUFFER_SIZE
),
263 HW_PARAM(BUFFER_BYTES
),
268 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
269 struct snd_pcm_hw_params
*params
)
272 struct snd_pcm_hardware
*hw
;
273 struct snd_interval
*i
= NULL
;
274 struct snd_mask
*m
= NULL
;
275 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
276 unsigned int rstamps
[constrs
->rules_num
];
277 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
278 unsigned int stamp
= 2;
282 params
->fifo_size
= 0;
283 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
285 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
286 params
->rate_num
= 0;
287 params
->rate_den
= 0;
290 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
291 m
= hw_param_mask(params
, k
);
292 if (snd_mask_empty(m
))
294 if (!(params
->rmask
& (1 << k
)))
297 pr_debug("%s = ", snd_pcm_hw_param_names
[k
]);
298 pr_cont("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
300 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
302 pr_cont("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
305 params
->cmask
|= 1 << k
;
310 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
311 i
= hw_param_interval(params
, k
);
312 if (snd_interval_empty(i
))
314 if (!(params
->rmask
& (1 << k
)))
317 pr_debug("%s = ", snd_pcm_hw_param_names
[k
]);
322 i
->openmin
? '(' : '[', i
->min
,
323 i
->max
, i
->openmax
? ')' : ']');
326 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
331 pr_cont("%c%u %u%c\n",
332 i
->openmin
? '(' : '[', i
->min
,
333 i
->max
, i
->openmax
? ')' : ']');
336 params
->cmask
|= 1 << k
;
341 for (k
= 0; k
< constrs
->rules_num
; k
++)
343 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
344 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
347 for (k
= 0; k
< constrs
->rules_num
; k
++) {
348 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
351 if (r
->cond
&& !(r
->cond
& params
->flags
))
353 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
354 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
362 pr_debug("Rule %d [%p]: ", k
, r
->func
);
364 pr_cont("%s = ", snd_pcm_hw_param_names
[r
->var
]);
365 if (hw_is_mask(r
->var
)) {
366 m
= hw_param_mask(params
, r
->var
);
367 pr_cont("%x", *m
->bits
);
369 i
= hw_param_interval(params
, r
->var
);
374 i
->openmin
? '(' : '[', i
->min
,
375 i
->max
, i
->openmax
? ')' : ']');
379 changed
= r
->func(params
, r
);
383 if (hw_is_mask(r
->var
))
384 pr_cont("%x", *m
->bits
);
390 i
->openmin
? '(' : '[', i
->min
,
391 i
->max
, i
->openmax
? ')' : ']');
397 if (changed
&& r
->var
>= 0) {
398 params
->cmask
|= (1 << r
->var
);
399 vstamps
[r
->var
] = stamp
;
407 if (!params
->msbits
) {
408 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
409 if (snd_interval_single(i
))
410 params
->msbits
= snd_interval_value(i
);
413 if (!params
->rate_den
) {
414 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
415 if (snd_interval_single(i
)) {
416 params
->rate_num
= snd_interval_value(i
);
417 params
->rate_den
= 1;
421 hw
= &substream
->runtime
->hw
;
423 params
->info
= hw
->info
& ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES
|
424 SNDRV_PCM_INFO_DRAIN_TRIGGER
);
425 if (!hw_support_mmap(substream
))
426 params
->info
&= ~(SNDRV_PCM_INFO_MMAP
|
427 SNDRV_PCM_INFO_MMAP_VALID
);
429 if (!params
->fifo_size
) {
430 m
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
431 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
432 if (snd_mask_min(m
) == snd_mask_max(m
) &&
433 snd_interval_min(i
) == snd_interval_max(i
)) {
434 changed
= substream
->ops
->ioctl(substream
,
435 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
444 EXPORT_SYMBOL(snd_pcm_hw_refine
);
446 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
447 struct snd_pcm_hw_params __user
* _params
)
449 struct snd_pcm_hw_params
*params
;
452 params
= memdup_user(_params
, sizeof(*params
));
454 return PTR_ERR(params
);
456 err
= snd_pcm_hw_refine(substream
, params
);
457 if (copy_to_user(_params
, params
, sizeof(*params
))) {
466 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
471 return -1; /* invalid */
473 /* take 75% of period time as the deadline */
474 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
475 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
481 static void snd_pcm_set_state(struct snd_pcm_substream
*substream
, int state
)
483 snd_pcm_stream_lock_irq(substream
);
484 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DISCONNECTED
)
485 substream
->runtime
->status
->state
= state
;
486 snd_pcm_stream_unlock_irq(substream
);
489 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
490 struct snd_pcm_hw_params
*params
)
492 struct snd_pcm_runtime
*runtime
;
495 snd_pcm_uframes_t frames
;
497 if (PCM_RUNTIME_CHECK(substream
))
499 runtime
= substream
->runtime
;
500 snd_pcm_stream_lock_irq(substream
);
501 switch (runtime
->status
->state
) {
502 case SNDRV_PCM_STATE_OPEN
:
503 case SNDRV_PCM_STATE_SETUP
:
504 case SNDRV_PCM_STATE_PREPARED
:
507 snd_pcm_stream_unlock_irq(substream
);
510 snd_pcm_stream_unlock_irq(substream
);
511 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
512 if (!substream
->oss
.oss
)
514 if (atomic_read(&substream
->mmap_count
))
518 err
= snd_pcm_hw_refine(substream
, params
);
522 err
= snd_pcm_hw_params_choose(substream
, params
);
526 if (substream
->ops
->hw_params
!= NULL
) {
527 err
= substream
->ops
->hw_params(substream
, params
);
532 runtime
->access
= params_access(params
);
533 runtime
->format
= params_format(params
);
534 runtime
->subformat
= params_subformat(params
);
535 runtime
->channels
= params_channels(params
);
536 runtime
->rate
= params_rate(params
);
537 runtime
->period_size
= params_period_size(params
);
538 runtime
->periods
= params_periods(params
);
539 runtime
->buffer_size
= params_buffer_size(params
);
540 runtime
->info
= params
->info
;
541 runtime
->rate_num
= params
->rate_num
;
542 runtime
->rate_den
= params
->rate_den
;
543 runtime
->no_period_wakeup
=
544 (params
->info
& SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
) &&
545 (params
->flags
& SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
);
547 bits
= snd_pcm_format_physical_width(runtime
->format
);
548 runtime
->sample_bits
= bits
;
549 bits
*= runtime
->channels
;
550 runtime
->frame_bits
= bits
;
552 while (bits
% 8 != 0) {
556 runtime
->byte_align
= bits
/ 8;
557 runtime
->min_align
= frames
;
559 /* Default sw params */
560 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
561 runtime
->period_step
= 1;
562 runtime
->control
->avail_min
= runtime
->period_size
;
563 runtime
->start_threshold
= 1;
564 runtime
->stop_threshold
= runtime
->buffer_size
;
565 runtime
->silence_threshold
= 0;
566 runtime
->silence_size
= 0;
567 runtime
->boundary
= runtime
->buffer_size
;
568 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
569 runtime
->boundary
*= 2;
571 snd_pcm_timer_resolution_change(substream
);
572 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_SETUP
);
574 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
575 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
576 if ((usecs
= period_to_usecs(runtime
)) >= 0)
577 pm_qos_add_request(&substream
->latency_pm_qos_req
,
578 PM_QOS_CPU_DMA_LATENCY
, usecs
);
581 /* hardware might be unusable from this time,
582 so we force application to retry to set
583 the correct hardware parameter settings */
584 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
585 if (substream
->ops
->hw_free
!= NULL
)
586 substream
->ops
->hw_free(substream
);
590 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
591 struct snd_pcm_hw_params __user
* _params
)
593 struct snd_pcm_hw_params
*params
;
596 params
= memdup_user(_params
, sizeof(*params
));
598 return PTR_ERR(params
);
600 err
= snd_pcm_hw_params(substream
, params
);
601 if (copy_to_user(_params
, params
, sizeof(*params
))) {
610 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
612 struct snd_pcm_runtime
*runtime
;
615 if (PCM_RUNTIME_CHECK(substream
))
617 runtime
= substream
->runtime
;
618 snd_pcm_stream_lock_irq(substream
);
619 switch (runtime
->status
->state
) {
620 case SNDRV_PCM_STATE_SETUP
:
621 case SNDRV_PCM_STATE_PREPARED
:
624 snd_pcm_stream_unlock_irq(substream
);
627 snd_pcm_stream_unlock_irq(substream
);
628 if (atomic_read(&substream
->mmap_count
))
630 if (substream
->ops
->hw_free
)
631 result
= substream
->ops
->hw_free(substream
);
632 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_OPEN
);
633 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
637 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
638 struct snd_pcm_sw_params
*params
)
640 struct snd_pcm_runtime
*runtime
;
643 if (PCM_RUNTIME_CHECK(substream
))
645 runtime
= substream
->runtime
;
646 snd_pcm_stream_lock_irq(substream
);
647 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
648 snd_pcm_stream_unlock_irq(substream
);
651 snd_pcm_stream_unlock_irq(substream
);
653 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
655 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
656 params
->tstamp_type
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
658 if (params
->avail_min
== 0)
660 if (params
->silence_size
>= runtime
->boundary
) {
661 if (params
->silence_threshold
!= 0)
664 if (params
->silence_size
> params
->silence_threshold
)
666 if (params
->silence_threshold
> runtime
->buffer_size
)
670 snd_pcm_stream_lock_irq(substream
);
671 runtime
->tstamp_mode
= params
->tstamp_mode
;
672 if (params
->proto
>= SNDRV_PROTOCOL_VERSION(2, 0, 12))
673 runtime
->tstamp_type
= params
->tstamp_type
;
674 runtime
->period_step
= params
->period_step
;
675 runtime
->control
->avail_min
= params
->avail_min
;
676 runtime
->start_threshold
= params
->start_threshold
;
677 runtime
->stop_threshold
= params
->stop_threshold
;
678 runtime
->silence_threshold
= params
->silence_threshold
;
679 runtime
->silence_size
= params
->silence_size
;
680 params
->boundary
= runtime
->boundary
;
681 if (snd_pcm_running(substream
)) {
682 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
683 runtime
->silence_size
> 0)
684 snd_pcm_playback_silence(substream
, ULONG_MAX
);
685 err
= snd_pcm_update_state(substream
, runtime
);
687 snd_pcm_stream_unlock_irq(substream
);
691 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
692 struct snd_pcm_sw_params __user
* _params
)
694 struct snd_pcm_sw_params params
;
696 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
698 err
= snd_pcm_sw_params(substream
, ¶ms
);
699 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
704 int snd_pcm_status(struct snd_pcm_substream
*substream
,
705 struct snd_pcm_status
*status
)
707 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
709 snd_pcm_stream_lock_irq(substream
);
711 snd_pcm_unpack_audio_tstamp_config(status
->audio_tstamp_data
,
712 &runtime
->audio_tstamp_config
);
714 /* backwards compatible behavior */
715 if (runtime
->audio_tstamp_config
.type_requested
==
716 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT
) {
717 if (runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_WALL_CLOCK
)
718 runtime
->audio_tstamp_config
.type_requested
=
719 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
721 runtime
->audio_tstamp_config
.type_requested
=
722 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
723 runtime
->audio_tstamp_report
.valid
= 0;
725 runtime
->audio_tstamp_report
.valid
= 1;
727 status
->state
= runtime
->status
->state
;
728 status
->suspended_state
= runtime
->status
->suspended_state
;
729 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
731 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
732 if (snd_pcm_running(substream
)) {
733 snd_pcm_update_hw_ptr(substream
);
734 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
735 status
->tstamp
= runtime
->status
->tstamp
;
736 status
->driver_tstamp
= runtime
->driver_tstamp
;
737 status
->audio_tstamp
=
738 runtime
->status
->audio_tstamp
;
739 if (runtime
->audio_tstamp_report
.valid
== 1)
740 /* backwards compatibility, no report provided in COMPAT mode */
741 snd_pcm_pack_audio_tstamp_report(&status
->audio_tstamp_data
,
742 &status
->audio_tstamp_accuracy
,
743 &runtime
->audio_tstamp_report
);
748 /* get tstamp only in fallback mode and only if enabled */
749 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
)
750 snd_pcm_gettime(runtime
, &status
->tstamp
);
753 status
->appl_ptr
= runtime
->control
->appl_ptr
;
754 status
->hw_ptr
= runtime
->status
->hw_ptr
;
755 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
756 status
->avail
= snd_pcm_playback_avail(runtime
);
757 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
758 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
759 status
->delay
= runtime
->buffer_size
- status
->avail
;
760 status
->delay
+= runtime
->delay
;
764 status
->avail
= snd_pcm_capture_avail(runtime
);
765 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
766 status
->delay
= status
->avail
+ runtime
->delay
;
770 status
->avail_max
= runtime
->avail_max
;
771 status
->overrange
= runtime
->overrange
;
772 runtime
->avail_max
= 0;
773 runtime
->overrange
= 0;
775 snd_pcm_stream_unlock_irq(substream
);
779 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
780 struct snd_pcm_status __user
* _status
,
783 struct snd_pcm_status status
;
786 memset(&status
, 0, sizeof(status
));
788 * with extension, parameters are read/write,
789 * get audio_tstamp_data from user,
790 * ignore rest of status structure
792 if (ext
&& get_user(status
.audio_tstamp_data
,
793 (u32 __user
*)(&_status
->audio_tstamp_data
)))
795 res
= snd_pcm_status(substream
, &status
);
798 if (copy_to_user(_status
, &status
, sizeof(status
)))
803 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
804 struct snd_pcm_channel_info
* info
)
806 struct snd_pcm_runtime
*runtime
;
807 unsigned int channel
;
809 channel
= info
->channel
;
810 runtime
= substream
->runtime
;
811 snd_pcm_stream_lock_irq(substream
);
812 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
813 snd_pcm_stream_unlock_irq(substream
);
816 snd_pcm_stream_unlock_irq(substream
);
817 if (channel
>= runtime
->channels
)
819 memset(info
, 0, sizeof(*info
));
820 info
->channel
= channel
;
821 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
824 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
825 struct snd_pcm_channel_info __user
* _info
)
827 struct snd_pcm_channel_info info
;
830 if (copy_from_user(&info
, _info
, sizeof(info
)))
832 res
= snd_pcm_channel_info(substream
, &info
);
835 if (copy_to_user(_info
, &info
, sizeof(info
)))
840 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
842 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
843 if (runtime
->trigger_master
== NULL
)
845 if (runtime
->trigger_master
== substream
) {
846 if (!runtime
->trigger_tstamp_latched
)
847 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
849 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
850 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
852 runtime
->trigger_master
= NULL
;
856 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
857 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
858 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
859 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
863 * this functions is core for handling of linked stream
864 * Note: the stream state might be changed also on failure
865 * Note2: call with calling stream lock + link lock
867 static int snd_pcm_action_group(struct action_ops
*ops
,
868 struct snd_pcm_substream
*substream
,
869 int state
, int do_lock
)
871 struct snd_pcm_substream
*s
= NULL
;
872 struct snd_pcm_substream
*s1
;
873 int res
= 0, depth
= 1;
875 snd_pcm_group_for_each_entry(s
, substream
) {
876 if (do_lock
&& s
!= substream
) {
877 if (s
->pcm
->nonatomic
)
878 mutex_lock_nested(&s
->self_group
.mutex
, depth
);
880 spin_lock_nested(&s
->self_group
.lock
, depth
);
883 res
= ops
->pre_action(s
, state
);
887 snd_pcm_group_for_each_entry(s
, substream
) {
888 res
= ops
->do_action(s
, state
);
890 if (ops
->undo_action
) {
891 snd_pcm_group_for_each_entry(s1
, substream
) {
892 if (s1
== s
) /* failed stream */
894 ops
->undo_action(s1
, state
);
897 s
= NULL
; /* unlock all */
901 snd_pcm_group_for_each_entry(s
, substream
) {
902 ops
->post_action(s
, state
);
907 snd_pcm_group_for_each_entry(s1
, substream
) {
908 if (s1
!= substream
) {
909 if (s1
->pcm
->nonatomic
)
910 mutex_unlock(&s1
->self_group
.mutex
);
912 spin_unlock(&s1
->self_group
.lock
);
914 if (s1
== s
) /* end */
922 * Note: call with stream lock
924 static int snd_pcm_action_single(struct action_ops
*ops
,
925 struct snd_pcm_substream
*substream
,
930 res
= ops
->pre_action(substream
, state
);
933 res
= ops
->do_action(substream
, state
);
935 ops
->post_action(substream
, state
);
936 else if (ops
->undo_action
)
937 ops
->undo_action(substream
, state
);
942 * Note: call with stream lock
944 static int snd_pcm_action(struct action_ops
*ops
,
945 struct snd_pcm_substream
*substream
,
950 if (!snd_pcm_stream_linked(substream
))
951 return snd_pcm_action_single(ops
, substream
, state
);
953 if (substream
->pcm
->nonatomic
) {
954 if (!mutex_trylock(&substream
->group
->mutex
)) {
955 mutex_unlock(&substream
->self_group
.mutex
);
956 mutex_lock(&substream
->group
->mutex
);
957 mutex_lock(&substream
->self_group
.mutex
);
959 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
960 mutex_unlock(&substream
->group
->mutex
);
962 if (!spin_trylock(&substream
->group
->lock
)) {
963 spin_unlock(&substream
->self_group
.lock
);
964 spin_lock(&substream
->group
->lock
);
965 spin_lock(&substream
->self_group
.lock
);
967 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
968 spin_unlock(&substream
->group
->lock
);
974 * Note: don't use any locks before
976 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
977 struct snd_pcm_substream
*substream
,
982 snd_pcm_stream_lock_irq(substream
);
983 res
= snd_pcm_action(ops
, substream
, state
);
984 snd_pcm_stream_unlock_irq(substream
);
990 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
991 struct snd_pcm_substream
*substream
,
996 down_read(&snd_pcm_link_rwsem
);
997 if (snd_pcm_stream_linked(substream
))
998 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
1000 res
= snd_pcm_action_single(ops
, substream
, state
);
1001 up_read(&snd_pcm_link_rwsem
);
1008 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
1010 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1011 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
1013 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1014 !snd_pcm_playback_data(substream
))
1016 runtime
->trigger_tstamp_latched
= false;
1017 runtime
->trigger_master
= substream
;
1021 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
1023 if (substream
->runtime
->trigger_master
!= substream
)
1025 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1028 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
1030 if (substream
->runtime
->trigger_master
== substream
)
1031 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1034 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
1036 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1037 snd_pcm_trigger_tstamp(substream
);
1038 runtime
->hw_ptr_jiffies
= jiffies
;
1039 runtime
->hw_ptr_buffer_jiffies
= (runtime
->buffer_size
* HZ
) /
1041 runtime
->status
->state
= state
;
1042 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1043 runtime
->silence_size
> 0)
1044 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1045 if (substream
->timer
)
1046 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
1047 &runtime
->trigger_tstamp
);
1050 static struct action_ops snd_pcm_action_start
= {
1051 .pre_action
= snd_pcm_pre_start
,
1052 .do_action
= snd_pcm_do_start
,
1053 .undo_action
= snd_pcm_undo_start
,
1054 .post_action
= snd_pcm_post_start
1058 * snd_pcm_start - start all linked streams
1059 * @substream: the PCM substream instance
1061 * Return: Zero if successful, or a negative error code.
1063 int snd_pcm_start(struct snd_pcm_substream
*substream
)
1065 return snd_pcm_action(&snd_pcm_action_start
, substream
,
1066 SNDRV_PCM_STATE_RUNNING
);
1072 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
1074 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1075 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1077 runtime
->trigger_master
= substream
;
1081 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
1083 if (substream
->runtime
->trigger_master
== substream
&&
1084 snd_pcm_running(substream
))
1085 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1086 return 0; /* unconditonally stop all substreams */
1089 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
1091 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1092 if (runtime
->status
->state
!= state
) {
1093 snd_pcm_trigger_tstamp(substream
);
1094 runtime
->status
->state
= state
;
1095 if (substream
->timer
)
1096 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
1097 &runtime
->trigger_tstamp
);
1099 wake_up(&runtime
->sleep
);
1100 wake_up(&runtime
->tsleep
);
1103 static struct action_ops snd_pcm_action_stop
= {
1104 .pre_action
= snd_pcm_pre_stop
,
1105 .do_action
= snd_pcm_do_stop
,
1106 .post_action
= snd_pcm_post_stop
1110 * snd_pcm_stop - try to stop all running streams in the substream group
1111 * @substream: the PCM substream instance
1112 * @state: PCM state after stopping the stream
1114 * The state of each stream is then changed to the given state unconditionally.
1116 * Return: Zero if successful, or a negative error code.
1118 int snd_pcm_stop(struct snd_pcm_substream
*substream
, snd_pcm_state_t state
)
1120 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
1123 EXPORT_SYMBOL(snd_pcm_stop
);
1126 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1127 * @substream: the PCM substream
1129 * After stopping, the state is changed to SETUP.
1130 * Unlike snd_pcm_stop(), this affects only the given stream.
1132 * Return: Zero if succesful, or a negative error code.
1134 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
1136 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
1137 SNDRV_PCM_STATE_SETUP
);
1141 * snd_pcm_stop_xrun - stop the running streams as XRUN
1142 * @substream: the PCM substream instance
1144 * This stops the given running substream (and all linked substreams) as XRUN.
1145 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1147 * Return: Zero if successful, or a negative error code.
1149 int snd_pcm_stop_xrun(struct snd_pcm_substream
*substream
)
1151 unsigned long flags
;
1154 snd_pcm_stream_lock_irqsave(substream
, flags
);
1155 if (snd_pcm_running(substream
))
1156 ret
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1157 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1160 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun
);
1165 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
1167 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1168 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1171 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1173 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1175 runtime
->trigger_master
= substream
;
1179 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1181 if (substream
->runtime
->trigger_master
!= substream
)
1183 /* some drivers might use hw_ptr to recover from the pause -
1184 update the hw_ptr now */
1186 snd_pcm_update_hw_ptr(substream
);
1187 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1188 * a delta between the current jiffies, this gives a large enough
1189 * delta, effectively to skip the check once.
1191 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1192 return substream
->ops
->trigger(substream
,
1193 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1194 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1197 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1199 if (substream
->runtime
->trigger_master
== substream
)
1200 substream
->ops
->trigger(substream
,
1201 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1202 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1205 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1207 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1208 snd_pcm_trigger_tstamp(substream
);
1210 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1211 if (substream
->timer
)
1212 snd_timer_notify(substream
->timer
,
1213 SNDRV_TIMER_EVENT_MPAUSE
,
1214 &runtime
->trigger_tstamp
);
1215 wake_up(&runtime
->sleep
);
1216 wake_up(&runtime
->tsleep
);
1218 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1219 if (substream
->timer
)
1220 snd_timer_notify(substream
->timer
,
1221 SNDRV_TIMER_EVENT_MCONTINUE
,
1222 &runtime
->trigger_tstamp
);
1226 static struct action_ops snd_pcm_action_pause
= {
1227 .pre_action
= snd_pcm_pre_pause
,
1228 .do_action
= snd_pcm_do_pause
,
1229 .undo_action
= snd_pcm_undo_pause
,
1230 .post_action
= snd_pcm_post_pause
1234 * Push/release the pause for all linked streams.
1236 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1238 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1244 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1246 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1247 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1249 runtime
->trigger_master
= substream
;
1253 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1255 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1256 if (runtime
->trigger_master
!= substream
)
1258 if (! snd_pcm_running(substream
))
1260 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1261 return 0; /* suspend unconditionally */
1264 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1266 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1267 snd_pcm_trigger_tstamp(substream
);
1268 runtime
->status
->suspended_state
= runtime
->status
->state
;
1269 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1270 if (substream
->timer
)
1271 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1272 &runtime
->trigger_tstamp
);
1273 wake_up(&runtime
->sleep
);
1274 wake_up(&runtime
->tsleep
);
1277 static struct action_ops snd_pcm_action_suspend
= {
1278 .pre_action
= snd_pcm_pre_suspend
,
1279 .do_action
= snd_pcm_do_suspend
,
1280 .post_action
= snd_pcm_post_suspend
1284 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1285 * @substream: the PCM substream
1287 * After this call, all streams are changed to SUSPENDED state.
1289 * Return: Zero if successful (or @substream is %NULL), or a negative error
1292 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1295 unsigned long flags
;
1300 snd_pcm_stream_lock_irqsave(substream
, flags
);
1301 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1302 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1306 EXPORT_SYMBOL(snd_pcm_suspend
);
1309 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1310 * @pcm: the PCM instance
1312 * After this call, all streams are changed to SUSPENDED state.
1314 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1316 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1318 struct snd_pcm_substream
*substream
;
1319 int stream
, err
= 0;
1324 for (stream
= 0; stream
< 2; stream
++) {
1325 for (substream
= pcm
->streams
[stream
].substream
;
1326 substream
; substream
= substream
->next
) {
1327 /* FIXME: the open/close code should lock this as well */
1328 if (substream
->runtime
== NULL
)
1330 err
= snd_pcm_suspend(substream
);
1331 if (err
< 0 && err
!= -EBUSY
)
1338 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1342 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1344 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1345 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1347 runtime
->trigger_master
= substream
;
1351 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1353 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1354 if (runtime
->trigger_master
!= substream
)
1356 /* DMA not running previously? */
1357 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1358 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1359 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1361 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1364 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1366 if (substream
->runtime
->trigger_master
== substream
&&
1367 snd_pcm_running(substream
))
1368 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1371 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1373 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1374 snd_pcm_trigger_tstamp(substream
);
1375 runtime
->status
->state
= runtime
->status
->suspended_state
;
1376 if (substream
->timer
)
1377 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1378 &runtime
->trigger_tstamp
);
1381 static struct action_ops snd_pcm_action_resume
= {
1382 .pre_action
= snd_pcm_pre_resume
,
1383 .do_action
= snd_pcm_do_resume
,
1384 .undo_action
= snd_pcm_undo_resume
,
1385 .post_action
= snd_pcm_post_resume
1388 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1390 struct snd_card
*card
= substream
->pcm
->card
;
1393 snd_power_lock(card
);
1394 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1395 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1396 snd_power_unlock(card
);
1402 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1407 #endif /* CONFIG_PM */
1412 * Change the RUNNING stream(s) to XRUN state.
1414 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1416 struct snd_card
*card
= substream
->pcm
->card
;
1417 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1420 snd_power_lock(card
);
1421 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1422 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1427 snd_pcm_stream_lock_irq(substream
);
1428 switch (runtime
->status
->state
) {
1429 case SNDRV_PCM_STATE_XRUN
:
1430 result
= 0; /* already there */
1432 case SNDRV_PCM_STATE_RUNNING
:
1433 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1438 snd_pcm_stream_unlock_irq(substream
);
1440 snd_power_unlock(card
);
1447 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1449 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1450 switch (runtime
->status
->state
) {
1451 case SNDRV_PCM_STATE_RUNNING
:
1452 case SNDRV_PCM_STATE_PREPARED
:
1453 case SNDRV_PCM_STATE_PAUSED
:
1454 case SNDRV_PCM_STATE_SUSPENDED
:
1461 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1463 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1464 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1467 runtime
->hw_ptr_base
= 0;
1468 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1469 runtime
->status
->hw_ptr
% runtime
->period_size
;
1470 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1471 runtime
->silence_filled
= 0;
1475 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1477 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1478 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1479 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1480 runtime
->silence_size
> 0)
1481 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1484 static struct action_ops snd_pcm_action_reset
= {
1485 .pre_action
= snd_pcm_pre_reset
,
1486 .do_action
= snd_pcm_do_reset
,
1487 .post_action
= snd_pcm_post_reset
1490 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1492 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1498 /* we use the second argument for updating f_flags */
1499 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1502 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1503 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1504 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1506 if (snd_pcm_running(substream
))
1508 substream
->f_flags
= f_flags
;
1512 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1515 err
= substream
->ops
->prepare(substream
);
1518 return snd_pcm_do_reset(substream
, 0);
1521 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1523 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1524 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1525 snd_pcm_set_state(substream
, SNDRV_PCM_STATE_PREPARED
);
1528 static struct action_ops snd_pcm_action_prepare
= {
1529 .pre_action
= snd_pcm_pre_prepare
,
1530 .do_action
= snd_pcm_do_prepare
,
1531 .post_action
= snd_pcm_post_prepare
1535 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1536 * @substream: the PCM substream instance
1537 * @file: file to refer f_flags
1539 * Return: Zero if successful, or a negative error code.
1541 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1545 struct snd_card
*card
= substream
->pcm
->card
;
1549 f_flags
= file
->f_flags
;
1551 f_flags
= substream
->f_flags
;
1553 snd_power_lock(card
);
1554 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1555 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1556 substream
, f_flags
);
1557 snd_power_unlock(card
);
1565 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1567 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1568 switch (runtime
->status
->state
) {
1569 case SNDRV_PCM_STATE_OPEN
:
1570 case SNDRV_PCM_STATE_DISCONNECTED
:
1571 case SNDRV_PCM_STATE_SUSPENDED
:
1574 runtime
->trigger_master
= substream
;
1578 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1580 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1581 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1582 switch (runtime
->status
->state
) {
1583 case SNDRV_PCM_STATE_PREPARED
:
1584 /* start playback stream if possible */
1585 if (! snd_pcm_playback_empty(substream
)) {
1586 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1587 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1589 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1592 case SNDRV_PCM_STATE_RUNNING
:
1593 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1595 case SNDRV_PCM_STATE_XRUN
:
1596 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
1602 /* stop running stream */
1603 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1604 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1605 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1606 snd_pcm_do_stop(substream
, new_state
);
1607 snd_pcm_post_stop(substream
, new_state
);
1611 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
&&
1612 runtime
->trigger_master
== substream
&&
1613 (runtime
->hw
.info
& SNDRV_PCM_INFO_DRAIN_TRIGGER
))
1614 return substream
->ops
->trigger(substream
,
1615 SNDRV_PCM_TRIGGER_DRAIN
);
1620 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1624 static struct action_ops snd_pcm_action_drain_init
= {
1625 .pre_action
= snd_pcm_pre_drain_init
,
1626 .do_action
= snd_pcm_do_drain_init
,
1627 .post_action
= snd_pcm_post_drain_init
1630 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1633 * Drain the stream(s).
1634 * When the substream is linked, sync until the draining of all playback streams
1636 * After this call, all streams are supposed to be either SETUP or DRAINING
1637 * (capture only) state.
1639 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1642 struct snd_card
*card
;
1643 struct snd_pcm_runtime
*runtime
;
1644 struct snd_pcm_substream
*s
;
1649 card
= substream
->pcm
->card
;
1650 runtime
= substream
->runtime
;
1652 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1655 snd_power_lock(card
);
1656 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1657 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1659 snd_power_unlock(card
);
1665 if (file
->f_flags
& O_NONBLOCK
)
1667 } else if (substream
->f_flags
& O_NONBLOCK
)
1670 down_read(&snd_pcm_link_rwsem
);
1671 snd_pcm_stream_lock_irq(substream
);
1673 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1674 snd_pcm_pause(substream
, 0);
1676 /* pre-start/stop - all running streams are changed to DRAINING state */
1677 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1680 /* in non-blocking, we don't wait in ioctl but let caller poll */
1688 struct snd_pcm_runtime
*to_check
;
1689 if (signal_pending(current
)) {
1690 result
= -ERESTARTSYS
;
1693 /* find a substream to drain */
1695 snd_pcm_group_for_each_entry(s
, substream
) {
1696 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1698 runtime
= s
->runtime
;
1699 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1705 break; /* all drained */
1706 init_waitqueue_entry(&wait
, current
);
1707 add_wait_queue(&to_check
->sleep
, &wait
);
1708 snd_pcm_stream_unlock_irq(substream
);
1709 up_read(&snd_pcm_link_rwsem
);
1710 snd_power_unlock(card
);
1711 if (runtime
->no_period_wakeup
)
1712 tout
= MAX_SCHEDULE_TIMEOUT
;
1715 if (runtime
->rate
) {
1716 long t
= runtime
->period_size
* 2 / runtime
->rate
;
1717 tout
= max(t
, tout
);
1719 tout
= msecs_to_jiffies(tout
* 1000);
1721 tout
= schedule_timeout_interruptible(tout
);
1722 snd_power_lock(card
);
1723 down_read(&snd_pcm_link_rwsem
);
1724 snd_pcm_stream_lock_irq(substream
);
1725 remove_wait_queue(&to_check
->sleep
, &wait
);
1726 if (card
->shutdown
) {
1731 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1734 dev_dbg(substream
->pcm
->card
->dev
,
1735 "playback drain error (DMA or IRQ trouble?)\n");
1736 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1744 snd_pcm_stream_unlock_irq(substream
);
1745 up_read(&snd_pcm_link_rwsem
);
1746 snd_power_unlock(card
);
1754 * Immediately put all linked substreams into SETUP state.
1756 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1758 struct snd_pcm_runtime
*runtime
;
1761 if (PCM_RUNTIME_CHECK(substream
))
1763 runtime
= substream
->runtime
;
1765 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1766 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1767 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1770 snd_pcm_stream_lock_irq(substream
);
1772 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1773 snd_pcm_pause(substream
, 0);
1775 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1776 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1777 snd_pcm_stream_unlock_irq(substream
);
1783 static bool is_pcm_file(struct file
*file
)
1785 struct inode
*inode
= file_inode(file
);
1788 if (!S_ISCHR(inode
->i_mode
) || imajor(inode
) != snd_major
)
1790 minor
= iminor(inode
);
1791 return snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) ||
1792 snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
1798 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1801 struct snd_pcm_file
*pcm_file
;
1802 struct snd_pcm_substream
*substream1
;
1803 struct snd_pcm_group
*group
;
1804 struct fd f
= fdget(fd
);
1808 if (!is_pcm_file(f
.file
)) {
1812 pcm_file
= f
.file
->private_data
;
1813 substream1
= pcm_file
->substream
;
1814 group
= kmalloc(sizeof(*group
), GFP_KERNEL
);
1819 down_write(&snd_pcm_link_rwsem
);
1820 write_lock_irq(&snd_pcm_link_rwlock
);
1821 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1822 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
||
1823 substream
->pcm
->nonatomic
!= substream1
->pcm
->nonatomic
) {
1827 if (snd_pcm_stream_linked(substream1
)) {
1831 if (!snd_pcm_stream_linked(substream
)) {
1832 substream
->group
= group
;
1834 spin_lock_init(&substream
->group
->lock
);
1835 mutex_init(&substream
->group
->mutex
);
1836 INIT_LIST_HEAD(&substream
->group
->substreams
);
1837 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1838 substream
->group
->count
= 1;
1840 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1841 substream
->group
->count
++;
1842 substream1
->group
= substream
->group
;
1844 write_unlock_irq(&snd_pcm_link_rwlock
);
1845 up_write(&snd_pcm_link_rwsem
);
1847 snd_card_unref(substream1
->pcm
->card
);
1854 static void relink_to_local(struct snd_pcm_substream
*substream
)
1856 substream
->group
= &substream
->self_group
;
1857 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1858 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1861 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1863 struct snd_pcm_substream
*s
;
1866 down_write(&snd_pcm_link_rwsem
);
1867 write_lock_irq(&snd_pcm_link_rwlock
);
1868 if (!snd_pcm_stream_linked(substream
)) {
1872 list_del(&substream
->link_list
);
1873 substream
->group
->count
--;
1874 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1875 snd_pcm_group_for_each_entry(s
, substream
) {
1879 kfree(substream
->group
);
1881 relink_to_local(substream
);
1883 write_unlock_irq(&snd_pcm_link_rwlock
);
1884 up_write(&snd_pcm_link_rwsem
);
1891 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1892 struct snd_pcm_hw_rule
*rule
)
1894 struct snd_interval t
;
1895 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1896 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1897 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1900 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1901 struct snd_pcm_hw_rule
*rule
)
1903 struct snd_interval t
;
1904 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1905 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1906 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1909 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1910 struct snd_pcm_hw_rule
*rule
)
1912 struct snd_interval t
;
1913 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1914 hw_param_interval_c(params
, rule
->deps
[1]),
1915 (unsigned long) rule
->private, &t
);
1916 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1919 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1920 struct snd_pcm_hw_rule
*rule
)
1922 struct snd_interval t
;
1923 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1924 (unsigned long) rule
->private,
1925 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1926 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1929 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1930 struct snd_pcm_hw_rule
*rule
)
1933 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1935 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1937 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1939 if (! snd_mask_test(mask
, k
))
1941 bits
= snd_pcm_format_physical_width(k
);
1943 continue; /* ignore invalid formats */
1944 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1945 snd_mask_reset(&m
, k
);
1947 return snd_mask_refine(mask
, &m
);
1950 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1951 struct snd_pcm_hw_rule
*rule
)
1953 struct snd_interval t
;
1959 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1961 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1963 bits
= snd_pcm_format_physical_width(k
);
1965 continue; /* ignore invalid formats */
1966 if (t
.min
> (unsigned)bits
)
1968 if (t
.max
< (unsigned)bits
)
1972 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1975 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1976 #error "Change this table"
1979 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1980 48000, 64000, 88200, 96000, 176400, 192000 };
1982 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1983 .count
= ARRAY_SIZE(rates
),
1987 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1988 struct snd_pcm_hw_rule
*rule
)
1990 struct snd_pcm_hardware
*hw
= rule
->private;
1991 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1992 snd_pcm_known_rates
.count
,
1993 snd_pcm_known_rates
.list
, hw
->rates
);
1996 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1997 struct snd_pcm_hw_rule
*rule
)
1999 struct snd_interval t
;
2000 struct snd_pcm_substream
*substream
= rule
->private;
2002 t
.max
= substream
->buffer_bytes_max
;
2006 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
2009 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
2011 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2012 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
2015 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
2016 snd_mask_any(constrs_mask(constrs
, k
));
2019 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
2020 snd_interval_any(constrs_interval(constrs
, k
));
2023 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
2024 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
2025 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
2026 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
2027 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
2029 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
2030 snd_pcm_hw_rule_format
, NULL
,
2031 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2034 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2035 snd_pcm_hw_rule_sample_bits
, NULL
,
2036 SNDRV_PCM_HW_PARAM_FORMAT
,
2037 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2040 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
2041 snd_pcm_hw_rule_div
, NULL
,
2042 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2045 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2046 snd_pcm_hw_rule_mul
, NULL
,
2047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
2050 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2051 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2052 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2055 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
2056 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2057 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
2060 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
2061 snd_pcm_hw_rule_div
, NULL
,
2062 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
2065 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2066 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2067 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
2070 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2071 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2072 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
2075 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
2076 snd_pcm_hw_rule_div
, NULL
,
2077 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
2080 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2081 snd_pcm_hw_rule_div
, NULL
,
2082 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2085 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2086 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2087 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2090 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
2091 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2092 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2095 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2096 snd_pcm_hw_rule_mul
, NULL
,
2097 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
2100 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2101 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
2102 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2105 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
2106 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
2107 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2110 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2111 snd_pcm_hw_rule_muldivk
, (void*) 8,
2112 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2115 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2116 snd_pcm_hw_rule_muldivk
, (void*) 8,
2117 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
2120 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
2121 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2122 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2125 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
2126 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
2127 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
2133 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
2135 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2136 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
2138 unsigned int mask
= 0;
2140 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2141 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
2142 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2143 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
2144 if (hw_support_mmap(substream
)) {
2145 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
2146 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
2147 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
2148 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
2149 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
2150 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
2152 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
2156 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
2160 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
2164 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
2165 hw
->channels_min
, hw
->channels_max
);
2169 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
2170 hw
->rate_min
, hw
->rate_max
);
2174 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
2175 hw
->period_bytes_min
, hw
->period_bytes_max
);
2179 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
2180 hw
->periods_min
, hw
->periods_max
);
2184 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2185 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
2189 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
2190 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
2191 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
2196 if (runtime
->dma_bytes
) {
2197 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2202 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2203 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2204 snd_pcm_hw_rule_rate
, hw
,
2205 SNDRV_PCM_HW_PARAM_RATE
, -1);
2210 /* FIXME: this belong to lowlevel */
2211 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2216 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2218 snd_pcm_unlink(substream
);
2221 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2223 substream
->ref_count
--;
2224 if (substream
->ref_count
> 0)
2227 snd_pcm_drop(substream
);
2228 if (substream
->hw_opened
) {
2229 if (substream
->ops
->hw_free
!= NULL
)
2230 substream
->ops
->hw_free(substream
);
2231 substream
->ops
->close(substream
);
2232 substream
->hw_opened
= 0;
2234 if (pm_qos_request_active(&substream
->latency_pm_qos_req
))
2235 pm_qos_remove_request(&substream
->latency_pm_qos_req
);
2236 if (substream
->pcm_release
) {
2237 substream
->pcm_release(substream
);
2238 substream
->pcm_release
= NULL
;
2240 snd_pcm_detach_substream(substream
);
2243 EXPORT_SYMBOL(snd_pcm_release_substream
);
2245 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2247 struct snd_pcm_substream
**rsubstream
)
2249 struct snd_pcm_substream
*substream
;
2252 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2255 if (substream
->ref_count
> 1) {
2256 *rsubstream
= substream
;
2260 err
= snd_pcm_hw_constraints_init(substream
);
2262 pcm_dbg(pcm
, "snd_pcm_hw_constraints_init failed\n");
2266 if ((err
= substream
->ops
->open(substream
)) < 0)
2269 substream
->hw_opened
= 1;
2271 err
= snd_pcm_hw_constraints_complete(substream
);
2273 pcm_dbg(pcm
, "snd_pcm_hw_constraints_complete failed\n");
2277 *rsubstream
= substream
;
2281 snd_pcm_release_substream(substream
);
2285 EXPORT_SYMBOL(snd_pcm_open_substream
);
2287 static int snd_pcm_open_file(struct file
*file
,
2288 struct snd_pcm
*pcm
,
2291 struct snd_pcm_file
*pcm_file
;
2292 struct snd_pcm_substream
*substream
;
2295 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2299 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2300 if (pcm_file
== NULL
) {
2301 snd_pcm_release_substream(substream
);
2304 pcm_file
->substream
= substream
;
2305 if (substream
->ref_count
== 1) {
2306 substream
->file
= pcm_file
;
2307 substream
->pcm_release
= pcm_release_private
;
2309 file
->private_data
= pcm_file
;
2314 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2316 struct snd_pcm
*pcm
;
2317 int err
= nonseekable_open(inode
, file
);
2320 pcm
= snd_lookup_minor_data(iminor(inode
),
2321 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2322 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2324 snd_card_unref(pcm
->card
);
2328 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2330 struct snd_pcm
*pcm
;
2331 int err
= nonseekable_open(inode
, file
);
2334 pcm
= snd_lookup_minor_data(iminor(inode
),
2335 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2336 err
= snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2338 snd_card_unref(pcm
->card
);
2342 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2351 err
= snd_card_file_add(pcm
->card
, file
);
2354 if (!try_module_get(pcm
->card
->module
)) {
2358 init_waitqueue_entry(&wait
, current
);
2359 add_wait_queue(&pcm
->open_wait
, &wait
);
2360 mutex_lock(&pcm
->open_mutex
);
2362 err
= snd_pcm_open_file(file
, pcm
, stream
);
2365 if (err
== -EAGAIN
) {
2366 if (file
->f_flags
& O_NONBLOCK
) {
2372 set_current_state(TASK_INTERRUPTIBLE
);
2373 mutex_unlock(&pcm
->open_mutex
);
2375 mutex_lock(&pcm
->open_mutex
);
2376 if (pcm
->card
->shutdown
) {
2380 if (signal_pending(current
)) {
2385 remove_wait_queue(&pcm
->open_wait
, &wait
);
2386 mutex_unlock(&pcm
->open_mutex
);
2392 module_put(pcm
->card
->module
);
2394 snd_card_file_remove(pcm
->card
, file
);
2399 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2401 struct snd_pcm
*pcm
;
2402 struct snd_pcm_substream
*substream
;
2403 struct snd_pcm_file
*pcm_file
;
2405 pcm_file
= file
->private_data
;
2406 substream
= pcm_file
->substream
;
2407 if (snd_BUG_ON(!substream
))
2409 pcm
= substream
->pcm
;
2410 mutex_lock(&pcm
->open_mutex
);
2411 snd_pcm_release_substream(substream
);
2413 mutex_unlock(&pcm
->open_mutex
);
2414 wake_up(&pcm
->open_wait
);
2415 module_put(pcm
->card
->module
);
2416 snd_card_file_remove(pcm
->card
, file
);
2420 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2421 snd_pcm_uframes_t frames
)
2423 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2424 snd_pcm_sframes_t appl_ptr
;
2425 snd_pcm_sframes_t ret
;
2426 snd_pcm_sframes_t hw_avail
;
2431 snd_pcm_stream_lock_irq(substream
);
2432 switch (runtime
->status
->state
) {
2433 case SNDRV_PCM_STATE_PREPARED
:
2435 case SNDRV_PCM_STATE_DRAINING
:
2436 case SNDRV_PCM_STATE_RUNNING
:
2437 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2440 case SNDRV_PCM_STATE_XRUN
:
2443 case SNDRV_PCM_STATE_SUSPENDED
:
2451 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2452 if (hw_avail
<= 0) {
2456 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2458 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2460 appl_ptr
+= runtime
->boundary
;
2461 runtime
->control
->appl_ptr
= appl_ptr
;
2464 snd_pcm_stream_unlock_irq(substream
);
2468 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2469 snd_pcm_uframes_t frames
)
2471 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2472 snd_pcm_sframes_t appl_ptr
;
2473 snd_pcm_sframes_t ret
;
2474 snd_pcm_sframes_t hw_avail
;
2479 snd_pcm_stream_lock_irq(substream
);
2480 switch (runtime
->status
->state
) {
2481 case SNDRV_PCM_STATE_PREPARED
:
2482 case SNDRV_PCM_STATE_DRAINING
:
2484 case SNDRV_PCM_STATE_RUNNING
:
2485 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2488 case SNDRV_PCM_STATE_XRUN
:
2491 case SNDRV_PCM_STATE_SUSPENDED
:
2499 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2500 if (hw_avail
<= 0) {
2504 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2506 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2508 appl_ptr
+= runtime
->boundary
;
2509 runtime
->control
->appl_ptr
= appl_ptr
;
2512 snd_pcm_stream_unlock_irq(substream
);
2516 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2517 snd_pcm_uframes_t frames
)
2519 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2520 snd_pcm_sframes_t appl_ptr
;
2521 snd_pcm_sframes_t ret
;
2522 snd_pcm_sframes_t avail
;
2527 snd_pcm_stream_lock_irq(substream
);
2528 switch (runtime
->status
->state
) {
2529 case SNDRV_PCM_STATE_PREPARED
:
2530 case SNDRV_PCM_STATE_PAUSED
:
2532 case SNDRV_PCM_STATE_DRAINING
:
2533 case SNDRV_PCM_STATE_RUNNING
:
2534 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2537 case SNDRV_PCM_STATE_XRUN
:
2540 case SNDRV_PCM_STATE_SUSPENDED
:
2548 avail
= snd_pcm_playback_avail(runtime
);
2553 if (frames
> (snd_pcm_uframes_t
)avail
)
2555 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2556 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2557 appl_ptr
-= runtime
->boundary
;
2558 runtime
->control
->appl_ptr
= appl_ptr
;
2561 snd_pcm_stream_unlock_irq(substream
);
2565 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2566 snd_pcm_uframes_t frames
)
2568 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2569 snd_pcm_sframes_t appl_ptr
;
2570 snd_pcm_sframes_t ret
;
2571 snd_pcm_sframes_t avail
;
2576 snd_pcm_stream_lock_irq(substream
);
2577 switch (runtime
->status
->state
) {
2578 case SNDRV_PCM_STATE_PREPARED
:
2579 case SNDRV_PCM_STATE_DRAINING
:
2580 case SNDRV_PCM_STATE_PAUSED
:
2582 case SNDRV_PCM_STATE_RUNNING
:
2583 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2586 case SNDRV_PCM_STATE_XRUN
:
2589 case SNDRV_PCM_STATE_SUSPENDED
:
2597 avail
= snd_pcm_capture_avail(runtime
);
2602 if (frames
> (snd_pcm_uframes_t
)avail
)
2604 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2605 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2606 appl_ptr
-= runtime
->boundary
;
2607 runtime
->control
->appl_ptr
= appl_ptr
;
2610 snd_pcm_stream_unlock_irq(substream
);
2614 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2616 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2619 snd_pcm_stream_lock_irq(substream
);
2620 switch (runtime
->status
->state
) {
2621 case SNDRV_PCM_STATE_DRAINING
:
2622 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2625 case SNDRV_PCM_STATE_RUNNING
:
2626 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2629 case SNDRV_PCM_STATE_PREPARED
:
2630 case SNDRV_PCM_STATE_SUSPENDED
:
2633 case SNDRV_PCM_STATE_XRUN
:
2641 snd_pcm_stream_unlock_irq(substream
);
2645 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2646 snd_pcm_sframes_t __user
*res
)
2648 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2650 snd_pcm_sframes_t n
= 0;
2652 snd_pcm_stream_lock_irq(substream
);
2653 switch (runtime
->status
->state
) {
2654 case SNDRV_PCM_STATE_DRAINING
:
2655 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2658 case SNDRV_PCM_STATE_RUNNING
:
2659 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2662 case SNDRV_PCM_STATE_PREPARED
:
2663 case SNDRV_PCM_STATE_SUSPENDED
:
2665 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2666 n
= snd_pcm_playback_hw_avail(runtime
);
2668 n
= snd_pcm_capture_avail(runtime
);
2669 n
+= runtime
->delay
;
2671 case SNDRV_PCM_STATE_XRUN
:
2679 snd_pcm_stream_unlock_irq(substream
);
2681 if (put_user(n
, res
))
2686 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2687 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2689 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2690 struct snd_pcm_sync_ptr sync_ptr
;
2691 volatile struct snd_pcm_mmap_status
*status
;
2692 volatile struct snd_pcm_mmap_control
*control
;
2695 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2696 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2698 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2700 status
= runtime
->status
;
2701 control
= runtime
->control
;
2702 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2703 err
= snd_pcm_hwsync(substream
);
2707 snd_pcm_stream_lock_irq(substream
);
2708 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2709 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2711 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2712 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2713 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2715 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2716 sync_ptr
.s
.status
.state
= status
->state
;
2717 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2718 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2719 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2720 snd_pcm_stream_unlock_irq(substream
);
2721 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2726 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2728 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2731 if (get_user(arg
, _arg
))
2733 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2735 runtime
->tstamp_type
= arg
;
2739 static int snd_pcm_common_ioctl1(struct file
*file
,
2740 struct snd_pcm_substream
*substream
,
2741 unsigned int cmd
, void __user
*arg
)
2744 case SNDRV_PCM_IOCTL_PVERSION
:
2745 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2746 case SNDRV_PCM_IOCTL_INFO
:
2747 return snd_pcm_info_user(substream
, arg
);
2748 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2750 case SNDRV_PCM_IOCTL_TTSTAMP
:
2751 return snd_pcm_tstamp(substream
, arg
);
2752 case SNDRV_PCM_IOCTL_HW_REFINE
:
2753 return snd_pcm_hw_refine_user(substream
, arg
);
2754 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2755 return snd_pcm_hw_params_user(substream
, arg
);
2756 case SNDRV_PCM_IOCTL_HW_FREE
:
2757 return snd_pcm_hw_free(substream
);
2758 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2759 return snd_pcm_sw_params_user(substream
, arg
);
2760 case SNDRV_PCM_IOCTL_STATUS
:
2761 return snd_pcm_status_user(substream
, arg
, false);
2762 case SNDRV_PCM_IOCTL_STATUS_EXT
:
2763 return snd_pcm_status_user(substream
, arg
, true);
2764 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2765 return snd_pcm_channel_info_user(substream
, arg
);
2766 case SNDRV_PCM_IOCTL_PREPARE
:
2767 return snd_pcm_prepare(substream
, file
);
2768 case SNDRV_PCM_IOCTL_RESET
:
2769 return snd_pcm_reset(substream
);
2770 case SNDRV_PCM_IOCTL_START
:
2771 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2772 case SNDRV_PCM_IOCTL_LINK
:
2773 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2774 case SNDRV_PCM_IOCTL_UNLINK
:
2775 return snd_pcm_unlink(substream
);
2776 case SNDRV_PCM_IOCTL_RESUME
:
2777 return snd_pcm_resume(substream
);
2778 case SNDRV_PCM_IOCTL_XRUN
:
2779 return snd_pcm_xrun(substream
);
2780 case SNDRV_PCM_IOCTL_HWSYNC
:
2781 return snd_pcm_hwsync(substream
);
2782 case SNDRV_PCM_IOCTL_DELAY
:
2783 return snd_pcm_delay(substream
, arg
);
2784 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2785 return snd_pcm_sync_ptr(substream
, arg
);
2786 #ifdef CONFIG_SND_SUPPORT_OLD_API
2787 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2788 return snd_pcm_hw_refine_old_user(substream
, arg
);
2789 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2790 return snd_pcm_hw_params_old_user(substream
, arg
);
2792 case SNDRV_PCM_IOCTL_DRAIN
:
2793 return snd_pcm_drain(substream
, file
);
2794 case SNDRV_PCM_IOCTL_DROP
:
2795 return snd_pcm_drop(substream
);
2796 case SNDRV_PCM_IOCTL_PAUSE
:
2799 snd_pcm_stream_lock_irq(substream
);
2800 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2801 snd_pcm_stream_unlock_irq(substream
);
2805 pcm_dbg(substream
->pcm
, "unknown ioctl = 0x%x\n", cmd
);
2809 static int snd_pcm_playback_ioctl1(struct file
*file
,
2810 struct snd_pcm_substream
*substream
,
2811 unsigned int cmd
, void __user
*arg
)
2813 if (snd_BUG_ON(!substream
))
2815 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2818 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2820 struct snd_xferi xferi
;
2821 struct snd_xferi __user
*_xferi
= arg
;
2822 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2823 snd_pcm_sframes_t result
;
2824 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2826 if (put_user(0, &_xferi
->result
))
2828 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2830 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2831 __put_user(result
, &_xferi
->result
);
2832 return result
< 0 ? result
: 0;
2834 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2836 struct snd_xfern xfern
;
2837 struct snd_xfern __user
*_xfern
= arg
;
2838 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2840 snd_pcm_sframes_t result
;
2841 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2843 if (runtime
->channels
> 128)
2845 if (put_user(0, &_xfern
->result
))
2847 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2850 bufs
= memdup_user(xfern
.bufs
,
2851 sizeof(void *) * runtime
->channels
);
2853 return PTR_ERR(bufs
);
2854 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2856 __put_user(result
, &_xfern
->result
);
2857 return result
< 0 ? result
: 0;
2859 case SNDRV_PCM_IOCTL_REWIND
:
2861 snd_pcm_uframes_t frames
;
2862 snd_pcm_uframes_t __user
*_frames
= arg
;
2863 snd_pcm_sframes_t result
;
2864 if (get_user(frames
, _frames
))
2866 if (put_user(0, _frames
))
2868 result
= snd_pcm_playback_rewind(substream
, frames
);
2869 __put_user(result
, _frames
);
2870 return result
< 0 ? result
: 0;
2872 case SNDRV_PCM_IOCTL_FORWARD
:
2874 snd_pcm_uframes_t frames
;
2875 snd_pcm_uframes_t __user
*_frames
= arg
;
2876 snd_pcm_sframes_t result
;
2877 if (get_user(frames
, _frames
))
2879 if (put_user(0, _frames
))
2881 result
= snd_pcm_playback_forward(substream
, frames
);
2882 __put_user(result
, _frames
);
2883 return result
< 0 ? result
: 0;
2886 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2889 static int snd_pcm_capture_ioctl1(struct file
*file
,
2890 struct snd_pcm_substream
*substream
,
2891 unsigned int cmd
, void __user
*arg
)
2893 if (snd_BUG_ON(!substream
))
2895 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2898 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2900 struct snd_xferi xferi
;
2901 struct snd_xferi __user
*_xferi
= arg
;
2902 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2903 snd_pcm_sframes_t result
;
2904 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2906 if (put_user(0, &_xferi
->result
))
2908 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2910 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2911 __put_user(result
, &_xferi
->result
);
2912 return result
< 0 ? result
: 0;
2914 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2916 struct snd_xfern xfern
;
2917 struct snd_xfern __user
*_xfern
= arg
;
2918 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2920 snd_pcm_sframes_t result
;
2921 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2923 if (runtime
->channels
> 128)
2925 if (put_user(0, &_xfern
->result
))
2927 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2930 bufs
= memdup_user(xfern
.bufs
,
2931 sizeof(void *) * runtime
->channels
);
2933 return PTR_ERR(bufs
);
2934 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2936 __put_user(result
, &_xfern
->result
);
2937 return result
< 0 ? result
: 0;
2939 case SNDRV_PCM_IOCTL_REWIND
:
2941 snd_pcm_uframes_t frames
;
2942 snd_pcm_uframes_t __user
*_frames
= arg
;
2943 snd_pcm_sframes_t result
;
2944 if (get_user(frames
, _frames
))
2946 if (put_user(0, _frames
))
2948 result
= snd_pcm_capture_rewind(substream
, frames
);
2949 __put_user(result
, _frames
);
2950 return result
< 0 ? result
: 0;
2952 case SNDRV_PCM_IOCTL_FORWARD
:
2954 snd_pcm_uframes_t frames
;
2955 snd_pcm_uframes_t __user
*_frames
= arg
;
2956 snd_pcm_sframes_t result
;
2957 if (get_user(frames
, _frames
))
2959 if (put_user(0, _frames
))
2961 result
= snd_pcm_capture_forward(substream
, frames
);
2962 __put_user(result
, _frames
);
2963 return result
< 0 ? result
: 0;
2966 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2969 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2972 struct snd_pcm_file
*pcm_file
;
2974 pcm_file
= file
->private_data
;
2976 if (((cmd
>> 8) & 0xff) != 'A')
2979 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2980 (void __user
*)arg
);
2983 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2986 struct snd_pcm_file
*pcm_file
;
2988 pcm_file
= file
->private_data
;
2990 if (((cmd
>> 8) & 0xff) != 'A')
2993 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2994 (void __user
*)arg
);
2997 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2998 unsigned int cmd
, void *arg
)
3003 fs
= snd_enter_user();
3004 switch (substream
->stream
) {
3005 case SNDRV_PCM_STREAM_PLAYBACK
:
3006 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
3007 (void __user
*)arg
);
3009 case SNDRV_PCM_STREAM_CAPTURE
:
3010 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
3011 (void __user
*)arg
);
3021 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
3023 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
3026 struct snd_pcm_file
*pcm_file
;
3027 struct snd_pcm_substream
*substream
;
3028 struct snd_pcm_runtime
*runtime
;
3029 snd_pcm_sframes_t result
;
3031 pcm_file
= file
->private_data
;
3032 substream
= pcm_file
->substream
;
3033 if (PCM_RUNTIME_CHECK(substream
))
3035 runtime
= substream
->runtime
;
3036 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3038 if (!frame_aligned(runtime
, count
))
3040 count
= bytes_to_frames(runtime
, count
);
3041 result
= snd_pcm_lib_read(substream
, buf
, count
);
3043 result
= frames_to_bytes(runtime
, result
);
3047 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
3048 size_t count
, loff_t
* offset
)
3050 struct snd_pcm_file
*pcm_file
;
3051 struct snd_pcm_substream
*substream
;
3052 struct snd_pcm_runtime
*runtime
;
3053 snd_pcm_sframes_t result
;
3055 pcm_file
= file
->private_data
;
3056 substream
= pcm_file
->substream
;
3057 if (PCM_RUNTIME_CHECK(substream
))
3059 runtime
= substream
->runtime
;
3060 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3062 if (!frame_aligned(runtime
, count
))
3064 count
= bytes_to_frames(runtime
, count
);
3065 result
= snd_pcm_lib_write(substream
, buf
, count
);
3067 result
= frames_to_bytes(runtime
, result
);
3071 static ssize_t
snd_pcm_readv(struct kiocb
*iocb
, struct iov_iter
*to
)
3073 struct snd_pcm_file
*pcm_file
;
3074 struct snd_pcm_substream
*substream
;
3075 struct snd_pcm_runtime
*runtime
;
3076 snd_pcm_sframes_t result
;
3079 snd_pcm_uframes_t frames
;
3081 pcm_file
= iocb
->ki_filp
->private_data
;
3082 substream
= pcm_file
->substream
;
3083 if (PCM_RUNTIME_CHECK(substream
))
3085 runtime
= substream
->runtime
;
3086 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3088 if (!iter_is_iovec(to
))
3090 if (to
->nr_segs
> 1024 || to
->nr_segs
!= runtime
->channels
)
3092 if (!frame_aligned(runtime
, to
->iov
->iov_len
))
3094 frames
= bytes_to_samples(runtime
, to
->iov
->iov_len
);
3095 bufs
= kmalloc(sizeof(void *) * to
->nr_segs
, GFP_KERNEL
);
3098 for (i
= 0; i
< to
->nr_segs
; ++i
)
3099 bufs
[i
] = to
->iov
[i
].iov_base
;
3100 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
3102 result
= frames_to_bytes(runtime
, result
);
3107 static ssize_t
snd_pcm_writev(struct kiocb
*iocb
, struct iov_iter
*from
)
3109 struct snd_pcm_file
*pcm_file
;
3110 struct snd_pcm_substream
*substream
;
3111 struct snd_pcm_runtime
*runtime
;
3112 snd_pcm_sframes_t result
;
3115 snd_pcm_uframes_t frames
;
3117 pcm_file
= iocb
->ki_filp
->private_data
;
3118 substream
= pcm_file
->substream
;
3119 if (PCM_RUNTIME_CHECK(substream
))
3121 runtime
= substream
->runtime
;
3122 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3124 if (!iter_is_iovec(from
))
3126 if (from
->nr_segs
> 128 || from
->nr_segs
!= runtime
->channels
||
3127 !frame_aligned(runtime
, from
->iov
->iov_len
))
3129 frames
= bytes_to_samples(runtime
, from
->iov
->iov_len
);
3130 bufs
= kmalloc(sizeof(void *) * from
->nr_segs
, GFP_KERNEL
);
3133 for (i
= 0; i
< from
->nr_segs
; ++i
)
3134 bufs
[i
] = from
->iov
[i
].iov_base
;
3135 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
3137 result
= frames_to_bytes(runtime
, result
);
3142 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
3144 struct snd_pcm_file
*pcm_file
;
3145 struct snd_pcm_substream
*substream
;
3146 struct snd_pcm_runtime
*runtime
;
3148 snd_pcm_uframes_t avail
;
3150 pcm_file
= file
->private_data
;
3152 substream
= pcm_file
->substream
;
3153 if (PCM_RUNTIME_CHECK(substream
))
3155 runtime
= substream
->runtime
;
3157 poll_wait(file
, &runtime
->sleep
, wait
);
3159 snd_pcm_stream_lock_irq(substream
);
3160 avail
= snd_pcm_playback_avail(runtime
);
3161 switch (runtime
->status
->state
) {
3162 case SNDRV_PCM_STATE_RUNNING
:
3163 case SNDRV_PCM_STATE_PREPARED
:
3164 case SNDRV_PCM_STATE_PAUSED
:
3165 if (avail
>= runtime
->control
->avail_min
) {
3166 mask
= POLLOUT
| POLLWRNORM
;
3170 case SNDRV_PCM_STATE_DRAINING
:
3174 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
3177 snd_pcm_stream_unlock_irq(substream
);
3181 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
3183 struct snd_pcm_file
*pcm_file
;
3184 struct snd_pcm_substream
*substream
;
3185 struct snd_pcm_runtime
*runtime
;
3187 snd_pcm_uframes_t avail
;
3189 pcm_file
= file
->private_data
;
3191 substream
= pcm_file
->substream
;
3192 if (PCM_RUNTIME_CHECK(substream
))
3194 runtime
= substream
->runtime
;
3196 poll_wait(file
, &runtime
->sleep
, wait
);
3198 snd_pcm_stream_lock_irq(substream
);
3199 avail
= snd_pcm_capture_avail(runtime
);
3200 switch (runtime
->status
->state
) {
3201 case SNDRV_PCM_STATE_RUNNING
:
3202 case SNDRV_PCM_STATE_PREPARED
:
3203 case SNDRV_PCM_STATE_PAUSED
:
3204 if (avail
>= runtime
->control
->avail_min
) {
3205 mask
= POLLIN
| POLLRDNORM
;
3210 case SNDRV_PCM_STATE_DRAINING
:
3212 mask
= POLLIN
| POLLRDNORM
;
3217 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3220 snd_pcm_stream_unlock_irq(substream
);
3229 * Only on coherent architectures, we can mmap the status and the control records
3230 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3232 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3234 * mmap status record
3236 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
3237 struct vm_fault
*vmf
)
3239 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3240 struct snd_pcm_runtime
*runtime
;
3242 if (substream
== NULL
)
3243 return VM_FAULT_SIGBUS
;
3244 runtime
= substream
->runtime
;
3245 vmf
->page
= virt_to_page(runtime
->status
);
3246 get_page(vmf
->page
);
3250 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3252 .fault
= snd_pcm_mmap_status_fault
,
3255 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3256 struct vm_area_struct
*area
)
3259 if (!(area
->vm_flags
& VM_READ
))
3261 size
= area
->vm_end
- area
->vm_start
;
3262 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3264 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3265 area
->vm_private_data
= substream
;
3266 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3271 * mmap control record
3273 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3274 struct vm_fault
*vmf
)
3276 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3277 struct snd_pcm_runtime
*runtime
;
3279 if (substream
== NULL
)
3280 return VM_FAULT_SIGBUS
;
3281 runtime
= substream
->runtime
;
3282 vmf
->page
= virt_to_page(runtime
->control
);
3283 get_page(vmf
->page
);
3287 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3289 .fault
= snd_pcm_mmap_control_fault
,
3292 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3293 struct vm_area_struct
*area
)
3296 if (!(area
->vm_flags
& VM_READ
))
3298 size
= area
->vm_end
- area
->vm_start
;
3299 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3301 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3302 area
->vm_private_data
= substream
;
3303 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3306 #else /* ! coherent mmap */
3308 * don't support mmap for status and control records.
3310 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3311 struct vm_area_struct
*area
)
3315 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3316 struct vm_area_struct
*area
)
3320 #endif /* coherent mmap */
3322 static inline struct page
*
3323 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3325 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3326 return virt_to_page(vaddr
);
3330 * fault callback for mmapping a RAM page
3332 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3333 struct vm_fault
*vmf
)
3335 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3336 struct snd_pcm_runtime
*runtime
;
3337 unsigned long offset
;
3341 if (substream
== NULL
)
3342 return VM_FAULT_SIGBUS
;
3343 runtime
= substream
->runtime
;
3344 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3345 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3346 if (offset
> dma_bytes
- PAGE_SIZE
)
3347 return VM_FAULT_SIGBUS
;
3348 if (substream
->ops
->page
)
3349 page
= substream
->ops
->page(substream
, offset
);
3351 page
= snd_pcm_default_page_ops(substream
, offset
);
3353 return VM_FAULT_SIGBUS
;
3359 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3360 .open
= snd_pcm_mmap_data_open
,
3361 .close
= snd_pcm_mmap_data_close
,
3364 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3365 .open
= snd_pcm_mmap_data_open
,
3366 .close
= snd_pcm_mmap_data_close
,
3367 .fault
= snd_pcm_mmap_data_fault
,
3371 * mmap the DMA buffer on RAM
3375 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3376 * @substream: PCM substream
3379 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3380 * this function is invoked implicitly.
3382 int snd_pcm_lib_default_mmap(struct snd_pcm_substream
*substream
,
3383 struct vm_area_struct
*area
)
3385 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
3386 #ifdef CONFIG_GENERIC_ALLOCATOR
3387 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV_IRAM
) {
3388 area
->vm_page_prot
= pgprot_writecombine(area
->vm_page_prot
);
3389 return remap_pfn_range(area
, area
->vm_start
,
3390 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
3391 area
->vm_end
- area
->vm_start
, area
->vm_page_prot
);
3393 #endif /* CONFIG_GENERIC_ALLOCATOR */
3394 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3395 if (!substream
->ops
->page
&&
3396 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3397 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3399 substream
->runtime
->dma_area
,
3400 substream
->runtime
->dma_addr
,
3401 area
->vm_end
- area
->vm_start
);
3402 #endif /* CONFIG_X86 */
3403 /* mmap with fault handler */
3404 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3407 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap
);
3410 * mmap the DMA buffer on I/O memory area
3412 #if SNDRV_PCM_INFO_MMAP_IOMEM
3414 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3415 * @substream: PCM substream
3418 * When your hardware uses the iomapped pages as the hardware buffer and
3419 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3420 * is supposed to work only on limited architectures.
3422 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3423 struct vm_area_struct
*area
)
3425 struct snd_pcm_runtime
*runtime
= substream
->runtime
;;
3427 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3428 return vm_iomap_memory(area
, runtime
->dma_addr
, runtime
->dma_bytes
);
3431 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3432 #endif /* SNDRV_PCM_INFO_MMAP */
3437 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3438 struct vm_area_struct
*area
)
3440 struct snd_pcm_runtime
*runtime
;
3442 unsigned long offset
;
3446 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3447 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3450 if (!(area
->vm_flags
& VM_READ
))
3453 runtime
= substream
->runtime
;
3454 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3456 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3458 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3459 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3461 size
= area
->vm_end
- area
->vm_start
;
3462 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3463 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3464 if ((size_t)size
> dma_bytes
)
3466 if (offset
> dma_bytes
- size
)
3469 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3470 area
->vm_private_data
= substream
;
3471 if (substream
->ops
->mmap
)
3472 err
= substream
->ops
->mmap(substream
, area
);
3474 err
= snd_pcm_lib_default_mmap(substream
, area
);
3476 atomic_inc(&substream
->mmap_count
);
3480 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3482 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3484 struct snd_pcm_file
* pcm_file
;
3485 struct snd_pcm_substream
*substream
;
3486 unsigned long offset
;
3488 pcm_file
= file
->private_data
;
3489 substream
= pcm_file
->substream
;
3490 if (PCM_RUNTIME_CHECK(substream
))
3493 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3495 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3496 if (pcm_file
->no_compat_mmap
)
3498 return snd_pcm_mmap_status(substream
, file
, area
);
3499 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3500 if (pcm_file
->no_compat_mmap
)
3502 return snd_pcm_mmap_control(substream
, file
, area
);
3504 return snd_pcm_mmap_data(substream
, file
, area
);
3509 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3511 struct snd_pcm_file
* pcm_file
;
3512 struct snd_pcm_substream
*substream
;
3513 struct snd_pcm_runtime
*runtime
;
3515 pcm_file
= file
->private_data
;
3516 substream
= pcm_file
->substream
;
3517 if (PCM_RUNTIME_CHECK(substream
))
3519 runtime
= substream
->runtime
;
3520 return fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3526 #ifdef CONFIG_COMPAT
3527 #include "pcm_compat.c"
3529 #define snd_pcm_ioctl_compat NULL
3533 * To be removed helpers to keep binary compatibility
3536 #ifdef CONFIG_SND_SUPPORT_OLD_API
3537 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3538 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3540 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3541 struct snd_pcm_hw_params_old
*oparams
)
3545 memset(params
, 0, sizeof(*params
));
3546 params
->flags
= oparams
->flags
;
3547 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3548 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3549 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3550 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3551 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3552 params
->info
= oparams
->info
;
3553 params
->msbits
= oparams
->msbits
;
3554 params
->rate_num
= oparams
->rate_num
;
3555 params
->rate_den
= oparams
->rate_den
;
3556 params
->fifo_size
= oparams
->fifo_size
;
3559 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3560 struct snd_pcm_hw_params
*params
)
3564 memset(oparams
, 0, sizeof(*oparams
));
3565 oparams
->flags
= params
->flags
;
3566 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3567 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3568 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3569 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3570 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3571 oparams
->info
= params
->info
;
3572 oparams
->msbits
= params
->msbits
;
3573 oparams
->rate_num
= params
->rate_num
;
3574 oparams
->rate_den
= params
->rate_den
;
3575 oparams
->fifo_size
= params
->fifo_size
;
3578 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3579 struct snd_pcm_hw_params_old __user
* _oparams
)
3581 struct snd_pcm_hw_params
*params
;
3582 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3585 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3589 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3590 if (IS_ERR(oparams
)) {
3591 err
= PTR_ERR(oparams
);
3594 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3595 err
= snd_pcm_hw_refine(substream
, params
);
3596 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3597 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3608 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3609 struct snd_pcm_hw_params_old __user
* _oparams
)
3611 struct snd_pcm_hw_params
*params
;
3612 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3615 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3619 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3620 if (IS_ERR(oparams
)) {
3621 err
= PTR_ERR(oparams
);
3624 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3625 err
= snd_pcm_hw_params(substream
, params
);
3626 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3627 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3637 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3640 static unsigned long snd_pcm_get_unmapped_area(struct file
*file
,
3643 unsigned long pgoff
,
3644 unsigned long flags
)
3646 struct snd_pcm_file
*pcm_file
= file
->private_data
;
3647 struct snd_pcm_substream
*substream
= pcm_file
->substream
;
3648 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
3649 unsigned long offset
= pgoff
<< PAGE_SHIFT
;
3652 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3653 return (unsigned long)runtime
->status
;
3654 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3655 return (unsigned long)runtime
->control
;
3657 return (unsigned long)runtime
->dma_area
+ offset
;
3661 # define snd_pcm_get_unmapped_area NULL
3668 const struct file_operations snd_pcm_f_ops
[2] = {
3670 .owner
= THIS_MODULE
,
3671 .write
= snd_pcm_write
,
3672 .write_iter
= snd_pcm_writev
,
3673 .open
= snd_pcm_playback_open
,
3674 .release
= snd_pcm_release
,
3675 .llseek
= no_llseek
,
3676 .poll
= snd_pcm_playback_poll
,
3677 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3678 .compat_ioctl
= snd_pcm_ioctl_compat
,
3679 .mmap
= snd_pcm_mmap
,
3680 .fasync
= snd_pcm_fasync
,
3681 .get_unmapped_area
= snd_pcm_get_unmapped_area
,
3684 .owner
= THIS_MODULE
,
3685 .read
= snd_pcm_read
,
3686 .read_iter
= snd_pcm_readv
,
3687 .open
= snd_pcm_capture_open
,
3688 .release
= snd_pcm_release
,
3689 .llseek
= no_llseek
,
3690 .poll
= snd_pcm_capture_poll
,
3691 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3692 .compat_ioctl
= snd_pcm_ioctl_compat
,
3693 .mmap
= snd_pcm_mmap
,
3694 .fasync
= snd_pcm_fasync
,
3695 .get_unmapped_area
= snd_pcm_get_unmapped_area
,