2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/uio.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/info.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/timer.h>
34 #include <sound/minors.h>
40 struct sndrv_pcm_hw_params_old
{
42 unsigned int masks
[SNDRV_PCM_HW_PARAM_SUBFORMAT
-
43 SNDRV_PCM_HW_PARAM_ACCESS
+ 1];
44 struct sndrv_interval intervals
[SNDRV_PCM_HW_PARAM_TICK_TIME
-
45 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
+ 1];
50 unsigned int rate_num
;
51 unsigned int rate_den
;
52 sndrv_pcm_uframes_t fifo_size
;
53 unsigned char reserved
[64];
56 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct sndrv_pcm_hw_params_old)
57 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct sndrv_pcm_hw_params_old)
59 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old
* _oparams
);
60 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old
* _oparams
);
66 rwlock_t snd_pcm_link_rwlock
= RW_LOCK_UNLOCKED
;
69 static inline mm_segment_t
snd_enter_user(void)
71 mm_segment_t fs
= get_fs();
76 static inline void snd_leave_user(mm_segment_t fs
)
83 int snd_pcm_info(snd_pcm_substream_t
* substream
, snd_pcm_info_t
*info
)
85 snd_pcm_runtime_t
* runtime
;
86 snd_pcm_t
*pcm
= substream
->pcm
;
87 snd_pcm_str_t
*pstr
= substream
->pstr
;
89 snd_assert(substream
!= NULL
, return -ENXIO
);
90 memset(info
, 0, sizeof(*info
));
91 info
->card
= pcm
->card
->number
;
92 info
->device
= pcm
->device
;
93 info
->stream
= substream
->stream
;
94 info
->subdevice
= substream
->number
;
95 strlcpy(info
->id
, pcm
->id
, sizeof(info
->id
));
96 strlcpy(info
->name
, pcm
->name
, sizeof(info
->name
));
97 info
->dev_class
= pcm
->dev_class
;
98 info
->dev_subclass
= pcm
->dev_subclass
;
99 info
->subdevices_count
= pstr
->substream_count
;
100 info
->subdevices_avail
= pstr
->substream_count
- pstr
->substream_opened
;
101 strlcpy(info
->subname
, substream
->name
, sizeof(info
->subname
));
102 runtime
= substream
->runtime
;
103 /* AB: FIXME!!! This is definitely nonsense */
105 info
->sync
= runtime
->sync
;
106 substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_INFO
, info
);
111 int snd_pcm_info_user(snd_pcm_substream_t
* substream
, snd_pcm_info_t
* _info
)
114 int err
= snd_pcm_info(substream
, &info
);
115 if (copy_to_user(_info
, &info
, sizeof(info
)))
123 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
124 char *snd_pcm_hw_param_names
[] = {
128 HW_PARAM(SAMPLE_BITS
),
129 HW_PARAM(FRAME_BITS
),
132 HW_PARAM(PERIOD_TIME
),
133 HW_PARAM(PERIOD_SIZE
),
134 HW_PARAM(PERIOD_BYTES
),
136 HW_PARAM(BUFFER_TIME
),
137 HW_PARAM(BUFFER_SIZE
),
138 HW_PARAM(BUFFER_BYTES
),
143 int snd_pcm_hw_refine(snd_pcm_substream_t
*substream
,
144 snd_pcm_hw_params_t
*params
)
147 snd_pcm_hardware_t
*hw
;
148 snd_interval_t
*i
= NULL
;
149 snd_mask_t
*m
= NULL
;
150 snd_pcm_hw_constraints_t
*constrs
= &substream
->runtime
->hw_constraints
;
151 unsigned int rstamps
[constrs
->rules_num
];
152 unsigned int vstamps
[SNDRV_PCM_HW_PARAM_LAST_INTERVAL
+ 1];
153 unsigned int stamp
= 2;
157 params
->fifo_size
= 0;
158 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS
))
160 if (params
->rmask
& (1 << SNDRV_PCM_HW_PARAM_RATE
)) {
161 params
->rate_num
= 0;
162 params
->rate_den
= 0;
165 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
166 m
= hw_param_mask(params
, k
);
167 if (snd_mask_empty(m
))
169 if (!(params
->rmask
& (1 << k
)))
172 printk("%s = ", snd_pcm_hw_param_names
[k
]);
173 printk("%04x%04x%04x%04x -> ", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
175 changed
= snd_mask_refine(m
, constrs_mask(constrs
, k
));
177 printk("%04x%04x%04x%04x\n", m
->bits
[3], m
->bits
[2], m
->bits
[1], m
->bits
[0]);
180 params
->cmask
|= 1 << k
;
185 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
186 i
= hw_param_interval(params
, k
);
187 if (snd_interval_empty(i
))
189 if (!(params
->rmask
& (1 << k
)))
192 printk("%s = ", snd_pcm_hw_param_names
[k
]);
197 i
->openmin
? '(' : '[', i
->min
,
198 i
->max
, i
->openmax
? ')' : ']');
201 changed
= snd_interval_refine(i
, constrs_interval(constrs
, k
));
206 printk("%c%u %u%c\n",
207 i
->openmin
? '(' : '[', i
->min
,
208 i
->max
, i
->openmax
? ')' : ']');
211 params
->cmask
|= 1 << k
;
216 for (k
= 0; k
< constrs
->rules_num
; k
++)
218 for (k
= 0; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++)
219 vstamps
[k
] = (params
->rmask
& (1 << k
)) ? 1 : 0;
222 for (k
= 0; k
< constrs
->rules_num
; k
++) {
223 snd_pcm_hw_rule_t
*r
= &constrs
->rules
[k
];
226 if (r
->cond
&& !(r
->cond
& params
->flags
))
228 for (d
= 0; r
->deps
[d
] >= 0; d
++) {
229 if (vstamps
[r
->deps
[d
]] > rstamps
[k
]) {
237 printk("Rule %d [%p]: ", k
, r
->func
);
239 printk("%s = ", snd_pcm_hw_param_names
[r
->var
]);
240 if (hw_is_mask(r
->var
)) {
241 m
= hw_param_mask(params
, r
->var
);
242 printk("%x", *m
->bits
);
244 i
= hw_param_interval(params
, r
->var
);
249 i
->openmin
? '(' : '[', i
->min
,
250 i
->max
, i
->openmax
? ')' : ']');
254 changed
= r
->func(params
, r
);
258 if (hw_is_mask(r
->var
))
259 printk("%x", *m
->bits
);
265 i
->openmin
? '(' : '[', i
->min
,
266 i
->max
, i
->openmax
? ')' : ']');
272 if (changed
&& r
->var
>= 0) {
273 params
->cmask
|= (1 << r
->var
);
274 vstamps
[r
->var
] = stamp
;
282 if (!params
->msbits
) {
283 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
);
284 if (snd_interval_single(i
))
285 params
->msbits
= snd_interval_value(i
);
288 if (!params
->rate_den
) {
289 i
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
290 if (snd_interval_single(i
)) {
291 params
->rate_num
= snd_interval_value(i
);
292 params
->rate_den
= 1;
296 hw
= &substream
->runtime
->hw
;
298 params
->info
= hw
->info
;
299 if (!params
->fifo_size
)
300 params
->fifo_size
= hw
->fifo_size
;
305 static int snd_pcm_hw_refine_user(snd_pcm_substream_t
* substream
, snd_pcm_hw_params_t
* _params
)
307 snd_pcm_hw_params_t params
;
309 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
311 err
= snd_pcm_hw_refine(substream
, ¶ms
);
312 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
317 static int snd_pcm_hw_params(snd_pcm_substream_t
*substream
,
318 snd_pcm_hw_params_t
*params
)
320 snd_pcm_runtime_t
*runtime
;
323 snd_pcm_uframes_t frames
;
325 snd_assert(substream
!= NULL
, return -ENXIO
);
326 runtime
= substream
->runtime
;
327 snd_assert(runtime
!= NULL
, return -ENXIO
);
328 switch (runtime
->status
->state
) {
329 case SNDRV_PCM_STATE_OPEN
:
330 case SNDRV_PCM_STATE_SETUP
:
331 case SNDRV_PCM_STATE_PREPARED
:
336 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
337 if (!substream
->oss
.oss
)
339 if (atomic_read(&runtime
->mmap_count
))
343 err
= snd_pcm_hw_refine(substream
, params
);
347 err
= snd_pcm_hw_params_choose(substream
, params
);
351 if (substream
->ops
->hw_params
!= NULL
) {
352 err
= substream
->ops
->hw_params(substream
, params
);
357 runtime
->access
= params_access(params
);
358 runtime
->format
= params_format(params
);
359 runtime
->subformat
= params_subformat(params
);
360 runtime
->channels
= params_channels(params
);
361 runtime
->rate
= params_rate(params
);
362 runtime
->period_size
= params_period_size(params
);
363 runtime
->periods
= params_periods(params
);
364 runtime
->buffer_size
= params_buffer_size(params
);
365 runtime
->tick_time
= params_tick_time(params
);
366 runtime
->info
= params
->info
;
367 runtime
->rate_num
= params
->rate_num
;
368 runtime
->rate_den
= params
->rate_den
;
370 bits
= snd_pcm_format_physical_width(runtime
->format
);
371 runtime
->sample_bits
= bits
;
372 bits
*= runtime
->channels
;
373 runtime
->frame_bits
= bits
;
375 while (bits
% 8 != 0) {
379 runtime
->byte_align
= bits
/ 8;
380 runtime
->min_align
= frames
;
382 /* Default sw params */
383 runtime
->tstamp_mode
= SNDRV_PCM_TSTAMP_NONE
;
384 runtime
->period_step
= 1;
385 runtime
->sleep_min
= 0;
386 runtime
->control
->avail_min
= runtime
->period_size
;
387 runtime
->xfer_align
= runtime
->period_size
;
388 runtime
->start_threshold
= 1;
389 runtime
->stop_threshold
= runtime
->buffer_size
;
390 runtime
->silence_threshold
= 0;
391 runtime
->silence_size
= 0;
392 runtime
->boundary
= runtime
->buffer_size
;
393 while (runtime
->boundary
* 2 <= LONG_MAX
- runtime
->buffer_size
)
394 runtime
->boundary
*= 2;
396 snd_pcm_timer_resolution_change(substream
);
397 runtime
->status
->state
= SNDRV_PCM_STATE_SETUP
;
400 /* hardware might be unuseable from this time,
401 so we force application to retry to set
402 the correct hardware parameter settings */
403 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
404 if (substream
->ops
->hw_free
!= NULL
)
405 substream
->ops
->hw_free(substream
);
409 static int snd_pcm_hw_params_user(snd_pcm_substream_t
* substream
, snd_pcm_hw_params_t
* _params
)
411 snd_pcm_hw_params_t params
;
413 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
415 err
= snd_pcm_hw_params(substream
, ¶ms
);
416 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
421 static int snd_pcm_hw_free(snd_pcm_substream_t
* substream
)
423 snd_pcm_runtime_t
*runtime
;
426 snd_assert(substream
!= NULL
, return -ENXIO
);
427 runtime
= substream
->runtime
;
428 snd_assert(runtime
!= NULL
, return -ENXIO
);
429 switch (runtime
->status
->state
) {
430 case SNDRV_PCM_STATE_SETUP
:
431 case SNDRV_PCM_STATE_PREPARED
:
436 if (atomic_read(&runtime
->mmap_count
))
438 if (substream
->ops
->hw_free
== NULL
) {
439 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
442 result
= substream
->ops
->hw_free(substream
);
443 runtime
->status
->state
= SNDRV_PCM_STATE_OPEN
;
447 static int snd_pcm_sw_params(snd_pcm_substream_t
* substream
, snd_pcm_sw_params_t
*params
)
449 snd_pcm_runtime_t
*runtime
;
450 snd_assert(substream
!= NULL
, return -ENXIO
);
451 runtime
= substream
->runtime
;
452 snd_assert(runtime
!= NULL
, return -ENXIO
);
453 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
456 if (params
->tstamp_mode
> SNDRV_PCM_TSTAMP_LAST
)
458 if (params
->avail_min
== 0)
460 if (params
->xfer_align
== 0 ||
461 params
->xfer_align
% runtime
->min_align
!= 0)
463 if (params
->silence_size
>= runtime
->boundary
) {
464 if (params
->silence_threshold
!= 0)
467 if (params
->silence_size
> params
->silence_threshold
)
469 if (params
->silence_threshold
> runtime
->buffer_size
)
472 snd_pcm_stream_lock_irq(substream
);
473 runtime
->tstamp_mode
= params
->tstamp_mode
;
474 runtime
->sleep_min
= params
->sleep_min
;
475 runtime
->period_step
= params
->period_step
;
476 runtime
->control
->avail_min
= params
->avail_min
;
477 runtime
->start_threshold
= params
->start_threshold
;
478 runtime
->stop_threshold
= params
->stop_threshold
;
479 runtime
->silence_threshold
= params
->silence_threshold
;
480 runtime
->silence_size
= params
->silence_size
;
481 runtime
->xfer_align
= params
->xfer_align
;
482 params
->boundary
= runtime
->boundary
;
483 if (snd_pcm_running(substream
)) {
484 if (runtime
->sleep_min
)
485 snd_pcm_tick_prepare(substream
);
487 snd_pcm_tick_set(substream
, 0);
488 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
489 runtime
->silence_size
> 0)
490 snd_pcm_playback_silence(substream
, ULONG_MAX
);
491 wake_up(&runtime
->sleep
);
493 snd_pcm_stream_unlock_irq(substream
);
497 static int snd_pcm_sw_params_user(snd_pcm_substream_t
* substream
, snd_pcm_sw_params_t
* _params
)
499 snd_pcm_sw_params_t params
;
501 if (copy_from_user(¶ms
, _params
, sizeof(params
)))
503 err
= snd_pcm_sw_params(substream
, ¶ms
);
504 if (copy_to_user(_params
, ¶ms
, sizeof(params
)))
509 int snd_pcm_status(snd_pcm_substream_t
*substream
,
510 snd_pcm_status_t
*status
)
512 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
514 snd_pcm_stream_lock_irq(substream
);
515 status
->state
= runtime
->status
->state
;
516 status
->suspended_state
= runtime
->status
->suspended_state
;
517 if (status
->state
== SNDRV_PCM_STATE_OPEN
)
519 status
->trigger_tstamp
= runtime
->trigger_tstamp
;
520 if (snd_pcm_running(substream
)) {
521 snd_pcm_update_hw_ptr(substream
);
522 if (runtime
->tstamp_mode
& SNDRV_PCM_TSTAMP_MMAP
)
523 status
->tstamp
= runtime
->status
->tstamp
;
525 snd_timestamp_now(&status
->tstamp
, runtime
->tstamp_timespec
);
527 snd_timestamp_now(&status
->tstamp
, runtime
->tstamp_timespec
);
528 status
->appl_ptr
= runtime
->control
->appl_ptr
;
529 status
->hw_ptr
= runtime
->status
->hw_ptr
;
530 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
531 status
->avail
= snd_pcm_playback_avail(runtime
);
532 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
||
533 runtime
->status
->state
== SNDRV_PCM_STATE_DRAINING
)
534 status
->delay
= runtime
->buffer_size
- status
->avail
;
538 status
->avail
= snd_pcm_capture_avail(runtime
);
539 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
)
540 status
->delay
= status
->avail
;
544 status
->avail_max
= runtime
->avail_max
;
545 status
->overrange
= runtime
->overrange
;
546 runtime
->avail_max
= 0;
547 runtime
->overrange
= 0;
549 snd_pcm_stream_unlock_irq(substream
);
553 static int snd_pcm_status_user(snd_pcm_substream_t
* substream
, snd_pcm_status_t
* _status
)
555 snd_pcm_status_t status
;
556 snd_pcm_runtime_t
*runtime
;
559 snd_assert(substream
!= NULL
, return -ENXIO
);
560 runtime
= substream
->runtime
;
561 memset(&status
, 0, sizeof(status
));
562 res
= snd_pcm_status(substream
, &status
);
565 if (copy_to_user(_status
, &status
, sizeof(status
)))
570 static int snd_pcm_channel_info(snd_pcm_substream_t
* substream
, snd_pcm_channel_info_t
* _info
)
572 snd_pcm_channel_info_t info
;
573 snd_pcm_runtime_t
*runtime
;
575 unsigned int channel
;
577 snd_assert(substream
!= NULL
, return -ENXIO
);
578 if (copy_from_user(&info
, _info
, sizeof(info
)))
580 channel
= info
.channel
;
581 runtime
= substream
->runtime
;
582 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
584 if (channel
>= runtime
->channels
)
586 memset(&info
, 0, sizeof(info
));
587 info
.channel
= channel
;
588 res
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_CHANNEL_INFO
, &info
);
591 if (copy_to_user(_info
, &info
, sizeof(info
)))
596 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t
*substream
)
598 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
599 if (runtime
->trigger_master
== NULL
)
601 if (runtime
->trigger_master
== substream
) {
602 snd_timestamp_now(&runtime
->trigger_tstamp
, runtime
->tstamp_timespec
);
604 snd_pcm_trigger_tstamp(runtime
->trigger_master
);
605 runtime
->trigger_tstamp
= runtime
->trigger_master
->runtime
->trigger_tstamp
;
607 runtime
->trigger_master
= NULL
;
611 int (*pre_action
)(snd_pcm_substream_t
*substream
, int state
);
612 int (*do_action
)(snd_pcm_substream_t
*substream
, int state
);
613 void (*post_action
)(snd_pcm_substream_t
*substream
, int state
);
617 * this functions is core for handling of linked stream
618 * Note: the stream state might be changed also on failure
619 * Note2: call with calling stream lock + link lock
621 static int snd_pcm_action_group(struct action_ops
*ops
,
622 snd_pcm_substream_t
*substream
,
625 struct list_head
*pos
;
626 snd_pcm_substream_t
*s
= NULL
;
629 snd_pcm_group_for_each(pos
, substream
) {
630 s
= snd_pcm_group_substream_entry(pos
);
632 spin_lock(&s
->self_group
.lock
);
633 res
= ops
->pre_action(s
, state
);
638 snd_pcm_group_for_each(pos
, substream
) {
639 s
= snd_pcm_group_substream_entry(pos
);
640 err
= ops
->do_action(s
, state
);
645 ops
->post_action(s
, state
);
648 spin_unlock(&s
->self_group
.lock
);
651 snd_pcm_substream_t
*s1
;
652 /* unlock all streams */
653 snd_pcm_group_for_each(pos
, substream
) {
654 s1
= snd_pcm_group_substream_entry(pos
);
656 spin_unlock(&s1
->self_group
.lock
);
657 if (s1
== s
) /* end */
665 * Note: call with stream lock
667 static int snd_pcm_action_single(struct action_ops
*ops
,
668 snd_pcm_substream_t
*substream
,
673 res
= ops
->pre_action(substream
, state
);
676 res
= ops
->do_action(substream
, state
);
678 ops
->post_action(substream
, state
);
684 * Note: call with stream lock
686 static int snd_pcm_action(struct action_ops
*ops
,
687 snd_pcm_substream_t
*substream
,
692 if (snd_pcm_stream_linked(substream
)) {
693 if (!spin_trylock(&substream
->group
->lock
)) {
694 spin_unlock(&substream
->self_group
.lock
);
695 spin_lock(&substream
->group
->lock
);
696 spin_lock(&substream
->self_group
.lock
);
698 res
= snd_pcm_action_group(ops
, substream
, state
);
699 spin_unlock(&substream
->group
->lock
);
701 res
= snd_pcm_action_single(ops
, substream
, state
);
707 * Note: don't use any locks before
709 static int snd_pcm_action_lock_irq(struct action_ops
*ops
,
710 snd_pcm_substream_t
*substream
,
715 read_lock_irq(&snd_pcm_link_rwlock
);
716 if (snd_pcm_stream_linked(substream
)) {
717 spin_lock(&substream
->group
->lock
);
718 spin_lock(&substream
->self_group
.lock
);
719 res
= snd_pcm_action_group(ops
, substream
, state
);
720 spin_unlock(&substream
->self_group
.lock
);
721 spin_unlock(&substream
->group
->lock
);
723 spin_lock(&substream
->self_group
.lock
);
724 res
= snd_pcm_action_single(ops
, substream
, state
);
725 spin_unlock(&substream
->self_group
.lock
);
727 read_unlock_irq(&snd_pcm_link_rwlock
);
731 static int snd_pcm_pre_start(snd_pcm_substream_t
*substream
, int state
)
733 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
734 if (runtime
->status
->state
!= SNDRV_PCM_STATE_PREPARED
)
736 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
737 !snd_pcm_playback_data(substream
))
739 runtime
->trigger_master
= substream
;
743 static int snd_pcm_do_start(snd_pcm_substream_t
*substream
, int state
)
745 if (substream
->runtime
->trigger_master
!= substream
)
747 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_START
);
750 static void snd_pcm_post_start(snd_pcm_substream_t
*substream
, int state
)
752 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
753 snd_pcm_trigger_tstamp(substream
);
754 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
755 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
756 runtime
->silence_size
> 0)
757 snd_pcm_playback_silence(substream
, ULONG_MAX
);
758 if (runtime
->sleep_min
)
759 snd_pcm_tick_prepare(substream
);
760 if (substream
->timer
)
761 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTART
, &runtime
->trigger_tstamp
);
764 static struct action_ops snd_pcm_action_start
= {
765 .pre_action
= snd_pcm_pre_start
,
766 .do_action
= snd_pcm_do_start
,
767 .post_action
= snd_pcm_post_start
773 int snd_pcm_start(snd_pcm_substream_t
*substream
)
775 return snd_pcm_action(&snd_pcm_action_start
, substream
, 0);
778 static int snd_pcm_pre_stop(snd_pcm_substream_t
*substream
, int state
)
780 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
781 if (substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
&&
782 substream
->runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
)
784 runtime
->trigger_master
= substream
;
788 static int snd_pcm_do_stop(snd_pcm_substream_t
*substream
, int state
)
790 if (substream
->runtime
->trigger_master
!= substream
)
792 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
795 static void snd_pcm_post_stop(snd_pcm_substream_t
*substream
, int state
)
797 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
798 snd_pcm_trigger_tstamp(substream
);
799 if (substream
->timer
)
800 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MSTOP
, &runtime
->trigger_tstamp
);
801 runtime
->status
->state
= state
;
802 snd_pcm_tick_set(substream
, 0);
803 wake_up(&runtime
->sleep
);
806 static struct action_ops snd_pcm_action_stop
= {
807 .pre_action
= snd_pcm_pre_stop
,
808 .do_action
= snd_pcm_do_stop
,
809 .post_action
= snd_pcm_post_stop
815 int snd_pcm_stop(snd_pcm_substream_t
*substream
, int state
)
817 return snd_pcm_action(&snd_pcm_action_stop
, substream
, state
);
820 static int snd_pcm_pre_pause(snd_pcm_substream_t
*substream
, int push
)
822 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
823 if (!(runtime
->info
& SNDRV_PCM_INFO_PAUSE
))
826 if (runtime
->status
->state
!= SNDRV_PCM_STATE_RUNNING
)
828 } else if (runtime
->status
->state
!= SNDRV_PCM_STATE_PAUSED
)
830 runtime
->trigger_master
= substream
;
834 static int snd_pcm_do_pause(snd_pcm_substream_t
*substream
, int push
)
836 if (substream
->runtime
->trigger_master
!= substream
)
838 return substream
->ops
->trigger(substream
,
839 push
? SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
840 SNDRV_PCM_TRIGGER_PAUSE_RELEASE
);
843 static void snd_pcm_post_pause(snd_pcm_substream_t
*substream
, int push
)
845 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
846 snd_pcm_trigger_tstamp(substream
);
848 runtime
->status
->state
= SNDRV_PCM_STATE_PAUSED
;
849 if (substream
->timer
)
850 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MPAUSE
, &runtime
->trigger_tstamp
);
851 snd_pcm_tick_set(substream
, 0);
852 wake_up(&runtime
->sleep
);
854 runtime
->status
->state
= SNDRV_PCM_STATE_RUNNING
;
855 if (runtime
->sleep_min
)
856 snd_pcm_tick_prepare(substream
);
857 if (substream
->timer
)
858 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MCONTINUE
, &runtime
->trigger_tstamp
);
862 static struct action_ops snd_pcm_action_pause
= {
863 .pre_action
= snd_pcm_pre_pause
,
864 .do_action
= snd_pcm_do_pause
,
865 .post_action
= snd_pcm_post_pause
868 static int snd_pcm_pause(snd_pcm_substream_t
*substream
, int push
)
870 return snd_pcm_action(&snd_pcm_action_pause
, substream
, push
);
876 static int snd_pcm_pre_suspend(snd_pcm_substream_t
*substream
, int state
)
878 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
879 if (runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
)
881 runtime
->status
->suspended_state
= runtime
->status
->state
;
882 runtime
->trigger_master
= substream
;
886 static int snd_pcm_do_suspend(snd_pcm_substream_t
*substream
, int state
)
888 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
889 if (runtime
->trigger_master
!= substream
)
891 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
)
893 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_SUSPEND
);
896 static void snd_pcm_post_suspend(snd_pcm_substream_t
*substream
, int state
)
898 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
899 snd_pcm_trigger_tstamp(substream
);
900 if (substream
->timer
)
901 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MPAUSE
, &runtime
->trigger_tstamp
);
902 runtime
->status
->state
= SNDRV_PCM_STATE_SUSPENDED
;
903 snd_pcm_tick_set(substream
, 0);
904 wake_up(&runtime
->sleep
);
907 static struct action_ops snd_pcm_action_suspend
= {
908 .pre_action
= snd_pcm_pre_suspend
,
909 .do_action
= snd_pcm_do_suspend
,
910 .post_action
= snd_pcm_post_suspend
916 int snd_pcm_suspend(snd_pcm_substream_t
*substream
)
918 return snd_pcm_action(&snd_pcm_action_suspend
, substream
, 0);
922 * snd_pcm_suspend_all
924 int snd_pcm_suspend_all(snd_pcm_t
*pcm
)
926 snd_pcm_substream_t
*substream
;
929 for (stream
= 0; stream
< 2; stream
++) {
930 for (substream
= pcm
->streams
[stream
].substream
; substream
; substream
= substream
->next
) {
931 /* FIXME: the open/close code should lock this as well */
932 if (substream
->runtime
== NULL
)
934 snd_pcm_stream_lock(substream
);
935 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
) {
936 snd_pcm_stream_unlock(substream
);
939 if ((err
= snd_pcm_suspend(substream
)) < 0) {
940 snd_pcm_stream_unlock(substream
);
943 snd_pcm_stream_unlock(substream
);
951 static int snd_pcm_pre_resume(snd_pcm_substream_t
*substream
, int state
)
953 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
954 if (!(runtime
->info
& SNDRV_PCM_INFO_RESUME
))
956 runtime
->trigger_master
= substream
;
960 static int snd_pcm_do_resume(snd_pcm_substream_t
*substream
, int state
)
962 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
963 if (runtime
->trigger_master
!= substream
)
965 if (runtime
->status
->suspended_state
!= SNDRV_PCM_STATE_RUNNING
)
967 return substream
->ops
->trigger(substream
, SNDRV_PCM_TRIGGER_RESUME
);
970 static void snd_pcm_post_resume(snd_pcm_substream_t
*substream
, int state
)
972 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
973 snd_pcm_trigger_tstamp(substream
);
974 if (substream
->timer
)
975 snd_timer_notify(substream
->timer
, SNDRV_TIMER_EVENT_MCONTINUE
, &runtime
->trigger_tstamp
);
976 runtime
->status
->state
= runtime
->status
->suspended_state
;
977 if (runtime
->sleep_min
)
978 snd_pcm_tick_prepare(substream
);
981 static struct action_ops snd_pcm_action_resume
= {
982 .pre_action
= snd_pcm_pre_resume
,
983 .do_action
= snd_pcm_do_resume
,
984 .post_action
= snd_pcm_post_resume
987 static int snd_pcm_resume(snd_pcm_substream_t
*substream
)
989 snd_card_t
*card
= substream
->pcm
->card
;
992 snd_power_lock(card
);
993 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
)) >= 0)
994 return snd_pcm_action_lock_irq(&snd_pcm_action_resume
, substream
, 0);
995 snd_power_unlock(card
);
1001 static int snd_pcm_resume(snd_pcm_substream_t
*substream
)
1006 #endif /* CONFIG_PM */
1008 static int snd_pcm_xrun(snd_pcm_substream_t
*substream
)
1010 snd_card_t
*card
= substream
->pcm
->card
;
1011 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1014 snd_power_lock(card
);
1015 snd_pcm_stream_lock_irq(substream
);
1017 switch (runtime
->status
->state
) {
1018 case SNDRV_PCM_STATE_XRUN
:
1019 result
= 0; /* already there */
1021 case SNDRV_PCM_STATE_RUNNING
:
1022 result
= snd_pcm_stop(substream
, SNDRV_PCM_STATE_XRUN
);
1024 case SNDRV_PCM_STATE_SUSPENDED
:
1025 snd_pcm_stream_unlock_irq(substream
);
1026 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1027 snd_pcm_stream_lock_irq(substream
);
1029 goto _xrun_recovery
;
1034 snd_pcm_stream_unlock_irq(substream
);
1035 snd_power_unlock(card
);
1039 static int snd_pcm_pre_reset(snd_pcm_substream_t
* substream
, int state
)
1041 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1042 switch (runtime
->status
->state
) {
1043 case SNDRV_PCM_STATE_RUNNING
:
1044 case SNDRV_PCM_STATE_PREPARED
:
1045 case SNDRV_PCM_STATE_PAUSED
:
1046 case SNDRV_PCM_STATE_SUSPENDED
:
1053 static int snd_pcm_do_reset(snd_pcm_substream_t
* substream
, int state
)
1055 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1056 int err
= substream
->ops
->ioctl(substream
, SNDRV_PCM_IOCTL1_RESET
, 0);
1059 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1060 runtime
->hw_ptr_base
= 0;
1061 runtime
->hw_ptr_interrupt
= runtime
->status
->hw_ptr
- runtime
->status
->hw_ptr
% runtime
->period_size
;
1062 runtime
->silence_start
= runtime
->status
->hw_ptr
;
1063 runtime
->silence_filled
= 0;
1067 static void snd_pcm_post_reset(snd_pcm_substream_t
* substream
, int state
)
1069 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1070 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1071 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
1072 runtime
->silence_size
> 0)
1073 snd_pcm_playback_silence(substream
, ULONG_MAX
);
1076 static struct action_ops snd_pcm_action_reset
= {
1077 .pre_action
= snd_pcm_pre_reset
,
1078 .do_action
= snd_pcm_do_reset
,
1079 .post_action
= snd_pcm_post_reset
1082 static int snd_pcm_reset(snd_pcm_substream_t
*substream
)
1084 return snd_pcm_action_lock_irq(&snd_pcm_action_reset
, substream
, 0);
1087 static int snd_pcm_pre_prepare(snd_pcm_substream_t
* substream
, int state
)
1089 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1090 switch (runtime
->status
->state
) {
1091 case SNDRV_PCM_STATE_OPEN
:
1093 case SNDRV_PCM_STATE_RUNNING
:
1100 static int snd_pcm_do_prepare(snd_pcm_substream_t
* substream
, int state
)
1103 err
= substream
->ops
->prepare(substream
);
1106 return snd_pcm_do_reset(substream
, 0);
1109 static void snd_pcm_post_prepare(snd_pcm_substream_t
* substream
, int state
)
1111 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1112 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1113 runtime
->status
->state
= SNDRV_PCM_STATE_PREPARED
;
1116 static struct action_ops snd_pcm_action_prepare
= {
1117 .pre_action
= snd_pcm_pre_prepare
,
1118 .do_action
= snd_pcm_do_prepare
,
1119 .post_action
= snd_pcm_post_prepare
1125 int snd_pcm_prepare(snd_pcm_substream_t
*substream
)
1128 snd_card_t
*card
= substream
->pcm
->card
;
1130 snd_power_lock(card
);
1131 if ((res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
)) >= 0)
1132 res
= snd_pcm_action_lock_irq(&snd_pcm_action_prepare
, substream
, 0);
1133 snd_power_unlock(card
);
1137 static void snd_pcm_change_state(snd_pcm_substream_t
*substream
, int state
)
1139 struct list_head
*pos
;
1140 snd_pcm_substream_t
*s
;
1142 if (snd_pcm_stream_linked(substream
)) {
1143 if (!spin_trylock(&substream
->group
->lock
)) {
1144 spin_unlock(&substream
->self_group
.lock
);
1145 spin_lock(&substream
->group
->lock
);
1146 spin_lock(&substream
->self_group
.lock
);
1148 snd_pcm_group_for_each(pos
, substream
) {
1149 s
= snd_pcm_group_substream_entry(pos
);
1151 spin_lock(&s
->self_group
.lock
);
1152 s
->runtime
->status
->state
= state
;
1154 spin_unlock(&s
->self_group
.lock
);
1156 spin_unlock(&substream
->group
->lock
);
1158 substream
->runtime
->status
->state
= state
;
1162 static int snd_pcm_playback_drop(snd_pcm_substream_t
*substream
);
1164 static int snd_pcm_playback_drain(snd_pcm_substream_t
* substream
)
1167 snd_pcm_runtime_t
*runtime
;
1168 int err
, result
= 0;
1170 enum { READY
, EXPIRED
, SUSPENDED
, SIGNALED
} state
= READY
;
1171 snd_pcm_uframes_t stop_threshold
;
1173 snd_assert(substream
!= NULL
, return -ENXIO
);
1174 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
1175 runtime
= substream
->runtime
;
1176 card
= substream
->pcm
->card
;
1178 snd_power_lock(card
);
1179 snd_pcm_stream_lock_irq(substream
);
1181 /* stop_threshold fixup to avoid endless loop when */
1182 /* stop_threshold > buffer_size */
1183 stop_threshold
= runtime
->stop_threshold
;
1184 if (runtime
->stop_threshold
> runtime
->buffer_size
)
1185 runtime
->stop_threshold
= runtime
->buffer_size
;
1187 switch (runtime
->status
->state
) {
1188 case SNDRV_PCM_STATE_PAUSED
:
1189 snd_pcm_pause(substream
, 0);
1191 case SNDRV_PCM_STATE_RUNNING
:
1192 case SNDRV_PCM_STATE_DRAINING
:
1194 case SNDRV_PCM_STATE_SUSPENDED
:
1195 snd_pcm_stream_unlock_irq(substream
);
1196 result
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1197 snd_pcm_stream_lock_irq(substream
);
1199 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_SETUP
);
1201 case SNDRV_PCM_STATE_OPEN
:
1204 case SNDRV_PCM_STATE_PREPARED
:
1205 if (!snd_pcm_playback_empty(substream
)) {
1206 err
= snd_pcm_start(substream
);
1214 case SNDRV_PCM_STATE_XRUN
:
1215 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_SETUP
);
1217 case SNDRV_PCM_STATE_SETUP
:
1223 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
) {
1224 if (snd_pcm_playback_empty(substream
)) {
1225 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1228 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_DRAINING
);
1231 if (substream
->ffile
->f_flags
& O_NONBLOCK
) {
1236 init_waitqueue_entry(&wait
, current
);
1237 add_wait_queue(&runtime
->sleep
, &wait
);
1240 if (signal_pending(current
)) {
1244 set_current_state(TASK_INTERRUPTIBLE
);
1245 snd_pcm_stream_unlock_irq(substream
);
1246 tout
= schedule_timeout(10 * HZ
);
1247 snd_pcm_stream_lock_irq(substream
);
1249 state
= runtime
->status
->state
== SNDRV_PCM_STATE_SUSPENDED
? SUSPENDED
: EXPIRED
;
1252 if (runtime
->status
->state
!= SNDRV_PCM_STATE_DRAINING
) {
1257 remove_wait_queue(&runtime
->sleep
, &wait
);
1261 result
= -ERESTARTSYS
;
1267 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1275 runtime
->stop_threshold
= stop_threshold
;
1276 snd_pcm_stream_unlock_irq(substream
);
1277 snd_power_unlock(card
);
1278 if (state
== EXPIRED
)
1279 snd_pcm_playback_drop(substream
);
1284 static int snd_pcm_playback_drop(snd_pcm_substream_t
*substream
)
1286 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1287 snd_card_t
*card
= substream
->pcm
->card
;
1290 snd_power_lock(card
);
1291 snd_pcm_stream_lock_irq(substream
);
1292 switch (runtime
->status
->state
) {
1293 case SNDRV_PCM_STATE_OPEN
:
1296 case SNDRV_PCM_STATE_SETUP
:
1298 case SNDRV_PCM_STATE_PAUSED
:
1299 snd_pcm_pause(substream
, 0);
1301 case SNDRV_PCM_STATE_RUNNING
:
1302 case SNDRV_PCM_STATE_DRAINING
:
1303 if (snd_pcm_update_hw_ptr(substream
) >= 0) {
1304 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1308 case SNDRV_PCM_STATE_PREPARED
:
1309 case SNDRV_PCM_STATE_XRUN
:
1310 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_SETUP
);
1312 case SNDRV_PCM_STATE_SUSPENDED
:
1313 snd_pcm_stream_unlock_irq(substream
);
1314 res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1315 snd_pcm_stream_lock_irq(substream
);
1317 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_SETUP
);
1322 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1323 snd_pcm_stream_unlock_irq(substream
);
1324 snd_power_unlock(card
);
1328 static int snd_pcm_capture_drain(snd_pcm_substream_t
* substream
)
1330 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1331 snd_card_t
*card
= substream
->pcm
->card
;
1334 snd_power_lock(card
);
1335 snd_pcm_stream_lock_irq(substream
);
1336 switch (runtime
->status
->state
) {
1337 case SNDRV_PCM_STATE_OPEN
:
1340 case SNDRV_PCM_STATE_PREPARED
:
1341 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_SETUP
);
1343 case SNDRV_PCM_STATE_SETUP
:
1344 case SNDRV_PCM_STATE_DRAINING
:
1346 case SNDRV_PCM_STATE_PAUSED
:
1347 snd_pcm_pause(substream
, 0);
1349 case SNDRV_PCM_STATE_RUNNING
:
1350 if (snd_pcm_update_hw_ptr(substream
) >= 0) {
1351 snd_pcm_stop(substream
,
1352 snd_pcm_capture_avail(runtime
) > 0 ?
1353 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
);
1357 case SNDRV_PCM_STATE_XRUN
:
1359 snd_pcm_change_state(substream
,
1360 snd_pcm_capture_avail(runtime
) > 0 ?
1361 SNDRV_PCM_STATE_DRAINING
: SNDRV_PCM_STATE_SETUP
);
1363 case SNDRV_PCM_STATE_SUSPENDED
:
1364 snd_pcm_stream_unlock_irq(substream
);
1365 res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1366 snd_pcm_stream_lock_irq(substream
);
1368 goto _xrun_recovery
;
1373 snd_pcm_stream_unlock_irq(substream
);
1374 snd_power_unlock(card
);
1378 static int snd_pcm_capture_drop(snd_pcm_substream_t
* substream
)
1380 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1381 snd_card_t
*card
= substream
->pcm
->card
;
1384 snd_power_lock(card
);
1385 snd_pcm_stream_lock_irq(substream
);
1386 switch (runtime
->status
->state
) {
1387 case SNDRV_PCM_STATE_OPEN
:
1390 case SNDRV_PCM_STATE_SETUP
:
1392 case SNDRV_PCM_STATE_PAUSED
:
1393 snd_pcm_pause(substream
, 0);
1395 case SNDRV_PCM_STATE_RUNNING
:
1396 snd_pcm_stop(substream
, SNDRV_PCM_STATE_SETUP
);
1398 case SNDRV_PCM_STATE_SUSPENDED
:
1399 snd_pcm_stream_unlock_irq(substream
);
1400 res
= snd_power_wait(card
, SNDRV_CTL_POWER_D0
, substream
->ffile
);
1401 snd_pcm_stream_lock_irq(substream
);
1405 case SNDRV_PCM_STATE_PREPARED
:
1406 case SNDRV_PCM_STATE_DRAINING
:
1407 case SNDRV_PCM_STATE_XRUN
:
1408 snd_pcm_change_state(substream
, SNDRV_PCM_STATE_SETUP
);
1413 runtime
->control
->appl_ptr
= runtime
->status
->hw_ptr
;
1415 snd_pcm_stream_unlock_irq(substream
);
1416 snd_power_unlock(card
);
1420 /* WARNING: Don't forget to fput back the file */
1421 extern int snd_major
;
1422 static struct file
*snd_pcm_file_fd(int fd
)
1425 struct inode
*inode
;
1426 unsigned short minor
;
1430 inode
= file
->f_dentry
->d_inode
;
1431 if (!S_ISCHR(inode
->i_mode
) ||
1432 imajor(inode
) != snd_major
) {
1436 minor
= iminor(inode
);
1438 minor
% SNDRV_MINOR_DEVICES
< SNDRV_MINOR_PCM_PLAYBACK
) {
1445 static int snd_pcm_link(snd_pcm_substream_t
*substream
, int fd
)
1449 snd_pcm_file_t
*pcm_file
;
1450 snd_pcm_substream_t
*substream1
;
1452 if (substream
->runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
1454 file
= snd_pcm_file_fd(fd
);
1457 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
1458 substream1
= pcm_file
->substream
;
1459 write_lock_irq(&snd_pcm_link_rwlock
);
1460 if (substream
->runtime
->status
->state
!= substream1
->runtime
->status
->state
) {
1464 if (snd_pcm_stream_linked(substream1
)) {
1468 if (!snd_pcm_stream_linked(substream
)) {
1469 substream
->group
= kmalloc(sizeof(snd_pcm_group_t
), GFP_ATOMIC
);
1470 if (substream
->group
== NULL
) {
1474 spin_lock_init(&substream
->group
->lock
);
1475 INIT_LIST_HEAD(&substream
->group
->substreams
);
1476 list_add_tail(&substream
->link_list
, &substream
->group
->substreams
);
1478 list_add_tail(&substream1
->link_list
, &substream
->group
->substreams
);
1479 substream1
->group
= substream
->group
;
1481 write_unlock_irq(&snd_pcm_link_rwlock
);
1486 static void relink_to_local(snd_pcm_substream_t
*substream
)
1488 substream
->group
= &substream
->self_group
;
1489 INIT_LIST_HEAD(&substream
->self_group
.substreams
);
1490 list_add_tail(&substream
->link_list
, &substream
->self_group
.substreams
);
1493 static int snd_pcm_unlink(snd_pcm_substream_t
*substream
)
1495 struct list_head
*pos
;
1496 int res
= 0, count
= 0;
1498 write_lock_irq(&snd_pcm_link_rwlock
);
1499 if (!snd_pcm_stream_linked(substream
)) {
1503 list_del(&substream
->link_list
);
1504 snd_pcm_group_for_each(pos
, substream
) {
1508 if (count
== 1) { /* detach the last stream, too */
1509 snd_pcm_group_for_each(pos
, substream
) {
1510 relink_to_local(snd_pcm_group_substream_entry(pos
));
1513 kfree(substream
->group
);
1515 relink_to_local(substream
);
1517 write_unlock_irq(&snd_pcm_link_rwlock
);
1521 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t
*params
,
1522 snd_pcm_hw_rule_t
*rule
)
1525 snd_interval_mul(hw_param_interval_c(params
, rule
->deps
[0]),
1526 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1527 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1530 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t
*params
,
1531 snd_pcm_hw_rule_t
*rule
)
1534 snd_interval_div(hw_param_interval_c(params
, rule
->deps
[0]),
1535 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1536 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1539 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t
*params
,
1540 snd_pcm_hw_rule_t
*rule
)
1543 snd_interval_muldivk(hw_param_interval_c(params
, rule
->deps
[0]),
1544 hw_param_interval_c(params
, rule
->deps
[1]),
1545 (unsigned long) rule
->private, &t
);
1546 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1549 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t
*params
,
1550 snd_pcm_hw_rule_t
*rule
)
1553 snd_interval_mulkdiv(hw_param_interval_c(params
, rule
->deps
[0]),
1554 (unsigned long) rule
->private,
1555 hw_param_interval_c(params
, rule
->deps
[1]), &t
);
1556 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1559 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t
*params
,
1560 snd_pcm_hw_rule_t
*rule
)
1563 snd_interval_t
*i
= hw_param_interval(params
, rule
->deps
[0]);
1565 snd_mask_t
*mask
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
1567 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1569 if (! snd_mask_test(mask
, k
))
1571 bits
= snd_pcm_format_physical_width(k
);
1573 continue; /* ignore invalid formats */
1574 if ((unsigned)bits
< i
->min
|| (unsigned)bits
> i
->max
)
1575 snd_mask_reset(&m
, k
);
1577 return snd_mask_refine(mask
, &m
);
1580 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t
*params
,
1581 snd_pcm_hw_rule_t
*rule
)
1589 for (k
= 0; k
<= SNDRV_PCM_FORMAT_LAST
; ++k
) {
1591 if (! snd_mask_test(hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
), k
))
1593 bits
= snd_pcm_format_physical_width(k
);
1595 continue; /* ignore invalid formats */
1596 if (t
.min
> (unsigned)bits
)
1598 if (t
.max
< (unsigned)bits
)
1602 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1605 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1606 #error "Change this table"
1609 static unsigned int rates
[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1610 48000, 64000, 88200, 96000, 176400, 192000 };
1612 #define RATES (sizeof(rates) / sizeof(rates[0]))
1614 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t
*params
,
1615 snd_pcm_hw_rule_t
*rule
)
1617 snd_pcm_hardware_t
*hw
= rule
->private;
1618 return snd_interval_list(hw_param_interval(params
, rule
->var
), RATES
, rates
, hw
->rates
);
1621 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t
*params
,
1622 snd_pcm_hw_rule_t
*rule
)
1625 snd_pcm_substream_t
*substream
= rule
->private;
1627 t
.max
= substream
->buffer_bytes_max
;
1631 return snd_interval_refine(hw_param_interval(params
, rule
->var
), &t
);
1634 int snd_pcm_hw_constraints_init(snd_pcm_substream_t
*substream
)
1636 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1637 snd_pcm_hw_constraints_t
*constrs
= &runtime
->hw_constraints
;
1640 for (k
= SNDRV_PCM_HW_PARAM_FIRST_MASK
; k
<= SNDRV_PCM_HW_PARAM_LAST_MASK
; k
++) {
1641 snd_mask_any(constrs_mask(constrs
, k
));
1644 for (k
= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL
; k
<= SNDRV_PCM_HW_PARAM_LAST_INTERVAL
; k
++) {
1645 snd_interval_any(constrs_interval(constrs
, k
));
1648 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_CHANNELS
));
1649 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
));
1650 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
));
1651 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
));
1652 snd_interval_setinteger(constrs_interval(constrs
, SNDRV_PCM_HW_PARAM_FRAME_BITS
));
1654 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
1655 snd_pcm_hw_rule_format
, 0,
1656 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1659 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1660 snd_pcm_hw_rule_sample_bits
, 0,
1661 SNDRV_PCM_HW_PARAM_FORMAT
,
1662 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1665 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
1666 snd_pcm_hw_rule_div
, 0,
1667 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1670 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1671 snd_pcm_hw_rule_mul
, 0,
1672 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
1675 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1676 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1677 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1680 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS
,
1681 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1682 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, -1);
1685 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
1686 snd_pcm_hw_rule_div
, 0,
1687 SNDRV_PCM_HW_PARAM_FRAME_BITS
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
, -1);
1690 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1691 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1692 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
, -1);
1695 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1696 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1697 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_BUFFER_TIME
, -1);
1700 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIODS
,
1701 snd_pcm_hw_rule_div
, 0,
1702 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, -1);
1705 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1706 snd_pcm_hw_rule_div
, 0,
1707 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1710 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1711 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1712 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1715 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
,
1716 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1717 SNDRV_PCM_HW_PARAM_PERIOD_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1720 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1721 snd_pcm_hw_rule_mul
, 0,
1722 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_PERIODS
, -1);
1725 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1726 snd_pcm_hw_rule_mulkdiv
, (void*) 8,
1727 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1730 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE
,
1731 snd_pcm_hw_rule_muldivk
, (void*) 1000000,
1732 SNDRV_PCM_HW_PARAM_BUFFER_TIME
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1735 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1736 snd_pcm_hw_rule_muldivk
, (void*) 8,
1737 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1740 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1741 snd_pcm_hw_rule_muldivk
, (void*) 8,
1742 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_FRAME_BITS
, -1);
1745 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
1746 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1747 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1750 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME
,
1751 snd_pcm_hw_rule_mulkdiv
, (void*) 1000000,
1752 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, SNDRV_PCM_HW_PARAM_RATE
, -1);
1758 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t
*substream
)
1760 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
1761 snd_pcm_hardware_t
*hw
= &runtime
->hw
;
1763 unsigned int mask
= 0;
1765 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1766 mask
|= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
1767 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1768 mask
|= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
;
1769 if (hw
->info
& SNDRV_PCM_INFO_MMAP
) {
1770 if (hw
->info
& SNDRV_PCM_INFO_INTERLEAVED
)
1771 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED
;
1772 if (hw
->info
& SNDRV_PCM_INFO_NONINTERLEAVED
)
1773 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED
;
1774 if (hw
->info
& SNDRV_PCM_INFO_COMPLEX
)
1775 mask
|= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX
;
1777 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_ACCESS
, mask
);
1778 snd_assert(err
>= 0, return -EINVAL
);
1780 err
= snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
, hw
->formats
);
1781 snd_assert(err
>= 0, return -EINVAL
);
1783 err
= snd_pcm_hw_constraint_mask(runtime
, SNDRV_PCM_HW_PARAM_SUBFORMAT
, 1 << SNDRV_PCM_SUBFORMAT_STD
);
1784 snd_assert(err
>= 0, return -EINVAL
);
1786 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
,
1787 hw
->channels_min
, hw
->channels_max
);
1788 snd_assert(err
>= 0, return -EINVAL
);
1790 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_RATE
,
1791 hw
->rate_min
, hw
->rate_max
);
1792 snd_assert(err
>= 0, return -EINVAL
);
1794 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
1795 hw
->period_bytes_min
, hw
->period_bytes_max
);
1796 snd_assert(err
>= 0, return -EINVAL
);
1798 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
,
1799 hw
->periods_min
, hw
->periods_max
);
1800 snd_assert(err
>= 0, return -EINVAL
);
1802 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1803 hw
->period_bytes_min
, hw
->buffer_bytes_max
);
1804 snd_assert(err
>= 0, return -EINVAL
);
1806 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
,
1807 snd_pcm_hw_rule_buffer_bytes_max
, substream
,
1808 SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, -1);
1813 if (runtime
->dma_bytes
) {
1814 err
= snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 0, runtime
->dma_bytes
);
1815 snd_assert(err
>= 0, return -EINVAL
);
1818 if (!(hw
->rates
& (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))) {
1819 err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
1820 snd_pcm_hw_rule_rate
, hw
,
1821 SNDRV_PCM_HW_PARAM_RATE
, -1);
1826 /* FIXME: this belong to lowlevel */
1827 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_TICK_TIME
,
1828 1000000 / HZ
, 1000000 / HZ
);
1829 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_SIZE
);
1834 static void snd_pcm_add_file(snd_pcm_str_t
*str
,
1835 snd_pcm_file_t
*pcm_file
)
1837 pcm_file
->next
= str
->files
;
1838 str
->files
= pcm_file
;
1841 static void snd_pcm_remove_file(snd_pcm_str_t
*str
,
1842 snd_pcm_file_t
*pcm_file
)
1844 snd_pcm_file_t
* pcm_file1
;
1845 if (str
->files
== pcm_file
) {
1846 str
->files
= pcm_file
->next
;
1848 pcm_file1
= str
->files
;
1849 while (pcm_file1
&& pcm_file1
->next
!= pcm_file
)
1850 pcm_file1
= pcm_file1
->next
;
1851 if (pcm_file1
!= NULL
)
1852 pcm_file1
->next
= pcm_file
->next
;
1856 static int snd_pcm_release_file(snd_pcm_file_t
* pcm_file
)
1858 snd_pcm_substream_t
*substream
;
1859 snd_pcm_runtime_t
*runtime
;
1860 snd_pcm_str_t
* str
;
1862 snd_assert(pcm_file
!= NULL
, return -ENXIO
);
1863 substream
= pcm_file
->substream
;
1864 snd_assert(substream
!= NULL
, return -ENXIO
);
1865 runtime
= substream
->runtime
;
1866 str
= substream
->pstr
;
1867 snd_pcm_unlink(substream
);
1868 if (substream
->open_flag
) {
1869 if (substream
->ops
->hw_free
!= NULL
)
1870 substream
->ops
->hw_free(substream
);
1871 substream
->ops
->close(substream
);
1872 substream
->open_flag
= 0;
1874 substream
->ffile
= NULL
;
1875 snd_pcm_remove_file(str
, pcm_file
);
1876 snd_pcm_release_substream(substream
);
1877 snd_magic_kfree(pcm_file
);
1881 static int snd_pcm_open_file(struct file
*file
,
1884 snd_pcm_file_t
**rpcm_file
)
1887 snd_pcm_file_t
*pcm_file
;
1888 snd_pcm_substream_t
*substream
;
1891 snd_assert(rpcm_file
!= NULL
, return -EINVAL
);
1894 pcm_file
= snd_magic_kcalloc(snd_pcm_file_t
, 0, GFP_KERNEL
);
1895 if (pcm_file
== NULL
) {
1899 if ((err
= snd_pcm_open_substream(pcm
, stream
, &substream
)) < 0) {
1900 snd_magic_kfree(pcm_file
);
1904 str
= substream
->pstr
;
1905 substream
->file
= pcm_file
;
1907 pcm_file
->substream
= substream
;
1909 snd_pcm_add_file(str
, pcm_file
);
1911 err
= snd_pcm_hw_constraints_init(substream
);
1913 snd_printd("snd_pcm_hw_constraints_init failed\n");
1914 snd_pcm_release_file(pcm_file
);
1918 if ((err
= substream
->ops
->open(substream
)) < 0) {
1919 snd_pcm_release_file(pcm_file
);
1922 substream
->open_flag
= 1;
1924 err
= snd_pcm_hw_constraints_complete(substream
);
1926 snd_printd("snd_pcm_hw_constraints_complete failed\n");
1927 substream
->ops
->close(substream
);
1928 snd_pcm_release_file(pcm_file
);
1932 substream
->ffile
= file
;
1934 file
->private_data
= pcm_file
;
1935 *rpcm_file
= pcm_file
;
1939 int snd_pcm_open(struct inode
*inode
, struct file
*file
)
1941 int cardnum
= SNDRV_MINOR_CARD(iminor(inode
));
1942 int device
= SNDRV_MINOR_DEVICE(iminor(inode
));
1945 snd_pcm_file_t
*pcm_file
;
1948 snd_runtime_check(device
>= SNDRV_MINOR_PCM_PLAYBACK
&& device
< SNDRV_MINOR_DEVICES
, return -ENXIO
);
1949 pcm
= snd_pcm_devices
[(cardnum
* SNDRV_PCM_DEVICES
) + (device
% SNDRV_MINOR_PCMS
)];
1954 err
= snd_card_file_add(pcm
->card
, file
);
1957 if (!try_module_get(pcm
->card
->module
)) {
1961 init_waitqueue_entry(&wait
, current
);
1962 add_wait_queue(&pcm
->open_wait
, &wait
);
1963 down(&pcm
->open_mutex
);
1965 err
= snd_pcm_open_file(file
, pcm
, device
>= SNDRV_MINOR_PCM_CAPTURE
? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
, &pcm_file
);
1968 if (err
== -EAGAIN
) {
1969 if (file
->f_flags
& O_NONBLOCK
) {
1975 set_current_state(TASK_INTERRUPTIBLE
);
1976 up(&pcm
->open_mutex
);
1978 down(&pcm
->open_mutex
);
1979 if (signal_pending(current
)) {
1984 remove_wait_queue(&pcm
->open_wait
, &wait
);
1985 up(&pcm
->open_mutex
);
1991 module_put(pcm
->card
->module
);
1993 snd_card_file_remove(pcm
->card
, file
);
1998 int snd_pcm_release(struct inode
*inode
, struct file
*file
)
2001 snd_pcm_substream_t
*substream
;
2002 snd_pcm_file_t
*pcm_file
;
2004 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2005 substream
= pcm_file
->substream
;
2006 snd_assert(substream
!= NULL
, return -ENXIO
);
2007 snd_assert(!atomic_read(&substream
->runtime
->mmap_count
), );
2008 pcm
= substream
->pcm
;
2009 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2010 snd_pcm_playback_drop(substream
);
2012 snd_pcm_capture_drop(substream
);
2013 fasync_helper(-1, file
, 0, &substream
->runtime
->fasync
);
2014 down(&pcm
->open_mutex
);
2015 snd_pcm_release_file(pcm_file
);
2016 up(&pcm
->open_mutex
);
2017 wake_up(&pcm
->open_wait
);
2018 module_put(pcm
->card
->module
);
2019 snd_card_file_remove(pcm
->card
, file
);
2023 snd_pcm_sframes_t
snd_pcm_playback_rewind(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2025 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2026 snd_pcm_sframes_t appl_ptr
;
2027 snd_pcm_sframes_t ret
;
2028 snd_pcm_sframes_t hw_avail
;
2033 snd_pcm_stream_lock_irq(substream
);
2034 switch (runtime
->status
->state
) {
2035 case SNDRV_PCM_STATE_PREPARED
:
2037 case SNDRV_PCM_STATE_DRAINING
:
2038 case SNDRV_PCM_STATE_RUNNING
:
2039 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2042 case SNDRV_PCM_STATE_XRUN
:
2050 hw_avail
= snd_pcm_playback_hw_avail(runtime
);
2051 if (hw_avail
<= 0) {
2055 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2058 frames
-= frames
% runtime
->xfer_align
;
2059 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2061 appl_ptr
+= runtime
->boundary
;
2062 runtime
->control
->appl_ptr
= appl_ptr
;
2063 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2065 snd_pcm_tick_prepare(substream
);
2068 snd_pcm_stream_unlock_irq(substream
);
2072 snd_pcm_sframes_t
snd_pcm_capture_rewind(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2074 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2075 snd_pcm_sframes_t appl_ptr
;
2076 snd_pcm_sframes_t ret
;
2077 snd_pcm_sframes_t hw_avail
;
2082 snd_pcm_stream_lock_irq(substream
);
2083 switch (runtime
->status
->state
) {
2084 case SNDRV_PCM_STATE_PREPARED
:
2085 case SNDRV_PCM_STATE_DRAINING
:
2087 case SNDRV_PCM_STATE_RUNNING
:
2088 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2091 case SNDRV_PCM_STATE_XRUN
:
2099 hw_avail
= snd_pcm_capture_hw_avail(runtime
);
2100 if (hw_avail
<= 0) {
2104 if (frames
> (snd_pcm_uframes_t
)hw_avail
)
2107 frames
-= frames
% runtime
->xfer_align
;
2108 appl_ptr
= runtime
->control
->appl_ptr
- frames
;
2110 appl_ptr
+= runtime
->boundary
;
2111 runtime
->control
->appl_ptr
= appl_ptr
;
2112 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2114 snd_pcm_tick_prepare(substream
);
2117 snd_pcm_stream_unlock_irq(substream
);
2121 snd_pcm_sframes_t
snd_pcm_playback_forward(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2123 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2124 snd_pcm_sframes_t appl_ptr
;
2125 snd_pcm_sframes_t ret
;
2126 snd_pcm_sframes_t avail
;
2131 snd_pcm_stream_lock_irq(substream
);
2132 switch (runtime
->status
->state
) {
2133 case SNDRV_PCM_STATE_PREPARED
:
2134 case SNDRV_PCM_STATE_PAUSED
:
2136 case SNDRV_PCM_STATE_DRAINING
:
2137 case SNDRV_PCM_STATE_RUNNING
:
2138 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2141 case SNDRV_PCM_STATE_XRUN
:
2149 avail
= snd_pcm_playback_avail(runtime
);
2154 if (frames
> (snd_pcm_uframes_t
)avail
)
2157 frames
-= frames
% runtime
->xfer_align
;
2158 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2159 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2160 appl_ptr
-= runtime
->boundary
;
2161 runtime
->control
->appl_ptr
= appl_ptr
;
2162 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2164 snd_pcm_tick_prepare(substream
);
2167 snd_pcm_stream_unlock_irq(substream
);
2171 snd_pcm_sframes_t
snd_pcm_capture_forward(snd_pcm_substream_t
*substream
, snd_pcm_uframes_t frames
)
2173 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2174 snd_pcm_sframes_t appl_ptr
;
2175 snd_pcm_sframes_t ret
;
2176 snd_pcm_sframes_t avail
;
2181 snd_pcm_stream_lock_irq(substream
);
2182 switch (runtime
->status
->state
) {
2183 case SNDRV_PCM_STATE_PREPARED
:
2184 case SNDRV_PCM_STATE_DRAINING
:
2185 case SNDRV_PCM_STATE_PAUSED
:
2187 case SNDRV_PCM_STATE_RUNNING
:
2188 if (snd_pcm_update_hw_ptr(substream
) >= 0)
2191 case SNDRV_PCM_STATE_XRUN
:
2199 avail
= snd_pcm_capture_avail(runtime
);
2204 if (frames
> (snd_pcm_uframes_t
)avail
)
2207 frames
-= frames
% runtime
->xfer_align
;
2208 appl_ptr
= runtime
->control
->appl_ptr
+ frames
;
2209 if (appl_ptr
>= (snd_pcm_sframes_t
)runtime
->boundary
)
2210 appl_ptr
-= runtime
->boundary
;
2211 runtime
->control
->appl_ptr
= appl_ptr
;
2212 if (runtime
->status
->state
== SNDRV_PCM_STATE_RUNNING
&&
2214 snd_pcm_tick_prepare(substream
);
2217 snd_pcm_stream_unlock_irq(substream
);
2221 static int snd_pcm_hwsync(snd_pcm_substream_t
*substream
)
2223 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2226 snd_pcm_stream_lock_irq(substream
);
2227 switch (runtime
->status
->state
) {
2228 case SNDRV_PCM_STATE_DRAINING
:
2229 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2231 case SNDRV_PCM_STATE_RUNNING
:
2232 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2235 case SNDRV_PCM_STATE_PREPARED
:
2236 case SNDRV_PCM_STATE_SUSPENDED
:
2239 case SNDRV_PCM_STATE_XRUN
:
2247 snd_pcm_stream_unlock_irq(substream
);
2251 static int snd_pcm_delay(snd_pcm_substream_t
*substream
, snd_pcm_sframes_t
*res
)
2253 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2255 snd_pcm_sframes_t n
= 0;
2257 snd_pcm_stream_lock_irq(substream
);
2258 switch (runtime
->status
->state
) {
2259 case SNDRV_PCM_STATE_DRAINING
:
2260 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
2262 case SNDRV_PCM_STATE_RUNNING
:
2263 if ((err
= snd_pcm_update_hw_ptr(substream
)) < 0)
2266 case SNDRV_PCM_STATE_PREPARED
:
2267 case SNDRV_PCM_STATE_SUSPENDED
:
2269 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
2270 n
= snd_pcm_playback_hw_avail(runtime
);
2272 n
= snd_pcm_capture_avail(runtime
);
2274 case SNDRV_PCM_STATE_XRUN
:
2282 snd_pcm_stream_unlock_irq(substream
);
2284 if (put_user(n
, res
))
2289 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t
*substream
,
2290 unsigned int cmd
, void *arg
);
2291 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t
*substream
,
2292 unsigned int cmd
, void *arg
);
2294 static int snd_pcm_common_ioctl1(snd_pcm_substream_t
*substream
,
2295 unsigned int cmd
, void *arg
)
2297 snd_assert(substream
!= NULL
, return -ENXIO
);
2300 case SNDRV_PCM_IOCTL_PVERSION
:
2301 return put_user(SNDRV_PCM_VERSION
, (int *)arg
) ? -EFAULT
: 0;
2302 case SNDRV_PCM_IOCTL_INFO
:
2303 return snd_pcm_info_user(substream
, (snd_pcm_info_t
*) arg
);
2304 case SNDRV_PCM_IOCTL_TSTAMP
:
2307 if (get_user(xarg
, (int *) arg
))
2309 substream
->runtime
->tstamp_timespec
= xarg
? 1 : 0;
2312 case SNDRV_PCM_IOCTL_HW_REFINE
:
2313 return snd_pcm_hw_refine_user(substream
, (snd_pcm_hw_params_t
*) arg
);
2314 case SNDRV_PCM_IOCTL_HW_PARAMS
:
2315 return snd_pcm_hw_params_user(substream
, (snd_pcm_hw_params_t
*) arg
);
2316 case SNDRV_PCM_IOCTL_HW_FREE
:
2317 return snd_pcm_hw_free(substream
);
2318 case SNDRV_PCM_IOCTL_SW_PARAMS
:
2319 return snd_pcm_sw_params_user(substream
, (snd_pcm_sw_params_t
*) arg
);
2320 case SNDRV_PCM_IOCTL_STATUS
:
2321 return snd_pcm_status_user(substream
, (snd_pcm_status_t
*) arg
);
2322 case SNDRV_PCM_IOCTL_CHANNEL_INFO
:
2323 return snd_pcm_channel_info(substream
, (snd_pcm_channel_info_t
*) arg
);
2324 case SNDRV_PCM_IOCTL_PREPARE
:
2325 return snd_pcm_prepare(substream
);
2326 case SNDRV_PCM_IOCTL_RESET
:
2327 return snd_pcm_reset(substream
);
2328 case SNDRV_PCM_IOCTL_START
:
2329 return snd_pcm_action(&snd_pcm_action_start
, substream
, 0);
2330 case SNDRV_PCM_IOCTL_LINK
:
2331 return snd_pcm_link(substream
, (long) arg
);
2332 case SNDRV_PCM_IOCTL_UNLINK
:
2333 return snd_pcm_unlink(substream
);
2334 case SNDRV_PCM_IOCTL_RESUME
:
2335 return snd_pcm_resume(substream
);
2336 case SNDRV_PCM_IOCTL_XRUN
:
2337 return snd_pcm_xrun(substream
);
2338 case SNDRV_PCM_IOCTL_HWSYNC
:
2339 return snd_pcm_hwsync(substream
);
2340 case SNDRV_PCM_IOCTL_DELAY
:
2341 return snd_pcm_delay(substream
, (snd_pcm_sframes_t
*) arg
);
2342 case SNDRV_PCM_IOCTL_HW_REFINE_OLD
:
2343 return snd_pcm_hw_refine_old_user(substream
, (struct sndrv_pcm_hw_params_old
*) arg
);
2344 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD
:
2345 return snd_pcm_hw_params_old_user(substream
, (struct sndrv_pcm_hw_params_old
*) arg
);
2347 snd_printd("unknown ioctl = 0x%x\n", cmd
);
2351 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t
*substream
,
2352 unsigned int cmd
, void *arg
)
2354 snd_assert(substream
!= NULL
, return -ENXIO
);
2355 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
, return -EINVAL
);
2357 case SNDRV_PCM_IOCTL_WRITEI_FRAMES
:
2359 snd_xferi_t xferi
, *_xferi
= arg
;
2360 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2361 snd_pcm_sframes_t result
;
2362 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2364 if (put_user(0, &_xferi
->result
))
2366 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2368 result
= snd_pcm_lib_write(substream
, xferi
.buf
, xferi
.frames
);
2369 __put_user(result
, &_xferi
->result
);
2370 return result
< 0 ? result
: 0;
2372 case SNDRV_PCM_IOCTL_WRITEN_FRAMES
:
2374 snd_xfern_t xfern
, *_xfern
= arg
;
2375 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2377 snd_pcm_sframes_t result
;
2378 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2380 if (runtime
->channels
> 128)
2382 if (put_user(0, &_xfern
->result
))
2384 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2386 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2389 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2393 result
= snd_pcm_lib_writev(substream
, bufs
, xfern
.frames
);
2395 __put_user(result
, &_xfern
->result
);
2396 return result
< 0 ? result
: 0;
2398 case SNDRV_PCM_IOCTL_REWIND
:
2400 snd_pcm_uframes_t frames
, *_frames
= arg
;
2401 snd_pcm_sframes_t result
;
2402 if (get_user(frames
, _frames
))
2404 if (put_user(0, _frames
))
2406 result
= snd_pcm_playback_rewind(substream
, frames
);
2407 __put_user(result
, _frames
);
2408 return result
< 0 ? result
: 0;
2410 case SNDRV_PCM_IOCTL_FORWARD
:
2412 snd_pcm_uframes_t frames
, *_frames
= arg
;
2413 snd_pcm_sframes_t result
;
2414 if (get_user(frames
, _frames
))
2416 if (put_user(0, _frames
))
2418 result
= snd_pcm_playback_forward(substream
, frames
);
2419 __put_user(result
, _frames
);
2420 return result
< 0 ? result
: 0;
2422 case SNDRV_PCM_IOCTL_PAUSE
:
2425 snd_pcm_stream_lock_irq(substream
);
2426 res
= snd_pcm_pause(substream
, (long) arg
);
2427 snd_pcm_stream_unlock_irq(substream
);
2430 case SNDRV_PCM_IOCTL_DRAIN
:
2431 return snd_pcm_playback_drain(substream
);
2432 case SNDRV_PCM_IOCTL_DROP
:
2433 return snd_pcm_playback_drop(substream
);
2435 return snd_pcm_common_ioctl1(substream
, cmd
, arg
);
2438 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t
*substream
,
2439 unsigned int cmd
, void *arg
)
2441 snd_assert(substream
!= NULL
, return -ENXIO
);
2442 snd_assert(substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
, return -EINVAL
);
2444 case SNDRV_PCM_IOCTL_READI_FRAMES
:
2446 snd_xferi_t xferi
, *_xferi
= arg
;
2447 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2448 snd_pcm_sframes_t result
;
2449 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2451 if (put_user(0, &_xferi
->result
))
2453 if (copy_from_user(&xferi
, _xferi
, sizeof(xferi
)))
2455 result
= snd_pcm_lib_read(substream
, xferi
.buf
, xferi
.frames
);
2456 __put_user(result
, &_xferi
->result
);
2457 return result
< 0 ? result
: 0;
2459 case SNDRV_PCM_IOCTL_READN_FRAMES
:
2461 snd_xfern_t xfern
, *_xfern
= arg
;
2462 snd_pcm_runtime_t
*runtime
= substream
->runtime
;
2464 snd_pcm_sframes_t result
;
2465 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2467 if (runtime
->channels
> 128)
2469 if (put_user(0, &_xfern
->result
))
2471 if (copy_from_user(&xfern
, _xfern
, sizeof(xfern
)))
2473 bufs
= kmalloc(sizeof(void *) * runtime
->channels
, GFP_KERNEL
);
2476 if (copy_from_user(bufs
, xfern
.bufs
, sizeof(void *) * runtime
->channels
)) {
2480 result
= snd_pcm_lib_readv(substream
, bufs
, xfern
.frames
);
2482 __put_user(result
, &_xfern
->result
);
2483 return result
< 0 ? result
: 0;
2485 case SNDRV_PCM_IOCTL_REWIND
:
2487 snd_pcm_uframes_t frames
, *_frames
= arg
;
2488 snd_pcm_sframes_t result
;
2489 if (get_user(frames
, _frames
))
2491 if (put_user(0, _frames
))
2493 result
= snd_pcm_capture_rewind(substream
, frames
);
2494 __put_user(result
, _frames
);
2495 return result
< 0 ? result
: 0;
2497 case SNDRV_PCM_IOCTL_FORWARD
:
2499 snd_pcm_uframes_t frames
, *_frames
= arg
;
2500 snd_pcm_sframes_t result
;
2501 if (get_user(frames
, _frames
))
2503 if (put_user(0, _frames
))
2505 result
= snd_pcm_capture_forward(substream
, frames
);
2506 __put_user(result
, _frames
);
2507 return result
< 0 ? result
: 0;
2509 case SNDRV_PCM_IOCTL_DRAIN
:
2510 return snd_pcm_capture_drain(substream
);
2511 case SNDRV_PCM_IOCTL_DROP
:
2512 return snd_pcm_capture_drop(substream
);
2514 return snd_pcm_common_ioctl1(substream
, cmd
, arg
);
2517 static int snd_pcm_playback_ioctl(struct inode
*inode
, struct file
*file
,
2518 unsigned int cmd
, unsigned long arg
)
2520 snd_pcm_file_t
*pcm_file
;
2522 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2524 if (((cmd
>> 8) & 0xff) != 'A')
2527 return snd_pcm_playback_ioctl1(pcm_file
->substream
, cmd
, (void *) arg
);
2530 static int snd_pcm_capture_ioctl(struct inode
*inode
, struct file
*file
,
2531 unsigned int cmd
, unsigned long arg
)
2533 snd_pcm_file_t
*pcm_file
;
2535 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2537 if (((cmd
>> 8) & 0xff) != 'A')
2540 return snd_pcm_capture_ioctl1(pcm_file
->substream
, cmd
, (void *) arg
);
2543 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t
*substream
,
2544 unsigned int cmd
, void *arg
)
2549 fs
= snd_enter_user();
2550 result
= snd_pcm_playback_ioctl1(substream
, cmd
, arg
);
2555 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t
*substream
,
2556 unsigned int cmd
, void *arg
)
2561 fs
= snd_enter_user();
2562 result
= snd_pcm_capture_ioctl1(substream
, cmd
, arg
);
2567 int snd_pcm_kernel_ioctl(snd_pcm_substream_t
*substream
,
2568 unsigned int cmd
, void *arg
)
2570 switch (substream
->stream
) {
2571 case SNDRV_PCM_STREAM_PLAYBACK
:
2572 return snd_pcm_kernel_playback_ioctl(substream
, cmd
, arg
);
2573 case SNDRV_PCM_STREAM_CAPTURE
:
2574 return snd_pcm_kernel_capture_ioctl(substream
, cmd
, arg
);
2580 static ssize_t
snd_pcm_read(struct file
*file
, char *buf
, size_t count
, loff_t
* offset
)
2582 snd_pcm_file_t
*pcm_file
;
2583 snd_pcm_substream_t
*substream
;
2584 snd_pcm_runtime_t
*runtime
;
2585 snd_pcm_sframes_t result
;
2587 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2588 substream
= pcm_file
->substream
;
2589 snd_assert(substream
!= NULL
, return -ENXIO
);
2590 runtime
= substream
->runtime
;
2591 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2593 if (!frame_aligned(runtime
, count
))
2595 count
= bytes_to_frames(runtime
, count
);
2596 result
= snd_pcm_lib_read(substream
, buf
, count
);
2598 result
= frames_to_bytes(runtime
, result
);
2602 static ssize_t
snd_pcm_write(struct file
*file
, const char *buf
, size_t count
, loff_t
* offset
)
2604 snd_pcm_file_t
*pcm_file
;
2605 snd_pcm_substream_t
*substream
;
2606 snd_pcm_runtime_t
*runtime
;
2607 snd_pcm_sframes_t result
;
2609 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, result
= -ENXIO
; goto end
);
2610 substream
= pcm_file
->substream
;
2611 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2612 runtime
= substream
->runtime
;
2613 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2617 if (!frame_aligned(runtime
, count
)) {
2621 count
= bytes_to_frames(runtime
, count
);
2622 result
= snd_pcm_lib_write(substream
, buf
, count
);
2624 result
= frames_to_bytes(runtime
, result
);
2629 static ssize_t
snd_pcm_readv(struct file
*file
, const struct iovec
*_vector
,
2630 unsigned long count
, loff_t
* offset
)
2633 snd_pcm_file_t
*pcm_file
;
2634 snd_pcm_substream_t
*substream
;
2635 snd_pcm_runtime_t
*runtime
;
2636 snd_pcm_sframes_t result
;
2639 snd_pcm_uframes_t frames
;
2641 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2642 substream
= pcm_file
->substream
;
2643 snd_assert(substream
!= NULL
, return -ENXIO
);
2644 runtime
= substream
->runtime
;
2645 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2647 if (count
> 1024 || count
!= runtime
->channels
)
2649 if (!frame_aligned(runtime
, _vector
->iov_len
))
2651 frames
= bytes_to_samples(runtime
, _vector
->iov_len
);
2652 bufs
= kmalloc(sizeof(void *) * count
, GFP_KERNEL
);
2655 for (i
= 0; i
< count
; ++i
)
2656 bufs
[i
] = _vector
[i
].iov_base
;
2657 result
= snd_pcm_lib_readv(substream
, bufs
, frames
);
2659 result
= frames_to_bytes(runtime
, result
);
2664 static ssize_t
snd_pcm_writev(struct file
*file
, const struct iovec
*_vector
,
2665 unsigned long count
, loff_t
* offset
)
2667 snd_pcm_file_t
*pcm_file
;
2668 snd_pcm_substream_t
*substream
;
2669 snd_pcm_runtime_t
*runtime
;
2670 snd_pcm_sframes_t result
;
2673 snd_pcm_uframes_t frames
;
2675 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, result
= -ENXIO
; goto end
);
2676 substream
= pcm_file
->substream
;
2677 snd_assert(substream
!= NULL
, result
= -ENXIO
; goto end
);
2678 runtime
= substream
->runtime
;
2679 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
) {
2683 if (count
> 128 || count
!= runtime
->channels
||
2684 !frame_aligned(runtime
, _vector
->iov_len
)) {
2688 frames
= bytes_to_samples(runtime
, _vector
->iov_len
);
2689 bufs
= kmalloc(sizeof(void *) * count
, GFP_KERNEL
);
2692 for (i
= 0; i
< count
; ++i
)
2693 bufs
[i
] = _vector
[i
].iov_base
;
2694 result
= snd_pcm_lib_writev(substream
, bufs
, frames
);
2696 result
= frames_to_bytes(runtime
, result
);
2702 unsigned int snd_pcm_playback_poll(struct file
*file
, poll_table
* wait
)
2704 snd_pcm_file_t
*pcm_file
;
2705 snd_pcm_substream_t
*substream
;
2706 snd_pcm_runtime_t
*runtime
;
2708 snd_pcm_uframes_t avail
;
2710 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return 0);
2712 substream
= pcm_file
->substream
;
2713 snd_assert(substream
!= NULL
, return -ENXIO
);
2714 runtime
= substream
->runtime
;
2716 poll_wait(file
, &runtime
->sleep
, wait
);
2718 snd_pcm_stream_lock_irq(substream
);
2719 avail
= snd_pcm_playback_avail(runtime
);
2720 switch (runtime
->status
->state
) {
2721 case SNDRV_PCM_STATE_RUNNING
:
2722 case SNDRV_PCM_STATE_PREPARED
:
2723 case SNDRV_PCM_STATE_PAUSED
:
2724 if (avail
>= runtime
->control
->avail_min
) {
2725 mask
= POLLOUT
| POLLWRNORM
;
2729 case SNDRV_PCM_STATE_DRAINING
:
2733 mask
= POLLOUT
| POLLWRNORM
| POLLERR
;
2736 snd_pcm_stream_unlock_irq(substream
);
2740 unsigned int snd_pcm_capture_poll(struct file
*file
, poll_table
* wait
)
2742 snd_pcm_file_t
*pcm_file
;
2743 snd_pcm_substream_t
*substream
;
2744 snd_pcm_runtime_t
*runtime
;
2746 snd_pcm_uframes_t avail
;
2748 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return 0);
2750 substream
= pcm_file
->substream
;
2751 snd_assert(substream
!= NULL
, return -ENXIO
);
2752 runtime
= substream
->runtime
;
2754 poll_wait(file
, &runtime
->sleep
, wait
);
2756 snd_pcm_stream_lock_irq(substream
);
2757 avail
= snd_pcm_capture_avail(runtime
);
2758 switch (runtime
->status
->state
) {
2759 case SNDRV_PCM_STATE_RUNNING
:
2760 case SNDRV_PCM_STATE_PREPARED
:
2761 case SNDRV_PCM_STATE_PAUSED
:
2762 if (avail
>= runtime
->control
->avail_min
) {
2763 mask
= POLLIN
| POLLRDNORM
;
2768 case SNDRV_PCM_STATE_DRAINING
:
2770 mask
= POLLIN
| POLLRDNORM
;
2775 mask
= POLLIN
| POLLRDNORM
| POLLERR
;
2778 snd_pcm_stream_unlock_irq(substream
);
2782 static struct page
* snd_pcm_mmap_status_nopage(struct vm_area_struct
*area
, unsigned long address
, int *type
)
2784 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2785 snd_pcm_runtime_t
*runtime
;
2788 if (substream
== NULL
)
2790 runtime
= substream
->runtime
;
2791 page
= virt_to_page(runtime
->status
);
2792 if (!PageReserved(page
))
2795 *type
= VM_FAULT_MINOR
;
2799 static struct vm_operations_struct snd_pcm_vm_ops_status
=
2801 .nopage
= snd_pcm_mmap_status_nopage
,
2804 int snd_pcm_mmap_status(snd_pcm_substream_t
*substream
, struct file
*file
,
2805 struct vm_area_struct
*area
)
2807 snd_pcm_runtime_t
*runtime
;
2809 if (!(area
->vm_flags
& VM_READ
))
2811 runtime
= substream
->runtime
;
2812 snd_assert(runtime
!= NULL
, return -EAGAIN
);
2813 size
= area
->vm_end
- area
->vm_start
;
2814 if (size
!= PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t
)))
2816 area
->vm_ops
= &snd_pcm_vm_ops_status
;
2817 area
->vm_private_data
= substream
;
2818 area
->vm_flags
|= VM_RESERVED
;
2822 static struct page
* snd_pcm_mmap_control_nopage(struct vm_area_struct
*area
, unsigned long address
, int *type
)
2824 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2825 snd_pcm_runtime_t
*runtime
;
2828 if (substream
== NULL
)
2830 runtime
= substream
->runtime
;
2831 page
= virt_to_page(runtime
->control
);
2832 if (!PageReserved(page
))
2835 *type
= VM_FAULT_MINOR
;
2839 static struct vm_operations_struct snd_pcm_vm_ops_control
=
2841 .nopage
= snd_pcm_mmap_control_nopage
,
2844 static int snd_pcm_mmap_control(snd_pcm_substream_t
*substream
, struct file
*file
,
2845 struct vm_area_struct
*area
)
2847 snd_pcm_runtime_t
*runtime
;
2849 if (!(area
->vm_flags
& VM_READ
))
2851 runtime
= substream
->runtime
;
2852 snd_assert(runtime
!= NULL
, return -EAGAIN
);
2853 size
= area
->vm_end
- area
->vm_start
;
2854 if (size
!= PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t
)))
2856 area
->vm_ops
= &snd_pcm_vm_ops_control
;
2857 area
->vm_private_data
= substream
;
2858 area
->vm_flags
|= VM_RESERVED
;
2862 static void snd_pcm_mmap_data_open(struct vm_area_struct
*area
)
2864 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2865 atomic_inc(&substream
->runtime
->mmap_count
);
2868 static void snd_pcm_mmap_data_close(struct vm_area_struct
*area
)
2870 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2871 atomic_dec(&substream
->runtime
->mmap_count
);
2874 static struct page
* snd_pcm_mmap_data_nopage(struct vm_area_struct
*area
, unsigned long address
, int *type
)
2876 snd_pcm_substream_t
*substream
= (snd_pcm_substream_t
*)area
->vm_private_data
;
2877 snd_pcm_runtime_t
*runtime
;
2878 unsigned long offset
;
2883 if (substream
== NULL
)
2885 runtime
= substream
->runtime
;
2886 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
2887 offset
+= address
- area
->vm_start
;
2888 snd_assert((offset
% PAGE_SIZE
) == 0, return NOPAGE_OOM
);
2889 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
2890 if (offset
> dma_bytes
- PAGE_SIZE
)
2891 return NOPAGE_SIGBUS
;
2892 if (substream
->ops
->page
) {
2893 page
= substream
->ops
->page(substream
, offset
);
2897 vaddr
= runtime
->dma_area
+ offset
;
2898 page
= virt_to_page(vaddr
);
2900 if (!PageReserved(page
))
2903 *type
= VM_FAULT_MINOR
;
2907 static struct vm_operations_struct snd_pcm_vm_ops_data
=
2909 .open
= snd_pcm_mmap_data_open
,
2910 .close
= snd_pcm_mmap_data_close
,
2911 .nopage
= snd_pcm_mmap_data_nopage
,
2914 int snd_pcm_mmap_data(snd_pcm_substream_t
*substream
, struct file
*file
,
2915 struct vm_area_struct
*area
)
2917 snd_pcm_runtime_t
*runtime
;
2919 unsigned long offset
;
2922 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
2923 if (!(area
->vm_flags
& (VM_WRITE
|VM_READ
)))
2926 if (!(area
->vm_flags
& VM_READ
))
2929 runtime
= substream
->runtime
;
2930 snd_assert(runtime
!= NULL
, return -EAGAIN
);
2931 if (runtime
->status
->state
== SNDRV_PCM_STATE_OPEN
)
2933 if (!(runtime
->info
& SNDRV_PCM_INFO_MMAP
))
2935 if (runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
2936 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
)
2938 size
= area
->vm_end
- area
->vm_start
;
2939 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
2940 dma_bytes
= PAGE_ALIGN(runtime
->dma_bytes
);
2941 if ((size_t)size
> dma_bytes
)
2943 if (offset
> dma_bytes
- size
)
2946 area
->vm_ops
= &snd_pcm_vm_ops_data
;
2947 area
->vm_private_data
= substream
;
2948 area
->vm_flags
|= VM_RESERVED
;
2949 atomic_inc(&runtime
->mmap_count
);
2953 static int snd_pcm_mmap(struct file
*file
, struct vm_area_struct
*area
)
2955 snd_pcm_file_t
* pcm_file
;
2956 snd_pcm_substream_t
*substream
;
2957 unsigned long offset
;
2959 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2960 substream
= pcm_file
->substream
;
2961 snd_assert(substream
!= NULL
, return -ENXIO
);
2963 offset
= area
->vm_pgoff
<< PAGE_SHIFT
;
2965 case SNDRV_PCM_MMAP_OFFSET_STATUS
:
2966 return snd_pcm_mmap_status(substream
, file
, area
);
2967 case SNDRV_PCM_MMAP_OFFSET_CONTROL
:
2968 return snd_pcm_mmap_control(substream
, file
, area
);
2970 return snd_pcm_mmap_data(substream
, file
, area
);
2975 static int snd_pcm_fasync(int fd
, struct file
* file
, int on
)
2977 snd_pcm_file_t
* pcm_file
;
2978 snd_pcm_substream_t
*substream
;
2979 snd_pcm_runtime_t
*runtime
;
2982 pcm_file
= snd_magic_cast(snd_pcm_file_t
, file
->private_data
, return -ENXIO
);
2983 substream
= pcm_file
->substream
;
2984 snd_assert(substream
!= NULL
, return -ENXIO
);
2985 runtime
= substream
->runtime
;
2987 err
= fasync_helper(fd
, file
, on
, &runtime
->fasync
);
2994 * To be removed helpers to keep binary compatibility
2997 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
2998 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3000 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t
*params
, struct sndrv_pcm_hw_params_old
*oparams
)
3004 memset(params
, 0, sizeof(*params
));
3005 params
->flags
= oparams
->flags
;
3006 for (i
= 0; i
< sizeof(oparams
->masks
) / sizeof(unsigned int); i
++)
3007 params
->masks
[i
].bits
[0] = oparams
->masks
[i
];
3008 memcpy(params
->intervals
, oparams
->intervals
, sizeof(oparams
->intervals
));
3009 params
->rmask
= __OLD_TO_NEW_MASK(oparams
->rmask
);
3010 params
->cmask
= __OLD_TO_NEW_MASK(oparams
->cmask
);
3011 params
->info
= oparams
->info
;
3012 params
->msbits
= oparams
->msbits
;
3013 params
->rate_num
= oparams
->rate_num
;
3014 params
->rate_den
= oparams
->rate_den
;
3015 params
->fifo_size
= oparams
->fifo_size
;
3018 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old
*oparams
, snd_pcm_hw_params_t
*params
)
3022 memset(oparams
, 0, sizeof(*oparams
));
3023 oparams
->flags
= params
->flags
;
3024 for (i
= 0; i
< sizeof(oparams
->masks
) / sizeof(unsigned int); i
++)
3025 oparams
->masks
[i
] = params
->masks
[i
].bits
[0];
3026 memcpy(oparams
->intervals
, params
->intervals
, sizeof(oparams
->intervals
));
3027 oparams
->rmask
= __NEW_TO_OLD_MASK(params
->rmask
);
3028 oparams
->cmask
= __NEW_TO_OLD_MASK(params
->cmask
);
3029 oparams
->info
= params
->info
;
3030 oparams
->msbits
= params
->msbits
;
3031 oparams
->rate_num
= params
->rate_num
;
3032 oparams
->rate_den
= params
->rate_den
;
3033 oparams
->fifo_size
= params
->fifo_size
;
3036 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old
* _oparams
)
3038 snd_pcm_hw_params_t params
;
3039 struct sndrv_pcm_hw_params_old oparams
;
3041 if (copy_from_user(&oparams
, _oparams
, sizeof(oparams
)))
3043 snd_pcm_hw_convert_from_old_params(¶ms
, &oparams
);
3044 err
= snd_pcm_hw_refine(substream
, ¶ms
);
3045 snd_pcm_hw_convert_to_old_params(&oparams
, ¶ms
);
3046 if (copy_to_user(_oparams
, &oparams
, sizeof(oparams
)))
3051 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t
* substream
, struct sndrv_pcm_hw_params_old
* _oparams
)
3053 snd_pcm_hw_params_t params
;
3054 struct sndrv_pcm_hw_params_old oparams
;
3056 if (copy_from_user(&oparams
, _oparams
, sizeof(oparams
)))
3058 snd_pcm_hw_convert_from_old_params(¶ms
, &oparams
);
3059 err
= snd_pcm_hw_params(substream
, ¶ms
);
3060 snd_pcm_hw_convert_to_old_params(&oparams
, ¶ms
);
3061 if (copy_to_user(_oparams
, &oparams
, sizeof(oparams
)))
3070 static struct file_operations snd_pcm_f_ops_playback
= {
3071 .owner
= THIS_MODULE
,
3072 .write
= snd_pcm_write
,
3073 .writev
= snd_pcm_writev
,
3074 .open
= snd_pcm_open
,
3075 .release
= snd_pcm_release
,
3076 .poll
= snd_pcm_playback_poll
,
3077 .ioctl
= snd_pcm_playback_ioctl
,
3078 .mmap
= snd_pcm_mmap
,
3079 .fasync
= snd_pcm_fasync
,
3082 static struct file_operations snd_pcm_f_ops_capture
= {
3083 .owner
= THIS_MODULE
,
3084 .read
= snd_pcm_read
,
3085 .readv
= snd_pcm_readv
,
3086 .open
= snd_pcm_open
,
3087 .release
= snd_pcm_release
,
3088 .poll
= snd_pcm_capture_poll
,
3089 .ioctl
= snd_pcm_capture_ioctl
,
3090 .mmap
= snd_pcm_mmap
,
3091 .fasync
= snd_pcm_fasync
,
3094 snd_minor_t snd_pcm_reg
[2] =
3097 .comment
= "digital audio playback",
3098 .f_ops
= &snd_pcm_f_ops_playback
,
3101 .comment
= "digital audio capture",
3102 .f_ops
= &snd_pcm_f_ops_capture
,