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 <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
42 struct snd_pcm_hw_params_old
{
44 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
45 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
46 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
52 unsigned int rate_num
;
53 unsigned int rate_den
;
54 snd_pcm_uframes_t fifo_size
;
55 unsigned char reserved
[64];
58 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
63 struct snd_pcm_hw_params_old __user
* _oparams
);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
65 struct snd_pcm_hw_params_old __user
* _oparams
);
67 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
74 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
78 static inline mm_segment_t
snd_enter_user(void)
80 mm_segment_t fs
= get_fs();
85 static inline void snd_leave_user(mm_segment_t fs
)
92 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
94 struct snd_pcm_runtime
*runtime
;
95 struct snd_pcm
*pcm
= substream
->pcm
;
96 struct snd_pcm_str
*pstr
= substream
->pstr
;
98 memset(info
, 0, sizeof(*info
));
99 info
->card
= pcm
->card
->number
;
100 info
->device
= pcm
->device
;
101 info
->stream
= substream
->stream
;
102 info
->subdevice
= substream
->number
;
103 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
104 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
105 info
->dev_class
= pcm
->dev_class
;
106 info
->dev_subclass
= pcm
->dev_subclass
;
107 info
->subdevices_count
= pstr
->substream_count
;
108 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
109 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
110 runtime
= substream
->runtime
;
111 /* AB: FIXME!!! This is definitely nonsense */
113 info
->sync
= runtime
->sync
;
114 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
119 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
120 struct snd_pcm_info __user
* _info
)
122 struct snd_pcm_info
*info
;
125 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
128 err
= snd_pcm_info(substream
, info
);
130 if (copy_to_user(_info
, info
, sizeof(*info
)))
140 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141 char *snd_pcm_hw_param_names
[] = {
145 HW_PARAM(SAMPLE_BITS
),
146 HW_PARAM(FRAME_BITS
),
149 HW_PARAM(PERIOD_TIME
),
150 HW_PARAM(PERIOD_SIZE
),
151 HW_PARAM(PERIOD_BYTES
),
153 HW_PARAM(BUFFER_TIME
),
154 HW_PARAM(BUFFER_SIZE
),
155 HW_PARAM(BUFFER_BYTES
),
160 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
161 struct snd_pcm_hw_params
*params
)
164 struct snd_pcm_hardware
*hw
;
165 struct snd_interval
*i
= NULL
;
166 struct snd_mask
*m
= NULL
;
167 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
168 unsigned int rstamps
[constrs
->rules_num
];
169 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
170 unsigned int stamp
= 2;
174 params
->fifo_size
= 0;
175 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
177 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
178 params
->rate_num
= 0;
179 params
->rate_den
= 0;
182 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
183 m
= hw_param_mask(params
, k
);
184 if (snd_mask_empty(m
))
186 if (!(params
->rmask
& (1 << k
)))
189 printk("%s = ", snd_pcm_hw_param_names
[k
]);
190 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
192 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
194 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
197 params
->cmask
|= 1 << k
;
202 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
203 i
= hw_param_interval(params
, k
);
204 if (snd_interval_empty(i
))
206 if (!(params
->rmask
& (1 << k
)))
209 printk("%s = ", snd_pcm_hw_param_names
[k
]);
214 i
->openmin
? '(' : '[', i
->min
,
215 i
->max
, i
->openmax
? ')' : ']');
218 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
223 printk("%c%u %u%c\n",
224 i
->openmin
? '(' : '[', i
->min
,
225 i
->max
, i
->openmax
? ')' : ']');
228 params
->cmask
|= 1 << k
;
233 for (k
= 0; k
< constrs
->rules_num
; k
++)
235 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
236 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
239 for (k
= 0; k
< constrs
->rules_num
; k
++) {
240 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
243 if (r
->cond
&& !(r
->cond
& params
->flags
))
245 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
246 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
254 printk("Rule %d [%p]: ", k
, r
->func
);
256 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
257 if (hw_is_mask(r
->var
)) {
258 m
= hw_param_mask(params
, r
->var
);
259 printk("%x", *m
->bits
);
261 i
= hw_param_interval(params
, r
->var
);
266 i
->openmin
? '(' : '[', i
->min
,
267 i
->max
, i
->openmax
? ')' : ']');
271 changed
= r
->func(params
, r
);
275 if (hw_is_mask(r
->var
))
276 printk("%x", *m
->bits
);
282 i
->openmin
? '(' : '[', i
->min
,
283 i
->max
, i
->openmax
? ')' : ']');
289 if (changed
&& r
->var
>= 0) {
290 params
->cmask
|= (1 << r
->var
);
291 vstamps
[r
->var
] = stamp
;
299 if (!params
->msbits
) {
300 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
301 if (snd_interval_single(i
))
302 params
->msbits
= snd_interval_value(i
);
305 if (!params
->rate_den
) {
306 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
307 if (snd_interval_single(i
)) {
308 params
->rate_num
= snd_interval_value(i
);
309 params
->rate_den
= 1;
313 hw
= &substream
->runtime
->hw
;
315 params
->info
= hw
->info
;
316 if (!params
->fifo_size
)
317 params
->fifo_size
= hw
->fifo_size
;
322 EXPORT_SYMBOL(snd_pcm_hw_refine
);
324 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
325 struct snd_pcm_hw_params __user
* _params
)
327 struct snd_pcm_hw_params
*params
;
330 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
335 if (copy_from_user(params
, _params
, sizeof(*params
))) {
339 err
= snd_pcm_hw_refine(substream
, params
);
340 if (copy_to_user(_params
, params
, sizeof(*params
))) {
349 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
354 return -1; /* invalid */
356 /* take 75% of period time as the deadline */
357 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
358 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
364 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
365 struct snd_pcm_hw_params
*params
)
367 struct snd_pcm_runtime
*runtime
;
370 snd_pcm_uframes_t frames
;
372 if (PCM_RUNTIME_CHECK(substream
))
374 runtime
= substream
->runtime
;
375 snd_pcm_stream_lock_irq(substream
);
376 switch (runtime
->status
->state
) {
377 case SNDRV_PCM_STATE_OPEN
:
378 case SNDRV_PCM_STATE_SETUP
:
379 case SNDRV_PCM_STATE_PREPARED
:
382 snd_pcm_stream_unlock_irq(substream
);
385 snd_pcm_stream_unlock_irq(substream
);
386 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
387 if (!substream
->oss
.oss
)
389 if (atomic_read(&substream
->mmap_count
))
393 err
= snd_pcm_hw_refine(substream
, params
);
397 err
= snd_pcm_hw_params_choose(substream
, params
);
401 if (substream
->ops
->hw_params
!= NULL
) {
402 err
= substream
->ops
->hw_params(substream
, params
);
407 runtime
->access
= params_access(params
);
408 runtime
->format
= params_format(params
);
409 runtime
->subformat
= params_subformat(params
);
410 runtime
->channels
= params_channels(params
);
411 runtime
->rate
= params_rate(params
);
412 runtime
->period_size
= params_period_size(params
);
413 runtime
->periods
= params_periods(params
);
414 runtime
->buffer_size
= params_buffer_size(params
);
415 runtime
->info
= params
->info
;
416 runtime
->rate_num
= params
->rate_num
;
417 runtime
->rate_den
= params
->rate_den
;
419 bits
= snd_pcm_format_physical_width(runtime
->format
);
420 runtime
->sample_bits
= bits
;
421 bits
*= runtime
->channels
;
422 runtime
->frame_bits
= bits
;
424 while (bits
% 8 != 0) {
428 runtime
->byte_align
= bits
/ 8;
429 runtime
->min_align
= frames
;
431 /* Default sw params */
432 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
433 runtime
->period_step
= 1;
434 runtime
->control
->avail_min
= runtime
->period_size
;
435 runtime
->start_threshold
= 1;
436 runtime
->stop_threshold
= runtime
->buffer_size
;
437 runtime
->silence_threshold
= 0;
438 runtime
->silence_size
= 0;
439 runtime
->boundary
= runtime
->buffer_size
;
440 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
441 runtime
->boundary
*= 2;
443 snd_pcm_timer_resolution_change(substream
);
444 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
446 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
447 substream
->latency_id
);
448 if ((usecs
= period_to_usecs(runtime
)) >= 0)
449 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY
,
450 substream
->latency_id
, usecs
);
453 /* hardware might be unuseable from this time,
454 so we force application to retry to set
455 the correct hardware parameter settings */
456 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
457 if (substream
->ops
->hw_free
!= NULL
)
458 substream
->ops
->hw_free(substream
);
462 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
463 struct snd_pcm_hw_params __user
* _params
)
465 struct snd_pcm_hw_params
*params
;
468 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
473 if (copy_from_user(params
, _params
, sizeof(*params
))) {
477 err
= snd_pcm_hw_params(substream
, params
);
478 if (copy_to_user(_params
, params
, sizeof(*params
))) {
487 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
489 struct snd_pcm_runtime
*runtime
;
492 if (PCM_RUNTIME_CHECK(substream
))
494 runtime
= substream
->runtime
;
495 snd_pcm_stream_lock_irq(substream
);
496 switch (runtime
->status
->state
) {
497 case SNDRV_PCM_STATE_SETUP
:
498 case SNDRV_PCM_STATE_PREPARED
:
501 snd_pcm_stream_unlock_irq(substream
);
504 snd_pcm_stream_unlock_irq(substream
);
505 if (atomic_read(&substream
->mmap_count
))
507 if (substream
->ops
->hw_free
)
508 result
= substream
->ops
->hw_free(substream
);
509 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
510 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
511 substream
->latency_id
);
515 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
516 struct snd_pcm_sw_params
*params
)
518 struct snd_pcm_runtime
*runtime
;
520 if (PCM_RUNTIME_CHECK(substream
))
522 runtime
= substream
->runtime
;
523 snd_pcm_stream_lock_irq(substream
);
524 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
525 snd_pcm_stream_unlock_irq(substream
);
528 snd_pcm_stream_unlock_irq(substream
);
530 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
532 if (params
->avail_min
== 0)
534 if (params
->silence_size
>= runtime
->boundary
) {
535 if (params
->silence_threshold
!= 0)
538 if (params
->silence_size
> params
->silence_threshold
)
540 if (params
->silence_threshold
> runtime
->buffer_size
)
543 snd_pcm_stream_lock_irq(substream
);
544 runtime
->tstamp_mode
= params
->tstamp_mode
;
545 runtime
->period_step
= params
->period_step
;
546 runtime
->control
->avail_min
= params
->avail_min
;
547 runtime
->start_threshold
= params
->start_threshold
;
548 runtime
->stop_threshold
= params
->stop_threshold
;
549 runtime
->silence_threshold
= params
->silence_threshold
;
550 runtime
->silence_size
= params
->silence_size
;
551 params
->boundary
= runtime
->boundary
;
552 if (snd_pcm_running(substream
)) {
553 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
554 runtime
->silence_size
> 0)
555 snd_pcm_playback_silence(substream
, ULONG_MAX
);
556 wake_up(&runtime
->sleep
);
558 snd_pcm_stream_unlock_irq(substream
);
562 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
563 struct snd_pcm_sw_params __user
* _params
)
565 struct snd_pcm_sw_params params
;
567 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
569 err
= snd_pcm_sw_params(substream
, ¶ms
);
570 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
575 int snd_pcm_status(struct snd_pcm_substream
*substream
,
576 struct snd_pcm_status
*status
)
578 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
580 snd_pcm_stream_lock_irq(substream
);
581 status
->state
= runtime
->status
->state
;
582 status
->suspended_state
= runtime
->status
->suspended_state
;
583 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
585 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
586 if (snd_pcm_running(substream
)) {
587 snd_pcm_update_hw_ptr(substream
);
588 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
589 status
->tstamp
= runtime
->status
->tstamp
;
593 snd_pcm_gettime(runtime
, &status
->tstamp
);
595 status
->appl_ptr
= runtime
->control
->appl_ptr
;
596 status
->hw_ptr
= runtime
->status
->hw_ptr
;
597 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
598 status
->avail
= snd_pcm_playback_avail(runtime
);
599 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
600 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
601 status
->delay
= runtime
->buffer_size
- status
->avail
;
605 status
->avail
= snd_pcm_capture_avail(runtime
);
606 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
607 status
->delay
= status
->avail
;
611 status
->avail_max
= runtime
->avail_max
;
612 status
->overrange
= runtime
->overrange
;
613 runtime
->avail_max
= 0;
614 runtime
->overrange
= 0;
616 snd_pcm_stream_unlock_irq(substream
);
620 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
621 struct snd_pcm_status __user
* _status
)
623 struct snd_pcm_status status
;
626 memset(&status
, 0, sizeof(status
));
627 res
= snd_pcm_status(substream
, &status
);
630 if (copy_to_user(_status
, &status
, sizeof(status
)))
635 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
636 struct snd_pcm_channel_info
* info
)
638 struct snd_pcm_runtime
*runtime
;
639 unsigned int channel
;
641 channel
= info
->channel
;
642 runtime
= substream
->runtime
;
643 snd_pcm_stream_lock_irq(substream
);
644 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
645 snd_pcm_stream_unlock_irq(substream
);
648 snd_pcm_stream_unlock_irq(substream
);
649 if (channel
>= runtime
->channels
)
651 memset(info
, 0, sizeof(*info
));
652 info
->channel
= channel
;
653 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
656 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
657 struct snd_pcm_channel_info __user
* _info
)
659 struct snd_pcm_channel_info info
;
662 if (copy_from_user(&info
, _info
, sizeof(info
)))
664 res
= snd_pcm_channel_info(substream
, &info
);
667 if (copy_to_user(_info
, &info
, sizeof(info
)))
672 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
674 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
675 if (runtime
->trigger_master
== NULL
)
677 if (runtime
->trigger_master
== substream
) {
678 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
680 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
681 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
683 runtime
->trigger_master
= NULL
;
687 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
688 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
689 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
690 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
694 * this functions is core for handling of linked stream
695 * Note: the stream state might be changed also on failure
696 * Note2: call with calling stream lock + link lock
698 static int snd_pcm_action_group(struct action_ops
*ops
,
699 struct snd_pcm_substream
*substream
,
700 int state
, int do_lock
)
702 struct snd_pcm_substream
*s
= NULL
;
703 struct snd_pcm_substream
*s1
;
706 snd_pcm_group_for_each_entry(s
, substream
) {
707 if (do_lock
&& s
!= substream
)
708 spin_lock_nested(&s
->self_group
.lock
,
709 SINGLE_DEPTH_NESTING
);
710 res
= ops
->pre_action(s
, state
);
714 snd_pcm_group_for_each_entry(s
, substream
) {
715 res
= ops
->do_action(s
, state
);
717 if (ops
->undo_action
) {
718 snd_pcm_group_for_each_entry(s1
, substream
) {
719 if (s1
== s
) /* failed stream */
721 ops
->undo_action(s1
, state
);
724 s
= NULL
; /* unlock all */
728 snd_pcm_group_for_each_entry(s
, substream
) {
729 ops
->post_action(s
, state
);
734 snd_pcm_group_for_each_entry(s1
, substream
) {
736 spin_unlock(&s1
->self_group
.lock
);
737 if (s1
== s
) /* end */
745 * Note: call with stream lock
747 static int snd_pcm_action_single(struct action_ops
*ops
,
748 struct snd_pcm_substream
*substream
,
753 res
= ops
->pre_action(substream
, state
);
756 res
= ops
->do_action(substream
, state
);
758 ops
->post_action(substream
, state
);
759 else if (ops
->undo_action
)
760 ops
->undo_action(substream
, state
);
765 * Note: call with stream lock
767 static int snd_pcm_action(struct action_ops
*ops
,
768 struct snd_pcm_substream
*substream
,
773 if (snd_pcm_stream_linked(substream
)) {
774 if (!spin_trylock(&substream
->group
->lock
)) {
775 spin_unlock(&substream
->self_group
.lock
);
776 spin_lock(&substream
->group
->lock
);
777 spin_lock(&substream
->self_group
.lock
);
779 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
780 spin_unlock(&substream
->group
->lock
);
782 res
= snd_pcm_action_single(ops
, substream
, state
);
788 * Note: don't use any locks before
790 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
791 struct snd_pcm_substream
*substream
,
796 read_lock_irq(&snd_pcm_link_rwlock
);
797 if (snd_pcm_stream_linked(substream
)) {
798 spin_lock(&substream
->group
->lock
);
799 spin_lock(&substream
->self_group
.lock
);
800 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
801 spin_unlock(&substream
->self_group
.lock
);
802 spin_unlock(&substream
->group
->lock
);
804 spin_lock(&substream
->self_group
.lock
);
805 res
= snd_pcm_action_single(ops
, substream
, state
);
806 spin_unlock(&substream
->self_group
.lock
);
808 read_unlock_irq(&snd_pcm_link_rwlock
);
814 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
815 struct snd_pcm_substream
*substream
,
820 down_read(&snd_pcm_link_rwsem
);
821 if (snd_pcm_stream_linked(substream
))
822 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
824 res
= snd_pcm_action_single(ops
, substream
, state
);
825 up_read(&snd_pcm_link_rwsem
);
832 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
834 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
835 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
837 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
838 !snd_pcm_playback_data(substream
))
840 runtime
->trigger_master
= substream
;
844 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
846 if (substream
->runtime
->trigger_master
!= substream
)
848 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
851 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
853 if (substream
->runtime
->trigger_master
== substream
)
854 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
857 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
859 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
860 snd_pcm_trigger_tstamp(substream
);
861 runtime
->status
->state
= state
;
862 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
863 runtime
->silence_size
> 0)
864 snd_pcm_playback_silence(substream
, ULONG_MAX
);
865 if (substream
->timer
)
866 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
867 &runtime
->trigger_tstamp
);
870 static struct action_ops snd_pcm_action_start
= {
871 .pre_action
= snd_pcm_pre_start
,
872 .do_action
= snd_pcm_do_start
,
873 .undo_action
= snd_pcm_undo_start
,
874 .post_action
= snd_pcm_post_start
879 * @substream: the PCM substream instance
881 * Start all linked streams.
883 int snd_pcm_start(struct snd_pcm_substream
*substream
)
885 return snd_pcm_action(&snd_pcm_action_start
, substream
,
886 SNDRV_PCM_STATE_RUNNING
);
892 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
894 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
895 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
897 runtime
->trigger_master
= substream
;
901 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
903 if (substream
->runtime
->trigger_master
== substream
&&
904 snd_pcm_running(substream
))
905 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
906 return 0; /* unconditonally stop all substreams */
909 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
911 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
912 if (runtime
->status
->state
!= state
) {
913 snd_pcm_trigger_tstamp(substream
);
914 if (substream
->timer
)
915 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
916 &runtime
->trigger_tstamp
);
917 runtime
->status
->state
= state
;
919 wake_up(&runtime
->sleep
);
922 static struct action_ops snd_pcm_action_stop
= {
923 .pre_action
= snd_pcm_pre_stop
,
924 .do_action
= snd_pcm_do_stop
,
925 .post_action
= snd_pcm_post_stop
930 * @substream: the PCM substream instance
931 * @state: PCM state after stopping the stream
933 * Try to stop all running streams in the substream group.
934 * The state of each stream is changed to the given value after that unconditionally.
936 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
938 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
941 EXPORT_SYMBOL(snd_pcm_stop
);
945 * @substream: the PCM substream
947 * Stop the DMA only when the given stream is playback.
948 * The state is changed to SETUP.
949 * Unlike snd_pcm_stop(), this affects only the given stream.
951 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
953 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
954 SNDRV_PCM_STATE_SETUP
);
960 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
962 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
963 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
966 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
968 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
970 runtime
->trigger_master
= substream
;
974 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
976 if (substream
->runtime
->trigger_master
!= substream
)
978 return substream
->ops
->trigger(substream
,
979 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
980 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
983 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
985 if (substream
->runtime
->trigger_master
== substream
)
986 substream
->ops
->trigger(substream
,
987 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
988 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
991 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
993 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
994 snd_pcm_trigger_tstamp(substream
);
996 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
997 if (substream
->timer
)
998 snd_timer_notify(substream
->timer
,
999 SNDRV_TIMER_EVENT_MPAUSE
,
1000 &runtime
->trigger_tstamp
);
1001 wake_up(&runtime
->sleep
);
1003 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1004 if (substream
->timer
)
1005 snd_timer_notify(substream
->timer
,
1006 SNDRV_TIMER_EVENT_MCONTINUE
,
1007 &runtime
->trigger_tstamp
);
1011 static struct action_ops snd_pcm_action_pause
= {
1012 .pre_action
= snd_pcm_pre_pause
,
1013 .do_action
= snd_pcm_do_pause
,
1014 .undo_action
= snd_pcm_undo_pause
,
1015 .post_action
= snd_pcm_post_pause
1019 * Push/release the pause for all linked streams.
1021 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1023 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1029 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1031 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1032 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1034 runtime
->trigger_master
= substream
;
1038 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1040 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1041 if (runtime
->trigger_master
!= substream
)
1043 if (! snd_pcm_running(substream
))
1045 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1046 return 0; /* suspend unconditionally */
1049 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1051 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1052 snd_pcm_trigger_tstamp(substream
);
1053 if (substream
->timer
)
1054 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1055 &runtime
->trigger_tstamp
);
1056 runtime
->status
->suspended_state
= runtime
->status
->state
;
1057 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1058 wake_up(&runtime
->sleep
);
1061 static struct action_ops snd_pcm_action_suspend
= {
1062 .pre_action
= snd_pcm_pre_suspend
,
1063 .do_action
= snd_pcm_do_suspend
,
1064 .post_action
= snd_pcm_post_suspend
1069 * @substream: the PCM substream
1071 * Trigger SUSPEND to all linked streams.
1072 * After this call, all streams are changed to SUSPENDED state.
1074 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1077 unsigned long flags
;
1082 snd_pcm_stream_lock_irqsave(substream
, flags
);
1083 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1084 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1088 EXPORT_SYMBOL(snd_pcm_suspend
);
1091 * snd_pcm_suspend_all
1092 * @pcm: the PCM instance
1094 * Trigger SUSPEND to all substreams in the given pcm.
1095 * After this call, all streams are changed to SUSPENDED state.
1097 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1099 struct snd_pcm_substream
*substream
;
1100 int stream
, err
= 0;
1105 for (stream
= 0; stream
< 2; stream
++) {
1106 for (substream
= pcm
->streams
[stream
].substream
;
1107 substream
; substream
= substream
->next
) {
1108 /* FIXME: the open/close code should lock this as well */
1109 if (substream
->runtime
== NULL
)
1111 err
= snd_pcm_suspend(substream
);
1112 if (err
< 0 && err
!= -EBUSY
)
1119 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1123 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1125 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1126 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1128 runtime
->trigger_master
= substream
;
1132 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1134 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1135 if (runtime
->trigger_master
!= substream
)
1137 /* DMA not running previously? */
1138 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1139 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1140 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1142 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1145 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1147 if (substream
->runtime
->trigger_master
== substream
&&
1148 snd_pcm_running(substream
))
1149 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1152 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1154 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1155 snd_pcm_trigger_tstamp(substream
);
1156 if (substream
->timer
)
1157 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1158 &runtime
->trigger_tstamp
);
1159 runtime
->status
->state
= runtime
->status
->suspended_state
;
1162 static struct action_ops snd_pcm_action_resume
= {
1163 .pre_action
= snd_pcm_pre_resume
,
1164 .do_action
= snd_pcm_do_resume
,
1165 .undo_action
= snd_pcm_undo_resume
,
1166 .post_action
= snd_pcm_post_resume
1169 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1171 struct snd_card
*card
= substream
->pcm
->card
;
1174 snd_power_lock(card
);
1175 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1176 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1177 snd_power_unlock(card
);
1183 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1188 #endif /* CONFIG_PM */
1193 * Change the RUNNING stream(s) to XRUN state.
1195 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1197 struct snd_card
*card
= substream
->pcm
->card
;
1198 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1201 snd_power_lock(card
);
1202 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1203 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1208 snd_pcm_stream_lock_irq(substream
);
1209 switch (runtime
->status
->state
) {
1210 case SNDRV_PCM_STATE_XRUN
:
1211 result
= 0; /* already there */
1213 case SNDRV_PCM_STATE_RUNNING
:
1214 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1219 snd_pcm_stream_unlock_irq(substream
);
1221 snd_power_unlock(card
);
1228 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1230 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1231 switch (runtime
->status
->state
) {
1232 case SNDRV_PCM_STATE_RUNNING
:
1233 case SNDRV_PCM_STATE_PREPARED
:
1234 case SNDRV_PCM_STATE_PAUSED
:
1235 case SNDRV_PCM_STATE_SUSPENDED
:
1242 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1244 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1245 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1248 runtime
->hw_ptr_base
= 0;
1249 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1250 runtime
->status
->hw_ptr
% runtime
->period_size
;
1251 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1252 runtime
->silence_filled
= 0;
1256 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1258 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1259 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1260 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1261 runtime
->silence_size
> 0)
1262 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1265 static struct action_ops snd_pcm_action_reset
= {
1266 .pre_action
= snd_pcm_pre_reset
,
1267 .do_action
= snd_pcm_do_reset
,
1268 .post_action
= snd_pcm_post_reset
1271 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1273 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1279 /* we use the second argument for updating f_flags */
1280 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1283 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1284 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1285 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1287 if (snd_pcm_running(substream
))
1289 substream
->f_flags
= f_flags
;
1293 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1296 err
= substream
->ops
->prepare(substream
);
1299 return snd_pcm_do_reset(substream
, 0);
1302 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1304 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1305 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1306 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1309 static struct action_ops snd_pcm_action_prepare
= {
1310 .pre_action
= snd_pcm_pre_prepare
,
1311 .do_action
= snd_pcm_do_prepare
,
1312 .post_action
= snd_pcm_post_prepare
1317 * @substream: the PCM substream instance
1318 * @file: file to refer f_flags
1320 * Prepare the PCM substream to be triggerable.
1322 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1326 struct snd_card
*card
= substream
->pcm
->card
;
1330 f_flags
= file
->f_flags
;
1332 f_flags
= substream
->f_flags
;
1334 snd_power_lock(card
);
1335 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1336 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1337 substream
, f_flags
);
1338 snd_power_unlock(card
);
1346 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1348 if (substream
->f_flags
& O_NONBLOCK
)
1350 substream
->runtime
->trigger_master
= substream
;
1354 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1356 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1357 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1358 switch (runtime
->status
->state
) {
1359 case SNDRV_PCM_STATE_PREPARED
:
1360 /* start playback stream if possible */
1361 if (! snd_pcm_playback_empty(substream
)) {
1362 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1363 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1366 case SNDRV_PCM_STATE_RUNNING
:
1367 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1373 /* stop running stream */
1374 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1375 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1376 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1377 snd_pcm_do_stop(substream
, new_state
);
1378 snd_pcm_post_stop(substream
, new_state
);
1384 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1388 static struct action_ops snd_pcm_action_drain_init
= {
1389 .pre_action
= snd_pcm_pre_drain_init
,
1390 .do_action
= snd_pcm_do_drain_init
,
1391 .post_action
= snd_pcm_post_drain_init
1395 struct snd_pcm_substream
*substream
;
1397 snd_pcm_uframes_t stop_threshold
;
1400 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1403 * Drain the stream(s).
1404 * When the substream is linked, sync until the draining of all playback streams
1406 * After this call, all streams are supposed to be either SETUP or DRAINING
1407 * (capture only) state.
1409 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
;
1416 struct drain_rec
*drec
, drec_tmp
, *d
;
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
);
1433 /* allocate temporary record for drain sync */
1434 down_read(&snd_pcm_link_rwsem
);
1435 if (snd_pcm_stream_linked(substream
)) {
1436 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1438 up_read(&snd_pcm_link_rwsem
);
1439 snd_power_unlock(card
);
1445 /* count only playback streams */
1447 snd_pcm_group_for_each_entry(s
, substream
) {
1448 runtime
= s
->runtime
;
1449 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1450 d
= &drec
[num_drecs
++];
1452 init_waitqueue_entry(&d
->wait
, current
);
1453 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1454 /* stop_threshold fixup to avoid endless loop when
1455 * stop_threshold > buffer_size
1457 d
->stop_threshold
= runtime
->stop_threshold
;
1458 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1459 runtime
->stop_threshold
= runtime
->buffer_size
;
1462 up_read(&snd_pcm_link_rwsem
);
1464 snd_pcm_stream_lock_irq(substream
);
1466 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1467 snd_pcm_pause(substream
, 0);
1469 /* pre-start/stop - all running streams are changed to DRAINING state */
1470 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1472 snd_pcm_stream_unlock_irq(substream
);
1478 if (signal_pending(current
)) {
1479 result
= -ERESTARTSYS
;
1483 for (i
= 0; i
< num_drecs
; i
++) {
1484 runtime
= drec
[i
].substream
->runtime
;
1485 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1489 break; /* yes, all drained */
1491 set_current_state(TASK_INTERRUPTIBLE
);
1492 snd_pcm_stream_unlock_irq(substream
);
1493 snd_power_unlock(card
);
1494 tout
= schedule_timeout(10 * HZ
);
1495 snd_power_lock(card
);
1496 snd_pcm_stream_lock_irq(substream
);
1498 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1501 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1502 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1509 snd_pcm_stream_unlock_irq(substream
);
1512 for (i
= 0; i
< num_drecs
; i
++) {
1514 runtime
= d
->substream
->runtime
;
1515 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1516 runtime
->stop_threshold
= d
->stop_threshold
;
1519 if (drec
!= &drec_tmp
)
1521 snd_power_unlock(card
);
1529 * Immediately put all linked substreams into SETUP state.
1531 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1533 struct snd_pcm_runtime
*runtime
;
1534 struct snd_card
*card
;
1537 if (PCM_RUNTIME_CHECK(substream
))
1539 runtime
= substream
->runtime
;
1540 card
= substream
->pcm
->card
;
1542 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1543 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1544 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1547 snd_pcm_stream_lock_irq(substream
);
1549 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1550 snd_pcm_pause(substream
, 0);
1552 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1553 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1554 snd_pcm_stream_unlock_irq(substream
);
1560 /* WARNING: Don't forget to fput back the file */
1561 static struct file
*snd_pcm_file_fd(int fd
)
1564 struct inode
*inode
;
1570 inode
= file
->f_path
.dentry
->d_inode
;
1571 if (!S_ISCHR(inode
->i_mode
) ||
1572 imajor(inode
) != snd_major
) {
1576 minor
= iminor(inode
);
1577 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1578 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1588 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1592 struct snd_pcm_file
*pcm_file
;
1593 struct snd_pcm_substream
*substream1
;
1595 file
= snd_pcm_file_fd(fd
);
1598 pcm_file
= file
->private_data
;
1599 substream1
= pcm_file
->substream
;
1600 down_write(&snd_pcm_link_rwsem
);
1601 write_lock_irq(&snd_pcm_link_rwlock
);
1602 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1603 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1607 if (snd_pcm_stream_linked(substream1
)) {
1611 if (!snd_pcm_stream_linked(substream
)) {
1612 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1613 if (substream
->group
== NULL
) {
1617 spin_lock_init(&substream
->group
->lock
);
1618 INIT_LIST_HEAD(&substream
->group
->substreams
);
1619 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1620 substream
->group
->count
= 1;
1622 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1623 substream
->group
->count
++;
1624 substream1
->group
= substream
->group
;
1626 write_unlock_irq(&snd_pcm_link_rwlock
);
1627 up_write(&snd_pcm_link_rwsem
);
1632 static void relink_to_local(struct snd_pcm_substream
*substream
)
1634 substream
->group
= &substream
->self_group
;
1635 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1636 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1639 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1641 struct snd_pcm_substream
*s
;
1644 down_write(&snd_pcm_link_rwsem
);
1645 write_lock_irq(&snd_pcm_link_rwlock
);
1646 if (!snd_pcm_stream_linked(substream
)) {
1650 list_del(&substream
->link_list
);
1651 substream
->group
->count
--;
1652 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1653 snd_pcm_group_for_each_entry(s
, substream
) {
1657 kfree(substream
->group
);
1659 relink_to_local(substream
);
1661 write_unlock_irq(&snd_pcm_link_rwlock
);
1662 up_write(&snd_pcm_link_rwsem
);
1669 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1670 struct snd_pcm_hw_rule
*rule
)
1672 struct snd_interval t
;
1673 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1674 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1675 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1678 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1679 struct snd_pcm_hw_rule
*rule
)
1681 struct snd_interval t
;
1682 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
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_muldivk(struct snd_pcm_hw_params
*params
,
1688 struct snd_pcm_hw_rule
*rule
)
1690 struct snd_interval t
;
1691 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1692 hw_param_interval_c(params
, rule
->deps
[1]),
1693 (unsigned long) rule
->private, &t
);
1694 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1697 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1698 struct snd_pcm_hw_rule
*rule
)
1700 struct snd_interval t
;
1701 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1702 (unsigned long) rule
->private,
1703 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1704 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1707 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1708 struct snd_pcm_hw_rule
*rule
)
1711 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1713 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1715 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1717 if (! snd_mask_test(mask
, k
))
1719 bits
= snd_pcm_format_physical_width(k
);
1721 continue; /* ignore invalid formats */
1722 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1723 snd_mask_reset(&m
, k
);
1725 return snd_mask_refine(mask
, &m
);
1728 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1729 struct snd_pcm_hw_rule
*rule
)
1731 struct snd_interval t
;
1737 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1739 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1741 bits
= snd_pcm_format_physical_width(k
);
1743 continue; /* ignore invalid formats */
1744 if (t
.min
> (unsigned)bits
)
1746 if (t
.max
< (unsigned)bits
)
1750 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1753 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1754 #error "Change this table"
1757 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1758 48000, 64000, 88200, 96000, 176400, 192000 };
1760 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1761 .count
= ARRAY_SIZE(rates
),
1765 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1766 struct snd_pcm_hw_rule
*rule
)
1768 struct snd_pcm_hardware
*hw
= rule
->private;
1769 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1770 snd_pcm_known_rates
.count
,
1771 snd_pcm_known_rates
.list
, hw
->rates
);
1774 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1775 struct snd_pcm_hw_rule
*rule
)
1777 struct snd_interval t
;
1778 struct snd_pcm_substream
*substream
= rule
->private;
1780 t
.max
= substream
->buffer_bytes_max
;
1784 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1787 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1789 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1790 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1793 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1794 snd_mask_any(constrs_mask(constrs
, k
));
1797 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1798 snd_interval_any(constrs_interval(constrs
, k
));
1801 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1802 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1803 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1804 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1805 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1807 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1808 snd_pcm_hw_rule_format
, NULL
,
1809 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1812 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1813 snd_pcm_hw_rule_sample_bits
, NULL
,
1814 SNDRV_PCM_HW_PARAM_FORMAT
,
1815 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1818 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1819 snd_pcm_hw_rule_div
, NULL
,
1820 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1823 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1824 snd_pcm_hw_rule_mul
, NULL
,
1825 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1828 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1829 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1830 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1833 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1834 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1835 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1838 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1839 snd_pcm_hw_rule_div
, NULL
,
1840 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1843 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1844 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1845 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1848 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1849 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1850 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1853 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1854 snd_pcm_hw_rule_div
, NULL
,
1855 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1858 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1859 snd_pcm_hw_rule_div
, NULL
,
1860 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1863 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1864 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1868 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1869 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1870 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1873 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1874 snd_pcm_hw_rule_mul
, NULL
,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1878 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1879 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1880 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1883 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1884 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1888 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1889 snd_pcm_hw_rule_muldivk
, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1893 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1894 snd_pcm_hw_rule_muldivk
, (void*) 8,
1895 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1898 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1899 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1903 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1904 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1905 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1911 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1913 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1914 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1916 unsigned int mask
= 0;
1918 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1919 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1920 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1921 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1922 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1923 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1924 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1925 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1926 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1927 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1928 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1930 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1934 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1938 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1942 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1943 hw
->channels_min
, hw
->channels_max
);
1947 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1948 hw
->rate_min
, hw
->rate_max
);
1952 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1953 hw
->period_bytes_min
, hw
->period_bytes_max
);
1957 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1958 hw
->periods_min
, hw
->periods_max
);
1962 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1963 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1967 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1968 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1969 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1974 if (runtime
->dma_bytes
) {
1975 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1980 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1981 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1982 snd_pcm_hw_rule_rate
, hw
,
1983 SNDRV_PCM_HW_PARAM_RATE
, -1);
1988 /* FIXME: this belong to lowlevel */
1989 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1994 static void pcm_release_private(struct snd_pcm_substream
*substream
)
1996 snd_pcm_unlink(substream
);
1999 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2001 substream
->ref_count
--;
2002 if (substream
->ref_count
> 0)
2005 snd_pcm_drop(substream
);
2006 if (substream
->hw_opened
) {
2007 if (substream
->ops
->hw_free
!= NULL
)
2008 substream
->ops
->hw_free(substream
);
2009 substream
->ops
->close(substream
);
2010 substream
->hw_opened
= 0;
2012 if (substream
->pcm_release
) {
2013 substream
->pcm_release(substream
);
2014 substream
->pcm_release
= NULL
;
2016 snd_pcm_detach_substream(substream
);
2019 EXPORT_SYMBOL(snd_pcm_release_substream
);
2021 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2023 struct snd_pcm_substream
**rsubstream
)
2025 struct snd_pcm_substream
*substream
;
2028 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2031 if (substream
->ref_count
> 1) {
2032 *rsubstream
= substream
;
2036 err
= snd_pcm_hw_constraints_init(substream
);
2038 snd_printd("snd_pcm_hw_constraints_init failed\n");
2042 if ((err
= substream
->ops
->open(substream
)) < 0)
2045 substream
->hw_opened
= 1;
2047 err
= snd_pcm_hw_constraints_complete(substream
);
2049 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2053 *rsubstream
= substream
;
2057 snd_pcm_release_substream(substream
);
2061 EXPORT_SYMBOL(snd_pcm_open_substream
);
2063 static int snd_pcm_open_file(struct file
*file
,
2064 struct snd_pcm
*pcm
,
2066 struct snd_pcm_file
**rpcm_file
)
2068 struct snd_pcm_file
*pcm_file
;
2069 struct snd_pcm_substream
*substream
;
2070 struct snd_pcm_str
*str
;
2076 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2080 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2081 if (pcm_file
== NULL
) {
2082 snd_pcm_release_substream(substream
);
2085 pcm_file
->substream
= substream
;
2086 if (substream
->ref_count
== 1) {
2087 str
= substream
->pstr
;
2088 substream
->file
= pcm_file
;
2089 substream
->pcm_release
= pcm_release_private
;
2091 file
->private_data
= pcm_file
;
2093 *rpcm_file
= pcm_file
;
2097 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2099 struct snd_pcm
*pcm
;
2101 pcm
= snd_lookup_minor_data(iminor(inode
),
2102 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2103 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2106 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2108 struct snd_pcm
*pcm
;
2110 pcm
= snd_lookup_minor_data(iminor(inode
),
2111 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2112 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2115 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2118 struct snd_pcm_file
*pcm_file
;
2125 err
= snd_card_file_add(pcm
->card
, file
);
2128 if (!try_module_get(pcm
->card
->module
)) {
2132 init_waitqueue_entry(&wait
, current
);
2133 add_wait_queue(&pcm
->open_wait
, &wait
);
2134 mutex_lock(&pcm
->open_mutex
);
2136 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2139 if (err
== -EAGAIN
) {
2140 if (file
->f_flags
& O_NONBLOCK
) {
2146 set_current_state(TASK_INTERRUPTIBLE
);
2147 mutex_unlock(&pcm
->open_mutex
);
2149 mutex_lock(&pcm
->open_mutex
);
2150 if (signal_pending(current
)) {
2155 remove_wait_queue(&pcm
->open_wait
, &wait
);
2156 mutex_unlock(&pcm
->open_mutex
);
2162 module_put(pcm
->card
->module
);
2164 snd_card_file_remove(pcm
->card
, file
);
2169 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2171 struct snd_pcm
*pcm
;
2172 struct snd_pcm_substream
*substream
;
2173 struct snd_pcm_file
*pcm_file
;
2175 pcm_file
= file
->private_data
;
2176 substream
= pcm_file
->substream
;
2177 if (snd_BUG_ON(!substream
))
2179 pcm
= substream
->pcm
;
2180 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2181 mutex_lock(&pcm
->open_mutex
);
2182 snd_pcm_release_substream(substream
);
2184 mutex_unlock(&pcm
->open_mutex
);
2185 wake_up(&pcm
->open_wait
);
2186 module_put(pcm
->card
->module
);
2187 snd_card_file_remove(pcm
->card
, file
);
2191 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2192 snd_pcm_uframes_t frames
)
2194 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2195 snd_pcm_sframes_t appl_ptr
;
2196 snd_pcm_sframes_t ret
;
2197 snd_pcm_sframes_t hw_avail
;
2202 snd_pcm_stream_lock_irq(substream
);
2203 switch (runtime
->status
->state
) {
2204 case SNDRV_PCM_STATE_PREPARED
:
2206 case SNDRV_PCM_STATE_DRAINING
:
2207 case SNDRV_PCM_STATE_RUNNING
:
2208 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2211 case SNDRV_PCM_STATE_XRUN
:
2219 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2220 if (hw_avail
<= 0) {
2224 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2226 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2228 appl_ptr
+= runtime
->boundary
;
2229 runtime
->control
->appl_ptr
= appl_ptr
;
2232 snd_pcm_stream_unlock_irq(substream
);
2236 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2237 snd_pcm_uframes_t frames
)
2239 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2240 snd_pcm_sframes_t appl_ptr
;
2241 snd_pcm_sframes_t ret
;
2242 snd_pcm_sframes_t hw_avail
;
2247 snd_pcm_stream_lock_irq(substream
);
2248 switch (runtime
->status
->state
) {
2249 case SNDRV_PCM_STATE_PREPARED
:
2250 case SNDRV_PCM_STATE_DRAINING
:
2252 case SNDRV_PCM_STATE_RUNNING
:
2253 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2256 case SNDRV_PCM_STATE_XRUN
:
2264 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2265 if (hw_avail
<= 0) {
2269 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2271 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2273 appl_ptr
+= runtime
->boundary
;
2274 runtime
->control
->appl_ptr
= appl_ptr
;
2277 snd_pcm_stream_unlock_irq(substream
);
2281 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2282 snd_pcm_uframes_t frames
)
2284 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2285 snd_pcm_sframes_t appl_ptr
;
2286 snd_pcm_sframes_t ret
;
2287 snd_pcm_sframes_t avail
;
2292 snd_pcm_stream_lock_irq(substream
);
2293 switch (runtime
->status
->state
) {
2294 case SNDRV_PCM_STATE_PREPARED
:
2295 case SNDRV_PCM_STATE_PAUSED
:
2297 case SNDRV_PCM_STATE_DRAINING
:
2298 case SNDRV_PCM_STATE_RUNNING
:
2299 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2302 case SNDRV_PCM_STATE_XRUN
:
2310 avail
= snd_pcm_playback_avail(runtime
);
2315 if (frames
> (snd_pcm_uframes_t
)avail
)
2317 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2318 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2319 appl_ptr
-= runtime
->boundary
;
2320 runtime
->control
->appl_ptr
= appl_ptr
;
2323 snd_pcm_stream_unlock_irq(substream
);
2327 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2328 snd_pcm_uframes_t frames
)
2330 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2331 snd_pcm_sframes_t appl_ptr
;
2332 snd_pcm_sframes_t ret
;
2333 snd_pcm_sframes_t avail
;
2338 snd_pcm_stream_lock_irq(substream
);
2339 switch (runtime
->status
->state
) {
2340 case SNDRV_PCM_STATE_PREPARED
:
2341 case SNDRV_PCM_STATE_DRAINING
:
2342 case SNDRV_PCM_STATE_PAUSED
:
2344 case SNDRV_PCM_STATE_RUNNING
:
2345 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2348 case SNDRV_PCM_STATE_XRUN
:
2356 avail
= snd_pcm_capture_avail(runtime
);
2361 if (frames
> (snd_pcm_uframes_t
)avail
)
2363 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2364 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2365 appl_ptr
-= runtime
->boundary
;
2366 runtime
->control
->appl_ptr
= appl_ptr
;
2369 snd_pcm_stream_unlock_irq(substream
);
2373 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2375 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2378 snd_pcm_stream_lock_irq(substream
);
2379 switch (runtime
->status
->state
) {
2380 case SNDRV_PCM_STATE_DRAINING
:
2381 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2383 case SNDRV_PCM_STATE_RUNNING
:
2384 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2387 case SNDRV_PCM_STATE_PREPARED
:
2388 case SNDRV_PCM_STATE_SUSPENDED
:
2391 case SNDRV_PCM_STATE_XRUN
:
2399 snd_pcm_stream_unlock_irq(substream
);
2403 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2404 snd_pcm_sframes_t __user
*res
)
2406 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2408 snd_pcm_sframes_t n
= 0;
2410 snd_pcm_stream_lock_irq(substream
);
2411 switch (runtime
->status
->state
) {
2412 case SNDRV_PCM_STATE_DRAINING
:
2413 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2415 case SNDRV_PCM_STATE_RUNNING
:
2416 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2419 case SNDRV_PCM_STATE_PREPARED
:
2420 case SNDRV_PCM_STATE_SUSPENDED
:
2422 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2423 n
= snd_pcm_playback_hw_avail(runtime
);
2425 n
= snd_pcm_capture_avail(runtime
);
2427 case SNDRV_PCM_STATE_XRUN
:
2435 snd_pcm_stream_unlock_irq(substream
);
2437 if (put_user(n
, res
))
2442 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2443 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2445 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2446 struct snd_pcm_sync_ptr sync_ptr
;
2447 volatile struct snd_pcm_mmap_status
*status
;
2448 volatile struct snd_pcm_mmap_control
*control
;
2451 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2452 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2454 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2456 status
= runtime
->status
;
2457 control
= runtime
->control
;
2458 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2459 err
= snd_pcm_hwsync(substream
);
2463 snd_pcm_stream_lock_irq(substream
);
2464 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2465 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2467 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2468 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2469 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2471 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2472 sync_ptr
.s
.status
.state
= status
->state
;
2473 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2474 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2475 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2476 snd_pcm_stream_unlock_irq(substream
);
2477 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2482 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2484 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2487 if (get_user(arg
, _arg
))
2489 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2491 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY
;
2492 if (arg
== SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
)
2493 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
;
2497 static int snd_pcm_common_ioctl1(struct file
*file
,
2498 struct snd_pcm_substream
*substream
,
2499 unsigned int cmd
, void __user
*arg
)
2502 case SNDRV_PCM_IOCTL_PVERSION
:
2503 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2504 case SNDRV_PCM_IOCTL_INFO
:
2505 return snd_pcm_info_user(substream
, arg
);
2506 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2508 case SNDRV_PCM_IOCTL_TTSTAMP
:
2509 return snd_pcm_tstamp(substream
, arg
);
2510 case SNDRV_PCM_IOCTL_HW_REFINE
:
2511 return snd_pcm_hw_refine_user(substream
, arg
);
2512 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2513 return snd_pcm_hw_params_user(substream
, arg
);
2514 case SNDRV_PCM_IOCTL_HW_FREE
:
2515 return snd_pcm_hw_free(substream
);
2516 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2517 return snd_pcm_sw_params_user(substream
, arg
);
2518 case SNDRV_PCM_IOCTL_STATUS
:
2519 return snd_pcm_status_user(substream
, arg
);
2520 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2521 return snd_pcm_channel_info_user(substream
, arg
);
2522 case SNDRV_PCM_IOCTL_PREPARE
:
2523 return snd_pcm_prepare(substream
, file
);
2524 case SNDRV_PCM_IOCTL_RESET
:
2525 return snd_pcm_reset(substream
);
2526 case SNDRV_PCM_IOCTL_START
:
2527 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2528 case SNDRV_PCM_IOCTL_LINK
:
2529 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2530 case SNDRV_PCM_IOCTL_UNLINK
:
2531 return snd_pcm_unlink(substream
);
2532 case SNDRV_PCM_IOCTL_RESUME
:
2533 return snd_pcm_resume(substream
);
2534 case SNDRV_PCM_IOCTL_XRUN
:
2535 return snd_pcm_xrun(substream
);
2536 case SNDRV_PCM_IOCTL_HWSYNC
:
2537 return snd_pcm_hwsync(substream
);
2538 case SNDRV_PCM_IOCTL_DELAY
:
2539 return snd_pcm_delay(substream
, arg
);
2540 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2541 return snd_pcm_sync_ptr(substream
, arg
);
2542 #ifdef CONFIG_SND_SUPPORT_OLD_API
2543 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2544 return snd_pcm_hw_refine_old_user(substream
, arg
);
2545 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2546 return snd_pcm_hw_params_old_user(substream
, arg
);
2548 case SNDRV_PCM_IOCTL_DRAIN
:
2549 return snd_pcm_drain(substream
);
2550 case SNDRV_PCM_IOCTL_DROP
:
2551 return snd_pcm_drop(substream
);
2552 case SNDRV_PCM_IOCTL_PAUSE
:
2555 snd_pcm_stream_lock_irq(substream
);
2556 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2557 snd_pcm_stream_unlock_irq(substream
);
2561 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2565 static int snd_pcm_playback_ioctl1(struct file
*file
,
2566 struct snd_pcm_substream
*substream
,
2567 unsigned int cmd
, void __user
*arg
)
2569 if (snd_BUG_ON(!substream
))
2571 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2574 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2576 struct snd_xferi xferi
;
2577 struct snd_xferi __user
*_xferi
= arg
;
2578 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2579 snd_pcm_sframes_t result
;
2580 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2582 if (put_user(0, &_xferi
->result
))
2584 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2586 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2587 __put_user(result
, &_xferi
->result
);
2588 return result
< 0 ? result
: 0;
2590 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2592 struct snd_xfern xfern
;
2593 struct snd_xfern __user
*_xfern
= arg
;
2594 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2596 snd_pcm_sframes_t result
;
2597 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2599 if (runtime
->channels
> 128)
2601 if (put_user(0, &_xfern
->result
))
2603 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2605 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2608 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2612 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2614 __put_user(result
, &_xfern
->result
);
2615 return result
< 0 ? result
: 0;
2617 case SNDRV_PCM_IOCTL_REWIND
:
2619 snd_pcm_uframes_t frames
;
2620 snd_pcm_uframes_t __user
*_frames
= arg
;
2621 snd_pcm_sframes_t result
;
2622 if (get_user(frames
, _frames
))
2624 if (put_user(0, _frames
))
2626 result
= snd_pcm_playback_rewind(substream
, frames
);
2627 __put_user(result
, _frames
);
2628 return result
< 0 ? result
: 0;
2630 case SNDRV_PCM_IOCTL_FORWARD
:
2632 snd_pcm_uframes_t frames
;
2633 snd_pcm_uframes_t __user
*_frames
= arg
;
2634 snd_pcm_sframes_t result
;
2635 if (get_user(frames
, _frames
))
2637 if (put_user(0, _frames
))
2639 result
= snd_pcm_playback_forward(substream
, frames
);
2640 __put_user(result
, _frames
);
2641 return result
< 0 ? result
: 0;
2644 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2647 static int snd_pcm_capture_ioctl1(struct file
*file
,
2648 struct snd_pcm_substream
*substream
,
2649 unsigned int cmd
, void __user
*arg
)
2651 if (snd_BUG_ON(!substream
))
2653 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2656 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2658 struct snd_xferi xferi
;
2659 struct snd_xferi __user
*_xferi
= arg
;
2660 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2661 snd_pcm_sframes_t result
;
2662 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2664 if (put_user(0, &_xferi
->result
))
2666 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2668 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2669 __put_user(result
, &_xferi
->result
);
2670 return result
< 0 ? result
: 0;
2672 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2674 struct snd_xfern xfern
;
2675 struct snd_xfern __user
*_xfern
= arg
;
2676 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2678 snd_pcm_sframes_t result
;
2679 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2681 if (runtime
->channels
> 128)
2683 if (put_user(0, &_xfern
->result
))
2685 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2687 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2690 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2694 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2696 __put_user(result
, &_xfern
->result
);
2697 return result
< 0 ? result
: 0;
2699 case SNDRV_PCM_IOCTL_REWIND
:
2701 snd_pcm_uframes_t frames
;
2702 snd_pcm_uframes_t __user
*_frames
= arg
;
2703 snd_pcm_sframes_t result
;
2704 if (get_user(frames
, _frames
))
2706 if (put_user(0, _frames
))
2708 result
= snd_pcm_capture_rewind(substream
, frames
);
2709 __put_user(result
, _frames
);
2710 return result
< 0 ? result
: 0;
2712 case SNDRV_PCM_IOCTL_FORWARD
:
2714 snd_pcm_uframes_t frames
;
2715 snd_pcm_uframes_t __user
*_frames
= arg
;
2716 snd_pcm_sframes_t result
;
2717 if (get_user(frames
, _frames
))
2719 if (put_user(0, _frames
))
2721 result
= snd_pcm_capture_forward(substream
, frames
);
2722 __put_user(result
, _frames
);
2723 return result
< 0 ? result
: 0;
2726 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2729 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2732 struct snd_pcm_file
*pcm_file
;
2734 pcm_file
= file
->private_data
;
2736 if (((cmd
>> 8) & 0xff) != 'A')
2739 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2740 (void __user
*)arg
);
2743 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2746 struct snd_pcm_file
*pcm_file
;
2748 pcm_file
= file
->private_data
;
2750 if (((cmd
>> 8) & 0xff) != 'A')
2753 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2754 (void __user
*)arg
);
2757 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2758 unsigned int cmd
, void *arg
)
2763 fs
= snd_enter_user();
2764 switch (substream
->stream
) {
2765 case SNDRV_PCM_STREAM_PLAYBACK
:
2766 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2767 (void __user
*)arg
);
2769 case SNDRV_PCM_STREAM_CAPTURE
:
2770 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2771 (void __user
*)arg
);
2781 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2783 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2786 struct snd_pcm_file
*pcm_file
;
2787 struct snd_pcm_substream
*substream
;
2788 struct snd_pcm_runtime
*runtime
;
2789 snd_pcm_sframes_t result
;
2791 pcm_file
= file
->private_data
;
2792 substream
= pcm_file
->substream
;
2793 if (PCM_RUNTIME_CHECK(substream
))
2795 runtime
= substream
->runtime
;
2796 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2798 if (!frame_aligned(runtime
, count
))
2800 count
= bytes_to_frames(runtime
, count
);
2801 result
= snd_pcm_lib_read(substream
, buf
, count
);
2803 result
= frames_to_bytes(runtime
, result
);
2807 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2808 size_t count
, loff_t
* offset
)
2810 struct snd_pcm_file
*pcm_file
;
2811 struct snd_pcm_substream
*substream
;
2812 struct snd_pcm_runtime
*runtime
;
2813 snd_pcm_sframes_t result
;
2815 pcm_file
= file
->private_data
;
2816 substream
= pcm_file
->substream
;
2817 if (PCM_RUNTIME_CHECK(substream
))
2819 runtime
= substream
->runtime
;
2820 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2822 if (!frame_aligned(runtime
, count
))
2824 count
= bytes_to_frames(runtime
, count
);
2825 result
= snd_pcm_lib_write(substream
, buf
, count
);
2827 result
= frames_to_bytes(runtime
, result
);
2831 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2832 unsigned long nr_segs
, loff_t pos
)
2835 struct snd_pcm_file
*pcm_file
;
2836 struct snd_pcm_substream
*substream
;
2837 struct snd_pcm_runtime
*runtime
;
2838 snd_pcm_sframes_t result
;
2841 snd_pcm_uframes_t frames
;
2843 pcm_file
= iocb
->ki_filp
->private_data
;
2844 substream
= pcm_file
->substream
;
2845 if (PCM_RUNTIME_CHECK(substream
))
2847 runtime
= substream
->runtime
;
2848 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2850 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2852 if (!frame_aligned(runtime
, iov
->iov_len
))
2854 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2855 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2858 for (i
= 0; i
< nr_segs
; ++i
)
2859 bufs
[i
] = iov
[i
].iov_base
;
2860 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2862 result
= frames_to_bytes(runtime
, result
);
2867 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2868 unsigned long nr_segs
, loff_t pos
)
2870 struct snd_pcm_file
*pcm_file
;
2871 struct snd_pcm_substream
*substream
;
2872 struct snd_pcm_runtime
*runtime
;
2873 snd_pcm_sframes_t result
;
2876 snd_pcm_uframes_t frames
;
2878 pcm_file
= iocb
->ki_filp
->private_data
;
2879 substream
= pcm_file
->substream
;
2880 if (PCM_RUNTIME_CHECK(substream
))
2882 runtime
= substream
->runtime
;
2883 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2885 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2886 !frame_aligned(runtime
, iov
->iov_len
))
2888 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2889 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2892 for (i
= 0; i
< nr_segs
; ++i
)
2893 bufs
[i
] = iov
[i
].iov_base
;
2894 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2896 result
= frames_to_bytes(runtime
, result
);
2901 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2903 struct snd_pcm_file
*pcm_file
;
2904 struct snd_pcm_substream
*substream
;
2905 struct snd_pcm_runtime
*runtime
;
2907 snd_pcm_uframes_t avail
;
2909 pcm_file
= file
->private_data
;
2911 substream
= pcm_file
->substream
;
2912 if (PCM_RUNTIME_CHECK(substream
))
2914 runtime
= substream
->runtime
;
2916 poll_wait(file
, &runtime
->sleep
, wait
);
2918 snd_pcm_stream_lock_irq(substream
);
2919 avail
= snd_pcm_playback_avail(runtime
);
2920 switch (runtime
->status
->state
) {
2921 case SNDRV_PCM_STATE_RUNNING
:
2922 case SNDRV_PCM_STATE_PREPARED
:
2923 case SNDRV_PCM_STATE_PAUSED
:
2924 if (avail
>= runtime
->control
->avail_min
) {
2925 mask
= POLLOUT
| POLLWRNORM
;
2929 case SNDRV_PCM_STATE_DRAINING
:
2933 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2936 snd_pcm_stream_unlock_irq(substream
);
2940 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2942 struct snd_pcm_file
*pcm_file
;
2943 struct snd_pcm_substream
*substream
;
2944 struct snd_pcm_runtime
*runtime
;
2946 snd_pcm_uframes_t avail
;
2948 pcm_file
= file
->private_data
;
2950 substream
= pcm_file
->substream
;
2951 if (PCM_RUNTIME_CHECK(substream
))
2953 runtime
= substream
->runtime
;
2955 poll_wait(file
, &runtime
->sleep
, wait
);
2957 snd_pcm_stream_lock_irq(substream
);
2958 avail
= snd_pcm_capture_avail(runtime
);
2959 switch (runtime
->status
->state
) {
2960 case SNDRV_PCM_STATE_RUNNING
:
2961 case SNDRV_PCM_STATE_PREPARED
:
2962 case SNDRV_PCM_STATE_PAUSED
:
2963 if (avail
>= runtime
->control
->avail_min
) {
2964 mask
= POLLIN
| POLLRDNORM
;
2969 case SNDRV_PCM_STATE_DRAINING
:
2971 mask
= POLLIN
| POLLRDNORM
;
2976 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2979 snd_pcm_stream_unlock_irq(substream
);
2988 * Only on coherent architectures, we can mmap the status and the control records
2989 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2991 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2993 * mmap status record
2995 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
2996 struct vm_fault
*vmf
)
2998 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
2999 struct snd_pcm_runtime
*runtime
;
3001 if (substream
== NULL
)
3002 return VM_FAULT_SIGBUS
;
3003 runtime
= substream
->runtime
;
3004 vmf
->page
= virt_to_page(runtime
->status
);
3005 get_page(vmf
->page
);
3009 static struct vm_operations_struct snd_pcm_vm_ops_status
=
3011 .fault
= snd_pcm_mmap_status_fault
,
3014 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3015 struct vm_area_struct
*area
)
3017 struct snd_pcm_runtime
*runtime
;
3019 if (!(area
->vm_flags
& VM_READ
))
3021 runtime
= substream
->runtime
;
3022 size
= area
->vm_end
- area
->vm_start
;
3023 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3025 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3026 area
->vm_private_data
= substream
;
3027 area
->vm_flags
|= VM_RESERVED
;
3032 * mmap control record
3034 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3035 struct vm_fault
*vmf
)
3037 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3038 struct snd_pcm_runtime
*runtime
;
3040 if (substream
== NULL
)
3041 return VM_FAULT_SIGBUS
;
3042 runtime
= substream
->runtime
;
3043 vmf
->page
= virt_to_page(runtime
->control
);
3044 get_page(vmf
->page
);
3048 static struct vm_operations_struct snd_pcm_vm_ops_control
=
3050 .fault
= snd_pcm_mmap_control_fault
,
3053 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3054 struct vm_area_struct
*area
)
3056 struct snd_pcm_runtime
*runtime
;
3058 if (!(area
->vm_flags
& VM_READ
))
3060 runtime
= substream
->runtime
;
3061 size
= area
->vm_end
- area
->vm_start
;
3062 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3064 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3065 area
->vm_private_data
= substream
;
3066 area
->vm_flags
|= VM_RESERVED
;
3069 #else /* ! coherent mmap */
3071 * don't support mmap for status and control records.
3073 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3074 struct vm_area_struct
*area
)
3078 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3079 struct vm_area_struct
*area
)
3083 #endif /* coherent mmap */
3086 * fault callback for mmapping a RAM page
3088 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3089 struct vm_fault
*vmf
)
3091 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3092 struct snd_pcm_runtime
*runtime
;
3093 unsigned long offset
;
3098 if (substream
== NULL
)
3099 return VM_FAULT_SIGBUS
;
3100 runtime
= substream
->runtime
;
3101 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3102 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3103 if (offset
> dma_bytes
- PAGE_SIZE
)
3104 return VM_FAULT_SIGBUS
;
3105 if (substream
->ops
->page
) {
3106 page
= substream
->ops
->page(substream
, offset
);
3108 return VM_FAULT_SIGBUS
;
3110 vaddr
= runtime
->dma_area
+ offset
;
3111 page
= virt_to_page(vaddr
);
3118 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3120 .open
= snd_pcm_mmap_data_open
,
3121 .close
= snd_pcm_mmap_data_close
,
3122 .fault
= snd_pcm_mmap_data_fault
,
3126 * mmap the DMA buffer on RAM
3128 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3129 struct vm_area_struct
*area
)
3131 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3132 area
->vm_private_data
= substream
;
3133 area
->vm_flags
|= VM_RESERVED
;
3134 atomic_inc(&substream
->mmap_count
);
3139 * mmap the DMA buffer on I/O memory area
3141 #if SNDRV_PCM_INFO_MMAP_IOMEM
3142 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3144 .open
= snd_pcm_mmap_data_open
,
3145 .close
= snd_pcm_mmap_data_close
,
3148 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3149 struct vm_area_struct
*area
)
3152 unsigned long offset
;
3154 #ifdef pgprot_noncached
3155 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3157 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3158 area
->vm_private_data
= substream
;
3159 area
->vm_flags
|= VM_IO
;
3160 size
= area
->vm_end
- area
->vm_start
;
3161 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3162 if (io_remap_pfn_range(area
, area
->vm_start
,
3163 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3164 size
, area
->vm_page_prot
))
3166 atomic_inc(&substream
->mmap_count
);
3170 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3171 #endif /* SNDRV_PCM_INFO_MMAP */
3176 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3177 struct vm_area_struct
*area
)
3179 struct snd_pcm_runtime
*runtime
;
3181 unsigned long offset
;
3184 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3185 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3188 if (!(area
->vm_flags
& VM_READ
))
3191 runtime
= substream
->runtime
;
3192 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3194 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3196 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3197 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3199 size
= area
->vm_end
- area
->vm_start
;
3200 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3201 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3202 if ((size_t)size
> dma_bytes
)
3204 if (offset
> dma_bytes
- size
)
3207 if (substream
->ops
->mmap
)
3208 return substream
->ops
->mmap(substream
, area
);
3210 return snd_pcm_default_mmap(substream
, area
);
3213 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3215 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3217 struct snd_pcm_file
* pcm_file
;
3218 struct snd_pcm_substream
*substream
;
3219 unsigned long offset
;
3221 pcm_file
= file
->private_data
;
3222 substream
= pcm_file
->substream
;
3223 if (PCM_RUNTIME_CHECK(substream
))
3226 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3228 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3229 if (pcm_file
->no_compat_mmap
)
3231 return snd_pcm_mmap_status(substream
, file
, area
);
3232 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3233 if (pcm_file
->no_compat_mmap
)
3235 return snd_pcm_mmap_control(substream
, file
, area
);
3237 return snd_pcm_mmap_data(substream
, file
, area
);
3242 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3244 struct snd_pcm_file
* pcm_file
;
3245 struct snd_pcm_substream
*substream
;
3246 struct snd_pcm_runtime
*runtime
;
3250 pcm_file
= file
->private_data
;
3251 substream
= pcm_file
->substream
;
3252 if (PCM_RUNTIME_CHECK(substream
))
3254 runtime
= substream
->runtime
;
3255 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3266 #ifdef CONFIG_COMPAT
3267 #include "pcm_compat.c"
3269 #define snd_pcm_ioctl_compat NULL
3273 * To be removed helpers to keep binary compatibility
3276 #ifdef CONFIG_SND_SUPPORT_OLD_API
3277 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3278 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3280 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3281 struct snd_pcm_hw_params_old
*oparams
)
3285 memset(params
, 0, sizeof(*params
));
3286 params
->flags
= oparams
->flags
;
3287 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3288 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3289 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3290 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3291 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3292 params
->info
= oparams
->info
;
3293 params
->msbits
= oparams
->msbits
;
3294 params
->rate_num
= oparams
->rate_num
;
3295 params
->rate_den
= oparams
->rate_den
;
3296 params
->fifo_size
= oparams
->fifo_size
;
3299 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3300 struct snd_pcm_hw_params
*params
)
3304 memset(oparams
, 0, sizeof(*oparams
));
3305 oparams
->flags
= params
->flags
;
3306 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3307 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3308 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3309 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3310 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3311 oparams
->info
= params
->info
;
3312 oparams
->msbits
= params
->msbits
;
3313 oparams
->rate_num
= params
->rate_num
;
3314 oparams
->rate_den
= params
->rate_den
;
3315 oparams
->fifo_size
= params
->fifo_size
;
3318 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3319 struct snd_pcm_hw_params_old __user
* _oparams
)
3321 struct snd_pcm_hw_params
*params
;
3322 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3325 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3330 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3336 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3340 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3341 err
= snd_pcm_hw_refine(substream
, params
);
3342 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3343 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3353 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3354 struct snd_pcm_hw_params_old __user
* _oparams
)
3356 struct snd_pcm_hw_params
*params
;
3357 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3360 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3365 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3370 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3374 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3375 err
= snd_pcm_hw_params(substream
, params
);
3376 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3377 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3386 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3392 const struct file_operations snd_pcm_f_ops
[2] = {
3394 .owner
= THIS_MODULE
,
3395 .write
= snd_pcm_write
,
3396 .aio_write
= snd_pcm_aio_write
,
3397 .open
= snd_pcm_playback_open
,
3398 .release
= snd_pcm_release
,
3399 .poll
= snd_pcm_playback_poll
,
3400 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3401 .compat_ioctl
= snd_pcm_ioctl_compat
,
3402 .mmap
= snd_pcm_mmap
,
3403 .fasync
= snd_pcm_fasync
,
3406 .owner
= THIS_MODULE
,
3407 .read
= snd_pcm_read
,
3408 .aio_read
= snd_pcm_aio_read
,
3409 .open
= snd_pcm_capture_open
,
3410 .release
= snd_pcm_release
,
3411 .poll
= snd_pcm_capture_poll
,
3412 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3413 .compat_ioctl
= snd_pcm_ioctl_compat
,
3414 .mmap
= snd_pcm_mmap
,
3415 .fasync
= snd_pcm_fasync
,