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>
44 struct snd_pcm_hw_params_old {
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
54 unsigned int rate_num;
55 unsigned int rate_den;
56 snd_pcm_uframes_t fifo_size;
57 unsigned char reserved[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
69 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
75 DEFINE_RWLOCK(snd_pcm_link_rwlock);
76 EXPORT_SYMBOL(snd_pcm_link_rwlock);
78 static DECLARE_RWSEM(snd_pcm_link_rwsem);
80 static inline mm_segment_t snd_enter_user(void)
82 mm_segment_t fs = get_fs();
87 static inline void snd_leave_user(mm_segment_t fs)
94 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
96 struct snd_pcm_runtime *runtime;
97 struct snd_pcm *pcm = substream->pcm;
98 struct snd_pcm_str *pstr = substream->pstr;
100 memset(info, 0, sizeof(*info));
101 info->card = pcm->card->number;
102 info->device = pcm->device;
103 info->stream = substream->stream;
104 info->subdevice = substream->number;
105 strlcpy(info->id, pcm->id, sizeof(info->id));
106 strlcpy(info->name, pcm->name, sizeof(info->name));
107 info->dev_class = pcm->dev_class;
108 info->dev_subclass = pcm->dev_subclass;
109 info->subdevices_count = pstr->substream_count;
110 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
111 strlcpy(info->subname, substream->name, sizeof(info->subname));
112 runtime = substream->runtime;
113 /* AB: FIXME!!! This is definitely nonsense */
115 info->sync = runtime->sync;
116 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
121 int snd_pcm_info_user(struct snd_pcm_substream *substream,
122 struct snd_pcm_info __user * _info)
124 struct snd_pcm_info *info;
127 info = kmalloc(sizeof(*info), GFP_KERNEL);
130 err = snd_pcm_info(substream, info);
132 if (copy_to_user(_info, info, sizeof(*info)))
142 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
143 char *snd_pcm_hw_param_names[] = {
147 HW_PARAM(SAMPLE_BITS),
148 HW_PARAM(FRAME_BITS),
151 HW_PARAM(PERIOD_TIME),
152 HW_PARAM(PERIOD_SIZE),
153 HW_PARAM(PERIOD_BYTES),
155 HW_PARAM(BUFFER_TIME),
156 HW_PARAM(BUFFER_SIZE),
157 HW_PARAM(BUFFER_BYTES),
162 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
163 struct snd_pcm_hw_params *params)
166 struct snd_pcm_hardware *hw;
167 struct snd_interval *i = NULL;
168 struct snd_mask *m = NULL;
169 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
170 unsigned int rstamps[constrs->rules_num];
171 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
172 unsigned int stamp = 2;
176 params->fifo_size = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
180 params->rate_num = 0;
181 params->rate_den = 0;
184 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
185 m = hw_param_mask(params, k);
186 if (snd_mask_empty(m))
188 if (!(params->rmask & (1 << k)))
191 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
192 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
194 changed = snd_mask_refine(m, constrs_mask(constrs, k));
196 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
199 params->cmask |= 1 << k;
204 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
205 i = hw_param_interval(params, k);
206 if (snd_interval_empty(i))
208 if (!(params->rmask & (1 << k)))
211 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
216 i->openmin ? '(' : '[', i->min,
217 i->max, i->openmax ? ')' : ']');
220 changed = snd_interval_refine(i, constrs_interval(constrs, k));
225 printk("%c%u %u%c\n",
226 i->openmin ? '(' : '[', i->min,
227 i->max, i->openmax ? ')' : ']');
230 params->cmask |= 1 << k;
235 for (k = 0; k < constrs->rules_num; k++)
237 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
238 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
241 for (k = 0; k < constrs->rules_num; k++) {
242 struct snd_pcm_hw_rule *r = &constrs->rules[k];
245 if (r->cond && !(r->cond & params->flags))
247 for (d = 0; r->deps[d] >= 0; d++) {
248 if (vstamps[r->deps[d]] > rstamps[k]) {
256 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
258 printk("%s = ", snd_pcm_hw_param_names[r->var]);
259 if (hw_is_mask(r->var)) {
260 m = hw_param_mask(params, r->var);
261 printk("%x", *m->bits);
263 i = hw_param_interval(params, r->var);
268 i->openmin ? '(' : '[', i->min,
269 i->max, i->openmax ? ')' : ']');
273 changed = r->func(params, r);
277 if (hw_is_mask(r->var))
278 printk("%x", *m->bits);
284 i->openmin ? '(' : '[', i->min,
285 i->max, i->openmax ? ')' : ']');
291 if (changed && r->var >= 0) {
292 params->cmask |= (1 << r->var);
293 vstamps[r->var] = stamp;
301 if (!params->msbits) {
302 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
303 if (snd_interval_single(i))
304 params->msbits = snd_interval_value(i);
307 if (!params->rate_den) {
308 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
309 if (snd_interval_single(i)) {
310 params->rate_num = snd_interval_value(i);
311 params->rate_den = 1;
315 hw = &substream->runtime->hw;
317 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
318 if (!params->fifo_size) {
319 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
320 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
321 if (snd_mask_min(m) == snd_mask_max(m) &&
322 snd_interval_min(i) == snd_interval_max(i)) {
323 changed = substream->ops->ioctl(substream,
324 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
333 EXPORT_SYMBOL(snd_pcm_hw_refine);
335 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params __user * _params)
338 struct snd_pcm_hw_params *params;
341 params = memdup_user(_params, sizeof(*params));
343 return PTR_ERR(params);
345 err = snd_pcm_hw_refine(substream, params);
346 if (copy_to_user(_params, params, sizeof(*params))) {
355 static int period_to_usecs(struct snd_pcm_runtime *runtime)
360 return -1; /* invalid */
362 /* take 75% of period time as the deadline */
363 usecs = (750000 / runtime->rate) * runtime->period_size;
364 usecs += ((750000 % runtime->rate) * runtime->period_size) /
370 static int calc_boundary(struct snd_pcm_runtime *runtime)
374 boundary = (u_int64_t)runtime->buffer_size *
375 (u_int64_t)runtime->period_size;
376 #if BITS_PER_LONG < 64
377 /* try to find lowest common multiple for buffer and period */
378 if (boundary > LONG_MAX - runtime->buffer_size) {
379 u_int32_t remainder = -1;
380 u_int32_t divident = runtime->buffer_size;
381 u_int32_t divisor = runtime->period_size;
383 remainder = divident % divisor;
389 boundary = div_u64(boundary, divisor);
390 if (boundary > LONG_MAX - runtime->buffer_size)
396 runtime->boundary = boundary;
397 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
398 runtime->boundary *= 2;
402 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
403 struct snd_pcm_hw_params *params)
405 struct snd_pcm_runtime *runtime;
408 snd_pcm_uframes_t frames;
410 if (PCM_RUNTIME_CHECK(substream))
412 runtime = substream->runtime;
413 snd_pcm_stream_lock_irq(substream);
414 switch (runtime->status->state) {
415 case SNDRV_PCM_STATE_OPEN:
416 case SNDRV_PCM_STATE_SETUP:
417 case SNDRV_PCM_STATE_PREPARED:
420 snd_pcm_stream_unlock_irq(substream);
423 snd_pcm_stream_unlock_irq(substream);
424 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
425 if (!substream->oss.oss)
427 if (atomic_read(&substream->mmap_count))
431 err = snd_pcm_hw_refine(substream, params);
435 err = snd_pcm_hw_params_choose(substream, params);
439 if (substream->ops->hw_params != NULL) {
440 err = substream->ops->hw_params(substream, params);
445 runtime->access = params_access(params);
446 runtime->format = params_format(params);
447 runtime->subformat = params_subformat(params);
448 runtime->channels = params_channels(params);
449 runtime->rate = params_rate(params);
450 runtime->period_size = params_period_size(params);
451 runtime->periods = params_periods(params);
452 runtime->buffer_size = params_buffer_size(params);
453 runtime->info = params->info;
454 runtime->rate_num = params->rate_num;
455 runtime->rate_den = params->rate_den;
457 bits = snd_pcm_format_physical_width(runtime->format);
458 runtime->sample_bits = bits;
459 bits *= runtime->channels;
460 runtime->frame_bits = bits;
462 while (bits % 8 != 0) {
466 runtime->byte_align = bits / 8;
467 runtime->min_align = frames;
469 /* Default sw params */
470 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
471 runtime->period_step = 1;
472 runtime->control->avail_min = runtime->period_size;
473 runtime->start_threshold = 1;
474 runtime->stop_threshold = runtime->buffer_size;
475 runtime->silence_threshold = 0;
476 runtime->silence_size = 0;
477 err = calc_boundary(runtime);
481 snd_pcm_timer_resolution_change(substream);
482 runtime->status->state = SNDRV_PCM_STATE_SETUP;
484 if (substream->latency_pm_qos_req) {
485 pm_qos_remove_request(substream->latency_pm_qos_req);
486 substream->latency_pm_qos_req = NULL;
488 if ((usecs = period_to_usecs(runtime)) >= 0)
489 substream->latency_pm_qos_req = pm_qos_add_request(
490 PM_QOS_CPU_DMA_LATENCY, usecs);
493 /* hardware might be unuseable from this time,
494 so we force application to retry to set
495 the correct hardware parameter settings */
496 runtime->status->state = SNDRV_PCM_STATE_OPEN;
497 if (substream->ops->hw_free != NULL)
498 substream->ops->hw_free(substream);
502 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
503 struct snd_pcm_hw_params __user * _params)
505 struct snd_pcm_hw_params *params;
508 params = memdup_user(_params, sizeof(*params));
510 return PTR_ERR(params);
512 err = snd_pcm_hw_params(substream, params);
513 if (copy_to_user(_params, params, sizeof(*params))) {
522 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
524 struct snd_pcm_runtime *runtime;
527 if (PCM_RUNTIME_CHECK(substream))
529 runtime = substream->runtime;
530 snd_pcm_stream_lock_irq(substream);
531 switch (runtime->status->state) {
532 case SNDRV_PCM_STATE_SETUP:
533 case SNDRV_PCM_STATE_PREPARED:
536 snd_pcm_stream_unlock_irq(substream);
539 snd_pcm_stream_unlock_irq(substream);
540 if (atomic_read(&substream->mmap_count))
542 if (substream->ops->hw_free)
543 result = substream->ops->hw_free(substream);
544 runtime->status->state = SNDRV_PCM_STATE_OPEN;
545 pm_qos_remove_request(substream->latency_pm_qos_req);
546 substream->latency_pm_qos_req = NULL;
550 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
551 struct snd_pcm_sw_params *params)
553 struct snd_pcm_runtime *runtime;
556 if (PCM_RUNTIME_CHECK(substream))
558 runtime = substream->runtime;
559 snd_pcm_stream_lock_irq(substream);
560 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
561 snd_pcm_stream_unlock_irq(substream);
564 snd_pcm_stream_unlock_irq(substream);
566 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
568 if (params->avail_min == 0)
570 if (params->silence_size >= runtime->boundary) {
571 if (params->silence_threshold != 0)
574 if (params->silence_size > params->silence_threshold)
576 if (params->silence_threshold > runtime->buffer_size)
580 snd_pcm_stream_lock_irq(substream);
581 runtime->tstamp_mode = params->tstamp_mode;
582 runtime->period_step = params->period_step;
583 runtime->control->avail_min = params->avail_min;
584 runtime->start_threshold = params->start_threshold;
585 runtime->stop_threshold = params->stop_threshold;
586 runtime->silence_threshold = params->silence_threshold;
587 runtime->silence_size = params->silence_size;
588 params->boundary = runtime->boundary;
589 if (snd_pcm_running(substream)) {
590 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
591 runtime->silence_size > 0)
592 snd_pcm_playback_silence(substream, ULONG_MAX);
593 err = snd_pcm_update_state(substream, runtime);
595 snd_pcm_stream_unlock_irq(substream);
599 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
600 struct snd_pcm_sw_params __user * _params)
602 struct snd_pcm_sw_params params;
604 if (copy_from_user(¶ms, _params, sizeof(params)))
606 err = snd_pcm_sw_params(substream, ¶ms);
607 if (copy_to_user(_params, ¶ms, sizeof(params)))
612 int snd_pcm_status(struct snd_pcm_substream *substream,
613 struct snd_pcm_status *status)
615 struct snd_pcm_runtime *runtime = substream->runtime;
617 snd_pcm_stream_lock_irq(substream);
618 status->state = runtime->status->state;
619 status->suspended_state = runtime->status->suspended_state;
620 if (status->state == SNDRV_PCM_STATE_OPEN)
622 status->trigger_tstamp = runtime->trigger_tstamp;
623 if (snd_pcm_running(substream)) {
624 snd_pcm_update_hw_ptr(substream);
625 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
626 status->tstamp = runtime->status->tstamp;
630 snd_pcm_gettime(runtime, &status->tstamp);
632 status->appl_ptr = runtime->control->appl_ptr;
633 status->hw_ptr = runtime->status->hw_ptr;
634 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
635 status->avail = snd_pcm_playback_avail(runtime);
636 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
637 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
638 status->delay = runtime->buffer_size - status->avail;
639 status->delay += runtime->delay;
643 status->avail = snd_pcm_capture_avail(runtime);
644 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
645 status->delay = status->avail + runtime->delay;
649 status->avail_max = runtime->avail_max;
650 status->overrange = runtime->overrange;
651 runtime->avail_max = 0;
652 runtime->overrange = 0;
654 snd_pcm_stream_unlock_irq(substream);
658 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
659 struct snd_pcm_status __user * _status)
661 struct snd_pcm_status status;
664 memset(&status, 0, sizeof(status));
665 res = snd_pcm_status(substream, &status);
668 if (copy_to_user(_status, &status, sizeof(status)))
673 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
674 struct snd_pcm_channel_info * info)
676 struct snd_pcm_runtime *runtime;
677 unsigned int channel;
679 channel = info->channel;
680 runtime = substream->runtime;
681 snd_pcm_stream_lock_irq(substream);
682 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
683 snd_pcm_stream_unlock_irq(substream);
686 snd_pcm_stream_unlock_irq(substream);
687 if (channel >= runtime->channels)
689 memset(info, 0, sizeof(*info));
690 info->channel = channel;
691 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
694 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
695 struct snd_pcm_channel_info __user * _info)
697 struct snd_pcm_channel_info info;
700 if (copy_from_user(&info, _info, sizeof(info)))
702 res = snd_pcm_channel_info(substream, &info);
705 if (copy_to_user(_info, &info, sizeof(info)))
710 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
712 struct snd_pcm_runtime *runtime = substream->runtime;
713 if (runtime->trigger_master == NULL)
715 if (runtime->trigger_master == substream) {
716 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
718 snd_pcm_trigger_tstamp(runtime->trigger_master);
719 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
721 runtime->trigger_master = NULL;
725 int (*pre_action)(struct snd_pcm_substream *substream, int state);
726 int (*do_action)(struct snd_pcm_substream *substream, int state);
727 void (*undo_action)(struct snd_pcm_substream *substream, int state);
728 void (*post_action)(struct snd_pcm_substream *substream, int state);
732 * this functions is core for handling of linked stream
733 * Note: the stream state might be changed also on failure
734 * Note2: call with calling stream lock + link lock
736 static int snd_pcm_action_group(struct action_ops *ops,
737 struct snd_pcm_substream *substream,
738 int state, int do_lock)
740 struct snd_pcm_substream *s = NULL;
741 struct snd_pcm_substream *s1;
744 snd_pcm_group_for_each_entry(s, substream) {
745 if (do_lock && s != substream)
746 spin_lock_nested(&s->self_group.lock,
747 SINGLE_DEPTH_NESTING);
748 res = ops->pre_action(s, state);
752 snd_pcm_group_for_each_entry(s, substream) {
753 res = ops->do_action(s, state);
755 if (ops->undo_action) {
756 snd_pcm_group_for_each_entry(s1, substream) {
757 if (s1 == s) /* failed stream */
759 ops->undo_action(s1, state);
762 s = NULL; /* unlock all */
766 snd_pcm_group_for_each_entry(s, substream) {
767 ops->post_action(s, state);
772 snd_pcm_group_for_each_entry(s1, substream) {
774 spin_unlock(&s1->self_group.lock);
775 if (s1 == s) /* end */
783 * Note: call with stream lock
785 static int snd_pcm_action_single(struct action_ops *ops,
786 struct snd_pcm_substream *substream,
791 res = ops->pre_action(substream, state);
794 res = ops->do_action(substream, state);
796 ops->post_action(substream, state);
797 else if (ops->undo_action)
798 ops->undo_action(substream, state);
803 * Note: call with stream lock
805 static int snd_pcm_action(struct action_ops *ops,
806 struct snd_pcm_substream *substream,
811 if (snd_pcm_stream_linked(substream)) {
812 if (!spin_trylock(&substream->group->lock)) {
813 spin_unlock(&substream->self_group.lock);
814 spin_lock(&substream->group->lock);
815 spin_lock(&substream->self_group.lock);
817 res = snd_pcm_action_group(ops, substream, state, 1);
818 spin_unlock(&substream->group->lock);
820 res = snd_pcm_action_single(ops, substream, state);
826 * Note: don't use any locks before
828 static int snd_pcm_action_lock_irq(struct action_ops *ops,
829 struct snd_pcm_substream *substream,
834 read_lock_irq(&snd_pcm_link_rwlock);
835 if (snd_pcm_stream_linked(substream)) {
836 spin_lock(&substream->group->lock);
837 spin_lock(&substream->self_group.lock);
838 res = snd_pcm_action_group(ops, substream, state, 1);
839 spin_unlock(&substream->self_group.lock);
840 spin_unlock(&substream->group->lock);
842 spin_lock(&substream->self_group.lock);
843 res = snd_pcm_action_single(ops, substream, state);
844 spin_unlock(&substream->self_group.lock);
846 read_unlock_irq(&snd_pcm_link_rwlock);
852 static int snd_pcm_action_nonatomic(struct action_ops *ops,
853 struct snd_pcm_substream *substream,
858 down_read(&snd_pcm_link_rwsem);
859 if (snd_pcm_stream_linked(substream))
860 res = snd_pcm_action_group(ops, substream, state, 0);
862 res = snd_pcm_action_single(ops, substream, state);
863 up_read(&snd_pcm_link_rwsem);
870 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
872 struct snd_pcm_runtime *runtime = substream->runtime;
873 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
875 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
876 !snd_pcm_playback_data(substream))
878 runtime->trigger_master = substream;
882 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
884 if (substream->runtime->trigger_master != substream)
886 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
889 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
891 if (substream->runtime->trigger_master == substream)
892 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
895 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
897 struct snd_pcm_runtime *runtime = substream->runtime;
898 snd_pcm_trigger_tstamp(substream);
899 runtime->hw_ptr_jiffies = jiffies;
900 runtime->status->state = state;
901 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
902 runtime->silence_size > 0)
903 snd_pcm_playback_silence(substream, ULONG_MAX);
904 if (substream->timer)
905 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
906 &runtime->trigger_tstamp);
909 static struct action_ops snd_pcm_action_start = {
910 .pre_action = snd_pcm_pre_start,
911 .do_action = snd_pcm_do_start,
912 .undo_action = snd_pcm_undo_start,
913 .post_action = snd_pcm_post_start
917 * snd_pcm_start - start all linked streams
918 * @substream: the PCM substream instance
920 int snd_pcm_start(struct snd_pcm_substream *substream)
922 return snd_pcm_action(&snd_pcm_action_start, substream,
923 SNDRV_PCM_STATE_RUNNING);
929 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
931 struct snd_pcm_runtime *runtime = substream->runtime;
932 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
934 runtime->trigger_master = substream;
938 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
940 if (substream->runtime->trigger_master == substream &&
941 snd_pcm_running(substream))
942 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
943 return 0; /* unconditonally stop all substreams */
946 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
948 struct snd_pcm_runtime *runtime = substream->runtime;
949 if (runtime->status->state != state) {
950 snd_pcm_trigger_tstamp(substream);
951 if (substream->timer)
952 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
953 &runtime->trigger_tstamp);
954 runtime->status->state = state;
956 wake_up(&runtime->sleep);
957 wake_up(&runtime->tsleep);
960 static struct action_ops snd_pcm_action_stop = {
961 .pre_action = snd_pcm_pre_stop,
962 .do_action = snd_pcm_do_stop,
963 .post_action = snd_pcm_post_stop
967 * snd_pcm_stop - try to stop all running streams in the substream group
968 * @substream: the PCM substream instance
969 * @state: PCM state after stopping the stream
971 * The state of each stream is then changed to the given state unconditionally.
973 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
975 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
978 EXPORT_SYMBOL(snd_pcm_stop);
981 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
982 * @substream: the PCM substream
984 * After stopping, the state is changed to SETUP.
985 * Unlike snd_pcm_stop(), this affects only the given stream.
987 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
989 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
990 SNDRV_PCM_STATE_SETUP);
996 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
998 struct snd_pcm_runtime *runtime = substream->runtime;
999 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1002 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1004 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1006 runtime->trigger_master = substream;
1010 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1012 if (substream->runtime->trigger_master != substream)
1014 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1015 * a delta betwen the current jiffies, this gives a large enough
1016 * delta, effectively to skip the check once.
1018 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1019 return substream->ops->trigger(substream,
1020 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1021 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1024 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1026 if (substream->runtime->trigger_master == substream)
1027 substream->ops->trigger(substream,
1028 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1029 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1032 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1034 struct snd_pcm_runtime *runtime = substream->runtime;
1035 snd_pcm_trigger_tstamp(substream);
1037 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1038 if (substream->timer)
1039 snd_timer_notify(substream->timer,
1040 SNDRV_TIMER_EVENT_MPAUSE,
1041 &runtime->trigger_tstamp);
1042 wake_up(&runtime->sleep);
1043 wake_up(&runtime->tsleep);
1045 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1046 if (substream->timer)
1047 snd_timer_notify(substream->timer,
1048 SNDRV_TIMER_EVENT_MCONTINUE,
1049 &runtime->trigger_tstamp);
1053 static struct action_ops snd_pcm_action_pause = {
1054 .pre_action = snd_pcm_pre_pause,
1055 .do_action = snd_pcm_do_pause,
1056 .undo_action = snd_pcm_undo_pause,
1057 .post_action = snd_pcm_post_pause
1061 * Push/release the pause for all linked streams.
1063 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1065 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1071 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1073 struct snd_pcm_runtime *runtime = substream->runtime;
1074 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1076 runtime->trigger_master = substream;
1080 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1082 struct snd_pcm_runtime *runtime = substream->runtime;
1083 if (runtime->trigger_master != substream)
1085 if (! snd_pcm_running(substream))
1087 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1088 return 0; /* suspend unconditionally */
1091 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 snd_pcm_trigger_tstamp(substream);
1095 if (substream->timer)
1096 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1097 &runtime->trigger_tstamp);
1098 runtime->status->suspended_state = runtime->status->state;
1099 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1100 wake_up(&runtime->sleep);
1101 wake_up(&runtime->tsleep);
1104 static struct action_ops snd_pcm_action_suspend = {
1105 .pre_action = snd_pcm_pre_suspend,
1106 .do_action = snd_pcm_do_suspend,
1107 .post_action = snd_pcm_post_suspend
1111 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1112 * @substream: the PCM substream
1114 * After this call, all streams are changed to SUSPENDED state.
1116 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1119 unsigned long flags;
1124 snd_pcm_stream_lock_irqsave(substream, flags);
1125 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1126 snd_pcm_stream_unlock_irqrestore(substream, flags);
1130 EXPORT_SYMBOL(snd_pcm_suspend);
1133 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1134 * @pcm: the PCM instance
1136 * After this call, all streams are changed to SUSPENDED state.
1138 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1140 struct snd_pcm_substream *substream;
1141 int stream, err = 0;
1146 for (stream = 0; stream < 2; stream++) {
1147 for (substream = pcm->streams[stream].substream;
1148 substream; substream = substream->next) {
1149 /* FIXME: the open/close code should lock this as well */
1150 if (substream->runtime == NULL)
1152 err = snd_pcm_suspend(substream);
1153 if (err < 0 && err != -EBUSY)
1160 EXPORT_SYMBOL(snd_pcm_suspend_all);
1164 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1166 struct snd_pcm_runtime *runtime = substream->runtime;
1167 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1169 runtime->trigger_master = substream;
1173 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1175 struct snd_pcm_runtime *runtime = substream->runtime;
1176 if (runtime->trigger_master != substream)
1178 /* DMA not running previously? */
1179 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1180 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1181 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1183 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1186 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1188 if (substream->runtime->trigger_master == substream &&
1189 snd_pcm_running(substream))
1190 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1193 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1195 struct snd_pcm_runtime *runtime = substream->runtime;
1196 snd_pcm_trigger_tstamp(substream);
1197 if (substream->timer)
1198 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1199 &runtime->trigger_tstamp);
1200 runtime->status->state = runtime->status->suspended_state;
1203 static struct action_ops snd_pcm_action_resume = {
1204 .pre_action = snd_pcm_pre_resume,
1205 .do_action = snd_pcm_do_resume,
1206 .undo_action = snd_pcm_undo_resume,
1207 .post_action = snd_pcm_post_resume
1210 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1212 struct snd_card *card = substream->pcm->card;
1215 snd_power_lock(card);
1216 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1217 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1218 snd_power_unlock(card);
1224 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1229 #endif /* CONFIG_PM */
1234 * Change the RUNNING stream(s) to XRUN state.
1236 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1238 struct snd_card *card = substream->pcm->card;
1239 struct snd_pcm_runtime *runtime = substream->runtime;
1242 snd_power_lock(card);
1243 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1244 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1249 snd_pcm_stream_lock_irq(substream);
1250 switch (runtime->status->state) {
1251 case SNDRV_PCM_STATE_XRUN:
1252 result = 0; /* already there */
1254 case SNDRV_PCM_STATE_RUNNING:
1255 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1260 snd_pcm_stream_unlock_irq(substream);
1262 snd_power_unlock(card);
1269 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1271 struct snd_pcm_runtime *runtime = substream->runtime;
1272 switch (runtime->status->state) {
1273 case SNDRV_PCM_STATE_RUNNING:
1274 case SNDRV_PCM_STATE_PREPARED:
1275 case SNDRV_PCM_STATE_PAUSED:
1276 case SNDRV_PCM_STATE_SUSPENDED:
1283 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1285 struct snd_pcm_runtime *runtime = substream->runtime;
1286 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1289 runtime->hw_ptr_base = 0;
1290 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1291 runtime->status->hw_ptr % runtime->period_size;
1292 runtime->silence_start = runtime->status->hw_ptr;
1293 runtime->silence_filled = 0;
1297 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1299 struct snd_pcm_runtime *runtime = substream->runtime;
1300 runtime->control->appl_ptr = runtime->status->hw_ptr;
1301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1302 runtime->silence_size > 0)
1303 snd_pcm_playback_silence(substream, ULONG_MAX);
1306 static struct action_ops snd_pcm_action_reset = {
1307 .pre_action = snd_pcm_pre_reset,
1308 .do_action = snd_pcm_do_reset,
1309 .post_action = snd_pcm_post_reset
1312 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1314 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1320 /* we use the second argument for updating f_flags */
1321 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1324 struct snd_pcm_runtime *runtime = substream->runtime;
1325 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1326 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1328 if (snd_pcm_running(substream))
1330 substream->f_flags = f_flags;
1334 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1337 err = substream->ops->prepare(substream);
1340 return snd_pcm_do_reset(substream, 0);
1343 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1345 struct snd_pcm_runtime *runtime = substream->runtime;
1346 runtime->control->appl_ptr = runtime->status->hw_ptr;
1347 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1350 static struct action_ops snd_pcm_action_prepare = {
1351 .pre_action = snd_pcm_pre_prepare,
1352 .do_action = snd_pcm_do_prepare,
1353 .post_action = snd_pcm_post_prepare
1357 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1358 * @substream: the PCM substream instance
1359 * @file: file to refer f_flags
1361 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1365 struct snd_card *card = substream->pcm->card;
1369 f_flags = file->f_flags;
1371 f_flags = substream->f_flags;
1373 snd_power_lock(card);
1374 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1375 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1376 substream, f_flags);
1377 snd_power_unlock(card);
1385 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1387 substream->runtime->trigger_master = substream;
1391 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1393 struct snd_pcm_runtime *runtime = substream->runtime;
1394 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1395 switch (runtime->status->state) {
1396 case SNDRV_PCM_STATE_PREPARED:
1397 /* start playback stream if possible */
1398 if (! snd_pcm_playback_empty(substream)) {
1399 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1400 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1403 case SNDRV_PCM_STATE_RUNNING:
1404 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1410 /* stop running stream */
1411 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1412 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1413 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1414 snd_pcm_do_stop(substream, new_state);
1415 snd_pcm_post_stop(substream, new_state);
1421 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1425 static struct action_ops snd_pcm_action_drain_init = {
1426 .pre_action = snd_pcm_pre_drain_init,
1427 .do_action = snd_pcm_do_drain_init,
1428 .post_action = snd_pcm_post_drain_init
1431 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1434 * Drain the stream(s).
1435 * When the substream is linked, sync until the draining of all playback streams
1437 * After this call, all streams are supposed to be either SETUP or DRAINING
1438 * (capture only) state.
1440 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1443 struct snd_card *card;
1444 struct snd_pcm_runtime *runtime;
1445 struct snd_pcm_substream *s;
1450 card = substream->pcm->card;
1451 runtime = substream->runtime;
1453 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1456 snd_power_lock(card);
1457 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1458 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1460 snd_power_unlock(card);
1466 if (file->f_flags & O_NONBLOCK)
1468 } else if (substream->f_flags & O_NONBLOCK)
1471 down_read(&snd_pcm_link_rwsem);
1472 snd_pcm_stream_lock_irq(substream);
1474 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1475 snd_pcm_pause(substream, 0);
1477 /* pre-start/stop - all running streams are changed to DRAINING state */
1478 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1481 /* in non-blocking, we don't wait in ioctl but let caller poll */
1489 struct snd_pcm_runtime *to_check;
1490 if (signal_pending(current)) {
1491 result = -ERESTARTSYS;
1494 /* find a substream to drain */
1496 snd_pcm_group_for_each_entry(s, substream) {
1497 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1499 runtime = s->runtime;
1500 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1506 break; /* all drained */
1507 init_waitqueue_entry(&wait, current);
1508 add_wait_queue(&to_check->sleep, &wait);
1509 set_current_state(TASK_INTERRUPTIBLE);
1510 snd_pcm_stream_unlock_irq(substream);
1511 up_read(&snd_pcm_link_rwsem);
1512 snd_power_unlock(card);
1513 tout = schedule_timeout(10 * HZ);
1514 snd_power_lock(card);
1515 down_read(&snd_pcm_link_rwsem);
1516 snd_pcm_stream_lock_irq(substream);
1517 remove_wait_queue(&to_check->sleep, &wait);
1519 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1522 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1523 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1531 snd_pcm_stream_unlock_irq(substream);
1532 up_read(&snd_pcm_link_rwsem);
1533 snd_power_unlock(card);
1541 * Immediately put all linked substreams into SETUP state.
1543 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1545 struct snd_pcm_runtime *runtime;
1546 struct snd_card *card;
1549 if (PCM_RUNTIME_CHECK(substream))
1551 runtime = substream->runtime;
1552 card = substream->pcm->card;
1554 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1555 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1556 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1559 snd_pcm_stream_lock_irq(substream);
1561 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1562 snd_pcm_pause(substream, 0);
1564 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1565 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1566 snd_pcm_stream_unlock_irq(substream);
1572 /* WARNING: Don't forget to fput back the file */
1573 static struct file *snd_pcm_file_fd(int fd)
1576 struct inode *inode;
1582 inode = file->f_path.dentry->d_inode;
1583 if (!S_ISCHR(inode->i_mode) ||
1584 imajor(inode) != snd_major) {
1588 minor = iminor(inode);
1589 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1590 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1600 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1604 struct snd_pcm_file *pcm_file;
1605 struct snd_pcm_substream *substream1;
1607 file = snd_pcm_file_fd(fd);
1610 pcm_file = file->private_data;
1611 substream1 = pcm_file->substream;
1612 down_write(&snd_pcm_link_rwsem);
1613 write_lock_irq(&snd_pcm_link_rwlock);
1614 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1615 substream->runtime->status->state != substream1->runtime->status->state) {
1619 if (snd_pcm_stream_linked(substream1)) {
1623 if (!snd_pcm_stream_linked(substream)) {
1624 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1625 if (substream->group == NULL) {
1629 spin_lock_init(&substream->group->lock);
1630 INIT_LIST_HEAD(&substream->group->substreams);
1631 list_add_tail(&substream->link_list, &substream->group->substreams);
1632 substream->group->count = 1;
1634 list_add_tail(&substream1->link_list, &substream->group->substreams);
1635 substream->group->count++;
1636 substream1->group = substream->group;
1638 write_unlock_irq(&snd_pcm_link_rwlock);
1639 up_write(&snd_pcm_link_rwsem);
1644 static void relink_to_local(struct snd_pcm_substream *substream)
1646 substream->group = &substream->self_group;
1647 INIT_LIST_HEAD(&substream->self_group.substreams);
1648 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1651 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1653 struct snd_pcm_substream *s;
1656 down_write(&snd_pcm_link_rwsem);
1657 write_lock_irq(&snd_pcm_link_rwlock);
1658 if (!snd_pcm_stream_linked(substream)) {
1662 list_del(&substream->link_list);
1663 substream->group->count--;
1664 if (substream->group->count == 1) { /* detach the last stream, too */
1665 snd_pcm_group_for_each_entry(s, substream) {
1669 kfree(substream->group);
1671 relink_to_local(substream);
1673 write_unlock_irq(&snd_pcm_link_rwlock);
1674 up_write(&snd_pcm_link_rwsem);
1681 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1682 struct snd_pcm_hw_rule *rule)
1684 struct snd_interval t;
1685 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1686 hw_param_interval_c(params, rule->deps[1]), &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
1693 struct snd_interval t;
1694 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1695 hw_param_interval_c(params, rule->deps[1]), &t);
1696 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1699 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1700 struct snd_pcm_hw_rule *rule)
1702 struct snd_interval t;
1703 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1704 hw_param_interval_c(params, rule->deps[1]),
1705 (unsigned long) rule->private, &t);
1706 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1709 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1710 struct snd_pcm_hw_rule *rule)
1712 struct snd_interval t;
1713 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1714 (unsigned long) rule->private,
1715 hw_param_interval_c(params, rule->deps[1]), &t);
1716 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1719 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1720 struct snd_pcm_hw_rule *rule)
1723 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1725 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1727 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1729 if (! snd_mask_test(mask, k))
1731 bits = snd_pcm_format_physical_width(k);
1733 continue; /* ignore invalid formats */
1734 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1735 snd_mask_reset(&m, k);
1737 return snd_mask_refine(mask, &m);
1740 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
1743 struct snd_interval t;
1749 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1751 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1753 bits = snd_pcm_format_physical_width(k);
1755 continue; /* ignore invalid formats */
1756 if (t.min > (unsigned)bits)
1758 if (t.max < (unsigned)bits)
1762 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1765 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1766 #error "Change this table"
1769 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1770 48000, 64000, 88200, 96000, 176400, 192000 };
1772 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1773 .count = ARRAY_SIZE(rates),
1777 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1778 struct snd_pcm_hw_rule *rule)
1780 struct snd_pcm_hardware *hw = rule->private;
1781 return snd_interval_list(hw_param_interval(params, rule->var),
1782 snd_pcm_known_rates.count,
1783 snd_pcm_known_rates.list, hw->rates);
1786 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1787 struct snd_pcm_hw_rule *rule)
1789 struct snd_interval t;
1790 struct snd_pcm_substream *substream = rule->private;
1792 t.max = substream->buffer_bytes_max;
1796 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1799 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1801 struct snd_pcm_runtime *runtime = substream->runtime;
1802 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1805 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1806 snd_mask_any(constrs_mask(constrs, k));
1809 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1810 snd_interval_any(constrs_interval(constrs, k));
1813 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1819 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1820 snd_pcm_hw_rule_format, NULL,
1821 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1824 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1825 snd_pcm_hw_rule_sample_bits, NULL,
1826 SNDRV_PCM_HW_PARAM_FORMAT,
1827 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1831 snd_pcm_hw_rule_div, NULL,
1832 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1836 snd_pcm_hw_rule_mul, NULL,
1837 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1841 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1842 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1846 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1847 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1850 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1851 snd_pcm_hw_rule_div, NULL,
1852 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1856 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1860 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1861 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1862 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1865 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1866 snd_pcm_hw_rule_div, NULL,
1867 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1870 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1871 snd_pcm_hw_rule_div, NULL,
1872 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1875 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1876 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1877 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1880 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1881 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1882 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1885 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1886 snd_pcm_hw_rule_mul, NULL,
1887 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1890 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1891 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1892 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1895 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1896 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1897 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1900 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1901 snd_pcm_hw_rule_muldivk, (void*) 8,
1902 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1905 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1906 snd_pcm_hw_rule_muldivk, (void*) 8,
1907 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1910 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1911 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1912 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1915 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1916 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1917 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1923 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1925 struct snd_pcm_runtime *runtime = substream->runtime;
1926 struct snd_pcm_hardware *hw = &runtime->hw;
1928 unsigned int mask = 0;
1930 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1931 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1932 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1933 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1934 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1935 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1936 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1937 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1938 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1939 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1940 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1942 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1946 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1950 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1954 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1955 hw->channels_min, hw->channels_max);
1959 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1960 hw->rate_min, hw->rate_max);
1964 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1965 hw->period_bytes_min, hw->period_bytes_max);
1969 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1970 hw->periods_min, hw->periods_max);
1974 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1975 hw->period_bytes_min, hw->buffer_bytes_max);
1979 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1980 snd_pcm_hw_rule_buffer_bytes_max, substream,
1981 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1986 if (runtime->dma_bytes) {
1987 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1992 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1993 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1994 snd_pcm_hw_rule_rate, hw,
1995 SNDRV_PCM_HW_PARAM_RATE, -1);
2000 /* FIXME: this belong to lowlevel */
2001 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2006 static void pcm_release_private(struct snd_pcm_substream *substream)
2008 snd_pcm_unlink(substream);
2011 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2013 substream->ref_count--;
2014 if (substream->ref_count > 0)
2017 snd_pcm_drop(substream);
2018 if (substream->hw_opened) {
2019 if (substream->ops->hw_free != NULL)
2020 substream->ops->hw_free(substream);
2021 substream->ops->close(substream);
2022 substream->hw_opened = 0;
2024 if (substream->pcm_release) {
2025 substream->pcm_release(substream);
2026 substream->pcm_release = NULL;
2028 snd_pcm_detach_substream(substream);
2031 EXPORT_SYMBOL(snd_pcm_release_substream);
2033 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2035 struct snd_pcm_substream **rsubstream)
2037 struct snd_pcm_substream *substream;
2040 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2043 if (substream->ref_count > 1) {
2044 *rsubstream = substream;
2048 err = snd_pcm_hw_constraints_init(substream);
2050 snd_printd("snd_pcm_hw_constraints_init failed\n");
2054 if ((err = substream->ops->open(substream)) < 0)
2057 substream->hw_opened = 1;
2059 err = snd_pcm_hw_constraints_complete(substream);
2061 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2065 *rsubstream = substream;
2069 snd_pcm_release_substream(substream);
2073 EXPORT_SYMBOL(snd_pcm_open_substream);
2075 static int snd_pcm_open_file(struct file *file,
2076 struct snd_pcm *pcm,
2078 struct snd_pcm_file **rpcm_file)
2080 struct snd_pcm_file *pcm_file;
2081 struct snd_pcm_substream *substream;
2082 struct snd_pcm_str *str;
2088 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2092 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2093 if (pcm_file == NULL) {
2094 snd_pcm_release_substream(substream);
2097 pcm_file->substream = substream;
2098 if (substream->ref_count == 1) {
2099 str = substream->pstr;
2100 substream->file = pcm_file;
2101 substream->pcm_release = pcm_release_private;
2103 file->private_data = pcm_file;
2105 *rpcm_file = pcm_file;
2109 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2111 struct snd_pcm *pcm;
2113 pcm = snd_lookup_minor_data(iminor(inode),
2114 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2115 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2118 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2120 struct snd_pcm *pcm;
2122 pcm = snd_lookup_minor_data(iminor(inode),
2123 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2124 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2127 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2130 struct snd_pcm_file *pcm_file;
2137 err = snd_card_file_add(pcm->card, file);
2140 if (!try_module_get(pcm->card->module)) {
2144 init_waitqueue_entry(&wait, current);
2145 add_wait_queue(&pcm->open_wait, &wait);
2146 mutex_lock(&pcm->open_mutex);
2148 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2151 if (err == -EAGAIN) {
2152 if (file->f_flags & O_NONBLOCK) {
2158 set_current_state(TASK_INTERRUPTIBLE);
2159 mutex_unlock(&pcm->open_mutex);
2161 mutex_lock(&pcm->open_mutex);
2162 if (signal_pending(current)) {
2167 remove_wait_queue(&pcm->open_wait, &wait);
2168 mutex_unlock(&pcm->open_mutex);
2174 module_put(pcm->card->module);
2176 snd_card_file_remove(pcm->card, file);
2181 static int snd_pcm_release(struct inode *inode, struct file *file)
2183 struct snd_pcm *pcm;
2184 struct snd_pcm_substream *substream;
2185 struct snd_pcm_file *pcm_file;
2187 pcm_file = file->private_data;
2188 substream = pcm_file->substream;
2189 if (snd_BUG_ON(!substream))
2191 pcm = substream->pcm;
2192 mutex_lock(&pcm->open_mutex);
2193 snd_pcm_release_substream(substream);
2195 mutex_unlock(&pcm->open_mutex);
2196 wake_up(&pcm->open_wait);
2197 module_put(pcm->card->module);
2198 snd_card_file_remove(pcm->card, file);
2202 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2203 snd_pcm_uframes_t frames)
2205 struct snd_pcm_runtime *runtime = substream->runtime;
2206 snd_pcm_sframes_t appl_ptr;
2207 snd_pcm_sframes_t ret;
2208 snd_pcm_sframes_t hw_avail;
2213 snd_pcm_stream_lock_irq(substream);
2214 switch (runtime->status->state) {
2215 case SNDRV_PCM_STATE_PREPARED:
2217 case SNDRV_PCM_STATE_DRAINING:
2218 case SNDRV_PCM_STATE_RUNNING:
2219 if (snd_pcm_update_hw_ptr(substream) >= 0)
2222 case SNDRV_PCM_STATE_XRUN:
2225 case SNDRV_PCM_STATE_SUSPENDED:
2233 hw_avail = snd_pcm_playback_hw_avail(runtime);
2234 if (hw_avail <= 0) {
2238 if (frames > (snd_pcm_uframes_t)hw_avail)
2240 appl_ptr = runtime->control->appl_ptr - frames;
2242 appl_ptr += runtime->boundary;
2243 runtime->control->appl_ptr = appl_ptr;
2246 snd_pcm_stream_unlock_irq(substream);
2250 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2251 snd_pcm_uframes_t frames)
2253 struct snd_pcm_runtime *runtime = substream->runtime;
2254 snd_pcm_sframes_t appl_ptr;
2255 snd_pcm_sframes_t ret;
2256 snd_pcm_sframes_t hw_avail;
2261 snd_pcm_stream_lock_irq(substream);
2262 switch (runtime->status->state) {
2263 case SNDRV_PCM_STATE_PREPARED:
2264 case SNDRV_PCM_STATE_DRAINING:
2266 case SNDRV_PCM_STATE_RUNNING:
2267 if (snd_pcm_update_hw_ptr(substream) >= 0)
2270 case SNDRV_PCM_STATE_XRUN:
2273 case SNDRV_PCM_STATE_SUSPENDED:
2281 hw_avail = snd_pcm_capture_hw_avail(runtime);
2282 if (hw_avail <= 0) {
2286 if (frames > (snd_pcm_uframes_t)hw_avail)
2288 appl_ptr = runtime->control->appl_ptr - frames;
2290 appl_ptr += runtime->boundary;
2291 runtime->control->appl_ptr = appl_ptr;
2294 snd_pcm_stream_unlock_irq(substream);
2298 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2299 snd_pcm_uframes_t frames)
2301 struct snd_pcm_runtime *runtime = substream->runtime;
2302 snd_pcm_sframes_t appl_ptr;
2303 snd_pcm_sframes_t ret;
2304 snd_pcm_sframes_t avail;
2309 snd_pcm_stream_lock_irq(substream);
2310 switch (runtime->status->state) {
2311 case SNDRV_PCM_STATE_PREPARED:
2312 case SNDRV_PCM_STATE_PAUSED:
2314 case SNDRV_PCM_STATE_DRAINING:
2315 case SNDRV_PCM_STATE_RUNNING:
2316 if (snd_pcm_update_hw_ptr(substream) >= 0)
2319 case SNDRV_PCM_STATE_XRUN:
2322 case SNDRV_PCM_STATE_SUSPENDED:
2330 avail = snd_pcm_playback_avail(runtime);
2335 if (frames > (snd_pcm_uframes_t)avail)
2337 appl_ptr = runtime->control->appl_ptr + frames;
2338 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2339 appl_ptr -= runtime->boundary;
2340 runtime->control->appl_ptr = appl_ptr;
2343 snd_pcm_stream_unlock_irq(substream);
2347 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2348 snd_pcm_uframes_t frames)
2350 struct snd_pcm_runtime *runtime = substream->runtime;
2351 snd_pcm_sframes_t appl_ptr;
2352 snd_pcm_sframes_t ret;
2353 snd_pcm_sframes_t avail;
2358 snd_pcm_stream_lock_irq(substream);
2359 switch (runtime->status->state) {
2360 case SNDRV_PCM_STATE_PREPARED:
2361 case SNDRV_PCM_STATE_DRAINING:
2362 case SNDRV_PCM_STATE_PAUSED:
2364 case SNDRV_PCM_STATE_RUNNING:
2365 if (snd_pcm_update_hw_ptr(substream) >= 0)
2368 case SNDRV_PCM_STATE_XRUN:
2371 case SNDRV_PCM_STATE_SUSPENDED:
2379 avail = snd_pcm_capture_avail(runtime);
2384 if (frames > (snd_pcm_uframes_t)avail)
2386 appl_ptr = runtime->control->appl_ptr + frames;
2387 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2388 appl_ptr -= runtime->boundary;
2389 runtime->control->appl_ptr = appl_ptr;
2392 snd_pcm_stream_unlock_irq(substream);
2396 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2398 struct snd_pcm_runtime *runtime = substream->runtime;
2401 snd_pcm_stream_lock_irq(substream);
2402 switch (runtime->status->state) {
2403 case SNDRV_PCM_STATE_DRAINING:
2404 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2406 case SNDRV_PCM_STATE_RUNNING:
2407 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2410 case SNDRV_PCM_STATE_PREPARED:
2411 case SNDRV_PCM_STATE_SUSPENDED:
2414 case SNDRV_PCM_STATE_XRUN:
2422 snd_pcm_stream_unlock_irq(substream);
2426 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2427 snd_pcm_sframes_t __user *res)
2429 struct snd_pcm_runtime *runtime = substream->runtime;
2431 snd_pcm_sframes_t n = 0;
2433 snd_pcm_stream_lock_irq(substream);
2434 switch (runtime->status->state) {
2435 case SNDRV_PCM_STATE_DRAINING:
2436 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2438 case SNDRV_PCM_STATE_RUNNING:
2439 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2442 case SNDRV_PCM_STATE_PREPARED:
2443 case SNDRV_PCM_STATE_SUSPENDED:
2445 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2446 n = snd_pcm_playback_hw_avail(runtime);
2448 n = snd_pcm_capture_avail(runtime);
2449 n += runtime->delay;
2451 case SNDRV_PCM_STATE_XRUN:
2459 snd_pcm_stream_unlock_irq(substream);
2461 if (put_user(n, res))
2466 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2467 struct snd_pcm_sync_ptr __user *_sync_ptr)
2469 struct snd_pcm_runtime *runtime = substream->runtime;
2470 struct snd_pcm_sync_ptr sync_ptr;
2471 volatile struct snd_pcm_mmap_status *status;
2472 volatile struct snd_pcm_mmap_control *control;
2475 memset(&sync_ptr, 0, sizeof(sync_ptr));
2476 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2478 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2480 status = runtime->status;
2481 control = runtime->control;
2482 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2483 err = snd_pcm_hwsync(substream);
2487 snd_pcm_stream_lock_irq(substream);
2488 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2489 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2491 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2492 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2493 control->avail_min = sync_ptr.c.control.avail_min;
2495 sync_ptr.c.control.avail_min = control->avail_min;
2496 sync_ptr.s.status.state = status->state;
2497 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2498 sync_ptr.s.status.tstamp = status->tstamp;
2499 sync_ptr.s.status.suspended_state = status->suspended_state;
2500 snd_pcm_stream_unlock_irq(substream);
2501 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2506 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2508 struct snd_pcm_runtime *runtime = substream->runtime;
2511 if (get_user(arg, _arg))
2513 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2515 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2516 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2517 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2521 static int snd_pcm_common_ioctl1(struct file *file,
2522 struct snd_pcm_substream *substream,
2523 unsigned int cmd, void __user *arg)
2526 case SNDRV_PCM_IOCTL_PVERSION:
2527 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2528 case SNDRV_PCM_IOCTL_INFO:
2529 return snd_pcm_info_user(substream, arg);
2530 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2532 case SNDRV_PCM_IOCTL_TTSTAMP:
2533 return snd_pcm_tstamp(substream, arg);
2534 case SNDRV_PCM_IOCTL_HW_REFINE:
2535 return snd_pcm_hw_refine_user(substream, arg);
2536 case SNDRV_PCM_IOCTL_HW_PARAMS:
2537 return snd_pcm_hw_params_user(substream, arg);
2538 case SNDRV_PCM_IOCTL_HW_FREE:
2539 return snd_pcm_hw_free(substream);
2540 case SNDRV_PCM_IOCTL_SW_PARAMS:
2541 return snd_pcm_sw_params_user(substream, arg);
2542 case SNDRV_PCM_IOCTL_STATUS:
2543 return snd_pcm_status_user(substream, arg);
2544 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2545 return snd_pcm_channel_info_user(substream, arg);
2546 case SNDRV_PCM_IOCTL_PREPARE:
2547 return snd_pcm_prepare(substream, file);
2548 case SNDRV_PCM_IOCTL_RESET:
2549 return snd_pcm_reset(substream);
2550 case SNDRV_PCM_IOCTL_START:
2551 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2552 case SNDRV_PCM_IOCTL_LINK:
2553 return snd_pcm_link(substream, (int)(unsigned long) arg);
2554 case SNDRV_PCM_IOCTL_UNLINK:
2555 return snd_pcm_unlink(substream);
2556 case SNDRV_PCM_IOCTL_RESUME:
2557 return snd_pcm_resume(substream);
2558 case SNDRV_PCM_IOCTL_XRUN:
2559 return snd_pcm_xrun(substream);
2560 case SNDRV_PCM_IOCTL_HWSYNC:
2561 return snd_pcm_hwsync(substream);
2562 case SNDRV_PCM_IOCTL_DELAY:
2563 return snd_pcm_delay(substream, arg);
2564 case SNDRV_PCM_IOCTL_SYNC_PTR:
2565 return snd_pcm_sync_ptr(substream, arg);
2566 #ifdef CONFIG_SND_SUPPORT_OLD_API
2567 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2568 return snd_pcm_hw_refine_old_user(substream, arg);
2569 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2570 return snd_pcm_hw_params_old_user(substream, arg);
2572 case SNDRV_PCM_IOCTL_DRAIN:
2573 return snd_pcm_drain(substream, file);
2574 case SNDRV_PCM_IOCTL_DROP:
2575 return snd_pcm_drop(substream);
2576 case SNDRV_PCM_IOCTL_PAUSE:
2579 snd_pcm_stream_lock_irq(substream);
2580 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2581 snd_pcm_stream_unlock_irq(substream);
2585 snd_printd("unknown ioctl = 0x%x\n", cmd);
2589 static int snd_pcm_playback_ioctl1(struct file *file,
2590 struct snd_pcm_substream *substream,
2591 unsigned int cmd, void __user *arg)
2593 if (snd_BUG_ON(!substream))
2595 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2598 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2600 struct snd_xferi xferi;
2601 struct snd_xferi __user *_xferi = arg;
2602 struct snd_pcm_runtime *runtime = substream->runtime;
2603 snd_pcm_sframes_t result;
2604 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2606 if (put_user(0, &_xferi->result))
2608 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2610 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2611 __put_user(result, &_xferi->result);
2612 return result < 0 ? result : 0;
2614 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2616 struct snd_xfern xfern;
2617 struct snd_xfern __user *_xfern = arg;
2618 struct snd_pcm_runtime *runtime = substream->runtime;
2620 snd_pcm_sframes_t result;
2621 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2623 if (runtime->channels > 128)
2625 if (put_user(0, &_xfern->result))
2627 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2630 bufs = memdup_user(xfern.bufs,
2631 sizeof(void *) * runtime->channels);
2633 return PTR_ERR(bufs);
2634 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2636 __put_user(result, &_xfern->result);
2637 return result < 0 ? result : 0;
2639 case SNDRV_PCM_IOCTL_REWIND:
2641 snd_pcm_uframes_t frames;
2642 snd_pcm_uframes_t __user *_frames = arg;
2643 snd_pcm_sframes_t result;
2644 if (get_user(frames, _frames))
2646 if (put_user(0, _frames))
2648 result = snd_pcm_playback_rewind(substream, frames);
2649 __put_user(result, _frames);
2650 return result < 0 ? result : 0;
2652 case SNDRV_PCM_IOCTL_FORWARD:
2654 snd_pcm_uframes_t frames;
2655 snd_pcm_uframes_t __user *_frames = arg;
2656 snd_pcm_sframes_t result;
2657 if (get_user(frames, _frames))
2659 if (put_user(0, _frames))
2661 result = snd_pcm_playback_forward(substream, frames);
2662 __put_user(result, _frames);
2663 return result < 0 ? result : 0;
2666 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2669 static int snd_pcm_capture_ioctl1(struct file *file,
2670 struct snd_pcm_substream *substream,
2671 unsigned int cmd, void __user *arg)
2673 if (snd_BUG_ON(!substream))
2675 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2678 case SNDRV_PCM_IOCTL_READI_FRAMES:
2680 struct snd_xferi xferi;
2681 struct snd_xferi __user *_xferi = arg;
2682 struct snd_pcm_runtime *runtime = substream->runtime;
2683 snd_pcm_sframes_t result;
2684 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2686 if (put_user(0, &_xferi->result))
2688 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2690 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2691 __put_user(result, &_xferi->result);
2692 return result < 0 ? result : 0;
2694 case SNDRV_PCM_IOCTL_READN_FRAMES:
2696 struct snd_xfern xfern;
2697 struct snd_xfern __user *_xfern = arg;
2698 struct snd_pcm_runtime *runtime = substream->runtime;
2700 snd_pcm_sframes_t result;
2701 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2703 if (runtime->channels > 128)
2705 if (put_user(0, &_xfern->result))
2707 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2710 bufs = memdup_user(xfern.bufs,
2711 sizeof(void *) * runtime->channels);
2713 return PTR_ERR(bufs);
2714 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2716 __put_user(result, &_xfern->result);
2717 return result < 0 ? result : 0;
2719 case SNDRV_PCM_IOCTL_REWIND:
2721 snd_pcm_uframes_t frames;
2722 snd_pcm_uframes_t __user *_frames = arg;
2723 snd_pcm_sframes_t result;
2724 if (get_user(frames, _frames))
2726 if (put_user(0, _frames))
2728 result = snd_pcm_capture_rewind(substream, frames);
2729 __put_user(result, _frames);
2730 return result < 0 ? result : 0;
2732 case SNDRV_PCM_IOCTL_FORWARD:
2734 snd_pcm_uframes_t frames;
2735 snd_pcm_uframes_t __user *_frames = arg;
2736 snd_pcm_sframes_t result;
2737 if (get_user(frames, _frames))
2739 if (put_user(0, _frames))
2741 result = snd_pcm_capture_forward(substream, frames);
2742 __put_user(result, _frames);
2743 return result < 0 ? result : 0;
2746 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2749 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2752 struct snd_pcm_file *pcm_file;
2754 pcm_file = file->private_data;
2756 if (((cmd >> 8) & 0xff) != 'A')
2759 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2760 (void __user *)arg);
2763 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2766 struct snd_pcm_file *pcm_file;
2768 pcm_file = file->private_data;
2770 if (((cmd >> 8) & 0xff) != 'A')
2773 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2774 (void __user *)arg);
2777 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2778 unsigned int cmd, void *arg)
2783 fs = snd_enter_user();
2784 switch (substream->stream) {
2785 case SNDRV_PCM_STREAM_PLAYBACK:
2786 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2787 (void __user *)arg);
2789 case SNDRV_PCM_STREAM_CAPTURE:
2790 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2791 (void __user *)arg);
2801 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2803 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2806 struct snd_pcm_file *pcm_file;
2807 struct snd_pcm_substream *substream;
2808 struct snd_pcm_runtime *runtime;
2809 snd_pcm_sframes_t result;
2811 pcm_file = file->private_data;
2812 substream = pcm_file->substream;
2813 if (PCM_RUNTIME_CHECK(substream))
2815 runtime = substream->runtime;
2816 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2818 if (!frame_aligned(runtime, count))
2820 count = bytes_to_frames(runtime, count);
2821 result = snd_pcm_lib_read(substream, buf, count);
2823 result = frames_to_bytes(runtime, result);
2827 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2828 size_t count, loff_t * offset)
2830 struct snd_pcm_file *pcm_file;
2831 struct snd_pcm_substream *substream;
2832 struct snd_pcm_runtime *runtime;
2833 snd_pcm_sframes_t result;
2835 pcm_file = file->private_data;
2836 substream = pcm_file->substream;
2837 if (PCM_RUNTIME_CHECK(substream))
2839 runtime = substream->runtime;
2840 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2842 if (!frame_aligned(runtime, count))
2844 count = bytes_to_frames(runtime, count);
2845 result = snd_pcm_lib_write(substream, buf, count);
2847 result = frames_to_bytes(runtime, result);
2851 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2852 unsigned long nr_segs, loff_t pos)
2855 struct snd_pcm_file *pcm_file;
2856 struct snd_pcm_substream *substream;
2857 struct snd_pcm_runtime *runtime;
2858 snd_pcm_sframes_t result;
2861 snd_pcm_uframes_t frames;
2863 pcm_file = iocb->ki_filp->private_data;
2864 substream = pcm_file->substream;
2865 if (PCM_RUNTIME_CHECK(substream))
2867 runtime = substream->runtime;
2868 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2870 if (nr_segs > 1024 || nr_segs != runtime->channels)
2872 if (!frame_aligned(runtime, iov->iov_len))
2874 frames = bytes_to_samples(runtime, iov->iov_len);
2875 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2878 for (i = 0; i < nr_segs; ++i)
2879 bufs[i] = iov[i].iov_base;
2880 result = snd_pcm_lib_readv(substream, bufs, frames);
2882 result = frames_to_bytes(runtime, result);
2887 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2888 unsigned long nr_segs, loff_t pos)
2890 struct snd_pcm_file *pcm_file;
2891 struct snd_pcm_substream *substream;
2892 struct snd_pcm_runtime *runtime;
2893 snd_pcm_sframes_t result;
2896 snd_pcm_uframes_t frames;
2898 pcm_file = iocb->ki_filp->private_data;
2899 substream = pcm_file->substream;
2900 if (PCM_RUNTIME_CHECK(substream))
2902 runtime = substream->runtime;
2903 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2905 if (nr_segs > 128 || nr_segs != runtime->channels ||
2906 !frame_aligned(runtime, iov->iov_len))
2908 frames = bytes_to_samples(runtime, iov->iov_len);
2909 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2912 for (i = 0; i < nr_segs; ++i)
2913 bufs[i] = iov[i].iov_base;
2914 result = snd_pcm_lib_writev(substream, bufs, frames);
2916 result = frames_to_bytes(runtime, result);
2921 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2923 struct snd_pcm_file *pcm_file;
2924 struct snd_pcm_substream *substream;
2925 struct snd_pcm_runtime *runtime;
2927 snd_pcm_uframes_t avail;
2929 pcm_file = file->private_data;
2931 substream = pcm_file->substream;
2932 if (PCM_RUNTIME_CHECK(substream))
2934 runtime = substream->runtime;
2936 poll_wait(file, &runtime->sleep, wait);
2938 snd_pcm_stream_lock_irq(substream);
2939 avail = snd_pcm_playback_avail(runtime);
2940 switch (runtime->status->state) {
2941 case SNDRV_PCM_STATE_RUNNING:
2942 case SNDRV_PCM_STATE_PREPARED:
2943 case SNDRV_PCM_STATE_PAUSED:
2944 if (avail >= runtime->control->avail_min) {
2945 mask = POLLOUT | POLLWRNORM;
2949 case SNDRV_PCM_STATE_DRAINING:
2953 mask = POLLOUT | POLLWRNORM | POLLERR;
2956 snd_pcm_stream_unlock_irq(substream);
2960 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2962 struct snd_pcm_file *pcm_file;
2963 struct snd_pcm_substream *substream;
2964 struct snd_pcm_runtime *runtime;
2966 snd_pcm_uframes_t avail;
2968 pcm_file = file->private_data;
2970 substream = pcm_file->substream;
2971 if (PCM_RUNTIME_CHECK(substream))
2973 runtime = substream->runtime;
2975 poll_wait(file, &runtime->sleep, wait);
2977 snd_pcm_stream_lock_irq(substream);
2978 avail = snd_pcm_capture_avail(runtime);
2979 switch (runtime->status->state) {
2980 case SNDRV_PCM_STATE_RUNNING:
2981 case SNDRV_PCM_STATE_PREPARED:
2982 case SNDRV_PCM_STATE_PAUSED:
2983 if (avail >= runtime->control->avail_min) {
2984 mask = POLLIN | POLLRDNORM;
2989 case SNDRV_PCM_STATE_DRAINING:
2991 mask = POLLIN | POLLRDNORM;
2996 mask = POLLIN | POLLRDNORM | POLLERR;
2999 snd_pcm_stream_unlock_irq(substream);
3008 * Only on coherent architectures, we can mmap the status and the control records
3009 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3011 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3013 * mmap status record
3015 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3016 struct vm_fault *vmf)
3018 struct snd_pcm_substream *substream = area->vm_private_data;
3019 struct snd_pcm_runtime *runtime;
3021 if (substream == NULL)
3022 return VM_FAULT_SIGBUS;
3023 runtime = substream->runtime;
3024 vmf->page = virt_to_page(runtime->status);
3025 get_page(vmf->page);
3029 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3031 .fault = snd_pcm_mmap_status_fault,
3034 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3035 struct vm_area_struct *area)
3037 struct snd_pcm_runtime *runtime;
3039 if (!(area->vm_flags & VM_READ))
3041 runtime = substream->runtime;
3042 size = area->vm_end - area->vm_start;
3043 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3045 area->vm_ops = &snd_pcm_vm_ops_status;
3046 area->vm_private_data = substream;
3047 area->vm_flags |= VM_RESERVED;
3052 * mmap control record
3054 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3055 struct vm_fault *vmf)
3057 struct snd_pcm_substream *substream = area->vm_private_data;
3058 struct snd_pcm_runtime *runtime;
3060 if (substream == NULL)
3061 return VM_FAULT_SIGBUS;
3062 runtime = substream->runtime;
3063 vmf->page = virt_to_page(runtime->control);
3064 get_page(vmf->page);
3068 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3070 .fault = snd_pcm_mmap_control_fault,
3073 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3074 struct vm_area_struct *area)
3076 struct snd_pcm_runtime *runtime;
3078 if (!(area->vm_flags & VM_READ))
3080 runtime = substream->runtime;
3081 size = area->vm_end - area->vm_start;
3082 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3084 area->vm_ops = &snd_pcm_vm_ops_control;
3085 area->vm_private_data = substream;
3086 area->vm_flags |= VM_RESERVED;
3089 #else /* ! coherent mmap */
3091 * don't support mmap for status and control records.
3093 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3094 struct vm_area_struct *area)
3098 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3099 struct vm_area_struct *area)
3103 #endif /* coherent mmap */
3105 static inline struct page *
3106 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3108 void *vaddr = substream->runtime->dma_area + ofs;
3109 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3110 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3111 return virt_to_page(CAC_ADDR(vaddr));
3113 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3114 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3115 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3116 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3117 /* assume dma_handle set via pfn_to_phys() in
3118 * mm/dma-noncoherent.c
3120 return pfn_to_page(addr >> PAGE_SHIFT);
3123 return virt_to_page(vaddr);
3127 * fault callback for mmapping a RAM page
3129 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3130 struct vm_fault *vmf)
3132 struct snd_pcm_substream *substream = area->vm_private_data;
3133 struct snd_pcm_runtime *runtime;
3134 unsigned long offset;
3138 if (substream == NULL)
3139 return VM_FAULT_SIGBUS;
3140 runtime = substream->runtime;
3141 offset = vmf->pgoff << PAGE_SHIFT;
3142 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3143 if (offset > dma_bytes - PAGE_SIZE)
3144 return VM_FAULT_SIGBUS;
3145 if (substream->ops->page)
3146 page = substream->ops->page(substream, offset);
3148 page = snd_pcm_default_page_ops(substream, offset);
3150 return VM_FAULT_SIGBUS;
3156 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3157 .open = snd_pcm_mmap_data_open,
3158 .close = snd_pcm_mmap_data_close,
3161 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3162 .open = snd_pcm_mmap_data_open,
3163 .close = snd_pcm_mmap_data_close,
3164 .fault = snd_pcm_mmap_data_fault,
3167 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3168 /* This should be defined / handled globally! */
3170 #define ARCH_HAS_DMA_MMAP_COHERENT
3175 * mmap the DMA buffer on RAM
3177 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3178 struct vm_area_struct *area)
3180 area->vm_flags |= VM_RESERVED;
3181 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3182 if (!substream->ops->page &&
3183 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3184 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3186 substream->runtime->dma_area,
3187 substream->runtime->dma_addr,
3188 area->vm_end - area->vm_start);
3189 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3190 /* mmap with fault handler */
3191 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3196 * mmap the DMA buffer on I/O memory area
3198 #if SNDRV_PCM_INFO_MMAP_IOMEM
3199 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3200 struct vm_area_struct *area)
3203 unsigned long offset;
3205 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3206 area->vm_flags |= VM_IO;
3207 size = area->vm_end - area->vm_start;
3208 offset = area->vm_pgoff << PAGE_SHIFT;
3209 if (io_remap_pfn_range(area, area->vm_start,
3210 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3211 size, area->vm_page_prot))
3216 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3217 #endif /* SNDRV_PCM_INFO_MMAP */
3219 /* mmap callback with pgprot_noncached */
3220 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3221 struct vm_area_struct *area)
3223 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3224 return snd_pcm_default_mmap(substream, area);
3226 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3231 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3232 struct vm_area_struct *area)
3234 struct snd_pcm_runtime *runtime;
3236 unsigned long offset;
3240 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3241 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3244 if (!(area->vm_flags & VM_READ))
3247 runtime = substream->runtime;
3248 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3250 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3252 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3253 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3255 size = area->vm_end - area->vm_start;
3256 offset = area->vm_pgoff << PAGE_SHIFT;
3257 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3258 if ((size_t)size > dma_bytes)
3260 if (offset > dma_bytes - size)
3263 area->vm_ops = &snd_pcm_vm_ops_data;
3264 area->vm_private_data = substream;
3265 if (substream->ops->mmap)
3266 err = substream->ops->mmap(substream, area);
3268 err = snd_pcm_default_mmap(substream, area);
3270 atomic_inc(&substream->mmap_count);
3274 EXPORT_SYMBOL(snd_pcm_mmap_data);
3276 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3278 struct snd_pcm_file * pcm_file;
3279 struct snd_pcm_substream *substream;
3280 unsigned long offset;
3282 pcm_file = file->private_data;
3283 substream = pcm_file->substream;
3284 if (PCM_RUNTIME_CHECK(substream))
3287 offset = area->vm_pgoff << PAGE_SHIFT;
3289 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3290 if (pcm_file->no_compat_mmap)
3292 return snd_pcm_mmap_status(substream, file, area);
3293 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3294 if (pcm_file->no_compat_mmap)
3296 return snd_pcm_mmap_control(substream, file, area);
3298 return snd_pcm_mmap_data(substream, file, area);
3303 static int snd_pcm_fasync(int fd, struct file * file, int on)
3305 struct snd_pcm_file * pcm_file;
3306 struct snd_pcm_substream *substream;
3307 struct snd_pcm_runtime *runtime;
3311 pcm_file = file->private_data;
3312 substream = pcm_file->substream;
3313 if (PCM_RUNTIME_CHECK(substream))
3315 runtime = substream->runtime;
3316 err = fasync_helper(fd, file, on, &runtime->fasync);
3325 #ifdef CONFIG_COMPAT
3326 #include "pcm_compat.c"
3328 #define snd_pcm_ioctl_compat NULL
3332 * To be removed helpers to keep binary compatibility
3335 #ifdef CONFIG_SND_SUPPORT_OLD_API
3336 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3337 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3339 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3340 struct snd_pcm_hw_params_old *oparams)
3344 memset(params, 0, sizeof(*params));
3345 params->flags = oparams->flags;
3346 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3347 params->masks[i].bits[0] = oparams->masks[i];
3348 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3349 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3350 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3351 params->info = oparams->info;
3352 params->msbits = oparams->msbits;
3353 params->rate_num = oparams->rate_num;
3354 params->rate_den = oparams->rate_den;
3355 params->fifo_size = oparams->fifo_size;
3358 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3359 struct snd_pcm_hw_params *params)
3363 memset(oparams, 0, sizeof(*oparams));
3364 oparams->flags = params->flags;
3365 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3366 oparams->masks[i] = params->masks[i].bits[0];
3367 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3368 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3369 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3370 oparams->info = params->info;
3371 oparams->msbits = params->msbits;
3372 oparams->rate_num = params->rate_num;
3373 oparams->rate_den = params->rate_den;
3374 oparams->fifo_size = params->fifo_size;
3377 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3378 struct snd_pcm_hw_params_old __user * _oparams)
3380 struct snd_pcm_hw_params *params;
3381 struct snd_pcm_hw_params_old *oparams = NULL;
3384 params = kmalloc(sizeof(*params), GFP_KERNEL);
3388 oparams = memdup_user(_oparams, sizeof(*oparams));
3389 if (IS_ERR(oparams)) {
3390 err = PTR_ERR(oparams);
3393 snd_pcm_hw_convert_from_old_params(params, oparams);
3394 err = snd_pcm_hw_refine(substream, params);
3395 snd_pcm_hw_convert_to_old_params(oparams, params);
3396 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3407 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3408 struct snd_pcm_hw_params_old __user * _oparams)
3410 struct snd_pcm_hw_params *params;
3411 struct snd_pcm_hw_params_old *oparams = NULL;
3414 params = kmalloc(sizeof(*params), GFP_KERNEL);
3418 oparams = memdup_user(_oparams, sizeof(*oparams));
3419 if (IS_ERR(oparams)) {
3420 err = PTR_ERR(oparams);
3423 snd_pcm_hw_convert_from_old_params(params, oparams);
3424 err = snd_pcm_hw_params(substream, params);
3425 snd_pcm_hw_convert_to_old_params(oparams, params);
3426 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3436 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3439 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3440 unsigned long len, unsigned long pgoff,
3441 unsigned long flags)
3446 # define dummy_get_unmapped_area NULL
3453 const struct file_operations snd_pcm_f_ops[2] = {
3455 .owner = THIS_MODULE,
3456 .write = snd_pcm_write,
3457 .aio_write = snd_pcm_aio_write,
3458 .open = snd_pcm_playback_open,
3459 .release = snd_pcm_release,
3460 .poll = snd_pcm_playback_poll,
3461 .unlocked_ioctl = snd_pcm_playback_ioctl,
3462 .compat_ioctl = snd_pcm_ioctl_compat,
3463 .mmap = snd_pcm_mmap,
3464 .fasync = snd_pcm_fasync,
3465 .get_unmapped_area = dummy_get_unmapped_area,
3468 .owner = THIS_MODULE,
3469 .read = snd_pcm_read,
3470 .aio_read = snd_pcm_aio_read,
3471 .open = snd_pcm_capture_open,
3472 .release = snd_pcm_release,
3473 .poll = snd_pcm_capture_poll,
3474 .unlocked_ioctl = snd_pcm_capture_ioctl,
3475 .compat_ioctl = snd_pcm_ioctl_compat,
3476 .mmap = snd_pcm_mmap,
3477 .fasync = snd_pcm_fasync,
3478 .get_unmapped_area = dummy_get_unmapped_area,