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;
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;
2125 pcm = snd_lookup_minor_data(iminor(inode),
2126 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2127 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2130 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2133 struct snd_pcm_file *pcm_file;
2140 err = snd_card_file_add(pcm->card, file);
2143 if (!try_module_get(pcm->card->module)) {
2147 init_waitqueue_entry(&wait, current);
2148 add_wait_queue(&pcm->open_wait, &wait);
2149 mutex_lock(&pcm->open_mutex);
2151 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2154 if (err == -EAGAIN) {
2155 if (file->f_flags & O_NONBLOCK) {
2161 set_current_state(TASK_INTERRUPTIBLE);
2162 mutex_unlock(&pcm->open_mutex);
2164 mutex_lock(&pcm->open_mutex);
2165 if (signal_pending(current)) {
2170 remove_wait_queue(&pcm->open_wait, &wait);
2171 mutex_unlock(&pcm->open_mutex);
2177 module_put(pcm->card->module);
2179 snd_card_file_remove(pcm->card, file);
2184 static int snd_pcm_release(struct inode *inode, struct file *file)
2186 struct snd_pcm *pcm;
2187 struct snd_pcm_substream *substream;
2188 struct snd_pcm_file *pcm_file;
2190 pcm_file = file->private_data;
2191 substream = pcm_file->substream;
2192 if (snd_BUG_ON(!substream))
2194 pcm = substream->pcm;
2195 mutex_lock(&pcm->open_mutex);
2196 snd_pcm_release_substream(substream);
2198 mutex_unlock(&pcm->open_mutex);
2199 wake_up(&pcm->open_wait);
2200 module_put(pcm->card->module);
2201 snd_card_file_remove(pcm->card, file);
2205 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2206 snd_pcm_uframes_t frames)
2208 struct snd_pcm_runtime *runtime = substream->runtime;
2209 snd_pcm_sframes_t appl_ptr;
2210 snd_pcm_sframes_t ret;
2211 snd_pcm_sframes_t hw_avail;
2216 snd_pcm_stream_lock_irq(substream);
2217 switch (runtime->status->state) {
2218 case SNDRV_PCM_STATE_PREPARED:
2220 case SNDRV_PCM_STATE_DRAINING:
2221 case SNDRV_PCM_STATE_RUNNING:
2222 if (snd_pcm_update_hw_ptr(substream) >= 0)
2225 case SNDRV_PCM_STATE_XRUN:
2228 case SNDRV_PCM_STATE_SUSPENDED:
2236 hw_avail = snd_pcm_playback_hw_avail(runtime);
2237 if (hw_avail <= 0) {
2241 if (frames > (snd_pcm_uframes_t)hw_avail)
2243 appl_ptr = runtime->control->appl_ptr - frames;
2245 appl_ptr += runtime->boundary;
2246 runtime->control->appl_ptr = appl_ptr;
2249 snd_pcm_stream_unlock_irq(substream);
2253 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2254 snd_pcm_uframes_t frames)
2256 struct snd_pcm_runtime *runtime = substream->runtime;
2257 snd_pcm_sframes_t appl_ptr;
2258 snd_pcm_sframes_t ret;
2259 snd_pcm_sframes_t hw_avail;
2264 snd_pcm_stream_lock_irq(substream);
2265 switch (runtime->status->state) {
2266 case SNDRV_PCM_STATE_PREPARED:
2267 case SNDRV_PCM_STATE_DRAINING:
2269 case SNDRV_PCM_STATE_RUNNING:
2270 if (snd_pcm_update_hw_ptr(substream) >= 0)
2273 case SNDRV_PCM_STATE_XRUN:
2276 case SNDRV_PCM_STATE_SUSPENDED:
2284 hw_avail = snd_pcm_capture_hw_avail(runtime);
2285 if (hw_avail <= 0) {
2289 if (frames > (snd_pcm_uframes_t)hw_avail)
2291 appl_ptr = runtime->control->appl_ptr - frames;
2293 appl_ptr += runtime->boundary;
2294 runtime->control->appl_ptr = appl_ptr;
2297 snd_pcm_stream_unlock_irq(substream);
2301 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2302 snd_pcm_uframes_t frames)
2304 struct snd_pcm_runtime *runtime = substream->runtime;
2305 snd_pcm_sframes_t appl_ptr;
2306 snd_pcm_sframes_t ret;
2307 snd_pcm_sframes_t avail;
2312 snd_pcm_stream_lock_irq(substream);
2313 switch (runtime->status->state) {
2314 case SNDRV_PCM_STATE_PREPARED:
2315 case SNDRV_PCM_STATE_PAUSED:
2317 case SNDRV_PCM_STATE_DRAINING:
2318 case SNDRV_PCM_STATE_RUNNING:
2319 if (snd_pcm_update_hw_ptr(substream) >= 0)
2322 case SNDRV_PCM_STATE_XRUN:
2325 case SNDRV_PCM_STATE_SUSPENDED:
2333 avail = snd_pcm_playback_avail(runtime);
2338 if (frames > (snd_pcm_uframes_t)avail)
2340 appl_ptr = runtime->control->appl_ptr + frames;
2341 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2342 appl_ptr -= runtime->boundary;
2343 runtime->control->appl_ptr = appl_ptr;
2346 snd_pcm_stream_unlock_irq(substream);
2350 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2351 snd_pcm_uframes_t frames)
2353 struct snd_pcm_runtime *runtime = substream->runtime;
2354 snd_pcm_sframes_t appl_ptr;
2355 snd_pcm_sframes_t ret;
2356 snd_pcm_sframes_t avail;
2361 snd_pcm_stream_lock_irq(substream);
2362 switch (runtime->status->state) {
2363 case SNDRV_PCM_STATE_PREPARED:
2364 case SNDRV_PCM_STATE_DRAINING:
2365 case SNDRV_PCM_STATE_PAUSED:
2367 case SNDRV_PCM_STATE_RUNNING:
2368 if (snd_pcm_update_hw_ptr(substream) >= 0)
2371 case SNDRV_PCM_STATE_XRUN:
2374 case SNDRV_PCM_STATE_SUSPENDED:
2382 avail = snd_pcm_capture_avail(runtime);
2387 if (frames > (snd_pcm_uframes_t)avail)
2389 appl_ptr = runtime->control->appl_ptr + frames;
2390 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2391 appl_ptr -= runtime->boundary;
2392 runtime->control->appl_ptr = appl_ptr;
2395 snd_pcm_stream_unlock_irq(substream);
2399 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2401 struct snd_pcm_runtime *runtime = substream->runtime;
2404 snd_pcm_stream_lock_irq(substream);
2405 switch (runtime->status->state) {
2406 case SNDRV_PCM_STATE_DRAINING:
2407 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2409 case SNDRV_PCM_STATE_RUNNING:
2410 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2413 case SNDRV_PCM_STATE_PREPARED:
2414 case SNDRV_PCM_STATE_SUSPENDED:
2417 case SNDRV_PCM_STATE_XRUN:
2425 snd_pcm_stream_unlock_irq(substream);
2429 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2430 snd_pcm_sframes_t __user *res)
2432 struct snd_pcm_runtime *runtime = substream->runtime;
2434 snd_pcm_sframes_t n = 0;
2436 snd_pcm_stream_lock_irq(substream);
2437 switch (runtime->status->state) {
2438 case SNDRV_PCM_STATE_DRAINING:
2439 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2441 case SNDRV_PCM_STATE_RUNNING:
2442 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2445 case SNDRV_PCM_STATE_PREPARED:
2446 case SNDRV_PCM_STATE_SUSPENDED:
2448 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2449 n = snd_pcm_playback_hw_avail(runtime);
2451 n = snd_pcm_capture_avail(runtime);
2452 n += runtime->delay;
2454 case SNDRV_PCM_STATE_XRUN:
2462 snd_pcm_stream_unlock_irq(substream);
2464 if (put_user(n, res))
2469 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2470 struct snd_pcm_sync_ptr __user *_sync_ptr)
2472 struct snd_pcm_runtime *runtime = substream->runtime;
2473 struct snd_pcm_sync_ptr sync_ptr;
2474 volatile struct snd_pcm_mmap_status *status;
2475 volatile struct snd_pcm_mmap_control *control;
2478 memset(&sync_ptr, 0, sizeof(sync_ptr));
2479 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2481 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2483 status = runtime->status;
2484 control = runtime->control;
2485 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2486 err = snd_pcm_hwsync(substream);
2490 snd_pcm_stream_lock_irq(substream);
2491 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2492 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2494 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2495 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2496 control->avail_min = sync_ptr.c.control.avail_min;
2498 sync_ptr.c.control.avail_min = control->avail_min;
2499 sync_ptr.s.status.state = status->state;
2500 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2501 sync_ptr.s.status.tstamp = status->tstamp;
2502 sync_ptr.s.status.suspended_state = status->suspended_state;
2503 snd_pcm_stream_unlock_irq(substream);
2504 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2509 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2511 struct snd_pcm_runtime *runtime = substream->runtime;
2514 if (get_user(arg, _arg))
2516 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2518 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2519 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2520 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2524 static int snd_pcm_common_ioctl1(struct file *file,
2525 struct snd_pcm_substream *substream,
2526 unsigned int cmd, void __user *arg)
2529 case SNDRV_PCM_IOCTL_PVERSION:
2530 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2531 case SNDRV_PCM_IOCTL_INFO:
2532 return snd_pcm_info_user(substream, arg);
2533 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2535 case SNDRV_PCM_IOCTL_TTSTAMP:
2536 return snd_pcm_tstamp(substream, arg);
2537 case SNDRV_PCM_IOCTL_HW_REFINE:
2538 return snd_pcm_hw_refine_user(substream, arg);
2539 case SNDRV_PCM_IOCTL_HW_PARAMS:
2540 return snd_pcm_hw_params_user(substream, arg);
2541 case SNDRV_PCM_IOCTL_HW_FREE:
2542 return snd_pcm_hw_free(substream);
2543 case SNDRV_PCM_IOCTL_SW_PARAMS:
2544 return snd_pcm_sw_params_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_STATUS:
2546 return snd_pcm_status_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2548 return snd_pcm_channel_info_user(substream, arg);
2549 case SNDRV_PCM_IOCTL_PREPARE:
2550 return snd_pcm_prepare(substream, file);
2551 case SNDRV_PCM_IOCTL_RESET:
2552 return snd_pcm_reset(substream);
2553 case SNDRV_PCM_IOCTL_START:
2554 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2555 case SNDRV_PCM_IOCTL_LINK:
2556 return snd_pcm_link(substream, (int)(unsigned long) arg);
2557 case SNDRV_PCM_IOCTL_UNLINK:
2558 return snd_pcm_unlink(substream);
2559 case SNDRV_PCM_IOCTL_RESUME:
2560 return snd_pcm_resume(substream);
2561 case SNDRV_PCM_IOCTL_XRUN:
2562 return snd_pcm_xrun(substream);
2563 case SNDRV_PCM_IOCTL_HWSYNC:
2564 return snd_pcm_hwsync(substream);
2565 case SNDRV_PCM_IOCTL_DELAY:
2566 return snd_pcm_delay(substream, arg);
2567 case SNDRV_PCM_IOCTL_SYNC_PTR:
2568 return snd_pcm_sync_ptr(substream, arg);
2569 #ifdef CONFIG_SND_SUPPORT_OLD_API
2570 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2571 return snd_pcm_hw_refine_old_user(substream, arg);
2572 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2573 return snd_pcm_hw_params_old_user(substream, arg);
2575 case SNDRV_PCM_IOCTL_DRAIN:
2576 return snd_pcm_drain(substream, file);
2577 case SNDRV_PCM_IOCTL_DROP:
2578 return snd_pcm_drop(substream);
2579 case SNDRV_PCM_IOCTL_PAUSE:
2582 snd_pcm_stream_lock_irq(substream);
2583 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2584 snd_pcm_stream_unlock_irq(substream);
2588 snd_printd("unknown ioctl = 0x%x\n", cmd);
2592 static int snd_pcm_playback_ioctl1(struct file *file,
2593 struct snd_pcm_substream *substream,
2594 unsigned int cmd, void __user *arg)
2596 if (snd_BUG_ON(!substream))
2598 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2601 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2603 struct snd_xferi xferi;
2604 struct snd_xferi __user *_xferi = arg;
2605 struct snd_pcm_runtime *runtime = substream->runtime;
2606 snd_pcm_sframes_t result;
2607 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2609 if (put_user(0, &_xferi->result))
2611 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2613 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2614 __put_user(result, &_xferi->result);
2615 return result < 0 ? result : 0;
2617 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2619 struct snd_xfern xfern;
2620 struct snd_xfern __user *_xfern = arg;
2621 struct snd_pcm_runtime *runtime = substream->runtime;
2623 snd_pcm_sframes_t result;
2624 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2626 if (runtime->channels > 128)
2628 if (put_user(0, &_xfern->result))
2630 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2633 bufs = memdup_user(xfern.bufs,
2634 sizeof(void *) * runtime->channels);
2636 return PTR_ERR(bufs);
2637 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2639 __put_user(result, &_xfern->result);
2640 return result < 0 ? result : 0;
2642 case SNDRV_PCM_IOCTL_REWIND:
2644 snd_pcm_uframes_t frames;
2645 snd_pcm_uframes_t __user *_frames = arg;
2646 snd_pcm_sframes_t result;
2647 if (get_user(frames, _frames))
2649 if (put_user(0, _frames))
2651 result = snd_pcm_playback_rewind(substream, frames);
2652 __put_user(result, _frames);
2653 return result < 0 ? result : 0;
2655 case SNDRV_PCM_IOCTL_FORWARD:
2657 snd_pcm_uframes_t frames;
2658 snd_pcm_uframes_t __user *_frames = arg;
2659 snd_pcm_sframes_t result;
2660 if (get_user(frames, _frames))
2662 if (put_user(0, _frames))
2664 result = snd_pcm_playback_forward(substream, frames);
2665 __put_user(result, _frames);
2666 return result < 0 ? result : 0;
2669 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2672 static int snd_pcm_capture_ioctl1(struct file *file,
2673 struct snd_pcm_substream *substream,
2674 unsigned int cmd, void __user *arg)
2676 if (snd_BUG_ON(!substream))
2678 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2681 case SNDRV_PCM_IOCTL_READI_FRAMES:
2683 struct snd_xferi xferi;
2684 struct snd_xferi __user *_xferi = arg;
2685 struct snd_pcm_runtime *runtime = substream->runtime;
2686 snd_pcm_sframes_t result;
2687 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2689 if (put_user(0, &_xferi->result))
2691 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2693 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2694 __put_user(result, &_xferi->result);
2695 return result < 0 ? result : 0;
2697 case SNDRV_PCM_IOCTL_READN_FRAMES:
2699 struct snd_xfern xfern;
2700 struct snd_xfern __user *_xfern = arg;
2701 struct snd_pcm_runtime *runtime = substream->runtime;
2703 snd_pcm_sframes_t result;
2704 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2706 if (runtime->channels > 128)
2708 if (put_user(0, &_xfern->result))
2710 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2713 bufs = memdup_user(xfern.bufs,
2714 sizeof(void *) * runtime->channels);
2716 return PTR_ERR(bufs);
2717 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2719 __put_user(result, &_xfern->result);
2720 return result < 0 ? result : 0;
2722 case SNDRV_PCM_IOCTL_REWIND:
2724 snd_pcm_uframes_t frames;
2725 snd_pcm_uframes_t __user *_frames = arg;
2726 snd_pcm_sframes_t result;
2727 if (get_user(frames, _frames))
2729 if (put_user(0, _frames))
2731 result = snd_pcm_capture_rewind(substream, frames);
2732 __put_user(result, _frames);
2733 return result < 0 ? result : 0;
2735 case SNDRV_PCM_IOCTL_FORWARD:
2737 snd_pcm_uframes_t frames;
2738 snd_pcm_uframes_t __user *_frames = arg;
2739 snd_pcm_sframes_t result;
2740 if (get_user(frames, _frames))
2742 if (put_user(0, _frames))
2744 result = snd_pcm_capture_forward(substream, frames);
2745 __put_user(result, _frames);
2746 return result < 0 ? result : 0;
2749 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2752 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2755 struct snd_pcm_file *pcm_file;
2757 pcm_file = file->private_data;
2759 if (((cmd >> 8) & 0xff) != 'A')
2762 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2763 (void __user *)arg);
2766 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2769 struct snd_pcm_file *pcm_file;
2771 pcm_file = file->private_data;
2773 if (((cmd >> 8) & 0xff) != 'A')
2776 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2777 (void __user *)arg);
2780 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2781 unsigned int cmd, void *arg)
2786 fs = snd_enter_user();
2787 switch (substream->stream) {
2788 case SNDRV_PCM_STREAM_PLAYBACK:
2789 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2790 (void __user *)arg);
2792 case SNDRV_PCM_STREAM_CAPTURE:
2793 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2794 (void __user *)arg);
2804 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2806 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2809 struct snd_pcm_file *pcm_file;
2810 struct snd_pcm_substream *substream;
2811 struct snd_pcm_runtime *runtime;
2812 snd_pcm_sframes_t result;
2814 pcm_file = file->private_data;
2815 substream = pcm_file->substream;
2816 if (PCM_RUNTIME_CHECK(substream))
2818 runtime = substream->runtime;
2819 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2821 if (!frame_aligned(runtime, count))
2823 count = bytes_to_frames(runtime, count);
2824 result = snd_pcm_lib_read(substream, buf, count);
2826 result = frames_to_bytes(runtime, result);
2830 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2831 size_t count, loff_t * offset)
2833 struct snd_pcm_file *pcm_file;
2834 struct snd_pcm_substream *substream;
2835 struct snd_pcm_runtime *runtime;
2836 snd_pcm_sframes_t result;
2838 pcm_file = file->private_data;
2839 substream = pcm_file->substream;
2840 if (PCM_RUNTIME_CHECK(substream))
2842 runtime = substream->runtime;
2843 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2845 if (!frame_aligned(runtime, count))
2847 count = bytes_to_frames(runtime, count);
2848 result = snd_pcm_lib_write(substream, buf, count);
2850 result = frames_to_bytes(runtime, result);
2854 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2855 unsigned long nr_segs, loff_t pos)
2858 struct snd_pcm_file *pcm_file;
2859 struct snd_pcm_substream *substream;
2860 struct snd_pcm_runtime *runtime;
2861 snd_pcm_sframes_t result;
2864 snd_pcm_uframes_t frames;
2866 pcm_file = iocb->ki_filp->private_data;
2867 substream = pcm_file->substream;
2868 if (PCM_RUNTIME_CHECK(substream))
2870 runtime = substream->runtime;
2871 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2873 if (nr_segs > 1024 || nr_segs != runtime->channels)
2875 if (!frame_aligned(runtime, iov->iov_len))
2877 frames = bytes_to_samples(runtime, iov->iov_len);
2878 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2881 for (i = 0; i < nr_segs; ++i)
2882 bufs[i] = iov[i].iov_base;
2883 result = snd_pcm_lib_readv(substream, bufs, frames);
2885 result = frames_to_bytes(runtime, result);
2890 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2891 unsigned long nr_segs, loff_t pos)
2893 struct snd_pcm_file *pcm_file;
2894 struct snd_pcm_substream *substream;
2895 struct snd_pcm_runtime *runtime;
2896 snd_pcm_sframes_t result;
2899 snd_pcm_uframes_t frames;
2901 pcm_file = iocb->ki_filp->private_data;
2902 substream = pcm_file->substream;
2903 if (PCM_RUNTIME_CHECK(substream))
2905 runtime = substream->runtime;
2906 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2908 if (nr_segs > 128 || nr_segs != runtime->channels ||
2909 !frame_aligned(runtime, iov->iov_len))
2911 frames = bytes_to_samples(runtime, iov->iov_len);
2912 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2915 for (i = 0; i < nr_segs; ++i)
2916 bufs[i] = iov[i].iov_base;
2917 result = snd_pcm_lib_writev(substream, bufs, frames);
2919 result = frames_to_bytes(runtime, result);
2924 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2926 struct snd_pcm_file *pcm_file;
2927 struct snd_pcm_substream *substream;
2928 struct snd_pcm_runtime *runtime;
2930 snd_pcm_uframes_t avail;
2932 pcm_file = file->private_data;
2934 substream = pcm_file->substream;
2935 if (PCM_RUNTIME_CHECK(substream))
2937 runtime = substream->runtime;
2939 poll_wait(file, &runtime->sleep, wait);
2941 snd_pcm_stream_lock_irq(substream);
2942 avail = snd_pcm_playback_avail(runtime);
2943 switch (runtime->status->state) {
2944 case SNDRV_PCM_STATE_RUNNING:
2945 case SNDRV_PCM_STATE_PREPARED:
2946 case SNDRV_PCM_STATE_PAUSED:
2947 if (avail >= runtime->control->avail_min) {
2948 mask = POLLOUT | POLLWRNORM;
2952 case SNDRV_PCM_STATE_DRAINING:
2956 mask = POLLOUT | POLLWRNORM | POLLERR;
2959 snd_pcm_stream_unlock_irq(substream);
2963 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2965 struct snd_pcm_file *pcm_file;
2966 struct snd_pcm_substream *substream;
2967 struct snd_pcm_runtime *runtime;
2969 snd_pcm_uframes_t avail;
2971 pcm_file = file->private_data;
2973 substream = pcm_file->substream;
2974 if (PCM_RUNTIME_CHECK(substream))
2976 runtime = substream->runtime;
2978 poll_wait(file, &runtime->sleep, wait);
2980 snd_pcm_stream_lock_irq(substream);
2981 avail = snd_pcm_capture_avail(runtime);
2982 switch (runtime->status->state) {
2983 case SNDRV_PCM_STATE_RUNNING:
2984 case SNDRV_PCM_STATE_PREPARED:
2985 case SNDRV_PCM_STATE_PAUSED:
2986 if (avail >= runtime->control->avail_min) {
2987 mask = POLLIN | POLLRDNORM;
2992 case SNDRV_PCM_STATE_DRAINING:
2994 mask = POLLIN | POLLRDNORM;
2999 mask = POLLIN | POLLRDNORM | POLLERR;
3002 snd_pcm_stream_unlock_irq(substream);
3011 * Only on coherent architectures, we can mmap the status and the control records
3012 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3014 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3016 * mmap status record
3018 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3019 struct vm_fault *vmf)
3021 struct snd_pcm_substream *substream = area->vm_private_data;
3022 struct snd_pcm_runtime *runtime;
3024 if (substream == NULL)
3025 return VM_FAULT_SIGBUS;
3026 runtime = substream->runtime;
3027 vmf->page = virt_to_page(runtime->status);
3028 get_page(vmf->page);
3032 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3034 .fault = snd_pcm_mmap_status_fault,
3037 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3038 struct vm_area_struct *area)
3040 struct snd_pcm_runtime *runtime;
3042 if (!(area->vm_flags & VM_READ))
3044 runtime = substream->runtime;
3045 size = area->vm_end - area->vm_start;
3046 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3048 area->vm_ops = &snd_pcm_vm_ops_status;
3049 area->vm_private_data = substream;
3050 area->vm_flags |= VM_RESERVED;
3055 * mmap control record
3057 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3058 struct vm_fault *vmf)
3060 struct snd_pcm_substream *substream = area->vm_private_data;
3061 struct snd_pcm_runtime *runtime;
3063 if (substream == NULL)
3064 return VM_FAULT_SIGBUS;
3065 runtime = substream->runtime;
3066 vmf->page = virt_to_page(runtime->control);
3067 get_page(vmf->page);
3071 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3073 .fault = snd_pcm_mmap_control_fault,
3076 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3077 struct vm_area_struct *area)
3079 struct snd_pcm_runtime *runtime;
3081 if (!(area->vm_flags & VM_READ))
3083 runtime = substream->runtime;
3084 size = area->vm_end - area->vm_start;
3085 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3087 area->vm_ops = &snd_pcm_vm_ops_control;
3088 area->vm_private_data = substream;
3089 area->vm_flags |= VM_RESERVED;
3092 #else /* ! coherent mmap */
3094 * don't support mmap for status and control records.
3096 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3097 struct vm_area_struct *area)
3101 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3102 struct vm_area_struct *area)
3106 #endif /* coherent mmap */
3108 static inline struct page *
3109 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3111 void *vaddr = substream->runtime->dma_area + ofs;
3112 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3113 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3114 return virt_to_page(CAC_ADDR(vaddr));
3116 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3117 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3118 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3119 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3120 /* assume dma_handle set via pfn_to_phys() in
3121 * mm/dma-noncoherent.c
3123 return pfn_to_page(addr >> PAGE_SHIFT);
3126 return virt_to_page(vaddr);
3130 * fault callback for mmapping a RAM page
3132 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3133 struct vm_fault *vmf)
3135 struct snd_pcm_substream *substream = area->vm_private_data;
3136 struct snd_pcm_runtime *runtime;
3137 unsigned long offset;
3141 if (substream == NULL)
3142 return VM_FAULT_SIGBUS;
3143 runtime = substream->runtime;
3144 offset = vmf->pgoff << PAGE_SHIFT;
3145 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3146 if (offset > dma_bytes - PAGE_SIZE)
3147 return VM_FAULT_SIGBUS;
3148 if (substream->ops->page)
3149 page = substream->ops->page(substream, offset);
3151 page = snd_pcm_default_page_ops(substream, offset);
3153 return VM_FAULT_SIGBUS;
3159 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3160 .open = snd_pcm_mmap_data_open,
3161 .close = snd_pcm_mmap_data_close,
3164 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3165 .open = snd_pcm_mmap_data_open,
3166 .close = snd_pcm_mmap_data_close,
3167 .fault = snd_pcm_mmap_data_fault,
3170 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3171 /* This should be defined / handled globally! */
3173 #define ARCH_HAS_DMA_MMAP_COHERENT
3178 * mmap the DMA buffer on RAM
3180 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3181 struct vm_area_struct *area)
3183 area->vm_flags |= VM_RESERVED;
3184 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3185 if (!substream->ops->page &&
3186 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3187 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3189 substream->runtime->dma_area,
3190 substream->runtime->dma_addr,
3191 area->vm_end - area->vm_start);
3192 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3193 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3194 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3195 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3196 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3197 /* mmap with fault handler */
3198 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3203 * mmap the DMA buffer on I/O memory area
3205 #if SNDRV_PCM_INFO_MMAP_IOMEM
3206 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3207 struct vm_area_struct *area)
3210 unsigned long offset;
3212 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3213 area->vm_flags |= VM_IO;
3214 size = area->vm_end - area->vm_start;
3215 offset = area->vm_pgoff << PAGE_SHIFT;
3216 if (io_remap_pfn_range(area, area->vm_start,
3217 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3218 size, area->vm_page_prot))
3223 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3224 #endif /* SNDRV_PCM_INFO_MMAP */
3226 /* mmap callback with pgprot_noncached */
3227 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3228 struct vm_area_struct *area)
3230 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3231 return snd_pcm_default_mmap(substream, area);
3233 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3238 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3239 struct vm_area_struct *area)
3241 struct snd_pcm_runtime *runtime;
3243 unsigned long offset;
3247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3248 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3251 if (!(area->vm_flags & VM_READ))
3254 runtime = substream->runtime;
3255 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3257 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3259 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3260 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3262 size = area->vm_end - area->vm_start;
3263 offset = area->vm_pgoff << PAGE_SHIFT;
3264 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3265 if ((size_t)size > dma_bytes)
3267 if (offset > dma_bytes - size)
3270 area->vm_ops = &snd_pcm_vm_ops_data;
3271 area->vm_private_data = substream;
3272 if (substream->ops->mmap)
3273 err = substream->ops->mmap(substream, area);
3275 err = snd_pcm_default_mmap(substream, area);
3277 atomic_inc(&substream->mmap_count);
3281 EXPORT_SYMBOL(snd_pcm_mmap_data);
3283 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3285 struct snd_pcm_file * pcm_file;
3286 struct snd_pcm_substream *substream;
3287 unsigned long offset;
3289 pcm_file = file->private_data;
3290 substream = pcm_file->substream;
3291 if (PCM_RUNTIME_CHECK(substream))
3294 offset = area->vm_pgoff << PAGE_SHIFT;
3296 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3297 if (pcm_file->no_compat_mmap)
3299 return snd_pcm_mmap_status(substream, file, area);
3300 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3301 if (pcm_file->no_compat_mmap)
3303 return snd_pcm_mmap_control(substream, file, area);
3305 return snd_pcm_mmap_data(substream, file, area);
3310 static int snd_pcm_fasync(int fd, struct file * file, int on)
3312 struct snd_pcm_file * pcm_file;
3313 struct snd_pcm_substream *substream;
3314 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 err = fasync_helper(fd, file, on, &runtime->fasync);
3332 #ifdef CONFIG_COMPAT
3333 #include "pcm_compat.c"
3335 #define snd_pcm_ioctl_compat NULL
3339 * To be removed helpers to keep binary compatibility
3342 #ifdef CONFIG_SND_SUPPORT_OLD_API
3343 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3344 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3346 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3347 struct snd_pcm_hw_params_old *oparams)
3351 memset(params, 0, sizeof(*params));
3352 params->flags = oparams->flags;
3353 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3354 params->masks[i].bits[0] = oparams->masks[i];
3355 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3356 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3357 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3358 params->info = oparams->info;
3359 params->msbits = oparams->msbits;
3360 params->rate_num = oparams->rate_num;
3361 params->rate_den = oparams->rate_den;
3362 params->fifo_size = oparams->fifo_size;
3365 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3366 struct snd_pcm_hw_params *params)
3370 memset(oparams, 0, sizeof(*oparams));
3371 oparams->flags = params->flags;
3372 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3373 oparams->masks[i] = params->masks[i].bits[0];
3374 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3375 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3376 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3377 oparams->info = params->info;
3378 oparams->msbits = params->msbits;
3379 oparams->rate_num = params->rate_num;
3380 oparams->rate_den = params->rate_den;
3381 oparams->fifo_size = params->fifo_size;
3384 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3385 struct snd_pcm_hw_params_old __user * _oparams)
3387 struct snd_pcm_hw_params *params;
3388 struct snd_pcm_hw_params_old *oparams = NULL;
3391 params = kmalloc(sizeof(*params), GFP_KERNEL);
3395 oparams = memdup_user(_oparams, sizeof(*oparams));
3396 if (IS_ERR(oparams)) {
3397 err = PTR_ERR(oparams);
3400 snd_pcm_hw_convert_from_old_params(params, oparams);
3401 err = snd_pcm_hw_refine(substream, params);
3402 snd_pcm_hw_convert_to_old_params(oparams, params);
3403 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3414 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3415 struct snd_pcm_hw_params_old __user * _oparams)
3417 struct snd_pcm_hw_params *params;
3418 struct snd_pcm_hw_params_old *oparams = NULL;
3421 params = kmalloc(sizeof(*params), GFP_KERNEL);
3425 oparams = memdup_user(_oparams, sizeof(*oparams));
3426 if (IS_ERR(oparams)) {
3427 err = PTR_ERR(oparams);
3430 snd_pcm_hw_convert_from_old_params(params, oparams);
3431 err = snd_pcm_hw_params(substream, params);
3432 snd_pcm_hw_convert_to_old_params(oparams, params);
3433 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3443 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3446 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3447 unsigned long len, unsigned long pgoff,
3448 unsigned long flags)
3453 # define dummy_get_unmapped_area NULL
3460 const struct file_operations snd_pcm_f_ops[2] = {
3462 .owner = THIS_MODULE,
3463 .write = snd_pcm_write,
3464 .aio_write = snd_pcm_aio_write,
3465 .open = snd_pcm_playback_open,
3466 .release = snd_pcm_release,
3467 .poll = snd_pcm_playback_poll,
3468 .unlocked_ioctl = snd_pcm_playback_ioctl,
3469 .compat_ioctl = snd_pcm_ioctl_compat,
3470 .mmap = snd_pcm_mmap,
3471 .fasync = snd_pcm_fasync,
3472 .get_unmapped_area = dummy_get_unmapped_area,
3475 .owner = THIS_MODULE,
3476 .read = snd_pcm_read,
3477 .aio_read = snd_pcm_aio_read,
3478 .open = snd_pcm_capture_open,
3479 .release = snd_pcm_release,
3480 .poll = snd_pcm_capture_poll,
3481 .unlocked_ioctl = snd_pcm_capture_ioctl,
3482 .compat_ioctl = snd_pcm_ioctl_compat,
3483 .mmap = snd_pcm_mmap,
3484 .fasync = snd_pcm_fasync,
3485 .get_unmapped_area = dummy_get_unmapped_area,