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 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)
472 result = substream->ops->hw_free(substream);
473 runtime->status->state = SNDRV_PCM_STATE_OPEN;
477 static int snd_pcm_sw_params(snd_pcm_substream_t * substream, snd_pcm_sw_params_t *params)
479 snd_pcm_runtime_t *runtime;
481 snd_assert(substream != NULL, return -ENXIO);
482 runtime = substream->runtime;
483 snd_assert(runtime != NULL, return -ENXIO);
484 snd_pcm_stream_lock_irq(substream);
485 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
486 snd_pcm_stream_unlock_irq(substream);
489 snd_pcm_stream_unlock_irq(substream);
491 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
493 if (params->avail_min == 0)
495 if (params->xfer_align == 0 ||
496 params->xfer_align % runtime->min_align != 0)
498 if (params->silence_size >= runtime->boundary) {
499 if (params->silence_threshold != 0)
502 if (params->silence_size > params->silence_threshold)
504 if (params->silence_threshold > runtime->buffer_size)
507 snd_pcm_stream_lock_irq(substream);
508 runtime->tstamp_mode = params->tstamp_mode;
509 runtime->sleep_min = params->sleep_min;
510 runtime->period_step = params->period_step;
511 runtime->control->avail_min = params->avail_min;
512 runtime->start_threshold = params->start_threshold;
513 runtime->stop_threshold = params->stop_threshold;
514 runtime->silence_threshold = params->silence_threshold;
515 runtime->silence_size = params->silence_size;
516 runtime->xfer_align = params->xfer_align;
517 params->boundary = runtime->boundary;
518 if (snd_pcm_running(substream)) {
519 if (runtime->sleep_min)
520 snd_pcm_tick_prepare(substream);
522 snd_pcm_tick_set(substream, 0);
523 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
524 runtime->silence_size > 0)
525 snd_pcm_playback_silence(substream, ULONG_MAX);
526 wake_up(&runtime->sleep);
528 snd_pcm_stream_unlock_irq(substream);
532 static int snd_pcm_sw_params_user(snd_pcm_substream_t * substream, snd_pcm_sw_params_t __user * _params)
534 snd_pcm_sw_params_t params;
536 if (copy_from_user(¶ms, _params, sizeof(params)))
538 err = snd_pcm_sw_params(substream, ¶ms);
539 if (copy_to_user(_params, ¶ms, sizeof(params)))
544 int snd_pcm_status(snd_pcm_substream_t *substream,
545 snd_pcm_status_t *status)
547 snd_pcm_runtime_t *runtime = substream->runtime;
549 snd_pcm_stream_lock_irq(substream);
550 status->state = runtime->status->state;
551 status->suspended_state = runtime->status->suspended_state;
552 if (status->state == SNDRV_PCM_STATE_OPEN)
554 status->trigger_tstamp = runtime->trigger_tstamp;
555 if (snd_pcm_running(substream)) {
556 snd_pcm_update_hw_ptr(substream);
557 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
558 status->tstamp = runtime->status->tstamp;
560 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
562 snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
563 status->appl_ptr = runtime->control->appl_ptr;
564 status->hw_ptr = runtime->status->hw_ptr;
565 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
566 status->avail = snd_pcm_playback_avail(runtime);
567 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
568 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
569 status->delay = runtime->buffer_size - status->avail;
573 status->avail = snd_pcm_capture_avail(runtime);
574 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
575 status->delay = status->avail;
579 status->avail_max = runtime->avail_max;
580 status->overrange = runtime->overrange;
581 runtime->avail_max = 0;
582 runtime->overrange = 0;
584 snd_pcm_stream_unlock_irq(substream);
588 static int snd_pcm_status_user(snd_pcm_substream_t * substream, snd_pcm_status_t __user * _status)
590 snd_pcm_status_t status;
591 snd_pcm_runtime_t *runtime;
594 snd_assert(substream != NULL, return -ENXIO);
595 runtime = substream->runtime;
596 memset(&status, 0, sizeof(status));
597 res = snd_pcm_status(substream, &status);
600 if (copy_to_user(_status, &status, sizeof(status)))
605 static int snd_pcm_channel_info(snd_pcm_substream_t * substream, snd_pcm_channel_info_t __user * _info)
607 snd_pcm_channel_info_t info;
608 snd_pcm_runtime_t *runtime;
610 unsigned int channel;
612 snd_assert(substream != NULL, return -ENXIO);
613 if (copy_from_user(&info, _info, sizeof(info)))
615 channel = info.channel;
616 runtime = substream->runtime;
617 snd_pcm_stream_lock_irq(substream);
618 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
619 snd_pcm_stream_unlock_irq(substream);
622 snd_pcm_stream_unlock_irq(substream);
623 if (channel >= runtime->channels)
625 memset(&info, 0, sizeof(info));
626 info.channel = channel;
627 res = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, &info);
630 if (copy_to_user(_info, &info, sizeof(info)))
635 static void snd_pcm_trigger_tstamp(snd_pcm_substream_t *substream)
637 snd_pcm_runtime_t *runtime = substream->runtime;
638 if (runtime->trigger_master == NULL)
640 if (runtime->trigger_master == substream) {
641 snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
643 snd_pcm_trigger_tstamp(runtime->trigger_master);
644 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
646 runtime->trigger_master = NULL;
650 int (*pre_action)(snd_pcm_substream_t *substream, int state);
651 int (*do_action)(snd_pcm_substream_t *substream, int state);
652 void (*undo_action)(snd_pcm_substream_t *substream, int state);
653 void (*post_action)(snd_pcm_substream_t *substream, int state);
657 * this functions is core for handling of linked stream
658 * Note: the stream state might be changed also on failure
659 * Note2: call with calling stream lock + link lock
661 static int snd_pcm_action_group(struct action_ops *ops,
662 snd_pcm_substream_t *substream,
663 int state, int do_lock)
665 struct list_head *pos;
666 snd_pcm_substream_t *s = NULL;
667 snd_pcm_substream_t *s1;
670 snd_pcm_group_for_each(pos, substream) {
671 s = snd_pcm_group_substream_entry(pos);
672 if (do_lock && s != substream)
673 spin_lock(&s->self_group.lock);
674 res = ops->pre_action(s, state);
678 snd_pcm_group_for_each(pos, substream) {
679 s = snd_pcm_group_substream_entry(pos);
680 res = ops->do_action(s, state);
682 if (ops->undo_action) {
683 snd_pcm_group_for_each(pos, substream) {
684 s1 = snd_pcm_group_substream_entry(pos);
685 if (s1 == s) /* failed stream */
687 ops->undo_action(s1, state);
690 s = NULL; /* unlock all */
694 snd_pcm_group_for_each(pos, substream) {
695 s = snd_pcm_group_substream_entry(pos);
696 ops->post_action(s, state);
701 snd_pcm_group_for_each(pos, substream) {
702 s1 = snd_pcm_group_substream_entry(pos);
704 spin_unlock(&s1->self_group.lock);
705 if (s1 == s) /* end */
713 * Note: call with stream lock
715 static int snd_pcm_action_single(struct action_ops *ops,
716 snd_pcm_substream_t *substream,
721 res = ops->pre_action(substream, state);
724 res = ops->do_action(substream, state);
726 ops->post_action(substream, state);
727 else if (ops->undo_action)
728 ops->undo_action(substream, state);
733 * Note: call with stream lock
735 static int snd_pcm_action(struct action_ops *ops,
736 snd_pcm_substream_t *substream,
741 if (snd_pcm_stream_linked(substream)) {
742 if (!spin_trylock(&substream->group->lock)) {
743 spin_unlock(&substream->self_group.lock);
744 spin_lock(&substream->group->lock);
745 spin_lock(&substream->self_group.lock);
747 res = snd_pcm_action_group(ops, substream, state, 1);
748 spin_unlock(&substream->group->lock);
750 res = snd_pcm_action_single(ops, substream, state);
756 * Note: don't use any locks before
758 static int snd_pcm_action_lock_irq(struct action_ops *ops,
759 snd_pcm_substream_t *substream,
764 read_lock_irq(&snd_pcm_link_rwlock);
765 if (snd_pcm_stream_linked(substream)) {
766 spin_lock(&substream->group->lock);
767 spin_lock(&substream->self_group.lock);
768 res = snd_pcm_action_group(ops, substream, state, 1);
769 spin_unlock(&substream->self_group.lock);
770 spin_unlock(&substream->group->lock);
772 spin_lock(&substream->self_group.lock);
773 res = snd_pcm_action_single(ops, substream, state);
774 spin_unlock(&substream->self_group.lock);
776 read_unlock_irq(&snd_pcm_link_rwlock);
782 static int snd_pcm_action_nonatomic(struct action_ops *ops,
783 snd_pcm_substream_t *substream,
788 down_read(&snd_pcm_link_rwsem);
789 if (snd_pcm_stream_linked(substream))
790 res = snd_pcm_action_group(ops, substream, state, 0);
792 res = snd_pcm_action_single(ops, substream, state);
793 up_read(&snd_pcm_link_rwsem);
800 static int snd_pcm_pre_start(snd_pcm_substream_t *substream, int state)
802 snd_pcm_runtime_t *runtime = substream->runtime;
803 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
805 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
806 !snd_pcm_playback_data(substream))
808 runtime->trigger_master = substream;
812 static int snd_pcm_do_start(snd_pcm_substream_t *substream, int state)
814 if (substream->runtime->trigger_master != substream)
816 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
819 static void snd_pcm_undo_start(snd_pcm_substream_t *substream, int state)
821 if (substream->runtime->trigger_master == substream)
822 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
825 static void snd_pcm_post_start(snd_pcm_substream_t *substream, int state)
827 snd_pcm_runtime_t *runtime = substream->runtime;
828 snd_pcm_trigger_tstamp(substream);
829 runtime->status->state = state;
830 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
831 runtime->silence_size > 0)
832 snd_pcm_playback_silence(substream, ULONG_MAX);
833 if (runtime->sleep_min)
834 snd_pcm_tick_prepare(substream);
835 if (substream->timer)
836 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART, &runtime->trigger_tstamp);
839 static struct action_ops snd_pcm_action_start = {
840 .pre_action = snd_pcm_pre_start,
841 .do_action = snd_pcm_do_start,
842 .undo_action = snd_pcm_undo_start,
843 .post_action = snd_pcm_post_start
849 * Start all linked streams.
851 int snd_pcm_start(snd_pcm_substream_t *substream)
853 return snd_pcm_action(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
859 static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
861 snd_pcm_runtime_t *runtime = substream->runtime;
862 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
864 runtime->trigger_master = substream;
868 static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
870 if (substream->runtime->trigger_master == substream &&
871 snd_pcm_running(substream))
872 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
873 return 0; /* unconditonally stop all substreams */
876 static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
878 snd_pcm_runtime_t *runtime = substream->runtime;
879 if (runtime->status->state != state) {
880 snd_pcm_trigger_tstamp(substream);
881 if (substream->timer)
882 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
883 runtime->status->state = state;
884 snd_pcm_tick_set(substream, 0);
886 wake_up(&runtime->sleep);
889 static struct action_ops snd_pcm_action_stop = {
890 .pre_action = snd_pcm_pre_stop,
891 .do_action = snd_pcm_do_stop,
892 .post_action = snd_pcm_post_stop
898 * Try to stop all running streams in the substream group.
899 * The state of each stream is changed to the given value after that unconditionally.
901 int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
903 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
909 * Stop the DMA only when the given stream is playback.
910 * The state is changed to SETUP.
911 * Unlike snd_pcm_stop(), this affects only the given stream.
913 int snd_pcm_drain_done(snd_pcm_substream_t *substream)
915 return snd_pcm_action_single(&snd_pcm_action_stop, substream, SNDRV_PCM_STATE_SETUP);
921 static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
923 snd_pcm_runtime_t *runtime = substream->runtime;
924 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
927 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
929 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
931 runtime->trigger_master = substream;
935 static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
937 if (substream->runtime->trigger_master != substream)
939 return substream->ops->trigger(substream,
940 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
941 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
944 static void snd_pcm_undo_pause(snd_pcm_substream_t *substream, int push)
946 if (substream->runtime->trigger_master == substream)
947 substream->ops->trigger(substream,
948 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
949 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
952 static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
954 snd_pcm_runtime_t *runtime = substream->runtime;
955 snd_pcm_trigger_tstamp(substream);
957 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
958 if (substream->timer)
959 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
960 snd_pcm_tick_set(substream, 0);
961 wake_up(&runtime->sleep);
963 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
964 if (runtime->sleep_min)
965 snd_pcm_tick_prepare(substream);
966 if (substream->timer)
967 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
971 static struct action_ops snd_pcm_action_pause = {
972 .pre_action = snd_pcm_pre_pause,
973 .do_action = snd_pcm_do_pause,
974 .undo_action = snd_pcm_undo_pause,
975 .post_action = snd_pcm_post_pause
979 * Push/release the pause for all linked streams.
981 static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
983 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
989 static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
991 snd_pcm_runtime_t *runtime = substream->runtime;
992 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
994 runtime->trigger_master = substream;
998 static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
1000 snd_pcm_runtime_t *runtime = substream->runtime;
1001 if (runtime->trigger_master != substream)
1003 if (! snd_pcm_running(substream))
1005 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1006 return 0; /* suspend unconditionally */
1009 static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
1011 snd_pcm_runtime_t *runtime = substream->runtime;
1012 snd_pcm_trigger_tstamp(substream);
1013 if (substream->timer)
1014 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
1015 runtime->status->suspended_state = runtime->status->state;
1016 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1017 snd_pcm_tick_set(substream, 0);
1018 wake_up(&runtime->sleep);
1021 static struct action_ops snd_pcm_action_suspend = {
1022 .pre_action = snd_pcm_pre_suspend,
1023 .do_action = snd_pcm_do_suspend,
1024 .post_action = snd_pcm_post_suspend
1030 * Trigger SUSPEND to all linked streams.
1031 * After this call, all streams are changed to SUSPENDED state.
1033 int snd_pcm_suspend(snd_pcm_substream_t *substream)
1035 return snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1039 * snd_pcm_suspend_all
1041 * Trigger SUSPEND to all substreams in the given pcm.
1042 * After this call, all streams are changed to SUSPENDED state.
1044 int snd_pcm_suspend_all(snd_pcm_t *pcm)
1046 snd_pcm_substream_t *substream;
1047 int stream, err = 0;
1049 for (stream = 0; stream < 2; stream++) {
1050 for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
1051 /* FIXME: the open/close code should lock this as well */
1052 if (substream->runtime == NULL)
1054 snd_pcm_stream_lock(substream);
1055 if (substream->runtime->status->state != SNDRV_PCM_STATE_SUSPENDED)
1056 err = snd_pcm_suspend(substream);
1057 snd_pcm_stream_unlock(substream);
1067 static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
1069 snd_pcm_runtime_t *runtime = substream->runtime;
1070 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1072 runtime->trigger_master = substream;
1076 static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
1078 snd_pcm_runtime_t *runtime = substream->runtime;
1079 if (runtime->trigger_master != substream)
1081 /* DMA not running previously? */
1082 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1083 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1084 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1086 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1089 static void snd_pcm_undo_resume(snd_pcm_substream_t *substream, int state)
1091 if (substream->runtime->trigger_master == substream &&
1092 snd_pcm_running(substream))
1093 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1096 static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
1098 snd_pcm_runtime_t *runtime = substream->runtime;
1099 snd_pcm_trigger_tstamp(substream);
1100 if (substream->timer)
1101 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
1102 runtime->status->state = runtime->status->suspended_state;
1103 if (runtime->sleep_min)
1104 snd_pcm_tick_prepare(substream);
1107 static struct action_ops snd_pcm_action_resume = {
1108 .pre_action = snd_pcm_pre_resume,
1109 .do_action = snd_pcm_do_resume,
1110 .undo_action = snd_pcm_undo_resume,
1111 .post_action = snd_pcm_post_resume
1114 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1116 snd_card_t *card = substream->pcm->card;
1119 snd_power_lock(card);
1120 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1121 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1122 snd_power_unlock(card);
1128 static int snd_pcm_resume(snd_pcm_substream_t *substream)
1133 #endif /* CONFIG_PM */
1138 * Change the RUNNING stream(s) to XRUN state.
1140 static int snd_pcm_xrun(snd_pcm_substream_t *substream)
1142 snd_card_t *card = substream->pcm->card;
1143 snd_pcm_runtime_t *runtime = substream->runtime;
1146 snd_power_lock(card);
1147 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1148 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1153 snd_pcm_stream_lock_irq(substream);
1154 switch (runtime->status->state) {
1155 case SNDRV_PCM_STATE_XRUN:
1156 result = 0; /* already there */
1158 case SNDRV_PCM_STATE_RUNNING:
1159 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1164 snd_pcm_stream_unlock_irq(substream);
1166 snd_power_unlock(card);
1173 static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
1175 snd_pcm_runtime_t *runtime = substream->runtime;
1176 switch (runtime->status->state) {
1177 case SNDRV_PCM_STATE_RUNNING:
1178 case SNDRV_PCM_STATE_PREPARED:
1179 case SNDRV_PCM_STATE_PAUSED:
1180 case SNDRV_PCM_STATE_SUSPENDED:
1187 static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
1189 snd_pcm_runtime_t *runtime = substream->runtime;
1190 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1193 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1194 runtime->hw_ptr_base = 0;
1195 runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
1196 runtime->silence_start = runtime->status->hw_ptr;
1197 runtime->silence_filled = 0;
1201 static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
1203 snd_pcm_runtime_t *runtime = substream->runtime;
1204 runtime->control->appl_ptr = runtime->status->hw_ptr;
1205 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1206 runtime->silence_size > 0)
1207 snd_pcm_playback_silence(substream, ULONG_MAX);
1210 static struct action_ops snd_pcm_action_reset = {
1211 .pre_action = snd_pcm_pre_reset,
1212 .do_action = snd_pcm_do_reset,
1213 .post_action = snd_pcm_post_reset
1216 static int snd_pcm_reset(snd_pcm_substream_t *substream)
1218 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1224 static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
1226 snd_pcm_runtime_t *runtime = substream->runtime;
1227 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1229 if (snd_pcm_running(substream))
1234 static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
1237 err = substream->ops->prepare(substream);
1240 return snd_pcm_do_reset(substream, 0);
1243 static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
1245 snd_pcm_runtime_t *runtime = substream->runtime;
1246 runtime->control->appl_ptr = runtime->status->hw_ptr;
1247 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1250 static struct action_ops snd_pcm_action_prepare = {
1251 .pre_action = snd_pcm_pre_prepare,
1252 .do_action = snd_pcm_do_prepare,
1253 .post_action = snd_pcm_post_prepare
1259 int snd_pcm_prepare(snd_pcm_substream_t *substream)
1262 snd_card_t *card = substream->pcm->card;
1264 snd_power_lock(card);
1265 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1266 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1267 snd_power_unlock(card);
1275 static int snd_pcm_pre_drain_init(snd_pcm_substream_t * substream, int state)
1277 if (substream->ffile->f_flags & O_NONBLOCK)
1279 substream->runtime->trigger_master = substream;
1283 static int snd_pcm_do_drain_init(snd_pcm_substream_t * substream, int state)
1285 snd_pcm_runtime_t *runtime = substream->runtime;
1286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1287 switch (runtime->status->state) {
1288 case SNDRV_PCM_STATE_PREPARED:
1289 /* start playback stream if possible */
1290 if (! snd_pcm_playback_empty(substream)) {
1291 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1292 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1295 case SNDRV_PCM_STATE_RUNNING:
1296 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1302 /* stop running stream */
1303 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1304 int state = snd_pcm_capture_avail(runtime) > 0 ?
1305 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1306 snd_pcm_do_stop(substream, state);
1307 snd_pcm_post_stop(substream, state);
1313 static void snd_pcm_post_drain_init(snd_pcm_substream_t * substream, int state)
1317 static struct action_ops snd_pcm_action_drain_init = {
1318 .pre_action = snd_pcm_pre_drain_init,
1319 .do_action = snd_pcm_do_drain_init,
1320 .post_action = snd_pcm_post_drain_init
1324 snd_pcm_substream_t *substream;
1326 snd_pcm_uframes_t stop_threshold;
1329 static int snd_pcm_drop(snd_pcm_substream_t *substream);
1332 * Drain the stream(s).
1333 * When the substream is linked, sync until the draining of all playback streams
1335 * After this call, all streams are supposed to be either SETUP or DRAINING
1336 * (capture only) state.
1338 static int snd_pcm_drain(snd_pcm_substream_t *substream)
1341 snd_pcm_runtime_t *runtime;
1342 struct list_head *pos;
1345 struct drain_rec *drec, drec_tmp, *d;
1347 snd_assert(substream != NULL, return -ENXIO);
1348 card = substream->pcm->card;
1349 runtime = substream->runtime;
1351 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1354 down_read(&snd_pcm_link_rwsem);
1355 snd_power_lock(card);
1356 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1357 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1362 /* allocate temporary record for drain sync */
1363 if (snd_pcm_stream_linked(substream)) {
1364 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1372 snd_pcm_stream_lock_irq(substream);
1374 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1375 snd_pcm_pause(substream, 0);
1377 /* pre-start/stop - all running streams are changed to DRAINING state */
1378 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1382 /* check streams with PLAYBACK & DRAINING */
1384 snd_pcm_group_for_each(pos, substream) {
1385 snd_pcm_substream_t *s = snd_pcm_group_substream_entry(pos);
1386 runtime = s->runtime;
1387 if (runtime->status->state != SNDRV_PCM_STATE_DRAINING) {
1388 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1391 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1392 d = &drec[num_drecs++];
1394 init_waitqueue_entry(&d->wait, current);
1395 add_wait_queue(&runtime->sleep, &d->wait);
1396 /* stop_threshold fixup to avoid endless loop when
1397 * stop_threshold > buffer_size
1399 d->stop_threshold = runtime->stop_threshold;
1400 if (runtime->stop_threshold > runtime->buffer_size)
1401 runtime->stop_threshold = runtime->buffer_size;
1410 if (signal_pending(current)) {
1411 result = -ERESTARTSYS;
1414 set_current_state(TASK_INTERRUPTIBLE);
1415 snd_pcm_stream_unlock_irq(substream);
1416 snd_power_unlock(card);
1417 tout = schedule_timeout(10 * HZ);
1418 snd_power_lock(card);
1419 snd_pcm_stream_lock_irq(substream);
1421 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1424 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1425 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1431 for (i = 0; i < num_drecs; i++) {
1432 runtime = drec[i].substream->runtime;
1433 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1439 for (i = 0; i < num_drecs; i++) {
1441 runtime = d->substream->runtime;
1442 remove_wait_queue(&runtime->sleep, &d->wait);
1443 runtime->stop_threshold = d->stop_threshold;
1447 snd_pcm_stream_unlock_irq(substream);
1448 if (drec != &drec_tmp)
1451 snd_power_unlock(card);
1452 up_read(&snd_pcm_link_rwsem);
1460 * Immediately put all linked substreams into SETUP state.
1462 static int snd_pcm_drop(snd_pcm_substream_t *substream)
1464 snd_pcm_runtime_t *runtime;
1468 snd_assert(substream != NULL, return -ENXIO);
1469 runtime = substream->runtime;
1470 card = substream->pcm->card;
1472 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1475 snd_power_lock(card);
1476 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1477 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1482 snd_pcm_stream_lock_irq(substream);
1484 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1485 snd_pcm_pause(substream, 0);
1487 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1488 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1489 snd_pcm_stream_unlock_irq(substream);
1491 snd_power_unlock(card);
1496 /* WARNING: Don't forget to fput back the file */
1497 extern int snd_major;
1498 static struct file *snd_pcm_file_fd(int fd)
1501 struct inode *inode;
1502 unsigned short minor;
1506 inode = file->f_dentry->d_inode;
1507 if (!S_ISCHR(inode->i_mode) ||
1508 imajor(inode) != snd_major) {
1512 minor = iminor(inode);
1514 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1524 static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
1528 snd_pcm_file_t *pcm_file;
1529 snd_pcm_substream_t *substream1;
1531 file = snd_pcm_file_fd(fd);
1534 pcm_file = file->private_data;
1535 substream1 = pcm_file->substream;
1536 down_write(&snd_pcm_link_rwsem);
1537 write_lock_irq(&snd_pcm_link_rwlock);
1538 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1539 substream->runtime->status->state != substream1->runtime->status->state) {
1543 if (snd_pcm_stream_linked(substream1)) {
1547 if (!snd_pcm_stream_linked(substream)) {
1548 substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
1549 if (substream->group == NULL) {
1553 spin_lock_init(&substream->group->lock);
1554 INIT_LIST_HEAD(&substream->group->substreams);
1555 list_add_tail(&substream->link_list, &substream->group->substreams);
1556 substream->group->count = 1;
1558 list_add_tail(&substream1->link_list, &substream->group->substreams);
1559 substream->group->count++;
1560 substream1->group = substream->group;
1562 write_unlock_irq(&snd_pcm_link_rwlock);
1563 up_write(&snd_pcm_link_rwsem);
1568 static void relink_to_local(snd_pcm_substream_t *substream)
1570 substream->group = &substream->self_group;
1571 INIT_LIST_HEAD(&substream->self_group.substreams);
1572 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1575 static int snd_pcm_unlink(snd_pcm_substream_t *substream)
1577 struct list_head *pos;
1580 down_write(&snd_pcm_link_rwsem);
1581 write_lock_irq(&snd_pcm_link_rwlock);
1582 if (!snd_pcm_stream_linked(substream)) {
1586 list_del(&substream->link_list);
1587 substream->group->count--;
1588 if (substream->group->count == 1) { /* detach the last stream, too */
1589 snd_pcm_group_for_each(pos, substream) {
1590 relink_to_local(snd_pcm_group_substream_entry(pos));
1593 kfree(substream->group);
1595 relink_to_local(substream);
1597 write_unlock_irq(&snd_pcm_link_rwlock);
1598 up_write(&snd_pcm_link_rwsem);
1605 static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
1606 snd_pcm_hw_rule_t *rule)
1609 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1610 hw_param_interval_c(params, rule->deps[1]), &t);
1611 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1614 static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
1615 snd_pcm_hw_rule_t *rule)
1618 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1619 hw_param_interval_c(params, rule->deps[1]), &t);
1620 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1623 static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
1624 snd_pcm_hw_rule_t *rule)
1627 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1628 hw_param_interval_c(params, rule->deps[1]),
1629 (unsigned long) rule->private, &t);
1630 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1633 static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
1634 snd_pcm_hw_rule_t *rule)
1637 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1638 (unsigned long) rule->private,
1639 hw_param_interval_c(params, rule->deps[1]), &t);
1640 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1643 static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
1644 snd_pcm_hw_rule_t *rule)
1647 snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
1649 snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1651 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1653 if (! snd_mask_test(mask, k))
1655 bits = snd_pcm_format_physical_width(k);
1657 continue; /* ignore invalid formats */
1658 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1659 snd_mask_reset(&m, k);
1661 return snd_mask_refine(mask, &m);
1664 static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
1665 snd_pcm_hw_rule_t *rule)
1673 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1675 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1677 bits = snd_pcm_format_physical_width(k);
1679 continue; /* ignore invalid formats */
1680 if (t.min > (unsigned)bits)
1682 if (t.max < (unsigned)bits)
1686 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1689 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1690 #error "Change this table"
1693 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1694 48000, 64000, 88200, 96000, 176400, 192000 };
1696 static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
1697 snd_pcm_hw_rule_t *rule)
1699 snd_pcm_hardware_t *hw = rule->private;
1700 return snd_interval_list(hw_param_interval(params, rule->var),
1701 ARRAY_SIZE(rates), rates, hw->rates);
1704 static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
1705 snd_pcm_hw_rule_t *rule)
1708 snd_pcm_substream_t *substream = rule->private;
1710 t.max = substream->buffer_bytes_max;
1714 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1717 int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
1719 snd_pcm_runtime_t *runtime = substream->runtime;
1720 snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
1723 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1724 snd_mask_any(constrs_mask(constrs, k));
1727 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1728 snd_interval_any(constrs_interval(constrs, k));
1731 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1732 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1733 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1734 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1735 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1737 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1738 snd_pcm_hw_rule_format, NULL,
1739 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1742 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1743 snd_pcm_hw_rule_sample_bits, NULL,
1744 SNDRV_PCM_HW_PARAM_FORMAT,
1745 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1748 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1749 snd_pcm_hw_rule_div, NULL,
1750 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1753 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1754 snd_pcm_hw_rule_mul, NULL,
1755 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1758 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1759 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1760 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1763 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1764 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1765 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1768 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1769 snd_pcm_hw_rule_div, NULL,
1770 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1773 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1774 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1775 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1778 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1779 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1780 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1783 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1784 snd_pcm_hw_rule_div, NULL,
1785 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1788 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1789 snd_pcm_hw_rule_div, NULL,
1790 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1793 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1794 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1795 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1798 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1799 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1800 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1804 snd_pcm_hw_rule_mul, NULL,
1805 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1809 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1810 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1814 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1815 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1819 snd_pcm_hw_rule_muldivk, (void*) 8,
1820 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1824 snd_pcm_hw_rule_muldivk, (void*) 8,
1825 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1829 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1830 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1834 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1841 int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
1843 snd_pcm_runtime_t *runtime = substream->runtime;
1844 snd_pcm_hardware_t *hw = &runtime->hw;
1846 unsigned int mask = 0;
1848 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1849 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1850 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1851 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1852 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1853 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1854 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1855 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1856 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1857 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1858 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1860 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1861 snd_assert(err >= 0, return -EINVAL);
1863 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1864 snd_assert(err >= 0, return -EINVAL);
1866 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1867 snd_assert(err >= 0, return -EINVAL);
1869 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1870 hw->channels_min, hw->channels_max);
1871 snd_assert(err >= 0, return -EINVAL);
1873 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1874 hw->rate_min, hw->rate_max);
1875 snd_assert(err >= 0, return -EINVAL);
1877 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1878 hw->period_bytes_min, hw->period_bytes_max);
1879 snd_assert(err >= 0, return -EINVAL);
1881 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1882 hw->periods_min, hw->periods_max);
1883 snd_assert(err >= 0, return -EINVAL);
1885 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1886 hw->period_bytes_min, hw->buffer_bytes_max);
1887 snd_assert(err >= 0, return -EINVAL);
1889 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1890 snd_pcm_hw_rule_buffer_bytes_max, substream,
1891 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1896 if (runtime->dma_bytes) {
1897 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1898 snd_assert(err >= 0, return -EINVAL);
1901 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1902 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1903 snd_pcm_hw_rule_rate, hw,
1904 SNDRV_PCM_HW_PARAM_RATE, -1);
1909 /* FIXME: this belong to lowlevel */
1910 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1911 1000000 / HZ, 1000000 / HZ);
1912 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1917 static void snd_pcm_add_file(snd_pcm_str_t *str,
1918 snd_pcm_file_t *pcm_file)
1920 pcm_file->next = str->files;
1921 str->files = pcm_file;
1924 static void snd_pcm_remove_file(snd_pcm_str_t *str,
1925 snd_pcm_file_t *pcm_file)
1927 snd_pcm_file_t * pcm_file1;
1928 if (str->files == pcm_file) {
1929 str->files = pcm_file->next;
1931 pcm_file1 = str->files;
1932 while (pcm_file1 && pcm_file1->next != pcm_file)
1933 pcm_file1 = pcm_file1->next;
1934 if (pcm_file1 != NULL)
1935 pcm_file1->next = pcm_file->next;
1939 static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
1941 snd_pcm_substream_t *substream;
1942 snd_pcm_runtime_t *runtime;
1943 snd_pcm_str_t * str;
1945 snd_assert(pcm_file != NULL, return -ENXIO);
1946 substream = pcm_file->substream;
1947 snd_assert(substream != NULL, return -ENXIO);
1948 runtime = substream->runtime;
1949 str = substream->pstr;
1950 snd_pcm_unlink(substream);
1951 if (substream->open_flag) {
1952 if (substream->ops->hw_free != NULL)
1953 substream->ops->hw_free(substream);
1954 substream->ops->close(substream);
1955 substream->open_flag = 0;
1957 substream->ffile = NULL;
1958 snd_pcm_remove_file(str, pcm_file);
1959 snd_pcm_release_substream(substream);
1964 static int snd_pcm_open_file(struct file *file,
1967 snd_pcm_file_t **rpcm_file)
1970 snd_pcm_file_t *pcm_file;
1971 snd_pcm_substream_t *substream;
1974 snd_assert(rpcm_file != NULL, return -EINVAL);
1977 pcm_file = kcalloc(1, sizeof(*pcm_file), GFP_KERNEL);
1978 if (pcm_file == NULL) {
1982 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
1987 str = substream->pstr;
1988 substream->file = pcm_file;
1989 substream->no_mmap_ctrl = 0;
1991 pcm_file->substream = substream;
1993 snd_pcm_add_file(str, pcm_file);
1995 err = snd_pcm_hw_constraints_init(substream);
1997 snd_printd("snd_pcm_hw_constraints_init failed\n");
1998 snd_pcm_release_file(pcm_file);
2002 if ((err = substream->ops->open(substream)) < 0) {
2003 snd_pcm_release_file(pcm_file);
2006 substream->open_flag = 1;
2008 err = snd_pcm_hw_constraints_complete(substream);
2010 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2011 substream->ops->close(substream);
2012 snd_pcm_release_file(pcm_file);
2016 substream->ffile = file;
2018 file->private_data = pcm_file;
2019 *rpcm_file = pcm_file;
2023 static int snd_pcm_open(struct inode *inode, struct file *file)
2025 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
2026 int device = SNDRV_MINOR_DEVICE(iminor(inode));
2029 snd_pcm_file_t *pcm_file;
2032 snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
2033 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2038 err = snd_card_file_add(pcm->card, file);
2041 if (!try_module_get(pcm->card->module)) {
2045 init_waitqueue_entry(&wait, current);
2046 add_wait_queue(&pcm->open_wait, &wait);
2047 down(&pcm->open_mutex);
2049 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2052 if (err == -EAGAIN) {
2053 if (file->f_flags & O_NONBLOCK) {
2059 set_current_state(TASK_INTERRUPTIBLE);
2060 up(&pcm->open_mutex);
2062 down(&pcm->open_mutex);
2063 if (signal_pending(current)) {
2068 remove_wait_queue(&pcm->open_wait, &wait);
2069 up(&pcm->open_mutex);
2075 module_put(pcm->card->module);
2077 snd_card_file_remove(pcm->card, file);
2082 static int snd_pcm_release(struct inode *inode, struct file *file)
2085 snd_pcm_substream_t *substream;
2086 snd_pcm_file_t *pcm_file;
2088 pcm_file = file->private_data;
2089 substream = pcm_file->substream;
2090 snd_assert(substream != NULL, return -ENXIO);
2091 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2092 pcm = substream->pcm;
2093 snd_pcm_drop(substream);
2094 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2095 down(&pcm->open_mutex);
2096 snd_pcm_release_file(pcm_file);
2097 up(&pcm->open_mutex);
2098 wake_up(&pcm->open_wait);
2099 module_put(pcm->card->module);
2100 snd_card_file_remove(pcm->card, file);
2104 static snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2106 snd_pcm_runtime_t *runtime = substream->runtime;
2107 snd_pcm_sframes_t appl_ptr;
2108 snd_pcm_sframes_t ret;
2109 snd_pcm_sframes_t hw_avail;
2114 snd_pcm_stream_lock_irq(substream);
2115 switch (runtime->status->state) {
2116 case SNDRV_PCM_STATE_PREPARED:
2118 case SNDRV_PCM_STATE_DRAINING:
2119 case SNDRV_PCM_STATE_RUNNING:
2120 if (snd_pcm_update_hw_ptr(substream) >= 0)
2123 case SNDRV_PCM_STATE_XRUN:
2131 hw_avail = snd_pcm_playback_hw_avail(runtime);
2132 if (hw_avail <= 0) {
2136 if (frames > (snd_pcm_uframes_t)hw_avail)
2139 frames -= frames % runtime->xfer_align;
2140 appl_ptr = runtime->control->appl_ptr - frames;
2142 appl_ptr += runtime->boundary;
2143 runtime->control->appl_ptr = appl_ptr;
2144 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2146 snd_pcm_tick_prepare(substream);
2149 snd_pcm_stream_unlock_irq(substream);
2153 static snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2155 snd_pcm_runtime_t *runtime = substream->runtime;
2156 snd_pcm_sframes_t appl_ptr;
2157 snd_pcm_sframes_t ret;
2158 snd_pcm_sframes_t hw_avail;
2163 snd_pcm_stream_lock_irq(substream);
2164 switch (runtime->status->state) {
2165 case SNDRV_PCM_STATE_PREPARED:
2166 case SNDRV_PCM_STATE_DRAINING:
2168 case SNDRV_PCM_STATE_RUNNING:
2169 if (snd_pcm_update_hw_ptr(substream) >= 0)
2172 case SNDRV_PCM_STATE_XRUN:
2180 hw_avail = snd_pcm_capture_hw_avail(runtime);
2181 if (hw_avail <= 0) {
2185 if (frames > (snd_pcm_uframes_t)hw_avail)
2188 frames -= frames % runtime->xfer_align;
2189 appl_ptr = runtime->control->appl_ptr - frames;
2191 appl_ptr += runtime->boundary;
2192 runtime->control->appl_ptr = appl_ptr;
2193 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2195 snd_pcm_tick_prepare(substream);
2198 snd_pcm_stream_unlock_irq(substream);
2202 static snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2204 snd_pcm_runtime_t *runtime = substream->runtime;
2205 snd_pcm_sframes_t appl_ptr;
2206 snd_pcm_sframes_t ret;
2207 snd_pcm_sframes_t avail;
2212 snd_pcm_stream_lock_irq(substream);
2213 switch (runtime->status->state) {
2214 case SNDRV_PCM_STATE_PREPARED:
2215 case SNDRV_PCM_STATE_PAUSED:
2217 case SNDRV_PCM_STATE_DRAINING:
2218 case SNDRV_PCM_STATE_RUNNING:
2219 if (snd_pcm_update_hw_ptr(substream) >= 0)
2222 case SNDRV_PCM_STATE_XRUN:
2230 avail = snd_pcm_playback_avail(runtime);
2235 if (frames > (snd_pcm_uframes_t)avail)
2238 frames -= frames % runtime->xfer_align;
2239 appl_ptr = runtime->control->appl_ptr + frames;
2240 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2241 appl_ptr -= runtime->boundary;
2242 runtime->control->appl_ptr = appl_ptr;
2243 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2245 snd_pcm_tick_prepare(substream);
2248 snd_pcm_stream_unlock_irq(substream);
2252 static snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
2254 snd_pcm_runtime_t *runtime = substream->runtime;
2255 snd_pcm_sframes_t appl_ptr;
2256 snd_pcm_sframes_t ret;
2257 snd_pcm_sframes_t avail;
2262 snd_pcm_stream_lock_irq(substream);
2263 switch (runtime->status->state) {
2264 case SNDRV_PCM_STATE_PREPARED:
2265 case SNDRV_PCM_STATE_DRAINING:
2266 case SNDRV_PCM_STATE_PAUSED:
2268 case SNDRV_PCM_STATE_RUNNING:
2269 if (snd_pcm_update_hw_ptr(substream) >= 0)
2272 case SNDRV_PCM_STATE_XRUN:
2280 avail = snd_pcm_capture_avail(runtime);
2285 if (frames > (snd_pcm_uframes_t)avail)
2288 frames -= frames % runtime->xfer_align;
2289 appl_ptr = runtime->control->appl_ptr + frames;
2290 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2291 appl_ptr -= runtime->boundary;
2292 runtime->control->appl_ptr = appl_ptr;
2293 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2295 snd_pcm_tick_prepare(substream);
2298 snd_pcm_stream_unlock_irq(substream);
2302 static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
2304 snd_pcm_runtime_t *runtime = substream->runtime;
2307 snd_pcm_stream_lock_irq(substream);
2308 switch (runtime->status->state) {
2309 case SNDRV_PCM_STATE_DRAINING:
2310 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2312 case SNDRV_PCM_STATE_RUNNING:
2313 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2316 case SNDRV_PCM_STATE_PREPARED:
2317 case SNDRV_PCM_STATE_SUSPENDED:
2320 case SNDRV_PCM_STATE_XRUN:
2328 snd_pcm_stream_unlock_irq(substream);
2332 static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t __user *res)
2334 snd_pcm_runtime_t *runtime = substream->runtime;
2336 snd_pcm_sframes_t n = 0;
2338 snd_pcm_stream_lock_irq(substream);
2339 switch (runtime->status->state) {
2340 case SNDRV_PCM_STATE_DRAINING:
2341 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2343 case SNDRV_PCM_STATE_RUNNING:
2344 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2347 case SNDRV_PCM_STATE_PREPARED:
2348 case SNDRV_PCM_STATE_SUSPENDED:
2350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2351 n = snd_pcm_playback_hw_avail(runtime);
2353 n = snd_pcm_capture_avail(runtime);
2355 case SNDRV_PCM_STATE_XRUN:
2363 snd_pcm_stream_unlock_irq(substream);
2365 if (put_user(n, res))
2370 static int snd_pcm_sync_ptr(snd_pcm_substream_t *substream, struct sndrv_pcm_sync_ptr __user *_sync_ptr)
2372 snd_pcm_runtime_t *runtime = substream->runtime;
2373 struct sndrv_pcm_sync_ptr sync_ptr;
2374 volatile struct sndrv_pcm_mmap_status *status;
2375 volatile struct sndrv_pcm_mmap_control *control;
2378 memset(&sync_ptr, 0, sizeof(sync_ptr));
2379 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2381 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct sndrv_pcm_mmap_control)))
2383 status = runtime->status;
2384 control = runtime->control;
2385 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2386 err = snd_pcm_hwsync(substream);
2390 snd_pcm_stream_lock_irq(substream);
2391 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2392 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2394 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2395 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2396 control->avail_min = sync_ptr.c.control.avail_min;
2398 sync_ptr.c.control.avail_min = control->avail_min;
2399 sync_ptr.s.status.state = status->state;
2400 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2401 sync_ptr.s.status.tstamp = status->tstamp;
2402 sync_ptr.s.status.suspended_state = status->suspended_state;
2403 snd_pcm_stream_unlock_irq(substream);
2404 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2409 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2410 unsigned int cmd, void __user *arg);
2411 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2412 unsigned int cmd, void __user *arg);
2414 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2415 unsigned int cmd, void __user *arg)
2417 snd_assert(substream != NULL, return -ENXIO);
2420 case SNDRV_PCM_IOCTL_PVERSION:
2421 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2422 case SNDRV_PCM_IOCTL_INFO:
2423 return snd_pcm_info_user(substream, arg);
2424 case SNDRV_PCM_IOCTL_TSTAMP:
2427 if (get_user(xarg, (int __user *)arg))
2429 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2432 case SNDRV_PCM_IOCTL_HW_REFINE:
2433 return snd_pcm_hw_refine_user(substream, arg);
2434 case SNDRV_PCM_IOCTL_HW_PARAMS:
2435 return snd_pcm_hw_params_user(substream, arg);
2436 case SNDRV_PCM_IOCTL_HW_FREE:
2437 return snd_pcm_hw_free(substream);
2438 case SNDRV_PCM_IOCTL_SW_PARAMS:
2439 return snd_pcm_sw_params_user(substream, arg);
2440 case SNDRV_PCM_IOCTL_STATUS:
2441 return snd_pcm_status_user(substream, arg);
2442 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2443 return snd_pcm_channel_info(substream, arg);
2444 case SNDRV_PCM_IOCTL_PREPARE:
2445 return snd_pcm_prepare(substream);
2446 case SNDRV_PCM_IOCTL_RESET:
2447 return snd_pcm_reset(substream);
2448 case SNDRV_PCM_IOCTL_START:
2449 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2450 case SNDRV_PCM_IOCTL_LINK:
2451 return snd_pcm_link(substream, (int)(unsigned long) arg);
2452 case SNDRV_PCM_IOCTL_UNLINK:
2453 return snd_pcm_unlink(substream);
2454 case SNDRV_PCM_IOCTL_RESUME:
2455 return snd_pcm_resume(substream);
2456 case SNDRV_PCM_IOCTL_XRUN:
2457 return snd_pcm_xrun(substream);
2458 case SNDRV_PCM_IOCTL_HWSYNC:
2459 return snd_pcm_hwsync(substream);
2460 case SNDRV_PCM_IOCTL_DELAY:
2461 return snd_pcm_delay(substream, arg);
2462 case SNDRV_PCM_IOCTL_SYNC_PTR:
2463 return snd_pcm_sync_ptr(substream, arg);
2464 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2465 return snd_pcm_hw_refine_old_user(substream, arg);
2466 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2467 return snd_pcm_hw_params_old_user(substream, arg);
2468 case SNDRV_PCM_IOCTL_DRAIN:
2469 return snd_pcm_drain(substream);
2470 case SNDRV_PCM_IOCTL_DROP:
2471 return snd_pcm_drop(substream);
2473 snd_printd("unknown ioctl = 0x%x\n", cmd);
2477 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2478 unsigned int cmd, void __user *arg)
2480 snd_assert(substream != NULL, return -ENXIO);
2481 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2483 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2486 snd_xferi_t __user *_xferi = arg;
2487 snd_pcm_runtime_t *runtime = substream->runtime;
2488 snd_pcm_sframes_t result;
2489 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2491 if (put_user(0, &_xferi->result))
2493 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2495 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2496 __put_user(result, &_xferi->result);
2497 return result < 0 ? result : 0;
2499 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2502 snd_xfern_t __user *_xfern = arg;
2503 snd_pcm_runtime_t *runtime = substream->runtime;
2505 snd_pcm_sframes_t result;
2506 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2508 if (runtime->channels > 128)
2510 if (put_user(0, &_xfern->result))
2512 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2514 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2517 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2521 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2523 __put_user(result, &_xfern->result);
2524 return result < 0 ? result : 0;
2526 case SNDRV_PCM_IOCTL_REWIND:
2528 snd_pcm_uframes_t frames;
2529 snd_pcm_uframes_t __user *_frames = arg;
2530 snd_pcm_sframes_t result;
2531 if (get_user(frames, _frames))
2533 if (put_user(0, _frames))
2535 result = snd_pcm_playback_rewind(substream, frames);
2536 __put_user(result, _frames);
2537 return result < 0 ? result : 0;
2539 case SNDRV_PCM_IOCTL_FORWARD:
2541 snd_pcm_uframes_t frames;
2542 snd_pcm_uframes_t __user *_frames = arg;
2543 snd_pcm_sframes_t result;
2544 if (get_user(frames, _frames))
2546 if (put_user(0, _frames))
2548 result = snd_pcm_playback_forward(substream, frames);
2549 __put_user(result, _frames);
2550 return result < 0 ? result : 0;
2552 case SNDRV_PCM_IOCTL_PAUSE:
2555 snd_pcm_stream_lock_irq(substream);
2556 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2557 snd_pcm_stream_unlock_irq(substream);
2561 return snd_pcm_common_ioctl1(substream, cmd, arg);
2564 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2565 unsigned int cmd, void __user *arg)
2567 snd_assert(substream != NULL, return -ENXIO);
2568 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2570 case SNDRV_PCM_IOCTL_READI_FRAMES:
2573 snd_xferi_t __user *_xferi = arg;
2574 snd_pcm_runtime_t *runtime = substream->runtime;
2575 snd_pcm_sframes_t result;
2576 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2578 if (put_user(0, &_xferi->result))
2580 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2582 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2583 __put_user(result, &_xferi->result);
2584 return result < 0 ? result : 0;
2586 case SNDRV_PCM_IOCTL_READN_FRAMES:
2589 snd_xfern_t __user *_xfern = arg;
2590 snd_pcm_runtime_t *runtime = substream->runtime;
2592 snd_pcm_sframes_t result;
2593 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2595 if (runtime->channels > 128)
2597 if (put_user(0, &_xfern->result))
2599 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2601 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2604 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2608 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2610 __put_user(result, &_xfern->result);
2611 return result < 0 ? result : 0;
2613 case SNDRV_PCM_IOCTL_REWIND:
2615 snd_pcm_uframes_t frames;
2616 snd_pcm_uframes_t __user *_frames = arg;
2617 snd_pcm_sframes_t result;
2618 if (get_user(frames, _frames))
2620 if (put_user(0, _frames))
2622 result = snd_pcm_capture_rewind(substream, frames);
2623 __put_user(result, _frames);
2624 return result < 0 ? result : 0;
2626 case SNDRV_PCM_IOCTL_FORWARD:
2628 snd_pcm_uframes_t frames;
2629 snd_pcm_uframes_t __user *_frames = arg;
2630 snd_pcm_sframes_t result;
2631 if (get_user(frames, _frames))
2633 if (put_user(0, _frames))
2635 result = snd_pcm_capture_forward(substream, frames);
2636 __put_user(result, _frames);
2637 return result < 0 ? result : 0;
2640 return snd_pcm_common_ioctl1(substream, cmd, arg);
2643 static int snd_pcm_playback_ioctl(struct inode *inode, struct file *file,
2644 unsigned int cmd, unsigned long arg)
2646 snd_pcm_file_t *pcm_file;
2649 pcm_file = file->private_data;
2651 if (((cmd >> 8) & 0xff) != 'A')
2654 /* FIXME: need to unlock BKL to allow preemption */
2656 err = snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2661 static int snd_pcm_capture_ioctl(struct inode *inode, struct file *file,
2662 unsigned int cmd, unsigned long arg)
2664 snd_pcm_file_t *pcm_file;
2667 pcm_file = file->private_data;
2669 if (((cmd >> 8) & 0xff) != 'A')
2672 /* FIXME: need to unlock BKL to allow preemption */
2674 err = snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2679 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2680 unsigned int cmd, void *arg)
2685 fs = snd_enter_user();
2686 result = snd_pcm_playback_ioctl1(substream, cmd, arg);
2691 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2692 unsigned int cmd, void *arg)
2697 fs = snd_enter_user();
2698 result = snd_pcm_capture_ioctl1(substream, cmd, arg);
2703 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2704 unsigned int cmd, void *arg)
2706 switch (substream->stream) {
2707 case SNDRV_PCM_STREAM_PLAYBACK:
2708 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2709 case SNDRV_PCM_STREAM_CAPTURE:
2710 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2716 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
2718 snd_pcm_file_t *pcm_file;
2719 snd_pcm_substream_t *substream;
2720 snd_pcm_runtime_t *runtime;
2721 snd_pcm_sframes_t result;
2723 pcm_file = file->private_data;
2724 substream = pcm_file->substream;
2725 snd_assert(substream != NULL, return -ENXIO);
2726 runtime = substream->runtime;
2727 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2729 if (!frame_aligned(runtime, count))
2731 count = bytes_to_frames(runtime, count);
2732 result = snd_pcm_lib_read(substream, buf, count);
2734 result = frames_to_bytes(runtime, result);
2738 static ssize_t snd_pcm_write(struct file *file, const char __user *buf, size_t count, loff_t * offset)
2740 snd_pcm_file_t *pcm_file;
2741 snd_pcm_substream_t *substream;
2742 snd_pcm_runtime_t *runtime;
2743 snd_pcm_sframes_t result;
2745 pcm_file = file->private_data;
2746 substream = pcm_file->substream;
2747 snd_assert(substream != NULL, result = -ENXIO; goto end);
2748 runtime = substream->runtime;
2749 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2753 if (!frame_aligned(runtime, count)) {
2757 count = bytes_to_frames(runtime, count);
2758 result = snd_pcm_lib_write(substream, buf, count);
2760 result = frames_to_bytes(runtime, result);
2765 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2766 unsigned long count, loff_t * offset)
2769 snd_pcm_file_t *pcm_file;
2770 snd_pcm_substream_t *substream;
2771 snd_pcm_runtime_t *runtime;
2772 snd_pcm_sframes_t result;
2775 snd_pcm_uframes_t frames;
2777 pcm_file = file->private_data;
2778 substream = pcm_file->substream;
2779 snd_assert(substream != NULL, return -ENXIO);
2780 runtime = substream->runtime;
2781 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2783 if (count > 1024 || count != runtime->channels)
2785 if (!frame_aligned(runtime, _vector->iov_len))
2787 frames = bytes_to_samples(runtime, _vector->iov_len);
2788 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2791 for (i = 0; i < count; ++i)
2792 bufs[i] = _vector[i].iov_base;
2793 result = snd_pcm_lib_readv(substream, bufs, frames);
2795 result = frames_to_bytes(runtime, result);
2800 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2801 unsigned long count, loff_t * offset)
2803 snd_pcm_file_t *pcm_file;
2804 snd_pcm_substream_t *substream;
2805 snd_pcm_runtime_t *runtime;
2806 snd_pcm_sframes_t result;
2809 snd_pcm_uframes_t frames;
2811 pcm_file = file->private_data;
2812 substream = pcm_file->substream;
2813 snd_assert(substream != NULL, result = -ENXIO; goto end);
2814 runtime = substream->runtime;
2815 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2819 if (count > 128 || count != runtime->channels ||
2820 !frame_aligned(runtime, _vector->iov_len)) {
2824 frames = bytes_to_samples(runtime, _vector->iov_len);
2825 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2828 for (i = 0; i < count; ++i)
2829 bufs[i] = _vector[i].iov_base;
2830 result = snd_pcm_lib_writev(substream, bufs, frames);
2832 result = frames_to_bytes(runtime, result);
2838 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2840 snd_pcm_file_t *pcm_file;
2841 snd_pcm_substream_t *substream;
2842 snd_pcm_runtime_t *runtime;
2844 snd_pcm_uframes_t avail;
2846 pcm_file = file->private_data;
2848 substream = pcm_file->substream;
2849 snd_assert(substream != NULL, return -ENXIO);
2850 runtime = substream->runtime;
2852 poll_wait(file, &runtime->sleep, wait);
2854 snd_pcm_stream_lock_irq(substream);
2855 avail = snd_pcm_playback_avail(runtime);
2856 switch (runtime->status->state) {
2857 case SNDRV_PCM_STATE_RUNNING:
2858 case SNDRV_PCM_STATE_PREPARED:
2859 case SNDRV_PCM_STATE_PAUSED:
2860 if (avail >= runtime->control->avail_min) {
2861 mask = POLLOUT | POLLWRNORM;
2865 case SNDRV_PCM_STATE_DRAINING:
2869 mask = POLLOUT | POLLWRNORM | POLLERR;
2872 snd_pcm_stream_unlock_irq(substream);
2876 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2878 snd_pcm_file_t *pcm_file;
2879 snd_pcm_substream_t *substream;
2880 snd_pcm_runtime_t *runtime;
2882 snd_pcm_uframes_t avail;
2884 pcm_file = file->private_data;
2886 substream = pcm_file->substream;
2887 snd_assert(substream != NULL, return -ENXIO);
2888 runtime = substream->runtime;
2890 poll_wait(file, &runtime->sleep, wait);
2892 snd_pcm_stream_lock_irq(substream);
2893 avail = snd_pcm_capture_avail(runtime);
2894 switch (runtime->status->state) {
2895 case SNDRV_PCM_STATE_RUNNING:
2896 case SNDRV_PCM_STATE_PREPARED:
2897 case SNDRV_PCM_STATE_PAUSED:
2898 if (avail >= runtime->control->avail_min) {
2899 mask = POLLIN | POLLRDNORM;
2904 case SNDRV_PCM_STATE_DRAINING:
2906 mask = POLLIN | POLLRDNORM;
2911 mask = POLLIN | POLLRDNORM | POLLERR;
2914 snd_pcm_stream_unlock_irq(substream);
2923 * Only on coherent architectures, we can mmap the status and the control records
2924 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2926 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2928 * mmap status record
2930 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2932 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2933 snd_pcm_runtime_t *runtime;
2936 if (substream == NULL)
2938 runtime = substream->runtime;
2939 page = virt_to_page(runtime->status);
2940 if (!PageReserved(page))
2943 *type = VM_FAULT_MINOR;
2947 static struct vm_operations_struct snd_pcm_vm_ops_status =
2949 .nopage = snd_pcm_mmap_status_nopage,
2952 static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2953 struct vm_area_struct *area)
2955 snd_pcm_runtime_t *runtime;
2957 if (!(area->vm_flags & VM_READ))
2959 runtime = substream->runtime;
2960 snd_assert(runtime != NULL, return -EAGAIN);
2961 size = area->vm_end - area->vm_start;
2962 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2964 area->vm_ops = &snd_pcm_vm_ops_status;
2965 area->vm_private_data = substream;
2966 area->vm_flags |= VM_RESERVED;
2971 * mmap control record
2973 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2975 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2976 snd_pcm_runtime_t *runtime;
2979 if (substream == NULL)
2981 runtime = substream->runtime;
2982 page = virt_to_page(runtime->control);
2983 if (!PageReserved(page))
2986 *type = VM_FAULT_MINOR;
2990 static struct vm_operations_struct snd_pcm_vm_ops_control =
2992 .nopage = snd_pcm_mmap_control_nopage,
2995 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
2996 struct vm_area_struct *area)
2998 snd_pcm_runtime_t *runtime;
3000 if (!(area->vm_flags & VM_READ))
3002 runtime = substream->runtime;
3003 snd_assert(runtime != NULL, return -EAGAIN);
3004 size = area->vm_end - area->vm_start;
3005 if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
3007 area->vm_ops = &snd_pcm_vm_ops_control;
3008 area->vm_private_data = substream;
3009 area->vm_flags |= VM_RESERVED;
3012 #else /* ! coherent mmap */
3014 * don't support mmap for status and control records.
3016 static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
3017 struct vm_area_struct *area)
3021 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
3022 struct vm_area_struct *area)
3026 #endif /* coherent mmap */
3029 * nopage callback for mmapping a RAM page
3031 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
3033 snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
3034 snd_pcm_runtime_t *runtime;
3035 unsigned long offset;
3040 if (substream == NULL)
3042 runtime = substream->runtime;
3043 offset = area->vm_pgoff << PAGE_SHIFT;
3044 offset += address - area->vm_start;
3045 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3046 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3047 if (offset > dma_bytes - PAGE_SIZE)
3048 return NOPAGE_SIGBUS;
3049 if (substream->ops->page) {
3050 page = substream->ops->page(substream, offset);
3054 vaddr = runtime->dma_area + offset;
3055 page = virt_to_page(vaddr);
3057 if (!PageReserved(page))
3060 *type = VM_FAULT_MINOR;
3064 static struct vm_operations_struct snd_pcm_vm_ops_data =
3066 .open = snd_pcm_mmap_data_open,
3067 .close = snd_pcm_mmap_data_close,
3068 .nopage = snd_pcm_mmap_data_nopage,
3072 * mmap the DMA buffer on RAM
3074 static int snd_pcm_default_mmap(snd_pcm_substream_t *substream, struct vm_area_struct *area)
3076 area->vm_ops = &snd_pcm_vm_ops_data;
3077 area->vm_private_data = substream;
3078 area->vm_flags |= VM_RESERVED;
3079 atomic_inc(&substream->runtime->mmap_count);
3084 * mmap the DMA buffer on I/O memory area
3086 #if SNDRV_PCM_INFO_MMAP_IOMEM
3087 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3089 .open = snd_pcm_mmap_data_open,
3090 .close = snd_pcm_mmap_data_close,
3093 int snd_pcm_lib_mmap_iomem(snd_pcm_substream_t *substream, struct vm_area_struct *area)
3096 unsigned long offset;
3098 #ifdef pgprot_noncached
3099 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3101 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3102 area->vm_private_data = substream;
3103 area->vm_flags |= VM_IO;
3104 size = area->vm_end - area->vm_start;
3105 offset = area->vm_pgoff << PAGE_SHIFT;
3106 if (io_remap_page_range(area, area->vm_start,
3107 substream->runtime->dma_addr + offset,
3108 size, area->vm_page_prot))
3110 atomic_inc(&substream->runtime->mmap_count);
3113 #endif /* SNDRV_PCM_INFO_MMAP */
3118 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
3119 struct vm_area_struct *area)
3121 snd_pcm_runtime_t *runtime;
3123 unsigned long offset;
3126 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3127 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3130 if (!(area->vm_flags & VM_READ))
3133 runtime = substream->runtime;
3134 snd_assert(runtime != NULL, return -EAGAIN);
3135 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3137 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3139 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3140 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3142 size = area->vm_end - area->vm_start;
3143 offset = area->vm_pgoff << PAGE_SHIFT;
3144 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3145 if ((size_t)size > dma_bytes)
3147 if (offset > dma_bytes - size)
3150 if (substream->ops->mmap)
3151 return substream->ops->mmap(substream, area);
3153 return snd_pcm_default_mmap(substream, area);
3156 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3158 snd_pcm_file_t * pcm_file;
3159 snd_pcm_substream_t *substream;
3160 unsigned long offset;
3162 pcm_file = file->private_data;
3163 substream = pcm_file->substream;
3164 snd_assert(substream != NULL, return -ENXIO);
3166 offset = area->vm_pgoff << PAGE_SHIFT;
3168 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3169 if (substream->no_mmap_ctrl)
3171 return snd_pcm_mmap_status(substream, file, area);
3172 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3173 if (substream->no_mmap_ctrl)
3175 return snd_pcm_mmap_control(substream, file, area);
3177 return snd_pcm_mmap_data(substream, file, area);
3182 static int snd_pcm_fasync(int fd, struct file * file, int on)
3184 snd_pcm_file_t * pcm_file;
3185 snd_pcm_substream_t *substream;
3186 snd_pcm_runtime_t *runtime;
3189 pcm_file = file->private_data;
3190 substream = pcm_file->substream;
3191 snd_assert(substream != NULL, return -ENXIO);
3192 runtime = substream->runtime;
3194 err = fasync_helper(fd, file, on, &runtime->fasync);
3201 * To be removed helpers to keep binary compatibility
3204 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3205 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3207 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
3211 memset(params, 0, sizeof(*params));
3212 params->flags = oparams->flags;
3213 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3214 params->masks[i].bits[0] = oparams->masks[i];
3215 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3216 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3217 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3218 params->info = oparams->info;
3219 params->msbits = oparams->msbits;
3220 params->rate_num = oparams->rate_num;
3221 params->rate_den = oparams->rate_den;
3222 params->fifo_size = oparams->fifo_size;
3225 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3229 memset(oparams, 0, sizeof(*oparams));
3230 oparams->flags = params->flags;
3231 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3232 oparams->masks[i] = params->masks[i].bits[0];
3233 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3234 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3235 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3236 oparams->info = params->info;
3237 oparams->msbits = params->msbits;
3238 oparams->rate_num = params->rate_num;
3239 oparams->rate_den = params->rate_den;
3240 oparams->fifo_size = params->fifo_size;
3243 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
3245 snd_pcm_hw_params_t *params;
3246 struct sndrv_pcm_hw_params_old *oparams = NULL;
3249 params = kmalloc(sizeof(*params), GFP_KERNEL);
3254 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3260 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3264 snd_pcm_hw_convert_from_old_params(params, oparams);
3265 err = snd_pcm_hw_refine(substream, params);
3266 snd_pcm_hw_convert_to_old_params(oparams, params);
3267 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3277 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
3279 snd_pcm_hw_params_t *params;
3280 struct sndrv_pcm_hw_params_old *oparams = NULL;
3283 params = kmalloc(sizeof(*params), GFP_KERNEL);
3288 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3293 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3297 snd_pcm_hw_convert_from_old_params(params, oparams);
3298 err = snd_pcm_hw_params(substream, params);
3299 snd_pcm_hw_convert_to_old_params(oparams, params);
3300 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3314 static struct file_operations snd_pcm_f_ops_playback = {
3315 .owner = THIS_MODULE,
3316 .write = snd_pcm_write,
3317 .writev = snd_pcm_writev,
3318 .open = snd_pcm_open,
3319 .release = snd_pcm_release,
3320 .poll = snd_pcm_playback_poll,
3321 .ioctl = snd_pcm_playback_ioctl,
3322 .mmap = snd_pcm_mmap,
3323 .fasync = snd_pcm_fasync,
3326 static struct file_operations snd_pcm_f_ops_capture = {
3327 .owner = THIS_MODULE,
3328 .read = snd_pcm_read,
3329 .readv = snd_pcm_readv,
3330 .open = snd_pcm_open,
3331 .release = snd_pcm_release,
3332 .poll = snd_pcm_capture_poll,
3333 .ioctl = snd_pcm_capture_ioctl,
3334 .mmap = snd_pcm_mmap,
3335 .fasync = snd_pcm_fasync,
3338 snd_minor_t snd_pcm_reg[2] =
3341 .comment = "digital audio playback",
3342 .f_ops = &snd_pcm_f_ops_playback,
3345 .comment = "digital audio capture",
3346 .f_ops = &snd_pcm_f_ops_capture,