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 rwlock_t snd_pcm_link_rwlock = RW_LOCK_UNLOCKED;
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)
117 int err = snd_pcm_info(substream, &info);
118 if (copy_to_user(_info, &info, sizeof(info)))
126 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
127 char *snd_pcm_hw_param_names[] = {
131 HW_PARAM(SAMPLE_BITS),
132 HW_PARAM(FRAME_BITS),
135 HW_PARAM(PERIOD_TIME),
136 HW_PARAM(PERIOD_SIZE),
137 HW_PARAM(PERIOD_BYTES),
139 HW_PARAM(BUFFER_TIME),
140 HW_PARAM(BUFFER_SIZE),
141 HW_PARAM(BUFFER_BYTES),
146 int snd_pcm_hw_refine(snd_pcm_substream_t *substream,
147 snd_pcm_hw_params_t *params)
150 snd_pcm_hardware_t *hw;
151 snd_interval_t *i = NULL;
152 snd_mask_t *m = NULL;
153 snd_pcm_hw_constraints_t *constrs = &substream->runtime->hw_constraints;
154 unsigned int rstamps[constrs->rules_num];
155 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
156 unsigned int stamp = 2;
160 params->fifo_size = 0;
161 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
163 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
164 params->rate_num = 0;
165 params->rate_den = 0;
168 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
169 m = hw_param_mask(params, k);
170 if (snd_mask_empty(m))
172 if (!(params->rmask & (1 << k)))
175 printk("%s = ", snd_pcm_hw_param_names[k]);
176 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
178 changed = snd_mask_refine(m, constrs_mask(constrs, k));
180 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
183 params->cmask |= 1 << k;
188 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
189 i = hw_param_interval(params, k);
190 if (snd_interval_empty(i))
192 if (!(params->rmask & (1 << k)))
195 printk("%s = ", snd_pcm_hw_param_names[k]);
200 i->openmin ? '(' : '[', i->min,
201 i->max, i->openmax ? ')' : ']');
204 changed = snd_interval_refine(i, constrs_interval(constrs, k));
209 printk("%c%u %u%c\n",
210 i->openmin ? '(' : '[', i->min,
211 i->max, i->openmax ? ')' : ']');
214 params->cmask |= 1 << k;
219 for (k = 0; k < constrs->rules_num; k++)
221 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
222 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
225 for (k = 0; k < constrs->rules_num; k++) {
226 snd_pcm_hw_rule_t *r = &constrs->rules[k];
229 if (r->cond && !(r->cond & params->flags))
231 for (d = 0; r->deps[d] >= 0; d++) {
232 if (vstamps[r->deps[d]] > rstamps[k]) {
240 printk("Rule %d [%p]: ", k, r->func);
242 printk("%s = ", snd_pcm_hw_param_names[r->var]);
243 if (hw_is_mask(r->var)) {
244 m = hw_param_mask(params, r->var);
245 printk("%x", *m->bits);
247 i = hw_param_interval(params, r->var);
252 i->openmin ? '(' : '[', i->min,
253 i->max, i->openmax ? ')' : ']');
257 changed = r->func(params, r);
261 if (hw_is_mask(r->var))
262 printk("%x", *m->bits);
268 i->openmin ? '(' : '[', i->min,
269 i->max, i->openmax ? ')' : ']');
275 if (changed && r->var >= 0) {
276 params->cmask |= (1 << r->var);
277 vstamps[r->var] = stamp;
285 if (!params->msbits) {
286 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
287 if (snd_interval_single(i))
288 params->msbits = snd_interval_value(i);
291 if (!params->rate_den) {
292 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
293 if (snd_interval_single(i)) {
294 params->rate_num = snd_interval_value(i);
295 params->rate_den = 1;
299 hw = &substream->runtime->hw;
301 params->info = hw->info;
302 if (!params->fifo_size)
303 params->fifo_size = hw->fifo_size;
308 static int snd_pcm_hw_refine_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t __user * _params)
310 snd_pcm_hw_params_t *params;
313 params = kmalloc(sizeof(*params), GFP_KERNEL);
318 if (copy_from_user(params, _params, sizeof(*params))) {
322 err = snd_pcm_hw_refine(substream, params);
323 if (copy_to_user(_params, params, sizeof(*params))) {
332 static int snd_pcm_hw_params(snd_pcm_substream_t *substream,
333 snd_pcm_hw_params_t *params)
335 snd_pcm_runtime_t *runtime;
338 snd_pcm_uframes_t frames;
340 snd_assert(substream != NULL, return -ENXIO);
341 runtime = substream->runtime;
342 snd_assert(runtime != NULL, return -ENXIO);
343 snd_pcm_stream_lock_irq(substream);
344 switch (runtime->status->state) {
345 case SNDRV_PCM_STATE_OPEN:
346 case SNDRV_PCM_STATE_SETUP:
347 case SNDRV_PCM_STATE_PREPARED:
350 snd_pcm_stream_unlock_irq(substream);
353 snd_pcm_stream_unlock_irq(substream);
354 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
355 if (!substream->oss.oss)
357 if (atomic_read(&runtime->mmap_count))
361 err = snd_pcm_hw_refine(substream, params);
365 err = snd_pcm_hw_params_choose(substream, params);
369 if (substream->ops->hw_params != NULL) {
370 err = substream->ops->hw_params(substream, params);
375 runtime->access = params_access(params);
376 runtime->format = params_format(params);
377 runtime->subformat = params_subformat(params);
378 runtime->channels = params_channels(params);
379 runtime->rate = params_rate(params);
380 runtime->period_size = params_period_size(params);
381 runtime->periods = params_periods(params);
382 runtime->buffer_size = params_buffer_size(params);
383 runtime->tick_time = params_tick_time(params);
384 runtime->info = params->info;
385 runtime->rate_num = params->rate_num;
386 runtime->rate_den = params->rate_den;
388 bits = snd_pcm_format_physical_width(runtime->format);
389 runtime->sample_bits = bits;
390 bits *= runtime->channels;
391 runtime->frame_bits = bits;
393 while (bits % 8 != 0) {
397 runtime->byte_align = bits / 8;
398 runtime->min_align = frames;
400 /* Default sw params */
401 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
402 runtime->period_step = 1;
403 runtime->sleep_min = 0;
404 runtime->control->avail_min = runtime->period_size;
405 runtime->xfer_align = runtime->period_size;
406 runtime->start_threshold = 1;
407 runtime->stop_threshold = runtime->buffer_size;
408 runtime->silence_threshold = 0;
409 runtime->silence_size = 0;
410 runtime->boundary = runtime->buffer_size;
411 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
412 runtime->boundary *= 2;
414 snd_pcm_timer_resolution_change(substream);
415 runtime->status->state = SNDRV_PCM_STATE_SETUP;
418 /* hardware might be unuseable from this time,
419 so we force application to retry to set
420 the correct hardware parameter settings */
421 runtime->status->state = SNDRV_PCM_STATE_OPEN;
422 if (substream->ops->hw_free != NULL)
423 substream->ops->hw_free(substream);
427 static int snd_pcm_hw_params_user(snd_pcm_substream_t * substream, snd_pcm_hw_params_t __user * _params)
429 snd_pcm_hw_params_t *params;
432 params = kmalloc(sizeof(*params), GFP_KERNEL);
437 if (copy_from_user(params, _params, sizeof(*params))) {
441 err = snd_pcm_hw_params(substream, params);
442 if (copy_to_user(_params, params, sizeof(*params))) {
451 static int snd_pcm_hw_free(snd_pcm_substream_t * substream)
453 snd_pcm_runtime_t *runtime;
456 snd_assert(substream != NULL, return -ENXIO);
457 runtime = substream->runtime;
458 snd_assert(runtime != NULL, return -ENXIO);
459 snd_pcm_stream_lock_irq(substream);
460 switch (runtime->status->state) {
461 case SNDRV_PCM_STATE_SETUP:
462 case SNDRV_PCM_STATE_PREPARED:
465 snd_pcm_stream_unlock_irq(substream);
468 snd_pcm_stream_unlock_irq(substream);
469 if (atomic_read(&runtime->mmap_count))
471 if (substream->ops->hw_free == NULL) {
472 runtime->status->state = SNDRV_PCM_STATE_OPEN;
475 result = substream->ops->hw_free(substream);
476 runtime->status->state = SNDRV_PCM_STATE_OPEN;
480 static int snd_pcm_sw_params(snd_pcm_substream_t * substream, snd_pcm_sw_params_t *params)
482 snd_pcm_runtime_t *runtime;
484 snd_assert(substream != NULL, return -ENXIO);
485 runtime = substream->runtime;
486 snd_assert(runtime != NULL, return -ENXIO);
487 snd_pcm_stream_lock_irq(substream);
488 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
489 snd_pcm_stream_unlock_irq(substream);
492 snd_pcm_stream_unlock_irq(substream);
494 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
496 if (params->avail_min == 0)
498 if (params->xfer_align == 0 ||
499 params->xfer_align % runtime->min_align != 0)
501 if (params->silence_size >= runtime->boundary) {
502 if (params->silence_threshold != 0)
505 if (params->silence_size > params->silence_threshold)
507 if (params->silence_threshold > runtime->buffer_size)
510 snd_pcm_stream_lock_irq(substream);
511 runtime->tstamp_mode = params->tstamp_mode;
512 runtime->sleep_min = params->sleep_min;
513 runtime->period_step = params->period_step;
514 runtime->control->avail_min = params->avail_min;
515 runtime->start_threshold = params->start_threshold;
516 runtime->stop_threshold = params->stop_threshold;
517 runtime->silence_threshold = params->silence_threshold;
518 runtime->silence_size = params->silence_size;
519 runtime->xfer_align = params->xfer_align;
520 params->boundary = runtime->boundary;
521 if (snd_pcm_running(substream)) {
522 if (runtime->sleep_min)
523 snd_pcm_tick_prepare(substream);
525 snd_pcm_tick_set(substream, 0);
526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
527 runtime->silence_size > 0)
528 snd_pcm_playback_silence(substream, ULONG_MAX);
529 wake_up(&runtime->sleep);
531 snd_pcm_stream_unlock_irq(substream);
535 static int snd_pcm_sw_params_user(snd_pcm_substream_t * substream, snd_pcm_sw_params_t __user * _params)
537 snd_pcm_sw_params_t params;
539 if (copy_from_user(¶ms, _params, sizeof(params)))
541 err = snd_pcm_sw_params(substream, ¶ms);
542 if (copy_to_user(_params, ¶ms, sizeof(params)))
547 int snd_pcm_status(snd_pcm_substream_t *substream,
548 snd_pcm_status_t *status)
550 snd_pcm_runtime_t *runtime = substream->runtime;
552 snd_pcm_stream_lock_irq(substream);
553 status->state = runtime->status->state;
554 status->suspended_state = runtime->status->suspended_state;
555 if (status->state == SNDRV_PCM_STATE_OPEN)
557 status->trigger_tstamp = runtime->trigger_tstamp;
558 if (snd_pcm_running(substream)) {
559 snd_pcm_update_hw_ptr(substream);
560 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
561 status->tstamp = runtime->status->tstamp;
563 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
565 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
566 status->appl_ptr = runtime->control->appl_ptr;
567 status->hw_ptr = runtime->status->hw_ptr;
568 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
569 status->avail = snd_pcm_playback_avail(runtime);
570 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
571 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
572 status->delay = runtime->buffer_size - status->avail;
576 status->avail = snd_pcm_capture_avail(runtime);
577 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
578 status->delay = status->avail;
582 status->avail_max = runtime->avail_max;
583 status->overrange = runtime->overrange;
584 runtime->avail_max = 0;
585 runtime->overrange = 0;
587 snd_pcm_stream_unlock_irq(substream);
591 static int snd_pcm_status_user(snd_pcm_substream_t * substream, snd_pcm_status_t __user * _status)
593 snd_pcm_status_t status;
594 snd_pcm_runtime_t *runtime;
597 snd_assert(substream != NULL, return -ENXIO);
598 runtime = substream->runtime;
599 memset(&status, 0, sizeof(status));
600 res = snd_pcm_status(substream, &status);
603 if (copy_to_user(_status, &status, sizeof(status)))
608 static int snd_pcm_channel_info(snd_pcm_substream_t * substream, snd_pcm_channel_info_t __user * _info)
610 snd_pcm_channel_info_t info;
611 snd_pcm_runtime_t *runtime;
613 unsigned int channel;
615 snd_assert(substream != NULL, return -ENXIO);
616 if (copy_from_user(&info, _info, sizeof(info)))
618 channel = info.channel;
619 runtime = substream->runtime;
620 snd_pcm_stream_lock_irq(substream);
621 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
622 snd_pcm_stream_unlock_irq(substream);
625 snd_pcm_stream_unlock_irq(substream);
626 if (channel >= runtime->channels)
628 memset(&info, 0, sizeof(info));
629 info.channel = channel;
630 res = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, &info);
633 if (copy_to_user(_info, &info, sizeof(info)))
638 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t *substream)
640 snd_pcm_runtime_t *runtime = substream->runtime;
641 if (runtime->trigger_master == NULL)
643 if (runtime->trigger_master == substream) {
644 snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
646 snd_pcm_trigger_tstamp(runtime->trigger_master);
647 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
649 runtime->trigger_master = NULL;
653 int (*pre_action)(snd_pcm_substream_t *substream, int state);
654 int (*do_action)(snd_pcm_substream_t *substream, int state);
655 void (*post_action)(snd_pcm_substream_t *substream, int state);
659 * this functions is core for handling of linked stream
660 * Note: the stream state might be changed also on failure
661 * Note2: call with calling stream lock + link lock
663 static int snd_pcm_action_group(struct action_ops *ops,
664 snd_pcm_substream_t *substream,
665 int state, int do_lock)
667 struct list_head *pos;
668 snd_pcm_substream_t *s = NULL;
671 snd_pcm_group_for_each(pos, substream) {
672 s = snd_pcm_group_substream_entry(pos);
673 if (do_lock && s != substream)
674 spin_lock(&s->self_group.lock);
675 res = ops->pre_action(s, state);
680 snd_pcm_group_for_each(pos, substream) {
681 s = snd_pcm_group_substream_entry(pos);
682 err = ops->do_action(s, state);
687 ops->post_action(s, state);
689 if (do_lock && s != substream)
690 spin_unlock(&s->self_group.lock);
692 } else if (do_lock) {
693 snd_pcm_substream_t *s1;
694 /* unlock all streams */
695 snd_pcm_group_for_each(pos, substream) {
696 s1 = snd_pcm_group_substream_entry(pos);
698 spin_unlock(&s1->self_group.lock);
699 if (s1 == s) /* end */
707 * Note: call with stream lock
709 static int snd_pcm_action_single(struct action_ops *ops,
710 snd_pcm_substream_t *substream,
715 res = ops->pre_action(substream, state);
718 res = ops->do_action(substream, state);
720 ops->post_action(substream, state);
726 * Note: call with stream lock
728 static int snd_pcm_action(struct action_ops *ops,
729 snd_pcm_substream_t *substream,
734 if (snd_pcm_stream_linked(substream)) {
735 if (!spin_trylock(&substream->group->lock)) {
736 spin_unlock(&substream->self_group.lock);
737 spin_lock(&substream->group->lock);
738 spin_lock(&substream->self_group.lock);
740 res = snd_pcm_action_group(ops, substream, state, 1);
741 spin_unlock(&substream->group->lock);
743 res = snd_pcm_action_single(ops, substream, state);
749 * Note: don't use any locks before
751 static int snd_pcm_action_lock_irq(struct action_ops *ops,
752 snd_pcm_substream_t *substream,
757 read_lock_irq(&snd_pcm_link_rwlock);
758 if (snd_pcm_stream_linked(substream)) {
759 spin_lock(&substream->group->lock);
760 spin_lock(&substream->self_group.lock);
761 res = snd_pcm_action_group(ops, substream, state, 1);
762 spin_unlock(&substream->self_group.lock);
763 spin_unlock(&substream->group->lock);
765 spin_lock(&substream->self_group.lock);
766 res = snd_pcm_action_single(ops, substream, state);
767 spin_unlock(&substream->self_group.lock);
769 read_unlock_irq(&snd_pcm_link_rwlock);
775 static int snd_pcm_action_nonatomic(struct action_ops *ops,
776 snd_pcm_substream_t *substream,
781 down_read(&snd_pcm_link_rwsem);
782 if (snd_pcm_stream_linked(substream))
783 res = snd_pcm_action_group(ops, substream, state, 0);
785 res = snd_pcm_action_single(ops, substream, state);
786 up_read(&snd_pcm_link_rwsem);
790 static int snd_pcm_pre_start(snd_pcm_substream_t *substream, int state)
792 snd_pcm_runtime_t *runtime = substream->runtime;
793 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
795 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
796 !snd_pcm_playback_data(substream))
798 runtime->trigger_master = substream;
802 static int snd_pcm_do_start(snd_pcm_substream_t *substream, int state)
804 if (substream->runtime->trigger_master != substream)
806 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
809 static void snd_pcm_post_start(snd_pcm_substream_t *substream, int state)
811 snd_pcm_runtime_t *runtime = substream->runtime;
812 snd_pcm_trigger_tstamp(substream);
813 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
814 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
815 runtime->silence_size > 0)
816 snd_pcm_playback_silence(substream, ULONG_MAX);
817 if (runtime->sleep_min)
818 snd_pcm_tick_prepare(substream);
819 if (substream->timer)
820 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART, &runtime->trigger_tstamp);
823 static struct action_ops snd_pcm_action_start = {
824 .pre_action = snd_pcm_pre_start,
825 .do_action = snd_pcm_do_start,
826 .post_action = snd_pcm_post_start
832 int snd_pcm_start(snd_pcm_substream_t *substream)
834 return snd_pcm_action(&snd_pcm_action_start, substream, 0);
837 static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
839 snd_pcm_runtime_t *runtime = substream->runtime;
840 if (substream->runtime->status->state != SNDRV_PCM_STATE_RUNNING &&
841 substream->runtime->status->state != SNDRV_PCM_STATE_DRAINING)
843 runtime->trigger_master = substream;
847 static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
849 if (substream->runtime->trigger_master != substream)
851 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
854 static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
856 snd_pcm_runtime_t *runtime = substream->runtime;
857 snd_pcm_trigger_tstamp(substream);
858 if (substream->timer)
859 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
860 runtime->status->state = state;
861 snd_pcm_tick_set(substream, 0);
862 wake_up(&runtime->sleep);
865 static struct action_ops snd_pcm_action_stop = {
866 .pre_action = snd_pcm_pre_stop,
867 .do_action = snd_pcm_do_stop,
868 .post_action = snd_pcm_post_stop
874 int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
876 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
879 static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
881 snd_pcm_runtime_t *runtime = substream->runtime;
882 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
885 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
887 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
889 runtime->trigger_master = substream;
893 static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
895 if (substream->runtime->trigger_master != substream)
897 return substream->ops->trigger(substream,
898 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
899 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
902 static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
904 snd_pcm_runtime_t *runtime = substream->runtime;
905 snd_pcm_trigger_tstamp(substream);
907 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
908 if (substream->timer)
909 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
910 snd_pcm_tick_set(substream, 0);
911 wake_up(&runtime->sleep);
913 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
914 if (runtime->sleep_min)
915 snd_pcm_tick_prepare(substream);
916 if (substream->timer)
917 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
921 static struct action_ops snd_pcm_action_pause = {
922 .pre_action = snd_pcm_pre_pause,
923 .do_action = snd_pcm_do_pause,
924 .post_action = snd_pcm_post_pause
927 static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
929 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
935 static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
937 snd_pcm_runtime_t *runtime = substream->runtime;
938 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
940 runtime->status->suspended_state = runtime->status->state;
941 runtime->trigger_master = substream;
945 static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
947 snd_pcm_runtime_t *runtime = substream->runtime;
948 if (runtime->trigger_master != substream)
950 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
951 runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING)
953 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
956 static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
958 snd_pcm_runtime_t *runtime = substream->runtime;
959 snd_pcm_trigger_tstamp(substream);
960 if (substream->timer)
961 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
962 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
963 snd_pcm_tick_set(substream, 0);
964 wake_up(&runtime->sleep);
967 static struct action_ops snd_pcm_action_suspend = {
968 .pre_action = snd_pcm_pre_suspend,
969 .do_action = snd_pcm_do_suspend,
970 .post_action = snd_pcm_post_suspend
976 int snd_pcm_suspend(snd_pcm_substream_t *substream)
978 return snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
982 * snd_pcm_suspend_all
984 int snd_pcm_suspend_all(snd_pcm_t *pcm)
986 snd_pcm_substream_t *substream;
989 for (stream = 0; stream < 2; stream++) {
990 for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
991 /* FIXME: the open/close code should lock this as well */
992 if (substream->runtime == NULL)
994 snd_pcm_stream_lock(substream);
995 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
996 snd_pcm_stream_unlock(substream);
999 if ((err = snd_pcm_suspend(substream)) < 0) {
1000 snd_pcm_stream_unlock(substream);
1003 snd_pcm_stream_unlock(substream);
1011 static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
1013 snd_pcm_runtime_t *runtime = substream->runtime;
1014 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1016 runtime->trigger_master = substream;
1020 static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
1022 snd_pcm_runtime_t *runtime = substream->runtime;
1023 if (runtime->trigger_master != substream)
1025 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1026 runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING)
1028 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1031 static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
1033 snd_pcm_runtime_t *runtime = substream->runtime;
1034 snd_pcm_trigger_tstamp(substream);
1035 if (substream->timer)
1036 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
1037 runtime->status->state = runtime->status->suspended_state;
1038 if (runtime->sleep_min)
1039 snd_pcm_tick_prepare(substream);
1042 static struct action_ops snd_pcm_action_resume = {
1043 .pre_action = snd_pcm_pre_resume,
1044 .do_action = snd_pcm_do_resume,
1045 .post_action = snd_pcm_post_resume
1048 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1050 snd_card_t *card = substream->pcm->card;
1053 snd_power_lock(card);
1054 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1055 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1056 snd_power_unlock(card);
1062 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1067 #endif /* CONFIG_PM */
1069 static int snd_pcm_xrun(snd_pcm_substream_t *substream)
1071 snd_card_t *card = substream->pcm->card;
1072 snd_pcm_runtime_t *runtime = substream->runtime;
1075 snd_power_lock(card);
1076 snd_pcm_stream_lock_irq(substream);
1078 switch (runtime->status->state) {
1079 case SNDRV_PCM_STATE_XRUN:
1080 result = 0; /* already there */
1082 case SNDRV_PCM_STATE_RUNNING:
1083 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1085 case SNDRV_PCM_STATE_SUSPENDED:
1086 snd_pcm_stream_unlock_irq(substream);
1087 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1088 snd_pcm_stream_lock_irq(substream);
1090 goto _xrun_recovery;
1095 snd_pcm_stream_unlock_irq(substream);
1096 snd_power_unlock(card);
1100 static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
1102 snd_pcm_runtime_t *runtime = substream->runtime;
1103 switch (runtime->status->state) {
1104 case SNDRV_PCM_STATE_RUNNING:
1105 case SNDRV_PCM_STATE_PREPARED:
1106 case SNDRV_PCM_STATE_PAUSED:
1107 case SNDRV_PCM_STATE_SUSPENDED:
1114 static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
1116 snd_pcm_runtime_t *runtime = substream->runtime;
1117 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1120 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1121 runtime->hw_ptr_base = 0;
1122 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
1123 runtime->silence_start = runtime->status->hw_ptr;
1124 runtime->silence_filled = 0;
1128 static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
1130 snd_pcm_runtime_t *runtime = substream->runtime;
1131 runtime->control->appl_ptr = runtime->status->hw_ptr;
1132 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1133 runtime->silence_size > 0)
1134 snd_pcm_playback_silence(substream, ULONG_MAX);
1137 static struct action_ops snd_pcm_action_reset = {
1138 .pre_action = snd_pcm_pre_reset,
1139 .do_action = snd_pcm_do_reset,
1140 .post_action = snd_pcm_post_reset
1143 static int snd_pcm_reset(snd_pcm_substream_t *substream)
1145 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1148 static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
1150 snd_pcm_runtime_t *runtime = substream->runtime;
1151 switch (runtime->status->state) {
1152 case SNDRV_PCM_STATE_OPEN:
1154 case SNDRV_PCM_STATE_RUNNING:
1161 static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
1164 err = substream->ops->prepare(substream);
1167 return snd_pcm_do_reset(substream, 0);
1170 static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
1172 snd_pcm_runtime_t *runtime = substream->runtime;
1173 runtime->control->appl_ptr = runtime->status->hw_ptr;
1174 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1177 static struct action_ops snd_pcm_action_prepare = {
1178 .pre_action = snd_pcm_pre_prepare,
1179 .do_action = snd_pcm_do_prepare,
1180 .post_action = snd_pcm_post_prepare
1186 int snd_pcm_prepare(snd_pcm_substream_t *substream)
1189 snd_card_t *card = substream->pcm->card;
1191 snd_power_lock(card);
1192 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1193 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1194 snd_power_unlock(card);
1198 static void snd_pcm_change_state(snd_pcm_substream_t *substream, int state)
1200 struct list_head *pos;
1201 snd_pcm_substream_t *s;
1203 if (snd_pcm_stream_linked(substream)) {
1204 if (!spin_trylock(&substream->group->lock)) {
1205 spin_unlock(&substream->self_group.lock);
1206 spin_lock(&substream->group->lock);
1207 spin_lock(&substream->self_group.lock);
1209 snd_pcm_group_for_each(pos, substream) {
1210 s = snd_pcm_group_substream_entry(pos);
1212 spin_lock(&s->self_group.lock);
1213 s->runtime->status->state = state;
1215 spin_unlock(&s->self_group.lock);
1217 spin_unlock(&substream->group->lock);
1219 substream->runtime->status->state = state;
1223 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream);
1225 static int snd_pcm_playback_drain(snd_pcm_substream_t * substream)
1228 snd_pcm_runtime_t *runtime;
1229 int err, result = 0;
1231 enum { READY, EXPIRED, SUSPENDED, SIGNALED } state = READY;
1232 snd_pcm_uframes_t stop_threshold;
1234 snd_assert(substream != NULL, return -ENXIO);
1235 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
1236 runtime = substream->runtime;
1237 card = substream->pcm->card;
1239 snd_power_lock(card);
1240 snd_pcm_stream_lock_irq(substream);
1242 /* stop_threshold fixup to avoid endless loop when */
1243 /* stop_threshold > buffer_size */
1244 stop_threshold = runtime->stop_threshold;
1245 if (runtime->stop_threshold > runtime->buffer_size)
1246 runtime->stop_threshold = runtime->buffer_size;
1248 switch (runtime->status->state) {
1249 case SNDRV_PCM_STATE_PAUSED:
1250 snd_pcm_pause(substream, 0);
1252 case SNDRV_PCM_STATE_RUNNING:
1253 case SNDRV_PCM_STATE_DRAINING:
1255 case SNDRV_PCM_STATE_SUSPENDED:
1256 snd_pcm_stream_unlock_irq(substream);
1257 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1258 snd_pcm_stream_lock_irq(substream);
1260 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1262 case SNDRV_PCM_STATE_OPEN:
1265 case SNDRV_PCM_STATE_PREPARED:
1266 if (!snd_pcm_playback_empty(substream)) {
1267 err = snd_pcm_start(substream);
1275 case SNDRV_PCM_STATE_XRUN:
1276 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1278 case SNDRV_PCM_STATE_SETUP:
1284 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1285 if (snd_pcm_playback_empty(substream)) {
1286 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1289 snd_pcm_change_state(substream, SNDRV_PCM_STATE_DRAINING);
1292 if (substream->ffile->f_flags & O_NONBLOCK) {
1297 init_waitqueue_entry(&wait, current);
1298 add_wait_queue(&runtime->sleep, &wait);
1301 if (signal_pending(current)) {
1305 set_current_state(TASK_INTERRUPTIBLE);
1306 snd_pcm_stream_unlock_irq(substream);
1307 snd_power_unlock(card);
1308 tout = schedule_timeout(10 * HZ);
1309 snd_power_lock(card);
1310 snd_pcm_stream_lock_irq(substream);
1312 state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
1315 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING) {
1320 remove_wait_queue(&runtime->sleep, &wait);
1324 result = -ERESTARTSYS;
1330 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1338 runtime->stop_threshold = stop_threshold;
1339 snd_pcm_stream_unlock_irq(substream);
1340 snd_power_unlock(card);
1341 if (state == EXPIRED)
1342 snd_pcm_playback_drop(substream);
1347 static int snd_pcm_playback_drop(snd_pcm_substream_t *substream)
1349 snd_pcm_runtime_t *runtime = substream->runtime;
1350 snd_card_t *card = substream->pcm->card;
1353 snd_power_lock(card);
1354 snd_pcm_stream_lock_irq(substream);
1355 switch (runtime->status->state) {
1356 case SNDRV_PCM_STATE_OPEN:
1359 case SNDRV_PCM_STATE_SETUP:
1361 case SNDRV_PCM_STATE_PAUSED:
1362 snd_pcm_pause(substream, 0);
1364 case SNDRV_PCM_STATE_RUNNING:
1365 case SNDRV_PCM_STATE_DRAINING:
1366 if (snd_pcm_update_hw_ptr(substream) >= 0) {
1367 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1371 case SNDRV_PCM_STATE_PREPARED:
1372 case SNDRV_PCM_STATE_XRUN:
1373 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1375 case SNDRV_PCM_STATE_SUSPENDED:
1376 snd_pcm_stream_unlock_irq(substream);
1377 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1378 snd_pcm_stream_lock_irq(substream);
1380 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1385 runtime->control->appl_ptr = runtime->status->hw_ptr;
1386 snd_pcm_stream_unlock_irq(substream);
1387 snd_power_unlock(card);
1391 static int snd_pcm_capture_drain(snd_pcm_substream_t * substream)
1393 snd_pcm_runtime_t *runtime = substream->runtime;
1394 snd_card_t *card = substream->pcm->card;
1397 snd_power_lock(card);
1398 snd_pcm_stream_lock_irq(substream);
1399 switch (runtime->status->state) {
1400 case SNDRV_PCM_STATE_OPEN:
1403 case SNDRV_PCM_STATE_PREPARED:
1404 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1406 case SNDRV_PCM_STATE_SETUP:
1407 case SNDRV_PCM_STATE_DRAINING:
1409 case SNDRV_PCM_STATE_PAUSED:
1410 snd_pcm_pause(substream, 0);
1412 case SNDRV_PCM_STATE_RUNNING:
1413 if (snd_pcm_update_hw_ptr(substream) >= 0) {
1414 snd_pcm_stop(substream,
1415 snd_pcm_capture_avail(runtime) > 0 ?
1416 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP);
1420 case SNDRV_PCM_STATE_XRUN:
1422 snd_pcm_change_state(substream,
1423 snd_pcm_capture_avail(runtime) > 0 ?
1424 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP);
1426 case SNDRV_PCM_STATE_SUSPENDED:
1427 snd_pcm_stream_unlock_irq(substream);
1428 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1429 snd_pcm_stream_lock_irq(substream);
1431 goto _xrun_recovery;
1436 snd_pcm_stream_unlock_irq(substream);
1437 snd_power_unlock(card);
1441 static int snd_pcm_capture_drop(snd_pcm_substream_t * substream)
1443 snd_pcm_runtime_t *runtime = substream->runtime;
1444 snd_card_t *card = substream->pcm->card;
1447 snd_power_lock(card);
1448 snd_pcm_stream_lock_irq(substream);
1449 switch (runtime->status->state) {
1450 case SNDRV_PCM_STATE_OPEN:
1453 case SNDRV_PCM_STATE_SETUP:
1455 case SNDRV_PCM_STATE_PAUSED:
1456 snd_pcm_pause(substream, 0);
1458 case SNDRV_PCM_STATE_RUNNING:
1459 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1461 case SNDRV_PCM_STATE_SUSPENDED:
1462 snd_pcm_stream_unlock_irq(substream);
1463 res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1464 snd_pcm_stream_lock_irq(substream);
1468 case SNDRV_PCM_STATE_PREPARED:
1469 case SNDRV_PCM_STATE_DRAINING:
1470 case SNDRV_PCM_STATE_XRUN:
1471 snd_pcm_change_state(substream, SNDRV_PCM_STATE_SETUP);
1476 runtime->control->appl_ptr = runtime->status->hw_ptr;
1478 snd_pcm_stream_unlock_irq(substream);
1479 snd_power_unlock(card);
1483 /* WARNING: Don't forget to fput back the file */
1484 extern int snd_major;
1485 static struct file *snd_pcm_file_fd(int fd)
1488 struct inode *inode;
1489 unsigned short minor;
1493 inode = file->f_dentry->d_inode;
1494 if (!S_ISCHR(inode->i_mode) ||
1495 imajor(inode) != snd_major) {
1499 minor = iminor(inode);
1501 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1508 static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
1512 snd_pcm_file_t *pcm_file;
1513 snd_pcm_substream_t *substream1;
1515 snd_pcm_stream_lock_irq(substream);
1516 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1517 snd_pcm_stream_unlock_irq(substream);
1520 snd_pcm_stream_unlock_irq(substream);
1521 file = snd_pcm_file_fd(fd);
1524 pcm_file = file->private_data;
1525 substream1 = pcm_file->substream;
1526 down_write(&snd_pcm_link_rwsem);
1527 write_lock_irq(&snd_pcm_link_rwlock);
1528 if (substream->runtime->status->state != substream1->runtime->status->state) {
1532 if (snd_pcm_stream_linked(substream1)) {
1536 if (!snd_pcm_stream_linked(substream)) {
1537 substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
1538 if (substream->group == NULL) {
1542 spin_lock_init(&substream->group->lock);
1543 INIT_LIST_HEAD(&substream->group->substreams);
1544 list_add_tail(&substream->link_list, &substream->group->substreams);
1546 list_add_tail(&substream1->link_list, &substream->group->substreams);
1547 substream1->group = substream->group;
1549 write_unlock_irq(&snd_pcm_link_rwlock);
1550 up_write(&snd_pcm_link_rwsem);
1555 static void relink_to_local(snd_pcm_substream_t *substream)
1557 substream->group = &substream->self_group;
1558 INIT_LIST_HEAD(&substream->self_group.substreams);
1559 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1562 static int snd_pcm_unlink(snd_pcm_substream_t *substream)
1564 struct list_head *pos;
1565 int res = 0, count = 0;
1567 down_write(&snd_pcm_link_rwsem);
1568 write_lock_irq(&snd_pcm_link_rwlock);
1569 if (!snd_pcm_stream_linked(substream)) {
1573 list_del(&substream->link_list);
1574 snd_pcm_group_for_each(pos, substream) {
1578 if (count == 1) { /* detach the last stream, too */
1579 snd_pcm_group_for_each(pos, substream) {
1580 relink_to_local(snd_pcm_group_substream_entry(pos));
1583 kfree(substream->group);
1585 relink_to_local(substream);
1587 write_unlock_irq(&snd_pcm_link_rwlock);
1588 up_write(&snd_pcm_link_rwsem);
1592 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
1593 snd_pcm_hw_rule_t *rule)
1596 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1597 hw_param_interval_c(params, rule->deps[1]), &t);
1598 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1601 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
1602 snd_pcm_hw_rule_t *rule)
1605 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1606 hw_param_interval_c(params, rule->deps[1]), &t);
1607 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1610 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
1611 snd_pcm_hw_rule_t *rule)
1614 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1615 hw_param_interval_c(params, rule->deps[1]),
1616 (unsigned long) rule->private, &t);
1617 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1620 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
1621 snd_pcm_hw_rule_t *rule)
1624 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1625 (unsigned long) rule->private,
1626 hw_param_interval_c(params, rule->deps[1]), &t);
1627 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1630 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
1631 snd_pcm_hw_rule_t *rule)
1634 snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
1636 snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1638 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1640 if (! snd_mask_test(mask, k))
1642 bits = snd_pcm_format_physical_width(k);
1644 continue; /* ignore invalid formats */
1645 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1646 snd_mask_reset(&m, k);
1648 return snd_mask_refine(mask, &m);
1651 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
1652 snd_pcm_hw_rule_t *rule)
1660 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1662 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1664 bits = snd_pcm_format_physical_width(k);
1666 continue; /* ignore invalid formats */
1667 if (t.min > (unsigned)bits)
1669 if (t.max < (unsigned)bits)
1673 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1676 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1677 #error "Change this table"
1680 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1681 48000, 64000, 88200, 96000, 176400, 192000 };
1683 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
1684 snd_pcm_hw_rule_t *rule)
1686 snd_pcm_hardware_t *hw = rule->private;
1687 return snd_interval_list(hw_param_interval(params, rule->var),
1688 ARRAY_SIZE(rates), rates, hw->rates);
1691 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
1692 snd_pcm_hw_rule_t *rule)
1695 snd_pcm_substream_t *substream = rule->private;
1697 t.max = substream->buffer_bytes_max;
1701 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1704 int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
1706 snd_pcm_runtime_t *runtime = substream->runtime;
1707 snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
1710 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1711 snd_mask_any(constrs_mask(constrs, k));
1714 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1715 snd_interval_any(constrs_interval(constrs, k));
1718 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1719 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1720 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1721 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1722 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1724 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1725 snd_pcm_hw_rule_format, NULL,
1726 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1729 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1730 snd_pcm_hw_rule_sample_bits, NULL,
1731 SNDRV_PCM_HW_PARAM_FORMAT,
1732 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1735 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1736 snd_pcm_hw_rule_div, NULL,
1737 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1740 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1741 snd_pcm_hw_rule_mul, NULL,
1742 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1745 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1746 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1747 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1750 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1751 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1752 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1755 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1756 snd_pcm_hw_rule_div, NULL,
1757 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1760 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1761 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1762 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1765 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1766 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1767 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1770 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1771 snd_pcm_hw_rule_div, NULL,
1772 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1775 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1776 snd_pcm_hw_rule_div, NULL,
1777 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1780 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1781 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1782 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1785 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1786 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1787 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1790 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1791 snd_pcm_hw_rule_mul, NULL,
1792 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1796 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1797 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1800 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1801 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1802 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1805 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1806 snd_pcm_hw_rule_muldivk, (void*) 8,
1807 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1810 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1811 snd_pcm_hw_rule_muldivk, (void*) 8,
1812 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1815 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1816 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1817 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1821 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1822 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1828 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
1830 snd_pcm_runtime_t *runtime = substream->runtime;
1831 snd_pcm_hardware_t *hw = &runtime->hw;
1833 unsigned int mask = 0;
1835 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1836 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1837 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1838 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1839 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1840 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1841 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1842 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1843 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1844 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1845 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1847 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1848 snd_assert(err >= 0, return -EINVAL);
1850 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1851 snd_assert(err >= 0, return -EINVAL);
1853 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1854 snd_assert(err >= 0, return -EINVAL);
1856 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1857 hw->channels_min, hw->channels_max);
1858 snd_assert(err >= 0, return -EINVAL);
1860 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1861 hw->rate_min, hw->rate_max);
1862 snd_assert(err >= 0, return -EINVAL);
1864 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1865 hw->period_bytes_min, hw->period_bytes_max);
1866 snd_assert(err >= 0, return -EINVAL);
1868 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1869 hw->periods_min, hw->periods_max);
1870 snd_assert(err >= 0, return -EINVAL);
1872 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1873 hw->period_bytes_min, hw->buffer_bytes_max);
1874 snd_assert(err >= 0, return -EINVAL);
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1877 snd_pcm_hw_rule_buffer_bytes_max, substream,
1878 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1883 if (runtime->dma_bytes) {
1884 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1885 snd_assert(err >= 0, return -EINVAL);
1888 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1889 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1890 snd_pcm_hw_rule_rate, hw,
1891 SNDRV_PCM_HW_PARAM_RATE, -1);
1896 /* FIXME: this belong to lowlevel */
1897 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1898 1000000 / HZ, 1000000 / HZ);
1899 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1904 static void snd_pcm_add_file(snd_pcm_str_t *str,
1905 snd_pcm_file_t *pcm_file)
1907 pcm_file->next = str->files;
1908 str->files = pcm_file;
1911 static void snd_pcm_remove_file(snd_pcm_str_t *str,
1912 snd_pcm_file_t *pcm_file)
1914 snd_pcm_file_t * pcm_file1;
1915 if (str->files == pcm_file) {
1916 str->files = pcm_file->next;
1918 pcm_file1 = str->files;
1919 while (pcm_file1 && pcm_file1->next != pcm_file)
1920 pcm_file1 = pcm_file1->next;
1921 if (pcm_file1 != NULL)
1922 pcm_file1->next = pcm_file->next;
1926 static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
1928 snd_pcm_substream_t *substream;
1929 snd_pcm_runtime_t *runtime;
1930 snd_pcm_str_t * str;
1932 snd_assert(pcm_file != NULL, return -ENXIO);
1933 substream = pcm_file->substream;
1934 snd_assert(substream != NULL, return -ENXIO);
1935 runtime = substream->runtime;
1936 str = substream->pstr;
1937 snd_pcm_unlink(substream);
1938 if (substream->open_flag) {
1939 if (substream->ops->hw_free != NULL)
1940 substream->ops->hw_free(substream);
1941 substream->ops->close(substream);
1942 substream->open_flag = 0;
1944 substream->ffile = NULL;
1945 snd_pcm_remove_file(str, pcm_file);
1946 snd_pcm_release_substream(substream);
1951 static int snd_pcm_open_file(struct file *file,
1954 snd_pcm_file_t **rpcm_file)
1957 snd_pcm_file_t *pcm_file;
1958 snd_pcm_substream_t *substream;
1961 snd_assert(rpcm_file != NULL, return -EINVAL);
1964 pcm_file = kcalloc(1, sizeof(*pcm_file), GFP_KERNEL);
1965 if (pcm_file == NULL) {
1969 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
1974 str = substream->pstr;
1975 substream->file = pcm_file;
1977 pcm_file->substream = substream;
1979 snd_pcm_add_file(str, pcm_file);
1981 err = snd_pcm_hw_constraints_init(substream);
1983 snd_printd("snd_pcm_hw_constraints_init failed\n");
1984 snd_pcm_release_file(pcm_file);
1988 if ((err = substream->ops->open(substream)) < 0) {
1989 snd_pcm_release_file(pcm_file);
1992 substream->open_flag = 1;
1994 err = snd_pcm_hw_constraints_complete(substream);
1996 snd_printd("snd_pcm_hw_constraints_complete failed\n");
1997 substream->ops->close(substream);
1998 snd_pcm_release_file(pcm_file);
2002 substream->ffile = file;
2004 file->private_data = pcm_file;
2005 *rpcm_file = pcm_file;
2009 int snd_pcm_open(struct inode *inode, struct file *file)
2011 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
2012 int device = SNDRV_MINOR_DEVICE(iminor(inode));
2015 snd_pcm_file_t *pcm_file;
2018 snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
2019 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2024 err = snd_card_file_add(pcm->card, file);
2027 if (!try_module_get(pcm->card->module)) {
2031 init_waitqueue_entry(&wait, current);
2032 add_wait_queue(&pcm->open_wait, &wait);
2033 down(&pcm->open_mutex);
2035 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2038 if (err == -EAGAIN) {
2039 if (file->f_flags & O_NONBLOCK) {
2045 set_current_state(TASK_INTERRUPTIBLE);
2046 up(&pcm->open_mutex);
2048 down(&pcm->open_mutex);
2049 if (signal_pending(current)) {
2054 remove_wait_queue(&pcm->open_wait, &wait);
2055 up(&pcm->open_mutex);
2061 module_put(pcm->card->module);
2063 snd_card_file_remove(pcm->card, file);
2068 int snd_pcm_release(struct inode *inode, struct file *file)
2071 snd_pcm_substream_t *substream;
2072 snd_pcm_file_t *pcm_file;
2074 pcm_file = file->private_data;
2075 substream = pcm_file->substream;
2076 snd_assert(substream != NULL, return -ENXIO);
2077 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2078 pcm = substream->pcm;
2079 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2080 snd_pcm_playback_drop(substream);
2082 snd_pcm_capture_drop(substream);
2083 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2084 down(&pcm->open_mutex);
2085 snd_pcm_release_file(pcm_file);
2086 up(&pcm->open_mutex);
2087 wake_up(&pcm->open_wait);
2088 module_put(pcm->card->module);
2089 snd_card_file_remove(pcm->card, file);
2093 snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2095 snd_pcm_runtime_t *runtime = substream->runtime;
2096 snd_pcm_sframes_t appl_ptr;
2097 snd_pcm_sframes_t ret;
2098 snd_pcm_sframes_t hw_avail;
2103 snd_pcm_stream_lock_irq(substream);
2104 switch (runtime->status->state) {
2105 case SNDRV_PCM_STATE_PREPARED:
2107 case SNDRV_PCM_STATE_DRAINING:
2108 case SNDRV_PCM_STATE_RUNNING:
2109 if (snd_pcm_update_hw_ptr(substream) >= 0)
2112 case SNDRV_PCM_STATE_XRUN:
2120 hw_avail = snd_pcm_playback_hw_avail(runtime);
2121 if (hw_avail <= 0) {
2125 if (frames > (snd_pcm_uframes_t)hw_avail)
2128 frames -= frames % runtime->xfer_align;
2129 appl_ptr = runtime->control->appl_ptr - frames;
2131 appl_ptr += runtime->boundary;
2132 runtime->control->appl_ptr = appl_ptr;
2133 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2135 snd_pcm_tick_prepare(substream);
2138 snd_pcm_stream_unlock_irq(substream);
2142 snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2144 snd_pcm_runtime_t *runtime = substream->runtime;
2145 snd_pcm_sframes_t appl_ptr;
2146 snd_pcm_sframes_t ret;
2147 snd_pcm_sframes_t hw_avail;
2152 snd_pcm_stream_lock_irq(substream);
2153 switch (runtime->status->state) {
2154 case SNDRV_PCM_STATE_PREPARED:
2155 case SNDRV_PCM_STATE_DRAINING:
2157 case SNDRV_PCM_STATE_RUNNING:
2158 if (snd_pcm_update_hw_ptr(substream) >= 0)
2161 case SNDRV_PCM_STATE_XRUN:
2169 hw_avail = snd_pcm_capture_hw_avail(runtime);
2170 if (hw_avail <= 0) {
2174 if (frames > (snd_pcm_uframes_t)hw_avail)
2177 frames -= frames % runtime->xfer_align;
2178 appl_ptr = runtime->control->appl_ptr - frames;
2180 appl_ptr += runtime->boundary;
2181 runtime->control->appl_ptr = appl_ptr;
2182 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2184 snd_pcm_tick_prepare(substream);
2187 snd_pcm_stream_unlock_irq(substream);
2191 snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2193 snd_pcm_runtime_t *runtime = substream->runtime;
2194 snd_pcm_sframes_t appl_ptr;
2195 snd_pcm_sframes_t ret;
2196 snd_pcm_sframes_t avail;
2201 snd_pcm_stream_lock_irq(substream);
2202 switch (runtime->status->state) {
2203 case SNDRV_PCM_STATE_PREPARED:
2204 case SNDRV_PCM_STATE_PAUSED:
2206 case SNDRV_PCM_STATE_DRAINING:
2207 case SNDRV_PCM_STATE_RUNNING:
2208 if (snd_pcm_update_hw_ptr(substream) >= 0)
2211 case SNDRV_PCM_STATE_XRUN:
2219 avail = snd_pcm_playback_avail(runtime);
2224 if (frames > (snd_pcm_uframes_t)avail)
2227 frames -= frames % runtime->xfer_align;
2228 appl_ptr = runtime->control->appl_ptr + frames;
2229 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2230 appl_ptr -= runtime->boundary;
2231 runtime->control->appl_ptr = appl_ptr;
2232 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2234 snd_pcm_tick_prepare(substream);
2237 snd_pcm_stream_unlock_irq(substream);
2241 snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2243 snd_pcm_runtime_t *runtime = substream->runtime;
2244 snd_pcm_sframes_t appl_ptr;
2245 snd_pcm_sframes_t ret;
2246 snd_pcm_sframes_t avail;
2251 snd_pcm_stream_lock_irq(substream);
2252 switch (runtime->status->state) {
2253 case SNDRV_PCM_STATE_PREPARED:
2254 case SNDRV_PCM_STATE_DRAINING:
2255 case SNDRV_PCM_STATE_PAUSED:
2257 case SNDRV_PCM_STATE_RUNNING:
2258 if (snd_pcm_update_hw_ptr(substream) >= 0)
2261 case SNDRV_PCM_STATE_XRUN:
2269 avail = snd_pcm_capture_avail(runtime);
2274 if (frames > (snd_pcm_uframes_t)avail)
2277 frames -= frames % runtime->xfer_align;
2278 appl_ptr = runtime->control->appl_ptr + frames;
2279 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2280 appl_ptr -= runtime->boundary;
2281 runtime->control->appl_ptr = appl_ptr;
2282 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2284 snd_pcm_tick_prepare(substream);
2287 snd_pcm_stream_unlock_irq(substream);
2291 static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
2293 snd_pcm_runtime_t *runtime = substream->runtime;
2296 snd_pcm_stream_lock_irq(substream);
2297 switch (runtime->status->state) {
2298 case SNDRV_PCM_STATE_DRAINING:
2299 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2301 case SNDRV_PCM_STATE_RUNNING:
2302 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2305 case SNDRV_PCM_STATE_PREPARED:
2306 case SNDRV_PCM_STATE_SUSPENDED:
2309 case SNDRV_PCM_STATE_XRUN:
2317 snd_pcm_stream_unlock_irq(substream);
2321 static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t __user *res)
2323 snd_pcm_runtime_t *runtime = substream->runtime;
2325 snd_pcm_sframes_t n = 0;
2327 snd_pcm_stream_lock_irq(substream);
2328 switch (runtime->status->state) {
2329 case SNDRV_PCM_STATE_DRAINING:
2330 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2332 case SNDRV_PCM_STATE_RUNNING:
2333 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2336 case SNDRV_PCM_STATE_PREPARED:
2337 case SNDRV_PCM_STATE_SUSPENDED:
2339 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2340 n = snd_pcm_playback_hw_avail(runtime);
2342 n = snd_pcm_capture_avail(runtime);
2344 case SNDRV_PCM_STATE_XRUN:
2352 snd_pcm_stream_unlock_irq(substream);
2354 if (put_user(n, res))
2359 static int snd_pcm_sync_ptr(snd_pcm_substream_t *substream, struct sndrv_pcm_sync_ptr __user *_sync_ptr)
2361 snd_pcm_runtime_t *runtime = substream->runtime;
2362 struct sndrv_pcm_sync_ptr sync_ptr;
2363 volatile struct sndrv_pcm_mmap_status *status;
2364 volatile struct sndrv_pcm_mmap_control *control;
2367 memset(&sync_ptr, 0, sizeof(sync_ptr));
2368 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2370 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct sndrv_pcm_mmap_control)))
2372 status = runtime->status;
2373 control = runtime->control;
2374 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2375 err = snd_pcm_hwsync(substream);
2379 snd_pcm_stream_lock_irq(substream);
2380 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2381 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2383 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2384 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2385 control->avail_min = sync_ptr.c.control.avail_min;
2387 sync_ptr.c.control.avail_min = control->avail_min;
2388 sync_ptr.s.status.state = status->state;
2389 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2390 sync_ptr.s.status.tstamp = status->tstamp;
2391 sync_ptr.s.status.suspended_state = status->suspended_state;
2392 snd_pcm_stream_unlock_irq(substream);
2393 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2398 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2399 unsigned int cmd, void __user *arg);
2400 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2401 unsigned int cmd, void __user *arg);
2403 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2404 unsigned int cmd, void __user *arg)
2406 snd_assert(substream != NULL, return -ENXIO);
2409 case SNDRV_PCM_IOCTL_PVERSION:
2410 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2411 case SNDRV_PCM_IOCTL_INFO:
2412 return snd_pcm_info_user(substream, arg);
2413 case SNDRV_PCM_IOCTL_TSTAMP:
2416 if (get_user(xarg, (int __user *)arg))
2418 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2421 case SNDRV_PCM_IOCTL_HW_REFINE:
2422 return snd_pcm_hw_refine_user(substream, arg);
2423 case SNDRV_PCM_IOCTL_HW_PARAMS:
2424 return snd_pcm_hw_params_user(substream, arg);
2425 case SNDRV_PCM_IOCTL_HW_FREE:
2426 return snd_pcm_hw_free(substream);
2427 case SNDRV_PCM_IOCTL_SW_PARAMS:
2428 return snd_pcm_sw_params_user(substream, arg);
2429 case SNDRV_PCM_IOCTL_STATUS:
2430 return snd_pcm_status_user(substream, arg);
2431 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2432 return snd_pcm_channel_info(substream, arg);
2433 case SNDRV_PCM_IOCTL_PREPARE:
2434 return snd_pcm_prepare(substream);
2435 case SNDRV_PCM_IOCTL_RESET:
2436 return snd_pcm_reset(substream);
2437 case SNDRV_PCM_IOCTL_START:
2438 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, 0);
2439 case SNDRV_PCM_IOCTL_LINK:
2440 return snd_pcm_link(substream, (int)(unsigned long) arg);
2441 case SNDRV_PCM_IOCTL_UNLINK:
2442 return snd_pcm_unlink(substream);
2443 case SNDRV_PCM_IOCTL_RESUME:
2444 return snd_pcm_resume(substream);
2445 case SNDRV_PCM_IOCTL_XRUN:
2446 return snd_pcm_xrun(substream);
2447 case SNDRV_PCM_IOCTL_HWSYNC:
2448 return snd_pcm_hwsync(substream);
2449 case SNDRV_PCM_IOCTL_DELAY:
2450 return snd_pcm_delay(substream, arg);
2451 case SNDRV_PCM_IOCTL_SYNC_PTR:
2452 return snd_pcm_sync_ptr(substream, arg);
2453 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2454 return snd_pcm_hw_refine_old_user(substream, arg);
2455 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2456 return snd_pcm_hw_params_old_user(substream, arg);
2458 snd_printd("unknown ioctl = 0x%x\n", cmd);
2462 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2463 unsigned int cmd, void __user *arg)
2465 snd_assert(substream != NULL, return -ENXIO);
2466 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2468 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2471 snd_xferi_t __user *_xferi = arg;
2472 snd_pcm_runtime_t *runtime = substream->runtime;
2473 snd_pcm_sframes_t result;
2474 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2476 if (put_user(0, &_xferi->result))
2478 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2480 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2481 __put_user(result, &_xferi->result);
2482 return result < 0 ? result : 0;
2484 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2487 snd_xfern_t __user *_xfern = arg;
2488 snd_pcm_runtime_t *runtime = substream->runtime;
2490 snd_pcm_sframes_t result;
2491 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2493 if (runtime->channels > 128)
2495 if (put_user(0, &_xfern->result))
2497 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2499 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2502 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2506 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2508 __put_user(result, &_xfern->result);
2509 return result < 0 ? result : 0;
2511 case SNDRV_PCM_IOCTL_REWIND:
2513 snd_pcm_uframes_t frames;
2514 snd_pcm_uframes_t __user *_frames = arg;
2515 snd_pcm_sframes_t result;
2516 if (get_user(frames, _frames))
2518 if (put_user(0, _frames))
2520 result = snd_pcm_playback_rewind(substream, frames);
2521 __put_user(result, _frames);
2522 return result < 0 ? result : 0;
2524 case SNDRV_PCM_IOCTL_FORWARD:
2526 snd_pcm_uframes_t frames;
2527 snd_pcm_uframes_t __user *_frames = arg;
2528 snd_pcm_sframes_t result;
2529 if (get_user(frames, _frames))
2531 if (put_user(0, _frames))
2533 result = snd_pcm_playback_forward(substream, frames);
2534 __put_user(result, _frames);
2535 return result < 0 ? result : 0;
2537 case SNDRV_PCM_IOCTL_PAUSE:
2540 snd_pcm_stream_lock_irq(substream);
2541 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2542 snd_pcm_stream_unlock_irq(substream);
2545 case SNDRV_PCM_IOCTL_DRAIN:
2546 return snd_pcm_playback_drain(substream);
2547 case SNDRV_PCM_IOCTL_DROP:
2548 return snd_pcm_playback_drop(substream);
2550 return snd_pcm_common_ioctl1(substream, cmd, arg);
2553 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2554 unsigned int cmd, void __user *arg)
2556 snd_assert(substream != NULL, return -ENXIO);
2557 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2559 case SNDRV_PCM_IOCTL_READI_FRAMES:
2562 snd_xferi_t __user *_xferi = arg;
2563 snd_pcm_runtime_t *runtime = substream->runtime;
2564 snd_pcm_sframes_t result;
2565 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2567 if (put_user(0, &_xferi->result))
2569 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2571 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2572 __put_user(result, &_xferi->result);
2573 return result < 0 ? result : 0;
2575 case SNDRV_PCM_IOCTL_READN_FRAMES:
2578 snd_xfern_t __user *_xfern = arg;
2579 snd_pcm_runtime_t *runtime = substream->runtime;
2581 snd_pcm_sframes_t result;
2582 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2584 if (runtime->channels > 128)
2586 if (put_user(0, &_xfern->result))
2588 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2590 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2593 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2597 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2599 __put_user(result, &_xfern->result);
2600 return result < 0 ? result : 0;
2602 case SNDRV_PCM_IOCTL_REWIND:
2604 snd_pcm_uframes_t frames;
2605 snd_pcm_uframes_t __user *_frames = arg;
2606 snd_pcm_sframes_t result;
2607 if (get_user(frames, _frames))
2609 if (put_user(0, _frames))
2611 result = snd_pcm_capture_rewind(substream, frames);
2612 __put_user(result, _frames);
2613 return result < 0 ? result : 0;
2615 case SNDRV_PCM_IOCTL_FORWARD:
2617 snd_pcm_uframes_t frames;
2618 snd_pcm_uframes_t __user *_frames = arg;
2619 snd_pcm_sframes_t result;
2620 if (get_user(frames, _frames))
2622 if (put_user(0, _frames))
2624 result = snd_pcm_capture_forward(substream, frames);
2625 __put_user(result, _frames);
2626 return result < 0 ? result : 0;
2628 case SNDRV_PCM_IOCTL_DRAIN:
2629 return snd_pcm_capture_drain(substream);
2630 case SNDRV_PCM_IOCTL_DROP:
2631 return snd_pcm_capture_drop(substream);
2633 return snd_pcm_common_ioctl1(substream, cmd, arg);
2636 static int snd_pcm_playback_ioctl(struct inode *inode, struct file *file,
2637 unsigned int cmd, unsigned long arg)
2639 snd_pcm_file_t *pcm_file;
2642 pcm_file = file->private_data;
2644 if (((cmd >> 8) & 0xff) != 'A')
2647 /* FIXME: need to unlock BKL to allow preemption */
2649 err = snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2654 static int snd_pcm_capture_ioctl(struct inode *inode, struct file *file,
2655 unsigned int cmd, unsigned long arg)
2657 snd_pcm_file_t *pcm_file;
2660 pcm_file = file->private_data;
2662 if (((cmd >> 8) & 0xff) != 'A')
2665 /* FIXME: need to unlock BKL to allow preemption */
2667 err = snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2672 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2673 unsigned int cmd, void *arg)
2678 fs = snd_enter_user();
2679 result = snd_pcm_playback_ioctl1(substream, cmd, arg);
2684 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2685 unsigned int cmd, void *arg)
2690 fs = snd_enter_user();
2691 result = snd_pcm_capture_ioctl1(substream, cmd, arg);
2696 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2697 unsigned int cmd, void *arg)
2699 switch (substream->stream) {
2700 case SNDRV_PCM_STREAM_PLAYBACK:
2701 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2702 case SNDRV_PCM_STREAM_CAPTURE:
2703 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2709 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
2711 snd_pcm_file_t *pcm_file;
2712 snd_pcm_substream_t *substream;
2713 snd_pcm_runtime_t *runtime;
2714 snd_pcm_sframes_t result;
2716 pcm_file = file->private_data;
2717 substream = pcm_file->substream;
2718 snd_assert(substream != NULL, return -ENXIO);
2719 runtime = substream->runtime;
2720 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2722 if (!frame_aligned(runtime, count))
2724 count = bytes_to_frames(runtime, count);
2725 result = snd_pcm_lib_read(substream, buf, count);
2727 result = frames_to_bytes(runtime, result);
2731 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, size_t count, loff_t * offset)
2733 snd_pcm_file_t *pcm_file;
2734 snd_pcm_substream_t *substream;
2735 snd_pcm_runtime_t *runtime;
2736 snd_pcm_sframes_t result;
2738 pcm_file = file->private_data;
2739 substream = pcm_file->substream;
2740 snd_assert(substream != NULL, result = -ENXIO; goto end);
2741 runtime = substream->runtime;
2742 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2746 if (!frame_aligned(runtime, count)) {
2750 count = bytes_to_frames(runtime, count);
2751 result = snd_pcm_lib_write(substream, buf, count);
2753 result = frames_to_bytes(runtime, result);
2758 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2759 unsigned long count, loff_t * offset)
2762 snd_pcm_file_t *pcm_file;
2763 snd_pcm_substream_t *substream;
2764 snd_pcm_runtime_t *runtime;
2765 snd_pcm_sframes_t result;
2768 snd_pcm_uframes_t frames;
2770 pcm_file = file->private_data;
2771 substream = pcm_file->substream;
2772 snd_assert(substream != NULL, return -ENXIO);
2773 runtime = substream->runtime;
2774 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2776 if (count > 1024 || count != runtime->channels)
2778 if (!frame_aligned(runtime, _vector->iov_len))
2780 frames = bytes_to_samples(runtime, _vector->iov_len);
2781 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2784 for (i = 0; i < count; ++i)
2785 bufs[i] = _vector[i].iov_base;
2786 result = snd_pcm_lib_readv(substream, bufs, frames);
2788 result = frames_to_bytes(runtime, result);
2793 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2794 unsigned long count, loff_t * offset)
2796 snd_pcm_file_t *pcm_file;
2797 snd_pcm_substream_t *substream;
2798 snd_pcm_runtime_t *runtime;
2799 snd_pcm_sframes_t result;
2802 snd_pcm_uframes_t frames;
2804 pcm_file = file->private_data;
2805 substream = pcm_file->substream;
2806 snd_assert(substream != NULL, result = -ENXIO; goto end);
2807 runtime = substream->runtime;
2808 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2812 if (count > 128 || count != runtime->channels ||
2813 !frame_aligned(runtime, _vector->iov_len)) {
2817 frames = bytes_to_samples(runtime, _vector->iov_len);
2818 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2821 for (i = 0; i < count; ++i)
2822 bufs[i] = _vector[i].iov_base;
2823 result = snd_pcm_lib_writev(substream, bufs, frames);
2825 result = frames_to_bytes(runtime, result);
2831 unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2833 snd_pcm_file_t *pcm_file;
2834 snd_pcm_substream_t *substream;
2835 snd_pcm_runtime_t *runtime;
2837 snd_pcm_uframes_t avail;
2839 pcm_file = file->private_data;
2841 substream = pcm_file->substream;
2842 snd_assert(substream != NULL, return -ENXIO);
2843 runtime = substream->runtime;
2845 poll_wait(file, &runtime->sleep, wait);
2847 snd_pcm_stream_lock_irq(substream);
2848 avail = snd_pcm_playback_avail(runtime);
2849 switch (runtime->status->state) {
2850 case SNDRV_PCM_STATE_RUNNING:
2851 case SNDRV_PCM_STATE_PREPARED:
2852 case SNDRV_PCM_STATE_PAUSED:
2853 if (avail >= runtime->control->avail_min) {
2854 mask = POLLOUT | POLLWRNORM;
2858 case SNDRV_PCM_STATE_DRAINING:
2862 mask = POLLOUT | POLLWRNORM | POLLERR;
2865 snd_pcm_stream_unlock_irq(substream);
2869 unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2871 snd_pcm_file_t *pcm_file;
2872 snd_pcm_substream_t *substream;
2873 snd_pcm_runtime_t *runtime;
2875 snd_pcm_uframes_t avail;
2877 pcm_file = file->private_data;
2879 substream = pcm_file->substream;
2880 snd_assert(substream != NULL, return -ENXIO);
2881 runtime = substream->runtime;
2883 poll_wait(file, &runtime->sleep, wait);
2885 snd_pcm_stream_lock_irq(substream);
2886 avail = snd_pcm_capture_avail(runtime);
2887 switch (runtime->status->state) {
2888 case SNDRV_PCM_STATE_RUNNING:
2889 case SNDRV_PCM_STATE_PREPARED:
2890 case SNDRV_PCM_STATE_PAUSED:
2891 if (avail >= runtime->control->avail_min) {
2892 mask = POLLIN | POLLRDNORM;
2897 case SNDRV_PCM_STATE_DRAINING:
2899 mask = POLLIN | POLLRDNORM;
2904 mask = POLLIN | POLLRDNORM | POLLERR;
2907 snd_pcm_stream_unlock_irq(substream);
2916 * Only on coherent architectures, we can mmap the status and the control records
2917 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2919 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2921 * mmap status record
2923 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2925 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2926 snd_pcm_runtime_t *runtime;
2929 if (substream == NULL)
2931 runtime = substream->runtime;
2932 page = virt_to_page(runtime->status);
2933 if (!PageReserved(page))
2936 *type = VM_FAULT_MINOR;
2940 static struct vm_operations_struct snd_pcm_vm_ops_status =
2942 .nopage = snd_pcm_mmap_status_nopage,
2945 static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2946 struct vm_area_struct *area)
2948 snd_pcm_runtime_t *runtime;
2950 if (!(area->vm_flags & VM_READ))
2952 runtime = substream->runtime;
2953 snd_assert(runtime != NULL, return -EAGAIN);
2954 size = area->vm_end - area->vm_start;
2955 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2957 area->vm_ops = &snd_pcm_vm_ops_status;
2958 area->vm_private_data = substream;
2959 area->vm_flags |= VM_RESERVED;
2964 * mmap control record
2966 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2968 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2969 snd_pcm_runtime_t *runtime;
2972 if (substream == NULL)
2974 runtime = substream->runtime;
2975 page = virt_to_page(runtime->control);
2976 if (!PageReserved(page))
2979 *type = VM_FAULT_MINOR;
2983 static struct vm_operations_struct snd_pcm_vm_ops_control =
2985 .nopage = snd_pcm_mmap_control_nopage,
2988 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
2989 struct vm_area_struct *area)
2991 snd_pcm_runtime_t *runtime;
2993 if (!(area->vm_flags & VM_READ))
2995 runtime = substream->runtime;
2996 snd_assert(runtime != NULL, return -EAGAIN);
2997 size = area->vm_end - area->vm_start;
2998 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
3000 area->vm_ops = &snd_pcm_vm_ops_control;
3001 area->vm_private_data = substream;
3002 area->vm_flags |= VM_RESERVED;
3005 #else /* ! coherent mmap */
3007 * don't support mmap for status and control records.
3009 static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
3010 struct vm_area_struct *area)
3014 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
3015 struct vm_area_struct *area)
3019 #endif /* coherent mmap */
3022 * nopage callback for mmapping a RAM page
3024 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
3026 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
3027 snd_pcm_runtime_t *runtime;
3028 unsigned long offset;
3033 if (substream == NULL)
3035 runtime = substream->runtime;
3036 offset = area->vm_pgoff << PAGE_SHIFT;
3037 offset += address - area->vm_start;
3038 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3039 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3040 if (offset > dma_bytes - PAGE_SIZE)
3041 return NOPAGE_SIGBUS;
3042 if (substream->ops->page) {
3043 page = substream->ops->page(substream, offset);
3047 vaddr = runtime->dma_area + offset;
3048 page = virt_to_page(vaddr);
3050 if (!PageReserved(page))
3053 *type = VM_FAULT_MINOR;
3057 static struct vm_operations_struct snd_pcm_vm_ops_data =
3059 .open = snd_pcm_mmap_data_open,
3060 .close = snd_pcm_mmap_data_close,
3061 .nopage = snd_pcm_mmap_data_nopage,
3065 * mmap the DMA buffer on RAM
3067 static int snd_pcm_default_mmap(snd_pcm_substream_t *substream, struct vm_area_struct *area)
3069 area->vm_ops = &snd_pcm_vm_ops_data;
3070 area->vm_private_data = substream;
3071 area->vm_flags |= VM_RESERVED;
3072 atomic_inc(&substream->runtime->mmap_count);
3077 * mmap the DMA buffer on I/O memory area
3079 #if SNDRV_PCM_INFO_MMAP_IOMEM
3080 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3082 .open = snd_pcm_mmap_data_open,
3083 .close = snd_pcm_mmap_data_close,
3086 int snd_pcm_lib_mmap_iomem(snd_pcm_substream_t *substream, struct vm_area_struct *area)
3089 unsigned long offset;
3091 #ifdef pgprot_noncached
3092 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3094 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3095 area->vm_flags |= VM_IO;
3096 size = area->vm_end - area->vm_start;
3097 offset = area->vm_pgoff << PAGE_SHIFT;
3098 if (io_remap_page_range(area, area->vm_start,
3099 substream->runtime->dma_addr + offset,
3100 size, area->vm_page_prot))
3102 atomic_inc(&substream->runtime->mmap_count);
3105 #endif /* SNDRV_PCM_INFO_MMAP */
3110 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
3111 struct vm_area_struct *area)
3113 snd_pcm_runtime_t *runtime;
3115 unsigned long offset;
3118 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3119 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3122 if (!(area->vm_flags & VM_READ))
3125 runtime = substream->runtime;
3126 snd_assert(runtime != NULL, return -EAGAIN);
3127 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3129 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3131 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3132 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3134 size = area->vm_end - area->vm_start;
3135 offset = area->vm_pgoff << PAGE_SHIFT;
3136 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3137 if ((size_t)size > dma_bytes)
3139 if (offset > dma_bytes - size)
3142 if (substream->ops->mmap)
3143 return substream->ops->mmap(substream, area);
3145 return snd_pcm_default_mmap(substream, area);
3148 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3150 snd_pcm_file_t * pcm_file;
3151 snd_pcm_substream_t *substream;
3152 unsigned long offset;
3154 pcm_file = file->private_data;
3155 substream = pcm_file->substream;
3156 snd_assert(substream != NULL, return -ENXIO);
3158 offset = area->vm_pgoff << PAGE_SHIFT;
3160 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3161 return snd_pcm_mmap_status(substream, file, area);
3162 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3163 return snd_pcm_mmap_control(substream, file, area);
3165 return snd_pcm_mmap_data(substream, file, area);
3170 static int snd_pcm_fasync(int fd, struct file * file, int on)
3172 snd_pcm_file_t * pcm_file;
3173 snd_pcm_substream_t *substream;
3174 snd_pcm_runtime_t *runtime;
3177 pcm_file = file->private_data;
3178 substream = pcm_file->substream;
3179 snd_assert(substream != NULL, return -ENXIO);
3180 runtime = substream->runtime;
3182 err = fasync_helper(fd, file, on, &runtime->fasync);
3189 * To be removed helpers to keep binary compatibility
3192 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3193 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3195 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
3199 memset(params, 0, sizeof(*params));
3200 params->flags = oparams->flags;
3201 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3202 params->masks[i].bits[0] = oparams->masks[i];
3203 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3204 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3205 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3206 params->info = oparams->info;
3207 params->msbits = oparams->msbits;
3208 params->rate_num = oparams->rate_num;
3209 params->rate_den = oparams->rate_den;
3210 params->fifo_size = oparams->fifo_size;
3213 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3217 memset(oparams, 0, sizeof(*oparams));
3218 oparams->flags = params->flags;
3219 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3220 oparams->masks[i] = params->masks[i].bits[0];
3221 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3222 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3223 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3224 oparams->info = params->info;
3225 oparams->msbits = params->msbits;
3226 oparams->rate_num = params->rate_num;
3227 oparams->rate_den = params->rate_den;
3228 oparams->fifo_size = params->fifo_size;
3231 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
3233 snd_pcm_hw_params_t *params;
3234 struct sndrv_pcm_hw_params_old *oparams = NULL;
3237 params = kmalloc(sizeof(*params), GFP_KERNEL);
3242 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3248 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3252 snd_pcm_hw_convert_from_old_params(params, oparams);
3253 err = snd_pcm_hw_refine(substream, params);
3254 snd_pcm_hw_convert_to_old_params(oparams, params);
3255 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3265 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
3267 snd_pcm_hw_params_t *params;
3268 struct sndrv_pcm_hw_params_old *oparams = NULL;
3271 params = kmalloc(sizeof(*params), GFP_KERNEL);
3276 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3281 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3285 snd_pcm_hw_convert_from_old_params(params, oparams);
3286 err = snd_pcm_hw_params(substream, params);
3287 snd_pcm_hw_convert_to_old_params(oparams, params);
3288 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3302 static struct file_operations snd_pcm_f_ops_playback = {
3303 .owner = THIS_MODULE,
3304 .write = snd_pcm_write,
3305 .writev = snd_pcm_writev,
3306 .open = snd_pcm_open,
3307 .release = snd_pcm_release,
3308 .poll = snd_pcm_playback_poll,
3309 .ioctl = snd_pcm_playback_ioctl,
3310 .mmap = snd_pcm_mmap,
3311 .fasync = snd_pcm_fasync,
3314 static struct file_operations snd_pcm_f_ops_capture = {
3315 .owner = THIS_MODULE,
3316 .read = snd_pcm_read,
3317 .readv = snd_pcm_readv,
3318 .open = snd_pcm_open,
3319 .release = snd_pcm_release,
3320 .poll = snd_pcm_capture_poll,
3321 .ioctl = snd_pcm_capture_ioctl,
3322 .mmap = snd_pcm_mmap,
3323 .fasync = snd_pcm_fasync,
3326 snd_minor_t snd_pcm_reg[2] =
3329 .comment = "digital audio playback",
3330 .f_ops = &snd_pcm_f_ops_playback,
3333 .comment = "digital audio capture",
3334 .f_ops = &snd_pcm_f_ops_capture,