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/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
->info
= params
->info
;
417 runtime
->rate_num
= params
->rate_num
;
418 runtime
->rate_den
= params
->rate_den
;
420 bits
= snd_pcm_format_physical_width(runtime
->format
);
421 runtime
->sample_bits
= bits
;
422 bits
*= runtime
->channels
;
423 runtime
->frame_bits
= bits
;
425 while (bits
% 8 != 0) {
429 runtime
->byte_align
= bits
/ 8;
430 runtime
->min_align
= frames
;
432 /* Default sw params */
433 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
434 runtime
->period_step
= 1;
435 runtime
->control
->avail_min
= runtime
->period_size
;
436 runtime
->start_threshold
= 1;
437 runtime
->stop_threshold
= runtime
->buffer_size
;
438 runtime
->silence_threshold
= 0;
439 runtime
->silence_size
= 0;
440 runtime
->boundary
= runtime
->buffer_size
;
441 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
442 runtime
->boundary
*= 2;
444 snd_pcm_timer_resolution_change(substream
);
445 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
447 remove_acceptable_latency(substream
->latency_id
);
448 if ((usecs
= period_to_usecs(runtime
)) >= 0)
449 set_acceptable_latency(substream
->latency_id
, usecs
);
452 /* hardware might be unuseable from this time,
453 so we force application to retry to set
454 the correct hardware parameter settings */
455 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
456 if (substream
->ops
->hw_free
!= NULL
)
457 substream
->ops
->hw_free(substream
);
461 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
462 struct snd_pcm_hw_params __user
* _params
)
464 struct snd_pcm_hw_params
*params
;
467 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
472 if (copy_from_user(params
, _params
, sizeof(*params
))) {
476 err
= snd_pcm_hw_params(substream
, params
);
477 if (copy_to_user(_params
, params
, sizeof(*params
))) {
486 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
488 struct snd_pcm_runtime
*runtime
;
491 snd_assert(substream
!= NULL
, return -ENXIO
);
492 runtime
= substream
->runtime
;
493 snd_assert(runtime
!= NULL
, return -ENXIO
);
494 snd_pcm_stream_lock_irq(substream
);
495 switch (runtime
->status
->state
) {
496 case SNDRV_PCM_STATE_SETUP
:
497 case SNDRV_PCM_STATE_PREPARED
:
500 snd_pcm_stream_unlock_irq(substream
);
503 snd_pcm_stream_unlock_irq(substream
);
504 if (atomic_read(&substream
->mmap_count
))
506 if (substream
->ops
->hw_free
)
507 result
= substream
->ops
->hw_free(substream
);
508 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
509 remove_acceptable_latency(substream
->latency_id
);
513 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
514 struct snd_pcm_sw_params
*params
)
516 struct snd_pcm_runtime
*runtime
;
518 snd_assert(substream
!= NULL
, return -ENXIO
);
519 runtime
= substream
->runtime
;
520 snd_assert(runtime
!= NULL
, return -ENXIO
);
521 snd_pcm_stream_lock_irq(substream
);
522 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
523 snd_pcm_stream_unlock_irq(substream
);
526 snd_pcm_stream_unlock_irq(substream
);
528 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
530 if (params
->avail_min
== 0)
532 if (params
->silence_size
>= runtime
->boundary
) {
533 if (params
->silence_threshold
!= 0)
536 if (params
->silence_size
> params
->silence_threshold
)
538 if (params
->silence_threshold
> runtime
->buffer_size
)
541 snd_pcm_stream_lock_irq(substream
);
542 runtime
->tstamp_mode
= params
->tstamp_mode
;
543 runtime
->period_step
= params
->period_step
;
544 runtime
->control
->avail_min
= params
->avail_min
;
545 runtime
->start_threshold
= params
->start_threshold
;
546 runtime
->stop_threshold
= params
->stop_threshold
;
547 runtime
->silence_threshold
= params
->silence_threshold
;
548 runtime
->silence_size
= params
->silence_size
;
549 params
->boundary
= runtime
->boundary
;
550 if (snd_pcm_running(substream
)) {
551 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
552 runtime
->silence_size
> 0)
553 snd_pcm_playback_silence(substream
, ULONG_MAX
);
554 wake_up(&runtime
->sleep
);
556 snd_pcm_stream_unlock_irq(substream
);
560 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
561 struct snd_pcm_sw_params __user
* _params
)
563 struct snd_pcm_sw_params params
;
565 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
567 err
= snd_pcm_sw_params(substream
, ¶ms
);
568 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
573 int snd_pcm_status(struct snd_pcm_substream
*substream
,
574 struct snd_pcm_status
*status
)
576 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
578 snd_pcm_stream_lock_irq(substream
);
579 status
->state
= runtime
->status
->state
;
580 status
->suspended_state
= runtime
->status
->suspended_state
;
581 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
583 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
584 if (snd_pcm_running(substream
))
585 snd_pcm_update_hw_ptr(substream
);
586 snd_pcm_gettime(runtime
, &status
->tstamp
);
587 status
->appl_ptr
= runtime
->control
->appl_ptr
;
588 status
->hw_ptr
= runtime
->status
->hw_ptr
;
589 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
590 status
->avail
= snd_pcm_playback_avail(runtime
);
591 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
592 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
593 status
->delay
= runtime
->buffer_size
- status
->avail
;
597 status
->avail
= snd_pcm_capture_avail(runtime
);
598 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
599 status
->delay
= status
->avail
;
603 status
->avail_max
= runtime
->avail_max
;
604 status
->overrange
= runtime
->overrange
;
605 runtime
->avail_max
= 0;
606 runtime
->overrange
= 0;
608 snd_pcm_stream_unlock_irq(substream
);
612 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
613 struct snd_pcm_status __user
* _status
)
615 struct snd_pcm_status status
;
616 struct snd_pcm_runtime
*runtime
;
619 snd_assert(substream
!= NULL
, return -ENXIO
);
620 runtime
= substream
->runtime
;
621 memset(&status
, 0, sizeof(status
));
622 res
= snd_pcm_status(substream
, &status
);
625 if (copy_to_user(_status
, &status
, sizeof(status
)))
630 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
631 struct snd_pcm_channel_info
* info
)
633 struct snd_pcm_runtime
*runtime
;
634 unsigned int channel
;
636 snd_assert(substream
!= NULL
, return -ENXIO
);
637 channel
= info
->channel
;
638 runtime
= substream
->runtime
;
639 snd_pcm_stream_lock_irq(substream
);
640 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
641 snd_pcm_stream_unlock_irq(substream
);
644 snd_pcm_stream_unlock_irq(substream
);
645 if (channel
>= runtime
->channels
)
647 memset(info
, 0, sizeof(*info
));
648 info
->channel
= channel
;
649 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
652 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
653 struct snd_pcm_channel_info __user
* _info
)
655 struct snd_pcm_channel_info info
;
658 if (copy_from_user(&info
, _info
, sizeof(info
)))
660 res
= snd_pcm_channel_info(substream
, &info
);
663 if (copy_to_user(_info
, &info
, sizeof(info
)))
668 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
670 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
671 if (runtime
->trigger_master
== NULL
)
673 if (runtime
->trigger_master
== substream
) {
674 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
676 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
677 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
679 runtime
->trigger_master
= NULL
;
683 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
684 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
685 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
686 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
690 * this functions is core for handling of linked stream
691 * Note: the stream state might be changed also on failure
692 * Note2: call with calling stream lock + link lock
694 static int snd_pcm_action_group(struct action_ops
*ops
,
695 struct snd_pcm_substream
*substream
,
696 int state
, int do_lock
)
698 struct snd_pcm_substream
*s
= NULL
;
699 struct snd_pcm_substream
*s1
;
702 snd_pcm_group_for_each_entry(s
, substream
) {
703 if (do_lock
&& s
!= substream
)
704 spin_lock_nested(&s
->self_group
.lock
,
705 SINGLE_DEPTH_NESTING
);
706 res
= ops
->pre_action(s
, state
);
710 snd_pcm_group_for_each_entry(s
, substream
) {
711 res
= ops
->do_action(s
, state
);
713 if (ops
->undo_action
) {
714 snd_pcm_group_for_each_entry(s1
, substream
) {
715 if (s1
== s
) /* failed stream */
717 ops
->undo_action(s1
, state
);
720 s
= NULL
; /* unlock all */
724 snd_pcm_group_for_each_entry(s
, substream
) {
725 ops
->post_action(s
, state
);
730 snd_pcm_group_for_each_entry(s1
, substream
) {
732 spin_unlock(&s1
->self_group
.lock
);
733 if (s1
== s
) /* end */
741 * Note: call with stream lock
743 static int snd_pcm_action_single(struct action_ops
*ops
,
744 struct snd_pcm_substream
*substream
,
749 res
= ops
->pre_action(substream
, state
);
752 res
= ops
->do_action(substream
, state
);
754 ops
->post_action(substream
, state
);
755 else if (ops
->undo_action
)
756 ops
->undo_action(substream
, state
);
761 * Note: call with stream lock
763 static int snd_pcm_action(struct action_ops
*ops
,
764 struct snd_pcm_substream
*substream
,
769 if (snd_pcm_stream_linked(substream
)) {
770 if (!spin_trylock(&substream
->group
->lock
)) {
771 spin_unlock(&substream
->self_group
.lock
);
772 spin_lock(&substream
->group
->lock
);
773 spin_lock(&substream
->self_group
.lock
);
775 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
776 spin_unlock(&substream
->group
->lock
);
778 res
= snd_pcm_action_single(ops
, substream
, state
);
784 * Note: don't use any locks before
786 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
787 struct snd_pcm_substream
*substream
,
792 read_lock_irq(&snd_pcm_link_rwlock
);
793 if (snd_pcm_stream_linked(substream
)) {
794 spin_lock(&substream
->group
->lock
);
795 spin_lock(&substream
->self_group
.lock
);
796 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
797 spin_unlock(&substream
->self_group
.lock
);
798 spin_unlock(&substream
->group
->lock
);
800 spin_lock(&substream
->self_group
.lock
);
801 res
= snd_pcm_action_single(ops
, substream
, state
);
802 spin_unlock(&substream
->self_group
.lock
);
804 read_unlock_irq(&snd_pcm_link_rwlock
);
810 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
811 struct snd_pcm_substream
*substream
,
816 down_read(&snd_pcm_link_rwsem
);
817 if (snd_pcm_stream_linked(substream
))
818 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
820 res
= snd_pcm_action_single(ops
, substream
, state
);
821 up_read(&snd_pcm_link_rwsem
);
828 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
830 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
831 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
833 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
834 !snd_pcm_playback_data(substream
))
836 runtime
->trigger_master
= substream
;
840 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
842 if (substream
->runtime
->trigger_master
!= substream
)
844 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
847 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
849 if (substream
->runtime
->trigger_master
== substream
)
850 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
853 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
855 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
856 snd_pcm_trigger_tstamp(substream
);
857 runtime
->status
->state
= state
;
858 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
859 runtime
->silence_size
> 0)
860 snd_pcm_playback_silence(substream
, ULONG_MAX
);
861 if (substream
->timer
)
862 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
863 &runtime
->trigger_tstamp
);
866 static struct action_ops snd_pcm_action_start
= {
867 .pre_action
= snd_pcm_pre_start
,
868 .do_action
= snd_pcm_do_start
,
869 .undo_action
= snd_pcm_undo_start
,
870 .post_action
= snd_pcm_post_start
875 * @substream: the PCM substream instance
877 * Start all linked streams.
879 int snd_pcm_start(struct snd_pcm_substream
*substream
)
881 return snd_pcm_action(&snd_pcm_action_start
, substream
,
882 SNDRV_PCM_STATE_RUNNING
);
888 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
890 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
891 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
893 runtime
->trigger_master
= substream
;
897 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
899 if (substream
->runtime
->trigger_master
== substream
&&
900 snd_pcm_running(substream
))
901 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
902 return 0; /* unconditonally stop all substreams */
905 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
907 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
908 if (runtime
->status
->state
!= state
) {
909 snd_pcm_trigger_tstamp(substream
);
910 if (substream
->timer
)
911 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
912 &runtime
->trigger_tstamp
);
913 runtime
->status
->state
= state
;
915 wake_up(&runtime
->sleep
);
918 static struct action_ops snd_pcm_action_stop
= {
919 .pre_action
= snd_pcm_pre_stop
,
920 .do_action
= snd_pcm_do_stop
,
921 .post_action
= snd_pcm_post_stop
926 * @substream: the PCM substream instance
927 * @state: PCM state after stopping the stream
929 * Try to stop all running streams in the substream group.
930 * The state of each stream is changed to the given value after that unconditionally.
932 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
934 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
937 EXPORT_SYMBOL(snd_pcm_stop
);
941 * @substream: the PCM substream
943 * Stop the DMA only when the given stream is playback.
944 * The state is changed to SETUP.
945 * Unlike snd_pcm_stop(), this affects only the given stream.
947 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
949 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
950 SNDRV_PCM_STATE_SETUP
);
956 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
958 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
959 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
962 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
964 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
966 runtime
->trigger_master
= substream
;
970 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
972 if (substream
->runtime
->trigger_master
!= substream
)
974 return substream
->ops
->trigger(substream
,
975 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
976 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
979 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
981 if (substream
->runtime
->trigger_master
== substream
)
982 substream
->ops
->trigger(substream
,
983 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
984 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
987 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
989 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
990 snd_pcm_trigger_tstamp(substream
);
992 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
993 if (substream
->timer
)
994 snd_timer_notify(substream
->timer
,
995 SNDRV_TIMER_EVENT_MPAUSE
,
996 &runtime
->trigger_tstamp
);
997 wake_up(&runtime
->sleep
);
999 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1000 if (substream
->timer
)
1001 snd_timer_notify(substream
->timer
,
1002 SNDRV_TIMER_EVENT_MCONTINUE
,
1003 &runtime
->trigger_tstamp
);
1007 static struct action_ops snd_pcm_action_pause
= {
1008 .pre_action
= snd_pcm_pre_pause
,
1009 .do_action
= snd_pcm_do_pause
,
1010 .undo_action
= snd_pcm_undo_pause
,
1011 .post_action
= snd_pcm_post_pause
1015 * Push/release the pause for all linked streams.
1017 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1019 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1025 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1027 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1028 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1030 runtime
->trigger_master
= substream
;
1034 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1036 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1037 if (runtime
->trigger_master
!= substream
)
1039 if (! snd_pcm_running(substream
))
1041 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1042 return 0; /* suspend unconditionally */
1045 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1047 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1048 snd_pcm_trigger_tstamp(substream
);
1049 if (substream
->timer
)
1050 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1051 &runtime
->trigger_tstamp
);
1052 runtime
->status
->suspended_state
= runtime
->status
->state
;
1053 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1054 wake_up(&runtime
->sleep
);
1057 static struct action_ops snd_pcm_action_suspend
= {
1058 .pre_action
= snd_pcm_pre_suspend
,
1059 .do_action
= snd_pcm_do_suspend
,
1060 .post_action
= snd_pcm_post_suspend
1065 * @substream: the PCM substream
1067 * Trigger SUSPEND to all linked streams.
1068 * After this call, all streams are changed to SUSPENDED state.
1070 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1073 unsigned long flags
;
1078 snd_pcm_stream_lock_irqsave(substream
, flags
);
1079 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1080 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1084 EXPORT_SYMBOL(snd_pcm_suspend
);
1087 * snd_pcm_suspend_all
1088 * @pcm: the PCM instance
1090 * Trigger SUSPEND to all substreams in the given pcm.
1091 * After this call, all streams are changed to SUSPENDED state.
1093 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1095 struct snd_pcm_substream
*substream
;
1096 int stream
, err
= 0;
1101 for (stream
= 0; stream
< 2; stream
++) {
1102 for (substream
= pcm
->streams
[stream
].substream
;
1103 substream
; substream
= substream
->next
) {
1104 /* FIXME: the open/close code should lock this as well */
1105 if (substream
->runtime
== NULL
)
1107 err
= snd_pcm_suspend(substream
);
1108 if (err
< 0 && err
!= -EBUSY
)
1115 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1119 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1121 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1122 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1124 runtime
->trigger_master
= substream
;
1128 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1130 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1131 if (runtime
->trigger_master
!= substream
)
1133 /* DMA not running previously? */
1134 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1135 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1136 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1138 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1141 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1143 if (substream
->runtime
->trigger_master
== substream
&&
1144 snd_pcm_running(substream
))
1145 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1148 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1150 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1151 snd_pcm_trigger_tstamp(substream
);
1152 if (substream
->timer
)
1153 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1154 &runtime
->trigger_tstamp
);
1155 runtime
->status
->state
= runtime
->status
->suspended_state
;
1158 static struct action_ops snd_pcm_action_resume
= {
1159 .pre_action
= snd_pcm_pre_resume
,
1160 .do_action
= snd_pcm_do_resume
,
1161 .undo_action
= snd_pcm_undo_resume
,
1162 .post_action
= snd_pcm_post_resume
1165 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1167 struct snd_card
*card
= substream
->pcm
->card
;
1170 snd_power_lock(card
);
1171 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1172 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1173 snd_power_unlock(card
);
1179 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1184 #endif /* CONFIG_PM */
1189 * Change the RUNNING stream(s) to XRUN state.
1191 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1193 struct snd_card
*card
= substream
->pcm
->card
;
1194 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1197 snd_power_lock(card
);
1198 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1199 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1204 snd_pcm_stream_lock_irq(substream
);
1205 switch (runtime
->status
->state
) {
1206 case SNDRV_PCM_STATE_XRUN
:
1207 result
= 0; /* already there */
1209 case SNDRV_PCM_STATE_RUNNING
:
1210 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1215 snd_pcm_stream_unlock_irq(substream
);
1217 snd_power_unlock(card
);
1224 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1226 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1227 switch (runtime
->status
->state
) {
1228 case SNDRV_PCM_STATE_RUNNING
:
1229 case SNDRV_PCM_STATE_PREPARED
:
1230 case SNDRV_PCM_STATE_PAUSED
:
1231 case SNDRV_PCM_STATE_SUSPENDED
:
1238 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1240 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1241 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1244 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1245 runtime
->hw_ptr_base
= 0;
1246 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1247 runtime
->status
->hw_ptr
% runtime
->period_size
;
1248 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1249 runtime
->silence_filled
= 0;
1253 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1255 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1256 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1257 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1258 runtime
->silence_size
> 0)
1259 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1262 static struct action_ops snd_pcm_action_reset
= {
1263 .pre_action
= snd_pcm_pre_reset
,
1264 .do_action
= snd_pcm_do_reset
,
1265 .post_action
= snd_pcm_post_reset
1268 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1270 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1276 /* we use the second argument for updating f_flags */
1277 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1280 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1281 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1282 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1284 if (snd_pcm_running(substream
))
1286 substream
->f_flags
= f_flags
;
1290 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1293 err
= substream
->ops
->prepare(substream
);
1296 return snd_pcm_do_reset(substream
, 0);
1299 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1301 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1302 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1303 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1306 static struct action_ops snd_pcm_action_prepare
= {
1307 .pre_action
= snd_pcm_pre_prepare
,
1308 .do_action
= snd_pcm_do_prepare
,
1309 .post_action
= snd_pcm_post_prepare
1314 * @substream: the PCM substream instance
1315 * @file: file to refer f_flags
1317 * Prepare the PCM substream to be triggerable.
1319 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1323 struct snd_card
*card
= substream
->pcm
->card
;
1327 f_flags
= file
->f_flags
;
1329 f_flags
= substream
->f_flags
;
1331 snd_power_lock(card
);
1332 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1333 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1334 substream
, f_flags
);
1335 snd_power_unlock(card
);
1343 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1345 if (substream
->f_flags
& O_NONBLOCK
)
1347 substream
->runtime
->trigger_master
= substream
;
1351 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1353 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1354 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1355 switch (runtime
->status
->state
) {
1356 case SNDRV_PCM_STATE_PREPARED
:
1357 /* start playback stream if possible */
1358 if (! snd_pcm_playback_empty(substream
)) {
1359 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1360 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1363 case SNDRV_PCM_STATE_RUNNING
:
1364 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1370 /* stop running stream */
1371 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1372 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1373 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1374 snd_pcm_do_stop(substream
, new_state
);
1375 snd_pcm_post_stop(substream
, new_state
);
1381 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1385 static struct action_ops snd_pcm_action_drain_init
= {
1386 .pre_action
= snd_pcm_pre_drain_init
,
1387 .do_action
= snd_pcm_do_drain_init
,
1388 .post_action
= snd_pcm_post_drain_init
1392 struct snd_pcm_substream
*substream
;
1394 snd_pcm_uframes_t stop_threshold
;
1397 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1400 * Drain the stream(s).
1401 * When the substream is linked, sync until the draining of all playback streams
1403 * After this call, all streams are supposed to be either SETUP or DRAINING
1404 * (capture only) state.
1406 static int snd_pcm_drain(struct snd_pcm_substream
*substream
)
1408 struct snd_card
*card
;
1409 struct snd_pcm_runtime
*runtime
;
1410 struct snd_pcm_substream
*s
;
1413 struct drain_rec
*drec
, drec_tmp
, *d
;
1415 snd_assert(substream
!= NULL
, return -ENXIO
);
1416 card
= substream
->pcm
->card
;
1417 runtime
= substream
->runtime
;
1419 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1422 snd_power_lock(card
);
1423 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1424 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1426 snd_power_unlock(card
);
1431 /* allocate temporary record for drain sync */
1432 down_read(&snd_pcm_link_rwsem
);
1433 if (snd_pcm_stream_linked(substream
)) {
1434 drec
= kmalloc(substream
->group
->count
* sizeof(*drec
), GFP_KERNEL
);
1436 up_read(&snd_pcm_link_rwsem
);
1437 snd_power_unlock(card
);
1443 /* count only playback streams */
1445 snd_pcm_group_for_each_entry(s
, substream
) {
1446 runtime
= s
->runtime
;
1447 if (s
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1448 d
= &drec
[num_drecs
++];
1450 init_waitqueue_entry(&d
->wait
, current
);
1451 add_wait_queue(&runtime
->sleep
, &d
->wait
);
1452 /* stop_threshold fixup to avoid endless loop when
1453 * stop_threshold > buffer_size
1455 d
->stop_threshold
= runtime
->stop_threshold
;
1456 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1457 runtime
->stop_threshold
= runtime
->buffer_size
;
1460 up_read(&snd_pcm_link_rwsem
);
1462 snd_pcm_stream_lock_irq(substream
);
1464 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1465 snd_pcm_pause(substream
, 0);
1467 /* pre-start/stop - all running streams are changed to DRAINING state */
1468 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1470 snd_pcm_stream_unlock_irq(substream
);
1476 if (signal_pending(current
)) {
1477 result
= -ERESTARTSYS
;
1481 for (i
= 0; i
< num_drecs
; i
++) {
1482 runtime
= drec
[i
].substream
->runtime
;
1483 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
1487 break; /* yes, all drained */
1489 set_current_state(TASK_INTERRUPTIBLE
);
1490 snd_pcm_stream_unlock_irq(substream
);
1491 snd_power_unlock(card
);
1492 tout
= schedule_timeout(10 * HZ
);
1493 snd_power_lock(card
);
1494 snd_pcm_stream_lock_irq(substream
);
1496 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1499 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1500 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1507 snd_pcm_stream_unlock_irq(substream
);
1510 for (i
= 0; i
< num_drecs
; i
++) {
1512 runtime
= d
->substream
->runtime
;
1513 remove_wait_queue(&runtime
->sleep
, &d
->wait
);
1514 runtime
->stop_threshold
= d
->stop_threshold
;
1517 if (drec
!= &drec_tmp
)
1519 snd_power_unlock(card
);
1527 * Immediately put all linked substreams into SETUP state.
1529 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1531 struct snd_pcm_runtime
*runtime
;
1532 struct snd_card
*card
;
1535 snd_assert(substream
!= NULL
, return -ENXIO
);
1536 runtime
= substream
->runtime
;
1537 card
= substream
->pcm
->card
;
1539 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1540 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1543 snd_power_lock(card
);
1544 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1545 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1550 snd_pcm_stream_lock_irq(substream
);
1552 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1553 snd_pcm_pause(substream
, 0);
1555 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1556 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1557 snd_pcm_stream_unlock_irq(substream
);
1559 snd_power_unlock(card
);
1564 /* WARNING: Don't forget to fput back the file */
1565 static struct file
*snd_pcm_file_fd(int fd
)
1568 struct inode
*inode
;
1574 inode
= file
->f_path
.dentry
->d_inode
;
1575 if (!S_ISCHR(inode
->i_mode
) ||
1576 imajor(inode
) != snd_major
) {
1580 minor
= iminor(inode
);
1581 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1582 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1592 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1596 struct snd_pcm_file
*pcm_file
;
1597 struct snd_pcm_substream
*substream1
;
1599 file
= snd_pcm_file_fd(fd
);
1602 pcm_file
= file
->private_data
;
1603 substream1
= pcm_file
->substream
;
1604 down_write(&snd_pcm_link_rwsem
);
1605 write_lock_irq(&snd_pcm_link_rwlock
);
1606 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1607 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1611 if (snd_pcm_stream_linked(substream1
)) {
1615 if (!snd_pcm_stream_linked(substream
)) {
1616 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1617 if (substream
->group
== NULL
) {
1621 spin_lock_init(&substream
->group
->lock
);
1622 INIT_LIST_HEAD(&substream
->group
->substreams
);
1623 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1624 substream
->group
->count
= 1;
1626 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1627 substream
->group
->count
++;
1628 substream1
->group
= substream
->group
;
1630 write_unlock_irq(&snd_pcm_link_rwlock
);
1631 up_write(&snd_pcm_link_rwsem
);
1636 static void relink_to_local(struct snd_pcm_substream
*substream
)
1638 substream
->group
= &substream
->self_group
;
1639 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1640 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1643 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1645 struct snd_pcm_substream
*s
;
1648 down_write(&snd_pcm_link_rwsem
);
1649 write_lock_irq(&snd_pcm_link_rwlock
);
1650 if (!snd_pcm_stream_linked(substream
)) {
1654 list_del(&substream
->link_list
);
1655 substream
->group
->count
--;
1656 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1657 snd_pcm_group_for_each_entry(s
, substream
) {
1661 kfree(substream
->group
);
1663 relink_to_local(substream
);
1665 write_unlock_irq(&snd_pcm_link_rwlock
);
1666 up_write(&snd_pcm_link_rwsem
);
1673 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1674 struct snd_pcm_hw_rule
*rule
)
1676 struct snd_interval t
;
1677 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1678 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1679 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1682 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1683 struct snd_pcm_hw_rule
*rule
)
1685 struct snd_interval t
;
1686 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1687 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1688 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1691 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1692 struct snd_pcm_hw_rule
*rule
)
1694 struct snd_interval t
;
1695 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1696 hw_param_interval_c(params
, rule
->deps
[1]),
1697 (unsigned long) rule
->private, &t
);
1698 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1701 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1702 struct snd_pcm_hw_rule
*rule
)
1704 struct snd_interval t
;
1705 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1706 (unsigned long) rule
->private,
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_format(struct snd_pcm_hw_params
*params
,
1712 struct snd_pcm_hw_rule
*rule
)
1715 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1717 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1719 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1721 if (! snd_mask_test(mask
, k
))
1723 bits
= snd_pcm_format_physical_width(k
);
1725 continue; /* ignore invalid formats */
1726 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1727 snd_mask_reset(&m
, k
);
1729 return snd_mask_refine(mask
, &m
);
1732 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1733 struct snd_pcm_hw_rule
*rule
)
1735 struct snd_interval t
;
1741 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1743 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1745 bits
= snd_pcm_format_physical_width(k
);
1747 continue; /* ignore invalid formats */
1748 if (t
.min
> (unsigned)bits
)
1750 if (t
.max
< (unsigned)bits
)
1754 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1757 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1758 #error "Change this table"
1761 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1762 48000, 64000, 88200, 96000, 176400, 192000 };
1764 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1765 .count
= ARRAY_SIZE(rates
),
1769 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1770 struct snd_pcm_hw_rule
*rule
)
1772 struct snd_pcm_hardware
*hw
= rule
->private;
1773 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1774 snd_pcm_known_rates
.count
,
1775 snd_pcm_known_rates
.list
, hw
->rates
);
1778 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1779 struct snd_pcm_hw_rule
*rule
)
1781 struct snd_interval t
;
1782 struct snd_pcm_substream
*substream
= rule
->private;
1784 t
.max
= substream
->buffer_bytes_max
;
1788 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1791 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1793 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1794 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1797 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1798 snd_mask_any(constrs_mask(constrs
, k
));
1801 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1802 snd_interval_any(constrs_interval(constrs
, k
));
1805 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1806 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1807 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1808 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1809 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1811 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1812 snd_pcm_hw_rule_format
, NULL
,
1813 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1816 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1817 snd_pcm_hw_rule_sample_bits
, NULL
,
1818 SNDRV_PCM_HW_PARAM_FORMAT
,
1819 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1822 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1823 snd_pcm_hw_rule_div
, NULL
,
1824 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1827 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1828 snd_pcm_hw_rule_mul
, NULL
,
1829 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1832 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1833 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1834 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1837 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1838 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1839 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1842 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1843 snd_pcm_hw_rule_div
, NULL
,
1844 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1847 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1848 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1849 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1852 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1853 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1854 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1857 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1858 snd_pcm_hw_rule_div
, NULL
,
1859 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1862 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1863 snd_pcm_hw_rule_div
, NULL
,
1864 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1867 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1868 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1869 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1872 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1873 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1874 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1877 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1878 snd_pcm_hw_rule_mul
, NULL
,
1879 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1882 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1883 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1884 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1887 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1888 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1889 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1892 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1893 snd_pcm_hw_rule_muldivk
, (void*) 8,
1894 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1897 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1898 snd_pcm_hw_rule_muldivk
, (void*) 8,
1899 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1902 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1903 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1904 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1907 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1908 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1909 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1915 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1917 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1918 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1920 unsigned int mask
= 0;
1922 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1923 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1924 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1925 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1926 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1927 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1928 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1929 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1930 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1931 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1932 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1934 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1935 snd_assert(err
>= 0, return -EINVAL
);
1937 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1938 snd_assert(err
>= 0, return -EINVAL
);
1940 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1941 snd_assert(err
>= 0, return -EINVAL
);
1943 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1944 hw
->channels_min
, hw
->channels_max
);
1945 snd_assert(err
>= 0, return -EINVAL
);
1947 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1948 hw
->rate_min
, hw
->rate_max
);
1949 snd_assert(err
>= 0, return -EINVAL
);
1951 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1952 hw
->period_bytes_min
, hw
->period_bytes_max
);
1953 snd_assert(err
>= 0, return -EINVAL
);
1955 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1956 hw
->periods_min
, hw
->periods_max
);
1957 snd_assert(err
>= 0, return -EINVAL
);
1959 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1960 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1961 snd_assert(err
>= 0, return -EINVAL
);
1963 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1964 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1965 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1970 if (runtime
->dma_bytes
) {
1971 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1972 snd_assert(err
>= 0, return -EINVAL
);
1975 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1976 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1977 snd_pcm_hw_rule_rate
, hw
,
1978 SNDRV_PCM_HW_PARAM_RATE
, -1);
1983 /* FIXME: this belong to lowlevel */
1984 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1989 static void pcm_release_private(struct snd_pcm_substream
*substream
)
1991 snd_pcm_unlink(substream
);
1994 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
1996 substream
->ref_count
--;
1997 if (substream
->ref_count
> 0)
2000 snd_pcm_drop(substream
);
2001 if (substream
->hw_opened
) {
2002 if (substream
->ops
->hw_free
!= NULL
)
2003 substream
->ops
->hw_free(substream
);
2004 substream
->ops
->close(substream
);
2005 substream
->hw_opened
= 0;
2007 if (substream
->pcm_release
) {
2008 substream
->pcm_release(substream
);
2009 substream
->pcm_release
= NULL
;
2011 snd_pcm_detach_substream(substream
);
2014 EXPORT_SYMBOL(snd_pcm_release_substream
);
2016 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2018 struct snd_pcm_substream
**rsubstream
)
2020 struct snd_pcm_substream
*substream
;
2023 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2026 if (substream
->ref_count
> 1) {
2027 *rsubstream
= substream
;
2031 err
= snd_pcm_hw_constraints_init(substream
);
2033 snd_printd("snd_pcm_hw_constraints_init failed\n");
2037 if ((err
= substream
->ops
->open(substream
)) < 0)
2040 substream
->hw_opened
= 1;
2042 err
= snd_pcm_hw_constraints_complete(substream
);
2044 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2048 *rsubstream
= substream
;
2052 snd_pcm_release_substream(substream
);
2056 EXPORT_SYMBOL(snd_pcm_open_substream
);
2058 static int snd_pcm_open_file(struct file
*file
,
2059 struct snd_pcm
*pcm
,
2061 struct snd_pcm_file
**rpcm_file
)
2063 struct snd_pcm_file
*pcm_file
;
2064 struct snd_pcm_substream
*substream
;
2065 struct snd_pcm_str
*str
;
2068 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
2071 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2075 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2076 if (pcm_file
== NULL
) {
2077 snd_pcm_release_substream(substream
);
2080 pcm_file
->substream
= substream
;
2081 if (substream
->ref_count
== 1) {
2082 str
= substream
->pstr
;
2083 substream
->file
= pcm_file
;
2084 substream
->pcm_release
= pcm_release_private
;
2086 file
->private_data
= pcm_file
;
2087 *rpcm_file
= pcm_file
;
2091 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2093 struct snd_pcm
*pcm
;
2095 pcm
= snd_lookup_minor_data(iminor(inode
),
2096 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2097 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2100 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2102 struct snd_pcm
*pcm
;
2104 pcm
= snd_lookup_minor_data(iminor(inode
),
2105 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2106 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2109 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2112 struct snd_pcm_file
*pcm_file
;
2119 err
= snd_card_file_add(pcm
->card
, file
);
2122 if (!try_module_get(pcm
->card
->module
)) {
2126 init_waitqueue_entry(&wait
, current
);
2127 add_wait_queue(&pcm
->open_wait
, &wait
);
2128 mutex_lock(&pcm
->open_mutex
);
2130 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2133 if (err
== -EAGAIN
) {
2134 if (file
->f_flags
& O_NONBLOCK
) {
2140 set_current_state(TASK_INTERRUPTIBLE
);
2141 mutex_unlock(&pcm
->open_mutex
);
2143 mutex_lock(&pcm
->open_mutex
);
2144 if (signal_pending(current
)) {
2149 remove_wait_queue(&pcm
->open_wait
, &wait
);
2150 mutex_unlock(&pcm
->open_mutex
);
2156 module_put(pcm
->card
->module
);
2158 snd_card_file_remove(pcm
->card
, file
);
2163 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2165 struct snd_pcm
*pcm
;
2166 struct snd_pcm_substream
*substream
;
2167 struct snd_pcm_file
*pcm_file
;
2169 pcm_file
= file
->private_data
;
2170 substream
= pcm_file
->substream
;
2171 snd_assert(substream
!= NULL
, return -ENXIO
);
2172 pcm
= substream
->pcm
;
2173 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2174 mutex_lock(&pcm
->open_mutex
);
2175 snd_pcm_release_substream(substream
);
2177 mutex_unlock(&pcm
->open_mutex
);
2178 wake_up(&pcm
->open_wait
);
2179 module_put(pcm
->card
->module
);
2180 snd_card_file_remove(pcm
->card
, file
);
2184 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2185 snd_pcm_uframes_t frames
)
2187 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2188 snd_pcm_sframes_t appl_ptr
;
2189 snd_pcm_sframes_t ret
;
2190 snd_pcm_sframes_t hw_avail
;
2195 snd_pcm_stream_lock_irq(substream
);
2196 switch (runtime
->status
->state
) {
2197 case SNDRV_PCM_STATE_PREPARED
:
2199 case SNDRV_PCM_STATE_DRAINING
:
2200 case SNDRV_PCM_STATE_RUNNING
:
2201 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2204 case SNDRV_PCM_STATE_XRUN
:
2212 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2213 if (hw_avail
<= 0) {
2217 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2219 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2221 appl_ptr
+= runtime
->boundary
;
2222 runtime
->control
->appl_ptr
= appl_ptr
;
2225 snd_pcm_stream_unlock_irq(substream
);
2229 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2230 snd_pcm_uframes_t frames
)
2232 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2233 snd_pcm_sframes_t appl_ptr
;
2234 snd_pcm_sframes_t ret
;
2235 snd_pcm_sframes_t hw_avail
;
2240 snd_pcm_stream_lock_irq(substream
);
2241 switch (runtime
->status
->state
) {
2242 case SNDRV_PCM_STATE_PREPARED
:
2243 case SNDRV_PCM_STATE_DRAINING
:
2245 case SNDRV_PCM_STATE_RUNNING
:
2246 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2249 case SNDRV_PCM_STATE_XRUN
:
2257 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2258 if (hw_avail
<= 0) {
2262 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2264 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2266 appl_ptr
+= runtime
->boundary
;
2267 runtime
->control
->appl_ptr
= appl_ptr
;
2270 snd_pcm_stream_unlock_irq(substream
);
2274 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2275 snd_pcm_uframes_t frames
)
2277 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2278 snd_pcm_sframes_t appl_ptr
;
2279 snd_pcm_sframes_t ret
;
2280 snd_pcm_sframes_t avail
;
2285 snd_pcm_stream_lock_irq(substream
);
2286 switch (runtime
->status
->state
) {
2287 case SNDRV_PCM_STATE_PREPARED
:
2288 case SNDRV_PCM_STATE_PAUSED
:
2290 case SNDRV_PCM_STATE_DRAINING
:
2291 case SNDRV_PCM_STATE_RUNNING
:
2292 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2295 case SNDRV_PCM_STATE_XRUN
:
2303 avail
= snd_pcm_playback_avail(runtime
);
2308 if (frames
> (snd_pcm_uframes_t
)avail
)
2310 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2311 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2312 appl_ptr
-= runtime
->boundary
;
2313 runtime
->control
->appl_ptr
= appl_ptr
;
2316 snd_pcm_stream_unlock_irq(substream
);
2320 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2321 snd_pcm_uframes_t frames
)
2323 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2324 snd_pcm_sframes_t appl_ptr
;
2325 snd_pcm_sframes_t ret
;
2326 snd_pcm_sframes_t avail
;
2331 snd_pcm_stream_lock_irq(substream
);
2332 switch (runtime
->status
->state
) {
2333 case SNDRV_PCM_STATE_PREPARED
:
2334 case SNDRV_PCM_STATE_DRAINING
:
2335 case SNDRV_PCM_STATE_PAUSED
:
2337 case SNDRV_PCM_STATE_RUNNING
:
2338 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2341 case SNDRV_PCM_STATE_XRUN
:
2349 avail
= snd_pcm_capture_avail(runtime
);
2354 if (frames
> (snd_pcm_uframes_t
)avail
)
2356 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2357 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2358 appl_ptr
-= runtime
->boundary
;
2359 runtime
->control
->appl_ptr
= appl_ptr
;
2362 snd_pcm_stream_unlock_irq(substream
);
2366 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2368 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2371 snd_pcm_stream_lock_irq(substream
);
2372 switch (runtime
->status
->state
) {
2373 case SNDRV_PCM_STATE_DRAINING
:
2374 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2376 case SNDRV_PCM_STATE_RUNNING
:
2377 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2380 case SNDRV_PCM_STATE_PREPARED
:
2381 case SNDRV_PCM_STATE_SUSPENDED
:
2384 case SNDRV_PCM_STATE_XRUN
:
2392 snd_pcm_stream_unlock_irq(substream
);
2396 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2397 snd_pcm_sframes_t __user
*res
)
2399 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2401 snd_pcm_sframes_t n
= 0;
2403 snd_pcm_stream_lock_irq(substream
);
2404 switch (runtime
->status
->state
) {
2405 case SNDRV_PCM_STATE_DRAINING
:
2406 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2408 case SNDRV_PCM_STATE_RUNNING
:
2409 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2412 case SNDRV_PCM_STATE_PREPARED
:
2413 case SNDRV_PCM_STATE_SUSPENDED
:
2415 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2416 n
= snd_pcm_playback_hw_avail(runtime
);
2418 n
= snd_pcm_capture_avail(runtime
);
2420 case SNDRV_PCM_STATE_XRUN
:
2428 snd_pcm_stream_unlock_irq(substream
);
2430 if (put_user(n
, res
))
2435 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2436 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2438 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2439 struct snd_pcm_sync_ptr sync_ptr
;
2440 volatile struct snd_pcm_mmap_status
*status
;
2441 volatile struct snd_pcm_mmap_control
*control
;
2444 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2445 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2447 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2449 status
= runtime
->status
;
2450 control
= runtime
->control
;
2451 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2452 err
= snd_pcm_hwsync(substream
);
2456 snd_pcm_stream_lock_irq(substream
);
2457 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2458 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2460 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2461 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2462 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2464 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2465 sync_ptr
.s
.status
.state
= status
->state
;
2466 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2467 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2468 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2469 snd_pcm_stream_unlock_irq(substream
);
2470 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2475 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2477 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2480 if (get_user(arg
, _arg
))
2482 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2484 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY
;
2485 if (arg
== SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
)
2486 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
;
2490 static int snd_pcm_common_ioctl1(struct file
*file
,
2491 struct snd_pcm_substream
*substream
,
2492 unsigned int cmd
, void __user
*arg
)
2494 snd_assert(substream
!= NULL
, return -ENXIO
);
2497 case SNDRV_PCM_IOCTL_PVERSION
:
2498 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2499 case SNDRV_PCM_IOCTL_INFO
:
2500 return snd_pcm_info_user(substream
, arg
);
2501 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2503 case SNDRV_PCM_IOCTL_TTSTAMP
:
2504 return snd_pcm_tstamp(substream
, arg
);
2505 case SNDRV_PCM_IOCTL_HW_REFINE
:
2506 return snd_pcm_hw_refine_user(substream
, arg
);
2507 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2508 return snd_pcm_hw_params_user(substream
, arg
);
2509 case SNDRV_PCM_IOCTL_HW_FREE
:
2510 return snd_pcm_hw_free(substream
);
2511 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2512 return snd_pcm_sw_params_user(substream
, arg
);
2513 case SNDRV_PCM_IOCTL_STATUS
:
2514 return snd_pcm_status_user(substream
, arg
);
2515 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2516 return snd_pcm_channel_info_user(substream
, arg
);
2517 case SNDRV_PCM_IOCTL_PREPARE
:
2518 return snd_pcm_prepare(substream
, file
);
2519 case SNDRV_PCM_IOCTL_RESET
:
2520 return snd_pcm_reset(substream
);
2521 case SNDRV_PCM_IOCTL_START
:
2522 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2523 case SNDRV_PCM_IOCTL_LINK
:
2524 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2525 case SNDRV_PCM_IOCTL_UNLINK
:
2526 return snd_pcm_unlink(substream
);
2527 case SNDRV_PCM_IOCTL_RESUME
:
2528 return snd_pcm_resume(substream
);
2529 case SNDRV_PCM_IOCTL_XRUN
:
2530 return snd_pcm_xrun(substream
);
2531 case SNDRV_PCM_IOCTL_HWSYNC
:
2532 return snd_pcm_hwsync(substream
);
2533 case SNDRV_PCM_IOCTL_DELAY
:
2534 return snd_pcm_delay(substream
, arg
);
2535 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2536 return snd_pcm_sync_ptr(substream
, arg
);
2537 #ifdef CONFIG_SND_SUPPORT_OLD_API
2538 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2539 return snd_pcm_hw_refine_old_user(substream
, arg
);
2540 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2541 return snd_pcm_hw_params_old_user(substream
, arg
);
2543 case SNDRV_PCM_IOCTL_DRAIN
:
2544 return snd_pcm_drain(substream
);
2545 case SNDRV_PCM_IOCTL_DROP
:
2546 return snd_pcm_drop(substream
);
2547 case SNDRV_PCM_IOCTL_PAUSE
:
2550 snd_pcm_stream_lock_irq(substream
);
2551 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2552 snd_pcm_stream_unlock_irq(substream
);
2556 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2560 static int snd_pcm_playback_ioctl1(struct file
*file
,
2561 struct snd_pcm_substream
*substream
,
2562 unsigned int cmd
, void __user
*arg
)
2564 snd_assert(substream
!= NULL
, return -ENXIO
);
2565 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2567 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2569 struct snd_xferi xferi
;
2570 struct snd_xferi __user
*_xferi
= arg
;
2571 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2572 snd_pcm_sframes_t result
;
2573 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2575 if (put_user(0, &_xferi
->result
))
2577 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2579 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2580 __put_user(result
, &_xferi
->result
);
2581 return result
< 0 ? result
: 0;
2583 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2585 struct snd_xfern xfern
;
2586 struct snd_xfern __user
*_xfern
= arg
;
2587 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2589 snd_pcm_sframes_t result
;
2590 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2592 if (runtime
->channels
> 128)
2594 if (put_user(0, &_xfern
->result
))
2596 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2598 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2601 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2605 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2607 __put_user(result
, &_xfern
->result
);
2608 return result
< 0 ? result
: 0;
2610 case SNDRV_PCM_IOCTL_REWIND
:
2612 snd_pcm_uframes_t frames
;
2613 snd_pcm_uframes_t __user
*_frames
= arg
;
2614 snd_pcm_sframes_t result
;
2615 if (get_user(frames
, _frames
))
2617 if (put_user(0, _frames
))
2619 result
= snd_pcm_playback_rewind(substream
, frames
);
2620 __put_user(result
, _frames
);
2621 return result
< 0 ? result
: 0;
2623 case SNDRV_PCM_IOCTL_FORWARD
:
2625 snd_pcm_uframes_t frames
;
2626 snd_pcm_uframes_t __user
*_frames
= arg
;
2627 snd_pcm_sframes_t result
;
2628 if (get_user(frames
, _frames
))
2630 if (put_user(0, _frames
))
2632 result
= snd_pcm_playback_forward(substream
, frames
);
2633 __put_user(result
, _frames
);
2634 return result
< 0 ? result
: 0;
2637 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2640 static int snd_pcm_capture_ioctl1(struct file
*file
,
2641 struct snd_pcm_substream
*substream
,
2642 unsigned int cmd
, void __user
*arg
)
2644 snd_assert(substream
!= NULL
, return -ENXIO
);
2645 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2647 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2649 struct snd_xferi xferi
;
2650 struct snd_xferi __user
*_xferi
= arg
;
2651 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2652 snd_pcm_sframes_t result
;
2653 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2655 if (put_user(0, &_xferi
->result
))
2657 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2659 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2660 __put_user(result
, &_xferi
->result
);
2661 return result
< 0 ? result
: 0;
2663 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2665 struct snd_xfern xfern
;
2666 struct snd_xfern __user
*_xfern
= arg
;
2667 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2669 snd_pcm_sframes_t result
;
2670 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2672 if (runtime
->channels
> 128)
2674 if (put_user(0, &_xfern
->result
))
2676 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2678 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2681 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2685 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2687 __put_user(result
, &_xfern
->result
);
2688 return result
< 0 ? result
: 0;
2690 case SNDRV_PCM_IOCTL_REWIND
:
2692 snd_pcm_uframes_t frames
;
2693 snd_pcm_uframes_t __user
*_frames
= arg
;
2694 snd_pcm_sframes_t result
;
2695 if (get_user(frames
, _frames
))
2697 if (put_user(0, _frames
))
2699 result
= snd_pcm_capture_rewind(substream
, frames
);
2700 __put_user(result
, _frames
);
2701 return result
< 0 ? result
: 0;
2703 case SNDRV_PCM_IOCTL_FORWARD
:
2705 snd_pcm_uframes_t frames
;
2706 snd_pcm_uframes_t __user
*_frames
= arg
;
2707 snd_pcm_sframes_t result
;
2708 if (get_user(frames
, _frames
))
2710 if (put_user(0, _frames
))
2712 result
= snd_pcm_capture_forward(substream
, frames
);
2713 __put_user(result
, _frames
);
2714 return result
< 0 ? result
: 0;
2717 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2720 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2723 struct snd_pcm_file
*pcm_file
;
2725 pcm_file
= file
->private_data
;
2727 if (((cmd
>> 8) & 0xff) != 'A')
2730 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2731 (void __user
*)arg
);
2734 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2737 struct snd_pcm_file
*pcm_file
;
2739 pcm_file
= file
->private_data
;
2741 if (((cmd
>> 8) & 0xff) != 'A')
2744 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2745 (void __user
*)arg
);
2748 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2749 unsigned int cmd
, void *arg
)
2754 fs
= snd_enter_user();
2755 switch (substream
->stream
) {
2756 case SNDRV_PCM_STREAM_PLAYBACK
:
2757 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2758 (void __user
*)arg
);
2760 case SNDRV_PCM_STREAM_CAPTURE
:
2761 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2762 (void __user
*)arg
);
2772 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2774 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2777 struct snd_pcm_file
*pcm_file
;
2778 struct snd_pcm_substream
*substream
;
2779 struct snd_pcm_runtime
*runtime
;
2780 snd_pcm_sframes_t result
;
2782 pcm_file
= file
->private_data
;
2783 substream
= pcm_file
->substream
;
2784 snd_assert(substream
!= NULL
, return -ENXIO
);
2785 runtime
= substream
->runtime
;
2786 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2788 if (!frame_aligned(runtime
, count
))
2790 count
= bytes_to_frames(runtime
, count
);
2791 result
= snd_pcm_lib_read(substream
, buf
, count
);
2793 result
= frames_to_bytes(runtime
, result
);
2797 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2798 size_t count
, loff_t
* offset
)
2800 struct snd_pcm_file
*pcm_file
;
2801 struct snd_pcm_substream
*substream
;
2802 struct snd_pcm_runtime
*runtime
;
2803 snd_pcm_sframes_t result
;
2805 pcm_file
= file
->private_data
;
2806 substream
= pcm_file
->substream
;
2807 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2808 runtime
= substream
->runtime
;
2809 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2813 if (!frame_aligned(runtime
, count
)) {
2817 count
= bytes_to_frames(runtime
, count
);
2818 result
= snd_pcm_lib_write(substream
, buf
, count
);
2820 result
= frames_to_bytes(runtime
, result
);
2825 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2826 unsigned long nr_segs
, loff_t pos
)
2829 struct snd_pcm_file
*pcm_file
;
2830 struct snd_pcm_substream
*substream
;
2831 struct snd_pcm_runtime
*runtime
;
2832 snd_pcm_sframes_t result
;
2835 snd_pcm_uframes_t frames
;
2837 pcm_file
= iocb
->ki_filp
->private_data
;
2838 substream
= pcm_file
->substream
;
2839 snd_assert(substream
!= NULL
, return -ENXIO
);
2840 runtime
= substream
->runtime
;
2841 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2843 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2845 if (!frame_aligned(runtime
, iov
->iov_len
))
2847 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2848 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2851 for (i
= 0; i
< nr_segs
; ++i
)
2852 bufs
[i
] = iov
[i
].iov_base
;
2853 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2855 result
= frames_to_bytes(runtime
, result
);
2860 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2861 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
, result
= -ENXIO
; goto end
);
2874 runtime
= substream
->runtime
;
2875 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2879 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2880 !frame_aligned(runtime
, iov
->iov_len
)) {
2884 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2885 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2888 for (i
= 0; i
< nr_segs
; ++i
)
2889 bufs
[i
] = iov
[i
].iov_base
;
2890 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2892 result
= frames_to_bytes(runtime
, result
);
2898 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2900 struct snd_pcm_file
*pcm_file
;
2901 struct snd_pcm_substream
*substream
;
2902 struct snd_pcm_runtime
*runtime
;
2904 snd_pcm_uframes_t avail
;
2906 pcm_file
= file
->private_data
;
2908 substream
= pcm_file
->substream
;
2909 snd_assert(substream
!= NULL
, return -ENXIO
);
2910 runtime
= substream
->runtime
;
2912 poll_wait(file
, &runtime
->sleep
, wait
);
2914 snd_pcm_stream_lock_irq(substream
);
2915 avail
= snd_pcm_playback_avail(runtime
);
2916 switch (runtime
->status
->state
) {
2917 case SNDRV_PCM_STATE_RUNNING
:
2918 case SNDRV_PCM_STATE_PREPARED
:
2919 case SNDRV_PCM_STATE_PAUSED
:
2920 if (avail
>= runtime
->control
->avail_min
) {
2921 mask
= POLLOUT
| POLLWRNORM
;
2925 case SNDRV_PCM_STATE_DRAINING
:
2929 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2932 snd_pcm_stream_unlock_irq(substream
);
2936 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2938 struct snd_pcm_file
*pcm_file
;
2939 struct snd_pcm_substream
*substream
;
2940 struct snd_pcm_runtime
*runtime
;
2942 snd_pcm_uframes_t avail
;
2944 pcm_file
= file
->private_data
;
2946 substream
= pcm_file
->substream
;
2947 snd_assert(substream
!= NULL
, return -ENXIO
);
2948 runtime
= substream
->runtime
;
2950 poll_wait(file
, &runtime
->sleep
, wait
);
2952 snd_pcm_stream_lock_irq(substream
);
2953 avail
= snd_pcm_capture_avail(runtime
);
2954 switch (runtime
->status
->state
) {
2955 case SNDRV_PCM_STATE_RUNNING
:
2956 case SNDRV_PCM_STATE_PREPARED
:
2957 case SNDRV_PCM_STATE_PAUSED
:
2958 if (avail
>= runtime
->control
->avail_min
) {
2959 mask
= POLLIN
| POLLRDNORM
;
2964 case SNDRV_PCM_STATE_DRAINING
:
2966 mask
= POLLIN
| POLLRDNORM
;
2971 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2974 snd_pcm_stream_unlock_irq(substream
);
2983 * Only on coherent architectures, we can mmap the status and the control records
2984 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2986 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2988 * mmap status record
2990 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
2991 struct vm_fault
*vmf
)
2993 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
2994 struct snd_pcm_runtime
*runtime
;
2996 if (substream
== NULL
)
2997 return VM_FAULT_SIGBUS
;
2998 runtime
= substream
->runtime
;
2999 vmf
->page
= virt_to_page(runtime
->status
);
3000 get_page(vmf
->page
);
3004 static struct vm_operations_struct snd_pcm_vm_ops_status
=
3006 .fault
= snd_pcm_mmap_status_fault
,
3009 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3010 struct vm_area_struct
*area
)
3012 struct snd_pcm_runtime
*runtime
;
3014 if (!(area
->vm_flags
& VM_READ
))
3016 runtime
= substream
->runtime
;
3017 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3018 size
= area
->vm_end
- area
->vm_start
;
3019 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3021 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3022 area
->vm_private_data
= substream
;
3023 area
->vm_flags
|= VM_RESERVED
;
3028 * mmap control record
3030 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3031 struct vm_fault
*vmf
)
3033 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3034 struct snd_pcm_runtime
*runtime
;
3036 if (substream
== NULL
)
3037 return VM_FAULT_SIGBUS
;
3038 runtime
= substream
->runtime
;
3039 vmf
->page
= virt_to_page(runtime
->control
);
3040 get_page(vmf
->page
);
3044 static struct vm_operations_struct snd_pcm_vm_ops_control
=
3046 .fault
= snd_pcm_mmap_control_fault
,
3049 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3050 struct vm_area_struct
*area
)
3052 struct snd_pcm_runtime
*runtime
;
3054 if (!(area
->vm_flags
& VM_READ
))
3056 runtime
= substream
->runtime
;
3057 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3058 size
= area
->vm_end
- area
->vm_start
;
3059 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3061 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3062 area
->vm_private_data
= substream
;
3063 area
->vm_flags
|= VM_RESERVED
;
3066 #else /* ! coherent mmap */
3068 * don't support mmap for status and control records.
3070 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3071 struct vm_area_struct
*area
)
3075 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3076 struct vm_area_struct
*area
)
3080 #endif /* coherent mmap */
3083 * fault callback for mmapping a RAM page
3085 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3086 struct vm_fault
*vmf
)
3088 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3089 struct snd_pcm_runtime
*runtime
;
3090 unsigned long offset
;
3095 if (substream
== NULL
)
3096 return VM_FAULT_SIGBUS
;
3097 runtime
= substream
->runtime
;
3098 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3099 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3100 if (offset
> dma_bytes
- PAGE_SIZE
)
3101 return VM_FAULT_SIGBUS
;
3102 if (substream
->ops
->page
) {
3103 page
= substream
->ops
->page(substream
, offset
);
3105 return VM_FAULT_SIGBUS
;
3107 vaddr
= runtime
->dma_area
+ offset
;
3108 page
= virt_to_page(vaddr
);
3115 static struct vm_operations_struct snd_pcm_vm_ops_data
=
3117 .open
= snd_pcm_mmap_data_open
,
3118 .close
= snd_pcm_mmap_data_close
,
3119 .fault
= snd_pcm_mmap_data_fault
,
3123 * mmap the DMA buffer on RAM
3125 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3126 struct vm_area_struct
*area
)
3128 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3129 area
->vm_private_data
= substream
;
3130 area
->vm_flags
|= VM_RESERVED
;
3131 atomic_inc(&substream
->mmap_count
);
3136 * mmap the DMA buffer on I/O memory area
3138 #if SNDRV_PCM_INFO_MMAP_IOMEM
3139 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio
=
3141 .open
= snd_pcm_mmap_data_open
,
3142 .close
= snd_pcm_mmap_data_close
,
3145 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3146 struct vm_area_struct
*area
)
3149 unsigned long offset
;
3151 #ifdef pgprot_noncached
3152 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3154 area
->vm_ops
= &snd_pcm_vm_ops_data_mmio
;
3155 area
->vm_private_data
= substream
;
3156 area
->vm_flags
|= VM_IO
;
3157 size
= area
->vm_end
- area
->vm_start
;
3158 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3159 if (io_remap_pfn_range(area
, area
->vm_start
,
3160 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3161 size
, area
->vm_page_prot
))
3163 atomic_inc(&substream
->mmap_count
);
3167 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3168 #endif /* SNDRV_PCM_INFO_MMAP */
3173 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3174 struct vm_area_struct
*area
)
3176 struct snd_pcm_runtime
*runtime
;
3178 unsigned long offset
;
3181 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3182 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3185 if (!(area
->vm_flags
& VM_READ
))
3188 runtime
= substream
->runtime
;
3189 snd_assert(runtime
!= NULL
, return -EAGAIN
);
3190 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3192 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3194 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3195 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3197 size
= area
->vm_end
- area
->vm_start
;
3198 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3199 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3200 if ((size_t)size
> dma_bytes
)
3202 if (offset
> dma_bytes
- size
)
3205 if (substream
->ops
->mmap
)
3206 return substream
->ops
->mmap(substream
, area
);
3208 return snd_pcm_default_mmap(substream
, area
);
3211 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3213 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3215 struct snd_pcm_file
* pcm_file
;
3216 struct snd_pcm_substream
*substream
;
3217 unsigned long offset
;
3219 pcm_file
= file
->private_data
;
3220 substream
= pcm_file
->substream
;
3221 snd_assert(substream
!= NULL
, return -ENXIO
);
3223 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3225 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3226 if (pcm_file
->no_compat_mmap
)
3228 return snd_pcm_mmap_status(substream
, file
, area
);
3229 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3230 if (pcm_file
->no_compat_mmap
)
3232 return snd_pcm_mmap_control(substream
, file
, area
);
3234 return snd_pcm_mmap_data(substream
, file
, area
);
3239 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3241 struct snd_pcm_file
* pcm_file
;
3242 struct snd_pcm_substream
*substream
;
3243 struct snd_pcm_runtime
*runtime
;
3246 pcm_file
= file
->private_data
;
3247 substream
= pcm_file
->substream
;
3248 snd_assert(substream
!= NULL
, return -ENXIO
);
3249 runtime
= substream
->runtime
;
3251 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3260 #ifdef CONFIG_COMPAT
3261 #include "pcm_compat.c"
3263 #define snd_pcm_ioctl_compat NULL
3267 * To be removed helpers to keep binary compatibility
3270 #ifdef CONFIG_SND_SUPPORT_OLD_API
3271 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3272 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3274 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3275 struct snd_pcm_hw_params_old
*oparams
)
3279 memset(params
, 0, sizeof(*params
));
3280 params
->flags
= oparams
->flags
;
3281 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3282 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3283 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3284 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3285 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3286 params
->info
= oparams
->info
;
3287 params
->msbits
= oparams
->msbits
;
3288 params
->rate_num
= oparams
->rate_num
;
3289 params
->rate_den
= oparams
->rate_den
;
3290 params
->fifo_size
= oparams
->fifo_size
;
3293 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3294 struct snd_pcm_hw_params
*params
)
3298 memset(oparams
, 0, sizeof(*oparams
));
3299 oparams
->flags
= params
->flags
;
3300 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3301 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3302 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3303 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3304 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3305 oparams
->info
= params
->info
;
3306 oparams
->msbits
= params
->msbits
;
3307 oparams
->rate_num
= params
->rate_num
;
3308 oparams
->rate_den
= params
->rate_den
;
3309 oparams
->fifo_size
= params
->fifo_size
;
3312 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3313 struct snd_pcm_hw_params_old __user
* _oparams
)
3315 struct snd_pcm_hw_params
*params
;
3316 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3319 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3324 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3330 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3334 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3335 err
= snd_pcm_hw_refine(substream
, params
);
3336 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3337 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3347 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3348 struct snd_pcm_hw_params_old __user
* _oparams
)
3350 struct snd_pcm_hw_params
*params
;
3351 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3354 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3359 oparams
= kmalloc(sizeof(*oparams
), GFP_KERNEL
);
3364 if (copy_from_user(oparams
, _oparams
, sizeof(*oparams
))) {
3368 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3369 err
= snd_pcm_hw_params(substream
, params
);
3370 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3371 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3380 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3386 const struct file_operations snd_pcm_f_ops
[2] = {
3388 .owner
= THIS_MODULE
,
3389 .write
= snd_pcm_write
,
3390 .aio_write
= snd_pcm_aio_write
,
3391 .open
= snd_pcm_playback_open
,
3392 .release
= snd_pcm_release
,
3393 .poll
= snd_pcm_playback_poll
,
3394 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3395 .compat_ioctl
= snd_pcm_ioctl_compat
,
3396 .mmap
= snd_pcm_mmap
,
3397 .fasync
= snd_pcm_fasync
,
3400 .owner
= THIS_MODULE
,
3401 .read
= snd_pcm_read
,
3402 .aio_read
= snd_pcm_aio_read
,
3403 .open
= snd_pcm_capture_open
,
3404 .release
= snd_pcm_release
,
3405 .poll
= snd_pcm_capture_poll
,
3406 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3407 .compat_ioctl
= snd_pcm_ioctl_compat
,
3408 .mmap
= snd_pcm_mmap
,
3409 .fasync
= snd_pcm_fasync
,