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/file.h>
24 #include <linux/slab.h>
25 #include <linux/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.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>
38 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
39 #include <dma-coherence.h>
46 struct snd_pcm_hw_params_old
{
48 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
49 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
50 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
51 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
56 unsigned int rate_num
;
57 unsigned int rate_den
;
58 snd_pcm_uframes_t fifo_size
;
59 unsigned char reserved
[64];
62 #ifdef CONFIG_SND_SUPPORT_OLD_API
63 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
64 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
66 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
67 struct snd_pcm_hw_params_old __user
* _oparams
);
68 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
69 struct snd_pcm_hw_params_old __user
* _oparams
);
71 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
77 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
78 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
80 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
82 static inline mm_segment_t
snd_enter_user(void)
84 mm_segment_t fs
= get_fs();
89 static inline void snd_leave_user(mm_segment_t fs
)
96 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
98 struct snd_pcm_runtime
*runtime
;
99 struct snd_pcm
*pcm
= substream
->pcm
;
100 struct snd_pcm_str
*pstr
= substream
->pstr
;
102 memset(info
, 0, sizeof(*info
));
103 info
->card
= pcm
->card
->number
;
104 info
->device
= pcm
->device
;
105 info
->stream
= substream
->stream
;
106 info
->subdevice
= substream
->number
;
107 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
108 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
109 info
->dev_class
= pcm
->dev_class
;
110 info
->dev_subclass
= pcm
->dev_subclass
;
111 info
->subdevices_count
= pstr
->substream_count
;
112 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
113 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
114 runtime
= substream
->runtime
;
115 /* AB: FIXME!!! This is definitely nonsense */
117 info
->sync
= runtime
->sync
;
118 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
123 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
124 struct snd_pcm_info __user
* _info
)
126 struct snd_pcm_info
*info
;
129 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
132 err
= snd_pcm_info(substream
, info
);
134 if (copy_to_user(_info
, info
, sizeof(*info
)))
144 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
145 char *snd_pcm_hw_param_names
[] = {
149 HW_PARAM(SAMPLE_BITS
),
150 HW_PARAM(FRAME_BITS
),
153 HW_PARAM(PERIOD_TIME
),
154 HW_PARAM(PERIOD_SIZE
),
155 HW_PARAM(PERIOD_BYTES
),
157 HW_PARAM(BUFFER_TIME
),
158 HW_PARAM(BUFFER_SIZE
),
159 HW_PARAM(BUFFER_BYTES
),
164 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
165 struct snd_pcm_hw_params
*params
)
168 struct snd_pcm_hardware
*hw
;
169 struct snd_interval
*i
= NULL
;
170 struct snd_mask
*m
= NULL
;
171 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
172 unsigned int rstamps
[constrs
->rules_num
];
173 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
174 unsigned int stamp
= 2;
178 params
->fifo_size
= 0;
179 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
181 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
182 params
->rate_num
= 0;
183 params
->rate_den
= 0;
186 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
187 m
= hw_param_mask(params
, k
);
188 if (snd_mask_empty(m
))
190 if (!(params
->rmask
& (1 << k
)))
193 printk(KERN_DEBUG
"%s = ", snd_pcm_hw_param_names
[k
]);
194 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
196 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
198 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
201 params
->cmask
|= 1 << k
;
206 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
207 i
= hw_param_interval(params
, k
);
208 if (snd_interval_empty(i
))
210 if (!(params
->rmask
& (1 << k
)))
213 printk(KERN_DEBUG
"%s = ", snd_pcm_hw_param_names
[k
]);
218 i
->openmin
? '(' : '[', i
->min
,
219 i
->max
, i
->openmax
? ')' : ']');
222 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
227 printk("%c%u %u%c\n",
228 i
->openmin
? '(' : '[', i
->min
,
229 i
->max
, i
->openmax
? ')' : ']');
232 params
->cmask
|= 1 << k
;
237 for (k
= 0; k
< constrs
->rules_num
; k
++)
239 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
240 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
243 for (k
= 0; k
< constrs
->rules_num
; k
++) {
244 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
247 if (r
->cond
&& !(r
->cond
& params
->flags
))
249 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
250 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
258 printk(KERN_DEBUG
"Rule %d [%p]: ", k
, r
->func
);
260 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
261 if (hw_is_mask(r
->var
)) {
262 m
= hw_param_mask(params
, r
->var
);
263 printk("%x", *m
->bits
);
265 i
= hw_param_interval(params
, r
->var
);
270 i
->openmin
? '(' : '[', i
->min
,
271 i
->max
, i
->openmax
? ')' : ']');
275 changed
= r
->func(params
, r
);
279 if (hw_is_mask(r
->var
))
280 printk("%x", *m
->bits
);
286 i
->openmin
? '(' : '[', i
->min
,
287 i
->max
, i
->openmax
? ')' : ']');
293 if (changed
&& r
->var
>= 0) {
294 params
->cmask
|= (1 << r
->var
);
295 vstamps
[r
->var
] = stamp
;
303 if (!params
->msbits
) {
304 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
305 if (snd_interval_single(i
))
306 params
->msbits
= snd_interval_value(i
);
309 if (!params
->rate_den
) {
310 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
311 if (snd_interval_single(i
)) {
312 params
->rate_num
= snd_interval_value(i
);
313 params
->rate_den
= 1;
317 hw
= &substream
->runtime
->hw
;
319 params
->info
= hw
->info
& ~SNDRV_PCM_INFO_FIFO_IN_FRAMES
;
320 if (!params
->fifo_size
) {
321 m
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
322 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
323 if (snd_mask_min(m
) == snd_mask_max(m
) &&
324 snd_interval_min(i
) == snd_interval_max(i
)) {
325 changed
= substream
->ops
->ioctl(substream
,
326 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
335 EXPORT_SYMBOL(snd_pcm_hw_refine
);
337 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
338 struct snd_pcm_hw_params __user
* _params
)
340 struct snd_pcm_hw_params
*params
;
343 params
= memdup_user(_params
, sizeof(*params
));
345 return PTR_ERR(params
);
347 err
= snd_pcm_hw_refine(substream
, params
);
348 if (copy_to_user(_params
, params
, sizeof(*params
))) {
357 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
362 return -1; /* invalid */
364 /* take 75% of period time as the deadline */
365 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
366 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
372 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
373 struct snd_pcm_hw_params
*params
)
375 struct snd_pcm_runtime
*runtime
;
378 snd_pcm_uframes_t frames
;
380 if (PCM_RUNTIME_CHECK(substream
))
382 runtime
= substream
->runtime
;
383 snd_pcm_stream_lock_irq(substream
);
384 switch (runtime
->status
->state
) {
385 case SNDRV_PCM_STATE_OPEN
:
386 case SNDRV_PCM_STATE_SETUP
:
387 case SNDRV_PCM_STATE_PREPARED
:
390 snd_pcm_stream_unlock_irq(substream
);
393 snd_pcm_stream_unlock_irq(substream
);
394 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
395 if (!substream
->oss
.oss
)
397 if (atomic_read(&substream
->mmap_count
))
401 err
= snd_pcm_hw_refine(substream
, params
);
405 err
= snd_pcm_hw_params_choose(substream
, params
);
409 if (substream
->ops
->hw_params
!= NULL
) {
410 err
= substream
->ops
->hw_params(substream
, params
);
415 runtime
->access
= params_access(params
);
416 runtime
->format
= params_format(params
);
417 runtime
->subformat
= params_subformat(params
);
418 runtime
->channels
= params_channels(params
);
419 runtime
->rate
= params_rate(params
);
420 runtime
->period_size
= params_period_size(params
);
421 runtime
->periods
= params_periods(params
);
422 runtime
->buffer_size
= params_buffer_size(params
);
423 runtime
->info
= params
->info
;
424 runtime
->rate_num
= params
->rate_num
;
425 runtime
->rate_den
= params
->rate_den
;
427 bits
= snd_pcm_format_physical_width(runtime
->format
);
428 runtime
->sample_bits
= bits
;
429 bits
*= runtime
->channels
;
430 runtime
->frame_bits
= bits
;
432 while (bits
% 8 != 0) {
436 runtime
->byte_align
= bits
/ 8;
437 runtime
->min_align
= frames
;
439 /* Default sw params */
440 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
441 runtime
->period_step
= 1;
442 runtime
->control
->avail_min
= runtime
->period_size
;
443 runtime
->start_threshold
= 1;
444 runtime
->stop_threshold
= runtime
->buffer_size
;
445 runtime
->silence_threshold
= 0;
446 runtime
->silence_size
= 0;
447 runtime
->boundary
= runtime
->buffer_size
;
448 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
449 runtime
->boundary
*= 2;
451 snd_pcm_timer_resolution_change(substream
);
452 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
454 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
455 substream
->latency_id
);
456 if ((usecs
= period_to_usecs(runtime
)) >= 0)
457 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY
,
458 substream
->latency_id
, usecs
);
461 /* hardware might be unuseable from this time,
462 so we force application to retry to set
463 the correct hardware parameter settings */
464 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
465 if (substream
->ops
->hw_free
!= NULL
)
466 substream
->ops
->hw_free(substream
);
470 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
471 struct snd_pcm_hw_params __user
* _params
)
473 struct snd_pcm_hw_params
*params
;
476 params
= memdup_user(_params
, sizeof(*params
));
478 return PTR_ERR(params
);
480 err
= snd_pcm_hw_params(substream
, params
);
481 if (copy_to_user(_params
, params
, sizeof(*params
))) {
490 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
492 struct snd_pcm_runtime
*runtime
;
495 if (PCM_RUNTIME_CHECK(substream
))
497 runtime
= substream
->runtime
;
498 snd_pcm_stream_lock_irq(substream
);
499 switch (runtime
->status
->state
) {
500 case SNDRV_PCM_STATE_SETUP
:
501 case SNDRV_PCM_STATE_PREPARED
:
504 snd_pcm_stream_unlock_irq(substream
);
507 snd_pcm_stream_unlock_irq(substream
);
508 if (atomic_read(&substream
->mmap_count
))
510 if (substream
->ops
->hw_free
)
511 result
= substream
->ops
->hw_free(substream
);
512 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
513 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
514 substream
->latency_id
);
518 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
519 struct snd_pcm_sw_params
*params
)
521 struct snd_pcm_runtime
*runtime
;
524 if (PCM_RUNTIME_CHECK(substream
))
526 runtime
= substream
->runtime
;
527 snd_pcm_stream_lock_irq(substream
);
528 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
529 snd_pcm_stream_unlock_irq(substream
);
532 snd_pcm_stream_unlock_irq(substream
);
534 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
536 if (params
->avail_min
== 0)
538 if (params
->silence_size
>= runtime
->boundary
) {
539 if (params
->silence_threshold
!= 0)
542 if (params
->silence_size
> params
->silence_threshold
)
544 if (params
->silence_threshold
> runtime
->buffer_size
)
548 snd_pcm_stream_lock_irq(substream
);
549 runtime
->tstamp_mode
= params
->tstamp_mode
;
550 runtime
->period_step
= params
->period_step
;
551 runtime
->control
->avail_min
= params
->avail_min
;
552 runtime
->start_threshold
= params
->start_threshold
;
553 runtime
->stop_threshold
= params
->stop_threshold
;
554 runtime
->silence_threshold
= params
->silence_threshold
;
555 runtime
->silence_size
= params
->silence_size
;
556 params
->boundary
= runtime
->boundary
;
557 if (snd_pcm_running(substream
)) {
558 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
559 runtime
->silence_size
> 0)
560 snd_pcm_playback_silence(substream
, ULONG_MAX
);
561 err
= snd_pcm_update_state(substream
, runtime
);
563 snd_pcm_stream_unlock_irq(substream
);
567 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
568 struct snd_pcm_sw_params __user
* _params
)
570 struct snd_pcm_sw_params params
;
572 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
574 err
= snd_pcm_sw_params(substream
, ¶ms
);
575 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
580 int snd_pcm_status(struct snd_pcm_substream
*substream
,
581 struct snd_pcm_status
*status
)
583 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
585 snd_pcm_stream_lock_irq(substream
);
586 status
->state
= runtime
->status
->state
;
587 status
->suspended_state
= runtime
->status
->suspended_state
;
588 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
590 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
591 if (snd_pcm_running(substream
)) {
592 snd_pcm_update_hw_ptr(substream
);
593 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
594 status
->tstamp
= runtime
->status
->tstamp
;
598 snd_pcm_gettime(runtime
, &status
->tstamp
);
600 status
->appl_ptr
= runtime
->control
->appl_ptr
;
601 status
->hw_ptr
= runtime
->status
->hw_ptr
;
602 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
603 status
->avail
= snd_pcm_playback_avail(runtime
);
604 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
605 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
606 status
->delay
= runtime
->buffer_size
- status
->avail
;
607 status
->delay
+= runtime
->delay
;
611 status
->avail
= snd_pcm_capture_avail(runtime
);
612 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
613 status
->delay
= status
->avail
+ runtime
->delay
;
617 status
->avail_max
= runtime
->avail_max
;
618 status
->overrange
= runtime
->overrange
;
619 runtime
->avail_max
= 0;
620 runtime
->overrange
= 0;
622 snd_pcm_stream_unlock_irq(substream
);
626 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
627 struct snd_pcm_status __user
* _status
)
629 struct snd_pcm_status status
;
632 memset(&status
, 0, sizeof(status
));
633 res
= snd_pcm_status(substream
, &status
);
636 if (copy_to_user(_status
, &status
, sizeof(status
)))
641 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
642 struct snd_pcm_channel_info
* info
)
644 struct snd_pcm_runtime
*runtime
;
645 unsigned int channel
;
647 channel
= info
->channel
;
648 runtime
= substream
->runtime
;
649 snd_pcm_stream_lock_irq(substream
);
650 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
651 snd_pcm_stream_unlock_irq(substream
);
654 snd_pcm_stream_unlock_irq(substream
);
655 if (channel
>= runtime
->channels
)
657 memset(info
, 0, sizeof(*info
));
658 info
->channel
= channel
;
659 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
662 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
663 struct snd_pcm_channel_info __user
* _info
)
665 struct snd_pcm_channel_info info
;
668 if (copy_from_user(&info
, _info
, sizeof(info
)))
670 res
= snd_pcm_channel_info(substream
, &info
);
673 if (copy_to_user(_info
, &info
, sizeof(info
)))
678 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
680 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
681 if (runtime
->trigger_master
== NULL
)
683 if (runtime
->trigger_master
== substream
) {
684 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
686 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
687 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
689 runtime
->trigger_master
= NULL
;
693 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
694 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
695 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
696 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
700 * this functions is core for handling of linked stream
701 * Note: the stream state might be changed also on failure
702 * Note2: call with calling stream lock + link lock
704 static int snd_pcm_action_group(struct action_ops
*ops
,
705 struct snd_pcm_substream
*substream
,
706 int state
, int do_lock
)
708 struct snd_pcm_substream
*s
= NULL
;
709 struct snd_pcm_substream
*s1
;
712 snd_pcm_group_for_each_entry(s
, substream
) {
713 if (do_lock
&& s
!= substream
)
714 spin_lock_nested(&s
->self_group
.lock
,
715 SINGLE_DEPTH_NESTING
);
716 res
= ops
->pre_action(s
, state
);
720 snd_pcm_group_for_each_entry(s
, substream
) {
721 res
= ops
->do_action(s
, state
);
723 if (ops
->undo_action
) {
724 snd_pcm_group_for_each_entry(s1
, substream
) {
725 if (s1
== s
) /* failed stream */
727 ops
->undo_action(s1
, state
);
730 s
= NULL
; /* unlock all */
734 snd_pcm_group_for_each_entry(s
, substream
) {
735 ops
->post_action(s
, state
);
740 snd_pcm_group_for_each_entry(s1
, substream
) {
742 spin_unlock(&s1
->self_group
.lock
);
743 if (s1
== s
) /* end */
751 * Note: call with stream lock
753 static int snd_pcm_action_single(struct action_ops
*ops
,
754 struct snd_pcm_substream
*substream
,
759 res
= ops
->pre_action(substream
, state
);
762 res
= ops
->do_action(substream
, state
);
764 ops
->post_action(substream
, state
);
765 else if (ops
->undo_action
)
766 ops
->undo_action(substream
, state
);
771 * Note: call with stream lock
773 static int snd_pcm_action(struct action_ops
*ops
,
774 struct snd_pcm_substream
*substream
,
779 if (snd_pcm_stream_linked(substream
)) {
780 if (!spin_trylock(&substream
->group
->lock
)) {
781 spin_unlock(&substream
->self_group
.lock
);
782 spin_lock(&substream
->group
->lock
);
783 spin_lock(&substream
->self_group
.lock
);
785 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
786 spin_unlock(&substream
->group
->lock
);
788 res
= snd_pcm_action_single(ops
, substream
, state
);
794 * Note: don't use any locks before
796 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
797 struct snd_pcm_substream
*substream
,
802 read_lock_irq(&snd_pcm_link_rwlock
);
803 if (snd_pcm_stream_linked(substream
)) {
804 spin_lock(&substream
->group
->lock
);
805 spin_lock(&substream
->self_group
.lock
);
806 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
807 spin_unlock(&substream
->self_group
.lock
);
808 spin_unlock(&substream
->group
->lock
);
810 spin_lock(&substream
->self_group
.lock
);
811 res
= snd_pcm_action_single(ops
, substream
, state
);
812 spin_unlock(&substream
->self_group
.lock
);
814 read_unlock_irq(&snd_pcm_link_rwlock
);
820 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
821 struct snd_pcm_substream
*substream
,
826 down_read(&snd_pcm_link_rwsem
);
827 if (snd_pcm_stream_linked(substream
))
828 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
830 res
= snd_pcm_action_single(ops
, substream
, state
);
831 up_read(&snd_pcm_link_rwsem
);
838 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
840 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
841 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
843 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
844 !snd_pcm_playback_data(substream
))
846 runtime
->trigger_master
= substream
;
850 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
852 if (substream
->runtime
->trigger_master
!= substream
)
854 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
857 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
859 if (substream
->runtime
->trigger_master
== substream
)
860 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
863 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
865 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
866 snd_pcm_trigger_tstamp(substream
);
867 runtime
->hw_ptr_jiffies
= jiffies
;
868 runtime
->status
->state
= state
;
869 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
870 runtime
->silence_size
> 0)
871 snd_pcm_playback_silence(substream
, ULONG_MAX
);
872 if (substream
->timer
)
873 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
874 &runtime
->trigger_tstamp
);
877 static struct action_ops snd_pcm_action_start
= {
878 .pre_action
= snd_pcm_pre_start
,
879 .do_action
= snd_pcm_do_start
,
880 .undo_action
= snd_pcm_undo_start
,
881 .post_action
= snd_pcm_post_start
885 * snd_pcm_start - start all linked streams
886 * @substream: the PCM substream instance
888 int snd_pcm_start(struct snd_pcm_substream
*substream
)
890 return snd_pcm_action(&snd_pcm_action_start
, substream
,
891 SNDRV_PCM_STATE_RUNNING
);
897 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
899 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
900 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
902 runtime
->trigger_master
= substream
;
906 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
908 if (substream
->runtime
->trigger_master
== substream
&&
909 snd_pcm_running(substream
))
910 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
911 return 0; /* unconditonally stop all substreams */
914 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
916 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
917 if (runtime
->status
->state
!= state
) {
918 snd_pcm_trigger_tstamp(substream
);
919 if (substream
->timer
)
920 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
921 &runtime
->trigger_tstamp
);
922 runtime
->status
->state
= state
;
924 wake_up(&runtime
->sleep
);
925 wake_up(&runtime
->tsleep
);
928 static struct action_ops snd_pcm_action_stop
= {
929 .pre_action
= snd_pcm_pre_stop
,
930 .do_action
= snd_pcm_do_stop
,
931 .post_action
= snd_pcm_post_stop
935 * snd_pcm_stop - try to stop all running streams in the substream group
936 * @substream: the PCM substream instance
937 * @state: PCM state after stopping the stream
939 * The state of each stream is then changed to the given state unconditionally.
941 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
943 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
946 EXPORT_SYMBOL(snd_pcm_stop
);
949 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
950 * @substream: the PCM substream
952 * After stopping, the state is changed to SETUP.
953 * Unlike snd_pcm_stop(), this affects only the given stream.
955 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
957 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
958 SNDRV_PCM_STATE_SETUP
);
964 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
966 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
967 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
970 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
972 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
974 runtime
->trigger_master
= substream
;
978 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
980 if (substream
->runtime
->trigger_master
!= substream
)
982 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
983 * a delta betwen the current jiffies, this gives a large enough
984 * delta, effectively to skip the check once.
986 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
987 return substream
->ops
->trigger(substream
,
988 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
989 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
992 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
994 if (substream
->runtime
->trigger_master
== substream
)
995 substream
->ops
->trigger(substream
,
996 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
997 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1000 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1002 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1003 snd_pcm_trigger_tstamp(substream
);
1005 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1006 if (substream
->timer
)
1007 snd_timer_notify(substream
->timer
,
1008 SNDRV_TIMER_EVENT_MPAUSE
,
1009 &runtime
->trigger_tstamp
);
1010 wake_up(&runtime
->sleep
);
1011 wake_up(&runtime
->tsleep
);
1013 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1014 if (substream
->timer
)
1015 snd_timer_notify(substream
->timer
,
1016 SNDRV_TIMER_EVENT_MCONTINUE
,
1017 &runtime
->trigger_tstamp
);
1021 static struct action_ops snd_pcm_action_pause
= {
1022 .pre_action
= snd_pcm_pre_pause
,
1023 .do_action
= snd_pcm_do_pause
,
1024 .undo_action
= snd_pcm_undo_pause
,
1025 .post_action
= snd_pcm_post_pause
1029 * Push/release the pause for all linked streams.
1031 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1033 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1039 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1041 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1042 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1044 runtime
->trigger_master
= substream
;
1048 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1050 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1051 if (runtime
->trigger_master
!= substream
)
1053 if (! snd_pcm_running(substream
))
1055 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1056 return 0; /* suspend unconditionally */
1059 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1061 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1062 snd_pcm_trigger_tstamp(substream
);
1063 if (substream
->timer
)
1064 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1065 &runtime
->trigger_tstamp
);
1066 runtime
->status
->suspended_state
= runtime
->status
->state
;
1067 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1068 wake_up(&runtime
->sleep
);
1069 wake_up(&runtime
->tsleep
);
1072 static struct action_ops snd_pcm_action_suspend
= {
1073 .pre_action
= snd_pcm_pre_suspend
,
1074 .do_action
= snd_pcm_do_suspend
,
1075 .post_action
= snd_pcm_post_suspend
1079 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1080 * @substream: the PCM substream
1082 * After this call, all streams are changed to SUSPENDED state.
1084 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1087 unsigned long flags
;
1092 snd_pcm_stream_lock_irqsave(substream
, flags
);
1093 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1094 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1098 EXPORT_SYMBOL(snd_pcm_suspend
);
1101 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1102 * @pcm: the PCM instance
1104 * After this call, all streams are changed to SUSPENDED state.
1106 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1108 struct snd_pcm_substream
*substream
;
1109 int stream
, err
= 0;
1114 for (stream
= 0; stream
< 2; stream
++) {
1115 for (substream
= pcm
->streams
[stream
].substream
;
1116 substream
; substream
= substream
->next
) {
1117 /* FIXME: the open/close code should lock this as well */
1118 if (substream
->runtime
== NULL
)
1120 err
= snd_pcm_suspend(substream
);
1121 if (err
< 0 && err
!= -EBUSY
)
1128 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1132 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1134 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1135 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1137 runtime
->trigger_master
= substream
;
1141 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1143 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1144 if (runtime
->trigger_master
!= substream
)
1146 /* DMA not running previously? */
1147 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1148 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1149 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1151 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1154 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1156 if (substream
->runtime
->trigger_master
== substream
&&
1157 snd_pcm_running(substream
))
1158 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1161 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1163 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1164 snd_pcm_trigger_tstamp(substream
);
1165 if (substream
->timer
)
1166 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1167 &runtime
->trigger_tstamp
);
1168 runtime
->status
->state
= runtime
->status
->suspended_state
;
1171 static struct action_ops snd_pcm_action_resume
= {
1172 .pre_action
= snd_pcm_pre_resume
,
1173 .do_action
= snd_pcm_do_resume
,
1174 .undo_action
= snd_pcm_undo_resume
,
1175 .post_action
= snd_pcm_post_resume
1178 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1180 struct snd_card
*card
= substream
->pcm
->card
;
1183 snd_power_lock(card
);
1184 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1185 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1186 snd_power_unlock(card
);
1192 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1197 #endif /* CONFIG_PM */
1202 * Change the RUNNING stream(s) to XRUN state.
1204 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1206 struct snd_card
*card
= substream
->pcm
->card
;
1207 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1210 snd_power_lock(card
);
1211 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1212 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1217 snd_pcm_stream_lock_irq(substream
);
1218 switch (runtime
->status
->state
) {
1219 case SNDRV_PCM_STATE_XRUN
:
1220 result
= 0; /* already there */
1222 case SNDRV_PCM_STATE_RUNNING
:
1223 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1228 snd_pcm_stream_unlock_irq(substream
);
1230 snd_power_unlock(card
);
1237 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1239 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1240 switch (runtime
->status
->state
) {
1241 case SNDRV_PCM_STATE_RUNNING
:
1242 case SNDRV_PCM_STATE_PREPARED
:
1243 case SNDRV_PCM_STATE_PAUSED
:
1244 case SNDRV_PCM_STATE_SUSPENDED
:
1251 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1253 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1254 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1257 runtime
->hw_ptr_base
= 0;
1258 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1259 runtime
->status
->hw_ptr
% runtime
->period_size
;
1260 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1261 runtime
->silence_filled
= 0;
1265 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1267 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1268 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1269 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1270 runtime
->silence_size
> 0)
1271 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1274 static struct action_ops snd_pcm_action_reset
= {
1275 .pre_action
= snd_pcm_pre_reset
,
1276 .do_action
= snd_pcm_do_reset
,
1277 .post_action
= snd_pcm_post_reset
1280 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1282 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1288 /* we use the second argument for updating f_flags */
1289 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1292 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1293 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1294 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1296 if (snd_pcm_running(substream
))
1298 substream
->f_flags
= f_flags
;
1302 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1305 err
= substream
->ops
->prepare(substream
);
1308 return snd_pcm_do_reset(substream
, 0);
1311 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1313 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1314 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1315 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1318 static struct action_ops snd_pcm_action_prepare
= {
1319 .pre_action
= snd_pcm_pre_prepare
,
1320 .do_action
= snd_pcm_do_prepare
,
1321 .post_action
= snd_pcm_post_prepare
1325 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1326 * @substream: the PCM substream instance
1327 * @file: file to refer f_flags
1329 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1333 struct snd_card
*card
= substream
->pcm
->card
;
1337 f_flags
= file
->f_flags
;
1339 f_flags
= substream
->f_flags
;
1341 snd_power_lock(card
);
1342 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1343 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1344 substream
, f_flags
);
1345 snd_power_unlock(card
);
1353 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1355 substream
->runtime
->trigger_master
= substream
;
1359 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1361 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1362 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1363 switch (runtime
->status
->state
) {
1364 case SNDRV_PCM_STATE_PREPARED
:
1365 /* start playback stream if possible */
1366 if (! snd_pcm_playback_empty(substream
)) {
1367 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1368 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1371 case SNDRV_PCM_STATE_RUNNING
:
1372 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1378 /* stop running stream */
1379 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1380 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1381 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1382 snd_pcm_do_stop(substream
, new_state
);
1383 snd_pcm_post_stop(substream
, new_state
);
1389 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1393 static struct action_ops snd_pcm_action_drain_init
= {
1394 .pre_action
= snd_pcm_pre_drain_init
,
1395 .do_action
= snd_pcm_do_drain_init
,
1396 .post_action
= snd_pcm_post_drain_init
1399 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1402 * Drain the stream(s).
1403 * When the substream is linked, sync until the draining of all playback streams
1405 * After this call, all streams are supposed to be either SETUP or DRAINING
1406 * (capture only) state.
1408 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1411 struct snd_card
*card
;
1412 struct snd_pcm_runtime
*runtime
;
1413 struct snd_pcm_substream
*s
;
1418 card
= substream
->pcm
->card
;
1419 runtime
= substream
->runtime
;
1421 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1424 snd_power_lock(card
);
1425 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1426 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1428 snd_power_unlock(card
);
1434 if (file
->f_flags
& O_NONBLOCK
)
1436 } else if (substream
->f_flags
& O_NONBLOCK
)
1439 down_read(&snd_pcm_link_rwsem
);
1440 snd_pcm_stream_lock_irq(substream
);
1442 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1443 snd_pcm_pause(substream
, 0);
1445 /* pre-start/stop - all running streams are changed to DRAINING state */
1446 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1449 /* in non-blocking, we don't wait in ioctl but let caller poll */
1457 struct snd_pcm_runtime
*to_check
;
1458 if (signal_pending(current
)) {
1459 result
= -ERESTARTSYS
;
1462 /* find a substream to drain */
1464 snd_pcm_group_for_each_entry(s
, substream
) {
1465 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1467 runtime
= s
->runtime
;
1468 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1474 break; /* all drained */
1475 init_waitqueue_entry(&wait
, current
);
1476 add_wait_queue(&to_check
->sleep
, &wait
);
1477 set_current_state(TASK_INTERRUPTIBLE
);
1478 snd_pcm_stream_unlock_irq(substream
);
1479 up_read(&snd_pcm_link_rwsem
);
1480 snd_power_unlock(card
);
1481 tout
= schedule_timeout(10 * HZ
);
1482 snd_power_lock(card
);
1483 down_read(&snd_pcm_link_rwsem
);
1484 snd_pcm_stream_lock_irq(substream
);
1485 remove_wait_queue(&to_check
->sleep
, &wait
);
1487 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1490 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1491 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1499 snd_pcm_stream_unlock_irq(substream
);
1500 up_read(&snd_pcm_link_rwsem
);
1501 snd_power_unlock(card
);
1509 * Immediately put all linked substreams into SETUP state.
1511 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1513 struct snd_pcm_runtime
*runtime
;
1514 struct snd_card
*card
;
1517 if (PCM_RUNTIME_CHECK(substream
))
1519 runtime
= substream
->runtime
;
1520 card
= substream
->pcm
->card
;
1522 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1523 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1524 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1527 snd_pcm_stream_lock_irq(substream
);
1529 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1530 snd_pcm_pause(substream
, 0);
1532 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1533 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1534 snd_pcm_stream_unlock_irq(substream
);
1540 /* WARNING: Don't forget to fput back the file */
1541 static struct file
*snd_pcm_file_fd(int fd
)
1544 struct inode
*inode
;
1550 inode
= file
->f_path
.dentry
->d_inode
;
1551 if (!S_ISCHR(inode
->i_mode
) ||
1552 imajor(inode
) != snd_major
) {
1556 minor
= iminor(inode
);
1557 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1558 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1568 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1572 struct snd_pcm_file
*pcm_file
;
1573 struct snd_pcm_substream
*substream1
;
1575 file
= snd_pcm_file_fd(fd
);
1578 pcm_file
= file
->private_data
;
1579 substream1
= pcm_file
->substream
;
1580 down_write(&snd_pcm_link_rwsem
);
1581 write_lock_irq(&snd_pcm_link_rwlock
);
1582 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1583 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1587 if (snd_pcm_stream_linked(substream1
)) {
1591 if (!snd_pcm_stream_linked(substream
)) {
1592 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1593 if (substream
->group
== NULL
) {
1597 spin_lock_init(&substream
->group
->lock
);
1598 INIT_LIST_HEAD(&substream
->group
->substreams
);
1599 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1600 substream
->group
->count
= 1;
1602 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1603 substream
->group
->count
++;
1604 substream1
->group
= substream
->group
;
1606 write_unlock_irq(&snd_pcm_link_rwlock
);
1607 up_write(&snd_pcm_link_rwsem
);
1612 static void relink_to_local(struct snd_pcm_substream
*substream
)
1614 substream
->group
= &substream
->self_group
;
1615 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1616 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1619 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1621 struct snd_pcm_substream
*s
;
1624 down_write(&snd_pcm_link_rwsem
);
1625 write_lock_irq(&snd_pcm_link_rwlock
);
1626 if (!snd_pcm_stream_linked(substream
)) {
1630 list_del(&substream
->link_list
);
1631 substream
->group
->count
--;
1632 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1633 snd_pcm_group_for_each_entry(s
, substream
) {
1637 kfree(substream
->group
);
1639 relink_to_local(substream
);
1641 write_unlock_irq(&snd_pcm_link_rwlock
);
1642 up_write(&snd_pcm_link_rwsem
);
1649 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1650 struct snd_pcm_hw_rule
*rule
)
1652 struct snd_interval t
;
1653 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1654 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1655 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1658 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1659 struct snd_pcm_hw_rule
*rule
)
1661 struct snd_interval t
;
1662 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1663 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1664 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1667 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1668 struct snd_pcm_hw_rule
*rule
)
1670 struct snd_interval t
;
1671 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1672 hw_param_interval_c(params
, rule
->deps
[1]),
1673 (unsigned long) rule
->private, &t
);
1674 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1677 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1678 struct snd_pcm_hw_rule
*rule
)
1680 struct snd_interval t
;
1681 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1682 (unsigned long) rule
->private,
1683 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1684 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1687 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1688 struct snd_pcm_hw_rule
*rule
)
1691 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1693 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1695 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1697 if (! snd_mask_test(mask
, k
))
1699 bits
= snd_pcm_format_physical_width(k
);
1701 continue; /* ignore invalid formats */
1702 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1703 snd_mask_reset(&m
, k
);
1705 return snd_mask_refine(mask
, &m
);
1708 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1709 struct snd_pcm_hw_rule
*rule
)
1711 struct snd_interval t
;
1717 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1719 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1721 bits
= snd_pcm_format_physical_width(k
);
1723 continue; /* ignore invalid formats */
1724 if (t
.min
> (unsigned)bits
)
1726 if (t
.max
< (unsigned)bits
)
1730 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1733 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1734 #error "Change this table"
1737 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1738 48000, 64000, 88200, 96000, 176400, 192000 };
1740 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1741 .count
= ARRAY_SIZE(rates
),
1745 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1746 struct snd_pcm_hw_rule
*rule
)
1748 struct snd_pcm_hardware
*hw
= rule
->private;
1749 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1750 snd_pcm_known_rates
.count
,
1751 snd_pcm_known_rates
.list
, hw
->rates
);
1754 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1755 struct snd_pcm_hw_rule
*rule
)
1757 struct snd_interval t
;
1758 struct snd_pcm_substream
*substream
= rule
->private;
1760 t
.max
= substream
->buffer_bytes_max
;
1764 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1767 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1769 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1770 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1773 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1774 snd_mask_any(constrs_mask(constrs
, k
));
1777 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1778 snd_interval_any(constrs_interval(constrs
, k
));
1781 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1782 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1783 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1784 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1785 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1787 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1788 snd_pcm_hw_rule_format
, NULL
,
1789 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1792 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1793 snd_pcm_hw_rule_sample_bits
, NULL
,
1794 SNDRV_PCM_HW_PARAM_FORMAT
,
1795 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1798 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1799 snd_pcm_hw_rule_div
, NULL
,
1800 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1803 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1804 snd_pcm_hw_rule_mul
, NULL
,
1805 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1808 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1809 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1810 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1813 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1814 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1815 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1818 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1819 snd_pcm_hw_rule_div
, NULL
,
1820 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1823 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1824 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1825 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1828 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1829 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1830 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1833 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1834 snd_pcm_hw_rule_div
, NULL
,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1838 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1839 snd_pcm_hw_rule_div
, NULL
,
1840 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1843 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1844 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1845 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1848 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1849 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1850 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1853 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1854 snd_pcm_hw_rule_mul
, NULL
,
1855 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1858 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1859 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1860 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1863 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1864 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1865 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1868 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1869 snd_pcm_hw_rule_muldivk
, (void*) 8,
1870 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1873 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1874 snd_pcm_hw_rule_muldivk
, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1878 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1879 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1883 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1884 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1891 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1893 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1894 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1896 unsigned int mask
= 0;
1898 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1899 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1900 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1901 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1902 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1903 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1904 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1905 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1906 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1907 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1908 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1910 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1914 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1918 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1922 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1923 hw
->channels_min
, hw
->channels_max
);
1927 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1928 hw
->rate_min
, hw
->rate_max
);
1932 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1933 hw
->period_bytes_min
, hw
->period_bytes_max
);
1937 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1938 hw
->periods_min
, hw
->periods_max
);
1942 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1943 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1947 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1948 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1949 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1954 if (runtime
->dma_bytes
) {
1955 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1960 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1961 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1962 snd_pcm_hw_rule_rate
, hw
,
1963 SNDRV_PCM_HW_PARAM_RATE
, -1);
1968 /* FIXME: this belong to lowlevel */
1969 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1974 static void pcm_release_private(struct snd_pcm_substream
*substream
)
1976 snd_pcm_unlink(substream
);
1979 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
1981 substream
->ref_count
--;
1982 if (substream
->ref_count
> 0)
1985 snd_pcm_drop(substream
);
1986 if (substream
->hw_opened
) {
1987 if (substream
->ops
->hw_free
!= NULL
)
1988 substream
->ops
->hw_free(substream
);
1989 substream
->ops
->close(substream
);
1990 substream
->hw_opened
= 0;
1992 if (substream
->pcm_release
) {
1993 substream
->pcm_release(substream
);
1994 substream
->pcm_release
= NULL
;
1996 snd_pcm_detach_substream(substream
);
1999 EXPORT_SYMBOL(snd_pcm_release_substream
);
2001 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2003 struct snd_pcm_substream
**rsubstream
)
2005 struct snd_pcm_substream
*substream
;
2008 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2011 if (substream
->ref_count
> 1) {
2012 *rsubstream
= substream
;
2016 err
= snd_pcm_hw_constraints_init(substream
);
2018 snd_printd("snd_pcm_hw_constraints_init failed\n");
2022 if ((err
= substream
->ops
->open(substream
)) < 0)
2025 substream
->hw_opened
= 1;
2027 err
= snd_pcm_hw_constraints_complete(substream
);
2029 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2033 *rsubstream
= substream
;
2037 snd_pcm_release_substream(substream
);
2041 EXPORT_SYMBOL(snd_pcm_open_substream
);
2043 static int snd_pcm_open_file(struct file
*file
,
2044 struct snd_pcm
*pcm
,
2046 struct snd_pcm_file
**rpcm_file
)
2048 struct snd_pcm_file
*pcm_file
;
2049 struct snd_pcm_substream
*substream
;
2050 struct snd_pcm_str
*str
;
2056 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2060 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2061 if (pcm_file
== NULL
) {
2062 snd_pcm_release_substream(substream
);
2065 pcm_file
->substream
= substream
;
2066 if (substream
->ref_count
== 1) {
2067 str
= substream
->pstr
;
2068 substream
->file
= pcm_file
;
2069 substream
->pcm_release
= pcm_release_private
;
2071 file
->private_data
= pcm_file
;
2073 *rpcm_file
= pcm_file
;
2077 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2079 struct snd_pcm
*pcm
;
2081 pcm
= snd_lookup_minor_data(iminor(inode
),
2082 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2083 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2086 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2088 struct snd_pcm
*pcm
;
2090 pcm
= snd_lookup_minor_data(iminor(inode
),
2091 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2092 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2095 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2098 struct snd_pcm_file
*pcm_file
;
2105 err
= snd_card_file_add(pcm
->card
, file
);
2108 if (!try_module_get(pcm
->card
->module
)) {
2112 init_waitqueue_entry(&wait
, current
);
2113 add_wait_queue(&pcm
->open_wait
, &wait
);
2114 mutex_lock(&pcm
->open_mutex
);
2116 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2119 if (err
== -EAGAIN
) {
2120 if (file
->f_flags
& O_NONBLOCK
) {
2126 set_current_state(TASK_INTERRUPTIBLE
);
2127 mutex_unlock(&pcm
->open_mutex
);
2129 mutex_lock(&pcm
->open_mutex
);
2130 if (signal_pending(current
)) {
2135 remove_wait_queue(&pcm
->open_wait
, &wait
);
2136 mutex_unlock(&pcm
->open_mutex
);
2142 module_put(pcm
->card
->module
);
2144 snd_card_file_remove(pcm
->card
, file
);
2149 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2151 struct snd_pcm
*pcm
;
2152 struct snd_pcm_substream
*substream
;
2153 struct snd_pcm_file
*pcm_file
;
2155 pcm_file
= file
->private_data
;
2156 substream
= pcm_file
->substream
;
2157 if (snd_BUG_ON(!substream
))
2159 pcm
= substream
->pcm
;
2160 mutex_lock(&pcm
->open_mutex
);
2161 snd_pcm_release_substream(substream
);
2163 mutex_unlock(&pcm
->open_mutex
);
2164 wake_up(&pcm
->open_wait
);
2165 module_put(pcm
->card
->module
);
2166 snd_card_file_remove(pcm
->card
, file
);
2170 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2171 snd_pcm_uframes_t frames
)
2173 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2174 snd_pcm_sframes_t appl_ptr
;
2175 snd_pcm_sframes_t ret
;
2176 snd_pcm_sframes_t hw_avail
;
2181 snd_pcm_stream_lock_irq(substream
);
2182 switch (runtime
->status
->state
) {
2183 case SNDRV_PCM_STATE_PREPARED
:
2185 case SNDRV_PCM_STATE_DRAINING
:
2186 case SNDRV_PCM_STATE_RUNNING
:
2187 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2190 case SNDRV_PCM_STATE_XRUN
:
2193 case SNDRV_PCM_STATE_SUSPENDED
:
2201 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2202 if (hw_avail
<= 0) {
2206 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2208 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2210 appl_ptr
+= runtime
->boundary
;
2211 runtime
->control
->appl_ptr
= appl_ptr
;
2214 snd_pcm_stream_unlock_irq(substream
);
2218 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2219 snd_pcm_uframes_t frames
)
2221 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2222 snd_pcm_sframes_t appl_ptr
;
2223 snd_pcm_sframes_t ret
;
2224 snd_pcm_sframes_t hw_avail
;
2229 snd_pcm_stream_lock_irq(substream
);
2230 switch (runtime
->status
->state
) {
2231 case SNDRV_PCM_STATE_PREPARED
:
2232 case SNDRV_PCM_STATE_DRAINING
:
2234 case SNDRV_PCM_STATE_RUNNING
:
2235 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2238 case SNDRV_PCM_STATE_XRUN
:
2241 case SNDRV_PCM_STATE_SUSPENDED
:
2249 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2250 if (hw_avail
<= 0) {
2254 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2256 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2258 appl_ptr
+= runtime
->boundary
;
2259 runtime
->control
->appl_ptr
= appl_ptr
;
2262 snd_pcm_stream_unlock_irq(substream
);
2266 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2267 snd_pcm_uframes_t frames
)
2269 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2270 snd_pcm_sframes_t appl_ptr
;
2271 snd_pcm_sframes_t ret
;
2272 snd_pcm_sframes_t avail
;
2277 snd_pcm_stream_lock_irq(substream
);
2278 switch (runtime
->status
->state
) {
2279 case SNDRV_PCM_STATE_PREPARED
:
2280 case SNDRV_PCM_STATE_PAUSED
:
2282 case SNDRV_PCM_STATE_DRAINING
:
2283 case SNDRV_PCM_STATE_RUNNING
:
2284 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2287 case SNDRV_PCM_STATE_XRUN
:
2290 case SNDRV_PCM_STATE_SUSPENDED
:
2298 avail
= snd_pcm_playback_avail(runtime
);
2303 if (frames
> (snd_pcm_uframes_t
)avail
)
2305 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2306 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2307 appl_ptr
-= runtime
->boundary
;
2308 runtime
->control
->appl_ptr
= appl_ptr
;
2311 snd_pcm_stream_unlock_irq(substream
);
2315 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2316 snd_pcm_uframes_t frames
)
2318 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2319 snd_pcm_sframes_t appl_ptr
;
2320 snd_pcm_sframes_t ret
;
2321 snd_pcm_sframes_t avail
;
2326 snd_pcm_stream_lock_irq(substream
);
2327 switch (runtime
->status
->state
) {
2328 case SNDRV_PCM_STATE_PREPARED
:
2329 case SNDRV_PCM_STATE_DRAINING
:
2330 case SNDRV_PCM_STATE_PAUSED
:
2332 case SNDRV_PCM_STATE_RUNNING
:
2333 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2336 case SNDRV_PCM_STATE_XRUN
:
2339 case SNDRV_PCM_STATE_SUSPENDED
:
2347 avail
= snd_pcm_capture_avail(runtime
);
2352 if (frames
> (snd_pcm_uframes_t
)avail
)
2354 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2355 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2356 appl_ptr
-= runtime
->boundary
;
2357 runtime
->control
->appl_ptr
= appl_ptr
;
2360 snd_pcm_stream_unlock_irq(substream
);
2364 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2366 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2369 snd_pcm_stream_lock_irq(substream
);
2370 switch (runtime
->status
->state
) {
2371 case SNDRV_PCM_STATE_DRAINING
:
2372 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2374 case SNDRV_PCM_STATE_RUNNING
:
2375 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2378 case SNDRV_PCM_STATE_PREPARED
:
2379 case SNDRV_PCM_STATE_SUSPENDED
:
2382 case SNDRV_PCM_STATE_XRUN
:
2390 snd_pcm_stream_unlock_irq(substream
);
2394 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2395 snd_pcm_sframes_t __user
*res
)
2397 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2399 snd_pcm_sframes_t n
= 0;
2401 snd_pcm_stream_lock_irq(substream
);
2402 switch (runtime
->status
->state
) {
2403 case SNDRV_PCM_STATE_DRAINING
:
2404 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2406 case SNDRV_PCM_STATE_RUNNING
:
2407 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2410 case SNDRV_PCM_STATE_PREPARED
:
2411 case SNDRV_PCM_STATE_SUSPENDED
:
2413 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2414 n
= snd_pcm_playback_hw_avail(runtime
);
2416 n
= snd_pcm_capture_avail(runtime
);
2417 n
+= runtime
->delay
;
2419 case SNDRV_PCM_STATE_XRUN
:
2427 snd_pcm_stream_unlock_irq(substream
);
2429 if (put_user(n
, res
))
2434 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2435 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2437 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2438 struct snd_pcm_sync_ptr sync_ptr
;
2439 volatile struct snd_pcm_mmap_status
*status
;
2440 volatile struct snd_pcm_mmap_control
*control
;
2443 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2444 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2446 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2448 status
= runtime
->status
;
2449 control
= runtime
->control
;
2450 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2451 err
= snd_pcm_hwsync(substream
);
2455 snd_pcm_stream_lock_irq(substream
);
2456 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2457 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2459 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2460 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2461 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2463 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2464 sync_ptr
.s
.status
.state
= status
->state
;
2465 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2466 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2467 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2468 snd_pcm_stream_unlock_irq(substream
);
2469 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2474 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2476 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2479 if (get_user(arg
, _arg
))
2481 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2483 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY
;
2484 if (arg
== SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
)
2485 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
;
2489 static int snd_pcm_common_ioctl1(struct file
*file
,
2490 struct snd_pcm_substream
*substream
,
2491 unsigned int cmd
, void __user
*arg
)
2494 case SNDRV_PCM_IOCTL_PVERSION
:
2495 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2496 case SNDRV_PCM_IOCTL_INFO
:
2497 return snd_pcm_info_user(substream
, arg
);
2498 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2500 case SNDRV_PCM_IOCTL_TTSTAMP
:
2501 return snd_pcm_tstamp(substream
, arg
);
2502 case SNDRV_PCM_IOCTL_HW_REFINE
:
2503 return snd_pcm_hw_refine_user(substream
, arg
);
2504 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2505 return snd_pcm_hw_params_user(substream
, arg
);
2506 case SNDRV_PCM_IOCTL_HW_FREE
:
2507 return snd_pcm_hw_free(substream
);
2508 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2509 return snd_pcm_sw_params_user(substream
, arg
);
2510 case SNDRV_PCM_IOCTL_STATUS
:
2511 return snd_pcm_status_user(substream
, arg
);
2512 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2513 return snd_pcm_channel_info_user(substream
, arg
);
2514 case SNDRV_PCM_IOCTL_PREPARE
:
2515 return snd_pcm_prepare(substream
, file
);
2516 case SNDRV_PCM_IOCTL_RESET
:
2517 return snd_pcm_reset(substream
);
2518 case SNDRV_PCM_IOCTL_START
:
2519 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2520 case SNDRV_PCM_IOCTL_LINK
:
2521 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2522 case SNDRV_PCM_IOCTL_UNLINK
:
2523 return snd_pcm_unlink(substream
);
2524 case SNDRV_PCM_IOCTL_RESUME
:
2525 return snd_pcm_resume(substream
);
2526 case SNDRV_PCM_IOCTL_XRUN
:
2527 return snd_pcm_xrun(substream
);
2528 case SNDRV_PCM_IOCTL_HWSYNC
:
2529 return snd_pcm_hwsync(substream
);
2530 case SNDRV_PCM_IOCTL_DELAY
:
2531 return snd_pcm_delay(substream
, arg
);
2532 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2533 return snd_pcm_sync_ptr(substream
, arg
);
2534 #ifdef CONFIG_SND_SUPPORT_OLD_API
2535 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2536 return snd_pcm_hw_refine_old_user(substream
, arg
);
2537 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2538 return snd_pcm_hw_params_old_user(substream
, arg
);
2540 case SNDRV_PCM_IOCTL_DRAIN
:
2541 return snd_pcm_drain(substream
, file
);
2542 case SNDRV_PCM_IOCTL_DROP
:
2543 return snd_pcm_drop(substream
);
2544 case SNDRV_PCM_IOCTL_PAUSE
:
2547 snd_pcm_stream_lock_irq(substream
);
2548 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2549 snd_pcm_stream_unlock_irq(substream
);
2553 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2557 static int snd_pcm_playback_ioctl1(struct file
*file
,
2558 struct snd_pcm_substream
*substream
,
2559 unsigned int cmd
, void __user
*arg
)
2561 if (snd_BUG_ON(!substream
))
2563 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2566 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2568 struct snd_xferi xferi
;
2569 struct snd_xferi __user
*_xferi
= arg
;
2570 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2571 snd_pcm_sframes_t result
;
2572 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2574 if (put_user(0, &_xferi
->result
))
2576 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2578 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2579 __put_user(result
, &_xferi
->result
);
2580 return result
< 0 ? result
: 0;
2582 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2584 struct snd_xfern xfern
;
2585 struct snd_xfern __user
*_xfern
= arg
;
2586 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2588 snd_pcm_sframes_t result
;
2589 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2591 if (runtime
->channels
> 128)
2593 if (put_user(0, &_xfern
->result
))
2595 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2598 bufs
= memdup_user(xfern
.bufs
,
2599 sizeof(void *) * runtime
->channels
);
2601 return PTR_ERR(bufs
);
2602 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2604 __put_user(result
, &_xfern
->result
);
2605 return result
< 0 ? result
: 0;
2607 case SNDRV_PCM_IOCTL_REWIND
:
2609 snd_pcm_uframes_t frames
;
2610 snd_pcm_uframes_t __user
*_frames
= arg
;
2611 snd_pcm_sframes_t result
;
2612 if (get_user(frames
, _frames
))
2614 if (put_user(0, _frames
))
2616 result
= snd_pcm_playback_rewind(substream
, frames
);
2617 __put_user(result
, _frames
);
2618 return result
< 0 ? result
: 0;
2620 case SNDRV_PCM_IOCTL_FORWARD
:
2622 snd_pcm_uframes_t frames
;
2623 snd_pcm_uframes_t __user
*_frames
= arg
;
2624 snd_pcm_sframes_t result
;
2625 if (get_user(frames
, _frames
))
2627 if (put_user(0, _frames
))
2629 result
= snd_pcm_playback_forward(substream
, frames
);
2630 __put_user(result
, _frames
);
2631 return result
< 0 ? result
: 0;
2634 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2637 static int snd_pcm_capture_ioctl1(struct file
*file
,
2638 struct snd_pcm_substream
*substream
,
2639 unsigned int cmd
, void __user
*arg
)
2641 if (snd_BUG_ON(!substream
))
2643 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2646 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2648 struct snd_xferi xferi
;
2649 struct snd_xferi __user
*_xferi
= arg
;
2650 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2651 snd_pcm_sframes_t result
;
2652 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2654 if (put_user(0, &_xferi
->result
))
2656 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2658 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2659 __put_user(result
, &_xferi
->result
);
2660 return result
< 0 ? result
: 0;
2662 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2664 struct snd_xfern xfern
;
2665 struct snd_xfern __user
*_xfern
= arg
;
2666 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2668 snd_pcm_sframes_t result
;
2669 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2671 if (runtime
->channels
> 128)
2673 if (put_user(0, &_xfern
->result
))
2675 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2678 bufs
= memdup_user(xfern
.bufs
,
2679 sizeof(void *) * runtime
->channels
);
2681 return PTR_ERR(bufs
);
2682 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2684 __put_user(result
, &_xfern
->result
);
2685 return result
< 0 ? result
: 0;
2687 case SNDRV_PCM_IOCTL_REWIND
:
2689 snd_pcm_uframes_t frames
;
2690 snd_pcm_uframes_t __user
*_frames
= arg
;
2691 snd_pcm_sframes_t result
;
2692 if (get_user(frames
, _frames
))
2694 if (put_user(0, _frames
))
2696 result
= snd_pcm_capture_rewind(substream
, frames
);
2697 __put_user(result
, _frames
);
2698 return result
< 0 ? result
: 0;
2700 case SNDRV_PCM_IOCTL_FORWARD
:
2702 snd_pcm_uframes_t frames
;
2703 snd_pcm_uframes_t __user
*_frames
= arg
;
2704 snd_pcm_sframes_t result
;
2705 if (get_user(frames
, _frames
))
2707 if (put_user(0, _frames
))
2709 result
= snd_pcm_capture_forward(substream
, frames
);
2710 __put_user(result
, _frames
);
2711 return result
< 0 ? result
: 0;
2714 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2717 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2720 struct snd_pcm_file
*pcm_file
;
2722 pcm_file
= file
->private_data
;
2724 if (((cmd
>> 8) & 0xff) != 'A')
2727 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2728 (void __user
*)arg
);
2731 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2734 struct snd_pcm_file
*pcm_file
;
2736 pcm_file
= file
->private_data
;
2738 if (((cmd
>> 8) & 0xff) != 'A')
2741 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2742 (void __user
*)arg
);
2745 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2746 unsigned int cmd
, void *arg
)
2751 fs
= snd_enter_user();
2752 switch (substream
->stream
) {
2753 case SNDRV_PCM_STREAM_PLAYBACK
:
2754 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2755 (void __user
*)arg
);
2757 case SNDRV_PCM_STREAM_CAPTURE
:
2758 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2759 (void __user
*)arg
);
2769 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2771 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2774 struct snd_pcm_file
*pcm_file
;
2775 struct snd_pcm_substream
*substream
;
2776 struct snd_pcm_runtime
*runtime
;
2777 snd_pcm_sframes_t result
;
2779 pcm_file
= file
->private_data
;
2780 substream
= pcm_file
->substream
;
2781 if (PCM_RUNTIME_CHECK(substream
))
2783 runtime
= substream
->runtime
;
2784 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2786 if (!frame_aligned(runtime
, count
))
2788 count
= bytes_to_frames(runtime
, count
);
2789 result
= snd_pcm_lib_read(substream
, buf
, count
);
2791 result
= frames_to_bytes(runtime
, result
);
2795 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2796 size_t count
, loff_t
* offset
)
2798 struct snd_pcm_file
*pcm_file
;
2799 struct snd_pcm_substream
*substream
;
2800 struct snd_pcm_runtime
*runtime
;
2801 snd_pcm_sframes_t result
;
2803 pcm_file
= file
->private_data
;
2804 substream
= pcm_file
->substream
;
2805 if (PCM_RUNTIME_CHECK(substream
))
2807 runtime
= substream
->runtime
;
2808 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2810 if (!frame_aligned(runtime
, count
))
2812 count
= bytes_to_frames(runtime
, count
);
2813 result
= snd_pcm_lib_write(substream
, buf
, count
);
2815 result
= frames_to_bytes(runtime
, result
);
2819 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2820 unsigned long nr_segs
, loff_t pos
)
2823 struct snd_pcm_file
*pcm_file
;
2824 struct snd_pcm_substream
*substream
;
2825 struct snd_pcm_runtime
*runtime
;
2826 snd_pcm_sframes_t result
;
2829 snd_pcm_uframes_t frames
;
2831 pcm_file
= iocb
->ki_filp
->private_data
;
2832 substream
= pcm_file
->substream
;
2833 if (PCM_RUNTIME_CHECK(substream
))
2835 runtime
= substream
->runtime
;
2836 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2838 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2840 if (!frame_aligned(runtime
, iov
->iov_len
))
2842 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2843 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2846 for (i
= 0; i
< nr_segs
; ++i
)
2847 bufs
[i
] = iov
[i
].iov_base
;
2848 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2850 result
= frames_to_bytes(runtime
, result
);
2855 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2856 unsigned long nr_segs
, loff_t pos
)
2858 struct snd_pcm_file
*pcm_file
;
2859 struct snd_pcm_substream
*substream
;
2860 struct snd_pcm_runtime
*runtime
;
2861 snd_pcm_sframes_t result
;
2864 snd_pcm_uframes_t frames
;
2866 pcm_file
= iocb
->ki_filp
->private_data
;
2867 substream
= pcm_file
->substream
;
2868 if (PCM_RUNTIME_CHECK(substream
))
2870 runtime
= substream
->runtime
;
2871 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2873 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2874 !frame_aligned(runtime
, iov
->iov_len
))
2876 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2877 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2880 for (i
= 0; i
< nr_segs
; ++i
)
2881 bufs
[i
] = iov
[i
].iov_base
;
2882 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2884 result
= frames_to_bytes(runtime
, result
);
2889 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2891 struct snd_pcm_file
*pcm_file
;
2892 struct snd_pcm_substream
*substream
;
2893 struct snd_pcm_runtime
*runtime
;
2895 snd_pcm_uframes_t avail
;
2897 pcm_file
= file
->private_data
;
2899 substream
= pcm_file
->substream
;
2900 if (PCM_RUNTIME_CHECK(substream
))
2902 runtime
= substream
->runtime
;
2904 poll_wait(file
, &runtime
->sleep
, wait
);
2906 snd_pcm_stream_lock_irq(substream
);
2907 avail
= snd_pcm_playback_avail(runtime
);
2908 switch (runtime
->status
->state
) {
2909 case SNDRV_PCM_STATE_RUNNING
:
2910 case SNDRV_PCM_STATE_PREPARED
:
2911 case SNDRV_PCM_STATE_PAUSED
:
2912 if (avail
>= runtime
->control
->avail_min
) {
2913 mask
= POLLOUT
| POLLWRNORM
;
2917 case SNDRV_PCM_STATE_DRAINING
:
2921 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2924 snd_pcm_stream_unlock_irq(substream
);
2928 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2930 struct snd_pcm_file
*pcm_file
;
2931 struct snd_pcm_substream
*substream
;
2932 struct snd_pcm_runtime
*runtime
;
2934 snd_pcm_uframes_t avail
;
2936 pcm_file
= file
->private_data
;
2938 substream
= pcm_file
->substream
;
2939 if (PCM_RUNTIME_CHECK(substream
))
2941 runtime
= substream
->runtime
;
2943 poll_wait(file
, &runtime
->sleep
, wait
);
2945 snd_pcm_stream_lock_irq(substream
);
2946 avail
= snd_pcm_capture_avail(runtime
);
2947 switch (runtime
->status
->state
) {
2948 case SNDRV_PCM_STATE_RUNNING
:
2949 case SNDRV_PCM_STATE_PREPARED
:
2950 case SNDRV_PCM_STATE_PAUSED
:
2951 if (avail
>= runtime
->control
->avail_min
) {
2952 mask
= POLLIN
| POLLRDNORM
;
2957 case SNDRV_PCM_STATE_DRAINING
:
2959 mask
= POLLIN
| POLLRDNORM
;
2964 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2967 snd_pcm_stream_unlock_irq(substream
);
2976 * Only on coherent architectures, we can mmap the status and the control records
2977 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2979 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2981 * mmap status record
2983 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
2984 struct vm_fault
*vmf
)
2986 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
2987 struct snd_pcm_runtime
*runtime
;
2989 if (substream
== NULL
)
2990 return VM_FAULT_SIGBUS
;
2991 runtime
= substream
->runtime
;
2992 vmf
->page
= virt_to_page(runtime
->status
);
2993 get_page(vmf
->page
);
2997 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
2999 .fault
= snd_pcm_mmap_status_fault
,
3002 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3003 struct vm_area_struct
*area
)
3005 struct snd_pcm_runtime
*runtime
;
3007 if (!(area
->vm_flags
& VM_READ
))
3009 runtime
= substream
->runtime
;
3010 size
= area
->vm_end
- area
->vm_start
;
3011 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3013 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3014 area
->vm_private_data
= substream
;
3015 area
->vm_flags
|= VM_RESERVED
;
3020 * mmap control record
3022 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3023 struct vm_fault
*vmf
)
3025 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3026 struct snd_pcm_runtime
*runtime
;
3028 if (substream
== NULL
)
3029 return VM_FAULT_SIGBUS
;
3030 runtime
= substream
->runtime
;
3031 vmf
->page
= virt_to_page(runtime
->control
);
3032 get_page(vmf
->page
);
3036 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3038 .fault
= snd_pcm_mmap_control_fault
,
3041 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3042 struct vm_area_struct
*area
)
3044 struct snd_pcm_runtime
*runtime
;
3046 if (!(area
->vm_flags
& VM_READ
))
3048 runtime
= substream
->runtime
;
3049 size
= area
->vm_end
- area
->vm_start
;
3050 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3052 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3053 area
->vm_private_data
= substream
;
3054 area
->vm_flags
|= VM_RESERVED
;
3057 #else /* ! coherent mmap */
3059 * don't support mmap for status and control records.
3061 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3062 struct vm_area_struct
*area
)
3066 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3067 struct vm_area_struct
*area
)
3071 #endif /* coherent mmap */
3073 static inline struct page
*
3074 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3076 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3077 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3078 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3079 return virt_to_page(CAC_ADDR(vaddr
));
3081 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3082 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
) {
3083 dma_addr_t addr
= substream
->runtime
->dma_addr
+ ofs
;
3084 addr
-= get_dma_offset(substream
->dma_buffer
.dev
.dev
);
3085 /* assume dma_handle set via pfn_to_phys() in
3086 * mm/dma-noncoherent.c
3088 return pfn_to_page(addr
>> PAGE_SHIFT
);
3091 return virt_to_page(vaddr
);
3095 * fault callback for mmapping a RAM page
3097 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3098 struct vm_fault
*vmf
)
3100 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3101 struct snd_pcm_runtime
*runtime
;
3102 unsigned long offset
;
3106 if (substream
== NULL
)
3107 return VM_FAULT_SIGBUS
;
3108 runtime
= substream
->runtime
;
3109 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3110 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3111 if (offset
> dma_bytes
- PAGE_SIZE
)
3112 return VM_FAULT_SIGBUS
;
3113 if (substream
->ops
->page
)
3114 page
= substream
->ops
->page(substream
, offset
);
3116 page
= snd_pcm_default_page_ops(substream
, offset
);
3118 return VM_FAULT_SIGBUS
;
3124 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3125 .open
= snd_pcm_mmap_data_open
,
3126 .close
= snd_pcm_mmap_data_close
,
3129 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3130 .open
= snd_pcm_mmap_data_open
,
3131 .close
= snd_pcm_mmap_data_close
,
3132 .fault
= snd_pcm_mmap_data_fault
,
3135 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3136 /* This should be defined / handled globally! */
3138 #define ARCH_HAS_DMA_MMAP_COHERENT
3143 * mmap the DMA buffer on RAM
3145 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3146 struct vm_area_struct
*area
)
3148 area
->vm_flags
|= VM_RESERVED
;
3149 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3150 if (!substream
->ops
->page
&&
3151 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3152 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3154 substream
->runtime
->dma_area
,
3155 substream
->runtime
->dma_addr
,
3156 area
->vm_end
- area
->vm_start
);
3157 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3158 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
&&
3159 !plat_device_is_coherent(substream
->dma_buffer
.dev
.dev
))
3160 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3161 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3162 /* mmap with fault handler */
3163 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3168 * mmap the DMA buffer on I/O memory area
3170 #if SNDRV_PCM_INFO_MMAP_IOMEM
3171 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3172 struct vm_area_struct
*area
)
3175 unsigned long offset
;
3177 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3178 area
->vm_flags
|= VM_IO
;
3179 size
= area
->vm_end
- area
->vm_start
;
3180 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3181 if (io_remap_pfn_range(area
, area
->vm_start
,
3182 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3183 size
, area
->vm_page_prot
))
3188 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3189 #endif /* SNDRV_PCM_INFO_MMAP */
3191 /* mmap callback with pgprot_noncached */
3192 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream
*substream
,
3193 struct vm_area_struct
*area
)
3195 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3196 return snd_pcm_default_mmap(substream
, area
);
3198 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached
);
3203 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3204 struct vm_area_struct
*area
)
3206 struct snd_pcm_runtime
*runtime
;
3208 unsigned long offset
;
3212 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3213 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3216 if (!(area
->vm_flags
& VM_READ
))
3219 runtime
= substream
->runtime
;
3220 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3222 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3224 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3225 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3227 size
= area
->vm_end
- area
->vm_start
;
3228 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3229 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3230 if ((size_t)size
> dma_bytes
)
3232 if (offset
> dma_bytes
- size
)
3235 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3236 area
->vm_private_data
= substream
;
3237 if (substream
->ops
->mmap
)
3238 err
= substream
->ops
->mmap(substream
, area
);
3240 err
= snd_pcm_default_mmap(substream
, area
);
3242 atomic_inc(&substream
->mmap_count
);
3246 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3248 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3250 struct snd_pcm_file
* pcm_file
;
3251 struct snd_pcm_substream
*substream
;
3252 unsigned long offset
;
3254 pcm_file
= file
->private_data
;
3255 substream
= pcm_file
->substream
;
3256 if (PCM_RUNTIME_CHECK(substream
))
3259 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3261 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3262 if (pcm_file
->no_compat_mmap
)
3264 return snd_pcm_mmap_status(substream
, file
, area
);
3265 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3266 if (pcm_file
->no_compat_mmap
)
3268 return snd_pcm_mmap_control(substream
, file
, area
);
3270 return snd_pcm_mmap_data(substream
, file
, area
);
3275 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3277 struct snd_pcm_file
* pcm_file
;
3278 struct snd_pcm_substream
*substream
;
3279 struct snd_pcm_runtime
*runtime
;
3283 pcm_file
= file
->private_data
;
3284 substream
= pcm_file
->substream
;
3285 if (PCM_RUNTIME_CHECK(substream
))
3287 runtime
= substream
->runtime
;
3288 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3297 #ifdef CONFIG_COMPAT
3298 #include "pcm_compat.c"
3300 #define snd_pcm_ioctl_compat NULL
3304 * To be removed helpers to keep binary compatibility
3307 #ifdef CONFIG_SND_SUPPORT_OLD_API
3308 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3309 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3311 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3312 struct snd_pcm_hw_params_old
*oparams
)
3316 memset(params
, 0, sizeof(*params
));
3317 params
->flags
= oparams
->flags
;
3318 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3319 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3320 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3321 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3322 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3323 params
->info
= oparams
->info
;
3324 params
->msbits
= oparams
->msbits
;
3325 params
->rate_num
= oparams
->rate_num
;
3326 params
->rate_den
= oparams
->rate_den
;
3327 params
->fifo_size
= oparams
->fifo_size
;
3330 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3331 struct snd_pcm_hw_params
*params
)
3335 memset(oparams
, 0, sizeof(*oparams
));
3336 oparams
->flags
= params
->flags
;
3337 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3338 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3339 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3340 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3341 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3342 oparams
->info
= params
->info
;
3343 oparams
->msbits
= params
->msbits
;
3344 oparams
->rate_num
= params
->rate_num
;
3345 oparams
->rate_den
= params
->rate_den
;
3346 oparams
->fifo_size
= params
->fifo_size
;
3349 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3350 struct snd_pcm_hw_params_old __user
* _oparams
)
3352 struct snd_pcm_hw_params
*params
;
3353 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3356 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3360 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3361 if (IS_ERR(oparams
)) {
3362 err
= PTR_ERR(oparams
);
3365 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3366 err
= snd_pcm_hw_refine(substream
, params
);
3367 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3368 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3379 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3380 struct snd_pcm_hw_params_old __user
* _oparams
)
3382 struct snd_pcm_hw_params
*params
;
3383 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3386 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3390 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3391 if (IS_ERR(oparams
)) {
3392 err
= PTR_ERR(oparams
);
3395 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3396 err
= snd_pcm_hw_params(substream
, params
);
3397 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3398 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3408 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3411 unsigned long dummy_get_unmapped_area(struct file
*file
, unsigned long addr
,
3412 unsigned long len
, unsigned long pgoff
,
3413 unsigned long flags
)
3418 # define dummy_get_unmapped_area NULL
3425 const struct file_operations snd_pcm_f_ops
[2] = {
3427 .owner
= THIS_MODULE
,
3428 .write
= snd_pcm_write
,
3429 .aio_write
= snd_pcm_aio_write
,
3430 .open
= snd_pcm_playback_open
,
3431 .release
= snd_pcm_release
,
3432 .poll
= snd_pcm_playback_poll
,
3433 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3434 .compat_ioctl
= snd_pcm_ioctl_compat
,
3435 .mmap
= snd_pcm_mmap
,
3436 .fasync
= snd_pcm_fasync
,
3437 .get_unmapped_area
= dummy_get_unmapped_area
,
3440 .owner
= THIS_MODULE
,
3441 .read
= snd_pcm_read
,
3442 .aio_read
= snd_pcm_aio_read
,
3443 .open
= snd_pcm_capture_open
,
3444 .release
= snd_pcm_release
,
3445 .poll
= snd_pcm_capture_poll
,
3446 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3447 .compat_ioctl
= snd_pcm_ioctl_compat
,
3448 .mmap
= snd_pcm_mmap
,
3449 .fasync
= snd_pcm_fasync
,
3450 .get_unmapped_area
= dummy_get_unmapped_area
,