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/latency.h>
29 #include <linux/uio.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
43 struct snd_pcm_hw_params_old {
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 DEFINE_RWLOCK(snd_pcm_link_rwlock);
75 EXPORT_SYMBOL(snd_pcm_link_rwlock);
77 static DECLARE_RWSEM(snd_pcm_link_rwsem);
79 static inline mm_segment_t snd_enter_user(void)
81 mm_segment_t fs = get_fs();
86 static inline void snd_leave_user(mm_segment_t fs)
93 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
95 struct snd_pcm_runtime *runtime;
96 struct snd_pcm *pcm = substream->pcm;
97 struct snd_pcm_str *pstr = substream->pstr;
99 snd_assert(substream != NULL, return -ENXIO);
100 memset(info, 0, sizeof(*info));
101 info->card = pcm->card->number;
102 info->device = pcm->device;
103 info->stream = substream->stream;
104 info->subdevice = substream->number;
105 strlcpy(info->id, pcm->id, sizeof(info->id));
106 strlcpy(info->name, pcm->name, sizeof(info->name));
107 info->dev_class = pcm->dev_class;
108 info->dev_subclass = pcm->dev_subclass;
109 info->subdevices_count = pstr->substream_count;
110 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
111 strlcpy(info->subname, substream->name, sizeof(info->subname));
112 runtime = substream->runtime;
113 /* AB: FIXME!!! This is definitely nonsense */
115 info->sync = runtime->sync;
116 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
121 int snd_pcm_info_user(struct snd_pcm_substream *substream,
122 struct snd_pcm_info __user * _info)
124 struct snd_pcm_info *info;
127 info = kmalloc(sizeof(*info), GFP_KERNEL);
130 err = snd_pcm_info(substream, info);
132 if (copy_to_user(_info, info, sizeof(*info)))
142 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
143 char *snd_pcm_hw_param_names[] = {
147 HW_PARAM(SAMPLE_BITS),
148 HW_PARAM(FRAME_BITS),
151 HW_PARAM(PERIOD_TIME),
152 HW_PARAM(PERIOD_SIZE),
153 HW_PARAM(PERIOD_BYTES),
155 HW_PARAM(BUFFER_TIME),
156 HW_PARAM(BUFFER_SIZE),
157 HW_PARAM(BUFFER_BYTES),
162 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
163 struct snd_pcm_hw_params *params)
166 struct snd_pcm_hardware *hw;
167 struct snd_interval *i = NULL;
168 struct snd_mask *m = NULL;
169 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
170 unsigned int rstamps[constrs->rules_num];
171 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
172 unsigned int stamp = 2;
176 params->fifo_size = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
180 params->rate_num = 0;
181 params->rate_den = 0;
184 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
185 m = hw_param_mask(params, k);
186 if (snd_mask_empty(m))
188 if (!(params->rmask & (1 << k)))
191 printk("%s = ", snd_pcm_hw_param_names[k]);
192 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
194 changed = snd_mask_refine(m, constrs_mask(constrs, k));
196 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
199 params->cmask |= 1 << k;
204 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
205 i = hw_param_interval(params, k);
206 if (snd_interval_empty(i))
208 if (!(params->rmask & (1 << k)))
211 printk("%s = ", snd_pcm_hw_param_names[k]);
216 i->openmin ? '(' : '[', i->min,
217 i->max, i->openmax ? ')' : ']');
220 changed = snd_interval_refine(i, constrs_interval(constrs, k));
225 printk("%c%u %u%c\n",
226 i->openmin ? '(' : '[', i->min,
227 i->max, i->openmax ? ')' : ']');
230 params->cmask |= 1 << k;
235 for (k = 0; k < constrs->rules_num; k++)
237 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
238 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
241 for (k = 0; k < constrs->rules_num; k++) {
242 struct snd_pcm_hw_rule *r = &constrs->rules[k];
245 if (r->cond && !(r->cond & params->flags))
247 for (d = 0; r->deps[d] >= 0; d++) {
248 if (vstamps[r->deps[d]] > rstamps[k]) {
256 printk("Rule %d [%p]: ", k, r->func);
258 printk("%s = ", snd_pcm_hw_param_names[r->var]);
259 if (hw_is_mask(r->var)) {
260 m = hw_param_mask(params, r->var);
261 printk("%x", *m->bits);
263 i = hw_param_interval(params, r->var);
268 i->openmin ? '(' : '[', i->min,
269 i->max, i->openmax ? ')' : ']');
273 changed = r->func(params, r);
277 if (hw_is_mask(r->var))
278 printk("%x", *m->bits);
284 i->openmin ? '(' : '[', i->min,
285 i->max, i->openmax ? ')' : ']');
291 if (changed && r->var >= 0) {
292 params->cmask |= (1 << r->var);
293 vstamps[r->var] = stamp;
301 if (!params->msbits) {
302 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
303 if (snd_interval_single(i))
304 params->msbits = snd_interval_value(i);
307 if (!params->rate_den) {
308 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
309 if (snd_interval_single(i)) {
310 params->rate_num = snd_interval_value(i);
311 params->rate_den = 1;
315 hw = &substream->runtime->hw;
317 params->info = hw->info;
318 if (!params->fifo_size)
319 params->fifo_size = hw->fifo_size;
324 EXPORT_SYMBOL(snd_pcm_hw_refine);
326 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
327 struct snd_pcm_hw_params __user * _params)
329 struct snd_pcm_hw_params *params;
332 params = kmalloc(sizeof(*params), GFP_KERNEL);
337 if (copy_from_user(params, _params, sizeof(*params))) {
341 err = snd_pcm_hw_refine(substream, params);
342 if (copy_to_user(_params, params, sizeof(*params))) {
351 static int period_to_usecs(struct snd_pcm_runtime *runtime)
356 return -1; /* invalid */
358 /* take 75% of period time as the deadline */
359 usecs = (750000 / runtime->rate) * runtime->period_size;
360 usecs += ((750000 % runtime->rate) * runtime->period_size) /
366 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
367 struct snd_pcm_hw_params *params)
369 struct snd_pcm_runtime *runtime;
372 snd_pcm_uframes_t frames;
374 snd_assert(substream != NULL, return -ENXIO);
375 runtime = substream->runtime;
376 snd_assert(runtime != NULL, return -ENXIO);
377 snd_pcm_stream_lock_irq(substream);
378 switch (runtime->status->state) {
379 case SNDRV_PCM_STATE_OPEN:
380 case SNDRV_PCM_STATE_SETUP:
381 case SNDRV_PCM_STATE_PREPARED:
384 snd_pcm_stream_unlock_irq(substream);
387 snd_pcm_stream_unlock_irq(substream);
388 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
389 if (!substream->oss.oss)
391 if (atomic_read(&substream->mmap_count))
395 err = snd_pcm_hw_refine(substream, params);
399 err = snd_pcm_hw_params_choose(substream, params);
403 if (substream->ops->hw_params != NULL) {
404 err = substream->ops->hw_params(substream, params);
409 runtime->access = params_access(params);
410 runtime->format = params_format(params);
411 runtime->subformat = params_subformat(params);
412 runtime->channels = params_channels(params);
413 runtime->rate = params_rate(params);
414 runtime->period_size = params_period_size(params);
415 runtime->periods = params_periods(params);
416 runtime->buffer_size = params_buffer_size(params);
417 runtime->tick_time = params_tick_time(params);
418 runtime->info = params->info;
419 runtime->rate_num = params->rate_num;
420 runtime->rate_den = params->rate_den;
422 bits = snd_pcm_format_physical_width(runtime->format);
423 runtime->sample_bits = bits;
424 bits *= runtime->channels;
425 runtime->frame_bits = bits;
427 while (bits % 8 != 0) {
431 runtime->byte_align = bits / 8;
432 runtime->min_align = frames;
434 /* Default sw params */
435 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
436 runtime->period_step = 1;
437 runtime->sleep_min = 0;
438 runtime->control->avail_min = runtime->period_size;
439 runtime->xfer_align = runtime->period_size;
440 runtime->start_threshold = 1;
441 runtime->stop_threshold = runtime->buffer_size;
442 runtime->silence_threshold = 0;
443 runtime->silence_size = 0;
444 runtime->boundary = runtime->buffer_size;
445 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
446 runtime->boundary *= 2;
448 snd_pcm_timer_resolution_change(substream);
449 runtime->status->state = SNDRV_PCM_STATE_SETUP;
451 remove_acceptable_latency(substream->latency_id);
452 if ((usecs = period_to_usecs(runtime)) >= 0)
453 set_acceptable_latency(substream->latency_id, usecs);
456 /* hardware might be unuseable from this time,
457 so we force application to retry to set
458 the correct hardware parameter settings */
459 runtime->status->state = SNDRV_PCM_STATE_OPEN;
460 if (substream->ops->hw_free != NULL)
461 substream->ops->hw_free(substream);
465 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
466 struct snd_pcm_hw_params __user * _params)
468 struct snd_pcm_hw_params *params;
471 params = kmalloc(sizeof(*params), GFP_KERNEL);
476 if (copy_from_user(params, _params, sizeof(*params))) {
480 err = snd_pcm_hw_params(substream, params);
481 if (copy_to_user(_params, params, sizeof(*params))) {
490 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
492 struct snd_pcm_runtime *runtime;
495 snd_assert(substream != NULL, return -ENXIO);
496 runtime = substream->runtime;
497 snd_assert(runtime != NULL, return -ENXIO);
498 snd_pcm_stream_lock_irq(substream);
499 switch (runtime->status->state) {
500 case SNDRV_PCM_STATE_SETUP:
501 case SNDRV_PCM_STATE_PREPARED:
504 snd_pcm_stream_unlock_irq(substream);
507 snd_pcm_stream_unlock_irq(substream);
508 if (atomic_read(&substream->mmap_count))
510 if (substream->ops->hw_free)
511 result = substream->ops->hw_free(substream);
512 runtime->status->state = SNDRV_PCM_STATE_OPEN;
513 remove_acceptable_latency(substream->latency_id);
517 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
518 struct snd_pcm_sw_params *params)
520 struct snd_pcm_runtime *runtime;
522 snd_assert(substream != NULL, return -ENXIO);
523 runtime = substream->runtime;
524 snd_assert(runtime != NULL, return -ENXIO);
525 snd_pcm_stream_lock_irq(substream);
526 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
527 snd_pcm_stream_unlock_irq(substream);
530 snd_pcm_stream_unlock_irq(substream);
532 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
534 if (params->avail_min == 0)
536 if (params->xfer_align == 0 ||
537 params->xfer_align % runtime->min_align != 0)
539 if (params->silence_size >= runtime->boundary) {
540 if (params->silence_threshold != 0)
543 if (params->silence_size > params->silence_threshold)
545 if (params->silence_threshold > runtime->buffer_size)
548 snd_pcm_stream_lock_irq(substream);
549 runtime->tstamp_mode = params->tstamp_mode;
550 runtime->sleep_min = params->sleep_min;
551 runtime->period_step = params->period_step;
552 runtime->control->avail_min = params->avail_min;
553 runtime->start_threshold = params->start_threshold;
554 runtime->stop_threshold = params->stop_threshold;
555 runtime->silence_threshold = params->silence_threshold;
556 runtime->silence_size = params->silence_size;
557 runtime->xfer_align = params->xfer_align;
558 params->boundary = runtime->boundary;
559 if (snd_pcm_running(substream)) {
560 if (runtime->sleep_min)
561 snd_pcm_tick_prepare(substream);
563 snd_pcm_tick_set(substream, 0);
564 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
565 runtime->silence_size > 0)
566 snd_pcm_playback_silence(substream, ULONG_MAX);
567 wake_up(&runtime->sleep);
569 snd_pcm_stream_unlock_irq(substream);
573 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
574 struct snd_pcm_sw_params __user * _params)
576 struct snd_pcm_sw_params params;
578 if (copy_from_user(¶ms, _params, sizeof(params)))
580 err = snd_pcm_sw_params(substream, ¶ms);
581 if (copy_to_user(_params, ¶ms, sizeof(params)))
586 int snd_pcm_status(struct snd_pcm_substream *substream,
587 struct snd_pcm_status *status)
589 struct snd_pcm_runtime *runtime = substream->runtime;
591 snd_pcm_stream_lock_irq(substream);
592 status->state = runtime->status->state;
593 status->suspended_state = runtime->status->suspended_state;
594 if (status->state == SNDRV_PCM_STATE_OPEN)
596 status->trigger_tstamp = runtime->trigger_tstamp;
597 if (snd_pcm_running(substream)) {
598 snd_pcm_update_hw_ptr(substream);
599 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
600 status->tstamp = runtime->status->tstamp;
602 getnstimeofday(&status->tstamp);
604 getnstimeofday(&status->tstamp);
605 status->appl_ptr = runtime->control->appl_ptr;
606 status->hw_ptr = runtime->status->hw_ptr;
607 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
608 status->avail = snd_pcm_playback_avail(runtime);
609 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
610 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
611 status->delay = runtime->buffer_size - status->avail;
615 status->avail = snd_pcm_capture_avail(runtime);
616 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
617 status->delay = status->avail;
621 status->avail_max = runtime->avail_max;
622 status->overrange = runtime->overrange;
623 runtime->avail_max = 0;
624 runtime->overrange = 0;
626 snd_pcm_stream_unlock_irq(substream);
630 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
631 struct snd_pcm_status __user * _status)
633 struct snd_pcm_status status;
634 struct snd_pcm_runtime *runtime;
637 snd_assert(substream != NULL, return -ENXIO);
638 runtime = substream->runtime;
639 memset(&status, 0, sizeof(status));
640 res = snd_pcm_status(substream, &status);
643 if (copy_to_user(_status, &status, sizeof(status)))
648 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
649 struct snd_pcm_channel_info * info)
651 struct snd_pcm_runtime *runtime;
652 unsigned int channel;
654 snd_assert(substream != NULL, return -ENXIO);
655 channel = info->channel;
656 runtime = substream->runtime;
657 snd_pcm_stream_lock_irq(substream);
658 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
659 snd_pcm_stream_unlock_irq(substream);
662 snd_pcm_stream_unlock_irq(substream);
663 if (channel >= runtime->channels)
665 memset(info, 0, sizeof(*info));
666 info->channel = channel;
667 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
670 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
671 struct snd_pcm_channel_info __user * _info)
673 struct snd_pcm_channel_info info;
676 if (copy_from_user(&info, _info, sizeof(info)))
678 res = snd_pcm_channel_info(substream, &info);
681 if (copy_to_user(_info, &info, sizeof(info)))
686 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
688 struct snd_pcm_runtime *runtime = substream->runtime;
689 if (runtime->trigger_master == NULL)
691 if (runtime->trigger_master == substream) {
692 getnstimeofday(&runtime->trigger_tstamp);
694 snd_pcm_trigger_tstamp(runtime->trigger_master);
695 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
697 runtime->trigger_master = NULL;
701 int (*pre_action)(struct snd_pcm_substream *substream, int state);
702 int (*do_action)(struct snd_pcm_substream *substream, int state);
703 void (*undo_action)(struct snd_pcm_substream *substream, int state);
704 void (*post_action)(struct snd_pcm_substream *substream, int state);
708 * this functions is core for handling of linked stream
709 * Note: the stream state might be changed also on failure
710 * Note2: call with calling stream lock + link lock
712 static int snd_pcm_action_group(struct action_ops *ops,
713 struct snd_pcm_substream *substream,
714 int state, int do_lock)
716 struct list_head *pos;
717 struct snd_pcm_substream *s = NULL;
718 struct snd_pcm_substream *s1;
721 snd_pcm_group_for_each(pos, substream) {
722 s = snd_pcm_group_substream_entry(pos);
723 if (do_lock && s != substream)
724 spin_lock(&s->self_group.lock);
725 res = ops->pre_action(s, state);
729 snd_pcm_group_for_each(pos, substream) {
730 s = snd_pcm_group_substream_entry(pos);
731 res = ops->do_action(s, state);
733 if (ops->undo_action) {
734 snd_pcm_group_for_each(pos, substream) {
735 s1 = snd_pcm_group_substream_entry(pos);
736 if (s1 == s) /* failed stream */
738 ops->undo_action(s1, state);
741 s = NULL; /* unlock all */
745 snd_pcm_group_for_each(pos, substream) {
746 s = snd_pcm_group_substream_entry(pos);
747 ops->post_action(s, state);
752 snd_pcm_group_for_each(pos, substream) {
753 s1 = snd_pcm_group_substream_entry(pos);
755 spin_unlock(&s1->self_group.lock);
756 if (s1 == s) /* end */
764 * Note: call with stream lock
766 static int snd_pcm_action_single(struct action_ops *ops,
767 struct snd_pcm_substream *substream,
772 res = ops->pre_action(substream, state);
775 res = ops->do_action(substream, state);
777 ops->post_action(substream, state);
778 else if (ops->undo_action)
779 ops->undo_action(substream, state);
784 * Note: call with stream lock
786 static int snd_pcm_action(struct action_ops *ops,
787 struct snd_pcm_substream *substream,
792 if (snd_pcm_stream_linked(substream)) {
793 if (!spin_trylock(&substream->group->lock)) {
794 spin_unlock(&substream->self_group.lock);
795 spin_lock(&substream->group->lock);
796 spin_lock(&substream->self_group.lock);
798 res = snd_pcm_action_group(ops, substream, state, 1);
799 spin_unlock(&substream->group->lock);
801 res = snd_pcm_action_single(ops, substream, state);
807 * Note: don't use any locks before
809 static int snd_pcm_action_lock_irq(struct action_ops *ops,
810 struct snd_pcm_substream *substream,
815 read_lock_irq(&snd_pcm_link_rwlock);
816 if (snd_pcm_stream_linked(substream)) {
817 spin_lock(&substream->group->lock);
818 spin_lock(&substream->self_group.lock);
819 res = snd_pcm_action_group(ops, substream, state, 1);
820 spin_unlock(&substream->self_group.lock);
821 spin_unlock(&substream->group->lock);
823 spin_lock(&substream->self_group.lock);
824 res = snd_pcm_action_single(ops, substream, state);
825 spin_unlock(&substream->self_group.lock);
827 read_unlock_irq(&snd_pcm_link_rwlock);
833 static int snd_pcm_action_nonatomic(struct action_ops *ops,
834 struct snd_pcm_substream *substream,
839 down_read(&snd_pcm_link_rwsem);
840 if (snd_pcm_stream_linked(substream))
841 res = snd_pcm_action_group(ops, substream, state, 0);
843 res = snd_pcm_action_single(ops, substream, state);
844 up_read(&snd_pcm_link_rwsem);
851 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
853 struct snd_pcm_runtime *runtime = substream->runtime;
854 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
856 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
857 !snd_pcm_playback_data(substream))
859 runtime->trigger_master = substream;
863 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
865 if (substream->runtime->trigger_master != substream)
867 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
870 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
872 if (substream->runtime->trigger_master == substream)
873 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
876 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
878 struct snd_pcm_runtime *runtime = substream->runtime;
879 snd_pcm_trigger_tstamp(substream);
880 runtime->status->state = state;
881 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
882 runtime->silence_size > 0)
883 snd_pcm_playback_silence(substream, ULONG_MAX);
884 if (runtime->sleep_min)
885 snd_pcm_tick_prepare(substream);
886 if (substream->timer)
887 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
888 &runtime->trigger_tstamp);
891 static struct action_ops snd_pcm_action_start = {
892 .pre_action = snd_pcm_pre_start,
893 .do_action = snd_pcm_do_start,
894 .undo_action = snd_pcm_undo_start,
895 .post_action = snd_pcm_post_start
900 * @substream: the PCM substream instance
902 * Start all linked streams.
904 int snd_pcm_start(struct snd_pcm_substream *substream)
906 return snd_pcm_action(&snd_pcm_action_start, substream,
907 SNDRV_PCM_STATE_RUNNING);
913 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
915 struct snd_pcm_runtime *runtime = substream->runtime;
916 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
918 runtime->trigger_master = substream;
922 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
924 if (substream->runtime->trigger_master == substream &&
925 snd_pcm_running(substream))
926 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
927 return 0; /* unconditonally stop all substreams */
930 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
932 struct snd_pcm_runtime *runtime = substream->runtime;
933 if (runtime->status->state != state) {
934 snd_pcm_trigger_tstamp(substream);
935 if (substream->timer)
936 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
937 &runtime->trigger_tstamp);
938 runtime->status->state = state;
939 snd_pcm_tick_set(substream, 0);
941 wake_up(&runtime->sleep);
944 static struct action_ops snd_pcm_action_stop = {
945 .pre_action = snd_pcm_pre_stop,
946 .do_action = snd_pcm_do_stop,
947 .post_action = snd_pcm_post_stop
952 * @substream: the PCM substream instance
953 * @state: PCM state after stopping the stream
955 * Try to stop all running streams in the substream group.
956 * The state of each stream is changed to the given value after that unconditionally.
958 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
960 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
963 EXPORT_SYMBOL(snd_pcm_stop);
967 * @substream: the PCM substream
969 * Stop the DMA only when the given stream is playback.
970 * The state is changed to SETUP.
971 * Unlike snd_pcm_stop(), this affects only the given stream.
973 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
975 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
976 SNDRV_PCM_STATE_SETUP);
982 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
984 struct snd_pcm_runtime *runtime = substream->runtime;
985 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
988 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
990 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
992 runtime->trigger_master = substream;
996 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
998 if (substream->runtime->trigger_master != substream)
1000 return substream->ops->trigger(substream,
1001 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1002 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1005 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1007 if (substream->runtime->trigger_master == substream)
1008 substream->ops->trigger(substream,
1009 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1010 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1013 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1015 struct snd_pcm_runtime *runtime = substream->runtime;
1016 snd_pcm_trigger_tstamp(substream);
1018 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1019 if (substream->timer)
1020 snd_timer_notify(substream->timer,
1021 SNDRV_TIMER_EVENT_MPAUSE,
1022 &runtime->trigger_tstamp);
1023 snd_pcm_tick_set(substream, 0);
1024 wake_up(&runtime->sleep);
1026 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1027 if (runtime->sleep_min)
1028 snd_pcm_tick_prepare(substream);
1029 if (substream->timer)
1030 snd_timer_notify(substream->timer,
1031 SNDRV_TIMER_EVENT_MCONTINUE,
1032 &runtime->trigger_tstamp);
1036 static struct action_ops snd_pcm_action_pause = {
1037 .pre_action = snd_pcm_pre_pause,
1038 .do_action = snd_pcm_do_pause,
1039 .undo_action = snd_pcm_undo_pause,
1040 .post_action = snd_pcm_post_pause
1044 * Push/release the pause for all linked streams.
1046 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1048 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1054 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1056 struct snd_pcm_runtime *runtime = substream->runtime;
1057 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1059 runtime->trigger_master = substream;
1063 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1065 struct snd_pcm_runtime *runtime = substream->runtime;
1066 if (runtime->trigger_master != substream)
1068 if (! snd_pcm_running(substream))
1070 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1071 return 0; /* suspend unconditionally */
1074 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1076 struct snd_pcm_runtime *runtime = substream->runtime;
1077 snd_pcm_trigger_tstamp(substream);
1078 if (substream->timer)
1079 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1080 &runtime->trigger_tstamp);
1081 runtime->status->suspended_state = runtime->status->state;
1082 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1083 snd_pcm_tick_set(substream, 0);
1084 wake_up(&runtime->sleep);
1087 static struct action_ops snd_pcm_action_suspend = {
1088 .pre_action = snd_pcm_pre_suspend,
1089 .do_action = snd_pcm_do_suspend,
1090 .post_action = snd_pcm_post_suspend
1095 * @substream: the PCM substream
1097 * Trigger SUSPEND to all linked streams.
1098 * After this call, all streams are changed to SUSPENDED state.
1100 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1103 unsigned long flags;
1108 snd_pcm_stream_lock_irqsave(substream, flags);
1109 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1110 snd_pcm_stream_unlock_irqrestore(substream, flags);
1114 EXPORT_SYMBOL(snd_pcm_suspend);
1117 * snd_pcm_suspend_all
1118 * @pcm: the PCM instance
1120 * Trigger SUSPEND to all substreams in the given pcm.
1121 * After this call, all streams are changed to SUSPENDED state.
1123 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1125 struct snd_pcm_substream *substream;
1126 int stream, err = 0;
1131 for (stream = 0; stream < 2; stream++) {
1132 for (substream = pcm->streams[stream].substream;
1133 substream; substream = substream->next) {
1134 /* FIXME: the open/close code should lock this as well */
1135 if (substream->runtime == NULL)
1137 err = snd_pcm_suspend(substream);
1138 if (err < 0 && err != -EBUSY)
1145 EXPORT_SYMBOL(snd_pcm_suspend_all);
1149 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1151 struct snd_pcm_runtime *runtime = substream->runtime;
1152 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1154 runtime->trigger_master = substream;
1158 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1160 struct snd_pcm_runtime *runtime = substream->runtime;
1161 if (runtime->trigger_master != substream)
1163 /* DMA not running previously? */
1164 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1165 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1166 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1168 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1171 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1173 if (substream->runtime->trigger_master == substream &&
1174 snd_pcm_running(substream))
1175 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1178 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1180 struct snd_pcm_runtime *runtime = substream->runtime;
1181 snd_pcm_trigger_tstamp(substream);
1182 if (substream->timer)
1183 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1184 &runtime->trigger_tstamp);
1185 runtime->status->state = runtime->status->suspended_state;
1186 if (runtime->sleep_min)
1187 snd_pcm_tick_prepare(substream);
1190 static struct action_ops snd_pcm_action_resume = {
1191 .pre_action = snd_pcm_pre_resume,
1192 .do_action = snd_pcm_do_resume,
1193 .undo_action = snd_pcm_undo_resume,
1194 .post_action = snd_pcm_post_resume
1197 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1199 struct snd_card *card = substream->pcm->card;
1202 snd_power_lock(card);
1203 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1204 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1205 snd_power_unlock(card);
1211 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1216 #endif /* CONFIG_PM */
1221 * Change the RUNNING stream(s) to XRUN state.
1223 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1225 struct snd_card *card = substream->pcm->card;
1226 struct snd_pcm_runtime *runtime = substream->runtime;
1229 snd_power_lock(card);
1230 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1231 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1236 snd_pcm_stream_lock_irq(substream);
1237 switch (runtime->status->state) {
1238 case SNDRV_PCM_STATE_XRUN:
1239 result = 0; /* already there */
1241 case SNDRV_PCM_STATE_RUNNING:
1242 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1247 snd_pcm_stream_unlock_irq(substream);
1249 snd_power_unlock(card);
1256 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1258 struct snd_pcm_runtime *runtime = substream->runtime;
1259 switch (runtime->status->state) {
1260 case SNDRV_PCM_STATE_RUNNING:
1261 case SNDRV_PCM_STATE_PREPARED:
1262 case SNDRV_PCM_STATE_PAUSED:
1263 case SNDRV_PCM_STATE_SUSPENDED:
1270 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1272 struct snd_pcm_runtime *runtime = substream->runtime;
1273 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1276 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1277 runtime->hw_ptr_base = 0;
1278 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1279 runtime->status->hw_ptr % runtime->period_size;
1280 runtime->silence_start = runtime->status->hw_ptr;
1281 runtime->silence_filled = 0;
1285 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1287 struct snd_pcm_runtime *runtime = substream->runtime;
1288 runtime->control->appl_ptr = runtime->status->hw_ptr;
1289 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1290 runtime->silence_size > 0)
1291 snd_pcm_playback_silence(substream, ULONG_MAX);
1294 static struct action_ops snd_pcm_action_reset = {
1295 .pre_action = snd_pcm_pre_reset,
1296 .do_action = snd_pcm_do_reset,
1297 .post_action = snd_pcm_post_reset
1300 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1302 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1308 /* we use the second argument for updating f_flags */
1309 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1312 struct snd_pcm_runtime *runtime = substream->runtime;
1313 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1315 if (snd_pcm_running(substream))
1317 substream->f_flags = f_flags;
1321 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1324 err = substream->ops->prepare(substream);
1327 return snd_pcm_do_reset(substream, 0);
1330 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1332 struct snd_pcm_runtime *runtime = substream->runtime;
1333 runtime->control->appl_ptr = runtime->status->hw_ptr;
1334 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1337 static struct action_ops snd_pcm_action_prepare = {
1338 .pre_action = snd_pcm_pre_prepare,
1339 .do_action = snd_pcm_do_prepare,
1340 .post_action = snd_pcm_post_prepare
1345 * @substream: the PCM substream instance
1346 * @file: file to refer f_flags
1348 * Prepare the PCM substream to be triggerable.
1350 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1354 struct snd_card *card = substream->pcm->card;
1358 f_flags = file->f_flags;
1360 f_flags = substream->f_flags;
1362 snd_power_lock(card);
1363 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1364 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1365 substream, f_flags);
1366 snd_power_unlock(card);
1374 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1376 if (substream->f_flags & O_NONBLOCK)
1378 substream->runtime->trigger_master = substream;
1382 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1384 struct snd_pcm_runtime *runtime = substream->runtime;
1385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1386 switch (runtime->status->state) {
1387 case SNDRV_PCM_STATE_PREPARED:
1388 /* start playback stream if possible */
1389 if (! snd_pcm_playback_empty(substream)) {
1390 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1391 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1394 case SNDRV_PCM_STATE_RUNNING:
1395 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1401 /* stop running stream */
1402 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1403 int state = snd_pcm_capture_avail(runtime) > 0 ?
1404 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1405 snd_pcm_do_stop(substream, state);
1406 snd_pcm_post_stop(substream, state);
1412 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1416 static struct action_ops snd_pcm_action_drain_init = {
1417 .pre_action = snd_pcm_pre_drain_init,
1418 .do_action = snd_pcm_do_drain_init,
1419 .post_action = snd_pcm_post_drain_init
1423 struct snd_pcm_substream *substream;
1425 snd_pcm_uframes_t stop_threshold;
1428 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1431 * Drain the stream(s).
1432 * When the substream is linked, sync until the draining of all playback streams
1434 * After this call, all streams are supposed to be either SETUP or DRAINING
1435 * (capture only) state.
1437 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1439 struct snd_card *card;
1440 struct snd_pcm_runtime *runtime;
1441 struct list_head *pos;
1444 struct drain_rec *drec, drec_tmp, *d;
1446 snd_assert(substream != NULL, return -ENXIO);
1447 card = substream->pcm->card;
1448 runtime = substream->runtime;
1450 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1453 snd_power_lock(card);
1454 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1455 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1457 snd_power_unlock(card);
1462 /* allocate temporary record for drain sync */
1463 down_read(&snd_pcm_link_rwsem);
1464 if (snd_pcm_stream_linked(substream)) {
1465 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1467 up_read(&snd_pcm_link_rwsem);
1468 snd_power_unlock(card);
1474 /* count only playback streams */
1476 snd_pcm_group_for_each(pos, substream) {
1477 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
1478 runtime = s->runtime;
1479 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1480 d = &drec[num_drecs++];
1482 init_waitqueue_entry(&d->wait, current);
1483 add_wait_queue(&runtime->sleep, &d->wait);
1484 /* stop_threshold fixup to avoid endless loop when
1485 * stop_threshold > buffer_size
1487 d->stop_threshold = runtime->stop_threshold;
1488 if (runtime->stop_threshold > runtime->buffer_size)
1489 runtime->stop_threshold = runtime->buffer_size;
1492 up_read(&snd_pcm_link_rwsem);
1494 snd_pcm_stream_lock_irq(substream);
1496 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1497 snd_pcm_pause(substream, 0);
1499 /* pre-start/stop - all running streams are changed to DRAINING state */
1500 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1502 snd_pcm_stream_unlock_irq(substream);
1508 if (signal_pending(current)) {
1509 result = -ERESTARTSYS;
1513 for (i = 0; i < num_drecs; i++) {
1514 runtime = drec[i].substream->runtime;
1515 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1519 break; /* yes, all drained */
1521 set_current_state(TASK_INTERRUPTIBLE);
1522 snd_pcm_stream_unlock_irq(substream);
1523 snd_power_unlock(card);
1524 tout = schedule_timeout(10 * HZ);
1525 snd_power_lock(card);
1526 snd_pcm_stream_lock_irq(substream);
1528 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1531 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1532 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1539 snd_pcm_stream_unlock_irq(substream);
1542 for (i = 0; i < num_drecs; i++) {
1544 runtime = d->substream->runtime;
1545 remove_wait_queue(&runtime->sleep, &d->wait);
1546 runtime->stop_threshold = d->stop_threshold;
1549 if (drec != &drec_tmp)
1551 snd_power_unlock(card);
1559 * Immediately put all linked substreams into SETUP state.
1561 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1563 struct snd_pcm_runtime *runtime;
1564 struct snd_card *card;
1567 snd_assert(substream != NULL, return -ENXIO);
1568 runtime = substream->runtime;
1569 card = substream->pcm->card;
1571 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1574 snd_power_lock(card);
1575 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1576 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1581 snd_pcm_stream_lock_irq(substream);
1583 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1584 snd_pcm_pause(substream, 0);
1586 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1587 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1588 snd_pcm_stream_unlock_irq(substream);
1590 snd_power_unlock(card);
1595 /* WARNING: Don't forget to fput back the file */
1596 static struct file *snd_pcm_file_fd(int fd)
1599 struct inode *inode;
1605 inode = file->f_dentry->d_inode;
1606 if (!S_ISCHR(inode->i_mode) ||
1607 imajor(inode) != snd_major) {
1611 minor = iminor(inode);
1612 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1613 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1623 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1627 struct snd_pcm_file *pcm_file;
1628 struct snd_pcm_substream *substream1;
1630 file = snd_pcm_file_fd(fd);
1633 pcm_file = file->private_data;
1634 substream1 = pcm_file->substream;
1635 down_write(&snd_pcm_link_rwsem);
1636 write_lock_irq(&snd_pcm_link_rwlock);
1637 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1638 substream->runtime->status->state != substream1->runtime->status->state) {
1642 if (snd_pcm_stream_linked(substream1)) {
1646 if (!snd_pcm_stream_linked(substream)) {
1647 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1648 if (substream->group == NULL) {
1652 spin_lock_init(&substream->group->lock);
1653 INIT_LIST_HEAD(&substream->group->substreams);
1654 list_add_tail(&substream->link_list, &substream->group->substreams);
1655 substream->group->count = 1;
1657 list_add_tail(&substream1->link_list, &substream->group->substreams);
1658 substream->group->count++;
1659 substream1->group = substream->group;
1661 write_unlock_irq(&snd_pcm_link_rwlock);
1662 up_write(&snd_pcm_link_rwsem);
1667 static void relink_to_local(struct snd_pcm_substream *substream)
1669 substream->group = &substream->self_group;
1670 INIT_LIST_HEAD(&substream->self_group.substreams);
1671 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1674 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1676 struct list_head *pos;
1679 down_write(&snd_pcm_link_rwsem);
1680 write_lock_irq(&snd_pcm_link_rwlock);
1681 if (!snd_pcm_stream_linked(substream)) {
1685 list_del(&substream->link_list);
1686 substream->group->count--;
1687 if (substream->group->count == 1) { /* detach the last stream, too */
1688 snd_pcm_group_for_each(pos, substream) {
1689 relink_to_local(snd_pcm_group_substream_entry(pos));
1692 kfree(substream->group);
1694 relink_to_local(substream);
1696 write_unlock_irq(&snd_pcm_link_rwlock);
1697 up_write(&snd_pcm_link_rwsem);
1704 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1705 struct snd_pcm_hw_rule *rule)
1707 struct snd_interval t;
1708 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1709 hw_param_interval_c(params, rule->deps[1]), &t);
1710 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1713 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1714 struct snd_pcm_hw_rule *rule)
1716 struct snd_interval t;
1717 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1718 hw_param_interval_c(params, rule->deps[1]), &t);
1719 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1722 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1723 struct snd_pcm_hw_rule *rule)
1725 struct snd_interval t;
1726 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1727 hw_param_interval_c(params, rule->deps[1]),
1728 (unsigned long) rule->private, &t);
1729 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1732 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1733 struct snd_pcm_hw_rule *rule)
1735 struct snd_interval t;
1736 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1737 (unsigned long) rule->private,
1738 hw_param_interval_c(params, rule->deps[1]), &t);
1739 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1742 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1743 struct snd_pcm_hw_rule *rule)
1746 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1748 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1750 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1752 if (! snd_mask_test(mask, k))
1754 bits = snd_pcm_format_physical_width(k);
1756 continue; /* ignore invalid formats */
1757 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1758 snd_mask_reset(&m, k);
1760 return snd_mask_refine(mask, &m);
1763 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1764 struct snd_pcm_hw_rule *rule)
1766 struct snd_interval t;
1772 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1774 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1776 bits = snd_pcm_format_physical_width(k);
1778 continue; /* ignore invalid formats */
1779 if (t.min > (unsigned)bits)
1781 if (t.max < (unsigned)bits)
1785 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1788 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1789 #error "Change this table"
1792 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1793 48000, 64000, 88200, 96000, 176400, 192000 };
1795 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1796 struct snd_pcm_hw_rule *rule)
1798 struct snd_pcm_hardware *hw = rule->private;
1799 return snd_interval_list(hw_param_interval(params, rule->var),
1800 ARRAY_SIZE(rates), rates, hw->rates);
1803 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1804 struct snd_pcm_hw_rule *rule)
1806 struct snd_interval t;
1807 struct snd_pcm_substream *substream = rule->private;
1809 t.max = substream->buffer_bytes_max;
1813 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1816 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1818 struct snd_pcm_runtime *runtime = substream->runtime;
1819 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1822 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1823 snd_mask_any(constrs_mask(constrs, k));
1826 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1827 snd_interval_any(constrs_interval(constrs, k));
1830 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1831 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1832 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1833 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1834 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1837 snd_pcm_hw_rule_format, NULL,
1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1842 snd_pcm_hw_rule_sample_bits, NULL,
1843 SNDRV_PCM_HW_PARAM_FORMAT,
1844 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1848 snd_pcm_hw_rule_div, NULL,
1849 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1853 snd_pcm_hw_rule_mul, NULL,
1854 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1858 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1859 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1863 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1864 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1868 snd_pcm_hw_rule_div, NULL,
1869 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1873 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1874 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1878 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1879 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1883 snd_pcm_hw_rule_div, NULL,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1887 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1888 snd_pcm_hw_rule_div, NULL,
1889 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1892 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1893 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1894 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1897 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1898 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1899 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1902 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1903 snd_pcm_hw_rule_mul, NULL,
1904 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1907 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1908 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1909 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1912 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1913 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1914 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1917 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1918 snd_pcm_hw_rule_muldivk, (void*) 8,
1919 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1922 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1923 snd_pcm_hw_rule_muldivk, (void*) 8,
1924 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1927 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1928 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1929 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1932 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1933 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1934 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1940 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1942 struct snd_pcm_runtime *runtime = substream->runtime;
1943 struct snd_pcm_hardware *hw = &runtime->hw;
1945 unsigned int mask = 0;
1947 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1948 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1949 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1950 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1951 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1952 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1953 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1954 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1955 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1956 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1957 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1959 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1960 snd_assert(err >= 0, return -EINVAL);
1962 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1963 snd_assert(err >= 0, return -EINVAL);
1965 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1966 snd_assert(err >= 0, return -EINVAL);
1968 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1969 hw->channels_min, hw->channels_max);
1970 snd_assert(err >= 0, return -EINVAL);
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1973 hw->rate_min, hw->rate_max);
1974 snd_assert(err >= 0, return -EINVAL);
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1977 hw->period_bytes_min, hw->period_bytes_max);
1978 snd_assert(err >= 0, return -EINVAL);
1980 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1981 hw->periods_min, hw->periods_max);
1982 snd_assert(err >= 0, return -EINVAL);
1984 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1985 hw->period_bytes_min, hw->buffer_bytes_max);
1986 snd_assert(err >= 0, return -EINVAL);
1988 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1989 snd_pcm_hw_rule_buffer_bytes_max, substream,
1990 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1995 if (runtime->dma_bytes) {
1996 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1997 snd_assert(err >= 0, return -EINVAL);
2000 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2001 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2002 snd_pcm_hw_rule_rate, hw,
2003 SNDRV_PCM_HW_PARAM_RATE, -1);
2008 /* FIXME: this belong to lowlevel */
2009 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
2010 1000000 / HZ, 1000000 / HZ);
2011 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2016 static void pcm_release_private(struct snd_pcm_substream *substream)
2018 snd_pcm_unlink(substream);
2021 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2023 substream->ref_count--;
2024 if (substream->ref_count > 0)
2027 snd_pcm_drop(substream);
2028 if (substream->hw_opened) {
2029 if (substream->ops->hw_free != NULL)
2030 substream->ops->hw_free(substream);
2031 substream->ops->close(substream);
2032 substream->hw_opened = 0;
2034 if (substream->pcm_release) {
2035 substream->pcm_release(substream);
2036 substream->pcm_release = NULL;
2038 snd_pcm_detach_substream(substream);
2041 EXPORT_SYMBOL(snd_pcm_release_substream);
2043 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2045 struct snd_pcm_substream **rsubstream)
2047 struct snd_pcm_substream *substream;
2050 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2053 if (substream->ref_count > 1) {
2054 *rsubstream = substream;
2058 err = snd_pcm_hw_constraints_init(substream);
2060 snd_printd("snd_pcm_hw_constraints_init failed\n");
2064 if ((err = substream->ops->open(substream)) < 0)
2067 substream->hw_opened = 1;
2069 err = snd_pcm_hw_constraints_complete(substream);
2071 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2075 *rsubstream = substream;
2079 snd_pcm_release_substream(substream);
2083 EXPORT_SYMBOL(snd_pcm_open_substream);
2085 static int snd_pcm_open_file(struct file *file,
2086 struct snd_pcm *pcm,
2088 struct snd_pcm_file **rpcm_file)
2090 struct snd_pcm_file *pcm_file;
2091 struct snd_pcm_substream *substream;
2092 struct snd_pcm_str *str;
2095 snd_assert(rpcm_file != NULL, return -EINVAL);
2098 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2102 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2103 if (pcm_file == NULL) {
2104 snd_pcm_release_substream(substream);
2107 pcm_file->substream = substream;
2108 if (substream->ref_count == 1) {
2109 str = substream->pstr;
2110 substream->file = pcm_file;
2111 substream->pcm_release = pcm_release_private;
2113 file->private_data = pcm_file;
2114 *rpcm_file = pcm_file;
2118 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2120 struct snd_pcm *pcm;
2122 pcm = snd_lookup_minor_data(iminor(inode),
2123 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2124 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2127 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2129 struct snd_pcm *pcm;
2131 pcm = snd_lookup_minor_data(iminor(inode),
2132 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2133 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2136 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2139 struct snd_pcm_file *pcm_file;
2146 err = snd_card_file_add(pcm->card, file);
2149 if (!try_module_get(pcm->card->module)) {
2153 init_waitqueue_entry(&wait, current);
2154 add_wait_queue(&pcm->open_wait, &wait);
2155 mutex_lock(&pcm->open_mutex);
2157 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2160 if (err == -EAGAIN) {
2161 if (file->f_flags & O_NONBLOCK) {
2167 set_current_state(TASK_INTERRUPTIBLE);
2168 mutex_unlock(&pcm->open_mutex);
2170 mutex_lock(&pcm->open_mutex);
2171 if (signal_pending(current)) {
2176 remove_wait_queue(&pcm->open_wait, &wait);
2177 mutex_unlock(&pcm->open_mutex);
2183 module_put(pcm->card->module);
2185 snd_card_file_remove(pcm->card, file);
2190 static int snd_pcm_release(struct inode *inode, struct file *file)
2192 struct snd_pcm *pcm;
2193 struct snd_pcm_substream *substream;
2194 struct snd_pcm_file *pcm_file;
2196 pcm_file = file->private_data;
2197 substream = pcm_file->substream;
2198 snd_assert(substream != NULL, return -ENXIO);
2199 pcm = substream->pcm;
2200 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2201 mutex_lock(&pcm->open_mutex);
2202 snd_pcm_release_substream(substream);
2204 mutex_unlock(&pcm->open_mutex);
2205 wake_up(&pcm->open_wait);
2206 module_put(pcm->card->module);
2207 snd_card_file_remove(pcm->card, file);
2211 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2212 snd_pcm_uframes_t frames)
2214 struct snd_pcm_runtime *runtime = substream->runtime;
2215 snd_pcm_sframes_t appl_ptr;
2216 snd_pcm_sframes_t ret;
2217 snd_pcm_sframes_t hw_avail;
2222 snd_pcm_stream_lock_irq(substream);
2223 switch (runtime->status->state) {
2224 case SNDRV_PCM_STATE_PREPARED:
2226 case SNDRV_PCM_STATE_DRAINING:
2227 case SNDRV_PCM_STATE_RUNNING:
2228 if (snd_pcm_update_hw_ptr(substream) >= 0)
2231 case SNDRV_PCM_STATE_XRUN:
2239 hw_avail = snd_pcm_playback_hw_avail(runtime);
2240 if (hw_avail <= 0) {
2244 if (frames > (snd_pcm_uframes_t)hw_avail)
2247 frames -= frames % runtime->xfer_align;
2248 appl_ptr = runtime->control->appl_ptr - frames;
2250 appl_ptr += runtime->boundary;
2251 runtime->control->appl_ptr = appl_ptr;
2252 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2254 snd_pcm_tick_prepare(substream);
2257 snd_pcm_stream_unlock_irq(substream);
2261 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2262 snd_pcm_uframes_t frames)
2264 struct snd_pcm_runtime *runtime = substream->runtime;
2265 snd_pcm_sframes_t appl_ptr;
2266 snd_pcm_sframes_t ret;
2267 snd_pcm_sframes_t hw_avail;
2272 snd_pcm_stream_lock_irq(substream);
2273 switch (runtime->status->state) {
2274 case SNDRV_PCM_STATE_PREPARED:
2275 case SNDRV_PCM_STATE_DRAINING:
2277 case SNDRV_PCM_STATE_RUNNING:
2278 if (snd_pcm_update_hw_ptr(substream) >= 0)
2281 case SNDRV_PCM_STATE_XRUN:
2289 hw_avail = snd_pcm_capture_hw_avail(runtime);
2290 if (hw_avail <= 0) {
2294 if (frames > (snd_pcm_uframes_t)hw_avail)
2297 frames -= frames % runtime->xfer_align;
2298 appl_ptr = runtime->control->appl_ptr - frames;
2300 appl_ptr += runtime->boundary;
2301 runtime->control->appl_ptr = appl_ptr;
2302 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2304 snd_pcm_tick_prepare(substream);
2307 snd_pcm_stream_unlock_irq(substream);
2311 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2312 snd_pcm_uframes_t frames)
2314 struct snd_pcm_runtime *runtime = substream->runtime;
2315 snd_pcm_sframes_t appl_ptr;
2316 snd_pcm_sframes_t ret;
2317 snd_pcm_sframes_t avail;
2322 snd_pcm_stream_lock_irq(substream);
2323 switch (runtime->status->state) {
2324 case SNDRV_PCM_STATE_PREPARED:
2325 case SNDRV_PCM_STATE_PAUSED:
2327 case SNDRV_PCM_STATE_DRAINING:
2328 case SNDRV_PCM_STATE_RUNNING:
2329 if (snd_pcm_update_hw_ptr(substream) >= 0)
2332 case SNDRV_PCM_STATE_XRUN:
2340 avail = snd_pcm_playback_avail(runtime);
2345 if (frames > (snd_pcm_uframes_t)avail)
2348 frames -= frames % runtime->xfer_align;
2349 appl_ptr = runtime->control->appl_ptr + frames;
2350 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2351 appl_ptr -= runtime->boundary;
2352 runtime->control->appl_ptr = appl_ptr;
2353 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2355 snd_pcm_tick_prepare(substream);
2358 snd_pcm_stream_unlock_irq(substream);
2362 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2363 snd_pcm_uframes_t frames)
2365 struct snd_pcm_runtime *runtime = substream->runtime;
2366 snd_pcm_sframes_t appl_ptr;
2367 snd_pcm_sframes_t ret;
2368 snd_pcm_sframes_t avail;
2373 snd_pcm_stream_lock_irq(substream);
2374 switch (runtime->status->state) {
2375 case SNDRV_PCM_STATE_PREPARED:
2376 case SNDRV_PCM_STATE_DRAINING:
2377 case SNDRV_PCM_STATE_PAUSED:
2379 case SNDRV_PCM_STATE_RUNNING:
2380 if (snd_pcm_update_hw_ptr(substream) >= 0)
2383 case SNDRV_PCM_STATE_XRUN:
2391 avail = snd_pcm_capture_avail(runtime);
2396 if (frames > (snd_pcm_uframes_t)avail)
2399 frames -= frames % runtime->xfer_align;
2400 appl_ptr = runtime->control->appl_ptr + frames;
2401 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2402 appl_ptr -= runtime->boundary;
2403 runtime->control->appl_ptr = appl_ptr;
2404 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2406 snd_pcm_tick_prepare(substream);
2409 snd_pcm_stream_unlock_irq(substream);
2413 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2415 struct snd_pcm_runtime *runtime = substream->runtime;
2418 snd_pcm_stream_lock_irq(substream);
2419 switch (runtime->status->state) {
2420 case SNDRV_PCM_STATE_DRAINING:
2421 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2423 case SNDRV_PCM_STATE_RUNNING:
2424 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2427 case SNDRV_PCM_STATE_PREPARED:
2428 case SNDRV_PCM_STATE_SUSPENDED:
2431 case SNDRV_PCM_STATE_XRUN:
2439 snd_pcm_stream_unlock_irq(substream);
2443 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2444 snd_pcm_sframes_t __user *res)
2446 struct snd_pcm_runtime *runtime = substream->runtime;
2448 snd_pcm_sframes_t n = 0;
2450 snd_pcm_stream_lock_irq(substream);
2451 switch (runtime->status->state) {
2452 case SNDRV_PCM_STATE_DRAINING:
2453 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2455 case SNDRV_PCM_STATE_RUNNING:
2456 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2459 case SNDRV_PCM_STATE_PREPARED:
2460 case SNDRV_PCM_STATE_SUSPENDED:
2462 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2463 n = snd_pcm_playback_hw_avail(runtime);
2465 n = snd_pcm_capture_avail(runtime);
2467 case SNDRV_PCM_STATE_XRUN:
2475 snd_pcm_stream_unlock_irq(substream);
2477 if (put_user(n, res))
2482 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2483 struct snd_pcm_sync_ptr __user *_sync_ptr)
2485 struct snd_pcm_runtime *runtime = substream->runtime;
2486 struct snd_pcm_sync_ptr sync_ptr;
2487 volatile struct snd_pcm_mmap_status *status;
2488 volatile struct snd_pcm_mmap_control *control;
2491 memset(&sync_ptr, 0, sizeof(sync_ptr));
2492 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2494 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2496 status = runtime->status;
2497 control = runtime->control;
2498 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2499 err = snd_pcm_hwsync(substream);
2503 snd_pcm_stream_lock_irq(substream);
2504 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2505 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2507 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2508 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2509 control->avail_min = sync_ptr.c.control.avail_min;
2511 sync_ptr.c.control.avail_min = control->avail_min;
2512 sync_ptr.s.status.state = status->state;
2513 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2514 sync_ptr.s.status.tstamp = status->tstamp;
2515 sync_ptr.s.status.suspended_state = status->suspended_state;
2516 snd_pcm_stream_unlock_irq(substream);
2517 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2522 static int snd_pcm_common_ioctl1(struct file *file,
2523 struct snd_pcm_substream *substream,
2524 unsigned int cmd, void __user *arg)
2526 snd_assert(substream != NULL, return -ENXIO);
2529 case SNDRV_PCM_IOCTL_PVERSION:
2530 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2531 case SNDRV_PCM_IOCTL_INFO:
2532 return snd_pcm_info_user(substream, arg);
2533 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2535 case SNDRV_PCM_IOCTL_HW_REFINE:
2536 return snd_pcm_hw_refine_user(substream, arg);
2537 case SNDRV_PCM_IOCTL_HW_PARAMS:
2538 return snd_pcm_hw_params_user(substream, arg);
2539 case SNDRV_PCM_IOCTL_HW_FREE:
2540 return snd_pcm_hw_free(substream);
2541 case SNDRV_PCM_IOCTL_SW_PARAMS:
2542 return snd_pcm_sw_params_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_STATUS:
2544 return snd_pcm_status_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2546 return snd_pcm_channel_info_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_PREPARE:
2548 return snd_pcm_prepare(substream, file);
2549 case SNDRV_PCM_IOCTL_RESET:
2550 return snd_pcm_reset(substream);
2551 case SNDRV_PCM_IOCTL_START:
2552 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2553 case SNDRV_PCM_IOCTL_LINK:
2554 return snd_pcm_link(substream, (int)(unsigned long) arg);
2555 case SNDRV_PCM_IOCTL_UNLINK:
2556 return snd_pcm_unlink(substream);
2557 case SNDRV_PCM_IOCTL_RESUME:
2558 return snd_pcm_resume(substream);
2559 case SNDRV_PCM_IOCTL_XRUN:
2560 return snd_pcm_xrun(substream);
2561 case SNDRV_PCM_IOCTL_HWSYNC:
2562 return snd_pcm_hwsync(substream);
2563 case SNDRV_PCM_IOCTL_DELAY:
2564 return snd_pcm_delay(substream, arg);
2565 case SNDRV_PCM_IOCTL_SYNC_PTR:
2566 return snd_pcm_sync_ptr(substream, arg);
2567 #ifdef CONFIG_SND_SUPPORT_OLD_API
2568 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2569 return snd_pcm_hw_refine_old_user(substream, arg);
2570 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2571 return snd_pcm_hw_params_old_user(substream, arg);
2573 case SNDRV_PCM_IOCTL_DRAIN:
2574 return snd_pcm_drain(substream);
2575 case SNDRV_PCM_IOCTL_DROP:
2576 return snd_pcm_drop(substream);
2577 case SNDRV_PCM_IOCTL_PAUSE:
2580 snd_pcm_stream_lock_irq(substream);
2581 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2582 snd_pcm_stream_unlock_irq(substream);
2586 snd_printd("unknown ioctl = 0x%x\n", cmd);
2590 static int snd_pcm_playback_ioctl1(struct file *file,
2591 struct snd_pcm_substream *substream,
2592 unsigned int cmd, void __user *arg)
2594 snd_assert(substream != NULL, return -ENXIO);
2595 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2597 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2599 struct snd_xferi xferi;
2600 struct snd_xferi __user *_xferi = arg;
2601 struct snd_pcm_runtime *runtime = substream->runtime;
2602 snd_pcm_sframes_t result;
2603 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2605 if (put_user(0, &_xferi->result))
2607 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2609 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2610 __put_user(result, &_xferi->result);
2611 return result < 0 ? result : 0;
2613 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2615 struct snd_xfern xfern;
2616 struct snd_xfern __user *_xfern = arg;
2617 struct snd_pcm_runtime *runtime = substream->runtime;
2619 snd_pcm_sframes_t result;
2620 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2622 if (runtime->channels > 128)
2624 if (put_user(0, &_xfern->result))
2626 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2628 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2631 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2635 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2637 __put_user(result, &_xfern->result);
2638 return result < 0 ? result : 0;
2640 case SNDRV_PCM_IOCTL_REWIND:
2642 snd_pcm_uframes_t frames;
2643 snd_pcm_uframes_t __user *_frames = arg;
2644 snd_pcm_sframes_t result;
2645 if (get_user(frames, _frames))
2647 if (put_user(0, _frames))
2649 result = snd_pcm_playback_rewind(substream, frames);
2650 __put_user(result, _frames);
2651 return result < 0 ? result : 0;
2653 case SNDRV_PCM_IOCTL_FORWARD:
2655 snd_pcm_uframes_t frames;
2656 snd_pcm_uframes_t __user *_frames = arg;
2657 snd_pcm_sframes_t result;
2658 if (get_user(frames, _frames))
2660 if (put_user(0, _frames))
2662 result = snd_pcm_playback_forward(substream, frames);
2663 __put_user(result, _frames);
2664 return result < 0 ? result : 0;
2667 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2670 static int snd_pcm_capture_ioctl1(struct file *file,
2671 struct snd_pcm_substream *substream,
2672 unsigned int cmd, void __user *arg)
2674 snd_assert(substream != NULL, return -ENXIO);
2675 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2677 case SNDRV_PCM_IOCTL_READI_FRAMES:
2679 struct snd_xferi xferi;
2680 struct snd_xferi __user *_xferi = arg;
2681 struct snd_pcm_runtime *runtime = substream->runtime;
2682 snd_pcm_sframes_t result;
2683 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2685 if (put_user(0, &_xferi->result))
2687 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2689 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2690 __put_user(result, &_xferi->result);
2691 return result < 0 ? result : 0;
2693 case SNDRV_PCM_IOCTL_READN_FRAMES:
2695 struct snd_xfern xfern;
2696 struct snd_xfern __user *_xfern = arg;
2697 struct snd_pcm_runtime *runtime = substream->runtime;
2699 snd_pcm_sframes_t result;
2700 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2702 if (runtime->channels > 128)
2704 if (put_user(0, &_xfern->result))
2706 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2708 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2711 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2715 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2717 __put_user(result, &_xfern->result);
2718 return result < 0 ? result : 0;
2720 case SNDRV_PCM_IOCTL_REWIND:
2722 snd_pcm_uframes_t frames;
2723 snd_pcm_uframes_t __user *_frames = arg;
2724 snd_pcm_sframes_t result;
2725 if (get_user(frames, _frames))
2727 if (put_user(0, _frames))
2729 result = snd_pcm_capture_rewind(substream, frames);
2730 __put_user(result, _frames);
2731 return result < 0 ? result : 0;
2733 case SNDRV_PCM_IOCTL_FORWARD:
2735 snd_pcm_uframes_t frames;
2736 snd_pcm_uframes_t __user *_frames = arg;
2737 snd_pcm_sframes_t result;
2738 if (get_user(frames, _frames))
2740 if (put_user(0, _frames))
2742 result = snd_pcm_capture_forward(substream, frames);
2743 __put_user(result, _frames);
2744 return result < 0 ? result : 0;
2747 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2750 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2753 struct snd_pcm_file *pcm_file;
2755 pcm_file = file->private_data;
2757 if (((cmd >> 8) & 0xff) != 'A')
2760 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2761 (void __user *)arg);
2764 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2767 struct snd_pcm_file *pcm_file;
2769 pcm_file = file->private_data;
2771 if (((cmd >> 8) & 0xff) != 'A')
2774 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2775 (void __user *)arg);
2778 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2779 unsigned int cmd, void *arg)
2784 fs = snd_enter_user();
2785 switch (substream->stream) {
2786 case SNDRV_PCM_STREAM_PLAYBACK:
2787 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2788 (void __user *)arg);
2790 case SNDRV_PCM_STREAM_CAPTURE:
2791 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2792 (void __user *)arg);
2802 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2804 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2807 struct snd_pcm_file *pcm_file;
2808 struct snd_pcm_substream *substream;
2809 struct snd_pcm_runtime *runtime;
2810 snd_pcm_sframes_t result;
2812 pcm_file = file->private_data;
2813 substream = pcm_file->substream;
2814 snd_assert(substream != NULL, return -ENXIO);
2815 runtime = substream->runtime;
2816 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2818 if (!frame_aligned(runtime, count))
2820 count = bytes_to_frames(runtime, count);
2821 result = snd_pcm_lib_read(substream, buf, count);
2823 result = frames_to_bytes(runtime, result);
2827 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2828 size_t count, loff_t * offset)
2830 struct snd_pcm_file *pcm_file;
2831 struct snd_pcm_substream *substream;
2832 struct snd_pcm_runtime *runtime;
2833 snd_pcm_sframes_t result;
2835 pcm_file = file->private_data;
2836 substream = pcm_file->substream;
2837 snd_assert(substream != NULL, result = -ENXIO; goto end);
2838 runtime = substream->runtime;
2839 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2843 if (!frame_aligned(runtime, count)) {
2847 count = bytes_to_frames(runtime, count);
2848 result = snd_pcm_lib_write(substream, buf, count);
2850 result = frames_to_bytes(runtime, result);
2855 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2856 unsigned long nr_segs, loff_t pos)
2859 struct snd_pcm_file *pcm_file;
2860 struct snd_pcm_substream *substream;
2861 struct snd_pcm_runtime *runtime;
2862 snd_pcm_sframes_t result;
2865 snd_pcm_uframes_t frames;
2867 pcm_file = iocb->ki_filp->private_data;
2868 substream = pcm_file->substream;
2869 snd_assert(substream != NULL, return -ENXIO);
2870 runtime = substream->runtime;
2871 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2873 if (nr_segs > 1024 || nr_segs != runtime->channels)
2875 if (!frame_aligned(runtime, iov->iov_len))
2877 frames = bytes_to_samples(runtime, iov->iov_len);
2878 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2881 for (i = 0; i < nr_segs; ++i)
2882 bufs[i] = iov[i].iov_base;
2883 result = snd_pcm_lib_readv(substream, bufs, frames);
2885 result = frames_to_bytes(runtime, result);
2890 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2891 unsigned long nr_segs, loff_t pos)
2893 struct snd_pcm_file *pcm_file;
2894 struct snd_pcm_substream *substream;
2895 struct snd_pcm_runtime *runtime;
2896 snd_pcm_sframes_t result;
2899 snd_pcm_uframes_t frames;
2901 pcm_file = iocb->ki_filp->private_data;
2902 substream = pcm_file->substream;
2903 snd_assert(substream != NULL, result = -ENXIO; goto end);
2904 runtime = substream->runtime;
2905 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2909 if (nr_segs > 128 || nr_segs != runtime->channels ||
2910 !frame_aligned(runtime, iov->iov_len)) {
2914 frames = bytes_to_samples(runtime, iov->iov_len);
2915 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2918 for (i = 0; i < nr_segs; ++i)
2919 bufs[i] = iov[i].iov_base;
2920 result = snd_pcm_lib_writev(substream, bufs, frames);
2922 result = frames_to_bytes(runtime, result);
2928 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2930 struct snd_pcm_file *pcm_file;
2931 struct snd_pcm_substream *substream;
2932 struct snd_pcm_runtime *runtime;
2934 snd_pcm_uframes_t avail;
2936 pcm_file = file->private_data;
2938 substream = pcm_file->substream;
2939 snd_assert(substream != NULL, return -ENXIO);
2940 runtime = substream->runtime;
2942 poll_wait(file, &runtime->sleep, wait);
2944 snd_pcm_stream_lock_irq(substream);
2945 avail = snd_pcm_playback_avail(runtime);
2946 switch (runtime->status->state) {
2947 case SNDRV_PCM_STATE_RUNNING:
2948 case SNDRV_PCM_STATE_PREPARED:
2949 case SNDRV_PCM_STATE_PAUSED:
2950 if (avail >= runtime->control->avail_min) {
2951 mask = POLLOUT | POLLWRNORM;
2955 case SNDRV_PCM_STATE_DRAINING:
2959 mask = POLLOUT | POLLWRNORM | POLLERR;
2962 snd_pcm_stream_unlock_irq(substream);
2966 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2968 struct snd_pcm_file *pcm_file;
2969 struct snd_pcm_substream *substream;
2970 struct snd_pcm_runtime *runtime;
2972 snd_pcm_uframes_t avail;
2974 pcm_file = file->private_data;
2976 substream = pcm_file->substream;
2977 snd_assert(substream != NULL, return -ENXIO);
2978 runtime = substream->runtime;
2980 poll_wait(file, &runtime->sleep, wait);
2982 snd_pcm_stream_lock_irq(substream);
2983 avail = snd_pcm_capture_avail(runtime);
2984 switch (runtime->status->state) {
2985 case SNDRV_PCM_STATE_RUNNING:
2986 case SNDRV_PCM_STATE_PREPARED:
2987 case SNDRV_PCM_STATE_PAUSED:
2988 if (avail >= runtime->control->avail_min) {
2989 mask = POLLIN | POLLRDNORM;
2994 case SNDRV_PCM_STATE_DRAINING:
2996 mask = POLLIN | POLLRDNORM;
3001 mask = POLLIN | POLLRDNORM | POLLERR;
3004 snd_pcm_stream_unlock_irq(substream);
3013 * Only on coherent architectures, we can mmap the status and the control records
3014 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3016 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3018 * mmap status record
3020 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
3021 unsigned long address, int *type)
3023 struct snd_pcm_substream *substream = area->vm_private_data;
3024 struct snd_pcm_runtime *runtime;
3027 if (substream == NULL)
3029 runtime = substream->runtime;
3030 page = virt_to_page(runtime->status);
3033 *type = VM_FAULT_MINOR;
3037 static struct vm_operations_struct snd_pcm_vm_ops_status =
3039 .nopage = snd_pcm_mmap_status_nopage,
3042 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3043 struct vm_area_struct *area)
3045 struct snd_pcm_runtime *runtime;
3047 if (!(area->vm_flags & VM_READ))
3049 runtime = substream->runtime;
3050 snd_assert(runtime != NULL, return -EAGAIN);
3051 size = area->vm_end - area->vm_start;
3052 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3054 area->vm_ops = &snd_pcm_vm_ops_status;
3055 area->vm_private_data = substream;
3056 area->vm_flags |= VM_RESERVED;
3061 * mmap control record
3063 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3064 unsigned long address, int *type)
3066 struct snd_pcm_substream *substream = area->vm_private_data;
3067 struct snd_pcm_runtime *runtime;
3070 if (substream == NULL)
3072 runtime = substream->runtime;
3073 page = virt_to_page(runtime->control);
3076 *type = VM_FAULT_MINOR;
3080 static struct vm_operations_struct snd_pcm_vm_ops_control =
3082 .nopage = snd_pcm_mmap_control_nopage,
3085 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3086 struct vm_area_struct *area)
3088 struct snd_pcm_runtime *runtime;
3090 if (!(area->vm_flags & VM_READ))
3092 runtime = substream->runtime;
3093 snd_assert(runtime != NULL, return -EAGAIN);
3094 size = area->vm_end - area->vm_start;
3095 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3097 area->vm_ops = &snd_pcm_vm_ops_control;
3098 area->vm_private_data = substream;
3099 area->vm_flags |= VM_RESERVED;
3102 #else /* ! coherent mmap */
3104 * don't support mmap for status and control records.
3106 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3107 struct vm_area_struct *area)
3111 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3112 struct vm_area_struct *area)
3116 #endif /* coherent mmap */
3119 * nopage callback for mmapping a RAM page
3121 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3122 unsigned long address, int *type)
3124 struct snd_pcm_substream *substream = area->vm_private_data;
3125 struct snd_pcm_runtime *runtime;
3126 unsigned long offset;
3131 if (substream == NULL)
3133 runtime = substream->runtime;
3134 offset = area->vm_pgoff << PAGE_SHIFT;
3135 offset += address - area->vm_start;
3136 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3137 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3138 if (offset > dma_bytes - PAGE_SIZE)
3139 return NOPAGE_SIGBUS;
3140 if (substream->ops->page) {
3141 page = substream->ops->page(substream, offset);
3145 vaddr = runtime->dma_area + offset;
3146 page = virt_to_page(vaddr);
3150 *type = VM_FAULT_MINOR;
3154 static struct vm_operations_struct snd_pcm_vm_ops_data =
3156 .open = snd_pcm_mmap_data_open,
3157 .close = snd_pcm_mmap_data_close,
3158 .nopage = snd_pcm_mmap_data_nopage,
3162 * mmap the DMA buffer on RAM
3164 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3165 struct vm_area_struct *area)
3167 area->vm_ops = &snd_pcm_vm_ops_data;
3168 area->vm_private_data = substream;
3169 area->vm_flags |= VM_RESERVED;
3170 atomic_inc(&substream->mmap_count);
3175 * mmap the DMA buffer on I/O memory area
3177 #if SNDRV_PCM_INFO_MMAP_IOMEM
3178 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3180 .open = snd_pcm_mmap_data_open,
3181 .close = snd_pcm_mmap_data_close,
3184 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3185 struct vm_area_struct *area)
3188 unsigned long offset;
3190 #ifdef pgprot_noncached
3191 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3193 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3194 area->vm_private_data = substream;
3195 area->vm_flags |= VM_IO;
3196 size = area->vm_end - area->vm_start;
3197 offset = area->vm_pgoff << PAGE_SHIFT;
3198 if (io_remap_pfn_range(area, area->vm_start,
3199 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3200 size, area->vm_page_prot))
3202 atomic_inc(&substream->mmap_count);
3206 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3207 #endif /* SNDRV_PCM_INFO_MMAP */
3212 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3213 struct vm_area_struct *area)
3215 struct snd_pcm_runtime *runtime;
3217 unsigned long offset;
3220 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3221 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3224 if (!(area->vm_flags & VM_READ))
3227 runtime = substream->runtime;
3228 snd_assert(runtime != NULL, return -EAGAIN);
3229 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3231 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3233 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3234 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3236 size = area->vm_end - area->vm_start;
3237 offset = area->vm_pgoff << PAGE_SHIFT;
3238 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3239 if ((size_t)size > dma_bytes)
3241 if (offset > dma_bytes - size)
3244 if (substream->ops->mmap)
3245 return substream->ops->mmap(substream, area);
3247 return snd_pcm_default_mmap(substream, area);
3250 EXPORT_SYMBOL(snd_pcm_mmap_data);
3252 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3254 struct snd_pcm_file * pcm_file;
3255 struct snd_pcm_substream *substream;
3256 unsigned long offset;
3258 pcm_file = file->private_data;
3259 substream = pcm_file->substream;
3260 snd_assert(substream != NULL, return -ENXIO);
3262 offset = area->vm_pgoff << PAGE_SHIFT;
3264 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3265 if (pcm_file->no_compat_mmap)
3267 return snd_pcm_mmap_status(substream, file, area);
3268 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3269 if (pcm_file->no_compat_mmap)
3271 return snd_pcm_mmap_control(substream, file, area);
3273 return snd_pcm_mmap_data(substream, file, area);
3278 static int snd_pcm_fasync(int fd, struct file * file, int on)
3280 struct snd_pcm_file * pcm_file;
3281 struct snd_pcm_substream *substream;
3282 struct snd_pcm_runtime *runtime;
3285 pcm_file = file->private_data;
3286 substream = pcm_file->substream;
3287 snd_assert(substream != NULL, return -ENXIO);
3288 runtime = substream->runtime;
3290 err = fasync_helper(fd, file, on, &runtime->fasync);
3299 #ifdef CONFIG_COMPAT
3300 #include "pcm_compat.c"
3302 #define snd_pcm_ioctl_compat NULL
3306 * To be removed helpers to keep binary compatibility
3309 #ifdef CONFIG_SND_SUPPORT_OLD_API
3310 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3311 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3313 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3314 struct snd_pcm_hw_params_old *oparams)
3318 memset(params, 0, sizeof(*params));
3319 params->flags = oparams->flags;
3320 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3321 params->masks[i].bits[0] = oparams->masks[i];
3322 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3323 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3324 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3325 params->info = oparams->info;
3326 params->msbits = oparams->msbits;
3327 params->rate_num = oparams->rate_num;
3328 params->rate_den = oparams->rate_den;
3329 params->fifo_size = oparams->fifo_size;
3332 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3333 struct snd_pcm_hw_params *params)
3337 memset(oparams, 0, sizeof(*oparams));
3338 oparams->flags = params->flags;
3339 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3340 oparams->masks[i] = params->masks[i].bits[0];
3341 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3342 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3343 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3344 oparams->info = params->info;
3345 oparams->msbits = params->msbits;
3346 oparams->rate_num = params->rate_num;
3347 oparams->rate_den = params->rate_den;
3348 oparams->fifo_size = params->fifo_size;
3351 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3352 struct snd_pcm_hw_params_old __user * _oparams)
3354 struct snd_pcm_hw_params *params;
3355 struct snd_pcm_hw_params_old *oparams = NULL;
3358 params = kmalloc(sizeof(*params), GFP_KERNEL);
3363 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3369 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3373 snd_pcm_hw_convert_from_old_params(params, oparams);
3374 err = snd_pcm_hw_refine(substream, params);
3375 snd_pcm_hw_convert_to_old_params(oparams, params);
3376 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3386 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3387 struct snd_pcm_hw_params_old __user * _oparams)
3389 struct snd_pcm_hw_params *params;
3390 struct snd_pcm_hw_params_old *oparams = NULL;
3393 params = kmalloc(sizeof(*params), GFP_KERNEL);
3398 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3403 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3407 snd_pcm_hw_convert_from_old_params(params, oparams);
3408 err = snd_pcm_hw_params(substream, params);
3409 snd_pcm_hw_convert_to_old_params(oparams, params);
3410 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3419 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3425 struct file_operations snd_pcm_f_ops[2] = {
3427 .owner = THIS_MODULE,
3428 .write = snd_pcm_write,
3429 .aio_write = snd_pcm_aio_write,
3430 .open = snd_pcm_playback_open,
3431 .release = snd_pcm_release,
3432 .poll = snd_pcm_playback_poll,
3433 .unlocked_ioctl = snd_pcm_playback_ioctl,
3434 .compat_ioctl = snd_pcm_ioctl_compat,
3435 .mmap = snd_pcm_mmap,
3436 .fasync = snd_pcm_fasync,
3439 .owner = THIS_MODULE,
3440 .read = snd_pcm_read,
3441 .aio_read = snd_pcm_aio_read,
3442 .open = snd_pcm_capture_open,
3443 .release = snd_pcm_release,
3444 .poll = snd_pcm_capture_poll,
3445 .unlocked_ioctl = snd_pcm_capture_ioctl,
3446 .compat_ioctl = snd_pcm_ioctl_compat,
3447 .mmap = snd_pcm_mmap,
3448 .fasync = snd_pcm_fasync,