2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
42 struct snd_pcm_hw_params_old
{
44 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
45 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
46 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
52 unsigned int rate_num
;
53 unsigned int rate_den
;
54 snd_pcm_uframes_t fifo_size
;
55 unsigned char reserved
[64];
58 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
63 struct snd_pcm_hw_params_old __user
* _oparams
);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
65 struct snd_pcm_hw_params_old __user
* _oparams
);
67 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
74 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
78 static inline mm_segment_t
snd_enter_user(void)
80 mm_segment_t fs
= get_fs();
85 static inline void snd_leave_user(mm_segment_t fs
)
92 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
94 struct snd_pcm_runtime
*runtime
;
95 struct snd_pcm
*pcm
= substream
->pcm
;
96 struct snd_pcm_str
*pstr
= substream
->pstr
;
98 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 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
451 substream
->latency_id
);
452 if ((usecs
= period_to_usecs(runtime
)) >= 0)
453 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY
,
454 substream
->latency_id
, usecs
);
457 /* hardware might be unuseable from this time,
458 so we force application to retry to set
459 the correct hardware parameter settings */
460 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
461 if (substream
->ops
->hw_free
!= NULL
)
462 substream
->ops
->hw_free(substream
);
466 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
467 struct snd_pcm_hw_params __user
* _params
)
469 struct snd_pcm_hw_params
*params
;
472 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
477 if (copy_from_user(params
, _params
, sizeof(*params
))) {
481 err
= snd_pcm_hw_params(substream
, params
);
482 if (copy_to_user(_params
, params
, sizeof(*params
))) {
491 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
493 struct snd_pcm_runtime
*runtime
;
496 snd_assert(substream
!= NULL
, return -ENXIO
);
497 runtime
= substream
->runtime
;
498 snd_assert(runtime
!= NULL
, return -ENXIO
);
499 snd_pcm_stream_lock_irq(substream
);
500 switch (runtime
->status
->state
) {
501 case SNDRV_PCM_STATE_SETUP
:
502 case SNDRV_PCM_STATE_PREPARED
:
505 snd_pcm_stream_unlock_irq(substream
);
508 snd_pcm_stream_unlock_irq(substream
);
509 if (atomic_read(&substream
->mmap_count
))
511 if (substream
->ops
->hw_free
)
512 result
= substream
->ops
->hw_free(substream
);
513 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
514 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
515 substream
->latency_id
);
519 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
520 struct snd_pcm_sw_params
*params
)
522 struct snd_pcm_runtime
*runtime
;
524 snd_assert(substream
!= NULL
, return -ENXIO
);
525 runtime
= substream
->runtime
;
526 snd_assert(runtime
!= NULL
, return -ENXIO
);
527 snd_pcm_stream_lock_irq(substream
);
528 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
529 snd_pcm_stream_unlock_irq(substream
);
532 snd_pcm_stream_unlock_irq(substream
);
534 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
536 if (params
->avail_min
== 0)
538 if (params
->xfer_align
== 0 ||
539 params
->xfer_align
% runtime
->min_align
!= 0)
541 if (params
->silence_size
>= runtime
->boundary
) {
542 if (params
->silence_threshold
!= 0)
545 if (params
->silence_size
> params
->silence_threshold
)
547 if (params
->silence_threshold
> runtime
->buffer_size
)
550 snd_pcm_stream_lock_irq(substream
);
551 runtime
->tstamp_mode
= params
->tstamp_mode
;
552 runtime
->sleep_min
= params
->sleep_min
;
553 runtime
->period_step
= params
->period_step
;
554 runtime
->control
->avail_min
= params
->avail_min
;
555 runtime
->start_threshold
= params
->start_threshold
;
556 runtime
->stop_threshold
= params
->stop_threshold
;
557 runtime
->silence_threshold
= params
->silence_threshold
;
558 runtime
->silence_size
= params
->silence_size
;
559 runtime
->xfer_align
= params
->xfer_align
;
560 params
->boundary
= runtime
->boundary
;
561 if (snd_pcm_running(substream
)) {
562 if (runtime
->sleep_min
)
563 snd_pcm_tick_prepare(substream
);
565 snd_pcm_tick_set(substream
, 0);
566 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
567 runtime
->silence_size
> 0)
568 snd_pcm_playback_silence(substream
, ULONG_MAX
);
569 wake_up(&runtime
->sleep
);
571 snd_pcm_stream_unlock_irq(substream
);
575 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
576 struct snd_pcm_sw_params __user
* _params
)
578 struct snd_pcm_sw_params params
;
580 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
582 err
= snd_pcm_sw_params(substream
, ¶ms
);
583 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
588 int snd_pcm_status(struct snd_pcm_substream
*substream
,
589 struct snd_pcm_status
*status
)
591 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
593 snd_pcm_stream_lock_irq(substream
);
594 status
->state
= runtime
->status
->state
;
595 status
->suspended_state
= runtime
->status
->suspended_state
;
596 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
598 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
599 if (snd_pcm_running(substream
)) {
600 snd_pcm_update_hw_ptr(substream
);
601 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
602 status
->tstamp
= runtime
->status
->tstamp
;
604 getnstimeofday(&status
->tstamp
);
606 getnstimeofday(&status
->tstamp
);
607 status
->appl_ptr
= runtime
->control
->appl_ptr
;
608 status
->hw_ptr
= runtime
->status
->hw_ptr
;
609 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
610 status
->avail
= snd_pcm_playback_avail(runtime
);
611 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
612 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
613 status
->delay
= runtime
->buffer_size
- status
->avail
;
617 status
->avail
= snd_pcm_capture_avail(runtime
);
618 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
619 status
->delay
= status
->avail
;
623 status
->avail_max
= runtime
->avail_max
;
624 status
->overrange
= runtime
->overrange
;
625 runtime
->avail_max
= 0;
626 runtime
->overrange
= 0;
628 snd_pcm_stream_unlock_irq(substream
);
632 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
633 struct snd_pcm_status __user
* _status
)
635 struct snd_pcm_status status
;
636 struct snd_pcm_runtime
*runtime
;
639 snd_assert(substream
!= NULL
, return -ENXIO
);
640 runtime
= substream
->runtime
;
641 memset(&status
, 0, sizeof(status
));
642 res
= snd_pcm_status(substream
, &status
);
645 if (copy_to_user(_status
, &status
, sizeof(status
)))
650 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
651 struct snd_pcm_channel_info
* info
)
653 struct snd_pcm_runtime
*runtime
;
654 unsigned int channel
;
656 snd_assert(substream
!= NULL
, return -ENXIO
);
657 channel
= info
->channel
;
658 runtime
= substream
->runtime
;
659 snd_pcm_stream_lock_irq(substream
);
660 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
661 snd_pcm_stream_unlock_irq(substream
);
664 snd_pcm_stream_unlock_irq(substream
);
665 if (channel
>= runtime
->channels
)
667 memset(info
, 0, sizeof(*info
));
668 info
->channel
= channel
;
669 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
672 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
673 struct snd_pcm_channel_info __user
* _info
)
675 struct snd_pcm_channel_info info
;
678 if (copy_from_user(&info
, _info
, sizeof(info
)))
680 res
= snd_pcm_channel_info(substream
, &info
);
683 if (copy_to_user(_info
, &info
, sizeof(info
)))
688 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
690 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
691 if (runtime
->trigger_master
== NULL
)
693 if (runtime
->trigger_master
== substream
) {
694 getnstimeofday(&runtime
->trigger_tstamp
);
696 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
697 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
699 runtime
->trigger_master
= NULL
;
703 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
704 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
705 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
706 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
710 * this functions is core for handling of linked stream
711 * Note: the stream state might be changed also on failure
712 * Note2: call with calling stream lock + link lock
714 static int snd_pcm_action_group(struct action_ops
*ops
,
715 struct snd_pcm_substream
*substream
,
716 int state
, int do_lock
)
718 struct snd_pcm_substream
*s
= NULL
;
719 struct snd_pcm_substream
*s1
;
722 snd_pcm_group_for_each_entry(s
, substream
) {
723 if (do_lock
&& s
!= substream
)
724 spin_lock_nested(&s
->self_group
.lock
,
725 SINGLE_DEPTH_NESTING
);
726 res
= ops
->pre_action(s
, state
);
730 snd_pcm_group_for_each_entry(s
, substream
) {
731 res
= ops
->do_action(s
, state
);
733 if (ops
->undo_action
) {
734 snd_pcm_group_for_each_entry(s1
, substream
) {
735 if (s1
== s
) /* failed stream */
737 ops
->undo_action(s1
, state
);
740 s
= NULL
; /* unlock all */
744 snd_pcm_group_for_each_entry(s
, substream
) {
745 ops
->post_action(s
, state
);
750 snd_pcm_group_for_each_entry(s1
, substream
) {
752 spin_unlock(&s1
->self_group
.lock
);
753 if (s1
== s
) /* end */
761 * Note: call with stream lock
763 static int snd_pcm_action_single(struct action_ops
*ops
,
764 struct snd_pcm_substream
*substream
,
769 res
= ops
->pre_action(substream
, state
);
772 res
= ops
->do_action(substream
, state
);
774 ops
->post_action(substream
, state
);
775 else if (ops
->undo_action
)
776 ops
->undo_action(substream
, state
);
781 * Note: call with stream lock
783 static int snd_pcm_action(struct action_ops
*ops
,
784 struct snd_pcm_substream
*substream
,
789 if (snd_pcm_stream_linked(substream
)) {
790 if (!spin_trylock(&substream
->group
->lock
)) {
791 spin_unlock(&substream
->self_group
.lock
);
792 spin_lock(&substream
->group
->lock
);
793 spin_lock(&substream
->self_group
.lock
);
795 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
796 spin_unlock(&substream
->group
->lock
);
798 res
= snd_pcm_action_single(ops
, substream
, state
);
804 * Note: don't use any locks before
806 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
807 struct snd_pcm_substream
*substream
,
812 read_lock_irq(&snd_pcm_link_rwlock
);
813 if (snd_pcm_stream_linked(substream
)) {
814 spin_lock(&substream
->group
->lock
);
815 spin_lock(&substream
->self_group
.lock
);
816 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
817 spin_unlock(&substream
->self_group
.lock
);
818 spin_unlock(&substream
->group
->lock
);
820 spin_lock(&substream
->self_group
.lock
);
821 res
= snd_pcm_action_single(ops
, substream
, state
);
822 spin_unlock(&substream
->self_group
.lock
);
824 read_unlock_irq(&snd_pcm_link_rwlock
);
830 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
831 struct snd_pcm_substream
*substream
,
836 down_read(&snd_pcm_link_rwsem
);
837 if (snd_pcm_stream_linked(substream
))
838 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
840 res
= snd_pcm_action_single(ops
, substream
, state
);
841 up_read(&snd_pcm_link_rwsem
);
848 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
850 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
851 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
853 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
854 !snd_pcm_playback_data(substream
))
856 runtime
->trigger_master
= substream
;
860 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
862 if (substream
->runtime
->trigger_master
!= substream
)
864 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
867 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
869 if (substream
->runtime
->trigger_master
== substream
)
870 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
873 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
875 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
876 snd_pcm_trigger_tstamp(substream
);
877 runtime
->status
->state
= state
;
878 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
879 runtime
->silence_size
> 0)
880 snd_pcm_playback_silence(substream
, ULONG_MAX
);
881 if (runtime
->sleep_min
)
882 snd_pcm_tick_prepare(substream
);
883 if (substream
->timer
)
884 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
885 &runtime
->trigger_tstamp
);
888 static struct action_ops snd_pcm_action_start
= {
889 .pre_action
= snd_pcm_pre_start
,
890 .do_action
= snd_pcm_do_start
,
891 .undo_action
= snd_pcm_undo_start
,
892 .post_action
= snd_pcm_post_start
897 * @substream: the PCM substream instance
899 * Start all linked streams.
901 int snd_pcm_start(struct snd_pcm_substream
*substream
)
903 return snd_pcm_action(&snd_pcm_action_start
, substream
,
904 SNDRV_PCM_STATE_RUNNING
);
910 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
912 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
913 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
915 runtime
->trigger_master
= substream
;
919 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
921 if (substream
->runtime
->trigger_master
== substream
&&
922 snd_pcm_running(substream
))
923 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
924 return 0; /* unconditonally stop all substreams */
927 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
929 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
930 if (runtime
->status
->state
!= state
) {
931 snd_pcm_trigger_tstamp(substream
);
932 if (substream
->timer
)
933 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
934 &runtime
->trigger_tstamp
);
935 runtime
->status
->state
= state
;
936 snd_pcm_tick_set(substream
, 0);
938 wake_up(&runtime
->sleep
);
941 static struct action_ops snd_pcm_action_stop
= {
942 .pre_action
= snd_pcm_pre_stop
,
943 .do_action
= snd_pcm_do_stop
,
944 .post_action
= snd_pcm_post_stop
949 * @substream: the PCM substream instance
950 * @state: PCM state after stopping the stream
952 * Try to stop all running streams in the substream group.
953 * The state of each stream is changed to the given value after that unconditionally.
955 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
957 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
960 EXPORT_SYMBOL(snd_pcm_stop
);
964 * @substream: the PCM substream
966 * Stop the DMA only when the given stream is playback.
967 * The state is changed to SETUP.
968 * Unlike snd_pcm_stop(), this affects only the given stream.
970 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
972 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
973 SNDRV_PCM_STATE_SETUP
);
979 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
981 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
982 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
985 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
987 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
989 runtime
->trigger_master
= substream
;
993 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
995 if (substream
->runtime
->trigger_master
!= substream
)
997 return substream
->ops
->trigger(substream
,
998 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
999 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1002 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1004 if (substream
->runtime
->trigger_master
== substream
)
1005 substream
->ops
->trigger(substream
,
1006 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1007 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1010 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1012 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1013 snd_pcm_trigger_tstamp(substream
);
1015 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1016 if (substream
->timer
)
1017 snd_timer_notify(substream
->timer
,
1018 SNDRV_TIMER_EVENT_MPAUSE
,
1019 &runtime
->trigger_tstamp
);
1020 snd_pcm_tick_set(substream
, 0);
1021 wake_up(&runtime
->sleep
);
1023 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1024 if (runtime
->sleep_min
)
1025 snd_pcm_tick_prepare(substream
);
1026 if (substream
->timer
)
1027 snd_timer_notify(substream
->timer
,
1028 SNDRV_TIMER_EVENT_MCONTINUE
,
1029 &runtime
->trigger_tstamp
);
1033 static struct action_ops snd_pcm_action_pause
= {
1034 .pre_action
= snd_pcm_pre_pause
,
1035 .do_action
= snd_pcm_do_pause
,
1036 .undo_action
= snd_pcm_undo_pause
,
1037 .post_action
= snd_pcm_post_pause
1041 * Push/release the pause for all linked streams.
1043 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1045 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1051 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1053 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1054 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1056 runtime
->trigger_master
= substream
;
1060 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1062 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1063 if (runtime
->trigger_master
!= substream
)
1065 if (! snd_pcm_running(substream
))
1067 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1068 return 0; /* suspend unconditionally */
1071 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1073 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1074 snd_pcm_trigger_tstamp(substream
);
1075 if (substream
->timer
)
1076 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1077 &runtime
->trigger_tstamp
);
1078 runtime
->status
->suspended_state
= runtime
->status
->state
;
1079 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1080 snd_pcm_tick_set(substream
, 0);
1081 wake_up(&runtime
->sleep
);
1084 static struct action_ops snd_pcm_action_suspend
= {
1085 .pre_action
= snd_pcm_pre_suspend
,
1086 .do_action
= snd_pcm_do_suspend
,
1087 .post_action
= snd_pcm_post_suspend
1092 * @substream: the PCM substream
1094 * Trigger SUSPEND to all linked streams.
1095 * After this call, all streams are changed to SUSPENDED state.
1097 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1100 unsigned long flags
;
1105 snd_pcm_stream_lock_irqsave(substream
, flags
);
1106 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1107 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1111 EXPORT_SYMBOL(snd_pcm_suspend
);
1114 * snd_pcm_suspend_all
1115 * @pcm: the PCM instance
1117 * Trigger SUSPEND to all substreams in the given pcm.
1118 * After this call, all streams are changed to SUSPENDED state.
1120 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1122 struct snd_pcm_substream
*substream
;
1123 int stream
, err
= 0;
1128 for (stream
= 0; stream
< 2; stream
++) {
1129 for (substream
= pcm
->streams
[stream
].substream
;
1130 substream
; substream
= substream
->next
) {
1131 /* FIXME: the open/close code should lock this as well */
1132 if (substream
->runtime
== NULL
)
1134 err
= snd_pcm_suspend(substream
);
1135 if (err
< 0 && err
!= -EBUSY
)
1142 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1146 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1148 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1149 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1151 runtime
->trigger_master
= substream
;
1155 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1157 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1158 if (runtime
->trigger_master
!= substream
)
1160 /* DMA not running previously? */
1161 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1162 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1163 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1165 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1168 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1170 if (substream
->runtime
->trigger_master
== substream
&&
1171 snd_pcm_running(substream
))
1172 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1175 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1177 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1178 snd_pcm_trigger_tstamp(substream
);
1179 if (substream
->timer
)
1180 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1181 &runtime
->trigger_tstamp
);
1182 runtime
->status
->state
= runtime
->status
->suspended_state
;
1183 if (runtime
->sleep_min
)
1184 snd_pcm_tick_prepare(substream
);
1187 static struct action_ops snd_pcm_action_resume
= {
1188 .pre_action
= snd_pcm_pre_resume
,
1189 .do_action
= snd_pcm_do_resume
,
1190 .undo_action
= snd_pcm_undo_resume
,
1191 .post_action
= snd_pcm_post_resume
1194 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1196 struct snd_card
*card
= substream
->pcm
->card
;
1199 snd_power_lock(card
);
1200 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1201 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1202 snd_power_unlock(card
);
1208 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1213 #endif /* CONFIG_PM */
1218 * Change the RUNNING stream(s) to XRUN state.
1220 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1222 struct snd_card
*card
= substream
->pcm
->card
;
1223 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1226 snd_power_lock(card
);
1227 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1228 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1233 snd_pcm_stream_lock_irq(substream
);
1234 switch (runtime
->status
->state
) {
1235 case SNDRV_PCM_STATE_XRUN
:
1236 result
= 0; /* already there */
1238 case SNDRV_PCM_STATE_RUNNING
:
1239 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1244 snd_pcm_stream_unlock_irq(substream
);
1246 snd_power_unlock(card
);
1253 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1255 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1256 switch (runtime
->status
->state
) {
1257 case SNDRV_PCM_STATE_RUNNING
:
1258 case SNDRV_PCM_STATE_PREPARED
:
1259 case SNDRV_PCM_STATE_PAUSED
:
1260 case SNDRV_PCM_STATE_SUSPENDED
:
1267 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1269 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1270 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1273 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1274 runtime
->hw_ptr_base
= 0;
1275 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1276 runtime
->status
->hw_ptr
% runtime
->period_size
;
1277 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1278 runtime
->silence_filled
= 0;
1282 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1284 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1285 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1286 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1287 runtime
->silence_size
> 0)
1288 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1291 static struct action_ops snd_pcm_action_reset
= {
1292 .pre_action
= snd_pcm_pre_reset
,
1293 .do_action
= snd_pcm_do_reset
,
1294 .post_action
= snd_pcm_post_reset
1297 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1299 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1305 /* we use the second argument for updating f_flags */
1306 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1309 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1310 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1311 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1313 if (snd_pcm_running(substream
))
1315 substream
->f_flags
= f_flags
;
1319 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1322 err
= substream
->ops
->prepare(substream
);
1325 return snd_pcm_do_reset(substream
, 0);
1328 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1330 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1331 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1332 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1335 static struct action_ops snd_pcm_action_prepare
= {
1336 .pre_action
= snd_pcm_pre_prepare
,
1337 .do_action
= snd_pcm_do_prepare
,
1338 .post_action
= snd_pcm_post_prepare
1343 * @substream: the PCM substream instance
1344 * @file: file to refer f_flags
1346 * Prepare the PCM substream to be triggerable.
1348 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1352 struct snd_card
*card
= substream
->pcm
->card
;
1356 f_flags
= file
->f_flags
;
1358 f_flags
= substream
->f_flags
;
1360 snd_power_lock(card
);
1361 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1362 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1363 substream
, f_flags
);
1364 snd_power_unlock(card
);
1372 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1374 if (substream
->f_flags
& O_NONBLOCK
)
1376 substream
->runtime
->trigger_master
= substream
;
1380 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1382 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1383 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1384 switch (runtime
->status
->state
) {
1385 case SNDRV_PCM_STATE_PREPARED
:
1386 /* start playback stream if possible */
1387 if (! snd_pcm_playback_empty(substream
)) {
1388 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1389 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1392 case SNDRV_PCM_STATE_RUNNING
:
1393 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1399 /* stop running stream */
1400 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1401 int state
= snd_pcm_capture_avail(runtime
) > 0 ?
1402 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1403 snd_pcm_do_stop(substream
, state
);
1404 snd_pcm_post_stop(substream
, state
);
1410 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1414 static struct action_ops snd_pcm_action_drain_init
= {
1415 .pre_action
= snd_pcm_pre_drain_init
,
1416 .do_action
= snd_pcm_do_drain_init
,
1417 .post_action
= snd_pcm_post_drain_init
1421 struct snd_pcm_substream
*substream
;
1423 snd_pcm_uframes_t stop_threshold
;
1426 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1429 * Drain the stream(s).
1430 * When the substream is linked, sync until the draining of all playback streams
1432 * After this call, all streams are supposed to be either SETUP or DRAINING
1433 * (capture only) state.
1435 static int snd_pcm_drain(struct snd_pcm_substream
*substream
)
1437 struct snd_card
*card
;
1438 struct snd_pcm_runtime
*runtime
;
1439 struct snd_pcm_substream
*s
;
1442 struct drain_rec
*drec
, drec_tmp
, *d
;
1444 snd_assert(substream
!= NULL
, return -ENXIO
);
1445 card
= substream
->pcm
->card
;
1446 runtime
= substream
->runtime
;
1448 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1451 snd_power_lock(card
);
1452 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1453 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1455 snd_power_unlock(card
);
1460 /* allocate temporary record for drain sync */
1461 down_read(&snd_pcm_link_rwsem
);
1462 if (snd_pcm_stream_linked(substream
)) {
1463 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1465 up_read(&snd_pcm_link_rwsem
);
1466 snd_power_unlock(card
);
1472 /* count only playback streams */
1474 snd_pcm_group_for_each_entry(s
, substream
) {
1475 runtime
= s
->runtime
;
1476 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1477 d
= &drec
[num_drecs
++];
1479 init_waitqueue_entry(&d
->wait
, current
);
1480 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1481 /* stop_threshold fixup to avoid endless loop when
1482 * stop_threshold > buffer_size
1484 d
->stop_threshold
= runtime
->stop_threshold
;
1485 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1486 runtime
->stop_threshold
= runtime
->buffer_size
;
1489 up_read(&snd_pcm_link_rwsem
);
1491 snd_pcm_stream_lock_irq(substream
);
1493 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1494 snd_pcm_pause(substream
, 0);
1496 /* pre-start/stop - all running streams are changed to DRAINING state */
1497 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1499 snd_pcm_stream_unlock_irq(substream
);
1505 if (signal_pending(current
)) {
1506 result
= -ERESTARTSYS
;
1510 for (i
= 0; i
< num_drecs
; i
++) {
1511 runtime
= drec
[i
].substream
->runtime
;
1512 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1516 break; /* yes, all drained */
1518 set_current_state(TASK_INTERRUPTIBLE
);
1519 snd_pcm_stream_unlock_irq(substream
);
1520 snd_power_unlock(card
);
1521 tout
= schedule_timeout(10 * HZ
);
1522 snd_power_lock(card
);
1523 snd_pcm_stream_lock_irq(substream
);
1525 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1528 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1529 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1536 snd_pcm_stream_unlock_irq(substream
);
1539 for (i
= 0; i
< num_drecs
; i
++) {
1541 runtime
= d
->substream
->runtime
;
1542 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1543 runtime
->stop_threshold
= d
->stop_threshold
;
1546 if (drec
!= &drec_tmp
)
1548 snd_power_unlock(card
);
1556 * Immediately put all linked substreams into SETUP state.
1558 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1560 struct snd_pcm_runtime
*runtime
;
1561 struct snd_card
*card
;
1564 snd_assert(substream
!= NULL
, return -ENXIO
);
1565 runtime
= substream
->runtime
;
1566 card
= substream
->pcm
->card
;
1568 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1569 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1572 snd_power_lock(card
);
1573 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1574 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1579 snd_pcm_stream_lock_irq(substream
);
1581 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1582 snd_pcm_pause(substream
, 0);
1584 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1585 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1586 snd_pcm_stream_unlock_irq(substream
);
1588 snd_power_unlock(card
);
1593 /* WARNING: Don't forget to fput back the file */
1594 static struct file
*snd_pcm_file_fd(int fd
)
1597 struct inode
*inode
;
1603 inode
= file
->f_path
.dentry
->d_inode
;
1604 if (!S_ISCHR(inode
->i_mode
) ||
1605 imajor(inode
) != snd_major
) {
1609 minor
= iminor(inode
);
1610 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1611 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1621 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1625 struct snd_pcm_file
*pcm_file
;
1626 struct snd_pcm_substream
*substream1
;
1628 file
= snd_pcm_file_fd(fd
);
1631 pcm_file
= file
->private_data
;
1632 substream1
= pcm_file
->substream
;
1633 down_write(&snd_pcm_link_rwsem
);
1634 write_lock_irq(&snd_pcm_link_rwlock
);
1635 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1636 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1640 if (snd_pcm_stream_linked(substream1
)) {
1644 if (!snd_pcm_stream_linked(substream
)) {
1645 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1646 if (substream
->group
== NULL
) {
1650 spin_lock_init(&substream
->group
->lock
);
1651 INIT_LIST_HEAD(&substream
->group
->substreams
);
1652 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1653 substream
->group
->count
= 1;
1655 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1656 substream
->group
->count
++;
1657 substream1
->group
= substream
->group
;
1659 write_unlock_irq(&snd_pcm_link_rwlock
);
1660 up_write(&snd_pcm_link_rwsem
);
1665 static void relink_to_local(struct snd_pcm_substream
*substream
)
1667 substream
->group
= &substream
->self_group
;
1668 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1669 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1672 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1674 struct snd_pcm_substream
*s
;
1677 down_write(&snd_pcm_link_rwsem
);
1678 write_lock_irq(&snd_pcm_link_rwlock
);
1679 if (!snd_pcm_stream_linked(substream
)) {
1683 list_del(&substream
->link_list
);
1684 substream
->group
->count
--;
1685 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1686 snd_pcm_group_for_each_entry(s
, substream
) {
1690 kfree(substream
->group
);
1692 relink_to_local(substream
);
1694 write_unlock_irq(&snd_pcm_link_rwlock
);
1695 up_write(&snd_pcm_link_rwsem
);
1702 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1703 struct snd_pcm_hw_rule
*rule
)
1705 struct snd_interval t
;
1706 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1707 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1708 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1711 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1712 struct snd_pcm_hw_rule
*rule
)
1714 struct snd_interval t
;
1715 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1716 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1717 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1720 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1721 struct snd_pcm_hw_rule
*rule
)
1723 struct snd_interval t
;
1724 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1725 hw_param_interval_c(params
, rule
->deps
[1]),
1726 (unsigned long) rule
->private, &t
);
1727 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1730 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1731 struct snd_pcm_hw_rule
*rule
)
1733 struct snd_interval t
;
1734 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1735 (unsigned long) rule
->private,
1736 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1737 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1740 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1741 struct snd_pcm_hw_rule
*rule
)
1744 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1746 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1748 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1750 if (! snd_mask_test(mask
, k
))
1752 bits
= snd_pcm_format_physical_width(k
);
1754 continue; /* ignore invalid formats */
1755 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1756 snd_mask_reset(&m
, k
);
1758 return snd_mask_refine(mask
, &m
);
1761 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1762 struct snd_pcm_hw_rule
*rule
)
1764 struct snd_interval t
;
1770 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1772 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1774 bits
= snd_pcm_format_physical_width(k
);
1776 continue; /* ignore invalid formats */
1777 if (t
.min
> (unsigned)bits
)
1779 if (t
.max
< (unsigned)bits
)
1783 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1786 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1787 #error "Change this table"
1790 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1791 48000, 64000, 88200, 96000, 176400, 192000 };
1793 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1794 .count
= ARRAY_SIZE(rates
),
1798 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1799 struct snd_pcm_hw_rule
*rule
)
1801 struct snd_pcm_hardware
*hw
= rule
->private;
1802 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1803 snd_pcm_known_rates
.count
,
1804 snd_pcm_known_rates
.list
, hw
->rates
);
1807 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1808 struct snd_pcm_hw_rule
*rule
)
1810 struct snd_interval t
;
1811 struct snd_pcm_substream
*substream
= rule
->private;
1813 t
.max
= substream
->buffer_bytes_max
;
1817 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1820 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1822 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1823 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1826 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1827 snd_mask_any(constrs_mask(constrs
, k
));
1830 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1831 snd_interval_any(constrs_interval(constrs
, k
));
1834 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1835 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1836 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1837 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1838 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1840 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1841 snd_pcm_hw_rule_format
, NULL
,
1842 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1845 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1846 snd_pcm_hw_rule_sample_bits
, NULL
,
1847 SNDRV_PCM_HW_PARAM_FORMAT
,
1848 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1851 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1852 snd_pcm_hw_rule_div
, NULL
,
1853 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1856 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1857 snd_pcm_hw_rule_mul
, NULL
,
1858 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1861 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1862 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1866 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1867 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1868 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1871 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1872 snd_pcm_hw_rule_div
, NULL
,
1873 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1876 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1877 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1878 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1881 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1882 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1886 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1887 snd_pcm_hw_rule_div
, NULL
,
1888 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1891 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1892 snd_pcm_hw_rule_div
, NULL
,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1896 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1897 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1898 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1901 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1902 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1903 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1906 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1907 snd_pcm_hw_rule_mul
, NULL
,
1908 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1911 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1912 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1913 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1916 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1917 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1918 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1921 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1922 snd_pcm_hw_rule_muldivk
, (void*) 8,
1923 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1926 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1927 snd_pcm_hw_rule_muldivk
, (void*) 8,
1928 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1931 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1932 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1933 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1936 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1937 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1938 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1944 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1946 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1947 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1949 unsigned int mask
= 0;
1951 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1952 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1953 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1954 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1955 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1956 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1957 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1958 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1959 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1960 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1961 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1963 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1964 snd_assert(err
>= 0, return -EINVAL
);
1966 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1967 snd_assert(err
>= 0, return -EINVAL
);
1969 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1970 snd_assert(err
>= 0, return -EINVAL
);
1972 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1973 hw
->channels_min
, hw
->channels_max
);
1974 snd_assert(err
>= 0, return -EINVAL
);
1976 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1977 hw
->rate_min
, hw
->rate_max
);
1978 snd_assert(err
>= 0, return -EINVAL
);
1980 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1981 hw
->period_bytes_min
, hw
->period_bytes_max
);
1982 snd_assert(err
>= 0, return -EINVAL
);
1984 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1985 hw
->periods_min
, hw
->periods_max
);
1986 snd_assert(err
>= 0, return -EINVAL
);
1988 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1989 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1990 snd_assert(err
>= 0, return -EINVAL
);
1992 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1993 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1994 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1999 if (runtime
->dma_bytes
) {
2000 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
2001 snd_assert(err
>= 0, return -EINVAL
);
2004 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
2005 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
2006 snd_pcm_hw_rule_rate
, hw
,
2007 SNDRV_PCM_HW_PARAM_RATE
, -1);
2012 /* FIXME: this belong to lowlevel */
2013 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_TICK_TIME
,
2014 1000000 / HZ
, 1000000 / HZ
);
2015 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2020 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2022 snd_pcm_unlink(substream
);
2025 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2027 substream
->ref_count
--;
2028 if (substream
->ref_count
> 0)
2031 snd_pcm_drop(substream
);
2032 if (substream
->hw_opened
) {
2033 if (substream
->ops
->hw_free
!= NULL
)
2034 substream
->ops
->hw_free(substream
);
2035 substream
->ops
->close(substream
);
2036 substream
->hw_opened
= 0;
2038 if (substream
->pcm_release
) {
2039 substream
->pcm_release(substream
);
2040 substream
->pcm_release
= NULL
;
2042 snd_pcm_detach_substream(substream
);
2045 EXPORT_SYMBOL(snd_pcm_release_substream
);
2047 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2049 struct snd_pcm_substream
**rsubstream
)
2051 struct snd_pcm_substream
*substream
;
2054 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2057 if (substream
->ref_count
> 1) {
2058 *rsubstream
= substream
;
2062 err
= snd_pcm_hw_constraints_init(substream
);
2064 snd_printd("snd_pcm_hw_constraints_init failed\n");
2068 if ((err
= substream
->ops
->open(substream
)) < 0)
2071 substream
->hw_opened
= 1;
2073 err
= snd_pcm_hw_constraints_complete(substream
);
2075 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2079 *rsubstream
= substream
;
2083 snd_pcm_release_substream(substream
);
2087 EXPORT_SYMBOL(snd_pcm_open_substream
);
2089 static int snd_pcm_open_file(struct file
*file
,
2090 struct snd_pcm
*pcm
,
2092 struct snd_pcm_file
**rpcm_file
)
2094 struct snd_pcm_file
*pcm_file
;
2095 struct snd_pcm_substream
*substream
;
2096 struct snd_pcm_str
*str
;
2099 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
2102 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2106 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2107 if (pcm_file
== NULL
) {
2108 snd_pcm_release_substream(substream
);
2111 pcm_file
->substream
= substream
;
2112 if (substream
->ref_count
== 1) {
2113 str
= substream
->pstr
;
2114 substream
->file
= pcm_file
;
2115 substream
->pcm_release
= pcm_release_private
;
2117 file
->private_data
= pcm_file
;
2118 *rpcm_file
= pcm_file
;
2122 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2124 struct snd_pcm
*pcm
;
2126 pcm
= snd_lookup_minor_data(iminor(inode
),
2127 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2128 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2131 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2133 struct snd_pcm
*pcm
;
2135 pcm
= snd_lookup_minor_data(iminor(inode
),
2136 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2137 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2140 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2143 struct snd_pcm_file
*pcm_file
;
2150 err
= snd_card_file_add(pcm
->card
, file
);
2153 if (!try_module_get(pcm
->card
->module
)) {
2157 init_waitqueue_entry(&wait
, current
);
2158 add_wait_queue(&pcm
->open_wait
, &wait
);
2159 mutex_lock(&pcm
->open_mutex
);
2161 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2164 if (err
== -EAGAIN
) {
2165 if (file
->f_flags
& O_NONBLOCK
) {
2171 set_current_state(TASK_INTERRUPTIBLE
);
2172 mutex_unlock(&pcm
->open_mutex
);
2174 mutex_lock(&pcm
->open_mutex
);
2175 if (signal_pending(current
)) {
2180 remove_wait_queue(&pcm
->open_wait
, &wait
);
2181 mutex_unlock(&pcm
->open_mutex
);
2187 module_put(pcm
->card
->module
);
2189 snd_card_file_remove(pcm
->card
, file
);
2194 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2196 struct snd_pcm
*pcm
;
2197 struct snd_pcm_substream
*substream
;
2198 struct snd_pcm_file
*pcm_file
;
2200 pcm_file
= file
->private_data
;
2201 substream
= pcm_file
->substream
;
2202 snd_assert(substream
!= NULL
, return -ENXIO
);
2203 pcm
= substream
->pcm
;
2204 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2205 mutex_lock(&pcm
->open_mutex
);
2206 snd_pcm_release_substream(substream
);
2208 mutex_unlock(&pcm
->open_mutex
);
2209 wake_up(&pcm
->open_wait
);
2210 module_put(pcm
->card
->module
);
2211 snd_card_file_remove(pcm
->card
, file
);
2215 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2216 snd_pcm_uframes_t frames
)
2218 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2219 snd_pcm_sframes_t appl_ptr
;
2220 snd_pcm_sframes_t ret
;
2221 snd_pcm_sframes_t hw_avail
;
2226 snd_pcm_stream_lock_irq(substream
);
2227 switch (runtime
->status
->state
) {
2228 case SNDRV_PCM_STATE_PREPARED
:
2230 case SNDRV_PCM_STATE_DRAINING
:
2231 case SNDRV_PCM_STATE_RUNNING
:
2232 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2235 case SNDRV_PCM_STATE_XRUN
:
2243 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2244 if (hw_avail
<= 0) {
2248 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2251 frames
-= frames
% runtime
->xfer_align
;
2252 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2254 appl_ptr
+= runtime
->boundary
;
2255 runtime
->control
->appl_ptr
= appl_ptr
;
2256 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2258 snd_pcm_tick_prepare(substream
);
2261 snd_pcm_stream_unlock_irq(substream
);
2265 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2266 snd_pcm_uframes_t frames
)
2268 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2269 snd_pcm_sframes_t appl_ptr
;
2270 snd_pcm_sframes_t ret
;
2271 snd_pcm_sframes_t hw_avail
;
2276 snd_pcm_stream_lock_irq(substream
);
2277 switch (runtime
->status
->state
) {
2278 case SNDRV_PCM_STATE_PREPARED
:
2279 case SNDRV_PCM_STATE_DRAINING
:
2281 case SNDRV_PCM_STATE_RUNNING
:
2282 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2285 case SNDRV_PCM_STATE_XRUN
:
2293 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2294 if (hw_avail
<= 0) {
2298 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2301 frames
-= frames
% runtime
->xfer_align
;
2302 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2304 appl_ptr
+= runtime
->boundary
;
2305 runtime
->control
->appl_ptr
= appl_ptr
;
2306 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2308 snd_pcm_tick_prepare(substream
);
2311 snd_pcm_stream_unlock_irq(substream
);
2315 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2316 snd_pcm_uframes_t frames
)
2318 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2319 snd_pcm_sframes_t appl_ptr
;
2320 snd_pcm_sframes_t ret
;
2321 snd_pcm_sframes_t avail
;
2326 snd_pcm_stream_lock_irq(substream
);
2327 switch (runtime
->status
->state
) {
2328 case SNDRV_PCM_STATE_PREPARED
:
2329 case SNDRV_PCM_STATE_PAUSED
:
2331 case SNDRV_PCM_STATE_DRAINING
:
2332 case SNDRV_PCM_STATE_RUNNING
:
2333 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2336 case SNDRV_PCM_STATE_XRUN
:
2344 avail
= snd_pcm_playback_avail(runtime
);
2349 if (frames
> (snd_pcm_uframes_t
)avail
)
2352 frames
-= frames
% runtime
->xfer_align
;
2353 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2354 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2355 appl_ptr
-= runtime
->boundary
;
2356 runtime
->control
->appl_ptr
= appl_ptr
;
2357 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2359 snd_pcm_tick_prepare(substream
);
2362 snd_pcm_stream_unlock_irq(substream
);
2366 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2367 snd_pcm_uframes_t frames
)
2369 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2370 snd_pcm_sframes_t appl_ptr
;
2371 snd_pcm_sframes_t ret
;
2372 snd_pcm_sframes_t avail
;
2377 snd_pcm_stream_lock_irq(substream
);
2378 switch (runtime
->status
->state
) {
2379 case SNDRV_PCM_STATE_PREPARED
:
2380 case SNDRV_PCM_STATE_DRAINING
:
2381 case SNDRV_PCM_STATE_PAUSED
:
2383 case SNDRV_PCM_STATE_RUNNING
:
2384 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2387 case SNDRV_PCM_STATE_XRUN
:
2395 avail
= snd_pcm_capture_avail(runtime
);
2400 if (frames
> (snd_pcm_uframes_t
)avail
)
2403 frames
-= frames
% runtime
->xfer_align
;
2404 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2405 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2406 appl_ptr
-= runtime
->boundary
;
2407 runtime
->control
->appl_ptr
= appl_ptr
;
2408 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2410 snd_pcm_tick_prepare(substream
);
2413 snd_pcm_stream_unlock_irq(substream
);
2417 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2419 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2422 snd_pcm_stream_lock_irq(substream
);
2423 switch (runtime
->status
->state
) {
2424 case SNDRV_PCM_STATE_DRAINING
:
2425 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2427 case SNDRV_PCM_STATE_RUNNING
:
2428 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2431 case SNDRV_PCM_STATE_PREPARED
:
2432 case SNDRV_PCM_STATE_SUSPENDED
:
2435 case SNDRV_PCM_STATE_XRUN
:
2443 snd_pcm_stream_unlock_irq(substream
);
2447 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2448 snd_pcm_sframes_t __user
*res
)
2450 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2452 snd_pcm_sframes_t n
= 0;
2454 snd_pcm_stream_lock_irq(substream
);
2455 switch (runtime
->status
->state
) {
2456 case SNDRV_PCM_STATE_DRAINING
:
2457 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2459 case SNDRV_PCM_STATE_RUNNING
:
2460 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2463 case SNDRV_PCM_STATE_PREPARED
:
2464 case SNDRV_PCM_STATE_SUSPENDED
:
2466 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2467 n
= snd_pcm_playback_hw_avail(runtime
);
2469 n
= snd_pcm_capture_avail(runtime
);
2471 case SNDRV_PCM_STATE_XRUN
:
2479 snd_pcm_stream_unlock_irq(substream
);
2481 if (put_user(n
, res
))
2486 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2487 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2489 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2490 struct snd_pcm_sync_ptr sync_ptr
;
2491 volatile struct snd_pcm_mmap_status
*status
;
2492 volatile struct snd_pcm_mmap_control
*control
;
2495 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2496 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2498 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2500 status
= runtime
->status
;
2501 control
= runtime
->control
;
2502 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2503 err
= snd_pcm_hwsync(substream
);
2507 snd_pcm_stream_lock_irq(substream
);
2508 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2509 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2511 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2512 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2513 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2515 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2516 sync_ptr
.s
.status
.state
= status
->state
;
2517 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2518 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2519 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2520 snd_pcm_stream_unlock_irq(substream
);
2521 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2526 static int snd_pcm_common_ioctl1(struct file
*file
,
2527 struct snd_pcm_substream
*substream
,
2528 unsigned int cmd
, void __user
*arg
)
2530 snd_assert(substream
!= NULL
, return -ENXIO
);
2533 case SNDRV_PCM_IOCTL_PVERSION
:
2534 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2535 case SNDRV_PCM_IOCTL_INFO
:
2536 return snd_pcm_info_user(substream
, arg
);
2537 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2539 case SNDRV_PCM_IOCTL_HW_REFINE
:
2540 return snd_pcm_hw_refine_user(substream
, arg
);
2541 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2542 return snd_pcm_hw_params_user(substream
, arg
);
2543 case SNDRV_PCM_IOCTL_HW_FREE
:
2544 return snd_pcm_hw_free(substream
);
2545 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2546 return snd_pcm_sw_params_user(substream
, arg
);
2547 case SNDRV_PCM_IOCTL_STATUS
:
2548 return snd_pcm_status_user(substream
, arg
);
2549 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2550 return snd_pcm_channel_info_user(substream
, arg
);
2551 case SNDRV_PCM_IOCTL_PREPARE
:
2552 return snd_pcm_prepare(substream
, file
);
2553 case SNDRV_PCM_IOCTL_RESET
:
2554 return snd_pcm_reset(substream
);
2555 case SNDRV_PCM_IOCTL_START
:
2556 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2557 case SNDRV_PCM_IOCTL_LINK
:
2558 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2559 case SNDRV_PCM_IOCTL_UNLINK
:
2560 return snd_pcm_unlink(substream
);
2561 case SNDRV_PCM_IOCTL_RESUME
:
2562 return snd_pcm_resume(substream
);
2563 case SNDRV_PCM_IOCTL_XRUN
:
2564 return snd_pcm_xrun(substream
);
2565 case SNDRV_PCM_IOCTL_HWSYNC
:
2566 return snd_pcm_hwsync(substream
);
2567 case SNDRV_PCM_IOCTL_DELAY
:
2568 return snd_pcm_delay(substream
, arg
);
2569 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2570 return snd_pcm_sync_ptr(substream
, arg
);
2571 #ifdef CONFIG_SND_SUPPORT_OLD_API
2572 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2573 return snd_pcm_hw_refine_old_user(substream
, arg
);
2574 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2575 return snd_pcm_hw_params_old_user(substream
, arg
);
2577 case SNDRV_PCM_IOCTL_DRAIN
:
2578 return snd_pcm_drain(substream
);
2579 case SNDRV_PCM_IOCTL_DROP
:
2580 return snd_pcm_drop(substream
);
2581 case SNDRV_PCM_IOCTL_PAUSE
:
2584 snd_pcm_stream_lock_irq(substream
);
2585 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2586 snd_pcm_stream_unlock_irq(substream
);
2590 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2594 static int snd_pcm_playback_ioctl1(struct file
*file
,
2595 struct snd_pcm_substream
*substream
,
2596 unsigned int cmd
, void __user
*arg
)
2598 snd_assert(substream
!= NULL
, return -ENXIO
);
2599 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2601 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2603 struct snd_xferi xferi
;
2604 struct snd_xferi __user
*_xferi
= arg
;
2605 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2606 snd_pcm_sframes_t result
;
2607 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2609 if (put_user(0, &_xferi
->result
))
2611 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2613 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2614 __put_user(result
, &_xferi
->result
);
2615 return result
< 0 ? result
: 0;
2617 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2619 struct snd_xfern xfern
;
2620 struct snd_xfern __user
*_xfern
= arg
;
2621 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2623 snd_pcm_sframes_t result
;
2624 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2626 if (runtime
->channels
> 128)
2628 if (put_user(0, &_xfern
->result
))
2630 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2632 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2635 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2639 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2641 __put_user(result
, &_xfern
->result
);
2642 return result
< 0 ? result
: 0;
2644 case SNDRV_PCM_IOCTL_REWIND
:
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_playback_rewind(substream
, frames
);
2654 __put_user(result
, _frames
);
2655 return result
< 0 ? result
: 0;
2657 case SNDRV_PCM_IOCTL_FORWARD
:
2659 snd_pcm_uframes_t frames
;
2660 snd_pcm_uframes_t __user
*_frames
= arg
;
2661 snd_pcm_sframes_t result
;
2662 if (get_user(frames
, _frames
))
2664 if (put_user(0, _frames
))
2666 result
= snd_pcm_playback_forward(substream
, frames
);
2667 __put_user(result
, _frames
);
2668 return result
< 0 ? result
: 0;
2671 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2674 static int snd_pcm_capture_ioctl1(struct file
*file
,
2675 struct snd_pcm_substream
*substream
,
2676 unsigned int cmd
, void __user
*arg
)
2678 snd_assert(substream
!= NULL
, return -ENXIO
);
2679 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2681 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2683 struct snd_xferi xferi
;
2684 struct snd_xferi __user
*_xferi
= arg
;
2685 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2686 snd_pcm_sframes_t result
;
2687 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2689 if (put_user(0, &_xferi
->result
))
2691 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2693 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2694 __put_user(result
, &_xferi
->result
);
2695 return result
< 0 ? result
: 0;
2697 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2699 struct snd_xfern xfern
;
2700 struct snd_xfern __user
*_xfern
= arg
;
2701 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2703 snd_pcm_sframes_t result
;
2704 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2706 if (runtime
->channels
> 128)
2708 if (put_user(0, &_xfern
->result
))
2710 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2712 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2715 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2719 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2721 __put_user(result
, &_xfern
->result
);
2722 return result
< 0 ? result
: 0;
2724 case SNDRV_PCM_IOCTL_REWIND
:
2726 snd_pcm_uframes_t frames
;
2727 snd_pcm_uframes_t __user
*_frames
= arg
;
2728 snd_pcm_sframes_t result
;
2729 if (get_user(frames
, _frames
))
2731 if (put_user(0, _frames
))
2733 result
= snd_pcm_capture_rewind(substream
, frames
);
2734 __put_user(result
, _frames
);
2735 return result
< 0 ? result
: 0;
2737 case SNDRV_PCM_IOCTL_FORWARD
:
2739 snd_pcm_uframes_t frames
;
2740 snd_pcm_uframes_t __user
*_frames
= arg
;
2741 snd_pcm_sframes_t result
;
2742 if (get_user(frames
, _frames
))
2744 if (put_user(0, _frames
))
2746 result
= snd_pcm_capture_forward(substream
, frames
);
2747 __put_user(result
, _frames
);
2748 return result
< 0 ? result
: 0;
2751 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2754 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2757 struct snd_pcm_file
*pcm_file
;
2759 pcm_file
= file
->private_data
;
2761 if (((cmd
>> 8) & 0xff) != 'A')
2764 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2765 (void __user
*)arg
);
2768 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2771 struct snd_pcm_file
*pcm_file
;
2773 pcm_file
= file
->private_data
;
2775 if (((cmd
>> 8) & 0xff) != 'A')
2778 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2779 (void __user
*)arg
);
2782 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2783 unsigned int cmd
, void *arg
)
2788 fs
= snd_enter_user();
2789 switch (substream
->stream
) {
2790 case SNDRV_PCM_STREAM_PLAYBACK
:
2791 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2792 (void __user
*)arg
);
2794 case SNDRV_PCM_STREAM_CAPTURE
:
2795 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2796 (void __user
*)arg
);
2806 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2808 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2811 struct snd_pcm_file
*pcm_file
;
2812 struct snd_pcm_substream
*substream
;
2813 struct snd_pcm_runtime
*runtime
;
2814 snd_pcm_sframes_t result
;
2816 pcm_file
= file
->private_data
;
2817 substream
= pcm_file
->substream
;
2818 snd_assert(substream
!= NULL
, return -ENXIO
);
2819 runtime
= substream
->runtime
;
2820 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2822 if (!frame_aligned(runtime
, count
))
2824 count
= bytes_to_frames(runtime
, count
);
2825 result
= snd_pcm_lib_read(substream
, buf
, count
);
2827 result
= frames_to_bytes(runtime
, result
);
2831 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2832 size_t count
, loff_t
* offset
)
2834 struct snd_pcm_file
*pcm_file
;
2835 struct snd_pcm_substream
*substream
;
2836 struct snd_pcm_runtime
*runtime
;
2837 snd_pcm_sframes_t result
;
2839 pcm_file
= file
->private_data
;
2840 substream
= pcm_file
->substream
;
2841 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2842 runtime
= substream
->runtime
;
2843 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2847 if (!frame_aligned(runtime
, count
)) {
2851 count
= bytes_to_frames(runtime
, count
);
2852 result
= snd_pcm_lib_write(substream
, buf
, count
);
2854 result
= frames_to_bytes(runtime
, result
);
2859 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2860 unsigned long nr_segs
, loff_t pos
)
2863 struct snd_pcm_file
*pcm_file
;
2864 struct snd_pcm_substream
*substream
;
2865 struct snd_pcm_runtime
*runtime
;
2866 snd_pcm_sframes_t result
;
2869 snd_pcm_uframes_t frames
;
2871 pcm_file
= iocb
->ki_filp
->private_data
;
2872 substream
= pcm_file
->substream
;
2873 snd_assert(substream
!= NULL
, return -ENXIO
);
2874 runtime
= substream
->runtime
;
2875 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2877 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2879 if (!frame_aligned(runtime
, iov
->iov_len
))
2881 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2882 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2885 for (i
= 0; i
< nr_segs
; ++i
)
2886 bufs
[i
] = iov
[i
].iov_base
;
2887 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2889 result
= frames_to_bytes(runtime
, result
);
2894 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2895 unsigned long nr_segs
, loff_t pos
)
2897 struct snd_pcm_file
*pcm_file
;
2898 struct snd_pcm_substream
*substream
;
2899 struct snd_pcm_runtime
*runtime
;
2900 snd_pcm_sframes_t result
;
2903 snd_pcm_uframes_t frames
;
2905 pcm_file
= iocb
->ki_filp
->private_data
;
2906 substream
= pcm_file
->substream
;
2907 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2908 runtime
= substream
->runtime
;
2909 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2913 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2914 !frame_aligned(runtime
, iov
->iov_len
)) {
2918 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2919 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2922 for (i
= 0; i
< nr_segs
; ++i
)
2923 bufs
[i
] = iov
[i
].iov_base
;
2924 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2926 result
= frames_to_bytes(runtime
, result
);
2932 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2934 struct snd_pcm_file
*pcm_file
;
2935 struct snd_pcm_substream
*substream
;
2936 struct snd_pcm_runtime
*runtime
;
2938 snd_pcm_uframes_t avail
;
2940 pcm_file
= file
->private_data
;
2942 substream
= pcm_file
->substream
;
2943 snd_assert(substream
!= NULL
, return -ENXIO
);
2944 runtime
= substream
->runtime
;
2946 poll_wait(file
, &runtime
->sleep
, wait
);
2948 snd_pcm_stream_lock_irq(substream
);
2949 avail
= snd_pcm_playback_avail(runtime
);
2950 switch (runtime
->status
->state
) {
2951 case SNDRV_PCM_STATE_RUNNING
:
2952 case SNDRV_PCM_STATE_PREPARED
:
2953 case SNDRV_PCM_STATE_PAUSED
:
2954 if (avail
>= runtime
->control
->avail_min
) {
2955 mask
= POLLOUT
| POLLWRNORM
;
2959 case SNDRV_PCM_STATE_DRAINING
:
2963 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2966 snd_pcm_stream_unlock_irq(substream
);
2970 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2972 struct snd_pcm_file
*pcm_file
;
2973 struct snd_pcm_substream
*substream
;
2974 struct snd_pcm_runtime
*runtime
;
2976 snd_pcm_uframes_t avail
;
2978 pcm_file
= file
->private_data
;
2980 substream
= pcm_file
->substream
;
2981 snd_assert(substream
!= NULL
, return -ENXIO
);
2982 runtime
= substream
->runtime
;
2984 poll_wait(file
, &runtime
->sleep
, wait
);
2986 snd_pcm_stream_lock_irq(substream
);
2987 avail
= snd_pcm_capture_avail(runtime
);
2988 switch (runtime
->status
->state
) {
2989 case SNDRV_PCM_STATE_RUNNING
:
2990 case SNDRV_PCM_STATE_PREPARED
:
2991 case SNDRV_PCM_STATE_PAUSED
:
2992 if (avail
>= runtime
->control
->avail_min
) {
2993 mask
= POLLIN
| POLLRDNORM
;
2998 case SNDRV_PCM_STATE_DRAINING
:
3000 mask
= POLLIN
| POLLRDNORM
;
3005 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
3008 snd_pcm_stream_unlock_irq(substream
);
3017 * Only on coherent architectures, we can mmap the status and the control records
3018 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3020 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3022 * mmap status record
3024 static struct page
* snd_pcm_mmap_status_nopage(struct vm_area_struct
*area
,
3025 unsigned long address
, int *type
)
3027 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3028 struct snd_pcm_runtime
*runtime
;
3031 if (substream
== NULL
)
3032 return NOPAGE_SIGBUS
;
3033 runtime
= substream
->runtime
;
3034 page
= virt_to_page(runtime
->status
);
3037 *type
= VM_FAULT_MINOR
;
3041 static struct vm_operations_struct snd_pcm_vm_ops_status
=
3043 .nopage
= snd_pcm_mmap_status_nopage
,
3046 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3047 struct vm_area_struct
*area
)
3049 struct snd_pcm_runtime
*runtime
;
3051 if (!(area
->vm_flags
& VM_READ
))
3053 runtime
= substream
->runtime
;
3054 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3055 size
= area
->vm_end
- area
->vm_start
;
3056 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3058 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3059 area
->vm_private_data
= substream
;
3060 area
->vm_flags
|= VM_RESERVED
;
3065 * mmap control record
3067 static struct page
* snd_pcm_mmap_control_nopage(struct vm_area_struct
*area
,
3068 unsigned long address
, int *type
)
3070 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3071 struct snd_pcm_runtime
*runtime
;
3074 if (substream
== NULL
)
3075 return NOPAGE_SIGBUS
;
3076 runtime
= substream
->runtime
;
3077 page
= virt_to_page(runtime
->control
);
3080 *type
= VM_FAULT_MINOR
;
3084 static struct vm_operations_struct snd_pcm_vm_ops_control
=
3086 .nopage
= snd_pcm_mmap_control_nopage
,
3089 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3090 struct vm_area_struct
*area
)
3092 struct snd_pcm_runtime
*runtime
;
3094 if (!(area
->vm_flags
& VM_READ
))
3096 runtime
= substream
->runtime
;
3097 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3098 size
= area
->vm_end
- area
->vm_start
;
3099 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3101 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3102 area
->vm_private_data
= substream
;
3103 area
->vm_flags
|= VM_RESERVED
;
3106 #else /* ! coherent mmap */
3108 * don't support mmap for status and control records.
3110 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3111 struct vm_area_struct
*area
)
3115 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3116 struct vm_area_struct
*area
)
3120 #endif /* coherent mmap */
3123 * nopage callback for mmapping a RAM page
3125 static struct page
*snd_pcm_mmap_data_nopage(struct vm_area_struct
*area
,
3126 unsigned long address
, int *type
)
3128 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3129 struct snd_pcm_runtime
*runtime
;
3130 unsigned long offset
;
3135 if (substream
== NULL
)
3136 return NOPAGE_SIGBUS
;
3137 runtime
= substream
->runtime
;
3138 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3139 offset
+= address
- area
->vm_start
;
3140 snd_assert((offset
% PAGE_SIZE
) == 0, return NOPAGE_SIGBUS
);
3141 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3142 if (offset
> dma_bytes
- PAGE_SIZE
)
3143 return NOPAGE_SIGBUS
;
3144 if (substream
->ops
->page
) {
3145 page
= substream
->ops
->page(substream
, offset
);
3147 return NOPAGE_OOM
; /* XXX: is this really due to OOM? */
3149 vaddr
= runtime
->dma_area
+ offset
;
3150 page
= virt_to_page(vaddr
);
3154 *type
= VM_FAULT_MINOR
;
3158 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3160 .open
= snd_pcm_mmap_data_open
,
3161 .close
= snd_pcm_mmap_data_close
,
3162 .nopage
= snd_pcm_mmap_data_nopage
,
3166 * mmap the DMA buffer on RAM
3168 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3169 struct vm_area_struct
*area
)
3171 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3172 area
->vm_private_data
= substream
;
3173 area
->vm_flags
|= VM_RESERVED
;
3174 atomic_inc(&substream
->mmap_count
);
3179 * mmap the DMA buffer on I/O memory area
3181 #if SNDRV_PCM_INFO_MMAP_IOMEM
3182 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3184 .open
= snd_pcm_mmap_data_open
,
3185 .close
= snd_pcm_mmap_data_close
,
3188 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3189 struct vm_area_struct
*area
)
3192 unsigned long offset
;
3194 #ifdef pgprot_noncached
3195 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3197 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3198 area
->vm_private_data
= substream
;
3199 area
->vm_flags
|= VM_IO
;
3200 size
= area
->vm_end
- area
->vm_start
;
3201 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3202 if (io_remap_pfn_range(area
, area
->vm_start
,
3203 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3204 size
, area
->vm_page_prot
))
3206 atomic_inc(&substream
->mmap_count
);
3210 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3211 #endif /* SNDRV_PCM_INFO_MMAP */
3216 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3217 struct vm_area_struct
*area
)
3219 struct snd_pcm_runtime
*runtime
;
3221 unsigned long offset
;
3224 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3225 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3228 if (!(area
->vm_flags
& VM_READ
))
3231 runtime
= substream
->runtime
;
3232 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3233 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3235 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3237 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3238 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3240 size
= area
->vm_end
- area
->vm_start
;
3241 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3242 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3243 if ((size_t)size
> dma_bytes
)
3245 if (offset
> dma_bytes
- size
)
3248 if (substream
->ops
->mmap
)
3249 return substream
->ops
->mmap(substream
, area
);
3251 return snd_pcm_default_mmap(substream
, area
);
3254 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3256 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3258 struct snd_pcm_file
* pcm_file
;
3259 struct snd_pcm_substream
*substream
;
3260 unsigned long offset
;
3262 pcm_file
= file
->private_data
;
3263 substream
= pcm_file
->substream
;
3264 snd_assert(substream
!= NULL
, return -ENXIO
);
3266 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3268 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3269 if (pcm_file
->no_compat_mmap
)
3271 return snd_pcm_mmap_status(substream
, file
, area
);
3272 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3273 if (pcm_file
->no_compat_mmap
)
3275 return snd_pcm_mmap_control(substream
, file
, area
);
3277 return snd_pcm_mmap_data(substream
, file
, area
);
3282 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3284 struct snd_pcm_file
* pcm_file
;
3285 struct snd_pcm_substream
*substream
;
3286 struct snd_pcm_runtime
*runtime
;
3289 pcm_file
= file
->private_data
;
3290 substream
= pcm_file
->substream
;
3291 snd_assert(substream
!= NULL
, return -ENXIO
);
3292 runtime
= substream
->runtime
;
3294 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3303 #ifdef CONFIG_COMPAT
3304 #include "pcm_compat.c"
3306 #define snd_pcm_ioctl_compat NULL
3310 * To be removed helpers to keep binary compatibility
3313 #ifdef CONFIG_SND_SUPPORT_OLD_API
3314 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3315 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3317 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3318 struct snd_pcm_hw_params_old
*oparams
)
3322 memset(params
, 0, sizeof(*params
));
3323 params
->flags
= oparams
->flags
;
3324 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3325 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3326 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3327 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3328 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3329 params
->info
= oparams
->info
;
3330 params
->msbits
= oparams
->msbits
;
3331 params
->rate_num
= oparams
->rate_num
;
3332 params
->rate_den
= oparams
->rate_den
;
3333 params
->fifo_size
= oparams
->fifo_size
;
3336 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3337 struct snd_pcm_hw_params
*params
)
3341 memset(oparams
, 0, sizeof(*oparams
));
3342 oparams
->flags
= params
->flags
;
3343 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3344 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3345 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3346 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3347 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3348 oparams
->info
= params
->info
;
3349 oparams
->msbits
= params
->msbits
;
3350 oparams
->rate_num
= params
->rate_num
;
3351 oparams
->rate_den
= params
->rate_den
;
3352 oparams
->fifo_size
= params
->fifo_size
;
3355 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3356 struct snd_pcm_hw_params_old __user
* _oparams
)
3358 struct snd_pcm_hw_params
*params
;
3359 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3362 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3367 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3373 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3377 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3378 err
= snd_pcm_hw_refine(substream
, params
);
3379 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3380 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3390 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3391 struct snd_pcm_hw_params_old __user
* _oparams
)
3393 struct snd_pcm_hw_params
*params
;
3394 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3397 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3402 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3407 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3411 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3412 err
= snd_pcm_hw_params(substream
, params
);
3413 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3414 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3423 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3429 const struct file_operations snd_pcm_f_ops
[2] = {
3431 .owner
= THIS_MODULE
,
3432 .write
= snd_pcm_write
,
3433 .aio_write
= snd_pcm_aio_write
,
3434 .open
= snd_pcm_playback_open
,
3435 .release
= snd_pcm_release
,
3436 .poll
= snd_pcm_playback_poll
,
3437 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3438 .compat_ioctl
= snd_pcm_ioctl_compat
,
3439 .mmap
= snd_pcm_mmap
,
3440 .fasync
= snd_pcm_fasync
,
3443 .owner
= THIS_MODULE
,
3444 .read
= snd_pcm_read
,
3445 .aio_read
= snd_pcm_aio_read
,
3446 .open
= snd_pcm_capture_open
,
3447 .release
= snd_pcm_release
,
3448 .poll
= snd_pcm_capture_poll
,
3449 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3450 .compat_ioctl
= snd_pcm_ioctl_compat
,
3451 .mmap
= snd_pcm_mmap
,
3452 .fasync
= snd_pcm_fasync
,