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 if (substream->latency_pm_qos_req) {
488 pm_qos_remove_request(substream->latency_pm_qos_req);
489 substream->latency_pm_qos_req = NULL;
491 if ((usecs = period_to_usecs(runtime)) >= 0)
492 substream->latency_pm_qos_req = pm_qos_add_request(
493 PM_QOS_CPU_DMA_LATENCY, usecs);
496 /* hardware might be unuseable from this time,
497 so we force application to retry to set
498 the correct hardware parameter settings */
499 runtime->status->state = SNDRV_PCM_STATE_OPEN;
500 if (substream->ops->hw_free != NULL)
501 substream->ops->hw_free(substream);
505 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
506 struct snd_pcm_hw_params __user * _params)
508 struct snd_pcm_hw_params *params;
511 params = memdup_user(_params, sizeof(*params));
513 return PTR_ERR(params);
515 err = snd_pcm_hw_params(substream, params);
516 if (copy_to_user(_params, params, sizeof(*params))) {
525 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
527 struct snd_pcm_runtime *runtime;
530 if (PCM_RUNTIME_CHECK(substream))
532 runtime = substream->runtime;
533 snd_pcm_stream_lock_irq(substream);
534 switch (runtime->status->state) {
535 case SNDRV_PCM_STATE_SETUP:
536 case SNDRV_PCM_STATE_PREPARED:
539 snd_pcm_stream_unlock_irq(substream);
542 snd_pcm_stream_unlock_irq(substream);
543 if (atomic_read(&substream->mmap_count))
545 if (substream->ops->hw_free)
546 result = substream->ops->hw_free(substream);
547 runtime->status->state = SNDRV_PCM_STATE_OPEN;
548 pm_qos_remove_request(substream->latency_pm_qos_req);
549 substream->latency_pm_qos_req = NULL;
553 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
554 struct snd_pcm_sw_params *params)
556 struct snd_pcm_runtime *runtime;
559 if (PCM_RUNTIME_CHECK(substream))
561 runtime = substream->runtime;
562 snd_pcm_stream_lock_irq(substream);
563 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
564 snd_pcm_stream_unlock_irq(substream);
567 snd_pcm_stream_unlock_irq(substream);
569 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
571 if (params->avail_min == 0)
573 if (params->silence_size >= runtime->boundary) {
574 if (params->silence_threshold != 0)
577 if (params->silence_size > params->silence_threshold)
579 if (params->silence_threshold > runtime->buffer_size)
583 snd_pcm_stream_lock_irq(substream);
584 runtime->tstamp_mode = params->tstamp_mode;
585 runtime->period_step = params->period_step;
586 runtime->control->avail_min = params->avail_min;
587 runtime->start_threshold = params->start_threshold;
588 runtime->stop_threshold = params->stop_threshold;
589 runtime->silence_threshold = params->silence_threshold;
590 runtime->silence_size = params->silence_size;
591 params->boundary = runtime->boundary;
592 if (snd_pcm_running(substream)) {
593 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
594 runtime->silence_size > 0)
595 snd_pcm_playback_silence(substream, ULONG_MAX);
596 err = snd_pcm_update_state(substream, runtime);
598 snd_pcm_stream_unlock_irq(substream);
602 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
603 struct snd_pcm_sw_params __user * _params)
605 struct snd_pcm_sw_params params;
607 if (copy_from_user(¶ms, _params, sizeof(params)))
609 err = snd_pcm_sw_params(substream, ¶ms);
610 if (copy_to_user(_params, ¶ms, sizeof(params)))
615 int snd_pcm_status(struct snd_pcm_substream *substream,
616 struct snd_pcm_status *status)
618 struct snd_pcm_runtime *runtime = substream->runtime;
620 snd_pcm_stream_lock_irq(substream);
621 status->state = runtime->status->state;
622 status->suspended_state = runtime->status->suspended_state;
623 if (status->state == SNDRV_PCM_STATE_OPEN)
625 status->trigger_tstamp = runtime->trigger_tstamp;
626 if (snd_pcm_running(substream)) {
627 snd_pcm_update_hw_ptr(substream);
628 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
629 status->tstamp = runtime->status->tstamp;
633 snd_pcm_gettime(runtime, &status->tstamp);
635 status->appl_ptr = runtime->control->appl_ptr;
636 status->hw_ptr = runtime->status->hw_ptr;
637 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
638 status->avail = snd_pcm_playback_avail(runtime);
639 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
640 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
641 status->delay = runtime->buffer_size - status->avail;
642 status->delay += runtime->delay;
646 status->avail = snd_pcm_capture_avail(runtime);
647 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
648 status->delay = status->avail + runtime->delay;
652 status->avail_max = runtime->avail_max;
653 status->overrange = runtime->overrange;
654 runtime->avail_max = 0;
655 runtime->overrange = 0;
657 snd_pcm_stream_unlock_irq(substream);
661 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
662 struct snd_pcm_status __user * _status)
664 struct snd_pcm_status status;
667 memset(&status, 0, sizeof(status));
668 res = snd_pcm_status(substream, &status);
671 if (copy_to_user(_status, &status, sizeof(status)))
676 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
677 struct snd_pcm_channel_info * info)
679 struct snd_pcm_runtime *runtime;
680 unsigned int channel;
682 channel = info->channel;
683 runtime = substream->runtime;
684 snd_pcm_stream_lock_irq(substream);
685 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
686 snd_pcm_stream_unlock_irq(substream);
689 snd_pcm_stream_unlock_irq(substream);
690 if (channel >= runtime->channels)
692 memset(info, 0, sizeof(*info));
693 info->channel = channel;
694 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
697 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
698 struct snd_pcm_channel_info __user * _info)
700 struct snd_pcm_channel_info info;
703 if (copy_from_user(&info, _info, sizeof(info)))
705 res = snd_pcm_channel_info(substream, &info);
708 if (copy_to_user(_info, &info, sizeof(info)))
713 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
715 struct snd_pcm_runtime *runtime = substream->runtime;
716 if (runtime->trigger_master == NULL)
718 if (runtime->trigger_master == substream) {
719 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
721 snd_pcm_trigger_tstamp(runtime->trigger_master);
722 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
724 runtime->trigger_master = NULL;
728 int (*pre_action)(struct snd_pcm_substream *substream, int state);
729 int (*do_action)(struct snd_pcm_substream *substream, int state);
730 void (*undo_action)(struct snd_pcm_substream *substream, int state);
731 void (*post_action)(struct snd_pcm_substream *substream, int state);
735 * this functions is core for handling of linked stream
736 * Note: the stream state might be changed also on failure
737 * Note2: call with calling stream lock + link lock
739 static int snd_pcm_action_group(struct action_ops *ops,
740 struct snd_pcm_substream *substream,
741 int state, int do_lock)
743 struct snd_pcm_substream *s = NULL;
744 struct snd_pcm_substream *s1;
747 snd_pcm_group_for_each_entry(s, substream) {
748 if (do_lock && s != substream)
749 spin_lock_nested(&s->self_group.lock,
750 SINGLE_DEPTH_NESTING);
751 res = ops->pre_action(s, state);
755 snd_pcm_group_for_each_entry(s, substream) {
756 res = ops->do_action(s, state);
758 if (ops->undo_action) {
759 snd_pcm_group_for_each_entry(s1, substream) {
760 if (s1 == s) /* failed stream */
762 ops->undo_action(s1, state);
765 s = NULL; /* unlock all */
769 snd_pcm_group_for_each_entry(s, substream) {
770 ops->post_action(s, state);
775 snd_pcm_group_for_each_entry(s1, substream) {
777 spin_unlock(&s1->self_group.lock);
778 if (s1 == s) /* end */
786 * Note: call with stream lock
788 static int snd_pcm_action_single(struct action_ops *ops,
789 struct snd_pcm_substream *substream,
794 res = ops->pre_action(substream, state);
797 res = ops->do_action(substream, state);
799 ops->post_action(substream, state);
800 else if (ops->undo_action)
801 ops->undo_action(substream, state);
806 * Note: call with stream lock
808 static int snd_pcm_action(struct action_ops *ops,
809 struct snd_pcm_substream *substream,
814 if (snd_pcm_stream_linked(substream)) {
815 if (!spin_trylock(&substream->group->lock)) {
816 spin_unlock(&substream->self_group.lock);
817 spin_lock(&substream->group->lock);
818 spin_lock(&substream->self_group.lock);
820 res = snd_pcm_action_group(ops, substream, state, 1);
821 spin_unlock(&substream->group->lock);
823 res = snd_pcm_action_single(ops, substream, state);
829 * Note: don't use any locks before
831 static int snd_pcm_action_lock_irq(struct action_ops *ops,
832 struct snd_pcm_substream *substream,
837 read_lock_irq(&snd_pcm_link_rwlock);
838 if (snd_pcm_stream_linked(substream)) {
839 spin_lock(&substream->group->lock);
840 spin_lock(&substream->self_group.lock);
841 res = snd_pcm_action_group(ops, substream, state, 1);
842 spin_unlock(&substream->self_group.lock);
843 spin_unlock(&substream->group->lock);
845 spin_lock(&substream->self_group.lock);
846 res = snd_pcm_action_single(ops, substream, state);
847 spin_unlock(&substream->self_group.lock);
849 read_unlock_irq(&snd_pcm_link_rwlock);
855 static int snd_pcm_action_nonatomic(struct action_ops *ops,
856 struct snd_pcm_substream *substream,
861 down_read(&snd_pcm_link_rwsem);
862 if (snd_pcm_stream_linked(substream))
863 res = snd_pcm_action_group(ops, substream, state, 0);
865 res = snd_pcm_action_single(ops, substream, state);
866 up_read(&snd_pcm_link_rwsem);
873 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
875 struct snd_pcm_runtime *runtime = substream->runtime;
876 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
878 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
879 !snd_pcm_playback_data(substream))
881 runtime->trigger_master = substream;
885 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
887 if (substream->runtime->trigger_master != substream)
889 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
892 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
894 if (substream->runtime->trigger_master == substream)
895 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
898 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
900 struct snd_pcm_runtime *runtime = substream->runtime;
901 snd_pcm_trigger_tstamp(substream);
902 runtime->hw_ptr_jiffies = jiffies;
903 runtime->status->state = state;
904 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
905 runtime->silence_size > 0)
906 snd_pcm_playback_silence(substream, ULONG_MAX);
907 if (substream->timer)
908 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
909 &runtime->trigger_tstamp);
912 static struct action_ops snd_pcm_action_start = {
913 .pre_action = snd_pcm_pre_start,
914 .do_action = snd_pcm_do_start,
915 .undo_action = snd_pcm_undo_start,
916 .post_action = snd_pcm_post_start
920 * snd_pcm_start - start all linked streams
921 * @substream: the PCM substream instance
923 int snd_pcm_start(struct snd_pcm_substream *substream)
925 return snd_pcm_action(&snd_pcm_action_start, substream,
926 SNDRV_PCM_STATE_RUNNING);
932 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
934 struct snd_pcm_runtime *runtime = substream->runtime;
935 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
937 runtime->trigger_master = substream;
941 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
943 if (substream->runtime->trigger_master == substream &&
944 snd_pcm_running(substream))
945 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
946 return 0; /* unconditonally stop all substreams */
949 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
951 struct snd_pcm_runtime *runtime = substream->runtime;
952 if (runtime->status->state != state) {
953 snd_pcm_trigger_tstamp(substream);
954 if (substream->timer)
955 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
956 &runtime->trigger_tstamp);
957 runtime->status->state = state;
959 wake_up(&runtime->sleep);
960 wake_up(&runtime->tsleep);
963 static struct action_ops snd_pcm_action_stop = {
964 .pre_action = snd_pcm_pre_stop,
965 .do_action = snd_pcm_do_stop,
966 .post_action = snd_pcm_post_stop
970 * snd_pcm_stop - try to stop all running streams in the substream group
971 * @substream: the PCM substream instance
972 * @state: PCM state after stopping the stream
974 * The state of each stream is then changed to the given state unconditionally.
976 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
978 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
981 EXPORT_SYMBOL(snd_pcm_stop);
984 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
985 * @substream: the PCM substream
987 * After stopping, the state is changed to SETUP.
988 * Unlike snd_pcm_stop(), this affects only the given stream.
990 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
992 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
993 SNDRV_PCM_STATE_SETUP);
999 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1001 struct snd_pcm_runtime *runtime = substream->runtime;
1002 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1005 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1007 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1009 runtime->trigger_master = substream;
1013 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1015 if (substream->runtime->trigger_master != substream)
1017 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1018 * a delta betwen the current jiffies, this gives a large enough
1019 * delta, effectively to skip the check once.
1021 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1022 return substream->ops->trigger(substream,
1023 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1024 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1027 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1029 if (substream->runtime->trigger_master == substream)
1030 substream->ops->trigger(substream,
1031 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1032 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1035 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1037 struct snd_pcm_runtime *runtime = substream->runtime;
1038 snd_pcm_trigger_tstamp(substream);
1040 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1041 if (substream->timer)
1042 snd_timer_notify(substream->timer,
1043 SNDRV_TIMER_EVENT_MPAUSE,
1044 &runtime->trigger_tstamp);
1045 wake_up(&runtime->sleep);
1046 wake_up(&runtime->tsleep);
1048 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1049 if (substream->timer)
1050 snd_timer_notify(substream->timer,
1051 SNDRV_TIMER_EVENT_MCONTINUE,
1052 &runtime->trigger_tstamp);
1056 static struct action_ops snd_pcm_action_pause = {
1057 .pre_action = snd_pcm_pre_pause,
1058 .do_action = snd_pcm_do_pause,
1059 .undo_action = snd_pcm_undo_pause,
1060 .post_action = snd_pcm_post_pause
1064 * Push/release the pause for all linked streams.
1066 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1068 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1074 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1076 struct snd_pcm_runtime *runtime = substream->runtime;
1077 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1079 runtime->trigger_master = substream;
1083 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1085 struct snd_pcm_runtime *runtime = substream->runtime;
1086 if (runtime->trigger_master != substream)
1088 if (! snd_pcm_running(substream))
1090 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1091 return 0; /* suspend unconditionally */
1094 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1096 struct snd_pcm_runtime *runtime = substream->runtime;
1097 snd_pcm_trigger_tstamp(substream);
1098 if (substream->timer)
1099 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1100 &runtime->trigger_tstamp);
1101 runtime->status->suspended_state = runtime->status->state;
1102 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1103 wake_up(&runtime->sleep);
1104 wake_up(&runtime->tsleep);
1107 static struct action_ops snd_pcm_action_suspend = {
1108 .pre_action = snd_pcm_pre_suspend,
1109 .do_action = snd_pcm_do_suspend,
1110 .post_action = snd_pcm_post_suspend
1114 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1115 * @substream: the PCM substream
1117 * After this call, all streams are changed to SUSPENDED state.
1119 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1122 unsigned long flags;
1127 snd_pcm_stream_lock_irqsave(substream, flags);
1128 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1129 snd_pcm_stream_unlock_irqrestore(substream, flags);
1133 EXPORT_SYMBOL(snd_pcm_suspend);
1136 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1137 * @pcm: the PCM instance
1139 * After this call, all streams are changed to SUSPENDED state.
1141 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1143 struct snd_pcm_substream *substream;
1144 int stream, err = 0;
1149 for (stream = 0; stream < 2; stream++) {
1150 for (substream = pcm->streams[stream].substream;
1151 substream; substream = substream->next) {
1152 /* FIXME: the open/close code should lock this as well */
1153 if (substream->runtime == NULL)
1155 err = snd_pcm_suspend(substream);
1156 if (err < 0 && err != -EBUSY)
1163 EXPORT_SYMBOL(snd_pcm_suspend_all);
1167 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1169 struct snd_pcm_runtime *runtime = substream->runtime;
1170 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1172 runtime->trigger_master = substream;
1176 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1178 struct snd_pcm_runtime *runtime = substream->runtime;
1179 if (runtime->trigger_master != substream)
1181 /* DMA not running previously? */
1182 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1183 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1184 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1186 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1189 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1191 if (substream->runtime->trigger_master == substream &&
1192 snd_pcm_running(substream))
1193 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1196 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1198 struct snd_pcm_runtime *runtime = substream->runtime;
1199 snd_pcm_trigger_tstamp(substream);
1200 if (substream->timer)
1201 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1202 &runtime->trigger_tstamp);
1203 runtime->status->state = runtime->status->suspended_state;
1206 static struct action_ops snd_pcm_action_resume = {
1207 .pre_action = snd_pcm_pre_resume,
1208 .do_action = snd_pcm_do_resume,
1209 .undo_action = snd_pcm_undo_resume,
1210 .post_action = snd_pcm_post_resume
1213 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1215 struct snd_card *card = substream->pcm->card;
1218 snd_power_lock(card);
1219 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1220 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1221 snd_power_unlock(card);
1227 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1232 #endif /* CONFIG_PM */
1237 * Change the RUNNING stream(s) to XRUN state.
1239 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1241 struct snd_card *card = substream->pcm->card;
1242 struct snd_pcm_runtime *runtime = substream->runtime;
1245 snd_power_lock(card);
1246 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1247 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1252 snd_pcm_stream_lock_irq(substream);
1253 switch (runtime->status->state) {
1254 case SNDRV_PCM_STATE_XRUN:
1255 result = 0; /* already there */
1257 case SNDRV_PCM_STATE_RUNNING:
1258 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1263 snd_pcm_stream_unlock_irq(substream);
1265 snd_power_unlock(card);
1272 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1274 struct snd_pcm_runtime *runtime = substream->runtime;
1275 switch (runtime->status->state) {
1276 case SNDRV_PCM_STATE_RUNNING:
1277 case SNDRV_PCM_STATE_PREPARED:
1278 case SNDRV_PCM_STATE_PAUSED:
1279 case SNDRV_PCM_STATE_SUSPENDED:
1286 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1288 struct snd_pcm_runtime *runtime = substream->runtime;
1289 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1292 runtime->hw_ptr_base = 0;
1293 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1294 runtime->status->hw_ptr % runtime->period_size;
1295 runtime->silence_start = runtime->status->hw_ptr;
1296 runtime->silence_filled = 0;
1300 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1302 struct snd_pcm_runtime *runtime = substream->runtime;
1303 runtime->control->appl_ptr = runtime->status->hw_ptr;
1304 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1305 runtime->silence_size > 0)
1306 snd_pcm_playback_silence(substream, ULONG_MAX);
1309 static struct action_ops snd_pcm_action_reset = {
1310 .pre_action = snd_pcm_pre_reset,
1311 .do_action = snd_pcm_do_reset,
1312 .post_action = snd_pcm_post_reset
1315 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1317 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1323 /* we use the second argument for updating f_flags */
1324 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1327 struct snd_pcm_runtime *runtime = substream->runtime;
1328 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1329 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1331 if (snd_pcm_running(substream))
1333 substream->f_flags = f_flags;
1337 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1340 err = substream->ops->prepare(substream);
1343 return snd_pcm_do_reset(substream, 0);
1346 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1348 struct snd_pcm_runtime *runtime = substream->runtime;
1349 runtime->control->appl_ptr = runtime->status->hw_ptr;
1350 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1353 static struct action_ops snd_pcm_action_prepare = {
1354 .pre_action = snd_pcm_pre_prepare,
1355 .do_action = snd_pcm_do_prepare,
1356 .post_action = snd_pcm_post_prepare
1360 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1361 * @substream: the PCM substream instance
1362 * @file: file to refer f_flags
1364 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1368 struct snd_card *card = substream->pcm->card;
1372 f_flags = file->f_flags;
1374 f_flags = substream->f_flags;
1376 snd_power_lock(card);
1377 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1378 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1379 substream, f_flags);
1380 snd_power_unlock(card);
1388 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1390 substream->runtime->trigger_master = substream;
1394 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1396 struct snd_pcm_runtime *runtime = substream->runtime;
1397 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1398 switch (runtime->status->state) {
1399 case SNDRV_PCM_STATE_PREPARED:
1400 /* start playback stream if possible */
1401 if (! snd_pcm_playback_empty(substream)) {
1402 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1403 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1406 case SNDRV_PCM_STATE_RUNNING:
1407 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1413 /* stop running stream */
1414 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1415 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1416 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1417 snd_pcm_do_stop(substream, new_state);
1418 snd_pcm_post_stop(substream, new_state);
1424 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1428 static struct action_ops snd_pcm_action_drain_init = {
1429 .pre_action = snd_pcm_pre_drain_init,
1430 .do_action = snd_pcm_do_drain_init,
1431 .post_action = snd_pcm_post_drain_init
1434 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1437 * Drain the stream(s).
1438 * When the substream is linked, sync until the draining of all playback streams
1440 * After this call, all streams are supposed to be either SETUP or DRAINING
1441 * (capture only) state.
1443 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1446 struct snd_card *card;
1447 struct snd_pcm_runtime *runtime;
1448 struct snd_pcm_substream *s;
1453 card = substream->pcm->card;
1454 runtime = substream->runtime;
1456 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1459 snd_power_lock(card);
1460 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1461 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1463 snd_power_unlock(card);
1469 if (file->f_flags & O_NONBLOCK)
1471 } else if (substream->f_flags & O_NONBLOCK)
1474 down_read(&snd_pcm_link_rwsem);
1475 snd_pcm_stream_lock_irq(substream);
1477 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1478 snd_pcm_pause(substream, 0);
1480 /* pre-start/stop - all running streams are changed to DRAINING state */
1481 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1484 /* in non-blocking, we don't wait in ioctl but let caller poll */
1492 struct snd_pcm_runtime *to_check;
1493 if (signal_pending(current)) {
1494 result = -ERESTARTSYS;
1497 /* find a substream to drain */
1499 snd_pcm_group_for_each_entry(s, substream) {
1500 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1502 runtime = s->runtime;
1503 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1509 break; /* all drained */
1510 init_waitqueue_entry(&wait, current);
1511 add_wait_queue(&to_check->sleep, &wait);
1512 set_current_state(TASK_INTERRUPTIBLE);
1513 snd_pcm_stream_unlock_irq(substream);
1514 up_read(&snd_pcm_link_rwsem);
1515 snd_power_unlock(card);
1516 tout = schedule_timeout(10 * HZ);
1517 snd_power_lock(card);
1518 down_read(&snd_pcm_link_rwsem);
1519 snd_pcm_stream_lock_irq(substream);
1520 remove_wait_queue(&to_check->sleep, &wait);
1522 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1525 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1526 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1534 snd_pcm_stream_unlock_irq(substream);
1535 up_read(&snd_pcm_link_rwsem);
1536 snd_power_unlock(card);
1544 * Immediately put all linked substreams into SETUP state.
1546 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1548 struct snd_pcm_runtime *runtime;
1549 struct snd_card *card;
1552 if (PCM_RUNTIME_CHECK(substream))
1554 runtime = substream->runtime;
1555 card = substream->pcm->card;
1557 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1558 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1559 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1562 snd_pcm_stream_lock_irq(substream);
1564 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1565 snd_pcm_pause(substream, 0);
1567 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1568 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1569 snd_pcm_stream_unlock_irq(substream);
1575 /* WARNING: Don't forget to fput back the file */
1576 static struct file *snd_pcm_file_fd(int fd)
1579 struct inode *inode;
1585 inode = file->f_path.dentry->d_inode;
1586 if (!S_ISCHR(inode->i_mode) ||
1587 imajor(inode) != snd_major) {
1591 minor = iminor(inode);
1592 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1593 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1603 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1607 struct snd_pcm_file *pcm_file;
1608 struct snd_pcm_substream *substream1;
1610 file = snd_pcm_file_fd(fd);
1613 pcm_file = file->private_data;
1614 substream1 = pcm_file->substream;
1615 down_write(&snd_pcm_link_rwsem);
1616 write_lock_irq(&snd_pcm_link_rwlock);
1617 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1618 substream->runtime->status->state != substream1->runtime->status->state) {
1622 if (snd_pcm_stream_linked(substream1)) {
1626 if (!snd_pcm_stream_linked(substream)) {
1627 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1628 if (substream->group == NULL) {
1632 spin_lock_init(&substream->group->lock);
1633 INIT_LIST_HEAD(&substream->group->substreams);
1634 list_add_tail(&substream->link_list, &substream->group->substreams);
1635 substream->group->count = 1;
1637 list_add_tail(&substream1->link_list, &substream->group->substreams);
1638 substream->group->count++;
1639 substream1->group = substream->group;
1641 write_unlock_irq(&snd_pcm_link_rwlock);
1642 up_write(&snd_pcm_link_rwsem);
1647 static void relink_to_local(struct snd_pcm_substream *substream)
1649 substream->group = &substream->self_group;
1650 INIT_LIST_HEAD(&substream->self_group.substreams);
1651 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1654 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1656 struct snd_pcm_substream *s;
1659 down_write(&snd_pcm_link_rwsem);
1660 write_lock_irq(&snd_pcm_link_rwlock);
1661 if (!snd_pcm_stream_linked(substream)) {
1665 list_del(&substream->link_list);
1666 substream->group->count--;
1667 if (substream->group->count == 1) { /* detach the last stream, too */
1668 snd_pcm_group_for_each_entry(s, substream) {
1672 kfree(substream->group);
1674 relink_to_local(substream);
1676 write_unlock_irq(&snd_pcm_link_rwlock);
1677 up_write(&snd_pcm_link_rwsem);
1684 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1685 struct snd_pcm_hw_rule *rule)
1687 struct snd_interval t;
1688 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1689 hw_param_interval_c(params, rule->deps[1]), &t);
1690 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1693 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1694 struct snd_pcm_hw_rule *rule)
1696 struct snd_interval t;
1697 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1698 hw_param_interval_c(params, rule->deps[1]), &t);
1699 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1702 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1703 struct snd_pcm_hw_rule *rule)
1705 struct snd_interval t;
1706 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1707 hw_param_interval_c(params, rule->deps[1]),
1708 (unsigned long) rule->private, &t);
1709 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1712 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1713 struct snd_pcm_hw_rule *rule)
1715 struct snd_interval t;
1716 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1717 (unsigned long) rule->private,
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_format(struct snd_pcm_hw_params *params,
1723 struct snd_pcm_hw_rule *rule)
1726 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1728 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1730 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1732 if (! snd_mask_test(mask, k))
1734 bits = snd_pcm_format_physical_width(k);
1736 continue; /* ignore invalid formats */
1737 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1738 snd_mask_reset(&m, k);
1740 return snd_mask_refine(mask, &m);
1743 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1744 struct snd_pcm_hw_rule *rule)
1746 struct snd_interval t;
1752 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1754 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1756 bits = snd_pcm_format_physical_width(k);
1758 continue; /* ignore invalid formats */
1759 if (t.min > (unsigned)bits)
1761 if (t.max < (unsigned)bits)
1765 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1768 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1769 #error "Change this table"
1772 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1773 48000, 64000, 88200, 96000, 176400, 192000 };
1775 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1776 .count = ARRAY_SIZE(rates),
1780 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1781 struct snd_pcm_hw_rule *rule)
1783 struct snd_pcm_hardware *hw = rule->private;
1784 return snd_interval_list(hw_param_interval(params, rule->var),
1785 snd_pcm_known_rates.count,
1786 snd_pcm_known_rates.list, hw->rates);
1789 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1790 struct snd_pcm_hw_rule *rule)
1792 struct snd_interval t;
1793 struct snd_pcm_substream *substream = rule->private;
1795 t.max = substream->buffer_bytes_max;
1799 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1802 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1804 struct snd_pcm_runtime *runtime = substream->runtime;
1805 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1808 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1809 snd_mask_any(constrs_mask(constrs, k));
1812 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1813 snd_interval_any(constrs_interval(constrs, k));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1818 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1819 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1820 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1823 snd_pcm_hw_rule_format, NULL,
1824 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1827 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1828 snd_pcm_hw_rule_sample_bits, NULL,
1829 SNDRV_PCM_HW_PARAM_FORMAT,
1830 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1834 snd_pcm_hw_rule_div, NULL,
1835 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1839 snd_pcm_hw_rule_mul, NULL,
1840 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1844 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1845 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1849 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1850 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1854 snd_pcm_hw_rule_div, NULL,
1855 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1859 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1864 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1865 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1869 snd_pcm_hw_rule_div, NULL,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1874 snd_pcm_hw_rule_div, NULL,
1875 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1879 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1880 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1884 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1889 snd_pcm_hw_rule_mul, NULL,
1890 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1893 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1894 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1895 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1898 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1899 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1900 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1903 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1904 snd_pcm_hw_rule_muldivk, (void*) 8,
1905 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1908 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1909 snd_pcm_hw_rule_muldivk, (void*) 8,
1910 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1913 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1914 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1915 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1918 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1919 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1920 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1926 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1928 struct snd_pcm_runtime *runtime = substream->runtime;
1929 struct snd_pcm_hardware *hw = &runtime->hw;
1931 unsigned int mask = 0;
1933 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1934 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1935 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1936 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1937 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1938 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1939 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1940 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1941 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1942 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1943 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1945 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1949 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1953 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1957 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1958 hw->channels_min, hw->channels_max);
1962 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1963 hw->rate_min, hw->rate_max);
1967 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1968 hw->period_bytes_min, hw->period_bytes_max);
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1973 hw->periods_min, hw->periods_max);
1977 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1978 hw->period_bytes_min, hw->buffer_bytes_max);
1982 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1983 snd_pcm_hw_rule_buffer_bytes_max, substream,
1984 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1989 if (runtime->dma_bytes) {
1990 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1995 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1996 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1997 snd_pcm_hw_rule_rate, hw,
1998 SNDRV_PCM_HW_PARAM_RATE, -1);
2003 /* FIXME: this belong to lowlevel */
2004 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2009 static void pcm_release_private(struct snd_pcm_substream *substream)
2011 snd_pcm_unlink(substream);
2014 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2016 substream->ref_count--;
2017 if (substream->ref_count > 0)
2020 snd_pcm_drop(substream);
2021 if (substream->hw_opened) {
2022 if (substream->ops->hw_free != NULL)
2023 substream->ops->hw_free(substream);
2024 substream->ops->close(substream);
2025 substream->hw_opened = 0;
2027 if (substream->pcm_release) {
2028 substream->pcm_release(substream);
2029 substream->pcm_release = NULL;
2031 snd_pcm_detach_substream(substream);
2034 EXPORT_SYMBOL(snd_pcm_release_substream);
2036 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2038 struct snd_pcm_substream **rsubstream)
2040 struct snd_pcm_substream *substream;
2043 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2046 if (substream->ref_count > 1) {
2047 *rsubstream = substream;
2051 err = snd_pcm_hw_constraints_init(substream);
2053 snd_printd("snd_pcm_hw_constraints_init failed\n");
2057 if ((err = substream->ops->open(substream)) < 0)
2060 substream->hw_opened = 1;
2062 err = snd_pcm_hw_constraints_complete(substream);
2064 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2068 *rsubstream = substream;
2072 snd_pcm_release_substream(substream);
2076 EXPORT_SYMBOL(snd_pcm_open_substream);
2078 static int snd_pcm_open_file(struct file *file,
2079 struct snd_pcm *pcm,
2081 struct snd_pcm_file **rpcm_file)
2083 struct snd_pcm_file *pcm_file;
2084 struct snd_pcm_substream *substream;
2085 struct snd_pcm_str *str;
2091 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2095 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2096 if (pcm_file == NULL) {
2097 snd_pcm_release_substream(substream);
2100 pcm_file->substream = substream;
2101 if (substream->ref_count == 1) {
2102 str = substream->pstr;
2103 substream->file = pcm_file;
2104 substream->pcm_release = pcm_release_private;
2106 file->private_data = pcm_file;
2108 *rpcm_file = pcm_file;
2112 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2114 struct snd_pcm *pcm;
2115 int err = nonseekable_open(inode, file);
2118 pcm = snd_lookup_minor_data(iminor(inode),
2119 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2120 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2123 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2125 struct snd_pcm *pcm;
2126 int err = nonseekable_open(inode, file);
2129 pcm = snd_lookup_minor_data(iminor(inode),
2130 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2131 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2134 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2137 struct snd_pcm_file *pcm_file;
2144 err = snd_card_file_add(pcm->card, file);
2147 if (!try_module_get(pcm->card->module)) {
2151 init_waitqueue_entry(&wait, current);
2152 add_wait_queue(&pcm->open_wait, &wait);
2153 mutex_lock(&pcm->open_mutex);
2155 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2158 if (err == -EAGAIN) {
2159 if (file->f_flags & O_NONBLOCK) {
2165 set_current_state(TASK_INTERRUPTIBLE);
2166 mutex_unlock(&pcm->open_mutex);
2168 mutex_lock(&pcm->open_mutex);
2169 if (signal_pending(current)) {
2174 remove_wait_queue(&pcm->open_wait, &wait);
2175 mutex_unlock(&pcm->open_mutex);
2181 module_put(pcm->card->module);
2183 snd_card_file_remove(pcm->card, file);
2188 static int snd_pcm_release(struct inode *inode, struct file *file)
2190 struct snd_pcm *pcm;
2191 struct snd_pcm_substream *substream;
2192 struct snd_pcm_file *pcm_file;
2194 pcm_file = file->private_data;
2195 substream = pcm_file->substream;
2196 if (snd_BUG_ON(!substream))
2198 pcm = substream->pcm;
2199 mutex_lock(&pcm->open_mutex);
2200 snd_pcm_release_substream(substream);
2202 mutex_unlock(&pcm->open_mutex);
2203 wake_up(&pcm->open_wait);
2204 module_put(pcm->card->module);
2205 snd_card_file_remove(pcm->card, file);
2209 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2210 snd_pcm_uframes_t frames)
2212 struct snd_pcm_runtime *runtime = substream->runtime;
2213 snd_pcm_sframes_t appl_ptr;
2214 snd_pcm_sframes_t ret;
2215 snd_pcm_sframes_t hw_avail;
2220 snd_pcm_stream_lock_irq(substream);
2221 switch (runtime->status->state) {
2222 case SNDRV_PCM_STATE_PREPARED:
2224 case SNDRV_PCM_STATE_DRAINING:
2225 case SNDRV_PCM_STATE_RUNNING:
2226 if (snd_pcm_update_hw_ptr(substream) >= 0)
2229 case SNDRV_PCM_STATE_XRUN:
2232 case SNDRV_PCM_STATE_SUSPENDED:
2240 hw_avail = snd_pcm_playback_hw_avail(runtime);
2241 if (hw_avail <= 0) {
2245 if (frames > (snd_pcm_uframes_t)hw_avail)
2247 appl_ptr = runtime->control->appl_ptr - frames;
2249 appl_ptr += runtime->boundary;
2250 runtime->control->appl_ptr = appl_ptr;
2253 snd_pcm_stream_unlock_irq(substream);
2257 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2258 snd_pcm_uframes_t frames)
2260 struct snd_pcm_runtime *runtime = substream->runtime;
2261 snd_pcm_sframes_t appl_ptr;
2262 snd_pcm_sframes_t ret;
2263 snd_pcm_sframes_t hw_avail;
2268 snd_pcm_stream_lock_irq(substream);
2269 switch (runtime->status->state) {
2270 case SNDRV_PCM_STATE_PREPARED:
2271 case SNDRV_PCM_STATE_DRAINING:
2273 case SNDRV_PCM_STATE_RUNNING:
2274 if (snd_pcm_update_hw_ptr(substream) >= 0)
2277 case SNDRV_PCM_STATE_XRUN:
2280 case SNDRV_PCM_STATE_SUSPENDED:
2288 hw_avail = snd_pcm_capture_hw_avail(runtime);
2289 if (hw_avail <= 0) {
2293 if (frames > (snd_pcm_uframes_t)hw_avail)
2295 appl_ptr = runtime->control->appl_ptr - frames;
2297 appl_ptr += runtime->boundary;
2298 runtime->control->appl_ptr = appl_ptr;
2301 snd_pcm_stream_unlock_irq(substream);
2305 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2306 snd_pcm_uframes_t frames)
2308 struct snd_pcm_runtime *runtime = substream->runtime;
2309 snd_pcm_sframes_t appl_ptr;
2310 snd_pcm_sframes_t ret;
2311 snd_pcm_sframes_t avail;
2316 snd_pcm_stream_lock_irq(substream);
2317 switch (runtime->status->state) {
2318 case SNDRV_PCM_STATE_PREPARED:
2319 case SNDRV_PCM_STATE_PAUSED:
2321 case SNDRV_PCM_STATE_DRAINING:
2322 case SNDRV_PCM_STATE_RUNNING:
2323 if (snd_pcm_update_hw_ptr(substream) >= 0)
2326 case SNDRV_PCM_STATE_XRUN:
2329 case SNDRV_PCM_STATE_SUSPENDED:
2337 avail = snd_pcm_playback_avail(runtime);
2342 if (frames > (snd_pcm_uframes_t)avail)
2344 appl_ptr = runtime->control->appl_ptr + frames;
2345 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2346 appl_ptr -= runtime->boundary;
2347 runtime->control->appl_ptr = appl_ptr;
2350 snd_pcm_stream_unlock_irq(substream);
2354 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2355 snd_pcm_uframes_t frames)
2357 struct snd_pcm_runtime *runtime = substream->runtime;
2358 snd_pcm_sframes_t appl_ptr;
2359 snd_pcm_sframes_t ret;
2360 snd_pcm_sframes_t avail;
2365 snd_pcm_stream_lock_irq(substream);
2366 switch (runtime->status->state) {
2367 case SNDRV_PCM_STATE_PREPARED:
2368 case SNDRV_PCM_STATE_DRAINING:
2369 case SNDRV_PCM_STATE_PAUSED:
2371 case SNDRV_PCM_STATE_RUNNING:
2372 if (snd_pcm_update_hw_ptr(substream) >= 0)
2375 case SNDRV_PCM_STATE_XRUN:
2378 case SNDRV_PCM_STATE_SUSPENDED:
2386 avail = snd_pcm_capture_avail(runtime);
2391 if (frames > (snd_pcm_uframes_t)avail)
2393 appl_ptr = runtime->control->appl_ptr + frames;
2394 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2395 appl_ptr -= runtime->boundary;
2396 runtime->control->appl_ptr = appl_ptr;
2399 snd_pcm_stream_unlock_irq(substream);
2403 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2405 struct snd_pcm_runtime *runtime = substream->runtime;
2408 snd_pcm_stream_lock_irq(substream);
2409 switch (runtime->status->state) {
2410 case SNDRV_PCM_STATE_DRAINING:
2411 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2413 case SNDRV_PCM_STATE_RUNNING:
2414 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2417 case SNDRV_PCM_STATE_PREPARED:
2418 case SNDRV_PCM_STATE_SUSPENDED:
2421 case SNDRV_PCM_STATE_XRUN:
2429 snd_pcm_stream_unlock_irq(substream);
2433 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2434 snd_pcm_sframes_t __user *res)
2436 struct snd_pcm_runtime *runtime = substream->runtime;
2438 snd_pcm_sframes_t n = 0;
2440 snd_pcm_stream_lock_irq(substream);
2441 switch (runtime->status->state) {
2442 case SNDRV_PCM_STATE_DRAINING:
2443 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2445 case SNDRV_PCM_STATE_RUNNING:
2446 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2449 case SNDRV_PCM_STATE_PREPARED:
2450 case SNDRV_PCM_STATE_SUSPENDED:
2452 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2453 n = snd_pcm_playback_hw_avail(runtime);
2455 n = snd_pcm_capture_avail(runtime);
2456 n += runtime->delay;
2458 case SNDRV_PCM_STATE_XRUN:
2466 snd_pcm_stream_unlock_irq(substream);
2468 if (put_user(n, res))
2473 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2474 struct snd_pcm_sync_ptr __user *_sync_ptr)
2476 struct snd_pcm_runtime *runtime = substream->runtime;
2477 struct snd_pcm_sync_ptr sync_ptr;
2478 volatile struct snd_pcm_mmap_status *status;
2479 volatile struct snd_pcm_mmap_control *control;
2482 memset(&sync_ptr, 0, sizeof(sync_ptr));
2483 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2485 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2487 status = runtime->status;
2488 control = runtime->control;
2489 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2490 err = snd_pcm_hwsync(substream);
2494 snd_pcm_stream_lock_irq(substream);
2495 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2496 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2498 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2499 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2500 control->avail_min = sync_ptr.c.control.avail_min;
2502 sync_ptr.c.control.avail_min = control->avail_min;
2503 sync_ptr.s.status.state = status->state;
2504 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2505 sync_ptr.s.status.tstamp = status->tstamp;
2506 sync_ptr.s.status.suspended_state = status->suspended_state;
2507 snd_pcm_stream_unlock_irq(substream);
2508 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2513 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2515 struct snd_pcm_runtime *runtime = substream->runtime;
2518 if (get_user(arg, _arg))
2520 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2522 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2523 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2524 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2528 static int snd_pcm_common_ioctl1(struct file *file,
2529 struct snd_pcm_substream *substream,
2530 unsigned int cmd, void __user *arg)
2533 case SNDRV_PCM_IOCTL_PVERSION:
2534 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2535 case SNDRV_PCM_IOCTL_INFO:
2536 return snd_pcm_info_user(substream, arg);
2537 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2539 case SNDRV_PCM_IOCTL_TTSTAMP:
2540 return snd_pcm_tstamp(substream, arg);
2541 case SNDRV_PCM_IOCTL_HW_REFINE:
2542 return snd_pcm_hw_refine_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_HW_PARAMS:
2544 return snd_pcm_hw_params_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_HW_FREE:
2546 return snd_pcm_hw_free(substream);
2547 case SNDRV_PCM_IOCTL_SW_PARAMS:
2548 return snd_pcm_sw_params_user(substream, arg);
2549 case SNDRV_PCM_IOCTL_STATUS:
2550 return snd_pcm_status_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2552 return snd_pcm_channel_info_user(substream, arg);
2553 case SNDRV_PCM_IOCTL_PREPARE:
2554 return snd_pcm_prepare(substream, file);
2555 case SNDRV_PCM_IOCTL_RESET:
2556 return snd_pcm_reset(substream);
2557 case SNDRV_PCM_IOCTL_START:
2558 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2559 case SNDRV_PCM_IOCTL_LINK:
2560 return snd_pcm_link(substream, (int)(unsigned long) arg);
2561 case SNDRV_PCM_IOCTL_UNLINK:
2562 return snd_pcm_unlink(substream);
2563 case SNDRV_PCM_IOCTL_RESUME:
2564 return snd_pcm_resume(substream);
2565 case SNDRV_PCM_IOCTL_XRUN:
2566 return snd_pcm_xrun(substream);
2567 case SNDRV_PCM_IOCTL_HWSYNC:
2568 return snd_pcm_hwsync(substream);
2569 case SNDRV_PCM_IOCTL_DELAY:
2570 return snd_pcm_delay(substream, arg);
2571 case SNDRV_PCM_IOCTL_SYNC_PTR:
2572 return snd_pcm_sync_ptr(substream, arg);
2573 #ifdef CONFIG_SND_SUPPORT_OLD_API
2574 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2575 return snd_pcm_hw_refine_old_user(substream, arg);
2576 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2577 return snd_pcm_hw_params_old_user(substream, arg);
2579 case SNDRV_PCM_IOCTL_DRAIN:
2580 return snd_pcm_drain(substream, file);
2581 case SNDRV_PCM_IOCTL_DROP:
2582 return snd_pcm_drop(substream);
2583 case SNDRV_PCM_IOCTL_PAUSE:
2586 snd_pcm_stream_lock_irq(substream);
2587 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2588 snd_pcm_stream_unlock_irq(substream);
2592 snd_printd("unknown ioctl = 0x%x\n", cmd);
2596 static int snd_pcm_playback_ioctl1(struct file *file,
2597 struct snd_pcm_substream *substream,
2598 unsigned int cmd, void __user *arg)
2600 if (snd_BUG_ON(!substream))
2602 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2605 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2607 struct snd_xferi xferi;
2608 struct snd_xferi __user *_xferi = arg;
2609 struct snd_pcm_runtime *runtime = substream->runtime;
2610 snd_pcm_sframes_t result;
2611 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2613 if (put_user(0, &_xferi->result))
2615 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2617 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2618 __put_user(result, &_xferi->result);
2619 return result < 0 ? result : 0;
2621 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2623 struct snd_xfern xfern;
2624 struct snd_xfern __user *_xfern = arg;
2625 struct snd_pcm_runtime *runtime = substream->runtime;
2627 snd_pcm_sframes_t result;
2628 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2630 if (runtime->channels > 128)
2632 if (put_user(0, &_xfern->result))
2634 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2637 bufs = memdup_user(xfern.bufs,
2638 sizeof(void *) * runtime->channels);
2640 return PTR_ERR(bufs);
2641 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2643 __put_user(result, &_xfern->result);
2644 return result < 0 ? result : 0;
2646 case SNDRV_PCM_IOCTL_REWIND:
2648 snd_pcm_uframes_t frames;
2649 snd_pcm_uframes_t __user *_frames = arg;
2650 snd_pcm_sframes_t result;
2651 if (get_user(frames, _frames))
2653 if (put_user(0, _frames))
2655 result = snd_pcm_playback_rewind(substream, frames);
2656 __put_user(result, _frames);
2657 return result < 0 ? result : 0;
2659 case SNDRV_PCM_IOCTL_FORWARD:
2661 snd_pcm_uframes_t frames;
2662 snd_pcm_uframes_t __user *_frames = arg;
2663 snd_pcm_sframes_t result;
2664 if (get_user(frames, _frames))
2666 if (put_user(0, _frames))
2668 result = snd_pcm_playback_forward(substream, frames);
2669 __put_user(result, _frames);
2670 return result < 0 ? result : 0;
2673 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2676 static int snd_pcm_capture_ioctl1(struct file *file,
2677 struct snd_pcm_substream *substream,
2678 unsigned int cmd, void __user *arg)
2680 if (snd_BUG_ON(!substream))
2682 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2685 case SNDRV_PCM_IOCTL_READI_FRAMES:
2687 struct snd_xferi xferi;
2688 struct snd_xferi __user *_xferi = arg;
2689 struct snd_pcm_runtime *runtime = substream->runtime;
2690 snd_pcm_sframes_t result;
2691 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2693 if (put_user(0, &_xferi->result))
2695 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2697 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2698 __put_user(result, &_xferi->result);
2699 return result < 0 ? result : 0;
2701 case SNDRV_PCM_IOCTL_READN_FRAMES:
2703 struct snd_xfern xfern;
2704 struct snd_xfern __user *_xfern = arg;
2705 struct snd_pcm_runtime *runtime = substream->runtime;
2707 snd_pcm_sframes_t result;
2708 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2710 if (runtime->channels > 128)
2712 if (put_user(0, &_xfern->result))
2714 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2717 bufs = memdup_user(xfern.bufs,
2718 sizeof(void *) * runtime->channels);
2720 return PTR_ERR(bufs);
2721 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2723 __put_user(result, &_xfern->result);
2724 return result < 0 ? result : 0;
2726 case SNDRV_PCM_IOCTL_REWIND:
2728 snd_pcm_uframes_t frames;
2729 snd_pcm_uframes_t __user *_frames = arg;
2730 snd_pcm_sframes_t result;
2731 if (get_user(frames, _frames))
2733 if (put_user(0, _frames))
2735 result = snd_pcm_capture_rewind(substream, frames);
2736 __put_user(result, _frames);
2737 return result < 0 ? result : 0;
2739 case SNDRV_PCM_IOCTL_FORWARD:
2741 snd_pcm_uframes_t frames;
2742 snd_pcm_uframes_t __user *_frames = arg;
2743 snd_pcm_sframes_t result;
2744 if (get_user(frames, _frames))
2746 if (put_user(0, _frames))
2748 result = snd_pcm_capture_forward(substream, frames);
2749 __put_user(result, _frames);
2750 return result < 0 ? result : 0;
2753 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2756 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2759 struct snd_pcm_file *pcm_file;
2761 pcm_file = file->private_data;
2763 if (((cmd >> 8) & 0xff) != 'A')
2766 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2767 (void __user *)arg);
2770 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2773 struct snd_pcm_file *pcm_file;
2775 pcm_file = file->private_data;
2777 if (((cmd >> 8) & 0xff) != 'A')
2780 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2781 (void __user *)arg);
2784 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2785 unsigned int cmd, void *arg)
2790 fs = snd_enter_user();
2791 switch (substream->stream) {
2792 case SNDRV_PCM_STREAM_PLAYBACK:
2793 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2794 (void __user *)arg);
2796 case SNDRV_PCM_STREAM_CAPTURE:
2797 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2798 (void __user *)arg);
2808 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2810 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2813 struct snd_pcm_file *pcm_file;
2814 struct snd_pcm_substream *substream;
2815 struct snd_pcm_runtime *runtime;
2816 snd_pcm_sframes_t result;
2818 pcm_file = file->private_data;
2819 substream = pcm_file->substream;
2820 if (PCM_RUNTIME_CHECK(substream))
2822 runtime = substream->runtime;
2823 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2825 if (!frame_aligned(runtime, count))
2827 count = bytes_to_frames(runtime, count);
2828 result = snd_pcm_lib_read(substream, buf, count);
2830 result = frames_to_bytes(runtime, result);
2834 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2835 size_t count, loff_t * offset)
2837 struct snd_pcm_file *pcm_file;
2838 struct snd_pcm_substream *substream;
2839 struct snd_pcm_runtime *runtime;
2840 snd_pcm_sframes_t result;
2842 pcm_file = file->private_data;
2843 substream = pcm_file->substream;
2844 if (PCM_RUNTIME_CHECK(substream))
2846 runtime = substream->runtime;
2847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2849 if (!frame_aligned(runtime, count))
2851 count = bytes_to_frames(runtime, count);
2852 result = snd_pcm_lib_write(substream, buf, count);
2854 result = frames_to_bytes(runtime, result);
2858 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2859 unsigned long nr_segs, loff_t pos)
2862 struct snd_pcm_file *pcm_file;
2863 struct snd_pcm_substream *substream;
2864 struct snd_pcm_runtime *runtime;
2865 snd_pcm_sframes_t result;
2868 snd_pcm_uframes_t frames;
2870 pcm_file = iocb->ki_filp->private_data;
2871 substream = pcm_file->substream;
2872 if (PCM_RUNTIME_CHECK(substream))
2874 runtime = substream->runtime;
2875 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2877 if (nr_segs > 1024 || nr_segs != runtime->channels)
2879 if (!frame_aligned(runtime, iov->iov_len))
2881 frames = bytes_to_samples(runtime, iov->iov_len);
2882 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2885 for (i = 0; i < nr_segs; ++i)
2886 bufs[i] = iov[i].iov_base;
2887 result = snd_pcm_lib_readv(substream, bufs, frames);
2889 result = frames_to_bytes(runtime, result);
2894 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2895 unsigned long nr_segs, loff_t pos)
2897 struct snd_pcm_file *pcm_file;
2898 struct snd_pcm_substream *substream;
2899 struct snd_pcm_runtime *runtime;
2900 snd_pcm_sframes_t result;
2903 snd_pcm_uframes_t frames;
2905 pcm_file = iocb->ki_filp->private_data;
2906 substream = pcm_file->substream;
2907 if (PCM_RUNTIME_CHECK(substream))
2909 runtime = substream->runtime;
2910 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2912 if (nr_segs > 128 || nr_segs != runtime->channels ||
2913 !frame_aligned(runtime, iov->iov_len))
2915 frames = bytes_to_samples(runtime, iov->iov_len);
2916 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2919 for (i = 0; i < nr_segs; ++i)
2920 bufs[i] = iov[i].iov_base;
2921 result = snd_pcm_lib_writev(substream, bufs, frames);
2923 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 if (PCM_RUNTIME_CHECK(substream))
2941 runtime = substream->runtime;
2943 poll_wait(file, &runtime->sleep, wait);
2945 snd_pcm_stream_lock_irq(substream);
2946 avail = snd_pcm_playback_avail(runtime);
2947 switch (runtime->status->state) {
2948 case SNDRV_PCM_STATE_RUNNING:
2949 case SNDRV_PCM_STATE_PREPARED:
2950 case SNDRV_PCM_STATE_PAUSED:
2951 if (avail >= runtime->control->avail_min) {
2952 mask = POLLOUT | POLLWRNORM;
2956 case SNDRV_PCM_STATE_DRAINING:
2960 mask = POLLOUT | POLLWRNORM | POLLERR;
2963 snd_pcm_stream_unlock_irq(substream);
2967 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2969 struct snd_pcm_file *pcm_file;
2970 struct snd_pcm_substream *substream;
2971 struct snd_pcm_runtime *runtime;
2973 snd_pcm_uframes_t avail;
2975 pcm_file = file->private_data;
2977 substream = pcm_file->substream;
2978 if (PCM_RUNTIME_CHECK(substream))
2980 runtime = substream->runtime;
2982 poll_wait(file, &runtime->sleep, wait);
2984 snd_pcm_stream_lock_irq(substream);
2985 avail = snd_pcm_capture_avail(runtime);
2986 switch (runtime->status->state) {
2987 case SNDRV_PCM_STATE_RUNNING:
2988 case SNDRV_PCM_STATE_PREPARED:
2989 case SNDRV_PCM_STATE_PAUSED:
2990 if (avail >= runtime->control->avail_min) {
2991 mask = POLLIN | POLLRDNORM;
2996 case SNDRV_PCM_STATE_DRAINING:
2998 mask = POLLIN | POLLRDNORM;
3003 mask = POLLIN | POLLRDNORM | POLLERR;
3006 snd_pcm_stream_unlock_irq(substream);
3015 * Only on coherent architectures, we can mmap the status and the control records
3016 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3018 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3020 * mmap status record
3022 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3023 struct vm_fault *vmf)
3025 struct snd_pcm_substream *substream = area->vm_private_data;
3026 struct snd_pcm_runtime *runtime;
3028 if (substream == NULL)
3029 return VM_FAULT_SIGBUS;
3030 runtime = substream->runtime;
3031 vmf->page = virt_to_page(runtime->status);
3032 get_page(vmf->page);
3036 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3038 .fault = snd_pcm_mmap_status_fault,
3041 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3042 struct vm_area_struct *area)
3044 struct snd_pcm_runtime *runtime;
3046 if (!(area->vm_flags & VM_READ))
3048 runtime = substream->runtime;
3049 size = area->vm_end - area->vm_start;
3050 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3052 area->vm_ops = &snd_pcm_vm_ops_status;
3053 area->vm_private_data = substream;
3054 area->vm_flags |= VM_RESERVED;
3059 * mmap control record
3061 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3062 struct vm_fault *vmf)
3064 struct snd_pcm_substream *substream = area->vm_private_data;
3065 struct snd_pcm_runtime *runtime;
3067 if (substream == NULL)
3068 return VM_FAULT_SIGBUS;
3069 runtime = substream->runtime;
3070 vmf->page = virt_to_page(runtime->control);
3071 get_page(vmf->page);
3075 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3077 .fault = snd_pcm_mmap_control_fault,
3080 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3081 struct vm_area_struct *area)
3083 struct snd_pcm_runtime *runtime;
3085 if (!(area->vm_flags & VM_READ))
3087 runtime = substream->runtime;
3088 size = area->vm_end - area->vm_start;
3089 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3091 area->vm_ops = &snd_pcm_vm_ops_control;
3092 area->vm_private_data = substream;
3093 area->vm_flags |= VM_RESERVED;
3096 #else /* ! coherent mmap */
3098 * don't support mmap for status and control records.
3100 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3101 struct vm_area_struct *area)
3105 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3106 struct vm_area_struct *area)
3110 #endif /* coherent mmap */
3112 static inline struct page *
3113 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3115 void *vaddr = substream->runtime->dma_area + ofs;
3116 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3117 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3118 return virt_to_page(CAC_ADDR(vaddr));
3120 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3121 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3122 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3123 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3124 /* assume dma_handle set via pfn_to_phys() in
3125 * mm/dma-noncoherent.c
3127 return pfn_to_page(addr >> PAGE_SHIFT);
3130 return virt_to_page(vaddr);
3134 * fault callback for mmapping a RAM page
3136 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3137 struct vm_fault *vmf)
3139 struct snd_pcm_substream *substream = area->vm_private_data;
3140 struct snd_pcm_runtime *runtime;
3141 unsigned long offset;
3145 if (substream == NULL)
3146 return VM_FAULT_SIGBUS;
3147 runtime = substream->runtime;
3148 offset = vmf->pgoff << PAGE_SHIFT;
3149 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3150 if (offset > dma_bytes - PAGE_SIZE)
3151 return VM_FAULT_SIGBUS;
3152 if (substream->ops->page)
3153 page = substream->ops->page(substream, offset);
3155 page = snd_pcm_default_page_ops(substream, offset);
3157 return VM_FAULT_SIGBUS;
3163 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3164 .open = snd_pcm_mmap_data_open,
3165 .close = snd_pcm_mmap_data_close,
3168 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3169 .open = snd_pcm_mmap_data_open,
3170 .close = snd_pcm_mmap_data_close,
3171 .fault = snd_pcm_mmap_data_fault,
3174 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3175 /* This should be defined / handled globally! */
3177 #define ARCH_HAS_DMA_MMAP_COHERENT
3182 * mmap the DMA buffer on RAM
3184 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3185 struct vm_area_struct *area)
3187 area->vm_flags |= VM_RESERVED;
3188 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3189 if (!substream->ops->page &&
3190 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3191 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3193 substream->runtime->dma_area,
3194 substream->runtime->dma_addr,
3195 area->vm_end - area->vm_start);
3196 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3197 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3198 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3199 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3200 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3201 /* mmap with fault handler */
3202 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3207 * mmap the DMA buffer on I/O memory area
3209 #if SNDRV_PCM_INFO_MMAP_IOMEM
3210 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3211 struct vm_area_struct *area)
3214 unsigned long offset;
3216 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3217 area->vm_flags |= VM_IO;
3218 size = area->vm_end - area->vm_start;
3219 offset = area->vm_pgoff << PAGE_SHIFT;
3220 if (io_remap_pfn_range(area, area->vm_start,
3221 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3222 size, area->vm_page_prot))
3227 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3228 #endif /* SNDRV_PCM_INFO_MMAP */
3230 /* mmap callback with pgprot_noncached */
3231 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3232 struct vm_area_struct *area)
3234 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3235 return snd_pcm_default_mmap(substream, area);
3237 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3242 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3243 struct vm_area_struct *area)
3245 struct snd_pcm_runtime *runtime;
3247 unsigned long offset;
3251 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3252 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3255 if (!(area->vm_flags & VM_READ))
3258 runtime = substream->runtime;
3259 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3261 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3263 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3264 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3266 size = area->vm_end - area->vm_start;
3267 offset = area->vm_pgoff << PAGE_SHIFT;
3268 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3269 if ((size_t)size > dma_bytes)
3271 if (offset > dma_bytes - size)
3274 area->vm_ops = &snd_pcm_vm_ops_data;
3275 area->vm_private_data = substream;
3276 if (substream->ops->mmap)
3277 err = substream->ops->mmap(substream, area);
3279 err = snd_pcm_default_mmap(substream, area);
3281 atomic_inc(&substream->mmap_count);
3285 EXPORT_SYMBOL(snd_pcm_mmap_data);
3287 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3289 struct snd_pcm_file * pcm_file;
3290 struct snd_pcm_substream *substream;
3291 unsigned long offset;
3293 pcm_file = file->private_data;
3294 substream = pcm_file->substream;
3295 if (PCM_RUNTIME_CHECK(substream))
3298 offset = area->vm_pgoff << PAGE_SHIFT;
3300 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3301 if (pcm_file->no_compat_mmap)
3303 return snd_pcm_mmap_status(substream, file, area);
3304 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3305 if (pcm_file->no_compat_mmap)
3307 return snd_pcm_mmap_control(substream, file, area);
3309 return snd_pcm_mmap_data(substream, file, area);
3314 static int snd_pcm_fasync(int fd, struct file * file, int on)
3316 struct snd_pcm_file * pcm_file;
3317 struct snd_pcm_substream *substream;
3318 struct snd_pcm_runtime *runtime;
3320 pcm_file = file->private_data;
3321 substream = pcm_file->substream;
3322 if (PCM_RUNTIME_CHECK(substream))
3324 runtime = substream->runtime;
3325 return fasync_helper(fd, file, on, &runtime->fasync);
3331 #ifdef CONFIG_COMPAT
3332 #include "pcm_compat.c"
3334 #define snd_pcm_ioctl_compat NULL
3338 * To be removed helpers to keep binary compatibility
3341 #ifdef CONFIG_SND_SUPPORT_OLD_API
3342 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3343 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3345 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3346 struct snd_pcm_hw_params_old *oparams)
3350 memset(params, 0, sizeof(*params));
3351 params->flags = oparams->flags;
3352 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3353 params->masks[i].bits[0] = oparams->masks[i];
3354 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3355 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3356 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3357 params->info = oparams->info;
3358 params->msbits = oparams->msbits;
3359 params->rate_num = oparams->rate_num;
3360 params->rate_den = oparams->rate_den;
3361 params->fifo_size = oparams->fifo_size;
3364 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3365 struct snd_pcm_hw_params *params)
3369 memset(oparams, 0, sizeof(*oparams));
3370 oparams->flags = params->flags;
3371 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3372 oparams->masks[i] = params->masks[i].bits[0];
3373 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3374 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3375 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3376 oparams->info = params->info;
3377 oparams->msbits = params->msbits;
3378 oparams->rate_num = params->rate_num;
3379 oparams->rate_den = params->rate_den;
3380 oparams->fifo_size = params->fifo_size;
3383 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3384 struct snd_pcm_hw_params_old __user * _oparams)
3386 struct snd_pcm_hw_params *params;
3387 struct snd_pcm_hw_params_old *oparams = NULL;
3390 params = kmalloc(sizeof(*params), GFP_KERNEL);
3394 oparams = memdup_user(_oparams, sizeof(*oparams));
3395 if (IS_ERR(oparams)) {
3396 err = PTR_ERR(oparams);
3399 snd_pcm_hw_convert_from_old_params(params, oparams);
3400 err = snd_pcm_hw_refine(substream, params);
3401 snd_pcm_hw_convert_to_old_params(oparams, params);
3402 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3413 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3414 struct snd_pcm_hw_params_old __user * _oparams)
3416 struct snd_pcm_hw_params *params;
3417 struct snd_pcm_hw_params_old *oparams = NULL;
3420 params = kmalloc(sizeof(*params), GFP_KERNEL);
3424 oparams = memdup_user(_oparams, sizeof(*oparams));
3425 if (IS_ERR(oparams)) {
3426 err = PTR_ERR(oparams);
3429 snd_pcm_hw_convert_from_old_params(params, oparams);
3430 err = snd_pcm_hw_params(substream, params);
3431 snd_pcm_hw_convert_to_old_params(oparams, params);
3432 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3442 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3445 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3448 unsigned long pgoff,
3449 unsigned long flags)
3451 struct snd_pcm_file *pcm_file = file->private_data;
3452 struct snd_pcm_substream *substream = pcm_file->substream;
3453 struct snd_pcm_runtime *runtime = substream->runtime;
3454 unsigned long offset = pgoff << PAGE_SHIFT;
3457 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3458 return (unsigned long)runtime->status;
3459 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3460 return (unsigned long)runtime->control;
3462 return (unsigned long)runtime->dma_area + offset;
3466 # define snd_pcm_get_unmapped_area NULL
3473 const struct file_operations snd_pcm_f_ops[2] = {
3475 .owner = THIS_MODULE,
3476 .write = snd_pcm_write,
3477 .aio_write = snd_pcm_aio_write,
3478 .open = snd_pcm_playback_open,
3479 .release = snd_pcm_release,
3480 .llseek = no_llseek,
3481 .poll = snd_pcm_playback_poll,
3482 .unlocked_ioctl = snd_pcm_playback_ioctl,
3483 .compat_ioctl = snd_pcm_ioctl_compat,
3484 .mmap = snd_pcm_mmap,
3485 .fasync = snd_pcm_fasync,
3486 .get_unmapped_area = snd_pcm_get_unmapped_area,
3489 .owner = THIS_MODULE,
3490 .read = snd_pcm_read,
3491 .aio_read = snd_pcm_aio_read,
3492 .open = snd_pcm_capture_open,
3493 .release = snd_pcm_release,
3494 .llseek = no_llseek,
3495 .poll = snd_pcm_capture_poll,
3496 .unlocked_ioctl = snd_pcm_capture_ioctl,
3497 .compat_ioctl = snd_pcm_ioctl_compat,
3498 .mmap = snd_pcm_mmap,
3499 .fasync = snd_pcm_fasync,
3500 .get_unmapped_area = snd_pcm_get_unmapped_area,