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
23 #include <linux/file.h>
24 #include <linux/slab.h>
25 #include <linux/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/math64.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
44 struct snd_pcm_hw_params_old
{
46 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
47 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
48 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
54 unsigned int rate_num
;
55 unsigned int rate_den
;
56 snd_pcm_uframes_t fifo_size
;
57 unsigned char reserved
[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
65 struct snd_pcm_hw_params_old __user
* _oparams
);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
67 struct snd_pcm_hw_params_old __user
* _oparams
);
69 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
75 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
76 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
78 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
80 static inline mm_segment_t
snd_enter_user(void)
82 mm_segment_t fs
= get_fs();
87 static inline void snd_leave_user(mm_segment_t fs
)
94 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
96 struct snd_pcm_runtime
*runtime
;
97 struct snd_pcm
*pcm
= substream
->pcm
;
98 struct snd_pcm_str
*pstr
= substream
->pstr
;
100 memset(info
, 0, sizeof(*info
));
101 info
->card
= pcm
->card
->number
;
102 info
->device
= pcm
->device
;
103 info
->stream
= substream
->stream
;
104 info
->subdevice
= substream
->number
;
105 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
106 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
107 info
->dev_class
= pcm
->dev_class
;
108 info
->dev_subclass
= pcm
->dev_subclass
;
109 info
->subdevices_count
= pstr
->substream_count
;
110 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
111 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
112 runtime
= substream
->runtime
;
113 /* AB: FIXME!!! This is definitely nonsense */
115 info
->sync
= runtime
->sync
;
116 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
121 int snd_pcm_info_user(struct snd_pcm_substream
*substream
,
122 struct snd_pcm_info __user
* _info
)
124 struct snd_pcm_info
*info
;
127 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
130 err
= snd_pcm_info(substream
, info
);
132 if (copy_to_user(_info
, info
, sizeof(*info
)))
142 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
143 char *snd_pcm_hw_param_names
[] = {
147 HW_PARAM(SAMPLE_BITS
),
148 HW_PARAM(FRAME_BITS
),
151 HW_PARAM(PERIOD_TIME
),
152 HW_PARAM(PERIOD_SIZE
),
153 HW_PARAM(PERIOD_BYTES
),
155 HW_PARAM(BUFFER_TIME
),
156 HW_PARAM(BUFFER_SIZE
),
157 HW_PARAM(BUFFER_BYTES
),
162 int snd_pcm_hw_refine(struct snd_pcm_substream
*substream
,
163 struct snd_pcm_hw_params
*params
)
166 struct snd_pcm_hardware
*hw
;
167 struct snd_interval
*i
= NULL
;
168 struct snd_mask
*m
= NULL
;
169 struct snd_pcm_hw_constraints
*constrs
= &substream
->runtime
->hw_constraints
;
170 unsigned int rstamps
[constrs
->rules_num
];
171 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
172 unsigned int stamp
= 2;
176 params
->fifo_size
= 0;
177 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
179 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
180 params
->rate_num
= 0;
181 params
->rate_den
= 0;
184 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
185 m
= hw_param_mask(params
, k
);
186 if (snd_mask_empty(m
))
188 if (!(params
->rmask
& (1 << k
)))
191 printk(KERN_DEBUG
"%s = ", snd_pcm_hw_param_names
[k
]);
192 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
194 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
196 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
199 params
->cmask
|= 1 << k
;
204 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
205 i
= hw_param_interval(params
, k
);
206 if (snd_interval_empty(i
))
208 if (!(params
->rmask
& (1 << k
)))
211 printk(KERN_DEBUG
"%s = ", snd_pcm_hw_param_names
[k
]);
216 i
->openmin
? '(' : '[', i
->min
,
217 i
->max
, i
->openmax
? ')' : ']');
220 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
225 printk("%c%u %u%c\n",
226 i
->openmin
? '(' : '[', i
->min
,
227 i
->max
, i
->openmax
? ')' : ']');
230 params
->cmask
|= 1 << k
;
235 for (k
= 0; k
< constrs
->rules_num
; k
++)
237 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
238 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
241 for (k
= 0; k
< constrs
->rules_num
; k
++) {
242 struct snd_pcm_hw_rule
*r
= &constrs
->rules
[k
];
245 if (r
->cond
&& !(r
->cond
& params
->flags
))
247 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
248 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
256 printk(KERN_DEBUG
"Rule %d [%p]: ", k
, r
->func
);
258 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
259 if (hw_is_mask(r
->var
)) {
260 m
= hw_param_mask(params
, r
->var
);
261 printk("%x", *m
->bits
);
263 i
= hw_param_interval(params
, r
->var
);
268 i
->openmin
? '(' : '[', i
->min
,
269 i
->max
, i
->openmax
? ')' : ']');
273 changed
= r
->func(params
, r
);
277 if (hw_is_mask(r
->var
))
278 printk("%x", *m
->bits
);
284 i
->openmin
? '(' : '[', i
->min
,
285 i
->max
, i
->openmax
? ')' : ']');
291 if (changed
&& r
->var
>= 0) {
292 params
->cmask
|= (1 << r
->var
);
293 vstamps
[r
->var
] = stamp
;
301 if (!params
->msbits
) {
302 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
303 if (snd_interval_single(i
))
304 params
->msbits
= snd_interval_value(i
);
307 if (!params
->rate_den
) {
308 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
309 if (snd_interval_single(i
)) {
310 params
->rate_num
= snd_interval_value(i
);
311 params
->rate_den
= 1;
315 hw
= &substream
->runtime
->hw
;
317 params
->info
= hw
->info
& ~SNDRV_PCM_INFO_FIFO_IN_FRAMES
;
318 if (!params
->fifo_size
) {
319 m
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
320 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
321 if (snd_mask_min(m
) == snd_mask_max(m
) &&
322 snd_interval_min(i
) == snd_interval_max(i
)) {
323 changed
= substream
->ops
->ioctl(substream
,
324 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
333 EXPORT_SYMBOL(snd_pcm_hw_refine
);
335 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
336 struct snd_pcm_hw_params __user
* _params
)
338 struct snd_pcm_hw_params
*params
;
341 params
= memdup_user(_params
, sizeof(*params
));
343 return PTR_ERR(params
);
345 err
= snd_pcm_hw_refine(substream
, params
);
346 if (copy_to_user(_params
, params
, sizeof(*params
))) {
355 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
360 return -1; /* invalid */
362 /* take 75% of period time as the deadline */
363 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
364 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
370 static int calc_boundary(struct snd_pcm_runtime
*runtime
)
374 boundary
= (u_int64_t
)runtime
->buffer_size
*
375 (u_int64_t
)runtime
->period_size
;
376 #if BITS_PER_LONG < 64
377 /* try to find lowest common multiple for buffer and period */
378 if (boundary
> LONG_MAX
- runtime
->buffer_size
) {
379 u_int32_t remainder
= -1;
380 u_int32_t divident
= runtime
->buffer_size
;
381 u_int32_t divisor
= runtime
->period_size
;
383 remainder
= divident
% divisor
;
389 boundary
= div_u64(boundary
, divisor
);
390 if (boundary
> LONG_MAX
- runtime
->buffer_size
)
396 runtime
->boundary
= boundary
;
397 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
398 runtime
->boundary
*= 2;
402 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
403 struct snd_pcm_hw_params
*params
)
405 struct snd_pcm_runtime
*runtime
;
408 snd_pcm_uframes_t frames
;
410 if (PCM_RUNTIME_CHECK(substream
))
412 runtime
= substream
->runtime
;
413 snd_pcm_stream_lock_irq(substream
);
414 switch (runtime
->status
->state
) {
415 case SNDRV_PCM_STATE_OPEN
:
416 case SNDRV_PCM_STATE_SETUP
:
417 case SNDRV_PCM_STATE_PREPARED
:
420 snd_pcm_stream_unlock_irq(substream
);
423 snd_pcm_stream_unlock_irq(substream
);
424 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
425 if (!substream
->oss
.oss
)
427 if (atomic_read(&substream
->mmap_count
))
431 err
= snd_pcm_hw_refine(substream
, params
);
435 err
= snd_pcm_hw_params_choose(substream
, params
);
439 if (substream
->ops
->hw_params
!= NULL
) {
440 err
= substream
->ops
->hw_params(substream
, params
);
445 runtime
->access
= params_access(params
);
446 runtime
->format
= params_format(params
);
447 runtime
->subformat
= params_subformat(params
);
448 runtime
->channels
= params_channels(params
);
449 runtime
->rate
= params_rate(params
);
450 runtime
->period_size
= params_period_size(params
);
451 runtime
->periods
= params_periods(params
);
452 runtime
->buffer_size
= params_buffer_size(params
);
453 runtime
->info
= params
->info
;
454 runtime
->rate_num
= params
->rate_num
;
455 runtime
->rate_den
= params
->rate_den
;
457 bits
= snd_pcm_format_physical_width(runtime
->format
);
458 runtime
->sample_bits
= bits
;
459 bits
*= runtime
->channels
;
460 runtime
->frame_bits
= bits
;
462 while (bits
% 8 != 0) {
466 runtime
->byte_align
= bits
/ 8;
467 runtime
->min_align
= frames
;
469 /* Default sw params */
470 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
471 runtime
->period_step
= 1;
472 runtime
->control
->avail_min
= runtime
->period_size
;
473 runtime
->start_threshold
= 1;
474 runtime
->stop_threshold
= runtime
->buffer_size
;
475 runtime
->silence_threshold
= 0;
476 runtime
->silence_size
= 0;
477 err
= calc_boundary(runtime
);
481 snd_pcm_timer_resolution_change(substream
);
482 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
484 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
485 substream
->latency_id
);
486 if ((usecs
= period_to_usecs(runtime
)) >= 0)
487 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY
,
488 substream
->latency_id
, usecs
);
491 /* hardware might be unuseable from this time,
492 so we force application to retry to set
493 the correct hardware parameter settings */
494 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
495 if (substream
->ops
->hw_free
!= NULL
)
496 substream
->ops
->hw_free(substream
);
500 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
501 struct snd_pcm_hw_params __user
* _params
)
503 struct snd_pcm_hw_params
*params
;
506 params
= memdup_user(_params
, sizeof(*params
));
508 return PTR_ERR(params
);
510 err
= snd_pcm_hw_params(substream
, params
);
511 if (copy_to_user(_params
, params
, sizeof(*params
))) {
520 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
522 struct snd_pcm_runtime
*runtime
;
525 if (PCM_RUNTIME_CHECK(substream
))
527 runtime
= substream
->runtime
;
528 snd_pcm_stream_lock_irq(substream
);
529 switch (runtime
->status
->state
) {
530 case SNDRV_PCM_STATE_SETUP
:
531 case SNDRV_PCM_STATE_PREPARED
:
534 snd_pcm_stream_unlock_irq(substream
);
537 snd_pcm_stream_unlock_irq(substream
);
538 if (atomic_read(&substream
->mmap_count
))
540 if (substream
->ops
->hw_free
)
541 result
= substream
->ops
->hw_free(substream
);
542 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
543 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
544 substream
->latency_id
);
548 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
549 struct snd_pcm_sw_params
*params
)
551 struct snd_pcm_runtime
*runtime
;
554 if (PCM_RUNTIME_CHECK(substream
))
556 runtime
= substream
->runtime
;
557 snd_pcm_stream_lock_irq(substream
);
558 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
559 snd_pcm_stream_unlock_irq(substream
);
562 snd_pcm_stream_unlock_irq(substream
);
564 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
566 if (params
->avail_min
== 0)
568 if (params
->silence_size
>= runtime
->boundary
) {
569 if (params
->silence_threshold
!= 0)
572 if (params
->silence_size
> params
->silence_threshold
)
574 if (params
->silence_threshold
> runtime
->buffer_size
)
578 snd_pcm_stream_lock_irq(substream
);
579 runtime
->tstamp_mode
= params
->tstamp_mode
;
580 runtime
->period_step
= params
->period_step
;
581 runtime
->control
->avail_min
= params
->avail_min
;
582 runtime
->start_threshold
= params
->start_threshold
;
583 runtime
->stop_threshold
= params
->stop_threshold
;
584 runtime
->silence_threshold
= params
->silence_threshold
;
585 runtime
->silence_size
= params
->silence_size
;
586 params
->boundary
= runtime
->boundary
;
587 if (snd_pcm_running(substream
)) {
588 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
589 runtime
->silence_size
> 0)
590 snd_pcm_playback_silence(substream
, ULONG_MAX
);
591 err
= snd_pcm_update_state(substream
, runtime
);
593 snd_pcm_stream_unlock_irq(substream
);
597 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
598 struct snd_pcm_sw_params __user
* _params
)
600 struct snd_pcm_sw_params params
;
602 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
604 err
= snd_pcm_sw_params(substream
, ¶ms
);
605 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
610 int snd_pcm_status(struct snd_pcm_substream
*substream
,
611 struct snd_pcm_status
*status
)
613 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
615 snd_pcm_stream_lock_irq(substream
);
616 status
->state
= runtime
->status
->state
;
617 status
->suspended_state
= runtime
->status
->suspended_state
;
618 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
620 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
621 if (snd_pcm_running(substream
)) {
622 snd_pcm_update_hw_ptr(substream
);
623 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
624 status
->tstamp
= runtime
->status
->tstamp
;
628 snd_pcm_gettime(runtime
, &status
->tstamp
);
630 status
->appl_ptr
= runtime
->control
->appl_ptr
;
631 status
->hw_ptr
= runtime
->status
->hw_ptr
;
632 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
633 status
->avail
= snd_pcm_playback_avail(runtime
);
634 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
635 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
636 status
->delay
= runtime
->buffer_size
- status
->avail
;
637 status
->delay
+= runtime
->delay
;
641 status
->avail
= snd_pcm_capture_avail(runtime
);
642 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
643 status
->delay
= status
->avail
+ runtime
->delay
;
647 status
->avail_max
= runtime
->avail_max
;
648 status
->overrange
= runtime
->overrange
;
649 runtime
->avail_max
= 0;
650 runtime
->overrange
= 0;
652 snd_pcm_stream_unlock_irq(substream
);
656 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
657 struct snd_pcm_status __user
* _status
)
659 struct snd_pcm_status status
;
662 memset(&status
, 0, sizeof(status
));
663 res
= snd_pcm_status(substream
, &status
);
666 if (copy_to_user(_status
, &status
, sizeof(status
)))
671 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
672 struct snd_pcm_channel_info
* info
)
674 struct snd_pcm_runtime
*runtime
;
675 unsigned int channel
;
677 channel
= info
->channel
;
678 runtime
= substream
->runtime
;
679 snd_pcm_stream_lock_irq(substream
);
680 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
681 snd_pcm_stream_unlock_irq(substream
);
684 snd_pcm_stream_unlock_irq(substream
);
685 if (channel
>= runtime
->channels
)
687 memset(info
, 0, sizeof(*info
));
688 info
->channel
= channel
;
689 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
692 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
693 struct snd_pcm_channel_info __user
* _info
)
695 struct snd_pcm_channel_info info
;
698 if (copy_from_user(&info
, _info
, sizeof(info
)))
700 res
= snd_pcm_channel_info(substream
, &info
);
703 if (copy_to_user(_info
, &info
, sizeof(info
)))
708 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
710 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
711 if (runtime
->trigger_master
== NULL
)
713 if (runtime
->trigger_master
== substream
) {
714 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
716 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
717 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
719 runtime
->trigger_master
= NULL
;
723 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
724 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
725 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
726 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
730 * this functions is core for handling of linked stream
731 * Note: the stream state might be changed also on failure
732 * Note2: call with calling stream lock + link lock
734 static int snd_pcm_action_group(struct action_ops
*ops
,
735 struct snd_pcm_substream
*substream
,
736 int state
, int do_lock
)
738 struct snd_pcm_substream
*s
= NULL
;
739 struct snd_pcm_substream
*s1
;
742 snd_pcm_group_for_each_entry(s
, substream
) {
743 if (do_lock
&& s
!= substream
)
744 spin_lock_nested(&s
->self_group
.lock
,
745 SINGLE_DEPTH_NESTING
);
746 res
= ops
->pre_action(s
, state
);
750 snd_pcm_group_for_each_entry(s
, substream
) {
751 res
= ops
->do_action(s
, state
);
753 if (ops
->undo_action
) {
754 snd_pcm_group_for_each_entry(s1
, substream
) {
755 if (s1
== s
) /* failed stream */
757 ops
->undo_action(s1
, state
);
760 s
= NULL
; /* unlock all */
764 snd_pcm_group_for_each_entry(s
, substream
) {
765 ops
->post_action(s
, state
);
770 snd_pcm_group_for_each_entry(s1
, substream
) {
772 spin_unlock(&s1
->self_group
.lock
);
773 if (s1
== s
) /* end */
781 * Note: call with stream lock
783 static int snd_pcm_action_single(struct action_ops
*ops
,
784 struct snd_pcm_substream
*substream
,
789 res
= ops
->pre_action(substream
, state
);
792 res
= ops
->do_action(substream
, state
);
794 ops
->post_action(substream
, state
);
795 else if (ops
->undo_action
)
796 ops
->undo_action(substream
, state
);
801 * Note: call with stream lock
803 static int snd_pcm_action(struct action_ops
*ops
,
804 struct snd_pcm_substream
*substream
,
809 if (snd_pcm_stream_linked(substream
)) {
810 if (!spin_trylock(&substream
->group
->lock
)) {
811 spin_unlock(&substream
->self_group
.lock
);
812 spin_lock(&substream
->group
->lock
);
813 spin_lock(&substream
->self_group
.lock
);
815 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
816 spin_unlock(&substream
->group
->lock
);
818 res
= snd_pcm_action_single(ops
, substream
, state
);
824 * Note: don't use any locks before
826 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
827 struct snd_pcm_substream
*substream
,
832 read_lock_irq(&snd_pcm_link_rwlock
);
833 if (snd_pcm_stream_linked(substream
)) {
834 spin_lock(&substream
->group
->lock
);
835 spin_lock(&substream
->self_group
.lock
);
836 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
837 spin_unlock(&substream
->self_group
.lock
);
838 spin_unlock(&substream
->group
->lock
);
840 spin_lock(&substream
->self_group
.lock
);
841 res
= snd_pcm_action_single(ops
, substream
, state
);
842 spin_unlock(&substream
->self_group
.lock
);
844 read_unlock_irq(&snd_pcm_link_rwlock
);
850 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
851 struct snd_pcm_substream
*substream
,
856 down_read(&snd_pcm_link_rwsem
);
857 if (snd_pcm_stream_linked(substream
))
858 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
860 res
= snd_pcm_action_single(ops
, substream
, state
);
861 up_read(&snd_pcm_link_rwsem
);
868 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
870 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
871 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
873 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
874 !snd_pcm_playback_data(substream
))
876 runtime
->trigger_master
= substream
;
880 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
882 if (substream
->runtime
->trigger_master
!= substream
)
884 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
887 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
889 if (substream
->runtime
->trigger_master
== substream
)
890 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
893 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
895 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
896 snd_pcm_trigger_tstamp(substream
);
897 runtime
->hw_ptr_jiffies
= jiffies
;
898 runtime
->status
->state
= state
;
899 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
900 runtime
->silence_size
> 0)
901 snd_pcm_playback_silence(substream
, ULONG_MAX
);
902 if (substream
->timer
)
903 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
904 &runtime
->trigger_tstamp
);
907 static struct action_ops snd_pcm_action_start
= {
908 .pre_action
= snd_pcm_pre_start
,
909 .do_action
= snd_pcm_do_start
,
910 .undo_action
= snd_pcm_undo_start
,
911 .post_action
= snd_pcm_post_start
915 * snd_pcm_start - start all linked streams
916 * @substream: the PCM substream instance
918 int snd_pcm_start(struct snd_pcm_substream
*substream
)
920 return snd_pcm_action(&snd_pcm_action_start
, substream
,
921 SNDRV_PCM_STATE_RUNNING
);
927 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
929 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
930 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
932 runtime
->trigger_master
= substream
;
936 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
938 if (substream
->runtime
->trigger_master
== substream
&&
939 snd_pcm_running(substream
))
940 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
941 return 0; /* unconditonally stop all substreams */
944 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
946 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
947 if (runtime
->status
->state
!= state
) {
948 snd_pcm_trigger_tstamp(substream
);
949 if (substream
->timer
)
950 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
951 &runtime
->trigger_tstamp
);
952 runtime
->status
->state
= state
;
954 wake_up(&runtime
->sleep
);
955 wake_up(&runtime
->tsleep
);
958 static struct action_ops snd_pcm_action_stop
= {
959 .pre_action
= snd_pcm_pre_stop
,
960 .do_action
= snd_pcm_do_stop
,
961 .post_action
= snd_pcm_post_stop
965 * snd_pcm_stop - try to stop all running streams in the substream group
966 * @substream: the PCM substream instance
967 * @state: PCM state after stopping the stream
969 * The state of each stream is then changed to the given state unconditionally.
971 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
973 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
976 EXPORT_SYMBOL(snd_pcm_stop
);
979 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
980 * @substream: the PCM substream
982 * After stopping, the state is changed to SETUP.
983 * Unlike snd_pcm_stop(), this affects only the given stream.
985 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
987 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
988 SNDRV_PCM_STATE_SETUP
);
994 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
996 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
997 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
1000 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
1002 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
1004 runtime
->trigger_master
= substream
;
1008 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
1010 if (substream
->runtime
->trigger_master
!= substream
)
1012 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1013 * a delta betwen the current jiffies, this gives a large enough
1014 * delta, effectively to skip the check once.
1016 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
1017 return substream
->ops
->trigger(substream
,
1018 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1019 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
1022 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
1024 if (substream
->runtime
->trigger_master
== substream
)
1025 substream
->ops
->trigger(substream
,
1026 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1027 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
1030 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
1032 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1033 snd_pcm_trigger_tstamp(substream
);
1035 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1036 if (substream
->timer
)
1037 snd_timer_notify(substream
->timer
,
1038 SNDRV_TIMER_EVENT_MPAUSE
,
1039 &runtime
->trigger_tstamp
);
1040 wake_up(&runtime
->sleep
);
1041 wake_up(&runtime
->tsleep
);
1043 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1044 if (substream
->timer
)
1045 snd_timer_notify(substream
->timer
,
1046 SNDRV_TIMER_EVENT_MCONTINUE
,
1047 &runtime
->trigger_tstamp
);
1051 static struct action_ops snd_pcm_action_pause
= {
1052 .pre_action
= snd_pcm_pre_pause
,
1053 .do_action
= snd_pcm_do_pause
,
1054 .undo_action
= snd_pcm_undo_pause
,
1055 .post_action
= snd_pcm_post_pause
1059 * Push/release the pause for all linked streams.
1061 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1063 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1069 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1071 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1072 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1074 runtime
->trigger_master
= substream
;
1078 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1080 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1081 if (runtime
->trigger_master
!= substream
)
1083 if (! snd_pcm_running(substream
))
1085 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1086 return 0; /* suspend unconditionally */
1089 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1091 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1092 snd_pcm_trigger_tstamp(substream
);
1093 if (substream
->timer
)
1094 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1095 &runtime
->trigger_tstamp
);
1096 runtime
->status
->suspended_state
= runtime
->status
->state
;
1097 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1098 wake_up(&runtime
->sleep
);
1099 wake_up(&runtime
->tsleep
);
1102 static struct action_ops snd_pcm_action_suspend
= {
1103 .pre_action
= snd_pcm_pre_suspend
,
1104 .do_action
= snd_pcm_do_suspend
,
1105 .post_action
= snd_pcm_post_suspend
1109 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1110 * @substream: the PCM substream
1112 * After this call, all streams are changed to SUSPENDED state.
1114 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1117 unsigned long flags
;
1122 snd_pcm_stream_lock_irqsave(substream
, flags
);
1123 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1124 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1128 EXPORT_SYMBOL(snd_pcm_suspend
);
1131 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1132 * @pcm: the PCM instance
1134 * After this call, all streams are changed to SUSPENDED state.
1136 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1138 struct snd_pcm_substream
*substream
;
1139 int stream
, err
= 0;
1144 for (stream
= 0; stream
< 2; stream
++) {
1145 for (substream
= pcm
->streams
[stream
].substream
;
1146 substream
; substream
= substream
->next
) {
1147 /* FIXME: the open/close code should lock this as well */
1148 if (substream
->runtime
== NULL
)
1150 err
= snd_pcm_suspend(substream
);
1151 if (err
< 0 && err
!= -EBUSY
)
1158 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1162 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1164 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1165 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1167 runtime
->trigger_master
= substream
;
1171 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1173 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1174 if (runtime
->trigger_master
!= substream
)
1176 /* DMA not running previously? */
1177 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1178 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1179 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1181 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1184 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1186 if (substream
->runtime
->trigger_master
== substream
&&
1187 snd_pcm_running(substream
))
1188 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1191 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1193 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1194 snd_pcm_trigger_tstamp(substream
);
1195 if (substream
->timer
)
1196 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1197 &runtime
->trigger_tstamp
);
1198 runtime
->status
->state
= runtime
->status
->suspended_state
;
1201 static struct action_ops snd_pcm_action_resume
= {
1202 .pre_action
= snd_pcm_pre_resume
,
1203 .do_action
= snd_pcm_do_resume
,
1204 .undo_action
= snd_pcm_undo_resume
,
1205 .post_action
= snd_pcm_post_resume
1208 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1210 struct snd_card
*card
= substream
->pcm
->card
;
1213 snd_power_lock(card
);
1214 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1215 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1216 snd_power_unlock(card
);
1222 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1227 #endif /* CONFIG_PM */
1232 * Change the RUNNING stream(s) to XRUN state.
1234 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1236 struct snd_card
*card
= substream
->pcm
->card
;
1237 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1240 snd_power_lock(card
);
1241 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1242 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1247 snd_pcm_stream_lock_irq(substream
);
1248 switch (runtime
->status
->state
) {
1249 case SNDRV_PCM_STATE_XRUN
:
1250 result
= 0; /* already there */
1252 case SNDRV_PCM_STATE_RUNNING
:
1253 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1258 snd_pcm_stream_unlock_irq(substream
);
1260 snd_power_unlock(card
);
1267 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1269 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1270 switch (runtime
->status
->state
) {
1271 case SNDRV_PCM_STATE_RUNNING
:
1272 case SNDRV_PCM_STATE_PREPARED
:
1273 case SNDRV_PCM_STATE_PAUSED
:
1274 case SNDRV_PCM_STATE_SUSPENDED
:
1281 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1283 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1284 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1287 runtime
->hw_ptr_base
= 0;
1288 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1289 runtime
->status
->hw_ptr
% runtime
->period_size
;
1290 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1291 runtime
->silence_filled
= 0;
1295 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1297 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1298 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1299 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1300 runtime
->silence_size
> 0)
1301 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1304 static struct action_ops snd_pcm_action_reset
= {
1305 .pre_action
= snd_pcm_pre_reset
,
1306 .do_action
= snd_pcm_do_reset
,
1307 .post_action
= snd_pcm_post_reset
1310 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1312 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1318 /* we use the second argument for updating f_flags */
1319 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1322 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1323 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1324 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1326 if (snd_pcm_running(substream
))
1328 substream
->f_flags
= f_flags
;
1332 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1335 err
= substream
->ops
->prepare(substream
);
1338 return snd_pcm_do_reset(substream
, 0);
1341 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1343 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1344 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1345 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1348 static struct action_ops snd_pcm_action_prepare
= {
1349 .pre_action
= snd_pcm_pre_prepare
,
1350 .do_action
= snd_pcm_do_prepare
,
1351 .post_action
= snd_pcm_post_prepare
1355 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1356 * @substream: the PCM substream instance
1357 * @file: file to refer f_flags
1359 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1363 struct snd_card
*card
= substream
->pcm
->card
;
1367 f_flags
= file
->f_flags
;
1369 f_flags
= substream
->f_flags
;
1371 snd_power_lock(card
);
1372 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1373 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1374 substream
, f_flags
);
1375 snd_power_unlock(card
);
1383 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
1385 substream
->runtime
->trigger_master
= substream
;
1389 static int snd_pcm_do_drain_init(struct snd_pcm_substream
*substream
, int state
)
1391 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1392 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
1393 switch (runtime
->status
->state
) {
1394 case SNDRV_PCM_STATE_PREPARED
:
1395 /* start playback stream if possible */
1396 if (! snd_pcm_playback_empty(substream
)) {
1397 snd_pcm_do_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1398 snd_pcm_post_start(substream
, SNDRV_PCM_STATE_DRAINING
);
1401 case SNDRV_PCM_STATE_RUNNING
:
1402 runtime
->status
->state
= SNDRV_PCM_STATE_DRAINING
;
1408 /* stop running stream */
1409 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1410 int new_state
= snd_pcm_capture_avail(runtime
) > 0 ?
1411 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
;
1412 snd_pcm_do_stop(substream
, new_state
);
1413 snd_pcm_post_stop(substream
, new_state
);
1419 static void snd_pcm_post_drain_init(struct snd_pcm_substream
*substream
, int state
)
1423 static struct action_ops snd_pcm_action_drain_init
= {
1424 .pre_action
= snd_pcm_pre_drain_init
,
1425 .do_action
= snd_pcm_do_drain_init
,
1426 .post_action
= snd_pcm_post_drain_init
1429 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1432 * Drain the stream(s).
1433 * When the substream is linked, sync until the draining of all playback streams
1435 * After this call, all streams are supposed to be either SETUP or DRAINING
1436 * (capture only) state.
1438 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1441 struct snd_card
*card
;
1442 struct snd_pcm_runtime
*runtime
;
1443 struct snd_pcm_substream
*s
;
1448 card
= substream
->pcm
->card
;
1449 runtime
= substream
->runtime
;
1451 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1454 snd_power_lock(card
);
1455 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1456 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1458 snd_power_unlock(card
);
1464 if (file
->f_flags
& O_NONBLOCK
)
1466 } else if (substream
->f_flags
& O_NONBLOCK
)
1469 down_read(&snd_pcm_link_rwsem
);
1470 snd_pcm_stream_lock_irq(substream
);
1472 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1473 snd_pcm_pause(substream
, 0);
1475 /* pre-start/stop - all running streams are changed to DRAINING state */
1476 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1479 /* in non-blocking, we don't wait in ioctl but let caller poll */
1487 struct snd_pcm_runtime
*to_check
;
1488 if (signal_pending(current
)) {
1489 result
= -ERESTARTSYS
;
1492 /* find a substream to drain */
1494 snd_pcm_group_for_each_entry(s
, substream
) {
1495 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1497 runtime
= s
->runtime
;
1498 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1504 break; /* all drained */
1505 init_waitqueue_entry(&wait
, current
);
1506 add_wait_queue(&to_check
->sleep
, &wait
);
1507 set_current_state(TASK_INTERRUPTIBLE
);
1508 snd_pcm_stream_unlock_irq(substream
);
1509 up_read(&snd_pcm_link_rwsem
);
1510 snd_power_unlock(card
);
1511 tout
= schedule_timeout(10 * HZ
);
1512 snd_power_lock(card
);
1513 down_read(&snd_pcm_link_rwsem
);
1514 snd_pcm_stream_lock_irq(substream
);
1515 remove_wait_queue(&to_check
->sleep
, &wait
);
1517 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1520 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1521 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1529 snd_pcm_stream_unlock_irq(substream
);
1530 up_read(&snd_pcm_link_rwsem
);
1531 snd_power_unlock(card
);
1539 * Immediately put all linked substreams into SETUP state.
1541 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1543 struct snd_pcm_runtime
*runtime
;
1544 struct snd_card
*card
;
1547 if (PCM_RUNTIME_CHECK(substream
))
1549 runtime
= substream
->runtime
;
1550 card
= substream
->pcm
->card
;
1552 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1553 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1554 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1557 snd_pcm_stream_lock_irq(substream
);
1559 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1560 snd_pcm_pause(substream
, 0);
1562 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1563 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1564 snd_pcm_stream_unlock_irq(substream
);
1570 /* WARNING: Don't forget to fput back the file */
1571 static struct file
*snd_pcm_file_fd(int fd
)
1574 struct inode
*inode
;
1580 inode
= file
->f_path
.dentry
->d_inode
;
1581 if (!S_ISCHR(inode
->i_mode
) ||
1582 imajor(inode
) != snd_major
) {
1586 minor
= iminor(inode
);
1587 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1588 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1598 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1602 struct snd_pcm_file
*pcm_file
;
1603 struct snd_pcm_substream
*substream1
;
1605 file
= snd_pcm_file_fd(fd
);
1608 pcm_file
= file
->private_data
;
1609 substream1
= pcm_file
->substream
;
1610 down_write(&snd_pcm_link_rwsem
);
1611 write_lock_irq(&snd_pcm_link_rwlock
);
1612 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1613 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1617 if (snd_pcm_stream_linked(substream1
)) {
1621 if (!snd_pcm_stream_linked(substream
)) {
1622 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1623 if (substream
->group
== NULL
) {
1627 spin_lock_init(&substream
->group
->lock
);
1628 INIT_LIST_HEAD(&substream
->group
->substreams
);
1629 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1630 substream
->group
->count
= 1;
1632 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1633 substream
->group
->count
++;
1634 substream1
->group
= substream
->group
;
1636 write_unlock_irq(&snd_pcm_link_rwlock
);
1637 up_write(&snd_pcm_link_rwsem
);
1642 static void relink_to_local(struct snd_pcm_substream
*substream
)
1644 substream
->group
= &substream
->self_group
;
1645 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1646 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1649 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1651 struct snd_pcm_substream
*s
;
1654 down_write(&snd_pcm_link_rwsem
);
1655 write_lock_irq(&snd_pcm_link_rwlock
);
1656 if (!snd_pcm_stream_linked(substream
)) {
1660 list_del(&substream
->link_list
);
1661 substream
->group
->count
--;
1662 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1663 snd_pcm_group_for_each_entry(s
, substream
) {
1667 kfree(substream
->group
);
1669 relink_to_local(substream
);
1671 write_unlock_irq(&snd_pcm_link_rwlock
);
1672 up_write(&snd_pcm_link_rwsem
);
1679 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1680 struct snd_pcm_hw_rule
*rule
)
1682 struct snd_interval t
;
1683 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1684 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1685 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1688 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1689 struct snd_pcm_hw_rule
*rule
)
1691 struct snd_interval t
;
1692 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1693 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1694 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1697 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1698 struct snd_pcm_hw_rule
*rule
)
1700 struct snd_interval t
;
1701 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1702 hw_param_interval_c(params
, rule
->deps
[1]),
1703 (unsigned long) rule
->private, &t
);
1704 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1707 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1708 struct snd_pcm_hw_rule
*rule
)
1710 struct snd_interval t
;
1711 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1712 (unsigned long) rule
->private,
1713 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1714 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1717 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1718 struct snd_pcm_hw_rule
*rule
)
1721 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1723 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1725 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1727 if (! snd_mask_test(mask
, k
))
1729 bits
= snd_pcm_format_physical_width(k
);
1731 continue; /* ignore invalid formats */
1732 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1733 snd_mask_reset(&m
, k
);
1735 return snd_mask_refine(mask
, &m
);
1738 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1739 struct snd_pcm_hw_rule
*rule
)
1741 struct snd_interval t
;
1747 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1749 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1751 bits
= snd_pcm_format_physical_width(k
);
1753 continue; /* ignore invalid formats */
1754 if (t
.min
> (unsigned)bits
)
1756 if (t
.max
< (unsigned)bits
)
1760 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1763 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1764 #error "Change this table"
1767 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1768 48000, 64000, 88200, 96000, 176400, 192000 };
1770 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1771 .count
= ARRAY_SIZE(rates
),
1775 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1776 struct snd_pcm_hw_rule
*rule
)
1778 struct snd_pcm_hardware
*hw
= rule
->private;
1779 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1780 snd_pcm_known_rates
.count
,
1781 snd_pcm_known_rates
.list
, hw
->rates
);
1784 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1785 struct snd_pcm_hw_rule
*rule
)
1787 struct snd_interval t
;
1788 struct snd_pcm_substream
*substream
= rule
->private;
1790 t
.max
= substream
->buffer_bytes_max
;
1794 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1797 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1799 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1800 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1803 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1804 snd_mask_any(constrs_mask(constrs
, k
));
1807 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1808 snd_interval_any(constrs_interval(constrs
, k
));
1811 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1812 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1813 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1814 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1815 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1817 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1818 snd_pcm_hw_rule_format
, NULL
,
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_sample_bits
, NULL
,
1824 SNDRV_PCM_HW_PARAM_FORMAT
,
1825 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1828 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1829 snd_pcm_hw_rule_div
, NULL
,
1830 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1833 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1834 snd_pcm_hw_rule_mul
, NULL
,
1835 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1838 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1839 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1840 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1843 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1844 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1845 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1848 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1849 snd_pcm_hw_rule_div
, NULL
,
1850 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1853 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1854 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1855 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1858 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1859 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1863 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1864 snd_pcm_hw_rule_div
, NULL
,
1865 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1868 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1869 snd_pcm_hw_rule_div
, NULL
,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1873 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1874 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1878 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1879 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1883 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1884 snd_pcm_hw_rule_mul
, NULL
,
1885 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1888 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1889 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1893 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1894 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1895 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1898 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1899 snd_pcm_hw_rule_muldivk
, (void*) 8,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1903 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1904 snd_pcm_hw_rule_muldivk
, (void*) 8,
1905 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1908 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1909 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1910 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1913 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1914 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1915 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1921 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1923 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1924 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1926 unsigned int mask
= 0;
1928 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1929 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1930 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1931 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1932 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1933 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1934 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1935 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1936 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1937 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1938 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1940 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1944 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1948 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1952 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1953 hw
->channels_min
, hw
->channels_max
);
1957 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1958 hw
->rate_min
, hw
->rate_max
);
1962 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1963 hw
->period_bytes_min
, hw
->period_bytes_max
);
1967 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1968 hw
->periods_min
, hw
->periods_max
);
1972 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1973 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1977 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1978 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1979 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1984 if (runtime
->dma_bytes
) {
1985 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1990 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1991 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1992 snd_pcm_hw_rule_rate
, hw
,
1993 SNDRV_PCM_HW_PARAM_RATE
, -1);
1998 /* FIXME: this belong to lowlevel */
1999 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
2004 static void pcm_release_private(struct snd_pcm_substream
*substream
)
2006 snd_pcm_unlink(substream
);
2009 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
2011 substream
->ref_count
--;
2012 if (substream
->ref_count
> 0)
2015 snd_pcm_drop(substream
);
2016 if (substream
->hw_opened
) {
2017 if (substream
->ops
->hw_free
!= NULL
)
2018 substream
->ops
->hw_free(substream
);
2019 substream
->ops
->close(substream
);
2020 substream
->hw_opened
= 0;
2022 if (substream
->pcm_release
) {
2023 substream
->pcm_release(substream
);
2024 substream
->pcm_release
= NULL
;
2026 snd_pcm_detach_substream(substream
);
2029 EXPORT_SYMBOL(snd_pcm_release_substream
);
2031 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
2033 struct snd_pcm_substream
**rsubstream
)
2035 struct snd_pcm_substream
*substream
;
2038 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2041 if (substream
->ref_count
> 1) {
2042 *rsubstream
= substream
;
2046 err
= snd_pcm_hw_constraints_init(substream
);
2048 snd_printd("snd_pcm_hw_constraints_init failed\n");
2052 if ((err
= substream
->ops
->open(substream
)) < 0)
2055 substream
->hw_opened
= 1;
2057 err
= snd_pcm_hw_constraints_complete(substream
);
2059 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2063 *rsubstream
= substream
;
2067 snd_pcm_release_substream(substream
);
2071 EXPORT_SYMBOL(snd_pcm_open_substream
);
2073 static int snd_pcm_open_file(struct file
*file
,
2074 struct snd_pcm
*pcm
,
2076 struct snd_pcm_file
**rpcm_file
)
2078 struct snd_pcm_file
*pcm_file
;
2079 struct snd_pcm_substream
*substream
;
2080 struct snd_pcm_str
*str
;
2086 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2090 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2091 if (pcm_file
== NULL
) {
2092 snd_pcm_release_substream(substream
);
2095 pcm_file
->substream
= substream
;
2096 if (substream
->ref_count
== 1) {
2097 str
= substream
->pstr
;
2098 substream
->file
= pcm_file
;
2099 substream
->pcm_release
= pcm_release_private
;
2101 file
->private_data
= pcm_file
;
2103 *rpcm_file
= pcm_file
;
2107 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2109 struct snd_pcm
*pcm
;
2111 pcm
= snd_lookup_minor_data(iminor(inode
),
2112 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2113 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2116 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2118 struct snd_pcm
*pcm
;
2120 pcm
= snd_lookup_minor_data(iminor(inode
),
2121 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2122 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2125 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2128 struct snd_pcm_file
*pcm_file
;
2135 err
= snd_card_file_add(pcm
->card
, file
);
2138 if (!try_module_get(pcm
->card
->module
)) {
2142 init_waitqueue_entry(&wait
, current
);
2143 add_wait_queue(&pcm
->open_wait
, &wait
);
2144 mutex_lock(&pcm
->open_mutex
);
2146 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2149 if (err
== -EAGAIN
) {
2150 if (file
->f_flags
& O_NONBLOCK
) {
2156 set_current_state(TASK_INTERRUPTIBLE
);
2157 mutex_unlock(&pcm
->open_mutex
);
2159 mutex_lock(&pcm
->open_mutex
);
2160 if (signal_pending(current
)) {
2165 remove_wait_queue(&pcm
->open_wait
, &wait
);
2166 mutex_unlock(&pcm
->open_mutex
);
2172 module_put(pcm
->card
->module
);
2174 snd_card_file_remove(pcm
->card
, file
);
2179 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2181 struct snd_pcm
*pcm
;
2182 struct snd_pcm_substream
*substream
;
2183 struct snd_pcm_file
*pcm_file
;
2185 pcm_file
= file
->private_data
;
2186 substream
= pcm_file
->substream
;
2187 if (snd_BUG_ON(!substream
))
2189 pcm
= substream
->pcm
;
2190 mutex_lock(&pcm
->open_mutex
);
2191 snd_pcm_release_substream(substream
);
2193 mutex_unlock(&pcm
->open_mutex
);
2194 wake_up(&pcm
->open_wait
);
2195 module_put(pcm
->card
->module
);
2196 snd_card_file_remove(pcm
->card
, file
);
2200 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2201 snd_pcm_uframes_t frames
)
2203 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2204 snd_pcm_sframes_t appl_ptr
;
2205 snd_pcm_sframes_t ret
;
2206 snd_pcm_sframes_t hw_avail
;
2211 snd_pcm_stream_lock_irq(substream
);
2212 switch (runtime
->status
->state
) {
2213 case SNDRV_PCM_STATE_PREPARED
:
2215 case SNDRV_PCM_STATE_DRAINING
:
2216 case SNDRV_PCM_STATE_RUNNING
:
2217 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2220 case SNDRV_PCM_STATE_XRUN
:
2223 case SNDRV_PCM_STATE_SUSPENDED
:
2231 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2232 if (hw_avail
<= 0) {
2236 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2238 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2240 appl_ptr
+= runtime
->boundary
;
2241 runtime
->control
->appl_ptr
= appl_ptr
;
2244 snd_pcm_stream_unlock_irq(substream
);
2248 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2249 snd_pcm_uframes_t frames
)
2251 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2252 snd_pcm_sframes_t appl_ptr
;
2253 snd_pcm_sframes_t ret
;
2254 snd_pcm_sframes_t hw_avail
;
2259 snd_pcm_stream_lock_irq(substream
);
2260 switch (runtime
->status
->state
) {
2261 case SNDRV_PCM_STATE_PREPARED
:
2262 case SNDRV_PCM_STATE_DRAINING
:
2264 case SNDRV_PCM_STATE_RUNNING
:
2265 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2268 case SNDRV_PCM_STATE_XRUN
:
2271 case SNDRV_PCM_STATE_SUSPENDED
:
2279 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2280 if (hw_avail
<= 0) {
2284 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2286 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2288 appl_ptr
+= runtime
->boundary
;
2289 runtime
->control
->appl_ptr
= appl_ptr
;
2292 snd_pcm_stream_unlock_irq(substream
);
2296 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2297 snd_pcm_uframes_t frames
)
2299 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2300 snd_pcm_sframes_t appl_ptr
;
2301 snd_pcm_sframes_t ret
;
2302 snd_pcm_sframes_t avail
;
2307 snd_pcm_stream_lock_irq(substream
);
2308 switch (runtime
->status
->state
) {
2309 case SNDRV_PCM_STATE_PREPARED
:
2310 case SNDRV_PCM_STATE_PAUSED
:
2312 case SNDRV_PCM_STATE_DRAINING
:
2313 case SNDRV_PCM_STATE_RUNNING
:
2314 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2317 case SNDRV_PCM_STATE_XRUN
:
2320 case SNDRV_PCM_STATE_SUSPENDED
:
2328 avail
= snd_pcm_playback_avail(runtime
);
2333 if (frames
> (snd_pcm_uframes_t
)avail
)
2335 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2336 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2337 appl_ptr
-= runtime
->boundary
;
2338 runtime
->control
->appl_ptr
= appl_ptr
;
2341 snd_pcm_stream_unlock_irq(substream
);
2345 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2346 snd_pcm_uframes_t frames
)
2348 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2349 snd_pcm_sframes_t appl_ptr
;
2350 snd_pcm_sframes_t ret
;
2351 snd_pcm_sframes_t avail
;
2356 snd_pcm_stream_lock_irq(substream
);
2357 switch (runtime
->status
->state
) {
2358 case SNDRV_PCM_STATE_PREPARED
:
2359 case SNDRV_PCM_STATE_DRAINING
:
2360 case SNDRV_PCM_STATE_PAUSED
:
2362 case SNDRV_PCM_STATE_RUNNING
:
2363 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2366 case SNDRV_PCM_STATE_XRUN
:
2369 case SNDRV_PCM_STATE_SUSPENDED
:
2377 avail
= snd_pcm_capture_avail(runtime
);
2382 if (frames
> (snd_pcm_uframes_t
)avail
)
2384 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2385 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2386 appl_ptr
-= runtime
->boundary
;
2387 runtime
->control
->appl_ptr
= appl_ptr
;
2390 snd_pcm_stream_unlock_irq(substream
);
2394 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2396 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2399 snd_pcm_stream_lock_irq(substream
);
2400 switch (runtime
->status
->state
) {
2401 case SNDRV_PCM_STATE_DRAINING
:
2402 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2404 case SNDRV_PCM_STATE_RUNNING
:
2405 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2408 case SNDRV_PCM_STATE_PREPARED
:
2409 case SNDRV_PCM_STATE_SUSPENDED
:
2412 case SNDRV_PCM_STATE_XRUN
:
2420 snd_pcm_stream_unlock_irq(substream
);
2424 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2425 snd_pcm_sframes_t __user
*res
)
2427 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2429 snd_pcm_sframes_t n
= 0;
2431 snd_pcm_stream_lock_irq(substream
);
2432 switch (runtime
->status
->state
) {
2433 case SNDRV_PCM_STATE_DRAINING
:
2434 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2436 case SNDRV_PCM_STATE_RUNNING
:
2437 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2440 case SNDRV_PCM_STATE_PREPARED
:
2441 case SNDRV_PCM_STATE_SUSPENDED
:
2443 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2444 n
= snd_pcm_playback_hw_avail(runtime
);
2446 n
= snd_pcm_capture_avail(runtime
);
2447 n
+= runtime
->delay
;
2449 case SNDRV_PCM_STATE_XRUN
:
2457 snd_pcm_stream_unlock_irq(substream
);
2459 if (put_user(n
, res
))
2464 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2465 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2467 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2468 struct snd_pcm_sync_ptr sync_ptr
;
2469 volatile struct snd_pcm_mmap_status
*status
;
2470 volatile struct snd_pcm_mmap_control
*control
;
2473 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2474 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2476 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2478 status
= runtime
->status
;
2479 control
= runtime
->control
;
2480 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2481 err
= snd_pcm_hwsync(substream
);
2485 snd_pcm_stream_lock_irq(substream
);
2486 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2487 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2489 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2490 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2491 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2493 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2494 sync_ptr
.s
.status
.state
= status
->state
;
2495 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2496 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2497 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2498 snd_pcm_stream_unlock_irq(substream
);
2499 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2504 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2506 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2509 if (get_user(arg
, _arg
))
2511 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2513 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY
;
2514 if (arg
== SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
)
2515 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
;
2519 static int snd_pcm_common_ioctl1(struct file
*file
,
2520 struct snd_pcm_substream
*substream
,
2521 unsigned int cmd
, void __user
*arg
)
2524 case SNDRV_PCM_IOCTL_PVERSION
:
2525 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2526 case SNDRV_PCM_IOCTL_INFO
:
2527 return snd_pcm_info_user(substream
, arg
);
2528 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2530 case SNDRV_PCM_IOCTL_TTSTAMP
:
2531 return snd_pcm_tstamp(substream
, arg
);
2532 case SNDRV_PCM_IOCTL_HW_REFINE
:
2533 return snd_pcm_hw_refine_user(substream
, arg
);
2534 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2535 return snd_pcm_hw_params_user(substream
, arg
);
2536 case SNDRV_PCM_IOCTL_HW_FREE
:
2537 return snd_pcm_hw_free(substream
);
2538 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2539 return snd_pcm_sw_params_user(substream
, arg
);
2540 case SNDRV_PCM_IOCTL_STATUS
:
2541 return snd_pcm_status_user(substream
, arg
);
2542 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2543 return snd_pcm_channel_info_user(substream
, arg
);
2544 case SNDRV_PCM_IOCTL_PREPARE
:
2545 return snd_pcm_prepare(substream
, file
);
2546 case SNDRV_PCM_IOCTL_RESET
:
2547 return snd_pcm_reset(substream
);
2548 case SNDRV_PCM_IOCTL_START
:
2549 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2550 case SNDRV_PCM_IOCTL_LINK
:
2551 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2552 case SNDRV_PCM_IOCTL_UNLINK
:
2553 return snd_pcm_unlink(substream
);
2554 case SNDRV_PCM_IOCTL_RESUME
:
2555 return snd_pcm_resume(substream
);
2556 case SNDRV_PCM_IOCTL_XRUN
:
2557 return snd_pcm_xrun(substream
);
2558 case SNDRV_PCM_IOCTL_HWSYNC
:
2559 return snd_pcm_hwsync(substream
);
2560 case SNDRV_PCM_IOCTL_DELAY
:
2561 return snd_pcm_delay(substream
, arg
);
2562 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2563 return snd_pcm_sync_ptr(substream
, arg
);
2564 #ifdef CONFIG_SND_SUPPORT_OLD_API
2565 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2566 return snd_pcm_hw_refine_old_user(substream
, arg
);
2567 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2568 return snd_pcm_hw_params_old_user(substream
, arg
);
2570 case SNDRV_PCM_IOCTL_DRAIN
:
2571 return snd_pcm_drain(substream
, file
);
2572 case SNDRV_PCM_IOCTL_DROP
:
2573 return snd_pcm_drop(substream
);
2574 case SNDRV_PCM_IOCTL_PAUSE
:
2577 snd_pcm_stream_lock_irq(substream
);
2578 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2579 snd_pcm_stream_unlock_irq(substream
);
2583 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2587 static int snd_pcm_playback_ioctl1(struct file
*file
,
2588 struct snd_pcm_substream
*substream
,
2589 unsigned int cmd
, void __user
*arg
)
2591 if (snd_BUG_ON(!substream
))
2593 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2596 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2598 struct snd_xferi xferi
;
2599 struct snd_xferi __user
*_xferi
= arg
;
2600 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2601 snd_pcm_sframes_t result
;
2602 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2604 if (put_user(0, &_xferi
->result
))
2606 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2608 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2609 __put_user(result
, &_xferi
->result
);
2610 return result
< 0 ? result
: 0;
2612 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2614 struct snd_xfern xfern
;
2615 struct snd_xfern __user
*_xfern
= arg
;
2616 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2618 snd_pcm_sframes_t result
;
2619 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2621 if (runtime
->channels
> 128)
2623 if (put_user(0, &_xfern
->result
))
2625 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2628 bufs
= memdup_user(xfern
.bufs
,
2629 sizeof(void *) * runtime
->channels
);
2631 return PTR_ERR(bufs
);
2632 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2634 __put_user(result
, &_xfern
->result
);
2635 return result
< 0 ? result
: 0;
2637 case SNDRV_PCM_IOCTL_REWIND
:
2639 snd_pcm_uframes_t frames
;
2640 snd_pcm_uframes_t __user
*_frames
= arg
;
2641 snd_pcm_sframes_t result
;
2642 if (get_user(frames
, _frames
))
2644 if (put_user(0, _frames
))
2646 result
= snd_pcm_playback_rewind(substream
, frames
);
2647 __put_user(result
, _frames
);
2648 return result
< 0 ? result
: 0;
2650 case SNDRV_PCM_IOCTL_FORWARD
:
2652 snd_pcm_uframes_t frames
;
2653 snd_pcm_uframes_t __user
*_frames
= arg
;
2654 snd_pcm_sframes_t result
;
2655 if (get_user(frames
, _frames
))
2657 if (put_user(0, _frames
))
2659 result
= snd_pcm_playback_forward(substream
, frames
);
2660 __put_user(result
, _frames
);
2661 return result
< 0 ? result
: 0;
2664 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2667 static int snd_pcm_capture_ioctl1(struct file
*file
,
2668 struct snd_pcm_substream
*substream
,
2669 unsigned int cmd
, void __user
*arg
)
2671 if (snd_BUG_ON(!substream
))
2673 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2676 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2678 struct snd_xferi xferi
;
2679 struct snd_xferi __user
*_xferi
= arg
;
2680 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2681 snd_pcm_sframes_t result
;
2682 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2684 if (put_user(0, &_xferi
->result
))
2686 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2688 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2689 __put_user(result
, &_xferi
->result
);
2690 return result
< 0 ? result
: 0;
2692 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2694 struct snd_xfern xfern
;
2695 struct snd_xfern __user
*_xfern
= arg
;
2696 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2698 snd_pcm_sframes_t result
;
2699 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2701 if (runtime
->channels
> 128)
2703 if (put_user(0, &_xfern
->result
))
2705 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2708 bufs
= memdup_user(xfern
.bufs
,
2709 sizeof(void *) * runtime
->channels
);
2711 return PTR_ERR(bufs
);
2712 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2714 __put_user(result
, &_xfern
->result
);
2715 return result
< 0 ? result
: 0;
2717 case SNDRV_PCM_IOCTL_REWIND
:
2719 snd_pcm_uframes_t frames
;
2720 snd_pcm_uframes_t __user
*_frames
= arg
;
2721 snd_pcm_sframes_t result
;
2722 if (get_user(frames
, _frames
))
2724 if (put_user(0, _frames
))
2726 result
= snd_pcm_capture_rewind(substream
, frames
);
2727 __put_user(result
, _frames
);
2728 return result
< 0 ? result
: 0;
2730 case SNDRV_PCM_IOCTL_FORWARD
:
2732 snd_pcm_uframes_t frames
;
2733 snd_pcm_uframes_t __user
*_frames
= arg
;
2734 snd_pcm_sframes_t result
;
2735 if (get_user(frames
, _frames
))
2737 if (put_user(0, _frames
))
2739 result
= snd_pcm_capture_forward(substream
, frames
);
2740 __put_user(result
, _frames
);
2741 return result
< 0 ? result
: 0;
2744 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2747 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2750 struct snd_pcm_file
*pcm_file
;
2752 pcm_file
= file
->private_data
;
2754 if (((cmd
>> 8) & 0xff) != 'A')
2757 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2758 (void __user
*)arg
);
2761 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2764 struct snd_pcm_file
*pcm_file
;
2766 pcm_file
= file
->private_data
;
2768 if (((cmd
>> 8) & 0xff) != 'A')
2771 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2772 (void __user
*)arg
);
2775 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2776 unsigned int cmd
, void *arg
)
2781 fs
= snd_enter_user();
2782 switch (substream
->stream
) {
2783 case SNDRV_PCM_STREAM_PLAYBACK
:
2784 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2785 (void __user
*)arg
);
2787 case SNDRV_PCM_STREAM_CAPTURE
:
2788 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2789 (void __user
*)arg
);
2799 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2801 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2804 struct snd_pcm_file
*pcm_file
;
2805 struct snd_pcm_substream
*substream
;
2806 struct snd_pcm_runtime
*runtime
;
2807 snd_pcm_sframes_t result
;
2809 pcm_file
= file
->private_data
;
2810 substream
= pcm_file
->substream
;
2811 if (PCM_RUNTIME_CHECK(substream
))
2813 runtime
= substream
->runtime
;
2814 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2816 if (!frame_aligned(runtime
, count
))
2818 count
= bytes_to_frames(runtime
, count
);
2819 result
= snd_pcm_lib_read(substream
, buf
, count
);
2821 result
= frames_to_bytes(runtime
, result
);
2825 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2826 size_t count
, loff_t
* offset
)
2828 struct snd_pcm_file
*pcm_file
;
2829 struct snd_pcm_substream
*substream
;
2830 struct snd_pcm_runtime
*runtime
;
2831 snd_pcm_sframes_t result
;
2833 pcm_file
= file
->private_data
;
2834 substream
= pcm_file
->substream
;
2835 if (PCM_RUNTIME_CHECK(substream
))
2837 runtime
= substream
->runtime
;
2838 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2840 if (!frame_aligned(runtime
, count
))
2842 count
= bytes_to_frames(runtime
, count
);
2843 result
= snd_pcm_lib_write(substream
, buf
, count
);
2845 result
= frames_to_bytes(runtime
, result
);
2849 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2850 unsigned long nr_segs
, loff_t pos
)
2853 struct snd_pcm_file
*pcm_file
;
2854 struct snd_pcm_substream
*substream
;
2855 struct snd_pcm_runtime
*runtime
;
2856 snd_pcm_sframes_t result
;
2859 snd_pcm_uframes_t frames
;
2861 pcm_file
= iocb
->ki_filp
->private_data
;
2862 substream
= pcm_file
->substream
;
2863 if (PCM_RUNTIME_CHECK(substream
))
2865 runtime
= substream
->runtime
;
2866 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2868 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2870 if (!frame_aligned(runtime
, iov
->iov_len
))
2872 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2873 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2876 for (i
= 0; i
< nr_segs
; ++i
)
2877 bufs
[i
] = iov
[i
].iov_base
;
2878 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2880 result
= frames_to_bytes(runtime
, result
);
2885 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2886 unsigned long nr_segs
, loff_t pos
)
2888 struct snd_pcm_file
*pcm_file
;
2889 struct snd_pcm_substream
*substream
;
2890 struct snd_pcm_runtime
*runtime
;
2891 snd_pcm_sframes_t result
;
2894 snd_pcm_uframes_t frames
;
2896 pcm_file
= iocb
->ki_filp
->private_data
;
2897 substream
= pcm_file
->substream
;
2898 if (PCM_RUNTIME_CHECK(substream
))
2900 runtime
= substream
->runtime
;
2901 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2903 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2904 !frame_aligned(runtime
, iov
->iov_len
))
2906 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2907 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2910 for (i
= 0; i
< nr_segs
; ++i
)
2911 bufs
[i
] = iov
[i
].iov_base
;
2912 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2914 result
= frames_to_bytes(runtime
, result
);
2919 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2921 struct snd_pcm_file
*pcm_file
;
2922 struct snd_pcm_substream
*substream
;
2923 struct snd_pcm_runtime
*runtime
;
2925 snd_pcm_uframes_t avail
;
2927 pcm_file
= file
->private_data
;
2929 substream
= pcm_file
->substream
;
2930 if (PCM_RUNTIME_CHECK(substream
))
2932 runtime
= substream
->runtime
;
2934 poll_wait(file
, &runtime
->sleep
, wait
);
2936 snd_pcm_stream_lock_irq(substream
);
2937 avail
= snd_pcm_playback_avail(runtime
);
2938 switch (runtime
->status
->state
) {
2939 case SNDRV_PCM_STATE_RUNNING
:
2940 case SNDRV_PCM_STATE_PREPARED
:
2941 case SNDRV_PCM_STATE_PAUSED
:
2942 if (avail
>= runtime
->control
->avail_min
) {
2943 mask
= POLLOUT
| POLLWRNORM
;
2947 case SNDRV_PCM_STATE_DRAINING
:
2951 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2954 snd_pcm_stream_unlock_irq(substream
);
2958 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2960 struct snd_pcm_file
*pcm_file
;
2961 struct snd_pcm_substream
*substream
;
2962 struct snd_pcm_runtime
*runtime
;
2964 snd_pcm_uframes_t avail
;
2966 pcm_file
= file
->private_data
;
2968 substream
= pcm_file
->substream
;
2969 if (PCM_RUNTIME_CHECK(substream
))
2971 runtime
= substream
->runtime
;
2973 poll_wait(file
, &runtime
->sleep
, wait
);
2975 snd_pcm_stream_lock_irq(substream
);
2976 avail
= snd_pcm_capture_avail(runtime
);
2977 switch (runtime
->status
->state
) {
2978 case SNDRV_PCM_STATE_RUNNING
:
2979 case SNDRV_PCM_STATE_PREPARED
:
2980 case SNDRV_PCM_STATE_PAUSED
:
2981 if (avail
>= runtime
->control
->avail_min
) {
2982 mask
= POLLIN
| POLLRDNORM
;
2987 case SNDRV_PCM_STATE_DRAINING
:
2989 mask
= POLLIN
| POLLRDNORM
;
2994 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2997 snd_pcm_stream_unlock_irq(substream
);
3006 * Only on coherent architectures, we can mmap the status and the control records
3007 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3009 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3011 * mmap status record
3013 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
3014 struct vm_fault
*vmf
)
3016 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3017 struct snd_pcm_runtime
*runtime
;
3019 if (substream
== NULL
)
3020 return VM_FAULT_SIGBUS
;
3021 runtime
= substream
->runtime
;
3022 vmf
->page
= virt_to_page(runtime
->status
);
3023 get_page(vmf
->page
);
3027 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
3029 .fault
= snd_pcm_mmap_status_fault
,
3032 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3033 struct vm_area_struct
*area
)
3035 struct snd_pcm_runtime
*runtime
;
3037 if (!(area
->vm_flags
& VM_READ
))
3039 runtime
= substream
->runtime
;
3040 size
= area
->vm_end
- area
->vm_start
;
3041 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3043 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3044 area
->vm_private_data
= substream
;
3045 area
->vm_flags
|= VM_RESERVED
;
3050 * mmap control record
3052 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3053 struct vm_fault
*vmf
)
3055 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3056 struct snd_pcm_runtime
*runtime
;
3058 if (substream
== NULL
)
3059 return VM_FAULT_SIGBUS
;
3060 runtime
= substream
->runtime
;
3061 vmf
->page
= virt_to_page(runtime
->control
);
3062 get_page(vmf
->page
);
3066 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3068 .fault
= snd_pcm_mmap_control_fault
,
3071 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3072 struct vm_area_struct
*area
)
3074 struct snd_pcm_runtime
*runtime
;
3076 if (!(area
->vm_flags
& VM_READ
))
3078 runtime
= substream
->runtime
;
3079 size
= area
->vm_end
- area
->vm_start
;
3080 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3082 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3083 area
->vm_private_data
= substream
;
3084 area
->vm_flags
|= VM_RESERVED
;
3087 #else /* ! coherent mmap */
3089 * don't support mmap for status and control records.
3091 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3092 struct vm_area_struct
*area
)
3096 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3097 struct vm_area_struct
*area
)
3101 #endif /* coherent mmap */
3103 static inline struct page
*
3104 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3106 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3107 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3108 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3109 return virt_to_page(CAC_ADDR(vaddr
));
3111 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3112 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
) {
3113 dma_addr_t addr
= substream
->runtime
->dma_addr
+ ofs
;
3114 addr
-= get_dma_offset(substream
->dma_buffer
.dev
.dev
);
3115 /* assume dma_handle set via pfn_to_phys() in
3116 * mm/dma-noncoherent.c
3118 return pfn_to_page(addr
>> PAGE_SHIFT
);
3121 return virt_to_page(vaddr
);
3125 * fault callback for mmapping a RAM page
3127 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3128 struct vm_fault
*vmf
)
3130 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3131 struct snd_pcm_runtime
*runtime
;
3132 unsigned long offset
;
3136 if (substream
== NULL
)
3137 return VM_FAULT_SIGBUS
;
3138 runtime
= substream
->runtime
;
3139 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3140 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3141 if (offset
> dma_bytes
- PAGE_SIZE
)
3142 return VM_FAULT_SIGBUS
;
3143 if (substream
->ops
->page
)
3144 page
= substream
->ops
->page(substream
, offset
);
3146 page
= snd_pcm_default_page_ops(substream
, offset
);
3148 return VM_FAULT_SIGBUS
;
3154 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3155 .open
= snd_pcm_mmap_data_open
,
3156 .close
= snd_pcm_mmap_data_close
,
3159 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3160 .open
= snd_pcm_mmap_data_open
,
3161 .close
= snd_pcm_mmap_data_close
,
3162 .fault
= snd_pcm_mmap_data_fault
,
3165 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3166 /* This should be defined / handled globally! */
3168 #define ARCH_HAS_DMA_MMAP_COHERENT
3173 * mmap the DMA buffer on RAM
3175 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3176 struct vm_area_struct
*area
)
3178 area
->vm_flags
|= VM_RESERVED
;
3179 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3180 if (!substream
->ops
->page
&&
3181 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3182 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3184 substream
->runtime
->dma_area
,
3185 substream
->runtime
->dma_addr
,
3186 area
->vm_end
- area
->vm_start
);
3187 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3188 /* mmap with fault handler */
3189 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3194 * mmap the DMA buffer on I/O memory area
3196 #if SNDRV_PCM_INFO_MMAP_IOMEM
3197 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3198 struct vm_area_struct
*area
)
3201 unsigned long offset
;
3203 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3204 area
->vm_flags
|= VM_IO
;
3205 size
= area
->vm_end
- area
->vm_start
;
3206 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3207 if (io_remap_pfn_range(area
, area
->vm_start
,
3208 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3209 size
, area
->vm_page_prot
))
3214 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3215 #endif /* SNDRV_PCM_INFO_MMAP */
3217 /* mmap callback with pgprot_noncached */
3218 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream
*substream
,
3219 struct vm_area_struct
*area
)
3221 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3222 return snd_pcm_default_mmap(substream
, area
);
3224 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached
);
3229 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3230 struct vm_area_struct
*area
)
3232 struct snd_pcm_runtime
*runtime
;
3234 unsigned long offset
;
3238 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3239 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3242 if (!(area
->vm_flags
& VM_READ
))
3245 runtime
= substream
->runtime
;
3246 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3248 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3250 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3251 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3253 size
= area
->vm_end
- area
->vm_start
;
3254 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3255 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3256 if ((size_t)size
> dma_bytes
)
3258 if (offset
> dma_bytes
- size
)
3261 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3262 area
->vm_private_data
= substream
;
3263 if (substream
->ops
->mmap
)
3264 err
= substream
->ops
->mmap(substream
, area
);
3266 err
= snd_pcm_default_mmap(substream
, area
);
3268 atomic_inc(&substream
->mmap_count
);
3272 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3274 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3276 struct snd_pcm_file
* pcm_file
;
3277 struct snd_pcm_substream
*substream
;
3278 unsigned long offset
;
3280 pcm_file
= file
->private_data
;
3281 substream
= pcm_file
->substream
;
3282 if (PCM_RUNTIME_CHECK(substream
))
3285 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3287 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3288 if (pcm_file
->no_compat_mmap
)
3290 return snd_pcm_mmap_status(substream
, file
, area
);
3291 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3292 if (pcm_file
->no_compat_mmap
)
3294 return snd_pcm_mmap_control(substream
, file
, area
);
3296 return snd_pcm_mmap_data(substream
, file
, area
);
3301 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3303 struct snd_pcm_file
* pcm_file
;
3304 struct snd_pcm_substream
*substream
;
3305 struct snd_pcm_runtime
*runtime
;
3309 pcm_file
= file
->private_data
;
3310 substream
= pcm_file
->substream
;
3311 if (PCM_RUNTIME_CHECK(substream
))
3313 runtime
= substream
->runtime
;
3314 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3323 #ifdef CONFIG_COMPAT
3324 #include "pcm_compat.c"
3326 #define snd_pcm_ioctl_compat NULL
3330 * To be removed helpers to keep binary compatibility
3333 #ifdef CONFIG_SND_SUPPORT_OLD_API
3334 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3335 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3337 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3338 struct snd_pcm_hw_params_old
*oparams
)
3342 memset(params
, 0, sizeof(*params
));
3343 params
->flags
= oparams
->flags
;
3344 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3345 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3346 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3347 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3348 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3349 params
->info
= oparams
->info
;
3350 params
->msbits
= oparams
->msbits
;
3351 params
->rate_num
= oparams
->rate_num
;
3352 params
->rate_den
= oparams
->rate_den
;
3353 params
->fifo_size
= oparams
->fifo_size
;
3356 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3357 struct snd_pcm_hw_params
*params
)
3361 memset(oparams
, 0, sizeof(*oparams
));
3362 oparams
->flags
= params
->flags
;
3363 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3364 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3365 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3366 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3367 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3368 oparams
->info
= params
->info
;
3369 oparams
->msbits
= params
->msbits
;
3370 oparams
->rate_num
= params
->rate_num
;
3371 oparams
->rate_den
= params
->rate_den
;
3372 oparams
->fifo_size
= params
->fifo_size
;
3375 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3376 struct snd_pcm_hw_params_old __user
* _oparams
)
3378 struct snd_pcm_hw_params
*params
;
3379 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3382 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3386 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3387 if (IS_ERR(oparams
)) {
3388 err
= PTR_ERR(oparams
);
3391 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3392 err
= snd_pcm_hw_refine(substream
, params
);
3393 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3394 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3405 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3406 struct snd_pcm_hw_params_old __user
* _oparams
)
3408 struct snd_pcm_hw_params
*params
;
3409 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3412 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3416 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3417 if (IS_ERR(oparams
)) {
3418 err
= PTR_ERR(oparams
);
3421 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3422 err
= snd_pcm_hw_params(substream
, params
);
3423 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3424 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3434 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3437 unsigned long dummy_get_unmapped_area(struct file
*file
, unsigned long addr
,
3438 unsigned long len
, unsigned long pgoff
,
3439 unsigned long flags
)
3444 # define dummy_get_unmapped_area NULL
3451 const struct file_operations snd_pcm_f_ops
[2] = {
3453 .owner
= THIS_MODULE
,
3454 .write
= snd_pcm_write
,
3455 .aio_write
= snd_pcm_aio_write
,
3456 .open
= snd_pcm_playback_open
,
3457 .release
= snd_pcm_release
,
3458 .poll
= snd_pcm_playback_poll
,
3459 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3460 .compat_ioctl
= snd_pcm_ioctl_compat
,
3461 .mmap
= snd_pcm_mmap
,
3462 .fasync
= snd_pcm_fasync
,
3463 .get_unmapped_area
= dummy_get_unmapped_area
,
3466 .owner
= THIS_MODULE
,
3467 .read
= snd_pcm_read
,
3468 .aio_read
= snd_pcm_aio_read
,
3469 .open
= snd_pcm_capture_open
,
3470 .release
= snd_pcm_release
,
3471 .poll
= snd_pcm_capture_poll
,
3472 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3473 .compat_ioctl
= snd_pcm_ioctl_compat
,
3474 .mmap
= snd_pcm_mmap
,
3475 .fasync
= snd_pcm_fasync
,
3476 .get_unmapped_area
= dummy_get_unmapped_area
,