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/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/latency.h>
28 #include <linux/uio.h>
29 #include <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
42 struct snd_pcm_hw_params_old
{
44 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
45 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
46 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
52 unsigned int rate_num
;
53 unsigned int rate_den
;
54 snd_pcm_uframes_t fifo_size
;
55 unsigned char reserved
[64];
58 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
63 struct snd_pcm_hw_params_old __user
* _oparams
);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
65 struct snd_pcm_hw_params_old __user
* _oparams
);
67 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
74 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
78 static inline mm_segment_t
snd_enter_user(void)
80 mm_segment_t fs
= get_fs();
85 static inline void snd_leave_user(mm_segment_t fs
)
92 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
94 struct snd_pcm_runtime
*runtime
;
95 struct snd_pcm
*pcm
= substream
->pcm
;
96 struct snd_pcm_str
*pstr
= substream
->pstr
;
98 snd_assert(substream
!= NULL
, return -ENXIO
);
99 memset(info
, 0, sizeof(*info
));
100 info
->card
= pcm
->card
->number
;
101 info
->device
= pcm
->device
;
102 info
->stream
= substream
->stream
;
103 info
->subdevice
= substream
->number
;
104 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
105 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
106 info
->dev_class
= pcm
->dev_class
;
107 info
->dev_subclass
= pcm
->dev_subclass
;
108 info
->subdevices_count
= pstr
->substream_count
;
109 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
110 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
111 runtime
= substream
->runtime
;
112 /* AB: FIXME!!! This is definitely nonsense */
114 info
->sync
= runtime
->sync
;
115 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
120 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
121 struct snd_pcm_info __user
* _info
)
123 struct snd_pcm_info
*info
;
126 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
129 err
= snd_pcm_info(substream
, info
);
131 if (copy_to_user(_info
, info
, sizeof(*info
)))
141 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142 char *snd_pcm_hw_param_names
[] = {
146 HW_PARAM(SAMPLE_BITS
),
147 HW_PARAM(FRAME_BITS
),
150 HW_PARAM(PERIOD_TIME
),
151 HW_PARAM(PERIOD_SIZE
),
152 HW_PARAM(PERIOD_BYTES
),
154 HW_PARAM(BUFFER_TIME
),
155 HW_PARAM(BUFFER_SIZE
),
156 HW_PARAM(BUFFER_BYTES
),
161 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
162 struct snd_pcm_hw_params
*params
)
165 struct snd_pcm_hardware
*hw
;
166 struct snd_interval
*i
= NULL
;
167 struct snd_mask
*m
= NULL
;
168 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
169 unsigned int rstamps
[constrs
->rules_num
];
170 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
171 unsigned int stamp
= 2;
175 params
->fifo_size
= 0;
176 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
178 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
179 params
->rate_num
= 0;
180 params
->rate_den
= 0;
183 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
184 m
= hw_param_mask(params
, k
);
185 if (snd_mask_empty(m
))
187 if (!(params
->rmask
& (1 << k
)))
190 printk("%s = ", snd_pcm_hw_param_names
[k
]);
191 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
193 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
195 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
198 params
->cmask
|= 1 << k
;
203 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
204 i
= hw_param_interval(params
, k
);
205 if (snd_interval_empty(i
))
207 if (!(params
->rmask
& (1 << k
)))
210 printk("%s = ", snd_pcm_hw_param_names
[k
]);
215 i
->openmin
? '(' : '[', i
->min
,
216 i
->max
, i
->openmax
? ')' : ']');
219 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
224 printk("%c%u %u%c\n",
225 i
->openmin
? '(' : '[', i
->min
,
226 i
->max
, i
->openmax
? ')' : ']');
229 params
->cmask
|= 1 << k
;
234 for (k
= 0; k
< constrs
->rules_num
; k
++)
236 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
237 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
240 for (k
= 0; k
< constrs
->rules_num
; k
++) {
241 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
244 if (r
->cond
&& !(r
->cond
& params
->flags
))
246 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
247 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
255 printk("Rule %d [%p]: ", k
, r
->func
);
257 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
258 if (hw_is_mask(r
->var
)) {
259 m
= hw_param_mask(params
, r
->var
);
260 printk("%x", *m
->bits
);
262 i
= hw_param_interval(params
, r
->var
);
267 i
->openmin
? '(' : '[', i
->min
,
268 i
->max
, i
->openmax
? ')' : ']');
272 changed
= r
->func(params
, r
);
276 if (hw_is_mask(r
->var
))
277 printk("%x", *m
->bits
);
283 i
->openmin
? '(' : '[', i
->min
,
284 i
->max
, i
->openmax
? ')' : ']');
290 if (changed
&& r
->var
>= 0) {
291 params
->cmask
|= (1 << r
->var
);
292 vstamps
[r
->var
] = stamp
;
300 if (!params
->msbits
) {
301 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
302 if (snd_interval_single(i
))
303 params
->msbits
= snd_interval_value(i
);
306 if (!params
->rate_den
) {
307 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
308 if (snd_interval_single(i
)) {
309 params
->rate_num
= snd_interval_value(i
);
310 params
->rate_den
= 1;
314 hw
= &substream
->runtime
->hw
;
316 params
->info
= hw
->info
;
317 if (!params
->fifo_size
)
318 params
->fifo_size
= hw
->fifo_size
;
323 EXPORT_SYMBOL(snd_pcm_hw_refine
);
325 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
326 struct snd_pcm_hw_params __user
* _params
)
328 struct snd_pcm_hw_params
*params
;
331 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
336 if (copy_from_user(params
, _params
, sizeof(*params
))) {
340 err
= snd_pcm_hw_refine(substream
, params
);
341 if (copy_to_user(_params
, params
, sizeof(*params
))) {
350 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
355 return -1; /* invalid */
357 /* take 75% of period time as the deadline */
358 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
359 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
365 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
366 struct snd_pcm_hw_params
*params
)
368 struct snd_pcm_runtime
*runtime
;
371 snd_pcm_uframes_t frames
;
373 snd_assert(substream
!= NULL
, return -ENXIO
);
374 runtime
= substream
->runtime
;
375 snd_assert(runtime
!= NULL
, return -ENXIO
);
376 snd_pcm_stream_lock_irq(substream
);
377 switch (runtime
->status
->state
) {
378 case SNDRV_PCM_STATE_OPEN
:
379 case SNDRV_PCM_STATE_SETUP
:
380 case SNDRV_PCM_STATE_PREPARED
:
383 snd_pcm_stream_unlock_irq(substream
);
386 snd_pcm_stream_unlock_irq(substream
);
387 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
388 if (!substream
->oss
.oss
)
390 if (atomic_read(&substream
->mmap_count
))
394 err
= snd_pcm_hw_refine(substream
, params
);
398 err
= snd_pcm_hw_params_choose(substream
, params
);
402 if (substream
->ops
->hw_params
!= NULL
) {
403 err
= substream
->ops
->hw_params(substream
, params
);
408 runtime
->access
= params_access(params
);
409 runtime
->format
= params_format(params
);
410 runtime
->subformat
= params_subformat(params
);
411 runtime
->channels
= params_channels(params
);
412 runtime
->rate
= params_rate(params
);
413 runtime
->period_size
= params_period_size(params
);
414 runtime
->periods
= params_periods(params
);
415 runtime
->buffer_size
= params_buffer_size(params
);
416 runtime
->tick_time
= params_tick_time(params
);
417 runtime
->info
= params
->info
;
418 runtime
->rate_num
= params
->rate_num
;
419 runtime
->rate_den
= params
->rate_den
;
421 bits
= snd_pcm_format_physical_width(runtime
->format
);
422 runtime
->sample_bits
= bits
;
423 bits
*= runtime
->channels
;
424 runtime
->frame_bits
= bits
;
426 while (bits
% 8 != 0) {
430 runtime
->byte_align
= bits
/ 8;
431 runtime
->min_align
= frames
;
433 /* Default sw params */
434 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
435 runtime
->period_step
= 1;
436 runtime
->sleep_min
= 0;
437 runtime
->control
->avail_min
= runtime
->period_size
;
438 runtime
->xfer_align
= runtime
->period_size
;
439 runtime
->start_threshold
= 1;
440 runtime
->stop_threshold
= runtime
->buffer_size
;
441 runtime
->silence_threshold
= 0;
442 runtime
->silence_size
= 0;
443 runtime
->boundary
= runtime
->buffer_size
;
444 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
445 runtime
->boundary
*= 2;
447 snd_pcm_timer_resolution_change(substream
);
448 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
450 remove_acceptable_latency(substream
->latency_id
);
451 if ((usecs
= period_to_usecs(runtime
)) >= 0)
452 set_acceptable_latency(substream
->latency_id
, usecs
);
455 /* hardware might be unuseable from this time,
456 so we force application to retry to set
457 the correct hardware parameter settings */
458 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
459 if (substream
->ops
->hw_free
!= NULL
)
460 substream
->ops
->hw_free(substream
);
464 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
465 struct snd_pcm_hw_params __user
* _params
)
467 struct snd_pcm_hw_params
*params
;
470 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
475 if (copy_from_user(params
, _params
, sizeof(*params
))) {
479 err
= snd_pcm_hw_params(substream
, params
);
480 if (copy_to_user(_params
, params
, sizeof(*params
))) {
489 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
491 struct snd_pcm_runtime
*runtime
;
494 snd_assert(substream
!= NULL
, return -ENXIO
);
495 runtime
= substream
->runtime
;
496 snd_assert(runtime
!= NULL
, return -ENXIO
);
497 snd_pcm_stream_lock_irq(substream
);
498 switch (runtime
->status
->state
) {
499 case SNDRV_PCM_STATE_SETUP
:
500 case SNDRV_PCM_STATE_PREPARED
:
503 snd_pcm_stream_unlock_irq(substream
);
506 snd_pcm_stream_unlock_irq(substream
);
507 if (atomic_read(&substream
->mmap_count
))
509 if (substream
->ops
->hw_free
)
510 result
= substream
->ops
->hw_free(substream
);
511 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
512 remove_acceptable_latency(substream
->latency_id
);
516 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
517 struct snd_pcm_sw_params
*params
)
519 struct snd_pcm_runtime
*runtime
;
521 snd_assert(substream
!= NULL
, return -ENXIO
);
522 runtime
= substream
->runtime
;
523 snd_assert(runtime
!= NULL
, return -ENXIO
);
524 snd_pcm_stream_lock_irq(substream
);
525 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
526 snd_pcm_stream_unlock_irq(substream
);
529 snd_pcm_stream_unlock_irq(substream
);
531 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
533 if (params
->avail_min
== 0)
535 if (params
->xfer_align
== 0 ||
536 params
->xfer_align
% runtime
->min_align
!= 0)
538 if (params
->silence_size
>= runtime
->boundary
) {
539 if (params
->silence_threshold
!= 0)
542 if (params
->silence_size
> params
->silence_threshold
)
544 if (params
->silence_threshold
> runtime
->buffer_size
)
547 snd_pcm_stream_lock_irq(substream
);
548 runtime
->tstamp_mode
= params
->tstamp_mode
;
549 runtime
->sleep_min
= params
->sleep_min
;
550 runtime
->period_step
= params
->period_step
;
551 runtime
->control
->avail_min
= params
->avail_min
;
552 runtime
->start_threshold
= params
->start_threshold
;
553 runtime
->stop_threshold
= params
->stop_threshold
;
554 runtime
->silence_threshold
= params
->silence_threshold
;
555 runtime
->silence_size
= params
->silence_size
;
556 runtime
->xfer_align
= params
->xfer_align
;
557 params
->boundary
= runtime
->boundary
;
558 if (snd_pcm_running(substream
)) {
559 if (runtime
->sleep_min
)
560 snd_pcm_tick_prepare(substream
);
562 snd_pcm_tick_set(substream
, 0);
563 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
564 runtime
->silence_size
> 0)
565 snd_pcm_playback_silence(substream
, ULONG_MAX
);
566 wake_up(&runtime
->sleep
);
568 snd_pcm_stream_unlock_irq(substream
);
572 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
573 struct snd_pcm_sw_params __user
* _params
)
575 struct snd_pcm_sw_params params
;
577 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
579 err
= snd_pcm_sw_params(substream
, ¶ms
);
580 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
585 int snd_pcm_status(struct snd_pcm_substream
*substream
,
586 struct snd_pcm_status
*status
)
588 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
590 snd_pcm_stream_lock_irq(substream
);
591 status
->state
= runtime
->status
->state
;
592 status
->suspended_state
= runtime
->status
->suspended_state
;
593 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
595 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
596 if (snd_pcm_running(substream
)) {
597 snd_pcm_update_hw_ptr(substream
);
598 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
599 status
->tstamp
= runtime
->status
->tstamp
;
601 getnstimeofday(&status
->tstamp
);
603 getnstimeofday(&status
->tstamp
);
604 status
->appl_ptr
= runtime
->control
->appl_ptr
;
605 status
->hw_ptr
= runtime
->status
->hw_ptr
;
606 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
607 status
->avail
= snd_pcm_playback_avail(runtime
);
608 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
609 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
610 status
->delay
= runtime
->buffer_size
- status
->avail
;
614 status
->avail
= snd_pcm_capture_avail(runtime
);
615 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
616 status
->delay
= status
->avail
;
620 status
->avail_max
= runtime
->avail_max
;
621 status
->overrange
= runtime
->overrange
;
622 runtime
->avail_max
= 0;
623 runtime
->overrange
= 0;
625 snd_pcm_stream_unlock_irq(substream
);
629 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
630 struct snd_pcm_status __user
* _status
)
632 struct snd_pcm_status status
;
633 struct snd_pcm_runtime
*runtime
;
636 snd_assert(substream
!= NULL
, return -ENXIO
);
637 runtime
= substream
->runtime
;
638 memset(&status
, 0, sizeof(status
));
639 res
= snd_pcm_status(substream
, &status
);
642 if (copy_to_user(_status
, &status
, sizeof(status
)))
647 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
648 struct snd_pcm_channel_info
* info
)
650 struct snd_pcm_runtime
*runtime
;
651 unsigned int channel
;
653 snd_assert(substream
!= NULL
, return -ENXIO
);
654 channel
= info
->channel
;
655 runtime
= substream
->runtime
;
656 snd_pcm_stream_lock_irq(substream
);
657 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
658 snd_pcm_stream_unlock_irq(substream
);
661 snd_pcm_stream_unlock_irq(substream
);
662 if (channel
>= runtime
->channels
)
664 memset(info
, 0, sizeof(*info
));
665 info
->channel
= channel
;
666 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
669 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
670 struct snd_pcm_channel_info __user
* _info
)
672 struct snd_pcm_channel_info info
;
675 if (copy_from_user(&info
, _info
, sizeof(info
)))
677 res
= snd_pcm_channel_info(substream
, &info
);
680 if (copy_to_user(_info
, &info
, sizeof(info
)))
685 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
687 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
688 if (runtime
->trigger_master
== NULL
)
690 if (runtime
->trigger_master
== substream
) {
691 getnstimeofday(&runtime
->trigger_tstamp
);
693 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
694 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
696 runtime
->trigger_master
= NULL
;
700 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
701 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
702 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
703 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
707 * this functions is core for handling of linked stream
708 * Note: the stream state might be changed also on failure
709 * Note2: call with calling stream lock + link lock
711 static int snd_pcm_action_group(struct action_ops
*ops
,
712 struct snd_pcm_substream
*substream
,
713 int state
, int do_lock
)
715 struct list_head
*pos
;
716 struct snd_pcm_substream
*s
= NULL
;
717 struct snd_pcm_substream
*s1
;
720 snd_pcm_group_for_each(pos
, substream
) {
721 s
= snd_pcm_group_substream_entry(pos
);
722 if (do_lock
&& s
!= substream
)
723 spin_lock(&s
->self_group
.lock
);
724 res
= ops
->pre_action(s
, state
);
728 snd_pcm_group_for_each(pos
, substream
) {
729 s
= snd_pcm_group_substream_entry(pos
);
730 res
= ops
->do_action(s
, state
);
732 if (ops
->undo_action
) {
733 snd_pcm_group_for_each(pos
, substream
) {
734 s1
= snd_pcm_group_substream_entry(pos
);
735 if (s1
== s
) /* failed stream */
737 ops
->undo_action(s1
, state
);
740 s
= NULL
; /* unlock all */
744 snd_pcm_group_for_each(pos
, substream
) {
745 s
= snd_pcm_group_substream_entry(pos
);
746 ops
->post_action(s
, state
);
751 snd_pcm_group_for_each(pos
, substream
) {
752 s1
= snd_pcm_group_substream_entry(pos
);
754 spin_unlock(&s1
->self_group
.lock
);
755 if (s1
== s
) /* end */
763 * Note: call with stream lock
765 static int snd_pcm_action_single(struct action_ops
*ops
,
766 struct snd_pcm_substream
*substream
,
771 res
= ops
->pre_action(substream
, state
);
774 res
= ops
->do_action(substream
, state
);
776 ops
->post_action(substream
, state
);
777 else if (ops
->undo_action
)
778 ops
->undo_action(substream
, state
);
783 * Note: call with stream lock
785 static int snd_pcm_action(struct action_ops
*ops
,
786 struct snd_pcm_substream
*substream
,
791 if (snd_pcm_stream_linked(substream
)) {
792 if (!spin_trylock(&substream
->group
->lock
)) {
793 spin_unlock(&substream
->self_group
.lock
);
794 spin_lock(&substream
->group
->lock
);
795 spin_lock(&substream
->self_group
.lock
);
797 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
798 spin_unlock(&substream
->group
->lock
);
800 res
= snd_pcm_action_single(ops
, substream
, state
);
806 * Note: don't use any locks before
808 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
809 struct snd_pcm_substream
*substream
,
814 read_lock_irq(&snd_pcm_link_rwlock
);
815 if (snd_pcm_stream_linked(substream
)) {
816 spin_lock(&substream
->group
->lock
);
817 spin_lock(&substream
->self_group
.lock
);
818 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
819 spin_unlock(&substream
->self_group
.lock
);
820 spin_unlock(&substream
->group
->lock
);
822 spin_lock(&substream
->self_group
.lock
);
823 res
= snd_pcm_action_single(ops
, substream
, state
);
824 spin_unlock(&substream
->self_group
.lock
);
826 read_unlock_irq(&snd_pcm_link_rwlock
);
832 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
833 struct snd_pcm_substream
*substream
,
838 down_read(&snd_pcm_link_rwsem
);
839 if (snd_pcm_stream_linked(substream
))
840 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
842 res
= snd_pcm_action_single(ops
, substream
, state
);
843 up_read(&snd_pcm_link_rwsem
);
850 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
852 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
853 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
855 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
856 !snd_pcm_playback_data(substream
))
858 runtime
->trigger_master
= substream
;
862 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
864 if (substream
->runtime
->trigger_master
!= substream
)
866 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
869 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
871 if (substream
->runtime
->trigger_master
== substream
)
872 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
875 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
877 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
878 snd_pcm_trigger_tstamp(substream
);
879 runtime
->status
->state
= state
;
880 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
881 runtime
->silence_size
> 0)
882 snd_pcm_playback_silence(substream
, ULONG_MAX
);
883 if (runtime
->sleep_min
)
884 snd_pcm_tick_prepare(substream
);
885 if (substream
->timer
)
886 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
887 &runtime
->trigger_tstamp
);
890 static struct action_ops snd_pcm_action_start
= {
891 .pre_action
= snd_pcm_pre_start
,
892 .do_action
= snd_pcm_do_start
,
893 .undo_action
= snd_pcm_undo_start
,
894 .post_action
= snd_pcm_post_start
899 * @substream: the PCM substream instance
901 * Start all linked streams.
903 int snd_pcm_start(struct snd_pcm_substream
*substream
)
905 return snd_pcm_action(&snd_pcm_action_start
, substream
,
906 SNDRV_PCM_STATE_RUNNING
);
912 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
914 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
915 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
917 runtime
->trigger_master
= substream
;
921 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
923 if (substream
->runtime
->trigger_master
== substream
&&
924 snd_pcm_running(substream
))
925 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
926 return 0; /* unconditonally stop all substreams */
929 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
931 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
932 if (runtime
->status
->state
!= state
) {
933 snd_pcm_trigger_tstamp(substream
);
934 if (substream
->timer
)
935 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
936 &runtime
->trigger_tstamp
);
937 runtime
->status
->state
= state
;
938 snd_pcm_tick_set(substream
, 0);
940 wake_up(&runtime
->sleep
);
943 static struct action_ops snd_pcm_action_stop
= {
944 .pre_action
= snd_pcm_pre_stop
,
945 .do_action
= snd_pcm_do_stop
,
946 .post_action
= snd_pcm_post_stop
951 * @substream: the PCM substream instance
952 * @state: PCM state after stopping the stream
954 * Try to stop all running streams in the substream group.
955 * The state of each stream is changed to the given value after that unconditionally.
957 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
959 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
962 EXPORT_SYMBOL(snd_pcm_stop
);
966 * @substream: the PCM substream
968 * Stop the DMA only when the given stream is playback.
969 * The state is changed to SETUP.
970 * Unlike snd_pcm_stop(), this affects only the given stream.
972 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
974 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
975 SNDRV_PCM_STATE_SETUP
);
981 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
983 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
984 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
987 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
989 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
991 runtime
->trigger_master
= substream
;
995 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
997 if (substream
->runtime
->trigger_master
!= substream
)
999 return substream
->ops
->trigger(substream
,
1000 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1001 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1004 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1006 if (substream
->runtime
->trigger_master
== substream
)
1007 substream
->ops
->trigger(substream
,
1008 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1009 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1012 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1014 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1015 snd_pcm_trigger_tstamp(substream
);
1017 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1018 if (substream
->timer
)
1019 snd_timer_notify(substream
->timer
,
1020 SNDRV_TIMER_EVENT_MPAUSE
,
1021 &runtime
->trigger_tstamp
);
1022 snd_pcm_tick_set(substream
, 0);
1023 wake_up(&runtime
->sleep
);
1025 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1026 if (runtime
->sleep_min
)
1027 snd_pcm_tick_prepare(substream
);
1028 if (substream
->timer
)
1029 snd_timer_notify(substream
->timer
,
1030 SNDRV_TIMER_EVENT_MCONTINUE
,
1031 &runtime
->trigger_tstamp
);
1035 static struct action_ops snd_pcm_action_pause
= {
1036 .pre_action
= snd_pcm_pre_pause
,
1037 .do_action
= snd_pcm_do_pause
,
1038 .undo_action
= snd_pcm_undo_pause
,
1039 .post_action
= snd_pcm_post_pause
1043 * Push/release the pause for all linked streams.
1045 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1047 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1053 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1055 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1056 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1058 runtime
->trigger_master
= substream
;
1062 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1064 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1065 if (runtime
->trigger_master
!= substream
)
1067 if (! snd_pcm_running(substream
))
1069 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1070 return 0; /* suspend unconditionally */
1073 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1075 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1076 snd_pcm_trigger_tstamp(substream
);
1077 if (substream
->timer
)
1078 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1079 &runtime
->trigger_tstamp
);
1080 runtime
->status
->suspended_state
= runtime
->status
->state
;
1081 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1082 snd_pcm_tick_set(substream
, 0);
1083 wake_up(&runtime
->sleep
);
1086 static struct action_ops snd_pcm_action_suspend
= {
1087 .pre_action
= snd_pcm_pre_suspend
,
1088 .do_action
= snd_pcm_do_suspend
,
1089 .post_action
= snd_pcm_post_suspend
1094 * @substream: the PCM substream
1096 * Trigger SUSPEND to all linked streams.
1097 * After this call, all streams are changed to SUSPENDED state.
1099 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1102 unsigned long flags
;
1107 snd_pcm_stream_lock_irqsave(substream
, flags
);
1108 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1109 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1113 EXPORT_SYMBOL(snd_pcm_suspend
);
1116 * snd_pcm_suspend_all
1117 * @pcm: the PCM instance
1119 * Trigger SUSPEND to all substreams in the given pcm.
1120 * After this call, all streams are changed to SUSPENDED state.
1122 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1124 struct snd_pcm_substream
*substream
;
1125 int stream
, err
= 0;
1130 for (stream
= 0; stream
< 2; stream
++) {
1131 for (substream
= pcm
->streams
[stream
].substream
;
1132 substream
; substream
= substream
->next
) {
1133 /* FIXME: the open/close code should lock this as well */
1134 if (substream
->runtime
== NULL
)
1136 err
= snd_pcm_suspend(substream
);
1137 if (err
< 0 && err
!= -EBUSY
)
1144 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1148 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1150 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1151 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1153 runtime
->trigger_master
= substream
;
1157 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1159 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1160 if (runtime
->trigger_master
!= substream
)
1162 /* DMA not running previously? */
1163 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1164 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1165 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1167 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1170 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1172 if (substream
->runtime
->trigger_master
== substream
&&
1173 snd_pcm_running(substream
))
1174 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1177 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1179 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1180 snd_pcm_trigger_tstamp(substream
);
1181 if (substream
->timer
)
1182 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1183 &runtime
->trigger_tstamp
);
1184 runtime
->status
->state
= runtime
->status
->suspended_state
;
1185 if (runtime
->sleep_min
)
1186 snd_pcm_tick_prepare(substream
);
1189 static struct action_ops snd_pcm_action_resume
= {
1190 .pre_action
= snd_pcm_pre_resume
,
1191 .do_action
= snd_pcm_do_resume
,
1192 .undo_action
= snd_pcm_undo_resume
,
1193 .post_action
= snd_pcm_post_resume
1196 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1198 struct snd_card
*card
= substream
->pcm
->card
;
1201 snd_power_lock(card
);
1202 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1203 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1204 snd_power_unlock(card
);
1210 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1215 #endif /* CONFIG_PM */
1220 * Change the RUNNING stream(s) to XRUN state.
1222 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1224 struct snd_card
*card
= substream
->pcm
->card
;
1225 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1228 snd_power_lock(card
);
1229 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1230 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1235 snd_pcm_stream_lock_irq(substream
);
1236 switch (runtime
->status
->state
) {
1237 case SNDRV_PCM_STATE_XRUN
:
1238 result
= 0; /* already there */
1240 case SNDRV_PCM_STATE_RUNNING
:
1241 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1246 snd_pcm_stream_unlock_irq(substream
);
1248 snd_power_unlock(card
);
1255 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1257 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1258 switch (runtime
->status
->state
) {
1259 case SNDRV_PCM_STATE_RUNNING
:
1260 case SNDRV_PCM_STATE_PREPARED
:
1261 case SNDRV_PCM_STATE_PAUSED
:
1262 case SNDRV_PCM_STATE_SUSPENDED
:
1269 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1271 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1272 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1275 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1276 runtime
->hw_ptr_base
= 0;
1277 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1278 runtime
->status
->hw_ptr
% runtime
->period_size
;
1279 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1280 runtime
->silence_filled
= 0;
1284 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1286 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1287 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1288 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1289 runtime
->silence_size
> 0)
1290 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1293 static struct action_ops snd_pcm_action_reset
= {
1294 .pre_action
= snd_pcm_pre_reset
,
1295 .do_action
= snd_pcm_do_reset
,
1296 .post_action
= snd_pcm_post_reset
1299 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1301 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1307 /* we use the second argument for updating f_flags */
1308 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1311 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1312 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1313 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1315 if (snd_pcm_running(substream
))
1317 substream
->f_flags
= f_flags
;
1321 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1324 err
= substream
->ops
->prepare(substream
);
1327 return snd_pcm_do_reset(substream
, 0);
1330 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1332 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1333 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1334 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1337 static struct action_ops snd_pcm_action_prepare
= {
1338 .pre_action
= snd_pcm_pre_prepare
,
1339 .do_action
= snd_pcm_do_prepare
,
1340 .post_action
= snd_pcm_post_prepare
1345 * @substream: the PCM substream instance
1346 * @file: file to refer f_flags
1348 * Prepare the PCM substream to be triggerable.
1350 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1354 struct snd_card
*card
= substream
->pcm
->card
;
1358 f_flags
= file
->f_flags
;
1360 f_flags
= substream
->f_flags
;
1362 snd_power_lock(card
);
1363 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1364 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1365 substream
, f_flags
);
1366 snd_power_unlock(card
);
1374 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1376 if (substream
->f_flags
& O_NONBLOCK
)
1378 substream
->runtime
->trigger_master
= substream
;
1382 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1384 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1385 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1386 switch (runtime
->status
->state
) {
1387 case SNDRV_PCM_STATE_PREPARED
:
1388 /* start playback stream if possible */
1389 if (! snd_pcm_playback_empty(substream
)) {
1390 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1391 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1394 case SNDRV_PCM_STATE_RUNNING
:
1395 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1401 /* stop running stream */
1402 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1403 int state
= snd_pcm_capture_avail(runtime
) > 0 ?
1404 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1405 snd_pcm_do_stop(substream
, state
);
1406 snd_pcm_post_stop(substream
, state
);
1412 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1416 static struct action_ops snd_pcm_action_drain_init
= {
1417 .pre_action
= snd_pcm_pre_drain_init
,
1418 .do_action
= snd_pcm_do_drain_init
,
1419 .post_action
= snd_pcm_post_drain_init
1423 struct snd_pcm_substream
*substream
;
1425 snd_pcm_uframes_t stop_threshold
;
1428 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1431 * Drain the stream(s).
1432 * When the substream is linked, sync until the draining of all playback streams
1434 * After this call, all streams are supposed to be either SETUP or DRAINING
1435 * (capture only) state.
1437 static int snd_pcm_drain(struct snd_pcm_substream
*substream
)
1439 struct snd_card
*card
;
1440 struct snd_pcm_runtime
*runtime
;
1441 struct list_head
*pos
;
1444 struct drain_rec
*drec
, drec_tmp
, *d
;
1446 snd_assert(substream
!= NULL
, return -ENXIO
);
1447 card
= substream
->pcm
->card
;
1448 runtime
= substream
->runtime
;
1450 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1453 snd_power_lock(card
);
1454 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1455 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1457 snd_power_unlock(card
);
1462 /* allocate temporary record for drain sync */
1463 down_read(&snd_pcm_link_rwsem
);
1464 if (snd_pcm_stream_linked(substream
)) {
1465 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1467 up_read(&snd_pcm_link_rwsem
);
1468 snd_power_unlock(card
);
1474 /* count only playback streams */
1476 snd_pcm_group_for_each(pos
, substream
) {
1477 struct snd_pcm_substream
*s
= snd_pcm_group_substream_entry(pos
);
1478 runtime
= s
->runtime
;
1479 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1480 d
= &drec
[num_drecs
++];
1482 init_waitqueue_entry(&d
->wait
, current
);
1483 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1484 /* stop_threshold fixup to avoid endless loop when
1485 * stop_threshold > buffer_size
1487 d
->stop_threshold
= runtime
->stop_threshold
;
1488 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1489 runtime
->stop_threshold
= runtime
->buffer_size
;
1492 up_read(&snd_pcm_link_rwsem
);
1494 snd_pcm_stream_lock_irq(substream
);
1496 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1497 snd_pcm_pause(substream
, 0);
1499 /* pre-start/stop - all running streams are changed to DRAINING state */
1500 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1502 snd_pcm_stream_unlock_irq(substream
);
1508 if (signal_pending(current
)) {
1509 result
= -ERESTARTSYS
;
1513 for (i
= 0; i
< num_drecs
; i
++) {
1514 runtime
= drec
[i
].substream
->runtime
;
1515 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1519 break; /* yes, all drained */
1521 set_current_state(TASK_INTERRUPTIBLE
);
1522 snd_pcm_stream_unlock_irq(substream
);
1523 snd_power_unlock(card
);
1524 tout
= schedule_timeout(10 * HZ
);
1525 snd_power_lock(card
);
1526 snd_pcm_stream_lock_irq(substream
);
1528 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1531 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1532 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1539 snd_pcm_stream_unlock_irq(substream
);
1542 for (i
= 0; i
< num_drecs
; i
++) {
1544 runtime
= d
->substream
->runtime
;
1545 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1546 runtime
->stop_threshold
= d
->stop_threshold
;
1549 if (drec
!= &drec_tmp
)
1551 snd_power_unlock(card
);
1559 * Immediately put all linked substreams into SETUP state.
1561 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1563 struct snd_pcm_runtime
*runtime
;
1564 struct snd_card
*card
;
1567 snd_assert(substream
!= NULL
, return -ENXIO
);
1568 runtime
= substream
->runtime
;
1569 card
= substream
->pcm
->card
;
1571 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1572 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1575 snd_power_lock(card
);
1576 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1577 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1582 snd_pcm_stream_lock_irq(substream
);
1584 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1585 snd_pcm_pause(substream
, 0);
1587 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1588 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1589 snd_pcm_stream_unlock_irq(substream
);
1591 snd_power_unlock(card
);
1596 /* WARNING: Don't forget to fput back the file */
1597 static struct file
*snd_pcm_file_fd(int fd
)
1600 struct inode
*inode
;
1606 inode
= file
->f_path
.dentry
->d_inode
;
1607 if (!S_ISCHR(inode
->i_mode
) ||
1608 imajor(inode
) != snd_major
) {
1612 minor
= iminor(inode
);
1613 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1614 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1624 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1628 struct snd_pcm_file
*pcm_file
;
1629 struct snd_pcm_substream
*substream1
;
1631 file
= snd_pcm_file_fd(fd
);
1634 pcm_file
= file
->private_data
;
1635 substream1
= pcm_file
->substream
;
1636 down_write(&snd_pcm_link_rwsem
);
1637 write_lock_irq(&snd_pcm_link_rwlock
);
1638 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1639 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1643 if (snd_pcm_stream_linked(substream1
)) {
1647 if (!snd_pcm_stream_linked(substream
)) {
1648 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1649 if (substream
->group
== NULL
) {
1653 spin_lock_init(&substream
->group
->lock
);
1654 INIT_LIST_HEAD(&substream
->group
->substreams
);
1655 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1656 substream
->group
->count
= 1;
1658 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1659 substream
->group
->count
++;
1660 substream1
->group
= substream
->group
;
1662 write_unlock_irq(&snd_pcm_link_rwlock
);
1663 up_write(&snd_pcm_link_rwsem
);
1668 static void relink_to_local(struct snd_pcm_substream
*substream
)
1670 substream
->group
= &substream
->self_group
;
1671 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1672 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1675 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1677 struct list_head
*pos
;
1680 down_write(&snd_pcm_link_rwsem
);
1681 write_lock_irq(&snd_pcm_link_rwlock
);
1682 if (!snd_pcm_stream_linked(substream
)) {
1686 list_del(&substream
->link_list
);
1687 substream
->group
->count
--;
1688 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1689 snd_pcm_group_for_each(pos
, substream
) {
1690 relink_to_local(snd_pcm_group_substream_entry(pos
));
1693 kfree(substream
->group
);
1695 relink_to_local(substream
);
1697 write_unlock_irq(&snd_pcm_link_rwlock
);
1698 up_write(&snd_pcm_link_rwsem
);
1705 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1706 struct snd_pcm_hw_rule
*rule
)
1708 struct snd_interval t
;
1709 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1710 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1711 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1714 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1715 struct snd_pcm_hw_rule
*rule
)
1717 struct snd_interval t
;
1718 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1719 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1720 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1723 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1724 struct snd_pcm_hw_rule
*rule
)
1726 struct snd_interval t
;
1727 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1728 hw_param_interval_c(params
, rule
->deps
[1]),
1729 (unsigned long) rule
->private, &t
);
1730 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1733 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1734 struct snd_pcm_hw_rule
*rule
)
1736 struct snd_interval t
;
1737 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1738 (unsigned long) rule
->private,
1739 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1740 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1743 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1744 struct snd_pcm_hw_rule
*rule
)
1747 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1749 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1751 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1753 if (! snd_mask_test(mask
, k
))
1755 bits
= snd_pcm_format_physical_width(k
);
1757 continue; /* ignore invalid formats */
1758 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1759 snd_mask_reset(&m
, k
);
1761 return snd_mask_refine(mask
, &m
);
1764 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1765 struct snd_pcm_hw_rule
*rule
)
1767 struct snd_interval t
;
1773 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1775 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1777 bits
= snd_pcm_format_physical_width(k
);
1779 continue; /* ignore invalid formats */
1780 if (t
.min
> (unsigned)bits
)
1782 if (t
.max
< (unsigned)bits
)
1786 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1789 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1790 #error "Change this table"
1793 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1794 48000, 64000, 88200, 96000, 176400, 192000 };
1796 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1797 struct snd_pcm_hw_rule
*rule
)
1799 struct snd_pcm_hardware
*hw
= rule
->private;
1800 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1801 ARRAY_SIZE(rates
), rates
, hw
->rates
);
1804 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1805 struct snd_pcm_hw_rule
*rule
)
1807 struct snd_interval t
;
1808 struct snd_pcm_substream
*substream
= rule
->private;
1810 t
.max
= substream
->buffer_bytes_max
;
1814 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1817 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1819 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1820 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1823 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1824 snd_mask_any(constrs_mask(constrs
, k
));
1827 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1828 snd_interval_any(constrs_interval(constrs
, k
));
1831 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1832 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1833 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1834 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1835 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1837 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1838 snd_pcm_hw_rule_format
, NULL
,
1839 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1842 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1843 snd_pcm_hw_rule_sample_bits
, NULL
,
1844 SNDRV_PCM_HW_PARAM_FORMAT
,
1845 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1848 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1849 snd_pcm_hw_rule_div
, NULL
,
1850 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1853 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1854 snd_pcm_hw_rule_mul
, NULL
,
1855 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1858 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1859 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1860 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1863 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1864 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1868 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1869 snd_pcm_hw_rule_div
, NULL
,
1870 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1873 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1874 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1878 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1879 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1883 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1884 snd_pcm_hw_rule_div
, NULL
,
1885 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1888 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1889 snd_pcm_hw_rule_div
, NULL
,
1890 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1893 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1894 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1895 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1898 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1899 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1900 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1903 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1904 snd_pcm_hw_rule_mul
, NULL
,
1905 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1908 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1909 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1910 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1913 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1914 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1915 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1918 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1919 snd_pcm_hw_rule_muldivk
, (void*) 8,
1920 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1923 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1924 snd_pcm_hw_rule_muldivk
, (void*) 8,
1925 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1928 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1929 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1930 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1933 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1934 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1935 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1941 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1943 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1944 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1946 unsigned int mask
= 0;
1948 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1949 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1950 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1951 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1952 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1953 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1954 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1955 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1956 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1957 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1958 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1960 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1961 snd_assert(err
>= 0, return -EINVAL
);
1963 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1964 snd_assert(err
>= 0, return -EINVAL
);
1966 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1967 snd_assert(err
>= 0, return -EINVAL
);
1969 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1970 hw
->channels_min
, hw
->channels_max
);
1971 snd_assert(err
>= 0, return -EINVAL
);
1973 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1974 hw
->rate_min
, hw
->rate_max
);
1975 snd_assert(err
>= 0, return -EINVAL
);
1977 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1978 hw
->period_bytes_min
, hw
->period_bytes_max
);
1979 snd_assert(err
>= 0, return -EINVAL
);
1981 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1982 hw
->periods_min
, hw
->periods_max
);
1983 snd_assert(err
>= 0, return -EINVAL
);
1985 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1986 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1987 snd_assert(err
>= 0, return -EINVAL
);
1989 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1990 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1991 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1996 if (runtime
->dma_bytes
) {
1997 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1998 snd_assert(err
>= 0, return -EINVAL
);
2001 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2002 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2003 snd_pcm_hw_rule_rate
, hw
,
2004 SNDRV_PCM_HW_PARAM_RATE
, -1);
2009 /* FIXME: this belong to lowlevel */
2010 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_TICK_TIME
,
2011 1000000 / HZ
, 1000000 / HZ
);
2012 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2017 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2019 snd_pcm_unlink(substream
);
2022 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2024 substream
->ref_count
--;
2025 if (substream
->ref_count
> 0)
2028 snd_pcm_drop(substream
);
2029 if (substream
->hw_opened
) {
2030 if (substream
->ops
->hw_free
!= NULL
)
2031 substream
->ops
->hw_free(substream
);
2032 substream
->ops
->close(substream
);
2033 substream
->hw_opened
= 0;
2035 if (substream
->pcm_release
) {
2036 substream
->pcm_release(substream
);
2037 substream
->pcm_release
= NULL
;
2039 snd_pcm_detach_substream(substream
);
2042 EXPORT_SYMBOL(snd_pcm_release_substream
);
2044 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2046 struct snd_pcm_substream
**rsubstream
)
2048 struct snd_pcm_substream
*substream
;
2051 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2054 if (substream
->ref_count
> 1) {
2055 *rsubstream
= substream
;
2059 err
= snd_pcm_hw_constraints_init(substream
);
2061 snd_printd("snd_pcm_hw_constraints_init failed\n");
2065 if ((err
= substream
->ops
->open(substream
)) < 0)
2068 substream
->hw_opened
= 1;
2070 err
= snd_pcm_hw_constraints_complete(substream
);
2072 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2076 *rsubstream
= substream
;
2080 snd_pcm_release_substream(substream
);
2084 EXPORT_SYMBOL(snd_pcm_open_substream
);
2086 static int snd_pcm_open_file(struct file
*file
,
2087 struct snd_pcm
*pcm
,
2089 struct snd_pcm_file
**rpcm_file
)
2091 struct snd_pcm_file
*pcm_file
;
2092 struct snd_pcm_substream
*substream
;
2093 struct snd_pcm_str
*str
;
2096 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
2099 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2103 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2104 if (pcm_file
== NULL
) {
2105 snd_pcm_release_substream(substream
);
2108 pcm_file
->substream
= substream
;
2109 if (substream
->ref_count
== 1) {
2110 str
= substream
->pstr
;
2111 substream
->file
= pcm_file
;
2112 substream
->pcm_release
= pcm_release_private
;
2114 file
->private_data
= pcm_file
;
2115 *rpcm_file
= pcm_file
;
2119 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2121 struct snd_pcm
*pcm
;
2123 pcm
= snd_lookup_minor_data(iminor(inode
),
2124 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2125 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2128 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2130 struct snd_pcm
*pcm
;
2132 pcm
= snd_lookup_minor_data(iminor(inode
),
2133 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2134 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2137 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2140 struct snd_pcm_file
*pcm_file
;
2147 err
= snd_card_file_add(pcm
->card
, file
);
2150 if (!try_module_get(pcm
->card
->module
)) {
2154 init_waitqueue_entry(&wait
, current
);
2155 add_wait_queue(&pcm
->open_wait
, &wait
);
2156 mutex_lock(&pcm
->open_mutex
);
2158 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2161 if (err
== -EAGAIN
) {
2162 if (file
->f_flags
& O_NONBLOCK
) {
2168 set_current_state(TASK_INTERRUPTIBLE
);
2169 mutex_unlock(&pcm
->open_mutex
);
2171 mutex_lock(&pcm
->open_mutex
);
2172 if (signal_pending(current
)) {
2177 remove_wait_queue(&pcm
->open_wait
, &wait
);
2178 mutex_unlock(&pcm
->open_mutex
);
2184 module_put(pcm
->card
->module
);
2186 snd_card_file_remove(pcm
->card
, file
);
2191 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2193 struct snd_pcm
*pcm
;
2194 struct snd_pcm_substream
*substream
;
2195 struct snd_pcm_file
*pcm_file
;
2197 pcm_file
= file
->private_data
;
2198 substream
= pcm_file
->substream
;
2199 snd_assert(substream
!= NULL
, return -ENXIO
);
2200 pcm
= substream
->pcm
;
2201 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2202 mutex_lock(&pcm
->open_mutex
);
2203 snd_pcm_release_substream(substream
);
2205 mutex_unlock(&pcm
->open_mutex
);
2206 wake_up(&pcm
->open_wait
);
2207 module_put(pcm
->card
->module
);
2208 snd_card_file_remove(pcm
->card
, file
);
2212 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2213 snd_pcm_uframes_t frames
)
2215 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2216 snd_pcm_sframes_t appl_ptr
;
2217 snd_pcm_sframes_t ret
;
2218 snd_pcm_sframes_t hw_avail
;
2223 snd_pcm_stream_lock_irq(substream
);
2224 switch (runtime
->status
->state
) {
2225 case SNDRV_PCM_STATE_PREPARED
:
2227 case SNDRV_PCM_STATE_DRAINING
:
2228 case SNDRV_PCM_STATE_RUNNING
:
2229 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2232 case SNDRV_PCM_STATE_XRUN
:
2240 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2241 if (hw_avail
<= 0) {
2245 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2248 frames
-= frames
% runtime
->xfer_align
;
2249 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2251 appl_ptr
+= runtime
->boundary
;
2252 runtime
->control
->appl_ptr
= appl_ptr
;
2253 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2255 snd_pcm_tick_prepare(substream
);
2258 snd_pcm_stream_unlock_irq(substream
);
2262 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2263 snd_pcm_uframes_t frames
)
2265 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2266 snd_pcm_sframes_t appl_ptr
;
2267 snd_pcm_sframes_t ret
;
2268 snd_pcm_sframes_t hw_avail
;
2273 snd_pcm_stream_lock_irq(substream
);
2274 switch (runtime
->status
->state
) {
2275 case SNDRV_PCM_STATE_PREPARED
:
2276 case SNDRV_PCM_STATE_DRAINING
:
2278 case SNDRV_PCM_STATE_RUNNING
:
2279 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2282 case SNDRV_PCM_STATE_XRUN
:
2290 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2291 if (hw_avail
<= 0) {
2295 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2298 frames
-= frames
% runtime
->xfer_align
;
2299 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2301 appl_ptr
+= runtime
->boundary
;
2302 runtime
->control
->appl_ptr
= appl_ptr
;
2303 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2305 snd_pcm_tick_prepare(substream
);
2308 snd_pcm_stream_unlock_irq(substream
);
2312 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2313 snd_pcm_uframes_t frames
)
2315 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2316 snd_pcm_sframes_t appl_ptr
;
2317 snd_pcm_sframes_t ret
;
2318 snd_pcm_sframes_t avail
;
2323 snd_pcm_stream_lock_irq(substream
);
2324 switch (runtime
->status
->state
) {
2325 case SNDRV_PCM_STATE_PREPARED
:
2326 case SNDRV_PCM_STATE_PAUSED
:
2328 case SNDRV_PCM_STATE_DRAINING
:
2329 case SNDRV_PCM_STATE_RUNNING
:
2330 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2333 case SNDRV_PCM_STATE_XRUN
:
2341 avail
= snd_pcm_playback_avail(runtime
);
2346 if (frames
> (snd_pcm_uframes_t
)avail
)
2349 frames
-= frames
% runtime
->xfer_align
;
2350 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2351 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2352 appl_ptr
-= runtime
->boundary
;
2353 runtime
->control
->appl_ptr
= appl_ptr
;
2354 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2356 snd_pcm_tick_prepare(substream
);
2359 snd_pcm_stream_unlock_irq(substream
);
2363 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2364 snd_pcm_uframes_t frames
)
2366 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2367 snd_pcm_sframes_t appl_ptr
;
2368 snd_pcm_sframes_t ret
;
2369 snd_pcm_sframes_t avail
;
2374 snd_pcm_stream_lock_irq(substream
);
2375 switch (runtime
->status
->state
) {
2376 case SNDRV_PCM_STATE_PREPARED
:
2377 case SNDRV_PCM_STATE_DRAINING
:
2378 case SNDRV_PCM_STATE_PAUSED
:
2380 case SNDRV_PCM_STATE_RUNNING
:
2381 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2384 case SNDRV_PCM_STATE_XRUN
:
2392 avail
= snd_pcm_capture_avail(runtime
);
2397 if (frames
> (snd_pcm_uframes_t
)avail
)
2400 frames
-= frames
% runtime
->xfer_align
;
2401 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2402 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2403 appl_ptr
-= runtime
->boundary
;
2404 runtime
->control
->appl_ptr
= appl_ptr
;
2405 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2407 snd_pcm_tick_prepare(substream
);
2410 snd_pcm_stream_unlock_irq(substream
);
2414 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2416 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2419 snd_pcm_stream_lock_irq(substream
);
2420 switch (runtime
->status
->state
) {
2421 case SNDRV_PCM_STATE_DRAINING
:
2422 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2424 case SNDRV_PCM_STATE_RUNNING
:
2425 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2428 case SNDRV_PCM_STATE_PREPARED
:
2429 case SNDRV_PCM_STATE_SUSPENDED
:
2432 case SNDRV_PCM_STATE_XRUN
:
2440 snd_pcm_stream_unlock_irq(substream
);
2444 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2445 snd_pcm_sframes_t __user
*res
)
2447 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2449 snd_pcm_sframes_t n
= 0;
2451 snd_pcm_stream_lock_irq(substream
);
2452 switch (runtime
->status
->state
) {
2453 case SNDRV_PCM_STATE_DRAINING
:
2454 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2456 case SNDRV_PCM_STATE_RUNNING
:
2457 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2460 case SNDRV_PCM_STATE_PREPARED
:
2461 case SNDRV_PCM_STATE_SUSPENDED
:
2463 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2464 n
= snd_pcm_playback_hw_avail(runtime
);
2466 n
= snd_pcm_capture_avail(runtime
);
2468 case SNDRV_PCM_STATE_XRUN
:
2476 snd_pcm_stream_unlock_irq(substream
);
2478 if (put_user(n
, res
))
2483 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2484 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2486 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2487 struct snd_pcm_sync_ptr sync_ptr
;
2488 volatile struct snd_pcm_mmap_status
*status
;
2489 volatile struct snd_pcm_mmap_control
*control
;
2492 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2493 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2495 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2497 status
= runtime
->status
;
2498 control
= runtime
->control
;
2499 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2500 err
= snd_pcm_hwsync(substream
);
2504 snd_pcm_stream_lock_irq(substream
);
2505 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2506 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2508 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2509 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2510 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2512 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2513 sync_ptr
.s
.status
.state
= status
->state
;
2514 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2515 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2516 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2517 snd_pcm_stream_unlock_irq(substream
);
2518 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2523 static int snd_pcm_common_ioctl1(struct file
*file
,
2524 struct snd_pcm_substream
*substream
,
2525 unsigned int cmd
, void __user
*arg
)
2527 snd_assert(substream
!= NULL
, return -ENXIO
);
2530 case SNDRV_PCM_IOCTL_PVERSION
:
2531 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2532 case SNDRV_PCM_IOCTL_INFO
:
2533 return snd_pcm_info_user(substream
, arg
);
2534 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2536 case SNDRV_PCM_IOCTL_HW_REFINE
:
2537 return snd_pcm_hw_refine_user(substream
, arg
);
2538 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2539 return snd_pcm_hw_params_user(substream
, arg
);
2540 case SNDRV_PCM_IOCTL_HW_FREE
:
2541 return snd_pcm_hw_free(substream
);
2542 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2543 return snd_pcm_sw_params_user(substream
, arg
);
2544 case SNDRV_PCM_IOCTL_STATUS
:
2545 return snd_pcm_status_user(substream
, arg
);
2546 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2547 return snd_pcm_channel_info_user(substream
, arg
);
2548 case SNDRV_PCM_IOCTL_PREPARE
:
2549 return snd_pcm_prepare(substream
, file
);
2550 case SNDRV_PCM_IOCTL_RESET
:
2551 return snd_pcm_reset(substream
);
2552 case SNDRV_PCM_IOCTL_START
:
2553 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2554 case SNDRV_PCM_IOCTL_LINK
:
2555 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2556 case SNDRV_PCM_IOCTL_UNLINK
:
2557 return snd_pcm_unlink(substream
);
2558 case SNDRV_PCM_IOCTL_RESUME
:
2559 return snd_pcm_resume(substream
);
2560 case SNDRV_PCM_IOCTL_XRUN
:
2561 return snd_pcm_xrun(substream
);
2562 case SNDRV_PCM_IOCTL_HWSYNC
:
2563 return snd_pcm_hwsync(substream
);
2564 case SNDRV_PCM_IOCTL_DELAY
:
2565 return snd_pcm_delay(substream
, arg
);
2566 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2567 return snd_pcm_sync_ptr(substream
, arg
);
2568 #ifdef CONFIG_SND_SUPPORT_OLD_API
2569 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2570 return snd_pcm_hw_refine_old_user(substream
, arg
);
2571 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2572 return snd_pcm_hw_params_old_user(substream
, arg
);
2574 case SNDRV_PCM_IOCTL_DRAIN
:
2575 return snd_pcm_drain(substream
);
2576 case SNDRV_PCM_IOCTL_DROP
:
2577 return snd_pcm_drop(substream
);
2578 case SNDRV_PCM_IOCTL_PAUSE
:
2581 snd_pcm_stream_lock_irq(substream
);
2582 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2583 snd_pcm_stream_unlock_irq(substream
);
2587 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2591 static int snd_pcm_playback_ioctl1(struct file
*file
,
2592 struct snd_pcm_substream
*substream
,
2593 unsigned int cmd
, void __user
*arg
)
2595 snd_assert(substream
!= NULL
, return -ENXIO
);
2596 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2598 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2600 struct snd_xferi xferi
;
2601 struct snd_xferi __user
*_xferi
= arg
;
2602 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2603 snd_pcm_sframes_t result
;
2604 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2606 if (put_user(0, &_xferi
->result
))
2608 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2610 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2611 __put_user(result
, &_xferi
->result
);
2612 return result
< 0 ? result
: 0;
2614 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2616 struct snd_xfern xfern
;
2617 struct snd_xfern __user
*_xfern
= arg
;
2618 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2620 snd_pcm_sframes_t result
;
2621 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2623 if (runtime
->channels
> 128)
2625 if (put_user(0, &_xfern
->result
))
2627 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2629 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2632 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2636 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2638 __put_user(result
, &_xfern
->result
);
2639 return result
< 0 ? result
: 0;
2641 case SNDRV_PCM_IOCTL_REWIND
:
2643 snd_pcm_uframes_t frames
;
2644 snd_pcm_uframes_t __user
*_frames
= arg
;
2645 snd_pcm_sframes_t result
;
2646 if (get_user(frames
, _frames
))
2648 if (put_user(0, _frames
))
2650 result
= snd_pcm_playback_rewind(substream
, frames
);
2651 __put_user(result
, _frames
);
2652 return result
< 0 ? result
: 0;
2654 case SNDRV_PCM_IOCTL_FORWARD
:
2656 snd_pcm_uframes_t frames
;
2657 snd_pcm_uframes_t __user
*_frames
= arg
;
2658 snd_pcm_sframes_t result
;
2659 if (get_user(frames
, _frames
))
2661 if (put_user(0, _frames
))
2663 result
= snd_pcm_playback_forward(substream
, frames
);
2664 __put_user(result
, _frames
);
2665 return result
< 0 ? result
: 0;
2668 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2671 static int snd_pcm_capture_ioctl1(struct file
*file
,
2672 struct snd_pcm_substream
*substream
,
2673 unsigned int cmd
, void __user
*arg
)
2675 snd_assert(substream
!= NULL
, return -ENXIO
);
2676 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2678 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2680 struct snd_xferi xferi
;
2681 struct snd_xferi __user
*_xferi
= arg
;
2682 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2683 snd_pcm_sframes_t result
;
2684 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2686 if (put_user(0, &_xferi
->result
))
2688 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2690 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2691 __put_user(result
, &_xferi
->result
);
2692 return result
< 0 ? result
: 0;
2694 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2696 struct snd_xfern xfern
;
2697 struct snd_xfern __user
*_xfern
= arg
;
2698 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2700 snd_pcm_sframes_t result
;
2701 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2703 if (runtime
->channels
> 128)
2705 if (put_user(0, &_xfern
->result
))
2707 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2709 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2712 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2716 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2718 __put_user(result
, &_xfern
->result
);
2719 return result
< 0 ? result
: 0;
2721 case SNDRV_PCM_IOCTL_REWIND
:
2723 snd_pcm_uframes_t frames
;
2724 snd_pcm_uframes_t __user
*_frames
= arg
;
2725 snd_pcm_sframes_t result
;
2726 if (get_user(frames
, _frames
))
2728 if (put_user(0, _frames
))
2730 result
= snd_pcm_capture_rewind(substream
, frames
);
2731 __put_user(result
, _frames
);
2732 return result
< 0 ? result
: 0;
2734 case SNDRV_PCM_IOCTL_FORWARD
:
2736 snd_pcm_uframes_t frames
;
2737 snd_pcm_uframes_t __user
*_frames
= arg
;
2738 snd_pcm_sframes_t result
;
2739 if (get_user(frames
, _frames
))
2741 if (put_user(0, _frames
))
2743 result
= snd_pcm_capture_forward(substream
, frames
);
2744 __put_user(result
, _frames
);
2745 return result
< 0 ? result
: 0;
2748 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2751 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2754 struct snd_pcm_file
*pcm_file
;
2756 pcm_file
= file
->private_data
;
2758 if (((cmd
>> 8) & 0xff) != 'A')
2761 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2762 (void __user
*)arg
);
2765 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2768 struct snd_pcm_file
*pcm_file
;
2770 pcm_file
= file
->private_data
;
2772 if (((cmd
>> 8) & 0xff) != 'A')
2775 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2776 (void __user
*)arg
);
2779 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2780 unsigned int cmd
, void *arg
)
2785 fs
= snd_enter_user();
2786 switch (substream
->stream
) {
2787 case SNDRV_PCM_STREAM_PLAYBACK
:
2788 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2789 (void __user
*)arg
);
2791 case SNDRV_PCM_STREAM_CAPTURE
:
2792 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2793 (void __user
*)arg
);
2803 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2805 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2808 struct snd_pcm_file
*pcm_file
;
2809 struct snd_pcm_substream
*substream
;
2810 struct snd_pcm_runtime
*runtime
;
2811 snd_pcm_sframes_t result
;
2813 pcm_file
= file
->private_data
;
2814 substream
= pcm_file
->substream
;
2815 snd_assert(substream
!= NULL
, return -ENXIO
);
2816 runtime
= substream
->runtime
;
2817 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2819 if (!frame_aligned(runtime
, count
))
2821 count
= bytes_to_frames(runtime
, count
);
2822 result
= snd_pcm_lib_read(substream
, buf
, count
);
2824 result
= frames_to_bytes(runtime
, result
);
2828 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2829 size_t count
, loff_t
* offset
)
2831 struct snd_pcm_file
*pcm_file
;
2832 struct snd_pcm_substream
*substream
;
2833 struct snd_pcm_runtime
*runtime
;
2834 snd_pcm_sframes_t result
;
2836 pcm_file
= file
->private_data
;
2837 substream
= pcm_file
->substream
;
2838 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2839 runtime
= substream
->runtime
;
2840 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2844 if (!frame_aligned(runtime
, count
)) {
2848 count
= bytes_to_frames(runtime
, count
);
2849 result
= snd_pcm_lib_write(substream
, buf
, count
);
2851 result
= frames_to_bytes(runtime
, result
);
2856 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2857 unsigned long nr_segs
, loff_t pos
)
2860 struct snd_pcm_file
*pcm_file
;
2861 struct snd_pcm_substream
*substream
;
2862 struct snd_pcm_runtime
*runtime
;
2863 snd_pcm_sframes_t result
;
2866 snd_pcm_uframes_t frames
;
2868 pcm_file
= iocb
->ki_filp
->private_data
;
2869 substream
= pcm_file
->substream
;
2870 snd_assert(substream
!= NULL
, return -ENXIO
);
2871 runtime
= substream
->runtime
;
2872 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2874 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2876 if (!frame_aligned(runtime
, iov
->iov_len
))
2878 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2879 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2882 for (i
= 0; i
< nr_segs
; ++i
)
2883 bufs
[i
] = iov
[i
].iov_base
;
2884 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2886 result
= frames_to_bytes(runtime
, result
);
2891 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2892 unsigned long nr_segs
, loff_t pos
)
2894 struct snd_pcm_file
*pcm_file
;
2895 struct snd_pcm_substream
*substream
;
2896 struct snd_pcm_runtime
*runtime
;
2897 snd_pcm_sframes_t result
;
2900 snd_pcm_uframes_t frames
;
2902 pcm_file
= iocb
->ki_filp
->private_data
;
2903 substream
= pcm_file
->substream
;
2904 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2905 runtime
= substream
->runtime
;
2906 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2910 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2911 !frame_aligned(runtime
, iov
->iov_len
)) {
2915 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2916 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2919 for (i
= 0; i
< nr_segs
; ++i
)
2920 bufs
[i
] = iov
[i
].iov_base
;
2921 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2923 result
= frames_to_bytes(runtime
, result
);
2929 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2931 struct snd_pcm_file
*pcm_file
;
2932 struct snd_pcm_substream
*substream
;
2933 struct snd_pcm_runtime
*runtime
;
2935 snd_pcm_uframes_t avail
;
2937 pcm_file
= file
->private_data
;
2939 substream
= pcm_file
->substream
;
2940 snd_assert(substream
!= NULL
, return -ENXIO
);
2941 runtime
= substream
->runtime
;
2943 poll_wait(file
, &runtime
->sleep
, wait
);
2945 snd_pcm_stream_lock_irq(substream
);
2946 avail
= snd_pcm_playback_avail(runtime
);
2947 switch (runtime
->status
->state
) {
2948 case SNDRV_PCM_STATE_RUNNING
:
2949 case SNDRV_PCM_STATE_PREPARED
:
2950 case SNDRV_PCM_STATE_PAUSED
:
2951 if (avail
>= runtime
->control
->avail_min
) {
2952 mask
= POLLOUT
| POLLWRNORM
;
2956 case SNDRV_PCM_STATE_DRAINING
:
2960 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2963 snd_pcm_stream_unlock_irq(substream
);
2967 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2969 struct snd_pcm_file
*pcm_file
;
2970 struct snd_pcm_substream
*substream
;
2971 struct snd_pcm_runtime
*runtime
;
2973 snd_pcm_uframes_t avail
;
2975 pcm_file
= file
->private_data
;
2977 substream
= pcm_file
->substream
;
2978 snd_assert(substream
!= NULL
, return -ENXIO
);
2979 runtime
= substream
->runtime
;
2981 poll_wait(file
, &runtime
->sleep
, wait
);
2983 snd_pcm_stream_lock_irq(substream
);
2984 avail
= snd_pcm_capture_avail(runtime
);
2985 switch (runtime
->status
->state
) {
2986 case SNDRV_PCM_STATE_RUNNING
:
2987 case SNDRV_PCM_STATE_PREPARED
:
2988 case SNDRV_PCM_STATE_PAUSED
:
2989 if (avail
>= runtime
->control
->avail_min
) {
2990 mask
= POLLIN
| POLLRDNORM
;
2995 case SNDRV_PCM_STATE_DRAINING
:
2997 mask
= POLLIN
| POLLRDNORM
;
3002 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3005 snd_pcm_stream_unlock_irq(substream
);
3014 * Only on coherent architectures, we can mmap the status and the control records
3015 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3017 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3019 * mmap status record
3021 static struct page
* snd_pcm_mmap_status_nopage(struct vm_area_struct
*area
,
3022 unsigned long address
, int *type
)
3024 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3025 struct snd_pcm_runtime
*runtime
;
3028 if (substream
== NULL
)
3029 return NOPAGE_SIGBUS
;
3030 runtime
= substream
->runtime
;
3031 page
= virt_to_page(runtime
->status
);
3034 *type
= VM_FAULT_MINOR
;
3038 static struct vm_operations_struct snd_pcm_vm_ops_status
=
3040 .nopage
= snd_pcm_mmap_status_nopage
,
3043 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3044 struct vm_area_struct
*area
)
3046 struct snd_pcm_runtime
*runtime
;
3048 if (!(area
->vm_flags
& VM_READ
))
3050 runtime
= substream
->runtime
;
3051 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3052 size
= area
->vm_end
- area
->vm_start
;
3053 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3055 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3056 area
->vm_private_data
= substream
;
3057 area
->vm_flags
|= VM_RESERVED
;
3062 * mmap control record
3064 static struct page
* snd_pcm_mmap_control_nopage(struct vm_area_struct
*area
,
3065 unsigned long address
, int *type
)
3067 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3068 struct snd_pcm_runtime
*runtime
;
3071 if (substream
== NULL
)
3072 return NOPAGE_SIGBUS
;
3073 runtime
= substream
->runtime
;
3074 page
= virt_to_page(runtime
->control
);
3077 *type
= VM_FAULT_MINOR
;
3081 static struct vm_operations_struct snd_pcm_vm_ops_control
=
3083 .nopage
= snd_pcm_mmap_control_nopage
,
3086 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3087 struct vm_area_struct
*area
)
3089 struct snd_pcm_runtime
*runtime
;
3091 if (!(area
->vm_flags
& VM_READ
))
3093 runtime
= substream
->runtime
;
3094 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3095 size
= area
->vm_end
- area
->vm_start
;
3096 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3098 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3099 area
->vm_private_data
= substream
;
3100 area
->vm_flags
|= VM_RESERVED
;
3103 #else /* ! coherent mmap */
3105 * don't support mmap for status and control records.
3107 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3108 struct vm_area_struct
*area
)
3112 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3113 struct vm_area_struct
*area
)
3117 #endif /* coherent mmap */
3120 * nopage callback for mmapping a RAM page
3122 static struct page
*snd_pcm_mmap_data_nopage(struct vm_area_struct
*area
,
3123 unsigned long address
, int *type
)
3125 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3126 struct snd_pcm_runtime
*runtime
;
3127 unsigned long offset
;
3132 if (substream
== NULL
)
3133 return NOPAGE_SIGBUS
;
3134 runtime
= substream
->runtime
;
3135 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3136 offset
+= address
- area
->vm_start
;
3137 snd_assert((offset
% PAGE_SIZE
) == 0, return NOPAGE_SIGBUS
);
3138 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3139 if (offset
> dma_bytes
- PAGE_SIZE
)
3140 return NOPAGE_SIGBUS
;
3141 if (substream
->ops
->page
) {
3142 page
= substream
->ops
->page(substream
, offset
);
3144 return NOPAGE_OOM
; /* XXX: is this really due to OOM? */
3146 vaddr
= runtime
->dma_area
+ offset
;
3147 page
= virt_to_page(vaddr
);
3151 *type
= VM_FAULT_MINOR
;
3155 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3157 .open
= snd_pcm_mmap_data_open
,
3158 .close
= snd_pcm_mmap_data_close
,
3159 .nopage
= snd_pcm_mmap_data_nopage
,
3163 * mmap the DMA buffer on RAM
3165 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3166 struct vm_area_struct
*area
)
3168 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3169 area
->vm_private_data
= substream
;
3170 area
->vm_flags
|= VM_RESERVED
;
3171 atomic_inc(&substream
->mmap_count
);
3176 * mmap the DMA buffer on I/O memory area
3178 #if SNDRV_PCM_INFO_MMAP_IOMEM
3179 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3181 .open
= snd_pcm_mmap_data_open
,
3182 .close
= snd_pcm_mmap_data_close
,
3185 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3186 struct vm_area_struct
*area
)
3189 unsigned long offset
;
3191 #ifdef pgprot_noncached
3192 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3194 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3195 area
->vm_private_data
= substream
;
3196 area
->vm_flags
|= VM_IO
;
3197 size
= area
->vm_end
- area
->vm_start
;
3198 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3199 if (io_remap_pfn_range(area
, area
->vm_start
,
3200 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3201 size
, area
->vm_page_prot
))
3203 atomic_inc(&substream
->mmap_count
);
3207 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3208 #endif /* SNDRV_PCM_INFO_MMAP */
3213 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3214 struct vm_area_struct
*area
)
3216 struct snd_pcm_runtime
*runtime
;
3218 unsigned long offset
;
3221 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3222 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3225 if (!(area
->vm_flags
& VM_READ
))
3228 runtime
= substream
->runtime
;
3229 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3230 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3232 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3234 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3235 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3237 size
= area
->vm_end
- area
->vm_start
;
3238 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3239 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3240 if ((size_t)size
> dma_bytes
)
3242 if (offset
> dma_bytes
- size
)
3245 if (substream
->ops
->mmap
)
3246 return substream
->ops
->mmap(substream
, area
);
3248 return snd_pcm_default_mmap(substream
, area
);
3251 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3253 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3255 struct snd_pcm_file
* pcm_file
;
3256 struct snd_pcm_substream
*substream
;
3257 unsigned long offset
;
3259 pcm_file
= file
->private_data
;
3260 substream
= pcm_file
->substream
;
3261 snd_assert(substream
!= NULL
, return -ENXIO
);
3263 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3265 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3266 if (pcm_file
->no_compat_mmap
)
3268 return snd_pcm_mmap_status(substream
, file
, area
);
3269 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3270 if (pcm_file
->no_compat_mmap
)
3272 return snd_pcm_mmap_control(substream
, file
, area
);
3274 return snd_pcm_mmap_data(substream
, file
, area
);
3279 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3281 struct snd_pcm_file
* pcm_file
;
3282 struct snd_pcm_substream
*substream
;
3283 struct snd_pcm_runtime
*runtime
;
3286 pcm_file
= file
->private_data
;
3287 substream
= pcm_file
->substream
;
3288 snd_assert(substream
!= NULL
, return -ENXIO
);
3289 runtime
= substream
->runtime
;
3291 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3300 #ifdef CONFIG_COMPAT
3301 #include "pcm_compat.c"
3303 #define snd_pcm_ioctl_compat NULL
3307 * To be removed helpers to keep binary compatibility
3310 #ifdef CONFIG_SND_SUPPORT_OLD_API
3311 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3312 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3314 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3315 struct snd_pcm_hw_params_old
*oparams
)
3319 memset(params
, 0, sizeof(*params
));
3320 params
->flags
= oparams
->flags
;
3321 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3322 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3323 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3324 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3325 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3326 params
->info
= oparams
->info
;
3327 params
->msbits
= oparams
->msbits
;
3328 params
->rate_num
= oparams
->rate_num
;
3329 params
->rate_den
= oparams
->rate_den
;
3330 params
->fifo_size
= oparams
->fifo_size
;
3333 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3334 struct snd_pcm_hw_params
*params
)
3338 memset(oparams
, 0, sizeof(*oparams
));
3339 oparams
->flags
= params
->flags
;
3340 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3341 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3342 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3343 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3344 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3345 oparams
->info
= params
->info
;
3346 oparams
->msbits
= params
->msbits
;
3347 oparams
->rate_num
= params
->rate_num
;
3348 oparams
->rate_den
= params
->rate_den
;
3349 oparams
->fifo_size
= params
->fifo_size
;
3352 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3353 struct snd_pcm_hw_params_old __user
* _oparams
)
3355 struct snd_pcm_hw_params
*params
;
3356 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3359 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3364 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3370 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3374 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3375 err
= snd_pcm_hw_refine(substream
, params
);
3376 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3377 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3387 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3388 struct snd_pcm_hw_params_old __user
* _oparams
)
3390 struct snd_pcm_hw_params
*params
;
3391 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3394 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3399 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3404 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3408 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3409 err
= snd_pcm_hw_params(substream
, params
);
3410 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3411 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3420 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3426 const struct file_operations snd_pcm_f_ops
[2] = {
3428 .owner
= THIS_MODULE
,
3429 .write
= snd_pcm_write
,
3430 .aio_write
= snd_pcm_aio_write
,
3431 .open
= snd_pcm_playback_open
,
3432 .release
= snd_pcm_release
,
3433 .poll
= snd_pcm_playback_poll
,
3434 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3435 .compat_ioctl
= snd_pcm_ioctl_compat
,
3436 .mmap
= snd_pcm_mmap
,
3437 .fasync
= snd_pcm_fasync
,
3440 .owner
= THIS_MODULE
,
3441 .read
= snd_pcm_read
,
3442 .aio_read
= snd_pcm_aio_read
,
3443 .open
= snd_pcm_capture_open
,
3444 .release
= snd_pcm_release
,
3445 .poll
= snd_pcm_capture_poll
,
3446 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3447 .compat_ioctl
= snd_pcm_ioctl_compat
,
3448 .mmap
= snd_pcm_mmap
,
3449 .fasync
= snd_pcm_fasync
,