2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.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
22 #include <sound/driver.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/latency.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 snd_assert(substream != NULL, return -ENXIO);
99 memset(info, 0, sizeof(*info));
100 info->card = pcm->card->number;
101 info->device = pcm->device;
102 info->stream = substream->stream;
103 info->subdevice = substream->number;
104 strlcpy(info->id, pcm->id, sizeof(info->id));
105 strlcpy(info->name, pcm->name, sizeof(info->name));
106 info->dev_class = pcm->dev_class;
107 info->dev_subclass = pcm->dev_subclass;
108 info->subdevices_count = pstr->substream_count;
109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
110 strlcpy(info->subname, substream->name, sizeof(info->subname));
111 runtime = substream->runtime;
112 /* AB: FIXME!!! This is definitely nonsense */
114 info->sync = runtime->sync;
115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
120 int snd_pcm_info_user(struct snd_pcm_substream *substream,
121 struct snd_pcm_info __user * _info)
123 struct snd_pcm_info *info;
126 info = kmalloc(sizeof(*info), GFP_KERNEL);
129 err = snd_pcm_info(substream, info);
131 if (copy_to_user(_info, info, sizeof(*info)))
141 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142 char *snd_pcm_hw_param_names[] = {
146 HW_PARAM(SAMPLE_BITS),
147 HW_PARAM(FRAME_BITS),
150 HW_PARAM(PERIOD_TIME),
151 HW_PARAM(PERIOD_SIZE),
152 HW_PARAM(PERIOD_BYTES),
154 HW_PARAM(BUFFER_TIME),
155 HW_PARAM(BUFFER_SIZE),
156 HW_PARAM(BUFFER_BYTES),
161 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
165 struct snd_pcm_hardware *hw;
166 struct snd_interval *i = NULL;
167 struct snd_mask *m = NULL;
168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
169 unsigned int rstamps[constrs->rules_num];
170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
171 unsigned int stamp = 2;
175 params->fifo_size = 0;
176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
179 params->rate_num = 0;
180 params->rate_den = 0;
183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
184 m = hw_param_mask(params, k);
185 if (snd_mask_empty(m))
187 if (!(params->rmask & (1 << k)))
190 printk("%s = ", snd_pcm_hw_param_names[k]);
191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
193 changed = snd_mask_refine(m, constrs_mask(constrs, k));
195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
198 params->cmask |= 1 << k;
203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
204 i = hw_param_interval(params, k);
205 if (snd_interval_empty(i))
207 if (!(params->rmask & (1 << k)))
210 printk("%s = ", snd_pcm_hw_param_names[k]);
215 i->openmin ? '(' : '[', i->min,
216 i->max, i->openmax ? ')' : ']');
219 changed = snd_interval_refine(i, constrs_interval(constrs, k));
224 printk("%c%u %u%c\n",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
229 params->cmask |= 1 << k;
234 for (k = 0; k < constrs->rules_num; k++)
236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
240 for (k = 0; k < constrs->rules_num; k++) {
241 struct snd_pcm_hw_rule *r = &constrs->rules[k];
244 if (r->cond && !(r->cond & params->flags))
246 for (d = 0; r->deps[d] >= 0; d++) {
247 if (vstamps[r->deps[d]] > rstamps[k]) {
255 printk("Rule %d [%p]: ", k, r->func);
257 printk("%s = ", snd_pcm_hw_param_names[r->var]);
258 if (hw_is_mask(r->var)) {
259 m = hw_param_mask(params, r->var);
260 printk("%x", *m->bits);
262 i = hw_param_interval(params, r->var);
267 i->openmin ? '(' : '[', i->min,
268 i->max, i->openmax ? ')' : ']');
272 changed = r->func(params, r);
276 if (hw_is_mask(r->var))
277 printk("%x", *m->bits);
283 i->openmin ? '(' : '[', i->min,
284 i->max, i->openmax ? ')' : ']');
290 if (changed && r->var >= 0) {
291 params->cmask |= (1 << r->var);
292 vstamps[r->var] = stamp;
300 if (!params->msbits) {
301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
302 if (snd_interval_single(i))
303 params->msbits = snd_interval_value(i);
306 if (!params->rate_den) {
307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
308 if (snd_interval_single(i)) {
309 params->rate_num = snd_interval_value(i);
310 params->rate_den = 1;
314 hw = &substream->runtime->hw;
316 params->info = hw->info;
317 if (!params->fifo_size)
318 params->fifo_size = hw->fifo_size;
323 EXPORT_SYMBOL(snd_pcm_hw_refine);
325 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
326 struct snd_pcm_hw_params __user * _params)
328 struct snd_pcm_hw_params *params;
331 params = kmalloc(sizeof(*params), GFP_KERNEL);
336 if (copy_from_user(params, _params, sizeof(*params))) {
340 err = snd_pcm_hw_refine(substream, params);
341 if (copy_to_user(_params, params, sizeof(*params))) {
350 static int period_to_usecs(struct snd_pcm_runtime *runtime)
355 return -1; /* invalid */
357 /* take 75% of period time as the deadline */
358 usecs = (750000 / runtime->rate) * runtime->period_size;
359 usecs += ((750000 % runtime->rate) * runtime->period_size) /
365 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
366 struct snd_pcm_hw_params *params)
368 struct snd_pcm_runtime *runtime;
371 snd_pcm_uframes_t frames;
373 snd_assert(substream != NULL, return -ENXIO);
374 runtime = substream->runtime;
375 snd_assert(runtime != NULL, return -ENXIO);
376 snd_pcm_stream_lock_irq(substream);
377 switch (runtime->status->state) {
378 case SNDRV_PCM_STATE_OPEN:
379 case SNDRV_PCM_STATE_SETUP:
380 case SNDRV_PCM_STATE_PREPARED:
383 snd_pcm_stream_unlock_irq(substream);
386 snd_pcm_stream_unlock_irq(substream);
387 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
388 if (!substream->oss.oss)
390 if (atomic_read(&substream->mmap_count))
394 err = snd_pcm_hw_refine(substream, params);
398 err = snd_pcm_hw_params_choose(substream, params);
402 if (substream->ops->hw_params != NULL) {
403 err = substream->ops->hw_params(substream, params);
408 runtime->access = params_access(params);
409 runtime->format = params_format(params);
410 runtime->subformat = params_subformat(params);
411 runtime->channels = params_channels(params);
412 runtime->rate = params_rate(params);
413 runtime->period_size = params_period_size(params);
414 runtime->periods = params_periods(params);
415 runtime->buffer_size = params_buffer_size(params);
416 runtime->tick_time = params_tick_time(params);
417 runtime->info = params->info;
418 runtime->rate_num = params->rate_num;
419 runtime->rate_den = params->rate_den;
421 bits = snd_pcm_format_physical_width(runtime->format);
422 runtime->sample_bits = bits;
423 bits *= runtime->channels;
424 runtime->frame_bits = bits;
426 while (bits % 8 != 0) {
430 runtime->byte_align = bits / 8;
431 runtime->min_align = frames;
433 /* Default sw params */
434 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
435 runtime->period_step = 1;
436 runtime->sleep_min = 0;
437 runtime->control->avail_min = runtime->period_size;
438 runtime->xfer_align = 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 remove_acceptable_latency(substream->latency_id);
451 if ((usecs = period_to_usecs(runtime)) >= 0)
452 set_acceptable_latency(substream->latency_id, usecs);
455 /* hardware might be unuseable from this time,
456 so we force application to retry to set
457 the correct hardware parameter settings */
458 runtime->status->state = SNDRV_PCM_STATE_OPEN;
459 if (substream->ops->hw_free != NULL)
460 substream->ops->hw_free(substream);
464 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
465 struct snd_pcm_hw_params __user * _params)
467 struct snd_pcm_hw_params *params;
470 params = kmalloc(sizeof(*params), GFP_KERNEL);
475 if (copy_from_user(params, _params, sizeof(*params))) {
479 err = snd_pcm_hw_params(substream, params);
480 if (copy_to_user(_params, params, sizeof(*params))) {
489 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
491 struct snd_pcm_runtime *runtime;
494 snd_assert(substream != NULL, return -ENXIO);
495 runtime = substream->runtime;
496 snd_assert(runtime != NULL, return -ENXIO);
497 snd_pcm_stream_lock_irq(substream);
498 switch (runtime->status->state) {
499 case SNDRV_PCM_STATE_SETUP:
500 case SNDRV_PCM_STATE_PREPARED:
503 snd_pcm_stream_unlock_irq(substream);
506 snd_pcm_stream_unlock_irq(substream);
507 if (atomic_read(&substream->mmap_count))
509 if (substream->ops->hw_free)
510 result = substream->ops->hw_free(substream);
511 runtime->status->state = SNDRV_PCM_STATE_OPEN;
512 remove_acceptable_latency(substream->latency_id);
516 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
517 struct snd_pcm_sw_params *params)
519 struct snd_pcm_runtime *runtime;
521 snd_assert(substream != NULL, return -ENXIO);
522 runtime = substream->runtime;
523 snd_assert(runtime != NULL, return -ENXIO);
524 snd_pcm_stream_lock_irq(substream);
525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
526 snd_pcm_stream_unlock_irq(substream);
529 snd_pcm_stream_unlock_irq(substream);
531 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
533 if (params->avail_min == 0)
535 if (params->xfer_align == 0 ||
536 params->xfer_align % runtime->min_align != 0)
538 if (params->silence_size >= runtime->boundary) {
539 if (params->silence_threshold != 0)
542 if (params->silence_size > params->silence_threshold)
544 if (params->silence_threshold > runtime->buffer_size)
547 snd_pcm_stream_lock_irq(substream);
548 runtime->tstamp_mode = params->tstamp_mode;
549 runtime->sleep_min = params->sleep_min;
550 runtime->period_step = params->period_step;
551 runtime->control->avail_min = params->avail_min;
552 runtime->start_threshold = params->start_threshold;
553 runtime->stop_threshold = params->stop_threshold;
554 runtime->silence_threshold = params->silence_threshold;
555 runtime->silence_size = params->silence_size;
556 runtime->xfer_align = params->xfer_align;
557 params->boundary = runtime->boundary;
558 if (snd_pcm_running(substream)) {
559 if (runtime->sleep_min)
560 snd_pcm_tick_prepare(substream);
562 snd_pcm_tick_set(substream, 0);
563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
564 runtime->silence_size > 0)
565 snd_pcm_playback_silence(substream, ULONG_MAX);
566 wake_up(&runtime->sleep);
568 snd_pcm_stream_unlock_irq(substream);
572 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
573 struct snd_pcm_sw_params __user * _params)
575 struct snd_pcm_sw_params params;
577 if (copy_from_user(¶ms, _params, sizeof(params)))
579 err = snd_pcm_sw_params(substream, ¶ms);
580 if (copy_to_user(_params, ¶ms, sizeof(params)))
585 int snd_pcm_status(struct snd_pcm_substream *substream,
586 struct snd_pcm_status *status)
588 struct snd_pcm_runtime *runtime = substream->runtime;
590 snd_pcm_stream_lock_irq(substream);
591 status->state = runtime->status->state;
592 status->suspended_state = runtime->status->suspended_state;
593 if (status->state == SNDRV_PCM_STATE_OPEN)
595 status->trigger_tstamp = runtime->trigger_tstamp;
596 if (snd_pcm_running(substream)) {
597 snd_pcm_update_hw_ptr(substream);
598 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
599 status->tstamp = runtime->status->tstamp;
601 getnstimeofday(&status->tstamp);
603 getnstimeofday(&status->tstamp);
604 status->appl_ptr = runtime->control->appl_ptr;
605 status->hw_ptr = runtime->status->hw_ptr;
606 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
607 status->avail = snd_pcm_playback_avail(runtime);
608 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
609 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
610 status->delay = runtime->buffer_size - status->avail;
614 status->avail = snd_pcm_capture_avail(runtime);
615 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
616 status->delay = status->avail;
620 status->avail_max = runtime->avail_max;
621 status->overrange = runtime->overrange;
622 runtime->avail_max = 0;
623 runtime->overrange = 0;
625 snd_pcm_stream_unlock_irq(substream);
629 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
630 struct snd_pcm_status __user * _status)
632 struct snd_pcm_status status;
633 struct snd_pcm_runtime *runtime;
636 snd_assert(substream != NULL, return -ENXIO);
637 runtime = substream->runtime;
638 memset(&status, 0, sizeof(status));
639 res = snd_pcm_status(substream, &status);
642 if (copy_to_user(_status, &status, sizeof(status)))
647 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
648 struct snd_pcm_channel_info * info)
650 struct snd_pcm_runtime *runtime;
651 unsigned int channel;
653 snd_assert(substream != NULL, return -ENXIO);
654 channel = info->channel;
655 runtime = substream->runtime;
656 snd_pcm_stream_lock_irq(substream);
657 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
658 snd_pcm_stream_unlock_irq(substream);
661 snd_pcm_stream_unlock_irq(substream);
662 if (channel >= runtime->channels)
664 memset(info, 0, sizeof(*info));
665 info->channel = channel;
666 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
669 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
670 struct snd_pcm_channel_info __user * _info)
672 struct snd_pcm_channel_info info;
675 if (copy_from_user(&info, _info, sizeof(info)))
677 res = snd_pcm_channel_info(substream, &info);
680 if (copy_to_user(_info, &info, sizeof(info)))
685 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
687 struct snd_pcm_runtime *runtime = substream->runtime;
688 if (runtime->trigger_master == NULL)
690 if (runtime->trigger_master == substream) {
691 getnstimeofday(&runtime->trigger_tstamp);
693 snd_pcm_trigger_tstamp(runtime->trigger_master);
694 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
696 runtime->trigger_master = NULL;
700 int (*pre_action)(struct snd_pcm_substream *substream, int state);
701 int (*do_action)(struct snd_pcm_substream *substream, int state);
702 void (*undo_action)(struct snd_pcm_substream *substream, int state);
703 void (*post_action)(struct snd_pcm_substream *substream, int state);
707 * this functions is core for handling of linked stream
708 * Note: the stream state might be changed also on failure
709 * Note2: call with calling stream lock + link lock
711 static int snd_pcm_action_group(struct action_ops *ops,
712 struct snd_pcm_substream *substream,
713 int state, int do_lock)
715 struct snd_pcm_substream *s = NULL;
716 struct snd_pcm_substream *s1;
719 snd_pcm_group_for_each_entry(s, substream) {
720 if (do_lock && s != substream)
721 spin_lock(&s->self_group.lock);
722 res = ops->pre_action(s, state);
726 snd_pcm_group_for_each_entry(s, substream) {
727 res = ops->do_action(s, state);
729 if (ops->undo_action) {
730 snd_pcm_group_for_each_entry(s1, substream) {
731 if (s1 == s) /* failed stream */
733 ops->undo_action(s1, state);
736 s = NULL; /* unlock all */
740 snd_pcm_group_for_each_entry(s, substream) {
741 ops->post_action(s, state);
746 snd_pcm_group_for_each_entry(s1, substream) {
748 spin_unlock(&s1->self_group.lock);
749 if (s1 == s) /* end */
757 * Note: call with stream lock
759 static int snd_pcm_action_single(struct action_ops *ops,
760 struct snd_pcm_substream *substream,
765 res = ops->pre_action(substream, state);
768 res = ops->do_action(substream, state);
770 ops->post_action(substream, state);
771 else if (ops->undo_action)
772 ops->undo_action(substream, state);
777 * Note: call with stream lock
779 static int snd_pcm_action(struct action_ops *ops,
780 struct snd_pcm_substream *substream,
785 if (snd_pcm_stream_linked(substream)) {
786 if (!spin_trylock(&substream->group->lock)) {
787 spin_unlock(&substream->self_group.lock);
788 spin_lock(&substream->group->lock);
789 spin_lock(&substream->self_group.lock);
791 res = snd_pcm_action_group(ops, substream, state, 1);
792 spin_unlock(&substream->group->lock);
794 res = snd_pcm_action_single(ops, substream, state);
800 * Note: don't use any locks before
802 static int snd_pcm_action_lock_irq(struct action_ops *ops,
803 struct snd_pcm_substream *substream,
808 read_lock_irq(&snd_pcm_link_rwlock);
809 if (snd_pcm_stream_linked(substream)) {
810 spin_lock(&substream->group->lock);
811 spin_lock(&substream->self_group.lock);
812 res = snd_pcm_action_group(ops, substream, state, 1);
813 spin_unlock(&substream->self_group.lock);
814 spin_unlock(&substream->group->lock);
816 spin_lock(&substream->self_group.lock);
817 res = snd_pcm_action_single(ops, substream, state);
818 spin_unlock(&substream->self_group.lock);
820 read_unlock_irq(&snd_pcm_link_rwlock);
826 static int snd_pcm_action_nonatomic(struct action_ops *ops,
827 struct snd_pcm_substream *substream,
832 down_read(&snd_pcm_link_rwsem);
833 if (snd_pcm_stream_linked(substream))
834 res = snd_pcm_action_group(ops, substream, state, 0);
836 res = snd_pcm_action_single(ops, substream, state);
837 up_read(&snd_pcm_link_rwsem);
844 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
846 struct snd_pcm_runtime *runtime = substream->runtime;
847 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
849 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
850 !snd_pcm_playback_data(substream))
852 runtime->trigger_master = substream;
856 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
858 if (substream->runtime->trigger_master != substream)
860 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
863 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
865 if (substream->runtime->trigger_master == substream)
866 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
869 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
871 struct snd_pcm_runtime *runtime = substream->runtime;
872 snd_pcm_trigger_tstamp(substream);
873 runtime->status->state = state;
874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
875 runtime->silence_size > 0)
876 snd_pcm_playback_silence(substream, ULONG_MAX);
877 if (runtime->sleep_min)
878 snd_pcm_tick_prepare(substream);
879 if (substream->timer)
880 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
881 &runtime->trigger_tstamp);
884 static struct action_ops snd_pcm_action_start = {
885 .pre_action = snd_pcm_pre_start,
886 .do_action = snd_pcm_do_start,
887 .undo_action = snd_pcm_undo_start,
888 .post_action = snd_pcm_post_start
893 * @substream: the PCM substream instance
895 * Start all linked streams.
897 int snd_pcm_start(struct snd_pcm_substream *substream)
899 return snd_pcm_action(&snd_pcm_action_start, substream,
900 SNDRV_PCM_STATE_RUNNING);
906 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
908 struct snd_pcm_runtime *runtime = substream->runtime;
909 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
911 runtime->trigger_master = substream;
915 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
917 if (substream->runtime->trigger_master == substream &&
918 snd_pcm_running(substream))
919 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
920 return 0; /* unconditonally stop all substreams */
923 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
925 struct snd_pcm_runtime *runtime = substream->runtime;
926 if (runtime->status->state != state) {
927 snd_pcm_trigger_tstamp(substream);
928 if (substream->timer)
929 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
930 &runtime->trigger_tstamp);
931 runtime->status->state = state;
932 snd_pcm_tick_set(substream, 0);
934 wake_up(&runtime->sleep);
937 static struct action_ops snd_pcm_action_stop = {
938 .pre_action = snd_pcm_pre_stop,
939 .do_action = snd_pcm_do_stop,
940 .post_action = snd_pcm_post_stop
945 * @substream: the PCM substream instance
946 * @state: PCM state after stopping the stream
948 * Try to stop all running streams in the substream group.
949 * The state of each stream is changed to the given value after that unconditionally.
951 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
953 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
956 EXPORT_SYMBOL(snd_pcm_stop);
960 * @substream: the PCM substream
962 * Stop the DMA only when the given stream is playback.
963 * The state is changed to SETUP.
964 * Unlike snd_pcm_stop(), this affects only the given stream.
966 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
968 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
969 SNDRV_PCM_STATE_SETUP);
975 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
977 struct snd_pcm_runtime *runtime = substream->runtime;
978 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
981 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
983 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
985 runtime->trigger_master = substream;
989 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
991 if (substream->runtime->trigger_master != substream)
993 return substream->ops->trigger(substream,
994 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
995 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
998 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1000 if (substream->runtime->trigger_master == substream)
1001 substream->ops->trigger(substream,
1002 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1003 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1006 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1008 struct snd_pcm_runtime *runtime = substream->runtime;
1009 snd_pcm_trigger_tstamp(substream);
1011 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1012 if (substream->timer)
1013 snd_timer_notify(substream->timer,
1014 SNDRV_TIMER_EVENT_MPAUSE,
1015 &runtime->trigger_tstamp);
1016 snd_pcm_tick_set(substream, 0);
1017 wake_up(&runtime->sleep);
1019 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1020 if (runtime->sleep_min)
1021 snd_pcm_tick_prepare(substream);
1022 if (substream->timer)
1023 snd_timer_notify(substream->timer,
1024 SNDRV_TIMER_EVENT_MCONTINUE,
1025 &runtime->trigger_tstamp);
1029 static struct action_ops snd_pcm_action_pause = {
1030 .pre_action = snd_pcm_pre_pause,
1031 .do_action = snd_pcm_do_pause,
1032 .undo_action = snd_pcm_undo_pause,
1033 .post_action = snd_pcm_post_pause
1037 * Push/release the pause for all linked streams.
1039 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1041 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1047 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1049 struct snd_pcm_runtime *runtime = substream->runtime;
1050 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1052 runtime->trigger_master = substream;
1056 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1058 struct snd_pcm_runtime *runtime = substream->runtime;
1059 if (runtime->trigger_master != substream)
1061 if (! snd_pcm_running(substream))
1063 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1064 return 0; /* suspend unconditionally */
1067 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1069 struct snd_pcm_runtime *runtime = substream->runtime;
1070 snd_pcm_trigger_tstamp(substream);
1071 if (substream->timer)
1072 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1073 &runtime->trigger_tstamp);
1074 runtime->status->suspended_state = runtime->status->state;
1075 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1076 snd_pcm_tick_set(substream, 0);
1077 wake_up(&runtime->sleep);
1080 static struct action_ops snd_pcm_action_suspend = {
1081 .pre_action = snd_pcm_pre_suspend,
1082 .do_action = snd_pcm_do_suspend,
1083 .post_action = snd_pcm_post_suspend
1088 * @substream: the PCM substream
1090 * Trigger SUSPEND to all linked streams.
1091 * After this call, all streams are changed to SUSPENDED state.
1093 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1096 unsigned long flags;
1101 snd_pcm_stream_lock_irqsave(substream, flags);
1102 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1103 snd_pcm_stream_unlock_irqrestore(substream, flags);
1107 EXPORT_SYMBOL(snd_pcm_suspend);
1110 * snd_pcm_suspend_all
1111 * @pcm: the PCM instance
1113 * Trigger SUSPEND to all substreams in the given pcm.
1114 * After this call, all streams are changed to SUSPENDED state.
1116 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1118 struct snd_pcm_substream *substream;
1119 int stream, err = 0;
1124 for (stream = 0; stream < 2; stream++) {
1125 for (substream = pcm->streams[stream].substream;
1126 substream; substream = substream->next) {
1127 /* FIXME: the open/close code should lock this as well */
1128 if (substream->runtime == NULL)
1130 err = snd_pcm_suspend(substream);
1131 if (err < 0 && err != -EBUSY)
1138 EXPORT_SYMBOL(snd_pcm_suspend_all);
1142 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1144 struct snd_pcm_runtime *runtime = substream->runtime;
1145 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1147 runtime->trigger_master = substream;
1151 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1153 struct snd_pcm_runtime *runtime = substream->runtime;
1154 if (runtime->trigger_master != substream)
1156 /* DMA not running previously? */
1157 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1158 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1159 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1161 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1164 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1166 if (substream->runtime->trigger_master == substream &&
1167 snd_pcm_running(substream))
1168 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1171 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1173 struct snd_pcm_runtime *runtime = substream->runtime;
1174 snd_pcm_trigger_tstamp(substream);
1175 if (substream->timer)
1176 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1177 &runtime->trigger_tstamp);
1178 runtime->status->state = runtime->status->suspended_state;
1179 if (runtime->sleep_min)
1180 snd_pcm_tick_prepare(substream);
1183 static struct action_ops snd_pcm_action_resume = {
1184 .pre_action = snd_pcm_pre_resume,
1185 .do_action = snd_pcm_do_resume,
1186 .undo_action = snd_pcm_undo_resume,
1187 .post_action = snd_pcm_post_resume
1190 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1192 struct snd_card *card = substream->pcm->card;
1195 snd_power_lock(card);
1196 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1197 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1198 snd_power_unlock(card);
1204 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1209 #endif /* CONFIG_PM */
1214 * Change the RUNNING stream(s) to XRUN state.
1216 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1218 struct snd_card *card = substream->pcm->card;
1219 struct snd_pcm_runtime *runtime = substream->runtime;
1222 snd_power_lock(card);
1223 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1224 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1229 snd_pcm_stream_lock_irq(substream);
1230 switch (runtime->status->state) {
1231 case SNDRV_PCM_STATE_XRUN:
1232 result = 0; /* already there */
1234 case SNDRV_PCM_STATE_RUNNING:
1235 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1240 snd_pcm_stream_unlock_irq(substream);
1242 snd_power_unlock(card);
1249 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1251 struct snd_pcm_runtime *runtime = substream->runtime;
1252 switch (runtime->status->state) {
1253 case SNDRV_PCM_STATE_RUNNING:
1254 case SNDRV_PCM_STATE_PREPARED:
1255 case SNDRV_PCM_STATE_PAUSED:
1256 case SNDRV_PCM_STATE_SUSPENDED:
1263 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1265 struct snd_pcm_runtime *runtime = substream->runtime;
1266 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1269 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1270 runtime->hw_ptr_base = 0;
1271 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1272 runtime->status->hw_ptr % runtime->period_size;
1273 runtime->silence_start = runtime->status->hw_ptr;
1274 runtime->silence_filled = 0;
1278 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1280 struct snd_pcm_runtime *runtime = substream->runtime;
1281 runtime->control->appl_ptr = runtime->status->hw_ptr;
1282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1283 runtime->silence_size > 0)
1284 snd_pcm_playback_silence(substream, ULONG_MAX);
1287 static struct action_ops snd_pcm_action_reset = {
1288 .pre_action = snd_pcm_pre_reset,
1289 .do_action = snd_pcm_do_reset,
1290 .post_action = snd_pcm_post_reset
1293 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1295 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1301 /* we use the second argument for updating f_flags */
1302 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1305 struct snd_pcm_runtime *runtime = substream->runtime;
1306 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1307 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1309 if (snd_pcm_running(substream))
1311 substream->f_flags = f_flags;
1315 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1318 err = substream->ops->prepare(substream);
1321 return snd_pcm_do_reset(substream, 0);
1324 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1326 struct snd_pcm_runtime *runtime = substream->runtime;
1327 runtime->control->appl_ptr = runtime->status->hw_ptr;
1328 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1331 static struct action_ops snd_pcm_action_prepare = {
1332 .pre_action = snd_pcm_pre_prepare,
1333 .do_action = snd_pcm_do_prepare,
1334 .post_action = snd_pcm_post_prepare
1339 * @substream: the PCM substream instance
1340 * @file: file to refer f_flags
1342 * Prepare the PCM substream to be triggerable.
1344 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1348 struct snd_card *card = substream->pcm->card;
1352 f_flags = file->f_flags;
1354 f_flags = substream->f_flags;
1356 snd_power_lock(card);
1357 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1358 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1359 substream, f_flags);
1360 snd_power_unlock(card);
1368 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1370 if (substream->f_flags & O_NONBLOCK)
1372 substream->runtime->trigger_master = substream;
1376 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1378 struct snd_pcm_runtime *runtime = substream->runtime;
1379 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1380 switch (runtime->status->state) {
1381 case SNDRV_PCM_STATE_PREPARED:
1382 /* start playback stream if possible */
1383 if (! snd_pcm_playback_empty(substream)) {
1384 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1385 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1388 case SNDRV_PCM_STATE_RUNNING:
1389 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1395 /* stop running stream */
1396 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1397 int state = snd_pcm_capture_avail(runtime) > 0 ?
1398 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1399 snd_pcm_do_stop(substream, state);
1400 snd_pcm_post_stop(substream, state);
1406 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1410 static struct action_ops snd_pcm_action_drain_init = {
1411 .pre_action = snd_pcm_pre_drain_init,
1412 .do_action = snd_pcm_do_drain_init,
1413 .post_action = snd_pcm_post_drain_init
1417 struct snd_pcm_substream *substream;
1419 snd_pcm_uframes_t stop_threshold;
1422 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1425 * Drain the stream(s).
1426 * When the substream is linked, sync until the draining of all playback streams
1428 * After this call, all streams are supposed to be either SETUP or DRAINING
1429 * (capture only) state.
1431 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1433 struct snd_card *card;
1434 struct snd_pcm_runtime *runtime;
1435 struct snd_pcm_substream *s;
1438 struct drain_rec *drec, drec_tmp, *d;
1440 snd_assert(substream != NULL, return -ENXIO);
1441 card = substream->pcm->card;
1442 runtime = substream->runtime;
1444 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1447 snd_power_lock(card);
1448 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1449 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1451 snd_power_unlock(card);
1456 /* allocate temporary record for drain sync */
1457 down_read(&snd_pcm_link_rwsem);
1458 if (snd_pcm_stream_linked(substream)) {
1459 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1461 up_read(&snd_pcm_link_rwsem);
1462 snd_power_unlock(card);
1468 /* count only playback streams */
1470 snd_pcm_group_for_each_entry(s, substream) {
1471 runtime = s->runtime;
1472 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1473 d = &drec[num_drecs++];
1475 init_waitqueue_entry(&d->wait, current);
1476 add_wait_queue(&runtime->sleep, &d->wait);
1477 /* stop_threshold fixup to avoid endless loop when
1478 * stop_threshold > buffer_size
1480 d->stop_threshold = runtime->stop_threshold;
1481 if (runtime->stop_threshold > runtime->buffer_size)
1482 runtime->stop_threshold = runtime->buffer_size;
1485 up_read(&snd_pcm_link_rwsem);
1487 snd_pcm_stream_lock_irq(substream);
1489 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1490 snd_pcm_pause(substream, 0);
1492 /* pre-start/stop - all running streams are changed to DRAINING state */
1493 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1495 snd_pcm_stream_unlock_irq(substream);
1501 if (signal_pending(current)) {
1502 result = -ERESTARTSYS;
1506 for (i = 0; i < num_drecs; i++) {
1507 runtime = drec[i].substream->runtime;
1508 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1512 break; /* yes, all drained */
1514 set_current_state(TASK_INTERRUPTIBLE);
1515 snd_pcm_stream_unlock_irq(substream);
1516 snd_power_unlock(card);
1517 tout = schedule_timeout(10 * HZ);
1518 snd_power_lock(card);
1519 snd_pcm_stream_lock_irq(substream);
1521 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1524 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1525 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1532 snd_pcm_stream_unlock_irq(substream);
1535 for (i = 0; i < num_drecs; i++) {
1537 runtime = d->substream->runtime;
1538 remove_wait_queue(&runtime->sleep, &d->wait);
1539 runtime->stop_threshold = d->stop_threshold;
1542 if (drec != &drec_tmp)
1544 snd_power_unlock(card);
1552 * Immediately put all linked substreams into SETUP state.
1554 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1556 struct snd_pcm_runtime *runtime;
1557 struct snd_card *card;
1560 snd_assert(substream != NULL, return -ENXIO);
1561 runtime = substream->runtime;
1562 card = substream->pcm->card;
1564 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1565 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1568 snd_power_lock(card);
1569 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1570 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1575 snd_pcm_stream_lock_irq(substream);
1577 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1578 snd_pcm_pause(substream, 0);
1580 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1581 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1582 snd_pcm_stream_unlock_irq(substream);
1584 snd_power_unlock(card);
1589 /* WARNING: Don't forget to fput back the file */
1590 static struct file *snd_pcm_file_fd(int fd)
1593 struct inode *inode;
1599 inode = file->f_path.dentry->d_inode;
1600 if (!S_ISCHR(inode->i_mode) ||
1601 imajor(inode) != snd_major) {
1605 minor = iminor(inode);
1606 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1607 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1617 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1621 struct snd_pcm_file *pcm_file;
1622 struct snd_pcm_substream *substream1;
1624 file = snd_pcm_file_fd(fd);
1627 pcm_file = file->private_data;
1628 substream1 = pcm_file->substream;
1629 down_write(&snd_pcm_link_rwsem);
1630 write_lock_irq(&snd_pcm_link_rwlock);
1631 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1632 substream->runtime->status->state != substream1->runtime->status->state) {
1636 if (snd_pcm_stream_linked(substream1)) {
1640 if (!snd_pcm_stream_linked(substream)) {
1641 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1642 if (substream->group == NULL) {
1646 spin_lock_init(&substream->group->lock);
1647 INIT_LIST_HEAD(&substream->group->substreams);
1648 list_add_tail(&substream->link_list, &substream->group->substreams);
1649 substream->group->count = 1;
1651 list_add_tail(&substream1->link_list, &substream->group->substreams);
1652 substream->group->count++;
1653 substream1->group = substream->group;
1655 write_unlock_irq(&snd_pcm_link_rwlock);
1656 up_write(&snd_pcm_link_rwsem);
1661 static void relink_to_local(struct snd_pcm_substream *substream)
1663 substream->group = &substream->self_group;
1664 INIT_LIST_HEAD(&substream->self_group.substreams);
1665 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1668 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1670 struct snd_pcm_substream *s;
1673 down_write(&snd_pcm_link_rwsem);
1674 write_lock_irq(&snd_pcm_link_rwlock);
1675 if (!snd_pcm_stream_linked(substream)) {
1679 list_del(&substream->link_list);
1680 substream->group->count--;
1681 if (substream->group->count == 1) { /* detach the last stream, too */
1682 snd_pcm_group_for_each_entry(s, substream) {
1686 kfree(substream->group);
1688 relink_to_local(substream);
1690 write_unlock_irq(&snd_pcm_link_rwlock);
1691 up_write(&snd_pcm_link_rwsem);
1698 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1699 struct snd_pcm_hw_rule *rule)
1701 struct snd_interval t;
1702 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1703 hw_param_interval_c(params, rule->deps[1]), &t);
1704 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1707 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1708 struct snd_pcm_hw_rule *rule)
1710 struct snd_interval t;
1711 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1712 hw_param_interval_c(params, rule->deps[1]), &t);
1713 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1716 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1717 struct snd_pcm_hw_rule *rule)
1719 struct snd_interval t;
1720 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1721 hw_param_interval_c(params, rule->deps[1]),
1722 (unsigned long) rule->private, &t);
1723 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1726 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1727 struct snd_pcm_hw_rule *rule)
1729 struct snd_interval t;
1730 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1731 (unsigned long) rule->private,
1732 hw_param_interval_c(params, rule->deps[1]), &t);
1733 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1736 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1737 struct snd_pcm_hw_rule *rule)
1740 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1742 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1744 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1746 if (! snd_mask_test(mask, k))
1748 bits = snd_pcm_format_physical_width(k);
1750 continue; /* ignore invalid formats */
1751 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1752 snd_mask_reset(&m, k);
1754 return snd_mask_refine(mask, &m);
1757 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1758 struct snd_pcm_hw_rule *rule)
1760 struct snd_interval t;
1766 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1768 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1770 bits = snd_pcm_format_physical_width(k);
1772 continue; /* ignore invalid formats */
1773 if (t.min > (unsigned)bits)
1775 if (t.max < (unsigned)bits)
1779 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1782 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1783 #error "Change this table"
1786 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1787 48000, 64000, 88200, 96000, 176400, 192000 };
1789 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1790 struct snd_pcm_hw_rule *rule)
1792 struct snd_pcm_hardware *hw = rule->private;
1793 return snd_interval_list(hw_param_interval(params, rule->var),
1794 ARRAY_SIZE(rates), rates, hw->rates);
1797 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1798 struct snd_pcm_hw_rule *rule)
1800 struct snd_interval t;
1801 struct snd_pcm_substream *substream = rule->private;
1803 t.max = substream->buffer_bytes_max;
1807 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1810 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1812 struct snd_pcm_runtime *runtime = substream->runtime;
1813 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1816 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1817 snd_mask_any(constrs_mask(constrs, k));
1820 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1821 snd_interval_any(constrs_interval(constrs, k));
1824 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1825 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1826 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1827 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1828 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1831 snd_pcm_hw_rule_format, NULL,
1832 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1836 snd_pcm_hw_rule_sample_bits, NULL,
1837 SNDRV_PCM_HW_PARAM_FORMAT,
1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1847 snd_pcm_hw_rule_mul, NULL,
1848 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1852 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1853 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1857 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1858 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1862 snd_pcm_hw_rule_div, NULL,
1863 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1867 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1868 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1872 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1877 snd_pcm_hw_rule_div, NULL,
1878 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1882 snd_pcm_hw_rule_div, NULL,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1888 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1892 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1893 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1897 snd_pcm_hw_rule_mul, NULL,
1898 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1902 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1903 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1907 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1908 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1912 snd_pcm_hw_rule_muldivk, (void*) 8,
1913 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1917 snd_pcm_hw_rule_muldivk, (void*) 8,
1918 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1921 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1922 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1923 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1926 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1927 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1928 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1934 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1936 struct snd_pcm_runtime *runtime = substream->runtime;
1937 struct snd_pcm_hardware *hw = &runtime->hw;
1939 unsigned int mask = 0;
1941 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1942 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1943 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1944 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1945 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1946 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1947 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1948 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1949 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1950 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1951 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1953 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1954 snd_assert(err >= 0, return -EINVAL);
1956 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1957 snd_assert(err >= 0, return -EINVAL);
1959 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1960 snd_assert(err >= 0, return -EINVAL);
1962 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1963 hw->channels_min, hw->channels_max);
1964 snd_assert(err >= 0, return -EINVAL);
1966 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1967 hw->rate_min, hw->rate_max);
1968 snd_assert(err >= 0, return -EINVAL);
1970 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1971 hw->period_bytes_min, hw->period_bytes_max);
1972 snd_assert(err >= 0, return -EINVAL);
1974 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1975 hw->periods_min, hw->periods_max);
1976 snd_assert(err >= 0, return -EINVAL);
1978 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1979 hw->period_bytes_min, hw->buffer_bytes_max);
1980 snd_assert(err >= 0, return -EINVAL);
1982 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1983 snd_pcm_hw_rule_buffer_bytes_max, substream,
1984 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1989 if (runtime->dma_bytes) {
1990 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1991 snd_assert(err >= 0, return -EINVAL);
1994 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1995 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1996 snd_pcm_hw_rule_rate, hw,
1997 SNDRV_PCM_HW_PARAM_RATE, -1);
2002 /* FIXME: this belong to lowlevel */
2003 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
2004 1000000 / HZ, 1000000 / HZ);
2005 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2010 static void pcm_release_private(struct snd_pcm_substream *substream)
2012 snd_pcm_unlink(substream);
2015 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2017 substream->ref_count--;
2018 if (substream->ref_count > 0)
2021 snd_pcm_drop(substream);
2022 if (substream->hw_opened) {
2023 if (substream->ops->hw_free != NULL)
2024 substream->ops->hw_free(substream);
2025 substream->ops->close(substream);
2026 substream->hw_opened = 0;
2028 if (substream->pcm_release) {
2029 substream->pcm_release(substream);
2030 substream->pcm_release = NULL;
2032 snd_pcm_detach_substream(substream);
2035 EXPORT_SYMBOL(snd_pcm_release_substream);
2037 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2039 struct snd_pcm_substream **rsubstream)
2041 struct snd_pcm_substream *substream;
2044 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2047 if (substream->ref_count > 1) {
2048 *rsubstream = substream;
2052 err = snd_pcm_hw_constraints_init(substream);
2054 snd_printd("snd_pcm_hw_constraints_init failed\n");
2058 if ((err = substream->ops->open(substream)) < 0)
2061 substream->hw_opened = 1;
2063 err = snd_pcm_hw_constraints_complete(substream);
2065 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2069 *rsubstream = substream;
2073 snd_pcm_release_substream(substream);
2077 EXPORT_SYMBOL(snd_pcm_open_substream);
2079 static int snd_pcm_open_file(struct file *file,
2080 struct snd_pcm *pcm,
2082 struct snd_pcm_file **rpcm_file)
2084 struct snd_pcm_file *pcm_file;
2085 struct snd_pcm_substream *substream;
2086 struct snd_pcm_str *str;
2089 snd_assert(rpcm_file != NULL, return -EINVAL);
2092 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2096 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2097 if (pcm_file == NULL) {
2098 snd_pcm_release_substream(substream);
2101 pcm_file->substream = substream;
2102 if (substream->ref_count == 1) {
2103 str = substream->pstr;
2104 substream->file = pcm_file;
2105 substream->pcm_release = pcm_release_private;
2107 file->private_data = pcm_file;
2108 *rpcm_file = pcm_file;
2112 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2114 struct snd_pcm *pcm;
2116 pcm = snd_lookup_minor_data(iminor(inode),
2117 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2118 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2121 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2123 struct snd_pcm *pcm;
2125 pcm = snd_lookup_minor_data(iminor(inode),
2126 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2127 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2130 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2133 struct snd_pcm_file *pcm_file;
2140 err = snd_card_file_add(pcm->card, file);
2143 if (!try_module_get(pcm->card->module)) {
2147 init_waitqueue_entry(&wait, current);
2148 add_wait_queue(&pcm->open_wait, &wait);
2149 mutex_lock(&pcm->open_mutex);
2151 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2154 if (err == -EAGAIN) {
2155 if (file->f_flags & O_NONBLOCK) {
2161 set_current_state(TASK_INTERRUPTIBLE);
2162 mutex_unlock(&pcm->open_mutex);
2164 mutex_lock(&pcm->open_mutex);
2165 if (signal_pending(current)) {
2170 remove_wait_queue(&pcm->open_wait, &wait);
2171 mutex_unlock(&pcm->open_mutex);
2177 module_put(pcm->card->module);
2179 snd_card_file_remove(pcm->card, file);
2184 static int snd_pcm_release(struct inode *inode, struct file *file)
2186 struct snd_pcm *pcm;
2187 struct snd_pcm_substream *substream;
2188 struct snd_pcm_file *pcm_file;
2190 pcm_file = file->private_data;
2191 substream = pcm_file->substream;
2192 snd_assert(substream != NULL, return -ENXIO);
2193 pcm = substream->pcm;
2194 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2195 mutex_lock(&pcm->open_mutex);
2196 snd_pcm_release_substream(substream);
2198 mutex_unlock(&pcm->open_mutex);
2199 wake_up(&pcm->open_wait);
2200 module_put(pcm->card->module);
2201 snd_card_file_remove(pcm->card, file);
2205 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2206 snd_pcm_uframes_t frames)
2208 struct snd_pcm_runtime *runtime = substream->runtime;
2209 snd_pcm_sframes_t appl_ptr;
2210 snd_pcm_sframes_t ret;
2211 snd_pcm_sframes_t hw_avail;
2216 snd_pcm_stream_lock_irq(substream);
2217 switch (runtime->status->state) {
2218 case SNDRV_PCM_STATE_PREPARED:
2220 case SNDRV_PCM_STATE_DRAINING:
2221 case SNDRV_PCM_STATE_RUNNING:
2222 if (snd_pcm_update_hw_ptr(substream) >= 0)
2225 case SNDRV_PCM_STATE_XRUN:
2233 hw_avail = snd_pcm_playback_hw_avail(runtime);
2234 if (hw_avail <= 0) {
2238 if (frames > (snd_pcm_uframes_t)hw_avail)
2241 frames -= frames % runtime->xfer_align;
2242 appl_ptr = runtime->control->appl_ptr - frames;
2244 appl_ptr += runtime->boundary;
2245 runtime->control->appl_ptr = appl_ptr;
2246 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2248 snd_pcm_tick_prepare(substream);
2251 snd_pcm_stream_unlock_irq(substream);
2255 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2256 snd_pcm_uframes_t frames)
2258 struct snd_pcm_runtime *runtime = substream->runtime;
2259 snd_pcm_sframes_t appl_ptr;
2260 snd_pcm_sframes_t ret;
2261 snd_pcm_sframes_t hw_avail;
2266 snd_pcm_stream_lock_irq(substream);
2267 switch (runtime->status->state) {
2268 case SNDRV_PCM_STATE_PREPARED:
2269 case SNDRV_PCM_STATE_DRAINING:
2271 case SNDRV_PCM_STATE_RUNNING:
2272 if (snd_pcm_update_hw_ptr(substream) >= 0)
2275 case SNDRV_PCM_STATE_XRUN:
2283 hw_avail = snd_pcm_capture_hw_avail(runtime);
2284 if (hw_avail <= 0) {
2288 if (frames > (snd_pcm_uframes_t)hw_avail)
2291 frames -= frames % runtime->xfer_align;
2292 appl_ptr = runtime->control->appl_ptr - frames;
2294 appl_ptr += runtime->boundary;
2295 runtime->control->appl_ptr = appl_ptr;
2296 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2298 snd_pcm_tick_prepare(substream);
2301 snd_pcm_stream_unlock_irq(substream);
2305 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2306 snd_pcm_uframes_t frames)
2308 struct snd_pcm_runtime *runtime = substream->runtime;
2309 snd_pcm_sframes_t appl_ptr;
2310 snd_pcm_sframes_t ret;
2311 snd_pcm_sframes_t avail;
2316 snd_pcm_stream_lock_irq(substream);
2317 switch (runtime->status->state) {
2318 case SNDRV_PCM_STATE_PREPARED:
2319 case SNDRV_PCM_STATE_PAUSED:
2321 case SNDRV_PCM_STATE_DRAINING:
2322 case SNDRV_PCM_STATE_RUNNING:
2323 if (snd_pcm_update_hw_ptr(substream) >= 0)
2326 case SNDRV_PCM_STATE_XRUN:
2334 avail = snd_pcm_playback_avail(runtime);
2339 if (frames > (snd_pcm_uframes_t)avail)
2342 frames -= frames % runtime->xfer_align;
2343 appl_ptr = runtime->control->appl_ptr + frames;
2344 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2345 appl_ptr -= runtime->boundary;
2346 runtime->control->appl_ptr = appl_ptr;
2347 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2349 snd_pcm_tick_prepare(substream);
2352 snd_pcm_stream_unlock_irq(substream);
2356 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2357 snd_pcm_uframes_t frames)
2359 struct snd_pcm_runtime *runtime = substream->runtime;
2360 snd_pcm_sframes_t appl_ptr;
2361 snd_pcm_sframes_t ret;
2362 snd_pcm_sframes_t avail;
2367 snd_pcm_stream_lock_irq(substream);
2368 switch (runtime->status->state) {
2369 case SNDRV_PCM_STATE_PREPARED:
2370 case SNDRV_PCM_STATE_DRAINING:
2371 case SNDRV_PCM_STATE_PAUSED:
2373 case SNDRV_PCM_STATE_RUNNING:
2374 if (snd_pcm_update_hw_ptr(substream) >= 0)
2377 case SNDRV_PCM_STATE_XRUN:
2385 avail = snd_pcm_capture_avail(runtime);
2390 if (frames > (snd_pcm_uframes_t)avail)
2393 frames -= frames % runtime->xfer_align;
2394 appl_ptr = runtime->control->appl_ptr + frames;
2395 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2396 appl_ptr -= runtime->boundary;
2397 runtime->control->appl_ptr = appl_ptr;
2398 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2400 snd_pcm_tick_prepare(substream);
2403 snd_pcm_stream_unlock_irq(substream);
2407 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2409 struct snd_pcm_runtime *runtime = substream->runtime;
2412 snd_pcm_stream_lock_irq(substream);
2413 switch (runtime->status->state) {
2414 case SNDRV_PCM_STATE_DRAINING:
2415 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2417 case SNDRV_PCM_STATE_RUNNING:
2418 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2421 case SNDRV_PCM_STATE_PREPARED:
2422 case SNDRV_PCM_STATE_SUSPENDED:
2425 case SNDRV_PCM_STATE_XRUN:
2433 snd_pcm_stream_unlock_irq(substream);
2437 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2438 snd_pcm_sframes_t __user *res)
2440 struct snd_pcm_runtime *runtime = substream->runtime;
2442 snd_pcm_sframes_t n = 0;
2444 snd_pcm_stream_lock_irq(substream);
2445 switch (runtime->status->state) {
2446 case SNDRV_PCM_STATE_DRAINING:
2447 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2449 case SNDRV_PCM_STATE_RUNNING:
2450 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2453 case SNDRV_PCM_STATE_PREPARED:
2454 case SNDRV_PCM_STATE_SUSPENDED:
2456 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2457 n = snd_pcm_playback_hw_avail(runtime);
2459 n = snd_pcm_capture_avail(runtime);
2461 case SNDRV_PCM_STATE_XRUN:
2469 snd_pcm_stream_unlock_irq(substream);
2471 if (put_user(n, res))
2476 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2477 struct snd_pcm_sync_ptr __user *_sync_ptr)
2479 struct snd_pcm_runtime *runtime = substream->runtime;
2480 struct snd_pcm_sync_ptr sync_ptr;
2481 volatile struct snd_pcm_mmap_status *status;
2482 volatile struct snd_pcm_mmap_control *control;
2485 memset(&sync_ptr, 0, sizeof(sync_ptr));
2486 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2488 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2490 status = runtime->status;
2491 control = runtime->control;
2492 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2493 err = snd_pcm_hwsync(substream);
2497 snd_pcm_stream_lock_irq(substream);
2498 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2499 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2501 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2502 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2503 control->avail_min = sync_ptr.c.control.avail_min;
2505 sync_ptr.c.control.avail_min = control->avail_min;
2506 sync_ptr.s.status.state = status->state;
2507 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2508 sync_ptr.s.status.tstamp = status->tstamp;
2509 sync_ptr.s.status.suspended_state = status->suspended_state;
2510 snd_pcm_stream_unlock_irq(substream);
2511 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2516 static int snd_pcm_common_ioctl1(struct file *file,
2517 struct snd_pcm_substream *substream,
2518 unsigned int cmd, void __user *arg)
2520 snd_assert(substream != NULL, return -ENXIO);
2523 case SNDRV_PCM_IOCTL_PVERSION:
2524 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2525 case SNDRV_PCM_IOCTL_INFO:
2526 return snd_pcm_info_user(substream, arg);
2527 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2529 case SNDRV_PCM_IOCTL_HW_REFINE:
2530 return snd_pcm_hw_refine_user(substream, arg);
2531 case SNDRV_PCM_IOCTL_HW_PARAMS:
2532 return snd_pcm_hw_params_user(substream, arg);
2533 case SNDRV_PCM_IOCTL_HW_FREE:
2534 return snd_pcm_hw_free(substream);
2535 case SNDRV_PCM_IOCTL_SW_PARAMS:
2536 return snd_pcm_sw_params_user(substream, arg);
2537 case SNDRV_PCM_IOCTL_STATUS:
2538 return snd_pcm_status_user(substream, arg);
2539 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2540 return snd_pcm_channel_info_user(substream, arg);
2541 case SNDRV_PCM_IOCTL_PREPARE:
2542 return snd_pcm_prepare(substream, file);
2543 case SNDRV_PCM_IOCTL_RESET:
2544 return snd_pcm_reset(substream);
2545 case SNDRV_PCM_IOCTL_START:
2546 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2547 case SNDRV_PCM_IOCTL_LINK:
2548 return snd_pcm_link(substream, (int)(unsigned long) arg);
2549 case SNDRV_PCM_IOCTL_UNLINK:
2550 return snd_pcm_unlink(substream);
2551 case SNDRV_PCM_IOCTL_RESUME:
2552 return snd_pcm_resume(substream);
2553 case SNDRV_PCM_IOCTL_XRUN:
2554 return snd_pcm_xrun(substream);
2555 case SNDRV_PCM_IOCTL_HWSYNC:
2556 return snd_pcm_hwsync(substream);
2557 case SNDRV_PCM_IOCTL_DELAY:
2558 return snd_pcm_delay(substream, arg);
2559 case SNDRV_PCM_IOCTL_SYNC_PTR:
2560 return snd_pcm_sync_ptr(substream, arg);
2561 #ifdef CONFIG_SND_SUPPORT_OLD_API
2562 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2563 return snd_pcm_hw_refine_old_user(substream, arg);
2564 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2565 return snd_pcm_hw_params_old_user(substream, arg);
2567 case SNDRV_PCM_IOCTL_DRAIN:
2568 return snd_pcm_drain(substream);
2569 case SNDRV_PCM_IOCTL_DROP:
2570 return snd_pcm_drop(substream);
2571 case SNDRV_PCM_IOCTL_PAUSE:
2574 snd_pcm_stream_lock_irq(substream);
2575 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2576 snd_pcm_stream_unlock_irq(substream);
2580 snd_printd("unknown ioctl = 0x%x\n", cmd);
2584 static int snd_pcm_playback_ioctl1(struct file *file,
2585 struct snd_pcm_substream *substream,
2586 unsigned int cmd, void __user *arg)
2588 snd_assert(substream != NULL, return -ENXIO);
2589 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2591 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2593 struct snd_xferi xferi;
2594 struct snd_xferi __user *_xferi = arg;
2595 struct snd_pcm_runtime *runtime = substream->runtime;
2596 snd_pcm_sframes_t result;
2597 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2599 if (put_user(0, &_xferi->result))
2601 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2603 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2604 __put_user(result, &_xferi->result);
2605 return result < 0 ? result : 0;
2607 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2609 struct snd_xfern xfern;
2610 struct snd_xfern __user *_xfern = arg;
2611 struct snd_pcm_runtime *runtime = substream->runtime;
2613 snd_pcm_sframes_t result;
2614 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2616 if (runtime->channels > 128)
2618 if (put_user(0, &_xfern->result))
2620 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2622 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2625 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2629 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2631 __put_user(result, &_xfern->result);
2632 return result < 0 ? result : 0;
2634 case SNDRV_PCM_IOCTL_REWIND:
2636 snd_pcm_uframes_t frames;
2637 snd_pcm_uframes_t __user *_frames = arg;
2638 snd_pcm_sframes_t result;
2639 if (get_user(frames, _frames))
2641 if (put_user(0, _frames))
2643 result = snd_pcm_playback_rewind(substream, frames);
2644 __put_user(result, _frames);
2645 return result < 0 ? result : 0;
2647 case SNDRV_PCM_IOCTL_FORWARD:
2649 snd_pcm_uframes_t frames;
2650 snd_pcm_uframes_t __user *_frames = arg;
2651 snd_pcm_sframes_t result;
2652 if (get_user(frames, _frames))
2654 if (put_user(0, _frames))
2656 result = snd_pcm_playback_forward(substream, frames);
2657 __put_user(result, _frames);
2658 return result < 0 ? result : 0;
2661 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2664 static int snd_pcm_capture_ioctl1(struct file *file,
2665 struct snd_pcm_substream *substream,
2666 unsigned int cmd, void __user *arg)
2668 snd_assert(substream != NULL, return -ENXIO);
2669 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2671 case SNDRV_PCM_IOCTL_READI_FRAMES:
2673 struct snd_xferi xferi;
2674 struct snd_xferi __user *_xferi = arg;
2675 struct snd_pcm_runtime *runtime = substream->runtime;
2676 snd_pcm_sframes_t result;
2677 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2679 if (put_user(0, &_xferi->result))
2681 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2683 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2684 __put_user(result, &_xferi->result);
2685 return result < 0 ? result : 0;
2687 case SNDRV_PCM_IOCTL_READN_FRAMES:
2689 struct snd_xfern xfern;
2690 struct snd_xfern __user *_xfern = arg;
2691 struct snd_pcm_runtime *runtime = substream->runtime;
2693 snd_pcm_sframes_t result;
2694 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2696 if (runtime->channels > 128)
2698 if (put_user(0, &_xfern->result))
2700 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2702 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2705 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2709 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2711 __put_user(result, &_xfern->result);
2712 return result < 0 ? result : 0;
2714 case SNDRV_PCM_IOCTL_REWIND:
2716 snd_pcm_uframes_t frames;
2717 snd_pcm_uframes_t __user *_frames = arg;
2718 snd_pcm_sframes_t result;
2719 if (get_user(frames, _frames))
2721 if (put_user(0, _frames))
2723 result = snd_pcm_capture_rewind(substream, frames);
2724 __put_user(result, _frames);
2725 return result < 0 ? result : 0;
2727 case SNDRV_PCM_IOCTL_FORWARD:
2729 snd_pcm_uframes_t frames;
2730 snd_pcm_uframes_t __user *_frames = arg;
2731 snd_pcm_sframes_t result;
2732 if (get_user(frames, _frames))
2734 if (put_user(0, _frames))
2736 result = snd_pcm_capture_forward(substream, frames);
2737 __put_user(result, _frames);
2738 return result < 0 ? result : 0;
2741 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2744 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2747 struct snd_pcm_file *pcm_file;
2749 pcm_file = file->private_data;
2751 if (((cmd >> 8) & 0xff) != 'A')
2754 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2755 (void __user *)arg);
2758 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2761 struct snd_pcm_file *pcm_file;
2763 pcm_file = file->private_data;
2765 if (((cmd >> 8) & 0xff) != 'A')
2768 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2769 (void __user *)arg);
2772 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2773 unsigned int cmd, void *arg)
2778 fs = snd_enter_user();
2779 switch (substream->stream) {
2780 case SNDRV_PCM_STREAM_PLAYBACK:
2781 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2782 (void __user *)arg);
2784 case SNDRV_PCM_STREAM_CAPTURE:
2785 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2786 (void __user *)arg);
2796 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2798 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2801 struct snd_pcm_file *pcm_file;
2802 struct snd_pcm_substream *substream;
2803 struct snd_pcm_runtime *runtime;
2804 snd_pcm_sframes_t result;
2806 pcm_file = file->private_data;
2807 substream = pcm_file->substream;
2808 snd_assert(substream != NULL, return -ENXIO);
2809 runtime = substream->runtime;
2810 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2812 if (!frame_aligned(runtime, count))
2814 count = bytes_to_frames(runtime, count);
2815 result = snd_pcm_lib_read(substream, buf, count);
2817 result = frames_to_bytes(runtime, result);
2821 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2822 size_t count, loff_t * offset)
2824 struct snd_pcm_file *pcm_file;
2825 struct snd_pcm_substream *substream;
2826 struct snd_pcm_runtime *runtime;
2827 snd_pcm_sframes_t result;
2829 pcm_file = file->private_data;
2830 substream = pcm_file->substream;
2831 snd_assert(substream != NULL, result = -ENXIO; goto end);
2832 runtime = substream->runtime;
2833 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2837 if (!frame_aligned(runtime, count)) {
2841 count = bytes_to_frames(runtime, count);
2842 result = snd_pcm_lib_write(substream, buf, count);
2844 result = frames_to_bytes(runtime, result);
2849 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2850 unsigned long nr_segs, loff_t pos)
2853 struct snd_pcm_file *pcm_file;
2854 struct snd_pcm_substream *substream;
2855 struct snd_pcm_runtime *runtime;
2856 snd_pcm_sframes_t result;
2859 snd_pcm_uframes_t frames;
2861 pcm_file = iocb->ki_filp->private_data;
2862 substream = pcm_file->substream;
2863 snd_assert(substream != NULL, return -ENXIO);
2864 runtime = substream->runtime;
2865 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 if (nr_segs > 1024 || nr_segs != runtime->channels)
2869 if (!frame_aligned(runtime, iov->iov_len))
2871 frames = bytes_to_samples(runtime, iov->iov_len);
2872 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2875 for (i = 0; i < nr_segs; ++i)
2876 bufs[i] = iov[i].iov_base;
2877 result = snd_pcm_lib_readv(substream, bufs, frames);
2879 result = frames_to_bytes(runtime, result);
2884 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2885 unsigned long nr_segs, loff_t pos)
2887 struct snd_pcm_file *pcm_file;
2888 struct snd_pcm_substream *substream;
2889 struct snd_pcm_runtime *runtime;
2890 snd_pcm_sframes_t result;
2893 snd_pcm_uframes_t frames;
2895 pcm_file = iocb->ki_filp->private_data;
2896 substream = pcm_file->substream;
2897 snd_assert(substream != NULL, result = -ENXIO; goto end);
2898 runtime = substream->runtime;
2899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2903 if (nr_segs > 128 || nr_segs != runtime->channels ||
2904 !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);
2922 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2924 struct snd_pcm_file *pcm_file;
2925 struct snd_pcm_substream *substream;
2926 struct snd_pcm_runtime *runtime;
2928 snd_pcm_uframes_t avail;
2930 pcm_file = file->private_data;
2932 substream = pcm_file->substream;
2933 snd_assert(substream != NULL, return -ENXIO);
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 snd_assert(substream != NULL, return -ENXIO);
2972 runtime = substream->runtime;
2974 poll_wait(file, &runtime->sleep, wait);
2976 snd_pcm_stream_lock_irq(substream);
2977 avail = snd_pcm_capture_avail(runtime);
2978 switch (runtime->status->state) {
2979 case SNDRV_PCM_STATE_RUNNING:
2980 case SNDRV_PCM_STATE_PREPARED:
2981 case SNDRV_PCM_STATE_PAUSED:
2982 if (avail >= runtime->control->avail_min) {
2983 mask = POLLIN | POLLRDNORM;
2988 case SNDRV_PCM_STATE_DRAINING:
2990 mask = POLLIN | POLLRDNORM;
2995 mask = POLLIN | POLLRDNORM | POLLERR;
2998 snd_pcm_stream_unlock_irq(substream);
3007 * Only on coherent architectures, we can mmap the status and the control records
3008 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3010 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3012 * mmap status record
3014 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
3015 unsigned long address, int *type)
3017 struct snd_pcm_substream *substream = area->vm_private_data;
3018 struct snd_pcm_runtime *runtime;
3021 if (substream == NULL)
3022 return NOPAGE_SIGBUS;
3023 runtime = substream->runtime;
3024 page = virt_to_page(runtime->status);
3027 *type = VM_FAULT_MINOR;
3031 static struct vm_operations_struct snd_pcm_vm_ops_status =
3033 .nopage = snd_pcm_mmap_status_nopage,
3036 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3037 struct vm_area_struct *area)
3039 struct snd_pcm_runtime *runtime;
3041 if (!(area->vm_flags & VM_READ))
3043 runtime = substream->runtime;
3044 snd_assert(runtime != NULL, return -EAGAIN);
3045 size = area->vm_end - area->vm_start;
3046 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3048 area->vm_ops = &snd_pcm_vm_ops_status;
3049 area->vm_private_data = substream;
3050 area->vm_flags |= VM_RESERVED;
3055 * mmap control record
3057 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3058 unsigned long address, int *type)
3060 struct snd_pcm_substream *substream = area->vm_private_data;
3061 struct snd_pcm_runtime *runtime;
3064 if (substream == NULL)
3065 return NOPAGE_SIGBUS;
3066 runtime = substream->runtime;
3067 page = virt_to_page(runtime->control);
3070 *type = VM_FAULT_MINOR;
3074 static struct vm_operations_struct snd_pcm_vm_ops_control =
3076 .nopage = snd_pcm_mmap_control_nopage,
3079 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3080 struct vm_area_struct *area)
3082 struct snd_pcm_runtime *runtime;
3084 if (!(area->vm_flags & VM_READ))
3086 runtime = substream->runtime;
3087 snd_assert(runtime != NULL, return -EAGAIN);
3088 size = area->vm_end - area->vm_start;
3089 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3091 area->vm_ops = &snd_pcm_vm_ops_control;
3092 area->vm_private_data = substream;
3093 area->vm_flags |= VM_RESERVED;
3096 #else /* ! coherent mmap */
3098 * don't support mmap for status and control records.
3100 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3101 struct vm_area_struct *area)
3105 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3106 struct vm_area_struct *area)
3110 #endif /* coherent mmap */
3113 * nopage callback for mmapping a RAM page
3115 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3116 unsigned long address, int *type)
3118 struct snd_pcm_substream *substream = area->vm_private_data;
3119 struct snd_pcm_runtime *runtime;
3120 unsigned long offset;
3125 if (substream == NULL)
3126 return NOPAGE_SIGBUS;
3127 runtime = substream->runtime;
3128 offset = area->vm_pgoff << PAGE_SHIFT;
3129 offset += address - area->vm_start;
3130 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_SIGBUS);
3131 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3132 if (offset > dma_bytes - PAGE_SIZE)
3133 return NOPAGE_SIGBUS;
3134 if (substream->ops->page) {
3135 page = substream->ops->page(substream, offset);
3137 return NOPAGE_OOM; /* XXX: is this really due to OOM? */
3139 vaddr = runtime->dma_area + offset;
3140 page = virt_to_page(vaddr);
3144 *type = VM_FAULT_MINOR;
3148 static struct vm_operations_struct snd_pcm_vm_ops_data =
3150 .open = snd_pcm_mmap_data_open,
3151 .close = snd_pcm_mmap_data_close,
3152 .nopage = snd_pcm_mmap_data_nopage,
3156 * mmap the DMA buffer on RAM
3158 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3159 struct vm_area_struct *area)
3161 area->vm_ops = &snd_pcm_vm_ops_data;
3162 area->vm_private_data = substream;
3163 area->vm_flags |= VM_RESERVED;
3164 atomic_inc(&substream->mmap_count);
3169 * mmap the DMA buffer on I/O memory area
3171 #if SNDRV_PCM_INFO_MMAP_IOMEM
3172 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3174 .open = snd_pcm_mmap_data_open,
3175 .close = snd_pcm_mmap_data_close,
3178 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3179 struct vm_area_struct *area)
3182 unsigned long offset;
3184 #ifdef pgprot_noncached
3185 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3187 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3188 area->vm_private_data = substream;
3189 area->vm_flags |= VM_IO;
3190 size = area->vm_end - area->vm_start;
3191 offset = area->vm_pgoff << PAGE_SHIFT;
3192 if (io_remap_pfn_range(area, area->vm_start,
3193 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3194 size, area->vm_page_prot))
3196 atomic_inc(&substream->mmap_count);
3200 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3201 #endif /* SNDRV_PCM_INFO_MMAP */
3206 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3207 struct vm_area_struct *area)
3209 struct snd_pcm_runtime *runtime;
3211 unsigned long offset;
3214 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3215 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3218 if (!(area->vm_flags & VM_READ))
3221 runtime = substream->runtime;
3222 snd_assert(runtime != NULL, return -EAGAIN);
3223 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3225 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3227 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3228 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3230 size = area->vm_end - area->vm_start;
3231 offset = area->vm_pgoff << PAGE_SHIFT;
3232 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3233 if ((size_t)size > dma_bytes)
3235 if (offset > dma_bytes - size)
3238 if (substream->ops->mmap)
3239 return substream->ops->mmap(substream, area);
3241 return snd_pcm_default_mmap(substream, area);
3244 EXPORT_SYMBOL(snd_pcm_mmap_data);
3246 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3248 struct snd_pcm_file * pcm_file;
3249 struct snd_pcm_substream *substream;
3250 unsigned long offset;
3252 pcm_file = file->private_data;
3253 substream = pcm_file->substream;
3254 snd_assert(substream != NULL, return -ENXIO);
3256 offset = area->vm_pgoff << PAGE_SHIFT;
3258 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3259 if (pcm_file->no_compat_mmap)
3261 return snd_pcm_mmap_status(substream, file, area);
3262 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3263 if (pcm_file->no_compat_mmap)
3265 return snd_pcm_mmap_control(substream, file, area);
3267 return snd_pcm_mmap_data(substream, file, area);
3272 static int snd_pcm_fasync(int fd, struct file * file, int on)
3274 struct snd_pcm_file * pcm_file;
3275 struct snd_pcm_substream *substream;
3276 struct snd_pcm_runtime *runtime;
3279 pcm_file = file->private_data;
3280 substream = pcm_file->substream;
3281 snd_assert(substream != NULL, return -ENXIO);
3282 runtime = substream->runtime;
3284 err = fasync_helper(fd, file, on, &runtime->fasync);
3293 #ifdef CONFIG_COMPAT
3294 #include "pcm_compat.c"
3296 #define snd_pcm_ioctl_compat NULL
3300 * To be removed helpers to keep binary compatibility
3303 #ifdef CONFIG_SND_SUPPORT_OLD_API
3304 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3305 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3307 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3308 struct snd_pcm_hw_params_old *oparams)
3312 memset(params, 0, sizeof(*params));
3313 params->flags = oparams->flags;
3314 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3315 params->masks[i].bits[0] = oparams->masks[i];
3316 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3317 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3318 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3319 params->info = oparams->info;
3320 params->msbits = oparams->msbits;
3321 params->rate_num = oparams->rate_num;
3322 params->rate_den = oparams->rate_den;
3323 params->fifo_size = oparams->fifo_size;
3326 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3327 struct snd_pcm_hw_params *params)
3331 memset(oparams, 0, sizeof(*oparams));
3332 oparams->flags = params->flags;
3333 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3334 oparams->masks[i] = params->masks[i].bits[0];
3335 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3336 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3337 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3338 oparams->info = params->info;
3339 oparams->msbits = params->msbits;
3340 oparams->rate_num = params->rate_num;
3341 oparams->rate_den = params->rate_den;
3342 oparams->fifo_size = params->fifo_size;
3345 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3346 struct snd_pcm_hw_params_old __user * _oparams)
3348 struct snd_pcm_hw_params *params;
3349 struct snd_pcm_hw_params_old *oparams = NULL;
3352 params = kmalloc(sizeof(*params), GFP_KERNEL);
3357 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3363 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3367 snd_pcm_hw_convert_from_old_params(params, oparams);
3368 err = snd_pcm_hw_refine(substream, params);
3369 snd_pcm_hw_convert_to_old_params(oparams, params);
3370 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3380 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3381 struct snd_pcm_hw_params_old __user * _oparams)
3383 struct snd_pcm_hw_params *params;
3384 struct snd_pcm_hw_params_old *oparams = NULL;
3387 params = kmalloc(sizeof(*params), GFP_KERNEL);
3392 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3397 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3401 snd_pcm_hw_convert_from_old_params(params, oparams);
3402 err = snd_pcm_hw_params(substream, params);
3403 snd_pcm_hw_convert_to_old_params(oparams, params);
3404 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3413 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3419 const struct file_operations snd_pcm_f_ops[2] = {
3421 .owner = THIS_MODULE,
3422 .write = snd_pcm_write,
3423 .aio_write = snd_pcm_aio_write,
3424 .open = snd_pcm_playback_open,
3425 .release = snd_pcm_release,
3426 .poll = snd_pcm_playback_poll,
3427 .unlocked_ioctl = snd_pcm_playback_ioctl,
3428 .compat_ioctl = snd_pcm_ioctl_compat,
3429 .mmap = snd_pcm_mmap,
3430 .fasync = snd_pcm_fasync,
3433 .owner = THIS_MODULE,
3434 .read = snd_pcm_read,
3435 .aio_read = snd_pcm_aio_read,
3436 .open = snd_pcm_capture_open,
3437 .release = snd_pcm_release,
3438 .poll = snd_pcm_capture_poll,
3439 .unlocked_ioctl = snd_pcm_capture_ioctl,
3440 .compat_ioctl = snd_pcm_ioctl_compat,
3441 .mmap = snd_pcm_mmap,
3442 .fasync = snd_pcm_fasync,