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 <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
43 struct snd_pcm_hw_params_old
{
45 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
46 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
47 struct snd_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
53 unsigned int rate_num
;
54 unsigned int rate_den
;
55 snd_pcm_uframes_t fifo_size
;
56 unsigned char reserved
[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
64 struct snd_pcm_hw_params_old __user
* _oparams
);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
66 struct snd_pcm_hw_params_old __user
* _oparams
);
68 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
);
74 DEFINE_RWLOCK(snd_pcm_link_rwlock
);
75 EXPORT_SYMBOL(snd_pcm_link_rwlock
);
77 static DECLARE_RWSEM(snd_pcm_link_rwsem
);
79 static inline mm_segment_t
snd_enter_user(void)
81 mm_segment_t fs
= get_fs();
86 static inline void snd_leave_user(mm_segment_t fs
)
93 int snd_pcm_info(struct snd_pcm_substream
*substream
, struct snd_pcm_info
*info
)
95 struct snd_pcm_runtime
*runtime
;
96 struct snd_pcm
*pcm
= substream
->pcm
;
97 struct snd_pcm_str
*pstr
= substream
->pstr
;
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(KERN_DEBUG
"%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(KERN_DEBUG
"%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(KERN_DEBUG
"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
& ~SNDRV_PCM_INFO_FIFO_IN_FRAMES
;
317 if (!params
->fifo_size
) {
318 if (snd_mask_min(¶ms
->masks
[SNDRV_PCM_HW_PARAM_FORMAT
]) ==
319 snd_mask_max(¶ms
->masks
[SNDRV_PCM_HW_PARAM_FORMAT
]) &&
320 snd_mask_min(¶ms
->masks
[SNDRV_PCM_HW_PARAM_CHANNELS
]) ==
321 snd_mask_max(¶ms
->masks
[SNDRV_PCM_HW_PARAM_CHANNELS
])) {
322 changed
= substream
->ops
->ioctl(substream
,
323 SNDRV_PCM_IOCTL1_FIFO_SIZE
, params
);
332 EXPORT_SYMBOL(snd_pcm_hw_refine
);
334 static int snd_pcm_hw_refine_user(struct snd_pcm_substream
*substream
,
335 struct snd_pcm_hw_params __user
* _params
)
337 struct snd_pcm_hw_params
*params
;
340 params
= memdup_user(_params
, sizeof(*params
));
342 return PTR_ERR(params
);
344 err
= snd_pcm_hw_refine(substream
, params
);
345 if (copy_to_user(_params
, params
, sizeof(*params
))) {
354 static int period_to_usecs(struct snd_pcm_runtime
*runtime
)
359 return -1; /* invalid */
361 /* take 75% of period time as the deadline */
362 usecs
= (750000 / runtime
->rate
) * runtime
->period_size
;
363 usecs
+= ((750000 % runtime
->rate
) * runtime
->period_size
) /
369 static int snd_pcm_hw_params(struct snd_pcm_substream
*substream
,
370 struct snd_pcm_hw_params
*params
)
372 struct snd_pcm_runtime
*runtime
;
375 snd_pcm_uframes_t frames
;
377 if (PCM_RUNTIME_CHECK(substream
))
379 runtime
= substream
->runtime
;
380 snd_pcm_stream_lock_irq(substream
);
381 switch (runtime
->status
->state
) {
382 case SNDRV_PCM_STATE_OPEN
:
383 case SNDRV_PCM_STATE_SETUP
:
384 case SNDRV_PCM_STATE_PREPARED
:
387 snd_pcm_stream_unlock_irq(substream
);
390 snd_pcm_stream_unlock_irq(substream
);
391 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
392 if (!substream
->oss
.oss
)
394 if (atomic_read(&substream
->mmap_count
))
398 err
= snd_pcm_hw_refine(substream
, params
);
402 err
= snd_pcm_hw_params_choose(substream
, params
);
406 if (substream
->ops
->hw_params
!= NULL
) {
407 err
= substream
->ops
->hw_params(substream
, params
);
412 runtime
->access
= params_access(params
);
413 runtime
->format
= params_format(params
);
414 runtime
->subformat
= params_subformat(params
);
415 runtime
->channels
= params_channels(params
);
416 runtime
->rate
= params_rate(params
);
417 runtime
->period_size
= params_period_size(params
);
418 runtime
->periods
= params_periods(params
);
419 runtime
->buffer_size
= params_buffer_size(params
);
420 runtime
->info
= params
->info
;
421 runtime
->rate_num
= params
->rate_num
;
422 runtime
->rate_den
= params
->rate_den
;
424 bits
= snd_pcm_format_physical_width(runtime
->format
);
425 runtime
->sample_bits
= bits
;
426 bits
*= runtime
->channels
;
427 runtime
->frame_bits
= bits
;
429 while (bits
% 8 != 0) {
433 runtime
->byte_align
= bits
/ 8;
434 runtime
->min_align
= frames
;
436 /* Default sw params */
437 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
438 runtime
->period_step
= 1;
439 runtime
->control
->avail_min
= runtime
->period_size
;
440 runtime
->start_threshold
= 1;
441 runtime
->stop_threshold
= runtime
->buffer_size
;
442 runtime
->silence_threshold
= 0;
443 runtime
->silence_size
= 0;
444 runtime
->boundary
= runtime
->buffer_size
;
445 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
446 runtime
->boundary
*= 2;
448 snd_pcm_timer_resolution_change(substream
);
449 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
451 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
452 substream
->latency_id
);
453 if ((usecs
= period_to_usecs(runtime
)) >= 0)
454 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY
,
455 substream
->latency_id
, usecs
);
458 /* hardware might be unuseable from this time,
459 so we force application to retry to set
460 the correct hardware parameter settings */
461 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
462 if (substream
->ops
->hw_free
!= NULL
)
463 substream
->ops
->hw_free(substream
);
467 static int snd_pcm_hw_params_user(struct snd_pcm_substream
*substream
,
468 struct snd_pcm_hw_params __user
* _params
)
470 struct snd_pcm_hw_params
*params
;
473 params
= memdup_user(_params
, sizeof(*params
));
475 return PTR_ERR(params
);
477 err
= snd_pcm_hw_params(substream
, params
);
478 if (copy_to_user(_params
, params
, sizeof(*params
))) {
487 static int snd_pcm_hw_free(struct snd_pcm_substream
*substream
)
489 struct snd_pcm_runtime
*runtime
;
492 if (PCM_RUNTIME_CHECK(substream
))
494 runtime
= substream
->runtime
;
495 snd_pcm_stream_lock_irq(substream
);
496 switch (runtime
->status
->state
) {
497 case SNDRV_PCM_STATE_SETUP
:
498 case SNDRV_PCM_STATE_PREPARED
:
501 snd_pcm_stream_unlock_irq(substream
);
504 snd_pcm_stream_unlock_irq(substream
);
505 if (atomic_read(&substream
->mmap_count
))
507 if (substream
->ops
->hw_free
)
508 result
= substream
->ops
->hw_free(substream
);
509 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
510 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY
,
511 substream
->latency_id
);
515 static int snd_pcm_sw_params(struct snd_pcm_substream
*substream
,
516 struct snd_pcm_sw_params
*params
)
518 struct snd_pcm_runtime
*runtime
;
520 if (PCM_RUNTIME_CHECK(substream
))
522 runtime
= substream
->runtime
;
523 snd_pcm_stream_lock_irq(substream
);
524 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
525 snd_pcm_stream_unlock_irq(substream
);
528 snd_pcm_stream_unlock_irq(substream
);
530 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
532 if (params
->avail_min
== 0)
534 if (params
->silence_size
>= runtime
->boundary
) {
535 if (params
->silence_threshold
!= 0)
538 if (params
->silence_size
> params
->silence_threshold
)
540 if (params
->silence_threshold
> runtime
->buffer_size
)
543 snd_pcm_stream_lock_irq(substream
);
544 runtime
->tstamp_mode
= params
->tstamp_mode
;
545 runtime
->period_step
= params
->period_step
;
546 runtime
->control
->avail_min
= params
->avail_min
;
547 runtime
->start_threshold
= params
->start_threshold
;
548 runtime
->stop_threshold
= params
->stop_threshold
;
549 runtime
->silence_threshold
= params
->silence_threshold
;
550 runtime
->silence_size
= params
->silence_size
;
551 params
->boundary
= runtime
->boundary
;
552 if (snd_pcm_running(substream
)) {
553 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
554 runtime
->silence_size
> 0)
555 snd_pcm_playback_silence(substream
, ULONG_MAX
);
556 wake_up(&runtime
->sleep
);
558 snd_pcm_stream_unlock_irq(substream
);
562 static int snd_pcm_sw_params_user(struct snd_pcm_substream
*substream
,
563 struct snd_pcm_sw_params __user
* _params
)
565 struct snd_pcm_sw_params params
;
567 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
569 err
= snd_pcm_sw_params(substream
, ¶ms
);
570 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
575 int snd_pcm_status(struct snd_pcm_substream
*substream
,
576 struct snd_pcm_status
*status
)
578 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
580 snd_pcm_stream_lock_irq(substream
);
581 status
->state
= runtime
->status
->state
;
582 status
->suspended_state
= runtime
->status
->suspended_state
;
583 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
585 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
586 if (snd_pcm_running(substream
)) {
587 snd_pcm_update_hw_ptr(substream
);
588 if (runtime
->tstamp_mode
== SNDRV_PCM_TSTAMP_ENABLE
) {
589 status
->tstamp
= runtime
->status
->tstamp
;
593 snd_pcm_gettime(runtime
, &status
->tstamp
);
595 status
->appl_ptr
= runtime
->control
->appl_ptr
;
596 status
->hw_ptr
= runtime
->status
->hw_ptr
;
597 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
598 status
->avail
= snd_pcm_playback_avail(runtime
);
599 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
600 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
601 status
->delay
= runtime
->buffer_size
- status
->avail
;
602 status
->delay
+= runtime
->delay
;
606 status
->avail
= snd_pcm_capture_avail(runtime
);
607 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
608 status
->delay
= status
->avail
+ runtime
->delay
;
612 status
->avail_max
= runtime
->avail_max
;
613 status
->overrange
= runtime
->overrange
;
614 runtime
->avail_max
= 0;
615 runtime
->overrange
= 0;
617 snd_pcm_stream_unlock_irq(substream
);
621 static int snd_pcm_status_user(struct snd_pcm_substream
*substream
,
622 struct snd_pcm_status __user
* _status
)
624 struct snd_pcm_status status
;
627 memset(&status
, 0, sizeof(status
));
628 res
= snd_pcm_status(substream
, &status
);
631 if (copy_to_user(_status
, &status
, sizeof(status
)))
636 static int snd_pcm_channel_info(struct snd_pcm_substream
*substream
,
637 struct snd_pcm_channel_info
* info
)
639 struct snd_pcm_runtime
*runtime
;
640 unsigned int channel
;
642 channel
= info
->channel
;
643 runtime
= substream
->runtime
;
644 snd_pcm_stream_lock_irq(substream
);
645 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
646 snd_pcm_stream_unlock_irq(substream
);
649 snd_pcm_stream_unlock_irq(substream
);
650 if (channel
>= runtime
->channels
)
652 memset(info
, 0, sizeof(*info
));
653 info
->channel
= channel
;
654 return substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, info
);
657 static int snd_pcm_channel_info_user(struct snd_pcm_substream
*substream
,
658 struct snd_pcm_channel_info __user
* _info
)
660 struct snd_pcm_channel_info info
;
663 if (copy_from_user(&info
, _info
, sizeof(info
)))
665 res
= snd_pcm_channel_info(substream
, &info
);
668 if (copy_to_user(_info
, &info
, sizeof(info
)))
673 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream
*substream
)
675 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
676 if (runtime
->trigger_master
== NULL
)
678 if (runtime
->trigger_master
== substream
) {
679 snd_pcm_gettime(runtime
, &runtime
->trigger_tstamp
);
681 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
682 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
684 runtime
->trigger_master
= NULL
;
688 int (*pre_action
)(struct snd_pcm_substream
*substream
, int state
);
689 int (*do_action
)(struct snd_pcm_substream
*substream
, int state
);
690 void (*undo_action
)(struct snd_pcm_substream
*substream
, int state
);
691 void (*post_action
)(struct snd_pcm_substream
*substream
, int state
);
695 * this functions is core for handling of linked stream
696 * Note: the stream state might be changed also on failure
697 * Note2: call with calling stream lock + link lock
699 static int snd_pcm_action_group(struct action_ops
*ops
,
700 struct snd_pcm_substream
*substream
,
701 int state
, int do_lock
)
703 struct snd_pcm_substream
*s
= NULL
;
704 struct snd_pcm_substream
*s1
;
707 snd_pcm_group_for_each_entry(s
, substream
) {
708 if (do_lock
&& s
!= substream
)
709 spin_lock_nested(&s
->self_group
.lock
,
710 SINGLE_DEPTH_NESTING
);
711 res
= ops
->pre_action(s
, state
);
715 snd_pcm_group_for_each_entry(s
, substream
) {
716 res
= ops
->do_action(s
, state
);
718 if (ops
->undo_action
) {
719 snd_pcm_group_for_each_entry(s1
, substream
) {
720 if (s1
== s
) /* failed stream */
722 ops
->undo_action(s1
, state
);
725 s
= NULL
; /* unlock all */
729 snd_pcm_group_for_each_entry(s
, substream
) {
730 ops
->post_action(s
, state
);
735 snd_pcm_group_for_each_entry(s1
, substream
) {
737 spin_unlock(&s1
->self_group
.lock
);
738 if (s1
== s
) /* end */
746 * Note: call with stream lock
748 static int snd_pcm_action_single(struct action_ops
*ops
,
749 struct snd_pcm_substream
*substream
,
754 res
= ops
->pre_action(substream
, state
);
757 res
= ops
->do_action(substream
, state
);
759 ops
->post_action(substream
, state
);
760 else if (ops
->undo_action
)
761 ops
->undo_action(substream
, state
);
766 * Note: call with stream lock
768 static int snd_pcm_action(struct action_ops
*ops
,
769 struct snd_pcm_substream
*substream
,
774 if (snd_pcm_stream_linked(substream
)) {
775 if (!spin_trylock(&substream
->group
->lock
)) {
776 spin_unlock(&substream
->self_group
.lock
);
777 spin_lock(&substream
->group
->lock
);
778 spin_lock(&substream
->self_group
.lock
);
780 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
781 spin_unlock(&substream
->group
->lock
);
783 res
= snd_pcm_action_single(ops
, substream
, state
);
789 * Note: don't use any locks before
791 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
792 struct snd_pcm_substream
*substream
,
797 read_lock_irq(&snd_pcm_link_rwlock
);
798 if (snd_pcm_stream_linked(substream
)) {
799 spin_lock(&substream
->group
->lock
);
800 spin_lock(&substream
->self_group
.lock
);
801 res
= snd_pcm_action_group(ops
, substream
, state
, 1);
802 spin_unlock(&substream
->self_group
.lock
);
803 spin_unlock(&substream
->group
->lock
);
805 spin_lock(&substream
->self_group
.lock
);
806 res
= snd_pcm_action_single(ops
, substream
, state
);
807 spin_unlock(&substream
->self_group
.lock
);
809 read_unlock_irq(&snd_pcm_link_rwlock
);
815 static int snd_pcm_action_nonatomic(struct action_ops
*ops
,
816 struct snd_pcm_substream
*substream
,
821 down_read(&snd_pcm_link_rwsem
);
822 if (snd_pcm_stream_linked(substream
))
823 res
= snd_pcm_action_group(ops
, substream
, state
, 0);
825 res
= snd_pcm_action_single(ops
, substream
, state
);
826 up_read(&snd_pcm_link_rwsem
);
833 static int snd_pcm_pre_start(struct snd_pcm_substream
*substream
, int state
)
835 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
836 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
838 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
839 !snd_pcm_playback_data(substream
))
841 runtime
->trigger_master
= substream
;
845 static int snd_pcm_do_start(struct snd_pcm_substream
*substream
, int state
)
847 if (substream
->runtime
->trigger_master
!= substream
)
849 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
852 static void snd_pcm_undo_start(struct snd_pcm_substream
*substream
, int state
)
854 if (substream
->runtime
->trigger_master
== substream
)
855 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
858 static void snd_pcm_post_start(struct snd_pcm_substream
*substream
, int state
)
860 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
861 snd_pcm_trigger_tstamp(substream
);
862 runtime
->hw_ptr_jiffies
= jiffies
;
863 runtime
->status
->state
= state
;
864 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
865 runtime
->silence_size
> 0)
866 snd_pcm_playback_silence(substream
, ULONG_MAX
);
867 if (substream
->timer
)
868 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
,
869 &runtime
->trigger_tstamp
);
872 static struct action_ops snd_pcm_action_start
= {
873 .pre_action
= snd_pcm_pre_start
,
874 .do_action
= snd_pcm_do_start
,
875 .undo_action
= snd_pcm_undo_start
,
876 .post_action
= snd_pcm_post_start
880 * snd_pcm_start - start all linked streams
881 * @substream: the PCM substream instance
883 int snd_pcm_start(struct snd_pcm_substream
*substream
)
885 return snd_pcm_action(&snd_pcm_action_start
, substream
,
886 SNDRV_PCM_STATE_RUNNING
);
892 static int snd_pcm_pre_stop(struct snd_pcm_substream
*substream
, int state
)
894 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
895 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
897 runtime
->trigger_master
= substream
;
901 static int snd_pcm_do_stop(struct snd_pcm_substream
*substream
, int state
)
903 if (substream
->runtime
->trigger_master
== substream
&&
904 snd_pcm_running(substream
))
905 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
906 return 0; /* unconditonally stop all substreams */
909 static void snd_pcm_post_stop(struct snd_pcm_substream
*substream
, int state
)
911 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
912 if (runtime
->status
->state
!= state
) {
913 snd_pcm_trigger_tstamp(substream
);
914 if (substream
->timer
)
915 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
,
916 &runtime
->trigger_tstamp
);
917 runtime
->status
->state
= state
;
919 wake_up(&runtime
->sleep
);
922 static struct action_ops snd_pcm_action_stop
= {
923 .pre_action
= snd_pcm_pre_stop
,
924 .do_action
= snd_pcm_do_stop
,
925 .post_action
= snd_pcm_post_stop
929 * snd_pcm_stop - try to stop all running streams in the substream group
930 * @substream: the PCM substream instance
931 * @state: PCM state after stopping the stream
933 * The state of each stream is then changed to the given state unconditionally.
935 int snd_pcm_stop(struct snd_pcm_substream
*substream
, int state
)
937 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
940 EXPORT_SYMBOL(snd_pcm_stop
);
943 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
944 * @substream: the PCM substream
946 * After stopping, the state is changed to SETUP.
947 * Unlike snd_pcm_stop(), this affects only the given stream.
949 int snd_pcm_drain_done(struct snd_pcm_substream
*substream
)
951 return snd_pcm_action_single(&snd_pcm_action_stop
, substream
,
952 SNDRV_PCM_STATE_SETUP
);
958 static int snd_pcm_pre_pause(struct snd_pcm_substream
*substream
, int push
)
960 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
961 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
964 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
966 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
968 runtime
->trigger_master
= substream
;
972 static int snd_pcm_do_pause(struct snd_pcm_substream
*substream
, int push
)
974 if (substream
->runtime
->trigger_master
!= substream
)
976 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
977 * a delta betwen the current jiffies, this gives a large enough
978 * delta, effectively to skip the check once.
980 substream
->runtime
->hw_ptr_jiffies
= jiffies
- HZ
* 1000;
981 return substream
->ops
->trigger(substream
,
982 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
983 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
986 static void snd_pcm_undo_pause(struct snd_pcm_substream
*substream
, int push
)
988 if (substream
->runtime
->trigger_master
== substream
)
989 substream
->ops
->trigger(substream
,
990 push
? SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
991 SNDRV_PCM_TRIGGER_PAUSE_PUSH
);
994 static void snd_pcm_post_pause(struct snd_pcm_substream
*substream
, int push
)
996 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
997 snd_pcm_trigger_tstamp(substream
);
999 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
1000 if (substream
->timer
)
1001 snd_timer_notify(substream
->timer
,
1002 SNDRV_TIMER_EVENT_MPAUSE
,
1003 &runtime
->trigger_tstamp
);
1004 wake_up(&runtime
->sleep
);
1006 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
1007 if (substream
->timer
)
1008 snd_timer_notify(substream
->timer
,
1009 SNDRV_TIMER_EVENT_MCONTINUE
,
1010 &runtime
->trigger_tstamp
);
1014 static struct action_ops snd_pcm_action_pause
= {
1015 .pre_action
= snd_pcm_pre_pause
,
1016 .do_action
= snd_pcm_do_pause
,
1017 .undo_action
= snd_pcm_undo_pause
,
1018 .post_action
= snd_pcm_post_pause
1022 * Push/release the pause for all linked streams.
1024 static int snd_pcm_pause(struct snd_pcm_substream
*substream
, int push
)
1026 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
1032 static int snd_pcm_pre_suspend(struct snd_pcm_substream
*substream
, int state
)
1034 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1035 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1037 runtime
->trigger_master
= substream
;
1041 static int snd_pcm_do_suspend(struct snd_pcm_substream
*substream
, int state
)
1043 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1044 if (runtime
->trigger_master
!= substream
)
1046 if (! snd_pcm_running(substream
))
1048 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1049 return 0; /* suspend unconditionally */
1052 static void snd_pcm_post_suspend(struct snd_pcm_substream
*substream
, int state
)
1054 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1055 snd_pcm_trigger_tstamp(substream
);
1056 if (substream
->timer
)
1057 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSUSPEND
,
1058 &runtime
->trigger_tstamp
);
1059 runtime
->status
->suspended_state
= runtime
->status
->state
;
1060 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
1061 wake_up(&runtime
->sleep
);
1064 static struct action_ops snd_pcm_action_suspend
= {
1065 .pre_action
= snd_pcm_pre_suspend
,
1066 .do_action
= snd_pcm_do_suspend
,
1067 .post_action
= snd_pcm_post_suspend
1071 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1072 * @substream: the PCM substream
1074 * After this call, all streams are changed to SUSPENDED state.
1076 int snd_pcm_suspend(struct snd_pcm_substream
*substream
)
1079 unsigned long flags
;
1084 snd_pcm_stream_lock_irqsave(substream
, flags
);
1085 err
= snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
1086 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
1090 EXPORT_SYMBOL(snd_pcm_suspend
);
1093 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1094 * @pcm: the PCM instance
1096 * After this call, all streams are changed to SUSPENDED state.
1098 int snd_pcm_suspend_all(struct snd_pcm
*pcm
)
1100 struct snd_pcm_substream
*substream
;
1101 int stream
, err
= 0;
1106 for (stream
= 0; stream
< 2; stream
++) {
1107 for (substream
= pcm
->streams
[stream
].substream
;
1108 substream
; substream
= substream
->next
) {
1109 /* FIXME: the open/close code should lock this as well */
1110 if (substream
->runtime
== NULL
)
1112 err
= snd_pcm_suspend(substream
);
1113 if (err
< 0 && err
!= -EBUSY
)
1120 EXPORT_SYMBOL(snd_pcm_suspend_all
);
1124 static int snd_pcm_pre_resume(struct snd_pcm_substream
*substream
, int state
)
1126 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1127 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
1129 runtime
->trigger_master
= substream
;
1133 static int snd_pcm_do_resume(struct snd_pcm_substream
*substream
, int state
)
1135 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1136 if (runtime
->trigger_master
!= substream
)
1138 /* DMA not running previously? */
1139 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
&&
1140 (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_DRAINING
||
1141 substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
1143 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
1146 static void snd_pcm_undo_resume(struct snd_pcm_substream
*substream
, int state
)
1148 if (substream
->runtime
->trigger_master
== substream
&&
1149 snd_pcm_running(substream
))
1150 substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
1153 static void snd_pcm_post_resume(struct snd_pcm_substream
*substream
, int state
)
1155 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1156 snd_pcm_trigger_tstamp(substream
);
1157 if (substream
->timer
)
1158 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MRESUME
,
1159 &runtime
->trigger_tstamp
);
1160 runtime
->status
->state
= runtime
->status
->suspended_state
;
1163 static struct action_ops snd_pcm_action_resume
= {
1164 .pre_action
= snd_pcm_pre_resume
,
1165 .do_action
= snd_pcm_do_resume
,
1166 .undo_action
= snd_pcm_undo_resume
,
1167 .post_action
= snd_pcm_post_resume
1170 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1172 struct snd_card
*card
= substream
->pcm
->card
;
1175 snd_power_lock(card
);
1176 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1177 res
= snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
1178 snd_power_unlock(card
);
1184 static int snd_pcm_resume(struct snd_pcm_substream
*substream
)
1189 #endif /* CONFIG_PM */
1194 * Change the RUNNING stream(s) to XRUN state.
1196 static int snd_pcm_xrun(struct snd_pcm_substream
*substream
)
1198 struct snd_card
*card
= substream
->pcm
->card
;
1199 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1202 snd_power_lock(card
);
1203 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1204 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1209 snd_pcm_stream_lock_irq(substream
);
1210 switch (runtime
->status
->state
) {
1211 case SNDRV_PCM_STATE_XRUN
:
1212 result
= 0; /* already there */
1214 case SNDRV_PCM_STATE_RUNNING
:
1215 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1220 snd_pcm_stream_unlock_irq(substream
);
1222 snd_power_unlock(card
);
1229 static int snd_pcm_pre_reset(struct snd_pcm_substream
*substream
, int state
)
1231 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1232 switch (runtime
->status
->state
) {
1233 case SNDRV_PCM_STATE_RUNNING
:
1234 case SNDRV_PCM_STATE_PREPARED
:
1235 case SNDRV_PCM_STATE_PAUSED
:
1236 case SNDRV_PCM_STATE_SUSPENDED
:
1243 static int snd_pcm_do_reset(struct snd_pcm_substream
*substream
, int state
)
1245 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1246 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, NULL
);
1249 runtime
->hw_ptr_base
= 0;
1250 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
-
1251 runtime
->status
->hw_ptr
% runtime
->period_size
;
1252 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1253 runtime
->silence_filled
= 0;
1257 static void snd_pcm_post_reset(struct snd_pcm_substream
*substream
, int state
)
1259 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1260 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1261 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1262 runtime
->silence_size
> 0)
1263 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1266 static struct action_ops snd_pcm_action_reset
= {
1267 .pre_action
= snd_pcm_pre_reset
,
1268 .do_action
= snd_pcm_do_reset
,
1269 .post_action
= snd_pcm_post_reset
1272 static int snd_pcm_reset(struct snd_pcm_substream
*substream
)
1274 return snd_pcm_action_nonatomic(&snd_pcm_action_reset
, substream
, 0);
1280 /* we use the second argument for updating f_flags */
1281 static int snd_pcm_pre_prepare(struct snd_pcm_substream
*substream
,
1284 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1285 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1286 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
)
1288 if (snd_pcm_running(substream
))
1290 substream
->f_flags
= f_flags
;
1294 static int snd_pcm_do_prepare(struct snd_pcm_substream
*substream
, int state
)
1297 err
= substream
->ops
->prepare(substream
);
1300 return snd_pcm_do_reset(substream
, 0);
1303 static void snd_pcm_post_prepare(struct snd_pcm_substream
*substream
, int state
)
1305 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1306 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1307 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1310 static struct action_ops snd_pcm_action_prepare
= {
1311 .pre_action
= snd_pcm_pre_prepare
,
1312 .do_action
= snd_pcm_do_prepare
,
1313 .post_action
= snd_pcm_post_prepare
1317 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1318 * @substream: the PCM substream instance
1319 * @file: file to refer f_flags
1321 static int snd_pcm_prepare(struct snd_pcm_substream
*substream
,
1325 struct snd_card
*card
= substream
->pcm
->card
;
1329 f_flags
= file
->f_flags
;
1331 f_flags
= substream
->f_flags
;
1333 snd_power_lock(card
);
1334 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
)) >= 0)
1335 res
= snd_pcm_action_nonatomic(&snd_pcm_action_prepare
,
1336 substream
, f_flags
);
1337 snd_power_unlock(card
);
1345 static int snd_pcm_pre_drain_init(struct snd_pcm_substream
*substream
, int state
)
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
1391 static int snd_pcm_drop(struct snd_pcm_substream
*substream
);
1394 * Drain the stream(s).
1395 * When the substream is linked, sync until the draining of all playback streams
1397 * After this call, all streams are supposed to be either SETUP or DRAINING
1398 * (capture only) state.
1400 static int snd_pcm_drain(struct snd_pcm_substream
*substream
,
1403 struct snd_card
*card
;
1404 struct snd_pcm_runtime
*runtime
;
1405 struct snd_pcm_substream
*s
;
1410 card
= substream
->pcm
->card
;
1411 runtime
= substream
->runtime
;
1413 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1416 snd_power_lock(card
);
1417 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
1418 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
);
1420 snd_power_unlock(card
);
1426 if (file
->f_flags
& O_NONBLOCK
)
1428 } else if (substream
->f_flags
& O_NONBLOCK
)
1431 down_read(&snd_pcm_link_rwsem
);
1432 snd_pcm_stream_lock_irq(substream
);
1434 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1435 snd_pcm_pause(substream
, 0);
1437 /* pre-start/stop - all running streams are changed to DRAINING state */
1438 result
= snd_pcm_action(&snd_pcm_action_drain_init
, substream
, 0);
1441 /* in non-blocking, we don't wait in ioctl but let caller poll */
1449 struct snd_pcm_runtime
*to_check
;
1450 if (signal_pending(current
)) {
1451 result
= -ERESTARTSYS
;
1454 /* find a substream to drain */
1456 snd_pcm_group_for_each_entry(s
, substream
) {
1457 if (s
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
)
1459 runtime
= s
->runtime
;
1460 if (runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
) {
1466 break; /* all drained */
1467 init_waitqueue_entry(&wait
, current
);
1468 add_wait_queue(&to_check
->sleep
, &wait
);
1469 set_current_state(TASK_INTERRUPTIBLE
);
1470 snd_pcm_stream_unlock_irq(substream
);
1471 up_read(&snd_pcm_link_rwsem
);
1472 snd_power_unlock(card
);
1473 tout
= schedule_timeout(10 * HZ
);
1474 snd_power_lock(card
);
1475 down_read(&snd_pcm_link_rwsem
);
1476 snd_pcm_stream_lock_irq(substream
);
1477 remove_wait_queue(&to_check
->sleep
, &wait
);
1479 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1482 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1483 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1491 snd_pcm_stream_unlock_irq(substream
);
1492 up_read(&snd_pcm_link_rwsem
);
1493 snd_power_unlock(card
);
1501 * Immediately put all linked substreams into SETUP state.
1503 static int snd_pcm_drop(struct snd_pcm_substream
*substream
)
1505 struct snd_pcm_runtime
*runtime
;
1506 struct snd_card
*card
;
1509 if (PCM_RUNTIME_CHECK(substream
))
1511 runtime
= substream
->runtime
;
1512 card
= substream
->pcm
->card
;
1514 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1515 runtime
->status
->state
== SNDRV_PCM_STATE_DISCONNECTED
||
1516 runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
1519 snd_pcm_stream_lock_irq(substream
);
1521 if (runtime
->status
->state
== SNDRV_PCM_STATE_PAUSED
)
1522 snd_pcm_pause(substream
, 0);
1524 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1525 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1526 snd_pcm_stream_unlock_irq(substream
);
1532 /* WARNING: Don't forget to fput back the file */
1533 static struct file
*snd_pcm_file_fd(int fd
)
1536 struct inode
*inode
;
1542 inode
= file
->f_path
.dentry
->d_inode
;
1543 if (!S_ISCHR(inode
->i_mode
) ||
1544 imajor(inode
) != snd_major
) {
1548 minor
= iminor(inode
);
1549 if (!snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_PLAYBACK
) &&
1550 !snd_lookup_minor_data(minor
, SNDRV_DEVICE_TYPE_PCM_CAPTURE
)) {
1560 static int snd_pcm_link(struct snd_pcm_substream
*substream
, int fd
)
1564 struct snd_pcm_file
*pcm_file
;
1565 struct snd_pcm_substream
*substream1
;
1567 file
= snd_pcm_file_fd(fd
);
1570 pcm_file
= file
->private_data
;
1571 substream1
= pcm_file
->substream
;
1572 down_write(&snd_pcm_link_rwsem
);
1573 write_lock_irq(&snd_pcm_link_rwlock
);
1574 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
||
1575 substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1579 if (snd_pcm_stream_linked(substream1
)) {
1583 if (!snd_pcm_stream_linked(substream
)) {
1584 substream
->group
= kmalloc(sizeof(struct snd_pcm_group
), GFP_ATOMIC
);
1585 if (substream
->group
== NULL
) {
1589 spin_lock_init(&substream
->group
->lock
);
1590 INIT_LIST_HEAD(&substream
->group
->substreams
);
1591 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1592 substream
->group
->count
= 1;
1594 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1595 substream
->group
->count
++;
1596 substream1
->group
= substream
->group
;
1598 write_unlock_irq(&snd_pcm_link_rwlock
);
1599 up_write(&snd_pcm_link_rwsem
);
1604 static void relink_to_local(struct snd_pcm_substream
*substream
)
1606 substream
->group
= &substream
->self_group
;
1607 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1608 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1611 static int snd_pcm_unlink(struct snd_pcm_substream
*substream
)
1613 struct snd_pcm_substream
*s
;
1616 down_write(&snd_pcm_link_rwsem
);
1617 write_lock_irq(&snd_pcm_link_rwlock
);
1618 if (!snd_pcm_stream_linked(substream
)) {
1622 list_del(&substream
->link_list
);
1623 substream
->group
->count
--;
1624 if (substream
->group
->count
== 1) { /* detach the last stream, too */
1625 snd_pcm_group_for_each_entry(s
, substream
) {
1629 kfree(substream
->group
);
1631 relink_to_local(substream
);
1633 write_unlock_irq(&snd_pcm_link_rwlock
);
1634 up_write(&snd_pcm_link_rwsem
);
1641 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params
*params
,
1642 struct snd_pcm_hw_rule
*rule
)
1644 struct snd_interval t
;
1645 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1646 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1647 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1650 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params
*params
,
1651 struct snd_pcm_hw_rule
*rule
)
1653 struct snd_interval t
;
1654 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1655 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1656 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1659 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params
*params
,
1660 struct snd_pcm_hw_rule
*rule
)
1662 struct snd_interval t
;
1663 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1664 hw_param_interval_c(params
, rule
->deps
[1]),
1665 (unsigned long) rule
->private, &t
);
1666 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1669 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params
*params
,
1670 struct snd_pcm_hw_rule
*rule
)
1672 struct snd_interval t
;
1673 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1674 (unsigned long) rule
->private,
1675 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1676 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1679 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params
*params
,
1680 struct snd_pcm_hw_rule
*rule
)
1683 struct snd_interval
*i
= hw_param_interval(params
, rule
->deps
[0]);
1685 struct snd_mask
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1687 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1689 if (! snd_mask_test(mask
, k
))
1691 bits
= snd_pcm_format_physical_width(k
);
1693 continue; /* ignore invalid formats */
1694 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1695 snd_mask_reset(&m
, k
);
1697 return snd_mask_refine(mask
, &m
);
1700 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params
*params
,
1701 struct snd_pcm_hw_rule
*rule
)
1703 struct snd_interval t
;
1709 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1711 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1713 bits
= snd_pcm_format_physical_width(k
);
1715 continue; /* ignore invalid formats */
1716 if (t
.min
> (unsigned)bits
)
1718 if (t
.max
< (unsigned)bits
)
1722 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1725 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1726 #error "Change this table"
1729 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1730 48000, 64000, 88200, 96000, 176400, 192000 };
1732 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates
= {
1733 .count
= ARRAY_SIZE(rates
),
1737 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params
*params
,
1738 struct snd_pcm_hw_rule
*rule
)
1740 struct snd_pcm_hardware
*hw
= rule
->private;
1741 return snd_interval_list(hw_param_interval(params
, rule
->var
),
1742 snd_pcm_known_rates
.count
,
1743 snd_pcm_known_rates
.list
, hw
->rates
);
1746 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params
*params
,
1747 struct snd_pcm_hw_rule
*rule
)
1749 struct snd_interval t
;
1750 struct snd_pcm_substream
*substream
= rule
->private;
1752 t
.max
= substream
->buffer_bytes_max
;
1756 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1759 int snd_pcm_hw_constraints_init(struct snd_pcm_substream
*substream
)
1761 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1762 struct snd_pcm_hw_constraints
*constrs
= &runtime
->hw_constraints
;
1765 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1766 snd_mask_any(constrs_mask(constrs
, k
));
1769 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1770 snd_interval_any(constrs_interval(constrs
, k
));
1773 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1774 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1775 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1776 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1777 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1779 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1780 snd_pcm_hw_rule_format
, NULL
,
1781 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1784 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1785 snd_pcm_hw_rule_sample_bits
, NULL
,
1786 SNDRV_PCM_HW_PARAM_FORMAT
,
1787 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1790 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1791 snd_pcm_hw_rule_div
, NULL
,
1792 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1795 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1796 snd_pcm_hw_rule_mul
, NULL
,
1797 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1800 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1801 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1802 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1805 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1806 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1807 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1810 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1811 snd_pcm_hw_rule_div
, NULL
,
1812 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1815 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1816 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1817 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1820 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1821 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1822 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1825 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1826 snd_pcm_hw_rule_div
, NULL
,
1827 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1830 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1831 snd_pcm_hw_rule_div
, NULL
,
1832 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1835 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1836 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1837 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1840 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1841 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1842 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1845 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1846 snd_pcm_hw_rule_mul
, NULL
,
1847 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1850 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1851 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1852 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1855 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1856 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1860 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1861 snd_pcm_hw_rule_muldivk
, (void*) 8,
1862 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1865 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1866 snd_pcm_hw_rule_muldivk
, (void*) 8,
1867 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1870 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1871 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1872 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1875 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1876 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1877 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1883 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream
*substream
)
1885 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1886 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
1888 unsigned int mask
= 0;
1890 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1891 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1892 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1893 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1894 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1895 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1896 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1897 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1898 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1899 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1900 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1902 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1906 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1910 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1914 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1915 hw
->channels_min
, hw
->channels_max
);
1919 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1920 hw
->rate_min
, hw
->rate_max
);
1924 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1925 hw
->period_bytes_min
, hw
->period_bytes_max
);
1929 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1930 hw
->periods_min
, hw
->periods_max
);
1934 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1935 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1939 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1940 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1941 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1946 if (runtime
->dma_bytes
) {
1947 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1952 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1953 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1954 snd_pcm_hw_rule_rate
, hw
,
1955 SNDRV_PCM_HW_PARAM_RATE
, -1);
1960 /* FIXME: this belong to lowlevel */
1961 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1966 static void pcm_release_private(struct snd_pcm_substream
*substream
)
1968 snd_pcm_unlink(substream
);
1971 void snd_pcm_release_substream(struct snd_pcm_substream
*substream
)
1973 substream
->ref_count
--;
1974 if (substream
->ref_count
> 0)
1977 snd_pcm_drop(substream
);
1978 if (substream
->hw_opened
) {
1979 if (substream
->ops
->hw_free
!= NULL
)
1980 substream
->ops
->hw_free(substream
);
1981 substream
->ops
->close(substream
);
1982 substream
->hw_opened
= 0;
1984 if (substream
->pcm_release
) {
1985 substream
->pcm_release(substream
);
1986 substream
->pcm_release
= NULL
;
1988 snd_pcm_detach_substream(substream
);
1991 EXPORT_SYMBOL(snd_pcm_release_substream
);
1993 int snd_pcm_open_substream(struct snd_pcm
*pcm
, int stream
,
1995 struct snd_pcm_substream
**rsubstream
)
1997 struct snd_pcm_substream
*substream
;
2000 err
= snd_pcm_attach_substream(pcm
, stream
, file
, &substream
);
2003 if (substream
->ref_count
> 1) {
2004 *rsubstream
= substream
;
2008 err
= snd_pcm_hw_constraints_init(substream
);
2010 snd_printd("snd_pcm_hw_constraints_init failed\n");
2014 if ((err
= substream
->ops
->open(substream
)) < 0)
2017 substream
->hw_opened
= 1;
2019 err
= snd_pcm_hw_constraints_complete(substream
);
2021 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2025 *rsubstream
= substream
;
2029 snd_pcm_release_substream(substream
);
2033 EXPORT_SYMBOL(snd_pcm_open_substream
);
2035 static int snd_pcm_open_file(struct file
*file
,
2036 struct snd_pcm
*pcm
,
2038 struct snd_pcm_file
**rpcm_file
)
2040 struct snd_pcm_file
*pcm_file
;
2041 struct snd_pcm_substream
*substream
;
2042 struct snd_pcm_str
*str
;
2048 err
= snd_pcm_open_substream(pcm
, stream
, file
, &substream
);
2052 pcm_file
= kzalloc(sizeof(*pcm_file
), GFP_KERNEL
);
2053 if (pcm_file
== NULL
) {
2054 snd_pcm_release_substream(substream
);
2057 pcm_file
->substream
= substream
;
2058 if (substream
->ref_count
== 1) {
2059 str
= substream
->pstr
;
2060 substream
->file
= pcm_file
;
2061 substream
->pcm_release
= pcm_release_private
;
2063 file
->private_data
= pcm_file
;
2065 *rpcm_file
= pcm_file
;
2069 static int snd_pcm_playback_open(struct inode
*inode
, struct file
*file
)
2071 struct snd_pcm
*pcm
;
2073 pcm
= snd_lookup_minor_data(iminor(inode
),
2074 SNDRV_DEVICE_TYPE_PCM_PLAYBACK
);
2075 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_PLAYBACK
);
2078 static int snd_pcm_capture_open(struct inode
*inode
, struct file
*file
)
2080 struct snd_pcm
*pcm
;
2082 pcm
= snd_lookup_minor_data(iminor(inode
),
2083 SNDRV_DEVICE_TYPE_PCM_CAPTURE
);
2084 return snd_pcm_open(file
, pcm
, SNDRV_PCM_STREAM_CAPTURE
);
2087 static int snd_pcm_open(struct file
*file
, struct snd_pcm
*pcm
, int stream
)
2090 struct snd_pcm_file
*pcm_file
;
2097 err
= snd_card_file_add(pcm
->card
, file
);
2100 if (!try_module_get(pcm
->card
->module
)) {
2104 init_waitqueue_entry(&wait
, current
);
2105 add_wait_queue(&pcm
->open_wait
, &wait
);
2106 mutex_lock(&pcm
->open_mutex
);
2108 err
= snd_pcm_open_file(file
, pcm
, stream
, &pcm_file
);
2111 if (err
== -EAGAIN
) {
2112 if (file
->f_flags
& O_NONBLOCK
) {
2118 set_current_state(TASK_INTERRUPTIBLE
);
2119 mutex_unlock(&pcm
->open_mutex
);
2121 mutex_lock(&pcm
->open_mutex
);
2122 if (signal_pending(current
)) {
2127 remove_wait_queue(&pcm
->open_wait
, &wait
);
2128 mutex_unlock(&pcm
->open_mutex
);
2134 module_put(pcm
->card
->module
);
2136 snd_card_file_remove(pcm
->card
, file
);
2141 static int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2143 struct snd_pcm
*pcm
;
2144 struct snd_pcm_substream
*substream
;
2145 struct snd_pcm_file
*pcm_file
;
2147 pcm_file
= file
->private_data
;
2148 substream
= pcm_file
->substream
;
2149 if (snd_BUG_ON(!substream
))
2151 pcm
= substream
->pcm
;
2152 mutex_lock(&pcm
->open_mutex
);
2153 snd_pcm_release_substream(substream
);
2155 mutex_unlock(&pcm
->open_mutex
);
2156 wake_up(&pcm
->open_wait
);
2157 module_put(pcm
->card
->module
);
2158 snd_card_file_remove(pcm
->card
, file
);
2162 static snd_pcm_sframes_t
snd_pcm_playback_rewind(struct snd_pcm_substream
*substream
,
2163 snd_pcm_uframes_t frames
)
2165 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2166 snd_pcm_sframes_t appl_ptr
;
2167 snd_pcm_sframes_t ret
;
2168 snd_pcm_sframes_t hw_avail
;
2173 snd_pcm_stream_lock_irq(substream
);
2174 switch (runtime
->status
->state
) {
2175 case SNDRV_PCM_STATE_PREPARED
:
2177 case SNDRV_PCM_STATE_DRAINING
:
2178 case SNDRV_PCM_STATE_RUNNING
:
2179 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2182 case SNDRV_PCM_STATE_XRUN
:
2185 case SNDRV_PCM_STATE_SUSPENDED
:
2193 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2194 if (hw_avail
<= 0) {
2198 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2200 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2202 appl_ptr
+= runtime
->boundary
;
2203 runtime
->control
->appl_ptr
= appl_ptr
;
2206 snd_pcm_stream_unlock_irq(substream
);
2210 static snd_pcm_sframes_t
snd_pcm_capture_rewind(struct snd_pcm_substream
*substream
,
2211 snd_pcm_uframes_t frames
)
2213 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2214 snd_pcm_sframes_t appl_ptr
;
2215 snd_pcm_sframes_t ret
;
2216 snd_pcm_sframes_t hw_avail
;
2221 snd_pcm_stream_lock_irq(substream
);
2222 switch (runtime
->status
->state
) {
2223 case SNDRV_PCM_STATE_PREPARED
:
2224 case SNDRV_PCM_STATE_DRAINING
:
2226 case SNDRV_PCM_STATE_RUNNING
:
2227 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2230 case SNDRV_PCM_STATE_XRUN
:
2233 case SNDRV_PCM_STATE_SUSPENDED
:
2241 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2242 if (hw_avail
<= 0) {
2246 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2248 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2250 appl_ptr
+= runtime
->boundary
;
2251 runtime
->control
->appl_ptr
= appl_ptr
;
2254 snd_pcm_stream_unlock_irq(substream
);
2258 static snd_pcm_sframes_t
snd_pcm_playback_forward(struct snd_pcm_substream
*substream
,
2259 snd_pcm_uframes_t frames
)
2261 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2262 snd_pcm_sframes_t appl_ptr
;
2263 snd_pcm_sframes_t ret
;
2264 snd_pcm_sframes_t avail
;
2269 snd_pcm_stream_lock_irq(substream
);
2270 switch (runtime
->status
->state
) {
2271 case SNDRV_PCM_STATE_PREPARED
:
2272 case SNDRV_PCM_STATE_PAUSED
:
2274 case SNDRV_PCM_STATE_DRAINING
:
2275 case SNDRV_PCM_STATE_RUNNING
:
2276 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2279 case SNDRV_PCM_STATE_XRUN
:
2282 case SNDRV_PCM_STATE_SUSPENDED
:
2290 avail
= snd_pcm_playback_avail(runtime
);
2295 if (frames
> (snd_pcm_uframes_t
)avail
)
2297 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2298 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2299 appl_ptr
-= runtime
->boundary
;
2300 runtime
->control
->appl_ptr
= appl_ptr
;
2303 snd_pcm_stream_unlock_irq(substream
);
2307 static snd_pcm_sframes_t
snd_pcm_capture_forward(struct snd_pcm_substream
*substream
,
2308 snd_pcm_uframes_t frames
)
2310 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2311 snd_pcm_sframes_t appl_ptr
;
2312 snd_pcm_sframes_t ret
;
2313 snd_pcm_sframes_t avail
;
2318 snd_pcm_stream_lock_irq(substream
);
2319 switch (runtime
->status
->state
) {
2320 case SNDRV_PCM_STATE_PREPARED
:
2321 case SNDRV_PCM_STATE_DRAINING
:
2322 case SNDRV_PCM_STATE_PAUSED
:
2324 case SNDRV_PCM_STATE_RUNNING
:
2325 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2328 case SNDRV_PCM_STATE_XRUN
:
2331 case SNDRV_PCM_STATE_SUSPENDED
:
2339 avail
= snd_pcm_capture_avail(runtime
);
2344 if (frames
> (snd_pcm_uframes_t
)avail
)
2346 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2347 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2348 appl_ptr
-= runtime
->boundary
;
2349 runtime
->control
->appl_ptr
= appl_ptr
;
2352 snd_pcm_stream_unlock_irq(substream
);
2356 static int snd_pcm_hwsync(struct snd_pcm_substream
*substream
)
2358 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2361 snd_pcm_stream_lock_irq(substream
);
2362 switch (runtime
->status
->state
) {
2363 case SNDRV_PCM_STATE_DRAINING
:
2364 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2366 case SNDRV_PCM_STATE_RUNNING
:
2367 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2370 case SNDRV_PCM_STATE_PREPARED
:
2371 case SNDRV_PCM_STATE_SUSPENDED
:
2374 case SNDRV_PCM_STATE_XRUN
:
2382 snd_pcm_stream_unlock_irq(substream
);
2386 static int snd_pcm_delay(struct snd_pcm_substream
*substream
,
2387 snd_pcm_sframes_t __user
*res
)
2389 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2391 snd_pcm_sframes_t n
= 0;
2393 snd_pcm_stream_lock_irq(substream
);
2394 switch (runtime
->status
->state
) {
2395 case SNDRV_PCM_STATE_DRAINING
:
2396 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2398 case SNDRV_PCM_STATE_RUNNING
:
2399 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2402 case SNDRV_PCM_STATE_PREPARED
:
2403 case SNDRV_PCM_STATE_SUSPENDED
:
2405 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2406 n
= snd_pcm_playback_hw_avail(runtime
);
2408 n
= snd_pcm_capture_avail(runtime
);
2409 n
+= runtime
->delay
;
2411 case SNDRV_PCM_STATE_XRUN
:
2419 snd_pcm_stream_unlock_irq(substream
);
2421 if (put_user(n
, res
))
2426 static int snd_pcm_sync_ptr(struct snd_pcm_substream
*substream
,
2427 struct snd_pcm_sync_ptr __user
*_sync_ptr
)
2429 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2430 struct snd_pcm_sync_ptr sync_ptr
;
2431 volatile struct snd_pcm_mmap_status
*status
;
2432 volatile struct snd_pcm_mmap_control
*control
;
2435 memset(&sync_ptr
, 0, sizeof(sync_ptr
));
2436 if (get_user(sync_ptr
.flags
, (unsigned __user
*)&(_sync_ptr
->flags
)))
2438 if (copy_from_user(&sync_ptr
.c
.control
, &(_sync_ptr
->c
.control
), sizeof(struct snd_pcm_mmap_control
)))
2440 status
= runtime
->status
;
2441 control
= runtime
->control
;
2442 if (sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_HWSYNC
) {
2443 err
= snd_pcm_hwsync(substream
);
2447 snd_pcm_stream_lock_irq(substream
);
2448 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_APPL
))
2449 control
->appl_ptr
= sync_ptr
.c
.control
.appl_ptr
;
2451 sync_ptr
.c
.control
.appl_ptr
= control
->appl_ptr
;
2452 if (!(sync_ptr
.flags
& SNDRV_PCM_SYNC_PTR_AVAIL_MIN
))
2453 control
->avail_min
= sync_ptr
.c
.control
.avail_min
;
2455 sync_ptr
.c
.control
.avail_min
= control
->avail_min
;
2456 sync_ptr
.s
.status
.state
= status
->state
;
2457 sync_ptr
.s
.status
.hw_ptr
= status
->hw_ptr
;
2458 sync_ptr
.s
.status
.tstamp
= status
->tstamp
;
2459 sync_ptr
.s
.status
.suspended_state
= status
->suspended_state
;
2460 snd_pcm_stream_unlock_irq(substream
);
2461 if (copy_to_user(_sync_ptr
, &sync_ptr
, sizeof(sync_ptr
)))
2466 static int snd_pcm_tstamp(struct snd_pcm_substream
*substream
, int __user
*_arg
)
2468 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2471 if (get_user(arg
, _arg
))
2473 if (arg
< 0 || arg
> SNDRV_PCM_TSTAMP_TYPE_LAST
)
2475 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY
;
2476 if (arg
== SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
)
2477 runtime
->tstamp_type
= SNDRV_PCM_TSTAMP_TYPE_MONOTONIC
;
2481 static int snd_pcm_common_ioctl1(struct file
*file
,
2482 struct snd_pcm_substream
*substream
,
2483 unsigned int cmd
, void __user
*arg
)
2486 case SNDRV_PCM_IOCTL_PVERSION
:
2487 return put_user(SNDRV_PCM_VERSION
, (int __user
*)arg
) ? -EFAULT
: 0;
2488 case SNDRV_PCM_IOCTL_INFO
:
2489 return snd_pcm_info_user(substream
, arg
);
2490 case SNDRV_PCM_IOCTL_TSTAMP
: /* just for compatibility */
2492 case SNDRV_PCM_IOCTL_TTSTAMP
:
2493 return snd_pcm_tstamp(substream
, arg
);
2494 case SNDRV_PCM_IOCTL_HW_REFINE
:
2495 return snd_pcm_hw_refine_user(substream
, arg
);
2496 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2497 return snd_pcm_hw_params_user(substream
, arg
);
2498 case SNDRV_PCM_IOCTL_HW_FREE
:
2499 return snd_pcm_hw_free(substream
);
2500 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2501 return snd_pcm_sw_params_user(substream
, arg
);
2502 case SNDRV_PCM_IOCTL_STATUS
:
2503 return snd_pcm_status_user(substream
, arg
);
2504 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2505 return snd_pcm_channel_info_user(substream
, arg
);
2506 case SNDRV_PCM_IOCTL_PREPARE
:
2507 return snd_pcm_prepare(substream
, file
);
2508 case SNDRV_PCM_IOCTL_RESET
:
2509 return snd_pcm_reset(substream
);
2510 case SNDRV_PCM_IOCTL_START
:
2511 return snd_pcm_action_lock_irq(&snd_pcm_action_start
, substream
, SNDRV_PCM_STATE_RUNNING
);
2512 case SNDRV_PCM_IOCTL_LINK
:
2513 return snd_pcm_link(substream
, (int)(unsigned long) arg
);
2514 case SNDRV_PCM_IOCTL_UNLINK
:
2515 return snd_pcm_unlink(substream
);
2516 case SNDRV_PCM_IOCTL_RESUME
:
2517 return snd_pcm_resume(substream
);
2518 case SNDRV_PCM_IOCTL_XRUN
:
2519 return snd_pcm_xrun(substream
);
2520 case SNDRV_PCM_IOCTL_HWSYNC
:
2521 return snd_pcm_hwsync(substream
);
2522 case SNDRV_PCM_IOCTL_DELAY
:
2523 return snd_pcm_delay(substream
, arg
);
2524 case SNDRV_PCM_IOCTL_SYNC_PTR
:
2525 return snd_pcm_sync_ptr(substream
, arg
);
2526 #ifdef CONFIG_SND_SUPPORT_OLD_API
2527 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2528 return snd_pcm_hw_refine_old_user(substream
, arg
);
2529 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2530 return snd_pcm_hw_params_old_user(substream
, arg
);
2532 case SNDRV_PCM_IOCTL_DRAIN
:
2533 return snd_pcm_drain(substream
, file
);
2534 case SNDRV_PCM_IOCTL_DROP
:
2535 return snd_pcm_drop(substream
);
2536 case SNDRV_PCM_IOCTL_PAUSE
:
2539 snd_pcm_stream_lock_irq(substream
);
2540 res
= snd_pcm_pause(substream
, (int)(unsigned long)arg
);
2541 snd_pcm_stream_unlock_irq(substream
);
2545 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2549 static int snd_pcm_playback_ioctl1(struct file
*file
,
2550 struct snd_pcm_substream
*substream
,
2551 unsigned int cmd
, void __user
*arg
)
2553 if (snd_BUG_ON(!substream
))
2555 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_PLAYBACK
))
2558 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2560 struct snd_xferi xferi
;
2561 struct snd_xferi __user
*_xferi
= arg
;
2562 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2563 snd_pcm_sframes_t result
;
2564 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2566 if (put_user(0, &_xferi
->result
))
2568 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2570 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2571 __put_user(result
, &_xferi
->result
);
2572 return result
< 0 ? result
: 0;
2574 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2576 struct snd_xfern xfern
;
2577 struct snd_xfern __user
*_xfern
= arg
;
2578 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2580 snd_pcm_sframes_t result
;
2581 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2583 if (runtime
->channels
> 128)
2585 if (put_user(0, &_xfern
->result
))
2587 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2590 bufs
= memdup_user(xfern
.bufs
,
2591 sizeof(void *) * runtime
->channels
);
2593 return PTR_ERR(bufs
);
2594 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2596 __put_user(result
, &_xfern
->result
);
2597 return result
< 0 ? result
: 0;
2599 case SNDRV_PCM_IOCTL_REWIND
:
2601 snd_pcm_uframes_t frames
;
2602 snd_pcm_uframes_t __user
*_frames
= arg
;
2603 snd_pcm_sframes_t result
;
2604 if (get_user(frames
, _frames
))
2606 if (put_user(0, _frames
))
2608 result
= snd_pcm_playback_rewind(substream
, frames
);
2609 __put_user(result
, _frames
);
2610 return result
< 0 ? result
: 0;
2612 case SNDRV_PCM_IOCTL_FORWARD
:
2614 snd_pcm_uframes_t frames
;
2615 snd_pcm_uframes_t __user
*_frames
= arg
;
2616 snd_pcm_sframes_t result
;
2617 if (get_user(frames
, _frames
))
2619 if (put_user(0, _frames
))
2621 result
= snd_pcm_playback_forward(substream
, frames
);
2622 __put_user(result
, _frames
);
2623 return result
< 0 ? result
: 0;
2626 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2629 static int snd_pcm_capture_ioctl1(struct file
*file
,
2630 struct snd_pcm_substream
*substream
,
2631 unsigned int cmd
, void __user
*arg
)
2633 if (snd_BUG_ON(!substream
))
2635 if (snd_BUG_ON(substream
->stream
!= SNDRV_PCM_STREAM_CAPTURE
))
2638 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2640 struct snd_xferi xferi
;
2641 struct snd_xferi __user
*_xferi
= arg
;
2642 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2643 snd_pcm_sframes_t result
;
2644 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2646 if (put_user(0, &_xferi
->result
))
2648 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2650 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2651 __put_user(result
, &_xferi
->result
);
2652 return result
< 0 ? result
: 0;
2654 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2656 struct snd_xfern xfern
;
2657 struct snd_xfern __user
*_xfern
= arg
;
2658 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2660 snd_pcm_sframes_t result
;
2661 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2663 if (runtime
->channels
> 128)
2665 if (put_user(0, &_xfern
->result
))
2667 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2670 bufs
= memdup_user(xfern
.bufs
,
2671 sizeof(void *) * runtime
->channels
);
2673 return PTR_ERR(bufs
);
2674 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2676 __put_user(result
, &_xfern
->result
);
2677 return result
< 0 ? result
: 0;
2679 case SNDRV_PCM_IOCTL_REWIND
:
2681 snd_pcm_uframes_t frames
;
2682 snd_pcm_uframes_t __user
*_frames
= arg
;
2683 snd_pcm_sframes_t result
;
2684 if (get_user(frames
, _frames
))
2686 if (put_user(0, _frames
))
2688 result
= snd_pcm_capture_rewind(substream
, frames
);
2689 __put_user(result
, _frames
);
2690 return result
< 0 ? result
: 0;
2692 case SNDRV_PCM_IOCTL_FORWARD
:
2694 snd_pcm_uframes_t frames
;
2695 snd_pcm_uframes_t __user
*_frames
= arg
;
2696 snd_pcm_sframes_t result
;
2697 if (get_user(frames
, _frames
))
2699 if (put_user(0, _frames
))
2701 result
= snd_pcm_capture_forward(substream
, frames
);
2702 __put_user(result
, _frames
);
2703 return result
< 0 ? result
: 0;
2706 return snd_pcm_common_ioctl1(file
, substream
, cmd
, arg
);
2709 static long snd_pcm_playback_ioctl(struct file
*file
, unsigned int cmd
,
2712 struct snd_pcm_file
*pcm_file
;
2714 pcm_file
= file
->private_data
;
2716 if (((cmd
>> 8) & 0xff) != 'A')
2719 return snd_pcm_playback_ioctl1(file
, pcm_file
->substream
, cmd
,
2720 (void __user
*)arg
);
2723 static long snd_pcm_capture_ioctl(struct file
*file
, unsigned int cmd
,
2726 struct snd_pcm_file
*pcm_file
;
2728 pcm_file
= file
->private_data
;
2730 if (((cmd
>> 8) & 0xff) != 'A')
2733 return snd_pcm_capture_ioctl1(file
, pcm_file
->substream
, cmd
,
2734 (void __user
*)arg
);
2737 int snd_pcm_kernel_ioctl(struct snd_pcm_substream
*substream
,
2738 unsigned int cmd
, void *arg
)
2743 fs
= snd_enter_user();
2744 switch (substream
->stream
) {
2745 case SNDRV_PCM_STREAM_PLAYBACK
:
2746 result
= snd_pcm_playback_ioctl1(NULL
, substream
, cmd
,
2747 (void __user
*)arg
);
2749 case SNDRV_PCM_STREAM_CAPTURE
:
2750 result
= snd_pcm_capture_ioctl1(NULL
, substream
, cmd
,
2751 (void __user
*)arg
);
2761 EXPORT_SYMBOL(snd_pcm_kernel_ioctl
);
2763 static ssize_t
snd_pcm_read(struct file
*file
, char __user
*buf
, size_t count
,
2766 struct snd_pcm_file
*pcm_file
;
2767 struct snd_pcm_substream
*substream
;
2768 struct snd_pcm_runtime
*runtime
;
2769 snd_pcm_sframes_t result
;
2771 pcm_file
= file
->private_data
;
2772 substream
= pcm_file
->substream
;
2773 if (PCM_RUNTIME_CHECK(substream
))
2775 runtime
= substream
->runtime
;
2776 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2778 if (!frame_aligned(runtime
, count
))
2780 count
= bytes_to_frames(runtime
, count
);
2781 result
= snd_pcm_lib_read(substream
, buf
, count
);
2783 result
= frames_to_bytes(runtime
, result
);
2787 static ssize_t
snd_pcm_write(struct file
*file
, const char __user
*buf
,
2788 size_t count
, loff_t
* offset
)
2790 struct snd_pcm_file
*pcm_file
;
2791 struct snd_pcm_substream
*substream
;
2792 struct snd_pcm_runtime
*runtime
;
2793 snd_pcm_sframes_t result
;
2795 pcm_file
= file
->private_data
;
2796 substream
= pcm_file
->substream
;
2797 if (PCM_RUNTIME_CHECK(substream
))
2799 runtime
= substream
->runtime
;
2800 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2802 if (!frame_aligned(runtime
, count
))
2804 count
= bytes_to_frames(runtime
, count
);
2805 result
= snd_pcm_lib_write(substream
, buf
, count
);
2807 result
= frames_to_bytes(runtime
, result
);
2811 static ssize_t
snd_pcm_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
2812 unsigned long nr_segs
, loff_t pos
)
2815 struct snd_pcm_file
*pcm_file
;
2816 struct snd_pcm_substream
*substream
;
2817 struct snd_pcm_runtime
*runtime
;
2818 snd_pcm_sframes_t result
;
2821 snd_pcm_uframes_t frames
;
2823 pcm_file
= iocb
->ki_filp
->private_data
;
2824 substream
= pcm_file
->substream
;
2825 if (PCM_RUNTIME_CHECK(substream
))
2827 runtime
= substream
->runtime
;
2828 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2830 if (nr_segs
> 1024 || nr_segs
!= runtime
->channels
)
2832 if (!frame_aligned(runtime
, iov
->iov_len
))
2834 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2835 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2838 for (i
= 0; i
< nr_segs
; ++i
)
2839 bufs
[i
] = iov
[i
].iov_base
;
2840 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2842 result
= frames_to_bytes(runtime
, result
);
2847 static ssize_t
snd_pcm_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
2848 unsigned long nr_segs
, loff_t pos
)
2850 struct snd_pcm_file
*pcm_file
;
2851 struct snd_pcm_substream
*substream
;
2852 struct snd_pcm_runtime
*runtime
;
2853 snd_pcm_sframes_t result
;
2856 snd_pcm_uframes_t frames
;
2858 pcm_file
= iocb
->ki_filp
->private_data
;
2859 substream
= pcm_file
->substream
;
2860 if (PCM_RUNTIME_CHECK(substream
))
2862 runtime
= substream
->runtime
;
2863 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2865 if (nr_segs
> 128 || nr_segs
!= runtime
->channels
||
2866 !frame_aligned(runtime
, iov
->iov_len
))
2868 frames
= bytes_to_samples(runtime
, iov
->iov_len
);
2869 bufs
= kmalloc(sizeof(void *) * nr_segs
, GFP_KERNEL
);
2872 for (i
= 0; i
< nr_segs
; ++i
)
2873 bufs
[i
] = iov
[i
].iov_base
;
2874 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2876 result
= frames_to_bytes(runtime
, result
);
2881 static unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2883 struct snd_pcm_file
*pcm_file
;
2884 struct snd_pcm_substream
*substream
;
2885 struct snd_pcm_runtime
*runtime
;
2887 snd_pcm_uframes_t avail
;
2889 pcm_file
= file
->private_data
;
2891 substream
= pcm_file
->substream
;
2892 if (PCM_RUNTIME_CHECK(substream
))
2894 runtime
= substream
->runtime
;
2896 poll_wait(file
, &runtime
->sleep
, wait
);
2898 snd_pcm_stream_lock_irq(substream
);
2899 avail
= snd_pcm_playback_avail(runtime
);
2900 switch (runtime
->status
->state
) {
2901 case SNDRV_PCM_STATE_RUNNING
:
2902 case SNDRV_PCM_STATE_PREPARED
:
2903 case SNDRV_PCM_STATE_PAUSED
:
2904 if (avail
>= runtime
->control
->avail_min
) {
2905 mask
= POLLOUT
| POLLWRNORM
;
2909 case SNDRV_PCM_STATE_DRAINING
:
2913 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2916 snd_pcm_stream_unlock_irq(substream
);
2920 static unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2922 struct snd_pcm_file
*pcm_file
;
2923 struct snd_pcm_substream
*substream
;
2924 struct snd_pcm_runtime
*runtime
;
2926 snd_pcm_uframes_t avail
;
2928 pcm_file
= file
->private_data
;
2930 substream
= pcm_file
->substream
;
2931 if (PCM_RUNTIME_CHECK(substream
))
2933 runtime
= substream
->runtime
;
2935 poll_wait(file
, &runtime
->sleep
, wait
);
2937 snd_pcm_stream_lock_irq(substream
);
2938 avail
= snd_pcm_capture_avail(runtime
);
2939 switch (runtime
->status
->state
) {
2940 case SNDRV_PCM_STATE_RUNNING
:
2941 case SNDRV_PCM_STATE_PREPARED
:
2942 case SNDRV_PCM_STATE_PAUSED
:
2943 if (avail
>= runtime
->control
->avail_min
) {
2944 mask
= POLLIN
| POLLRDNORM
;
2949 case SNDRV_PCM_STATE_DRAINING
:
2951 mask
= POLLIN
| POLLRDNORM
;
2956 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2959 snd_pcm_stream_unlock_irq(substream
);
2968 * Only on coherent architectures, we can mmap the status and the control records
2969 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2971 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2973 * mmap status record
2975 static int snd_pcm_mmap_status_fault(struct vm_area_struct
*area
,
2976 struct vm_fault
*vmf
)
2978 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
2979 struct snd_pcm_runtime
*runtime
;
2981 if (substream
== NULL
)
2982 return VM_FAULT_SIGBUS
;
2983 runtime
= substream
->runtime
;
2984 vmf
->page
= virt_to_page(runtime
->status
);
2985 get_page(vmf
->page
);
2989 static const struct vm_operations_struct snd_pcm_vm_ops_status
=
2991 .fault
= snd_pcm_mmap_status_fault
,
2994 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
2995 struct vm_area_struct
*area
)
2997 struct snd_pcm_runtime
*runtime
;
2999 if (!(area
->vm_flags
& VM_READ
))
3001 runtime
= substream
->runtime
;
3002 size
= area
->vm_end
- area
->vm_start
;
3003 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status
)))
3005 area
->vm_ops
= &snd_pcm_vm_ops_status
;
3006 area
->vm_private_data
= substream
;
3007 area
->vm_flags
|= VM_RESERVED
;
3012 * mmap control record
3014 static int snd_pcm_mmap_control_fault(struct vm_area_struct
*area
,
3015 struct vm_fault
*vmf
)
3017 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3018 struct snd_pcm_runtime
*runtime
;
3020 if (substream
== NULL
)
3021 return VM_FAULT_SIGBUS
;
3022 runtime
= substream
->runtime
;
3023 vmf
->page
= virt_to_page(runtime
->control
);
3024 get_page(vmf
->page
);
3028 static const struct vm_operations_struct snd_pcm_vm_ops_control
=
3030 .fault
= snd_pcm_mmap_control_fault
,
3033 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3034 struct vm_area_struct
*area
)
3036 struct snd_pcm_runtime
*runtime
;
3038 if (!(area
->vm_flags
& VM_READ
))
3040 runtime
= substream
->runtime
;
3041 size
= area
->vm_end
- area
->vm_start
;
3042 if (size
!= PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control
)))
3044 area
->vm_ops
= &snd_pcm_vm_ops_control
;
3045 area
->vm_private_data
= substream
;
3046 area
->vm_flags
|= VM_RESERVED
;
3049 #else /* ! coherent mmap */
3051 * don't support mmap for status and control records.
3053 static int snd_pcm_mmap_status(struct snd_pcm_substream
*substream
, struct file
*file
,
3054 struct vm_area_struct
*area
)
3058 static int snd_pcm_mmap_control(struct snd_pcm_substream
*substream
, struct file
*file
,
3059 struct vm_area_struct
*area
)
3063 #endif /* coherent mmap */
3065 static inline struct page
*
3066 snd_pcm_default_page_ops(struct snd_pcm_substream
*substream
, unsigned long ofs
)
3068 void *vaddr
= substream
->runtime
->dma_area
+ ofs
;
3069 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3070 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3071 return virt_to_page(CAC_ADDR(vaddr
));
3073 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3074 if (substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
) {
3075 dma_addr_t addr
= substream
->runtime
->dma_addr
+ ofs
;
3076 addr
-= get_dma_offset(substream
->dma_buffer
.dev
.dev
);
3077 /* assume dma_handle set via pfn_to_phys() in
3078 * mm/dma-noncoherent.c
3080 return pfn_to_page(addr
>> PAGE_SHIFT
);
3083 return virt_to_page(vaddr
);
3087 * fault callback for mmapping a RAM page
3089 static int snd_pcm_mmap_data_fault(struct vm_area_struct
*area
,
3090 struct vm_fault
*vmf
)
3092 struct snd_pcm_substream
*substream
= area
->vm_private_data
;
3093 struct snd_pcm_runtime
*runtime
;
3094 unsigned long offset
;
3098 if (substream
== NULL
)
3099 return VM_FAULT_SIGBUS
;
3100 runtime
= substream
->runtime
;
3101 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
3102 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3103 if (offset
> dma_bytes
- PAGE_SIZE
)
3104 return VM_FAULT_SIGBUS
;
3105 if (substream
->ops
->page
)
3106 page
= substream
->ops
->page(substream
, offset
);
3108 page
= snd_pcm_default_page_ops(substream
, offset
);
3110 return VM_FAULT_SIGBUS
;
3116 static const struct vm_operations_struct snd_pcm_vm_ops_data
= {
3117 .open
= snd_pcm_mmap_data_open
,
3118 .close
= snd_pcm_mmap_data_close
,
3121 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault
= {
3122 .open
= snd_pcm_mmap_data_open
,
3123 .close
= snd_pcm_mmap_data_close
,
3124 .fault
= snd_pcm_mmap_data_fault
,
3127 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3128 /* This should be defined / handled globally! */
3130 #define ARCH_HAS_DMA_MMAP_COHERENT
3135 * mmap the DMA buffer on RAM
3137 static int snd_pcm_default_mmap(struct snd_pcm_substream
*substream
,
3138 struct vm_area_struct
*area
)
3140 area
->vm_flags
|= VM_RESERVED
;
3141 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3142 if (!substream
->ops
->page
&&
3143 substream
->dma_buffer
.dev
.type
== SNDRV_DMA_TYPE_DEV
)
3144 return dma_mmap_coherent(substream
->dma_buffer
.dev
.dev
,
3146 substream
->runtime
->dma_area
,
3147 substream
->runtime
->dma_addr
,
3148 area
->vm_end
- area
->vm_start
);
3149 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3150 /* mmap with fault handler */
3151 area
->vm_ops
= &snd_pcm_vm_ops_data_fault
;
3156 * mmap the DMA buffer on I/O memory area
3158 #if SNDRV_PCM_INFO_MMAP_IOMEM
3159 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream
*substream
,
3160 struct vm_area_struct
*area
)
3163 unsigned long offset
;
3165 #ifdef pgprot_noncached
3166 area
->vm_page_prot
= pgprot_noncached(area
->vm_page_prot
);
3168 area
->vm_flags
|= VM_IO
;
3169 size
= area
->vm_end
- area
->vm_start
;
3170 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3171 if (io_remap_pfn_range(area
, area
->vm_start
,
3172 (substream
->runtime
->dma_addr
+ offset
) >> PAGE_SHIFT
,
3173 size
, area
->vm_page_prot
))
3178 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem
);
3179 #endif /* SNDRV_PCM_INFO_MMAP */
3184 int snd_pcm_mmap_data(struct snd_pcm_substream
*substream
, struct file
*file
,
3185 struct vm_area_struct
*area
)
3187 struct snd_pcm_runtime
*runtime
;
3189 unsigned long offset
;
3193 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
3194 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
3197 if (!(area
->vm_flags
& VM_READ
))
3200 runtime
= substream
->runtime
;
3201 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
3203 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
3205 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
3206 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
3208 size
= area
->vm_end
- area
->vm_start
;
3209 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3210 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
3211 if ((size_t)size
> dma_bytes
)
3213 if (offset
> dma_bytes
- size
)
3216 area
->vm_ops
= &snd_pcm_vm_ops_data
;
3217 area
->vm_private_data
= substream
;
3218 if (substream
->ops
->mmap
)
3219 err
= substream
->ops
->mmap(substream
, area
);
3221 err
= snd_pcm_default_mmap(substream
, area
);
3223 atomic_inc(&substream
->mmap_count
);
3227 EXPORT_SYMBOL(snd_pcm_mmap_data
);
3229 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
3231 struct snd_pcm_file
* pcm_file
;
3232 struct snd_pcm_substream
*substream
;
3233 unsigned long offset
;
3235 pcm_file
= file
->private_data
;
3236 substream
= pcm_file
->substream
;
3237 if (PCM_RUNTIME_CHECK(substream
))
3240 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
3242 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
3243 if (pcm_file
->no_compat_mmap
)
3245 return snd_pcm_mmap_status(substream
, file
, area
);
3246 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
3247 if (pcm_file
->no_compat_mmap
)
3249 return snd_pcm_mmap_control(substream
, file
, area
);
3251 return snd_pcm_mmap_data(substream
, file
, area
);
3256 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
3258 struct snd_pcm_file
* pcm_file
;
3259 struct snd_pcm_substream
*substream
;
3260 struct snd_pcm_runtime
*runtime
;
3264 pcm_file
= file
->private_data
;
3265 substream
= pcm_file
->substream
;
3266 if (PCM_RUNTIME_CHECK(substream
))
3268 runtime
= substream
->runtime
;
3269 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
3278 #ifdef CONFIG_COMPAT
3279 #include "pcm_compat.c"
3281 #define snd_pcm_ioctl_compat NULL
3285 * To be removed helpers to keep binary compatibility
3288 #ifdef CONFIG_SND_SUPPORT_OLD_API
3289 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3290 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3292 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params
*params
,
3293 struct snd_pcm_hw_params_old
*oparams
)
3297 memset(params
, 0, sizeof(*params
));
3298 params
->flags
= oparams
->flags
;
3299 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3300 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3301 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3302 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3303 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3304 params
->info
= oparams
->info
;
3305 params
->msbits
= oparams
->msbits
;
3306 params
->rate_num
= oparams
->rate_num
;
3307 params
->rate_den
= oparams
->rate_den
;
3308 params
->fifo_size
= oparams
->fifo_size
;
3311 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old
*oparams
,
3312 struct snd_pcm_hw_params
*params
)
3316 memset(oparams
, 0, sizeof(*oparams
));
3317 oparams
->flags
= params
->flags
;
3318 for (i
= 0; i
< ARRAY_SIZE(oparams
->masks
); i
++)
3319 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3320 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3321 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3322 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3323 oparams
->info
= params
->info
;
3324 oparams
->msbits
= params
->msbits
;
3325 oparams
->rate_num
= params
->rate_num
;
3326 oparams
->rate_den
= params
->rate_den
;
3327 oparams
->fifo_size
= params
->fifo_size
;
3330 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream
*substream
,
3331 struct snd_pcm_hw_params_old __user
* _oparams
)
3333 struct snd_pcm_hw_params
*params
;
3334 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3337 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3341 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3342 if (IS_ERR(oparams
)) {
3343 err
= PTR_ERR(oparams
);
3346 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3347 err
= snd_pcm_hw_refine(substream
, params
);
3348 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3349 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3360 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream
*substream
,
3361 struct snd_pcm_hw_params_old __user
* _oparams
)
3363 struct snd_pcm_hw_params
*params
;
3364 struct snd_pcm_hw_params_old
*oparams
= NULL
;
3367 params
= kmalloc(sizeof(*params
), GFP_KERNEL
);
3371 oparams
= memdup_user(_oparams
, sizeof(*oparams
));
3372 if (IS_ERR(oparams
)) {
3373 err
= PTR_ERR(oparams
);
3376 snd_pcm_hw_convert_from_old_params(params
, oparams
);
3377 err
= snd_pcm_hw_params(substream
, params
);
3378 snd_pcm_hw_convert_to_old_params(oparams
, params
);
3379 if (copy_to_user(_oparams
, oparams
, sizeof(*oparams
))) {
3389 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3392 unsigned long dummy_get_unmapped_area(struct file
*file
, unsigned long addr
,
3393 unsigned long len
, unsigned long pgoff
,
3394 unsigned long flags
)
3399 # define dummy_get_unmapped_area NULL
3406 const struct file_operations snd_pcm_f_ops
[2] = {
3408 .owner
= THIS_MODULE
,
3409 .write
= snd_pcm_write
,
3410 .aio_write
= snd_pcm_aio_write
,
3411 .open
= snd_pcm_playback_open
,
3412 .release
= snd_pcm_release
,
3413 .poll
= snd_pcm_playback_poll
,
3414 .unlocked_ioctl
= snd_pcm_playback_ioctl
,
3415 .compat_ioctl
= snd_pcm_ioctl_compat
,
3416 .mmap
= snd_pcm_mmap
,
3417 .fasync
= snd_pcm_fasync
,
3418 .get_unmapped_area
= dummy_get_unmapped_area
,
3421 .owner
= THIS_MODULE
,
3422 .read
= snd_pcm_read
,
3423 .aio_read
= snd_pcm_aio_read
,
3424 .open
= snd_pcm_capture_open
,
3425 .release
= snd_pcm_release
,
3426 .poll
= snd_pcm_capture_poll
,
3427 .unlocked_ioctl
= snd_pcm_capture_ioctl
,
3428 .compat_ioctl
= snd_pcm_ioctl_compat
,
3429 .mmap
= snd_pcm_mmap
,
3430 .fasync
= snd_pcm_fasync
,
3431 .get_unmapped_area
= dummy_get_unmapped_area
,