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/latency.h>
29 #include <linux/uio.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
43 struct snd_pcm_hw_params_old
{
45 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
46 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
47 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
53 unsigned int rate_num
;
54 unsigned int rate_den
;
55 snd_pcm_uframes_t fifo_size
;
56 unsigned char reserved
[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
64 struct snd_pcm_hw_params_old __user
* _oparams
);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
66 struct snd_pcm_hw_params_old __user
* _oparams
);
68 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
74 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
75 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
77 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
79 static inline mm_segment_t
snd_enter_user(void)
81 mm_segment_t fs
= get_fs();
86 static inline void snd_leave_user(mm_segment_t fs
)
93 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
95 struct snd_pcm_runtime
*runtime
;
96 struct snd_pcm
*pcm
= substream
->pcm
;
97 struct snd_pcm_str
*pstr
= substream
->pstr
;
99 snd_assert(substream
!= NULL
, return -ENXIO
);
100 memset(info
, 0, sizeof(*info
));
101 info
->card
= pcm
->card
->number
;
102 info
->device
= pcm
->device
;
103 info
->stream
= substream
->stream
;
104 info
->subdevice
= substream
->number
;
105 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
106 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
107 info
->dev_class
= pcm
->dev_class
;
108 info
->dev_subclass
= pcm
->dev_subclass
;
109 info
->subdevices_count
= pstr
->substream_count
;
110 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
111 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
112 runtime
= substream
->runtime
;
113 /* AB: FIXME!!! This is definitely nonsense */
115 info
->sync
= runtime
->sync
;
116 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
121 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
122 struct snd_pcm_info __user
* _info
)
124 struct snd_pcm_info
*info
;
127 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
130 err
= snd_pcm_info(substream
, info
);
132 if (copy_to_user(_info
, info
, sizeof(*info
)))
142 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
143 char *snd_pcm_hw_param_names
[] = {
147 HW_PARAM(SAMPLE_BITS
),
148 HW_PARAM(FRAME_BITS
),
151 HW_PARAM(PERIOD_TIME
),
152 HW_PARAM(PERIOD_SIZE
),
153 HW_PARAM(PERIOD_BYTES
),
155 HW_PARAM(BUFFER_TIME
),
156 HW_PARAM(BUFFER_SIZE
),
157 HW_PARAM(BUFFER_BYTES
),
162 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
163 struct snd_pcm_hw_params
*params
)
166 struct snd_pcm_hardware
*hw
;
167 struct snd_interval
*i
= NULL
;
168 struct snd_mask
*m
= NULL
;
169 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
170 unsigned int rstamps
[constrs
->rules_num
];
171 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
172 unsigned int stamp
= 2;
176 params
->fifo_size
= 0;
177 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
179 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
180 params
->rate_num
= 0;
181 params
->rate_den
= 0;
184 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
185 m
= hw_param_mask(params
, k
);
186 if (snd_mask_empty(m
))
188 if (!(params
->rmask
& (1 << k
)))
191 printk("%s = ", snd_pcm_hw_param_names
[k
]);
192 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
194 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
196 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
199 params
->cmask
|= 1 << k
;
204 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
205 i
= hw_param_interval(params
, k
);
206 if (snd_interval_empty(i
))
208 if (!(params
->rmask
& (1 << k
)))
211 printk("%s = ", snd_pcm_hw_param_names
[k
]);
216 i
->openmin
? '(' : '[', i
->min
,
217 i
->max
, i
->openmax
? ')' : ']');
220 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
225 printk("%c%u %u%c\n",
226 i
->openmin
? '(' : '[', i
->min
,
227 i
->max
, i
->openmax
? ')' : ']');
230 params
->cmask
|= 1 << k
;
235 for (k
= 0; k
< constrs
->rules_num
; k
++)
237 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
238 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
241 for (k
= 0; k
< constrs
->rules_num
; k
++) {
242 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
245 if (r
->cond
&& !(r
->cond
& params
->flags
))
247 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
248 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
256 printk("Rule %d [%p]: ", k
, r
->func
);
258 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
259 if (hw_is_mask(r
->var
)) {
260 m
= hw_param_mask(params
, r
->var
);
261 printk("%x", *m
->bits
);
263 i
= hw_param_interval(params
, r
->var
);
268 i
->openmin
? '(' : '[', i
->min
,
269 i
->max
, i
->openmax
? ')' : ']');
273 changed
= r
->func(params
, r
);
277 if (hw_is_mask(r
->var
))
278 printk("%x", *m
->bits
);
284 i
->openmin
? '(' : '[', i
->min
,
285 i
->max
, i
->openmax
? ')' : ']');
291 if (changed
&& r
->var
>= 0) {
292 params
->cmask
|= (1 << r
->var
);
293 vstamps
[r
->var
] = stamp
;
301 if (!params
->msbits
) {
302 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
303 if (snd_interval_single(i
))
304 params
->msbits
= snd_interval_value(i
);
307 if (!params
->rate_den
) {
308 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
309 if (snd_interval_single(i
)) {
310 params
->rate_num
= snd_interval_value(i
);
311 params
->rate_den
= 1;
315 hw
= &substream
->runtime
->hw
;
317 params
->info
= hw
->info
;
318 if (!params
->fifo_size
)
319 params
->fifo_size
= hw
->fifo_size
;
324 EXPORT_SYMBOL(snd_pcm_hw_refine
);
326 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
327 struct snd_pcm_hw_params __user
* _params
)
329 struct snd_pcm_hw_params
*params
;
332 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
337 if (copy_from_user(params
, _params
, sizeof(*params
))) {
341 err
= snd_pcm_hw_refine(substream
, params
);
342 if (copy_to_user(_params
, params
, sizeof(*params
))) {
351 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
356 return -1; /* invalid */
358 /* take 75% of period time as the deadline */
359 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
360 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
366 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
367 struct snd_pcm_hw_params
*params
)
369 struct snd_pcm_runtime
*runtime
;
372 snd_pcm_uframes_t frames
;
374 snd_assert(substream
!= NULL
, return -ENXIO
);
375 runtime
= substream
->runtime
;
376 snd_assert(runtime
!= NULL
, return -ENXIO
);
377 snd_pcm_stream_lock_irq(substream
);
378 switch (runtime
->status
->state
) {
379 case SNDRV_PCM_STATE_OPEN
:
380 case SNDRV_PCM_STATE_SETUP
:
381 case SNDRV_PCM_STATE_PREPARED
:
384 snd_pcm_stream_unlock_irq(substream
);
387 snd_pcm_stream_unlock_irq(substream
);
388 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
389 if (!substream
->oss
.oss
)
391 if (atomic_read(&substream
->mmap_count
))
395 err
= snd_pcm_hw_refine(substream
, params
);
399 err
= snd_pcm_hw_params_choose(substream
, params
);
403 if (substream
->ops
->hw_params
!= NULL
) {
404 err
= substream
->ops
->hw_params(substream
, params
);
409 runtime
->access
= params_access(params
);
410 runtime
->format
= params_format(params
);
411 runtime
->subformat
= params_subformat(params
);
412 runtime
->channels
= params_channels(params
);
413 runtime
->rate
= params_rate(params
);
414 runtime
->period_size
= params_period_size(params
);
415 runtime
->periods
= params_periods(params
);
416 runtime
->buffer_size
= params_buffer_size(params
);
417 runtime
->tick_time
= params_tick_time(params
);
418 runtime
->info
= params
->info
;
419 runtime
->rate_num
= params
->rate_num
;
420 runtime
->rate_den
= params
->rate_den
;
422 bits
= snd_pcm_format_physical_width(runtime
->format
);
423 runtime
->sample_bits
= bits
;
424 bits
*= runtime
->channels
;
425 runtime
->frame_bits
= bits
;
427 while (bits
% 8 != 0) {
431 runtime
->byte_align
= bits
/ 8;
432 runtime
->min_align
= frames
;
434 /* Default sw params */
435 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
436 runtime
->period_step
= 1;
437 runtime
->sleep_min
= 0;
438 runtime
->control
->avail_min
= runtime
->period_size
;
439 runtime
->xfer_align
= runtime
->period_size
;
440 runtime
->start_threshold
= 1;
441 runtime
->stop_threshold
= runtime
->buffer_size
;
442 runtime
->silence_threshold
= 0;
443 runtime
->silence_size
= 0;
444 runtime
->boundary
= runtime
->buffer_size
;
445 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
446 runtime
->boundary
*= 2;
448 snd_pcm_timer_resolution_change(substream
);
449 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
451 remove_acceptable_latency(substream
->latency_id
);
452 if ((usecs
= period_to_usecs(runtime
)) >= 0)
453 set_acceptable_latency(substream
->latency_id
, usecs
);
456 /* hardware might be unuseable from this time,
457 so we force application to retry to set
458 the correct hardware parameter settings */
459 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
460 if (substream
->ops
->hw_free
!= NULL
)
461 substream
->ops
->hw_free(substream
);
465 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
466 struct snd_pcm_hw_params __user
* _params
)
468 struct snd_pcm_hw_params
*params
;
471 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
476 if (copy_from_user(params
, _params
, sizeof(*params
))) {
480 err
= snd_pcm_hw_params(substream
, params
);
481 if (copy_to_user(_params
, params
, sizeof(*params
))) {
490 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
492 struct snd_pcm_runtime
*runtime
;
495 snd_assert(substream
!= NULL
, return -ENXIO
);
496 runtime
= substream
->runtime
;
497 snd_assert(runtime
!= NULL
, return -ENXIO
);
498 snd_pcm_stream_lock_irq(substream
);
499 switch (runtime
->status
->state
) {
500 case SNDRV_PCM_STATE_SETUP
:
501 case SNDRV_PCM_STATE_PREPARED
:
504 snd_pcm_stream_unlock_irq(substream
);
507 snd_pcm_stream_unlock_irq(substream
);
508 if (atomic_read(&substream
->mmap_count
))
510 if (substream
->ops
->hw_free
)
511 result
= substream
->ops
->hw_free(substream
);
512 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
513 remove_acceptable_latency(substream
->latency_id
);
517 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
518 struct snd_pcm_sw_params
*params
)
520 struct snd_pcm_runtime
*runtime
;
522 snd_assert(substream
!= NULL
, return -ENXIO
);
523 runtime
= substream
->runtime
;
524 snd_assert(runtime
!= NULL
, return -ENXIO
);
525 snd_pcm_stream_lock_irq(substream
);
526 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
527 snd_pcm_stream_unlock_irq(substream
);
530 snd_pcm_stream_unlock_irq(substream
);
532 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
534 if (params
->avail_min
== 0)
536 if (params
->xfer_align
== 0 ||
537 params
->xfer_align
% runtime
->min_align
!= 0)
539 if (params
->silence_size
>= runtime
->boundary
) {
540 if (params
->silence_threshold
!= 0)
543 if (params
->silence_size
> params
->silence_threshold
)
545 if (params
->silence_threshold
> runtime
->buffer_size
)
548 snd_pcm_stream_lock_irq(substream
);
549 runtime
->tstamp_mode
= params
->tstamp_mode
;
550 runtime
->sleep_min
= params
->sleep_min
;
551 runtime
->period_step
= params
->period_step
;
552 runtime
->control
->avail_min
= params
->avail_min
;
553 runtime
->start_threshold
= params
->start_threshold
;
554 runtime
->stop_threshold
= params
->stop_threshold
;
555 runtime
->silence_threshold
= params
->silence_threshold
;
556 runtime
->silence_size
= params
->silence_size
;
557 runtime
->xfer_align
= params
->xfer_align
;
558 params
->boundary
= runtime
->boundary
;
559 if (snd_pcm_running(substream
)) {
560 if (runtime
->sleep_min
)
561 snd_pcm_tick_prepare(substream
);
563 snd_pcm_tick_set(substream
, 0);
564 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
565 runtime
->silence_size
> 0)
566 snd_pcm_playback_silence(substream
, ULONG_MAX
);
567 wake_up(&runtime
->sleep
);
569 snd_pcm_stream_unlock_irq(substream
);
573 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
574 struct snd_pcm_sw_params __user
* _params
)
576 struct snd_pcm_sw_params params
;
578 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
580 err
= snd_pcm_sw_params(substream
, ¶ms
);
581 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
586 int snd_pcm_status(struct snd_pcm_substream
*substream
,
587 struct snd_pcm_status
*status
)
589 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
591 snd_pcm_stream_lock_irq(substream
);
592 status
->state
= runtime
->status
->state
;
593 status
->suspended_state
= runtime
->status
->suspended_state
;
594 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
596 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
597 if (snd_pcm_running(substream
)) {
598 snd_pcm_update_hw_ptr(substream
);
599 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
600 status
->tstamp
= runtime
->status
->tstamp
;
602 getnstimeofday(&status
->tstamp
);
604 getnstimeofday(&status
->tstamp
);
605 status
->appl_ptr
= runtime
->control
->appl_ptr
;
606 status
->hw_ptr
= runtime
->status
->hw_ptr
;
607 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
608 status
->avail
= snd_pcm_playback_avail(runtime
);
609 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
610 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
611 status
->delay
= runtime
->buffer_size
- status
->avail
;
615 status
->avail
= snd_pcm_capture_avail(runtime
);
616 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
617 status
->delay
= status
->avail
;
621 status
->avail_max
= runtime
->avail_max
;
622 status
->overrange
= runtime
->overrange
;
623 runtime
->avail_max
= 0;
624 runtime
->overrange
= 0;
626 snd_pcm_stream_unlock_irq(substream
);
630 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
631 struct snd_pcm_status __user
* _status
)
633 struct snd_pcm_status status
;
634 struct snd_pcm_runtime
*runtime
;
637 snd_assert(substream
!= NULL
, return -ENXIO
);
638 runtime
= substream
->runtime
;
639 memset(&status
, 0, sizeof(status
));
640 res
= snd_pcm_status(substream
, &status
);
643 if (copy_to_user(_status
, &status
, sizeof(status
)))
648 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
649 struct snd_pcm_channel_info
* info
)
651 struct snd_pcm_runtime
*runtime
;
652 unsigned int channel
;
654 snd_assert(substream
!= NULL
, return -ENXIO
);
655 channel
= info
->channel
;
656 runtime
= substream
->runtime
;
657 snd_pcm_stream_lock_irq(substream
);
658 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
659 snd_pcm_stream_unlock_irq(substream
);
662 snd_pcm_stream_unlock_irq(substream
);
663 if (channel
>= runtime
->channels
)
665 memset(info
, 0, sizeof(*info
));
666 info
->channel
= channel
;
667 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
670 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
671 struct snd_pcm_channel_info __user
* _info
)
673 struct snd_pcm_channel_info info
;
676 if (copy_from_user(&info
, _info
, sizeof(info
)))
678 res
= snd_pcm_channel_info(substream
, &info
);
681 if (copy_to_user(_info
, &info
, sizeof(info
)))
686 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
688 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
689 if (runtime
->trigger_master
== NULL
)
691 if (runtime
->trigger_master
== substream
) {
692 getnstimeofday(&runtime
->trigger_tstamp
);
694 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
695 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
697 runtime
->trigger_master
= NULL
;
701 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
702 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
703 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
704 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
708 * this functions is core for handling of linked stream
709 * Note: the stream state might be changed also on failure
710 * Note2: call with calling stream lock + link lock
712 static int snd_pcm_action_group(struct action_ops
*ops
,
713 struct snd_pcm_substream
*substream
,
714 int state
, int do_lock
)
716 struct list_head
*pos
;
717 struct snd_pcm_substream
*s
= NULL
;
718 struct snd_pcm_substream
*s1
;
721 snd_pcm_group_for_each(pos
, substream
) {
722 s
= snd_pcm_group_substream_entry(pos
);
723 if (do_lock
&& s
!= substream
)
724 spin_lock(&s
->self_group
.lock
);
725 res
= ops
->pre_action(s
, state
);
729 snd_pcm_group_for_each(pos
, substream
) {
730 s
= snd_pcm_group_substream_entry(pos
);
731 res
= ops
->do_action(s
, state
);
733 if (ops
->undo_action
) {
734 snd_pcm_group_for_each(pos
, substream
) {
735 s1
= snd_pcm_group_substream_entry(pos
);
736 if (s1
== s
) /* failed stream */
738 ops
->undo_action(s1
, state
);
741 s
= NULL
; /* unlock all */
745 snd_pcm_group_for_each(pos
, substream
) {
746 s
= snd_pcm_group_substream_entry(pos
);
747 ops
->post_action(s
, state
);
752 snd_pcm_group_for_each(pos
, substream
) {
753 s1
= snd_pcm_group_substream_entry(pos
);
755 spin_unlock(&s1
->self_group
.lock
);
756 if (s1
== s
) /* end */
764 * Note: call with stream lock
766 static int snd_pcm_action_single(struct action_ops
*ops
,
767 struct snd_pcm_substream
*substream
,
772 res
= ops
->pre_action(substream
, state
);
775 res
= ops
->do_action(substream
, state
);
777 ops
->post_action(substream
, state
);
778 else if (ops
->undo_action
)
779 ops
->undo_action(substream
, state
);
784 * Note: call with stream lock
786 static int snd_pcm_action(struct action_ops
*ops
,
787 struct snd_pcm_substream
*substream
,
792 if (snd_pcm_stream_linked(substream
)) {
793 if (!spin_trylock(&substream
->group
->lock
)) {
794 spin_unlock(&substream
->self_group
.lock
);
795 spin_lock(&substream
->group
->lock
);
796 spin_lock(&substream
->self_group
.lock
);
798 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
799 spin_unlock(&substream
->group
->lock
);
801 res
= snd_pcm_action_single(ops
, substream
, state
);
807 * Note: don't use any locks before
809 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
810 struct snd_pcm_substream
*substream
,
815 read_lock_irq(&snd_pcm_link_rwlock
);
816 if (snd_pcm_stream_linked(substream
)) {
817 spin_lock(&substream
->group
->lock
);
818 spin_lock(&substream
->self_group
.lock
);
819 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
820 spin_unlock(&substream
->self_group
.lock
);
821 spin_unlock(&substream
->group
->lock
);
823 spin_lock(&substream
->self_group
.lock
);
824 res
= snd_pcm_action_single(ops
, substream
, state
);
825 spin_unlock(&substream
->self_group
.lock
);
827 read_unlock_irq(&snd_pcm_link_rwlock
);
833 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
834 struct snd_pcm_substream
*substream
,
839 down_read(&snd_pcm_link_rwsem
);
840 if (snd_pcm_stream_linked(substream
))
841 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
843 res
= snd_pcm_action_single(ops
, substream
, state
);
844 up_read(&snd_pcm_link_rwsem
);
851 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
853 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
854 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
856 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
857 !snd_pcm_playback_data(substream
))
859 runtime
->trigger_master
= substream
;
863 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
865 if (substream
->runtime
->trigger_master
!= substream
)
867 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
870 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
872 if (substream
->runtime
->trigger_master
== substream
)
873 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
876 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
878 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
879 snd_pcm_trigger_tstamp(substream
);
880 runtime
->status
->state
= state
;
881 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
882 runtime
->silence_size
> 0)
883 snd_pcm_playback_silence(substream
, ULONG_MAX
);
884 if (runtime
->sleep_min
)
885 snd_pcm_tick_prepare(substream
);
886 if (substream
->timer
)
887 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
888 &runtime
->trigger_tstamp
);
891 static struct action_ops snd_pcm_action_start
= {
892 .pre_action
= snd_pcm_pre_start
,
893 .do_action
= snd_pcm_do_start
,
894 .undo_action
= snd_pcm_undo_start
,
895 .post_action
= snd_pcm_post_start
900 * @substream: the PCM substream instance
902 * Start all linked streams.
904 int snd_pcm_start(struct snd_pcm_substream
*substream
)
906 return snd_pcm_action(&snd_pcm_action_start
, substream
,
907 SNDRV_PCM_STATE_RUNNING
);
913 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
915 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
916 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
918 runtime
->trigger_master
= substream
;
922 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
924 if (substream
->runtime
->trigger_master
== substream
&&
925 snd_pcm_running(substream
))
926 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
927 return 0; /* unconditonally stop all substreams */
930 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
932 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
933 if (runtime
->status
->state
!= state
) {
934 snd_pcm_trigger_tstamp(substream
);
935 if (substream
->timer
)
936 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
937 &runtime
->trigger_tstamp
);
938 runtime
->status
->state
= state
;
939 snd_pcm_tick_set(substream
, 0);
941 wake_up(&runtime
->sleep
);
944 static struct action_ops snd_pcm_action_stop
= {
945 .pre_action
= snd_pcm_pre_stop
,
946 .do_action
= snd_pcm_do_stop
,
947 .post_action
= snd_pcm_post_stop
952 * @substream: the PCM substream instance
953 * @state: PCM state after stopping the stream
955 * Try to stop all running streams in the substream group.
956 * The state of each stream is changed to the given value after that unconditionally.
958 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
960 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
963 EXPORT_SYMBOL(snd_pcm_stop
);
967 * @substream: the PCM substream
969 * Stop the DMA only when the given stream is playback.
970 * The state is changed to SETUP.
971 * Unlike snd_pcm_stop(), this affects only the given stream.
973 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
975 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
976 SNDRV_PCM_STATE_SETUP
);
982 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
984 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
985 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
988 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
990 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
992 runtime
->trigger_master
= substream
;
996 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
998 if (substream
->runtime
->trigger_master
!= substream
)
1000 return substream
->ops
->trigger(substream
,
1001 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1002 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1005 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1007 if (substream
->runtime
->trigger_master
== substream
)
1008 substream
->ops
->trigger(substream
,
1009 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1010 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1013 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1015 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1016 snd_pcm_trigger_tstamp(substream
);
1018 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1019 if (substream
->timer
)
1020 snd_timer_notify(substream
->timer
,
1021 SNDRV_TIMER_EVENT_MPAUSE
,
1022 &runtime
->trigger_tstamp
);
1023 snd_pcm_tick_set(substream
, 0);
1024 wake_up(&runtime
->sleep
);
1026 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1027 if (runtime
->sleep_min
)
1028 snd_pcm_tick_prepare(substream
);
1029 if (substream
->timer
)
1030 snd_timer_notify(substream
->timer
,
1031 SNDRV_TIMER_EVENT_MCONTINUE
,
1032 &runtime
->trigger_tstamp
);
1036 static struct action_ops snd_pcm_action_pause
= {
1037 .pre_action
= snd_pcm_pre_pause
,
1038 .do_action
= snd_pcm_do_pause
,
1039 .undo_action
= snd_pcm_undo_pause
,
1040 .post_action
= snd_pcm_post_pause
1044 * Push/release the pause for all linked streams.
1046 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1048 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1054 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1056 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1057 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1059 runtime
->trigger_master
= substream
;
1063 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1065 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1066 if (runtime
->trigger_master
!= substream
)
1068 if (! snd_pcm_running(substream
))
1070 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1071 return 0; /* suspend unconditionally */
1074 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1076 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1077 snd_pcm_trigger_tstamp(substream
);
1078 if (substream
->timer
)
1079 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1080 &runtime
->trigger_tstamp
);
1081 runtime
->status
->suspended_state
= runtime
->status
->state
;
1082 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1083 snd_pcm_tick_set(substream
, 0);
1084 wake_up(&runtime
->sleep
);
1087 static struct action_ops snd_pcm_action_suspend
= {
1088 .pre_action
= snd_pcm_pre_suspend
,
1089 .do_action
= snd_pcm_do_suspend
,
1090 .post_action
= snd_pcm_post_suspend
1095 * @substream: the PCM substream
1097 * Trigger SUSPEND to all linked streams.
1098 * After this call, all streams are changed to SUSPENDED state.
1100 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1103 unsigned long flags
;
1108 snd_pcm_stream_lock_irqsave(substream
, flags
);
1109 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1110 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1114 EXPORT_SYMBOL(snd_pcm_suspend
);
1117 * snd_pcm_suspend_all
1118 * @pcm: the PCM instance
1120 * Trigger SUSPEND to all substreams in the given pcm.
1121 * After this call, all streams are changed to SUSPENDED state.
1123 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1125 struct snd_pcm_substream
*substream
;
1126 int stream
, err
= 0;
1131 for (stream
= 0; stream
< 2; stream
++) {
1132 for (substream
= pcm
->streams
[stream
].substream
;
1133 substream
; substream
= substream
->next
) {
1134 /* FIXME: the open/close code should lock this as well */
1135 if (substream
->runtime
== NULL
)
1137 err
= snd_pcm_suspend(substream
);
1138 if (err
< 0 && err
!= -EBUSY
)
1145 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1149 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1151 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1152 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1154 runtime
->trigger_master
= substream
;
1158 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1160 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1161 if (runtime
->trigger_master
!= substream
)
1163 /* DMA not running previously? */
1164 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1165 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1166 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1168 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1171 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1173 if (substream
->runtime
->trigger_master
== substream
&&
1174 snd_pcm_running(substream
))
1175 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1178 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1180 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1181 snd_pcm_trigger_tstamp(substream
);
1182 if (substream
->timer
)
1183 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1184 &runtime
->trigger_tstamp
);
1185 runtime
->status
->state
= runtime
->status
->suspended_state
;
1186 if (runtime
->sleep_min
)
1187 snd_pcm_tick_prepare(substream
);
1190 static struct action_ops snd_pcm_action_resume
= {
1191 .pre_action
= snd_pcm_pre_resume
,
1192 .do_action
= snd_pcm_do_resume
,
1193 .undo_action
= snd_pcm_undo_resume
,
1194 .post_action
= snd_pcm_post_resume
1197 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1199 struct snd_card
*card
= substream
->pcm
->card
;
1202 snd_power_lock(card
);
1203 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1204 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1205 snd_power_unlock(card
);
1211 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1216 #endif /* CONFIG_PM */
1221 * Change the RUNNING stream(s) to XRUN state.
1223 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1225 struct snd_card
*card
= substream
->pcm
->card
;
1226 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1229 snd_power_lock(card
);
1230 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1231 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1236 snd_pcm_stream_lock_irq(substream
);
1237 switch (runtime
->status
->state
) {
1238 case SNDRV_PCM_STATE_XRUN
:
1239 result
= 0; /* already there */
1241 case SNDRV_PCM_STATE_RUNNING
:
1242 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1247 snd_pcm_stream_unlock_irq(substream
);
1249 snd_power_unlock(card
);
1256 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1258 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1259 switch (runtime
->status
->state
) {
1260 case SNDRV_PCM_STATE_RUNNING
:
1261 case SNDRV_PCM_STATE_PREPARED
:
1262 case SNDRV_PCM_STATE_PAUSED
:
1263 case SNDRV_PCM_STATE_SUSPENDED
:
1270 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1272 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1273 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1276 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1277 runtime
->hw_ptr_base
= 0;
1278 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1279 runtime
->status
->hw_ptr
% runtime
->period_size
;
1280 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1281 runtime
->silence_filled
= 0;
1285 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1287 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1288 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1289 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1290 runtime
->silence_size
> 0)
1291 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1294 static struct action_ops snd_pcm_action_reset
= {
1295 .pre_action
= snd_pcm_pre_reset
,
1296 .do_action
= snd_pcm_do_reset
,
1297 .post_action
= snd_pcm_post_reset
1300 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1302 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1308 /* we use the second argument for updating f_flags */
1309 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1312 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1313 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1314 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1316 if (snd_pcm_running(substream
))
1318 substream
->f_flags
= f_flags
;
1322 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1325 err
= substream
->ops
->prepare(substream
);
1328 return snd_pcm_do_reset(substream
, 0);
1331 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1333 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1334 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1335 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1338 static struct action_ops snd_pcm_action_prepare
= {
1339 .pre_action
= snd_pcm_pre_prepare
,
1340 .do_action
= snd_pcm_do_prepare
,
1341 .post_action
= snd_pcm_post_prepare
1346 * @substream: the PCM substream instance
1347 * @file: file to refer f_flags
1349 * Prepare the PCM substream to be triggerable.
1351 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1355 struct snd_card
*card
= substream
->pcm
->card
;
1359 f_flags
= file
->f_flags
;
1361 f_flags
= substream
->f_flags
;
1363 snd_power_lock(card
);
1364 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1365 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1366 substream
, f_flags
);
1367 snd_power_unlock(card
);
1375 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1377 if (substream
->f_flags
& O_NONBLOCK
)
1379 substream
->runtime
->trigger_master
= substream
;
1383 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1385 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1386 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1387 switch (runtime
->status
->state
) {
1388 case SNDRV_PCM_STATE_PREPARED
:
1389 /* start playback stream if possible */
1390 if (! snd_pcm_playback_empty(substream
)) {
1391 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1392 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1395 case SNDRV_PCM_STATE_RUNNING
:
1396 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1402 /* stop running stream */
1403 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1404 int state
= snd_pcm_capture_avail(runtime
) > 0 ?
1405 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1406 snd_pcm_do_stop(substream
, state
);
1407 snd_pcm_post_stop(substream
, state
);
1413 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1417 static struct action_ops snd_pcm_action_drain_init
= {
1418 .pre_action
= snd_pcm_pre_drain_init
,
1419 .do_action
= snd_pcm_do_drain_init
,
1420 .post_action
= snd_pcm_post_drain_init
1424 struct snd_pcm_substream
*substream
;
1426 snd_pcm_uframes_t stop_threshold
;
1429 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1432 * Drain the stream(s).
1433 * When the substream is linked, sync until the draining of all playback streams
1435 * After this call, all streams are supposed to be either SETUP or DRAINING
1436 * (capture only) state.
1438 static int snd_pcm_drain(struct snd_pcm_substream
*substream
)
1440 struct snd_card
*card
;
1441 struct snd_pcm_runtime
*runtime
;
1442 struct list_head
*pos
;
1445 struct drain_rec
*drec
, drec_tmp
, *d
;
1447 snd_assert(substream
!= NULL
, return -ENXIO
);
1448 card
= substream
->pcm
->card
;
1449 runtime
= substream
->runtime
;
1451 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1454 snd_power_lock(card
);
1455 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1456 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1458 snd_power_unlock(card
);
1463 /* allocate temporary record for drain sync */
1464 down_read(&snd_pcm_link_rwsem
);
1465 if (snd_pcm_stream_linked(substream
)) {
1466 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1468 up_read(&snd_pcm_link_rwsem
);
1469 snd_power_unlock(card
);
1475 /* count only playback streams */
1477 snd_pcm_group_for_each(pos
, substream
) {
1478 struct snd_pcm_substream
*s
= snd_pcm_group_substream_entry(pos
);
1479 runtime
= s
->runtime
;
1480 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1481 d
= &drec
[num_drecs
++];
1483 init_waitqueue_entry(&d
->wait
, current
);
1484 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1485 /* stop_threshold fixup to avoid endless loop when
1486 * stop_threshold > buffer_size
1488 d
->stop_threshold
= runtime
->stop_threshold
;
1489 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1490 runtime
->stop_threshold
= runtime
->buffer_size
;
1493 up_read(&snd_pcm_link_rwsem
);
1495 snd_pcm_stream_lock_irq(substream
);
1497 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1498 snd_pcm_pause(substream
, 0);
1500 /* pre-start/stop - all running streams are changed to DRAINING state */
1501 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1503 snd_pcm_stream_unlock_irq(substream
);
1509 if (signal_pending(current
)) {
1510 result
= -ERESTARTSYS
;
1514 for (i
= 0; i
< num_drecs
; i
++) {
1515 runtime
= drec
[i
].substream
->runtime
;
1516 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1520 break; /* yes, all drained */
1522 set_current_state(TASK_INTERRUPTIBLE
);
1523 snd_pcm_stream_unlock_irq(substream
);
1524 snd_power_unlock(card
);
1525 tout
= schedule_timeout(10 * HZ
);
1526 snd_power_lock(card
);
1527 snd_pcm_stream_lock_irq(substream
);
1529 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1532 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1533 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1540 snd_pcm_stream_unlock_irq(substream
);
1543 for (i
= 0; i
< num_drecs
; i
++) {
1545 runtime
= d
->substream
->runtime
;
1546 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1547 runtime
->stop_threshold
= d
->stop_threshold
;
1550 if (drec
!= &drec_tmp
)
1552 snd_power_unlock(card
);
1560 * Immediately put all linked substreams into SETUP state.
1562 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1564 struct snd_pcm_runtime
*runtime
;
1565 struct snd_card
*card
;
1568 snd_assert(substream
!= NULL
, return -ENXIO
);
1569 runtime
= substream
->runtime
;
1570 card
= substream
->pcm
->card
;
1572 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1573 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1576 snd_power_lock(card
);
1577 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1578 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1583 snd_pcm_stream_lock_irq(substream
);
1585 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1586 snd_pcm_pause(substream
, 0);
1588 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1589 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1590 snd_pcm_stream_unlock_irq(substream
);
1592 snd_power_unlock(card
);
1597 /* WARNING: Don't forget to fput back the file */
1598 static struct file
*snd_pcm_file_fd(int fd
)
1601 struct inode
*inode
;
1607 inode
= file
->f_path
.dentry
->d_inode
;
1608 if (!S_ISCHR(inode
->i_mode
) ||
1609 imajor(inode
) != snd_major
) {
1613 minor
= iminor(inode
);
1614 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1615 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1625 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1629 struct snd_pcm_file
*pcm_file
;
1630 struct snd_pcm_substream
*substream1
;
1632 file
= snd_pcm_file_fd(fd
);
1635 pcm_file
= file
->private_data
;
1636 substream1
= pcm_file
->substream
;
1637 down_write(&snd_pcm_link_rwsem
);
1638 write_lock_irq(&snd_pcm_link_rwlock
);
1639 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1640 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1644 if (snd_pcm_stream_linked(substream1
)) {
1648 if (!snd_pcm_stream_linked(substream
)) {
1649 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1650 if (substream
->group
== NULL
) {
1654 spin_lock_init(&substream
->group
->lock
);
1655 INIT_LIST_HEAD(&substream
->group
->substreams
);
1656 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1657 substream
->group
->count
= 1;
1659 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1660 substream
->group
->count
++;
1661 substream1
->group
= substream
->group
;
1663 write_unlock_irq(&snd_pcm_link_rwlock
);
1664 up_write(&snd_pcm_link_rwsem
);
1669 static void relink_to_local(struct snd_pcm_substream
*substream
)
1671 substream
->group
= &substream
->self_group
;
1672 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1673 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1676 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1678 struct list_head
*pos
;
1681 down_write(&snd_pcm_link_rwsem
);
1682 write_lock_irq(&snd_pcm_link_rwlock
);
1683 if (!snd_pcm_stream_linked(substream
)) {
1687 list_del(&substream
->link_list
);
1688 substream
->group
->count
--;
1689 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1690 snd_pcm_group_for_each(pos
, substream
) {
1691 relink_to_local(snd_pcm_group_substream_entry(pos
));
1694 kfree(substream
->group
);
1696 relink_to_local(substream
);
1698 write_unlock_irq(&snd_pcm_link_rwlock
);
1699 up_write(&snd_pcm_link_rwsem
);
1706 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1707 struct snd_pcm_hw_rule
*rule
)
1709 struct snd_interval t
;
1710 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1711 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1712 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1715 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1716 struct snd_pcm_hw_rule
*rule
)
1718 struct snd_interval t
;
1719 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1720 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1721 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1724 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1725 struct snd_pcm_hw_rule
*rule
)
1727 struct snd_interval t
;
1728 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1729 hw_param_interval_c(params
, rule
->deps
[1]),
1730 (unsigned long) rule
->private, &t
);
1731 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1734 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1735 struct snd_pcm_hw_rule
*rule
)
1737 struct snd_interval t
;
1738 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1739 (unsigned long) rule
->private,
1740 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1741 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1744 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1745 struct snd_pcm_hw_rule
*rule
)
1748 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1750 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1752 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1754 if (! snd_mask_test(mask
, k
))
1756 bits
= snd_pcm_format_physical_width(k
);
1758 continue; /* ignore invalid formats */
1759 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1760 snd_mask_reset(&m
, k
);
1762 return snd_mask_refine(mask
, &m
);
1765 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1766 struct snd_pcm_hw_rule
*rule
)
1768 struct snd_interval t
;
1774 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1776 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1778 bits
= snd_pcm_format_physical_width(k
);
1780 continue; /* ignore invalid formats */
1781 if (t
.min
> (unsigned)bits
)
1783 if (t
.max
< (unsigned)bits
)
1787 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1790 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1791 #error "Change this table"
1794 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1795 48000, 64000, 88200, 96000, 176400, 192000 };
1797 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1798 struct snd_pcm_hw_rule
*rule
)
1800 struct snd_pcm_hardware
*hw
= rule
->private;
1801 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1802 ARRAY_SIZE(rates
), rates
, hw
->rates
);
1805 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1806 struct snd_pcm_hw_rule
*rule
)
1808 struct snd_interval t
;
1809 struct snd_pcm_substream
*substream
= rule
->private;
1811 t
.max
= substream
->buffer_bytes_max
;
1815 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1818 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1820 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1821 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1824 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1825 snd_mask_any(constrs_mask(constrs
, k
));
1828 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1829 snd_interval_any(constrs_interval(constrs
, k
));
1832 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1833 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1834 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1835 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1836 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1838 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1839 snd_pcm_hw_rule_format
, NULL
,
1840 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1843 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1844 snd_pcm_hw_rule_sample_bits
, NULL
,
1845 SNDRV_PCM_HW_PARAM_FORMAT
,
1846 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1849 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1850 snd_pcm_hw_rule_div
, NULL
,
1851 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1854 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1855 snd_pcm_hw_rule_mul
, NULL
,
1856 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1859 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1860 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1861 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1864 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1865 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1866 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1869 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1870 snd_pcm_hw_rule_div
, NULL
,
1871 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1874 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1875 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1876 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1879 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1880 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1881 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1884 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1885 snd_pcm_hw_rule_div
, NULL
,
1886 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1889 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1890 snd_pcm_hw_rule_div
, NULL
,
1891 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1894 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1895 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1896 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1899 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1900 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1901 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1904 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1905 snd_pcm_hw_rule_mul
, NULL
,
1906 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1909 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1910 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1911 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1914 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1915 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1916 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1919 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1920 snd_pcm_hw_rule_muldivk
, (void*) 8,
1921 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1924 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1925 snd_pcm_hw_rule_muldivk
, (void*) 8,
1926 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1929 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1930 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1931 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1934 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1935 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1936 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1942 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1944 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1945 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1947 unsigned int mask
= 0;
1949 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1950 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1951 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1952 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1953 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1954 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1955 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1956 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1957 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1958 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1959 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1961 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1962 snd_assert(err
>= 0, return -EINVAL
);
1964 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1965 snd_assert(err
>= 0, return -EINVAL
);
1967 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1968 snd_assert(err
>= 0, return -EINVAL
);
1970 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1971 hw
->channels_min
, hw
->channels_max
);
1972 snd_assert(err
>= 0, return -EINVAL
);
1974 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1975 hw
->rate_min
, hw
->rate_max
);
1976 snd_assert(err
>= 0, return -EINVAL
);
1978 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1979 hw
->period_bytes_min
, hw
->period_bytes_max
);
1980 snd_assert(err
>= 0, return -EINVAL
);
1982 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1983 hw
->periods_min
, hw
->periods_max
);
1984 snd_assert(err
>= 0, return -EINVAL
);
1986 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1987 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1988 snd_assert(err
>= 0, return -EINVAL
);
1990 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1991 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1992 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1997 if (runtime
->dma_bytes
) {
1998 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1999 snd_assert(err
>= 0, return -EINVAL
);
2002 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2003 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2004 snd_pcm_hw_rule_rate
, hw
,
2005 SNDRV_PCM_HW_PARAM_RATE
, -1);
2010 /* FIXME: this belong to lowlevel */
2011 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_TICK_TIME
,
2012 1000000 / HZ
, 1000000 / HZ
);
2013 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2018 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2020 snd_pcm_unlink(substream
);
2023 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2025 substream
->ref_count
--;
2026 if (substream
->ref_count
> 0)
2029 snd_pcm_drop(substream
);
2030 if (substream
->hw_opened
) {
2031 if (substream
->ops
->hw_free
!= NULL
)
2032 substream
->ops
->hw_free(substream
);
2033 substream
->ops
->close(substream
);
2034 substream
->hw_opened
= 0;
2036 if (substream
->pcm_release
) {
2037 substream
->pcm_release(substream
);
2038 substream
->pcm_release
= NULL
;
2040 snd_pcm_detach_substream(substream
);
2043 EXPORT_SYMBOL(snd_pcm_release_substream
);
2045 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2047 struct snd_pcm_substream
**rsubstream
)
2049 struct snd_pcm_substream
*substream
;
2052 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2055 if (substream
->ref_count
> 1) {
2056 *rsubstream
= substream
;
2060 err
= snd_pcm_hw_constraints_init(substream
);
2062 snd_printd("snd_pcm_hw_constraints_init failed\n");
2066 if ((err
= substream
->ops
->open(substream
)) < 0)
2069 substream
->hw_opened
= 1;
2071 err
= snd_pcm_hw_constraints_complete(substream
);
2073 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2077 *rsubstream
= substream
;
2081 snd_pcm_release_substream(substream
);
2085 EXPORT_SYMBOL(snd_pcm_open_substream
);
2087 static int snd_pcm_open_file(struct file
*file
,
2088 struct snd_pcm
*pcm
,
2090 struct snd_pcm_file
**rpcm_file
)
2092 struct snd_pcm_file
*pcm_file
;
2093 struct snd_pcm_substream
*substream
;
2094 struct snd_pcm_str
*str
;
2097 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
2100 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2104 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2105 if (pcm_file
== NULL
) {
2106 snd_pcm_release_substream(substream
);
2109 pcm_file
->substream
= substream
;
2110 if (substream
->ref_count
== 1) {
2111 str
= substream
->pstr
;
2112 substream
->file
= pcm_file
;
2113 substream
->pcm_release
= pcm_release_private
;
2115 file
->private_data
= pcm_file
;
2116 *rpcm_file
= pcm_file
;
2120 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2122 struct snd_pcm
*pcm
;
2124 pcm
= snd_lookup_minor_data(iminor(inode
),
2125 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2126 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2129 static int snd_pcm_capture_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_CAPTURE
);
2135 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2138 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2141 struct snd_pcm_file
*pcm_file
;
2148 err
= snd_card_file_add(pcm
->card
, file
);
2151 if (!try_module_get(pcm
->card
->module
)) {
2155 init_waitqueue_entry(&wait
, current
);
2156 add_wait_queue(&pcm
->open_wait
, &wait
);
2157 mutex_lock(&pcm
->open_mutex
);
2159 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2162 if (err
== -EAGAIN
) {
2163 if (file
->f_flags
& O_NONBLOCK
) {
2169 set_current_state(TASK_INTERRUPTIBLE
);
2170 mutex_unlock(&pcm
->open_mutex
);
2172 mutex_lock(&pcm
->open_mutex
);
2173 if (signal_pending(current
)) {
2178 remove_wait_queue(&pcm
->open_wait
, &wait
);
2179 mutex_unlock(&pcm
->open_mutex
);
2185 module_put(pcm
->card
->module
);
2187 snd_card_file_remove(pcm
->card
, file
);
2192 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2194 struct snd_pcm
*pcm
;
2195 struct snd_pcm_substream
*substream
;
2196 struct snd_pcm_file
*pcm_file
;
2198 pcm_file
= file
->private_data
;
2199 substream
= pcm_file
->substream
;
2200 snd_assert(substream
!= NULL
, return -ENXIO
);
2201 pcm
= substream
->pcm
;
2202 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2203 mutex_lock(&pcm
->open_mutex
);
2204 snd_pcm_release_substream(substream
);
2206 mutex_unlock(&pcm
->open_mutex
);
2207 wake_up(&pcm
->open_wait
);
2208 module_put(pcm
->card
->module
);
2209 snd_card_file_remove(pcm
->card
, file
);
2213 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2214 snd_pcm_uframes_t frames
)
2216 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2217 snd_pcm_sframes_t appl_ptr
;
2218 snd_pcm_sframes_t ret
;
2219 snd_pcm_sframes_t hw_avail
;
2224 snd_pcm_stream_lock_irq(substream
);
2225 switch (runtime
->status
->state
) {
2226 case SNDRV_PCM_STATE_PREPARED
:
2228 case SNDRV_PCM_STATE_DRAINING
:
2229 case SNDRV_PCM_STATE_RUNNING
:
2230 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2233 case SNDRV_PCM_STATE_XRUN
:
2241 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2242 if (hw_avail
<= 0) {
2246 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2249 frames
-= frames
% runtime
->xfer_align
;
2250 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2252 appl_ptr
+= runtime
->boundary
;
2253 runtime
->control
->appl_ptr
= appl_ptr
;
2254 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2256 snd_pcm_tick_prepare(substream
);
2259 snd_pcm_stream_unlock_irq(substream
);
2263 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2264 snd_pcm_uframes_t frames
)
2266 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2267 snd_pcm_sframes_t appl_ptr
;
2268 snd_pcm_sframes_t ret
;
2269 snd_pcm_sframes_t hw_avail
;
2274 snd_pcm_stream_lock_irq(substream
);
2275 switch (runtime
->status
->state
) {
2276 case SNDRV_PCM_STATE_PREPARED
:
2277 case SNDRV_PCM_STATE_DRAINING
:
2279 case SNDRV_PCM_STATE_RUNNING
:
2280 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2283 case SNDRV_PCM_STATE_XRUN
:
2291 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2292 if (hw_avail
<= 0) {
2296 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2299 frames
-= frames
% runtime
->xfer_align
;
2300 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2302 appl_ptr
+= runtime
->boundary
;
2303 runtime
->control
->appl_ptr
= appl_ptr
;
2304 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2306 snd_pcm_tick_prepare(substream
);
2309 snd_pcm_stream_unlock_irq(substream
);
2313 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2314 snd_pcm_uframes_t frames
)
2316 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2317 snd_pcm_sframes_t appl_ptr
;
2318 snd_pcm_sframes_t ret
;
2319 snd_pcm_sframes_t avail
;
2324 snd_pcm_stream_lock_irq(substream
);
2325 switch (runtime
->status
->state
) {
2326 case SNDRV_PCM_STATE_PREPARED
:
2327 case SNDRV_PCM_STATE_PAUSED
:
2329 case SNDRV_PCM_STATE_DRAINING
:
2330 case SNDRV_PCM_STATE_RUNNING
:
2331 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2334 case SNDRV_PCM_STATE_XRUN
:
2342 avail
= snd_pcm_playback_avail(runtime
);
2347 if (frames
> (snd_pcm_uframes_t
)avail
)
2350 frames
-= frames
% runtime
->xfer_align
;
2351 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2352 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2353 appl_ptr
-= runtime
->boundary
;
2354 runtime
->control
->appl_ptr
= appl_ptr
;
2355 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2357 snd_pcm_tick_prepare(substream
);
2360 snd_pcm_stream_unlock_irq(substream
);
2364 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2365 snd_pcm_uframes_t frames
)
2367 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2368 snd_pcm_sframes_t appl_ptr
;
2369 snd_pcm_sframes_t ret
;
2370 snd_pcm_sframes_t avail
;
2375 snd_pcm_stream_lock_irq(substream
);
2376 switch (runtime
->status
->state
) {
2377 case SNDRV_PCM_STATE_PREPARED
:
2378 case SNDRV_PCM_STATE_DRAINING
:
2379 case SNDRV_PCM_STATE_PAUSED
:
2381 case SNDRV_PCM_STATE_RUNNING
:
2382 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2385 case SNDRV_PCM_STATE_XRUN
:
2393 avail
= snd_pcm_capture_avail(runtime
);
2398 if (frames
> (snd_pcm_uframes_t
)avail
)
2401 frames
-= frames
% runtime
->xfer_align
;
2402 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2403 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2404 appl_ptr
-= runtime
->boundary
;
2405 runtime
->control
->appl_ptr
= appl_ptr
;
2406 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2408 snd_pcm_tick_prepare(substream
);
2411 snd_pcm_stream_unlock_irq(substream
);
2415 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2417 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2420 snd_pcm_stream_lock_irq(substream
);
2421 switch (runtime
->status
->state
) {
2422 case SNDRV_PCM_STATE_DRAINING
:
2423 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2425 case SNDRV_PCM_STATE_RUNNING
:
2426 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2429 case SNDRV_PCM_STATE_PREPARED
:
2430 case SNDRV_PCM_STATE_SUSPENDED
:
2433 case SNDRV_PCM_STATE_XRUN
:
2441 snd_pcm_stream_unlock_irq(substream
);
2445 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2446 snd_pcm_sframes_t __user
*res
)
2448 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2450 snd_pcm_sframes_t n
= 0;
2452 snd_pcm_stream_lock_irq(substream
);
2453 switch (runtime
->status
->state
) {
2454 case SNDRV_PCM_STATE_DRAINING
:
2455 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2457 case SNDRV_PCM_STATE_RUNNING
:
2458 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2461 case SNDRV_PCM_STATE_PREPARED
:
2462 case SNDRV_PCM_STATE_SUSPENDED
:
2464 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2465 n
= snd_pcm_playback_hw_avail(runtime
);
2467 n
= snd_pcm_capture_avail(runtime
);
2469 case SNDRV_PCM_STATE_XRUN
:
2477 snd_pcm_stream_unlock_irq(substream
);
2479 if (put_user(n
, res
))
2484 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2485 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2487 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2488 struct snd_pcm_sync_ptr sync_ptr
;
2489 volatile struct snd_pcm_mmap_status
*status
;
2490 volatile struct snd_pcm_mmap_control
*control
;
2493 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2494 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2496 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2498 status
= runtime
->status
;
2499 control
= runtime
->control
;
2500 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2501 err
= snd_pcm_hwsync(substream
);
2505 snd_pcm_stream_lock_irq(substream
);
2506 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2507 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2509 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2510 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2511 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2513 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2514 sync_ptr
.s
.status
.state
= status
->state
;
2515 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2516 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2517 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2518 snd_pcm_stream_unlock_irq(substream
);
2519 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2524 static int snd_pcm_common_ioctl1(struct file
*file
,
2525 struct snd_pcm_substream
*substream
,
2526 unsigned int cmd
, void __user
*arg
)
2528 snd_assert(substream
!= NULL
, return -ENXIO
);
2531 case SNDRV_PCM_IOCTL_PVERSION
:
2532 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2533 case SNDRV_PCM_IOCTL_INFO
:
2534 return snd_pcm_info_user(substream
, arg
);
2535 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2537 case SNDRV_PCM_IOCTL_HW_REFINE
:
2538 return snd_pcm_hw_refine_user(substream
, arg
);
2539 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2540 return snd_pcm_hw_params_user(substream
, arg
);
2541 case SNDRV_PCM_IOCTL_HW_FREE
:
2542 return snd_pcm_hw_free(substream
);
2543 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2544 return snd_pcm_sw_params_user(substream
, arg
);
2545 case SNDRV_PCM_IOCTL_STATUS
:
2546 return snd_pcm_status_user(substream
, arg
);
2547 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2548 return snd_pcm_channel_info_user(substream
, arg
);
2549 case SNDRV_PCM_IOCTL_PREPARE
:
2550 return snd_pcm_prepare(substream
, file
);
2551 case SNDRV_PCM_IOCTL_RESET
:
2552 return snd_pcm_reset(substream
);
2553 case SNDRV_PCM_IOCTL_START
:
2554 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2555 case SNDRV_PCM_IOCTL_LINK
:
2556 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2557 case SNDRV_PCM_IOCTL_UNLINK
:
2558 return snd_pcm_unlink(substream
);
2559 case SNDRV_PCM_IOCTL_RESUME
:
2560 return snd_pcm_resume(substream
);
2561 case SNDRV_PCM_IOCTL_XRUN
:
2562 return snd_pcm_xrun(substream
);
2563 case SNDRV_PCM_IOCTL_HWSYNC
:
2564 return snd_pcm_hwsync(substream
);
2565 case SNDRV_PCM_IOCTL_DELAY
:
2566 return snd_pcm_delay(substream
, arg
);
2567 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2568 return snd_pcm_sync_ptr(substream
, arg
);
2569 #ifdef CONFIG_SND_SUPPORT_OLD_API
2570 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2571 return snd_pcm_hw_refine_old_user(substream
, arg
);
2572 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2573 return snd_pcm_hw_params_old_user(substream
, arg
);
2575 case SNDRV_PCM_IOCTL_DRAIN
:
2576 return snd_pcm_drain(substream
);
2577 case SNDRV_PCM_IOCTL_DROP
:
2578 return snd_pcm_drop(substream
);
2579 case SNDRV_PCM_IOCTL_PAUSE
:
2582 snd_pcm_stream_lock_irq(substream
);
2583 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2584 snd_pcm_stream_unlock_irq(substream
);
2588 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2592 static int snd_pcm_playback_ioctl1(struct file
*file
,
2593 struct snd_pcm_substream
*substream
,
2594 unsigned int cmd
, void __user
*arg
)
2596 snd_assert(substream
!= NULL
, return -ENXIO
);
2597 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2599 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2601 struct snd_xferi xferi
;
2602 struct snd_xferi __user
*_xferi
= arg
;
2603 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2604 snd_pcm_sframes_t result
;
2605 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2607 if (put_user(0, &_xferi
->result
))
2609 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2611 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2612 __put_user(result
, &_xferi
->result
);
2613 return result
< 0 ? result
: 0;
2615 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2617 struct snd_xfern xfern
;
2618 struct snd_xfern __user
*_xfern
= arg
;
2619 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2621 snd_pcm_sframes_t result
;
2622 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2624 if (runtime
->channels
> 128)
2626 if (put_user(0, &_xfern
->result
))
2628 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2630 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2633 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2637 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2639 __put_user(result
, &_xfern
->result
);
2640 return result
< 0 ? result
: 0;
2642 case SNDRV_PCM_IOCTL_REWIND
:
2644 snd_pcm_uframes_t frames
;
2645 snd_pcm_uframes_t __user
*_frames
= arg
;
2646 snd_pcm_sframes_t result
;
2647 if (get_user(frames
, _frames
))
2649 if (put_user(0, _frames
))
2651 result
= snd_pcm_playback_rewind(substream
, frames
);
2652 __put_user(result
, _frames
);
2653 return result
< 0 ? result
: 0;
2655 case SNDRV_PCM_IOCTL_FORWARD
:
2657 snd_pcm_uframes_t frames
;
2658 snd_pcm_uframes_t __user
*_frames
= arg
;
2659 snd_pcm_sframes_t result
;
2660 if (get_user(frames
, _frames
))
2662 if (put_user(0, _frames
))
2664 result
= snd_pcm_playback_forward(substream
, frames
);
2665 __put_user(result
, _frames
);
2666 return result
< 0 ? result
: 0;
2669 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2672 static int snd_pcm_capture_ioctl1(struct file
*file
,
2673 struct snd_pcm_substream
*substream
,
2674 unsigned int cmd
, void __user
*arg
)
2676 snd_assert(substream
!= NULL
, return -ENXIO
);
2677 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2679 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2681 struct snd_xferi xferi
;
2682 struct snd_xferi __user
*_xferi
= arg
;
2683 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2684 snd_pcm_sframes_t result
;
2685 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2687 if (put_user(0, &_xferi
->result
))
2689 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2691 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2692 __put_user(result
, &_xferi
->result
);
2693 return result
< 0 ? result
: 0;
2695 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2697 struct snd_xfern xfern
;
2698 struct snd_xfern __user
*_xfern
= arg
;
2699 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2701 snd_pcm_sframes_t result
;
2702 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2704 if (runtime
->channels
> 128)
2706 if (put_user(0, &_xfern
->result
))
2708 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2710 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2713 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2717 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2719 __put_user(result
, &_xfern
->result
);
2720 return result
< 0 ? result
: 0;
2722 case SNDRV_PCM_IOCTL_REWIND
:
2724 snd_pcm_uframes_t frames
;
2725 snd_pcm_uframes_t __user
*_frames
= arg
;
2726 snd_pcm_sframes_t result
;
2727 if (get_user(frames
, _frames
))
2729 if (put_user(0, _frames
))
2731 result
= snd_pcm_capture_rewind(substream
, frames
);
2732 __put_user(result
, _frames
);
2733 return result
< 0 ? result
: 0;
2735 case SNDRV_PCM_IOCTL_FORWARD
:
2737 snd_pcm_uframes_t frames
;
2738 snd_pcm_uframes_t __user
*_frames
= arg
;
2739 snd_pcm_sframes_t result
;
2740 if (get_user(frames
, _frames
))
2742 if (put_user(0, _frames
))
2744 result
= snd_pcm_capture_forward(substream
, frames
);
2745 __put_user(result
, _frames
);
2746 return result
< 0 ? result
: 0;
2749 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2752 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2755 struct snd_pcm_file
*pcm_file
;
2757 pcm_file
= file
->private_data
;
2759 if (((cmd
>> 8) & 0xff) != 'A')
2762 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2763 (void __user
*)arg
);
2766 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2769 struct snd_pcm_file
*pcm_file
;
2771 pcm_file
= file
->private_data
;
2773 if (((cmd
>> 8) & 0xff) != 'A')
2776 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2777 (void __user
*)arg
);
2780 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2781 unsigned int cmd
, void *arg
)
2786 fs
= snd_enter_user();
2787 switch (substream
->stream
) {
2788 case SNDRV_PCM_STREAM_PLAYBACK
:
2789 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2790 (void __user
*)arg
);
2792 case SNDRV_PCM_STREAM_CAPTURE
:
2793 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2794 (void __user
*)arg
);
2804 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2806 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2809 struct snd_pcm_file
*pcm_file
;
2810 struct snd_pcm_substream
*substream
;
2811 struct snd_pcm_runtime
*runtime
;
2812 snd_pcm_sframes_t result
;
2814 pcm_file
= file
->private_data
;
2815 substream
= pcm_file
->substream
;
2816 snd_assert(substream
!= NULL
, return -ENXIO
);
2817 runtime
= substream
->runtime
;
2818 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2820 if (!frame_aligned(runtime
, count
))
2822 count
= bytes_to_frames(runtime
, count
);
2823 result
= snd_pcm_lib_read(substream
, buf
, count
);
2825 result
= frames_to_bytes(runtime
, result
);
2829 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2830 size_t count
, loff_t
* offset
)
2832 struct snd_pcm_file
*pcm_file
;
2833 struct snd_pcm_substream
*substream
;
2834 struct snd_pcm_runtime
*runtime
;
2835 snd_pcm_sframes_t result
;
2837 pcm_file
= file
->private_data
;
2838 substream
= pcm_file
->substream
;
2839 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2840 runtime
= substream
->runtime
;
2841 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2845 if (!frame_aligned(runtime
, count
)) {
2849 count
= bytes_to_frames(runtime
, count
);
2850 result
= snd_pcm_lib_write(substream
, buf
, count
);
2852 result
= frames_to_bytes(runtime
, result
);
2857 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2858 unsigned long nr_segs
, loff_t pos
)
2861 struct snd_pcm_file
*pcm_file
;
2862 struct snd_pcm_substream
*substream
;
2863 struct snd_pcm_runtime
*runtime
;
2864 snd_pcm_sframes_t result
;
2867 snd_pcm_uframes_t frames
;
2869 pcm_file
= iocb
->ki_filp
->private_data
;
2870 substream
= pcm_file
->substream
;
2871 snd_assert(substream
!= NULL
, return -ENXIO
);
2872 runtime
= substream
->runtime
;
2873 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2875 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2877 if (!frame_aligned(runtime
, iov
->iov_len
))
2879 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2880 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2883 for (i
= 0; i
< nr_segs
; ++i
)
2884 bufs
[i
] = iov
[i
].iov_base
;
2885 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2887 result
= frames_to_bytes(runtime
, result
);
2892 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2893 unsigned long nr_segs
, loff_t pos
)
2895 struct snd_pcm_file
*pcm_file
;
2896 struct snd_pcm_substream
*substream
;
2897 struct snd_pcm_runtime
*runtime
;
2898 snd_pcm_sframes_t result
;
2901 snd_pcm_uframes_t frames
;
2903 pcm_file
= iocb
->ki_filp
->private_data
;
2904 substream
= pcm_file
->substream
;
2905 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2906 runtime
= substream
->runtime
;
2907 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2911 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2912 !frame_aligned(runtime
, iov
->iov_len
)) {
2916 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2917 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2920 for (i
= 0; i
< nr_segs
; ++i
)
2921 bufs
[i
] = iov
[i
].iov_base
;
2922 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2924 result
= frames_to_bytes(runtime
, result
);
2930 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2932 struct snd_pcm_file
*pcm_file
;
2933 struct snd_pcm_substream
*substream
;
2934 struct snd_pcm_runtime
*runtime
;
2936 snd_pcm_uframes_t avail
;
2938 pcm_file
= file
->private_data
;
2940 substream
= pcm_file
->substream
;
2941 snd_assert(substream
!= NULL
, return -ENXIO
);
2942 runtime
= substream
->runtime
;
2944 poll_wait(file
, &runtime
->sleep
, wait
);
2946 snd_pcm_stream_lock_irq(substream
);
2947 avail
= snd_pcm_playback_avail(runtime
);
2948 switch (runtime
->status
->state
) {
2949 case SNDRV_PCM_STATE_RUNNING
:
2950 case SNDRV_PCM_STATE_PREPARED
:
2951 case SNDRV_PCM_STATE_PAUSED
:
2952 if (avail
>= runtime
->control
->avail_min
) {
2953 mask
= POLLOUT
| POLLWRNORM
;
2957 case SNDRV_PCM_STATE_DRAINING
:
2961 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2964 snd_pcm_stream_unlock_irq(substream
);
2968 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2970 struct snd_pcm_file
*pcm_file
;
2971 struct snd_pcm_substream
*substream
;
2972 struct snd_pcm_runtime
*runtime
;
2974 snd_pcm_uframes_t avail
;
2976 pcm_file
= file
->private_data
;
2978 substream
= pcm_file
->substream
;
2979 snd_assert(substream
!= NULL
, return -ENXIO
);
2980 runtime
= substream
->runtime
;
2982 poll_wait(file
, &runtime
->sleep
, wait
);
2984 snd_pcm_stream_lock_irq(substream
);
2985 avail
= snd_pcm_capture_avail(runtime
);
2986 switch (runtime
->status
->state
) {
2987 case SNDRV_PCM_STATE_RUNNING
:
2988 case SNDRV_PCM_STATE_PREPARED
:
2989 case SNDRV_PCM_STATE_PAUSED
:
2990 if (avail
>= runtime
->control
->avail_min
) {
2991 mask
= POLLIN
| POLLRDNORM
;
2996 case SNDRV_PCM_STATE_DRAINING
:
2998 mask
= POLLIN
| POLLRDNORM
;
3003 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3006 snd_pcm_stream_unlock_irq(substream
);
3015 * Only on coherent architectures, we can mmap the status and the control records
3016 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3018 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3020 * mmap status record
3022 static struct page
* snd_pcm_mmap_status_nopage(struct vm_area_struct
*area
,
3023 unsigned long address
, int *type
)
3025 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3026 struct snd_pcm_runtime
*runtime
;
3029 if (substream
== NULL
)
3030 return NOPAGE_SIGBUS
;
3031 runtime
= substream
->runtime
;
3032 page
= virt_to_page(runtime
->status
);
3035 *type
= VM_FAULT_MINOR
;
3039 static struct vm_operations_struct snd_pcm_vm_ops_status
=
3041 .nopage
= snd_pcm_mmap_status_nopage
,
3044 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3045 struct vm_area_struct
*area
)
3047 struct snd_pcm_runtime
*runtime
;
3049 if (!(area
->vm_flags
& VM_READ
))
3051 runtime
= substream
->runtime
;
3052 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3053 size
= area
->vm_end
- area
->vm_start
;
3054 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3056 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3057 area
->vm_private_data
= substream
;
3058 area
->vm_flags
|= VM_RESERVED
;
3063 * mmap control record
3065 static struct page
* snd_pcm_mmap_control_nopage(struct vm_area_struct
*area
,
3066 unsigned long address
, int *type
)
3068 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3069 struct snd_pcm_runtime
*runtime
;
3072 if (substream
== NULL
)
3073 return NOPAGE_SIGBUS
;
3074 runtime
= substream
->runtime
;
3075 page
= virt_to_page(runtime
->control
);
3078 *type
= VM_FAULT_MINOR
;
3082 static struct vm_operations_struct snd_pcm_vm_ops_control
=
3084 .nopage
= snd_pcm_mmap_control_nopage
,
3087 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3088 struct vm_area_struct
*area
)
3090 struct snd_pcm_runtime
*runtime
;
3092 if (!(area
->vm_flags
& VM_READ
))
3094 runtime
= substream
->runtime
;
3095 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3096 size
= area
->vm_end
- area
->vm_start
;
3097 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3099 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3100 area
->vm_private_data
= substream
;
3101 area
->vm_flags
|= VM_RESERVED
;
3104 #else /* ! coherent mmap */
3106 * don't support mmap for status and control records.
3108 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3109 struct vm_area_struct
*area
)
3113 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3114 struct vm_area_struct
*area
)
3118 #endif /* coherent mmap */
3121 * nopage callback for mmapping a RAM page
3123 static struct page
*snd_pcm_mmap_data_nopage(struct vm_area_struct
*area
,
3124 unsigned long address
, int *type
)
3126 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3127 struct snd_pcm_runtime
*runtime
;
3128 unsigned long offset
;
3133 if (substream
== NULL
)
3134 return NOPAGE_SIGBUS
;
3135 runtime
= substream
->runtime
;
3136 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3137 offset
+= address
- area
->vm_start
;
3138 snd_assert((offset
% PAGE_SIZE
) == 0, return NOPAGE_SIGBUS
);
3139 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3140 if (offset
> dma_bytes
- PAGE_SIZE
)
3141 return NOPAGE_SIGBUS
;
3142 if (substream
->ops
->page
) {
3143 page
= substream
->ops
->page(substream
, offset
);
3145 return NOPAGE_OOM
; /* XXX: is this really due to OOM? */
3147 vaddr
= runtime
->dma_area
+ offset
;
3148 page
= virt_to_page(vaddr
);
3152 *type
= VM_FAULT_MINOR
;
3156 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3158 .open
= snd_pcm_mmap_data_open
,
3159 .close
= snd_pcm_mmap_data_close
,
3160 .nopage
= snd_pcm_mmap_data_nopage
,
3164 * mmap the DMA buffer on RAM
3166 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3167 struct vm_area_struct
*area
)
3169 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3170 area
->vm_private_data
= substream
;
3171 area
->vm_flags
|= VM_RESERVED
;
3172 atomic_inc(&substream
->mmap_count
);
3177 * mmap the DMA buffer on I/O memory area
3179 #if SNDRV_PCM_INFO_MMAP_IOMEM
3180 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3182 .open
= snd_pcm_mmap_data_open
,
3183 .close
= snd_pcm_mmap_data_close
,
3186 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3187 struct vm_area_struct
*area
)
3190 unsigned long offset
;
3192 #ifdef pgprot_noncached
3193 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3195 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3196 area
->vm_private_data
= substream
;
3197 area
->vm_flags
|= VM_IO
;
3198 size
= area
->vm_end
- area
->vm_start
;
3199 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3200 if (io_remap_pfn_range(area
, area
->vm_start
,
3201 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3202 size
, area
->vm_page_prot
))
3204 atomic_inc(&substream
->mmap_count
);
3208 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3209 #endif /* SNDRV_PCM_INFO_MMAP */
3214 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3215 struct vm_area_struct
*area
)
3217 struct snd_pcm_runtime
*runtime
;
3219 unsigned long offset
;
3222 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3223 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3226 if (!(area
->vm_flags
& VM_READ
))
3229 runtime
= substream
->runtime
;
3230 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3231 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3233 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3235 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3236 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3238 size
= area
->vm_end
- area
->vm_start
;
3239 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3240 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3241 if ((size_t)size
> dma_bytes
)
3243 if (offset
> dma_bytes
- size
)
3246 if (substream
->ops
->mmap
)
3247 return substream
->ops
->mmap(substream
, area
);
3249 return snd_pcm_default_mmap(substream
, area
);
3252 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3254 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3256 struct snd_pcm_file
* pcm_file
;
3257 struct snd_pcm_substream
*substream
;
3258 unsigned long offset
;
3260 pcm_file
= file
->private_data
;
3261 substream
= pcm_file
->substream
;
3262 snd_assert(substream
!= NULL
, return -ENXIO
);
3264 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3266 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3267 if (pcm_file
->no_compat_mmap
)
3269 return snd_pcm_mmap_status(substream
, file
, area
);
3270 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3271 if (pcm_file
->no_compat_mmap
)
3273 return snd_pcm_mmap_control(substream
, file
, area
);
3275 return snd_pcm_mmap_data(substream
, file
, area
);
3280 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3282 struct snd_pcm_file
* pcm_file
;
3283 struct snd_pcm_substream
*substream
;
3284 struct snd_pcm_runtime
*runtime
;
3287 pcm_file
= file
->private_data
;
3288 substream
= pcm_file
->substream
;
3289 snd_assert(substream
!= NULL
, return -ENXIO
);
3290 runtime
= substream
->runtime
;
3292 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3301 #ifdef CONFIG_COMPAT
3302 #include "pcm_compat.c"
3304 #define snd_pcm_ioctl_compat NULL
3308 * To be removed helpers to keep binary compatibility
3311 #ifdef CONFIG_SND_SUPPORT_OLD_API
3312 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3313 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3315 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3316 struct snd_pcm_hw_params_old
*oparams
)
3320 memset(params
, 0, sizeof(*params
));
3321 params
->flags
= oparams
->flags
;
3322 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3323 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3324 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3325 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3326 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3327 params
->info
= oparams
->info
;
3328 params
->msbits
= oparams
->msbits
;
3329 params
->rate_num
= oparams
->rate_num
;
3330 params
->rate_den
= oparams
->rate_den
;
3331 params
->fifo_size
= oparams
->fifo_size
;
3334 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3335 struct snd_pcm_hw_params
*params
)
3339 memset(oparams
, 0, sizeof(*oparams
));
3340 oparams
->flags
= params
->flags
;
3341 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3342 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3343 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3344 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3345 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3346 oparams
->info
= params
->info
;
3347 oparams
->msbits
= params
->msbits
;
3348 oparams
->rate_num
= params
->rate_num
;
3349 oparams
->rate_den
= params
->rate_den
;
3350 oparams
->fifo_size
= params
->fifo_size
;
3353 static int snd_pcm_hw_refine_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
);
3371 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3375 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3376 err
= snd_pcm_hw_refine(substream
, params
);
3377 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3378 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3388 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3389 struct snd_pcm_hw_params_old __user
* _oparams
)
3391 struct snd_pcm_hw_params
*params
;
3392 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3395 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3400 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3405 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3409 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3410 err
= snd_pcm_hw_params(substream
, params
);
3411 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3412 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3421 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3427 const struct file_operations snd_pcm_f_ops
[2] = {
3429 .owner
= THIS_MODULE
,
3430 .write
= snd_pcm_write
,
3431 .aio_write
= snd_pcm_aio_write
,
3432 .open
= snd_pcm_playback_open
,
3433 .release
= snd_pcm_release
,
3434 .poll
= snd_pcm_playback_poll
,
3435 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3436 .compat_ioctl
= snd_pcm_ioctl_compat
,
3437 .mmap
= snd_pcm_mmap
,
3438 .fasync
= snd_pcm_fasync
,
3441 .owner
= THIS_MODULE
,
3442 .read
= snd_pcm_read
,
3443 .aio_read
= snd_pcm_aio_read
,
3444 .open
= snd_pcm_capture_open
,
3445 .release
= snd_pcm_release
,
3446 .poll
= snd_pcm_capture_poll
,
3447 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3448 .compat_ioctl
= snd_pcm_ioctl_compat
,
3449 .mmap
= snd_pcm_mmap
,
3450 .fasync
= snd_pcm_fasync
,