2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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
22 #include <sound/driver.h>
24 #include <linux/smp_lock.h>
25 #include <linux/file.h>
26 #include <linux/slab.h>
27 #include <linux/time.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 snd_assert(substream
!= NULL
, return -ENXIO
);
99 memset(info
, 0, sizeof(*info
));
100 info
->card
= pcm
->card
->number
;
101 info
->device
= pcm
->device
;
102 info
->stream
= substream
->stream
;
103 info
->subdevice
= substream
->number
;
104 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
105 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
106 info
->dev_class
= pcm
->dev_class
;
107 info
->dev_subclass
= pcm
->dev_subclass
;
108 info
->subdevices_count
= pstr
->substream_count
;
109 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
110 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
111 runtime
= substream
->runtime
;
112 /* AB: FIXME!!! This is definitely nonsense */
114 info
->sync
= runtime
->sync
;
115 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
120 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
121 struct snd_pcm_info __user
* _info
)
123 struct snd_pcm_info
*info
;
126 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
129 err
= snd_pcm_info(substream
, info
);
131 if (copy_to_user(_info
, info
, sizeof(*info
)))
141 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142 char *snd_pcm_hw_param_names
[] = {
146 HW_PARAM(SAMPLE_BITS
),
147 HW_PARAM(FRAME_BITS
),
150 HW_PARAM(PERIOD_TIME
),
151 HW_PARAM(PERIOD_SIZE
),
152 HW_PARAM(PERIOD_BYTES
),
154 HW_PARAM(BUFFER_TIME
),
155 HW_PARAM(BUFFER_SIZE
),
156 HW_PARAM(BUFFER_BYTES
),
161 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
162 struct snd_pcm_hw_params
*params
)
165 struct snd_pcm_hardware
*hw
;
166 struct snd_interval
*i
= NULL
;
167 struct snd_mask
*m
= NULL
;
168 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
169 unsigned int rstamps
[constrs
->rules_num
];
170 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
171 unsigned int stamp
= 2;
175 params
->fifo_size
= 0;
176 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
178 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
179 params
->rate_num
= 0;
180 params
->rate_den
= 0;
183 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
184 m
= hw_param_mask(params
, k
);
185 if (snd_mask_empty(m
))
187 if (!(params
->rmask
& (1 << k
)))
190 printk("%s = ", snd_pcm_hw_param_names
[k
]);
191 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
193 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
195 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
198 params
->cmask
|= 1 << k
;
203 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
204 i
= hw_param_interval(params
, k
);
205 if (snd_interval_empty(i
))
207 if (!(params
->rmask
& (1 << k
)))
210 printk("%s = ", snd_pcm_hw_param_names
[k
]);
215 i
->openmin
? '(' : '[', i
->min
,
216 i
->max
, i
->openmax
? ')' : ']');
219 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
224 printk("%c%u %u%c\n",
225 i
->openmin
? '(' : '[', i
->min
,
226 i
->max
, i
->openmax
? ')' : ']');
229 params
->cmask
|= 1 << k
;
234 for (k
= 0; k
< constrs
->rules_num
; k
++)
236 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
237 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
240 for (k
= 0; k
< constrs
->rules_num
; k
++) {
241 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
244 if (r
->cond
&& !(r
->cond
& params
->flags
))
246 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
247 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
255 printk("Rule %d [%p]: ", k
, r
->func
);
257 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
258 if (hw_is_mask(r
->var
)) {
259 m
= hw_param_mask(params
, r
->var
);
260 printk("%x", *m
->bits
);
262 i
= hw_param_interval(params
, r
->var
);
267 i
->openmin
? '(' : '[', i
->min
,
268 i
->max
, i
->openmax
? ')' : ']');
272 changed
= r
->func(params
, r
);
276 if (hw_is_mask(r
->var
))
277 printk("%x", *m
->bits
);
283 i
->openmin
? '(' : '[', i
->min
,
284 i
->max
, i
->openmax
? ')' : ']');
290 if (changed
&& r
->var
>= 0) {
291 params
->cmask
|= (1 << r
->var
);
292 vstamps
[r
->var
] = stamp
;
300 if (!params
->msbits
) {
301 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
302 if (snd_interval_single(i
))
303 params
->msbits
= snd_interval_value(i
);
306 if (!params
->rate_den
) {
307 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
308 if (snd_interval_single(i
)) {
309 params
->rate_num
= snd_interval_value(i
);
310 params
->rate_den
= 1;
314 hw
= &substream
->runtime
->hw
;
316 params
->info
= hw
->info
;
317 if (!params
->fifo_size
)
318 params
->fifo_size
= hw
->fifo_size
;
323 EXPORT_SYMBOL(snd_pcm_hw_refine
);
325 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
326 struct snd_pcm_hw_params __user
* _params
)
328 struct snd_pcm_hw_params
*params
;
331 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
336 if (copy_from_user(params
, _params
, sizeof(*params
))) {
340 err
= snd_pcm_hw_refine(substream
, params
);
341 if (copy_to_user(_params
, params
, sizeof(*params
))) {
350 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
351 struct snd_pcm_hw_params
*params
)
353 struct snd_pcm_runtime
*runtime
;
356 snd_pcm_uframes_t frames
;
358 snd_assert(substream
!= NULL
, return -ENXIO
);
359 runtime
= substream
->runtime
;
360 snd_assert(runtime
!= NULL
, return -ENXIO
);
361 snd_pcm_stream_lock_irq(substream
);
362 switch (runtime
->status
->state
) {
363 case SNDRV_PCM_STATE_OPEN
:
364 case SNDRV_PCM_STATE_SETUP
:
365 case SNDRV_PCM_STATE_PREPARED
:
368 snd_pcm_stream_unlock_irq(substream
);
371 snd_pcm_stream_unlock_irq(substream
);
372 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
373 if (!substream
->oss
.oss
)
375 if (atomic_read(&runtime
->mmap_count
))
379 err
= snd_pcm_hw_refine(substream
, params
);
383 err
= snd_pcm_hw_params_choose(substream
, params
);
387 if (substream
->ops
->hw_params
!= NULL
) {
388 err
= substream
->ops
->hw_params(substream
, params
);
393 runtime
->access
= params_access(params
);
394 runtime
->format
= params_format(params
);
395 runtime
->subformat
= params_subformat(params
);
396 runtime
->channels
= params_channels(params
);
397 runtime
->rate
= params_rate(params
);
398 runtime
->period_size
= params_period_size(params
);
399 runtime
->periods
= params_periods(params
);
400 runtime
->buffer_size
= params_buffer_size(params
);
401 runtime
->tick_time
= params_tick_time(params
);
402 runtime
->info
= params
->info
;
403 runtime
->rate_num
= params
->rate_num
;
404 runtime
->rate_den
= params
->rate_den
;
406 bits
= snd_pcm_format_physical_width(runtime
->format
);
407 runtime
->sample_bits
= bits
;
408 bits
*= runtime
->channels
;
409 runtime
->frame_bits
= bits
;
411 while (bits
% 8 != 0) {
415 runtime
->byte_align
= bits
/ 8;
416 runtime
->min_align
= frames
;
418 /* Default sw params */
419 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
420 runtime
->period_step
= 1;
421 runtime
->sleep_min
= 0;
422 runtime
->control
->avail_min
= runtime
->period_size
;
423 runtime
->xfer_align
= runtime
->period_size
;
424 runtime
->start_threshold
= 1;
425 runtime
->stop_threshold
= runtime
->buffer_size
;
426 runtime
->silence_threshold
= 0;
427 runtime
->silence_size
= 0;
428 runtime
->boundary
= runtime
->buffer_size
;
429 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
430 runtime
->boundary
*= 2;
432 snd_pcm_timer_resolution_change(substream
);
433 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
436 /* hardware might be unuseable from this time,
437 so we force application to retry to set
438 the correct hardware parameter settings */
439 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
440 if (substream
->ops
->hw_free
!= NULL
)
441 substream
->ops
->hw_free(substream
);
445 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
446 struct snd_pcm_hw_params __user
* _params
)
448 struct snd_pcm_hw_params
*params
;
451 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
456 if (copy_from_user(params
, _params
, sizeof(*params
))) {
460 err
= snd_pcm_hw_params(substream
, params
);
461 if (copy_to_user(_params
, params
, sizeof(*params
))) {
470 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
472 struct snd_pcm_runtime
*runtime
;
475 snd_assert(substream
!= NULL
, return -ENXIO
);
476 runtime
= substream
->runtime
;
477 snd_assert(runtime
!= NULL
, return -ENXIO
);
478 snd_pcm_stream_lock_irq(substream
);
479 switch (runtime
->status
->state
) {
480 case SNDRV_PCM_STATE_SETUP
:
481 case SNDRV_PCM_STATE_PREPARED
:
484 snd_pcm_stream_unlock_irq(substream
);
487 snd_pcm_stream_unlock_irq(substream
);
488 if (atomic_read(&runtime
->mmap_count
))
490 if (substream
->ops
->hw_free
)
491 result
= substream
->ops
->hw_free(substream
);
492 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
496 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
497 struct snd_pcm_sw_params
*params
)
499 struct snd_pcm_runtime
*runtime
;
501 snd_assert(substream
!= NULL
, return -ENXIO
);
502 runtime
= substream
->runtime
;
503 snd_assert(runtime
!= NULL
, return -ENXIO
);
504 snd_pcm_stream_lock_irq(substream
);
505 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
506 snd_pcm_stream_unlock_irq(substream
);
509 snd_pcm_stream_unlock_irq(substream
);
511 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
513 if (params
->avail_min
== 0)
515 if (params
->xfer_align
== 0 ||
516 params
->xfer_align
% runtime
->min_align
!= 0)
518 if (params
->silence_size
>= runtime
->boundary
) {
519 if (params
->silence_threshold
!= 0)
522 if (params
->silence_size
> params
->silence_threshold
)
524 if (params
->silence_threshold
> runtime
->buffer_size
)
527 snd_pcm_stream_lock_irq(substream
);
528 runtime
->tstamp_mode
= params
->tstamp_mode
;
529 runtime
->sleep_min
= params
->sleep_min
;
530 runtime
->period_step
= params
->period_step
;
531 runtime
->control
->avail_min
= params
->avail_min
;
532 runtime
->start_threshold
= params
->start_threshold
;
533 runtime
->stop_threshold
= params
->stop_threshold
;
534 runtime
->silence_threshold
= params
->silence_threshold
;
535 runtime
->silence_size
= params
->silence_size
;
536 runtime
->xfer_align
= params
->xfer_align
;
537 params
->boundary
= runtime
->boundary
;
538 if (snd_pcm_running(substream
)) {
539 if (runtime
->sleep_min
)
540 snd_pcm_tick_prepare(substream
);
542 snd_pcm_tick_set(substream
, 0);
543 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
544 runtime
->silence_size
> 0)
545 snd_pcm_playback_silence(substream
, ULONG_MAX
);
546 wake_up(&runtime
->sleep
);
548 snd_pcm_stream_unlock_irq(substream
);
552 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
553 struct snd_pcm_sw_params __user
* _params
)
555 struct snd_pcm_sw_params params
;
557 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
559 err
= snd_pcm_sw_params(substream
, ¶ms
);
560 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
565 int snd_pcm_status(struct snd_pcm_substream
*substream
,
566 struct snd_pcm_status
*status
)
568 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
570 snd_pcm_stream_lock_irq(substream
);
571 status
->state
= runtime
->status
->state
;
572 status
->suspended_state
= runtime
->status
->suspended_state
;
573 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
575 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
576 if (snd_pcm_running(substream
)) {
577 snd_pcm_update_hw_ptr(substream
);
578 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
579 status
->tstamp
= runtime
->status
->tstamp
;
581 getnstimeofday(&status
->tstamp
);
583 getnstimeofday(&status
->tstamp
);
584 status
->appl_ptr
= runtime
->control
->appl_ptr
;
585 status
->hw_ptr
= runtime
->status
->hw_ptr
;
586 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
587 status
->avail
= snd_pcm_playback_avail(runtime
);
588 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
589 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
590 status
->delay
= runtime
->buffer_size
- status
->avail
;
594 status
->avail
= snd_pcm_capture_avail(runtime
);
595 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
596 status
->delay
= status
->avail
;
600 status
->avail_max
= runtime
->avail_max
;
601 status
->overrange
= runtime
->overrange
;
602 runtime
->avail_max
= 0;
603 runtime
->overrange
= 0;
605 snd_pcm_stream_unlock_irq(substream
);
609 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
610 struct snd_pcm_status __user
* _status
)
612 struct snd_pcm_status status
;
613 struct snd_pcm_runtime
*runtime
;
616 snd_assert(substream
!= NULL
, return -ENXIO
);
617 runtime
= substream
->runtime
;
618 memset(&status
, 0, sizeof(status
));
619 res
= snd_pcm_status(substream
, &status
);
622 if (copy_to_user(_status
, &status
, sizeof(status
)))
627 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
628 struct snd_pcm_channel_info
* info
)
630 struct snd_pcm_runtime
*runtime
;
631 unsigned int channel
;
633 snd_assert(substream
!= NULL
, return -ENXIO
);
634 channel
= info
->channel
;
635 runtime
= substream
->runtime
;
636 snd_pcm_stream_lock_irq(substream
);
637 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
638 snd_pcm_stream_unlock_irq(substream
);
641 snd_pcm_stream_unlock_irq(substream
);
642 if (channel
>= runtime
->channels
)
644 memset(info
, 0, sizeof(*info
));
645 info
->channel
= channel
;
646 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
649 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
650 struct snd_pcm_channel_info __user
* _info
)
652 struct snd_pcm_channel_info info
;
655 if (copy_from_user(&info
, _info
, sizeof(info
)))
657 res
= snd_pcm_channel_info(substream
, &info
);
660 if (copy_to_user(_info
, &info
, sizeof(info
)))
665 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
667 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
668 if (runtime
->trigger_master
== NULL
)
670 if (runtime
->trigger_master
== substream
) {
671 getnstimeofday(&runtime
->trigger_tstamp
);
673 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
674 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
676 runtime
->trigger_master
= NULL
;
680 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
681 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
682 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
683 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
687 * this functions is core for handling of linked stream
688 * Note: the stream state might be changed also on failure
689 * Note2: call with calling stream lock + link lock
691 static int snd_pcm_action_group(struct action_ops
*ops
,
692 struct snd_pcm_substream
*substream
,
693 int state
, int do_lock
)
695 struct list_head
*pos
;
696 struct snd_pcm_substream
*s
= NULL
;
697 struct snd_pcm_substream
*s1
;
700 snd_pcm_group_for_each(pos
, substream
) {
701 s
= snd_pcm_group_substream_entry(pos
);
702 if (do_lock
&& s
!= substream
)
703 spin_lock(&s
->self_group
.lock
);
704 res
= ops
->pre_action(s
, state
);
708 snd_pcm_group_for_each(pos
, substream
) {
709 s
= snd_pcm_group_substream_entry(pos
);
710 res
= ops
->do_action(s
, state
);
712 if (ops
->undo_action
) {
713 snd_pcm_group_for_each(pos
, substream
) {
714 s1
= snd_pcm_group_substream_entry(pos
);
715 if (s1
== s
) /* failed stream */
717 ops
->undo_action(s1
, state
);
720 s
= NULL
; /* unlock all */
724 snd_pcm_group_for_each(pos
, substream
) {
725 s
= snd_pcm_group_substream_entry(pos
);
726 ops
->post_action(s
, state
);
731 snd_pcm_group_for_each(pos
, substream
) {
732 s1
= snd_pcm_group_substream_entry(pos
);
734 spin_unlock(&s1
->self_group
.lock
);
735 if (s1
== s
) /* end */
743 * Note: call with stream lock
745 static int snd_pcm_action_single(struct action_ops
*ops
,
746 struct snd_pcm_substream
*substream
,
751 res
= ops
->pre_action(substream
, state
);
754 res
= ops
->do_action(substream
, state
);
756 ops
->post_action(substream
, state
);
757 else if (ops
->undo_action
)
758 ops
->undo_action(substream
, state
);
763 * Note: call with stream lock
765 static int snd_pcm_action(struct action_ops
*ops
,
766 struct snd_pcm_substream
*substream
,
771 if (snd_pcm_stream_linked(substream
)) {
772 if (!spin_trylock(&substream
->group
->lock
)) {
773 spin_unlock(&substream
->self_group
.lock
);
774 spin_lock(&substream
->group
->lock
);
775 spin_lock(&substream
->self_group
.lock
);
777 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
778 spin_unlock(&substream
->group
->lock
);
780 res
= snd_pcm_action_single(ops
, substream
, state
);
786 * Note: don't use any locks before
788 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
789 struct snd_pcm_substream
*substream
,
794 read_lock_irq(&snd_pcm_link_rwlock
);
795 if (snd_pcm_stream_linked(substream
)) {
796 spin_lock(&substream
->group
->lock
);
797 spin_lock(&substream
->self_group
.lock
);
798 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
799 spin_unlock(&substream
->self_group
.lock
);
800 spin_unlock(&substream
->group
->lock
);
802 spin_lock(&substream
->self_group
.lock
);
803 res
= snd_pcm_action_single(ops
, substream
, state
);
804 spin_unlock(&substream
->self_group
.lock
);
806 read_unlock_irq(&snd_pcm_link_rwlock
);
812 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
813 struct snd_pcm_substream
*substream
,
818 down_read(&snd_pcm_link_rwsem
);
819 if (snd_pcm_stream_linked(substream
))
820 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
822 res
= snd_pcm_action_single(ops
, substream
, state
);
823 up_read(&snd_pcm_link_rwsem
);
830 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
832 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
833 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
835 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
836 !snd_pcm_playback_data(substream
))
838 runtime
->trigger_master
= substream
;
842 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
844 if (substream
->runtime
->trigger_master
!= substream
)
846 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
849 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
851 if (substream
->runtime
->trigger_master
== substream
)
852 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
855 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
857 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
858 snd_pcm_trigger_tstamp(substream
);
859 runtime
->status
->state
= state
;
860 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
861 runtime
->silence_size
> 0)
862 snd_pcm_playback_silence(substream
, ULONG_MAX
);
863 if (runtime
->sleep_min
)
864 snd_pcm_tick_prepare(substream
);
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
;
918 snd_pcm_tick_set(substream
, 0);
920 wake_up(&runtime
->sleep
);
923 static struct action_ops snd_pcm_action_stop
= {
924 .pre_action
= snd_pcm_pre_stop
,
925 .do_action
= snd_pcm_do_stop
,
926 .post_action
= snd_pcm_post_stop
931 * @substream: the PCM substream instance
932 * @state: PCM state after stopping the stream
934 * Try to stop all running streams in the substream group.
935 * The state of each stream is changed to the given value after that unconditionally.
937 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
939 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
942 EXPORT_SYMBOL(snd_pcm_stop
);
946 * @substream: the PCM substream
948 * Stop the DMA only when the given stream is playback.
949 * The state is changed to SETUP.
950 * Unlike snd_pcm_stop(), this affects only the given stream.
952 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
954 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
955 SNDRV_PCM_STATE_SETUP
);
961 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
963 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
964 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
967 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
969 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
971 runtime
->trigger_master
= substream
;
975 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
977 if (substream
->runtime
->trigger_master
!= substream
)
979 return substream
->ops
->trigger(substream
,
980 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
981 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
984 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
986 if (substream
->runtime
->trigger_master
== substream
)
987 substream
->ops
->trigger(substream
,
988 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
989 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
992 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
994 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
995 snd_pcm_trigger_tstamp(substream
);
997 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
998 if (substream
->timer
)
999 snd_timer_notify(substream
->timer
,
1000 SNDRV_TIMER_EVENT_MPAUSE
,
1001 &runtime
->trigger_tstamp
);
1002 snd_pcm_tick_set(substream
, 0);
1003 wake_up(&runtime
->sleep
);
1005 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1006 if (runtime
->sleep_min
)
1007 snd_pcm_tick_prepare(substream
);
1008 if (substream
->timer
)
1009 snd_timer_notify(substream
->timer
,
1010 SNDRV_TIMER_EVENT_MCONTINUE
,
1011 &runtime
->trigger_tstamp
);
1015 static struct action_ops snd_pcm_action_pause
= {
1016 .pre_action
= snd_pcm_pre_pause
,
1017 .do_action
= snd_pcm_do_pause
,
1018 .undo_action
= snd_pcm_undo_pause
,
1019 .post_action
= snd_pcm_post_pause
1023 * Push/release the pause for all linked streams.
1025 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1027 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1033 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1035 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1036 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1038 runtime
->trigger_master
= substream
;
1042 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1044 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1045 if (runtime
->trigger_master
!= substream
)
1047 if (! snd_pcm_running(substream
))
1049 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1050 return 0; /* suspend unconditionally */
1053 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1055 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1056 snd_pcm_trigger_tstamp(substream
);
1057 if (substream
->timer
)
1058 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1059 &runtime
->trigger_tstamp
);
1060 runtime
->status
->suspended_state
= runtime
->status
->state
;
1061 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1062 snd_pcm_tick_set(substream
, 0);
1063 wake_up(&runtime
->sleep
);
1066 static struct action_ops snd_pcm_action_suspend
= {
1067 .pre_action
= snd_pcm_pre_suspend
,
1068 .do_action
= snd_pcm_do_suspend
,
1069 .post_action
= snd_pcm_post_suspend
1074 * @substream: the PCM substream
1076 * Trigger SUSPEND to all linked streams.
1077 * After this call, all streams are changed to SUSPENDED state.
1079 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1082 unsigned long flags
;
1087 snd_pcm_stream_lock_irqsave(substream
, flags
);
1088 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1089 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1093 EXPORT_SYMBOL(snd_pcm_suspend
);
1096 * snd_pcm_suspend_all
1097 * @pcm: the PCM instance
1099 * Trigger SUSPEND to all substreams in the given pcm.
1100 * After this call, all streams are changed to SUSPENDED state.
1102 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1104 struct snd_pcm_substream
*substream
;
1105 int stream
, err
= 0;
1110 for (stream
= 0; stream
< 2; stream
++) {
1111 for (substream
= pcm
->streams
[stream
].substream
;
1112 substream
; substream
= substream
->next
) {
1113 /* FIXME: the open/close code should lock this as well */
1114 if (substream
->runtime
== NULL
)
1116 err
= snd_pcm_suspend(substream
);
1117 if (err
< 0 && err
!= -EBUSY
)
1124 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1128 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1130 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1131 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1133 runtime
->trigger_master
= substream
;
1137 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1139 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1140 if (runtime
->trigger_master
!= substream
)
1142 /* DMA not running previously? */
1143 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1144 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1145 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1147 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1150 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1152 if (substream
->runtime
->trigger_master
== substream
&&
1153 snd_pcm_running(substream
))
1154 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1157 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1159 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1160 snd_pcm_trigger_tstamp(substream
);
1161 if (substream
->timer
)
1162 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1163 &runtime
->trigger_tstamp
);
1164 runtime
->status
->state
= runtime
->status
->suspended_state
;
1165 if (runtime
->sleep_min
)
1166 snd_pcm_tick_prepare(substream
);
1169 static struct action_ops snd_pcm_action_resume
= {
1170 .pre_action
= snd_pcm_pre_resume
,
1171 .do_action
= snd_pcm_do_resume
,
1172 .undo_action
= snd_pcm_undo_resume
,
1173 .post_action
= snd_pcm_post_resume
1176 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1178 struct snd_card
*card
= substream
->pcm
->card
;
1181 snd_power_lock(card
);
1182 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1183 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1184 snd_power_unlock(card
);
1190 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1195 #endif /* CONFIG_PM */
1200 * Change the RUNNING stream(s) to XRUN state.
1202 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1204 struct snd_card
*card
= substream
->pcm
->card
;
1205 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1208 snd_power_lock(card
);
1209 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1210 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1215 snd_pcm_stream_lock_irq(substream
);
1216 switch (runtime
->status
->state
) {
1217 case SNDRV_PCM_STATE_XRUN
:
1218 result
= 0; /* already there */
1220 case SNDRV_PCM_STATE_RUNNING
:
1221 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1226 snd_pcm_stream_unlock_irq(substream
);
1228 snd_power_unlock(card
);
1235 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1237 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1238 switch (runtime
->status
->state
) {
1239 case SNDRV_PCM_STATE_RUNNING
:
1240 case SNDRV_PCM_STATE_PREPARED
:
1241 case SNDRV_PCM_STATE_PAUSED
:
1242 case SNDRV_PCM_STATE_SUSPENDED
:
1249 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1251 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1252 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1255 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1256 runtime
->hw_ptr_base
= 0;
1257 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1258 runtime
->status
->hw_ptr
% runtime
->period_size
;
1259 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1260 runtime
->silence_filled
= 0;
1264 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1266 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1267 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1268 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1269 runtime
->silence_size
> 0)
1270 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1273 static struct action_ops snd_pcm_action_reset
= {
1274 .pre_action
= snd_pcm_pre_reset
,
1275 .do_action
= snd_pcm_do_reset
,
1276 .post_action
= snd_pcm_post_reset
1279 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1281 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1287 /* we use the second argument for updating f_flags */
1288 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1291 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1292 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1294 if (snd_pcm_running(substream
))
1296 substream
->f_flags
= f_flags
;
1300 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1303 err
= substream
->ops
->prepare(substream
);
1306 return snd_pcm_do_reset(substream
, 0);
1309 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1311 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1312 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1313 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1316 static struct action_ops snd_pcm_action_prepare
= {
1317 .pre_action
= snd_pcm_pre_prepare
,
1318 .do_action
= snd_pcm_do_prepare
,
1319 .post_action
= snd_pcm_post_prepare
1324 * @substream: the PCM substream instance
1325 * @file: file to refer f_flags
1327 * Prepare the PCM substream to be triggerable.
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 if (substream
->f_flags
& O_NONBLOCK
)
1357 substream
->runtime
->trigger_master
= substream
;
1361 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1363 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1364 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1365 switch (runtime
->status
->state
) {
1366 case SNDRV_PCM_STATE_PREPARED
:
1367 /* start playback stream if possible */
1368 if (! snd_pcm_playback_empty(substream
)) {
1369 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1370 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1373 case SNDRV_PCM_STATE_RUNNING
:
1374 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1380 /* stop running stream */
1381 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1382 int state
= snd_pcm_capture_avail(runtime
) > 0 ?
1383 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1384 snd_pcm_do_stop(substream
, state
);
1385 snd_pcm_post_stop(substream
, state
);
1391 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1395 static struct action_ops snd_pcm_action_drain_init
= {
1396 .pre_action
= snd_pcm_pre_drain_init
,
1397 .do_action
= snd_pcm_do_drain_init
,
1398 .post_action
= snd_pcm_post_drain_init
1402 struct snd_pcm_substream
*substream
;
1404 snd_pcm_uframes_t stop_threshold
;
1407 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1410 * Drain the stream(s).
1411 * When the substream is linked, sync until the draining of all playback streams
1413 * After this call, all streams are supposed to be either SETUP or DRAINING
1414 * (capture only) state.
1416 static int snd_pcm_drain(struct snd_pcm_substream
*substream
)
1418 struct snd_card
*card
;
1419 struct snd_pcm_runtime
*runtime
;
1420 struct list_head
*pos
;
1423 struct drain_rec
*drec
, drec_tmp
, *d
;
1425 snd_assert(substream
!= NULL
, return -ENXIO
);
1426 card
= substream
->pcm
->card
;
1427 runtime
= substream
->runtime
;
1429 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1432 snd_power_lock(card
);
1433 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1434 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1436 snd_power_unlock(card
);
1441 /* allocate temporary record for drain sync */
1442 down_read(&snd_pcm_link_rwsem
);
1443 if (snd_pcm_stream_linked(substream
)) {
1444 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1446 up_read(&snd_pcm_link_rwsem
);
1447 snd_power_unlock(card
);
1453 /* count only playback streams */
1455 snd_pcm_group_for_each(pos
, substream
) {
1456 struct snd_pcm_substream
*s
= snd_pcm_group_substream_entry(pos
);
1457 runtime
= s
->runtime
;
1458 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1459 d
= &drec
[num_drecs
++];
1461 init_waitqueue_entry(&d
->wait
, current
);
1462 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1463 /* stop_threshold fixup to avoid endless loop when
1464 * stop_threshold > buffer_size
1466 d
->stop_threshold
= runtime
->stop_threshold
;
1467 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1468 runtime
->stop_threshold
= runtime
->buffer_size
;
1471 up_read(&snd_pcm_link_rwsem
);
1475 snd_pcm_stream_lock_irq(substream
);
1477 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1478 snd_pcm_pause(substream
, 0);
1480 /* pre-start/stop - all running streams are changed to DRAINING state */
1481 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1483 snd_pcm_stream_unlock_irq(substream
);
1489 if (signal_pending(current
)) {
1490 result
= -ERESTARTSYS
;
1494 for (i
= 0; i
< num_drecs
; i
++) {
1495 runtime
= drec
[i
].substream
->runtime
;
1496 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1500 break; /* yes, all drained */
1502 set_current_state(TASK_INTERRUPTIBLE
);
1503 snd_pcm_stream_unlock_irq(substream
);
1504 snd_power_unlock(card
);
1505 tout
= schedule_timeout(10 * HZ
);
1506 snd_power_lock(card
);
1507 snd_pcm_stream_lock_irq(substream
);
1509 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1512 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1513 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1520 snd_pcm_stream_unlock_irq(substream
);
1523 for (i
= 0; i
< num_drecs
; i
++) {
1525 runtime
= d
->substream
->runtime
;
1526 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1527 runtime
->stop_threshold
= d
->stop_threshold
;
1530 if (drec
!= &drec_tmp
)
1532 snd_power_unlock(card
);
1540 * Immediately put all linked substreams into SETUP state.
1542 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1544 struct snd_pcm_runtime
*runtime
;
1545 struct snd_card
*card
;
1548 snd_assert(substream
!= NULL
, return -ENXIO
);
1549 runtime
= substream
->runtime
;
1550 card
= substream
->pcm
->card
;
1552 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1555 snd_power_lock(card
);
1556 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1557 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1562 snd_pcm_stream_lock_irq(substream
);
1564 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1565 snd_pcm_pause(substream
, 0);
1567 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1568 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1569 snd_pcm_stream_unlock_irq(substream
);
1571 snd_power_unlock(card
);
1576 /* WARNING: Don't forget to fput back the file */
1577 static struct file
*snd_pcm_file_fd(int fd
)
1580 struct inode
*inode
;
1586 inode
= file
->f_dentry
->d_inode
;
1587 if (!S_ISCHR(inode
->i_mode
) ||
1588 imajor(inode
) != snd_major
) {
1592 minor
= iminor(inode
);
1593 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1594 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1604 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1608 struct snd_pcm_file
*pcm_file
;
1609 struct snd_pcm_substream
*substream1
;
1611 file
= snd_pcm_file_fd(fd
);
1614 pcm_file
= file
->private_data
;
1615 substream1
= pcm_file
->substream
;
1616 down_write(&snd_pcm_link_rwsem
);
1617 write_lock_irq(&snd_pcm_link_rwlock
);
1618 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1619 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1623 if (snd_pcm_stream_linked(substream1
)) {
1627 if (!snd_pcm_stream_linked(substream
)) {
1628 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1629 if (substream
->group
== NULL
) {
1633 spin_lock_init(&substream
->group
->lock
);
1634 INIT_LIST_HEAD(&substream
->group
->substreams
);
1635 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1636 substream
->group
->count
= 1;
1638 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1639 substream
->group
->count
++;
1640 substream1
->group
= substream
->group
;
1642 write_unlock_irq(&snd_pcm_link_rwlock
);
1643 up_write(&snd_pcm_link_rwsem
);
1648 static void relink_to_local(struct snd_pcm_substream
*substream
)
1650 substream
->group
= &substream
->self_group
;
1651 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1652 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1655 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1657 struct list_head
*pos
;
1660 down_write(&snd_pcm_link_rwsem
);
1661 write_lock_irq(&snd_pcm_link_rwlock
);
1662 if (!snd_pcm_stream_linked(substream
)) {
1666 list_del(&substream
->link_list
);
1667 substream
->group
->count
--;
1668 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1669 snd_pcm_group_for_each(pos
, substream
) {
1670 relink_to_local(snd_pcm_group_substream_entry(pos
));
1673 kfree(substream
->group
);
1675 relink_to_local(substream
);
1677 write_unlock_irq(&snd_pcm_link_rwlock
);
1678 up_write(&snd_pcm_link_rwsem
);
1685 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1686 struct snd_pcm_hw_rule
*rule
)
1688 struct snd_interval t
;
1689 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1690 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1691 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1694 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1695 struct snd_pcm_hw_rule
*rule
)
1697 struct snd_interval t
;
1698 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1699 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1700 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1703 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1704 struct snd_pcm_hw_rule
*rule
)
1706 struct snd_interval t
;
1707 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1708 hw_param_interval_c(params
, rule
->deps
[1]),
1709 (unsigned long) rule
->private, &t
);
1710 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1713 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1714 struct snd_pcm_hw_rule
*rule
)
1716 struct snd_interval t
;
1717 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1718 (unsigned long) rule
->private,
1719 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1720 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1723 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1724 struct snd_pcm_hw_rule
*rule
)
1727 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1729 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1731 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1733 if (! snd_mask_test(mask
, k
))
1735 bits
= snd_pcm_format_physical_width(k
);
1737 continue; /* ignore invalid formats */
1738 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1739 snd_mask_reset(&m
, k
);
1741 return snd_mask_refine(mask
, &m
);
1744 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1745 struct snd_pcm_hw_rule
*rule
)
1747 struct snd_interval t
;
1753 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1755 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1757 bits
= snd_pcm_format_physical_width(k
);
1759 continue; /* ignore invalid formats */
1760 if (t
.min
> (unsigned)bits
)
1762 if (t
.max
< (unsigned)bits
)
1766 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1769 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1770 #error "Change this table"
1773 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1774 48000, 64000, 88200, 96000, 176400, 192000 };
1776 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1777 struct snd_pcm_hw_rule
*rule
)
1779 struct snd_pcm_hardware
*hw
= rule
->private;
1780 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1781 ARRAY_SIZE(rates
), rates
, hw
->rates
);
1784 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1785 struct snd_pcm_hw_rule
*rule
)
1787 struct snd_interval t
;
1788 struct snd_pcm_substream
*substream
= rule
->private;
1790 t
.max
= substream
->buffer_bytes_max
;
1794 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1797 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1799 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1800 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1803 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1804 snd_mask_any(constrs_mask(constrs
, k
));
1807 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1808 snd_interval_any(constrs_interval(constrs
, k
));
1811 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1812 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1813 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1814 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1815 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1817 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1818 snd_pcm_hw_rule_format
, NULL
,
1819 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1822 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1823 snd_pcm_hw_rule_sample_bits
, NULL
,
1824 SNDRV_PCM_HW_PARAM_FORMAT
,
1825 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1828 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1829 snd_pcm_hw_rule_div
, NULL
,
1830 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1833 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1834 snd_pcm_hw_rule_mul
, NULL
,
1835 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1838 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1839 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1840 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1843 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1844 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1845 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1848 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1849 snd_pcm_hw_rule_div
, NULL
,
1850 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1853 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1854 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1855 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1858 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1859 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1863 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1864 snd_pcm_hw_rule_div
, NULL
,
1865 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1868 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1869 snd_pcm_hw_rule_div
, NULL
,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1873 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1874 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1878 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1879 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1883 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1884 snd_pcm_hw_rule_mul
, NULL
,
1885 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1888 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1889 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1893 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1894 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1895 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1898 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1899 snd_pcm_hw_rule_muldivk
, (void*) 8,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1903 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1904 snd_pcm_hw_rule_muldivk
, (void*) 8,
1905 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1908 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1909 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1910 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1913 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1914 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1915 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1921 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1923 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1924 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1926 unsigned int mask
= 0;
1928 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1929 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1930 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1931 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1932 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1933 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1934 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1935 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1936 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1937 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1938 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1940 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1941 snd_assert(err
>= 0, return -EINVAL
);
1943 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1944 snd_assert(err
>= 0, return -EINVAL
);
1946 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1947 snd_assert(err
>= 0, return -EINVAL
);
1949 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1950 hw
->channels_min
, hw
->channels_max
);
1951 snd_assert(err
>= 0, return -EINVAL
);
1953 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1954 hw
->rate_min
, hw
->rate_max
);
1955 snd_assert(err
>= 0, return -EINVAL
);
1957 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1958 hw
->period_bytes_min
, hw
->period_bytes_max
);
1959 snd_assert(err
>= 0, return -EINVAL
);
1961 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1962 hw
->periods_min
, hw
->periods_max
);
1963 snd_assert(err
>= 0, return -EINVAL
);
1965 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1966 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1967 snd_assert(err
>= 0, return -EINVAL
);
1969 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1970 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1971 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1976 if (runtime
->dma_bytes
) {
1977 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1978 snd_assert(err
>= 0, return -EINVAL
);
1981 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1982 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1983 snd_pcm_hw_rule_rate
, hw
,
1984 SNDRV_PCM_HW_PARAM_RATE
, -1);
1989 /* FIXME: this belong to lowlevel */
1990 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_TICK_TIME
,
1991 1000000 / HZ
, 1000000 / HZ
);
1992 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1997 static void snd_pcm_add_file(struct snd_pcm_str
*str
,
1998 struct snd_pcm_file
*pcm_file
)
2000 pcm_file
->next
= str
->files
;
2001 str
->files
= pcm_file
;
2004 static void snd_pcm_remove_file(struct snd_pcm_str
*str
,
2005 struct snd_pcm_file
*pcm_file
)
2007 struct snd_pcm_file
* pcm_file1
;
2008 if (str
->files
== pcm_file
) {
2009 str
->files
= pcm_file
->next
;
2011 pcm_file1
= str
->files
;
2012 while (pcm_file1
&& pcm_file1
->next
!= pcm_file
)
2013 pcm_file1
= pcm_file1
->next
;
2014 if (pcm_file1
!= NULL
)
2015 pcm_file1
->next
= pcm_file
->next
;
2019 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2021 struct snd_pcm_file
*pcm_file
= substream
->file
;
2023 snd_pcm_unlink(substream
);
2024 snd_pcm_remove_file(substream
->pstr
, pcm_file
);
2028 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2030 substream
->ref_count
--;
2031 if (substream
->ref_count
> 0)
2034 snd_pcm_drop(substream
);
2035 if (substream
->hw_opened
) {
2036 if (substream
->ops
->hw_free
!= NULL
)
2037 substream
->ops
->hw_free(substream
);
2038 substream
->ops
->close(substream
);
2039 substream
->hw_opened
= 0;
2041 if (substream
->pcm_release
) {
2042 substream
->pcm_release(substream
);
2043 substream
->pcm_release
= NULL
;
2045 snd_pcm_detach_substream(substream
);
2048 EXPORT_SYMBOL(snd_pcm_release_substream
);
2050 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2052 struct snd_pcm_substream
**rsubstream
)
2054 struct snd_pcm_substream
*substream
;
2057 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2060 if (substream
->ref_count
> 1) {
2061 *rsubstream
= substream
;
2065 substream
->no_mmap_ctrl
= 0;
2066 err
= snd_pcm_hw_constraints_init(substream
);
2068 snd_printd("snd_pcm_hw_constraints_init failed\n");
2072 if ((err
= substream
->ops
->open(substream
)) < 0)
2075 substream
->hw_opened
= 1;
2077 err
= snd_pcm_hw_constraints_complete(substream
);
2079 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2083 *rsubstream
= substream
;
2087 snd_pcm_release_substream(substream
);
2091 EXPORT_SYMBOL(snd_pcm_open_substream
);
2093 static int snd_pcm_open_file(struct file
*file
,
2094 struct snd_pcm
*pcm
,
2096 struct snd_pcm_file
**rpcm_file
)
2098 struct snd_pcm_file
*pcm_file
;
2099 struct snd_pcm_substream
*substream
;
2100 struct snd_pcm_str
*str
;
2103 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
2106 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2110 if (substream
->ref_count
> 1)
2111 pcm_file
= substream
->file
;
2113 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2114 if (pcm_file
== NULL
) {
2115 snd_pcm_release_substream(substream
);
2118 str
= substream
->pstr
;
2119 substream
->file
= pcm_file
;
2120 substream
->pcm_release
= pcm_release_private
;
2121 pcm_file
->substream
= substream
;
2122 snd_pcm_add_file(str
, pcm_file
);
2124 file
->private_data
= pcm_file
;
2125 *rpcm_file
= pcm_file
;
2129 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2131 struct snd_pcm
*pcm
;
2133 pcm
= snd_lookup_minor_data(iminor(inode
),
2134 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2135 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2138 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2140 struct snd_pcm
*pcm
;
2142 pcm
= snd_lookup_minor_data(iminor(inode
),
2143 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2144 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2147 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2150 struct snd_pcm_file
*pcm_file
;
2157 err
= snd_card_file_add(pcm
->card
, file
);
2160 if (!try_module_get(pcm
->card
->module
)) {
2164 init_waitqueue_entry(&wait
, current
);
2165 add_wait_queue(&pcm
->open_wait
, &wait
);
2166 mutex_lock(&pcm
->open_mutex
);
2168 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2171 if (err
== -EAGAIN
) {
2172 if (file
->f_flags
& O_NONBLOCK
) {
2178 set_current_state(TASK_INTERRUPTIBLE
);
2179 mutex_unlock(&pcm
->open_mutex
);
2181 mutex_lock(&pcm
->open_mutex
);
2182 if (signal_pending(current
)) {
2187 remove_wait_queue(&pcm
->open_wait
, &wait
);
2188 mutex_unlock(&pcm
->open_mutex
);
2194 module_put(pcm
->card
->module
);
2196 snd_card_file_remove(pcm
->card
, file
);
2201 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2203 struct snd_pcm
*pcm
;
2204 struct snd_pcm_substream
*substream
;
2205 struct snd_pcm_file
*pcm_file
;
2207 pcm_file
= file
->private_data
;
2208 substream
= pcm_file
->substream
;
2209 snd_assert(substream
!= NULL
, return -ENXIO
);
2210 snd_assert(!atomic_read(&substream
->runtime
->mmap_count
), );
2211 pcm
= substream
->pcm
;
2212 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2213 mutex_lock(&pcm
->open_mutex
);
2214 snd_pcm_release_substream(substream
);
2215 mutex_unlock(&pcm
->open_mutex
);
2216 wake_up(&pcm
->open_wait
);
2217 module_put(pcm
->card
->module
);
2218 snd_card_file_remove(pcm
->card
, file
);
2222 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2223 snd_pcm_uframes_t frames
)
2225 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2226 snd_pcm_sframes_t appl_ptr
;
2227 snd_pcm_sframes_t ret
;
2228 snd_pcm_sframes_t hw_avail
;
2233 snd_pcm_stream_lock_irq(substream
);
2234 switch (runtime
->status
->state
) {
2235 case SNDRV_PCM_STATE_PREPARED
:
2237 case SNDRV_PCM_STATE_DRAINING
:
2238 case SNDRV_PCM_STATE_RUNNING
:
2239 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2242 case SNDRV_PCM_STATE_XRUN
:
2250 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2251 if (hw_avail
<= 0) {
2255 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2258 frames
-= frames
% runtime
->xfer_align
;
2259 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2261 appl_ptr
+= runtime
->boundary
;
2262 runtime
->control
->appl_ptr
= appl_ptr
;
2263 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2265 snd_pcm_tick_prepare(substream
);
2268 snd_pcm_stream_unlock_irq(substream
);
2272 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2273 snd_pcm_uframes_t frames
)
2275 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2276 snd_pcm_sframes_t appl_ptr
;
2277 snd_pcm_sframes_t ret
;
2278 snd_pcm_sframes_t hw_avail
;
2283 snd_pcm_stream_lock_irq(substream
);
2284 switch (runtime
->status
->state
) {
2285 case SNDRV_PCM_STATE_PREPARED
:
2286 case SNDRV_PCM_STATE_DRAINING
:
2288 case SNDRV_PCM_STATE_RUNNING
:
2289 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2292 case SNDRV_PCM_STATE_XRUN
:
2300 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2301 if (hw_avail
<= 0) {
2305 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2308 frames
-= frames
% runtime
->xfer_align
;
2309 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2311 appl_ptr
+= runtime
->boundary
;
2312 runtime
->control
->appl_ptr
= appl_ptr
;
2313 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2315 snd_pcm_tick_prepare(substream
);
2318 snd_pcm_stream_unlock_irq(substream
);
2322 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2323 snd_pcm_uframes_t frames
)
2325 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2326 snd_pcm_sframes_t appl_ptr
;
2327 snd_pcm_sframes_t ret
;
2328 snd_pcm_sframes_t avail
;
2333 snd_pcm_stream_lock_irq(substream
);
2334 switch (runtime
->status
->state
) {
2335 case SNDRV_PCM_STATE_PREPARED
:
2336 case SNDRV_PCM_STATE_PAUSED
:
2338 case SNDRV_PCM_STATE_DRAINING
:
2339 case SNDRV_PCM_STATE_RUNNING
:
2340 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2343 case SNDRV_PCM_STATE_XRUN
:
2351 avail
= snd_pcm_playback_avail(runtime
);
2356 if (frames
> (snd_pcm_uframes_t
)avail
)
2359 frames
-= frames
% runtime
->xfer_align
;
2360 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2361 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2362 appl_ptr
-= runtime
->boundary
;
2363 runtime
->control
->appl_ptr
= appl_ptr
;
2364 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2366 snd_pcm_tick_prepare(substream
);
2369 snd_pcm_stream_unlock_irq(substream
);
2373 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2374 snd_pcm_uframes_t frames
)
2376 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2377 snd_pcm_sframes_t appl_ptr
;
2378 snd_pcm_sframes_t ret
;
2379 snd_pcm_sframes_t avail
;
2384 snd_pcm_stream_lock_irq(substream
);
2385 switch (runtime
->status
->state
) {
2386 case SNDRV_PCM_STATE_PREPARED
:
2387 case SNDRV_PCM_STATE_DRAINING
:
2388 case SNDRV_PCM_STATE_PAUSED
:
2390 case SNDRV_PCM_STATE_RUNNING
:
2391 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2394 case SNDRV_PCM_STATE_XRUN
:
2402 avail
= snd_pcm_capture_avail(runtime
);
2407 if (frames
> (snd_pcm_uframes_t
)avail
)
2410 frames
-= frames
% runtime
->xfer_align
;
2411 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2412 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2413 appl_ptr
-= runtime
->boundary
;
2414 runtime
->control
->appl_ptr
= appl_ptr
;
2415 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2417 snd_pcm_tick_prepare(substream
);
2420 snd_pcm_stream_unlock_irq(substream
);
2424 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2426 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2429 snd_pcm_stream_lock_irq(substream
);
2430 switch (runtime
->status
->state
) {
2431 case SNDRV_PCM_STATE_DRAINING
:
2432 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2434 case SNDRV_PCM_STATE_RUNNING
:
2435 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2438 case SNDRV_PCM_STATE_PREPARED
:
2439 case SNDRV_PCM_STATE_SUSPENDED
:
2442 case SNDRV_PCM_STATE_XRUN
:
2450 snd_pcm_stream_unlock_irq(substream
);
2454 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2455 snd_pcm_sframes_t __user
*res
)
2457 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2459 snd_pcm_sframes_t n
= 0;
2461 snd_pcm_stream_lock_irq(substream
);
2462 switch (runtime
->status
->state
) {
2463 case SNDRV_PCM_STATE_DRAINING
:
2464 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2466 case SNDRV_PCM_STATE_RUNNING
:
2467 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2470 case SNDRV_PCM_STATE_PREPARED
:
2471 case SNDRV_PCM_STATE_SUSPENDED
:
2473 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2474 n
= snd_pcm_playback_hw_avail(runtime
);
2476 n
= snd_pcm_capture_avail(runtime
);
2478 case SNDRV_PCM_STATE_XRUN
:
2486 snd_pcm_stream_unlock_irq(substream
);
2488 if (put_user(n
, res
))
2493 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2494 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2496 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2497 struct snd_pcm_sync_ptr sync_ptr
;
2498 volatile struct snd_pcm_mmap_status
*status
;
2499 volatile struct snd_pcm_mmap_control
*control
;
2502 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2503 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2505 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2507 status
= runtime
->status
;
2508 control
= runtime
->control
;
2509 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2510 err
= snd_pcm_hwsync(substream
);
2514 snd_pcm_stream_lock_irq(substream
);
2515 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2516 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2518 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2519 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2520 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2522 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2523 sync_ptr
.s
.status
.state
= status
->state
;
2524 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2525 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2526 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2527 snd_pcm_stream_unlock_irq(substream
);
2528 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2533 static int snd_pcm_common_ioctl1(struct file
*file
,
2534 struct snd_pcm_substream
*substream
,
2535 unsigned int cmd
, void __user
*arg
)
2537 snd_assert(substream
!= NULL
, return -ENXIO
);
2540 case SNDRV_PCM_IOCTL_PVERSION
:
2541 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2542 case SNDRV_PCM_IOCTL_INFO
:
2543 return snd_pcm_info_user(substream
, arg
);
2544 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2546 case SNDRV_PCM_IOCTL_HW_REFINE
:
2547 return snd_pcm_hw_refine_user(substream
, arg
);
2548 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2549 return snd_pcm_hw_params_user(substream
, arg
);
2550 case SNDRV_PCM_IOCTL_HW_FREE
:
2551 return snd_pcm_hw_free(substream
);
2552 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2553 return snd_pcm_sw_params_user(substream
, arg
);
2554 case SNDRV_PCM_IOCTL_STATUS
:
2555 return snd_pcm_status_user(substream
, arg
);
2556 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2557 return snd_pcm_channel_info_user(substream
, arg
);
2558 case SNDRV_PCM_IOCTL_PREPARE
:
2559 return snd_pcm_prepare(substream
, file
);
2560 case SNDRV_PCM_IOCTL_RESET
:
2561 return snd_pcm_reset(substream
);
2562 case SNDRV_PCM_IOCTL_START
:
2563 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2564 case SNDRV_PCM_IOCTL_LINK
:
2565 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2566 case SNDRV_PCM_IOCTL_UNLINK
:
2567 return snd_pcm_unlink(substream
);
2568 case SNDRV_PCM_IOCTL_RESUME
:
2569 return snd_pcm_resume(substream
);
2570 case SNDRV_PCM_IOCTL_XRUN
:
2571 return snd_pcm_xrun(substream
);
2572 case SNDRV_PCM_IOCTL_HWSYNC
:
2573 return snd_pcm_hwsync(substream
);
2574 case SNDRV_PCM_IOCTL_DELAY
:
2575 return snd_pcm_delay(substream
, arg
);
2576 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2577 return snd_pcm_sync_ptr(substream
, arg
);
2578 #ifdef CONFIG_SND_SUPPORT_OLD_API
2579 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2580 return snd_pcm_hw_refine_old_user(substream
, arg
);
2581 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2582 return snd_pcm_hw_params_old_user(substream
, arg
);
2584 case SNDRV_PCM_IOCTL_DRAIN
:
2585 return snd_pcm_drain(substream
);
2586 case SNDRV_PCM_IOCTL_DROP
:
2587 return snd_pcm_drop(substream
);
2588 case SNDRV_PCM_IOCTL_PAUSE
:
2591 snd_pcm_stream_lock_irq(substream
);
2592 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2593 snd_pcm_stream_unlock_irq(substream
);
2597 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2601 static int snd_pcm_playback_ioctl1(struct file
*file
,
2602 struct snd_pcm_substream
*substream
,
2603 unsigned int cmd
, void __user
*arg
)
2605 snd_assert(substream
!= NULL
, return -ENXIO
);
2606 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2608 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2610 struct snd_xferi xferi
;
2611 struct snd_xferi __user
*_xferi
= arg
;
2612 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2613 snd_pcm_sframes_t result
;
2614 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2616 if (put_user(0, &_xferi
->result
))
2618 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2620 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2621 __put_user(result
, &_xferi
->result
);
2622 return result
< 0 ? result
: 0;
2624 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2626 struct snd_xfern xfern
;
2627 struct snd_xfern __user
*_xfern
= arg
;
2628 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2630 snd_pcm_sframes_t result
;
2631 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2633 if (runtime
->channels
> 128)
2635 if (put_user(0, &_xfern
->result
))
2637 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2639 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2642 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2646 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2648 __put_user(result
, &_xfern
->result
);
2649 return result
< 0 ? result
: 0;
2651 case SNDRV_PCM_IOCTL_REWIND
:
2653 snd_pcm_uframes_t frames
;
2654 snd_pcm_uframes_t __user
*_frames
= arg
;
2655 snd_pcm_sframes_t result
;
2656 if (get_user(frames
, _frames
))
2658 if (put_user(0, _frames
))
2660 result
= snd_pcm_playback_rewind(substream
, frames
);
2661 __put_user(result
, _frames
);
2662 return result
< 0 ? result
: 0;
2664 case SNDRV_PCM_IOCTL_FORWARD
:
2666 snd_pcm_uframes_t frames
;
2667 snd_pcm_uframes_t __user
*_frames
= arg
;
2668 snd_pcm_sframes_t result
;
2669 if (get_user(frames
, _frames
))
2671 if (put_user(0, _frames
))
2673 result
= snd_pcm_playback_forward(substream
, frames
);
2674 __put_user(result
, _frames
);
2675 return result
< 0 ? result
: 0;
2678 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2681 static int snd_pcm_capture_ioctl1(struct file
*file
,
2682 struct snd_pcm_substream
*substream
,
2683 unsigned int cmd
, void __user
*arg
)
2685 snd_assert(substream
!= NULL
, return -ENXIO
);
2686 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2688 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2690 struct snd_xferi xferi
;
2691 struct snd_xferi __user
*_xferi
= arg
;
2692 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2693 snd_pcm_sframes_t result
;
2694 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2696 if (put_user(0, &_xferi
->result
))
2698 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2700 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2701 __put_user(result
, &_xferi
->result
);
2702 return result
< 0 ? result
: 0;
2704 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2706 struct snd_xfern xfern
;
2707 struct snd_xfern __user
*_xfern
= arg
;
2708 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2710 snd_pcm_sframes_t result
;
2711 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2713 if (runtime
->channels
> 128)
2715 if (put_user(0, &_xfern
->result
))
2717 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2719 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2722 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2726 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2728 __put_user(result
, &_xfern
->result
);
2729 return result
< 0 ? result
: 0;
2731 case SNDRV_PCM_IOCTL_REWIND
:
2733 snd_pcm_uframes_t frames
;
2734 snd_pcm_uframes_t __user
*_frames
= arg
;
2735 snd_pcm_sframes_t result
;
2736 if (get_user(frames
, _frames
))
2738 if (put_user(0, _frames
))
2740 result
= snd_pcm_capture_rewind(substream
, frames
);
2741 __put_user(result
, _frames
);
2742 return result
< 0 ? result
: 0;
2744 case SNDRV_PCM_IOCTL_FORWARD
:
2746 snd_pcm_uframes_t frames
;
2747 snd_pcm_uframes_t __user
*_frames
= arg
;
2748 snd_pcm_sframes_t result
;
2749 if (get_user(frames
, _frames
))
2751 if (put_user(0, _frames
))
2753 result
= snd_pcm_capture_forward(substream
, frames
);
2754 __put_user(result
, _frames
);
2755 return result
< 0 ? result
: 0;
2758 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2761 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2764 struct snd_pcm_file
*pcm_file
;
2766 pcm_file
= file
->private_data
;
2768 if (((cmd
>> 8) & 0xff) != 'A')
2771 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2772 (void __user
*)arg
);
2775 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2778 struct snd_pcm_file
*pcm_file
;
2780 pcm_file
= file
->private_data
;
2782 if (((cmd
>> 8) & 0xff) != 'A')
2785 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2786 (void __user
*)arg
);
2789 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2790 unsigned int cmd
, void *arg
)
2795 fs
= snd_enter_user();
2796 switch (substream
->stream
) {
2797 case SNDRV_PCM_STREAM_PLAYBACK
:
2798 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2799 (void __user
*)arg
);
2801 case SNDRV_PCM_STREAM_CAPTURE
:
2802 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2803 (void __user
*)arg
);
2813 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2815 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2818 struct snd_pcm_file
*pcm_file
;
2819 struct snd_pcm_substream
*substream
;
2820 struct snd_pcm_runtime
*runtime
;
2821 snd_pcm_sframes_t result
;
2823 pcm_file
= file
->private_data
;
2824 substream
= pcm_file
->substream
;
2825 snd_assert(substream
!= NULL
, return -ENXIO
);
2826 runtime
= substream
->runtime
;
2827 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2829 if (!frame_aligned(runtime
, count
))
2831 count
= bytes_to_frames(runtime
, count
);
2832 result
= snd_pcm_lib_read(substream
, buf
, count
);
2834 result
= frames_to_bytes(runtime
, result
);
2838 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2839 size_t count
, loff_t
* offset
)
2841 struct snd_pcm_file
*pcm_file
;
2842 struct snd_pcm_substream
*substream
;
2843 struct snd_pcm_runtime
*runtime
;
2844 snd_pcm_sframes_t result
;
2846 pcm_file
= file
->private_data
;
2847 substream
= pcm_file
->substream
;
2848 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2849 runtime
= substream
->runtime
;
2850 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2854 if (!frame_aligned(runtime
, count
)) {
2858 count
= bytes_to_frames(runtime
, count
);
2859 result
= snd_pcm_lib_write(substream
, buf
, count
);
2861 result
= frames_to_bytes(runtime
, result
);
2866 static ssize_t
snd_pcm_readv(struct file
*file
, const struct iovec
*_vector
,
2867 unsigned long count
, loff_t
* offset
)
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
= file
->private_data
;
2879 substream
= pcm_file
->substream
;
2880 snd_assert(substream
!= NULL
, return -ENXIO
);
2881 runtime
= substream
->runtime
;
2882 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2884 if (count
> 1024 || count
!= runtime
->channels
)
2886 if (!frame_aligned(runtime
, _vector
->iov_len
))
2888 frames
= bytes_to_samples(runtime
, _vector
->iov_len
);
2889 bufs
= kmalloc(sizeof(void *) * count
, GFP_KERNEL
);
2892 for (i
= 0; i
< count
; ++i
)
2893 bufs
[i
] = _vector
[i
].iov_base
;
2894 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2896 result
= frames_to_bytes(runtime
, result
);
2901 static ssize_t
snd_pcm_writev(struct file
*file
, const struct iovec
*_vector
,
2902 unsigned long count
, loff_t
* offset
)
2904 struct snd_pcm_file
*pcm_file
;
2905 struct snd_pcm_substream
*substream
;
2906 struct snd_pcm_runtime
*runtime
;
2907 snd_pcm_sframes_t result
;
2910 snd_pcm_uframes_t frames
;
2912 pcm_file
= file
->private_data
;
2913 substream
= pcm_file
->substream
;
2914 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2915 runtime
= substream
->runtime
;
2916 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2920 if (count
> 128 || count
!= runtime
->channels
||
2921 !frame_aligned(runtime
, _vector
->iov_len
)) {
2925 frames
= bytes_to_samples(runtime
, _vector
->iov_len
);
2926 bufs
= kmalloc(sizeof(void *) * count
, GFP_KERNEL
);
2929 for (i
= 0; i
< count
; ++i
)
2930 bufs
[i
] = _vector
[i
].iov_base
;
2931 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2933 result
= frames_to_bytes(runtime
, result
);
2939 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2941 struct snd_pcm_file
*pcm_file
;
2942 struct snd_pcm_substream
*substream
;
2943 struct snd_pcm_runtime
*runtime
;
2945 snd_pcm_uframes_t avail
;
2947 pcm_file
= file
->private_data
;
2949 substream
= pcm_file
->substream
;
2950 snd_assert(substream
!= NULL
, return -ENXIO
);
2951 runtime
= substream
->runtime
;
2953 poll_wait(file
, &runtime
->sleep
, wait
);
2955 snd_pcm_stream_lock_irq(substream
);
2956 avail
= snd_pcm_playback_avail(runtime
);
2957 switch (runtime
->status
->state
) {
2958 case SNDRV_PCM_STATE_RUNNING
:
2959 case SNDRV_PCM_STATE_PREPARED
:
2960 case SNDRV_PCM_STATE_PAUSED
:
2961 if (avail
>= runtime
->control
->avail_min
) {
2962 mask
= POLLOUT
| POLLWRNORM
;
2966 case SNDRV_PCM_STATE_DRAINING
:
2970 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2973 snd_pcm_stream_unlock_irq(substream
);
2977 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2979 struct snd_pcm_file
*pcm_file
;
2980 struct snd_pcm_substream
*substream
;
2981 struct snd_pcm_runtime
*runtime
;
2983 snd_pcm_uframes_t avail
;
2985 pcm_file
= file
->private_data
;
2987 substream
= pcm_file
->substream
;
2988 snd_assert(substream
!= NULL
, return -ENXIO
);
2989 runtime
= substream
->runtime
;
2991 poll_wait(file
, &runtime
->sleep
, wait
);
2993 snd_pcm_stream_lock_irq(substream
);
2994 avail
= snd_pcm_capture_avail(runtime
);
2995 switch (runtime
->status
->state
) {
2996 case SNDRV_PCM_STATE_RUNNING
:
2997 case SNDRV_PCM_STATE_PREPARED
:
2998 case SNDRV_PCM_STATE_PAUSED
:
2999 if (avail
>= runtime
->control
->avail_min
) {
3000 mask
= POLLIN
| POLLRDNORM
;
3005 case SNDRV_PCM_STATE_DRAINING
:
3007 mask
= POLLIN
| POLLRDNORM
;
3012 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3015 snd_pcm_stream_unlock_irq(substream
);
3024 * Only on coherent architectures, we can mmap the status and the control records
3025 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3027 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3029 * mmap status record
3031 static struct page
* snd_pcm_mmap_status_nopage(struct vm_area_struct
*area
,
3032 unsigned long address
, int *type
)
3034 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3035 struct snd_pcm_runtime
*runtime
;
3038 if (substream
== NULL
)
3040 runtime
= substream
->runtime
;
3041 page
= virt_to_page(runtime
->status
);
3044 *type
= VM_FAULT_MINOR
;
3048 static struct vm_operations_struct snd_pcm_vm_ops_status
=
3050 .nopage
= snd_pcm_mmap_status_nopage
,
3053 static int snd_pcm_mmap_status(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 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3062 size
= area
->vm_end
- area
->vm_start
;
3063 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3065 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3066 area
->vm_private_data
= substream
;
3067 area
->vm_flags
|= VM_RESERVED
;
3072 * mmap control record
3074 static struct page
* snd_pcm_mmap_control_nopage(struct vm_area_struct
*area
,
3075 unsigned long address
, int *type
)
3077 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3078 struct snd_pcm_runtime
*runtime
;
3081 if (substream
== NULL
)
3083 runtime
= substream
->runtime
;
3084 page
= virt_to_page(runtime
->control
);
3087 *type
= VM_FAULT_MINOR
;
3091 static struct vm_operations_struct snd_pcm_vm_ops_control
=
3093 .nopage
= snd_pcm_mmap_control_nopage
,
3096 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3097 struct vm_area_struct
*area
)
3099 struct snd_pcm_runtime
*runtime
;
3101 if (!(area
->vm_flags
& VM_READ
))
3103 runtime
= substream
->runtime
;
3104 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3105 size
= area
->vm_end
- area
->vm_start
;
3106 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3108 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3109 area
->vm_private_data
= substream
;
3110 area
->vm_flags
|= VM_RESERVED
;
3113 #else /* ! coherent mmap */
3115 * don't support mmap for status and control records.
3117 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3118 struct vm_area_struct
*area
)
3122 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3123 struct vm_area_struct
*area
)
3127 #endif /* coherent mmap */
3130 * nopage callback for mmapping a RAM page
3132 static struct page
*snd_pcm_mmap_data_nopage(struct vm_area_struct
*area
,
3133 unsigned long address
, int *type
)
3135 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3136 struct snd_pcm_runtime
*runtime
;
3137 unsigned long offset
;
3142 if (substream
== NULL
)
3144 runtime
= substream
->runtime
;
3145 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3146 offset
+= address
- area
->vm_start
;
3147 snd_assert((offset
% PAGE_SIZE
) == 0, return NOPAGE_OOM
);
3148 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3149 if (offset
> dma_bytes
- PAGE_SIZE
)
3150 return NOPAGE_SIGBUS
;
3151 if (substream
->ops
->page
) {
3152 page
= substream
->ops
->page(substream
, offset
);
3156 vaddr
= runtime
->dma_area
+ offset
;
3157 page
= virt_to_page(vaddr
);
3161 *type
= VM_FAULT_MINOR
;
3165 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3167 .open
= snd_pcm_mmap_data_open
,
3168 .close
= snd_pcm_mmap_data_close
,
3169 .nopage
= snd_pcm_mmap_data_nopage
,
3173 * mmap the DMA buffer on RAM
3175 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3176 struct vm_area_struct
*area
)
3178 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3179 area
->vm_private_data
= substream
;
3180 area
->vm_flags
|= VM_RESERVED
;
3181 atomic_inc(&substream
->runtime
->mmap_count
);
3186 * mmap the DMA buffer on I/O memory area
3188 #if SNDRV_PCM_INFO_MMAP_IOMEM
3189 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3191 .open
= snd_pcm_mmap_data_open
,
3192 .close
= snd_pcm_mmap_data_close
,
3195 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3196 struct vm_area_struct
*area
)
3199 unsigned long offset
;
3201 #ifdef pgprot_noncached
3202 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3204 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3205 area
->vm_private_data
= substream
;
3206 area
->vm_flags
|= VM_IO
;
3207 size
= area
->vm_end
- area
->vm_start
;
3208 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3209 if (io_remap_pfn_range(area
, area
->vm_start
,
3210 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3211 size
, area
->vm_page_prot
))
3213 atomic_inc(&substream
->runtime
->mmap_count
);
3217 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3218 #endif /* SNDRV_PCM_INFO_MMAP */
3223 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3224 struct vm_area_struct
*area
)
3226 struct snd_pcm_runtime
*runtime
;
3228 unsigned long offset
;
3231 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3232 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3235 if (!(area
->vm_flags
& VM_READ
))
3238 runtime
= substream
->runtime
;
3239 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3240 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3242 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3244 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3245 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3247 size
= area
->vm_end
- area
->vm_start
;
3248 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3249 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3250 if ((size_t)size
> dma_bytes
)
3252 if (offset
> dma_bytes
- size
)
3255 if (substream
->ops
->mmap
)
3256 return substream
->ops
->mmap(substream
, area
);
3258 return snd_pcm_default_mmap(substream
, area
);
3261 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3263 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3265 struct snd_pcm_file
* pcm_file
;
3266 struct snd_pcm_substream
*substream
;
3267 unsigned long offset
;
3269 pcm_file
= file
->private_data
;
3270 substream
= pcm_file
->substream
;
3271 snd_assert(substream
!= NULL
, return -ENXIO
);
3273 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3275 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3276 if (substream
->no_mmap_ctrl
)
3278 return snd_pcm_mmap_status(substream
, file
, area
);
3279 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3280 if (substream
->no_mmap_ctrl
)
3282 return snd_pcm_mmap_control(substream
, file
, area
);
3284 return snd_pcm_mmap_data(substream
, file
, area
);
3289 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3291 struct snd_pcm_file
* pcm_file
;
3292 struct snd_pcm_substream
*substream
;
3293 struct snd_pcm_runtime
*runtime
;
3296 pcm_file
= file
->private_data
;
3297 substream
= pcm_file
->substream
;
3298 snd_assert(substream
!= NULL
, return -ENXIO
);
3299 runtime
= substream
->runtime
;
3301 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3310 #ifdef CONFIG_COMPAT
3311 #include "pcm_compat.c"
3313 #define snd_pcm_ioctl_compat NULL
3317 * To be removed helpers to keep binary compatibility
3320 #ifdef CONFIG_SND_SUPPORT_OLD_API
3321 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3322 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3324 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3325 struct snd_pcm_hw_params_old
*oparams
)
3329 memset(params
, 0, sizeof(*params
));
3330 params
->flags
= oparams
->flags
;
3331 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3332 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3333 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3334 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3335 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3336 params
->info
= oparams
->info
;
3337 params
->msbits
= oparams
->msbits
;
3338 params
->rate_num
= oparams
->rate_num
;
3339 params
->rate_den
= oparams
->rate_den
;
3340 params
->fifo_size
= oparams
->fifo_size
;
3343 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3344 struct snd_pcm_hw_params
*params
)
3348 memset(oparams
, 0, sizeof(*oparams
));
3349 oparams
->flags
= params
->flags
;
3350 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3351 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3352 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3353 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3354 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3355 oparams
->info
= params
->info
;
3356 oparams
->msbits
= params
->msbits
;
3357 oparams
->rate_num
= params
->rate_num
;
3358 oparams
->rate_den
= params
->rate_den
;
3359 oparams
->fifo_size
= params
->fifo_size
;
3362 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3363 struct snd_pcm_hw_params_old __user
* _oparams
)
3365 struct snd_pcm_hw_params
*params
;
3366 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3369 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3374 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3380 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3384 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3385 err
= snd_pcm_hw_refine(substream
, params
);
3386 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3387 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3397 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3398 struct snd_pcm_hw_params_old __user
* _oparams
)
3400 struct snd_pcm_hw_params
*params
;
3401 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3404 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3409 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3414 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3418 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3419 err
= snd_pcm_hw_params(substream
, params
);
3420 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3421 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3430 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3436 struct file_operations snd_pcm_f_ops
[2] = {
3438 .owner
= THIS_MODULE
,
3439 .write
= snd_pcm_write
,
3440 .writev
= snd_pcm_writev
,
3441 .open
= snd_pcm_playback_open
,
3442 .release
= snd_pcm_release
,
3443 .poll
= snd_pcm_playback_poll
,
3444 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3445 .compat_ioctl
= snd_pcm_ioctl_compat
,
3446 .mmap
= snd_pcm_mmap
,
3447 .fasync
= snd_pcm_fasync
,
3450 .owner
= THIS_MODULE
,
3451 .read
= snd_pcm_read
,
3452 .readv
= snd_pcm_readv
,
3453 .open
= snd_pcm_capture_open
,
3454 .release
= snd_pcm_release
,
3455 .poll
= snd_pcm_capture_poll
,
3456 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3457 .compat_ioctl
= snd_pcm_ioctl_compat
,
3458 .mmap
= snd_pcm_mmap
,
3459 .fasync
= snd_pcm_fasync
,