2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.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
23 #include <linux/file.h>
24 #include <linux/slab.h>
25 #include <linux/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/math64.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
39 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
40 #include <dma-coherence.h>
47 struct snd_pcm_hw_params_old {
49 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
50 SNDRV_PCM_HW_PARAM_ACCESS + 1];
51 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
52 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
57 unsigned int rate_num;
58 unsigned int rate_den;
59 snd_pcm_uframes_t fifo_size;
60 unsigned char reserved[64];
63 #ifdef CONFIG_SND_SUPPORT_OLD_API
64 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
65 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
67 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params_old __user * _oparams);
69 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
70 struct snd_pcm_hw_params_old __user * _oparams);
72 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
78 DEFINE_RWLOCK(snd_pcm_link_rwlock);
79 EXPORT_SYMBOL(snd_pcm_link_rwlock);
81 static DECLARE_RWSEM(snd_pcm_link_rwsem);
83 static inline mm_segment_t snd_enter_user(void)
85 mm_segment_t fs = get_fs();
90 static inline void snd_leave_user(mm_segment_t fs)
97 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
99 struct snd_pcm_runtime *runtime;
100 struct snd_pcm *pcm = substream->pcm;
101 struct snd_pcm_str *pstr = substream->pstr;
103 memset(info, 0, sizeof(*info));
104 info->card = pcm->card->number;
105 info->device = pcm->device;
106 info->stream = substream->stream;
107 info->subdevice = substream->number;
108 strlcpy(info->id, pcm->id, sizeof(info->id));
109 strlcpy(info->name, pcm->name, sizeof(info->name));
110 info->dev_class = pcm->dev_class;
111 info->dev_subclass = pcm->dev_subclass;
112 info->subdevices_count = pstr->substream_count;
113 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
114 strlcpy(info->subname, substream->name, sizeof(info->subname));
115 runtime = substream->runtime;
116 /* AB: FIXME!!! This is definitely nonsense */
118 info->sync = runtime->sync;
119 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
124 int snd_pcm_info_user(struct snd_pcm_substream *substream,
125 struct snd_pcm_info __user * _info)
127 struct snd_pcm_info *info;
130 info = kmalloc(sizeof(*info), GFP_KERNEL);
133 err = snd_pcm_info(substream, info);
135 if (copy_to_user(_info, info, sizeof(*info)))
145 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
146 char *snd_pcm_hw_param_names[] = {
150 HW_PARAM(SAMPLE_BITS),
151 HW_PARAM(FRAME_BITS),
154 HW_PARAM(PERIOD_TIME),
155 HW_PARAM(PERIOD_SIZE),
156 HW_PARAM(PERIOD_BYTES),
158 HW_PARAM(BUFFER_TIME),
159 HW_PARAM(BUFFER_SIZE),
160 HW_PARAM(BUFFER_BYTES),
165 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
166 struct snd_pcm_hw_params *params)
169 struct snd_pcm_hardware *hw;
170 struct snd_interval *i = NULL;
171 struct snd_mask *m = NULL;
172 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
173 unsigned int rstamps[constrs->rules_num];
174 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
175 unsigned int stamp = 2;
179 params->fifo_size = 0;
180 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
182 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
183 params->rate_num = 0;
184 params->rate_den = 0;
187 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
188 m = hw_param_mask(params, k);
189 if (snd_mask_empty(m))
191 if (!(params->rmask & (1 << k)))
194 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
195 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
197 changed = snd_mask_refine(m, constrs_mask(constrs, k));
199 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
202 params->cmask |= 1 << k;
207 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
208 i = hw_param_interval(params, k);
209 if (snd_interval_empty(i))
211 if (!(params->rmask & (1 << k)))
214 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
219 i->openmin ? '(' : '[', i->min,
220 i->max, i->openmax ? ')' : ']');
223 changed = snd_interval_refine(i, constrs_interval(constrs, k));
228 printk("%c%u %u%c\n",
229 i->openmin ? '(' : '[', i->min,
230 i->max, i->openmax ? ')' : ']');
233 params->cmask |= 1 << k;
238 for (k = 0; k < constrs->rules_num; k++)
240 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
241 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
244 for (k = 0; k < constrs->rules_num; k++) {
245 struct snd_pcm_hw_rule *r = &constrs->rules[k];
248 if (r->cond && !(r->cond & params->flags))
250 for (d = 0; r->deps[d] >= 0; d++) {
251 if (vstamps[r->deps[d]] > rstamps[k]) {
259 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
261 printk("%s = ", snd_pcm_hw_param_names[r->var]);
262 if (hw_is_mask(r->var)) {
263 m = hw_param_mask(params, r->var);
264 printk("%x", *m->bits);
266 i = hw_param_interval(params, r->var);
271 i->openmin ? '(' : '[', i->min,
272 i->max, i->openmax ? ')' : ']');
276 changed = r->func(params, r);
280 if (hw_is_mask(r->var))
281 printk("%x", *m->bits);
287 i->openmin ? '(' : '[', i->min,
288 i->max, i->openmax ? ')' : ']');
294 if (changed && r->var >= 0) {
295 params->cmask |= (1 << r->var);
296 vstamps[r->var] = stamp;
304 if (!params->msbits) {
305 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
306 if (snd_interval_single(i))
307 params->msbits = snd_interval_value(i);
310 if (!params->rate_den) {
311 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
312 if (snd_interval_single(i)) {
313 params->rate_num = snd_interval_value(i);
314 params->rate_den = 1;
318 hw = &substream->runtime->hw;
320 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
321 if (!params->fifo_size) {
322 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
323 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
324 if (snd_mask_min(m) == snd_mask_max(m) &&
325 snd_interval_min(i) == snd_interval_max(i)) {
326 changed = substream->ops->ioctl(substream,
327 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
336 EXPORT_SYMBOL(snd_pcm_hw_refine);
338 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
339 struct snd_pcm_hw_params __user * _params)
341 struct snd_pcm_hw_params *params;
344 params = memdup_user(_params, sizeof(*params));
346 return PTR_ERR(params);
348 err = snd_pcm_hw_refine(substream, params);
349 if (copy_to_user(_params, params, sizeof(*params))) {
358 static int period_to_usecs(struct snd_pcm_runtime *runtime)
363 return -1; /* invalid */
365 /* take 75% of period time as the deadline */
366 usecs = (750000 / runtime->rate) * runtime->period_size;
367 usecs += ((750000 % runtime->rate) * runtime->period_size) /
373 static int calc_boundary(struct snd_pcm_runtime *runtime)
377 boundary = (u_int64_t)runtime->buffer_size *
378 (u_int64_t)runtime->period_size;
379 #if BITS_PER_LONG < 64
380 /* try to find lowest common multiple for buffer and period */
381 if (boundary > LONG_MAX - runtime->buffer_size) {
382 u_int32_t remainder = -1;
383 u_int32_t divident = runtime->buffer_size;
384 u_int32_t divisor = runtime->period_size;
386 remainder = divident % divisor;
392 boundary = div_u64(boundary, divisor);
393 if (boundary > LONG_MAX - runtime->buffer_size)
399 runtime->boundary = boundary;
400 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
401 runtime->boundary *= 2;
405 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
406 struct snd_pcm_hw_params *params)
408 struct snd_pcm_runtime *runtime;
411 snd_pcm_uframes_t frames;
413 if (PCM_RUNTIME_CHECK(substream))
415 runtime = substream->runtime;
416 snd_pcm_stream_lock_irq(substream);
417 switch (runtime->status->state) {
418 case SNDRV_PCM_STATE_OPEN:
419 case SNDRV_PCM_STATE_SETUP:
420 case SNDRV_PCM_STATE_PREPARED:
423 snd_pcm_stream_unlock_irq(substream);
426 snd_pcm_stream_unlock_irq(substream);
427 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
428 if (!substream->oss.oss)
430 if (atomic_read(&substream->mmap_count))
434 err = snd_pcm_hw_refine(substream, params);
438 err = snd_pcm_hw_params_choose(substream, params);
442 if (substream->ops->hw_params != NULL) {
443 err = substream->ops->hw_params(substream, params);
448 runtime->access = params_access(params);
449 runtime->format = params_format(params);
450 runtime->subformat = params_subformat(params);
451 runtime->channels = params_channels(params);
452 runtime->rate = params_rate(params);
453 runtime->period_size = params_period_size(params);
454 runtime->periods = params_periods(params);
455 runtime->buffer_size = params_buffer_size(params);
456 runtime->info = params->info;
457 runtime->rate_num = params->rate_num;
458 runtime->rate_den = params->rate_den;
460 bits = snd_pcm_format_physical_width(runtime->format);
461 runtime->sample_bits = bits;
462 bits *= runtime->channels;
463 runtime->frame_bits = bits;
465 while (bits % 8 != 0) {
469 runtime->byte_align = bits / 8;
470 runtime->min_align = frames;
472 /* Default sw params */
473 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
474 runtime->period_step = 1;
475 runtime->control->avail_min = runtime->period_size;
476 runtime->start_threshold = 1;
477 runtime->stop_threshold = runtime->buffer_size;
478 runtime->silence_threshold = 0;
479 runtime->silence_size = 0;
480 err = calc_boundary(runtime);
484 snd_pcm_timer_resolution_change(substream);
485 runtime->status->state = SNDRV_PCM_STATE_SETUP;
487 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
488 substream->latency_id);
489 if ((usecs = period_to_usecs(runtime)) >= 0)
490 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
491 substream->latency_id, usecs);
494 /* hardware might be unuseable from this time,
495 so we force application to retry to set
496 the correct hardware parameter settings */
497 runtime->status->state = SNDRV_PCM_STATE_OPEN;
498 if (substream->ops->hw_free != NULL)
499 substream->ops->hw_free(substream);
503 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
504 struct snd_pcm_hw_params __user * _params)
506 struct snd_pcm_hw_params *params;
509 params = memdup_user(_params, sizeof(*params));
511 return PTR_ERR(params);
513 err = snd_pcm_hw_params(substream, params);
514 if (copy_to_user(_params, params, sizeof(*params))) {
523 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
525 struct snd_pcm_runtime *runtime;
528 if (PCM_RUNTIME_CHECK(substream))
530 runtime = substream->runtime;
531 snd_pcm_stream_lock_irq(substream);
532 switch (runtime->status->state) {
533 case SNDRV_PCM_STATE_SETUP:
534 case SNDRV_PCM_STATE_PREPARED:
537 snd_pcm_stream_unlock_irq(substream);
540 snd_pcm_stream_unlock_irq(substream);
541 if (atomic_read(&substream->mmap_count))
543 if (substream->ops->hw_free)
544 result = substream->ops->hw_free(substream);
545 runtime->status->state = SNDRV_PCM_STATE_OPEN;
546 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
547 substream->latency_id);
551 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
552 struct snd_pcm_sw_params *params)
554 struct snd_pcm_runtime *runtime;
557 if (PCM_RUNTIME_CHECK(substream))
559 runtime = substream->runtime;
560 snd_pcm_stream_lock_irq(substream);
561 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
562 snd_pcm_stream_unlock_irq(substream);
565 snd_pcm_stream_unlock_irq(substream);
567 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
569 if (params->avail_min == 0)
571 if (params->silence_size >= runtime->boundary) {
572 if (params->silence_threshold != 0)
575 if (params->silence_size > params->silence_threshold)
577 if (params->silence_threshold > runtime->buffer_size)
581 snd_pcm_stream_lock_irq(substream);
582 runtime->tstamp_mode = params->tstamp_mode;
583 runtime->period_step = params->period_step;
584 runtime->control->avail_min = params->avail_min;
585 runtime->start_threshold = params->start_threshold;
586 runtime->stop_threshold = params->stop_threshold;
587 runtime->silence_threshold = params->silence_threshold;
588 runtime->silence_size = params->silence_size;
589 params->boundary = runtime->boundary;
590 if (snd_pcm_running(substream)) {
591 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
592 runtime->silence_size > 0)
593 snd_pcm_playback_silence(substream, ULONG_MAX);
594 err = snd_pcm_update_state(substream, runtime);
596 snd_pcm_stream_unlock_irq(substream);
600 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
601 struct snd_pcm_sw_params __user * _params)
603 struct snd_pcm_sw_params params;
605 if (copy_from_user(¶ms, _params, sizeof(params)))
607 err = snd_pcm_sw_params(substream, ¶ms);
608 if (copy_to_user(_params, ¶ms, sizeof(params)))
613 int snd_pcm_status(struct snd_pcm_substream *substream,
614 struct snd_pcm_status *status)
616 struct snd_pcm_runtime *runtime = substream->runtime;
618 snd_pcm_stream_lock_irq(substream);
619 status->state = runtime->status->state;
620 status->suspended_state = runtime->status->suspended_state;
621 if (status->state == SNDRV_PCM_STATE_OPEN)
623 status->trigger_tstamp = runtime->trigger_tstamp;
624 if (snd_pcm_running(substream)) {
625 snd_pcm_update_hw_ptr(substream);
626 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
627 status->tstamp = runtime->status->tstamp;
631 snd_pcm_gettime(runtime, &status->tstamp);
633 status->appl_ptr = runtime->control->appl_ptr;
634 status->hw_ptr = runtime->status->hw_ptr;
635 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
636 status->avail = snd_pcm_playback_avail(runtime);
637 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
638 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
639 status->delay = runtime->buffer_size - status->avail;
640 status->delay += runtime->delay;
644 status->avail = snd_pcm_capture_avail(runtime);
645 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
646 status->delay = status->avail + runtime->delay;
650 status->avail_max = runtime->avail_max;
651 status->overrange = runtime->overrange;
652 runtime->avail_max = 0;
653 runtime->overrange = 0;
655 snd_pcm_stream_unlock_irq(substream);
659 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
660 struct snd_pcm_status __user * _status)
662 struct snd_pcm_status status;
665 memset(&status, 0, sizeof(status));
666 res = snd_pcm_status(substream, &status);
669 if (copy_to_user(_status, &status, sizeof(status)))
674 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
675 struct snd_pcm_channel_info * info)
677 struct snd_pcm_runtime *runtime;
678 unsigned int channel;
680 channel = info->channel;
681 runtime = substream->runtime;
682 snd_pcm_stream_lock_irq(substream);
683 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
684 snd_pcm_stream_unlock_irq(substream);
687 snd_pcm_stream_unlock_irq(substream);
688 if (channel >= runtime->channels)
690 memset(info, 0, sizeof(*info));
691 info->channel = channel;
692 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
695 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
696 struct snd_pcm_channel_info __user * _info)
698 struct snd_pcm_channel_info info;
701 if (copy_from_user(&info, _info, sizeof(info)))
703 res = snd_pcm_channel_info(substream, &info);
706 if (copy_to_user(_info, &info, sizeof(info)))
711 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
713 struct snd_pcm_runtime *runtime = substream->runtime;
714 if (runtime->trigger_master == NULL)
716 if (runtime->trigger_master == substream) {
717 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
719 snd_pcm_trigger_tstamp(runtime->trigger_master);
720 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
722 runtime->trigger_master = NULL;
726 int (*pre_action)(struct snd_pcm_substream *substream, int state);
727 int (*do_action)(struct snd_pcm_substream *substream, int state);
728 void (*undo_action)(struct snd_pcm_substream *substream, int state);
729 void (*post_action)(struct snd_pcm_substream *substream, int state);
733 * this functions is core for handling of linked stream
734 * Note: the stream state might be changed also on failure
735 * Note2: call with calling stream lock + link lock
737 static int snd_pcm_action_group(struct action_ops *ops,
738 struct snd_pcm_substream *substream,
739 int state, int do_lock)
741 struct snd_pcm_substream *s = NULL;
742 struct snd_pcm_substream *s1;
745 snd_pcm_group_for_each_entry(s, substream) {
746 if (do_lock && s != substream)
747 spin_lock_nested(&s->self_group.lock,
748 SINGLE_DEPTH_NESTING);
749 res = ops->pre_action(s, state);
753 snd_pcm_group_for_each_entry(s, substream) {
754 res = ops->do_action(s, state);
756 if (ops->undo_action) {
757 snd_pcm_group_for_each_entry(s1, substream) {
758 if (s1 == s) /* failed stream */
760 ops->undo_action(s1, state);
763 s = NULL; /* unlock all */
767 snd_pcm_group_for_each_entry(s, substream) {
768 ops->post_action(s, state);
773 snd_pcm_group_for_each_entry(s1, substream) {
775 spin_unlock(&s1->self_group.lock);
776 if (s1 == s) /* end */
784 * Note: call with stream lock
786 static int snd_pcm_action_single(struct action_ops *ops,
787 struct snd_pcm_substream *substream,
792 res = ops->pre_action(substream, state);
795 res = ops->do_action(substream, state);
797 ops->post_action(substream, state);
798 else if (ops->undo_action)
799 ops->undo_action(substream, state);
804 * Note: call with stream lock
806 static int snd_pcm_action(struct action_ops *ops,
807 struct snd_pcm_substream *substream,
812 if (snd_pcm_stream_linked(substream)) {
813 if (!spin_trylock(&substream->group->lock)) {
814 spin_unlock(&substream->self_group.lock);
815 spin_lock(&substream->group->lock);
816 spin_lock(&substream->self_group.lock);
818 res = snd_pcm_action_group(ops, substream, state, 1);
819 spin_unlock(&substream->group->lock);
821 res = snd_pcm_action_single(ops, substream, state);
827 * Note: don't use any locks before
829 static int snd_pcm_action_lock_irq(struct action_ops *ops,
830 struct snd_pcm_substream *substream,
835 read_lock_irq(&snd_pcm_link_rwlock);
836 if (snd_pcm_stream_linked(substream)) {
837 spin_lock(&substream->group->lock);
838 spin_lock(&substream->self_group.lock);
839 res = snd_pcm_action_group(ops, substream, state, 1);
840 spin_unlock(&substream->self_group.lock);
841 spin_unlock(&substream->group->lock);
843 spin_lock(&substream->self_group.lock);
844 res = snd_pcm_action_single(ops, substream, state);
845 spin_unlock(&substream->self_group.lock);
847 read_unlock_irq(&snd_pcm_link_rwlock);
853 static int snd_pcm_action_nonatomic(struct action_ops *ops,
854 struct snd_pcm_substream *substream,
859 down_read(&snd_pcm_link_rwsem);
860 if (snd_pcm_stream_linked(substream))
861 res = snd_pcm_action_group(ops, substream, state, 0);
863 res = snd_pcm_action_single(ops, substream, state);
864 up_read(&snd_pcm_link_rwsem);
871 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
873 struct snd_pcm_runtime *runtime = substream->runtime;
874 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
876 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
877 !snd_pcm_playback_data(substream))
879 runtime->trigger_master = substream;
883 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
885 if (substream->runtime->trigger_master != substream)
887 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
890 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
892 if (substream->runtime->trigger_master == substream)
893 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
896 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
898 struct snd_pcm_runtime *runtime = substream->runtime;
899 snd_pcm_trigger_tstamp(substream);
900 runtime->hw_ptr_jiffies = jiffies;
901 runtime->status->state = state;
902 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
903 runtime->silence_size > 0)
904 snd_pcm_playback_silence(substream, ULONG_MAX);
905 if (substream->timer)
906 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
907 &runtime->trigger_tstamp);
910 static struct action_ops snd_pcm_action_start = {
911 .pre_action = snd_pcm_pre_start,
912 .do_action = snd_pcm_do_start,
913 .undo_action = snd_pcm_undo_start,
914 .post_action = snd_pcm_post_start
918 * snd_pcm_start - start all linked streams
919 * @substream: the PCM substream instance
921 int snd_pcm_start(struct snd_pcm_substream *substream)
923 return snd_pcm_action(&snd_pcm_action_start, substream,
924 SNDRV_PCM_STATE_RUNNING);
930 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
932 struct snd_pcm_runtime *runtime = substream->runtime;
933 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
935 runtime->trigger_master = substream;
939 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
941 if (substream->runtime->trigger_master == substream &&
942 snd_pcm_running(substream))
943 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
944 return 0; /* unconditonally stop all substreams */
947 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
949 struct snd_pcm_runtime *runtime = substream->runtime;
950 if (runtime->status->state != state) {
951 snd_pcm_trigger_tstamp(substream);
952 if (substream->timer)
953 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
954 &runtime->trigger_tstamp);
955 runtime->status->state = state;
957 wake_up(&runtime->sleep);
958 wake_up(&runtime->tsleep);
961 static struct action_ops snd_pcm_action_stop = {
962 .pre_action = snd_pcm_pre_stop,
963 .do_action = snd_pcm_do_stop,
964 .post_action = snd_pcm_post_stop
968 * snd_pcm_stop - try to stop all running streams in the substream group
969 * @substream: the PCM substream instance
970 * @state: PCM state after stopping the stream
972 * The state of each stream is then changed to the given state unconditionally.
974 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
976 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
979 EXPORT_SYMBOL(snd_pcm_stop);
982 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
983 * @substream: the PCM substream
985 * After stopping, the state is changed to SETUP.
986 * Unlike snd_pcm_stop(), this affects only the given stream.
988 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
990 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
991 SNDRV_PCM_STATE_SETUP);
997 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
999 struct snd_pcm_runtime *runtime = substream->runtime;
1000 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1003 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1005 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1007 runtime->trigger_master = substream;
1011 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1013 if (substream->runtime->trigger_master != substream)
1015 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1016 * a delta betwen the current jiffies, this gives a large enough
1017 * delta, effectively to skip the check once.
1019 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1020 return substream->ops->trigger(substream,
1021 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1022 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1025 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1027 if (substream->runtime->trigger_master == substream)
1028 substream->ops->trigger(substream,
1029 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1030 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1033 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1035 struct snd_pcm_runtime *runtime = substream->runtime;
1036 snd_pcm_trigger_tstamp(substream);
1038 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1039 if (substream->timer)
1040 snd_timer_notify(substream->timer,
1041 SNDRV_TIMER_EVENT_MPAUSE,
1042 &runtime->trigger_tstamp);
1043 wake_up(&runtime->sleep);
1044 wake_up(&runtime->tsleep);
1046 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1047 if (substream->timer)
1048 snd_timer_notify(substream->timer,
1049 SNDRV_TIMER_EVENT_MCONTINUE,
1050 &runtime->trigger_tstamp);
1054 static struct action_ops snd_pcm_action_pause = {
1055 .pre_action = snd_pcm_pre_pause,
1056 .do_action = snd_pcm_do_pause,
1057 .undo_action = snd_pcm_undo_pause,
1058 .post_action = snd_pcm_post_pause
1062 * Push/release the pause for all linked streams.
1064 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1066 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1072 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1074 struct snd_pcm_runtime *runtime = substream->runtime;
1075 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1077 runtime->trigger_master = substream;
1081 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1083 struct snd_pcm_runtime *runtime = substream->runtime;
1084 if (runtime->trigger_master != substream)
1086 if (! snd_pcm_running(substream))
1088 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1089 return 0; /* suspend unconditionally */
1092 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1094 struct snd_pcm_runtime *runtime = substream->runtime;
1095 snd_pcm_trigger_tstamp(substream);
1096 if (substream->timer)
1097 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1098 &runtime->trigger_tstamp);
1099 runtime->status->suspended_state = runtime->status->state;
1100 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1101 wake_up(&runtime->sleep);
1102 wake_up(&runtime->tsleep);
1105 static struct action_ops snd_pcm_action_suspend = {
1106 .pre_action = snd_pcm_pre_suspend,
1107 .do_action = snd_pcm_do_suspend,
1108 .post_action = snd_pcm_post_suspend
1112 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1113 * @substream: the PCM substream
1115 * After this call, all streams are changed to SUSPENDED state.
1117 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1120 unsigned long flags;
1125 snd_pcm_stream_lock_irqsave(substream, flags);
1126 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1127 snd_pcm_stream_unlock_irqrestore(substream, flags);
1131 EXPORT_SYMBOL(snd_pcm_suspend);
1134 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1135 * @pcm: the PCM instance
1137 * After this call, all streams are changed to SUSPENDED state.
1139 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1141 struct snd_pcm_substream *substream;
1142 int stream, err = 0;
1147 for (stream = 0; stream < 2; stream++) {
1148 for (substream = pcm->streams[stream].substream;
1149 substream; substream = substream->next) {
1150 /* FIXME: the open/close code should lock this as well */
1151 if (substream->runtime == NULL)
1153 err = snd_pcm_suspend(substream);
1154 if (err < 0 && err != -EBUSY)
1161 EXPORT_SYMBOL(snd_pcm_suspend_all);
1165 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1167 struct snd_pcm_runtime *runtime = substream->runtime;
1168 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1170 runtime->trigger_master = substream;
1174 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1176 struct snd_pcm_runtime *runtime = substream->runtime;
1177 if (runtime->trigger_master != substream)
1179 /* DMA not running previously? */
1180 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1181 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1182 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1184 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1187 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1189 if (substream->runtime->trigger_master == substream &&
1190 snd_pcm_running(substream))
1191 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1194 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1196 struct snd_pcm_runtime *runtime = substream->runtime;
1197 snd_pcm_trigger_tstamp(substream);
1198 if (substream->timer)
1199 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1200 &runtime->trigger_tstamp);
1201 runtime->status->state = runtime->status->suspended_state;
1204 static struct action_ops snd_pcm_action_resume = {
1205 .pre_action = snd_pcm_pre_resume,
1206 .do_action = snd_pcm_do_resume,
1207 .undo_action = snd_pcm_undo_resume,
1208 .post_action = snd_pcm_post_resume
1211 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1213 struct snd_card *card = substream->pcm->card;
1216 snd_power_lock(card);
1217 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1218 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1219 snd_power_unlock(card);
1225 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1230 #endif /* CONFIG_PM */
1235 * Change the RUNNING stream(s) to XRUN state.
1237 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1239 struct snd_card *card = substream->pcm->card;
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1243 snd_power_lock(card);
1244 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1245 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1250 snd_pcm_stream_lock_irq(substream);
1251 switch (runtime->status->state) {
1252 case SNDRV_PCM_STATE_XRUN:
1253 result = 0; /* already there */
1255 case SNDRV_PCM_STATE_RUNNING:
1256 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1261 snd_pcm_stream_unlock_irq(substream);
1263 snd_power_unlock(card);
1270 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1272 struct snd_pcm_runtime *runtime = substream->runtime;
1273 switch (runtime->status->state) {
1274 case SNDRV_PCM_STATE_RUNNING:
1275 case SNDRV_PCM_STATE_PREPARED:
1276 case SNDRV_PCM_STATE_PAUSED:
1277 case SNDRV_PCM_STATE_SUSPENDED:
1284 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1286 struct snd_pcm_runtime *runtime = substream->runtime;
1287 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1290 runtime->hw_ptr_base = 0;
1291 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1292 runtime->status->hw_ptr % runtime->period_size;
1293 runtime->silence_start = runtime->status->hw_ptr;
1294 runtime->silence_filled = 0;
1298 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1300 struct snd_pcm_runtime *runtime = substream->runtime;
1301 runtime->control->appl_ptr = runtime->status->hw_ptr;
1302 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1303 runtime->silence_size > 0)
1304 snd_pcm_playback_silence(substream, ULONG_MAX);
1307 static struct action_ops snd_pcm_action_reset = {
1308 .pre_action = snd_pcm_pre_reset,
1309 .do_action = snd_pcm_do_reset,
1310 .post_action = snd_pcm_post_reset
1313 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1315 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1321 /* we use the second argument for updating f_flags */
1322 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1325 struct snd_pcm_runtime *runtime = substream->runtime;
1326 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1327 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1329 if (snd_pcm_running(substream))
1331 substream->f_flags = f_flags;
1335 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1338 err = substream->ops->prepare(substream);
1341 return snd_pcm_do_reset(substream, 0);
1344 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1346 struct snd_pcm_runtime *runtime = substream->runtime;
1347 runtime->control->appl_ptr = runtime->status->hw_ptr;
1348 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1351 static struct action_ops snd_pcm_action_prepare = {
1352 .pre_action = snd_pcm_pre_prepare,
1353 .do_action = snd_pcm_do_prepare,
1354 .post_action = snd_pcm_post_prepare
1358 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1359 * @substream: the PCM substream instance
1360 * @file: file to refer f_flags
1362 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1366 struct snd_card *card = substream->pcm->card;
1370 f_flags = file->f_flags;
1372 f_flags = substream->f_flags;
1374 snd_power_lock(card);
1375 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1376 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1377 substream, f_flags);
1378 snd_power_unlock(card);
1386 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1388 substream->runtime->trigger_master = substream;
1392 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1394 struct snd_pcm_runtime *runtime = substream->runtime;
1395 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1396 switch (runtime->status->state) {
1397 case SNDRV_PCM_STATE_PREPARED:
1398 /* start playback stream if possible */
1399 if (! snd_pcm_playback_empty(substream)) {
1400 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1401 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1404 case SNDRV_PCM_STATE_RUNNING:
1405 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1411 /* stop running stream */
1412 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1413 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1414 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1415 snd_pcm_do_stop(substream, new_state);
1416 snd_pcm_post_stop(substream, new_state);
1422 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1426 static struct action_ops snd_pcm_action_drain_init = {
1427 .pre_action = snd_pcm_pre_drain_init,
1428 .do_action = snd_pcm_do_drain_init,
1429 .post_action = snd_pcm_post_drain_init
1432 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1435 * Drain the stream(s).
1436 * When the substream is linked, sync until the draining of all playback streams
1438 * After this call, all streams are supposed to be either SETUP or DRAINING
1439 * (capture only) state.
1441 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1444 struct snd_card *card;
1445 struct snd_pcm_runtime *runtime;
1446 struct snd_pcm_substream *s;
1451 card = substream->pcm->card;
1452 runtime = substream->runtime;
1454 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1457 snd_power_lock(card);
1458 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1459 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1461 snd_power_unlock(card);
1467 if (file->f_flags & O_NONBLOCK)
1469 } else if (substream->f_flags & O_NONBLOCK)
1472 down_read(&snd_pcm_link_rwsem);
1473 snd_pcm_stream_lock_irq(substream);
1475 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1476 snd_pcm_pause(substream, 0);
1478 /* pre-start/stop - all running streams are changed to DRAINING state */
1479 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1482 /* in non-blocking, we don't wait in ioctl but let caller poll */
1490 struct snd_pcm_runtime *to_check;
1491 if (signal_pending(current)) {
1492 result = -ERESTARTSYS;
1495 /* find a substream to drain */
1497 snd_pcm_group_for_each_entry(s, substream) {
1498 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1500 runtime = s->runtime;
1501 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1507 break; /* all drained */
1508 init_waitqueue_entry(&wait, current);
1509 add_wait_queue(&to_check->sleep, &wait);
1510 set_current_state(TASK_INTERRUPTIBLE);
1511 snd_pcm_stream_unlock_irq(substream);
1512 up_read(&snd_pcm_link_rwsem);
1513 snd_power_unlock(card);
1514 tout = schedule_timeout(10 * HZ);
1515 snd_power_lock(card);
1516 down_read(&snd_pcm_link_rwsem);
1517 snd_pcm_stream_lock_irq(substream);
1518 remove_wait_queue(&to_check->sleep, &wait);
1520 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1523 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1524 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1532 snd_pcm_stream_unlock_irq(substream);
1533 up_read(&snd_pcm_link_rwsem);
1534 snd_power_unlock(card);
1542 * Immediately put all linked substreams into SETUP state.
1544 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1546 struct snd_pcm_runtime *runtime;
1547 struct snd_card *card;
1550 if (PCM_RUNTIME_CHECK(substream))
1552 runtime = substream->runtime;
1553 card = substream->pcm->card;
1555 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1556 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1557 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1560 snd_pcm_stream_lock_irq(substream);
1562 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1563 snd_pcm_pause(substream, 0);
1565 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1566 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1567 snd_pcm_stream_unlock_irq(substream);
1573 /* WARNING: Don't forget to fput back the file */
1574 static struct file *snd_pcm_file_fd(int fd)
1577 struct inode *inode;
1583 inode = file->f_path.dentry->d_inode;
1584 if (!S_ISCHR(inode->i_mode) ||
1585 imajor(inode) != snd_major) {
1589 minor = iminor(inode);
1590 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1591 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1601 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1605 struct snd_pcm_file *pcm_file;
1606 struct snd_pcm_substream *substream1;
1608 file = snd_pcm_file_fd(fd);
1611 pcm_file = file->private_data;
1612 substream1 = pcm_file->substream;
1613 down_write(&snd_pcm_link_rwsem);
1614 write_lock_irq(&snd_pcm_link_rwlock);
1615 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1616 substream->runtime->status->state != substream1->runtime->status->state) {
1620 if (snd_pcm_stream_linked(substream1)) {
1624 if (!snd_pcm_stream_linked(substream)) {
1625 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1626 if (substream->group == NULL) {
1630 spin_lock_init(&substream->group->lock);
1631 INIT_LIST_HEAD(&substream->group->substreams);
1632 list_add_tail(&substream->link_list, &substream->group->substreams);
1633 substream->group->count = 1;
1635 list_add_tail(&substream1->link_list, &substream->group->substreams);
1636 substream->group->count++;
1637 substream1->group = substream->group;
1639 write_unlock_irq(&snd_pcm_link_rwlock);
1640 up_write(&snd_pcm_link_rwsem);
1645 static void relink_to_local(struct snd_pcm_substream *substream)
1647 substream->group = &substream->self_group;
1648 INIT_LIST_HEAD(&substream->self_group.substreams);
1649 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1652 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1654 struct snd_pcm_substream *s;
1657 down_write(&snd_pcm_link_rwsem);
1658 write_lock_irq(&snd_pcm_link_rwlock);
1659 if (!snd_pcm_stream_linked(substream)) {
1663 list_del(&substream->link_list);
1664 substream->group->count--;
1665 if (substream->group->count == 1) { /* detach the last stream, too */
1666 snd_pcm_group_for_each_entry(s, substream) {
1670 kfree(substream->group);
1672 relink_to_local(substream);
1674 write_unlock_irq(&snd_pcm_link_rwlock);
1675 up_write(&snd_pcm_link_rwsem);
1682 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
1685 struct snd_interval t;
1686 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1687 hw_param_interval_c(params, rule->deps[1]), &t);
1688 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1691 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1692 struct snd_pcm_hw_rule *rule)
1694 struct snd_interval t;
1695 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1696 hw_param_interval_c(params, rule->deps[1]), &t);
1697 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1700 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1701 struct snd_pcm_hw_rule *rule)
1703 struct snd_interval t;
1704 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1705 hw_param_interval_c(params, rule->deps[1]),
1706 (unsigned long) rule->private, &t);
1707 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1710 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1711 struct snd_pcm_hw_rule *rule)
1713 struct snd_interval t;
1714 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1715 (unsigned long) rule->private,
1716 hw_param_interval_c(params, rule->deps[1]), &t);
1717 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1720 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1721 struct snd_pcm_hw_rule *rule)
1724 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1726 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1728 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1730 if (! snd_mask_test(mask, k))
1732 bits = snd_pcm_format_physical_width(k);
1734 continue; /* ignore invalid formats */
1735 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1736 snd_mask_reset(&m, k);
1738 return snd_mask_refine(mask, &m);
1741 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1742 struct snd_pcm_hw_rule *rule)
1744 struct snd_interval t;
1750 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1752 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1754 bits = snd_pcm_format_physical_width(k);
1756 continue; /* ignore invalid formats */
1757 if (t.min > (unsigned)bits)
1759 if (t.max < (unsigned)bits)
1763 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1766 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1767 #error "Change this table"
1770 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1771 48000, 64000, 88200, 96000, 176400, 192000 };
1773 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1774 .count = ARRAY_SIZE(rates),
1778 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1779 struct snd_pcm_hw_rule *rule)
1781 struct snd_pcm_hardware *hw = rule->private;
1782 return snd_interval_list(hw_param_interval(params, rule->var),
1783 snd_pcm_known_rates.count,
1784 snd_pcm_known_rates.list, hw->rates);
1787 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1788 struct snd_pcm_hw_rule *rule)
1790 struct snd_interval t;
1791 struct snd_pcm_substream *substream = rule->private;
1793 t.max = substream->buffer_bytes_max;
1797 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1800 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1802 struct snd_pcm_runtime *runtime = substream->runtime;
1803 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1806 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1807 snd_mask_any(constrs_mask(constrs, k));
1810 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1811 snd_interval_any(constrs_interval(constrs, k));
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1818 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1821 snd_pcm_hw_rule_format, NULL,
1822 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1825 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1826 snd_pcm_hw_rule_sample_bits, NULL,
1827 SNDRV_PCM_HW_PARAM_FORMAT,
1828 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1832 snd_pcm_hw_rule_div, NULL,
1833 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1837 snd_pcm_hw_rule_mul, NULL,
1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1842 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1843 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1847 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1848 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1852 snd_pcm_hw_rule_div, NULL,
1853 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1857 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1858 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1863 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1867 snd_pcm_hw_rule_div, NULL,
1868 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1872 snd_pcm_hw_rule_div, NULL,
1873 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1882 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1887 snd_pcm_hw_rule_mul, NULL,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1897 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1898 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1902 snd_pcm_hw_rule_muldivk, (void*) 8,
1903 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1907 snd_pcm_hw_rule_muldivk, (void*) 8,
1908 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1912 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1913 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1917 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1918 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1924 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1926 struct snd_pcm_runtime *runtime = substream->runtime;
1927 struct snd_pcm_hardware *hw = &runtime->hw;
1929 unsigned int mask = 0;
1931 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1932 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1933 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1934 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1935 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1936 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1937 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1938 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1939 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1940 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1941 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1943 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1947 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1951 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1956 hw->channels_min, hw->channels_max);
1960 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1961 hw->rate_min, hw->rate_max);
1965 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1966 hw->period_bytes_min, hw->period_bytes_max);
1970 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1971 hw->periods_min, hw->periods_max);
1975 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1976 hw->period_bytes_min, hw->buffer_bytes_max);
1980 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1981 snd_pcm_hw_rule_buffer_bytes_max, substream,
1982 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1987 if (runtime->dma_bytes) {
1988 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1993 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1994 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1995 snd_pcm_hw_rule_rate, hw,
1996 SNDRV_PCM_HW_PARAM_RATE, -1);
2001 /* FIXME: this belong to lowlevel */
2002 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2007 static void pcm_release_private(struct snd_pcm_substream *substream)
2009 snd_pcm_unlink(substream);
2012 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2014 substream->ref_count--;
2015 if (substream->ref_count > 0)
2018 snd_pcm_drop(substream);
2019 if (substream->hw_opened) {
2020 if (substream->ops->hw_free != NULL)
2021 substream->ops->hw_free(substream);
2022 substream->ops->close(substream);
2023 substream->hw_opened = 0;
2025 if (substream->pcm_release) {
2026 substream->pcm_release(substream);
2027 substream->pcm_release = NULL;
2029 snd_pcm_detach_substream(substream);
2032 EXPORT_SYMBOL(snd_pcm_release_substream);
2034 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2036 struct snd_pcm_substream **rsubstream)
2038 struct snd_pcm_substream *substream;
2041 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2044 if (substream->ref_count > 1) {
2045 *rsubstream = substream;
2049 err = snd_pcm_hw_constraints_init(substream);
2051 snd_printd("snd_pcm_hw_constraints_init failed\n");
2055 if ((err = substream->ops->open(substream)) < 0)
2058 substream->hw_opened = 1;
2060 err = snd_pcm_hw_constraints_complete(substream);
2062 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2066 *rsubstream = substream;
2070 snd_pcm_release_substream(substream);
2074 EXPORT_SYMBOL(snd_pcm_open_substream);
2076 static int snd_pcm_open_file(struct file *file,
2077 struct snd_pcm *pcm,
2079 struct snd_pcm_file **rpcm_file)
2081 struct snd_pcm_file *pcm_file;
2082 struct snd_pcm_substream *substream;
2083 struct snd_pcm_str *str;
2089 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2093 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2094 if (pcm_file == NULL) {
2095 snd_pcm_release_substream(substream);
2098 pcm_file->substream = substream;
2099 if (substream->ref_count == 1) {
2100 str = substream->pstr;
2101 substream->file = pcm_file;
2102 substream->pcm_release = pcm_release_private;
2104 file->private_data = pcm_file;
2106 *rpcm_file = pcm_file;
2110 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2112 struct snd_pcm *pcm;
2113 int err = nonseekable_open(inode, file);
2116 pcm = snd_lookup_minor_data(iminor(inode),
2117 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2118 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2121 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2123 struct snd_pcm *pcm;
2124 int err = nonseekable_open(inode, file);
2127 pcm = snd_lookup_minor_data(iminor(inode),
2128 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2129 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2132 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2135 struct snd_pcm_file *pcm_file;
2142 err = snd_card_file_add(pcm->card, file);
2145 if (!try_module_get(pcm->card->module)) {
2149 init_waitqueue_entry(&wait, current);
2150 add_wait_queue(&pcm->open_wait, &wait);
2151 mutex_lock(&pcm->open_mutex);
2153 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2156 if (err == -EAGAIN) {
2157 if (file->f_flags & O_NONBLOCK) {
2163 set_current_state(TASK_INTERRUPTIBLE);
2164 mutex_unlock(&pcm->open_mutex);
2166 mutex_lock(&pcm->open_mutex);
2167 if (signal_pending(current)) {
2172 remove_wait_queue(&pcm->open_wait, &wait);
2173 mutex_unlock(&pcm->open_mutex);
2179 module_put(pcm->card->module);
2181 snd_card_file_remove(pcm->card, file);
2186 static int snd_pcm_release(struct inode *inode, struct file *file)
2188 struct snd_pcm *pcm;
2189 struct snd_pcm_substream *substream;
2190 struct snd_pcm_file *pcm_file;
2192 pcm_file = file->private_data;
2193 substream = pcm_file->substream;
2194 if (snd_BUG_ON(!substream))
2196 pcm = substream->pcm;
2197 mutex_lock(&pcm->open_mutex);
2198 snd_pcm_release_substream(substream);
2200 mutex_unlock(&pcm->open_mutex);
2201 wake_up(&pcm->open_wait);
2202 module_put(pcm->card->module);
2203 snd_card_file_remove(pcm->card, file);
2207 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2208 snd_pcm_uframes_t frames)
2210 struct snd_pcm_runtime *runtime = substream->runtime;
2211 snd_pcm_sframes_t appl_ptr;
2212 snd_pcm_sframes_t ret;
2213 snd_pcm_sframes_t hw_avail;
2218 snd_pcm_stream_lock_irq(substream);
2219 switch (runtime->status->state) {
2220 case SNDRV_PCM_STATE_PREPARED:
2222 case SNDRV_PCM_STATE_DRAINING:
2223 case SNDRV_PCM_STATE_RUNNING:
2224 if (snd_pcm_update_hw_ptr(substream) >= 0)
2227 case SNDRV_PCM_STATE_XRUN:
2230 case SNDRV_PCM_STATE_SUSPENDED:
2238 hw_avail = snd_pcm_playback_hw_avail(runtime);
2239 if (hw_avail <= 0) {
2243 if (frames > (snd_pcm_uframes_t)hw_avail)
2245 appl_ptr = runtime->control->appl_ptr - frames;
2247 appl_ptr += runtime->boundary;
2248 runtime->control->appl_ptr = appl_ptr;
2251 snd_pcm_stream_unlock_irq(substream);
2255 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2256 snd_pcm_uframes_t frames)
2258 struct snd_pcm_runtime *runtime = substream->runtime;
2259 snd_pcm_sframes_t appl_ptr;
2260 snd_pcm_sframes_t ret;
2261 snd_pcm_sframes_t hw_avail;
2266 snd_pcm_stream_lock_irq(substream);
2267 switch (runtime->status->state) {
2268 case SNDRV_PCM_STATE_PREPARED:
2269 case SNDRV_PCM_STATE_DRAINING:
2271 case SNDRV_PCM_STATE_RUNNING:
2272 if (snd_pcm_update_hw_ptr(substream) >= 0)
2275 case SNDRV_PCM_STATE_XRUN:
2278 case SNDRV_PCM_STATE_SUSPENDED:
2286 hw_avail = snd_pcm_capture_hw_avail(runtime);
2287 if (hw_avail <= 0) {
2291 if (frames > (snd_pcm_uframes_t)hw_avail)
2293 appl_ptr = runtime->control->appl_ptr - frames;
2295 appl_ptr += runtime->boundary;
2296 runtime->control->appl_ptr = appl_ptr;
2299 snd_pcm_stream_unlock_irq(substream);
2303 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2304 snd_pcm_uframes_t frames)
2306 struct snd_pcm_runtime *runtime = substream->runtime;
2307 snd_pcm_sframes_t appl_ptr;
2308 snd_pcm_sframes_t ret;
2309 snd_pcm_sframes_t avail;
2314 snd_pcm_stream_lock_irq(substream);
2315 switch (runtime->status->state) {
2316 case SNDRV_PCM_STATE_PREPARED:
2317 case SNDRV_PCM_STATE_PAUSED:
2319 case SNDRV_PCM_STATE_DRAINING:
2320 case SNDRV_PCM_STATE_RUNNING:
2321 if (snd_pcm_update_hw_ptr(substream) >= 0)
2324 case SNDRV_PCM_STATE_XRUN:
2327 case SNDRV_PCM_STATE_SUSPENDED:
2335 avail = snd_pcm_playback_avail(runtime);
2340 if (frames > (snd_pcm_uframes_t)avail)
2342 appl_ptr = runtime->control->appl_ptr + frames;
2343 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2344 appl_ptr -= runtime->boundary;
2345 runtime->control->appl_ptr = appl_ptr;
2348 snd_pcm_stream_unlock_irq(substream);
2352 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2353 snd_pcm_uframes_t frames)
2355 struct snd_pcm_runtime *runtime = substream->runtime;
2356 snd_pcm_sframes_t appl_ptr;
2357 snd_pcm_sframes_t ret;
2358 snd_pcm_sframes_t avail;
2363 snd_pcm_stream_lock_irq(substream);
2364 switch (runtime->status->state) {
2365 case SNDRV_PCM_STATE_PREPARED:
2366 case SNDRV_PCM_STATE_DRAINING:
2367 case SNDRV_PCM_STATE_PAUSED:
2369 case SNDRV_PCM_STATE_RUNNING:
2370 if (snd_pcm_update_hw_ptr(substream) >= 0)
2373 case SNDRV_PCM_STATE_XRUN:
2376 case SNDRV_PCM_STATE_SUSPENDED:
2384 avail = snd_pcm_capture_avail(runtime);
2389 if (frames > (snd_pcm_uframes_t)avail)
2391 appl_ptr = runtime->control->appl_ptr + frames;
2392 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2393 appl_ptr -= runtime->boundary;
2394 runtime->control->appl_ptr = appl_ptr;
2397 snd_pcm_stream_unlock_irq(substream);
2401 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2403 struct snd_pcm_runtime *runtime = substream->runtime;
2406 snd_pcm_stream_lock_irq(substream);
2407 switch (runtime->status->state) {
2408 case SNDRV_PCM_STATE_DRAINING:
2409 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2411 case SNDRV_PCM_STATE_RUNNING:
2412 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2415 case SNDRV_PCM_STATE_PREPARED:
2416 case SNDRV_PCM_STATE_SUSPENDED:
2419 case SNDRV_PCM_STATE_XRUN:
2427 snd_pcm_stream_unlock_irq(substream);
2431 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2432 snd_pcm_sframes_t __user *res)
2434 struct snd_pcm_runtime *runtime = substream->runtime;
2436 snd_pcm_sframes_t n = 0;
2438 snd_pcm_stream_lock_irq(substream);
2439 switch (runtime->status->state) {
2440 case SNDRV_PCM_STATE_DRAINING:
2441 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2443 case SNDRV_PCM_STATE_RUNNING:
2444 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2447 case SNDRV_PCM_STATE_PREPARED:
2448 case SNDRV_PCM_STATE_SUSPENDED:
2450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2451 n = snd_pcm_playback_hw_avail(runtime);
2453 n = snd_pcm_capture_avail(runtime);
2454 n += runtime->delay;
2456 case SNDRV_PCM_STATE_XRUN:
2464 snd_pcm_stream_unlock_irq(substream);
2466 if (put_user(n, res))
2471 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2472 struct snd_pcm_sync_ptr __user *_sync_ptr)
2474 struct snd_pcm_runtime *runtime = substream->runtime;
2475 struct snd_pcm_sync_ptr sync_ptr;
2476 volatile struct snd_pcm_mmap_status *status;
2477 volatile struct snd_pcm_mmap_control *control;
2480 memset(&sync_ptr, 0, sizeof(sync_ptr));
2481 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2483 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2485 status = runtime->status;
2486 control = runtime->control;
2487 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2488 err = snd_pcm_hwsync(substream);
2492 snd_pcm_stream_lock_irq(substream);
2493 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2494 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2496 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2497 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2498 control->avail_min = sync_ptr.c.control.avail_min;
2500 sync_ptr.c.control.avail_min = control->avail_min;
2501 sync_ptr.s.status.state = status->state;
2502 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2503 sync_ptr.s.status.tstamp = status->tstamp;
2504 sync_ptr.s.status.suspended_state = status->suspended_state;
2505 snd_pcm_stream_unlock_irq(substream);
2506 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2511 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2513 struct snd_pcm_runtime *runtime = substream->runtime;
2516 if (get_user(arg, _arg))
2518 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2520 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2521 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2522 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2526 static int snd_pcm_common_ioctl1(struct file *file,
2527 struct snd_pcm_substream *substream,
2528 unsigned int cmd, void __user *arg)
2531 case SNDRV_PCM_IOCTL_PVERSION:
2532 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2533 case SNDRV_PCM_IOCTL_INFO:
2534 return snd_pcm_info_user(substream, arg);
2535 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2537 case SNDRV_PCM_IOCTL_TTSTAMP:
2538 return snd_pcm_tstamp(substream, arg);
2539 case SNDRV_PCM_IOCTL_HW_REFINE:
2540 return snd_pcm_hw_refine_user(substream, arg);
2541 case SNDRV_PCM_IOCTL_HW_PARAMS:
2542 return snd_pcm_hw_params_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_HW_FREE:
2544 return snd_pcm_hw_free(substream);
2545 case SNDRV_PCM_IOCTL_SW_PARAMS:
2546 return snd_pcm_sw_params_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_STATUS:
2548 return snd_pcm_status_user(substream, arg);
2549 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2550 return snd_pcm_channel_info_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_PREPARE:
2552 return snd_pcm_prepare(substream, file);
2553 case SNDRV_PCM_IOCTL_RESET:
2554 return snd_pcm_reset(substream);
2555 case SNDRV_PCM_IOCTL_START:
2556 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2557 case SNDRV_PCM_IOCTL_LINK:
2558 return snd_pcm_link(substream, (int)(unsigned long) arg);
2559 case SNDRV_PCM_IOCTL_UNLINK:
2560 return snd_pcm_unlink(substream);
2561 case SNDRV_PCM_IOCTL_RESUME:
2562 return snd_pcm_resume(substream);
2563 case SNDRV_PCM_IOCTL_XRUN:
2564 return snd_pcm_xrun(substream);
2565 case SNDRV_PCM_IOCTL_HWSYNC:
2566 return snd_pcm_hwsync(substream);
2567 case SNDRV_PCM_IOCTL_DELAY:
2568 return snd_pcm_delay(substream, arg);
2569 case SNDRV_PCM_IOCTL_SYNC_PTR:
2570 return snd_pcm_sync_ptr(substream, arg);
2571 #ifdef CONFIG_SND_SUPPORT_OLD_API
2572 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2573 return snd_pcm_hw_refine_old_user(substream, arg);
2574 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2575 return snd_pcm_hw_params_old_user(substream, arg);
2577 case SNDRV_PCM_IOCTL_DRAIN:
2578 return snd_pcm_drain(substream, file);
2579 case SNDRV_PCM_IOCTL_DROP:
2580 return snd_pcm_drop(substream);
2581 case SNDRV_PCM_IOCTL_PAUSE:
2584 snd_pcm_stream_lock_irq(substream);
2585 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2586 snd_pcm_stream_unlock_irq(substream);
2590 snd_printd("unknown ioctl = 0x%x\n", cmd);
2594 static int snd_pcm_playback_ioctl1(struct file *file,
2595 struct snd_pcm_substream *substream,
2596 unsigned int cmd, void __user *arg)
2598 if (snd_BUG_ON(!substream))
2600 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2603 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2605 struct snd_xferi xferi;
2606 struct snd_xferi __user *_xferi = arg;
2607 struct snd_pcm_runtime *runtime = substream->runtime;
2608 snd_pcm_sframes_t result;
2609 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2611 if (put_user(0, &_xferi->result))
2613 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2615 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2616 __put_user(result, &_xferi->result);
2617 return result < 0 ? result : 0;
2619 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2621 struct snd_xfern xfern;
2622 struct snd_xfern __user *_xfern = arg;
2623 struct snd_pcm_runtime *runtime = substream->runtime;
2625 snd_pcm_sframes_t result;
2626 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2628 if (runtime->channels > 128)
2630 if (put_user(0, &_xfern->result))
2632 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2635 bufs = memdup_user(xfern.bufs,
2636 sizeof(void *) * runtime->channels);
2638 return PTR_ERR(bufs);
2639 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2641 __put_user(result, &_xfern->result);
2642 return result < 0 ? result : 0;
2644 case SNDRV_PCM_IOCTL_REWIND:
2646 snd_pcm_uframes_t frames;
2647 snd_pcm_uframes_t __user *_frames = arg;
2648 snd_pcm_sframes_t result;
2649 if (get_user(frames, _frames))
2651 if (put_user(0, _frames))
2653 result = snd_pcm_playback_rewind(substream, frames);
2654 __put_user(result, _frames);
2655 return result < 0 ? result : 0;
2657 case SNDRV_PCM_IOCTL_FORWARD:
2659 snd_pcm_uframes_t frames;
2660 snd_pcm_uframes_t __user *_frames = arg;
2661 snd_pcm_sframes_t result;
2662 if (get_user(frames, _frames))
2664 if (put_user(0, _frames))
2666 result = snd_pcm_playback_forward(substream, frames);
2667 __put_user(result, _frames);
2668 return result < 0 ? result : 0;
2671 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2674 static int snd_pcm_capture_ioctl1(struct file *file,
2675 struct snd_pcm_substream *substream,
2676 unsigned int cmd, void __user *arg)
2678 if (snd_BUG_ON(!substream))
2680 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2683 case SNDRV_PCM_IOCTL_READI_FRAMES:
2685 struct snd_xferi xferi;
2686 struct snd_xferi __user *_xferi = arg;
2687 struct snd_pcm_runtime *runtime = substream->runtime;
2688 snd_pcm_sframes_t result;
2689 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2691 if (put_user(0, &_xferi->result))
2693 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2695 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2696 __put_user(result, &_xferi->result);
2697 return result < 0 ? result : 0;
2699 case SNDRV_PCM_IOCTL_READN_FRAMES:
2701 struct snd_xfern xfern;
2702 struct snd_xfern __user *_xfern = arg;
2703 struct snd_pcm_runtime *runtime = substream->runtime;
2705 snd_pcm_sframes_t result;
2706 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2708 if (runtime->channels > 128)
2710 if (put_user(0, &_xfern->result))
2712 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2715 bufs = memdup_user(xfern.bufs,
2716 sizeof(void *) * runtime->channels);
2718 return PTR_ERR(bufs);
2719 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2721 __put_user(result, &_xfern->result);
2722 return result < 0 ? result : 0;
2724 case SNDRV_PCM_IOCTL_REWIND:
2726 snd_pcm_uframes_t frames;
2727 snd_pcm_uframes_t __user *_frames = arg;
2728 snd_pcm_sframes_t result;
2729 if (get_user(frames, _frames))
2731 if (put_user(0, _frames))
2733 result = snd_pcm_capture_rewind(substream, frames);
2734 __put_user(result, _frames);
2735 return result < 0 ? result : 0;
2737 case SNDRV_PCM_IOCTL_FORWARD:
2739 snd_pcm_uframes_t frames;
2740 snd_pcm_uframes_t __user *_frames = arg;
2741 snd_pcm_sframes_t result;
2742 if (get_user(frames, _frames))
2744 if (put_user(0, _frames))
2746 result = snd_pcm_capture_forward(substream, frames);
2747 __put_user(result, _frames);
2748 return result < 0 ? result : 0;
2751 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2754 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2757 struct snd_pcm_file *pcm_file;
2759 pcm_file = file->private_data;
2761 if (((cmd >> 8) & 0xff) != 'A')
2764 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2765 (void __user *)arg);
2768 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2771 struct snd_pcm_file *pcm_file;
2773 pcm_file = file->private_data;
2775 if (((cmd >> 8) & 0xff) != 'A')
2778 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2779 (void __user *)arg);
2782 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2783 unsigned int cmd, void *arg)
2788 fs = snd_enter_user();
2789 switch (substream->stream) {
2790 case SNDRV_PCM_STREAM_PLAYBACK:
2791 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2792 (void __user *)arg);
2794 case SNDRV_PCM_STREAM_CAPTURE:
2795 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2796 (void __user *)arg);
2806 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2808 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2811 struct snd_pcm_file *pcm_file;
2812 struct snd_pcm_substream *substream;
2813 struct snd_pcm_runtime *runtime;
2814 snd_pcm_sframes_t result;
2816 pcm_file = file->private_data;
2817 substream = pcm_file->substream;
2818 if (PCM_RUNTIME_CHECK(substream))
2820 runtime = substream->runtime;
2821 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2823 if (!frame_aligned(runtime, count))
2825 count = bytes_to_frames(runtime, count);
2826 result = snd_pcm_lib_read(substream, buf, count);
2828 result = frames_to_bytes(runtime, result);
2832 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2833 size_t count, loff_t * offset)
2835 struct snd_pcm_file *pcm_file;
2836 struct snd_pcm_substream *substream;
2837 struct snd_pcm_runtime *runtime;
2838 snd_pcm_sframes_t result;
2840 pcm_file = file->private_data;
2841 substream = pcm_file->substream;
2842 if (PCM_RUNTIME_CHECK(substream))
2844 runtime = substream->runtime;
2845 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2847 if (!frame_aligned(runtime, count))
2849 count = bytes_to_frames(runtime, count);
2850 result = snd_pcm_lib_write(substream, buf, count);
2852 result = frames_to_bytes(runtime, result);
2856 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2857 unsigned long nr_segs, loff_t pos)
2860 struct snd_pcm_file *pcm_file;
2861 struct snd_pcm_substream *substream;
2862 struct snd_pcm_runtime *runtime;
2863 snd_pcm_sframes_t result;
2866 snd_pcm_uframes_t frames;
2868 pcm_file = iocb->ki_filp->private_data;
2869 substream = pcm_file->substream;
2870 if (PCM_RUNTIME_CHECK(substream))
2872 runtime = substream->runtime;
2873 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2875 if (nr_segs > 1024 || nr_segs != runtime->channels)
2877 if (!frame_aligned(runtime, iov->iov_len))
2879 frames = bytes_to_samples(runtime, iov->iov_len);
2880 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2883 for (i = 0; i < nr_segs; ++i)
2884 bufs[i] = iov[i].iov_base;
2885 result = snd_pcm_lib_readv(substream, bufs, frames);
2887 result = frames_to_bytes(runtime, result);
2892 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2893 unsigned long nr_segs, loff_t pos)
2895 struct snd_pcm_file *pcm_file;
2896 struct snd_pcm_substream *substream;
2897 struct snd_pcm_runtime *runtime;
2898 snd_pcm_sframes_t result;
2901 snd_pcm_uframes_t frames;
2903 pcm_file = iocb->ki_filp->private_data;
2904 substream = pcm_file->substream;
2905 if (PCM_RUNTIME_CHECK(substream))
2907 runtime = substream->runtime;
2908 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2910 if (nr_segs > 128 || nr_segs != runtime->channels ||
2911 !frame_aligned(runtime, iov->iov_len))
2913 frames = bytes_to_samples(runtime, iov->iov_len);
2914 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2917 for (i = 0; i < nr_segs; ++i)
2918 bufs[i] = iov[i].iov_base;
2919 result = snd_pcm_lib_writev(substream, bufs, frames);
2921 result = frames_to_bytes(runtime, result);
2926 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2928 struct snd_pcm_file *pcm_file;
2929 struct snd_pcm_substream *substream;
2930 struct snd_pcm_runtime *runtime;
2932 snd_pcm_uframes_t avail;
2934 pcm_file = file->private_data;
2936 substream = pcm_file->substream;
2937 if (PCM_RUNTIME_CHECK(substream))
2939 runtime = substream->runtime;
2941 poll_wait(file, &runtime->sleep, wait);
2943 snd_pcm_stream_lock_irq(substream);
2944 avail = snd_pcm_playback_avail(runtime);
2945 switch (runtime->status->state) {
2946 case SNDRV_PCM_STATE_RUNNING:
2947 case SNDRV_PCM_STATE_PREPARED:
2948 case SNDRV_PCM_STATE_PAUSED:
2949 if (avail >= runtime->control->avail_min) {
2950 mask = POLLOUT | POLLWRNORM;
2954 case SNDRV_PCM_STATE_DRAINING:
2958 mask = POLLOUT | POLLWRNORM | POLLERR;
2961 snd_pcm_stream_unlock_irq(substream);
2965 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2967 struct snd_pcm_file *pcm_file;
2968 struct snd_pcm_substream *substream;
2969 struct snd_pcm_runtime *runtime;
2971 snd_pcm_uframes_t avail;
2973 pcm_file = file->private_data;
2975 substream = pcm_file->substream;
2976 if (PCM_RUNTIME_CHECK(substream))
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 int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3021 struct vm_fault *vmf)
3023 struct snd_pcm_substream *substream = area->vm_private_data;
3024 struct snd_pcm_runtime *runtime;
3026 if (substream == NULL)
3027 return VM_FAULT_SIGBUS;
3028 runtime = substream->runtime;
3029 vmf->page = virt_to_page(runtime->status);
3030 get_page(vmf->page);
3034 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3036 .fault = snd_pcm_mmap_status_fault,
3039 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3040 struct vm_area_struct *area)
3042 struct snd_pcm_runtime *runtime;
3044 if (!(area->vm_flags & VM_READ))
3046 runtime = substream->runtime;
3047 size = area->vm_end - area->vm_start;
3048 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3050 area->vm_ops = &snd_pcm_vm_ops_status;
3051 area->vm_private_data = substream;
3052 area->vm_flags |= VM_RESERVED;
3057 * mmap control record
3059 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3060 struct vm_fault *vmf)
3062 struct snd_pcm_substream *substream = area->vm_private_data;
3063 struct snd_pcm_runtime *runtime;
3065 if (substream == NULL)
3066 return VM_FAULT_SIGBUS;
3067 runtime = substream->runtime;
3068 vmf->page = virt_to_page(runtime->control);
3069 get_page(vmf->page);
3073 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3075 .fault = snd_pcm_mmap_control_fault,
3078 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3079 struct vm_area_struct *area)
3081 struct snd_pcm_runtime *runtime;
3083 if (!(area->vm_flags & VM_READ))
3085 runtime = substream->runtime;
3086 size = area->vm_end - area->vm_start;
3087 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3089 area->vm_ops = &snd_pcm_vm_ops_control;
3090 area->vm_private_data = substream;
3091 area->vm_flags |= VM_RESERVED;
3094 #else /* ! coherent mmap */
3096 * don't support mmap for status and control records.
3098 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3099 struct vm_area_struct *area)
3103 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3104 struct vm_area_struct *area)
3108 #endif /* coherent mmap */
3110 static inline struct page *
3111 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3113 void *vaddr = substream->runtime->dma_area + ofs;
3114 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3115 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3116 return virt_to_page(CAC_ADDR(vaddr));
3118 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3119 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3120 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3121 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3122 /* assume dma_handle set via pfn_to_phys() in
3123 * mm/dma-noncoherent.c
3125 return pfn_to_page(addr >> PAGE_SHIFT);
3128 return virt_to_page(vaddr);
3132 * fault callback for mmapping a RAM page
3134 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3135 struct vm_fault *vmf)
3137 struct snd_pcm_substream *substream = area->vm_private_data;
3138 struct snd_pcm_runtime *runtime;
3139 unsigned long offset;
3143 if (substream == NULL)
3144 return VM_FAULT_SIGBUS;
3145 runtime = substream->runtime;
3146 offset = vmf->pgoff << PAGE_SHIFT;
3147 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3148 if (offset > dma_bytes - PAGE_SIZE)
3149 return VM_FAULT_SIGBUS;
3150 if (substream->ops->page)
3151 page = substream->ops->page(substream, offset);
3153 page = snd_pcm_default_page_ops(substream, offset);
3155 return VM_FAULT_SIGBUS;
3161 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3162 .open = snd_pcm_mmap_data_open,
3163 .close = snd_pcm_mmap_data_close,
3166 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3167 .open = snd_pcm_mmap_data_open,
3168 .close = snd_pcm_mmap_data_close,
3169 .fault = snd_pcm_mmap_data_fault,
3172 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3173 /* This should be defined / handled globally! */
3175 #define ARCH_HAS_DMA_MMAP_COHERENT
3180 * mmap the DMA buffer on RAM
3182 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3183 struct vm_area_struct *area)
3185 area->vm_flags |= VM_RESERVED;
3186 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3187 if (!substream->ops->page &&
3188 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3189 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3191 substream->runtime->dma_area,
3192 substream->runtime->dma_addr,
3193 area->vm_end - area->vm_start);
3194 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3195 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3196 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3197 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3198 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3199 /* mmap with fault handler */
3200 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3205 * mmap the DMA buffer on I/O memory area
3207 #if SNDRV_PCM_INFO_MMAP_IOMEM
3208 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3209 struct vm_area_struct *area)
3212 unsigned long offset;
3214 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3215 area->vm_flags |= VM_IO;
3216 size = area->vm_end - area->vm_start;
3217 offset = area->vm_pgoff << PAGE_SHIFT;
3218 if (io_remap_pfn_range(area, area->vm_start,
3219 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3220 size, area->vm_page_prot))
3225 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3226 #endif /* SNDRV_PCM_INFO_MMAP */
3228 /* mmap callback with pgprot_noncached */
3229 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3230 struct vm_area_struct *area)
3232 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3233 return snd_pcm_default_mmap(substream, area);
3235 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3240 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3241 struct vm_area_struct *area)
3243 struct snd_pcm_runtime *runtime;
3245 unsigned long offset;
3249 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3250 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3253 if (!(area->vm_flags & VM_READ))
3256 runtime = substream->runtime;
3257 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3259 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3261 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3262 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3264 size = area->vm_end - area->vm_start;
3265 offset = area->vm_pgoff << PAGE_SHIFT;
3266 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3267 if ((size_t)size > dma_bytes)
3269 if (offset > dma_bytes - size)
3272 area->vm_ops = &snd_pcm_vm_ops_data;
3273 area->vm_private_data = substream;
3274 if (substream->ops->mmap)
3275 err = substream->ops->mmap(substream, area);
3277 err = snd_pcm_default_mmap(substream, area);
3279 atomic_inc(&substream->mmap_count);
3283 EXPORT_SYMBOL(snd_pcm_mmap_data);
3285 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3287 struct snd_pcm_file * pcm_file;
3288 struct snd_pcm_substream *substream;
3289 unsigned long offset;
3291 pcm_file = file->private_data;
3292 substream = pcm_file->substream;
3293 if (PCM_RUNTIME_CHECK(substream))
3296 offset = area->vm_pgoff << PAGE_SHIFT;
3298 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3299 if (pcm_file->no_compat_mmap)
3301 return snd_pcm_mmap_status(substream, file, area);
3302 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3303 if (pcm_file->no_compat_mmap)
3305 return snd_pcm_mmap_control(substream, file, area);
3307 return snd_pcm_mmap_data(substream, file, area);
3312 static int snd_pcm_fasync(int fd, struct file * file, int on)
3314 struct snd_pcm_file * pcm_file;
3315 struct snd_pcm_substream *substream;
3316 struct snd_pcm_runtime *runtime;
3318 pcm_file = file->private_data;
3319 substream = pcm_file->substream;
3320 if (PCM_RUNTIME_CHECK(substream))
3322 runtime = substream->runtime;
3323 return fasync_helper(fd, file, on, &runtime->fasync);
3329 #ifdef CONFIG_COMPAT
3330 #include "pcm_compat.c"
3332 #define snd_pcm_ioctl_compat NULL
3336 * To be removed helpers to keep binary compatibility
3339 #ifdef CONFIG_SND_SUPPORT_OLD_API
3340 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3341 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3343 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3344 struct snd_pcm_hw_params_old *oparams)
3348 memset(params, 0, sizeof(*params));
3349 params->flags = oparams->flags;
3350 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3351 params->masks[i].bits[0] = oparams->masks[i];
3352 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3353 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3354 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3355 params->info = oparams->info;
3356 params->msbits = oparams->msbits;
3357 params->rate_num = oparams->rate_num;
3358 params->rate_den = oparams->rate_den;
3359 params->fifo_size = oparams->fifo_size;
3362 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3363 struct snd_pcm_hw_params *params)
3367 memset(oparams, 0, sizeof(*oparams));
3368 oparams->flags = params->flags;
3369 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3370 oparams->masks[i] = params->masks[i].bits[0];
3371 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3372 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3373 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3374 oparams->info = params->info;
3375 oparams->msbits = params->msbits;
3376 oparams->rate_num = params->rate_num;
3377 oparams->rate_den = params->rate_den;
3378 oparams->fifo_size = params->fifo_size;
3381 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3382 struct snd_pcm_hw_params_old __user * _oparams)
3384 struct snd_pcm_hw_params *params;
3385 struct snd_pcm_hw_params_old *oparams = NULL;
3388 params = kmalloc(sizeof(*params), GFP_KERNEL);
3392 oparams = memdup_user(_oparams, sizeof(*oparams));
3393 if (IS_ERR(oparams)) {
3394 err = PTR_ERR(oparams);
3397 snd_pcm_hw_convert_from_old_params(params, oparams);
3398 err = snd_pcm_hw_refine(substream, params);
3399 snd_pcm_hw_convert_to_old_params(oparams, params);
3400 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3411 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3412 struct snd_pcm_hw_params_old __user * _oparams)
3414 struct snd_pcm_hw_params *params;
3415 struct snd_pcm_hw_params_old *oparams = NULL;
3418 params = kmalloc(sizeof(*params), GFP_KERNEL);
3422 oparams = memdup_user(_oparams, sizeof(*oparams));
3423 if (IS_ERR(oparams)) {
3424 err = PTR_ERR(oparams);
3427 snd_pcm_hw_convert_from_old_params(params, oparams);
3428 err = snd_pcm_hw_params(substream, params);
3429 snd_pcm_hw_convert_to_old_params(oparams, params);
3430 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3440 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3443 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3446 unsigned long pgoff,
3447 unsigned long flags)
3449 struct snd_pcm_file *pcm_file = file->private_data;
3450 struct snd_pcm_substream *substream = pcm_file->substream;
3451 struct snd_pcm_runtime *runtime = substream->runtime;
3452 unsigned long offset = pgoff << PAGE_SHIFT;
3455 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3456 return (unsigned long)runtime->status;
3457 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3458 return (unsigned long)runtime->control;
3460 return (unsigned long)runtime->dma_area + offset;
3464 # define snd_pcm_get_unmapped_area NULL
3471 const struct file_operations snd_pcm_f_ops[2] = {
3473 .owner = THIS_MODULE,
3474 .write = snd_pcm_write,
3475 .aio_write = snd_pcm_aio_write,
3476 .open = snd_pcm_playback_open,
3477 .release = snd_pcm_release,
3478 .llseek = no_llseek,
3479 .poll = snd_pcm_playback_poll,
3480 .unlocked_ioctl = snd_pcm_playback_ioctl,
3481 .compat_ioctl = snd_pcm_ioctl_compat,
3482 .mmap = snd_pcm_mmap,
3483 .fasync = snd_pcm_fasync,
3484 .get_unmapped_area = snd_pcm_get_unmapped_area,
3487 .owner = THIS_MODULE,
3488 .read = snd_pcm_read,
3489 .aio_read = snd_pcm_aio_read,
3490 .open = snd_pcm_capture_open,
3491 .release = snd_pcm_release,
3492 .llseek = no_llseek,
3493 .poll = snd_pcm_capture_poll,
3494 .unlocked_ioctl = snd_pcm_capture_ioctl,
3495 .compat_ioctl = snd_pcm_ioctl_compat,
3496 .mmap = snd_pcm_mmap,
3497 .fasync = snd_pcm_fasync,
3498 .get_unmapped_area = snd_pcm_get_unmapped_area,