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 sndrv_pcm_hw_params_old
{
44 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
45 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
46 struct sndrv_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 sndrv_pcm_uframes_t fifo_size
;
55 unsigned char reserved
[64];
58 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct sndrv_pcm_hw_params_old)
59 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct sndrv_pcm_hw_params_old)
61 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old __user
* _oparams
);
62 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old __user
* _oparams
);
68 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
69 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
72 static inline mm_segment_t
snd_enter_user(void)
74 mm_segment_t fs
= get_fs();
79 static inline void snd_leave_user(mm_segment_t fs
)
86 int snd_pcm_info(snd_pcm_substream_t
* substream
, snd_pcm_info_t
*info
)
88 snd_pcm_runtime_t
* runtime
;
89 snd_pcm_t
*pcm
= substream
->pcm
;
90 snd_pcm_str_t
*pstr
= substream
->pstr
;
92 snd_assert(substream
!= NULL
, return -ENXIO
);
93 memset(info
, 0, sizeof(*info
));
94 info
->card
= pcm
->card
->number
;
95 info
->device
= pcm
->device
;
96 info
->stream
= substream
->stream
;
97 info
->subdevice
= substream
->number
;
98 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
99 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
100 info
->dev_class
= pcm
->dev_class
;
101 info
->dev_subclass
= pcm
->dev_subclass
;
102 info
->subdevices_count
= pstr
->substream_count
;
103 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
104 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
105 runtime
= substream
->runtime
;
106 /* AB: FIXME!!! This is definitely nonsense */
108 info
->sync
= runtime
->sync
;
109 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
114 int snd_pcm_info_user(snd_pcm_substream_t
* substream
, snd_pcm_info_t __user
* _info
)
116 snd_pcm_info_t
*info
;
119 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
122 err
= snd_pcm_info(substream
, info
);
124 if (copy_to_user(_info
, info
, sizeof(*info
)))
134 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
135 char *snd_pcm_hw_param_names
[] = {
139 HW_PARAM(SAMPLE_BITS
),
140 HW_PARAM(FRAME_BITS
),
143 HW_PARAM(PERIOD_TIME
),
144 HW_PARAM(PERIOD_SIZE
),
145 HW_PARAM(PERIOD_BYTES
),
147 HW_PARAM(BUFFER_TIME
),
148 HW_PARAM(BUFFER_SIZE
),
149 HW_PARAM(BUFFER_BYTES
),
154 int snd_pcm_hw_refine(snd_pcm_substream_t
*substream
,
155 snd_pcm_hw_params_t
*params
)
158 snd_pcm_hardware_t
*hw
;
159 snd_interval_t
*i
= NULL
;
160 snd_mask_t
*m
= NULL
;
161 snd_pcm_hw_constraints_t
*constrs
= &substream
->runtime
->hw_constraints
;
162 unsigned int rstamps
[constrs
->rules_num
];
163 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
164 unsigned int stamp
= 2;
168 params
->fifo_size
= 0;
169 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
171 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
172 params
->rate_num
= 0;
173 params
->rate_den
= 0;
176 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
177 m
= hw_param_mask(params
, k
);
178 if (snd_mask_empty(m
))
180 if (!(params
->rmask
& (1 << k
)))
183 printk("%s = ", snd_pcm_hw_param_names
[k
]);
184 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
186 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
188 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
191 params
->cmask
|= 1 << k
;
196 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
197 i
= hw_param_interval(params
, k
);
198 if (snd_interval_empty(i
))
200 if (!(params
->rmask
& (1 << k
)))
203 printk("%s = ", snd_pcm_hw_param_names
[k
]);
208 i
->openmin
? '(' : '[', i
->min
,
209 i
->max
, i
->openmax
? ')' : ']');
212 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
217 printk("%c%u %u%c\n",
218 i
->openmin
? '(' : '[', i
->min
,
219 i
->max
, i
->openmax
? ')' : ']');
222 params
->cmask
|= 1 << k
;
227 for (k
= 0; k
< constrs
->rules_num
; k
++)
229 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
230 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
233 for (k
= 0; k
< constrs
->rules_num
; k
++) {
234 snd_pcm_hw_rule_t
*r
= &constrs
->rules
[k
];
237 if (r
->cond
&& !(r
->cond
& params
->flags
))
239 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
240 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
248 printk("Rule %d [%p]: ", k
, r
->func
);
250 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
251 if (hw_is_mask(r
->var
)) {
252 m
= hw_param_mask(params
, r
->var
);
253 printk("%x", *m
->bits
);
255 i
= hw_param_interval(params
, r
->var
);
260 i
->openmin
? '(' : '[', i
->min
,
261 i
->max
, i
->openmax
? ')' : ']');
265 changed
= r
->func(params
, r
);
269 if (hw_is_mask(r
->var
))
270 printk("%x", *m
->bits
);
276 i
->openmin
? '(' : '[', i
->min
,
277 i
->max
, i
->openmax
? ')' : ']');
283 if (changed
&& r
->var
>= 0) {
284 params
->cmask
|= (1 << r
->var
);
285 vstamps
[r
->var
] = stamp
;
293 if (!params
->msbits
) {
294 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
295 if (snd_interval_single(i
))
296 params
->msbits
= snd_interval_value(i
);
299 if (!params
->rate_den
) {
300 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
301 if (snd_interval_single(i
)) {
302 params
->rate_num
= snd_interval_value(i
);
303 params
->rate_den
= 1;
307 hw
= &substream
->runtime
->hw
;
309 params
->info
= hw
->info
;
310 if (!params
->fifo_size
)
311 params
->fifo_size
= hw
->fifo_size
;
316 static int snd_pcm_hw_refine_user(snd_pcm_substream_t
* substream
, snd_pcm_hw_params_t __user
* _params
)
318 snd_pcm_hw_params_t
*params
;
321 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
326 if (copy_from_user(params
, _params
, sizeof(*params
))) {
330 err
= snd_pcm_hw_refine(substream
, params
);
331 if (copy_to_user(_params
, params
, sizeof(*params
))) {
340 static int snd_pcm_hw_params(snd_pcm_substream_t
*substream
,
341 snd_pcm_hw_params_t
*params
)
343 snd_pcm_runtime_t
*runtime
;
346 snd_pcm_uframes_t frames
;
348 snd_assert(substream
!= NULL
, return -ENXIO
);
349 runtime
= substream
->runtime
;
350 snd_assert(runtime
!= NULL
, return -ENXIO
);
351 snd_pcm_stream_lock_irq(substream
);
352 switch (runtime
->status
->state
) {
353 case SNDRV_PCM_STATE_OPEN
:
354 case SNDRV_PCM_STATE_SETUP
:
355 case SNDRV_PCM_STATE_PREPARED
:
358 snd_pcm_stream_unlock_irq(substream
);
361 snd_pcm_stream_unlock_irq(substream
);
362 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
363 if (!substream
->oss
.oss
)
365 if (atomic_read(&runtime
->mmap_count
))
369 err
= snd_pcm_hw_refine(substream
, params
);
373 err
= snd_pcm_hw_params_choose(substream
, params
);
377 if (substream
->ops
->hw_params
!= NULL
) {
378 err
= substream
->ops
->hw_params(substream
, params
);
383 runtime
->access
= params_access(params
);
384 runtime
->format
= params_format(params
);
385 runtime
->subformat
= params_subformat(params
);
386 runtime
->channels
= params_channels(params
);
387 runtime
->rate
= params_rate(params
);
388 runtime
->period_size
= params_period_size(params
);
389 runtime
->periods
= params_periods(params
);
390 runtime
->buffer_size
= params_buffer_size(params
);
391 runtime
->tick_time
= params_tick_time(params
);
392 runtime
->info
= params
->info
;
393 runtime
->rate_num
= params
->rate_num
;
394 runtime
->rate_den
= params
->rate_den
;
396 bits
= snd_pcm_format_physical_width(runtime
->format
);
397 runtime
->sample_bits
= bits
;
398 bits
*= runtime
->channels
;
399 runtime
->frame_bits
= bits
;
401 while (bits
% 8 != 0) {
405 runtime
->byte_align
= bits
/ 8;
406 runtime
->min_align
= frames
;
408 /* Default sw params */
409 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
410 runtime
->period_step
= 1;
411 runtime
->sleep_min
= 0;
412 runtime
->control
->avail_min
= runtime
->period_size
;
413 runtime
->xfer_align
= runtime
->period_size
;
414 runtime
->start_threshold
= 1;
415 runtime
->stop_threshold
= runtime
->buffer_size
;
416 runtime
->silence_threshold
= 0;
417 runtime
->silence_size
= 0;
418 runtime
->boundary
= runtime
->buffer_size
;
419 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
420 runtime
->boundary
*= 2;
422 snd_pcm_timer_resolution_change(substream
);
423 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
426 /* hardware might be unuseable from this time,
427 so we force application to retry to set
428 the correct hardware parameter settings */
429 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
430 if (substream
->ops
->hw_free
!= NULL
)
431 substream
->ops
->hw_free(substream
);
435 static int snd_pcm_hw_params_user(snd_pcm_substream_t
* substream
, snd_pcm_hw_params_t __user
* _params
)
437 snd_pcm_hw_params_t
*params
;
440 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
445 if (copy_from_user(params
, _params
, sizeof(*params
))) {
449 err
= snd_pcm_hw_params(substream
, params
);
450 if (copy_to_user(_params
, params
, sizeof(*params
))) {
459 static int snd_pcm_hw_free(snd_pcm_substream_t
* substream
)
461 snd_pcm_runtime_t
*runtime
;
464 snd_assert(substream
!= NULL
, return -ENXIO
);
465 runtime
= substream
->runtime
;
466 snd_assert(runtime
!= NULL
, return -ENXIO
);
467 snd_pcm_stream_lock_irq(substream
);
468 switch (runtime
->status
->state
) {
469 case SNDRV_PCM_STATE_SETUP
:
470 case SNDRV_PCM_STATE_PREPARED
:
473 snd_pcm_stream_unlock_irq(substream
);
476 snd_pcm_stream_unlock_irq(substream
);
477 if (atomic_read(&runtime
->mmap_count
))
479 if (substream
->ops
->hw_free
)
480 result
= substream
->ops
->hw_free(substream
);
481 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
485 static int snd_pcm_sw_params(snd_pcm_substream_t
* substream
, snd_pcm_sw_params_t
*params
)
487 snd_pcm_runtime_t
*runtime
;
489 snd_assert(substream
!= NULL
, return -ENXIO
);
490 runtime
= substream
->runtime
;
491 snd_assert(runtime
!= NULL
, return -ENXIO
);
492 snd_pcm_stream_lock_irq(substream
);
493 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
494 snd_pcm_stream_unlock_irq(substream
);
497 snd_pcm_stream_unlock_irq(substream
);
499 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
501 if (params
->avail_min
== 0)
503 if (params
->xfer_align
== 0 ||
504 params
->xfer_align
% runtime
->min_align
!= 0)
506 if (params
->silence_size
>= runtime
->boundary
) {
507 if (params
->silence_threshold
!= 0)
510 if (params
->silence_size
> params
->silence_threshold
)
512 if (params
->silence_threshold
> runtime
->buffer_size
)
515 snd_pcm_stream_lock_irq(substream
);
516 runtime
->tstamp_mode
= params
->tstamp_mode
;
517 runtime
->sleep_min
= params
->sleep_min
;
518 runtime
->period_step
= params
->period_step
;
519 runtime
->control
->avail_min
= params
->avail_min
;
520 runtime
->start_threshold
= params
->start_threshold
;
521 runtime
->stop_threshold
= params
->stop_threshold
;
522 runtime
->silence_threshold
= params
->silence_threshold
;
523 runtime
->silence_size
= params
->silence_size
;
524 runtime
->xfer_align
= params
->xfer_align
;
525 params
->boundary
= runtime
->boundary
;
526 if (snd_pcm_running(substream
)) {
527 if (runtime
->sleep_min
)
528 snd_pcm_tick_prepare(substream
);
530 snd_pcm_tick_set(substream
, 0);
531 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
532 runtime
->silence_size
> 0)
533 snd_pcm_playback_silence(substream
, ULONG_MAX
);
534 wake_up(&runtime
->sleep
);
536 snd_pcm_stream_unlock_irq(substream
);
540 static int snd_pcm_sw_params_user(snd_pcm_substream_t
* substream
, snd_pcm_sw_params_t __user
* _params
)
542 snd_pcm_sw_params_t params
;
544 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
546 err
= snd_pcm_sw_params(substream
, ¶ms
);
547 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
552 int snd_pcm_status(snd_pcm_substream_t
*substream
,
553 snd_pcm_status_t
*status
)
555 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
557 snd_pcm_stream_lock_irq(substream
);
558 status
->state
= runtime
->status
->state
;
559 status
->suspended_state
= runtime
->status
->suspended_state
;
560 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
562 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
563 if (snd_pcm_running(substream
)) {
564 snd_pcm_update_hw_ptr(substream
);
565 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
566 status
->tstamp
= runtime
->status
->tstamp
;
568 getnstimeofday(&status
->tstamp
);
570 getnstimeofday(&status
->tstamp
);
571 status
->appl_ptr
= runtime
->control
->appl_ptr
;
572 status
->hw_ptr
= runtime
->status
->hw_ptr
;
573 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
574 status
->avail
= snd_pcm_playback_avail(runtime
);
575 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
576 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
577 status
->delay
= runtime
->buffer_size
- status
->avail
;
581 status
->avail
= snd_pcm_capture_avail(runtime
);
582 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
583 status
->delay
= status
->avail
;
587 status
->avail_max
= runtime
->avail_max
;
588 status
->overrange
= runtime
->overrange
;
589 runtime
->avail_max
= 0;
590 runtime
->overrange
= 0;
592 snd_pcm_stream_unlock_irq(substream
);
596 static int snd_pcm_status_user(snd_pcm_substream_t
* substream
, snd_pcm_status_t __user
* _status
)
598 snd_pcm_status_t status
;
599 snd_pcm_runtime_t
*runtime
;
602 snd_assert(substream
!= NULL
, return -ENXIO
);
603 runtime
= substream
->runtime
;
604 memset(&status
, 0, sizeof(status
));
605 res
= snd_pcm_status(substream
, &status
);
608 if (copy_to_user(_status
, &status
, sizeof(status
)))
613 static int snd_pcm_channel_info(snd_pcm_substream_t
* substream
, snd_pcm_channel_info_t
* info
)
615 snd_pcm_runtime_t
*runtime
;
616 unsigned int channel
;
618 snd_assert(substream
!= NULL
, return -ENXIO
);
619 channel
= info
->channel
;
620 runtime
= substream
->runtime
;
621 snd_pcm_stream_lock_irq(substream
);
622 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
623 snd_pcm_stream_unlock_irq(substream
);
626 snd_pcm_stream_unlock_irq(substream
);
627 if (channel
>= runtime
->channels
)
629 memset(info
, 0, sizeof(*info
));
630 info
->channel
= channel
;
631 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
634 static int snd_pcm_channel_info_user(snd_pcm_substream_t
* substream
, snd_pcm_channel_info_t __user
* _info
)
636 snd_pcm_channel_info_t info
;
639 if (copy_from_user(&info
, _info
, sizeof(info
)))
641 res
= snd_pcm_channel_info(substream
, &info
);
644 if (copy_to_user(_info
, &info
, sizeof(info
)))
649 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t
*substream
)
651 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
652 if (runtime
->trigger_master
== NULL
)
654 if (runtime
->trigger_master
== substream
) {
655 getnstimeofday(&runtime
->trigger_tstamp
);
657 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
658 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
660 runtime
->trigger_master
= NULL
;
664 int (*pre_action
)(snd_pcm_substream_t
*substream
, int state
);
665 int (*do_action
)(snd_pcm_substream_t
*substream
, int state
);
666 void (*undo_action
)(snd_pcm_substream_t
*substream
, int state
);
667 void (*post_action
)(snd_pcm_substream_t
*substream
, int state
);
671 * this functions is core for handling of linked stream
672 * Note: the stream state might be changed also on failure
673 * Note2: call with calling stream lock + link lock
675 static int snd_pcm_action_group(struct action_ops
*ops
,
676 snd_pcm_substream_t
*substream
,
677 int state
, int do_lock
)
679 struct list_head
*pos
;
680 snd_pcm_substream_t
*s
= NULL
;
681 snd_pcm_substream_t
*s1
;
684 snd_pcm_group_for_each(pos
, substream
) {
685 s
= snd_pcm_group_substream_entry(pos
);
686 if (do_lock
&& s
!= substream
)
687 spin_lock(&s
->self_group
.lock
);
688 res
= ops
->pre_action(s
, state
);
692 snd_pcm_group_for_each(pos
, substream
) {
693 s
= snd_pcm_group_substream_entry(pos
);
694 res
= ops
->do_action(s
, state
);
696 if (ops
->undo_action
) {
697 snd_pcm_group_for_each(pos
, substream
) {
698 s1
= snd_pcm_group_substream_entry(pos
);
699 if (s1
== s
) /* failed stream */
701 ops
->undo_action(s1
, state
);
704 s
= NULL
; /* unlock all */
708 snd_pcm_group_for_each(pos
, substream
) {
709 s
= snd_pcm_group_substream_entry(pos
);
710 ops
->post_action(s
, state
);
715 snd_pcm_group_for_each(pos
, substream
) {
716 s1
= snd_pcm_group_substream_entry(pos
);
718 spin_unlock(&s1
->self_group
.lock
);
719 if (s1
== s
) /* end */
727 * Note: call with stream lock
729 static int snd_pcm_action_single(struct action_ops
*ops
,
730 snd_pcm_substream_t
*substream
,
735 res
= ops
->pre_action(substream
, state
);
738 res
= ops
->do_action(substream
, state
);
740 ops
->post_action(substream
, state
);
741 else if (ops
->undo_action
)
742 ops
->undo_action(substream
, state
);
747 * Note: call with stream lock
749 static int snd_pcm_action(struct action_ops
*ops
,
750 snd_pcm_substream_t
*substream
,
755 if (snd_pcm_stream_linked(substream
)) {
756 if (!spin_trylock(&substream
->group
->lock
)) {
757 spin_unlock(&substream
->self_group
.lock
);
758 spin_lock(&substream
->group
->lock
);
759 spin_lock(&substream
->self_group
.lock
);
761 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
762 spin_unlock(&substream
->group
->lock
);
764 res
= snd_pcm_action_single(ops
, substream
, state
);
770 * Note: don't use any locks before
772 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
773 snd_pcm_substream_t
*substream
,
778 read_lock_irq(&snd_pcm_link_rwlock
);
779 if (snd_pcm_stream_linked(substream
)) {
780 spin_lock(&substream
->group
->lock
);
781 spin_lock(&substream
->self_group
.lock
);
782 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
783 spin_unlock(&substream
->self_group
.lock
);
784 spin_unlock(&substream
->group
->lock
);
786 spin_lock(&substream
->self_group
.lock
);
787 res
= snd_pcm_action_single(ops
, substream
, state
);
788 spin_unlock(&substream
->self_group
.lock
);
790 read_unlock_irq(&snd_pcm_link_rwlock
);
796 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
797 snd_pcm_substream_t
*substream
,
802 down_read(&snd_pcm_link_rwsem
);
803 if (snd_pcm_stream_linked(substream
))
804 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
806 res
= snd_pcm_action_single(ops
, substream
, state
);
807 up_read(&snd_pcm_link_rwsem
);
814 static int snd_pcm_pre_start(snd_pcm_substream_t
*substream
, int state
)
816 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
817 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
819 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
820 !snd_pcm_playback_data(substream
))
822 runtime
->trigger_master
= substream
;
826 static int snd_pcm_do_start(snd_pcm_substream_t
*substream
, int state
)
828 if (substream
->runtime
->trigger_master
!= substream
)
830 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
833 static void snd_pcm_undo_start(snd_pcm_substream_t
*substream
, int state
)
835 if (substream
->runtime
->trigger_master
== substream
)
836 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
839 static void snd_pcm_post_start(snd_pcm_substream_t
*substream
, int state
)
841 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
842 snd_pcm_trigger_tstamp(substream
);
843 runtime
->status
->state
= state
;
844 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
845 runtime
->silence_size
> 0)
846 snd_pcm_playback_silence(substream
, ULONG_MAX
);
847 if (runtime
->sleep_min
)
848 snd_pcm_tick_prepare(substream
);
849 if (substream
->timer
)
850 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
, &runtime
->trigger_tstamp
);
853 static struct action_ops snd_pcm_action_start
= {
854 .pre_action
= snd_pcm_pre_start
,
855 .do_action
= snd_pcm_do_start
,
856 .undo_action
= snd_pcm_undo_start
,
857 .post_action
= snd_pcm_post_start
862 * @substream: the PCM substream instance
864 * Start all linked streams.
866 int snd_pcm_start(snd_pcm_substream_t
*substream
)
868 return snd_pcm_action(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
874 static int snd_pcm_pre_stop(snd_pcm_substream_t
*substream
, int state
)
876 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
877 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
879 runtime
->trigger_master
= substream
;
883 static int snd_pcm_do_stop(snd_pcm_substream_t
*substream
, int state
)
885 if (substream
->runtime
->trigger_master
== substream
&&
886 snd_pcm_running(substream
))
887 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
888 return 0; /* unconditonally stop all substreams */
891 static void snd_pcm_post_stop(snd_pcm_substream_t
*substream
, int state
)
893 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
894 if (runtime
->status
->state
!= state
) {
895 snd_pcm_trigger_tstamp(substream
);
896 if (substream
->timer
)
897 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
, &runtime
->trigger_tstamp
);
898 runtime
->status
->state
= state
;
899 snd_pcm_tick_set(substream
, 0);
901 wake_up(&runtime
->sleep
);
904 static struct action_ops snd_pcm_action_stop
= {
905 .pre_action
= snd_pcm_pre_stop
,
906 .do_action
= snd_pcm_do_stop
,
907 .post_action
= snd_pcm_post_stop
912 * @substream: the PCM substream instance
913 * @state: PCM state after stopping the stream
915 * Try to stop all running streams in the substream group.
916 * The state of each stream is changed to the given value after that unconditionally.
918 int snd_pcm_stop(snd_pcm_substream_t
*substream
, int state
)
920 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
925 * @substream: the PCM substream
927 * Stop the DMA only when the given stream is playback.
928 * The state is changed to SETUP.
929 * Unlike snd_pcm_stop(), this affects only the given stream.
931 int snd_pcm_drain_done(snd_pcm_substream_t
*substream
)
933 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
, SNDRV_PCM_STATE_SETUP
);
939 static int snd_pcm_pre_pause(snd_pcm_substream_t
*substream
, int push
)
941 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
942 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
945 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
947 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
949 runtime
->trigger_master
= substream
;
953 static int snd_pcm_do_pause(snd_pcm_substream_t
*substream
, int push
)
955 if (substream
->runtime
->trigger_master
!= substream
)
957 return substream
->ops
->trigger(substream
,
958 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
959 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
962 static void snd_pcm_undo_pause(snd_pcm_substream_t
*substream
, int push
)
964 if (substream
->runtime
->trigger_master
== substream
)
965 substream
->ops
->trigger(substream
,
966 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
967 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
970 static void snd_pcm_post_pause(snd_pcm_substream_t
*substream
, int push
)
972 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
973 snd_pcm_trigger_tstamp(substream
);
975 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
976 if (substream
->timer
)
977 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MPAUSE
, &runtime
->trigger_tstamp
);
978 snd_pcm_tick_set(substream
, 0);
979 wake_up(&runtime
->sleep
);
981 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
982 if (runtime
->sleep_min
)
983 snd_pcm_tick_prepare(substream
);
984 if (substream
->timer
)
985 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MCONTINUE
, &runtime
->trigger_tstamp
);
989 static struct action_ops snd_pcm_action_pause
= {
990 .pre_action
= snd_pcm_pre_pause
,
991 .do_action
= snd_pcm_do_pause
,
992 .undo_action
= snd_pcm_undo_pause
,
993 .post_action
= snd_pcm_post_pause
997 * Push/release the pause for all linked streams.
999 static int snd_pcm_pause(snd_pcm_substream_t
*substream
, int push
)
1001 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1007 static int snd_pcm_pre_suspend(snd_pcm_substream_t
*substream
, int state
)
1009 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1010 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1012 runtime
->trigger_master
= substream
;
1016 static int snd_pcm_do_suspend(snd_pcm_substream_t
*substream
, int state
)
1018 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1019 if (runtime
->trigger_master
!= substream
)
1021 if (! snd_pcm_running(substream
))
1023 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1024 return 0; /* suspend unconditionally */
1027 static void snd_pcm_post_suspend(snd_pcm_substream_t
*substream
, int state
)
1029 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1030 snd_pcm_trigger_tstamp(substream
);
1031 if (substream
->timer
)
1032 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
, &runtime
->trigger_tstamp
);
1033 runtime
->status
->suspended_state
= runtime
->status
->state
;
1034 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1035 snd_pcm_tick_set(substream
, 0);
1036 wake_up(&runtime
->sleep
);
1039 static struct action_ops snd_pcm_action_suspend
= {
1040 .pre_action
= snd_pcm_pre_suspend
,
1041 .do_action
= snd_pcm_do_suspend
,
1042 .post_action
= snd_pcm_post_suspend
1047 * @substream: the PCM substream
1049 * Trigger SUSPEND to all linked streams.
1050 * After this call, all streams are changed to SUSPENDED state.
1052 int snd_pcm_suspend(snd_pcm_substream_t
*substream
)
1055 unsigned long flags
;
1057 snd_pcm_stream_lock_irqsave(substream
, flags
);
1058 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1059 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1064 * snd_pcm_suspend_all
1065 * @pcm: the PCM instance
1067 * Trigger SUSPEND to all substreams in the given pcm.
1068 * After this call, all streams are changed to SUSPENDED state.
1070 int snd_pcm_suspend_all(snd_pcm_t
*pcm
)
1072 snd_pcm_substream_t
*substream
;
1073 int stream
, err
= 0;
1075 for (stream
= 0; stream
< 2; stream
++) {
1076 for (substream
= pcm
->streams
[stream
].substream
; substream
; substream
= substream
->next
) {
1077 /* FIXME: the open/close code should lock this as well */
1078 if (substream
->runtime
== NULL
)
1080 err
= snd_pcm_suspend(substream
);
1081 if (err
< 0 && err
!= -EBUSY
)
1090 static int snd_pcm_pre_resume(snd_pcm_substream_t
*substream
, int state
)
1092 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1093 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1095 runtime
->trigger_master
= substream
;
1099 static int snd_pcm_do_resume(snd_pcm_substream_t
*substream
, int state
)
1101 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1102 if (runtime
->trigger_master
!= substream
)
1104 /* DMA not running previously? */
1105 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1106 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1107 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1109 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1112 static void snd_pcm_undo_resume(snd_pcm_substream_t
*substream
, int state
)
1114 if (substream
->runtime
->trigger_master
== substream
&&
1115 snd_pcm_running(substream
))
1116 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1119 static void snd_pcm_post_resume(snd_pcm_substream_t
*substream
, int state
)
1121 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1122 snd_pcm_trigger_tstamp(substream
);
1123 if (substream
->timer
)
1124 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
, &runtime
->trigger_tstamp
);
1125 runtime
->status
->state
= runtime
->status
->suspended_state
;
1126 if (runtime
->sleep_min
)
1127 snd_pcm_tick_prepare(substream
);
1130 static struct action_ops snd_pcm_action_resume
= {
1131 .pre_action
= snd_pcm_pre_resume
,
1132 .do_action
= snd_pcm_do_resume
,
1133 .undo_action
= snd_pcm_undo_resume
,
1134 .post_action
= snd_pcm_post_resume
1137 static int snd_pcm_resume(snd_pcm_substream_t
*substream
)
1139 snd_card_t
*card
= substream
->pcm
->card
;
1142 snd_power_lock(card
);
1143 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
)) >= 0)
1144 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1145 snd_power_unlock(card
);
1151 static int snd_pcm_resume(snd_pcm_substream_t
*substream
)
1156 #endif /* CONFIG_PM */
1161 * Change the RUNNING stream(s) to XRUN state.
1163 static int snd_pcm_xrun(snd_pcm_substream_t
*substream
)
1165 snd_card_t
*card
= substream
->pcm
->card
;
1166 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1169 snd_power_lock(card
);
1170 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1171 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1176 snd_pcm_stream_lock_irq(substream
);
1177 switch (runtime
->status
->state
) {
1178 case SNDRV_PCM_STATE_XRUN
:
1179 result
= 0; /* already there */
1181 case SNDRV_PCM_STATE_RUNNING
:
1182 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1187 snd_pcm_stream_unlock_irq(substream
);
1189 snd_power_unlock(card
);
1196 static int snd_pcm_pre_reset(snd_pcm_substream_t
* substream
, int state
)
1198 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1199 switch (runtime
->status
->state
) {
1200 case SNDRV_PCM_STATE_RUNNING
:
1201 case SNDRV_PCM_STATE_PREPARED
:
1202 case SNDRV_PCM_STATE_PAUSED
:
1203 case SNDRV_PCM_STATE_SUSPENDED
:
1210 static int snd_pcm_do_reset(snd_pcm_substream_t
* substream
, int state
)
1212 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1213 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1216 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1217 runtime
->hw_ptr_base
= 0;
1218 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
- runtime
->status
->hw_ptr
% runtime
->period_size
;
1219 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1220 runtime
->silence_filled
= 0;
1224 static void snd_pcm_post_reset(snd_pcm_substream_t
* substream
, int state
)
1226 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1227 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1228 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1229 runtime
->silence_size
> 0)
1230 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1233 static struct action_ops snd_pcm_action_reset
= {
1234 .pre_action
= snd_pcm_pre_reset
,
1235 .do_action
= snd_pcm_do_reset
,
1236 .post_action
= snd_pcm_post_reset
1239 static int snd_pcm_reset(snd_pcm_substream_t
*substream
)
1241 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1247 static int snd_pcm_pre_prepare(snd_pcm_substream_t
* substream
, int state
)
1249 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1250 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1252 if (snd_pcm_running(substream
))
1257 static int snd_pcm_do_prepare(snd_pcm_substream_t
* substream
, int state
)
1260 err
= substream
->ops
->prepare(substream
);
1263 return snd_pcm_do_reset(substream
, 0);
1266 static void snd_pcm_post_prepare(snd_pcm_substream_t
* substream
, int state
)
1268 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1269 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1270 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1273 static struct action_ops snd_pcm_action_prepare
= {
1274 .pre_action
= snd_pcm_pre_prepare
,
1275 .do_action
= snd_pcm_do_prepare
,
1276 .post_action
= snd_pcm_post_prepare
1281 * @substream: the PCM substream instance
1283 * Prepare the PCM substream to be triggerable.
1285 int snd_pcm_prepare(snd_pcm_substream_t
*substream
)
1288 snd_card_t
*card
= substream
->pcm
->card
;
1290 snd_power_lock(card
);
1291 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
)) >= 0)
1292 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
, substream
, 0);
1293 snd_power_unlock(card
);
1301 static int snd_pcm_pre_drain_init(snd_pcm_substream_t
* substream
, int state
)
1303 if (substream
->ffile
->f_flags
& O_NONBLOCK
)
1305 substream
->runtime
->trigger_master
= substream
;
1309 static int snd_pcm_do_drain_init(snd_pcm_substream_t
* substream
, int state
)
1311 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1312 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1313 switch (runtime
->status
->state
) {
1314 case SNDRV_PCM_STATE_PREPARED
:
1315 /* start playback stream if possible */
1316 if (! snd_pcm_playback_empty(substream
)) {
1317 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1318 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1321 case SNDRV_PCM_STATE_RUNNING
:
1322 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1328 /* stop running stream */
1329 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1330 int state
= snd_pcm_capture_avail(runtime
) > 0 ?
1331 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1332 snd_pcm_do_stop(substream
, state
);
1333 snd_pcm_post_stop(substream
, state
);
1339 static void snd_pcm_post_drain_init(snd_pcm_substream_t
* substream
, int state
)
1343 static struct action_ops snd_pcm_action_drain_init
= {
1344 .pre_action
= snd_pcm_pre_drain_init
,
1345 .do_action
= snd_pcm_do_drain_init
,
1346 .post_action
= snd_pcm_post_drain_init
1350 snd_pcm_substream_t
*substream
;
1352 snd_pcm_uframes_t stop_threshold
;
1355 static int snd_pcm_drop(snd_pcm_substream_t
*substream
);
1358 * Drain the stream(s).
1359 * When the substream is linked, sync until the draining of all playback streams
1361 * After this call, all streams are supposed to be either SETUP or DRAINING
1362 * (capture only) state.
1364 static int snd_pcm_drain(snd_pcm_substream_t
*substream
)
1367 snd_pcm_runtime_t
*runtime
;
1368 struct list_head
*pos
;
1371 struct drain_rec
*drec
, drec_tmp
, *d
;
1373 snd_assert(substream
!= NULL
, return -ENXIO
);
1374 card
= substream
->pcm
->card
;
1375 runtime
= substream
->runtime
;
1377 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1380 snd_power_lock(card
);
1381 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1382 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1384 snd_power_unlock(card
);
1389 /* allocate temporary record for drain sync */
1390 down_read(&snd_pcm_link_rwsem
);
1391 if (snd_pcm_stream_linked(substream
)) {
1392 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1394 up_read(&snd_pcm_link_rwsem
);
1395 snd_power_unlock(card
);
1401 /* count only playback streams */
1403 snd_pcm_group_for_each(pos
, substream
) {
1404 snd_pcm_substream_t
*s
= snd_pcm_group_substream_entry(pos
);
1405 runtime
= s
->runtime
;
1406 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1407 d
= &drec
[num_drecs
++];
1409 init_waitqueue_entry(&d
->wait
, current
);
1410 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1411 /* stop_threshold fixup to avoid endless loop when
1412 * stop_threshold > buffer_size
1414 d
->stop_threshold
= runtime
->stop_threshold
;
1415 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1416 runtime
->stop_threshold
= runtime
->buffer_size
;
1419 up_read(&snd_pcm_link_rwsem
);
1423 snd_pcm_stream_lock_irq(substream
);
1425 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1426 snd_pcm_pause(substream
, 0);
1428 /* pre-start/stop - all running streams are changed to DRAINING state */
1429 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1431 snd_pcm_stream_unlock_irq(substream
);
1437 if (signal_pending(current
)) {
1438 result
= -ERESTARTSYS
;
1442 for (i
= 0; i
< num_drecs
; i
++) {
1443 runtime
= drec
[i
].substream
->runtime
;
1444 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1448 break; /* yes, all drained */
1450 set_current_state(TASK_INTERRUPTIBLE
);
1451 snd_pcm_stream_unlock_irq(substream
);
1452 snd_power_unlock(card
);
1453 tout
= schedule_timeout(10 * HZ
);
1454 snd_power_lock(card
);
1455 snd_pcm_stream_lock_irq(substream
);
1457 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1460 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1461 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1468 snd_pcm_stream_unlock_irq(substream
);
1471 for (i
= 0; i
< num_drecs
; i
++) {
1473 runtime
= d
->substream
->runtime
;
1474 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1475 runtime
->stop_threshold
= d
->stop_threshold
;
1478 if (drec
!= &drec_tmp
)
1480 snd_power_unlock(card
);
1488 * Immediately put all linked substreams into SETUP state.
1490 static int snd_pcm_drop(snd_pcm_substream_t
*substream
)
1492 snd_pcm_runtime_t
*runtime
;
1496 snd_assert(substream
!= NULL
, return -ENXIO
);
1497 runtime
= substream
->runtime
;
1498 card
= substream
->pcm
->card
;
1500 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1503 snd_power_lock(card
);
1504 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1505 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1510 snd_pcm_stream_lock_irq(substream
);
1512 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1513 snd_pcm_pause(substream
, 0);
1515 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1516 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1517 snd_pcm_stream_unlock_irq(substream
);
1519 snd_power_unlock(card
);
1524 /* WARNING: Don't forget to fput back the file */
1525 static struct file
*snd_pcm_file_fd(int fd
)
1528 struct inode
*inode
;
1529 unsigned short minor
;
1533 inode
= file
->f_dentry
->d_inode
;
1534 if (!S_ISCHR(inode
->i_mode
) ||
1535 imajor(inode
) != snd_major
) {
1539 minor
= iminor(inode
);
1541 minor
% SNDRV_MINOR_DEVICES
< SNDRV_MINOR_PCM_PLAYBACK
) {
1551 static int snd_pcm_link(snd_pcm_substream_t
*substream
, int fd
)
1555 snd_pcm_file_t
*pcm_file
;
1556 snd_pcm_substream_t
*substream1
;
1558 file
= snd_pcm_file_fd(fd
);
1561 pcm_file
= file
->private_data
;
1562 substream1
= pcm_file
->substream
;
1563 down_write(&snd_pcm_link_rwsem
);
1564 write_lock_irq(&snd_pcm_link_rwlock
);
1565 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1566 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1570 if (snd_pcm_stream_linked(substream1
)) {
1574 if (!snd_pcm_stream_linked(substream
)) {
1575 substream
->group
= kmalloc(sizeof(snd_pcm_group_t
), GFP_ATOMIC
);
1576 if (substream
->group
== NULL
) {
1580 spin_lock_init(&substream
->group
->lock
);
1581 INIT_LIST_HEAD(&substream
->group
->substreams
);
1582 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1583 substream
->group
->count
= 1;
1585 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1586 substream
->group
->count
++;
1587 substream1
->group
= substream
->group
;
1589 write_unlock_irq(&snd_pcm_link_rwlock
);
1590 up_write(&snd_pcm_link_rwsem
);
1595 static void relink_to_local(snd_pcm_substream_t
*substream
)
1597 substream
->group
= &substream
->self_group
;
1598 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1599 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1602 static int snd_pcm_unlink(snd_pcm_substream_t
*substream
)
1604 struct list_head
*pos
;
1607 down_write(&snd_pcm_link_rwsem
);
1608 write_lock_irq(&snd_pcm_link_rwlock
);
1609 if (!snd_pcm_stream_linked(substream
)) {
1613 list_del(&substream
->link_list
);
1614 substream
->group
->count
--;
1615 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1616 snd_pcm_group_for_each(pos
, substream
) {
1617 relink_to_local(snd_pcm_group_substream_entry(pos
));
1620 kfree(substream
->group
);
1622 relink_to_local(substream
);
1624 write_unlock_irq(&snd_pcm_link_rwlock
);
1625 up_write(&snd_pcm_link_rwsem
);
1632 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t
*params
,
1633 snd_pcm_hw_rule_t
*rule
)
1636 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1637 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1638 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1641 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t
*params
,
1642 snd_pcm_hw_rule_t
*rule
)
1645 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1646 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1647 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1650 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t
*params
,
1651 snd_pcm_hw_rule_t
*rule
)
1654 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1655 hw_param_interval_c(params
, rule
->deps
[1]),
1656 (unsigned long) rule
->private, &t
);
1657 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1660 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t
*params
,
1661 snd_pcm_hw_rule_t
*rule
)
1664 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1665 (unsigned long) rule
->private,
1666 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1667 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1670 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t
*params
,
1671 snd_pcm_hw_rule_t
*rule
)
1674 snd_interval_t
*i
= hw_param_interval(params
, rule
->deps
[0]);
1676 snd_mask_t
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1678 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1680 if (! snd_mask_test(mask
, k
))
1682 bits
= snd_pcm_format_physical_width(k
);
1684 continue; /* ignore invalid formats */
1685 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1686 snd_mask_reset(&m
, k
);
1688 return snd_mask_refine(mask
, &m
);
1691 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t
*params
,
1692 snd_pcm_hw_rule_t
*rule
)
1700 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1702 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1704 bits
= snd_pcm_format_physical_width(k
);
1706 continue; /* ignore invalid formats */
1707 if (t
.min
> (unsigned)bits
)
1709 if (t
.max
< (unsigned)bits
)
1713 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1716 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1717 #error "Change this table"
1720 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1721 48000, 64000, 88200, 96000, 176400, 192000 };
1723 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t
*params
,
1724 snd_pcm_hw_rule_t
*rule
)
1726 snd_pcm_hardware_t
*hw
= rule
->private;
1727 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1728 ARRAY_SIZE(rates
), rates
, hw
->rates
);
1731 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t
*params
,
1732 snd_pcm_hw_rule_t
*rule
)
1735 snd_pcm_substream_t
*substream
= rule
->private;
1737 t
.max
= substream
->buffer_bytes_max
;
1741 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1744 int snd_pcm_hw_constraints_init(snd_pcm_substream_t
*substream
)
1746 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1747 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
1750 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1751 snd_mask_any(constrs_mask(constrs
, k
));
1754 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1755 snd_interval_any(constrs_interval(constrs
, k
));
1758 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1759 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1760 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1761 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1762 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1764 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1765 snd_pcm_hw_rule_format
, NULL
,
1766 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1769 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1770 snd_pcm_hw_rule_sample_bits
, NULL
,
1771 SNDRV_PCM_HW_PARAM_FORMAT
,
1772 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1775 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1776 snd_pcm_hw_rule_div
, NULL
,
1777 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1780 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1781 snd_pcm_hw_rule_mul
, NULL
,
1782 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1785 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1786 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1787 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1790 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1791 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1792 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1795 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1796 snd_pcm_hw_rule_div
, NULL
,
1797 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1800 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1801 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1802 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1805 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1806 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1807 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1810 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1811 snd_pcm_hw_rule_div
, NULL
,
1812 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1815 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1816 snd_pcm_hw_rule_div
, NULL
,
1817 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1820 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1821 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1822 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1825 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1826 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1827 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1830 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1831 snd_pcm_hw_rule_mul
, NULL
,
1832 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1835 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1836 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1837 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1840 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1841 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1842 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1845 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1846 snd_pcm_hw_rule_muldivk
, (void*) 8,
1847 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1850 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1851 snd_pcm_hw_rule_muldivk
, (void*) 8,
1852 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1855 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1856 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1860 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1861 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1862 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1868 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t
*substream
)
1870 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1871 snd_pcm_hardware_t
*hw
= &runtime
->hw
;
1873 unsigned int mask
= 0;
1875 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1876 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1877 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1878 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1879 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1880 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1881 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1882 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1883 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1884 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1885 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1887 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1888 snd_assert(err
>= 0, return -EINVAL
);
1890 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1891 snd_assert(err
>= 0, return -EINVAL
);
1893 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1894 snd_assert(err
>= 0, return -EINVAL
);
1896 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1897 hw
->channels_min
, hw
->channels_max
);
1898 snd_assert(err
>= 0, return -EINVAL
);
1900 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1901 hw
->rate_min
, hw
->rate_max
);
1902 snd_assert(err
>= 0, return -EINVAL
);
1904 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1905 hw
->period_bytes_min
, hw
->period_bytes_max
);
1906 snd_assert(err
>= 0, return -EINVAL
);
1908 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1909 hw
->periods_min
, hw
->periods_max
);
1910 snd_assert(err
>= 0, return -EINVAL
);
1912 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1913 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1914 snd_assert(err
>= 0, return -EINVAL
);
1916 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1917 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1918 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1923 if (runtime
->dma_bytes
) {
1924 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1925 snd_assert(err
>= 0, return -EINVAL
);
1928 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1929 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1930 snd_pcm_hw_rule_rate
, hw
,
1931 SNDRV_PCM_HW_PARAM_RATE
, -1);
1936 /* FIXME: this belong to lowlevel */
1937 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_TICK_TIME
,
1938 1000000 / HZ
, 1000000 / HZ
);
1939 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1944 static void snd_pcm_add_file(snd_pcm_str_t
*str
,
1945 snd_pcm_file_t
*pcm_file
)
1947 pcm_file
->next
= str
->files
;
1948 str
->files
= pcm_file
;
1951 static void snd_pcm_remove_file(snd_pcm_str_t
*str
,
1952 snd_pcm_file_t
*pcm_file
)
1954 snd_pcm_file_t
* pcm_file1
;
1955 if (str
->files
== pcm_file
) {
1956 str
->files
= pcm_file
->next
;
1958 pcm_file1
= str
->files
;
1959 while (pcm_file1
&& pcm_file1
->next
!= pcm_file
)
1960 pcm_file1
= pcm_file1
->next
;
1961 if (pcm_file1
!= NULL
)
1962 pcm_file1
->next
= pcm_file
->next
;
1966 static int snd_pcm_release_file(snd_pcm_file_t
* pcm_file
)
1968 snd_pcm_substream_t
*substream
;
1969 snd_pcm_runtime_t
*runtime
;
1970 snd_pcm_str_t
* str
;
1972 snd_assert(pcm_file
!= NULL
, return -ENXIO
);
1973 substream
= pcm_file
->substream
;
1974 snd_assert(substream
!= NULL
, return -ENXIO
);
1975 runtime
= substream
->runtime
;
1976 str
= substream
->pstr
;
1977 snd_pcm_unlink(substream
);
1978 if (substream
->ffile
!= NULL
) {
1979 if (substream
->ops
->hw_free
!= NULL
)
1980 substream
->ops
->hw_free(substream
);
1981 substream
->ops
->close(substream
);
1982 substream
->ffile
= NULL
;
1984 snd_pcm_remove_file(str
, pcm_file
);
1985 snd_pcm_release_substream(substream
);
1990 static int snd_pcm_open_file(struct file
*file
,
1993 snd_pcm_file_t
**rpcm_file
)
1996 snd_pcm_file_t
*pcm_file
;
1997 snd_pcm_substream_t
*substream
;
2000 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
2003 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2004 if (pcm_file
== NULL
) {
2008 if ((err
= snd_pcm_open_substream(pcm
, stream
, &substream
)) < 0) {
2013 str
= substream
->pstr
;
2014 substream
->file
= pcm_file
;
2015 substream
->no_mmap_ctrl
= 0;
2017 pcm_file
->substream
= substream
;
2019 snd_pcm_add_file(str
, pcm_file
);
2021 err
= snd_pcm_hw_constraints_init(substream
);
2023 snd_printd("snd_pcm_hw_constraints_init failed\n");
2024 snd_pcm_release_file(pcm_file
);
2028 if ((err
= substream
->ops
->open(substream
)) < 0) {
2029 snd_pcm_release_file(pcm_file
);
2032 substream
->ffile
= file
;
2034 err
= snd_pcm_hw_constraints_complete(substream
);
2036 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2037 snd_pcm_release_file(pcm_file
);
2041 file
->private_data
= pcm_file
;
2042 *rpcm_file
= pcm_file
;
2046 static int snd_pcm_open(struct inode
*inode
, struct file
*file
)
2048 int cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
2049 int device
= SNDRV_MINOR_DEVICE(iminor(inode
));
2052 snd_pcm_file_t
*pcm_file
;
2055 if (device
< SNDRV_MINOR_PCM_PLAYBACK
|| device
>= SNDRV_MINOR_DEVICES
)
2057 pcm
= snd_pcm_devices
[(cardnum
* SNDRV_PCM_DEVICES
) + (device
% SNDRV_MINOR_PCMS
)];
2062 err
= snd_card_file_add(pcm
->card
, file
);
2065 if (!try_module_get(pcm
->card
->module
)) {
2069 init_waitqueue_entry(&wait
, current
);
2070 add_wait_queue(&pcm
->open_wait
, &wait
);
2071 down(&pcm
->open_mutex
);
2073 err
= snd_pcm_open_file(file
, pcm
, device
>= SNDRV_MINOR_PCM_CAPTURE
? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
, &pcm_file
);
2076 if (err
== -EAGAIN
) {
2077 if (file
->f_flags
& O_NONBLOCK
) {
2083 set_current_state(TASK_INTERRUPTIBLE
);
2084 up(&pcm
->open_mutex
);
2086 down(&pcm
->open_mutex
);
2087 if (signal_pending(current
)) {
2092 remove_wait_queue(&pcm
->open_wait
, &wait
);
2093 up(&pcm
->open_mutex
);
2099 module_put(pcm
->card
->module
);
2101 snd_card_file_remove(pcm
->card
, file
);
2106 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2109 snd_pcm_substream_t
*substream
;
2110 snd_pcm_file_t
*pcm_file
;
2112 pcm_file
= file
->private_data
;
2113 substream
= pcm_file
->substream
;
2114 snd_assert(substream
!= NULL
, return -ENXIO
);
2115 snd_assert(!atomic_read(&substream
->runtime
->mmap_count
), );
2116 pcm
= substream
->pcm
;
2117 snd_pcm_drop(substream
);
2118 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2119 down(&pcm
->open_mutex
);
2120 snd_pcm_release_file(pcm_file
);
2121 up(&pcm
->open_mutex
);
2122 wake_up(&pcm
->open_wait
);
2123 module_put(pcm
->card
->module
);
2124 snd_card_file_remove(pcm
->card
, file
);
2128 static snd_pcm_sframes_t
snd_pcm_playback_rewind(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2130 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2131 snd_pcm_sframes_t appl_ptr
;
2132 snd_pcm_sframes_t ret
;
2133 snd_pcm_sframes_t hw_avail
;
2138 snd_pcm_stream_lock_irq(substream
);
2139 switch (runtime
->status
->state
) {
2140 case SNDRV_PCM_STATE_PREPARED
:
2142 case SNDRV_PCM_STATE_DRAINING
:
2143 case SNDRV_PCM_STATE_RUNNING
:
2144 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2147 case SNDRV_PCM_STATE_XRUN
:
2155 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2156 if (hw_avail
<= 0) {
2160 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2163 frames
-= frames
% runtime
->xfer_align
;
2164 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2166 appl_ptr
+= runtime
->boundary
;
2167 runtime
->control
->appl_ptr
= appl_ptr
;
2168 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2170 snd_pcm_tick_prepare(substream
);
2173 snd_pcm_stream_unlock_irq(substream
);
2177 static snd_pcm_sframes_t
snd_pcm_capture_rewind(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2179 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2180 snd_pcm_sframes_t appl_ptr
;
2181 snd_pcm_sframes_t ret
;
2182 snd_pcm_sframes_t hw_avail
;
2187 snd_pcm_stream_lock_irq(substream
);
2188 switch (runtime
->status
->state
) {
2189 case SNDRV_PCM_STATE_PREPARED
:
2190 case SNDRV_PCM_STATE_DRAINING
:
2192 case SNDRV_PCM_STATE_RUNNING
:
2193 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2196 case SNDRV_PCM_STATE_XRUN
:
2204 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2205 if (hw_avail
<= 0) {
2209 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2212 frames
-= frames
% runtime
->xfer_align
;
2213 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2215 appl_ptr
+= runtime
->boundary
;
2216 runtime
->control
->appl_ptr
= appl_ptr
;
2217 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2219 snd_pcm_tick_prepare(substream
);
2222 snd_pcm_stream_unlock_irq(substream
);
2226 static snd_pcm_sframes_t
snd_pcm_playback_forward(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2228 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2229 snd_pcm_sframes_t appl_ptr
;
2230 snd_pcm_sframes_t ret
;
2231 snd_pcm_sframes_t avail
;
2236 snd_pcm_stream_lock_irq(substream
);
2237 switch (runtime
->status
->state
) {
2238 case SNDRV_PCM_STATE_PREPARED
:
2239 case SNDRV_PCM_STATE_PAUSED
:
2241 case SNDRV_PCM_STATE_DRAINING
:
2242 case SNDRV_PCM_STATE_RUNNING
:
2243 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2246 case SNDRV_PCM_STATE_XRUN
:
2254 avail
= snd_pcm_playback_avail(runtime
);
2259 if (frames
> (snd_pcm_uframes_t
)avail
)
2262 frames
-= frames
% runtime
->xfer_align
;
2263 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2264 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2265 appl_ptr
-= runtime
->boundary
;
2266 runtime
->control
->appl_ptr
= appl_ptr
;
2267 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2269 snd_pcm_tick_prepare(substream
);
2272 snd_pcm_stream_unlock_irq(substream
);
2276 static snd_pcm_sframes_t
snd_pcm_capture_forward(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2278 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2279 snd_pcm_sframes_t appl_ptr
;
2280 snd_pcm_sframes_t ret
;
2281 snd_pcm_sframes_t avail
;
2286 snd_pcm_stream_lock_irq(substream
);
2287 switch (runtime
->status
->state
) {
2288 case SNDRV_PCM_STATE_PREPARED
:
2289 case SNDRV_PCM_STATE_DRAINING
:
2290 case SNDRV_PCM_STATE_PAUSED
:
2292 case SNDRV_PCM_STATE_RUNNING
:
2293 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2296 case SNDRV_PCM_STATE_XRUN
:
2304 avail
= snd_pcm_capture_avail(runtime
);
2309 if (frames
> (snd_pcm_uframes_t
)avail
)
2312 frames
-= frames
% runtime
->xfer_align
;
2313 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2314 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2315 appl_ptr
-= runtime
->boundary
;
2316 runtime
->control
->appl_ptr
= appl_ptr
;
2317 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2319 snd_pcm_tick_prepare(substream
);
2322 snd_pcm_stream_unlock_irq(substream
);
2326 static int snd_pcm_hwsync(snd_pcm_substream_t
*substream
)
2328 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2331 snd_pcm_stream_lock_irq(substream
);
2332 switch (runtime
->status
->state
) {
2333 case SNDRV_PCM_STATE_DRAINING
:
2334 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2336 case SNDRV_PCM_STATE_RUNNING
:
2337 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2340 case SNDRV_PCM_STATE_PREPARED
:
2341 case SNDRV_PCM_STATE_SUSPENDED
:
2344 case SNDRV_PCM_STATE_XRUN
:
2352 snd_pcm_stream_unlock_irq(substream
);
2356 static int snd_pcm_delay(snd_pcm_substream_t
*substream
, snd_pcm_sframes_t __user
*res
)
2358 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2360 snd_pcm_sframes_t n
= 0;
2362 snd_pcm_stream_lock_irq(substream
);
2363 switch (runtime
->status
->state
) {
2364 case SNDRV_PCM_STATE_DRAINING
:
2365 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2367 case SNDRV_PCM_STATE_RUNNING
:
2368 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2371 case SNDRV_PCM_STATE_PREPARED
:
2372 case SNDRV_PCM_STATE_SUSPENDED
:
2374 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2375 n
= snd_pcm_playback_hw_avail(runtime
);
2377 n
= snd_pcm_capture_avail(runtime
);
2379 case SNDRV_PCM_STATE_XRUN
:
2387 snd_pcm_stream_unlock_irq(substream
);
2389 if (put_user(n
, res
))
2394 static int snd_pcm_sync_ptr(snd_pcm_substream_t
*substream
, struct sndrv_pcm_sync_ptr __user
*_sync_ptr
)
2396 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2397 struct sndrv_pcm_sync_ptr sync_ptr
;
2398 volatile struct sndrv_pcm_mmap_status
*status
;
2399 volatile struct sndrv_pcm_mmap_control
*control
;
2402 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2403 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2405 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct sndrv_pcm_mmap_control
)))
2407 status
= runtime
->status
;
2408 control
= runtime
->control
;
2409 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2410 err
= snd_pcm_hwsync(substream
);
2414 snd_pcm_stream_lock_irq(substream
);
2415 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2416 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2418 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2419 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2420 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2422 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2423 sync_ptr
.s
.status
.state
= status
->state
;
2424 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2425 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2426 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2427 snd_pcm_stream_unlock_irq(substream
);
2428 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2433 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t
*substream
,
2434 unsigned int cmd
, void __user
*arg
);
2435 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t
*substream
,
2436 unsigned int cmd
, void __user
*arg
);
2438 static int snd_pcm_common_ioctl1(snd_pcm_substream_t
*substream
,
2439 unsigned int cmd
, void __user
*arg
)
2441 snd_assert(substream
!= NULL
, return -ENXIO
);
2444 case SNDRV_PCM_IOCTL_PVERSION
:
2445 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2446 case SNDRV_PCM_IOCTL_INFO
:
2447 return snd_pcm_info_user(substream
, arg
);
2448 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2450 case SNDRV_PCM_IOCTL_HW_REFINE
:
2451 return snd_pcm_hw_refine_user(substream
, arg
);
2452 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2453 return snd_pcm_hw_params_user(substream
, arg
);
2454 case SNDRV_PCM_IOCTL_HW_FREE
:
2455 return snd_pcm_hw_free(substream
);
2456 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2457 return snd_pcm_sw_params_user(substream
, arg
);
2458 case SNDRV_PCM_IOCTL_STATUS
:
2459 return snd_pcm_status_user(substream
, arg
);
2460 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2461 return snd_pcm_channel_info_user(substream
, arg
);
2462 case SNDRV_PCM_IOCTL_PREPARE
:
2463 return snd_pcm_prepare(substream
);
2464 case SNDRV_PCM_IOCTL_RESET
:
2465 return snd_pcm_reset(substream
);
2466 case SNDRV_PCM_IOCTL_START
:
2467 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2468 case SNDRV_PCM_IOCTL_LINK
:
2469 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2470 case SNDRV_PCM_IOCTL_UNLINK
:
2471 return snd_pcm_unlink(substream
);
2472 case SNDRV_PCM_IOCTL_RESUME
:
2473 return snd_pcm_resume(substream
);
2474 case SNDRV_PCM_IOCTL_XRUN
:
2475 return snd_pcm_xrun(substream
);
2476 case SNDRV_PCM_IOCTL_HWSYNC
:
2477 return snd_pcm_hwsync(substream
);
2478 case SNDRV_PCM_IOCTL_DELAY
:
2479 return snd_pcm_delay(substream
, arg
);
2480 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2481 return snd_pcm_sync_ptr(substream
, arg
);
2482 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2483 return snd_pcm_hw_refine_old_user(substream
, arg
);
2484 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2485 return snd_pcm_hw_params_old_user(substream
, arg
);
2486 case SNDRV_PCM_IOCTL_DRAIN
:
2487 return snd_pcm_drain(substream
);
2488 case SNDRV_PCM_IOCTL_DROP
:
2489 return snd_pcm_drop(substream
);
2491 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2495 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t
*substream
,
2496 unsigned int cmd
, void __user
*arg
)
2498 snd_assert(substream
!= NULL
, return -ENXIO
);
2499 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2501 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2504 snd_xferi_t __user
*_xferi
= arg
;
2505 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2506 snd_pcm_sframes_t result
;
2507 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2509 if (put_user(0, &_xferi
->result
))
2511 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2513 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2514 __put_user(result
, &_xferi
->result
);
2515 return result
< 0 ? result
: 0;
2517 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2520 snd_xfern_t __user
*_xfern
= arg
;
2521 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2523 snd_pcm_sframes_t result
;
2524 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2526 if (runtime
->channels
> 128)
2528 if (put_user(0, &_xfern
->result
))
2530 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2532 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2535 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2539 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2541 __put_user(result
, &_xfern
->result
);
2542 return result
< 0 ? result
: 0;
2544 case SNDRV_PCM_IOCTL_REWIND
:
2546 snd_pcm_uframes_t frames
;
2547 snd_pcm_uframes_t __user
*_frames
= arg
;
2548 snd_pcm_sframes_t result
;
2549 if (get_user(frames
, _frames
))
2551 if (put_user(0, _frames
))
2553 result
= snd_pcm_playback_rewind(substream
, frames
);
2554 __put_user(result
, _frames
);
2555 return result
< 0 ? result
: 0;
2557 case SNDRV_PCM_IOCTL_FORWARD
:
2559 snd_pcm_uframes_t frames
;
2560 snd_pcm_uframes_t __user
*_frames
= arg
;
2561 snd_pcm_sframes_t result
;
2562 if (get_user(frames
, _frames
))
2564 if (put_user(0, _frames
))
2566 result
= snd_pcm_playback_forward(substream
, frames
);
2567 __put_user(result
, _frames
);
2568 return result
< 0 ? result
: 0;
2570 case SNDRV_PCM_IOCTL_PAUSE
:
2573 snd_pcm_stream_lock_irq(substream
);
2574 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2575 snd_pcm_stream_unlock_irq(substream
);
2579 return snd_pcm_common_ioctl1(substream
, cmd
, arg
);
2582 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t
*substream
,
2583 unsigned int cmd
, void __user
*arg
)
2585 snd_assert(substream
!= NULL
, return -ENXIO
);
2586 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2588 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2591 snd_xferi_t __user
*_xferi
= arg
;
2592 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2593 snd_pcm_sframes_t result
;
2594 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2596 if (put_user(0, &_xferi
->result
))
2598 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2600 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2601 __put_user(result
, &_xferi
->result
);
2602 return result
< 0 ? result
: 0;
2604 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2607 snd_xfern_t __user
*_xfern
= arg
;
2608 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2610 snd_pcm_sframes_t result
;
2611 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2613 if (runtime
->channels
> 128)
2615 if (put_user(0, &_xfern
->result
))
2617 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2619 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2622 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2626 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2628 __put_user(result
, &_xfern
->result
);
2629 return result
< 0 ? result
: 0;
2631 case SNDRV_PCM_IOCTL_REWIND
:
2633 snd_pcm_uframes_t frames
;
2634 snd_pcm_uframes_t __user
*_frames
= arg
;
2635 snd_pcm_sframes_t result
;
2636 if (get_user(frames
, _frames
))
2638 if (put_user(0, _frames
))
2640 result
= snd_pcm_capture_rewind(substream
, frames
);
2641 __put_user(result
, _frames
);
2642 return result
< 0 ? result
: 0;
2644 case SNDRV_PCM_IOCTL_FORWARD
:
2646 snd_pcm_uframes_t frames
;
2647 snd_pcm_uframes_t __user
*_frames
= arg
;
2648 snd_pcm_sframes_t result
;
2649 if (get_user(frames
, _frames
))
2651 if (put_user(0, _frames
))
2653 result
= snd_pcm_capture_forward(substream
, frames
);
2654 __put_user(result
, _frames
);
2655 return result
< 0 ? result
: 0;
2658 return snd_pcm_common_ioctl1(substream
, cmd
, arg
);
2661 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2663 snd_pcm_file_t
*pcm_file
;
2665 pcm_file
= file
->private_data
;
2667 if (((cmd
>> 8) & 0xff) != 'A')
2670 return snd_pcm_playback_ioctl1(pcm_file
->substream
, cmd
, (void __user
*)arg
);
2673 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2675 snd_pcm_file_t
*pcm_file
;
2677 pcm_file
= file
->private_data
;
2679 if (((cmd
>> 8) & 0xff) != 'A')
2682 return snd_pcm_capture_ioctl1(pcm_file
->substream
, cmd
, (void __user
*)arg
);
2685 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t
*substream
,
2686 unsigned int cmd
, void *arg
)
2691 fs
= snd_enter_user();
2692 result
= snd_pcm_playback_ioctl1(substream
, cmd
, (void __user
*)arg
);
2697 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t
*substream
,
2698 unsigned int cmd
, void *arg
)
2703 fs
= snd_enter_user();
2704 result
= snd_pcm_capture_ioctl1(substream
, cmd
, (void __user
*)arg
);
2709 int snd_pcm_kernel_ioctl(snd_pcm_substream_t
*substream
,
2710 unsigned int cmd
, void *arg
)
2712 switch (substream
->stream
) {
2713 case SNDRV_PCM_STREAM_PLAYBACK
:
2714 return snd_pcm_kernel_playback_ioctl(substream
, cmd
, arg
);
2715 case SNDRV_PCM_STREAM_CAPTURE
:
2716 return snd_pcm_kernel_capture_ioctl(substream
, cmd
, arg
);
2722 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
* offset
)
2724 snd_pcm_file_t
*pcm_file
;
2725 snd_pcm_substream_t
*substream
;
2726 snd_pcm_runtime_t
*runtime
;
2727 snd_pcm_sframes_t result
;
2729 pcm_file
= file
->private_data
;
2730 substream
= pcm_file
->substream
;
2731 snd_assert(substream
!= NULL
, return -ENXIO
);
2732 runtime
= substream
->runtime
;
2733 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2735 if (!frame_aligned(runtime
, count
))
2737 count
= bytes_to_frames(runtime
, count
);
2738 result
= snd_pcm_lib_read(substream
, buf
, count
);
2740 result
= frames_to_bytes(runtime
, result
);
2744 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
* offset
)
2746 snd_pcm_file_t
*pcm_file
;
2747 snd_pcm_substream_t
*substream
;
2748 snd_pcm_runtime_t
*runtime
;
2749 snd_pcm_sframes_t result
;
2751 pcm_file
= file
->private_data
;
2752 substream
= pcm_file
->substream
;
2753 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2754 runtime
= substream
->runtime
;
2755 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2759 if (!frame_aligned(runtime
, count
)) {
2763 count
= bytes_to_frames(runtime
, count
);
2764 result
= snd_pcm_lib_write(substream
, buf
, count
);
2766 result
= frames_to_bytes(runtime
, result
);
2771 static ssize_t
snd_pcm_readv(struct file
*file
, const struct iovec
*_vector
,
2772 unsigned long count
, loff_t
* offset
)
2775 snd_pcm_file_t
*pcm_file
;
2776 snd_pcm_substream_t
*substream
;
2777 snd_pcm_runtime_t
*runtime
;
2778 snd_pcm_sframes_t result
;
2781 snd_pcm_uframes_t frames
;
2783 pcm_file
= file
->private_data
;
2784 substream
= pcm_file
->substream
;
2785 snd_assert(substream
!= NULL
, return -ENXIO
);
2786 runtime
= substream
->runtime
;
2787 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2789 if (count
> 1024 || count
!= runtime
->channels
)
2791 if (!frame_aligned(runtime
, _vector
->iov_len
))
2793 frames
= bytes_to_samples(runtime
, _vector
->iov_len
);
2794 bufs
= kmalloc(sizeof(void *) * count
, GFP_KERNEL
);
2797 for (i
= 0; i
< count
; ++i
)
2798 bufs
[i
] = _vector
[i
].iov_base
;
2799 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2801 result
= frames_to_bytes(runtime
, result
);
2806 static ssize_t
snd_pcm_writev(struct file
*file
, const struct iovec
*_vector
,
2807 unsigned long count
, loff_t
* offset
)
2809 snd_pcm_file_t
*pcm_file
;
2810 snd_pcm_substream_t
*substream
;
2811 snd_pcm_runtime_t
*runtime
;
2812 snd_pcm_sframes_t result
;
2815 snd_pcm_uframes_t frames
;
2817 pcm_file
= file
->private_data
;
2818 substream
= pcm_file
->substream
;
2819 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2820 runtime
= substream
->runtime
;
2821 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2825 if (count
> 128 || count
!= runtime
->channels
||
2826 !frame_aligned(runtime
, _vector
->iov_len
)) {
2830 frames
= bytes_to_samples(runtime
, _vector
->iov_len
);
2831 bufs
= kmalloc(sizeof(void *) * count
, GFP_KERNEL
);
2834 for (i
= 0; i
< count
; ++i
)
2835 bufs
[i
] = _vector
[i
].iov_base
;
2836 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2838 result
= frames_to_bytes(runtime
, result
);
2844 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2846 snd_pcm_file_t
*pcm_file
;
2847 snd_pcm_substream_t
*substream
;
2848 snd_pcm_runtime_t
*runtime
;
2850 snd_pcm_uframes_t avail
;
2852 pcm_file
= file
->private_data
;
2854 substream
= pcm_file
->substream
;
2855 snd_assert(substream
!= NULL
, return -ENXIO
);
2856 runtime
= substream
->runtime
;
2858 poll_wait(file
, &runtime
->sleep
, wait
);
2860 snd_pcm_stream_lock_irq(substream
);
2861 avail
= snd_pcm_playback_avail(runtime
);
2862 switch (runtime
->status
->state
) {
2863 case SNDRV_PCM_STATE_RUNNING
:
2864 case SNDRV_PCM_STATE_PREPARED
:
2865 case SNDRV_PCM_STATE_PAUSED
:
2866 if (avail
>= runtime
->control
->avail_min
) {
2867 mask
= POLLOUT
| POLLWRNORM
;
2871 case SNDRV_PCM_STATE_DRAINING
:
2875 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2878 snd_pcm_stream_unlock_irq(substream
);
2882 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2884 snd_pcm_file_t
*pcm_file
;
2885 snd_pcm_substream_t
*substream
;
2886 snd_pcm_runtime_t
*runtime
;
2888 snd_pcm_uframes_t avail
;
2890 pcm_file
= file
->private_data
;
2892 substream
= pcm_file
->substream
;
2893 snd_assert(substream
!= NULL
, return -ENXIO
);
2894 runtime
= substream
->runtime
;
2896 poll_wait(file
, &runtime
->sleep
, wait
);
2898 snd_pcm_stream_lock_irq(substream
);
2899 avail
= snd_pcm_capture_avail(runtime
);
2900 switch (runtime
->status
->state
) {
2901 case SNDRV_PCM_STATE_RUNNING
:
2902 case SNDRV_PCM_STATE_PREPARED
:
2903 case SNDRV_PCM_STATE_PAUSED
:
2904 if (avail
>= runtime
->control
->avail_min
) {
2905 mask
= POLLIN
| POLLRDNORM
;
2910 case SNDRV_PCM_STATE_DRAINING
:
2912 mask
= POLLIN
| POLLRDNORM
;
2917 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2920 snd_pcm_stream_unlock_irq(substream
);
2929 * Only on coherent architectures, we can mmap the status and the control records
2930 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2932 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2934 * mmap status record
2936 static struct page
* snd_pcm_mmap_status_nopage(struct vm_area_struct
*area
, unsigned long address
, int *type
)
2938 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2939 snd_pcm_runtime_t
*runtime
;
2942 if (substream
== NULL
)
2944 runtime
= substream
->runtime
;
2945 page
= virt_to_page(runtime
->status
);
2948 *type
= VM_FAULT_MINOR
;
2952 static struct vm_operations_struct snd_pcm_vm_ops_status
=
2954 .nopage
= snd_pcm_mmap_status_nopage
,
2957 static int snd_pcm_mmap_status(snd_pcm_substream_t
*substream
, struct file
*file
,
2958 struct vm_area_struct
*area
)
2960 snd_pcm_runtime_t
*runtime
;
2962 if (!(area
->vm_flags
& VM_READ
))
2964 runtime
= substream
->runtime
;
2965 snd_assert(runtime
!= NULL
, return -EAGAIN
);
2966 size
= area
->vm_end
- area
->vm_start
;
2967 if (size
!= PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t
)))
2969 area
->vm_ops
= &snd_pcm_vm_ops_status
;
2970 area
->vm_private_data
= substream
;
2971 area
->vm_flags
|= VM_RESERVED
;
2976 * mmap control record
2978 static struct page
* snd_pcm_mmap_control_nopage(struct vm_area_struct
*area
, unsigned long address
, int *type
)
2980 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2981 snd_pcm_runtime_t
*runtime
;
2984 if (substream
== NULL
)
2986 runtime
= substream
->runtime
;
2987 page
= virt_to_page(runtime
->control
);
2990 *type
= VM_FAULT_MINOR
;
2994 static struct vm_operations_struct snd_pcm_vm_ops_control
=
2996 .nopage
= snd_pcm_mmap_control_nopage
,
2999 static int snd_pcm_mmap_control(snd_pcm_substream_t
*substream
, struct file
*file
,
3000 struct vm_area_struct
*area
)
3002 snd_pcm_runtime_t
*runtime
;
3004 if (!(area
->vm_flags
& VM_READ
))
3006 runtime
= substream
->runtime
;
3007 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3008 size
= area
->vm_end
- area
->vm_start
;
3009 if (size
!= PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t
)))
3011 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3012 area
->vm_private_data
= substream
;
3013 area
->vm_flags
|= VM_RESERVED
;
3016 #else /* ! coherent mmap */
3018 * don't support mmap for status and control records.
3020 static int snd_pcm_mmap_status(snd_pcm_substream_t
*substream
, struct file
*file
,
3021 struct vm_area_struct
*area
)
3025 static int snd_pcm_mmap_control(snd_pcm_substream_t
*substream
, struct file
*file
,
3026 struct vm_area_struct
*area
)
3030 #endif /* coherent mmap */
3033 * nopage callback for mmapping a RAM page
3035 static struct page
*snd_pcm_mmap_data_nopage(struct vm_area_struct
*area
, unsigned long address
, int *type
)
3037 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
3038 snd_pcm_runtime_t
*runtime
;
3039 unsigned long offset
;
3044 if (substream
== NULL
)
3046 runtime
= substream
->runtime
;
3047 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3048 offset
+= address
- area
->vm_start
;
3049 snd_assert((offset
% PAGE_SIZE
) == 0, return NOPAGE_OOM
);
3050 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3051 if (offset
> dma_bytes
- PAGE_SIZE
)
3052 return NOPAGE_SIGBUS
;
3053 if (substream
->ops
->page
) {
3054 page
= substream
->ops
->page(substream
, offset
);
3058 vaddr
= runtime
->dma_area
+ offset
;
3059 page
= virt_to_page(vaddr
);
3063 *type
= VM_FAULT_MINOR
;
3067 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3069 .open
= snd_pcm_mmap_data_open
,
3070 .close
= snd_pcm_mmap_data_close
,
3071 .nopage
= snd_pcm_mmap_data_nopage
,
3075 * mmap the DMA buffer on RAM
3077 static int snd_pcm_default_mmap(snd_pcm_substream_t
*substream
, struct vm_area_struct
*area
)
3079 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3080 area
->vm_private_data
= substream
;
3081 area
->vm_flags
|= VM_RESERVED
;
3082 atomic_inc(&substream
->runtime
->mmap_count
);
3087 * mmap the DMA buffer on I/O memory area
3089 #if SNDRV_PCM_INFO_MMAP_IOMEM
3090 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3092 .open
= snd_pcm_mmap_data_open
,
3093 .close
= snd_pcm_mmap_data_close
,
3096 int snd_pcm_lib_mmap_iomem(snd_pcm_substream_t
*substream
, struct vm_area_struct
*area
)
3099 unsigned long offset
;
3101 #ifdef pgprot_noncached
3102 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3104 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3105 area
->vm_private_data
= substream
;
3106 area
->vm_flags
|= VM_IO
;
3107 size
= area
->vm_end
- area
->vm_start
;
3108 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3109 if (io_remap_pfn_range(area
, area
->vm_start
,
3110 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3111 size
, area
->vm_page_prot
))
3113 atomic_inc(&substream
->runtime
->mmap_count
);
3116 #endif /* SNDRV_PCM_INFO_MMAP */
3121 int snd_pcm_mmap_data(snd_pcm_substream_t
*substream
, struct file
*file
,
3122 struct vm_area_struct
*area
)
3124 snd_pcm_runtime_t
*runtime
;
3126 unsigned long offset
;
3129 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3130 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3133 if (!(area
->vm_flags
& VM_READ
))
3136 runtime
= substream
->runtime
;
3137 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3138 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3140 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3142 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3143 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3145 size
= area
->vm_end
- area
->vm_start
;
3146 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3147 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3148 if ((size_t)size
> dma_bytes
)
3150 if (offset
> dma_bytes
- size
)
3153 if (substream
->ops
->mmap
)
3154 return substream
->ops
->mmap(substream
, area
);
3156 return snd_pcm_default_mmap(substream
, area
);
3159 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3161 snd_pcm_file_t
* pcm_file
;
3162 snd_pcm_substream_t
*substream
;
3163 unsigned long offset
;
3165 pcm_file
= file
->private_data
;
3166 substream
= pcm_file
->substream
;
3167 snd_assert(substream
!= NULL
, return -ENXIO
);
3169 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3171 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3172 if (substream
->no_mmap_ctrl
)
3174 return snd_pcm_mmap_status(substream
, file
, area
);
3175 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3176 if (substream
->no_mmap_ctrl
)
3178 return snd_pcm_mmap_control(substream
, file
, area
);
3180 return snd_pcm_mmap_data(substream
, file
, area
);
3185 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3187 snd_pcm_file_t
* pcm_file
;
3188 snd_pcm_substream_t
*substream
;
3189 snd_pcm_runtime_t
*runtime
;
3192 pcm_file
= file
->private_data
;
3193 substream
= pcm_file
->substream
;
3194 snd_assert(substream
!= NULL
, return -ENXIO
);
3195 runtime
= substream
->runtime
;
3197 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3206 #ifdef CONFIG_COMPAT
3207 #include "pcm_compat.c"
3209 #define snd_pcm_ioctl_compat NULL
3213 * To be removed helpers to keep binary compatibility
3216 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3217 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3219 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t
*params
, struct sndrv_pcm_hw_params_old
*oparams
)
3223 memset(params
, 0, sizeof(*params
));
3224 params
->flags
= oparams
->flags
;
3225 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3226 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3227 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3228 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3229 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3230 params
->info
= oparams
->info
;
3231 params
->msbits
= oparams
->msbits
;
3232 params
->rate_num
= oparams
->rate_num
;
3233 params
->rate_den
= oparams
->rate_den
;
3234 params
->fifo_size
= oparams
->fifo_size
;
3237 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old
*oparams
, snd_pcm_hw_params_t
*params
)
3241 memset(oparams
, 0, sizeof(*oparams
));
3242 oparams
->flags
= params
->flags
;
3243 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3244 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3245 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3246 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3247 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3248 oparams
->info
= params
->info
;
3249 oparams
->msbits
= params
->msbits
;
3250 oparams
->rate_num
= params
->rate_num
;
3251 oparams
->rate_den
= params
->rate_den
;
3252 oparams
->fifo_size
= params
->fifo_size
;
3255 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old __user
* _oparams
)
3257 snd_pcm_hw_params_t
*params
;
3258 struct sndrv_pcm_hw_params_old
*oparams
= NULL
;
3261 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3266 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3272 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3276 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3277 err
= snd_pcm_hw_refine(substream
, params
);
3278 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3279 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3289 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old __user
* _oparams
)
3291 snd_pcm_hw_params_t
*params
;
3292 struct sndrv_pcm_hw_params_old
*oparams
= NULL
;
3295 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3300 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3305 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3309 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3310 err
= snd_pcm_hw_params(substream
, params
);
3311 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3312 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3326 static struct file_operations snd_pcm_f_ops_playback
= {
3327 .owner
= THIS_MODULE
,
3328 .write
= snd_pcm_write
,
3329 .writev
= snd_pcm_writev
,
3330 .open
= snd_pcm_open
,
3331 .release
= snd_pcm_release
,
3332 .poll
= snd_pcm_playback_poll
,
3333 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3334 .compat_ioctl
= snd_pcm_ioctl_compat
,
3335 .mmap
= snd_pcm_mmap
,
3336 .fasync
= snd_pcm_fasync
,
3339 static struct file_operations snd_pcm_f_ops_capture
= {
3340 .owner
= THIS_MODULE
,
3341 .read
= snd_pcm_read
,
3342 .readv
= snd_pcm_readv
,
3343 .open
= snd_pcm_open
,
3344 .release
= snd_pcm_release
,
3345 .poll
= snd_pcm_capture_poll
,
3346 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3347 .compat_ioctl
= snd_pcm_ioctl_compat
,
3348 .mmap
= snd_pcm_mmap
,
3349 .fasync
= snd_pcm_fasync
,
3352 snd_minor_t snd_pcm_reg
[2] =
3355 .comment
= "digital audio playback",
3356 .f_ops
= &snd_pcm_f_ops_playback
,
3359 .comment
= "digital audio capture",
3360 .f_ops
= &snd_pcm_f_ops_capture
,