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 <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
42 struct snd_pcm_hw_params_old {
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
46 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rate_num;
53 unsigned int rate_den;
54 snd_pcm_uframes_t fifo_size;
55 unsigned char reserved[64];
58 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
67 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock);
74 EXPORT_SYMBOL(snd_pcm_link_rwlock);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem);
78 static inline mm_segment_t snd_enter_user(void)
80 mm_segment_t fs = get_fs();
85 static inline void snd_leave_user(mm_segment_t fs)
92 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
94 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
98 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
122 struct snd_pcm_info *info;
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
128 err = snd_pcm_info(substream, info);
130 if (copy_to_user(_info, info, sizeof(*info)))
140 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141 char *snd_pcm_hw_param_names[] = {
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
160 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
186 if (!(params->rmask & (1 << k)))
189 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
197 params->cmask |= 1 << k;
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
206 if (!(params->rmask & (1 << k)))
209 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
228 params->cmask |= 1 << k;
233 for (k = 0; k < constrs->rules_num; k++)
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
239 for (k = 0; k < constrs->rules_num; k++) {
240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
243 if (r->cond && !(r->cond & params->flags))
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
254 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
261 i = hw_param_interval(params, r->var);
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
271 changed = r->func(params, r);
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
313 hw = &substream->runtime->hw;
315 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
316 if (!params->fifo_size) {
317 if (snd_mask_min(¶ms->masks[SNDRV_PCM_HW_PARAM_FORMAT]) ==
318 snd_mask_max(¶ms->masks[SNDRV_PCM_HW_PARAM_FORMAT]) &&
319 snd_mask_min(¶ms->masks[SNDRV_PCM_HW_PARAM_CHANNELS]) ==
320 snd_mask_max(¶ms->masks[SNDRV_PCM_HW_PARAM_CHANNELS])) {
321 changed = substream->ops->ioctl(substream,
322 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
331 EXPORT_SYMBOL(snd_pcm_hw_refine);
333 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
334 struct snd_pcm_hw_params __user * _params)
336 struct snd_pcm_hw_params *params;
339 params = memdup_user(_params, sizeof(*params));
341 return PTR_ERR(params);
343 err = snd_pcm_hw_refine(substream, params);
344 if (copy_to_user(_params, params, sizeof(*params))) {
353 static int period_to_usecs(struct snd_pcm_runtime *runtime)
358 return -1; /* invalid */
360 /* take 75% of period time as the deadline */
361 usecs = (750000 / runtime->rate) * runtime->period_size;
362 usecs += ((750000 % runtime->rate) * runtime->period_size) /
368 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
369 struct snd_pcm_hw_params *params)
371 struct snd_pcm_runtime *runtime;
374 snd_pcm_uframes_t frames;
376 if (PCM_RUNTIME_CHECK(substream))
378 runtime = substream->runtime;
379 snd_pcm_stream_lock_irq(substream);
380 switch (runtime->status->state) {
381 case SNDRV_PCM_STATE_OPEN:
382 case SNDRV_PCM_STATE_SETUP:
383 case SNDRV_PCM_STATE_PREPARED:
386 snd_pcm_stream_unlock_irq(substream);
389 snd_pcm_stream_unlock_irq(substream);
390 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
391 if (!substream->oss.oss)
393 if (atomic_read(&substream->mmap_count))
397 err = snd_pcm_hw_refine(substream, params);
401 err = snd_pcm_hw_params_choose(substream, params);
405 if (substream->ops->hw_params != NULL) {
406 err = substream->ops->hw_params(substream, params);
411 runtime->access = params_access(params);
412 runtime->format = params_format(params);
413 runtime->subformat = params_subformat(params);
414 runtime->channels = params_channels(params);
415 runtime->rate = params_rate(params);
416 runtime->period_size = params_period_size(params);
417 runtime->periods = params_periods(params);
418 runtime->buffer_size = params_buffer_size(params);
419 runtime->info = params->info;
420 runtime->rate_num = params->rate_num;
421 runtime->rate_den = params->rate_den;
423 bits = snd_pcm_format_physical_width(runtime->format);
424 runtime->sample_bits = bits;
425 bits *= runtime->channels;
426 runtime->frame_bits = bits;
428 while (bits % 8 != 0) {
432 runtime->byte_align = bits / 8;
433 runtime->min_align = frames;
435 /* Default sw params */
436 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
437 runtime->period_step = 1;
438 runtime->control->avail_min = runtime->period_size;
439 runtime->start_threshold = 1;
440 runtime->stop_threshold = runtime->buffer_size;
441 runtime->silence_threshold = 0;
442 runtime->silence_size = 0;
443 runtime->boundary = runtime->buffer_size;
444 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
445 runtime->boundary *= 2;
447 snd_pcm_timer_resolution_change(substream);
448 runtime->status->state = SNDRV_PCM_STATE_SETUP;
450 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
451 substream->latency_id);
452 if ((usecs = period_to_usecs(runtime)) >= 0)
453 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
454 substream->latency_id, usecs);
457 /* hardware might be unuseable from this time,
458 so we force application to retry to set
459 the correct hardware parameter settings */
460 runtime->status->state = SNDRV_PCM_STATE_OPEN;
461 if (substream->ops->hw_free != NULL)
462 substream->ops->hw_free(substream);
466 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params __user * _params)
469 struct snd_pcm_hw_params *params;
472 params = memdup_user(_params, sizeof(*params));
474 return PTR_ERR(params);
476 err = snd_pcm_hw_params(substream, params);
477 if (copy_to_user(_params, params, sizeof(*params))) {
486 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
488 struct snd_pcm_runtime *runtime;
491 if (PCM_RUNTIME_CHECK(substream))
493 runtime = substream->runtime;
494 snd_pcm_stream_lock_irq(substream);
495 switch (runtime->status->state) {
496 case SNDRV_PCM_STATE_SETUP:
497 case SNDRV_PCM_STATE_PREPARED:
500 snd_pcm_stream_unlock_irq(substream);
503 snd_pcm_stream_unlock_irq(substream);
504 if (atomic_read(&substream->mmap_count))
506 if (substream->ops->hw_free)
507 result = substream->ops->hw_free(substream);
508 runtime->status->state = SNDRV_PCM_STATE_OPEN;
509 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
510 substream->latency_id);
514 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
515 struct snd_pcm_sw_params *params)
517 struct snd_pcm_runtime *runtime;
519 if (PCM_RUNTIME_CHECK(substream))
521 runtime = substream->runtime;
522 snd_pcm_stream_lock_irq(substream);
523 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
524 snd_pcm_stream_unlock_irq(substream);
527 snd_pcm_stream_unlock_irq(substream);
529 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
531 if (params->avail_min == 0)
533 if (params->silence_size >= runtime->boundary) {
534 if (params->silence_threshold != 0)
537 if (params->silence_size > params->silence_threshold)
539 if (params->silence_threshold > runtime->buffer_size)
542 snd_pcm_stream_lock_irq(substream);
543 runtime->tstamp_mode = params->tstamp_mode;
544 runtime->period_step = params->period_step;
545 runtime->control->avail_min = params->avail_min;
546 runtime->start_threshold = params->start_threshold;
547 runtime->stop_threshold = params->stop_threshold;
548 runtime->silence_threshold = params->silence_threshold;
549 runtime->silence_size = params->silence_size;
550 params->boundary = runtime->boundary;
551 if (snd_pcm_running(substream)) {
552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
553 runtime->silence_size > 0)
554 snd_pcm_playback_silence(substream, ULONG_MAX);
555 wake_up(&runtime->sleep);
557 snd_pcm_stream_unlock_irq(substream);
561 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
562 struct snd_pcm_sw_params __user * _params)
564 struct snd_pcm_sw_params params;
566 if (copy_from_user(¶ms, _params, sizeof(params)))
568 err = snd_pcm_sw_params(substream, ¶ms);
569 if (copy_to_user(_params, ¶ms, sizeof(params)))
574 int snd_pcm_status(struct snd_pcm_substream *substream,
575 struct snd_pcm_status *status)
577 struct snd_pcm_runtime *runtime = substream->runtime;
579 snd_pcm_stream_lock_irq(substream);
580 status->state = runtime->status->state;
581 status->suspended_state = runtime->status->suspended_state;
582 if (status->state == SNDRV_PCM_STATE_OPEN)
584 status->trigger_tstamp = runtime->trigger_tstamp;
585 if (snd_pcm_running(substream)) {
586 snd_pcm_update_hw_ptr(substream);
587 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
588 status->tstamp = runtime->status->tstamp;
592 snd_pcm_gettime(runtime, &status->tstamp);
594 status->appl_ptr = runtime->control->appl_ptr;
595 status->hw_ptr = runtime->status->hw_ptr;
596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597 status->avail = snd_pcm_playback_avail(runtime);
598 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
599 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
600 status->delay = runtime->buffer_size - status->avail;
601 status->delay += runtime->delay;
605 status->avail = snd_pcm_capture_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
607 status->delay = status->avail + runtime->delay;
611 status->avail_max = runtime->avail_max;
612 status->overrange = runtime->overrange;
613 runtime->avail_max = 0;
614 runtime->overrange = 0;
616 snd_pcm_stream_unlock_irq(substream);
620 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
621 struct snd_pcm_status __user * _status)
623 struct snd_pcm_status status;
626 memset(&status, 0, sizeof(status));
627 res = snd_pcm_status(substream, &status);
630 if (copy_to_user(_status, &status, sizeof(status)))
635 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
636 struct snd_pcm_channel_info * info)
638 struct snd_pcm_runtime *runtime;
639 unsigned int channel;
641 channel = info->channel;
642 runtime = substream->runtime;
643 snd_pcm_stream_lock_irq(substream);
644 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
645 snd_pcm_stream_unlock_irq(substream);
648 snd_pcm_stream_unlock_irq(substream);
649 if (channel >= runtime->channels)
651 memset(info, 0, sizeof(*info));
652 info->channel = channel;
653 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
656 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
657 struct snd_pcm_channel_info __user * _info)
659 struct snd_pcm_channel_info info;
662 if (copy_from_user(&info, _info, sizeof(info)))
664 res = snd_pcm_channel_info(substream, &info);
667 if (copy_to_user(_info, &info, sizeof(info)))
672 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
674 struct snd_pcm_runtime *runtime = substream->runtime;
675 if (runtime->trigger_master == NULL)
677 if (runtime->trigger_master == substream) {
678 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
680 snd_pcm_trigger_tstamp(runtime->trigger_master);
681 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
683 runtime->trigger_master = NULL;
687 int (*pre_action)(struct snd_pcm_substream *substream, int state);
688 int (*do_action)(struct snd_pcm_substream *substream, int state);
689 void (*undo_action)(struct snd_pcm_substream *substream, int state);
690 void (*post_action)(struct snd_pcm_substream *substream, int state);
694 * this functions is core for handling of linked stream
695 * Note: the stream state might be changed also on failure
696 * Note2: call with calling stream lock + link lock
698 static int snd_pcm_action_group(struct action_ops *ops,
699 struct snd_pcm_substream *substream,
700 int state, int do_lock)
702 struct snd_pcm_substream *s = NULL;
703 struct snd_pcm_substream *s1;
706 snd_pcm_group_for_each_entry(s, substream) {
707 if (do_lock && s != substream)
708 spin_lock_nested(&s->self_group.lock,
709 SINGLE_DEPTH_NESTING);
710 res = ops->pre_action(s, state);
714 snd_pcm_group_for_each_entry(s, substream) {
715 res = ops->do_action(s, state);
717 if (ops->undo_action) {
718 snd_pcm_group_for_each_entry(s1, substream) {
719 if (s1 == s) /* failed stream */
721 ops->undo_action(s1, state);
724 s = NULL; /* unlock all */
728 snd_pcm_group_for_each_entry(s, substream) {
729 ops->post_action(s, state);
734 snd_pcm_group_for_each_entry(s1, substream) {
736 spin_unlock(&s1->self_group.lock);
737 if (s1 == s) /* end */
745 * Note: call with stream lock
747 static int snd_pcm_action_single(struct action_ops *ops,
748 struct snd_pcm_substream *substream,
753 res = ops->pre_action(substream, state);
756 res = ops->do_action(substream, state);
758 ops->post_action(substream, state);
759 else if (ops->undo_action)
760 ops->undo_action(substream, state);
765 * Note: call with stream lock
767 static int snd_pcm_action(struct action_ops *ops,
768 struct snd_pcm_substream *substream,
773 if (snd_pcm_stream_linked(substream)) {
774 if (!spin_trylock(&substream->group->lock)) {
775 spin_unlock(&substream->self_group.lock);
776 spin_lock(&substream->group->lock);
777 spin_lock(&substream->self_group.lock);
779 res = snd_pcm_action_group(ops, substream, state, 1);
780 spin_unlock(&substream->group->lock);
782 res = snd_pcm_action_single(ops, substream, state);
788 * Note: don't use any locks before
790 static int snd_pcm_action_lock_irq(struct action_ops *ops,
791 struct snd_pcm_substream *substream,
796 read_lock_irq(&snd_pcm_link_rwlock);
797 if (snd_pcm_stream_linked(substream)) {
798 spin_lock(&substream->group->lock);
799 spin_lock(&substream->self_group.lock);
800 res = snd_pcm_action_group(ops, substream, state, 1);
801 spin_unlock(&substream->self_group.lock);
802 spin_unlock(&substream->group->lock);
804 spin_lock(&substream->self_group.lock);
805 res = snd_pcm_action_single(ops, substream, state);
806 spin_unlock(&substream->self_group.lock);
808 read_unlock_irq(&snd_pcm_link_rwlock);
814 static int snd_pcm_action_nonatomic(struct action_ops *ops,
815 struct snd_pcm_substream *substream,
820 down_read(&snd_pcm_link_rwsem);
821 if (snd_pcm_stream_linked(substream))
822 res = snd_pcm_action_group(ops, substream, state, 0);
824 res = snd_pcm_action_single(ops, substream, state);
825 up_read(&snd_pcm_link_rwsem);
832 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
834 struct snd_pcm_runtime *runtime = substream->runtime;
835 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
837 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
838 !snd_pcm_playback_data(substream))
840 runtime->trigger_master = substream;
844 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
846 if (substream->runtime->trigger_master != substream)
848 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
851 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
853 if (substream->runtime->trigger_master == substream)
854 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
857 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
859 struct snd_pcm_runtime *runtime = substream->runtime;
860 snd_pcm_trigger_tstamp(substream);
861 runtime->hw_ptr_jiffies = jiffies;
862 runtime->status->state = state;
863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
864 runtime->silence_size > 0)
865 snd_pcm_playback_silence(substream, ULONG_MAX);
866 if (substream->timer)
867 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
868 &runtime->trigger_tstamp);
871 static struct action_ops snd_pcm_action_start = {
872 .pre_action = snd_pcm_pre_start,
873 .do_action = snd_pcm_do_start,
874 .undo_action = snd_pcm_undo_start,
875 .post_action = snd_pcm_post_start
879 * snd_pcm_start - start all linked streams
880 * @substream: the PCM substream instance
882 int snd_pcm_start(struct snd_pcm_substream *substream)
884 return snd_pcm_action(&snd_pcm_action_start, substream,
885 SNDRV_PCM_STATE_RUNNING);
891 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
893 struct snd_pcm_runtime *runtime = substream->runtime;
894 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
896 runtime->trigger_master = substream;
900 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
902 if (substream->runtime->trigger_master == substream &&
903 snd_pcm_running(substream))
904 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
905 return 0; /* unconditonally stop all substreams */
908 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 if (runtime->status->state != state) {
912 snd_pcm_trigger_tstamp(substream);
913 if (substream->timer)
914 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
915 &runtime->trigger_tstamp);
916 runtime->status->state = state;
918 wake_up(&runtime->sleep);
921 static struct action_ops snd_pcm_action_stop = {
922 .pre_action = snd_pcm_pre_stop,
923 .do_action = snd_pcm_do_stop,
924 .post_action = snd_pcm_post_stop
928 * snd_pcm_stop - try to stop all running streams in the substream group
929 * @substream: the PCM substream instance
930 * @state: PCM state after stopping the stream
932 * The state of each stream is then changed to the given state unconditionally.
934 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
936 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
939 EXPORT_SYMBOL(snd_pcm_stop);
942 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
943 * @substream: the PCM substream
945 * After stopping, the state is changed to SETUP.
946 * Unlike snd_pcm_stop(), this affects only the given stream.
948 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
950 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
951 SNDRV_PCM_STATE_SETUP);
957 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
959 struct snd_pcm_runtime *runtime = substream->runtime;
960 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
963 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
965 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
967 runtime->trigger_master = substream;
971 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
973 if (substream->runtime->trigger_master != substream)
975 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
976 * a delta betwen the current jiffies, this gives a large enough
977 * delta, effectively to skip the check once.
979 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
980 return substream->ops->trigger(substream,
981 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
982 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
985 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
987 if (substream->runtime->trigger_master == substream)
988 substream->ops->trigger(substream,
989 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
990 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
993 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
995 struct snd_pcm_runtime *runtime = substream->runtime;
996 snd_pcm_trigger_tstamp(substream);
998 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
999 if (substream->timer)
1000 snd_timer_notify(substream->timer,
1001 SNDRV_TIMER_EVENT_MPAUSE,
1002 &runtime->trigger_tstamp);
1003 wake_up(&runtime->sleep);
1005 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1006 if (substream->timer)
1007 snd_timer_notify(substream->timer,
1008 SNDRV_TIMER_EVENT_MCONTINUE,
1009 &runtime->trigger_tstamp);
1013 static struct action_ops snd_pcm_action_pause = {
1014 .pre_action = snd_pcm_pre_pause,
1015 .do_action = snd_pcm_do_pause,
1016 .undo_action = snd_pcm_undo_pause,
1017 .post_action = snd_pcm_post_pause
1021 * Push/release the pause for all linked streams.
1023 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1025 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1031 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1033 struct snd_pcm_runtime *runtime = substream->runtime;
1034 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1036 runtime->trigger_master = substream;
1040 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1042 struct snd_pcm_runtime *runtime = substream->runtime;
1043 if (runtime->trigger_master != substream)
1045 if (! snd_pcm_running(substream))
1047 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1048 return 0; /* suspend unconditionally */
1051 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1053 struct snd_pcm_runtime *runtime = substream->runtime;
1054 snd_pcm_trigger_tstamp(substream);
1055 if (substream->timer)
1056 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1057 &runtime->trigger_tstamp);
1058 runtime->status->suspended_state = runtime->status->state;
1059 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1060 wake_up(&runtime->sleep);
1063 static struct action_ops snd_pcm_action_suspend = {
1064 .pre_action = snd_pcm_pre_suspend,
1065 .do_action = snd_pcm_do_suspend,
1066 .post_action = snd_pcm_post_suspend
1070 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1071 * @substream: the PCM substream
1073 * After this call, all streams are changed to SUSPENDED state.
1075 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1078 unsigned long flags;
1083 snd_pcm_stream_lock_irqsave(substream, flags);
1084 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1085 snd_pcm_stream_unlock_irqrestore(substream, flags);
1089 EXPORT_SYMBOL(snd_pcm_suspend);
1092 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1093 * @pcm: the PCM instance
1095 * After this call, all streams are changed to SUSPENDED state.
1097 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1099 struct snd_pcm_substream *substream;
1100 int stream, err = 0;
1105 for (stream = 0; stream < 2; stream++) {
1106 for (substream = pcm->streams[stream].substream;
1107 substream; substream = substream->next) {
1108 /* FIXME: the open/close code should lock this as well */
1109 if (substream->runtime == NULL)
1111 err = snd_pcm_suspend(substream);
1112 if (err < 0 && err != -EBUSY)
1119 EXPORT_SYMBOL(snd_pcm_suspend_all);
1123 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1125 struct snd_pcm_runtime *runtime = substream->runtime;
1126 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1128 runtime->trigger_master = substream;
1132 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1134 struct snd_pcm_runtime *runtime = substream->runtime;
1135 if (runtime->trigger_master != substream)
1137 /* DMA not running previously? */
1138 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1139 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1140 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1142 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1145 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1147 if (substream->runtime->trigger_master == substream &&
1148 snd_pcm_running(substream))
1149 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1152 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1154 struct snd_pcm_runtime *runtime = substream->runtime;
1155 snd_pcm_trigger_tstamp(substream);
1156 if (substream->timer)
1157 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1158 &runtime->trigger_tstamp);
1159 runtime->status->state = runtime->status->suspended_state;
1162 static struct action_ops snd_pcm_action_resume = {
1163 .pre_action = snd_pcm_pre_resume,
1164 .do_action = snd_pcm_do_resume,
1165 .undo_action = snd_pcm_undo_resume,
1166 .post_action = snd_pcm_post_resume
1169 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1171 struct snd_card *card = substream->pcm->card;
1174 snd_power_lock(card);
1175 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1176 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1177 snd_power_unlock(card);
1183 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1188 #endif /* CONFIG_PM */
1193 * Change the RUNNING stream(s) to XRUN state.
1195 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1197 struct snd_card *card = substream->pcm->card;
1198 struct snd_pcm_runtime *runtime = substream->runtime;
1201 snd_power_lock(card);
1202 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1203 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1208 snd_pcm_stream_lock_irq(substream);
1209 switch (runtime->status->state) {
1210 case SNDRV_PCM_STATE_XRUN:
1211 result = 0; /* already there */
1213 case SNDRV_PCM_STATE_RUNNING:
1214 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1219 snd_pcm_stream_unlock_irq(substream);
1221 snd_power_unlock(card);
1228 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1230 struct snd_pcm_runtime *runtime = substream->runtime;
1231 switch (runtime->status->state) {
1232 case SNDRV_PCM_STATE_RUNNING:
1233 case SNDRV_PCM_STATE_PREPARED:
1234 case SNDRV_PCM_STATE_PAUSED:
1235 case SNDRV_PCM_STATE_SUSPENDED:
1242 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1244 struct snd_pcm_runtime *runtime = substream->runtime;
1245 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1248 runtime->hw_ptr_base = 0;
1249 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1250 runtime->status->hw_ptr % runtime->period_size;
1251 runtime->silence_start = runtime->status->hw_ptr;
1252 runtime->silence_filled = 0;
1256 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1258 struct snd_pcm_runtime *runtime = substream->runtime;
1259 runtime->control->appl_ptr = runtime->status->hw_ptr;
1260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1261 runtime->silence_size > 0)
1262 snd_pcm_playback_silence(substream, ULONG_MAX);
1265 static struct action_ops snd_pcm_action_reset = {
1266 .pre_action = snd_pcm_pre_reset,
1267 .do_action = snd_pcm_do_reset,
1268 .post_action = snd_pcm_post_reset
1271 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1273 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1279 /* we use the second argument for updating f_flags */
1280 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1283 struct snd_pcm_runtime *runtime = substream->runtime;
1284 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1285 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1287 if (snd_pcm_running(substream))
1289 substream->f_flags = f_flags;
1293 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1296 err = substream->ops->prepare(substream);
1299 return snd_pcm_do_reset(substream, 0);
1302 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1304 struct snd_pcm_runtime *runtime = substream->runtime;
1305 runtime->control->appl_ptr = runtime->status->hw_ptr;
1306 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1309 static struct action_ops snd_pcm_action_prepare = {
1310 .pre_action = snd_pcm_pre_prepare,
1311 .do_action = snd_pcm_do_prepare,
1312 .post_action = snd_pcm_post_prepare
1316 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1317 * @substream: the PCM substream instance
1318 * @file: file to refer f_flags
1320 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1324 struct snd_card *card = substream->pcm->card;
1328 f_flags = file->f_flags;
1330 f_flags = substream->f_flags;
1332 snd_power_lock(card);
1333 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1334 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1335 substream, f_flags);
1336 snd_power_unlock(card);
1344 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1346 if (substream->f_flags & O_NONBLOCK)
1348 substream->runtime->trigger_master = substream;
1352 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1354 struct snd_pcm_runtime *runtime = substream->runtime;
1355 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1356 switch (runtime->status->state) {
1357 case SNDRV_PCM_STATE_PREPARED:
1358 /* start playback stream if possible */
1359 if (! snd_pcm_playback_empty(substream)) {
1360 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1361 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1364 case SNDRV_PCM_STATE_RUNNING:
1365 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1371 /* stop running stream */
1372 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1373 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1374 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1375 snd_pcm_do_stop(substream, new_state);
1376 snd_pcm_post_stop(substream, new_state);
1382 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1386 static struct action_ops snd_pcm_action_drain_init = {
1387 .pre_action = snd_pcm_pre_drain_init,
1388 .do_action = snd_pcm_do_drain_init,
1389 .post_action = snd_pcm_post_drain_init
1393 struct snd_pcm_substream *substream;
1395 snd_pcm_uframes_t stop_threshold;
1398 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1401 * Drain the stream(s).
1402 * When the substream is linked, sync until the draining of all playback streams
1404 * After this call, all streams are supposed to be either SETUP or DRAINING
1405 * (capture only) state.
1407 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1409 struct snd_card *card;
1410 struct snd_pcm_runtime *runtime;
1411 struct snd_pcm_substream *s;
1414 struct drain_rec *drec, drec_tmp, *d;
1416 card = substream->pcm->card;
1417 runtime = substream->runtime;
1419 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1422 snd_power_lock(card);
1423 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1424 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1426 snd_power_unlock(card);
1431 /* allocate temporary record for drain sync */
1432 down_read(&snd_pcm_link_rwsem);
1433 if (snd_pcm_stream_linked(substream)) {
1434 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1436 up_read(&snd_pcm_link_rwsem);
1437 snd_power_unlock(card);
1443 /* count only playback streams */
1445 snd_pcm_group_for_each_entry(s, substream) {
1446 runtime = s->runtime;
1447 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1448 d = &drec[num_drecs++];
1450 init_waitqueue_entry(&d->wait, current);
1451 add_wait_queue(&runtime->sleep, &d->wait);
1452 /* stop_threshold fixup to avoid endless loop when
1453 * stop_threshold > buffer_size
1455 d->stop_threshold = runtime->stop_threshold;
1456 if (runtime->stop_threshold > runtime->buffer_size)
1457 runtime->stop_threshold = runtime->buffer_size;
1460 up_read(&snd_pcm_link_rwsem);
1462 snd_pcm_stream_lock_irq(substream);
1464 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1465 snd_pcm_pause(substream, 0);
1467 /* pre-start/stop - all running streams are changed to DRAINING state */
1468 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1470 snd_pcm_stream_unlock_irq(substream);
1476 if (signal_pending(current)) {
1477 result = -ERESTARTSYS;
1481 for (i = 0; i < num_drecs; i++) {
1482 runtime = drec[i].substream->runtime;
1483 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1487 break; /* yes, all drained */
1489 set_current_state(TASK_INTERRUPTIBLE);
1490 snd_pcm_stream_unlock_irq(substream);
1491 snd_power_unlock(card);
1492 tout = schedule_timeout(10 * HZ);
1493 snd_power_lock(card);
1494 snd_pcm_stream_lock_irq(substream);
1496 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1499 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1500 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1507 snd_pcm_stream_unlock_irq(substream);
1510 for (i = 0; i < num_drecs; i++) {
1512 runtime = d->substream->runtime;
1513 remove_wait_queue(&runtime->sleep, &d->wait);
1514 runtime->stop_threshold = d->stop_threshold;
1517 if (drec != &drec_tmp)
1519 snd_power_unlock(card);
1527 * Immediately put all linked substreams into SETUP state.
1529 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1531 struct snd_pcm_runtime *runtime;
1532 struct snd_card *card;
1535 if (PCM_RUNTIME_CHECK(substream))
1537 runtime = substream->runtime;
1538 card = substream->pcm->card;
1540 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1541 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1542 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1545 snd_pcm_stream_lock_irq(substream);
1547 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1548 snd_pcm_pause(substream, 0);
1550 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1551 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1552 snd_pcm_stream_unlock_irq(substream);
1558 /* WARNING: Don't forget to fput back the file */
1559 static struct file *snd_pcm_file_fd(int fd)
1562 struct inode *inode;
1568 inode = file->f_path.dentry->d_inode;
1569 if (!S_ISCHR(inode->i_mode) ||
1570 imajor(inode) != snd_major) {
1574 minor = iminor(inode);
1575 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1576 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1586 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1590 struct snd_pcm_file *pcm_file;
1591 struct snd_pcm_substream *substream1;
1593 file = snd_pcm_file_fd(fd);
1596 pcm_file = file->private_data;
1597 substream1 = pcm_file->substream;
1598 down_write(&snd_pcm_link_rwsem);
1599 write_lock_irq(&snd_pcm_link_rwlock);
1600 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1601 substream->runtime->status->state != substream1->runtime->status->state) {
1605 if (snd_pcm_stream_linked(substream1)) {
1609 if (!snd_pcm_stream_linked(substream)) {
1610 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1611 if (substream->group == NULL) {
1615 spin_lock_init(&substream->group->lock);
1616 INIT_LIST_HEAD(&substream->group->substreams);
1617 list_add_tail(&substream->link_list, &substream->group->substreams);
1618 substream->group->count = 1;
1620 list_add_tail(&substream1->link_list, &substream->group->substreams);
1621 substream->group->count++;
1622 substream1->group = substream->group;
1624 write_unlock_irq(&snd_pcm_link_rwlock);
1625 up_write(&snd_pcm_link_rwsem);
1630 static void relink_to_local(struct snd_pcm_substream *substream)
1632 substream->group = &substream->self_group;
1633 INIT_LIST_HEAD(&substream->self_group.substreams);
1634 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1637 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1639 struct snd_pcm_substream *s;
1642 down_write(&snd_pcm_link_rwsem);
1643 write_lock_irq(&snd_pcm_link_rwlock);
1644 if (!snd_pcm_stream_linked(substream)) {
1648 list_del(&substream->link_list);
1649 substream->group->count--;
1650 if (substream->group->count == 1) { /* detach the last stream, too */
1651 snd_pcm_group_for_each_entry(s, substream) {
1655 kfree(substream->group);
1657 relink_to_local(substream);
1659 write_unlock_irq(&snd_pcm_link_rwlock);
1660 up_write(&snd_pcm_link_rwsem);
1667 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1668 struct snd_pcm_hw_rule *rule)
1670 struct snd_interval t;
1671 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1672 hw_param_interval_c(params, rule->deps[1]), &t);
1673 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1676 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1677 struct snd_pcm_hw_rule *rule)
1679 struct snd_interval t;
1680 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1681 hw_param_interval_c(params, rule->deps[1]), &t);
1682 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1685 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1686 struct snd_pcm_hw_rule *rule)
1688 struct snd_interval t;
1689 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1690 hw_param_interval_c(params, rule->deps[1]),
1691 (unsigned long) rule->private, &t);
1692 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1695 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1696 struct snd_pcm_hw_rule *rule)
1698 struct snd_interval t;
1699 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1700 (unsigned long) rule->private,
1701 hw_param_interval_c(params, rule->deps[1]), &t);
1702 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1705 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1706 struct snd_pcm_hw_rule *rule)
1709 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1711 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1713 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1715 if (! snd_mask_test(mask, k))
1717 bits = snd_pcm_format_physical_width(k);
1719 continue; /* ignore invalid formats */
1720 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1721 snd_mask_reset(&m, k);
1723 return snd_mask_refine(mask, &m);
1726 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1727 struct snd_pcm_hw_rule *rule)
1729 struct snd_interval t;
1735 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1737 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1739 bits = snd_pcm_format_physical_width(k);
1741 continue; /* ignore invalid formats */
1742 if (t.min > (unsigned)bits)
1744 if (t.max < (unsigned)bits)
1748 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1751 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1752 #error "Change this table"
1755 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1756 48000, 64000, 88200, 96000, 176400, 192000 };
1758 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1759 .count = ARRAY_SIZE(rates),
1763 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1764 struct snd_pcm_hw_rule *rule)
1766 struct snd_pcm_hardware *hw = rule->private;
1767 return snd_interval_list(hw_param_interval(params, rule->var),
1768 snd_pcm_known_rates.count,
1769 snd_pcm_known_rates.list, hw->rates);
1772 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1773 struct snd_pcm_hw_rule *rule)
1775 struct snd_interval t;
1776 struct snd_pcm_substream *substream = rule->private;
1778 t.max = substream->buffer_bytes_max;
1782 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1785 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1787 struct snd_pcm_runtime *runtime = substream->runtime;
1788 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1791 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1792 snd_mask_any(constrs_mask(constrs, k));
1795 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1796 snd_interval_any(constrs_interval(constrs, k));
1799 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1800 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1801 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1802 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1803 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1805 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1806 snd_pcm_hw_rule_format, NULL,
1807 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1810 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1811 snd_pcm_hw_rule_sample_bits, NULL,
1812 SNDRV_PCM_HW_PARAM_FORMAT,
1813 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1816 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1817 snd_pcm_hw_rule_div, NULL,
1818 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1822 snd_pcm_hw_rule_mul, NULL,
1823 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1827 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1828 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1833 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1837 snd_pcm_hw_rule_div, NULL,
1838 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1842 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1843 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1847 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1848 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1852 snd_pcm_hw_rule_div, NULL,
1853 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1857 snd_pcm_hw_rule_div, NULL,
1858 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1867 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1868 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1872 snd_pcm_hw_rule_mul, NULL,
1873 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1882 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1887 snd_pcm_hw_rule_muldivk, (void*) 8,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1892 snd_pcm_hw_rule_muldivk, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1897 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1898 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1902 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1903 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1909 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1911 struct snd_pcm_runtime *runtime = substream->runtime;
1912 struct snd_pcm_hardware *hw = &runtime->hw;
1914 unsigned int mask = 0;
1916 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1917 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1918 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1919 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1920 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1921 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1922 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1923 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1924 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1925 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1926 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1928 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1932 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1936 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1941 hw->channels_min, hw->channels_max);
1945 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1946 hw->rate_min, hw->rate_max);
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1951 hw->period_bytes_min, hw->period_bytes_max);
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1956 hw->periods_min, hw->periods_max);
1960 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1961 hw->period_bytes_min, hw->buffer_bytes_max);
1965 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1966 snd_pcm_hw_rule_buffer_bytes_max, substream,
1967 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1972 if (runtime->dma_bytes) {
1973 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1978 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1979 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1980 snd_pcm_hw_rule_rate, hw,
1981 SNDRV_PCM_HW_PARAM_RATE, -1);
1986 /* FIXME: this belong to lowlevel */
1987 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1992 static void pcm_release_private(struct snd_pcm_substream *substream)
1994 snd_pcm_unlink(substream);
1997 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1999 substream->ref_count--;
2000 if (substream->ref_count > 0)
2003 snd_pcm_drop(substream);
2004 if (substream->hw_opened) {
2005 if (substream->ops->hw_free != NULL)
2006 substream->ops->hw_free(substream);
2007 substream->ops->close(substream);
2008 substream->hw_opened = 0;
2010 if (substream->pcm_release) {
2011 substream->pcm_release(substream);
2012 substream->pcm_release = NULL;
2014 snd_pcm_detach_substream(substream);
2017 EXPORT_SYMBOL(snd_pcm_release_substream);
2019 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2021 struct snd_pcm_substream **rsubstream)
2023 struct snd_pcm_substream *substream;
2026 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2029 if (substream->ref_count > 1) {
2030 *rsubstream = substream;
2034 err = snd_pcm_hw_constraints_init(substream);
2036 snd_printd("snd_pcm_hw_constraints_init failed\n");
2040 if ((err = substream->ops->open(substream)) < 0)
2043 substream->hw_opened = 1;
2045 err = snd_pcm_hw_constraints_complete(substream);
2047 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2051 *rsubstream = substream;
2055 snd_pcm_release_substream(substream);
2059 EXPORT_SYMBOL(snd_pcm_open_substream);
2061 static int snd_pcm_open_file(struct file *file,
2062 struct snd_pcm *pcm,
2064 struct snd_pcm_file **rpcm_file)
2066 struct snd_pcm_file *pcm_file;
2067 struct snd_pcm_substream *substream;
2068 struct snd_pcm_str *str;
2074 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2078 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2079 if (pcm_file == NULL) {
2080 snd_pcm_release_substream(substream);
2083 pcm_file->substream = substream;
2084 if (substream->ref_count == 1) {
2085 str = substream->pstr;
2086 substream->file = pcm_file;
2087 substream->pcm_release = pcm_release_private;
2089 file->private_data = pcm_file;
2091 *rpcm_file = pcm_file;
2095 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2097 struct snd_pcm *pcm;
2099 pcm = snd_lookup_minor_data(iminor(inode),
2100 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2101 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2104 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2106 struct snd_pcm *pcm;
2108 pcm = snd_lookup_minor_data(iminor(inode),
2109 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2110 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2113 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2116 struct snd_pcm_file *pcm_file;
2123 err = snd_card_file_add(pcm->card, file);
2126 if (!try_module_get(pcm->card->module)) {
2130 init_waitqueue_entry(&wait, current);
2131 add_wait_queue(&pcm->open_wait, &wait);
2132 mutex_lock(&pcm->open_mutex);
2134 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2137 if (err == -EAGAIN) {
2138 if (file->f_flags & O_NONBLOCK) {
2144 set_current_state(TASK_INTERRUPTIBLE);
2145 mutex_unlock(&pcm->open_mutex);
2147 mutex_lock(&pcm->open_mutex);
2148 if (signal_pending(current)) {
2153 remove_wait_queue(&pcm->open_wait, &wait);
2154 mutex_unlock(&pcm->open_mutex);
2160 module_put(pcm->card->module);
2162 snd_card_file_remove(pcm->card, file);
2167 static int snd_pcm_release(struct inode *inode, struct file *file)
2169 struct snd_pcm *pcm;
2170 struct snd_pcm_substream *substream;
2171 struct snd_pcm_file *pcm_file;
2173 pcm_file = file->private_data;
2174 substream = pcm_file->substream;
2175 if (snd_BUG_ON(!substream))
2177 pcm = substream->pcm;
2178 mutex_lock(&pcm->open_mutex);
2179 snd_pcm_release_substream(substream);
2181 mutex_unlock(&pcm->open_mutex);
2182 wake_up(&pcm->open_wait);
2183 module_put(pcm->card->module);
2184 snd_card_file_remove(pcm->card, file);
2188 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2189 snd_pcm_uframes_t frames)
2191 struct snd_pcm_runtime *runtime = substream->runtime;
2192 snd_pcm_sframes_t appl_ptr;
2193 snd_pcm_sframes_t ret;
2194 snd_pcm_sframes_t hw_avail;
2199 snd_pcm_stream_lock_irq(substream);
2200 switch (runtime->status->state) {
2201 case SNDRV_PCM_STATE_PREPARED:
2203 case SNDRV_PCM_STATE_DRAINING:
2204 case SNDRV_PCM_STATE_RUNNING:
2205 if (snd_pcm_update_hw_ptr(substream) >= 0)
2208 case SNDRV_PCM_STATE_XRUN:
2216 hw_avail = snd_pcm_playback_hw_avail(runtime);
2217 if (hw_avail <= 0) {
2221 if (frames > (snd_pcm_uframes_t)hw_avail)
2223 appl_ptr = runtime->control->appl_ptr - frames;
2225 appl_ptr += runtime->boundary;
2226 runtime->control->appl_ptr = appl_ptr;
2229 snd_pcm_stream_unlock_irq(substream);
2233 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2234 snd_pcm_uframes_t frames)
2236 struct snd_pcm_runtime *runtime = substream->runtime;
2237 snd_pcm_sframes_t appl_ptr;
2238 snd_pcm_sframes_t ret;
2239 snd_pcm_sframes_t hw_avail;
2244 snd_pcm_stream_lock_irq(substream);
2245 switch (runtime->status->state) {
2246 case SNDRV_PCM_STATE_PREPARED:
2247 case SNDRV_PCM_STATE_DRAINING:
2249 case SNDRV_PCM_STATE_RUNNING:
2250 if (snd_pcm_update_hw_ptr(substream) >= 0)
2253 case SNDRV_PCM_STATE_XRUN:
2261 hw_avail = snd_pcm_capture_hw_avail(runtime);
2262 if (hw_avail <= 0) {
2266 if (frames > (snd_pcm_uframes_t)hw_avail)
2268 appl_ptr = runtime->control->appl_ptr - frames;
2270 appl_ptr += runtime->boundary;
2271 runtime->control->appl_ptr = appl_ptr;
2274 snd_pcm_stream_unlock_irq(substream);
2278 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2279 snd_pcm_uframes_t frames)
2281 struct snd_pcm_runtime *runtime = substream->runtime;
2282 snd_pcm_sframes_t appl_ptr;
2283 snd_pcm_sframes_t ret;
2284 snd_pcm_sframes_t avail;
2289 snd_pcm_stream_lock_irq(substream);
2290 switch (runtime->status->state) {
2291 case SNDRV_PCM_STATE_PREPARED:
2292 case SNDRV_PCM_STATE_PAUSED:
2294 case SNDRV_PCM_STATE_DRAINING:
2295 case SNDRV_PCM_STATE_RUNNING:
2296 if (snd_pcm_update_hw_ptr(substream) >= 0)
2299 case SNDRV_PCM_STATE_XRUN:
2307 avail = snd_pcm_playback_avail(runtime);
2312 if (frames > (snd_pcm_uframes_t)avail)
2314 appl_ptr = runtime->control->appl_ptr + frames;
2315 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2316 appl_ptr -= runtime->boundary;
2317 runtime->control->appl_ptr = appl_ptr;
2320 snd_pcm_stream_unlock_irq(substream);
2324 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2325 snd_pcm_uframes_t frames)
2327 struct snd_pcm_runtime *runtime = substream->runtime;
2328 snd_pcm_sframes_t appl_ptr;
2329 snd_pcm_sframes_t ret;
2330 snd_pcm_sframes_t avail;
2335 snd_pcm_stream_lock_irq(substream);
2336 switch (runtime->status->state) {
2337 case SNDRV_PCM_STATE_PREPARED:
2338 case SNDRV_PCM_STATE_DRAINING:
2339 case SNDRV_PCM_STATE_PAUSED:
2341 case SNDRV_PCM_STATE_RUNNING:
2342 if (snd_pcm_update_hw_ptr(substream) >= 0)
2345 case SNDRV_PCM_STATE_XRUN:
2353 avail = snd_pcm_capture_avail(runtime);
2358 if (frames > (snd_pcm_uframes_t)avail)
2360 appl_ptr = runtime->control->appl_ptr + frames;
2361 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2362 appl_ptr -= runtime->boundary;
2363 runtime->control->appl_ptr = appl_ptr;
2366 snd_pcm_stream_unlock_irq(substream);
2370 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2372 struct snd_pcm_runtime *runtime = substream->runtime;
2375 snd_pcm_stream_lock_irq(substream);
2376 switch (runtime->status->state) {
2377 case SNDRV_PCM_STATE_DRAINING:
2378 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2380 case SNDRV_PCM_STATE_RUNNING:
2381 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2384 case SNDRV_PCM_STATE_PREPARED:
2385 case SNDRV_PCM_STATE_SUSPENDED:
2388 case SNDRV_PCM_STATE_XRUN:
2396 snd_pcm_stream_unlock_irq(substream);
2400 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2401 snd_pcm_sframes_t __user *res)
2403 struct snd_pcm_runtime *runtime = substream->runtime;
2405 snd_pcm_sframes_t n = 0;
2407 snd_pcm_stream_lock_irq(substream);
2408 switch (runtime->status->state) {
2409 case SNDRV_PCM_STATE_DRAINING:
2410 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2412 case SNDRV_PCM_STATE_RUNNING:
2413 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2416 case SNDRV_PCM_STATE_PREPARED:
2417 case SNDRV_PCM_STATE_SUSPENDED:
2419 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2420 n = snd_pcm_playback_hw_avail(runtime);
2422 n = snd_pcm_capture_avail(runtime);
2423 n += runtime->delay;
2425 case SNDRV_PCM_STATE_XRUN:
2433 snd_pcm_stream_unlock_irq(substream);
2435 if (put_user(n, res))
2440 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2441 struct snd_pcm_sync_ptr __user *_sync_ptr)
2443 struct snd_pcm_runtime *runtime = substream->runtime;
2444 struct snd_pcm_sync_ptr sync_ptr;
2445 volatile struct snd_pcm_mmap_status *status;
2446 volatile struct snd_pcm_mmap_control *control;
2449 memset(&sync_ptr, 0, sizeof(sync_ptr));
2450 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2452 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2454 status = runtime->status;
2455 control = runtime->control;
2456 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2457 err = snd_pcm_hwsync(substream);
2461 snd_pcm_stream_lock_irq(substream);
2462 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2463 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2465 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2466 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2467 control->avail_min = sync_ptr.c.control.avail_min;
2469 sync_ptr.c.control.avail_min = control->avail_min;
2470 sync_ptr.s.status.state = status->state;
2471 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2472 sync_ptr.s.status.tstamp = status->tstamp;
2473 sync_ptr.s.status.suspended_state = status->suspended_state;
2474 snd_pcm_stream_unlock_irq(substream);
2475 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2480 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2482 struct snd_pcm_runtime *runtime = substream->runtime;
2485 if (get_user(arg, _arg))
2487 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2489 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2490 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2491 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2495 static int snd_pcm_common_ioctl1(struct file *file,
2496 struct snd_pcm_substream *substream,
2497 unsigned int cmd, void __user *arg)
2500 case SNDRV_PCM_IOCTL_PVERSION:
2501 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2502 case SNDRV_PCM_IOCTL_INFO:
2503 return snd_pcm_info_user(substream, arg);
2504 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2506 case SNDRV_PCM_IOCTL_TTSTAMP:
2507 return snd_pcm_tstamp(substream, arg);
2508 case SNDRV_PCM_IOCTL_HW_REFINE:
2509 return snd_pcm_hw_refine_user(substream, arg);
2510 case SNDRV_PCM_IOCTL_HW_PARAMS:
2511 return snd_pcm_hw_params_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_HW_FREE:
2513 return snd_pcm_hw_free(substream);
2514 case SNDRV_PCM_IOCTL_SW_PARAMS:
2515 return snd_pcm_sw_params_user(substream, arg);
2516 case SNDRV_PCM_IOCTL_STATUS:
2517 return snd_pcm_status_user(substream, arg);
2518 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2519 return snd_pcm_channel_info_user(substream, arg);
2520 case SNDRV_PCM_IOCTL_PREPARE:
2521 return snd_pcm_prepare(substream, file);
2522 case SNDRV_PCM_IOCTL_RESET:
2523 return snd_pcm_reset(substream);
2524 case SNDRV_PCM_IOCTL_START:
2525 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2526 case SNDRV_PCM_IOCTL_LINK:
2527 return snd_pcm_link(substream, (int)(unsigned long) arg);
2528 case SNDRV_PCM_IOCTL_UNLINK:
2529 return snd_pcm_unlink(substream);
2530 case SNDRV_PCM_IOCTL_RESUME:
2531 return snd_pcm_resume(substream);
2532 case SNDRV_PCM_IOCTL_XRUN:
2533 return snd_pcm_xrun(substream);
2534 case SNDRV_PCM_IOCTL_HWSYNC:
2535 return snd_pcm_hwsync(substream);
2536 case SNDRV_PCM_IOCTL_DELAY:
2537 return snd_pcm_delay(substream, arg);
2538 case SNDRV_PCM_IOCTL_SYNC_PTR:
2539 return snd_pcm_sync_ptr(substream, arg);
2540 #ifdef CONFIG_SND_SUPPORT_OLD_API
2541 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2542 return snd_pcm_hw_refine_old_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2544 return snd_pcm_hw_params_old_user(substream, arg);
2546 case SNDRV_PCM_IOCTL_DRAIN:
2547 return snd_pcm_drain(substream);
2548 case SNDRV_PCM_IOCTL_DROP:
2549 return snd_pcm_drop(substream);
2550 case SNDRV_PCM_IOCTL_PAUSE:
2553 snd_pcm_stream_lock_irq(substream);
2554 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2555 snd_pcm_stream_unlock_irq(substream);
2559 snd_printd("unknown ioctl = 0x%x\n", cmd);
2563 static int snd_pcm_playback_ioctl1(struct file *file,
2564 struct snd_pcm_substream *substream,
2565 unsigned int cmd, void __user *arg)
2567 if (snd_BUG_ON(!substream))
2569 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2572 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2574 struct snd_xferi xferi;
2575 struct snd_xferi __user *_xferi = arg;
2576 struct snd_pcm_runtime *runtime = substream->runtime;
2577 snd_pcm_sframes_t result;
2578 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2580 if (put_user(0, &_xferi->result))
2582 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2584 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2585 __put_user(result, &_xferi->result);
2586 return result < 0 ? result : 0;
2588 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2590 struct snd_xfern xfern;
2591 struct snd_xfern __user *_xfern = arg;
2592 struct snd_pcm_runtime *runtime = substream->runtime;
2594 snd_pcm_sframes_t result;
2595 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2597 if (runtime->channels > 128)
2599 if (put_user(0, &_xfern->result))
2601 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2604 bufs = memdup_user(xfern.bufs,
2605 sizeof(void *) * runtime->channels);
2607 return PTR_ERR(bufs);
2608 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2610 __put_user(result, &_xfern->result);
2611 return result < 0 ? result : 0;
2613 case SNDRV_PCM_IOCTL_REWIND:
2615 snd_pcm_uframes_t frames;
2616 snd_pcm_uframes_t __user *_frames = arg;
2617 snd_pcm_sframes_t result;
2618 if (get_user(frames, _frames))
2620 if (put_user(0, _frames))
2622 result = snd_pcm_playback_rewind(substream, frames);
2623 __put_user(result, _frames);
2624 return result < 0 ? result : 0;
2626 case SNDRV_PCM_IOCTL_FORWARD:
2628 snd_pcm_uframes_t frames;
2629 snd_pcm_uframes_t __user *_frames = arg;
2630 snd_pcm_sframes_t result;
2631 if (get_user(frames, _frames))
2633 if (put_user(0, _frames))
2635 result = snd_pcm_playback_forward(substream, frames);
2636 __put_user(result, _frames);
2637 return result < 0 ? result : 0;
2640 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2643 static int snd_pcm_capture_ioctl1(struct file *file,
2644 struct snd_pcm_substream *substream,
2645 unsigned int cmd, void __user *arg)
2647 if (snd_BUG_ON(!substream))
2649 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2652 case SNDRV_PCM_IOCTL_READI_FRAMES:
2654 struct snd_xferi xferi;
2655 struct snd_xferi __user *_xferi = arg;
2656 struct snd_pcm_runtime *runtime = substream->runtime;
2657 snd_pcm_sframes_t result;
2658 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2660 if (put_user(0, &_xferi->result))
2662 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2664 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2665 __put_user(result, &_xferi->result);
2666 return result < 0 ? result : 0;
2668 case SNDRV_PCM_IOCTL_READN_FRAMES:
2670 struct snd_xfern xfern;
2671 struct snd_xfern __user *_xfern = arg;
2672 struct snd_pcm_runtime *runtime = substream->runtime;
2674 snd_pcm_sframes_t result;
2675 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2677 if (runtime->channels > 128)
2679 if (put_user(0, &_xfern->result))
2681 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2684 bufs = memdup_user(xfern.bufs,
2685 sizeof(void *) * runtime->channels);
2687 return PTR_ERR(bufs);
2688 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2690 __put_user(result, &_xfern->result);
2691 return result < 0 ? result : 0;
2693 case SNDRV_PCM_IOCTL_REWIND:
2695 snd_pcm_uframes_t frames;
2696 snd_pcm_uframes_t __user *_frames = arg;
2697 snd_pcm_sframes_t result;
2698 if (get_user(frames, _frames))
2700 if (put_user(0, _frames))
2702 result = snd_pcm_capture_rewind(substream, frames);
2703 __put_user(result, _frames);
2704 return result < 0 ? result : 0;
2706 case SNDRV_PCM_IOCTL_FORWARD:
2708 snd_pcm_uframes_t frames;
2709 snd_pcm_uframes_t __user *_frames = arg;
2710 snd_pcm_sframes_t result;
2711 if (get_user(frames, _frames))
2713 if (put_user(0, _frames))
2715 result = snd_pcm_capture_forward(substream, frames);
2716 __put_user(result, _frames);
2717 return result < 0 ? result : 0;
2720 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2723 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2726 struct snd_pcm_file *pcm_file;
2728 pcm_file = file->private_data;
2730 if (((cmd >> 8) & 0xff) != 'A')
2733 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2734 (void __user *)arg);
2737 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2740 struct snd_pcm_file *pcm_file;
2742 pcm_file = file->private_data;
2744 if (((cmd >> 8) & 0xff) != 'A')
2747 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2748 (void __user *)arg);
2751 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2752 unsigned int cmd, void *arg)
2757 fs = snd_enter_user();
2758 switch (substream->stream) {
2759 case SNDRV_PCM_STREAM_PLAYBACK:
2760 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2761 (void __user *)arg);
2763 case SNDRV_PCM_STREAM_CAPTURE:
2764 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2765 (void __user *)arg);
2775 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2777 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2780 struct snd_pcm_file *pcm_file;
2781 struct snd_pcm_substream *substream;
2782 struct snd_pcm_runtime *runtime;
2783 snd_pcm_sframes_t result;
2785 pcm_file = file->private_data;
2786 substream = pcm_file->substream;
2787 if (PCM_RUNTIME_CHECK(substream))
2789 runtime = substream->runtime;
2790 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2792 if (!frame_aligned(runtime, count))
2794 count = bytes_to_frames(runtime, count);
2795 result = snd_pcm_lib_read(substream, buf, count);
2797 result = frames_to_bytes(runtime, result);
2801 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2802 size_t count, loff_t * offset)
2804 struct snd_pcm_file *pcm_file;
2805 struct snd_pcm_substream *substream;
2806 struct snd_pcm_runtime *runtime;
2807 snd_pcm_sframes_t result;
2809 pcm_file = file->private_data;
2810 substream = pcm_file->substream;
2811 if (PCM_RUNTIME_CHECK(substream))
2813 runtime = substream->runtime;
2814 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2816 if (!frame_aligned(runtime, count))
2818 count = bytes_to_frames(runtime, count);
2819 result = snd_pcm_lib_write(substream, buf, count);
2821 result = frames_to_bytes(runtime, result);
2825 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2826 unsigned long nr_segs, loff_t pos)
2829 struct snd_pcm_file *pcm_file;
2830 struct snd_pcm_substream *substream;
2831 struct snd_pcm_runtime *runtime;
2832 snd_pcm_sframes_t result;
2835 snd_pcm_uframes_t frames;
2837 pcm_file = iocb->ki_filp->private_data;
2838 substream = pcm_file->substream;
2839 if (PCM_RUNTIME_CHECK(substream))
2841 runtime = substream->runtime;
2842 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2844 if (nr_segs > 1024 || nr_segs != runtime->channels)
2846 if (!frame_aligned(runtime, iov->iov_len))
2848 frames = bytes_to_samples(runtime, iov->iov_len);
2849 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2852 for (i = 0; i < nr_segs; ++i)
2853 bufs[i] = iov[i].iov_base;
2854 result = snd_pcm_lib_readv(substream, bufs, frames);
2856 result = frames_to_bytes(runtime, result);
2861 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2862 unsigned long nr_segs, loff_t pos)
2864 struct snd_pcm_file *pcm_file;
2865 struct snd_pcm_substream *substream;
2866 struct snd_pcm_runtime *runtime;
2867 snd_pcm_sframes_t result;
2870 snd_pcm_uframes_t frames;
2872 pcm_file = iocb->ki_filp->private_data;
2873 substream = pcm_file->substream;
2874 if (PCM_RUNTIME_CHECK(substream))
2876 runtime = substream->runtime;
2877 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2879 if (nr_segs > 128 || nr_segs != runtime->channels ||
2880 !frame_aligned(runtime, iov->iov_len))
2882 frames = bytes_to_samples(runtime, iov->iov_len);
2883 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2886 for (i = 0; i < nr_segs; ++i)
2887 bufs[i] = iov[i].iov_base;
2888 result = snd_pcm_lib_writev(substream, bufs, frames);
2890 result = frames_to_bytes(runtime, result);
2895 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2897 struct snd_pcm_file *pcm_file;
2898 struct snd_pcm_substream *substream;
2899 struct snd_pcm_runtime *runtime;
2901 snd_pcm_uframes_t avail;
2903 pcm_file = file->private_data;
2905 substream = pcm_file->substream;
2906 if (PCM_RUNTIME_CHECK(substream))
2908 runtime = substream->runtime;
2910 poll_wait(file, &runtime->sleep, wait);
2912 snd_pcm_stream_lock_irq(substream);
2913 avail = snd_pcm_playback_avail(runtime);
2914 switch (runtime->status->state) {
2915 case SNDRV_PCM_STATE_RUNNING:
2916 case SNDRV_PCM_STATE_PREPARED:
2917 case SNDRV_PCM_STATE_PAUSED:
2918 if (avail >= runtime->control->avail_min) {
2919 mask = POLLOUT | POLLWRNORM;
2923 case SNDRV_PCM_STATE_DRAINING:
2927 mask = POLLOUT | POLLWRNORM | POLLERR;
2930 snd_pcm_stream_unlock_irq(substream);
2934 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2936 struct snd_pcm_file *pcm_file;
2937 struct snd_pcm_substream *substream;
2938 struct snd_pcm_runtime *runtime;
2940 snd_pcm_uframes_t avail;
2942 pcm_file = file->private_data;
2944 substream = pcm_file->substream;
2945 if (PCM_RUNTIME_CHECK(substream))
2947 runtime = substream->runtime;
2949 poll_wait(file, &runtime->sleep, wait);
2951 snd_pcm_stream_lock_irq(substream);
2952 avail = snd_pcm_capture_avail(runtime);
2953 switch (runtime->status->state) {
2954 case SNDRV_PCM_STATE_RUNNING:
2955 case SNDRV_PCM_STATE_PREPARED:
2956 case SNDRV_PCM_STATE_PAUSED:
2957 if (avail >= runtime->control->avail_min) {
2958 mask = POLLIN | POLLRDNORM;
2963 case SNDRV_PCM_STATE_DRAINING:
2965 mask = POLLIN | POLLRDNORM;
2970 mask = POLLIN | POLLRDNORM | POLLERR;
2973 snd_pcm_stream_unlock_irq(substream);
2982 * Only on coherent architectures, we can mmap the status and the control records
2983 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2985 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2987 * mmap status record
2989 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2990 struct vm_fault *vmf)
2992 struct snd_pcm_substream *substream = area->vm_private_data;
2993 struct snd_pcm_runtime *runtime;
2995 if (substream == NULL)
2996 return VM_FAULT_SIGBUS;
2997 runtime = substream->runtime;
2998 vmf->page = virt_to_page(runtime->status);
2999 get_page(vmf->page);
3003 static struct vm_operations_struct snd_pcm_vm_ops_status =
3005 .fault = snd_pcm_mmap_status_fault,
3008 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3009 struct vm_area_struct *area)
3011 struct snd_pcm_runtime *runtime;
3013 if (!(area->vm_flags & VM_READ))
3015 runtime = substream->runtime;
3016 size = area->vm_end - area->vm_start;
3017 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3019 area->vm_ops = &snd_pcm_vm_ops_status;
3020 area->vm_private_data = substream;
3021 area->vm_flags |= VM_RESERVED;
3026 * mmap control record
3028 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3029 struct vm_fault *vmf)
3031 struct snd_pcm_substream *substream = area->vm_private_data;
3032 struct snd_pcm_runtime *runtime;
3034 if (substream == NULL)
3035 return VM_FAULT_SIGBUS;
3036 runtime = substream->runtime;
3037 vmf->page = virt_to_page(runtime->control);
3038 get_page(vmf->page);
3042 static struct vm_operations_struct snd_pcm_vm_ops_control =
3044 .fault = snd_pcm_mmap_control_fault,
3047 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3048 struct vm_area_struct *area)
3050 struct snd_pcm_runtime *runtime;
3052 if (!(area->vm_flags & VM_READ))
3054 runtime = substream->runtime;
3055 size = area->vm_end - area->vm_start;
3056 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3058 area->vm_ops = &snd_pcm_vm_ops_control;
3059 area->vm_private_data = substream;
3060 area->vm_flags |= VM_RESERVED;
3063 #else /* ! coherent mmap */
3065 * don't support mmap for status and control records.
3067 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3068 struct vm_area_struct *area)
3072 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3073 struct vm_area_struct *area)
3077 #endif /* coherent mmap */
3080 * fault callback for mmapping a RAM page
3082 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3083 struct vm_fault *vmf)
3085 struct snd_pcm_substream *substream = area->vm_private_data;
3086 struct snd_pcm_runtime *runtime;
3087 unsigned long offset;
3092 if (substream == NULL)
3093 return VM_FAULT_SIGBUS;
3094 runtime = substream->runtime;
3095 offset = vmf->pgoff << PAGE_SHIFT;
3096 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3097 if (offset > dma_bytes - PAGE_SIZE)
3098 return VM_FAULT_SIGBUS;
3099 if (substream->ops->page) {
3100 page = substream->ops->page(substream, offset);
3102 return VM_FAULT_SIGBUS;
3104 vaddr = runtime->dma_area + offset;
3105 page = virt_to_page(vaddr);
3112 static struct vm_operations_struct snd_pcm_vm_ops_data =
3114 .open = snd_pcm_mmap_data_open,
3115 .close = snd_pcm_mmap_data_close,
3116 .fault = snd_pcm_mmap_data_fault,
3120 * mmap the DMA buffer on RAM
3122 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3123 struct vm_area_struct *area)
3125 area->vm_ops = &snd_pcm_vm_ops_data;
3126 area->vm_private_data = substream;
3127 area->vm_flags |= VM_RESERVED;
3128 atomic_inc(&substream->mmap_count);
3133 * mmap the DMA buffer on I/O memory area
3135 #if SNDRV_PCM_INFO_MMAP_IOMEM
3136 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3138 .open = snd_pcm_mmap_data_open,
3139 .close = snd_pcm_mmap_data_close,
3142 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3143 struct vm_area_struct *area)
3146 unsigned long offset;
3148 #ifdef pgprot_noncached
3149 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3151 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3152 area->vm_private_data = substream;
3153 area->vm_flags |= VM_IO;
3154 size = area->vm_end - area->vm_start;
3155 offset = area->vm_pgoff << PAGE_SHIFT;
3156 if (io_remap_pfn_range(area, area->vm_start,
3157 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3158 size, area->vm_page_prot))
3160 atomic_inc(&substream->mmap_count);
3164 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3165 #endif /* SNDRV_PCM_INFO_MMAP */
3170 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3171 struct vm_area_struct *area)
3173 struct snd_pcm_runtime *runtime;
3175 unsigned long offset;
3178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3179 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3182 if (!(area->vm_flags & VM_READ))
3185 runtime = substream->runtime;
3186 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3188 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3190 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3191 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3193 size = area->vm_end - area->vm_start;
3194 offset = area->vm_pgoff << PAGE_SHIFT;
3195 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3196 if ((size_t)size > dma_bytes)
3198 if (offset > dma_bytes - size)
3201 if (substream->ops->mmap)
3202 return substream->ops->mmap(substream, area);
3204 return snd_pcm_default_mmap(substream, area);
3207 EXPORT_SYMBOL(snd_pcm_mmap_data);
3209 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3211 struct snd_pcm_file * pcm_file;
3212 struct snd_pcm_substream *substream;
3213 unsigned long offset;
3215 pcm_file = file->private_data;
3216 substream = pcm_file->substream;
3217 if (PCM_RUNTIME_CHECK(substream))
3220 offset = area->vm_pgoff << PAGE_SHIFT;
3222 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3223 if (pcm_file->no_compat_mmap)
3225 return snd_pcm_mmap_status(substream, file, area);
3226 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3227 if (pcm_file->no_compat_mmap)
3229 return snd_pcm_mmap_control(substream, file, area);
3231 return snd_pcm_mmap_data(substream, file, area);
3236 static int snd_pcm_fasync(int fd, struct file * file, int on)
3238 struct snd_pcm_file * pcm_file;
3239 struct snd_pcm_substream *substream;
3240 struct snd_pcm_runtime *runtime;
3244 pcm_file = file->private_data;
3245 substream = pcm_file->substream;
3246 if (PCM_RUNTIME_CHECK(substream))
3248 runtime = substream->runtime;
3249 err = fasync_helper(fd, file, on, &runtime->fasync);
3258 #ifdef CONFIG_COMPAT
3259 #include "pcm_compat.c"
3261 #define snd_pcm_ioctl_compat NULL
3265 * To be removed helpers to keep binary compatibility
3268 #ifdef CONFIG_SND_SUPPORT_OLD_API
3269 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3270 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3272 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3273 struct snd_pcm_hw_params_old *oparams)
3277 memset(params, 0, sizeof(*params));
3278 params->flags = oparams->flags;
3279 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3280 params->masks[i].bits[0] = oparams->masks[i];
3281 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3282 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3283 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3284 params->info = oparams->info;
3285 params->msbits = oparams->msbits;
3286 params->rate_num = oparams->rate_num;
3287 params->rate_den = oparams->rate_den;
3288 params->fifo_size = oparams->fifo_size;
3291 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3292 struct snd_pcm_hw_params *params)
3296 memset(oparams, 0, sizeof(*oparams));
3297 oparams->flags = params->flags;
3298 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3299 oparams->masks[i] = params->masks[i].bits[0];
3300 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3301 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3302 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3303 oparams->info = params->info;
3304 oparams->msbits = params->msbits;
3305 oparams->rate_num = params->rate_num;
3306 oparams->rate_den = params->rate_den;
3307 oparams->fifo_size = params->fifo_size;
3310 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3311 struct snd_pcm_hw_params_old __user * _oparams)
3313 struct snd_pcm_hw_params *params;
3314 struct snd_pcm_hw_params_old *oparams = NULL;
3317 params = kmalloc(sizeof(*params), GFP_KERNEL);
3321 oparams = memdup_user(_oparams, sizeof(*oparams));
3322 if (IS_ERR(oparams)) {
3323 err = PTR_ERR(oparams);
3326 snd_pcm_hw_convert_from_old_params(params, oparams);
3327 err = snd_pcm_hw_refine(substream, params);
3328 snd_pcm_hw_convert_to_old_params(oparams, params);
3329 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3340 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3341 struct snd_pcm_hw_params_old __user * _oparams)
3343 struct snd_pcm_hw_params *params;
3344 struct snd_pcm_hw_params_old *oparams = NULL;
3347 params = kmalloc(sizeof(*params), GFP_KERNEL);
3351 oparams = memdup_user(_oparams, sizeof(*oparams));
3352 if (IS_ERR(oparams)) {
3353 err = PTR_ERR(oparams);
3356 snd_pcm_hw_convert_from_old_params(params, oparams);
3357 err = snd_pcm_hw_params(substream, params);
3358 snd_pcm_hw_convert_to_old_params(oparams, params);
3359 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3369 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3372 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3373 unsigned long len, unsigned long pgoff,
3374 unsigned long flags)
3379 # define dummy_get_unmapped_area NULL
3386 const struct file_operations snd_pcm_f_ops[2] = {
3388 .owner = THIS_MODULE,
3389 .write = snd_pcm_write,
3390 .aio_write = snd_pcm_aio_write,
3391 .open = snd_pcm_playback_open,
3392 .release = snd_pcm_release,
3393 .poll = snd_pcm_playback_poll,
3394 .unlocked_ioctl = snd_pcm_playback_ioctl,
3395 .compat_ioctl = snd_pcm_ioctl_compat,
3396 .mmap = snd_pcm_mmap,
3397 .fasync = snd_pcm_fasync,
3398 .get_unmapped_area = dummy_get_unmapped_area,
3401 .owner = THIS_MODULE,
3402 .read = snd_pcm_read,
3403 .aio_read = snd_pcm_aio_read,
3404 .open = snd_pcm_capture_open,
3405 .release = snd_pcm_release,
3406 .poll = snd_pcm_capture_poll,
3407 .unlocked_ioctl = snd_pcm_capture_ioctl,
3408 .compat_ioctl = snd_pcm_ioctl_compat,
3409 .mmap = snd_pcm_mmap,
3410 .fasync = snd_pcm_fasync,
3411 .get_unmapped_area = dummy_get_unmapped_area,