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/smp_lock.h>
25 #include <linux/file.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/uio.h>
29 #include <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
42 struct sndrv_pcm_hw_params_old {
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
46 struct sndrv_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rate_num;
53 unsigned int rate_den;
54 sndrv_pcm_uframes_t fifo_size;
55 unsigned char reserved[64];
58 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct sndrv_pcm_hw_params_old)
59 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct sndrv_pcm_hw_params_old)
61 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams);
62 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams);
68 DEFINE_RWLOCK(snd_pcm_link_rwlock);
69 static DECLARE_RWSEM(snd_pcm_link_rwsem);
72 static inline mm_segment_t snd_enter_user(void)
74 mm_segment_t fs = get_fs();
79 static inline void snd_leave_user(mm_segment_t fs)
86 int snd_pcm_info(snd_pcm_substream_t * substream, snd_pcm_info_t *info)
88 snd_pcm_runtime_t * runtime;
89 snd_pcm_t *pcm = substream->pcm;
90 snd_pcm_str_t *pstr = substream->pstr;
92 snd_assert(substream != NULL, return -ENXIO);
93 memset(info, 0, sizeof(*info));
94 info->card = pcm->card->number;
95 info->device = pcm->device;
96 info->stream = substream->stream;
97 info->subdevice = substream->number;
98 strlcpy(info->id, pcm->id, sizeof(info->id));
99 strlcpy(info->name, pcm->name, sizeof(info->name));
100 info->dev_class = pcm->dev_class;
101 info->dev_subclass = pcm->dev_subclass;
102 info->subdevices_count = pstr->substream_count;
103 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
104 strlcpy(info->subname, substream->name, sizeof(info->subname));
105 runtime = substream->runtime;
106 /* AB: FIXME!!! This is definitely nonsense */
108 info->sync = runtime->sync;
109 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
114 int snd_pcm_info_user(snd_pcm_substream_t * substream, snd_pcm_info_t __user * _info)
116 snd_pcm_info_t *info;
119 info = kmalloc(sizeof(*info), GFP_KERNEL);
122 err = snd_pcm_info(substream, info);
124 if (copy_to_user(_info, info, sizeof(*info)))
134 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
135 char *snd_pcm_hw_param_names[] = {
139 HW_PARAM(SAMPLE_BITS),
140 HW_PARAM(FRAME_BITS),
143 HW_PARAM(PERIOD_TIME),
144 HW_PARAM(PERIOD_SIZE),
145 HW_PARAM(PERIOD_BYTES),
147 HW_PARAM(BUFFER_TIME),
148 HW_PARAM(BUFFER_SIZE),
149 HW_PARAM(BUFFER_BYTES),
154 int snd_pcm_hw_refine(snd_pcm_substream_t *substream,
155 snd_pcm_hw_params_t *params)
158 snd_pcm_hardware_t *hw;
159 snd_interval_t *i = NULL;
160 snd_mask_t *m = NULL;
161 snd_pcm_hw_constraints_t *constrs = &substream->runtime->hw_constraints;
162 unsigned int rstamps[constrs->rules_num];
163 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
164 unsigned int stamp = 2;
168 params->fifo_size = 0;
169 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
171 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
172 params->rate_num = 0;
173 params->rate_den = 0;
176 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
177 m = hw_param_mask(params, k);
178 if (snd_mask_empty(m))
180 if (!(params->rmask & (1 << k)))
183 printk("%s = ", snd_pcm_hw_param_names[k]);
184 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
186 changed = snd_mask_refine(m, constrs_mask(constrs, k));
188 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
191 params->cmask |= 1 << k;
196 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
197 i = hw_param_interval(params, k);
198 if (snd_interval_empty(i))
200 if (!(params->rmask & (1 << k)))
203 printk("%s = ", snd_pcm_hw_param_names[k]);
208 i->openmin ? '(' : '[', i->min,
209 i->max, i->openmax ? ')' : ']');
212 changed = snd_interval_refine(i, constrs_interval(constrs, k));
217 printk("%c%u %u%c\n",
218 i->openmin ? '(' : '[', i->min,
219 i->max, i->openmax ? ')' : ']');
222 params->cmask |= 1 << k;
227 for (k = 0; k < constrs->rules_num; k++)
229 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
230 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
233 for (k = 0; k < constrs->rules_num; k++) {
234 snd_pcm_hw_rule_t *r = &constrs->rules[k];
237 if (r->cond && !(r->cond & params->flags))
239 for (d = 0; r->deps[d] >= 0; d++) {
240 if (vstamps[r->deps[d]] > rstamps[k]) {
248 printk("Rule %d [%p]: ", k, r->func);
250 printk("%s = ", snd_pcm_hw_param_names[r->var]);
251 if (hw_is_mask(r->var)) {
252 m = hw_param_mask(params, r->var);
253 printk("%x", *m->bits);
255 i = hw_param_interval(params, r->var);
260 i->openmin ? '(' : '[', i->min,
261 i->max, i->openmax ? ')' : ']');
265 changed = r->func(params, r);
269 if (hw_is_mask(r->var))
270 printk("%x", *m->bits);
276 i->openmin ? '(' : '[', i->min,
277 i->max, i->openmax ? ')' : ']');
283 if (changed && r->var >= 0) {
284 params->cmask |= (1 << r->var);
285 vstamps[r->var] = stamp;
293 if (!params->msbits) {
294 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
295 if (snd_interval_single(i))
296 params->msbits = snd_interval_value(i);
299 if (!params->rate_den) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
301 if (snd_interval_single(i)) {
302 params->rate_num = snd_interval_value(i);
303 params->rate_den = 1;
307 hw = &substream->runtime->hw;
309 params->info = hw->info;
310 if (!params->fifo_size)
311 params->fifo_size = hw->fifo_size;
316 static int snd_pcm_hw_refine_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t __user * _params)
318 snd_pcm_hw_params_t *params;
321 params = kmalloc(sizeof(*params), GFP_KERNEL);
326 if (copy_from_user(params, _params, sizeof(*params))) {
330 err = snd_pcm_hw_refine(substream, params);
331 if (copy_to_user(_params, params, sizeof(*params))) {
340 static int snd_pcm_hw_params(snd_pcm_substream_t *substream,
341 snd_pcm_hw_params_t *params)
343 snd_pcm_runtime_t *runtime;
346 snd_pcm_uframes_t frames;
348 snd_assert(substream != NULL, return -ENXIO);
349 runtime = substream->runtime;
350 snd_assert(runtime != NULL, return -ENXIO);
351 snd_pcm_stream_lock_irq(substream);
352 switch (runtime->status->state) {
353 case SNDRV_PCM_STATE_OPEN:
354 case SNDRV_PCM_STATE_SETUP:
355 case SNDRV_PCM_STATE_PREPARED:
358 snd_pcm_stream_unlock_irq(substream);
361 snd_pcm_stream_unlock_irq(substream);
362 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
363 if (!substream->oss.oss)
365 if (atomic_read(&runtime->mmap_count))
369 err = snd_pcm_hw_refine(substream, params);
373 err = snd_pcm_hw_params_choose(substream, params);
377 if (substream->ops->hw_params != NULL) {
378 err = substream->ops->hw_params(substream, params);
383 runtime->access = params_access(params);
384 runtime->format = params_format(params);
385 runtime->subformat = params_subformat(params);
386 runtime->channels = params_channels(params);
387 runtime->rate = params_rate(params);
388 runtime->period_size = params_period_size(params);
389 runtime->periods = params_periods(params);
390 runtime->buffer_size = params_buffer_size(params);
391 runtime->tick_time = params_tick_time(params);
392 runtime->info = params->info;
393 runtime->rate_num = params->rate_num;
394 runtime->rate_den = params->rate_den;
396 bits = snd_pcm_format_physical_width(runtime->format);
397 runtime->sample_bits = bits;
398 bits *= runtime->channels;
399 runtime->frame_bits = bits;
401 while (bits % 8 != 0) {
405 runtime->byte_align = bits / 8;
406 runtime->min_align = frames;
408 /* Default sw params */
409 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
410 runtime->period_step = 1;
411 runtime->sleep_min = 0;
412 runtime->control->avail_min = runtime->period_size;
413 runtime->xfer_align = runtime->period_size;
414 runtime->start_threshold = 1;
415 runtime->stop_threshold = runtime->buffer_size;
416 runtime->silence_threshold = 0;
417 runtime->silence_size = 0;
418 runtime->boundary = runtime->buffer_size;
419 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
420 runtime->boundary *= 2;
422 snd_pcm_timer_resolution_change(substream);
423 runtime->status->state = SNDRV_PCM_STATE_SETUP;
426 /* hardware might be unuseable from this time,
427 so we force application to retry to set
428 the correct hardware parameter settings */
429 runtime->status->state = SNDRV_PCM_STATE_OPEN;
430 if (substream->ops->hw_free != NULL)
431 substream->ops->hw_free(substream);
435 static int snd_pcm_hw_params_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t __user * _params)
437 snd_pcm_hw_params_t *params;
440 params = kmalloc(sizeof(*params), GFP_KERNEL);
445 if (copy_from_user(params, _params, sizeof(*params))) {
449 err = snd_pcm_hw_params(substream, params);
450 if (copy_to_user(_params, params, sizeof(*params))) {
459 static int snd_pcm_hw_free(snd_pcm_substream_t * substream)
461 snd_pcm_runtime_t *runtime;
464 snd_assert(substream != NULL, return -ENXIO);
465 runtime = substream->runtime;
466 snd_assert(runtime != NULL, return -ENXIO);
467 snd_pcm_stream_lock_irq(substream);
468 switch (runtime->status->state) {
469 case SNDRV_PCM_STATE_SETUP:
470 case SNDRV_PCM_STATE_PREPARED:
473 snd_pcm_stream_unlock_irq(substream);
476 snd_pcm_stream_unlock_irq(substream);
477 if (atomic_read(&runtime->mmap_count))
479 if (substream->ops->hw_free)
480 result = substream->ops->hw_free(substream);
481 runtime->status->state = SNDRV_PCM_STATE_OPEN;
485 static int snd_pcm_sw_params(snd_pcm_substream_t * substream, snd_pcm_sw_params_t *params)
487 snd_pcm_runtime_t *runtime;
489 snd_assert(substream != NULL, return -ENXIO);
490 runtime = substream->runtime;
491 snd_assert(runtime != NULL, return -ENXIO);
492 snd_pcm_stream_lock_irq(substream);
493 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
494 snd_pcm_stream_unlock_irq(substream);
497 snd_pcm_stream_unlock_irq(substream);
499 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
501 if (params->avail_min == 0)
503 if (params->xfer_align == 0 ||
504 params->xfer_align % runtime->min_align != 0)
506 if (params->silence_size >= runtime->boundary) {
507 if (params->silence_threshold != 0)
510 if (params->silence_size > params->silence_threshold)
512 if (params->silence_threshold > runtime->buffer_size)
515 snd_pcm_stream_lock_irq(substream);
516 runtime->tstamp_mode = params->tstamp_mode;
517 runtime->sleep_min = params->sleep_min;
518 runtime->period_step = params->period_step;
519 runtime->control->avail_min = params->avail_min;
520 runtime->start_threshold = params->start_threshold;
521 runtime->stop_threshold = params->stop_threshold;
522 runtime->silence_threshold = params->silence_threshold;
523 runtime->silence_size = params->silence_size;
524 runtime->xfer_align = params->xfer_align;
525 params->boundary = runtime->boundary;
526 if (snd_pcm_running(substream)) {
527 if (runtime->sleep_min)
528 snd_pcm_tick_prepare(substream);
530 snd_pcm_tick_set(substream, 0);
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
532 runtime->silence_size > 0)
533 snd_pcm_playback_silence(substream, ULONG_MAX);
534 wake_up(&runtime->sleep);
536 snd_pcm_stream_unlock_irq(substream);
540 static int snd_pcm_sw_params_user(snd_pcm_substream_t * substream, snd_pcm_sw_params_t __user * _params)
542 snd_pcm_sw_params_t params;
544 if (copy_from_user(¶ms, _params, sizeof(params)))
546 err = snd_pcm_sw_params(substream, ¶ms);
547 if (copy_to_user(_params, ¶ms, sizeof(params)))
552 int snd_pcm_status(snd_pcm_substream_t *substream,
553 snd_pcm_status_t *status)
555 snd_pcm_runtime_t *runtime = substream->runtime;
557 snd_pcm_stream_lock_irq(substream);
558 status->state = runtime->status->state;
559 status->suspended_state = runtime->status->suspended_state;
560 if (status->state == SNDRV_PCM_STATE_OPEN)
562 status->trigger_tstamp = runtime->trigger_tstamp;
563 if (snd_pcm_running(substream)) {
564 snd_pcm_update_hw_ptr(substream);
565 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
566 status->tstamp = runtime->status->tstamp;
568 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
570 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
571 status->appl_ptr = runtime->control->appl_ptr;
572 status->hw_ptr = runtime->status->hw_ptr;
573 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
574 status->avail = snd_pcm_playback_avail(runtime);
575 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
576 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
577 status->delay = runtime->buffer_size - status->avail;
581 status->avail = snd_pcm_capture_avail(runtime);
582 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
583 status->delay = status->avail;
587 status->avail_max = runtime->avail_max;
588 status->overrange = runtime->overrange;
589 runtime->avail_max = 0;
590 runtime->overrange = 0;
592 snd_pcm_stream_unlock_irq(substream);
596 static int snd_pcm_status_user(snd_pcm_substream_t * substream, snd_pcm_status_t __user * _status)
598 snd_pcm_status_t status;
599 snd_pcm_runtime_t *runtime;
602 snd_assert(substream != NULL, return -ENXIO);
603 runtime = substream->runtime;
604 memset(&status, 0, sizeof(status));
605 res = snd_pcm_status(substream, &status);
608 if (copy_to_user(_status, &status, sizeof(status)))
613 static int snd_pcm_channel_info(snd_pcm_substream_t * substream, snd_pcm_channel_info_t * info)
615 snd_pcm_runtime_t *runtime;
616 unsigned int channel;
618 snd_assert(substream != NULL, return -ENXIO);
619 channel = info->channel;
620 runtime = substream->runtime;
621 snd_pcm_stream_lock_irq(substream);
622 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
623 snd_pcm_stream_unlock_irq(substream);
626 snd_pcm_stream_unlock_irq(substream);
627 if (channel >= runtime->channels)
629 memset(info, 0, sizeof(*info));
630 info->channel = channel;
631 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
634 static int snd_pcm_channel_info_user(snd_pcm_substream_t * substream, snd_pcm_channel_info_t __user * _info)
636 snd_pcm_channel_info_t info;
639 if (copy_from_user(&info, _info, sizeof(info)))
641 res = snd_pcm_channel_info(substream, &info);
644 if (copy_to_user(_info, &info, sizeof(info)))
649 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t *substream)
651 snd_pcm_runtime_t *runtime = substream->runtime;
652 if (runtime->trigger_master == NULL)
654 if (runtime->trigger_master == substream) {
655 snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
657 snd_pcm_trigger_tstamp(runtime->trigger_master);
658 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
660 runtime->trigger_master = NULL;
664 int (*pre_action)(snd_pcm_substream_t *substream, int state);
665 int (*do_action)(snd_pcm_substream_t *substream, int state);
666 void (*undo_action)(snd_pcm_substream_t *substream, int state);
667 void (*post_action)(snd_pcm_substream_t *substream, int state);
671 * this functions is core for handling of linked stream
672 * Note: the stream state might be changed also on failure
673 * Note2: call with calling stream lock + link lock
675 static int snd_pcm_action_group(struct action_ops *ops,
676 snd_pcm_substream_t *substream,
677 int state, int do_lock)
679 struct list_head *pos;
680 snd_pcm_substream_t *s = NULL;
681 snd_pcm_substream_t *s1;
684 snd_pcm_group_for_each(pos, substream) {
685 s = snd_pcm_group_substream_entry(pos);
686 if (do_lock && s != substream)
687 spin_lock(&s->self_group.lock);
688 res = ops->pre_action(s, state);
692 snd_pcm_group_for_each(pos, substream) {
693 s = snd_pcm_group_substream_entry(pos);
694 res = ops->do_action(s, state);
696 if (ops->undo_action) {
697 snd_pcm_group_for_each(pos, substream) {
698 s1 = snd_pcm_group_substream_entry(pos);
699 if (s1 == s) /* failed stream */
701 ops->undo_action(s1, state);
704 s = NULL; /* unlock all */
708 snd_pcm_group_for_each(pos, substream) {
709 s = snd_pcm_group_substream_entry(pos);
710 ops->post_action(s, state);
715 snd_pcm_group_for_each(pos, substream) {
716 s1 = snd_pcm_group_substream_entry(pos);
718 spin_unlock(&s1->self_group.lock);
719 if (s1 == s) /* end */
727 * Note: call with stream lock
729 static int snd_pcm_action_single(struct action_ops *ops,
730 snd_pcm_substream_t *substream,
735 res = ops->pre_action(substream, state);
738 res = ops->do_action(substream, state);
740 ops->post_action(substream, state);
741 else if (ops->undo_action)
742 ops->undo_action(substream, state);
747 * Note: call with stream lock
749 static int snd_pcm_action(struct action_ops *ops,
750 snd_pcm_substream_t *substream,
755 if (snd_pcm_stream_linked(substream)) {
756 if (!spin_trylock(&substream->group->lock)) {
757 spin_unlock(&substream->self_group.lock);
758 spin_lock(&substream->group->lock);
759 spin_lock(&substream->self_group.lock);
761 res = snd_pcm_action_group(ops, substream, state, 1);
762 spin_unlock(&substream->group->lock);
764 res = snd_pcm_action_single(ops, substream, state);
770 * Note: don't use any locks before
772 static int snd_pcm_action_lock_irq(struct action_ops *ops,
773 snd_pcm_substream_t *substream,
778 read_lock_irq(&snd_pcm_link_rwlock);
779 if (snd_pcm_stream_linked(substream)) {
780 spin_lock(&substream->group->lock);
781 spin_lock(&substream->self_group.lock);
782 res = snd_pcm_action_group(ops, substream, state, 1);
783 spin_unlock(&substream->self_group.lock);
784 spin_unlock(&substream->group->lock);
786 spin_lock(&substream->self_group.lock);
787 res = snd_pcm_action_single(ops, substream, state);
788 spin_unlock(&substream->self_group.lock);
790 read_unlock_irq(&snd_pcm_link_rwlock);
796 static int snd_pcm_action_nonatomic(struct action_ops *ops,
797 snd_pcm_substream_t *substream,
802 down_read(&snd_pcm_link_rwsem);
803 if (snd_pcm_stream_linked(substream))
804 res = snd_pcm_action_group(ops, substream, state, 0);
806 res = snd_pcm_action_single(ops, substream, state);
807 up_read(&snd_pcm_link_rwsem);
814 static int snd_pcm_pre_start(snd_pcm_substream_t *substream, int state)
816 snd_pcm_runtime_t *runtime = substream->runtime;
817 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
819 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
820 !snd_pcm_playback_data(substream))
822 runtime->trigger_master = substream;
826 static int snd_pcm_do_start(snd_pcm_substream_t *substream, int state)
828 if (substream->runtime->trigger_master != substream)
830 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
833 static void snd_pcm_undo_start(snd_pcm_substream_t *substream, int state)
835 if (substream->runtime->trigger_master == substream)
836 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
839 static void snd_pcm_post_start(snd_pcm_substream_t *substream, int state)
841 snd_pcm_runtime_t *runtime = substream->runtime;
842 snd_pcm_trigger_tstamp(substream);
843 runtime->status->state = state;
844 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
845 runtime->silence_size > 0)
846 snd_pcm_playback_silence(substream, ULONG_MAX);
847 if (runtime->sleep_min)
848 snd_pcm_tick_prepare(substream);
849 if (substream->timer)
850 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART, &runtime->trigger_tstamp);
853 static struct action_ops snd_pcm_action_start = {
854 .pre_action = snd_pcm_pre_start,
855 .do_action = snd_pcm_do_start,
856 .undo_action = snd_pcm_undo_start,
857 .post_action = snd_pcm_post_start
863 * Start all linked streams.
865 int snd_pcm_start(snd_pcm_substream_t *substream)
867 return snd_pcm_action(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
873 static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
875 snd_pcm_runtime_t *runtime = substream->runtime;
876 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
878 runtime->trigger_master = substream;
882 static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
884 if (substream->runtime->trigger_master == substream &&
885 snd_pcm_running(substream))
886 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
887 return 0; /* unconditonally stop all substreams */
890 static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
892 snd_pcm_runtime_t *runtime = substream->runtime;
893 if (runtime->status->state != state) {
894 snd_pcm_trigger_tstamp(substream);
895 if (substream->timer)
896 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
897 runtime->status->state = state;
898 snd_pcm_tick_set(substream, 0);
900 wake_up(&runtime->sleep);
903 static struct action_ops snd_pcm_action_stop = {
904 .pre_action = snd_pcm_pre_stop,
905 .do_action = snd_pcm_do_stop,
906 .post_action = snd_pcm_post_stop
912 * Try to stop all running streams in the substream group.
913 * The state of each stream is changed to the given value after that unconditionally.
915 int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
917 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
923 * Stop the DMA only when the given stream is playback.
924 * The state is changed to SETUP.
925 * Unlike snd_pcm_stop(), this affects only the given stream.
927 int snd_pcm_drain_done(snd_pcm_substream_t *substream)
929 return snd_pcm_action_single(&snd_pcm_action_stop, substream, SNDRV_PCM_STATE_SETUP);
935 static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
937 snd_pcm_runtime_t *runtime = substream->runtime;
938 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
941 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
943 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
945 runtime->trigger_master = substream;
949 static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
951 if (substream->runtime->trigger_master != substream)
953 return substream->ops->trigger(substream,
954 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
955 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
958 static void snd_pcm_undo_pause(snd_pcm_substream_t *substream, int push)
960 if (substream->runtime->trigger_master == substream)
961 substream->ops->trigger(substream,
962 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
963 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
966 static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
968 snd_pcm_runtime_t *runtime = substream->runtime;
969 snd_pcm_trigger_tstamp(substream);
971 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
972 if (substream->timer)
973 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
974 snd_pcm_tick_set(substream, 0);
975 wake_up(&runtime->sleep);
977 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
978 if (runtime->sleep_min)
979 snd_pcm_tick_prepare(substream);
980 if (substream->timer)
981 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
985 static struct action_ops snd_pcm_action_pause = {
986 .pre_action = snd_pcm_pre_pause,
987 .do_action = snd_pcm_do_pause,
988 .undo_action = snd_pcm_undo_pause,
989 .post_action = snd_pcm_post_pause
993 * Push/release the pause for all linked streams.
995 static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
997 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1003 static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
1005 snd_pcm_runtime_t *runtime = substream->runtime;
1006 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1008 runtime->trigger_master = substream;
1012 static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
1014 snd_pcm_runtime_t *runtime = substream->runtime;
1015 if (runtime->trigger_master != substream)
1017 if (! snd_pcm_running(substream))
1019 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1020 return 0; /* suspend unconditionally */
1023 static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
1025 snd_pcm_runtime_t *runtime = substream->runtime;
1026 snd_pcm_trigger_tstamp(substream);
1027 if (substream->timer)
1028 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
1029 runtime->status->suspended_state = runtime->status->state;
1030 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1031 snd_pcm_tick_set(substream, 0);
1032 wake_up(&runtime->sleep);
1035 static struct action_ops snd_pcm_action_suspend = {
1036 .pre_action = snd_pcm_pre_suspend,
1037 .do_action = snd_pcm_do_suspend,
1038 .post_action = snd_pcm_post_suspend
1044 * Trigger SUSPEND to all linked streams.
1045 * After this call, all streams are changed to SUSPENDED state.
1047 int snd_pcm_suspend(snd_pcm_substream_t *substream)
1050 unsigned long flags;
1052 snd_pcm_stream_lock_irqsave(substream, flags);
1053 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1054 snd_pcm_stream_unlock_irqrestore(substream, flags);
1059 * snd_pcm_suspend_all
1061 * Trigger SUSPEND to all substreams in the given pcm.
1062 * After this call, all streams are changed to SUSPENDED state.
1064 int snd_pcm_suspend_all(snd_pcm_t *pcm)
1066 snd_pcm_substream_t *substream;
1067 int stream, err = 0;
1069 for (stream = 0; stream < 2; stream++) {
1070 for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
1071 /* FIXME: the open/close code should lock this as well */
1072 if (substream->runtime == NULL)
1074 err = snd_pcm_suspend(substream);
1075 if (err < 0 && err != -EBUSY)
1084 static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
1086 snd_pcm_runtime_t *runtime = substream->runtime;
1087 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1089 runtime->trigger_master = substream;
1093 static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
1095 snd_pcm_runtime_t *runtime = substream->runtime;
1096 if (runtime->trigger_master != substream)
1098 /* DMA not running previously? */
1099 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1100 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1101 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1103 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1106 static void snd_pcm_undo_resume(snd_pcm_substream_t *substream, int state)
1108 if (substream->runtime->trigger_master == substream &&
1109 snd_pcm_running(substream))
1110 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1113 static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
1115 snd_pcm_runtime_t *runtime = substream->runtime;
1116 snd_pcm_trigger_tstamp(substream);
1117 if (substream->timer)
1118 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
1119 runtime->status->state = runtime->status->suspended_state;
1120 if (runtime->sleep_min)
1121 snd_pcm_tick_prepare(substream);
1124 static struct action_ops snd_pcm_action_resume = {
1125 .pre_action = snd_pcm_pre_resume,
1126 .do_action = snd_pcm_do_resume,
1127 .undo_action = snd_pcm_undo_resume,
1128 .post_action = snd_pcm_post_resume
1131 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1133 snd_card_t *card = substream->pcm->card;
1136 snd_power_lock(card);
1137 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1138 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1139 snd_power_unlock(card);
1145 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1150 #endif /* CONFIG_PM */
1155 * Change the RUNNING stream(s) to XRUN state.
1157 static int snd_pcm_xrun(snd_pcm_substream_t *substream)
1159 snd_card_t *card = substream->pcm->card;
1160 snd_pcm_runtime_t *runtime = substream->runtime;
1163 snd_power_lock(card);
1164 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1165 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1170 snd_pcm_stream_lock_irq(substream);
1171 switch (runtime->status->state) {
1172 case SNDRV_PCM_STATE_XRUN:
1173 result = 0; /* already there */
1175 case SNDRV_PCM_STATE_RUNNING:
1176 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1181 snd_pcm_stream_unlock_irq(substream);
1183 snd_power_unlock(card);
1190 static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
1192 snd_pcm_runtime_t *runtime = substream->runtime;
1193 switch (runtime->status->state) {
1194 case SNDRV_PCM_STATE_RUNNING:
1195 case SNDRV_PCM_STATE_PREPARED:
1196 case SNDRV_PCM_STATE_PAUSED:
1197 case SNDRV_PCM_STATE_SUSPENDED:
1204 static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
1206 snd_pcm_runtime_t *runtime = substream->runtime;
1207 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1210 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1211 runtime->hw_ptr_base = 0;
1212 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
1213 runtime->silence_start = runtime->status->hw_ptr;
1214 runtime->silence_filled = 0;
1218 static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
1220 snd_pcm_runtime_t *runtime = substream->runtime;
1221 runtime->control->appl_ptr = runtime->status->hw_ptr;
1222 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1223 runtime->silence_size > 0)
1224 snd_pcm_playback_silence(substream, ULONG_MAX);
1227 static struct action_ops snd_pcm_action_reset = {
1228 .pre_action = snd_pcm_pre_reset,
1229 .do_action = snd_pcm_do_reset,
1230 .post_action = snd_pcm_post_reset
1233 static int snd_pcm_reset(snd_pcm_substream_t *substream)
1235 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1241 static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
1243 snd_pcm_runtime_t *runtime = substream->runtime;
1244 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1246 if (snd_pcm_running(substream))
1251 static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
1254 err = substream->ops->prepare(substream);
1257 return snd_pcm_do_reset(substream, 0);
1260 static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
1262 snd_pcm_runtime_t *runtime = substream->runtime;
1263 runtime->control->appl_ptr = runtime->status->hw_ptr;
1264 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1267 static struct action_ops snd_pcm_action_prepare = {
1268 .pre_action = snd_pcm_pre_prepare,
1269 .do_action = snd_pcm_do_prepare,
1270 .post_action = snd_pcm_post_prepare
1276 int snd_pcm_prepare(snd_pcm_substream_t *substream)
1279 snd_card_t *card = substream->pcm->card;
1281 snd_power_lock(card);
1282 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1283 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1284 snd_power_unlock(card);
1292 static int snd_pcm_pre_drain_init(snd_pcm_substream_t * substream, int state)
1294 if (substream->ffile->f_flags & O_NONBLOCK)
1296 substream->runtime->trigger_master = substream;
1300 static int snd_pcm_do_drain_init(snd_pcm_substream_t * substream, int state)
1302 snd_pcm_runtime_t *runtime = substream->runtime;
1303 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1304 switch (runtime->status->state) {
1305 case SNDRV_PCM_STATE_PREPARED:
1306 /* start playback stream if possible */
1307 if (! snd_pcm_playback_empty(substream)) {
1308 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1309 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1312 case SNDRV_PCM_STATE_RUNNING:
1313 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1319 /* stop running stream */
1320 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1321 int state = snd_pcm_capture_avail(runtime) > 0 ?
1322 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1323 snd_pcm_do_stop(substream, state);
1324 snd_pcm_post_stop(substream, state);
1330 static void snd_pcm_post_drain_init(snd_pcm_substream_t * substream, int state)
1334 static struct action_ops snd_pcm_action_drain_init = {
1335 .pre_action = snd_pcm_pre_drain_init,
1336 .do_action = snd_pcm_do_drain_init,
1337 .post_action = snd_pcm_post_drain_init
1341 snd_pcm_substream_t *substream;
1343 snd_pcm_uframes_t stop_threshold;
1346 static int snd_pcm_drop(snd_pcm_substream_t *substream);
1349 * Drain the stream(s).
1350 * When the substream is linked, sync until the draining of all playback streams
1352 * After this call, all streams are supposed to be either SETUP or DRAINING
1353 * (capture only) state.
1355 static int snd_pcm_drain(snd_pcm_substream_t *substream)
1358 snd_pcm_runtime_t *runtime;
1359 struct list_head *pos;
1362 struct drain_rec *drec, drec_tmp, *d;
1364 snd_assert(substream != NULL, return -ENXIO);
1365 card = substream->pcm->card;
1366 runtime = substream->runtime;
1368 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1371 snd_power_lock(card);
1372 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1373 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1375 snd_power_unlock(card);
1380 /* allocate temporary record for drain sync */
1381 down_read(&snd_pcm_link_rwsem);
1382 if (snd_pcm_stream_linked(substream)) {
1383 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1385 up_read(&snd_pcm_link_rwsem);
1386 snd_power_unlock(card);
1392 /* count only playback streams */
1394 snd_pcm_group_for_each(pos, substream) {
1395 snd_pcm_substream_t *s = snd_pcm_group_substream_entry(pos);
1396 runtime = s->runtime;
1397 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1398 d = &drec[num_drecs++];
1400 init_waitqueue_entry(&d->wait, current);
1401 add_wait_queue(&runtime->sleep, &d->wait);
1402 /* stop_threshold fixup to avoid endless loop when
1403 * stop_threshold > buffer_size
1405 d->stop_threshold = runtime->stop_threshold;
1406 if (runtime->stop_threshold > runtime->buffer_size)
1407 runtime->stop_threshold = runtime->buffer_size;
1410 up_read(&snd_pcm_link_rwsem);
1414 snd_pcm_stream_lock_irq(substream);
1416 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1417 snd_pcm_pause(substream, 0);
1419 /* pre-start/stop - all running streams are changed to DRAINING state */
1420 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1422 snd_pcm_stream_unlock_irq(substream);
1428 if (signal_pending(current)) {
1429 result = -ERESTARTSYS;
1433 for (i = 0; i < num_drecs; i++) {
1434 runtime = drec[i].substream->runtime;
1435 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1439 break; /* yes, all drained */
1441 set_current_state(TASK_INTERRUPTIBLE);
1442 snd_pcm_stream_unlock_irq(substream);
1443 snd_power_unlock(card);
1444 tout = schedule_timeout(10 * HZ);
1445 snd_power_lock(card);
1446 snd_pcm_stream_lock_irq(substream);
1448 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1451 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1452 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1459 snd_pcm_stream_unlock_irq(substream);
1462 for (i = 0; i < num_drecs; i++) {
1464 runtime = d->substream->runtime;
1465 remove_wait_queue(&runtime->sleep, &d->wait);
1466 runtime->stop_threshold = d->stop_threshold;
1469 if (drec != &drec_tmp)
1471 snd_power_unlock(card);
1479 * Immediately put all linked substreams into SETUP state.
1481 static int snd_pcm_drop(snd_pcm_substream_t *substream)
1483 snd_pcm_runtime_t *runtime;
1487 snd_assert(substream != NULL, return -ENXIO);
1488 runtime = substream->runtime;
1489 card = substream->pcm->card;
1491 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1494 snd_power_lock(card);
1495 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1496 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1501 snd_pcm_stream_lock_irq(substream);
1503 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1504 snd_pcm_pause(substream, 0);
1506 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1507 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1508 snd_pcm_stream_unlock_irq(substream);
1510 snd_power_unlock(card);
1515 /* WARNING: Don't forget to fput back the file */
1516 extern int snd_major;
1517 static struct file *snd_pcm_file_fd(int fd)
1520 struct inode *inode;
1521 unsigned short minor;
1525 inode = file->f_dentry->d_inode;
1526 if (!S_ISCHR(inode->i_mode) ||
1527 imajor(inode) != snd_major) {
1531 minor = iminor(inode);
1533 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1543 static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
1547 snd_pcm_file_t *pcm_file;
1548 snd_pcm_substream_t *substream1;
1550 file = snd_pcm_file_fd(fd);
1553 pcm_file = file->private_data;
1554 substream1 = pcm_file->substream;
1555 down_write(&snd_pcm_link_rwsem);
1556 write_lock_irq(&snd_pcm_link_rwlock);
1557 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1558 substream->runtime->status->state != substream1->runtime->status->state) {
1562 if (snd_pcm_stream_linked(substream1)) {
1566 if (!snd_pcm_stream_linked(substream)) {
1567 substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
1568 if (substream->group == NULL) {
1572 spin_lock_init(&substream->group->lock);
1573 INIT_LIST_HEAD(&substream->group->substreams);
1574 list_add_tail(&substream->link_list, &substream->group->substreams);
1575 substream->group->count = 1;
1577 list_add_tail(&substream1->link_list, &substream->group->substreams);
1578 substream->group->count++;
1579 substream1->group = substream->group;
1581 write_unlock_irq(&snd_pcm_link_rwlock);
1582 up_write(&snd_pcm_link_rwsem);
1587 static void relink_to_local(snd_pcm_substream_t *substream)
1589 substream->group = &substream->self_group;
1590 INIT_LIST_HEAD(&substream->self_group.substreams);
1591 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1594 static int snd_pcm_unlink(snd_pcm_substream_t *substream)
1596 struct list_head *pos;
1599 down_write(&snd_pcm_link_rwsem);
1600 write_lock_irq(&snd_pcm_link_rwlock);
1601 if (!snd_pcm_stream_linked(substream)) {
1605 list_del(&substream->link_list);
1606 substream->group->count--;
1607 if (substream->group->count == 1) { /* detach the last stream, too */
1608 snd_pcm_group_for_each(pos, substream) {
1609 relink_to_local(snd_pcm_group_substream_entry(pos));
1612 kfree(substream->group);
1614 relink_to_local(substream);
1616 write_unlock_irq(&snd_pcm_link_rwlock);
1617 up_write(&snd_pcm_link_rwsem);
1624 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
1625 snd_pcm_hw_rule_t *rule)
1628 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1629 hw_param_interval_c(params, rule->deps[1]), &t);
1630 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1633 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
1634 snd_pcm_hw_rule_t *rule)
1637 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1638 hw_param_interval_c(params, rule->deps[1]), &t);
1639 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1642 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
1643 snd_pcm_hw_rule_t *rule)
1646 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1647 hw_param_interval_c(params, rule->deps[1]),
1648 (unsigned long) rule->private, &t);
1649 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1652 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
1653 snd_pcm_hw_rule_t *rule)
1656 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1657 (unsigned long) rule->private,
1658 hw_param_interval_c(params, rule->deps[1]), &t);
1659 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1662 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
1663 snd_pcm_hw_rule_t *rule)
1666 snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
1668 snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1670 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1672 if (! snd_mask_test(mask, k))
1674 bits = snd_pcm_format_physical_width(k);
1676 continue; /* ignore invalid formats */
1677 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1678 snd_mask_reset(&m, k);
1680 return snd_mask_refine(mask, &m);
1683 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
1684 snd_pcm_hw_rule_t *rule)
1692 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1694 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1696 bits = snd_pcm_format_physical_width(k);
1698 continue; /* ignore invalid formats */
1699 if (t.min > (unsigned)bits)
1701 if (t.max < (unsigned)bits)
1705 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1708 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1709 #error "Change this table"
1712 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1713 48000, 64000, 88200, 96000, 176400, 192000 };
1715 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
1716 snd_pcm_hw_rule_t *rule)
1718 snd_pcm_hardware_t *hw = rule->private;
1719 return snd_interval_list(hw_param_interval(params, rule->var),
1720 ARRAY_SIZE(rates), rates, hw->rates);
1723 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
1724 snd_pcm_hw_rule_t *rule)
1727 snd_pcm_substream_t *substream = rule->private;
1729 t.max = substream->buffer_bytes_max;
1733 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1736 int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
1738 snd_pcm_runtime_t *runtime = substream->runtime;
1739 snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
1742 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1743 snd_mask_any(constrs_mask(constrs, k));
1746 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1747 snd_interval_any(constrs_interval(constrs, k));
1750 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1751 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1752 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1753 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1754 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1756 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1757 snd_pcm_hw_rule_format, NULL,
1758 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1761 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1762 snd_pcm_hw_rule_sample_bits, NULL,
1763 SNDRV_PCM_HW_PARAM_FORMAT,
1764 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1767 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1768 snd_pcm_hw_rule_div, NULL,
1769 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1772 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1773 snd_pcm_hw_rule_mul, NULL,
1774 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1777 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1778 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1779 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1782 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1783 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1784 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1787 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1788 snd_pcm_hw_rule_div, NULL,
1789 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1792 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1793 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1794 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1797 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1798 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1799 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1802 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1803 snd_pcm_hw_rule_div, NULL,
1804 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1807 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1808 snd_pcm_hw_rule_div, NULL,
1809 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1812 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1813 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1814 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1817 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1818 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1819 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1823 snd_pcm_hw_rule_mul, NULL,
1824 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1827 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1828 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1829 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1833 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1834 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1838 snd_pcm_hw_rule_muldivk, (void*) 8,
1839 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1843 snd_pcm_hw_rule_muldivk, (void*) 8,
1844 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1848 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1849 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1853 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1854 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1860 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
1862 snd_pcm_runtime_t *runtime = substream->runtime;
1863 snd_pcm_hardware_t *hw = &runtime->hw;
1865 unsigned int mask = 0;
1867 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1868 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1869 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1870 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1871 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1872 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1873 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1874 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1875 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1876 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1877 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1879 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1880 snd_assert(err >= 0, return -EINVAL);
1882 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1883 snd_assert(err >= 0, return -EINVAL);
1885 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1886 snd_assert(err >= 0, return -EINVAL);
1888 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1889 hw->channels_min, hw->channels_max);
1890 snd_assert(err >= 0, return -EINVAL);
1892 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1893 hw->rate_min, hw->rate_max);
1894 snd_assert(err >= 0, return -EINVAL);
1896 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1897 hw->period_bytes_min, hw->period_bytes_max);
1898 snd_assert(err >= 0, return -EINVAL);
1900 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1901 hw->periods_min, hw->periods_max);
1902 snd_assert(err >= 0, return -EINVAL);
1904 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1905 hw->period_bytes_min, hw->buffer_bytes_max);
1906 snd_assert(err >= 0, return -EINVAL);
1908 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1909 snd_pcm_hw_rule_buffer_bytes_max, substream,
1910 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1915 if (runtime->dma_bytes) {
1916 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1917 snd_assert(err >= 0, return -EINVAL);
1920 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1921 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1922 snd_pcm_hw_rule_rate, hw,
1923 SNDRV_PCM_HW_PARAM_RATE, -1);
1928 /* FIXME: this belong to lowlevel */
1929 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1930 1000000 / HZ, 1000000 / HZ);
1931 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1936 static void snd_pcm_add_file(snd_pcm_str_t *str,
1937 snd_pcm_file_t *pcm_file)
1939 pcm_file->next = str->files;
1940 str->files = pcm_file;
1943 static void snd_pcm_remove_file(snd_pcm_str_t *str,
1944 snd_pcm_file_t *pcm_file)
1946 snd_pcm_file_t * pcm_file1;
1947 if (str->files == pcm_file) {
1948 str->files = pcm_file->next;
1950 pcm_file1 = str->files;
1951 while (pcm_file1 && pcm_file1->next != pcm_file)
1952 pcm_file1 = pcm_file1->next;
1953 if (pcm_file1 != NULL)
1954 pcm_file1->next = pcm_file->next;
1958 static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
1960 snd_pcm_substream_t *substream;
1961 snd_pcm_runtime_t *runtime;
1962 snd_pcm_str_t * str;
1964 snd_assert(pcm_file != NULL, return -ENXIO);
1965 substream = pcm_file->substream;
1966 snd_assert(substream != NULL, return -ENXIO);
1967 runtime = substream->runtime;
1968 str = substream->pstr;
1969 snd_pcm_unlink(substream);
1970 if (substream->open_flag) {
1971 if (substream->ops->hw_free != NULL)
1972 substream->ops->hw_free(substream);
1973 substream->ops->close(substream);
1974 substream->open_flag = 0;
1976 substream->ffile = NULL;
1977 snd_pcm_remove_file(str, pcm_file);
1978 snd_pcm_release_substream(substream);
1983 static int snd_pcm_open_file(struct file *file,
1986 snd_pcm_file_t **rpcm_file)
1989 snd_pcm_file_t *pcm_file;
1990 snd_pcm_substream_t *substream;
1993 snd_assert(rpcm_file != NULL, return -EINVAL);
1996 pcm_file = kcalloc(1, sizeof(*pcm_file), GFP_KERNEL);
1997 if (pcm_file == NULL) {
2001 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
2006 str = substream->pstr;
2007 substream->file = pcm_file;
2008 substream->no_mmap_ctrl = 0;
2010 pcm_file->substream = substream;
2012 snd_pcm_add_file(str, pcm_file);
2014 err = snd_pcm_hw_constraints_init(substream);
2016 snd_printd("snd_pcm_hw_constraints_init failed\n");
2017 snd_pcm_release_file(pcm_file);
2021 if ((err = substream->ops->open(substream)) < 0) {
2022 snd_pcm_release_file(pcm_file);
2025 substream->open_flag = 1;
2027 err = snd_pcm_hw_constraints_complete(substream);
2029 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2030 substream->ops->close(substream);
2031 snd_pcm_release_file(pcm_file);
2035 substream->ffile = file;
2037 file->private_data = pcm_file;
2038 *rpcm_file = pcm_file;
2042 static int snd_pcm_open(struct inode *inode, struct file *file)
2044 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
2045 int device = SNDRV_MINOR_DEVICE(iminor(inode));
2048 snd_pcm_file_t *pcm_file;
2051 snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
2052 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2057 err = snd_card_file_add(pcm->card, file);
2060 if (!try_module_get(pcm->card->module)) {
2064 init_waitqueue_entry(&wait, current);
2065 add_wait_queue(&pcm->open_wait, &wait);
2066 down(&pcm->open_mutex);
2068 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2071 if (err == -EAGAIN) {
2072 if (file->f_flags & O_NONBLOCK) {
2078 set_current_state(TASK_INTERRUPTIBLE);
2079 up(&pcm->open_mutex);
2081 down(&pcm->open_mutex);
2082 if (signal_pending(current)) {
2087 remove_wait_queue(&pcm->open_wait, &wait);
2088 up(&pcm->open_mutex);
2094 module_put(pcm->card->module);
2096 snd_card_file_remove(pcm->card, file);
2101 static int snd_pcm_release(struct inode *inode, struct file *file)
2104 snd_pcm_substream_t *substream;
2105 snd_pcm_file_t *pcm_file;
2107 pcm_file = file->private_data;
2108 substream = pcm_file->substream;
2109 snd_assert(substream != NULL, return -ENXIO);
2110 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2111 pcm = substream->pcm;
2112 snd_pcm_drop(substream);
2113 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2114 down(&pcm->open_mutex);
2115 snd_pcm_release_file(pcm_file);
2116 up(&pcm->open_mutex);
2117 wake_up(&pcm->open_wait);
2118 module_put(pcm->card->module);
2119 snd_card_file_remove(pcm->card, file);
2123 static snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2125 snd_pcm_runtime_t *runtime = substream->runtime;
2126 snd_pcm_sframes_t appl_ptr;
2127 snd_pcm_sframes_t ret;
2128 snd_pcm_sframes_t hw_avail;
2133 snd_pcm_stream_lock_irq(substream);
2134 switch (runtime->status->state) {
2135 case SNDRV_PCM_STATE_PREPARED:
2137 case SNDRV_PCM_STATE_DRAINING:
2138 case SNDRV_PCM_STATE_RUNNING:
2139 if (snd_pcm_update_hw_ptr(substream) >= 0)
2142 case SNDRV_PCM_STATE_XRUN:
2150 hw_avail = snd_pcm_playback_hw_avail(runtime);
2151 if (hw_avail <= 0) {
2155 if (frames > (snd_pcm_uframes_t)hw_avail)
2158 frames -= frames % runtime->xfer_align;
2159 appl_ptr = runtime->control->appl_ptr - frames;
2161 appl_ptr += runtime->boundary;
2162 runtime->control->appl_ptr = appl_ptr;
2163 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2165 snd_pcm_tick_prepare(substream);
2168 snd_pcm_stream_unlock_irq(substream);
2172 static snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2174 snd_pcm_runtime_t *runtime = substream->runtime;
2175 snd_pcm_sframes_t appl_ptr;
2176 snd_pcm_sframes_t ret;
2177 snd_pcm_sframes_t hw_avail;
2182 snd_pcm_stream_lock_irq(substream);
2183 switch (runtime->status->state) {
2184 case SNDRV_PCM_STATE_PREPARED:
2185 case SNDRV_PCM_STATE_DRAINING:
2187 case SNDRV_PCM_STATE_RUNNING:
2188 if (snd_pcm_update_hw_ptr(substream) >= 0)
2191 case SNDRV_PCM_STATE_XRUN:
2199 hw_avail = snd_pcm_capture_hw_avail(runtime);
2200 if (hw_avail <= 0) {
2204 if (frames > (snd_pcm_uframes_t)hw_avail)
2207 frames -= frames % runtime->xfer_align;
2208 appl_ptr = runtime->control->appl_ptr - frames;
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 snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2223 snd_pcm_runtime_t *runtime = substream->runtime;
2224 snd_pcm_sframes_t appl_ptr;
2225 snd_pcm_sframes_t ret;
2226 snd_pcm_sframes_t avail;
2231 snd_pcm_stream_lock_irq(substream);
2232 switch (runtime->status->state) {
2233 case SNDRV_PCM_STATE_PREPARED:
2234 case SNDRV_PCM_STATE_PAUSED:
2236 case SNDRV_PCM_STATE_DRAINING:
2237 case SNDRV_PCM_STATE_RUNNING:
2238 if (snd_pcm_update_hw_ptr(substream) >= 0)
2241 case SNDRV_PCM_STATE_XRUN:
2249 avail = snd_pcm_playback_avail(runtime);
2254 if (frames > (snd_pcm_uframes_t)avail)
2257 frames -= frames % runtime->xfer_align;
2258 appl_ptr = runtime->control->appl_ptr + frames;
2259 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2260 appl_ptr -= runtime->boundary;
2261 runtime->control->appl_ptr = appl_ptr;
2262 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2264 snd_pcm_tick_prepare(substream);
2267 snd_pcm_stream_unlock_irq(substream);
2271 static snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2273 snd_pcm_runtime_t *runtime = substream->runtime;
2274 snd_pcm_sframes_t appl_ptr;
2275 snd_pcm_sframes_t ret;
2276 snd_pcm_sframes_t avail;
2281 snd_pcm_stream_lock_irq(substream);
2282 switch (runtime->status->state) {
2283 case SNDRV_PCM_STATE_PREPARED:
2284 case SNDRV_PCM_STATE_DRAINING:
2285 case SNDRV_PCM_STATE_PAUSED:
2287 case SNDRV_PCM_STATE_RUNNING:
2288 if (snd_pcm_update_hw_ptr(substream) >= 0)
2291 case SNDRV_PCM_STATE_XRUN:
2299 avail = snd_pcm_capture_avail(runtime);
2304 if (frames > (snd_pcm_uframes_t)avail)
2307 frames -= frames % runtime->xfer_align;
2308 appl_ptr = runtime->control->appl_ptr + frames;
2309 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2310 appl_ptr -= runtime->boundary;
2311 runtime->control->appl_ptr = appl_ptr;
2312 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2314 snd_pcm_tick_prepare(substream);
2317 snd_pcm_stream_unlock_irq(substream);
2321 static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
2323 snd_pcm_runtime_t *runtime = substream->runtime;
2326 snd_pcm_stream_lock_irq(substream);
2327 switch (runtime->status->state) {
2328 case SNDRV_PCM_STATE_DRAINING:
2329 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2331 case SNDRV_PCM_STATE_RUNNING:
2332 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2335 case SNDRV_PCM_STATE_PREPARED:
2336 case SNDRV_PCM_STATE_SUSPENDED:
2339 case SNDRV_PCM_STATE_XRUN:
2347 snd_pcm_stream_unlock_irq(substream);
2351 static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t __user *res)
2353 snd_pcm_runtime_t *runtime = substream->runtime;
2355 snd_pcm_sframes_t n = 0;
2357 snd_pcm_stream_lock_irq(substream);
2358 switch (runtime->status->state) {
2359 case SNDRV_PCM_STATE_DRAINING:
2360 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2362 case SNDRV_PCM_STATE_RUNNING:
2363 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2366 case SNDRV_PCM_STATE_PREPARED:
2367 case SNDRV_PCM_STATE_SUSPENDED:
2369 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2370 n = snd_pcm_playback_hw_avail(runtime);
2372 n = snd_pcm_capture_avail(runtime);
2374 case SNDRV_PCM_STATE_XRUN:
2382 snd_pcm_stream_unlock_irq(substream);
2384 if (put_user(n, res))
2389 static int snd_pcm_sync_ptr(snd_pcm_substream_t *substream, struct sndrv_pcm_sync_ptr __user *_sync_ptr)
2391 snd_pcm_runtime_t *runtime = substream->runtime;
2392 struct sndrv_pcm_sync_ptr sync_ptr;
2393 volatile struct sndrv_pcm_mmap_status *status;
2394 volatile struct sndrv_pcm_mmap_control *control;
2397 memset(&sync_ptr, 0, sizeof(sync_ptr));
2398 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2400 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct sndrv_pcm_mmap_control)))
2402 status = runtime->status;
2403 control = runtime->control;
2404 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2405 err = snd_pcm_hwsync(substream);
2409 snd_pcm_stream_lock_irq(substream);
2410 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2411 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2413 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2414 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2415 control->avail_min = sync_ptr.c.control.avail_min;
2417 sync_ptr.c.control.avail_min = control->avail_min;
2418 sync_ptr.s.status.state = status->state;
2419 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2420 sync_ptr.s.status.tstamp = status->tstamp;
2421 sync_ptr.s.status.suspended_state = status->suspended_state;
2422 snd_pcm_stream_unlock_irq(substream);
2423 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2428 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2429 unsigned int cmd, void __user *arg);
2430 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2431 unsigned int cmd, void __user *arg);
2433 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2434 unsigned int cmd, void __user *arg)
2436 snd_assert(substream != NULL, return -ENXIO);
2439 case SNDRV_PCM_IOCTL_PVERSION:
2440 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2441 case SNDRV_PCM_IOCTL_INFO:
2442 return snd_pcm_info_user(substream, arg);
2443 case SNDRV_PCM_IOCTL_TSTAMP:
2446 if (get_user(xarg, (int __user *)arg))
2448 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2451 case SNDRV_PCM_IOCTL_HW_REFINE:
2452 return snd_pcm_hw_refine_user(substream, arg);
2453 case SNDRV_PCM_IOCTL_HW_PARAMS:
2454 return snd_pcm_hw_params_user(substream, arg);
2455 case SNDRV_PCM_IOCTL_HW_FREE:
2456 return snd_pcm_hw_free(substream);
2457 case SNDRV_PCM_IOCTL_SW_PARAMS:
2458 return snd_pcm_sw_params_user(substream, arg);
2459 case SNDRV_PCM_IOCTL_STATUS:
2460 return snd_pcm_status_user(substream, arg);
2461 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2462 return snd_pcm_channel_info_user(substream, arg);
2463 case SNDRV_PCM_IOCTL_PREPARE:
2464 return snd_pcm_prepare(substream);
2465 case SNDRV_PCM_IOCTL_RESET:
2466 return snd_pcm_reset(substream);
2467 case SNDRV_PCM_IOCTL_START:
2468 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2469 case SNDRV_PCM_IOCTL_LINK:
2470 return snd_pcm_link(substream, (int)(unsigned long) arg);
2471 case SNDRV_PCM_IOCTL_UNLINK:
2472 return snd_pcm_unlink(substream);
2473 case SNDRV_PCM_IOCTL_RESUME:
2474 return snd_pcm_resume(substream);
2475 case SNDRV_PCM_IOCTL_XRUN:
2476 return snd_pcm_xrun(substream);
2477 case SNDRV_PCM_IOCTL_HWSYNC:
2478 return snd_pcm_hwsync(substream);
2479 case SNDRV_PCM_IOCTL_DELAY:
2480 return snd_pcm_delay(substream, arg);
2481 case SNDRV_PCM_IOCTL_SYNC_PTR:
2482 return snd_pcm_sync_ptr(substream, arg);
2483 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2484 return snd_pcm_hw_refine_old_user(substream, arg);
2485 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2486 return snd_pcm_hw_params_old_user(substream, arg);
2487 case SNDRV_PCM_IOCTL_DRAIN:
2488 return snd_pcm_drain(substream);
2489 case SNDRV_PCM_IOCTL_DROP:
2490 return snd_pcm_drop(substream);
2492 snd_printd("unknown ioctl = 0x%x\n", cmd);
2496 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2497 unsigned int cmd, void __user *arg)
2499 snd_assert(substream != NULL, return -ENXIO);
2500 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2502 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2505 snd_xferi_t __user *_xferi = arg;
2506 snd_pcm_runtime_t *runtime = substream->runtime;
2507 snd_pcm_sframes_t result;
2508 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2510 if (put_user(0, &_xferi->result))
2512 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2514 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2515 __put_user(result, &_xferi->result);
2516 return result < 0 ? result : 0;
2518 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2521 snd_xfern_t __user *_xfern = arg;
2522 snd_pcm_runtime_t *runtime = substream->runtime;
2524 snd_pcm_sframes_t result;
2525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2527 if (runtime->channels > 128)
2529 if (put_user(0, &_xfern->result))
2531 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2533 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2536 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2540 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2542 __put_user(result, &_xfern->result);
2543 return result < 0 ? result : 0;
2545 case SNDRV_PCM_IOCTL_REWIND:
2547 snd_pcm_uframes_t frames;
2548 snd_pcm_uframes_t __user *_frames = arg;
2549 snd_pcm_sframes_t result;
2550 if (get_user(frames, _frames))
2552 if (put_user(0, _frames))
2554 result = snd_pcm_playback_rewind(substream, frames);
2555 __put_user(result, _frames);
2556 return result < 0 ? result : 0;
2558 case SNDRV_PCM_IOCTL_FORWARD:
2560 snd_pcm_uframes_t frames;
2561 snd_pcm_uframes_t __user *_frames = arg;
2562 snd_pcm_sframes_t result;
2563 if (get_user(frames, _frames))
2565 if (put_user(0, _frames))
2567 result = snd_pcm_playback_forward(substream, frames);
2568 __put_user(result, _frames);
2569 return result < 0 ? result : 0;
2571 case SNDRV_PCM_IOCTL_PAUSE:
2574 snd_pcm_stream_lock_irq(substream);
2575 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2576 snd_pcm_stream_unlock_irq(substream);
2580 return snd_pcm_common_ioctl1(substream, cmd, arg);
2583 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2584 unsigned int cmd, void __user *arg)
2586 snd_assert(substream != NULL, return -ENXIO);
2587 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2589 case SNDRV_PCM_IOCTL_READI_FRAMES:
2592 snd_xferi_t __user *_xferi = arg;
2593 snd_pcm_runtime_t *runtime = substream->runtime;
2594 snd_pcm_sframes_t result;
2595 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2597 if (put_user(0, &_xferi->result))
2599 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2601 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2602 __put_user(result, &_xferi->result);
2603 return result < 0 ? result : 0;
2605 case SNDRV_PCM_IOCTL_READN_FRAMES:
2608 snd_xfern_t __user *_xfern = arg;
2609 snd_pcm_runtime_t *runtime = substream->runtime;
2611 snd_pcm_sframes_t result;
2612 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2614 if (runtime->channels > 128)
2616 if (put_user(0, &_xfern->result))
2618 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2620 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2623 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2627 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2629 __put_user(result, &_xfern->result);
2630 return result < 0 ? result : 0;
2632 case SNDRV_PCM_IOCTL_REWIND:
2634 snd_pcm_uframes_t frames;
2635 snd_pcm_uframes_t __user *_frames = arg;
2636 snd_pcm_sframes_t result;
2637 if (get_user(frames, _frames))
2639 if (put_user(0, _frames))
2641 result = snd_pcm_capture_rewind(substream, frames);
2642 __put_user(result, _frames);
2643 return result < 0 ? result : 0;
2645 case SNDRV_PCM_IOCTL_FORWARD:
2647 snd_pcm_uframes_t frames;
2648 snd_pcm_uframes_t __user *_frames = arg;
2649 snd_pcm_sframes_t result;
2650 if (get_user(frames, _frames))
2652 if (put_user(0, _frames))
2654 result = snd_pcm_capture_forward(substream, frames);
2655 __put_user(result, _frames);
2656 return result < 0 ? result : 0;
2659 return snd_pcm_common_ioctl1(substream, cmd, arg);
2662 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2664 snd_pcm_file_t *pcm_file;
2666 pcm_file = file->private_data;
2668 if (((cmd >> 8) & 0xff) != 'A')
2671 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2674 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2676 snd_pcm_file_t *pcm_file;
2678 pcm_file = file->private_data;
2680 if (((cmd >> 8) & 0xff) != 'A')
2683 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2686 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2687 unsigned int cmd, void *arg)
2692 fs = snd_enter_user();
2693 result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
2698 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2699 unsigned int cmd, void *arg)
2704 fs = snd_enter_user();
2705 result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
2710 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2711 unsigned int cmd, void *arg)
2713 switch (substream->stream) {
2714 case SNDRV_PCM_STREAM_PLAYBACK:
2715 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2716 case SNDRV_PCM_STREAM_CAPTURE:
2717 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2723 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
2725 snd_pcm_file_t *pcm_file;
2726 snd_pcm_substream_t *substream;
2727 snd_pcm_runtime_t *runtime;
2728 snd_pcm_sframes_t result;
2730 pcm_file = file->private_data;
2731 substream = pcm_file->substream;
2732 snd_assert(substream != NULL, return -ENXIO);
2733 runtime = substream->runtime;
2734 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2736 if (!frame_aligned(runtime, count))
2738 count = bytes_to_frames(runtime, count);
2739 result = snd_pcm_lib_read(substream, buf, count);
2741 result = frames_to_bytes(runtime, result);
2745 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, size_t count, loff_t * offset)
2747 snd_pcm_file_t *pcm_file;
2748 snd_pcm_substream_t *substream;
2749 snd_pcm_runtime_t *runtime;
2750 snd_pcm_sframes_t result;
2752 pcm_file = file->private_data;
2753 substream = pcm_file->substream;
2754 snd_assert(substream != NULL, result = -ENXIO; goto end);
2755 runtime = substream->runtime;
2756 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2760 if (!frame_aligned(runtime, count)) {
2764 count = bytes_to_frames(runtime, count);
2765 result = snd_pcm_lib_write(substream, buf, count);
2767 result = frames_to_bytes(runtime, result);
2772 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2773 unsigned long count, loff_t * offset)
2776 snd_pcm_file_t *pcm_file;
2777 snd_pcm_substream_t *substream;
2778 snd_pcm_runtime_t *runtime;
2779 snd_pcm_sframes_t result;
2782 snd_pcm_uframes_t frames;
2784 pcm_file = file->private_data;
2785 substream = pcm_file->substream;
2786 snd_assert(substream != NULL, return -ENXIO);
2787 runtime = substream->runtime;
2788 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2790 if (count > 1024 || count != runtime->channels)
2792 if (!frame_aligned(runtime, _vector->iov_len))
2794 frames = bytes_to_samples(runtime, _vector->iov_len);
2795 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2798 for (i = 0; i < count; ++i)
2799 bufs[i] = _vector[i].iov_base;
2800 result = snd_pcm_lib_readv(substream, bufs, frames);
2802 result = frames_to_bytes(runtime, result);
2807 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2808 unsigned long count, loff_t * offset)
2810 snd_pcm_file_t *pcm_file;
2811 snd_pcm_substream_t *substream;
2812 snd_pcm_runtime_t *runtime;
2813 snd_pcm_sframes_t result;
2816 snd_pcm_uframes_t frames;
2818 pcm_file = file->private_data;
2819 substream = pcm_file->substream;
2820 snd_assert(substream != NULL, result = -ENXIO; goto end);
2821 runtime = substream->runtime;
2822 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2826 if (count > 128 || count != runtime->channels ||
2827 !frame_aligned(runtime, _vector->iov_len)) {
2831 frames = bytes_to_samples(runtime, _vector->iov_len);
2832 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2835 for (i = 0; i < count; ++i)
2836 bufs[i] = _vector[i].iov_base;
2837 result = snd_pcm_lib_writev(substream, bufs, frames);
2839 result = frames_to_bytes(runtime, result);
2845 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2847 snd_pcm_file_t *pcm_file;
2848 snd_pcm_substream_t *substream;
2849 snd_pcm_runtime_t *runtime;
2851 snd_pcm_uframes_t avail;
2853 pcm_file = file->private_data;
2855 substream = pcm_file->substream;
2856 snd_assert(substream != NULL, return -ENXIO);
2857 runtime = substream->runtime;
2859 poll_wait(file, &runtime->sleep, wait);
2861 snd_pcm_stream_lock_irq(substream);
2862 avail = snd_pcm_playback_avail(runtime);
2863 switch (runtime->status->state) {
2864 case SNDRV_PCM_STATE_RUNNING:
2865 case SNDRV_PCM_STATE_PREPARED:
2866 case SNDRV_PCM_STATE_PAUSED:
2867 if (avail >= runtime->control->avail_min) {
2868 mask = POLLOUT | POLLWRNORM;
2872 case SNDRV_PCM_STATE_DRAINING:
2876 mask = POLLOUT | POLLWRNORM | POLLERR;
2879 snd_pcm_stream_unlock_irq(substream);
2883 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2885 snd_pcm_file_t *pcm_file;
2886 snd_pcm_substream_t *substream;
2887 snd_pcm_runtime_t *runtime;
2889 snd_pcm_uframes_t avail;
2891 pcm_file = file->private_data;
2893 substream = pcm_file->substream;
2894 snd_assert(substream != NULL, return -ENXIO);
2895 runtime = substream->runtime;
2897 poll_wait(file, &runtime->sleep, wait);
2899 snd_pcm_stream_lock_irq(substream);
2900 avail = snd_pcm_capture_avail(runtime);
2901 switch (runtime->status->state) {
2902 case SNDRV_PCM_STATE_RUNNING:
2903 case SNDRV_PCM_STATE_PREPARED:
2904 case SNDRV_PCM_STATE_PAUSED:
2905 if (avail >= runtime->control->avail_min) {
2906 mask = POLLIN | POLLRDNORM;
2911 case SNDRV_PCM_STATE_DRAINING:
2913 mask = POLLIN | POLLRDNORM;
2918 mask = POLLIN | POLLRDNORM | POLLERR;
2921 snd_pcm_stream_unlock_irq(substream);
2930 * Only on coherent architectures, we can mmap the status and the control records
2931 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2933 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2935 * mmap status record
2937 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2939 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2940 snd_pcm_runtime_t *runtime;
2943 if (substream == NULL)
2945 runtime = substream->runtime;
2946 page = virt_to_page(runtime->status);
2947 if (!PageReserved(page))
2950 *type = VM_FAULT_MINOR;
2954 static struct vm_operations_struct snd_pcm_vm_ops_status =
2956 .nopage = snd_pcm_mmap_status_nopage,
2959 static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2960 struct vm_area_struct *area)
2962 snd_pcm_runtime_t *runtime;
2964 if (!(area->vm_flags & VM_READ))
2966 runtime = substream->runtime;
2967 snd_assert(runtime != NULL, return -EAGAIN);
2968 size = area->vm_end - area->vm_start;
2969 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2971 area->vm_ops = &snd_pcm_vm_ops_status;
2972 area->vm_private_data = substream;
2973 area->vm_flags |= VM_RESERVED;
2978 * mmap control record
2980 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2982 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2983 snd_pcm_runtime_t *runtime;
2986 if (substream == NULL)
2988 runtime = substream->runtime;
2989 page = virt_to_page(runtime->control);
2990 if (!PageReserved(page))
2993 *type = VM_FAULT_MINOR;
2997 static struct vm_operations_struct snd_pcm_vm_ops_control =
2999 .nopage = snd_pcm_mmap_control_nopage,
3002 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
3003 struct vm_area_struct *area)
3005 snd_pcm_runtime_t *runtime;
3007 if (!(area->vm_flags & VM_READ))
3009 runtime = substream->runtime;
3010 snd_assert(runtime != NULL, return -EAGAIN);
3011 size = area->vm_end - area->vm_start;
3012 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
3014 area->vm_ops = &snd_pcm_vm_ops_control;
3015 area->vm_private_data = substream;
3016 area->vm_flags |= VM_RESERVED;
3019 #else /* ! coherent mmap */
3021 * don't support mmap for status and control records.
3023 static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
3024 struct vm_area_struct *area)
3028 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
3029 struct vm_area_struct *area)
3033 #endif /* coherent mmap */
3036 * nopage callback for mmapping a RAM page
3038 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
3040 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
3041 snd_pcm_runtime_t *runtime;
3042 unsigned long offset;
3047 if (substream == NULL)
3049 runtime = substream->runtime;
3050 offset = area->vm_pgoff << PAGE_SHIFT;
3051 offset += address - area->vm_start;
3052 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3053 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3054 if (offset > dma_bytes - PAGE_SIZE)
3055 return NOPAGE_SIGBUS;
3056 if (substream->ops->page) {
3057 page = substream->ops->page(substream, offset);
3061 vaddr = runtime->dma_area + offset;
3062 page = virt_to_page(vaddr);
3064 if (!PageReserved(page))
3067 *type = VM_FAULT_MINOR;
3071 static struct vm_operations_struct snd_pcm_vm_ops_data =
3073 .open = snd_pcm_mmap_data_open,
3074 .close = snd_pcm_mmap_data_close,
3075 .nopage = snd_pcm_mmap_data_nopage,
3079 * mmap the DMA buffer on RAM
3081 static int snd_pcm_default_mmap(snd_pcm_substream_t *substream, struct vm_area_struct *area)
3083 area->vm_ops = &snd_pcm_vm_ops_data;
3084 area->vm_private_data = substream;
3085 area->vm_flags |= VM_RESERVED;
3086 atomic_inc(&substream->runtime->mmap_count);
3091 * mmap the DMA buffer on I/O memory area
3093 #if SNDRV_PCM_INFO_MMAP_IOMEM
3094 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3096 .open = snd_pcm_mmap_data_open,
3097 .close = snd_pcm_mmap_data_close,
3100 int snd_pcm_lib_mmap_iomem(snd_pcm_substream_t *substream, struct vm_area_struct *area)
3103 unsigned long offset;
3105 #ifdef pgprot_noncached
3106 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3108 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3109 area->vm_private_data = substream;
3110 area->vm_flags |= VM_IO;
3111 size = area->vm_end - area->vm_start;
3112 offset = area->vm_pgoff << PAGE_SHIFT;
3113 if (io_remap_pfn_range(area, area->vm_start,
3114 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3115 size, area->vm_page_prot))
3117 atomic_inc(&substream->runtime->mmap_count);
3120 #endif /* SNDRV_PCM_INFO_MMAP */
3125 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
3126 struct vm_area_struct *area)
3128 snd_pcm_runtime_t *runtime;
3130 unsigned long offset;
3133 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3134 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3137 if (!(area->vm_flags & VM_READ))
3140 runtime = substream->runtime;
3141 snd_assert(runtime != NULL, return -EAGAIN);
3142 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3144 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3146 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3147 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3149 size = area->vm_end - area->vm_start;
3150 offset = area->vm_pgoff << PAGE_SHIFT;
3151 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3152 if ((size_t)size > dma_bytes)
3154 if (offset > dma_bytes - size)
3157 if (substream->ops->mmap)
3158 return substream->ops->mmap(substream, area);
3160 return snd_pcm_default_mmap(substream, area);
3163 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3165 snd_pcm_file_t * pcm_file;
3166 snd_pcm_substream_t *substream;
3167 unsigned long offset;
3169 pcm_file = file->private_data;
3170 substream = pcm_file->substream;
3171 snd_assert(substream != NULL, return -ENXIO);
3173 offset = area->vm_pgoff << PAGE_SHIFT;
3175 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3176 if (substream->no_mmap_ctrl)
3178 return snd_pcm_mmap_status(substream, file, area);
3179 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3180 if (substream->no_mmap_ctrl)
3182 return snd_pcm_mmap_control(substream, file, area);
3184 return snd_pcm_mmap_data(substream, file, area);
3189 static int snd_pcm_fasync(int fd, struct file * file, int on)
3191 snd_pcm_file_t * pcm_file;
3192 snd_pcm_substream_t *substream;
3193 snd_pcm_runtime_t *runtime;
3196 pcm_file = file->private_data;
3197 substream = pcm_file->substream;
3198 snd_assert(substream != NULL, return -ENXIO);
3199 runtime = substream->runtime;
3201 err = fasync_helper(fd, file, on, &runtime->fasync);
3210 #ifdef CONFIG_COMPAT
3211 #include "pcm_compat.c"
3213 #define snd_pcm_ioctl_compat NULL
3217 * To be removed helpers to keep binary compatibility
3220 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3221 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3223 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
3227 memset(params, 0, sizeof(*params));
3228 params->flags = oparams->flags;
3229 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3230 params->masks[i].bits[0] = oparams->masks[i];
3231 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3232 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3233 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3234 params->info = oparams->info;
3235 params->msbits = oparams->msbits;
3236 params->rate_num = oparams->rate_num;
3237 params->rate_den = oparams->rate_den;
3238 params->fifo_size = oparams->fifo_size;
3241 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3245 memset(oparams, 0, sizeof(*oparams));
3246 oparams->flags = params->flags;
3247 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3248 oparams->masks[i] = params->masks[i].bits[0];
3249 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3250 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3251 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3252 oparams->info = params->info;
3253 oparams->msbits = params->msbits;
3254 oparams->rate_num = params->rate_num;
3255 oparams->rate_den = params->rate_den;
3256 oparams->fifo_size = params->fifo_size;
3259 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
3261 snd_pcm_hw_params_t *params;
3262 struct sndrv_pcm_hw_params_old *oparams = NULL;
3265 params = kmalloc(sizeof(*params), GFP_KERNEL);
3270 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3276 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3280 snd_pcm_hw_convert_from_old_params(params, oparams);
3281 err = snd_pcm_hw_refine(substream, params);
3282 snd_pcm_hw_convert_to_old_params(oparams, params);
3283 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3293 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
3295 snd_pcm_hw_params_t *params;
3296 struct sndrv_pcm_hw_params_old *oparams = NULL;
3299 params = kmalloc(sizeof(*params), GFP_KERNEL);
3304 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3309 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3313 snd_pcm_hw_convert_from_old_params(params, oparams);
3314 err = snd_pcm_hw_params(substream, params);
3315 snd_pcm_hw_convert_to_old_params(oparams, params);
3316 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3330 static struct file_operations snd_pcm_f_ops_playback = {
3331 .owner = THIS_MODULE,
3332 .write = snd_pcm_write,
3333 .writev = snd_pcm_writev,
3334 .open = snd_pcm_open,
3335 .release = snd_pcm_release,
3336 .poll = snd_pcm_playback_poll,
3337 .unlocked_ioctl = snd_pcm_playback_ioctl,
3338 .compat_ioctl = snd_pcm_ioctl_compat,
3339 .mmap = snd_pcm_mmap,
3340 .fasync = snd_pcm_fasync,
3343 static struct file_operations snd_pcm_f_ops_capture = {
3344 .owner = THIS_MODULE,
3345 .read = snd_pcm_read,
3346 .readv = snd_pcm_readv,
3347 .open = snd_pcm_open,
3348 .release = snd_pcm_release,
3349 .poll = snd_pcm_capture_poll,
3350 .unlocked_ioctl = snd_pcm_capture_ioctl,
3351 .compat_ioctl = snd_pcm_ioctl_compat,
3352 .mmap = snd_pcm_mmap,
3353 .fasync = snd_pcm_fasync,
3356 snd_minor_t snd_pcm_reg[2] =
3359 .comment = "digital audio playback",
3360 .f_ops = &snd_pcm_f_ops_playback,
3363 .comment = "digital audio capture",
3364 .f_ops = &snd_pcm_f_ops_capture,